Files
magpie/internal/gateway/busy_load_test.go
T
yetone 7503beee72 plugin, settings: a model's load goes past 100% for a vendor that queues (axelburks on Discord)
axelburks, on v0.1.2547: their vendor queues, so a model's load can be
past 100%, even 200%. magpie clamped every load to 100, and Busy models
last allowed 1-99 only.

- host.js reportLoad and plugin.noteLoads keep a load from 0 up. The
  floor is still 0, null still clears it, and a sanity cap of 10000
  (LOAD_MOST, plugin.LoadMost) keeps an absurd value from reaching the
  UI or the trace.
- settings.BusyLoad takes 1 to 9999 (BusyLoadMost-1). The segment
  choices stay Off/50/70/80/90, and a value set in settings.json (150)
  shows as its own choice, as before.
- The editor's badge reads "180%". It is a badge, not a bar, so nothing
  assumed 100 wide; the test checks it stays on one line.
- Docs: site plugins page (en, zh, ja) Values and Routing bullets, and
  provider-plugins.md and gateway-routing.md.

Run on mote via mote-run, on origin/main 8d8df0639+ rebased:
- go test ./internal/plugin ./internal/gateway ./internal/settings -run
  'ModelLoad|TestBusy' -v: all pass. TestPluginReportsModelLoad has
  over:250 kept and huge:1e9 at 10000. TestBusyModelGoesBehind has 180
  behind 120 at a threshold of 150, and 10000 refused.
- Broken by hand in a copy, with the old 100 clamps and the 99 limit:
  TestPluginReportsModelLoad red ("ModelLoad(over) = 100; want 250"),
  TestBusyModelGoesBehind red (150 refused).
- node --test model-load (with Queued 180% in every language, Chromium
  and WebKit, 420 and 900, plus Over 150% picked at 420) and i18n-keys:
  46/46 pass. go test ./internal/gui -run Budget: ok.
- gofmt -l: empty. GOOS=linux go vet -tags nogui and GOOS=windows go
  vet on plugin, settings and gateway: ok.

Not run before the push (owner: push and tag first): go test ./... and
the whole GUI suite. They are queued on mote behind another session's
suite.
2026-11-01 14:28:25 +08:00

149 lines
6.0 KiB
Go

