From ef63ef4a2284560310979b2e5c76e33008f4e7bf Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 06:46:31 +0800 Subject: [PATCH] provider: Codex's subagent models are kept while the catalog is held Codex's Agents row asked CodexSubagentModels each poll, and each time it took a copy of Codex's whole catalog (CatalogFor) and made a list from it: 21.8 MB of forty polls of the Agents, Providers and Drift answers with real accounts. The list is now kept as the catalog is (keptOf, per provider.StampNow and keptFor), read from the catalog's own kept lists (catalogKept) without a copy, and clipped so a caller appending makes a list of its own. Its one caller, codex.go, only ranges over it. TestCodexSubagentModelsKept: 200 models of a relay under Hold; asking again allocates a quarter or less of what building them does. Run on mote at c1457f2ee plus this change: - gofmt; go vet darwin, windows, linux: ok - TestCodexSubagentModelsKept alone 3/3 ok - broken by hand (CodexSubagentModels builds each time): red, "allocated 1089523 bytes, building them 1089513" - go test ./internal/provider ./internal/agent -timeout 45m: agent ok (231.7s); provider ok but for this test's first version, whose allocation-count bar failed (StampNow's stats count too); the test now compares bytes and was run alone and broken again as above, not in the whole package again - real accounts, sandbox HOME, network blocked, 40 polls, against c1457f2ee: CodexSubagentModels 21.8 MB -> not in the profile; codexIn.func84 22.3 MB -> gone; Picks 135 -> 92 MB; state and drift answers byte for byte the same, providers the same but for its fetched times; footprint peak 135 MB, RSS at end 178 MB Not run: the GUI suite (no GUI file changed), Windows and Linux test runs. Rebased onto e4384fc3c (gateway, redact and GUI commits, none in internal/provider or internal/agent); not run again there. --- internal/provider/codex_subagent.go | 15 ++++-- internal/provider/codex_subagent_kept_test.go | 52 +++++++++++++++++++ internal/provider/visible.go | 9 +++- 3 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 internal/provider/codex_subagent_kept_test.go diff --git a/internal/provider/codex_subagent.go b/internal/provider/codex_subagent.go index dcc094ed4..b15af15e7 100644 --- a/internal/provider/codex_subagent.go +++ b/internal/provider/codex_subagent.go @@ -2,6 +2,7 @@ package provider import ( "fmt" + "slices" "strings" "github.com/yetone/magpie/internal/settings" @@ -31,18 +32,24 @@ func CodexSubagentModel() string { // may be put on, the ones Codex's own lists show (a model taken out of // them, or not picked, isn't offered here either): the ChatGPT accounts' // first, as they open a task sealed for them too, then the rest in the -// catalog's order. +// catalog's order. The list is kept while the catalog is held, only for +// reading: Codex's Agents row asked it each poll, a copy of Codex's whole +// catalog and the list made from it, 17 MB of forty polls with real +// accounts. func CodexSubagentModels() []Entry { + return keptOf("codex-subagent-models", codexSubagentModels) +} + +func codexSubagentModels() []Entry { var chatgpt, rest []Entry - shown, _ := CatalogFor("codex") - for _, e := range shown { + for _, e := range catalogKept("codex").shown { if chatGPTEntry(e) { chatgpt = append(chatgpt, e) } else { rest = append(rest, e) } } - return append(chatgpt, rest...) + return slices.Clip(append(chatgpt, rest...)) } func chatGPTEntry(e Entry) bool { diff --git a/internal/provider/codex_subagent_kept_test.go b/internal/provider/codex_subagent_kept_test.go new file mode 100644 index 000000000..86608665e --- /dev/null +++ b/internal/provider/codex_subagent_kept_test.go @@ -0,0 +1,52 @@ +package provider + +import ( + "path/filepath" + "runtime" + "strconv" + "testing" + "time" +) + +// Codex's subagent models are kept while the catalog is held: its Agents +// row asked them each poll, a copy of Codex's whole catalog and a list +// made from it each time (17 MB of forty polls with real accounts). +func TestCodexSubagentModelsKept(t *testing.T) { + isolate(t) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + defer func(d, k time.Duration) { heldFor, keptFor = d, k }(heldFor, keptFor) + heldFor, keptFor = time.Minute, time.Minute + var models []string + for i := range 200 { + models = append(models, "m"+strconv.Itoa(i)) + } + if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil { + t.Fatal(err) + } + defer Hold()() + if n := len(CodexSubagentModels()); n < 200 { + t.Fatalf("Codex's subagents may go on %d models", n) + } + built := allocatedBy(func() { codexSubagentModels() }) + kept := allocatedBy(func() { CodexSubagentModels() }) + if kept*4 > built { + t.Fatalf("asking Codex's subagent models again allocated %d bytes, building them %d", kept, built) + } +} + +// allocatedBy is how many bytes a call of f allocates, on average of ten. +func allocatedBy(f func()) uint64 { + f() + var a, b runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&a) + for range 10 { + f() + } + runtime.ReadMemStats(&b) + return (b.TotalAlloc - a.TotalAlloc) / 10 +} diff --git a/internal/provider/visible.go b/internal/provider/visible.go index 6538ab550..2958c613c 100644 --- a/internal/provider/visible.go +++ b/internal/provider/visible.go @@ -71,12 +71,17 @@ func described() bool { return Described != nil && Described() } // catalog was ~140 MB of what twenty polls allocated. Each caller gets // its own copy of the lists. func CatalogFor(agent string) (shown, hidden []Entry) { - c := keptOf("catalog-for\x00"+agent, func() catalogLists { + c := catalogKept(agent) + return slices.Clone(c.shown), slices.Clone(c.hidden) +} + +// catalogKept is CatalogFor's lists as kept, only for reading. +func catalogKept(agent string) catalogLists { + return keptOf("catalog-for\x00"+agent, func() catalogLists { catalogsBuilt.Add(1) s, h := catalogFor(agent) return catalogLists{slices.Clip(s), slices.Clip(h)} }) - return slices.Clone(c.shown), slices.Clone(c.hidden) } type catalogLists struct{ shown, hidden []Entry }