axelburks (Discord #feedback) could see a model's load only on its provider's page, too deep to judge which model to pick on the Agents page. A plugin's reported load (magpie.load) now shows there too, as the same quiet grey badge the provider page uses: - by the picked model in each agent's model row, before the chevron; - on each row of that row's model picker, after the speed. GET /api/model-loads (speeds.go) answers every fresh load (LoadFresh, 5 minutes) by "provider/model" lowercased, the ref /api/model-speeds uses: plugin.FreshLoads copies the fresh entries under one lock, and provider.ModelLoads keys them by magpie's provider id, the user's own listed id too (ModelIDs), with model@size falling back to its model as ModelLoad does. With no plugin reporting it is one lock and an empty map. The page reads it when a model picker opens and with the Agents page's 15s drift check, each throttled to once per 30s, and the drift read only while a row shows a magpie model. No new poll; /api/state and Option are unchanged. The core script stays under its budget (1,533,802) by shortening comments in the picker and agent-row code; no budget raised. The route is named in the CLI's route list (read-only, U) like model-speeds. Run at this head (rebased on origin/main b4073c06a; the rebase ontodcc84dbc0touches none of these files), on mote: - go test ./internal/gui (TestModelLoadsByRef, TestModelLoadsNoneReported, budgets) ./internal/plugin ./internal/provider ./internal/cli: ok, except TestEveryRouteHasACommandOrIsNamed's POST /api/usage/checkin-account, red on main (#4826,e87140242). - Full go test ./... -timeout 45m before the rebase: 84 packages ok; reds were TestEveryRouteHasACommandOrIsNamed (this route, now named, and #4826), TestCodexPoolSaysTheClientsVersion (#4807, full-package run only) and TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27). - go vet for darwin, GOOS=linux -tags nogui and GOOS=windows on plugin, provider, gui, cli: ok. gofmt -l: clean. - agent-model-load.test.cjs: chromium and webkit, 1000 and 440px, en, zh, zh-TW, ja, de: 40 of 40. Screenshots looked at in both engines. - Whole GUI suite: not finished. Three whole-suite runs (WSL, mote) were cut at 1,266 and 642 tests by load and a session restart; their reds were all in provider-editor, Settings and Usage files. Then the files a new API call or the Agents row can reach (183: every agent-*, every model/picker/speed/load file, the five strict-fixture files, every file using .field, ag-open, renderAgents or /api/drift, i18n-keys), split mote/WSL, both engines: reds only in model-chip-steady (#4388), mobile-web (#3952), model-copy-id (#4621), balance-fix (#4683), allow-ips (#4357, output added), agent-update-all (#2988), account-eye (#4244), models-works-only, models-select-all-api, model-suffix-own, provider-detect-models, provider-models-rest, remote-magpie, routing-rule-models, usage-speed (WebKit on WSL, #4218/#4471). Each red file was run alone at this head and at origin/mainb4073c06a: the same reds both sides, except agent-update-all's fixed 200ms wait (#2988), red once at the head. codex-subagent-model, red under load, passed alone. Broken by hand, each red, then restored: the picker row's badge line, fieldBtn's badge line (red at the renderAgents redraw check), the readLoads painter, and ModelLoads' listed-id loop (TestModelLoadsByRef). Not tried: a real plugin reporting load in the live app; the tray panel layout; WebKitGTK in the real Linux app and the Windows app.
128 lines
3.6 KiB
Go
128 lines
3.6 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
|
|
}
|
|
|
|
// FreshLoads is every load reported less than LoadFresh before now, by
|
|
// plugin provider id and then the model id lowercased, as ModelLoad
|
|
// answers each: nil when none is (a magpie with no plugin reporting).
|
|
func FreshLoads(now time.Time) map[string]map[string]float64 {
|
|
loads.Lock()
|
|
defer loads.Unlock()
|
|
var out map[string]map[string]float64
|
|
for provider, m := range loads.m {
|
|
for k, l := range m {
|
|
if now.Sub(l.at) > LoadFresh {
|
|
continue
|
|
}
|
|
if out == nil {
|
|
out = map[string]map[string]float64{}
|
|
}
|
|
if out[provider] == nil {
|
|
out[provider] = map[string]float64{}
|
|
}
|
|
out[provider][k] = l.pct
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// 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()
|
|
}
|