I keep a list of agent failure modes. Model quality is on it, but it is not near the top. Near the top is this one:

Two processes, one working directory, both writing, last writer silently wins.

No exception. No diff. No conflict marker. The agent that lost reports success, because from its point of view it did succeed - the write went through. Twenty minutes later you open the file, and a chunk of the work is gone. Nobody can tell you which run removed it, because both runs logged success and neither log contains the other's version of the file.

I hit this with two terminals open on the same project. It is trivially reproducible once you know it exists, and it is almost impossible to reason about after the fact. That is the worst combination.

Why the application layer is the wrong place to fix this

The instinct is a lock file: write session.lock, check for it, delete it when done. It works right up until the process does not get to run its cleanup - Ctrl-C, OOM kill, a closed laptop, a crash inside the tool call. Now the lock file is a leftover, and the next run is blocked by a lock nobody holds. So you add a timestamp and a stale-lock timeout, and now you are writing a distributed systems textbook chapter about clock skew so that two local processes can agree on who owns a folder.

Every line of that is code you now own, and none of it knows the one thing the OS already knows: when a process dies.

Let the kernel hold the lock

An advisory file lock does exactly this and nothing more. On Unix it is flock, on Windows it is LockFileEx. The property that matters is not exclusivity - it is release-on-death. The kernel drops the lock the instant the holding process exits, however it exits. No stale state, no timeout, no cleanup path to forget.

import fcntl, os, pathlib def hold_session(path: str): f = open(path, "a+") try: fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: raise SystemExit("another agent already owns this directory") return f # keep the handle alive for the whole run 

LOCK_NB matters: a second agent should be told "occupied, pick another directory" immediately, not hang until the first one finishes. A blocking lock turns a concurrency bug into a mysterious freeze.

On Windows the same shape with msvcrt.locking or a LockFileEx call through ctypes. The two implementations differ in details I will not pretend are elegant. The guarantee is identical.

What this changes about how you design the agent

Once a session is exclusive by construction, a surprising number of other rules stop being necessary:

  • No "one writer at a time" prompt instruction. Prompts are suggestions; the lock is not.
  • No reconciliation logic. There is nothing to reconcile if the second writer never got in.
  • Crash recovery is free. The process died, the kernel released the lock, the next run starts clean.
  • Rollback becomes meaningful. "Restore the last known good state" is only well-defined if exactly one run was writing when that state was made.

That last point is the one I care about most, and it generalises well beyond locking:

Reversibility is not only about undo buttons. It is about never letting two writers touch the same state in the first place.

An undo history is worthless if you cannot say which of two concurrent edits it is undoing.

The boring checks I run before trusting a concurrency claim

If you are evaluating an agent runtime - mine or anyone's - these are cheap to test and hard to fake:

  1. Start two runs against the same directory and see whether the second one is refused, or whether it quietly interleaves.
  2. Kill the first run with kill -9 mid-write, then start a new one. If it is blocked by a lock nobody owns, the exclusivity is in the app layer, not the kernel.
  3. Check the log for a run identifier on every write. If a line cannot be attributed to a specific process, the audit trail is decorative.
  4. Try to reconstruct a file's history from logs alone. If you cannot, you do not have rollback, you have hope.

None of this requires reading source code. All of it caught something for me.

Where this came from

I run an agent on my own machine all day - files, shell, browser, and a phone over ADB. The concurrency failure above was real, in my own CLI, and the fix I shipped is deliberately unambitious: a session is now exclusive to the process that opened it, held by the kernel, released on death. A second CLI that hits an occupied session gets offered a new session or an exit, and that is the whole feature.

The rules I ended up with are the ones that survived contact with actual crashes: local execution, explicit permissions per tool call, an append-only log, and rollback for anything that touched disk. None of them are clever. All of them are checkable.

The runtime is GAL, if you want to compare it against your own setup - the exclusivity and the audit log are the parts I would test first. If you have hit a worse concurrency failure mode with agents, I would like to hear it, because my list is not finished.