Conversation
Without replace_all, the edit tool called String.prototype.replace with new_string as the replacement string, so `$$`, `$&`, `` $` `` and `$'` in new_string were expanded instead of written. An edit that inserts `$$HOME` into a Makefile or a `$$` math block into Markdown wrote different text than the model asked for. The replace_all path already wrote new_string verbatim. Pass a replacer function so both paths agree. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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
When an agent uses the built-in
edittool withoutreplace_all, any$$,$&,$`or$'innew_stringis rewritten before it reaches the file. The tool still reportsReplaced 1 occurrence, but the file contains different text than the model asked for. Common cases:$$HOMEor$$(pwd)in a Makefile becomes$HOME/$(pwd)$$(shell PID) or a$$ … $$math block in Markdown/LaTeX loses a dollar sign$&is replaced by the matchedold_string, and$'/$`by the rest of the file after / before the matchThe cause is that
content.replace(oldString, newString)still applies replacement patterns even when the search value is a string. Thereplace_allpath usessplit/joinand already writesnew_stringverbatim, so the same edit gives different files depending on that flag. Passing a replacer function (() => newString) makes the single-match path literal too. Matching, uniqueness checks and the result message are unchanged.Validation
bun test packages/runtime/tests/tools/built-in/fs.test.ts -t "edit writes"Before the fix (new test added, source unchanged):
After the fix, this test passes.
replace_all: truehalf of the test (line 53) passes both before and after, as do the existingwrite and edit …tests. This shows only the single-match path's output changes.bun test packages/runtime/tests/tools/built-in/fs.test.ts: 32 pass / 2 fail onmain, and 33 pass / 2 fail on this branch. The same two tests fail onmain:read, traversal, and search tools expand home pathsandgrep caps multibyte matching output. Both need ripgrep (rg), which is not installed locally; CI installs it.--max-warnings 0) on both touched files: clean onmainand on this branch.tsc --noEmit -p packages/runtime/tsconfig.json: clean onmainand on this branch.main(9 and 62); this change adds none.🤖 Generated with Claude Code