[BUG] edit writes corrupted file content when new_str contains $ replacement patterns ($$, $&, $')
Checks
- [x] I have updated to the latest minor and patch version of Strands
- [x] I have checked the documentation and this is not expected behavior
- [x] I have searched ./issues and there are no duplicates of my issue
SDK Language
TypeScript
Strands Version
@strands-agents/harness 0.1.x / repo main (eca8cf14b)
Language Runtime Version
Node v22.20.0
Operating System
macOS 15
Installation Method
npm / repo clone
Steps to Reproduce
The harness edit tool writes the new file content with String.prototype.replace (harness-ts/src/tools/file-tools.ts:188):
await sandbox.writeText(input.path, content.replace(input.old_str, input.new_str))
When the second argument of replace is a string, JavaScript scans it for $ sequences before inserting it: $$ becomes one $, $& becomes the matched text, $` becomes everything before the match, and $' becomes everything after it. The spec calls this step GetSubstitution. So new_str reaches the file already rewritten. The tool's own schema promises verbatim text ("Replace an exact string in a file", new_str: "Replacement text"). The Python harness edit keeps that promise: harness-py/src/strands_harness/tools/file_tools.py:164 uses Python's str.replace, which has no substitution pass. Identical input corrupts a file on one harness and edits it cleanly on the other.
Repro, using the same vitest setup as harness-ts/test/tools/file-tools.test.ts:
const path = join(dir, 'Makefile')
await write.invoke({ path, content: 'run:\n\tkill PLACEHOLDER\n' }, ctx())
await edit.invoke({ path, old_str: 'PLACEHOLDER', new_str: '$$SERVER_PID' }, ctx())
// file now contains: kill $SERVER_PID
Observed on main (eca8cf14b):
new_str: '$$SERVER_PID'writeskill $SERVER_PID. The Makefile is now wrong.new_str: 'literal $& text'writesliteral OLD text.new_str: "printf $'x'"on the filealpha PLACEHOLDER omegawritesalpha printf omega\nx' omega. Since$'means "everything after the match", the tail of the file gets inserted at the edit point, and the damage lands far from the edited line.
Expected Behavior
new_str lands in the file byte for byte, as documented and as the Python harness behaves.
Actual Behavior
Any new_str containing $$, $&, $` or $' is rewritten on the way to disk, and the tool still reports Edited <path>.. These sequences are common in the files a coding agent edits: $$ in every Makefile recipe and crontab shell line, $'…' in bash ANSI-C quoting, $& in sed scripts.
Two more places carry the same expression:
strands-cli/src/tui/permissions/file-change-preview.ts:43builds the approval diff for aneditcall withbefore.replace(oldText, newText). Today that diff matches the corrupted write. After the tool is fixed, the preview needs the same fix or it will show corruption that no longer happens.strands-cli/src/tui/project/python.ts:461builds the approval diff for a Python source-backed agent the same way. Python writes verbatim, so today the user approves a diff that differs from what lands on disk.
Possible Solution
Pass the replacement as a function. replace inserts a function's return value verbatim, with no $ scan: content.replace(input.old_str, () => input.new_str). The repo already uses this form for the same reason in strands-ts/src/vended-tools/notebook/notebook.ts:303. I have a PR ready covering all three sites with regression tests.