Files
magpie/internal/gateway/lone_allowance_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

80 lines
3.6 KiB
Go

package gateway
import (
"net/http"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// #1016: a provider with one key on — Kimi Code (China), its plan's 5
// hours 49% used and its week 45% — is weighed against nobody, but the
// routing trace still tells what it has left, so the Routing page shows
// it as it does for several; a lone subscription account the same. A key
// whose windows aren't read stays not known.
func TestLoneOneTellsWhatItHasLeft(t *testing.T) {
now := time.Now()
five, week := now.Add(2*time.Hour), now.Add(26*time.Hour)
kimi := provider.Allowance{
{Used: 45, Resets: week, Span: 7 * 24 * time.Hour},
{Used: 49, Resets: five, Span: 5 * time.Hour},
}
keyWindows(t, map[string]provider.Allowance{"sk-kimi-a": kimi})
old := allowances
t.Cleanup(func() { allowances = old })
allowances = func(string) map[string]provider.Allowance {
return map[string]provider.Allowance{"me@example.com": kimi}
}
p := provider.Provider{ID: "kimi-code-cn", Name: "Kimi Code (China)", Key: "sk-kimi-a", Chat: "https://api.kimi.com/coding/v1"}
cs := []candidate{{p: p, model: "kimi-for-coding", rest: "kimi-code-cn"}}
got, wg := weigh(p, cs, "kimi-for-coding", provider.Chat)
if len(got) != 1 || got[0].rest != "kimi-code-cn" {
t.Fatalf("the one key: %s", restsOf(got))
}
w := weighed(got[0], p, wg, false, provider.Chat)
if w.Kind != "provider" || !w.Known || w.Used != 49 || len(w.Renews) != 2 || !w.Renews[0].Equal(week) {
t.Fatalf("the one key's trace: %+v", w)
}
codex := provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}}
ac := []candidate{{p: codex, model: "gpt-6-astra", rest: "codex"}}
got, wg = weigh(codex, ac, "gpt-6-astra", provider.Responses)
if w := weighed(got[0], codex, wg, false, provider.Responses); w.Kind != "account" || !w.Known || w.Used != 49 {
t.Fatalf("the one account's trace: %+v", w)
}
plain := provider.Provider{ID: "deepseek", Key: "sk-ds", Chat: "https://api.deepseek.com/v1"}
pc := []candidate{{p: plain, model: "deepseek-chat", rest: "deepseek"}}
got, wg = weigh(plain, pc, "deepseek-chat", provider.Chat)
if w := weighed(got[0], plain, wg, false, provider.Chat); w.Known || w.Learns {
t.Fatalf("a key whose windows aren't read: %+v", w)
}
}
// The one key, all but used up, keeps the conversation it answered: there
// is nobody it could move to, and the routing story says it was kept,
// not that it moved.
func TestLoneSpentKeyKeepsItsConversation(t *testing.T) {
now := time.Now()
keyWindows(t, map[string]provider.Allowance{"sk-kimi-a": {{Used: 99, Resets: now.Add(2 * time.Hour), Span: 5 * time.Hour}}})
p := provider.Provider{ID: "kimi-code-cn", Key: "sk-kimi-a", Chat: "https://api.kimi.com/coding/v1"}
c := candidate{p: p, model: "kimi-for-coding", rest: "kimi-code-cn"}
cs, wg := weigh(p, []candidate{c}, "kimi-for-coding", provider.Chat)
pl := planned{order: []Weighed{weighed(cs[0], p, wg, false, provider.Chat)}}
if !pl.order[0].Known || pl.order[0].Used != 99 {
t.Fatalf("the trace: %+v", pl.order[0])
}
h := http.Header{}
h.Set("x-claude-code-session-id", "lone-spent-1016")
body := []byte(`{"messages":[{"role":"user","content":"hi"}]}`)
_, _, _, key := affine("t", provider.AffinitySession, false, false, h, provider.Chat, body, body, cs, pl)
answered(key, c, 1, Usage{})
t.Cleanup(func() { sticks.Lock(); delete(sticks.m, key); sticks.Unlock() })
got, _, aff, _ := affine("t", provider.AffinitySession, false, false, h, provider.Chat, body, body, cs, pl)
if got[0].rest != "kimi-code-cn" || aff.Why == "spent" || !aff.Kept {
t.Fatalf("kept %v, why %s", aff.Kept, aff.Why)
}
}