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 on7428f3229): 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 onto0fe9f97c1, 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.
182 lines
6.4 KiB
Go
182 lines
6.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/routes"
|
|
)
|
|
|
|
// connor_yang on Discord: three sessions on a provider with three accounts,
|
|
// Stays: Session, Smart routing, ended up on two accounts. A conversation
|
|
// nobody has answered yet went where routing put it first, whatever other
|
|
// sessions were on, so two sessions shared one account while another sat
|
|
// unused. Kept for the whole session, a new session now goes to the one the
|
|
// fewest others are on (spreadsSessions, leastHeld), and each then stays
|
|
// where it began; its subagents go with it (withLead).
|
|
func TestWholeSessionSpreadsNewSessions(t *testing.T) {
|
|
for _, mode := range []string{provider.AffinitySession, provider.AffinityStrict} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
fresh(t)
|
|
up := &keyed{}
|
|
serveOn(t, "sp", "k1", []string{"m1"}, up, "k2", "k3")
|
|
if err := provider.SetAffinity("sp", mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
routes.Forget("sp")
|
|
s := New()
|
|
post := func(session, sub, msgs string) string {
|
|
t.Helper()
|
|
n := len(up.tried)
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"sp/m1","messages":[`+msgs+`]}`))
|
|
req.Header.Set("x-session-id", session)
|
|
if sub != "" {
|
|
req.Header.Set("x-claude-code-agent-id", sub)
|
|
}
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 || len(up.tried) != n+1 {
|
|
t.Fatalf("%s: %d %s, tried %v", session, rec.Code, rec.Body.String(), up.tried[n:])
|
|
}
|
|
return up.tried[n]
|
|
}
|
|
on := map[string]string{}
|
|
seen := map[string]string{}
|
|
for _, ses := range []string{"s1", "s2", "s3"} {
|
|
k := post(ses, "", `{"role":"user","content":"start `+ses+`"}`)
|
|
if other, ok := seen[k]; ok {
|
|
t.Errorf("%s went to %s, which %s is on; a third account had none (%v)", ses, k, other, on)
|
|
}
|
|
seen[k], on[ses] = ses, k
|
|
}
|
|
// each stays where it began, turn after turn, as before
|
|
for _, ses := range []string{"s1", "s2", "s3"} {
|
|
msgs := `{"role":"user","content":"start ` + ses + `"},{"role":"assistant","content":"ok"},{"role":"user","content":"more"}`
|
|
if k := post(ses, "", msgs); k != on[ses] {
|
|
t.Errorf("%s's next turn went to %s, not %s where it began", ses, k, on[ses])
|
|
}
|
|
}
|
|
// a subagent of s2 goes with s2, not to the account fewest are on
|
|
if k := post("s2", "sub-1", `{"role":"user","content":"a subagent's task"}`); k != on["s2"] {
|
|
t.Errorf("s2's subagent went to %s, not s2's %s", k, on["s2"])
|
|
}
|
|
// a fourth session goes to one of the three, each holding one
|
|
post("s4", "", `{"role":"user","content":"start s4"}`)
|
|
})
|
|
}
|
|
}
|
|
|
|
// In order is the user's own order: every new session goes to the first
|
|
// account until it is used up or fails, Stays: Session or not.
|
|
func TestInOrderKeepsNewSessionsOnTheFirst(t *testing.T) {
|
|
fresh(t)
|
|
up := &keyed{}
|
|
serveOn(t, "so", "k1", []string{"m1"}, up, "k2", "k3")
|
|
if err := provider.SetAffinity("so", provider.AffinitySession); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SetRouting("so", provider.Ordered); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
routes.Forget("so")
|
|
s := New()
|
|
for _, ses := range []string{"s1", "s2", "s3"} {
|
|
if code, out := postAs(t, s, ses, `{"model":"so/m1","messages":[{"role":"user","content":"start `+ses+`"}]}`); code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
}
|
|
for _, k := range up.tried {
|
|
if k != "k1" {
|
|
t.Fatalf("In order sent a new session to %s: %v", k, up.tried)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSpreadsSessionsOnlyWhenKeptAndRoutedByWhatIsLeft(t *testing.T) {
|
|
for _, c := range []struct {
|
|
mode, routing string
|
|
want bool
|
|
}{
|
|
{provider.AffinitySession, "", true},
|
|
{provider.AffinityStrict, "", true},
|
|
{provider.AffinitySession, provider.LeastUsed, true},
|
|
{provider.AffinitySession, provider.Pace, true},
|
|
{"", "", false},
|
|
{provider.AffinityTurn, "", false},
|
|
{provider.AffinityOff, "", false},
|
|
{provider.AffinitySession, provider.Ordered, false},
|
|
{provider.AffinitySession, provider.Weighted, false},
|
|
{provider.AffinitySession, provider.FreeFirst, false},
|
|
{provider.AffinitySession, provider.Cheapest, false},
|
|
{provider.AffinitySession, provider.Rotate, false}, // spreads on its own
|
|
} {
|
|
if got := spreadsSessions(c.mode, c.routing); got != c.want {
|
|
t.Errorf("spreadsSessions(%q, %q) = %v, want %v", c.mode, c.routing, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Spreading a whole session's new conversations never puts one on an
|
|
// account Smart keeps new conversations off — used past lowShare — nor on a
|
|
// provider's fallback while the provider's own model leads: those stay
|
|
// behind, and with every other account holding as many sessions, routing's
|
|
// first takes it.
|
|
func TestWholeSessionSpreadLeavesTheNearlyUsedAndFallbacks(t *testing.T) {
|
|
fresh(t)
|
|
key := func(k string) candidate {
|
|
p := provider.Provider{ID: "sx", Key: k}
|
|
return candidate{p: p, model: "m1", rest: "sx#" + k}
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
last Weighed
|
|
}{
|
|
{"used past lowShare", Weighed{Known: true, Used: lowShare + 5}},
|
|
{"a fallback", Weighed{Fallback: true}},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
scope := "sx/m1:" + c.name
|
|
cs := []candidate{key("k1"), key("k2"), key("k3")}
|
|
sticks.Lock()
|
|
for i, ses := range []string{"a", "b"} {
|
|
sticks.m[scope+"|"+ses] = stick{rest: cs[i].rest, who: cs[i].who(), model: "m1", at: time.Now()}
|
|
}
|
|
sticks.Unlock()
|
|
t.Cleanup(func() {
|
|
sticks.Lock()
|
|
for k := range sticks.m {
|
|
if strings.HasPrefix(k, scope+"|") {
|
|
delete(sticks.m, k)
|
|
}
|
|
}
|
|
for k := range sticks.opening {
|
|
if strings.HasPrefix(k, scope+"|") {
|
|
delete(sticks.opening, k)
|
|
}
|
|
}
|
|
sticks.Unlock()
|
|
})
|
|
pl := planned{order: []Weighed{{}, {}, c.last}}
|
|
got, _ := leastHeld(scope, scope+"|c", cs, pl, true)
|
|
if got[0].who() != cs[0].who() {
|
|
t.Errorf("a new session went to %s, %s", got[0].who(), c.name)
|
|
}
|
|
// in turn spreads to one used past lowShare as it did, but
|
|
// never to a fallback while the provider's own model leads
|
|
// (standsIn; 01huadalang on Discord, 10-31): then to the one
|
|
// of the provider's the fewest are on, k2 (k1 holds a and c)
|
|
want := cs[2]
|
|
if c.last.Fallback {
|
|
want = cs[1]
|
|
}
|
|
got, _ = leastHeld(scope, scope+"|d", cs, planned{order: []Weighed{{}, {}, c.last}}, false)
|
|
if got[0].who() != want.who() {
|
|
t.Errorf("in turn, a new conversation went to %s, want %s", got[0].who(), want.who())
|
|
}
|
|
})
|
|
}
|
|
}
|