Skip to content

v1.8.14 — crash-prevention pack

Choose a tag to compare

@junaidtitan junaidtitan released this 19 May 06:21
· 317 commits to main since this release

Fixes a production crash where the guard daemon on tool-result-heavy sessions looped indefinitely at the 55% HARD threshold (265 reload cycles over 5h21m, never giving up) and two SessionStart hooks could spawn two daemons within 2ms.

Contributed by @ynaamane (11th PR). Reproducer + fix-pack developed via BMAD multi-agent investigation: code audit + log forensics across 43 production guard log files + pytest RED reproducers + 3 rounds of adversarial review.

Three independent fixes (schema v6 → v8)

1. HARD-threshold reload-loop bound

  • Exponential back-off after K=3 zero-byte prunes: min(interval * 2**(K-2), 300) → 30/60/120/240/300/300/...
  • sys.exit(0) at K=10 with honest diagnostic (no further protection until next SessionStart — type /clear or restart)
  • Counter resets on any prune freeing > 0 bytes

2. Cross-process daemon spawn lock — new spawn_lock.py

  • DaemonSpawnClaim context manager via O_CREAT|O_EXCL|O_NOFOLLOW directly on the PID file (no separate sentinel)
  • 5s freshness gate via COZEMPIC_PIDFILE_FRESH_SECONDS env (clamped 0-300s)
  • Loser returns DaemonAlreadyStarting(holder_pid=<real_pid>) — no pid=0 undefined state ever observed
  • .pid.tmp write also uses O_CREAT|O_EXCL|O_WRONLY|O_NOFOLLOW (defeats C1 symlink TOCTOU)

3. Idempotent SessionStart hook

  • Shell-level fast-path: if pidfile exists AND kill -0 succeeds, skip Python entirely
  • flock fd8 on startup-lock around the spawn invocation
  • Bash↔Python sanitizer parity (relaxed to ^[a-z0-9][a-z0-9_-]{11,}$ with leading-alphanumeric dash-collision defense preserved)

Tests

784/784 passing on merge target. Race tests (test_spawn_lock.py, test_guard_race_2026_05_18.py) verified 21/21 across 5 consecutive runs.

V4 stress: 10 concurrent processes × 30 iterations × 3 runs = 900 single-winner trials, 0 multi-winner.

Live smoke: 6h07m elapsed real cozempic guard --daemon with new v8 hooks — exactly 1 Guard daemon started line, 0 Prune deferred — lock held cascades, clean Signal 15 shutdown.

C1 symlink TOCTOU PoC: pre-plant symlink at .pid.tmp → victim file, attempt spawn, victim file unchanged, daemon refuses with structured failure.

Reviewed via 3-agent protocol

  • Security: CLEAN — no backdoors, no exfiltration, O_NOFOLLOW correctly defends symlink TOCTOU, env clamp bulletproof
  • Logic: Sound — back-off math verified, cross-lock matrix shows no deadlock (5 primitives compose cleanly), all Wave 1/Wave 2 protections preserved
  • Tests + dry-run: All 6 scenarios PASS (single spawn, concurrent spawn, 0-byte prune loop, hook fast-path, stale pidfile, adversarial PID 1)

Stats

~30K tracked installs, 303 stars, 30 countries, 144K+ prunes, 12.2B+ tokens saved.