Skip to content

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

Closed

Description

@schlessera

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):

  1. Pi edit_file (governed direct writes): packages/ui-backend-pi/src/file-tools.ts:142-143. It writes raw.replace(params.old_string, params.new_string), then computes the same replacement a second time for rememberBase.
  2. Hosted edit_file application route (every backend): packages/ui-server/src/brain/application.ts:417, content = raw.replace(input.old_string, input.new_string).
  3. Claude native-edit version tracking: packages/ui-backend-claude/src/permission-hooks.ts:168 rebuilds the bytes the native Edit wrote with originalFile.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 when newString contains $ 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), or indexOf + slice. For replaceAll, use split(old).join(replacement) or the function form.
  • In pi edit_file, compute the new content once and reuse it for the write and for rememberBase.
  • Add a regression test per site with $$, $&, $` and $' in the new text.

Out of scope

  • Other differences between how Claude's native Edit normalises input and how site 3 rebuilds it (for example quote normalisation).
  • Merging the two edit_file implementations, or any other refactor of the pi in-process writers.
  • Changes to the edit tool schemas or descriptions.

Acceptance criteria

  • Pi edit_file with a new_string containing $$, $&, $' and $` writes those characters to disk verbatim, proven by a test in packages/ui-backend-pi/tests/ that fails before the change.
  • The ui-server application route's edit operation with the same new_string stores it verbatim, proven by a test in packages/ui-server/tests/brain-application.test.ts (or a sibling) that fails before the change.
  • The Claude PostToolUse handler for a native Edit whose newString contains $& remembers the SHA-256 of the literal result (and the same holds for replaceAll: true), proven by a test in packages/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*[,)]' -- packages returns 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 typecheck

Contract 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

  1. added
    type: fix
    area: ui-server
    area: backends
    priority: p1
    agent-ready
    on Oct 21, 2026
  2. self-assigned this
    on Nov 17, 2026
  3. schlessera commented on Nov 17, 2026

    @schlessera
    OwnerAuthor

    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.

  4. schlessera commented on Nov 17, 2026

    @schlessera
    OwnerAuthor

    Citation refresh (2026-11-17), checked against main:

    • Pi edit_file moved to packages/ui-backend-pi/src/file-tools.ts:142-143. It still runs raw.replace(params.old_string, params.new_string) twice, for the write and for rememberBase.
    • The application route's site is now packages/ui-server/src/brain/application.ts:417.
    • The Claude native-edit tracking is now packages/ui-backend-claude/src/permission-hooks.ts:168.

    The scope and criteria are unchanged.

  5. added this to the 0.45.0 milestone on Nov 17, 2026
  6. schlessera commented on Nov 17, 2026

    @schlessera
    OwnerAuthor

    Delivered in #4167 (squash c26fb22). The pi edit_file, the hosted application route's edit and the Claude native Edit version 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: backendsarea: ui-serverpriority: p1type: fix

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions