Codex writes a [projects."…"] table for every project it is trusted in;
the owner's config.toml has 10,933 of them in 1.5 MB. go-toml's decoder
checks each table against its siblings one at a time, so every decode
of the file cost ~0.3s of CPU, and six readers decoded it again each
time Codex wrote it, plus a whole-file check before each write magpie
made to it.
- edit.TOMLWithout drops [projects…] tables in a single unstable.Parser
pass. The agent's codexTopOf, sessions' readCodexConfig, provider's
readCodexStoreSetting, library's rtk sandbox, hooks rekey and MCP
entries read through it. None of them reads projects. A file that
doesn't parse is passed through unchanged, so it still fails.
- validateTOML checks definitions with the rest of the file and the
projects decoded apart, 128 projects to a decode. A bare [projects]
table, a dotted projects.… key outside the tables, or any error
decodes the whole file, so the error is the one it gives.
Run on mote (M5 Pro):
- BenchmarkCodexConfigDecode (10,933 projects): 283ms -> 7.6ms a decode.
- BenchmarkValidateCodexConfig: 52ms (the whole decode was 283ms).
- Startup CPU profile, first 70s, the real-account copy (the real
1,524,099-byte config.toml): 2.76s -> 2.26s of samples; go-toml
0.32s -> none sampled.
- go test ./internal/edit ./internal/sessions ./internal/provider
./internal/library ./internal/agent: all ok.
- go vet of the same five on darwin, GOOS=linux and GOOS=windows; gofmt -l clean.
Broken by hand:
- Decoding the batches into struct{} instead of a map:
TestValidateTOMLDefinitionsInGroups went red ("a key twice", "a
subtable then its key"), so the check keeps maps.
Not tried:
- the whole suite. Only the five packages that read the file were run.
- Now that projects is left out, a project table defined twice no longer
fails these readers' decode. Codex itself refuses such a file.
135 lines
3.7 KiB
Go
135 lines
3.7 KiB
Go
package edit
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
)
|
|
|
|
func TestTOMLWithoutDropsOnlyThatTable(t *testing.T) {
|
|
src := `model_provider = "magpie" # projects = no
|
|
profile = "p"
|
|
sqlite_home = "/s"
|
|
|
|
[projects."/a"]
|
|
trust_level = "trusted"
|
|
note = """
|
|
[profiles.fake]
|
|
model = "inside a string"
|
|
"""
|
|
|
|
[ projects . "/b" . sub ]
|
|
x = 1
|
|
|
|
[profiles.p]
|
|
model = "m"
|
|
|
|
[[projects.list]]
|
|
y = 2
|
|
|
|
[features]
|
|
projects_view = true
|
|
[model_providers.magpie]
|
|
name = "projects"
|
|
`
|
|
got := string(TOMLWithout([]byte(src), "projects"))
|
|
if strings.Contains(got, "trust_level") || strings.Contains(got, "x = 1") || strings.Contains(got, "y = 2") || strings.Contains(got, "inside a string") {
|
|
t.Fatalf("a projects table was kept:\n%s", got)
|
|
}
|
|
var want, have map[string]any
|
|
if err := toml.Unmarshal([]byte(src), &want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := toml.Unmarshal([]byte(got), &have); err != nil {
|
|
t.Fatalf("%v:\n%s", err, got)
|
|
}
|
|
delete(want, "projects")
|
|
if !reflect.DeepEqual(want, have) {
|
|
t.Fatalf("\nwant %v\nhave %v", want, have)
|
|
}
|
|
}
|
|
|
|
func TestTOMLWithoutLeavesTheRestAlone(t *testing.T) {
|
|
for _, src := range []string{
|
|
"",
|
|
"model = \"m\"\n[profiles.p]\nmodel = \"x\"\n",
|
|
"[projects.\"/a\"\nbroken", // a parse error: the decode after it fails the same
|
|
"projects.\"/a\".trust_level = \"trusted\"\n", // a dotted key, not a table
|
|
} {
|
|
if got := TOMLWithout([]byte(src), "projects"); string(got) != src {
|
|
t.Fatalf("%q became %q", src, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func manyProjects(n int) []byte {
|
|
var b strings.Builder
|
|
b.WriteString("model_provider = \"magpie\"\nprofile = \"p\"\n[features]\nmulti_agent = true\n")
|
|
for i := range n {
|
|
fmt.Fprintf(&b, "\n[projects.\"/Users/someone/workspace/projects/repo-%05d\"]\ntrust_level = \"trusted\"\n", i)
|
|
}
|
|
b.WriteString("\n[profiles.p]\nmodel = \"m\"\n")
|
|
return []byte(b.String())
|
|
}
|
|
|
|
func BenchmarkCodexConfigDecode(b *testing.B) {
|
|
raw := manyProjects(10933)
|
|
type top struct {
|
|
Provider string `toml:"model_provider"`
|
|
Profiles map[string]any `toml:"profiles"`
|
|
}
|
|
b.Run("whole", func(b *testing.B) {
|
|
for b.Loop() {
|
|
var c top
|
|
toml.Unmarshal(raw, &c)
|
|
}
|
|
})
|
|
b.Run("without", func(b *testing.B) {
|
|
for b.Loop() {
|
|
var c top
|
|
toml.Unmarshal(TOMLWithout(raw, "projects"), &c)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestValidateTOMLDefinitionsInGroups: a config of many projects is checked
|
|
// project by project, and a table or key defined twice anywhere is still
|
|
// refused, with the error the whole file gives.
|
|
func TestValidateTOMLDefinitionsInGroups(t *testing.T) {
|
|
good := manyProjects(200)
|
|
if err := validateTOMLDefinitions(good); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var whole map[string]any
|
|
for name, bad := range map[string]string{
|
|
"a project twice": "\n[projects.\"/Users/someone/workspace/projects/repo-00007\"]\ntrust_level = \"x\"\n",
|
|
"a key twice": "\n[projects.\"/n\"]\na = 1\na = 2\n",
|
|
"a subtable then its key": "\n[projects.\"/m\".s]\nb = 1\n[projects.\"/m\"]\ns = 2\n",
|
|
"the rest's table twice": "\n[features]\nz = 1\n",
|
|
"a bare projects table": "\n[projects]\n\"/Users/someone/workspace/projects/repo-00003\" = 1\n",
|
|
} {
|
|
raw := append(append([]byte{}, good...), bad...)
|
|
want := toml.Unmarshal(raw, &whole)
|
|
got := validateTOMLDefinitions(raw)
|
|
if want == nil || got == nil || got.Error() != want.Error() {
|
|
t.Errorf("%s: got %v, want %v", name, got, want)
|
|
}
|
|
}
|
|
top := append([]byte("projects.\"/Users/someone/workspace/projects/repo-00001\".trust_level = \"x\"\n"), good...)
|
|
if validateTOMLDefinitions(top) == nil {
|
|
t.Error("a dotted projects key clashing with a table passed")
|
|
}
|
|
}
|
|
|
|
func BenchmarkValidateCodexConfig(b *testing.B) {
|
|
raw := manyProjects(10933)
|
|
for b.Loop() {
|
|
if err := validateTOMLDefinitions(raw); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|