package gateway
import (
"net/http"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
func busyLoadAt(t *testing.T, at int) {
t.Helper()
s := settings.Load()
was := s.BusyLoad
s.BusyLoad = at
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
s := settings.Load()
s.BusyLoad = was
settings.Save(s)
})
}
// A model its plugin says is busier than settings.BusyLoad goes behind
// the ready candidates, ahead of those resting after a failure
// (axelburks on Discord: an in-house vendor queues requests to its busy
// models). One with no load reported, or one older than
// plugin.LoadFresh, keeps its place; so does every one with the setting
// off, as it is by default.
func TestBusyModelGoesBehind(t *testing.T) {
plugin.ForgetLoads()
t.Cleanup(plugin.ForgetLoads)
now := time.Now()
plugin.NoteLoadsForTest("inhouse", map[string]float64{"big": 92, "mid": 60}, now)
plugin.NoteLoadsForTest("inhouse", map[string]float64{"old": 99}, now.Add(-plugin.LoadFresh-time.Minute))
p := provider.PluginForTest("inhouse-plugin", "inhouse")
c := func(model string) candidate { return candidate{p: p, model: model, rest: "inhouse-plugin/" + model} }
resting := candidate{p: provider.Provider{ID: "down"}, model: "m", rest: "down-busy-test"}
restingUntil.Lock()
restingUntil.m[resting.restKey()] = time.Now().Add(time.Minute)
restingUntil.Unlock()
t.Cleanup(func() { restingUntil.Lock(); delete(restingUntil.m, resting.restKey()); restingUntil.Unlock() })
order := func() ([]string, []float64) {
in := []candidate{c("big"), c("mid"), resting, c("old"), c("none")}
out, pl := restLast(in, planned{order: make([]Weighed, len(in))})
var got []string
var busy []float64
for i, x := range out {
got = append(got, x.rest)
busy = append(busy, pl.order[i].Busy)
}
return got, busy
}
same := func(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
off := []string{"inhouse-plugin/big", "inhouse-plugin/mid", "inhouse-plugin/old", "inhouse-plugin/none", "down-busy-test"}
if got, _ := order(); !same(got, off) {
t.Fatalf("off: %v, want %v", got, off)
}
busyLoadAt(t, 80)
want := []string{"inhouse-plugin/mid", "inhouse-plugin/old", "inhouse-plugin/none", "inhouse-plugin/big", "down-busy-test"}
got, busy := order()
if !same(got, want) {
t.Fatalf("over 80: %v, want %v", got, want)
}
if busy[3] != 92 || busy[0] != 0 || busy[1] != 0 {
t.Fatalf("the trace's Busy = %v", busy)
}
busyLoadAt(t, 50)
want = []string{"inhouse-plugin/old", "inhouse-plugin/none", "inhouse-plugin/big", "inhouse-plugin/mid", "down-busy-test"}
if got, _ := order(); !same(got, want) {
t.Fatalf("over 50: %v, want %v", got, want)
}
// a vendor that queues reports past 100 (axelburks: 200% happens),
// and a threshold past 100 goes by it
plugin.NoteLoadsForTest("inhouse", map[string]float64{"big": 180, "mid": 120}, now)
busyLoadAt(t, 150)
want = []string{"inhouse-plugin/mid", "inhouse-plugin/old", "inhouse-plugin/none", "inhouse-plugin/big", "down-busy-test"}
got, busy = order()
if !same(got, want) || busy[3] != 180 {
t.Fatalf("over 150: %v %v, want %v", got, busy, want)
}
s := settings.Load()
s.BusyLoad = settings.BusyLoadMost
if err := settings.Save(s); err == nil {
t.Fatalf("a threshold of %d was saved", settings.BusyLoadMost)
}
// a lone candidate is tried whatever its load
if out, pl := restLast([]candidate{c("big")}, planned{order: make([]Weighed, 1)}); len(out) != 1 || pl.order[0].Busy != 0 {
t.Fatalf("lone: %+v", pl.order)
}
}
// A conversation a model answered that has since gone busy goes to the
// ready one that leads at its next turn, whatever it cached; within a
// turn it stays, on the reasoning it sealed. A turn going round, or a new
// conversation spread, never lands on the busy one past a ready one.
func TestBusyModelNotKeptAtNewTurn(t *testing.T) {
p := provider.PluginForTest("inhouse-plugin", "inhouse")
cs := []candidate{{p: p, model: "mid", rest: "inhouse-plugin/mid"}, {p: p, model: "big", rest: "inhouse-plugin/big"}}
pl := planned{order: []Weighed{{Who: "mid"}, {Who: "big", Busy: 92}}}
h := http.Header{}
h.Set("x-claude-code-session-id", "busy-load-kept")
turn := []byte(`{"messages":[{"role":"user","content":"hi"}]}`)
within := []byte(`{"messages":[{"role":"user","content":"hi"},{"role":"assistant","content":null,"tool_calls":[{"id":"c1","type":"function","function":{"name":"f","arguments":"{}"}}]},{"role":"tool","tool_call_id":"c1","content":"ok"}]}`)
_, _, _, key := affine("t", provider.AffinitySession, false, false, false, h, provider.Chat, turn, turn, cs, pl)
answered(key, cs[1], 1, Usage{CacheRead: 50_000})
t.Cleanup(func() { sticks.Lock(); delete(sticks.m, key); sticks.Unlock() })
got, _, aff, _ := affine("t", provider.AffinitySession, false, false, false, h, provider.Chat, within, within, cs, pl)
if got[0].rest != "inhouse-plugin/big" || !aff.Kept {
t.Fatalf("within a turn: first %s, kept %v, why %s", got[0].rest, aff.Kept, aff.Why)
}
got, _, aff, _ = affine("t", provider.AffinitySession, false, false, false, h, provider.Chat, turn, turn, cs, pl)
if got[0].rest != "inhouse-plugin/mid" || aff.Kept || aff.Why != "busy" {
t.Fatalf("a new turn: first %s, kept %v, why %s", got[0].rest, aff.Kept, aff.Why)
}
// every one busy: kept where it was
both := planned{order: []Weighed{{Who: "mid", Busy: 85}, {Who: "big", Busy: 92}}}
got, _, aff, _ = affine("t", provider.AffinitySession, false, false, false, h, provider.Chat, turn, turn, cs, both)
if got[0].rest != "inhouse-plugin/big" || !aff.Kept {
t.Fatalf("all busy: first %s, kept %v, why %s", got[0].rest, aff.Kept, aff.Why)
}
if got, _ := after(append([]candidate(nil), cs...), planned{order: append([]Weighed(nil), pl.order...)}, 0); got[0].rest != "inhouse-plugin/mid" {
t.Fatalf("turn went to %s", got[0].rest)
}
if readyForNew(pl.order, 1) {
t.Fatal("a new conversation may go to the busy one")
}
}