Agent tool: subagent can overwrite file with stale content, destroying hours of work
Bug description
When multiple agents work on the same file sequentially (not in parallel), a later agent can silently overwrite the file with a stale version, destroying all changes made by previous agents in the same session.
What happened
During a long refactoring session on a large i18n.js file (~19,000 lines, 50 language blocks), I was making sequential changes through subagents — each one reading the file, transforming it, and writing it back. After ~15 successful agent runs (over several hours), I launched one more agent to move/rename a block of keys. That agent appears to have read or used a stale version of the file and wrote it back, reverting the file to a state from hours earlier.
All of the following work was silently destroyed:
- 6+ new section blocks created (Footer, Card, GLView, AppImportJSON, Sidebar action bar)
- ~30 dead keys removed across 50 language blocks
- 39 single-quote to double-quote normalizations
- Multiple key renames and moves between sections
- App.jsx references may now be out of sync with i18n.js
Expected behavior
An agent writing a file should NEVER be able to overwrite content that was modified after the agent's initial read. There should be a staleness check — compare the file's last-modified timestamp or content hash at write time against what it was at read time. If the file changed, the write should fail with an error, not silently overwrite.
Impact
- Severity: High — silent data loss with no warning, no diff, no recovery (unless git is tracking changes)
- This is especially dangerous in long sessions where users build up incremental changes through multiple agent invocations
- The user has no way to know the agent is working with stale data until after the damage is done
Environment
- Claude Code Desktop (Windows 11)
- Claude Opus 4.6 (1M context)
- Agent tool with general-purpose subagents
Suggested fix
- Before writing a file, agents should verify the file hasn't been modified since they last read it (timestamp or hash check)
- If a conflict is detected, the agent should abort and report the conflict rather than silently overwriting
- Consider a file locking mechanism when agents are writing to shared files
---
🤖 Generated with Claude Code
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