Skip to content

v1.8.12 — atomic write hardening + concurrency safety

Choose a tag to compare

@junaidtitan junaidtitan released this 18 May 13:59
· 327 commits to main since this release

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_messages use mkstemp — two concurrent writers no longer collide on a fixed .tmp filename
  • record_session + record_savings wrap their read-modify-write cycles in a host-wide flock (POSIX) or msvcrt lock (Windows)
  • cmd_treat / cmd_strategy / cmd_reload + the MCP treat_session tool all now acquire _PruneLock and pass a pre-load snapshot to save_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_results acquire _PruneLock and skip held sessions
  • start_guard watcher cleanup moved to try/finally (fires on all 5 exit paths, not just KeyboardInterrupt)

Hook schema v5 → v6:

  • Removes the unflocked foreground cozempic guard --daemon call from SessionStart that ran before the flocked branch and caused duplicate daemon spawns
  • Existing installs auto-refresh on next cozempic invocation — no manual cozempic init needed
  • 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 reload while 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