Files
magpie/internal/agent/magpie_models_kept_test.go
T
yetone 3baf3b6dfd 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.
2026-11-01 00:07:57 +08:00

111 lines
3.7 KiB
Go

package agent
import (
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// magpieModels answers from the list it built while nothing the catalog
// is built from changed: the window's drift poll asked each agent's Drift
// for its list again per field every 15s, each built whole with its
// prices and Vision's ranking (14.5 GB of 33 GB a live magpie allocated
// in 53 minutes). A provider or a setting saved since is seen at once.
func TestMagpieModelsKeptWhileCatalogStays(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)
}
ids := func() map[string]bool {
out := map[string]bool{}
for _, m := range magpieModels("pi") {
out[m.ID] = true
}
return out
}
if got := ids(); !got["oa/gpt-5.5"] || !got["oa/gpt-5.4"] {
t.Fatalf("oa's models not listed: %v", got)
}
built := testing.AllocsPerRun(5, func() { buildModels("pi") })
kept := testing.AllocsPerRun(20, func() { magpieModels("pi") })
// StampNow's look at three files and the copy; a list built is far more
if kept > 200 || kept*10 > built {
t.Fatalf("a list already built took %.0f allocations, building it %.0f", kept, built)
}
// each caller's copy is its own: sorting or writing one changes nothing
// the next caller is given
mine := magpieModels("pi")
mine[0].ID = "mine"
for _, m := range magpieModels("pi") {
if m.ID == "mine" {
t.Fatalf("a caller's write reached the list kept: %+v", m)
}
}
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 !ids()["nb/x"] {
t.Fatal("a provider saved since is not listed")
}
s := settings.Load()
s.HiddenModels = map[string][]string{"pi": {"oa/gpt-5.4"}}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if ids()["oa/gpt-5.4"] {
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")
}
}