Codex's config.toml keeps a table for every project it was trusted in
(1.5 MB, ~11k tables on the owner's Mac), parsed into ~4.5 MB that stayed
for as long as magpie ran: the Codex app's route (codexAppRoute) looks at
it every 3s, four GetTOMLTop/GetTOMLTable calls a tick, window closed too.
Now what GetTOMLTop and GetTOMLTable answered is kept beside the file's
read (tomlAnswers, a new file is a new set), and the whole parse is let
go once nothing has used it for a minute, then the memory is returned.
The 3s route check answers from the kept keys and parses nothing; a key
not asked before parses the file again (9 ms for the 1.5 MB config).
filememo gains DropKind.
Found with a traced build on mote, real-account copy: 627 reads of
.codex/config.toml in 150 s, 4 every 3 s, all from
KeepCodexAppOnItsProvider -> codexAppRoute.
Measured on mote (real-account copy), tomlIdle set to 15 s in the
measuring builds only:
- heap at 120 s idle: parseTOMLFile 3.5 MB in t48 (origin/main), absent
in t50 (this change);
- t51 (with the return to the system) at 200 s: heap profile 58 MB,
footprint 116 MB; t48 at 200 s: 88 MB heap, 115 MB footprint (its
stats were let go at 20 s in that build).
Run on mote: go vet ./internal/edit ./internal/filememo ./internal/agent
(darwin, windows, linux with CGO off); go test ./internal/edit
./internal/filememo ./internal/agent ./internal/provider ok.
TestTOMLParseLetGoWhenIdle is red with the DropKind call taken out ("a
new key ... after 0 parses") and with the kept answers skipped
("answered keys parsed the file 1 times").
Not tried: the full suite; a real Codex app switching routes across the
drop.
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package edit
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/filememo"
|
|
)
|
|
|
|
// Once nothing has used a file's whole parse for tomlIdle it is let go,
|
|
// and what GetTOMLTop and GetTOMLTable answered stays: the Codex app's
|
|
// route, looked at every 3s, parses nothing again, while a key no one
|
|
// asked before parses the file again.
|
|
func TestTOMLParseLetGoWhenIdle(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("model_provider = \"magpie\"\nother = 2\n\n[model_providers.magpie]\nbase_url = \"http://x\"\n")
|
|
if v, ok := GetTOMLTop(p, "model_provider"); v != "magpie" || !ok {
|
|
t.Fatalf("model_provider = %q, %v", v, ok)
|
|
}
|
|
tb, _ := GetTOMLTable(p, "model_providers.magpie")
|
|
if tb["base_url"] != "http://x" {
|
|
t.Fatalf("table = %v", tb)
|
|
}
|
|
tb["base_url"] = "changed by a caller"
|
|
if _, ok := GetTOMLTop(p, "absent"); ok {
|
|
t.Fatal("absent key found")
|
|
}
|
|
tomlKept.last.Store(time.Now().Add(-2 * tomlIdle).UnixNano())
|
|
tomlIdleCheck()
|
|
|
|
n := tomlParses.Load()
|
|
for range 3 {
|
|
if v, _ := GetTOMLTop(p, "model_provider"); v != "magpie" {
|
|
t.Fatalf("model_provider after the let-go = %q", v)
|
|
}
|
|
if _, ok := GetTOMLTop(p, "absent"); ok {
|
|
t.Fatal("absent key found after the let-go")
|
|
}
|
|
if tb, _ := GetTOMLTable(p, "model_providers.magpie"); tb["base_url"] != "http://x" {
|
|
t.Fatalf("table after the let-go = %v", tb)
|
|
}
|
|
}
|
|
if got := tomlParses.Load() - n; got != 0 {
|
|
t.Fatalf("answered keys parsed the file %d times", got)
|
|
}
|
|
if v, _ := GetTOMLTop(p, "other"); v != "2" || tomlParses.Load()-n != 1 {
|
|
t.Fatalf("a new key = %q after %d parses; want the file parsed again once it was let go", v, tomlParses.Load()-n)
|
|
}
|
|
|
|
write("model_provider = \"openai\"\n")
|
|
if v, _ := GetTOMLTop(p, "model_provider"); v != "openai" {
|
|
t.Fatalf("after a change model_provider = %q", v)
|
|
}
|
|
if tb, err := GetTOMLTable(p, "model_providers.magpie"); tb != nil || err != nil {
|
|
t.Fatalf("after a change the table = %v, %v", tb, err)
|
|
}
|
|
}
|