Repository navigation
fix(compliance-heartbeat): release orphaned batch claims without losing crash backoff - #8188
Conversation
…ng crash backoff The heartbeat claims a whole batch up front by pushing next_compliance_check_at out for the worst-case batch duration. When the worker process died mid-batch nothing released that claim, so every agent in the batch went dark for ~1h53m and no run row was written. In production one agent OOM-killed the worker a few minutes into each batch it was in, leaving a fixed cohort of agents without runs for 7-9 days. Releasing claims naively would turn that poison-pill agent into a crash loop, because the accidental lock was acting as crash backoff. Claims now record their owner and whether the agent's suite actually started: - Unstarted claims left by a dead process are released immediately with no penalty; claims the live process never reached are released at batch end. - A claim whose suite had started marks the agent as the one running when the process died: its orphan streak grows and it is backed off 30 minutes * 2^n (capped at 48 hours). Streak >= 3 logs at error level so it reaches #admin-errors. - An agent with a non-zero streak never shares a batch, so its next crash is attributable to it alone. Finishing a suite resets the streak. Orphans are detected at the start of every run: another process on the same machine (it cannot have survived a restart) or any process whose lock expired. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
There was a problem hiding this comment.
Ladon verdict: Approve
Approve — clean fix for the heartbeat batch-claim orphaning bug.
The reviewer verified the claim lifecycle end to end: fence released before claim on every processAgent exit, aborted agents fall through to releaseUnstartedHeartbeatClaims at batch end, and reconcile backoff reads the pre-increment streak while RETURNING yields the post-increment value for logging/escalation. Same-machine detection uses the machineId: colon-boundary prefix (tested against a neighbour id). Operational readiness checked: no stranded fence, FOR UPDATE + EvalPlanQual makes concurrent reclaim idempotent, release failures are logged to error rather than swallowed.
No protocol surface touched (no static/schemas/source/**, no docs/reference/**, no dist artifacts), so no changeset is required. The migration (622) is correctly judged as production-facing; it only adds claim-ownership tracking.
High-risk flag is true only via (added) files under server/src/addie/** and server/src/db/migrations/** plus one (modified) heartbeat job — but the reviewer found no medium-or-higher concerns on the modified file, so the modification is presumed safe. No (deleted) entries. No findings at any severity.
Decision table: rows 1–8 do not fire (no critical/high/medium findings, gated_paths false, no deleted high-risk file, no no-auto-approve team, fewer than three mediums). Falls through to row 9 → approve. Note: review_decision is REVIEW_REQUIRED, but gated_paths is false and no prose hard gate applies (no breaking protocol change), so no escalation is forced.
Problem
runComplianceHeartbeatJobclaims an entire batch up front by settingnext_compliance_check_atto the worst-case batch duration: about 1h53m for 6 agents. If the worker dies mid-batch, from an OOM kill or a deploy, nothing releases the claim. Every agent in the batch then goes dark for the full lock, and no run row is written.In production on 2026-10-13, one agent in a fixed six-agent cohort OOM-killed the worker each time the cohort was claimed. All six went 7–9 days without a run, and the cycle repeated every 1h53m. #8187 removes that crash. This PR makes the claim mechanism survive any crash.
Releasing orphaned claims at startup isn't enough on its own. The accidental 1h53m lock was acting as crash backoff, and a naive release would turn a poison-pill agent into a crash loop every few minutes.
Fix
Migration
622addsheartbeat_claim_owner,heartbeat_started_atandheartbeat_orphan_streaktoagent_registry_metadata. The claim SQL lives incompliance-heartbeat-claims.ts.FLY_MACHINE_ID:uuid). The start marker is set once the execution fence is acquired.processAgentexit path releases the claim, and claims this batch never reached are released at batch end.Known limits
Tests
compliance-heartbeat-claims.test.ts(21 tests) covering orphaned started and unstarted claims, escalation, a live claim on another machine left untouched, release on completion, release at batch end, and same-machine restart.compliance-heartbeat.test.ts.🤖 Generated with Claude Code