Skip to content

v1.8.13 — single-flight reload lock (Wave 2)

Choose a tag to compare

@junaidtitan junaidtitan released this 18 May 14:36
· 324 commits to main since this release

Closes the cascading reload bug observed in production twice. When cozempic reload and the guard daemon's auto-fire (55%/80% thresholds) — or OverflowRecovery reactive recovery — raced each other, both pipelines spawned watchers and both opened new Terminal windows via osascript, producing two Claude instances on the same JSONL → session conflict.

What changed

New _ReloadLock primitive (cozempic.reload_lock):

  • Per-session single-flight lock via O_CREAT|O_EXCL|O_NOFOLLOW (symlink-defense)
  • Stores PID + ISO timestamp + initiator for triage
  • Stale-PID auto-reclaim if holder is dead
  • Wedge detection at 60s
  • ReloadLockHeld exception with holder_pid, holder_initiator, age_sec, wedged fields

Integration in 3 spawn paths:

  • cli.cmd_reload acquires the lock around treat + watcher-spawn. Exit 2 on contention with clear "Aborted: Reload lock held by (PID , s ago)" — prints exact rm path when wedged.
  • guard.guard_prune_cycle wraps auto_reload block in the lock. On contention: prune still saves, reload deferred to the in-flight pipeline. Prints "Reload deferred — another pipeline in flight (...)".
  • overflow.OverflowRecovery._do_recover same pattern.

New cozempic reload --wait[=SECS] flag:

  • Opt-in queue instead of fail-fast. Default 30s when passed without value.
  • Prints "Queued; waiting..." on first failed acquisition so you know we're polling.
  • Wedged locks surface immediately (no point waiting on a stuck process).

Tests

749 passing (15 new in tests/test_reload_lock_wave2.py): acquire/release, metadata format, single-flight with 20-thread race, stale-PID cleanup, wedge detection, --wait happy path + wedge-fail-fast, session-ID sanitization including path-traversal, O_NOFOLLOW symlink defense, CLI flag, guard + overflow integration.

Known limitations

  • Late-fire: if a second pipeline starts within the sub-second window after we release the lock but before Claude exits, it can spawn its own watcher. Mitigated by guard timer interval (~10s) >> spawn latency. Full fix would require bash watcher to participate in the lock protocol — deferred to a future wave if observed in production.
  • Asymmetric upgrade: old guard daemon (≤1.8.12) running in memory while CLI is on 1.8.13: protection becomes symmetric after the next SessionStart hook auto-upgrades the daemon.

Stats

12.2B+ tokens saved, ~30K tracked installs, 302 stars, 30 countries.