agent: every agent's magpie options are built once per change, not per Drift poll

viaMagpie is the half of every agent's model picker that magpie serves.
It built CatalogFor, the own and first accounts, and the fast and ultra
picks on every call. The window's /api/drift poll asks each agent's
fields for their options every 15s. In a live magpie's 53-minute heap
profile (v0.1.2404), Claude Code's options alone (claudeViaMagpie)
allocated 4.7 GB of 32.8 GB.

viaMagpie now keeps its list per agent and prefix, the same way
magpieModels does since v0.1.2422. It builds the list again when
provider.StampNow moves, and after a minute for what the stamp can't
see (an account another app signed in to). Each caller gets its own
copy. The two now share stampedLists[T].

Run (mote, at this tree):
- go test ./internal/agent: ok, 191.501s.
- TestViaMagpieKeptWhileCatalogStays, TestMagpieModelsKept... and
  TestIsMagpieKept...: ok.
- go vet ./internal/agent: darwin, linux and windows ok.

Broken by hand, TestViaMagpieKeptWhileCatalogStays goes red each time:
- never kept: FAIL;
- the stamp not compared: FAIL;
- the kept list handed out unshared: FAIL.

Real A/B: magpie web --gateway under the same sandbox HOME as
v0.1.2422's record, 200 GET /api/drift each. Before = v0.1.2422's tree.
The drift JSON is byte-identical.
- allocated under Drift: 2065 MB before, 1717 MB after.
- under viaMagpie: 367 MB before, under pprof's cut after.
- under CatalogFor: 538 MB before, 229 MB after.
- CPU: 1.95s before, 1.80s after.
Most of what is left under Drift in this sandbox is theInstalled's read
of each agent's config. It runs only for a magpie beside the installed
one (this sandbox's gateway port), not for the installed app.
This commit is contained in:
yetone
2026-11-01 00:07:57 +08:00
parent 854fc81619
commit 3baf3b6dfd
2 changed files with 93 additions and 23 deletions
+49
View File
@@ -59,3 +59,52 @@ func TestMagpieModelsKeptWhileCatalogStays(t *testing.T) {
t.Fatal("a model hidden from pi since is still listed")
}
}
// viaMagpie, every agent's model picker, is kept the same way: Claude
// Code's options alone (claudeViaMagpie) were 4.7 GB of the 33 GB.
func TestViaMagpieKeptWhileCatalogStays(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)
}
refs := func(prefix string) map[string]bool {
out := map[string]bool{}
for _, o := range viaMagpie("pi", prefix) {
out[o.Value] = true
}
return out
}
if got := refs(""); !got["oa/gpt-5.5"] || !got["oa/gpt-5.4"] {
t.Fatalf("oa's models not offered: %v", got)
}
// a prefix is kept apart from the bare list
if got := refs("magpie/"); !got["magpie/oa/gpt-5.5"] {
t.Fatalf("the prefixed list: %v", got)
}
built := testing.AllocsPerRun(5, func() { buildVia("pi", "") })
kept := testing.AllocsPerRun(20, func() { viaMagpie("pi", "") })
if kept > 100 || kept*5 > built {
t.Fatalf("options already built took %.0f allocations, building them %.0f", kept, built)
}
mine := viaMagpie("pi", "")
for i := range mine {
mine[i].Value += "[1m]"
}
if got := refs(""); !got["oa/gpt-5.5"] {
t.Fatalf("a caller's write reached the options kept: %v", got)
}
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)
}
if !refs("")["nb/x"] {
t.Fatal("a provider saved since is not offered")
}
s := settings.Load()
s.HiddenModels = map[string][]string{"pi": {"oa/gpt-5.4"}}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if refs("")["oa/gpt-5.4"] {
t.Fatal("a model hidden from pi since is still offered")
}
}
+44 -23
View File
@@ -27,8 +27,21 @@ const RoutingGroups = "Routing groups"
// viaMagpie lists the catalog as agent is shown it, for a picker: the
// routing groups first, in one group of their own, then one group per
// provider.
// provider. Built again only when provider.StampNow moved, or
// magpieModelsFor after it was; each caller is given its own copy.
func viaMagpie(agent, prefix string) []Option {
return builtOptions.get(agent+"\x00"+prefix, func() []Option { return buildVia(agent, prefix) })
}
// 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
// (claudeViaMagpie) allocated 4.7 GB of the 33 GB a live magpie did in 53
// minutes.
var builtOptions stampedLists[Option]
// buildVia is viaMagpie built afresh.
func buildVia(agent, prefix string) []Option {
var out, groups []Option
shown, _ := provider.CatalogFor(agent)
// own: whether a provider is the account the agent is signed in to,
@@ -186,22 +199,7 @@ func firstOf(xs []string) string {
// provider.StampNow moved, or magpieModelsFor after it was, each caller
// given its own copy of the list.
func magpieModels(agent string) []catalog.Model {
builtModels.Lock()
b, ok := builtModels.m[agent]
builtModels.Unlock()
// the stamp is read before the list is built: a change while it is
// moves it past the one kept, and the next call builds it again
stamp := provider.StampNow()
if !ok || b.stamp != stamp || time.Since(b.at) >= magpieModelsFor {
b = builtList{buildModels(agent), stamp, time.Now()}
builtModels.Lock()
if builtModels.m == nil {
builtModels.m = map[string]builtList{}
}
builtModels.m[agent] = b
builtModels.Unlock()
}
return slices.Clone(b.models)
return builtModels.get(agent, func() []catalog.Model { return buildModels(agent) })
}
// builtModels is what magpieModels built last, per agent. The window asks
@@ -209,15 +207,38 @@ func magpieModels(agent string) []catalog.Model {
// per field, each built whole with its prices and Vision's ranking of the
// models that see: 14.5 GB of the 33 GB a live magpie allocated in 53
// minutes, the garbage its footprint peaks on.
var builtModels struct {
var builtModels stampedLists[catalog.Model]
// stampedLists keeps a list built per key while provider.StampNow stays
// the same, magpieModelsFor at most, and gives each caller a copy.
type stampedLists[T any] struct {
sync.Mutex
m map[string]builtList
m map[string]stampedList[T]
}
type builtList struct {
models []catalog.Model
stamp provider.Stamp
at time.Time
type stampedList[T any] struct {
list []T
stamp provider.Stamp
at time.Time
}
func (s *stampedLists[T]) get(key string, build func() []T) []T {
s.Lock()
b, ok := s.m[key]
s.Unlock()
// the stamp is read before the list is built: a change while it is
// moves it past the one kept, and the next call builds it again
stamp := provider.StampNow()
if !ok || b.stamp != stamp || time.Since(b.at) >= magpieModelsFor {
b = stampedList[T]{build(), stamp, time.Now()}
s.Lock()
if s.m == nil {
s.m = map[string]stampedList[T]{}
}
s.m[key] = b
s.Unlock()
}
return slices.Clone(b.list)
}
// magpieModelsFor is how long a list is kept while provider.StampNow says