Parallel agents overwrite shared state: how do you prevent lost updates?
When agents share files or state, determine whether updates were lost during writes based on concurrent reads or during merging. Separate branches can isolate code but do not automatically isolate a shared database or external resource.
Start troubleshooting here
When agents share files or state, determine whether updates were lost during writes based on concurrent reads or during merging. Separate branches can isolate code but do not automatically isolate a shared database or external resource. Assign explicit write scopes and use version checks or locks to prevent silent overwrites, then inspect the final result for every valid change.
Save the version each task read at startup
Typical symptoms are two tasks reporting success while only the later edit survives, or a stale value replacing current task state. Preserve each agent’s input version and output and check whether both started from outdated content. Do not reset the shared directory to restore one result, since that can delete other changes awaiting integration.
Locate stale-value overwrites and unconditional writes
Check whether each writer reads the latest version and later replaces the whole object based on an older version. Where version numbers or ETags are supported, verify conditional writes; unconditional last-writer-wins behavior hides conflicts. For code, inspect workspace isolation and confirm generated files or caches do not still target the same path.
Isolate files and conditionally update shared state
Separate independent file scopes and review differences during integration. For shared state, use conditional writes, then reread and merge on a version conflict. Lock only necessary critical sections rather than an entire long task. A same-directory temporary file followed by replacement prevents partial files; atomic replacement does not prevent two complete stale versions from overwriting each other.
Verify conflict detection and retention of valid changes
Test simultaneous changes to different fields and to the same field. Distinct valid changes should survive, and conflicting changes should be reported explicitly. Test interruption cleanup for locks and temporary files to avoid blocking later work. Acceptance should inspect merged content and version history, not merely count agents that individually returned success.
Locks, atomic replacement and version conditions solve different problems; choosing one does not establish general concurrency safety. This article modified no shared database and stopped no other agent. If an external API lacks conditional writes, serialize critical operations at the application layer. Switching Git branches does not recover overwritten external business state.
Checked sources
These sources support the concepts and methods described here. Examples and test scope are labeled separately; check the version of the tools you use.