Skip to content

fix(backends): write edit_file's new_string literally (#2486) - #4167

Merged
schlessera merged 3 commits into
mainfrom
fix/2486-literal-edit-replacement
Nov 17, 2026
Merged

schlessera merged 3 commits into
mainfrom
fix/2486-literal-edit-replacement

Conversation

@schlessera

@schlessera schlessera commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

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:

  • Pi edit_file (packages/ui-backend-pi/src/file-tools.ts): computes the result once and uses it for both the write and rememberBase.
  • Hosted application route edit (packages/ui-server/src/brain/application.ts).
  • Claude native Edit version tracking (packages/ui-backend-claude/src/permission-hooks.ts): rebuilds the written bytes the way the runtime applies an Edit, for both replace and replaceAll, 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

Criterion Proof
Pi edit_file writes $$, $&, $', $` verbatim packages/ui-backend-pi/tests/tool-factories.test.ts "editFileTool writes $ sequences…"
The application route's edit stores them verbatim packages/ui-server/tests/brain-application.test.ts "an edit stores $ sequences…"
The Claude PostToolUse handler remembers the literal result's hash, with and without replaceAll packages/ui-backend-claude/tests/permission-wiring-units.test.ts "native edit versions" (two cases)
The issue's grep returns no product edit site that passes user or agent text as a replacement See "Remaining grep hits"

Proof

  • Failing first: with the three source edits reverted, the same suite fails exactly these four new tests on top of the host's environment failures, and passes them with the fix. The pi test failed on the expected diff, the server test on the stored content, and the Claude tests on the remembered hash.
  • Mutation receipts (each restored):
    • Fix the pi write but remember the base from the expanding replacement: fails "editFileTool writes $ sequences…" with "File changed since you read it". The test runs with directWrites: true, so the remembered base is checked before the second edit. Without that flag, this half could not fail.
    • Revert only the replaceAll branch: fails only the replaceAll case.
  • Suite: 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), plus brain-application.test.ts's "generated capture content also obeys the byte bound" (application_failed here). Hosted CI runs them.
  • Typecheck, lint and check:pr pass.

Remaining grep hits

git grep -nP '\.replace(All)?\(\s*[A-Za-z_][\w.]*\s*,\s*(?!\()[A-Za-z_][\w.\[\]]*\s*[,)]' -- packages after the change:

  • compute-boat/src/redact.ts:39: keyLike is a replacer function and MASK an internal constant.
  • render-template/src/link-policy.ts:57 and ui-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's replace method, not String.prototype.replace.
  • ui-sdk/tests/fixtures/identity-helper.py:103: Python's os.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 4167 at 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

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>
@schlessera schlessera added this to the 0.45.0 milestone Nov 17, 2026
@schlessera

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed 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": []
}

@schlessera
schlessera marked this pull request as ready for review November 17, 2026 10:03
@schlessera
schlessera merged commit c26fb22 into main Nov 17, 2026
56 of 59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backends: edit_file expands $&, $$ and $' in new_string instead of writing it literally

1 participant