From dbaf9d68cc82047c5635132164a1c6267b32fc6a Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 04:09:55 +0800 Subject: [PATCH] edit: TOMLTables' list is kept with the file's other answers The Codex agent's drift lists its config's tables on every look (TOMLTables), and the list came from the whole parse. Over a Codex config.toml with ~11k projects, the drift polls of an open page kept that parse (3 to 5 MB) alive and it was never let go. GetTOMLTop and GetTOMLTable answers were already kept beside the parse. The table list is now kept there too, copied out of the file's text, so the idle drop can let the parse go while a page is open. Run (mote, at 648cd3fdb + this; mote's load was ~29 on 18 cores and mica's ~31 on 12, so neither had room and the run went to mote): - go vet ./internal/edit; go test -count=1 ./internal/edit ./internal/agent ./internal/library: ok. - New TestTOMLTablesKeptWhenIdle: red on the old toml.go ("listing the tables parsed the file 1 times"), green with this. - Real-account sandbox with the owner's real probe history, web mode, page open in Playwright Chromium, heap at 2m32s, alternating builds: w39 (before) live heap 80.2 / 80.8 MB, tomlFileOf holding 6 MB; w40 (this) live heap 76.4 / 76.7 MB, no tomlFileOf in the heap. The footprint is noise (127-136 MB in both). Not run: the full suite and the GUI suite (no GUI change). --- internal/edit/toml.go | 15 ++++++++++++ internal/edit/toml_idle.go | 28 ++++++++++++++++++++-- internal/edit/toml_idle_test.go | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/internal/edit/toml.go b/internal/edit/toml.go index 8804a97b5..64616cc1e 100644 --- a/internal/edit/toml.go +++ b/internal/edit/toml.go @@ -30,11 +30,26 @@ import ( // caller, so read only (an append makes a copy): a Codex config.toml with a // table for each of 11,000 projects had it made again for each look, 46 MB // of what twenty window polls allocated. +// +// The list is kept beside the file's other answers, so a poll that only +// lists tables doesn't hold the whole parse: the Codex agent's drift +// listed its config's tables on every look, and over a Codex config.toml +// of ~11k projects that kept its 3 to 5 MB parse for as long as a page +// was open. func TOMLTables(path string) ([]string, error) { + a := tomlAnswersOf(path) + if a != nil { + if names, ok := a.getNames(); ok { + return names, nil + } + } doc, err := tomlFileOf(path) if err != nil { return nil, err } + if a != nil { + a.setNames(doc.names) + } return doc.names, nil } diff --git a/internal/edit/toml_idle.go b/internal/edit/toml_idle.go index 4cbb254af..6d94a24ea 100644 --- a/internal/edit/toml_idle.go +++ b/internal/edit/toml_idle.go @@ -3,6 +3,7 @@ package edit import ( "maps" "runtime/debug" + "strings" "sync" "sync/atomic" "testing" @@ -69,8 +70,10 @@ func dropTOMLParses() { // tomlAnswers, empty. type tomlAnswers struct { sync.Mutex - top map[string]tomlTop - tables map[string]map[string]string // nil for a table the file hasn't + top map[string]tomlTop + tables map[string]map[string]string // nil for a table the file hasn't + names []string // TOMLTables' answer, once haveNames + haveNames bool } type tomlTop struct { @@ -125,3 +128,24 @@ func (a *tomlAnswers) setTable(name string, t map[string]string) { } a.tables[name] = maps.Clone(t) } + +// getNames is TOMLTables' kept answer, shared, so read only. +func (a *tomlAnswers) getNames() ([]string, bool) { + a.Lock() + defer a.Unlock() + return a.names, a.haveNames +} + +// setNames keeps a copy of names that holds no piece of the file's text. +func (a *tomlAnswers) setNames(names []string) { + var kept []string + if names != nil { + kept = make([]string, len(names)) + for i, n := range names { + kept[i] = strings.Clone(n) + } + } + a.Lock() + defer a.Unlock() + a.names, a.haveNames = kept, true +} diff --git a/internal/edit/toml_idle_test.go b/internal/edit/toml_idle_test.go index d0eb01a1e..f50aedaf2 100644 --- a/internal/edit/toml_idle_test.go +++ b/internal/edit/toml_idle_test.go @@ -3,6 +3,7 @@ package edit import ( "os" "path/filepath" + "slices" "testing" "time" @@ -68,3 +69,44 @@ func TestTOMLParseLetGoWhenIdle(t *testing.T) { t.Fatalf("after a change the table = %v, %v", tb, err) } } + +// TOMLTables' list is kept as the other answers are: a poll that only +// lists a file's tables parses nothing once the whole parse is let go, +// and a change to the file is listed. +func TestTOMLTablesKeptWhenIdle(t *testing.T) { + tomlKept.off = false + t.Cleanup(func() { tomlKept.off = true }) + p := filepath.Join(t.TempDir(), "config.toml") + old := time.Now().Add(-time.Hour) + write := func(s string) { + if err := os.WriteFile(p, []byte(s), 0o600); err != nil { + t.Fatal(err) + } + old = old.Add(time.Second) + os.Chtimes(p, old, old) + filememo.Forget() + } + write("a = 1\n\n[projects.\"/x\"]\ntrust_level = \"trusted\"\n\n[[arr]]\nb = 1\n\n[model_providers.magpie]\nbase_url = \"http://x\"\n") + want := []string{`projects."/x"`, "model_providers.magpie"} + names, err := TOMLTables(p) + if err != nil || !slices.Equal(names, want) { + t.Fatalf("tables = %q, %v; want %q", names, err, want) + } + tomlKept.last.Store(time.Now().Add(-2 * tomlIdle).UnixNano()) + tomlIdleCheck() + + n := tomlParses.Load() + for range 3 { + if names, _ := TOMLTables(p); !slices.Equal(names, want) { + t.Fatalf("tables after the let-go = %q", names) + } + } + if got := tomlParses.Load() - n; got != 0 { + t.Fatalf("listing the tables parsed the file %d times", got) + } + + write("[other]\nc = 1\n") + if names, _ := TOMLTables(p); !slices.Equal(names, []string{"other"}) { + t.Fatalf("after a change the tables = %q", names) + } +}