Claude Code sends its session's id (x-claude-code-session-id) with every
request of the session, its subagents' too; only x-claude-code-agent-id
says a request is a subagent's. Affinity kept one stick per scope and
session, so a subagent on the lead's model wrote its own answer over the
lead's stick. The lead's next turn was then judged by the subagent's cache
("no-cache" for a small one, "cold" for a 5-minute write past five
minutes) or kept wherever the subagent had gone, and with two Claude
accounts it moved to the other one: a new Claude Code there was told the
whole conversation and Anthropic wrote it all to that account's cache
again, at the 1-hour write price (slkassinthehood on X: two Pro accounts'
5 hours spent in ten minutes, ~318k tokens a request).
stickKey keeps a subagent's stick apart, by its agent id. The lead's key
is the session's, as before: sticks on disk are found after the upgrade,
and Codex's leadFirst, which reads a lead's stick by its thread id, is
unchanged. strictKeep reads the same key.
Run, at this commit's tree (base 42ec2700b):
- TestClaudeConversationKeepsItsAccountsCache: two Claude accounts (fake
Claude Code reporting a 300k 1-hour write), least-used routing, a lead
conversation through the gateway with Claude Code's headers. Red on the
old affinity.go: after a subagent's turn the lead was answered by tok-b
in a new Claude Code, logged "a new Claude Code is told the whole
conversation: 7 messages"; also red 20 minutes on. Green with the fix:
the lead stays on tok-a, in the same Claude Code, every step.
- go vet -tags nogui ./internal/gateway/
- go test -tags nogui ./internal/gateway/ -run
'Affinity|TestClaude|Strict|Rotate|Codex.*Lead|LeadFirst|Subagent|HourCache': ok
- Full suite (go test -tags nogui ./... -timeout 45m, suite-lock, sandbox
HOME, at 5e566f0c3 on base 42ec2700b): 70 packages ok, internal/gateway
ok (675s); internal/provider FAIL: "close of synctest channel from
outside bubble" in filememo.Forget under
TestUnreadLoginsWriteErrorsKeepCredentials, red on main since 091153bf
and filed as #3632. Not touched by this change.
- After rebasing onto origin/main 5214d5114 (the two claude bridge fixes):
gofmt -l, go vet and go test -tags nogui -timeout 45m ./internal/gateway/
at cf69f883b's tree: ok (419s).
Not tried: the reporter's own accounts and client; whether their lead
moved this way is not known from their screenshot (it shows the hour's
total, not reads against writes).