Skip to content

Feat: Keep a pinned session on its server across a rename - #1916

Closed
Alan-Cha wants to merge 1 commit into
mainfrom
feat/router-pin-follows-host
Closed

Alan-Cha wants to merge 1 commit into
mainfrom
feat/router-pin-follows-host

Conversation

@Alan-Cha

Copy link
Copy Markdown
Member

Summary

The inference-router pins each session to its server by name: the pin record, and the session's substitutes and moved-reasoning record, all name the server. Renaming a server, by hand today or with the agentop server rename that #1907 asks for, left every running session pinned to a name no server had. Each one got a 503 pinned_server_removed on its next request until it was moved.

A pin now keeps its server's host as well as its name, and a renamed server keeps its sessions. This is the second of three PRs for #1907. The third adds agentop server rename, which depends on this one.

How a pin finds its server

decidePin (decide.go) is a pure function of the loaded pin and the current config:

  1. Not routed stays as it is.
  2. Host first. A host names one server at a time (Decode refuses two on one), so the server on the pin's host is the one the session ran on, whatever it is called now. Host comes before name because an old name can be given to another server, or two names swapped. A name-first rule would quietly send the session to the wrong backend with the wrong key.
  3. Then the name, when no server has the pin's host. This is what happens when agentop server add --yes replaces a server's URL under the same name, whose sessions the add says follow it. The pin takes the new host.
  4. Neither: the server was removed, and OnRequest's 503 stands.

serverFor runs this after loading the pin and before decideServer rules on it. Running it first means a sessions: entry naming the new name keeps the pin (pin: existing) rather than moving it from the old name. It runs only for the pin's own agent, as decideServer already requires. A changed pin is saved at once, past the stored pin's once-a-day renewal rule.

What a followed pin carries

  • The session's substitutes (substitute/<session>/<agent>/<server>), copied to the new name when it has none, so the conversation keeps its model. saveSubstitutes is pulled out of keepSubstitute for this.
  • The session's moved-reasoning record, with the old name rewritten to the new one in Base, Writers and Last (renameInReasoning). Without this, every reasoning block the server wrote would read as another server's and be dropped from every later request.
  • A renamed_from detail on that request's modify/routed record. The pin values are unchanged.

What the router learned about the server itself (its model list, refusals and limits) is learned again under the new name. The doc says so, including the brief skip/no_substitute window before the new name's model list is first read.

Why not re-key the state on reload

A hot reload builds a fresh router (plugins.BuildWithDeps) that never sees the previous config, and neither store can list its keys. Resolving lazily per request needs neither. It also covers a rename made while the proxy was stopped, since the plugin store keeps the host.

Compatibility

  • pinRecord gains host,omitempty. An older proxy reading a newer record ignores the field.
  • Pins saved before this change have no host. They get one on their session's next request. Only a session idle since the upgrade and renamed before its next request still gets the 503 (TestRouter_APinWithNoHostGetsItsServersOnItsNextRequest).
  • Removing a server and adding one on its host, under any name, now carries its pinned sessions over instead of 503ing. This is documented in docs/inference-router-plugin.md.

Not in this PR

Test plan

  • New (follow_test.go):
    • TestDecidePin (9 rows: rename, old name reused, names swapped, URL replaced, no host, removed)
    • TestRouter_APinFollowsItsServerToANewName
    • TestRouter_APinFollowsItsHostWhenItsOldNameIsReused
    • TestRouter_APinFollowsANameWhoseURLChanged
    • TestRouter_APinWithNoHostGetsItsServersOnItsNextRequest
    • TestRouter_AnotherAgentsRequestDoesNotFollowThePin
    • TestRouter_AnOverrideToTheNewNameKeepsThePin
    • TestPinsFollowARenameAcrossAReload (through plugins.BuildWithDeps)
    • TestRouter_AFollowedPinKeepsItsSubstitute
    • TestRouter_AFollowedSessionKeepsItsOwnReasoning
    • TestRouter_WithAStoreAPinFollowsARenameMadeWhileStopped (file store, reopened)
    • TestRouter_AStoredPinWithNoHostGetsOneOnItsNextRequest
    • TestRouter_ARoutedOneShotsRecordCarriesNoTurn
  • Updated: the two whole-struct pin comparisons in plugin_test.go now include the host.
  • Mutation-checked, each failing the tests it should:
    • The follow disabled: 11 tests fail.
    • followRename skipped: the substitute and reasoning tests fail.
    • keep instead of save: both store tests fail.
    • Host-first removed: the host rows of TestDecidePin and every rename test fail.
    • turn added to routed records: the one-shot test fails.
  • Live, against a scratch cortex (full profile) on spare ports, with a Claude-Code-shaped session pinned to server a, then a renamed b by hand in the config:
    • On the released v0.9.5 binary the session's next request is 503 pinned_server_removed.
    • On this build it is 200, recorded as routed {server: b, pin: existing, renamed_from: a}. The next request is a plain pin: existing.
  • go vet ./... (core); go test -race ./plugins/inferencerouter/...; go test ./plugins/...; gofmt; cmd/cortex builds with the full profile

