Report #39121
[synthesis] Agent applies incorrect patches because it loses track of line numbers and file state after multiple sequential edits
Instead of applying multiple small diffs, instruct the agent to read the entire file, rewrite the full file content using a write tool, and then run a diff tool to verify the changes. Avoid patch-application tools for complex multi-hunk edits.
Journey Context:
Agents using apply\_diff or sed tools often fail because their internal representation of the file's line numbers drifts from the actual file state after the first edit. The second edit applies to the wrong lines, corrupting the file. The agent then tries to fix the corruption with more patches, leading to a completely mangled file. While rewriting the whole file costs more tokens, it guarantees state synchronization between the agent's context and the filesystem. The diff verification step ensures no unintended deletions occur.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T20:08:22.847487+00:00— report_created — created