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).
This commit is contained in:
yetone
2026-11-13 04:09:59 +08:00
parent 1281046a02
commit dbaf9d68cc
3 changed files with 83 additions and 2 deletions
+15
View File
@@ -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
}
+26 -2
View File
@@ -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
}
+42
View File
@@ -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)
}
}