From cdfdb62c699fb5e99acdf9ab7b17b905791f55c1 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 07:54:54 +0800 Subject: [PATCH] agent: a model field's list is joined with the catalog once while neither changed Each agent's model field returned append(its own models, viaMagpieShared(...)...), a copy of the whole catalog's options for each field of each agent on every poll: Picks was 94.7 MB of 496 MB over forty polls of the Agents page with real accounts. withVia keeps the joined list per agent and prefix while the own models read the same (sameOption, field by field) and the catalog's list is the very one kept (same array), and joins it again when either changed. The list is read only and clipped, as viaMagpieShared's is; every caller of a field's Options reads it, Picks clones it before it drops anything. The 52 `return append(own, viaMagpieShared(...)...)` sites call it. omp's and Reasonix's planner edit the joined list after the append, and Grok's writes Same into its copy, so they keep their own copies. TestWithViaKeepsTheJoinedList: the same own models give the same array, own models changed in a value or only an icon are joined anew, a provider saved since is listed, and nothing joined is the catalog's own list. TestSameOptionReadsEveryField sets each field of Option alone and fails when sameOption doesn't see it. Broken by hand: never answering from the kept list ("the same own models joined the catalog again"), and FastFor left out of sameOption ("sameOption doesn't read Option.FastFor"). Run on mote: both tests alone 3 of 3; internal/agent, internal/tui and internal/cli ok; internal/gui red only on TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners (#3949); gofmt, vet and GOOS=windows and linux vet clean. Real accounts in a sandbox HOME, forty polls: Picks 94.7 MB -> 51.2 MB, state 190.8 MB -> 154.0 MB, state and drift replies byte for byte the same as before. Footprint peak 136 MB. --- internal/agent/agents.go | 8 +-- internal/agent/agy.go | 2 +- internal/agent/alma.go | 2 +- internal/agent/aside.go | 2 +- internal/agent/astrbot.go | 2 +- internal/agent/atomcode.go | 2 +- internal/agent/cline.go | 2 +- internal/agent/codebuddy.go | 2 +- internal/agent/codex.go | 2 +- internal/agent/commandcode.go | 2 +- internal/agent/droid.go | 2 +- internal/agent/dsh.go | 2 +- internal/agent/empryo.go | 2 +- internal/agent/fx.go | 2 +- internal/agent/gemini.go | 2 +- internal/agent/grokapp.go | 2 +- internal/agent/hanako.go | 2 +- internal/agent/hermes.go | 4 +- internal/agent/jcode.go | 2 +- internal/agent/kimi.go | 2 +- internal/agent/minimax.go | 2 +- internal/agent/morph.go | 2 +- internal/agent/mothx.go | 2 +- internal/agent/muse.go | 2 +- internal/agent/openchamber.go | 2 +- internal/agent/openclaw.go | 2 +- internal/agent/penguin.go | 2 +- internal/agent/plugged.go | 2 +- internal/agent/qoder.go | 2 +- internal/agent/qwen.go | 2 +- internal/agent/reasonix.go | 2 +- internal/agent/route.go | 49 +++++++++++++++++ internal/agent/snow.go | 2 +- internal/agent/stepcode.go | 2 +- internal/agent/via_joined_test.go | 91 +++++++++++++++++++++++++++++++ internal/agent/vscode.go | 2 +- internal/agent/wsl.go | 26 ++++----- internal/agent/zed.go | 2 +- 38 files changed, 192 insertions(+), 52 deletions(-) create mode 100644 internal/agent/via_joined_test.go diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 67ae42675..6ef7ed4d9 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -1000,7 +1000,7 @@ func openCodeLike(at place, id, name, icon, bin, dir, auth string, ua []string, provider := openCodeKept(path, func() any { return magpieProviderJSONAt("opencode", id, at.gw()) }) opts := func(key string) func(map[string]string) []Option { return func(cur map[string]string) []Option { - return append(ownOptions(auth, cur[key]), viaMagpieShared(id, magpieID+"/")...) + return withVia(ownOptions(auth, cur[key]), id, magpieID+"/") } } get := func(k string) string { v, _ := edit.GetJSON(path, k); return v } @@ -1416,7 +1416,7 @@ func piLikeKeyed(at place, id, name, dir string, key func() string) *Agent { return piScopeWith(path, v) }, Options: func(cur map[string]string) []Option { - return append(ownOptionsFrom(piRegistry(id), auth, cur["model"]), viaMagpieShared(id, magpieID+"/")...) + return withVia(ownOptionsFrom(piRegistry(id), auth, cur["model"]), id, magpieID+"/") }, }, { @@ -1536,7 +1536,7 @@ func gooseIn(at place, cfg string) *Agent { // only the native providers this goose is set up with (#987: // every one of four was listed, OpenRouter's hundreds of // models on a goose that had only magpie) - return append(ownOptions("", cur["model"], gooseConfigured(path, keyEnv)...), viaMagpieShared("goose", gooseProviderID+"/")...) + return withVia(ownOptions("", cur["model"], gooseConfigured(path, keyEnv)...), "goose", gooseProviderID+"/") }, }, { // GOOSE_THINKING_EFFORT, the effort goose asks of a model that @@ -1785,7 +1785,7 @@ func crushAt(at place, path, data string) *Agent { } } } - return append(ownOptions("", cur[key], extra...), viaMagpieShared("crush", magpieID+"/")...) + return withVia(ownOptions("", cur[key], extra...), "crush", magpieID+"/") } } // replacePick puts v in place of the whole models. object in the diff --git a/internal/agent/agy.go b/internal/agent/agy.go index 8e1e11f1b..ccaba10f7 100644 --- a/internal/agent/agy.go +++ b/internal/agent/agy.go @@ -391,7 +391,7 @@ func agyIn(at place) *Agent { if c := cur["model"]; c != "" && !usesMagpie(c) && custom[c] == nil { own = append([]Option{{Value: c, Icon: modelIcon("", c), Direct: "Google"}}, own...) } - return append(group("Antigravity CLI", own), viaMagpieShared("agy", magpieID+"/")...) + return withVia(group("Antigravity CLI", own), "agy", magpieID+"/") }, }}, } diff --git a/internal/agent/alma.go b/internal/agent/alma.go index f7155bd21..9b30273bc 100644 --- a/internal/agent/alma.go +++ b/internal/agent/alma.go @@ -757,7 +757,7 @@ func alma() *Agent { Get: almaGet, Set: almaSet, Options: func(map[string]string) []Option { - return append(almaOwn(), viaMagpieShared("alma", magpieID+"/")...) + return withVia(almaOwn(), "alma", magpieID+"/") }, }}, } diff --git a/internal/agent/aside.go b/internal/agent/aside.go index a381c0fdd..ff5aa170a 100644 --- a/internal/agent/aside.go +++ b/internal/agent/aside.go @@ -54,7 +54,7 @@ func asideIn(at place) *Agent { } return opts } - return append(asideOwnOptions(c.models, cur[key]), viaMagpieShared("aside", "magpie/")...) + return withVia(asideOwnOptions(c.models, cur[key]), "aside", "magpie/") } a.Fields = append(a.Fields, f) } diff --git a/internal/agent/astrbot.go b/internal/agent/astrbot.go index 2c13261b6..9ac48a261 100644 --- a/internal/agent/astrbot.go +++ b/internal/agent/astrbot.go @@ -302,7 +302,7 @@ func astrbotAt(at place) *Agent { return setPick(v) }, Options: func(cur map[string]string) []Option { - return append(astrbotOwn(path, cur["model"]), viaMagpieShared("astrbot", magpieID+"/")...) + return withVia(astrbotOwn(path, cur["model"]), "astrbot", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/atomcode.go b/internal/agent/atomcode.go index 56740040d..77c4f5ef4 100644 --- a/internal/agent/atomcode.go +++ b/internal/agent/atomcode.go @@ -221,7 +221,7 @@ func atomcodeIn(at place) *Agent { return nil }, Options: func(c map[string]string) []Option { - return append(atomcodeOwnModels(path, c["model"]), viaMagpieShared("atomcode", magpieID+"/")...) + return withVia(atomcodeOwnModels(path, c["model"]), "atomcode", magpieID+"/") }, }, { // the current model table's reasoning_effort, sent with every diff --git a/internal/agent/cline.go b/internal/agent/cline.go index f69ae8599..a924220c1 100644 --- a/internal/agent/cline.go +++ b/internal/agent/cline.go @@ -254,7 +254,7 @@ func clineIn(at place) *Agent { edit.KV{Path: "providers." + p + ".settings.model", Value: v}) }, Options: func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared("cline", magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), "cline", magpieID+"/") }, }, { // the reasoning of the provider in use, what Cline's --thinking diff --git a/internal/agent/codebuddy.go b/internal/agent/codebuddy.go index aa36b7c06..84ba14a71 100644 --- a/internal/agent/codebuddy.go +++ b/internal/agent/codebuddy.go @@ -104,7 +104,7 @@ func codebuddyIn(at place) *Agent { if v := cur["model"]; v != "" && !usesMagpie(v) { out = append(out, Option{Value: v, Icon: modelIcon("", v)}) } - return append(out, viaMagpieShared("codebuddy", magpieID+"/")...) + return withVia(out, "codebuddy", magpieID+"/") }, }}, } diff --git a/internal/agent/codex.go b/internal/agent/codex.go index a698c55f1..02c8ac6a4 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -1265,7 +1265,7 @@ func codexIn(at place) *Agent { // models direct only). Routed, Codex's own say via magpie already. onSignIn := (p == "" || p == "openai" || isCCSwitchMirror(p)) && chatGPT() if !through && (!onSignIn || codexFailover()) { - return append(own, viaMagpieShared("codex", "")...) + return withVia(own, "codex", "") } return append(own, codexViaMagpieFor(own)...) } diff --git a/internal/agent/commandcode.go b/internal/agent/commandcode.go index 7caa3920d..fa624ba24 100644 --- a/internal/agent/commandcode.go +++ b/internal/agent/commandcode.go @@ -98,7 +98,7 @@ func commandCodeIn(at place) *Agent { if v := cur["model"]; v != "" && !usesMagpie(v) { out = append(out, Option{Value: v, Icon: modelIcon("", v)}) } - return append(out, viaMagpieShared("commandcode", magpieID+"/")...) + return withVia(out, "commandcode", magpieID+"/") }, }, { // settings.json's reasoningEffort keeps an effort for each model diff --git a/internal/agent/droid.go b/internal/agent/droid.go index e7ec24bfb..d385ff865 100644 --- a/internal/agent/droid.go +++ b/internal/agent/droid.go @@ -450,7 +450,7 @@ func droidIn(at place) *Agent { if c := cur["model"]; c != "" && !usesMagpie(c) && !seen[c] { own = append([]Option{{Value: c, Icon: modelIcon("", c)}}, own...) } - return append(group("Droid", own), viaMagpieShared("droid", magpieID+"/")...) + return withVia(group("Droid", own), "droid", magpieID+"/") }, }}, } diff --git a/internal/agent/dsh.go b/internal/agent/dsh.go index 9e0f04f50..17e6138e1 100644 --- a/internal/agent/dsh.go +++ b/internal/agent/dsh.go @@ -225,7 +225,7 @@ func dshAt(at place) *Agent { own[i].Direct = "DeepSeek" } } - return append(own, viaMagpieShared("dsh", magpieID+"/")...) + return withVia(own, "dsh", magpieID+"/") }, }, { // the thinking effort sessions start with: agent-default-model's diff --git a/internal/agent/empryo.go b/internal/agent/empryo.go index c5c7021f2..0e5c8648d 100644 --- a/internal/agent/empryo.go +++ b/internal/agent/empryo.go @@ -170,7 +170,7 @@ func empryoAt(at place) *Agent { _, id, _ := strings.Cut(c, "/") own = append(own, Option{Value: c, Icon: modelIcon("", id)}) } - return append(group("Empryo", own), viaMagpieShared("empryo", magpieID+"/")...) + return withVia(group("Empryo", own), "empryo", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/fx.go b/internal/agent/fx.go index 9a322b199..28d57944e 100644 --- a/internal/agent/fx.go +++ b/internal/agent/fx.go @@ -118,7 +118,7 @@ func fxIn(at place) *Agent { if v := cur["model"]; v != "" && !usesMagpie(v) { out = append(out, Option{Value: v, Icon: modelIcon("", v)}) } - return append(out, viaMagpieShared("fx", magpieID+"/")...) + return withVia(out, "fx", magpieID+"/") }, }}, } diff --git a/internal/agent/gemini.go b/internal/agent/gemini.go index 71681e4ad..7d9126330 100644 --- a/internal/agent/gemini.go +++ b/internal/agent/gemini.go @@ -261,7 +261,7 @@ func geminiIn(at place) *Agent { } } own := group("Gemini CLI", options(ms, "")) - return append(own, viaMagpieShared("gemini", "")...) + return withVia(own, "gemini", "") }, }, }, diff --git a/internal/agent/grokapp.go b/internal/agent/grokapp.go index 20b201648..33f7438c8 100644 --- a/internal/agent/grokapp.go +++ b/internal/agent/grokapp.go @@ -412,7 +412,7 @@ func grokApp(home string) *Agent { return setDefault(v) }, Options: func(cur map[string]string) []Option { - return append(grokAppOwn(path, keyDefault, cur["model"]), viaMagpieShared("grok-app", magpieID+"/")...) + return withVia(grokAppOwn(path, keyDefault, cur["model"]), "grok-app", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/hanako.go b/internal/agent/hanako.go index b6a7485ce..d3950fce0 100644 --- a/internal/agent/hanako.go +++ b/internal/agent/hanako.go @@ -169,7 +169,7 @@ func hanako(home string) *Agent { return hanakoChat(dir, agent, v) }, Options: func(cur map[string]string) []Option { - return append(hanakoOwn(dir, cur["model"]), viaMagpieShared("hanako", magpieID+"/")...) + return withVia(hanakoOwn(dir, cur["model"]), "hanako", magpieID+"/") }, }}, } diff --git a/internal/agent/hermes.go b/internal/agent/hermes.go index a560b2d57..4f5a47f39 100644 --- a/internal/agent/hermes.go +++ b/internal/agent/hermes.go @@ -222,7 +222,7 @@ func hermesIn(at place) *Agent { return edit.SetYAML(path, edit.KV{Path: "model.default", Value: v}) }, Options: func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared("hermes", magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), "hermes", magpieID+"/") }, }, { // agent.reasoning_effort, which Hermes's /reasoning saves; none @@ -323,7 +323,7 @@ func hermesProfileFields(at place, dir string, provider func(string) any) []Fiel return edit.SetYAML(p, edit.KV{Path: "model.default", Value: v}) }, Options: func(cur map[string]string) []Option { - return append(ownOptions("", cur[hermesProfileKey+name]), viaMagpieShared("hermes", magpieID+"/")...) + return withVia(ownOptions("", cur[hermesProfileKey+name]), "hermes", magpieID+"/") }, }) } diff --git a/internal/agent/jcode.go b/internal/agent/jcode.go index 90d05d8af..8fcc7efbd 100644 --- a/internal/agent/jcode.go +++ b/internal/agent/jcode.go @@ -353,7 +353,7 @@ func jcodeAt(at place) *Agent { return setPick(jcodePick{c.Provider.DefaultProvider, v}) }, Options: func(cur map[string]string) []Option { - return append(jcodeOwn(path, keyPick, cur["model"]), viaMagpieShared("jcode", magpieID+"/")...) + return withVia(jcodeOwn(path, keyPick, cur["model"]), "jcode", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/kimi.go b/internal/agent/kimi.go index d441d9392..768098183 100644 --- a/internal/agent/kimi.go +++ b/internal/agent/kimi.go @@ -361,7 +361,7 @@ func kimiIn(at place) *Agent { return dropMagpie() }, Options: func(cur map[string]string) []Option { - return append(kimiOwnOptions(path, cur["model"]), viaMagpieShared("kimi", magpieID+"/")...) + return withVia(kimiOwnOptions(path, cur["model"]), "kimi", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/minimax.go b/internal/agent/minimax.go index ab2a7e68e..d0d03d1fa 100644 --- a/internal/agent/minimax.go +++ b/internal/agent/minimax.go @@ -189,7 +189,7 @@ func miniMaxAt(at place, dir string) *Agent { return nil }, Options: func(cur map[string]string) []Option { - return append(miniMaxOwnOptions(path, cur["model"]), viaMagpieShared("minimax-code", magpieID+"/")...) + return withVia(miniMaxOwnOptions(path, cur["model"]), "minimax-code", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/morph.go b/internal/agent/morph.go index 82a93137b..5ff534941 100644 --- a/internal/agent/morph.go +++ b/internal/agent/morph.go @@ -153,7 +153,7 @@ func morphIn(at place) *Agent { return edit.SetYAML(path, edit.KV{Path: "llm.model", Value: v}) }, Options: func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared("morph", magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), "morph", magpieID+"/") }, }, { // llm.reasoning_effort; unset, morph sends none diff --git a/internal/agent/mothx.go b/internal/agent/mothx.go index cf9da4fb2..1b42444e9 100644 --- a/internal/agent/mothx.go +++ b/internal/agent/mothx.go @@ -119,7 +119,7 @@ func mothxIn(at place) *Agent { return pair(v) }, Options: func(cur map[string]string) []Option { - return append(mothxOwn(path, cur["model"]), viaMagpieShared("mothx", magpieID+"/")...) + return withVia(mothxOwn(path, cur["model"]), "mothx", magpieID+"/") }, }, { diff --git a/internal/agent/muse.go b/internal/agent/muse.go index f22e629b2..5c551c7a6 100644 --- a/internal/agent/muse.go +++ b/internal/agent/muse.go @@ -153,7 +153,7 @@ func museAt(at place, cfg string) *Agent { if c := cur["model"]; c != "" && !usesMagpie(c) { own = append(own, Option{Value: c, Icon: museIcon(c)}) } - return append(group("Muse Code", own), viaMagpieShared("muse", magpieID+"/")...) + return withVia(group("Muse Code", own), "muse", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/openchamber.go b/internal/agent/openchamber.go index 982805a28..9b5619afd 100644 --- a/internal/agent/openchamber.go +++ b/internal/agent/openchamber.go @@ -167,7 +167,7 @@ func openChamber(home, cfg string) *Agent { provider := openCodeKept(ocPath, func() any { return magpieProviderJSONFor("opencode", "opencode") }) opts := func(key string) func(map[string]string) []Option { return func(cur map[string]string) []Option { - return append(ownOptions(auth, cur[key]), viaMagpieShared("opencode", magpieID+"/")...) + return withVia(ownOptions(auth, cur[key]), "opencode", magpieID+"/") } } // stepped off magpie: its provider goes too, when nothing else in the diff --git a/internal/agent/openclaw.go b/internal/agent/openclaw.go index e5b1075a9..932c1ba08 100644 --- a/internal/agent/openclaw.go +++ b/internal/agent/openclaw.go @@ -478,7 +478,7 @@ func openClawAt(at place) *Agent { return patch([]edit.KV{{Path: openClawModel, Value: v}}, nil) }, Options: func(cur map[string]string) []Option { - return append(openClawOwn(keyWas, cur["model"]), viaMagpieShared("openclaw", magpieID+"/")...) + return withVia(openClawOwn(keyWas, cur["model"]), "openclaw", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/penguin.go b/internal/agent/penguin.go index 61d2c55a0..11d5352db 100644 --- a/internal/agent/penguin.go +++ b/internal/agent/penguin.go @@ -289,7 +289,7 @@ func penguinIn(at place) *Agent { return dropMagpie() }, Options: func(cur map[string]string) []Option { - return append(penguinOwnOptions(path, cur["model"]), viaMagpieShared("penguin", magpieID+"/")...) + return withVia(penguinOwnOptions(path, cur["model"]), "penguin", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/plugged.go b/internal/agent/plugged.go index b6b008c28..db53fca27 100644 --- a/internal/agent/plugged.go +++ b/internal/agent/plugged.go @@ -420,7 +420,7 @@ func plugged(home string, p *agentplug.Agent) *Agent { return f.set([]edit.KV{{Path: d.Model, Value: v}}) }, Options: func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared(id, magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), id, magpieID+"/") }, }}, // the catalog changed: what connect writes for the model may too diff --git a/internal/agent/qoder.go b/internal/agent/qoder.go index a72e009c7..a85de4b4e 100644 --- a/internal/agent/qoder.go +++ b/internal/agent/qoder.go @@ -189,7 +189,7 @@ func qoderAt(at place, b qoderBuild) *Agent { return edit.SetJSON(path, edit.KV{Path: "model.name", Value: v}) }, Options: func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared(b.id, magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), b.id, magpieID+"/") }, }, { // the effort of the model in use, in its model.preferences diff --git a/internal/agent/qwen.go b/internal/agent/qwen.go index f132597d4..9aa1b997f 100644 --- a/internal/agent/qwen.go +++ b/internal/agent/qwen.go @@ -406,7 +406,7 @@ func qwenIn(at place) *Agent { return setProviders(false) }, Options: func(cur map[string]string) []Option { - return append(qwenOwnOptions(path, cur["model"]), viaMagpieShared("qwen", magpieID+"/")...) + return withVia(qwenOwnOptions(path, cur["model"]), "qwen", magpieID+"/") }, }}, }, path) diff --git a/internal/agent/reasonix.go b/internal/agent/reasonix.go index d67317ec0..a7a7c7fbb 100644 --- a/internal/agent/reasonix.go +++ b/internal/agent/reasonix.go @@ -393,7 +393,7 @@ func reasonixIn(at place) *Agent { addNative(*state.Default) } addNative(cur["model"]) - return append(group("Reasonix Studio", own), viaMagpieShared("reasonix", magpieID+"/")...) + return withVia(group("Reasonix Studio", own), "reasonix", magpieID+"/") }, }, {Key: "effort", Label: "effort", Quiet: true, diff --git a/internal/agent/route.go b/internal/agent/route.go index 339e6cb73..92c2bd56a 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -41,6 +41,55 @@ func viaMagpieShared(agent, prefix string) []Option { return builtOptions.shared(agent+"\x00"+prefix, func() []Option { return buildVia(agent, prefix) }) } +// withVia is append(own, viaMagpieShared(agent, prefix)...), the list a +// model field offers, kept while own reads the same and the catalog's list +// is the one kept: the append copied the whole catalog for each field of +// each agent, every poll, about 60 MB of what forty polls of the Agents +// page allocated with real accounts. Read only, as viaMagpieShared's. +func withVia(own []Option, agent, prefix string) []Option { + via := viaMagpieShared(agent, prefix) + if len(own) == 0 { + return via + } + key := [2]string{agent, prefix} + joinedOptions.Lock() + defer joinedOptions.Unlock() + if j, ok := joinedOptions.m[key]; ok && sameList(j.via, via) && slices.EqualFunc(j.own, own, sameOption) { + return j.out + } + out := make([]Option, 0, len(own)+len(via)) + out = append(append(out, own...), via...) + if joinedOptions.m == nil { + joinedOptions.m = map[[2]string]joinedList{} + } + joinedOptions.m[key] = joinedList{own: out[:len(own)], via: via, out: out} + return out +} + +var joinedOptions struct { + sync.Mutex + m map[[2]string]joinedList +} + +type joinedList struct{ own, via, out []Option } + +// sameList reports whether a and b are the one list kept: the same array, +// as long. +func sameList(a, b []Option) bool { + return len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0]) +} + +// sameOption reports whether a and b read the same, field by field. +func sameOption(a, b Option) bool { + return a.Value == b.Value && a.Label == b.Label && a.Note == b.Note && a.Icon == b.Icon && + slices.Equal(a.Icons, b.Icons) && a.Group == b.Group && a.GroupIcon == b.GroupIcon && + a.Ref == b.Ref && a.Free == b.Free && a.Rate == b.Rate && a.RateWas == b.RateWas && + a.Context == b.Context && a.Takes == b.Takes && a.Direct == b.Direct && a.Via == b.Via && + a.Same == b.Same && a.Served == b.Served && a.Alias == b.Alias && a.FastFor == b.FastFor && + a.Fast == b.Fast && a.OwnFast == b.OwnFast && a.UltraOK == b.UltraOK && a.Ultra == b.Ultra && + a.own == b.own && a.sub == b.sub +} + // builtOptions is what viaMagpie built last, per agent and prefix. Each // agent's model field offers it, and the window's drift poll asked every // field's options of every agent each 15s: Claude Code's alone diff --git a/internal/agent/snow.go b/internal/agent/snow.go index 97bb8d327..53bdfa917 100644 --- a/internal/agent/snow.go +++ b/internal/agent/snow.go @@ -339,7 +339,7 @@ func snowIn(at place) *Agent { return leave(to) }, Options: func(cur map[string]string) []Option { - return append(snowOwnOptions(profiles, cur["model"]), viaMagpieShared("snow", magpieID+"/")...) + return withVia(snowOwnOptions(profiles, cur["model"]), "snow", magpieID+"/") }, }, { // basicModel, the light model Snow's summaries, compaction and diff --git a/internal/agent/stepcode.go b/internal/agent/stepcode.go index a3b4d96e9..7262fa6ba 100644 --- a/internal/agent/stepcode.go +++ b/internal/agent/stepcode.go @@ -626,7 +626,7 @@ func stepcodeIn(at place) *Agent { return nil }, Options: func(cur map[string]string) []Option { - return append(ownOptions(authPath, cur["model"]), viaMagpieShared("stepcode", magpieID+"/")...) + return withVia(ownOptions(authPath, cur["model"]), "stepcode", magpieID+"/") }, }, }, diff --git a/internal/agent/via_joined_test.go b/internal/agent/via_joined_test.go new file mode 100644 index 000000000..1938d47d0 --- /dev/null +++ b/internal/agent/via_joined_test.go @@ -0,0 +1,91 @@ +package agent + +import ( + "reflect" + "slices" + "testing" + "unsafe" + + "github.com/yetone/magpie/internal/provider" +) + +// A model field's list is its own models then the catalog's: the append +// copied the whole catalog for each field of each agent on every poll. +// withVia keeps the list joined while the own models read the same and the +// catalog's list is the one kept, and joins it again when either changed. +func TestWithViaKeepsTheJoinedList(t *testing.T) { + syncHome(t) + if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5", "gpt-5.4"}}); err != nil { + t.Fatal(err) + } + own := func(v string) []Option { return []Option{{Value: v, Note: "own"}} } + want := func(o []Option) []Option { return append(slices.Clone(o), viaMagpieShared("pi", magpieID+"/")...) } + equal := func(a, b []Option) bool { return slices.EqualFunc(a, b, sameOption) } + + first := withVia(own("m1"), "pi", magpieID+"/") + if len(first) < 3 || !equal(first, want(own("m1"))) { + t.Fatalf("the joined list is not own then the catalog: %+v", first) + } + again := withVia(own("m1"), "pi", magpieID+"/") + if &again[0] != &first[0] { + t.Fatal("the same own models joined the catalog again") + } + if n := testing.AllocsPerRun(20, func() { withVia(own("m1"), "pi", magpieID+"/") }); n > 60 { + t.Fatalf("a list already joined took %.0f allocations", n) + } + if cap(again) != len(again) { + t.Fatal("an append onto the kept list would write into it") + } + + // own models that read otherwise, an icon among them, are joined anew + changed := own("m1") + changed[0].Icons = []string{"x"} + if got := withVia(changed, "pi", magpieID+"/"); &got[0] == &first[0] || !equal(got, want(changed)) { + t.Fatalf("own models changed since are not what is listed: %+v", got[0]) + } + if got := withVia(own("m2"), "pi", magpieID+"/"); got[0].Value != "m2" || !equal(got, want(own("m2"))) { + t.Fatalf("own models changed since are not what is listed: %+v", got[0]) + } + + // a provider saved since: the catalog's list is built again, and so + // is the joined one + if err := provider.Save(provider.Provider{ID: "nb", Name: "NB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil { + t.Fatal(err) + } + got := withVia(own("m2"), "pi", magpieID+"/") + if !slices.ContainsFunc(got, func(o Option) bool { return o.Value == magpieID+"/nb/x" }) || !equal(got, want(own("m2"))) { + t.Fatalf("a provider saved since is not listed: %d options", len(got)) + } + + if got := withVia(nil, "pi", magpieID+"/"); !sameList(got, viaMagpieShared("pi", magpieID+"/")) { + t.Fatal("with no own models the catalog's list is not the one kept") + } +} + +// sameOption reads every field of an Option: one added and left out of it +// would keep a joined list whose own models changed only in that field. +func TestSameOptionReadsEveryField(t *testing.T) { + typ := reflect.TypeFor[Option]() + for i := range typ.NumField() { + var a, b Option + f := reflect.ValueOf(&b).Elem().Field(i) + f = reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Elem() + switch f.Kind() { + case reflect.String: + f.SetString("x") + case reflect.Bool: + f.SetBool(true) + case reflect.Int: + f.SetInt(1) + case reflect.Float64: + f.SetFloat(1) + case reflect.Slice: + f.Set(reflect.ValueOf([]string{"x"})) + default: + t.Fatalf("Option.%s is a %s, which this test can't set", typ.Field(i).Name, f.Kind()) + } + if sameOption(a, b) { + t.Errorf("sameOption doesn't read Option.%s", typ.Field(i).Name) + } + } +} diff --git a/internal/agent/vscode.go b/internal/agent/vscode.go index f62ff8f71..1a64527b7 100644 --- a/internal/agent/vscode.go +++ b/internal/agent/vscode.go @@ -380,7 +380,7 @@ func vscodeKindAt(k vscodeKind, dir string) *Agent { if v := cur["model"]; v != "" && v != "auto" && !usesMagpie(v) { own = append(own, Option{Value: v, Icon: modelIcon("", v)}) } - return append(group(k.name, own), viaMagpieShared(k.id, magpieID+"/")...) + return withVia(group(k.name, own), k.id, magpieID+"/") }, }}, }, path, models, stashPath()) diff --git a/internal/agent/wsl.go b/internal/agent/wsl.go index f0c9a1c59..43121786c 100644 --- a/internal/agent/wsl.go +++ b/internal/agent/wsl.go @@ -441,7 +441,7 @@ var wslKinds = []wslKind{ // over several accounts magpie routes, so they are // offered via magpie too, as on a live Codex (#1603) if codexFailover() { - return append(group("OpenAI", options(ownCodex(), "")), viaMagpieShared("codex", "")...) + return withVia(group("OpenAI", options(ownCodex(), "")), "codex", "") } own := codexInMagpie(group("OpenAI", options(ownCodex(), ""))) return append(own, codexViaMagpieFor(own)...) @@ -471,7 +471,7 @@ var wslKinds = []wslKind{ if key == "model" { // the providers of its auth.json are the distro's files return func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared("pi", magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), "pi", magpieID+"/") } } return nil @@ -482,7 +482,7 @@ var wslKinds = []wslKind{ asleep: func(key string) func(map[string]string) []Option { if key == "model" { return func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared("omo", magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), "omo", magpieID+"/") } } return nil @@ -493,7 +493,7 @@ var wslKinds = []wslKind{ asleep: func(key string) func(map[string]string) []Option { if key == "model" { return func(cur map[string]string) []Option { - return append(ownOptions("", cur["model"]), viaMagpieShared("prime-agent", magpieID+"/")...) + return withVia(ownOptions("", cur["model"]), "prime-agent", magpieID+"/") } } return nil @@ -560,7 +560,7 @@ var wslKinds = []wslKind{ return nil } return func(cur map[string]string) []Option { - return append(kimiOwnOptions("", cur["model"]), viaMagpieShared("kimi", magpieID+"/")...) + return withVia(kimiOwnOptions("", cur["model"]), "kimi", magpieID+"/") } }}, // Qwen Code's ~/.qwen, one settings.json of the same shape as this @@ -572,7 +572,7 @@ var wslKinds = []wslKind{ return nil } return func(cur map[string]string) []Option { - return append(qwenOwnOptions("", cur["model"]), viaMagpieShared("qwen", magpieID+"/")...) + return withVia(qwenOwnOptions("", cur["model"]), "qwen", magpieID+"/") } }}, {id: "omp", name: "omp", dir: ".omp", bin: "omp", in: ompIn, version: true, @@ -582,7 +582,7 @@ var wslKinds = []wslKind{ return nil } return func(cur map[string]string) []Option { - return append(ompOwnOptions("", cur[key]), viaMagpieShared("omp", magpieID+"/")...) + return withVia(ompOwnOptions("", cur[key]), "omp", magpieID+"/") } }}, {id: "crush", name: "Crush", dir: ".config/crush", bin: "crush", in: crushIn, @@ -620,7 +620,7 @@ var wslKinds = []wslKind{ if c := cur["model"]; c != "" && !usesMagpie(c) { own = append(own, Option{Value: c, Icon: modelIcon("", c)}) } - return append(group("Droid", own), viaMagpieShared("droid", magpieID+"/")...) + return withVia(group("Droid", own), "droid", magpieID+"/") } }}, // no bin: fx is also the JSON viewer's name @@ -635,7 +635,7 @@ var wslKinds = []wslKind{ return nil } return func(cur map[string]string) []Option { - return append(miniMaxOwnOptions("", cur["model"]), viaMagpieShared("minimax-code", magpieID+"/")...) + return withVia(miniMaxOwnOptions("", cur["model"]), "minimax-code", magpieID+"/") } }}, // version: a dsh with no profile yet is told from one before profiles @@ -651,7 +651,7 @@ var wslKinds = []wslKind{ for i := range own { own[i].Direct = "DeepSeek" } - return append(own, viaMagpieShared("dsh", magpieID+"/")...) + return withVia(own, "dsh", magpieID+"/") } } if key != "effort" { @@ -692,7 +692,7 @@ var wslKinds = []wslKind{ } // the models of its config.toml are the distro's files return func(cur map[string]string) []Option { - return append(atomcodeOwnModels("", cur["model"]), viaMagpieShared("atomcode", magpieID+"/")...) + return withVia(atomcodeOwnModels("", cur["model"]), "atomcode", magpieID+"/") } }}, // Snow CLI's ~/.snow, its profiles the distro's own; a stopped @@ -737,7 +737,7 @@ var wslKinds = []wslKind{ if c := cur["model"]; c != "" && !usesMagpie(c) { own = append(own, Option{Value: c, Icon: modelIcon("", c), Direct: "Google"}) } - return append(group("Antigravity CLI", own), viaMagpieShared("agy", magpieID+"/")...) + return withVia(group("Antigravity CLI", own), "agy", magpieID+"/") } }}, // Block's goose, by its folder alone: a goose command may be pressly's @@ -799,7 +799,7 @@ func wslOwnAsleep(id string, keys ...string) func(string) func(map[string]string return nil } return func(cur map[string]string) []Option { - return append(ownOptions("", cur[key]), viaMagpieShared(id, magpieID+"/")...) + return withVia(ownOptions("", cur[key]), id, magpieID+"/") } } } diff --git a/internal/agent/zed.go b/internal/agent/zed.go index feb0b7fc9..4d9df1c51 100644 --- a/internal/agent/zed.go +++ b/internal/agent/zed.go @@ -128,7 +128,7 @@ func zedAtWith(dir, bin string, processes []string) *Agent { if v := cur["model"]; v != "" && !usesMagpie(v) { own = append(own, Option{Value: v, Icon: modelIcon("", v)}) } - return append(group("Zed", own), viaMagpieShared("zed", magpieID+"/")...) + return withVia(group("Zed", own), "zed", magpieID+"/") }, }}, }, path, stashPath())