Files
magpie/internal/gateway/chatgpt_cold_test.go
T
yetone 06d36d4142 gateway: Stays: Session spreads new sessions over the accounts (connor_yang)
connor_yang on Discord: three sessions on one provider's three accounts,
Stays: Session (整个会话), ended up on two accounts. Under Smart routing a
conversation nobody had answered yet went wherever routing put first,
whatever other sessions were on; only In turn counted the sessions each
account holds (leastHeld, #946).

Kept for the whole session (Stays: Session or Strict) under a routing that
picks by what is left (Smart, Least used first, Weekly pace;
spreadsSessions), a new conversation now goes to the key, account or
group member the fewest other conversations of the last 30 minutes are on.
It passes over one routing keeps new conversations off (readyForNew:
resting, an aside key, a fallback while the provider's own model leads,
one used past lowShare), counts leads only, and keeps routing's order on a
tie. A Claude Code subagent's first request goes to its lead's account
(withLead), not to another as a session of its own. An answered
conversation is kept as before, and resting, spent and the rest/quota
fallback are unchanged. In order, By weight, Free first, Speed first,
Cheapest, Manual and plugin routings keep their order. Docs: reference,
gateway-routing, site start.html (en, zh, ja).

Run (mote, at this head on 2604d68db): go test ./internal/gateway -run
'TestWholeSession|TestInOrderKeepsNew|TestSpreadsSessionsOnly|
TestRotateSpreads|Affinity|Strict|Subagent|Lone|LoginSwitch|ChatGPT' ok;
GOOS=linux and GOOS=windows go vet ./internal/gateway ok; gofmt clean.
The full Go suite (-timeout 45m) was started at this head after the push.

Broken by hand: spreadsSessions always false (the old code's behaviour):
TestWholeSessionSpreadsNewSessions red, "s2 went to k1, which s1 is on"
for session and strict. Subagent sent through leastHeld instead of
withLead: red, "s2's subagent went to k1, not s2's k2". readyForNew
without the fallback and lowShare checks: TestWholeSessionSpreadLeaves
TheNearlyUsedAndFallbacks red.

Not tried: real subscription accounts (the tests use three API keys on one
provider, the same candidate path); a group of different providers end to
end (leastHeld's group path is the one In turn has used); the reporter's
exact provider and agent; sessions idle more than 30 minutes no longer
count as holding their account.
2026-10-31 01:05:44 +08:00

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, 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, 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")
}
}