fix(babysitter): retry and reconcile PR-open snapshot reads so a failed read cannot orphan a PR
Fixes #243.
The problem
#handlePrChange is the only path that spawns a babysitter. It read the PR meta through the mount and, on any failure, logged at debug and returned — no retry, no dead-letter, no recovery path. That read is failing in production ("error":"fetch failed", 364 occurrences).
No spawn means no owner, so #routeBabysitterEvent then discarded every subsequent event for that PR — reviews, comments, CI results, conflict state — also at debug. At default log level the babysitter failed completely silently.
What changed
1. The PR-open read retries. #readPrSnapshotContent retries 3× with linear backoff (250ms, 500ms). A transient mount fault no longer decides permanently that a PR is unshepherded. On exhaustion the path is dead-lettered instead of dropped.
Only the readFile is retried. parsePullSnapshot never throws — an undefined result is a structurally mismatched payload, which a retry cannot fix — so it stays outside the retry and is not dead-lettered.
2. Orphaned PRs are recoverable. #sweepOrphanedBabysitterPrs hangs off the existing completion-sweep timer, which previously just early-returned when the babysitter was enabled. It:
- drains the dead-letter book, and
- adopts in-flight records that still have no babysitter owner.
Adoption replays the PR meta through #handlePrChange rather than spawning directly. This was deliberate: a reconcile sweep is a second delivery of a missed event, never a way around the checks that event would have faced. It therefore stays subject to the existing ownership, weak-match (< 30) and path/payload-identity guards. An earlier draft called #ensureBabysitterForIssue directly and broke two existing guard tests (AR-408 body-only reference, AR-430 payload/path number mismatch) by spawning babysitters those guards exist to prevent — replaying through the validated path fixes that by construction.
If a PR is known from a dispatch receipt but its meta has not materialized in the mount, adoption skips it (counted as babysitterOrphanPrMetaUnavailable) and retries next sweep, rather than spawning off an unvalidated receipt.
3. Failures are visible at default log level.
could not read PR snapshot:debug→warn, escalating toerroronce sweep retries also fail. The escalated log is rate limited (every 20th failure) so a durably faulted mount cannot flood.ignored unowned PR event: warns once per PR identity (capped at 64), then falls back todebug. The cap gates the warn itself, so an unbounded set of identities cannot become an unbounded warn stream.
4. Counters are surfaced. A periodic warn reports babysitterPrSnapshotReadFailures, babysitterEventsIgnoredUnownedPr, babysitterFlatEventsUnreadable, adoption count, and dead-letter depth — plus the oldest stuck path, its error and its age, which is what an operator actually needs to act on. babysitterPrSnapshotDeadLetterDepth is also published into status().counters.
Bounding
- Dead-letter book capped at 256, oldest evicted first.
- Per-sweep drain capped at 16. Failed retries re-insert at the end, so successive sweeps rotate through the whole book instead of stalling behind it.
- Adoption runs on a 60s cadence (it probes for PRs); dead-letter retries are not gated by that cadence, since a dead-lettered read is a PR already known to be unshepherded.
Tests
Four new regression tests, plus the full suite green (1556 passed, 83 files).
still spawns the babysitter when the PR-open snapshot read fails transiently— first read throwsfetch failed; the babysitter still spawns and nothing is dead-lettered.dead-letters an exhausted PR snapshot read and recovers it on the reconcile sweep— mount stays faulted, read exhausts, no spawn,warnemitted; mount recovers, sweep adopts. The probe resolver returnsundefinedhere so recovery can only come from the dead-letter drain.adopts an already-open PR that no babysitter owns on the reconcile sweep— no PR-open event is emitted at all; the sweep adopts, and a second sweep does not respawn.warns once per unowned PR identity instead of silently dropping its events at debug.
One existing assertion changed: AR-408's babysitterPrDiscoveryWeakMatchIgnored moves from toBe(1) to toBeGreaterThanOrEqual(1). The sweep replays that PR meta, so the guard is exercised once per delivery attempt. It rejects every time; the no-spawn assertion is unchanged.
On verifying against cloud#3024
The issue asked to verify against AgentWorkforce/cloud#3024. That PR has since been closed (state: CLOSED, still CONFLICTING/DIRTY), so I could not verify against it live.
What I did verify: its head branch is factory/3022-chief-org-live-population, and factoryBranchMatchesIssue('factory/3022-chief-org-live-population', '3022') matches the factory/<issue>- prefix, scoring 30 — at the weak-match floor, so it passes. Given an in-flight record for that issue and its PR meta in the mount, the sweep would have adopted it. The adopts an already-open PR that no babysitter owns test reproduces exactly that shape.
Scope
Per the issue: branched from origin/main (the local codex/222-routed-pr-babysitter-v2 WIP was left untouched), mergePolicy: never — stopping at review. Kept distinct from #222 (babysitter scope) and #221 (babysitter write identity); this only addresses in-scope PRs never being routed at all.
🤖 Generated with Claude Code
Summary by cubic
Retries and reconciles PR-open snapshot reads so a failed read can’t orphan a PR. Previously, a single mount read failure at PR open logged at debug, never spawned a babysitter, and caused all later PR events to be dropped; now reads retry with backoff, dead-letter on exhaustion, and a sweep reconciles orphaned PRs. Addresses #243 by making orphaned PRs recoverable and failures observable.
- PR-open read now retries 3× with linear backoff; on exhaustion the path is dead-lettered. Only the
readFileis retried;parsePullSnapshotremains non-throwing and is not dead-lettered. - Reconcile sweep drains dead letters and adopts ownerless in-flight PRs by replaying PR meta through
#handlePrChange(respects ownership, weak-match, and path/payload identity guards). Cadence 60s; dead-letter retries run each sweep. Bounds: dead-letter book 256; drain 16 per sweep. - Visibility: unreadable snapshot warns, escalates to error after repeated sweep retries (rate-limited). Unowned PR events warn once per PR identity (cap 64), then debug. Periodic summary logs counters and oldest stuck path.
- Counters:
babysitterPrSnapshotReadFailures,babysitterPrSnapshotReadRetries,babysitterPrSnapshotReadRetrySucceeded,babysitterEventsIgnoredUnownedPr,babysitterFlatEventsUnreadable,babysitterOrphanedPrsAdopted, plusbabysitterPrSnapshotDeadLetterDepthinstatus().counters. - Tests: 4 new regressions cover transient retry, dead-letter recovery, orphan adoption, and unowned-event logging. AR-408 assertion loosened to
toBeGreaterThanOrEqual(1)since the sweep replays the guarded path.