edit: malformed JSON configs stay untouched when agents connect (#1242)

Validate the complete comment-stripped document before object and array edits, including previews. Regression tests fail on the old implementation because edits and VS Code Connect succeed and alter malformed files. Targeted edit and VS Code tests pass in an isolated HOME.
This commit is contained in:
Alex
2026-10-08 11:52:20 +08:00
committed by GitHub
parent 814dca9390
commit 2d9db1b60d
5 changed files with 115 additions and 0 deletions
+6
View File
@@ -42,6 +42,9 @@ func SetJSONItem(path string, where map[string]string, value any) error {
return WriteAtomic(path, []byte("[\n\t"+string(v)+"\n]\n"))
}
stripped := jsonc.ToJSONInPlace(append([]byte(nil), raw...))
if !json.Valid(stripped) {
return fmt.Errorf("%s: invalid JSON/JSONC", path)
}
root, open, close := topArray(stripped)
if close < 0 {
return fmt.Errorf("%s: top level is not a JSON array", path)
@@ -85,6 +88,9 @@ func DelJSONItem(path string, where map[string]string) error {
changed := false
for {
stripped := jsonc.ToJSONInPlace(append([]byte(nil), raw...))
if !json.Valid(stripped) {
return fmt.Errorf("%s: invalid JSON/JSONC", path)
}
r, ok := findItem(stripped, where)
if !ok {
break