Refs #1907

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com

The inference-router pins each session to its server by name. Renaming a
server, by hand or with the agentop server rename that #1907 asks for,
left every running session pinned to a name no server had, and each one
got a 503 pinned_server_removed until it was moved.

A pin now keeps its server's host as well as its name. A host names one
server at a time, so decidePin resolves a pin by its host first: to the
server on it now, whatever that server is called. A pin whose host no
server has follows its name, as a server whose URL was replaced under
the same name needs. Host comes before name because an old name can be
given to another server, or two names swapped.

- serverFor resolves the pin before decideServer rules on it, and only
  for the pin's own agent. The resolved pin is saved at once, past the
  once-a-day renewal rule.
- followRename carries the session's substitutes and its moved-reasoning
  record to the new name. The server-wide caches (model lists, refusals,
  limits) are learned again.
- A followed request's modify/routed record carries renamed_from.
- A pin saved before this change gets its server's host on its next
  request. One renamed before that still gets the 503, as today.

This works with the proxy running (hot reload) or stopped (the plugin
store keeps the host), and needs no reload hook: the store cannot list
its keys, and a reload builds a fresh router that never sees the
previous config.

Refs #1907

Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com>
Signed-off-by: Alan Cha <Alan.cha1@ibm.com>
@Alan-Cha
Alan-Cha requested a review from a team as a code owner November 17, 2026 03:41
@coderabbitai

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 16 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5cba698d-b4ed-4528-b288-fb3a06e98caf
Commits

Reviewing files that changed from the base of the PR and between 5392467 and 8d88965.

Files selected for processing: 8

  • core/plugins/inferencerouter/decide.go
  • core/plugins/inferencerouter/follow_test.go
  • core/plugins/inferencerouter/models.go
  • core/plugins/inferencerouter/pins.go
  • core/plugins/inferencerouter/plugin.go
  • core/plugins/inferencerouter/plugin_test.go
  • core/plugins/inferencerouter/strip.go
  • docs/inference-router-plugin.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions github-actions Bot added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 17, 2026

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Clean, well-tested change: pins now keep their server's host and follow a rename (host first, then name), with substitutes and the reasoning record carried over, and thorough coverage in follow_test.go (rename, name reuse, URL replacement, reload, stopped-proxy store, cross-agent pin ownership). No must-fix findings.

Author: Alan-Cha (MEMBER — maintainer)
Areas reviewed: Go (inferencerouter), docs, tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, signed-off: yes
CI status: passing (three jobs still pending: agentop, core peerproc macOS, README demo — all green on the equivalent PR)

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Marker for the review-state tracker; the full review is above.)

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

@Alan-Cha Alan-Cha mentioned this pull request Nov 17, 2026
6 of 7 tasks

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Host-first pin resolution is a sound design, and decidePin is clean and well covered. serverFor also gets the ordering against sessions: overrides right. I ran go vet and go test -race ./plugins/inferencerouter/... at head and both pass. I found one blocking problem, in the "two names swapped" case that the PR lists as handled. The pin follows correctly in that case, but a session that has already moved carries a reasoning record, and renameInReasoning merges the two servers' entries in it. The session then sends the other server's thinking signatures to its own server. Details and a reproducer are inline.

Author: Alan-Cha (MEMBER, maintainer)
Areas reviewed: Go (inferencerouter: decide, pins, plugin, strip, models), tests, docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (3 Go CI jobs still pending at review time)

Severity Count
must-fix 1
suggestion 1

