feat: Edit tool should auto-verify fresh file content before applying changes
Problem
When conversations become long, early Read tool results get compressed into summaries via context compaction. If Claude then tries to Edit based on stale/cached content, the old_string may not match the actual file state, causing silent overwrites of user's manual code changes.
This has been observed in practice — user's uncommitted changes were lost because an Edit was based on a stale conversation summary rather than the actual file on disk.
Current Behavior
- User manually edits files outside the conversation
- Claude reads files earlier in the conversation
- Context compaction compresses the Read results into summaries
- Claude attempts Edit using stale content from the summary
old_stringpartially matches or misses, potentially corrupting the file
Proposed Solution
The Edit tool should verify it has fresh file content before applying changes. Options:
- Auto-Read before Edit: If a file hasn't been Read in the current conversation turn (or since last file modification), automatically Read it first before applying the Edit
- Stale content rejection: If the
old_stringdoesn't exactly match the current file content, fail with a clear error instead of attempting a fuzzy/partial replacement - Configurable option: Add a setting like
"alwaysReadBeforeEdit": truein settings.json to enforce this at the platform level
Why Memory/Prompt Workarounds Are Insufficient
Saving a "always Read before Edit" instruction to memory helps, but:
- It relies on the AI following a prompt instruction, not a platform guarantee
- Memory can be ignored or forgotten in complex multi-step tasks
- New sessions may not load the memory in time
- It's a band-aid for what should be a platform-level safety check
Impact
This affects any user doing iterative code editing in long conversations — which is a core Claude Code use case. Losing uncommitted work due to stale content is a high-severity UX issue.
Environment
- Claude Code CLI (latest)
- Windows 11
- Large Java Spring Boot project (conversations tend to be long)
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