agent: the Codex app's route opens config.toml to see it can be read, not reads it whole
codexAppRoute runs every 3s for as long as magpie does (the Codex app's app-server watcher). It read the whole of Codex's config.toml with os.ReadFile only to learn the file could be read; the keys it uses come from filememo, which parses the file once per change. Codex keeps every project's trust level in that file, and the owner's is 1.5 MB, so this was 1.55 GB of the 33 GB a live magpie allocated in 53 minutes (heap profile: KeepCodexAppOnItsProvider → codexAppStep → os.ReadFile). It is now opened and closed: a file that can't be opened is still an unknown route, a missing one still a known one. Run on mote: - TestCodexAppRouteReadsNoWholeConfig (new): a 1.6 MB config, 10 looks allocate under 1 MB; a config with mode 0 gives no route. - Broken by hand: ReadFile put back, red (14.4 MB for 10 looks); the unreadable check dropped, red. - go test -run CodexAppRoute ./internal/agent: ok. - go test -count=1 -timeout 45m ./internal/agent ok (185.3s), at this tree rebased on 4a7d1c63a; gofmt -l clean; go vet ./internal/agent for darwin, linux and windows ok
This commit is contained in:
@@ -87,7 +87,12 @@ func codexAppRoute() (route string, out, known bool) {
|
||||
defer filememo.Hold()()
|
||||
home, _ := homes()
|
||||
a := codex(home)
|
||||
if _, err := os.ReadFile(a.Path); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
// opened, not read: a config.toml of 1.5 MB (Codex keeps every
|
||||
// project's trust level in it) read whole every 3s was 1.55 GB of a
|
||||
// live magpie's 33 GB in 53 minutes; the keys below come from filememo
|
||||
if f, err := os.Open(a.Path); err == nil {
|
||||
f.Close()
|
||||
} else if !errors.Is(err, fs.ErrNotExist) {
|
||||
return "", false, false
|
||||
}
|
||||
out = a.AppOut != nil && a.AppOut()
|
||||
|
||||
@@ -2,10 +2,14 @@ package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
@@ -147,3 +151,44 @@ func TestCodexAppRouteKeepsThroughListFlaps(t *testing.T) {
|
||||
t.Errorf("a catalog that can't be read changed the route:\n%s\nto\n%s", before, r)
|
||||
}
|
||||
}
|
||||
|
||||
// Asked every 3s, the route reads Codex's config.toml through filememo
|
||||
// alone: Codex keeps every project's trust level there, and a live 1.5 MB
|
||||
// one read whole on each look was 1.55 GB in 53 minutes. A config that
|
||||
// can't be read is still an unknown route.
|
||||
func TestCodexAppRouteReadsNoWholeConfig(t *testing.T) {
|
||||
var cfg strings.Builder
|
||||
cfg.WriteString("model = \"gpt-5.5\"\n")
|
||||
for i := range 20000 {
|
||||
fmt.Fprintf(&cfg, "[projects.\"/Users/someone/work/project-%05d\"]\ntrust_level = \"trusted\"\n", i)
|
||||
}
|
||||
home, _ := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, cfg.String())
|
||||
path := filepath.Join(home, ".codex", "config.toml")
|
||||
if fi, err := os.Stat(path); err != nil || fi.Size() < 1<<20 {
|
||||
t.Fatalf("config of %v bytes: %v", fi.Size(), err)
|
||||
}
|
||||
// written a while ago, as the live one: filememo compares the bytes of
|
||||
// a file written just now
|
||||
old := time.Now().Add(-time.Hour)
|
||||
os.Chtimes(path, old, old)
|
||||
if _, _, known := codexAppRoute(); !known {
|
||||
t.Fatal("route not known")
|
||||
}
|
||||
var m0, m1 runtime.MemStats
|
||||
runtime.ReadMemStats(&m0)
|
||||
for range 10 {
|
||||
codexAppRoute()
|
||||
}
|
||||
runtime.ReadMemStats(&m1)
|
||||
if n := m1.TotalAlloc - m0.TotalAlloc; n > 1<<20 {
|
||||
t.Fatalf("10 looks at an unchanged config allocated %d bytes", n)
|
||||
}
|
||||
if runtime.GOOS == "windows" || os.Getuid() == 0 {
|
||||
return // a mode doesn't stop the read
|
||||
}
|
||||
os.Chmod(path, 0)
|
||||
defer os.Chmod(path, 0o600)
|
||||
if _, _, known := codexAppRoute(); known {
|
||||
t.Fatal("a config that can't be read gave a route")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user