Files
magpie/internal/provider/codex_subagent_kept_test.go
T
yetone ef63ef4a22 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.
2026-11-01 06:46:50 +08:00

53 lines
1.5 KiB
Go

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
}