Commit Graph
1 Commits
Author SHA1 Message Date
yetone 76e4494dbd gateway: In turn passes over a provider's fallback while the provider is ready (01huadalang)
01huadalang on Discord (#feedback, 10-31): once the main provider has
quota again, requests keep going to the fallback model, which is only
meant for emergencies.

Cause: a provider routed In turn treated its fallback rows as more of
its own keys. A new turn went to the row after the last answerer
(after), which past the provider's last key is the fallback, and a new
conversation went to the row the fewest conversations held (leastHeld),
which is the fallback nobody is on. Both happened while every key of the
provider was ready. The session spread already skipped fallbacks
(readyForNew); rotate's after and leastHeld(ready=false) did not.

Fix: standsIn(order, i) marks a fallback row when the plan's first row
is the provider's own. after, leastHeld and readyForNew all skip it, so
a fallback takes a new turn or conversation only when nothing ahead of
it can. Within one turn (tool results) a conversation still stays on
whoever answered, because of sealed reasoning.

Stays: Strict is the user's own choice and is not changed. A
conversation it keeps on the fallback stays there. The trace now names
the provider's ready first row (Affinity.Main), and the Routing page
says why: the provider is ready again, Strict never moves a
conversation, and new conversations go to the provider (en, zh, zh-TW,
ja, de).

Tests (internal/gateway/affinity_fallback_paths_test.go):
- TestRotatingProviderKeepsItsFallbackForEmergencies (1 and 2 keys)
- TestFallbackGivesBackAcrossRoutings: auto, in turn, in turn+session,
  least used+session, stays turn, and a group fallback with auto, in
  turn and session
- TestStrictKeepsTheFallbackAndSaysTheMainIsReady
On the old code, the rotate provider's new turn went to spare-key (Why
new-turn), and in turn+session's new conversation went to the fallback
(Why first). Broken by hand: with after's standsIn removed and Strict's
Main dropped, the rotate test, the in-turn cases and the Strict test go
red. session_spread_test's fallback case pinned the old In turn pick;
it now expects k2.
GUI: routing-affinity-order has a "strict" case.

Run (via mote-run unless noted):
- go test -tags nogui -count=1 -timeout 45m ./... at b08f35fcc (this
  change on 7428f3229): every package ok except two reds, both filed
  and the same on main: internal/gui TestSettingsSaveKeepsWhatItDoesNotSend
  (SearchAll, #4514) and internal/usage TestRequestChunksFromDisk
  (TempDir cleanup, #4390).
- Rebased onto 0fe9f97c1, at this head: go test ./internal/gateway -run
  'Affinity|Rotat|Fallback|Strict|Spread|Turn|Stick|Lead|Order': ok;
  go vet -tags nogui ./internal/gateway: ok
- go vet ./internal/gateway; GOOS=linux and GOOS=windows go vet -tags
  nogui ./internal/gateway ./internal/gui: ok
- gofmt -l: clean
- GUI in WSL (wsl-run), routing-affinity-order, routing-affinity-cache-
  write and i18n-keys in chromium and webkit: 43 of 44. The red is
  i18n-keys' "Unmarked", the same on main (#4524).

Not tried: a real account. This was reproduced with fake upstreams only.
2026-11-01 01:59:59 +08:00