Files
magpie/internal/gateway/roomy_test.go
T
yetoneandMYCF711 6afbe3f304 routing: a member too small for the request keeps its row and says why (#3749)
roomy leaves out a member whose model's context window the request is
longer than, and the trace dropped it from the order with no word of
it: on the Routing page its row went away, and the reason was only an
absence. Now the trace lists it in the route's left with Weighed.window
set to the window it lacked. Its row stays, faded as a seat held out
is, and says "request longer than its 200K window · passed over"; the
why list says it is left out because the request is longer than its
context window, and is asked only once every one with room is out of
its allowance (en, zh, zh-TW, ja, de). When the last resort does ask
it, its row moves from left to the order with window cleared, as
askUnlisted and unhold move theirs.

MYCF711's patch (pr/small-seats) added a Route.Small list and a row
map fix. This takes their finding but not the shape: left already
carries seats not asked (unlisted, capped, out of credits), so a new
flag on Weighed reaches the page, the session copies and the last
resort without a second list, and the row map bug they fixed doesn't
exist on main. Limit: roomy fills smallW only when the order has one
row per candidate (pl.order parallel to the candidates); a member left
out otherwise still shows only by its absence, as before.

Run: go test ./internal/gateway -run
'Roomy|TooSmall|Small|Window|Trace|Left|Unhold|AutoReset' ok;
go test -timeout 45m ./internal/gateway/...
./internal/provider/... at fcf2a5271 plus this change: gateway ok
(426s); provider crashed in TestUnreadLoginsWriteErrorsKeepCredentials
(synctest close outside bubble, #3632) and with it skipped is ok. gofmt -l clean; GOOS=linux go vet -tags nogui and
GOOS=windows go vet of ./internal/gateway clean. go test
./internal/gui -run 'CoreScriptBudget|I18n|Strings|Plugin' ok.
tests/routing-small-seat.test.cjs (new), chromium and webkit,
en/zh/zh-TW/ja/de at 1100px and 420px: 11/11 each. The routing, panel-routing and
i18n GUI files, both engines, concurrency 3, load 40-280 from other
sessions: 1468 tests, 1410 pass, 58 fail. The reds are in
routing-pause (#3410), routing-rule-effort (#3411), intent-entry
(#3408), keys-fold (#1612, #2658), live-patch (#3637), rule-remove
(#3094, #3096) and the unfiled ones in #3786, plus seven more files
(agent-hue-not-red, columns, order-unknown, rule-remove, levels,
ctx-fold, many-keys). Those seven alone, one at a time: 68/81 here,
69/81 on origin/main d56c14e95, with only rule-remove red on both
(13 and 12). None of the red fixtures builds a left: list.
Again at the pushed head, on f92db4cdd: gofmt, linux/windows vet,
the gateway -run set above and the gui Go tests ok; routing-small-seat,
routing-free-seat, routing-held-member and i18n-keys 68/68.
Broken by hand: TestMemberTooSmallIsNotAsked red with smallW not
added to Left, and TestMemberTooSmallIsTheLastResort red with Left not
pruned after the last resort; routing-small-seat 0/11 on the client
without this change.
Not tried: a real request past a real 200K member's window in the
GUI; the trace was built by the gateway tests and the page by fixture.

Co-authored-by: MYCF711 <MYCF711@users.noreply.github.com>
2026-10-27 03:02:50 +08:00

188 lines
8.8 KiB
Go

package gateway
import (
"net/http/httptest"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// roomyMember saves a provider id serving model m on s, its window
// window tokens (0: not known).
func roomyMember(t *testing.T, id string, window int, s *scripted) {
t.Helper()
up := httptest.NewServer(s)
t.Cleanup(up.Close)
p := provider.Provider{ID: id, Name: strings.ToUpper(id), Key: "k", Chat: up.URL + "/v1", Models: []string{"m"}}
if window > 0 {
p.Contexts = map[string]int{"m": window}
}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
// A group's member whose model is known to take less than the request
// isn't asked it (dgqyushen on Discord: OpenCode Zen's free DeepSeek V4.1
// Flash, 200K, first, and a 1M subscription of it second). Asked, the
// free one only refuses it, and a refusal that doesn't say why, for a
// request not far enough past the window to be taken as too long for it
// (tooLongUnsaid's margin), reached the agent as the vendor's error while
// the next member had room. A request it holds still goes to it first.
func TestMemberTooSmallIsNotAsked(t *testing.T) {
fresh(t)
// the free member refuses a request past its window the way some
// vendors do, without saying it is too long
unsaid := `{"error":{"message":"Invalid request parameters"}}`
free := &scripted{replies: []reply{{400, "", unsaid}}}
paid := &scripted{replies: []reply{{200, "", chatOK}}}
roomyMember(t, "free", 2000, free)
roomyMember(t, "paid", 10000, paid)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"free/m", "paid/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
past := strings.Repeat("word ", 1760) // about 2200 tokens as estimated
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"`+past+`"}]}`)
r := lastRoute(s)
if code != 200 || !strings.Contains(body, "hello") || free.n != 0 || paid.n != 1 {
t.Fatalf("past the free member's window: %d %s (free %d, paid %d)", code, body, free.n, paid.n)
}
if len(r.Tries) != 1 || r.Tries[0].ID != "paid" {
t.Fatalf("tries %+v", r.Tries)
}
if slices.ContainsFunc(r.Order, func(w Weighed) bool { return w.Provider == "free" }) {
t.Fatalf("the free member is still in the order: %+v", r.Order)
}
// but the trace keeps it as left out, with the window it lacked, so
// the Routing page keeps its row and says why (#3749)
if i := slices.IndexFunc(r.Left, func(w Weighed) bool { return w.Provider == "free" }); i < 0 || r.Left[i].Window != 2000 {
t.Fatalf("the free member isn't left out with its window: %+v", r.Left)
}
// a request it holds goes to it, first
free.replies = []reply{{200, "", chatOK}}
free.n, paid.n = 0, 0
code, body = postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || free.n != 1 || paid.n != 0 {
t.Fatalf("within the window: %d %s (free %d, paid %d)", code, body, free.n, paid.n)
}
}
// A member left out as too small is the last resort: asked after all
// once every member with room is out of its allowance, for the agent to
// be told to compact (Kai on Discord: both Claude accounts spent until
// the next day, the Claude provider's Fallback a Codex model of 272K),
// but not for a failure of another kind, whose error the agent is told
// as before.
func TestMemberTooSmallIsTheLastResort(t *testing.T) {
fresh(t)
free := &scripted{replies: []reply{{200, "", chatOK}}}
paid := &scripted{replies: []reply{{401, "", `{"error":{"message":"Invalid API key"}}`}}}
roomyMember(t, "free", 2000, free)
roomyMember(t, "paid", 10000, paid)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"paid/m", "free/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
past := strings.Repeat("word ", 1760) // about 2200 tokens as estimated
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"`+past+`"}]}`)
if code != 401 || free.n != 0 || paid.n != 1 {
t.Fatalf("paid's key refused: %d %s (free %d, paid %d)", code, body, free.n, paid.n)
}
paid.replies = []reply{{429, "", `{"error":{"code":"insufficient_quota","message":"You exceeded your current quota, please check your plan and billing details."}}`}}
paid.n = 0
code, body = postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"`+past+`"}]}`)
r := lastRoute(s)
if code != 200 || free.n != 1 || paid.n != 1 {
t.Fatalf("paid out of its allowance: %d %s (free %d, paid %d)", code, body, free.n, paid.n)
}
if len(r.Tries) != 2 || r.Tries[0].ID != "paid" || r.Tries[0].Rest == nil || r.Tries[1].ID != "free" {
t.Fatalf("tries %+v", r.Tries)
}
if !slices.ContainsFunc(r.Order, func(w Weighed) bool { return w.Provider == "free" && w.Window == 0 }) {
t.Fatalf("the free member asked isn't in the order: %+v", r.Order)
}
// asked after all, it is no longer told as left out (#3749)
if slices.ContainsFunc(r.Left, func(w Weighed) bool { return w.Provider == "free" }) {
t.Fatalf("the free member asked is still left out: %+v", r.Left)
}
}
// A conversation that began on the free member, and is kept there by
// affinity, moves once it has grown past that member's window.
func TestConversationOutgrowingItsMemberMoves(t *testing.T) {
fresh(t)
free := &scripted{replies: []reply{{200, "", chatOK}}}
paid := &scripted{replies: []reply{{200, "", chatOK}}}
roomyMember(t, "free", 2000, free)
roomyMember(t, "paid", 10000, paid)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"free/m", "paid/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
if code, body := postAs(t, s, "conv", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`); code != 200 || free.n != 1 {
t.Fatalf("first turn: %d %s (free %d)", code, body, free.n)
}
big := strings.Repeat("word ", 2400)
code, body := postAs(t, s, "conv", `{"model":"group/g","messages":[{"role":"user","content":"hi"},{"role":"assistant","content":"hello"},{"role":"user","content":"`+big+`"}]}`)
if code != 200 || free.n != 1 || paid.n != 1 {
t.Fatalf("grown: %d %s (free %d, paid %d)", code, body, free.n, paid.n)
}
}
// What the check leaves alone: a member whose window isn't known, a
// request no member holds (the first is asked, and the agent told it
// overflowed, to compact), and one whose size can't be estimated.
func TestRoomyLeavesWhatItCantJudge(t *testing.T) {
fresh(t)
over := `{"error":{"message":"This model's maximum context length is 2000 tokens","code":"context_length_exceeded"}}`
unknown := &scripted{replies: []reply{{200, "", chatOK}}}
small := &scripted{replies: []reply{{400, "", over}}}
smaller := &scripted{replies: []reply{{400, "", over}}}
roomyMember(t, "unknown", 0, unknown)
roomyMember(t, "small", 2000, small)
roomyMember(t, "smaller", 1500, smaller)
for _, g := range []provider.Group{
{Name: "Unknown", Members: []string{"unknown/m", "small/m"}, Routing: provider.Ordered},
{Name: "None", Members: []string{"small/m", "smaller/m"}, Routing: provider.Ordered},
} {
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
}
s := New()
big := strings.Repeat("word ", 2400)
code, body := postAs(t, s, "", `{"model":"group/unknown","messages":[{"role":"user","content":"`+big+`"}]}`)
if code != 200 || unknown.n != 1 || small.n != 0 {
t.Fatalf("window not known: %d %s (unknown %d, small %d)", code, body, unknown.n, small.n)
}
code, body = postAs(t, s, "", `{"model":"group/none","messages":[{"role":"user","content":"`+big+`"}]}`)
if code != 400 || !strings.Contains(body, "context_length_exceeded") || small.n != 1 || smaller.n != 0 {
t.Fatalf("no member holds it: %d %s (small %d, smaller %d)", code, body, small.n, smaller.n)
}
// a body that doesn't parse is no estimate: every candidate is left
cs := []candidate{
{p: provider.Provider{ID: "small", Contexts: map[string]int{"m": 2000}}, model: "m", rest: "small"},
{p: provider.Provider{ID: "big", Contexts: map[string]int{"m": 10000}}, model: "m", rest: "big"},
}
pl := planned{order: []Weighed{{ID: "small"}, {ID: "big"}}}
got, gotPl, out, outW := roomy(cs, pl, provider.Chat, []byte(`{"messages":[`+strings.Repeat(`"word `, 4000)))
if len(got) != 2 || len(gotPl.order) != 2 || out != nil || outW != nil {
t.Fatalf("unparsed: %d candidates, order %+v", len(got), gotPl.order)
}
// and one that does leaves the small one out, its order with it,
// both handed back for the last resort
got, gotPl, out, outW = roomy(cs, pl, provider.Chat, []byte(`{"model":"g","messages":[{"role":"user","content":"`+big+`"}]}`))
if len(got) != 1 || got[0].rest != "big" || len(gotPl.order) != 1 || gotPl.order[0].ID != "big" {
t.Fatalf("parsed: %+v, order %+v", got, gotPl.order)
}
if len(out) != 1 || out[0].rest != "small" || len(outW) != 1 || outW[0].ID != "small" {
t.Fatalf("left out: %+v, rows %+v", out, outW)
}
}