Skip to content

fix(compliance-heartbeat): release orphaned batch claims without losing crash backoff - #8188

Merged
bokelley merged 1 commit into
mainfrom
bokelley/heartbeat-orphan-claims-a
Oct 14, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/heartbeat-orphan-claims-a

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Problem

runComplianceHeartbeatJob claims an entire batch up front by setting next_compliance_check_at to 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 622 adds heartbeat_claim_owner, heartbeat_started_at and heartbeat_orphan_streak to agent_registry_metadata. The claim SQL lives in compliance-heartbeat-claims.ts.

  • Ownership: each claim records its owning process (FLY_MACHINE_ID:uuid). The start marker is set once the execution fence is acquired.
  • Release: every processAgent exit path releases the claim, and claims this batch never reached are released at batch end.
  • Orphan reconciliation, before each selection: a claim owned by another process on the same machine, or one whose lock has expired, is orphaned.
    • Not started: released immediately, no penalty.
    • Started: the streak increments and the agent backs off 30 min × 2^(streak−1), capped at 48 h. A streak of 3 or more logs at error level, which routes to #admin-errors.
  • Isolation: 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.

Known limits

  • Deploys count as orphans. A deploy that kills the worker mid-suite looks like a crash, so that agent backs off 30 min and runs alone next time.
  • One heartbeat process per machine. Same-machine detection assumes this.

Tests

  • Integration: new real-DB suite 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.
  • Unit: a new "batch claim ownership" block in compliance-heartbeat.test.ts.
  • Mutation checks: both suites fail when suspect isolation or the same-machine check is removed.
  • No changeset: non-protocol server change.

🤖 Generated with Claude Code

…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]>

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant