fix: repair malformed JSON in edit_file tool call arguments#2452
Merged
dgageot merged 1 commit intodocker:mainfrom Apr 17, 2026
Merged
fix: repair malformed JSON in edit_file tool call arguments#2452dgageot merged 1 commit intodocker:mainfrom
dgageot merged 1 commit intodocker:mainfrom
Conversation
95c18e5 to
5b289dd
Compare
LLMs occasionally generate structurally invalid JSON for edit_file arguments when the text payload contains characters that confuse brace counting (YAML, Dockerfiles, shell scripts). The most common pattern is an extra closing brace or bracket near the end of the arguments. Go's json.Unmarshal scanner rejects this before any custom UnmarshalJSON method runs, so the existing double-serialization fix never fires for these cases. Add tryRepairEditFileJSON which iteratively removes the offending character at the json.SyntaxError offset (up to 3 rounds), handling extra }, extra ], stray backslash-n, and stray backslash before quotes. Replace NewHandler with a custom editFileHandler that calls ParseEditFileArgs (which does repair then unmarshal) before delegating to handleEditFile. Update all other call sites (ACP filesystem handler, TUI rendering) to use ParseEditFileArgs as well.
5b289dd to
092285f
Compare
dgageot
approved these changes
Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LLMs sometimes generate broken JSON when calling
edit_file. This happens because the text being edited (YAML files, Dockerfiles, shell scripts) contains braces, brackets, and escape characters that throw off the model's nesting depth tracking. The result is structurally invalid JSON that fails to parse, wasting a turn and forcing the model to retry.I looked at all
edit_filecalls across several sessions and found that 11 out of 53 calls (21%) had malformed arguments. All of them were close to valid — typically just one extra}or]in the wrong place.The existing
UnmarshalJSONfix for double-serializededits(#2144) doesn't help here because Go'sjson.Unmarshalscanner rejects the structurally invalid JSON before any custom unmarshaler runs.What this PR does
Adds
tryRepairEditFileJSONwhich, on ajson.SyntaxError, looks at the character at the error offset and removes it if it's a known garbage pattern. It loops up to 3 times to handle multiple issues in the same payload. The observed failure patterns from real sessions:}"}}]}instead of"}]}}]"}]]}instead of"}]}]\nbetween tokens"}\n]}\andn\before"{\"key"instead of{"key"\Since the repair needs to happen before
json.Unmarshal, theedit_filehandler now uses a customeditFileHandler()instead of the generictools.NewHandler, calling the new exportedParseEditFileArgsfunction directly. All other call sites (ACP filesystem handler, TUI rendering) are updated to useParseEditFileArgstoo.What it doesn't do
edit_fileonly, where the problem is most acute due to large text payloads{totally broken <<<>>>) still fails with a clear error