Commit Graph
4 Commits
Author SHA1 Message Date
JasonBuildAI 7c5bfd7a1d gateway: auto affinity keeps a conversation where a vendor's cache is kept without saying so (#2230, JasonBuildAI)
A vendor that keeps the prompt cached by itself and reports nothing of it —
OpenAI's automatic prefix cache, Gemini — says neither a read nor a write of
the turn that built the cache. affine judged that turn's stick by what the
reply read and wrote (0 + 0), found it under cacheWorth and moved the
conversation's second turn as "no-cache": the whole prompt went afresh to
another key or account and was paid for in full, on every turn after the
first, for anyone with more than one key or account and Affinity auto (the
default).

7f968b95b named this as not covered: "a vendor that caches without reporting
a write (OpenAI's automatic prefix cache, Gemini) still reports CacheWrite 0
on a first turn, so its second turn may still move".

What such a vendor holds of the conversation after a turn is the prompt that
turn was sent, which is what its cache now has. answered takes the reply's
usage rather than its two cache counts, and where the reply reports neither
a read nor a write, the prompt it was sent (Input) stands for the write: the
second turn stays to read it, as it does for a turn that wrote its cache. A
reply that reports no usage at all (Input 0) counts as no cache, as before,
and a prompt under cacheWorth (1024) still moves. A read or a write the
vendor does report is used as it was, and resting, spent, cold, strict, turn
and rotate are judged before this, so nothing else changes.

The observable change: with Affinity auto, a conversation whose last reply
reported neither a cache read nor a cache write, and was sent at least 1024
tokens, is kept where it was (Why "cache", Kept true) rather than moved
(Why "no-cache"). Its stick, affinity.json and the trace's
Affinity.CacheWrite carry the prompt sent, and Affinity.CacheRead stays 0,
which is what the Routing page shows as read and written; the page itself is
unchanged.

Tests:
- affinity_silent_cache_test.go (new): TestAffinityKeepsACacheTheVendorKept
  Silently. Two keys, least-used routing, and a Chat vendor reporting
  prompt_tokens 3000 with cached_tokens 0 — what OpenAI and Gemini send of a
  first turn — keeps the second turn on k1 as "cache", with the trace
  telling CacheRead 0 and CacheWrite 3000; a vendor reporting no usage at
  all still moves to k2 as "no-cache".
- TestAffinityKeepsACacheJustWritten, its "too little either way" (300/300,
  no-cache) case included, passes as it did.

Run:
- go test -tags nogui -count=1 -run TestAffinity ./internal/gateway: ok,
  80.982s (TestAffinity 39.36s, TestAffinityKeepsACacheJustWritten 26.54s,
  TestAffinityKeepsACacheTheVendorKeptSilently 9.68s).
- go test -tags nogui -count=1 -timeout 45m ./...: every package passes
  except 21 tests red on this Windows machine — agent 4
  (TestInstallWithoutNodeParses, TestWSLClaudeFollowerOnTheMainModelIsNoDrift,
  TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
  TestQoderSettingsFromAnIDEPluginIsNotTheCLI), gui 4
  (TestGatewayHeldByAContainer, TestGatewayFixQuitsOnlyAMagpie,
  TestFXStateDoesNotWait, TestUpdateThatWontStart), library 3
  (TestAgentsSkillsFolders, TestRemoveFoundSkill,
  TestHooksCodexTrustMovesWithTheGroups), gateway 2 (TestClaudeAsRootEnv,
  TestSweepBridgeProjectsTakesOnlyTheBridgesFolders) and one each in backup,
  davsync, pluginrt, provider, sessions, update and usage. A fresh worktree
  at the base e98440ae, without this change, fails the same 20 of them. The
  21st, gui's TestPurposeAssetMatchesUsage, fails only in this checkout,
  whose internal/gui/assets/purposes.js is CRLF (git says i/lf w/crlf) and so
  keeps the "\r" its TrimSuffix(";\n") doesn't cut; the file is not in this
  diff and the test passes in the base worktree.
- gofmt -l on the changed files: nothing. go vet ./..., GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./...: nothing. go build,
  GOOS=linux go build -tags nogui, GOOS=windows go build: ok.
- Broken by hand: with the substitution removed the new test fails —
  "silent cache: affinity &{... CacheRead:0 CacheWrite:0 ... Why:no-cache},
  want cache". At the base commit the case fails the same way, the second
  turn going to k2.

Not tried: the Playwright suite (no GUI asset changed; the Routing page
reads the same JSON) and a live OpenAI or Gemini key.
2026-10-15 03:34:58 +08:00
yetone 7f968b95dd gateway: auto affinity keeps a conversation on the cache its last reply wrote, not only one it read (vilicvane on Discord)
Affinity's auto mode kept a conversation on its last answerer across turns
only while that reply had read at least cacheWorth (1024) tokens from the
vendor's cache. A first turn reads nothing and writes the whole prompt
(Anthropic's cache_creation_input_tokens), so the second turn was moved as
"no-cache" and the cache just built was thrown away (vilicvane on Discord,
「会话保持 → 自动」: 「新的 cache 都建好了,再切走不可惜吗?」).

answered() is now given the usage's CacheWrite beside CacheRead (both
already parsed at that call site for every route: Anthropic, Chat's
cache_write_tokens / cache_creation_input_tokens, Cursor, Command Code,
plugins), and affine counts read + written against cacheWorth. The stick
keeps cacheWrite in memory and in affinity.json (`cacheWrite`, omitempty);
a file from before has no such field and reads as 0, so an old stick is
judged by its read alone, as before. The trace's Affinity carries
CacheWrite, and the Routing page's "cache" and "no-cache" explanations say
what was read and what was written (en, zh, zh-TW, ja, de).

Not covered: a vendor that caches without reporting a write (OpenAI's
automatic prefix cache, Gemini) still reports CacheWrite 0 on a first turn,
so its second turn may still move. Noted in gateway-routing.md.

Docs: docs/subsystems/gateway-routing.md (affinity bullet), docs/reference.md
(step 6 of how magpie picks an account).

Run:
- TestAffinityKeepsACacheJustWritten (new): an Anthropic Messages upstream
  streaming cache_creation_input_tokens 3000 / read 0; the second turn is
  kept as "cache", also across a restart (affinity.json reloaded); 600+600
  is kept, 300+300 moves as "no-cache". Broken by hand twice and watched
  red: the old `st.cacheRead < cacheWorth` (written: why no-cache), and
  stickOf not reading CacheWrite from the file (restarted: no-cache).
- routing-affinity-cache-write.test.cjs (new): chromium and webkit, 1100
  and 420 wide, en/zh/zh-TW/ja/de, 20/20 pass; red on the old routing.js
  and i18n.js. i18n-keys.test.cjs and routing-account-identity.test.cjs
  pass.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows for
  internal/gateway.
- go test ./internal/gateway/...: all pass except
  TestAgentGoneInTheRetryPause/group_replan, which failed in two full runs
  at load average 150-380 and passes alone (-count=3); its fixed 100ms
  sleep before cancelling assumes the pause has begun. Filed as #2175; it
  never reaches the affinity path this changes (a first request).
- go test -timeout 40m ./... at load ~300-377: 50 packages ok; agent and
  gateway hit the 40m package timeout (no test failed before it); gui's
  TestSetAnswersWhileDevinAndProvidersHang (#1375) and
  TestMainWindowCorners (#1936) failed, both filed load reds.
  internal/agent rerun alone: ok (1008s at load ~200).
- Not tried against a real Anthropic account.
2026-10-14 21:05:26 +08:00
TryWorld dd4801e92a fix(gateway): forget a broken-off reply's stick by the account alone (#776)
affine keeps a conversation on the account that answered it three ways:
the account and the model, the account and the model without its effort,
and the account alone — "the account alone when the model is gone", for a
group whose answerer's model has since left it. unanswered, which takes
the stick away when that reply breaks off (#733), asked for the model to
match as well. So a conversation kept on an account by the widest match —
the model it answered as has left the group — kept its stick when the
reply broke off with nothing resting it (a refusal, a request too long
for the model), and the agent's retry, which routing would have sent
elsewhere, went straight back to the account whose reply had just broken
off. The same stick a rest would have protected was left standing with
no rest at all.

unanswered forgets a stick whose account matches, whatever model it
answered as: the stick is one record of "this conversation stays with
this account", and a reply that broke off books nothing. A stick of
another account, or another conversation's, is left alone.

TestUnansweredForgetsTheAccountWhenItsModelIsGone fails on the old rule
("the conversation is still kept on the account whose model has left the
group") and passes: a stick kept by the account alone is forgotten when
the model that answered is gone from the group, and another account's
stick and another conversation's are not. TestLateStreamErrorNotKept,
TestAffinity and the rest of the gateway's affinity tests pass
unchanged.

Validation on Windows: go test ./internal/gateway ./internal/provider
under a temp HOME — the gateway package's failures are the Unix-only
ones the base commit leaves there (TestSweepBridgeProjectsTakesOnlyThe
BridgesFolders); go vet ./internal/..., the darwin, GOOS=linux and
GOOS=windows builds pass.
2026-10-04 17:41:39 +08:00
yetone 7cfb6d35e7 gateway: a reply that breaks off after it began isn't booked as an answer, and the conversation's retry goes to another member (#733, congee949 on v0.1.801: Codex on group/grok4-7 got Cursor's "Unable to reach the model provider" eleven times in a row, each retry kept on Cursor by the turn's affinity, last:cursor kept:true why:turn, while the group's Grok member was fine) — after hw.release() the call.Status < 400 branch took any try whose status was under 400 for an answer, and a stream that has sent its 200 and some text and then fails comes back from translate/relay as 200 with call.Error set: servedCandidate ended the member's rest and its failure count, answered() remembered it as the conversation's answerer, and ruleAnswered booked it for the rule, so the agent's retry went straight back to the member that had just failed. Such a try (status under 400, error set) now books none of that: unanswered() forgets the conversation's stick when it names that account and model (and rewrites affinity.json), and when the error reads as the vendor failing, not a conversation too long for the model (tooLong) or a request it refuses as it reads (refusedWords) — lateRests — and the agent hasn't gone, the member rests as restAfter says for the error at that status (a minute's cooldown, longer each time it fails again; out of credit or quota as those read), so the next request in the conversation goes to another member first; a group of one still asks it, at the back. A reply the agent stopped itself (its request context ended) is booked as before, its stick kept. The partial stream stays as it was, its error event included: nothing is spliced in or replayed. The trace's try keeps status 200 and its error, and gains a Fail (the rest's why) and the Rest; the Routing page reads a try with status under 400 and a fail as broken off (tryOk), not answered: its line says it began answering and broke off, and how long it rests, with the vendor's words below it (zh strings), the row and the member's tally count it as failed, and its rest isn't taken for lifted. Usage is kept as it was, error included, as the tokens were spent; the trace's Served/Swapped/Routed still name the model that began the reply. TestLateStreamErrorNotKept (a group of two, the first member streams text then an error with status 200, for a Chat and a Responses client; the same conversation's next request must go to the second, with no stick left and the try told as failed and resting) fails on the old code (tried broken,broken; no Fail, no Rest) and passes. go vet ./internal/..., builds (darwin nogui, GOOS=windows, GOOS=linux nogui) and go test -tags nogui -count=1 ./internal/gateway/ ./internal/provider/ under a temp HOME pass; node --check on routing.js and i18n.js, the Playwright GUI tests not run (no playwright in the worktree). Not tried with the reporter's Cursor plugin; why Cursor's backend broke off is not known. 2026-10-04 09:31:48 +08:00