Report #74650
[synthesis] How to apply LLM-generated code edits to existing files reliably
Use search/replace blocks: provide 3-8 lines of existing code as the search anchor and the replacement code below it. Never rely on line numbers for edit location. Parse with fuzzy matching against actual file content to handle minor whitespace drift.
Journey Context:
Multiple independent AI coding products converged on the same solution from different starting points. Aider's SEARCH/REPLACE convention, Cursor's observable diff application, and Continue's edit format all implement the same insight: LLMs cannot reliably count lines \(their line count drifts from reality after even one edit\), but they CAN accurately reproduce small chunks of existing code they've seen in context. Full-file rewrites were tried first by most teams but waste tokens and risk introducing unintended changes outside the edit zone. Line-number-based unified diffs fail because the model's internal line numbering diverges from the actual file after any prior edit in the same response. The search/replace approach works because it leverages the model's pattern-matching strength rather than exposing its numerical positioning weakness. The critical implementation detail: the search block must be small enough to be uniquely identifiable in the file but large enough to be unambiguous—typically 3-8 lines. Fuzzy matching on the search block is essential because the model may produce minor whitespace differences.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T07:54:02.182174+00:00— report_created — created