return
}
changed := false
if rec.Base == oldName {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must-fix: a rename onto a name the record already holds merges two servers' reasoning.

This rewrite is one-way, oldName -> newName. Entries that already say newName are left alone. When newName belonged to a different server before the change, the swap case in this PR's own test table, those entries now read as the destination's own, and keepOwnReasoning stops dropping them.

Here is a reproducer against head (drop it in core/plugins/inferencerouter):

func TestProbe_SwapWithMovedSession(t *testing.T) {
	store := newStore(t)
	onEte := sessionsConfig(`"claude-code": "ete"`, ``)
	send(t, store, onEte, history())
	send(t, store, onEte, history(thinkingTurn("e1")))           // ete writes e1
	onGLM := sessionsConfig(`"claude-code": "ete"`, `"s1": "glm"`)
	send(t, store, onGLM, history(thinkingTurn("e1")))           // moved to glm
	send(t, store, onGLM, history(thinkingTurn("e1"), thinkingTurn("g1")))
	swapped := `{"servers": {
		"glm": {"url": "https://ete.example.com", "key": "ete-key", "main": "opus", "helper": "haiku"},
		"ete": {"url": "https://glm.example.com:8443", "key": "glm-key", "main": "opus", "helper": "haiku"}},
		"agents": {"claude-code": "glm"}, "sessions": {"s1": "ete"}}`
	next := send(t, store, swapped, history(thinkingTurn("e1"), thinkingTurn("g1"), thinkingTurn("g2")))
	assertRouted(t, next, glmHost, "glm-key")   // passes: the pin followed the host
	assertSignatures(t, next, "g1", "g2")       // fails: got [e1 g1 g2]
}

Before the swap the same request correctly yields [g1 g2]. After it, Writers says e1 -> ete, g1 -> ete and Base says ete, so the real ete server's e1 signature goes to the glm host. That is exactly the cross-server artifact keepOwnReasoning exists to strip (the Bedrock 4.6 / invalid-signature failure). Any rename onto a name the record already uses for another server hits the same thing, for example removing ete and then renaming glm to ete.

A minimal fix is to treat the rename as a permutation: in the same pass, map entries that say newName to a tombstone no server can be named, then oldName -> newName. I checked that a tombstone version of this function makes the probe pass and keeps the rest of the package green. If you go that way, apply it only when the record still names oldName. Otherwise two concurrent requests in the session could both follow the rename, and the second would tombstone the entries the first just wrote. Recording the writer's host alongside the name would avoid the name ambiguity altogether.

followRename's substitute copy (len(p.substitutes(pctx, to)) == 0) has the same shape. On a swap, the session may already hold substitutes under newName from when that name was the other server. Those are kept, and the followed server's own substitutes are not copied over.

// oldName wrote is still read as its destination's. A session that already has
// substitutes under newName keeps them. The store deletes nothing, so the copies
// under oldName lapse with their TTL.
func (p *Router) followRename(pctx *pipeline.Context, oldName, newName string) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: renaming a server the session has left still loses that server's reasoning.

followRename runs only when the session's own pin follows. Take a session that ran on glm (writes g1), was moved to ete by a sessions: entry, and then glm is renamed zai. Its record still says g1 -> glm and Base: glm. If the session is later moved back with "s1": "zai", g1 reads as another server's and is dropped. I probed this at head: the request to zai carries no signatures, where [g1 ...] would be expected. Base has the same behaviour, and this PR doesn't claim to cover it, so it isn't blocking. Still, "a session moved back keeps its first server's" is something the reasoning record promises. The fix could be to resolve the record's names through hosts as well, or else to note the limitation in the doc's rename bullet.

@Alan-Cha

Copy link
Copy Markdown
Member Author

Rebased onto main, the host-first rule breaks: two servers can now share one host.

Since #1983, routerconfig.Decode accepts two servers with one URL, for example two teams' keys for one gateway:

servers:
  team-a: {url: https://gw.example.com/litellm, key: ${TEAM_A_KEY}, main: opus, helper: haiku}
  team-b: {url: https://gw.example.com/litellm, key: ${TEAM_B_KEY}, main: opus, helper: haiku}

decidePin's step 2 relies on "a host names one server at a time", which is no longer true. On main, byHost is map[string][]string, and serverAt returns ("", true) for a host that has more than one server. With that serverAt, step 2 sets had.server = "" and reports a change, so serverFor saves a not-routed pin. Every session pinned to team-a or team-b would then leave its server on its next request, in the middle of its conversation, with no rename involved.

A possible fix: follow the host only when exactly one server has it (len(names) == 1, the rule main's serverAt already uses), and otherwise fall through to step 3, the name. A rename on a shared host would then still give its sessions the 503. That limit needs a line in the docs, or something other than the host to tell the servers apart.

This also affects #1918, whose remove and rename wording describes the host rule.

@Alan-Cha

Copy link
Copy Markdown
Member Author

Closing this for now. #1918 (agentop server rename) no longer depends on it: it warns that sessions pinned to the old name get the removed server's 503, as remove does.

Two things would need to change before this approach could land:

  1. Since feat: Identify an inference server by name, so two servers can share one URL #1983, two servers can share one URL, so a host no longer names one server (comment).
  2. clawgenti's blocking finding: when two names are swapped, renameInReasoning merges the two servers' reasoning entries.

The branch stays, so this can be reopened or picked up later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants