feat: add fuzzy matching fallbacks for edit_file tool#2296
Open
trungutt wants to merge 1 commit intodocker:mainfrom
Open
feat: add fuzzy matching fallbacks for edit_file tool#2296trungutt wants to merge 1 commit intodocker:mainfrom
trungutt wants to merge 1 commit intodocker:mainfrom
Conversation
3cea383 to
713fb29
Compare
LLMs frequently confuse JSON-level escaping with content-level escaping when generating edit_file tool calls — e.g. sending oldText with plain quotes when the file contains backslash-escaped quotes. This causes repeated failures that the model cannot self-correct. Replace the bare strings.Contains check with a cascading chain of replacer strategies (exact → line-trimmed → escape-normalized → whitespace-normalized → indentation-flexible → trimmed-boundary). The first strategy to find exactly one unique match wins; ambiguous matches are rejected. On failure, error messages now include a diagnostic hint showing the closest near-miss and the first differing line, helping the model self-correct on retry. Both the builtin and ACP edit_file handlers share the same FindMatch function.
713fb29 to
b341b6e
Compare
Member
Contributor
Author
|
I open the PR again as edit_file still gives error on diff detection. We need to fix that, either by this PR or other PRs |
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 frequently confuse JSON-level escaping with content-level escaping when generating
edit_filetool calls — e.g. sendingecho "brew install failed"asoldTextwhen the file containsecho \"brew install failed\". The current exactstrings.Containscheck fails, and the model repeats the same mistake on retry.Replace bare
strings.Containswith a cascading replacer chain (inspired by opencode's edit tool). Both the builtin and ACPedit_filehandlers share the sameFindMatchfunction.How it works
FindMatchtries strategies in order — the first to find exactly one unique match wins:\"vs",\nvs newline,\tvs tab,\$vs$If a strategy finds multiple matches it is skipped. If no strategy matches, an error is returned. The chain is designed to be extended with more strategies as new failure patterns are observed.
Motivation
Observed in production: a session attempted to edit a Dockerfile three times, failing each time on the same
echo \"brew install failed\"vsecho "brew install failed"mismatch. The model re-read the file and retried with identical arguments because it could not self-correct the escaping. It eventually gave up and usedwrite_fileto overwrite the entire file.With this change, attempt 1 would succeed via the escape-normalized replacer.