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 }