From eb3b9376eabfe23747fd3ff2768a76cd12e2e20d Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 2 Nov 2026 03:38:47 +0800 Subject: [PATCH] plugin load: a model listed by another id gets its plugin's load (axelburks) axelburks on Discord: a plugin reports a model's load through input.magpie.load by its own model id. A model the user lists in magpie under another id (Listed as, settings' ModelIDs) showed no load on the Providers page, and with Busy models last on it was never judged busy; models whose id wasn't changed worked. The reporter's reading was right: plugin.ModelLoad keeps the load by the plugin's id and both callers asked it by the listed one. Both call sites of plugin.ModelLoad (grep: there are only two) now map the listed id back through Provider.OwnModelID first: - internal/gui/providers.go providerInfoWith (models[].load), which already computed OwnModelID for models[].vendor; it is now read once and used for both. - internal/gateway/fallback.go busyOf (restLast's busy ordering and the trace's busy). A listed id whose own id is a model@size (grok -> grok-4.7@256k) still falls back to the model's load inside ModelLoad. The provider-plugins subsystem page says the lookup is by the plugin's own id. Tests: - internal/gui TestModelLoadListedAsAnotherID: a plugin provider with fast -> big and grok -> grok-4.7@256k listed, loads noted for big, grok-4.7 and small; models[].load must be 87.4 / 60 / 12 for fast / grok / small and absent for quiet. - internal/gateway TestBusyModelListedAsAnotherID: busyOf at 80 judges fast, grok and big busy, and an unreported model not. Run at the pushed head on mote (mote-run): - at base de7953e3b with this change: gofmt -l clean; go vet of internal/gui, gateway, plugin for darwin, GOOS=linux (-tags nogui) and GOOS=windows: ok. go test -count=1 -timeout 45m ./internal/gui ./internal/gateway ./internal/plugin: plugin ok; gui red only on TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27 corners) and gateway red only on TestCodexPoolSaysTheClientsVersion (#4807, another test's Codex identity in a full package run); both filed, neither touches loads. internal/gui/tests/model-load.test.cjs (Chromium and WebKit, every language, narrow and wide): 82 of 82 pass. - at the pushed head 7a804cf03 (rebased; the rebase changed none of the touched files): vet for the three OSes ok, and go test -run "Load|Busy|ListedAs" of internal/gui, gateway and plugin: ok. Broken by hand: with both call sites put back to m.ID / c.model, both new tests fail ("fast: no load, want 87.4", "grok: no load, want 60"; "fast: load 0 busy false", "grok: load 0 busy false"); restored, both pass. Not tried: a real plugin reporting loads in the running app with a renamed model (the reporter's in-house vendor plugin isn't available here); model-load.test.cjs fakes the API, so it checks the badge's drawing, not this lookup. No full go test ./... (another session's full suite was running on mote). --- docs/subsystems/provider-plugins.md | 2 +- internal/gateway/busy_load_test.go | 30 ++++++++++++++ internal/gateway/fallback.go | 6 ++- internal/gui/model_load_listed_test.go | 55 ++++++++++++++++++++++++++ internal/gui/providers.go | 9 +++-- 5 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 internal/gui/model_load_listed_test.go diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index 7683dd54e..8a5bc4db3 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -88,7 +88,7 @@ A plugin can tell magpie of a request it sent its vendor on its own, outside any A plugin can say how busy each model is (axelburks on Discord: an in-house vendor queues requests to its busy models, and a load put in the model's name changed only when the list did). It calls `input.magpie.load(provider, {"": 0 and up | null})` whenever it likes, from its own timer, as usage data refreshes; magpie never asks. The host ([`host.js`](../../internal/plugin/host.js) `reportLoad`) throws a TypeError for a provider that isn't a string of 1–200 characters or a `loads` that isn't a plain object, clamps each finite number to 0–10000 (`LOAD_MOST`, `plugin.LoadMost`: past 100 is a vendor that queues, 200 twice what it serves at once, axelburks again; 10000 is a sanity cap), sends `null` as -1 (cleared), drops every other value and an id over 200 characters, takes at most 1000, and sends what is left as an unasked `{event:"load", provider, loads}` (`dispatch` in [`host.go`](../../internal/plugin/host.go)). It returns how many it took. -[`load.go`](../../internal/plugin/load.go) keeps each by the plugin's provider id and the model id lowercased, with when it came (`noteLoads`, at most 1000 a provider). `ModelLoad(provider, model, now)` answers it, a model@size falling back to its model, and unknown once it is older than `LoadFresh` (5 minutes): a plugin that stops reporting leaves its models unknown, never idle. Nothing is saved; a restart starts with none known. +[`load.go`](../../internal/plugin/load.go) keeps each by the plugin's provider id and the model id lowercased, with when it came (`noteLoads`, at most 1000 a provider). `ModelLoad(provider, model, now)` answers it by the plugin's own id of the model (its callers, the provider editor and `busyOf`, map a model the user lists by another id back through `OwnModelID` first), a model@size falling back to its model, and unknown once it is older than `LoadFresh` (5 minutes): a plugin that stops reporting leaves its models unknown, never idle. Nothing is saved; a restart starts with none known. The provider editor shows it by the model as a grey badge (`/api/providers`' `models[].load`, `loadTag` in app.js). Routing reads it only when the user sets *Busy models last* (`settings.BusyLoad`, `POST /api/settings/busy-load {at}`, off by default): see `restLast` and `busyBehind` in [gateway routing](gateway-routing.md). diff --git a/internal/gateway/busy_load_test.go b/internal/gateway/busy_load_test.go index 5c3cdcf66..50bf18f61 100644 --- a/internal/gateway/busy_load_test.go +++ b/internal/gateway/busy_load_test.go @@ -146,3 +146,33 @@ func TestBusyModelNotKeptAtNewTurn(t *testing.T) { t.Fatal("a new conversation may go to the busy one") } } + +// A plugin reports a model's load by its own id. One the user lists by +// another (Listed as) is judged busy by that load all the same (axelburks +// on Discord: a renamed model was never busy, the others were). +func TestBusyModelListedAsAnotherID(t *testing.T) { + plugin.ForgetLoads() + t.Cleanup(plugin.ForgetLoads) + s := settings.Load() + was := s.ModelIDs + s.ModelIDs = map[string]string{"inhouse-plugin/fast": "big", "inhouse-plugin/grok": "grok-4.7@256k"} + if err := settings.Save(s); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { s := settings.Load(); s.ModelIDs = was; settings.Save(s) }) + now := time.Now() + plugin.NoteLoadsForTest("inhouse", map[string]float64{"big": 92, "grok-4.7": 95}, now) + p := provider.PluginForTest("inhouse-plugin", "inhouse") + if own := p.OwnModelID("fast"); own != "big" { + t.Fatalf("OwnModelID(fast) = %q", own) + } + for _, model := range []string{"fast", "grok", "big"} { + load, busy := busyOf(candidate{p: p, model: model, rest: "inhouse-plugin/" + model}, 80, now) + if !busy || load < 92 { + t.Errorf("%s: load %v busy %v, want busy over 80", model, load, busy) + } + } + if load, busy := busyOf(candidate{p: p, model: "other", rest: "inhouse-plugin/other"}, 80, now); busy || load != 0 { + t.Errorf("other: load %v busy %v, want none", load, busy) + } +} diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index 7ca91a107..9ec8f8c27 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -1144,12 +1144,14 @@ func restLast(out []candidate, pl planned) ([]candidate, planned) { // busyOf is the load c's plugin last reported for its model, and whether // that is over the percent over (settings.BusyLoad; 0 is off). A model // with no load reported, or none within plugin.LoadFresh, isn't busy: -// unknown is never taken for idle nor for busy. +// unknown is never taken for idle nor for busy. The plugin reports it by +// its own id of the model, so a model the user lists by another is looked +// up by that (axelburks on Discord). func busyOf(c candidate, over int, now time.Time) (float64, bool) { if over <= 0 { return 0, false } - load, _, ok := plugin.ModelLoad(c.p.PluginProvider(), c.model, now) + load, _, ok := plugin.ModelLoad(c.p.PluginProvider(), c.p.OwnModelID(c.model), now) return load, ok && load > float64(over) } diff --git a/internal/gui/model_load_listed_test.go b/internal/gui/model_load_listed_test.go new file mode 100644 index 000000000..34d0267c2 --- /dev/null +++ b/internal/gui/model_load_listed_test.go @@ -0,0 +1,55 @@ +package gui + +import ( + "os" + "testing" + "time" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/plugin" + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// The Providers page shows a plugin model's load for a model the user +// lists by another id (Listed as) too: the plugin reports it by its own id +// (axelburks on Discord: a renamed model showed no load). +func TestModelLoadListedAsAnotherID(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", os.Getenv("HOME")) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + plugin.ForgetLoads() + t.Cleanup(plugin.ForgetLoads) + s := settings.Load() + s.ModelIDs = map[string]string{"inhouse-plugin/fast": "big", "inhouse-plugin/grok": "grok-4.7@256k"} + if err := settings.Save(s); err != nil { + t.Fatal(err) + } + plugin.NoteLoadsForTest("inhouse", map[string]float64{"big": 87.4, "grok-4.7": 60, "small": 12}, time.Now()) + p := provider.PluginForTest("inhouse-plugin", "inhouse") + ms := []catalog.Model{{ID: "fast"}, {ID: "grok"}, {ID: "small"}, {ID: "quiet"}} + info := providerInfoWith(p, provider.Listing{Available: ms, Exposed: ms}, nil, nil) + want := map[string]float64{"fast": 87.4, "grok": 60, "small": 12} + seen := 0 + for _, m := range info.Models { + w, has := want[m.ID] + switch { + case !has && m.Load != nil: + t.Errorf("%s: load %v, want none", m.ID, *m.Load) + case has && m.Load == nil: + t.Errorf("%s: no load, want %v", m.ID, w) + case has && *m.Load != w: + t.Errorf("%s: load %v, want %v", m.ID, *m.Load, w) + } + if has { + seen++ + } + if m.ID == "fast" && m.Vendor != "big" { + t.Errorf("fast: Vendor %q, want big", m.Vendor) + } + } + if seen != len(want) { + t.Fatalf("models %+v", info.Models) + } +} diff --git a/internal/gui/providers.go b/internal/gui/providers.go index 15952429d..3ffe82b14 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -849,7 +849,10 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs } window := p.WindowOf(m) j := modelJSON{ID: m.ID, Name: m.Name, Efforts: provider.EffortsOf(m), On: on, Context: window, Output: provider.OutputWithin(window, p.ReplyLimitIn(m, set)), Listed: provider.ListedWindow(m), Max: m.MaxContext, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Images: images, ImageSet: imageSet, Own: own, Best: newest[m.ID]} - if l, _, ok := plugin.ModelLoad(p.PluginProvider(), m.ID, time.Now()); ok { + // a plugin reports a model's load by its own id, not one the user + // lists it by (axelburks on Discord) + ownID := p.OwnModelID(m.ID) + if l, _, ok := plugin.ModelLoad(p.PluginProvider(), ownID, time.Now()); ok { j.Load = &l } if i := slices.IndexFunc(most, func(c catalog.Model) bool { return c.ID == m.ID }); j.Max == 0 && i >= 0 { @@ -871,8 +874,8 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs } j.NoAPI = noAPI(m.ID) j.Same = sames[ref] - if own := p.OwnModelID(m.ID); own != m.ID { - j.Vendor = own + if ownID != m.ID { + j.Vendor = ownID } j.IDs = p.TakesModelIDs() j.Kind = p.ModelKind(m.ID)