Fixes a production cascade where parallel SessionStart hooks could crash with FileNotFoundError and where cozempic reload/treat/strategy could silently overwrite the guard daemon's in-flight prune output.
What changed
Concurrency fixes:
_save_sidecar/save_messagesusemkstemp— two concurrent writers no longer collide on a fixed.tmpfilenamerecord_session+record_savingswrap their read-modify-write cycles in a host-wide flock (POSIX) or msvcrt lock (Windows)cmd_treat/cmd_strategy/cmd_reload+ the MCPtreat_sessiontool all now acquire_PruneLockand pass a pre-loadsnapshottosave_messages. They exit cleanly with code 2 (lock held) or 3 (Claude wrote new lines mid-prune) instead of clobbering.doctor.fix_corrupted_tool_use+doctor.fix_orphaned_tool_resultsacquire_PruneLockand skip held sessionsstart_guardwatcher cleanup moved totry/finally(fires on all 5 exit paths, not just KeyboardInterrupt)
Hook schema v5 → v6:
- Removes the unflocked foreground
cozempic guard --daemoncall from SessionStart that ran before the flocked branch and caused duplicate daemon spawns - Existing installs auto-refresh on next
cozempicinvocation — no manualcozempic initneeded - Net effect: SessionStart is ~50-200ms faster
New primitives in cozempic.helpers:
atomic_write_text(target, data)— mkstemp + fsync + os.replace_HostFileLock(target)— per-file blocking lock (fcntl on POSIX, msvcrt on Windows, no-op fallback)
Tests
734 passing (715 baseline + 19 Wave 1). Includes a behavioral test for the exact production race: cmd_treat exits 2 when _PruneLock is held by the guard daemon.
What you'll notice
cozempic reloadwhile the guard daemon is mid-prune now prints a clear "Aborted: another prune cycle is active" and exits 2 (instead of silently clobbering)- Less duplicated guard daemon startup logs in
/tmp/cozempic_guard_*.log(the unflocked call was causing 2x daemon spawn attempts per SessionStart)
Stats
- 12.2B tokens saved across 30K+ tracked installs
- 302 stars, 18 forks
- 30 countries