feat(agent): add fuzzy whitespace matching to edit_files tool#22446
Merged
feat(agent): add fuzzy whitespace matching to edit_files tool#22446
Conversation
Inspired by openai/codex's apply_patch implementation, this changes the edit_files search-and-replace to use a cascading match strategy when the exact search string isn't found: 1. Exact substring match (byte-for-byte) - existing behavior 2. Line-by-line match ignoring trailing whitespace 3. Line-by-line match ignoring all leading/trailing whitespace (indentation-tolerant) This fixes the common issue where the LLM generates a search string with spaces but the file uses tabs (or vice versa), or gets the indentation depth wrong. Previously these edits would silently fail, forcing the agent into retry loops with cat -A to diagnose whitespace. The icholy/replace streaming transformer dependency is removed in favor of reading the file into memory and applying replacements directly.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Inspired by openai/codex's
apply_patchimplementation, this changes theedit_filessearch-and-replace to use a cascading match strategy when the exact search string isn't found:Problem
When the chat agent uses
edit_files, it generates a search string that must match the file content exactly. LLMs frequently get whitespace wrong:When this happens, the edit silently does nothing, and the agent falls into a retry loop using
cat -Ato diagnose the exact whitespace characters.Solution
Adopted the same cascading fuzzy match strategy that openai/codex uses in
seek_sequence.rs:TrimRighteach line before comparing (trailing whitespace tolerance)TrimSpaceeach line before comparing (full indentation tolerance)When a fuzzy match is found, the matched lines in the original file are replaced with the replacement text. This preserves surrounding content exactly.
Changes
agent/agentfiles/files.go: Replacedicholy/replacestreaming transformer with in-memoryfuzzyReplace+ helper functions (seekLines,spliceLines)agent/agentfiles/files_test.go: Added 6 new test cases covering trailing whitespace, tabs-vs-spaces, different indent depths, exact match preference, no-match behavior, and mixed whitespace multiline editsicholy/replacedependency from go.mod/go.sum