Files
magpie/internal/plugin/load.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

104 lines
2.9 KiB
Go

package plugin
import (
"strings"
"sync"
"time"
)
// LoadFresh is how long a model's load counts from the plugin's call that
// reported it (magpie.load in host.js). Past it the load is unknown: a
// plugin that stopped reporting says nothing of how busy the vendor is
// now, and routing never takes its silence for idle.
const LoadFresh = 5 * time.Minute
// LoadMost caps a reported load, as host.js's LOAD_MOST does: a vendor
// that queues reports past 100 (200 is twice what it serves at once,
// axelburks on Discord), and 100 times full is past any real queue.
const LoadMost = 10000
// loadModels caps the models one provider keeps a load for, as host.js
// caps one call's; a plugin reporting new ids every call can't grow it.
const loadModels = 1000
type modelLoad struct {
pct float64
at time.Time
}
// loads are the models' loads the plugins reported, by plugin provider
// id and then by the model id lowercased.
var loads = struct {
sync.Mutex
m map[string]map[string]modelLoad
}{m: map[string]map[string]modelLoad{}}
// noteLoads keeps a "load" event's models: each a percent from 0 up to LoadMost,
// -1 clearing it. Models the event doesn't name keep theirs, each till
// its own LoadFresh runs out.
func noteLoads(provider string, got map[string]float64, now time.Time) {
if provider == "" || len(got) == 0 {
return
}
loads.Lock()
defer loads.Unlock()
m := loads.m[provider]
if m == nil {
m = map[string]modelLoad{}
loads.m[provider] = m
}
for id, v := range got {
k := strings.ToLower(id)
if v < 0 || v != v {
delete(m, k)
continue
}
m[k] = modelLoad{pct: min(v, LoadMost), at: now}
}
if len(m) > loadModels {
for k, l := range m {
if now.Sub(l.at) > LoadFresh || len(m) > loadModels {
delete(m, k)
}
}
}
if len(m) == 0 {
delete(loads.m, provider)
}
}
// ModelLoad is how busy the plugin provider's model is, a percent from
// 0 up to LoadMost as its plugin last reported it, and when (a model@size has its
// model's load unless reported itself); ok is false when the
// plugin never reported it, cleared it, or reported it more than
// LoadFresh before now.
func ModelLoad(provider, model string, now time.Time) (pct float64, at time.Time, ok bool) {
if provider == "" || model == "" {
return 0, time.Time{}, false
}
k := strings.ToLower(model)
loads.Lock()
l, ok := loads.m[provider][k]
if i := strings.LastIndexByte(k, '@'); !ok && i > 0 {
// grok-4.7@256k is the model grok-4.7 at a context size: as busy
l, ok = loads.m[provider][k[:i]]
}
loads.Unlock()
if !ok || now.Sub(l.at) > LoadFresh {
return 0, time.Time{}, false
}
return l.pct, l.at, true
}
// NoteLoadsForTest is a "load" event's loads reported at at, for a test.
func NoteLoadsForTest(provider string, got map[string]float64, at time.Time) {
noteLoads(provider, got, at)
}
// ForgetLoads drops every load reported, for a test.
func ForgetLoads() {
loads.Lock()
loads.m = map[string]map[string]modelLoad{}
loads.Unlock()
}