agent: Codex's config holds only keys Codex knows, checked against its schema (#6828)

hatanokokoroyo (#6828) and Nietzsche_Li on X: Codex 0.169 warns on
every launch that `disable_response_storage` is ignored, and
`codex --strict-config` refuses to start, with magpie 0.1.4291.

The key isn't magpie's. `git log --all -S disable_response_storage`
finds it in no commit, and nothing in the tree writes it. Codex removed
it in openai/codex#3212 (0.30.0, 2025-09); magpie's Codex paths assume
0.133 or later, so none needs it. CC Switch wrote it in its Codex
presets until 4.0.28, which stops writing it and clears it on the next
switch (cc-switch#7524), and relays' setup pages still tell users to
add it. magpie's CC Switch import reads only the model provider. Since
magpie can't show it wrote the line, the user's line is left as it is.

So this adds the guard: testdata holds Codex rust-v0.169.0's own
config.schema.json (descriptions, defaults and enums stripped, 86KB),
and TestCodexConfigHoldsOnlyKeysCodexKnows walks it over the config
magpie writes, in place and as the magpie.config.toml profile, from an
empty config and from a CC Switch one, after setting a model, an
effort, subagents and memories and a sync. A key Codex has removed or
never had fails it. A user config holding the line keeps it, once, and
it is the only unknown key.

Run at this tree (mote): go test -run TestCodexConfigHoldsOnlyKeysCodexKnows
-v ./internal/agent PASS; go test ./internal/agent ok; go test . ok;
GOOS=linux and GOOS=windows go vet ./internal/agent ok; gofmt -l clean.
Broken by hand: codex.go writing disable_response_storage beside the
effort: the test went red naming the key; restored, green.
Not run: the whole suite (a test file and testdata only, in one
package).
This commit is contained in:
yetone
2026-11-16 15:09:18 +08:00
parent afd2df09b9
commit c4d64a70b2
2 changed files with 179 additions and 0 deletions
+178
View File
@@ -0,0 +1,178 @@
package agent
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/pelletier/go-toml/v2"
)
// codexSchema is Codex's own config.schema.json (codex-rs/core, at
// rust-v0.169.0), its descriptions and defaults left out. Codex warns on
// every launch about a key it doesn't know ("`disable_response_storage`
// is ignored", #6828), and `codex --strict-config` refuses to start.
func codexSchema(t *testing.T) map[string]any {
t.Helper()
b, err := os.ReadFile(filepath.Join("testdata", "codex-config.schema.json"))
if err != nil {
t.Fatal(err)
}
var s map[string]any
if err := json.Unmarshal(b, &s); err != nil {
t.Fatal(err)
}
return s
}
// codexUnknownKeys lists the keys of v that the schema node s closes
// out: where an object's schema has additionalProperties false, every key
// must be one of its properties.
func codexUnknownKeys(root, s map[string]any, at string, v any) []string {
if ref, ok := s["$ref"].(string); ok {
def, _ := root["definitions"].(map[string]any)[strings.TrimPrefix(ref, "#/definitions/")].(map[string]any)
if def == nil {
return []string{at + ": schema has no " + ref}
}
return codexUnknownKeys(root, def, at, v)
}
var bad []string
for _, sub := range asSchemas(s["allOf"]) {
bad = append(bad, codexUnknownKeys(root, sub, at, v)...)
}
for _, one := range []string{"anyOf", "oneOf"} {
subs := asSchemas(s[one])
if len(subs) == 0 {
continue
}
var least []string
for i, sub := range subs {
b := codexUnknownKeys(root, sub, at, v)
if i == 0 || len(b) < len(least) {
least = b
}
}
bad = append(bad, least...)
}
obj, ok := v.(map[string]any)
if !ok {
return bad
}
props, _ := s["properties"].(map[string]any)
for k, sub := range obj {
where := strings.TrimPrefix(at+"."+k, ".")
if p, ok := props[k].(map[string]any); ok {
bad = append(bad, codexUnknownKeys(root, p, where, sub)...)
continue
}
switch extra := s["additionalProperties"].(type) {
case bool:
if !extra && (props != nil || s["type"] == "object") {
bad = append(bad, where)
}
case map[string]any:
bad = append(bad, codexUnknownKeys(root, extra, where, sub)...)
}
}
return bad
}
func asSchemas(v any) []map[string]any {
var out []map[string]any
list, _ := v.([]any)
for _, x := range list {
if m, ok := x.(map[string]any); ok {
out = append(out, m)
}
}
return out
}
func codexKeysCodexDoesntKnow(t *testing.T, root map[string]any, file string) []string {
t.Helper()
b, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
var doc map[string]any
if err := toml.Unmarshal(b, &doc); err != nil {
t.Fatalf("%s: %v\n%s", file, err, b)
}
bad := codexUnknownKeys(root, root, "", doc)
slices.Sort(bad)
return bad
}
// Every key magpie writes into Codex's config is one Codex knows: none it
// has removed (disable_response_storage went in Codex 0.30, #6828), and
// none it never had. Checked against Codex's own schema, for the config
// magpie connects in place and for the magpie.config.toml profile, with a
// model, an effort, subagents, memories and a sync. A key the user wrote
// stays theirs: magpie neither adds one nor takes it out.
func TestCodexConfigHoldsOnlyKeysCodexKnows(t *testing.T) {
root := codexSchema(t)
// the schema itself closes the top level, and doesn't know the key
// Codex removed: else this test could pass with anything written
if bad := codexUnknownKeys(root, root, "", map[string]any{"disable_response_storage": true, "model": "m"}); !slices.Equal(bad, []string{"disable_response_storage"}) {
t.Fatalf("schema check: %v", bad)
}
const auth = `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`
for _, tc := range []struct{ name, scope, config string }{
{"empty", "", ""},
{"cc switch", "", ccSwitchCodex},
{"profile", "profile", ""},
} {
t.Run(tc.name, func(t *testing.T) {
home, read := codexHome(t, auth, tc.config)
cx := codex(home)
if tc.scope != "" {
if err := cx.Field("scope").Set(tc.scope); err != nil {
t.Fatal(err)
}
cx = codex(home)
}
for _, set := range [][2]string{{"model", "fake/m1"}, {"effort", "high"}, {"subagent", "fake/m1"}, {"subagent_effort", "low"}, {"memories", "fake/m1"}} {
if cx.Field(set[0]) == nil {
if tc.scope == "" {
t.Fatalf("no field %s", set[0])
}
continue // the profile has fewer
}
if err := cx.Apply(set[0], set[1]); err != nil {
t.Fatalf("%s: %v", set[0], err)
}
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
for _, f := range []string{filepath.Join(home, ".codex", "config.toml"), cx.Path} {
if bad := codexKeysCodexDoesntKnow(t, root, f); len(bad) > 0 {
b, _ := os.ReadFile(f)
t.Errorf("%s holds keys Codex doesn't know: %v\n%s", filepath.Base(f), bad, b)
}
}
if tc.scope == "" && !strings.Contains(read(), `model = "fake/m1"`) {
t.Fatalf("not connected:\n%s", read())
}
})
}
// the user's own line (CC Switch before 4.0.28 wrote it, and relays'
// setup pages still say to) is left as it is, and only it is unknown
home, read := codexHome(t, auth, "disable_response_storage = true\n"+ccSwitchCodex)
cx := codex(home)
if err := cx.Apply("model", "fake/m1"); err != nil {
t.Fatal(err)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if bad := codexKeysCodexDoesntKnow(t, root, filepath.Join(home, ".codex", "config.toml")); !slices.Equal(bad, []string{"disable_response_storage"}) {
t.Fatalf("unknown keys %v, want the user's line only:\n%s", bad, read())
}
if n := strings.Count(read(), "disable_response_storage = true"); n != 1 {
t.Fatalf("user's line %d times:\n%s", n, read())
}
}
File diff suppressed because one or more lines are too long