Description
Description
Gemini Flash models (Flash and Flash Lite) and other smaller or fast models frequently struggle with the apply_diff tool, resulting in failed edit operations. Analysis suggests these failures stem from imprecise adherence to the expected diff format, leading to mismatches.
The current system instructions for apply_diff (defined in src/core/prompts/tools/native-tools/apply_diff.ts) are insufficient for these models.
Related Issues and PRs
- Zoo-Code Issue [BUG] Edit Unsuccessful, ??% similar, needs 100% #452 (Open): [BUG] Edit Unsuccessful, ??% similar, needs 100% — Users report frequent "Edit Unsuccessful" errors with high but non-100% similarities (e.g. 97%), often due to subtle line differences or formatting.
- Zoo-Code PR fix(edit-unsuccessfull): introduce configurable relaxed diff thresholds and diagnostics #470 (Open): fix(edit-unsuccessfull): introduce configurable relaxed diff thresholds and diagnostics — Integrates a configurable fuzzy-match threshold to bypass strict exact-matching errors, but highlights the persistent problem of precise string generation.
- Roo-Code Issue #12199 (Open): [BUG] apply_diff fails if :start_line:N is malformed with a misleading error — Demonstrates that models frequently hallucinate or malform the
:start_line:line syntax (e.g., using:start_line=Nor:start_line>Nor shorthand formats), triggering diff failure. - Roo-Code Issue #12210 (Open): [BUG] Separator after :start_line: in apply_diff is not validated — Demonstrates that models often fail to insert a newline immediately after the
-------separator line, causing search block syntax to fail.
Proposed Fix
Update the documentation and formatting instructions for the apply_diff tool to include stricter, clearer requirements that emphasize 100% string matches and the exact formatting of the :start_line: header.
Specifically, update the instructions in src/core/prompts/tools/native-tools/apply_diff.ts to include:
- Strict
:start_line:Formatting: Emphasize that the syntax must be exactly:start_line:[integer](e.g.,:start_line:220) and prohibit shorthand forms like:220. - Emphasis on Exact Matching: Explicitly instruct the model to copy exact lines from the source file for a 100% string match.
The following addition to AGENTS.md has been found to resolve the issue and should be integrated into the core Zoo prompts:
# Diff-Based Editing & apply_diff Tool Usage
When targeting text for a diff using the `apply_diff` tool:
- The `:start_line:[line_number]` header is required and must follow the exact syntax `:start_line:[integer]` (for example, `:start_line:220`).
- Do not write headers with shorthand forms like `:220`.
- Copy the exact lines from the source file for a 100% string match.
Test Procedure
- Verify existing
apply_diffbehavior withapps/vscode-e2e/src/suite/tools/apply-diff.test.ts. - Apply the documentation update.
- Observe if Gemini models show an improved success rate in
apply_diffoperations in subsequent interactions.
Activity