fix(state): serialize concurrent updateState/saveState with file lock and atomic write
Summary
The per-workspace job index (state.json) was persisted via an unguarded read-modify-write (loadState → mutate → saveState) with no lock, no atomic rename, and no compare-and-swap. When two companion invocations registered or updated jobs on the same workspace root concurrently, the last writer won and every other writer's job entry was silently dropped from the index. Worse, saveState treated "job missing from the retained list" as "job pruned" and deleted the losing jobs' .json payload and .log files — so the lost work wasn't just unindexed, its artifacts were destroyed.
Changes
- File locking (
withStateLock):O_EXCLlock-file creation with PID-based stale-lock detection (reclaims locks from crashed processes) and a 10 s timeout. Serializes concurrentupdateState/saveStatecalls across processes. - Atomic
updateState: the entireloadState → mutate → saveStatecycle now runs inside the lock, so each caller sees the latest committed state instead of a stale snapshot. saveStatesplit:saveStateLocked(internal, caller holds lock) +saveState(exported, acquires lock automatically).- Atomic write:
state.jsonis written to a temp file thenfs.renameSync'd, so concurrent readers never see a partially-written file. session-lifecycle-hook.mjs: migratedcleanupSessionJobsfrom the staleloadState+saveStatepattern toupdateState, closing the same race class for the SessionEnd cleanup path.
Reproduction
Verified the bug is reproducible on main before the fix: 5 concurrent upsertJob calls (via real child processes) left only 1 job in the index — the other 4 were silently dropped and their .json files deleted.
After the fix: all 5 jobs and their artifacts are preserved.
Testing
node --test tests/state.test.mjs tests/state-concurrency.test.mjs → 8/8 pass (3 existing + 5 new):
- Concurrent
upsertJobfrom 5 child processes — all jobs and.jsonfiles preserved saveStatewith stale snapshot — no temp files remain (atomic write)- Stale lock from dead PID — automatically reclaimed
- Atomic write —
state.jsonis valid JSON, no temp files left - Prune behavior — still correctly caps at
MAX_JOBS(50) with locking enabled
Existing state.test.mjs tests (3) pass unchanged — no regression.
Relationship to #439
#439 ships "safe, self-contained" concurrency hardening for the same file (state.mjs): it prevents saveState from deleting active (queued/running) jobs' files and adds atomic writes. Its description explicitly defers the state-transaction lock:
"It does not attempt the larger refactor the pattern calls for (a state-transaction lock and a broker-acquisition lease) — that's a design change better done as its own PR."
This PR implements that deferred state-transaction lock. The two approaches are complementary: #439 prevents artifact deletion as a mitigation, while this PR prevents the stale-snapshot race at its root. If #439 merges first, this PR will need a rebase (both touch saveState).
Closes #458