Skip to content

fix: tolerant parsing of :start_line directive in apply_diff#12201

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/tolerant-start-line-parsing
Draft

fix: tolerant parsing of :start_line directive in apply_diff#12201
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/tolerant-start-line-parsing

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 Bot commented Apr 26, 2026

Related GitHub Issue

Closes: #12199

Description

This PR attempts to address Issue #12199 where apply_diff fails with a confusing "insufficient match" error when the model produces a slightly malformed :start_line directive.

Root cause: When models write :start_line=18 (using = instead of : as delimiter), the regex fails to parse it, causing the directive text and separator to leak into the search content. The search then fails because the file does not contain :start_line=18.

Approach (different from PR #12200): Rather than making :start_line: strictly mandatory (which would break backward compatibility and require test changes), this PR takes a tolerant parsing approach:

  1. Regex update: The main diff-parsing regex now accepts both :start_line: and :start_line= formats (and same for :end_line:), so [:=] is accepted as the delimiter character.

  2. Fallback stripping: A new stripLeakedStartLineDirective() method detects and strips botched :start_line= directives that somehow leaked into search content, recovering the start line number and cleaning the search content rather than failing with a misleading similarity score.

This keeps backward compatibility -- :start_line: remains optional, existing tests pass without changes, and botched directives are handled gracefully.

Test Procedure

  • All 68 existing tests in multi-search-replace.spec.ts and multi-search-replace-trailing-newline.spec.ts pass
  • 5 new tests added covering:
    • :start_line= with equals delimiter via regex (single and multiple blocks)
    • Fallback stripping of leaked directives from search content
    • Non-leaked content is not affected
  • Lint and type-check pass across the full monorepo

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue.
  • Scope: Changes are focused on the linked issue.
  • Self-Review: Performed a thorough self-review.
  • Testing: New tests added to cover changes.
  • Documentation Impact: No documentation changes needed.
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Feedback and guidance are welcome.

Interactively review PR in Roo Code Cloud

The apply_diff regex now accepts both `:start_line:` and `:start_line=`
formats for the delimiter between the directive name and the line number.
This fixes cases where models produce `:start_line=18` instead of
`:start_line:18`, which previously caused the directive to leak into the
search content, resulting in confusing "insufficient match" errors.

Additionally, a fallback detector strips leaked `:start_line=` directives
from the search content if they somehow get past the regex, recovering
gracefully rather than failing with a misleading similarity score.

Closes #12199
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.

[BUG] apply_diff fails complaining about insufficient match when the match is 100% perfect

1 participant