Files
magpie/internal/plugin/load_test.go
T
yetone 8d8df06399 plugin, gateway: a plugin reports its models' load, and busy models can go last (axelburks on Discord)
axelburks (Discord #feedback): an in-house vendor queues requests to
its busy models, and the plugin could only put the load in the model's
name, which changed only when magpie re-read the list.

- A provider plugin calls input.magpie.load(provider, {"<model id>":
  0-100 | null}) whenever it likes, from its own timer. host.js
  (reportLoad) clamps each number to 0-100, null clears a model, every
  other value is dropped, at most 1000 a call, and it returns the count
  taken. A provider that isn't a string or loads that isn't an object
  throws a TypeError. It goes to Go as an unasked {event:"load"}.
- internal/plugin/load.go keeps each by provider and lowercased model
  id. ModelLoad answers unknown once a load is older than LoadFresh
  (5 minutes), never idle. model@size falls back to its model.
- The provider editor shows it by the model as a grey ctx badge
  ("87%", titled "87% busy now, as its plugin says").
- Settings > Network and sharing > Busy models last (settings.BusyLoad,
  POST /api/settings/busy-load, Off by default, over 50/70/80/90% or
  1-99 by hand): restLast puts a model reported over it behind the
  ready candidates, ahead of the resting. The trace row has busy:<load>.
  At a new turn affine moves a conversation off it (Why "busy",
  busyBehind); within a turn it stays. after, readyForNew and leastHeld
  skip it. Unknown load, a lone candidate, or all busy: nothing moves.
- Docs: site plugins page (en, zh, ja) "Model load" section and input
  table; docs/subsystems provider-plugins.md and gateway-routing.md.

Run, all on mote via mote-run at a42736807's tree rebased on
origin/main b683f1c0e:
- go test ./internal/plugin ./internal/gateway -run
  'ModelLoad|TestBusy' -v: TestPluginReportsModelLoad,
  TestModelLoadGoesStale, TestBusyModelGoesBehind,
  TestBusyModelNotKeptAtNewTurn pass.
- Broken by hand, each red: host.js not sending the event
  (TestPluginReportsModelLoad "the plugin's loads never arrived"), the
  busy case off in restLast (TestBusyModelGoesBehind order), the affine
  "busy" case off (TestBusyModelNotKeptAtNewTurn "kept true, why
  session").
- go test ./internal/gui -run Budget: ok. No budget raised. app.js
  +86 bytes, two comments trimmed to fit. The settings row lives in
  plugins/gateway/settings.js.
- node --test model-load, i18n-keys, bare-ids, model-rate: 69/69 pass.
  model-load is Chromium and WebKit, 420 and 900 wide, en zh zh-TW ja
  de.
- gofmt -l: empty. go vet ./... (darwin), GOOS=linux go vet -tags
  nogui ./..., GOOS=windows go vet ./..., go build: ok.

Not run before the push (owner: push and tag first): go test ./...
-timeout 45m and the whole GUI suite. mote and the WSL box were each
running another session's full suite. Both run next, and any red gets
its own commit.
Not tried: a real plugin and vendor reporting load. The plugin side is
tested through the real Bun host with a plugin written in the test.
2026-11-01 13:45:40 +08:00

95 lines
3.3 KiB
Go

package plugin
import (
"context"
"os"
"path/filepath"
"testing"
"time"
)
// A plugin reports its models' loads through input.magpie.load (axelburks
// on Discord: an in-house vendor's busy models queue requests), when it
// likes, from its own timer: magpie keeps each, a percent within 0 to 100,
// matched by id whatever its case, and a null clears one. What isn't a
// number is dropped, and a call that isn't a provider and a map throws.
func TestPluginReportsModelLoad(t *testing.T) {
sandbox(t)
ForgetLoads()
t.Cleanup(ForgetLoads)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
dir := filepath.Join(t.TempDir(), "loadco")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
src := `export default async (input) => {
let threw = 0
for (const bad of [[5, {}], ["loadco", null], ["loadco", [1]], ["", {}]]) {
try { input.magpie.load(...bad) } catch (e) { if (e instanceof TypeError) threw++ }
}
const n = input.magpie.load("loadco", { Big: 87.5, small: 0, over: 250, under: -3, word: "busy", nan: NaN, inf: Infinity, threw })
input.magpie.load("loadco", { taken: n })
setTimeout(() => input.magpie.load("loadco", { small: null, later: 12 }), 50)
return {}
}
`
if err := os.WriteFile(filepath.Join(dir, "index.js"), []byte(src), 0o644); err != nil {
t.Fatal(err)
}
if _, err := Add(ctx, filepath.Join(dir, "index.js")); err != nil {
t.Fatal(err)
}
if ls, err := Plugins(ctx); err != nil || len(ls) != 1 || ls[0].Error != "" {
t.Fatalf("Plugins = %+v, %v", ls, err)
}
// the timer's call comes last: once it is in, so is everything before it
for {
if _, _, ok := ModelLoad("loadco", "later", time.Now()); ok {
break
}
if ctx.Err() != nil {
t.Fatal("the plugin's loads never arrived")
}
time.Sleep(10 * time.Millisecond)
}
now := time.Now()
for model, want := range map[string]float64{"big": 87.5, "BIG": 87.5, "over": 100, "under": 0, "threw": 4, "taken": 5, "later": 12} {
if got, _, ok := ModelLoad("loadco", model, now); !ok || got != want {
t.Errorf("ModelLoad(%s) = %v, %v; want %v", model, got, ok, want)
}
}
for _, model := range []string{"small", "word", "nan", "inf", "nobody"} {
if got, _, ok := ModelLoad("loadco", model, now); ok {
t.Errorf("ModelLoad(%s) = %v, want unknown", model, got)
}
}
if _, _, ok := ModelLoad("other", "big", now); ok {
t.Error("another provider's model has loadco's load")
}
}
// A load counts for LoadFresh from the call that reported it, then is
// unknown, never taken for idle; a model@size has its model's load.
func TestModelLoadGoesStale(t *testing.T) {
ForgetLoads()
t.Cleanup(ForgetLoads)
at := time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC)
noteLoads("p", map[string]float64{"m": 80, "n": 10}, at)
noteLoads("p", map[string]float64{"n": 20}, at.Add(4*time.Minute))
if got, _, ok := ModelLoad("p", "m@256k", at.Add(LoadFresh)); !ok || got != 80 {
t.Fatalf("m@256k at LoadFresh = %v, %v", got, ok)
}
later := at.Add(LoadFresh + time.Second)
if got, _, ok := ModelLoad("p", "m", later); ok {
t.Fatalf("m past LoadFresh = %v, want unknown", got)
}
if got, _, ok := ModelLoad("p", "n", later); !ok || got != 20 {
t.Fatalf("n, reported again = %v, %v", got, ok)
}
noteLoads("p", map[string]float64{"n": -1}, later)
if _, _, ok := ModelLoad("p", "n", later); ok {
t.Fatal("a cleared load is still known")
}
}