[BUG] Edit tool may modify the wrong occurrence when quote-normalized matches are ambiguous
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
The Edit tool appears to support quote normalization when matching old_string against file contents, so straight quotes and curly quotes can be treated as equivalent.
However, the uniqueness check seems to count occurrences using the selected raw actualOldString, rather than counting quote-normalized equivalent matches. This can allow an ambiguous edit to pass validation and silently modify the wrong occurrence.
What Should Happen?
as description
Error Messages/Logs
Steps to Reproduce
Minimal example
File content:
message1 = "hello"
message2 = “hello”
Suppose the model intends to modify message2, but emits a straight-quote search string:
{
"file_path": "/path/to/file.py",
"old_string": "\"hello\"",
"new_string": "\"world\"",
"replace_all": false
}
Potential failure mode
- The tool first checks for an exact match.
- The straight-quote
"hello"matchesmessage1.
- Since raw
"hello"appears only once, the uniqueness check passes.
- The tool modifies
message1, even though the intended target wasmessage2.
A more subtle variant is when both file occurrences use visually similar but different curly quotes:
message1 = ”hello“
message2 = “hello”
Suppose the model intends to modify message2. If the model emits:
{
"old_string": "\"hello\"",
"new_string": "\"world\"",
"replace_all": false
}
then exact matching may fail, quote-normalized matching may find the first normalized-equivalent occurrence, and the later uniqueness check may still only count the selected raw quote form. That can miss the fact that multiple normalized-equivalent candidates exist.
Claude Model
None
Is this a regression?
Yes, this worked in a previous version
Last Working Version
_No response_
Claude Code Version
2.1.177 (Claude Code)
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
Suggested fix
When quote normalization is involved, perform uniqueness validation over normalized content:
const normalizedFile = normalizeQuotes(file)
const normalizedSearch = normalizeQuotes(old_string)
const normalizedMatchCount = countOccurrences(normalizedFile, normalizedSearch)
if (normalizedMatchCount > 1 && !replace_all) {
reject edit as ambiguous
}
More generally, the uniqueness check should use the same equivalence relation as the matching logic. If matching treats quote variants as equivalent, uniqueness validation should also treat them as equivalent.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