Skip to content

Copilot: write $ patterns in memory str_replace as they are - #350865

Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/copilot-memory-dollar
Draft

Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/copilot-memory-dollar

Conversation

@federicobrancasi

Copy link
Copy Markdown
Member

Fixes #323167

The memory tool's str_replace passed new_str to String.prototype.replace as a string, so $', $`, $& and $$ in it were expanded instead of written. A memory note containing $' or $` (shell quoting, regular expressions, prices like `32$`) copied the rest or the start of the file into itself, and $$ became $. The comments in #323167 trace a corrupted memory file back to this.

This PR passes a function as the replacement, so new_str is written as it is. insert builds the file with splice and wasn't affected.

How to test

Command Result
cd extensions/copilot && npx vitest --run --pool=forks src/extension/tools/node/test/memoryTool.spec.tsx 35 passed; the new test fails without the fix

The memory tool's str_replace passed new_str to String.replace as a
string, so $', $`, $& and $$ in it were expanded. A note containing $'
or $` copied the rest or the start of the file into it. Pass a function
instead.

Fixes #323167

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 28, 2026 16:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues require resolution, though tests were not independently run.

No open issues 🎉

What changed in this PR

Prevents Copilot memory-file corruption when str_replace replacement text contains special dollar patterns, addressing #323167.

Changes:

  • Uses a replacement callback so new_str is written literally rather than interpreted by String.prototype.replace.
  • Adds a regression test covering dollar patterns and preservation of surrounding file content.

No actionable findings remain. Tests were not independently run.

File Description
extensions/​copilot/​src/​extension/​tools/​node/​test/​memoryTool.spec.tsx Adds regression coverage for literal dollar-pattern replacements.
extensions/​copilot/​src/​extension/​tools/​node/​memoryTool.tsx Writes replacement text without expanding special dollar patterns.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

Memory tool corrupts files via recursive self-duplication (str_replace/insert re-injects the entire file body many times)

2 participants