Files
magpie/internal/agent/codex_big_config_test.go
T
yetone 2597c96365 agent: Codex's config.toml is decoded once a read for a sync's own looks
A sync to Codex decoded the whole config.toml again for each thing it
checked: codexMultiAgentV2On, putThreadTables, codexEnableEfforts and
codexAsyncNow each ran toml.Unmarshal over the file. Codex writes a
[projects."…"] table for every folder it was opened in; the owner's
config is 1.5 MB with 10,932 tables, and with 10,000 such tables one
sync allocated 21 MB.

codexTopOf decodes only the top tables these look at (features,
model_providers, desktop, profile, profiles) and keeps the result in
filememo by the file's mod time and size, so a sync and the polls
after it share one decode until the file changes. A missing file is
an empty config, as before.

TestCodexSyncDecodesTheConfigOnce: 10,000 project tables, one sync
to settle, then five more; each must allocate under 12x the file and
leave config.toml unwritten. Now ~5 MB a sync. Red on the old code:
51,257 KB a sync over an 868 KB file, past the 10,421 KB bound.

Run at this head on mote: gofmt clean; go vet (darwin, GOOS=windows,
GOOS=linux) ok; go test -timeout 45m ./internal/agent
./internal/gateway ok.
2026-11-01 04:06:09 +08:00

57 lines
1.7 KiB
Go

package agent
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// A config.toml with one table for each project Codex was opened in grows
// to megabytes (10,932 tables, 1.5 MB on one user's Mac). A sync that finds
// nothing to change decodes only the few tables it reads, not the whole
// file once for each of them: 21 MB a sync here before, about 5 MB after.
func TestCodexSyncDecodesTheConfigOnce(t *testing.T) {
var b strings.Builder
b.WriteString("model = \"gpt-5.5\"\n\n")
for i := range 10000 {
fmt.Fprintf(&b, "[projects.\"/Users/someone/workspace/projects/project-%d/sub\"]\ntrust_level = \"trusted\"\n\n", i)
}
home, _ := codexHome(t, "", b.String())
if err := codex(home).Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
SyncCatalog()
// Written an hour ago, so a read is the steady one, not a just-written
// file read again to compare.
old := time.Now().Add(-time.Hour)
cfg := filepath.Join(home, ".codex", "config.toml")
for _, f := range []string{cfg, filepath.Join(home, ".codex", "magpie-models.json")} {
if err := os.Chtimes(f, old, old); err != nil {
t.Fatal(err)
}
}
st, err := os.Stat(cfg)
if err != nil {
t.Fatal(err)
}
const syncs = 5
var m0, m1 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m0)
for range syncs {
SyncCatalog()
}
runtime.ReadMemStats(&m1)
per := (m1.TotalAlloc - m0.TotalAlloc) / syncs
if limit := uint64(12 * st.Size()); per > limit {
t.Errorf("a sync of a %d KB config allocates %d KB, over %d KB", st.Size()>>10, per>>10, limit>>10)
}
if after, _ := os.Stat(cfg); !after.ModTime().Equal(st.ModTime()) {
t.Errorf("a sync with nothing to change wrote config.toml")
}
}