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.
This commit is contained in:
yetone
2026-11-01 06:46:50 +08:00
parent e4384fc3cd
commit ef63ef4a22
3 changed files with 70 additions and 6 deletions
+11 -4
View File
@@ -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 {
@@ -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
}
+7 -2
View File
@@ -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 }