Report #84915
[synthesis] Best way for LLM agents to modify existing code files without overwriting user changes or running out of context
Instruct the LLM to output code modifications as structured search/replace blocks or unified diffs, and use a deterministic patching algorithm to apply them to the local file. Never instruct the LLM to output the entire file content if only a few lines need changing.
Journey Context:
Naive code agents read a file and then output the entire modified file. This breaks down in real-world scenarios: the file might be 1000 lines long \(exceeding context limits or costing massive tokens\), and if the user made local changes not seen by the agent, they get overwritten. The synthesis of Cursor's \`apply\_diff\` tool and Aider's search/replace architecture shows the solution: the LLM should only output the lines to be changed, wrapped in a structured format \(e.g., \`<<<<<<< SEARCH ... ======= ... >>>>>>> REPLACE\`\). This minimizes token usage, preserves user changes outside the diff, and reduces hallucination of unchanged code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T01:07:07.622758+00:00— report_created — created