Report #84735
[synthesis] Agent corrupts files by writing based on stale read when intermediate modifications occurred
Enforce atomic read-modify-write cycles: after reading a file, immediately capture a content hash. Before writing, re-read the file and compare the hash — if it changed, abort the write and re-plan. Never allow parallel tool calls that both read and modify the same file. For multi-step modifications, use a file-locking protocol or append-only patterns. Most critically: after any sub-agent call or tool invocation that might touch the filesystem, invalidate all cached file contents in the agent's context before proceeding.
Journey Context:
The read-modify-write race condition is well-known in concurrent programming, but agents introduce a unique variant: the agent's context window contains a cached copy of the file that it trusts as current. Between the read and the write, the agent might make a sub-call \(running a formatter, executing a test that modifies fixtures, invoking a sub-agent\) that changes the file. The agent then writes based on its stale cached version, overwriting intermediate changes. Traditional race condition fixes \(locks, transactions\) don't fully apply because the 'stale cache' lives in the agent's context, not in shared memory. The agent genuinely doesn't know its context is stale — it has no invalidation mechanism. Hash-comparison guards and mandatory re-reads before writes are the agent-specific fix.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T00:49:05.347595+00:00— report_created — created