yyhhyyyyyy on Discord made an Opus 5.5 group In order: an API key first,
a Claude subscription second, and 8 of 10 Claude Code requests went to
the subscription. With both healthy the plan is in order and every
request goes to the key. The cause is affinity: one failure of the key
(a 429, a 5xx, a rest) sent a conversation to the subscription, and Auto
affinity's "cache" kept it there at every turn after, since each turn
reads what the last wrote, while the key stood first and ready. A
provider's fallback already gave a conversation back (backToMain, "main");
a group's later member never did.
Now, in a group routed In order with Stays left on Auto, a new turn goes
back to the first ready member when that is another member than the one
that answered last (backToFirst, Affinity.Why "order"). Another key or
account of the same member, or a group in the group, keeps its own
affinity. Within a turn the conversation stays, on the reasoning the
later member sealed, as with "main". Stays: Session and Strict are the
user's own setting and keep it where it is. Smart and the other routings
keep it by its cache as before.
The Routing page says why it moved, for "order" and for a fallback's
"main", which showed no reason at all before (affWhy), in en, zh, zh-TW,
ja and de.
What was run (at the rebased head unless said):
- go test ./internal/gateway -run 'TestOrderedGroupGivesTheConversationBackToItsFirstMember|TestGroupMemberKeepsTheConversationOnceAnotherIsBack|TestFallbackGivesTheConversation|ChatGPTCold|LoginSwitch|LoneAllowance|Affinity' on mote: ok.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui,
GOOS=windows go build on mote, before the rebase: all ok.
- GUI in WSL magpie-desktop, Chromium and WebKit, 1100 and 420 wide,
every language: routing-affinity-order, routing-affinity-cache-write and
i18n-keys 44 of 44 before the rebase; routing-affinity-order and
i18n-keys again at the rebased head.
Broken by hand:
- the "order" case turned off (old behaviour): the new Go test fails,
"answered by ssssssssss, want ssskkkkkkk" and why "cache", want "order".
- the Within and Session guards removed: "the key failed once" fails with
skkkkkkkkk, and "Stays: Session" with skkkkkkkkk, want ssssssssss.
- client.js put back: routing-affinity-order 0 of 10 in Chromium
("order: goes in order not in").
Not tried: the reporter's own key and subscription (the subscription is
the test's fake Claude Code); that a failure of the key is what moved the
reporter's sessions is the likeliest cause the code shows, not confirmed
from their trace.
Full suite: go test -tags nogui -timeout 45m ./... on mote at the
pre-rebase head: 80 packages ok, two red. TestRuleTimeWindowAtTurnStart
("a 5 b 2") was this change: its group is In order, so turn 2 of "sess",
after the rule's hours, now goes back to a; the test now asserts that
turn's answerer and counts a 5 b 2. TestNoCommandBypassesProc
(internal/claudetui/child_unix.go:32) is red on main, #4491. At the
rebased head, go test ./internal/gateway -run
'Rule|Affin|Order|Group|Stay|Sticky|Cache|Turn|Fallback|Main|Cold|Login|Lone'
on mote: ok (107s).
84 lines
4.0 KiB
Go
84 lines
4.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A conversation on one of two ChatGPT accounts stays on it across a
|
|
// pause the ChatGPT backend still has it cached through (leelantao on X:
|
|
// two Plus accounts spent 90% in a day). Five minutes was every vendor's
|
|
// cold, so after a coffee break routing moved the conversation to the
|
|
// other account, which read none of it from cache and refused the
|
|
// reasoning the first had sealed. An API key's conversation is cold at
|
|
// five minutes as before, and a ChatGPT one past chatGPTCold.
|
|
func TestChatGPTConversationStaysPastFiveMinutes(t *testing.T) {
|
|
chatgpt := func(user string) candidate {
|
|
return candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: user}}, model: "gpt-6-sol", rest: "codex"}
|
|
}
|
|
keyed := func(key string) candidate {
|
|
return candidate{p: provider.Provider{ID: "oai", Key: key, Responses: "https://api.openai.com/v1"}, model: "gpt-6-sol", rest: "oai"}
|
|
}
|
|
first := []byte(`{"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the bug"}]}]}`)
|
|
again := []byte(`{"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the bug"}]},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"now the tests"}]}]}`)
|
|
for i, c := range []struct {
|
|
name string
|
|
last, next candidate
|
|
ago time.Duration
|
|
why string
|
|
}{
|
|
{"ChatGPT, 6 minutes on", chatgpt("a@example.com"), chatgpt("b@example.com"), 6 * time.Minute, "cache"},
|
|
{"ChatGPT, 12 minutes on", chatgpt("a@example.com"), chatgpt("b@example.com"), 12 * time.Minute, "cache"},
|
|
{"ChatGPT, past its window", chatgpt("a@example.com"), chatgpt("b@example.com"), chatGPTCold + time.Minute, "cold"},
|
|
{"an API key, 6 minutes on", keyed("sk-a"), keyed("sk-b"), 6 * time.Minute, "cold"},
|
|
{"an API key, 4 minutes on", keyed("sk-a"), keyed("sk-b"), 4 * time.Minute, "cache"},
|
|
} {
|
|
h := http.Header{}
|
|
h.Set("x-claude-code-session-id", "chatgpt-cold-"+string(rune('a'+i)))
|
|
// the other account goes first by routing: Smart puts the one with
|
|
// more left first, which after a turn on the last is the other
|
|
cs := []candidate{c.next, c.last}
|
|
pl := planned{order: []Weighed{{}, {}}}
|
|
_, _, _, key := affine("t", "", false, false, false, h, provider.Responses, first, first, cs, pl)
|
|
answered(key, c.last, 1, Usage{Input: 1200, CacheRead: 20000, CacheWrite: 1200})
|
|
t.Cleanup(func() { sticks.Lock(); delete(sticks.m, key); sticks.Unlock() })
|
|
sticks.Lock()
|
|
st := sticks.m[key]
|
|
st.at = st.at.Add(-c.ago)
|
|
sticks.m[key] = st
|
|
sticks.Unlock()
|
|
got, _, a, _ := affine("t", "", false, false, false, h, provider.Responses, again, again, cs, pl)
|
|
if a.Why != c.why || a.Kept != (c.why == "cache") {
|
|
t.Fatalf("%s: why %q kept %v, want %q", c.name, a.Why, a.Kept, c.why)
|
|
}
|
|
if want := c.next; c.why == "cache" {
|
|
want = c.last
|
|
if got[0].who() != want.who() {
|
|
t.Fatalf("%s: went to %s, want %s", c.name, got[0].who(), want.who())
|
|
}
|
|
} else if got[0].who() != want.who() {
|
|
t.Fatalf("%s: went to %s, want routing's %s", c.name, got[0].who(), want.who())
|
|
}
|
|
}
|
|
}
|
|
|
|
// A group's rules keep a ChatGPT conversation's effort through the same
|
|
// pause (keepsEffort): a lower pick then reads none of the cache back.
|
|
func TestChatGPTKeepsEffortPastFiveMinutes(t *testing.T) {
|
|
at := time.Date(2026, 10, 27, 9, 0, 0, 0, time.UTC)
|
|
tr := turnRule{effort: "high", at: at, answered: at, size: 40_000, cold: chatGPTCold}
|
|
if !keepsEffort(tr, "low", 41_000, at.Add(12*time.Minute)) {
|
|
t.Fatal("a ChatGPT conversation 12 minutes on dropped its effort")
|
|
}
|
|
if keepsEffort(tr, "low", 41_000, at.Add(chatGPTCold+time.Minute)) {
|
|
t.Fatal("kept its effort past the ChatGPT backend's window")
|
|
}
|
|
tr.cold = 0
|
|
if keepsEffort(tr, "low", 41_000, at.Add(6*time.Minute)) {
|
|
t.Fatal("another vendor's conversation kept its effort past five minutes")
|
|
}
|
|
}
|