Skip to content

fix(prompts): prevent models from hallucinating line numbers in apply_diff#641

Open
faitno wants to merge 1 commit into
Zoo-Code-Org:mainfrom
faitno:fix/apply-diff-line-numbers
Open

fix(prompts): prevent models from hallucinating line numbers in apply_diff#641
faitno wants to merge 1 commit into
Zoo-Code-Org:mainfrom
faitno:fix/apply-diff-line-numbers

Conversation

@faitno

@faitno faitno commented Jun 17, 2026

Copy link
Copy Markdown

Description

Models such as Gemini output line numbers in the read_file tool (e.g. 137: ...). When attempting to use apply_diff, models often copy these line numbers verbatim into the <<<<<<< SEARCH block. Since the actual source files do not contain these line numbers, the apply_diff tool strictly fails with an Edit Unsuccessful error, eventually leading to a consecutiveMistakeLimit failure in the extension.

This PR strictly updates the system prompt and instructions for the apply_diff tool to explicitly instruct models to strip line numbers from the SEARCH block so that it correctly matches the raw file content.

Summary by CodeRabbit

  • Bug Fixes
    • Clarified guidance for applying search-and-replace changes so line numbers are not included in exact-match text.
    • Added stronger warnings to prevent mismatches when using file content from read-only previews.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ff6baa5f-78d9-42c7-b6ad-900715e7ee12

📥 Commits

Reviewing files that changed from the base of the PR and between 9b6bd77 and 9a63336.

📒 Files selected for processing (1)
  • src/core/prompts/tools/native-tools/apply_diff.ts
✅ Files skipped from review due to trivial changes (1)
  • src/core/prompts/tools/native-tools/apply_diff.ts

📝 Walkthrough

Walkthrough

The apply_diff tool description strings in apply_diff.ts are updated to add explicit CRITICAL warnings: read_file-provided line numbers do not exist in actual file content and must be stripped before using SEARCH content for exact matching. The DIFF_PARAMETER_DESCRIPTION template now explicitly prohibits line-number prefixes in the [exact content to find] section.

Changes

apply_diff SEARCH Block Line-Number Warnings

Layer / File(s) Summary
SEARCH block line-number warning text
src/core/prompts/tools/native-tools/apply_diff.ts
APPLY_DIFF_DESCRIPTION and DIFF_PARAMETER_DESCRIPTION template strings are rewritten to include CRITICAL warnings that read_file line numbers must be removed and must not appear in SEARCH block content used for exact file matching.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Possibly related issues

Poem

🐇 A rabbit once searched for a line,
But numbers crept in — oh, what a sign!
"CRITICAL!" I cried with a thump,
"Strip those digits, don't let them clump!"
Now SEARCH blocks match perfectly fine. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the fix, but it omits the required issue link, test procedure, and checklist sections. Add the approved issue reference, a test procedure, and the required checklist/notes sections from the template.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly names the main change: preventing hallucinated line numbers in apply_diff.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed: private package registry requires authentication. Disable ESLint in CodeRabbit settings or use public packages.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@faitno faitno force-pushed the fix/apply-diff-line-numbers branch 2 times, most recently from b0e05a7 to 9b6bd77 Compare June 17, 2026 07:29
@navedmerchant

Copy link
Copy Markdown
Contributor

Thanks for this, please clean up the CR to keep changes relevant to issues at hand

@faitno faitno force-pushed the fix/apply-diff-line-numbers branch from 7361c07 to 9a63336 Compare June 24, 2026 14:04
@faitno

faitno commented Jun 24, 2026

Copy link
Copy Markdown
Author

Done! Force-pushed the branch with all unrelated changes removed — the PR now only touches src/core/prompts/tools/native-tools/apply_diff.ts.


const APPLY_DIFF_DESCRIPTION = `Apply precise, targeted modifications to an existing file using one or more search/replace blocks. This tool is for surgical edits only; the 'SEARCH' block must exactly match the existing content, including whitespace and indentation. To make multiple targeted changes, provide multiple SEARCH/REPLACE blocks in the 'diff' parameter. Use the 'read_file' tool first if you are not confident in the exact content to search for.`
const APPLY_DIFF_DESCRIPTION = `Apply precise, targeted modifications to an existing file using one or more search/replace blocks. This tool is for surgical edits only; the 'SEARCH' block must exactly match the existing content, including whitespace and indentation. To make multiple targeted changes, provide multiple SEARCH/REPLACE blocks in the 'diff' parameter. Use the 'read_file' tool first if you are not confident in the exact content to search for.
CRITICAL WARNING: The 'read_file' tool outputs line numbers (e.g. '1: ', '2: ') for your convenience, but these line numbers DO NOT exist in the actual file! You MUST STRIP all line numbers from the 'SEARCH' block. Your 'SEARCH' block must match the RAW file content exactly.`

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.

The example format looks off — read_file outputs lines as '1 | ' and '2 | ' (space-padded number + pipe), not '1: ' / '2: '. Could a model following this example end up looking for the wrong pattern to strip? (The actual format comes from addLineNumbers() in extract-text.ts:155.)

>>>>>>> REPLACE`
>>>>>>> REPLACE

CRITICAL: DO NOT include line numbers (like '137: ') in the [exact content to find] section! It must match the actual file content, which does not have line numbers.`

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.

Same here — read_file produces '137 | content' not '137: content'. Worth aligning the example with the actual output format, since the server-side auto-strip in multi-search-replace.ts also keys on the N | pattern.

@edelauna

Copy link
Copy Markdown
Contributor

I opened this issue: #716

@github-actions github-actions Bot added the awaiting-author PR is waiting for the author to address requested changes label Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-author PR is waiting for the author to address requested changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants