Skip to content

fix(apply_diff): detect malformed ------- separator and surface helpful error (#12210)#12213

Open
SAY-5 wants to merge 2 commits intoRooCodeInc:mainfrom
SAY-5:fix/apply-diff-malformed-separator-12210
Open

fix(apply_diff): detect malformed ------- separator and surface helpful error (#12210)#12213
SAY-5 wants to merge 2 commits intoRooCodeInc:mainfrom
SAY-5:fix/apply-diff-malformed-separator-12210

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 28, 2026

Closes #12210.

Summary

Smaller models (Qwen 3.6 in the issue) sometimes emit an apply_diff payload where the ------- separator is missing its trailing newline:

<<<<<<< SEARCH
:start_line:7
-------import { useTranslate } from '...';
...

The outer regex makes the separator group optional, so the run-on line is silently absorbed into the search content. Roo then reports:

No sufficiently similar match found at line: 7 (63% similar, needs 100%)

which sends the model into a loop, trying the same malformation again. The reporter notes the model can recover if told what's wrong — so a clearer error fixes the cause of the loop.

Change

Add detectMalformedSeparator(searchContent) that runs against each replacement's search content right after marker unescaping. Flags first lines that start with seven-or-more dashes followed by non-dash, non-whitespace content on the same line. Returns the offending line so the error message can quote it; otherwise returns null and the existing flow continues unchanged.

When malformed, emit:

Malformed separator in SEARCH section

Debug Info:
- The "-------" separator that follows :start_line:/:end_line: must be on its own line, followed by a newline before the search content begins.
- Got: "-------import { useTranslate } from '...';"
- Expected:
    :start_line:7
    -------
    <search content here>
- Tip: insert a newline immediately after "-------". Do not put the first line of search content on the same line as the separator.

False-positive guards: a search line that legitimately contains a long row of dashes inside the file content (markdown HR, comment header like /* ---------- header ---------- */) is NOT flagged because the helper requires a non-dash, non-whitespace tail.

Test plan

Three regression tests under malformed \-------` separator detection`:

  • Exact repro from the issue → "Malformed separator" + "must be on its own line" surfaced; the unhelpful "63% similar" message no longer appears.
  • Well-formed -------\n separator still applies cleanly.
  • SEARCH content with a long row of dashes inside the file is NOT misclassified.

Results:

  • pnpm vitest run core/diff/strategies/__tests__/66 passed; 0 failed (3 new + 63 existing).
  • pnpm run check-types → all 14 tasks pass.

Notes

  • No production-path behavior change for well-formed diffs.
  • Per-replacement (inside the for (const replacement of replacements) loop), so other replacements in the same diff still apply normally if only one is malformed.
  • Changeset added under .changeset/fix-apply-diff-malformed-separator.md (patch).

Interactively review PR in Roo Code Cloud

SAY-5 added 2 commits April 28, 2026 04:54
…pful error

Closes RooCodeInc#12210.

When a smaller model emits an apply_diff payload where the
`-------` separator is missing the trailing newline, the outer
regex's optional separator group quietly absorbs the run-on line
into the search content. Match then fails with a confusing
"63% similar (needs 100%)" error and the model loops.

Add `detectMalformedSeparator` that runs against each
replacement's search content right after marker unescaping. It
flags first lines that start with seven or more dashes followed
by non-dash, non-whitespace content on the same line. Markdown
HRs and bare separators are NOT flagged.

When detected, return an actionable error naming the exact
problem and showing the correct shape so the model can
self-correct instead of looping.

Three regression tests under `malformed \`-------\` separator
detection`:
- exact reproduction from the issue → "Malformed separator"
  error surfaced; "63% similar" message no longer appears
- well-formed separator still applies cleanly
- search content with a long row of dashes inside the file is
  NOT misclassified
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Separator after :start_line: in apply_diff is not validated

1 participant