Repository navigation
Description
Context
String.prototype.replace(string, string) matches its pattern literally, but it still expands $$, $&, $` and $' in the replacement string. The edit paths below pass the agent's new_string as that replacement, so text containing those sequences is changed before it reaches disk. Examples: LaTeX display math $$, a price such as $$5, or a shell snippet with $&.
bun -e 'console.log("a X b".replace("X", "$$5 and $&"))'
# prints: a $5 and X b (the text asked for was: a $$5 and $& b)Affected sites (product code, found with git grep -nP '\.replace(All)?\(\s*[A-Za-z_][\w.]*\s*,\s*(?!\()[A-Za-z_][\w.\[\]]*\s*[,)]' -- packages):
- Pi
edit_file(governed direct writes):packages/ui-backend-pi/src/file-tools.ts:142-143. It writesraw.replace(params.old_string, params.new_string), then computes the same replacement a second time forrememberBase. - Hosted
edit_fileapplication route (every backend):packages/ui-server/src/brain/application.ts:417,content = raw.replace(input.old_string, input.new_string). - Claude native-edit version tracking:
packages/ui-backend-claude/src/permission-hooks.ts:168rebuilds the bytes the nativeEditwrote withoriginalFile.replace(oldString, newString)/replaceAll(...), and remembers their SHA-256 (fileVersions.remember). The installed Claude runtime applies edits literally, with a function replacement (replace(old, () => new)). So whennewStringcontains$sequences, the recorded hash does not match the file. A later native write to that file can then be refused as stale (STALE_NATIVE_WRITE,packages/ui-sdk/src/server/native-file-versions.ts:7).
Sites 1 and 2 silently change the content written to the brain. Site 3 records a wrong version and can produce a false "file changed since you read it" refusal.
Checked and not affected: packages/ui-kit/tools/theme/layers.ts:11 and packages/render-template/src/link-policy.ts:57 replace with internal marker constants. The scripts/native-proof/* and scripts/prove-worker-identity.ts mutation harnesses use fixed authored replacement text.
Scope
- Replace the string-replacement form at the three sites with a literal splice:
raw.replace(old, () => replacement), orindexOf+ slice. ForreplaceAll, usesplit(old).join(replacement)or the function form. - In pi
edit_file, compute the new content once and reuse it for the write and forrememberBase. - Add a regression test per site with
$$,$&,$`and$'in the new text.
Out of scope
- Other differences between how Claude's native
Editnormalises input and how site 3 rebuilds it (for example quote normalisation). - Merging the two
edit_fileimplementations, or any other refactor of the pi in-process writers. - Changes to the edit tool schemas or descriptions.
Acceptance criteria
- Pi
edit_filewith anew_stringcontaining$$,$&,$'and$`writes those characters to disk verbatim, proven by a test inpackages/ui-backend-pi/tests/that fails before the change. - The ui-server application route's
editoperation with the samenew_stringstores it verbatim, proven by a test inpackages/ui-server/tests/brain-application.test.ts(or a sibling) that fails before the change. - The Claude
PostToolUsehandler for a nativeEditwhosenewStringcontains$&remembers the SHA-256 of the literal result (and the same holds forreplaceAll: true), proven by a test inpackages/ui-backend-claude/tests/that fails before the change. -
git grep -nP '\.replace(All)?\(\s*[A-Za-z_][\w.]*\s*,\s*(?!\()[A-Za-z_][\w.\[\]]*\s*[,)]' -- packagesreturns no product edit site that passes user or agent text as a replacement string. The PR lists any remaining hits and why each is safe.
Verification
bun run test packages/ui-backend-pi packages/ui-backend-claude packages/ui-server/tests/brain-application.test.ts
bun run typecheckContract impact
None. The tools' documented behaviour (replace an exact unique string) does not change; this makes the implementation match it.
Found by a repository review on 2026-10-22.
Activity
schlessera commentedon Nov 17, 2026
Claimed for implementation. Claim token:
claim-25852e81. Branch:fix/2486-literal-edit-replacement.Collisions: closure reports only (shared backend/ui-server test categories), no PR edits the three cited source files.
schlessera commentedon Nov 17, 2026
Citation refresh (2026-11-17), checked against
main:edit_filemoved topackages/ui-backend-pi/src/file-tools.ts:142-143. It still runsraw.replace(params.old_string, params.new_string)twice, for the write and forrememberBase.packages/ui-server/src/brain/application.ts:417.packages/ui-backend-claude/src/permission-hooks.ts:168.The scope and criteria are unchanged.
fix(backends): write edit_file's new_string literally (#2486) (#4167)
schlessera commentedon Nov 17, 2026
Delivered in #4167 (squash c26fb22). The pi
edit_file, the hosted application route'seditand the Claude nativeEditversion tracking now insert the new text literally with a function replacement. The pi edit computes its result once, for both the write and its remembered base. A regression test per site covers$$,$&,$'and$`, and failed before the change. The PR lists each remaining grep hit and why it is safe.