fix(udiff): insert diff replacement content literally (avoid $-substitution)#393
Open
Osamaali313 wants to merge 1 commit into
Open
fix(udiff): insert diff replacement content literally (avoid $-substitution)#393Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
applyDiff applied hunks with content.replace(before, after) where `after` is LLM-generated replacement code. String.prototype.replace treats `$` sequences in a string replacement specially ($& = whole match, $$ = $, $1/$`/$' = captured/surrounding text), so generated code containing those tokens -- very common in a webapp builder (text.replace(/x/g, '[$&]'), "$$5", jQuery $$, etc.) -- silently corrupted the applied file. Use a replacer function (() => replacement) at the three replacement sites so the content is inserted verbatim. Adds regression tests for `$&` and `$$`.
Collaborator
|
@Osamaali313 Thank you for your PR! I am reviewing it. |
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.
Summary
applyDiff(worker/agents/output-formats/diff-formats/udiff.ts) applies hunks with:where
afterBlockis the LLM-generated replacement code.String.prototype.replacewith a string replacement interprets$sequences specially —$&→ the whole match,$$→$,$`/$'/$1→ surrounding/captured text. So whenever generated code contains those tokens (extremely common in a webapp builder:text.replace(/x/g, '[$&]'), price strings like"$$5", jQuery$$, regex escaping, etc.), the applied diff silently corrupts the file.applyDiffis the diff applier consumed byscof.tsand re-exported asapplyUnifiedDiff, so this affects real generated edits. Three replacement sites are affected (the exact-match, indentation-normalized, and fuzzy-match paths).Reproduction against the current
applyDiff:Fix
Pass a replacer function at the three sites so the replacement is inserted verbatim (a function return value is never
$-interpreted):(same change for
matchedText/replacementTextandsearchBlock/replaceBlock).Testing
Added two regression tests to
udiff.test.ts($&and$$in replacement content). They fail onmain(the two new cases) and pass with the fix:(All pre-existing udiff tests still pass; the two new cases fail without the fix.)