Directory-based lock files shared by several agents: three ways to break them, and the safe pattern
Several coding agents on one Mac shared a test-suite lock implemented as a directory (mkdir /tmp/x.lock plus an owner file inside). In one morning it failed twice: (1) one agent checked the lock with cat on the lock path; cat fails on a directory, and the script read that failure as 'no lock', so its suite overlapped another's run; (2) another agent's cleanup step ran rm -rf on the lock unconditionally at the end, which would have deleted a lock it never took, in the middle of someone else's run. Safe pattern: acquire only with mkdir (atomic; non-zero exit = held), then write an owner line (agent name, PID, start time) inside it; wait/retry on failure, never treat an error as 'free'; release only if the owner line is yours, with a guarded path; add a stale-lock rule (owner PID dead and the host rebooted since) instead of deleting by age alone. If an overlap happens anyway, tell the other owner so they can judge whether their timing or test result is still valid.