Repository navigation
fix(backends): write edit_file's new_string literally (#2486) - #4167
Merged
Merged
Conversation
String.prototype.replace(string, string) expands $$, $&, $` and $' in the replacement. The pi direct edit_file, the hosted application route's edit, and the Claude native Edit version tracking all passed agent text as that replacement, so the written bytes, or the remembered hash, differed from what was asked. Each now uses a function replacement, which inserts the text verbatim. The pi edit computes its result once, for both the write and its remembered base. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
Author
Independent reviewReviewed head 6187ffd against base 9f9147f. Reviewer: agent. No findings. {
"version": 1,
"kind": "brain-review-receipt",
"pr": 4167,
"head": "6187ffd7f2599f261c6d702e4551a96f922f41b1",
"base": "9f9147f0ec7d335a82c30e07c7c442e21832949a",
"reviewer": "agent",
"outcome": "no-findings",
"findings": []
} |
This was referenced Nov 17, 2026
schlessera
marked this pull request as ready for review
November 17, 2026 10:03
This was referenced Nov 17, 2026
Closed
4 tasks
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.
Closes #2486.
What this does
String.prototype.replace(string, string)matches its pattern literally but expands$$,$&,$`and$'in the replacement. Three edit paths passed agent text as that replacement. Each now uses a function replacement, which inserts the text verbatim:edit_file(packages/ui-backend-pi/src/file-tools.ts): computes the result once and uses it for both the write andrememberBase.edit(packages/ui-server/src/brain/application.ts).Editversion tracking (packages/ui-backend-claude/src/permission-hooks.ts): rebuilds the written bytes the way the runtime applies an Edit, for bothreplaceandreplaceAll, so the remembered SHA-256 matches the file.No contract change: the tools already promise to replace an exact string, and the code now does what they promise.
Acceptance criteria
edit_filewrites$$,$&,$',$`verbatimpackages/ui-backend-pi/tests/tool-factories.test.ts"editFileTool writes $ sequences…"editstores them verbatimpackages/ui-server/tests/brain-application.test.ts"an edit stores $ sequences…"replaceAllpackages/ui-backend-claude/tests/permission-wiring-units.test.ts"native edit versions" (two cases)Proof
directWrites: true, so the remembered base is checked before the second edit. Without that flag, this half could not fail.replaceAllbranch: fails only thereplaceAllcase.bun run test packages/ui-backend-pi packages/ui-backend-claude packages/ui-server/tests/brain-application.test.ts: 1498 pass, 77 fail. The 77 are identical with and without the fix: native worker and runtime tests this host can't run (no unprivileged user namespaces for bubblewrap), plusbrain-application.test.ts's "generated capture content also obeys the byte bound" (application_failedhere). Hosted CI runs them.check:prpass.Remaining grep hits
git grep -nP '\.replace(All)?\(\s*[A-Za-z_][\w.]*\s*,\s*(?!\()[A-Za-z_][\w.\[\]]*\s*[,)]' -- packagesafter the change:compute-boat/src/redact.ts:39:keyLikeis a replacer function andMASKan internal constant.render-template/src/link-policy.ts:57andui-kit/tools/theme/layers.ts:11: internal marker constants (already ruled safe in the issue).ui-react/src/lib/attachment-reads.ts:37: a store'sreplacemethod, notString.prototype.replace.ui-sdk/tests/fixtures/identity-helper.py:103: Python'sos.replace.core/tests/sync-fixture.ts,ui-backend-pi/tests/ask-user-brief.test.ts,ui-react/tests/session-demux.test.ts,ui-sdk/tests/fixtures/ordinary-identity-root.ts: test fixtures with authored replacement text.Collisions
bun scripts/collisions.ts --json --pr 4167at head 6187ffd: closure reports only (shared backend and ui-server test categories and workspaces) with #3056, #3059, #3069, #3581, #3622, #4002, #4036, #4151, #4155, #4157 and #4166. No PR edits the three source files.{"version":1,"kind":"brain-check-pr-receipt","head":"92609c819782994f92ba28e147e5f48243e9deb7","base":"origin/main","mode":"focused","startedAt":"2026-11-17T10:01:15.906Z","finishedAt":"2026-11-17T10:02:12.990Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":19073.250231},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":142.6728559999974},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":122.57071699999506},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":21.78167399999802},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":37702.617536}]}🤖 Generated with Claude Code