diff --git a/internal/agent/magpie_models_kept_test.go b/internal/agent/magpie_models_kept_test.go index a22aa37c8..3aec9d359 100644 --- a/internal/agent/magpie_models_kept_test.go +++ b/internal/agent/magpie_models_kept_test.go @@ -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") + } +} diff --git a/internal/agent/route.go b/internal/agent/route.go index c983cfbf9..67bb3baba 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -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