Report #40677
[agent\_craft] Agent produces non-applicable diffs or hallucinates line numbers when applying code changes
Use search/replace blocks with 3-line context windows rather than line-number-based diffs or full-file rewrites. Format each edit as: <<<<<<< SEARCH \(3 lines before \+ target line \+ 3 lines after\) ======= \(new code\) >>>>>>> REPLACE. This handles line-number drift and avoids destroying unrelated code or comments.
Journey Context:
Line-number-based diffs are fragile; inserting a single line earlier in the file invalidates all subsequent line references. Full-file rewrites are robust but consume massive tokens and destroy manual formatting, comments, or minor edits made by users in the interim. Search/replace with context \(similar to git's unified diff but without line numbers\) leverages unique local context to anchor changes. The 3-line window is a sweet spot: unique enough to avoid collisions in most files, short enough to keep token counts manageable. This is the standard used by Aider and other high-reliability coding agents.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T22:44:55.544972+00:00— report_created — created