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) + } +}