Repository navigation
fix(maintenance): enforce rebuild pass deadlines inside replay work - #3494
Conversation
Problem: rebuild_index_from_source checked a bounded pass's deadline only AFTER the whole page's replay_source() call (census + every cohort + planner-stats refresh) returned. Live recovery evidence: operation 3f8fa7b0 configured pass_deadline_ms=300000, yet 100-row pages ran roughly 8-9 minutes -- the advertised bounded-pass contract was not enforced at the work boundary. A page can also expand into a much larger authority cohort, making the overshoot unbounded in the worst case. What changed: backfill_historical_revision_evidence (sources/revision_backfill.py) now accepts an optional deadline_check callback, invoked at the top of every REPLAY-phase cohort iteration (both the byte-cohort and membership-cohort loops) -- between cohorts, not only after the whole call returns. It is expected to raise the new RebuildDeadlineExceededError to interrupt. maintenance/replay.py threads the parameter through unchanged. rebuild_index.py builds a deadline_check closure from the resumable transaction's pass_deadline_ms and catches RebuildDeadlineExceededError around the replay_source() call. On interrupt it deliberately does NOT advance the transaction's cursor/processed counters -- the next pass re-derives from the exact same source-order position, which is safe by the existing content-hash idempotency invariant (re-applying an already-committed cohort is a no-op upsert, never a duplicate) and matches the crash-recovery contract an open replay batch already has. The deferred receipt is zero-shaped like a normal replay dict so every existing consumer that indexes replay-dict keys unconditionally (the daemon-mode CLI text formatter, the daemon HTTP route's raw receipt.to_dict() response) keeps working. This diff also carries polylogue-6mvg's small remaining slice (RebuildPassCost.selection_s + the selection-phase timing measurement around next_raw_page/select_rebuild_raw_ids) because it shares the same function bodies as the deadline fix above and could not be split into a separate commit without fragmenting a single hunk; see the following commit for its dedicated test. Alternatives rejected: proactive page-sizing against a throughput estimate (checks only BEFORE a page, not between cohorts inside it, so still misses the description's own worst case) and per-raw interruption inside apply_raw_revision_replay itself (would need to interrupt mid-transaction-write, corrupting the owned-inactive- generation state) -- both noted as considered-and-deferred in the bead's own prior investigation. Verification: devtools test tests/unit/maintenance/test_rebuild_index_deadline.py tests/unit/maintenance/test_rebuild_index_phase_timing.py tests/unit/storage/test_incremental_rebuild_equivalence.py tests/unit/daemon/test_bulk_rebuild_ownership.py tests/unit/cli/test_archive_maintenance_cli.py tests/unit/daemon/test_route_contracts.py tests/unit/maintenance/test_rebuild_index_bulk_build.py tests/unit/maintenance/test_rebuild_index_ownership.py tests/unit/maintenance/test_rebuild_index_selection.py tests/unit/maintenance/test_rebuild_parse_apply_split.py tests/unit/storage/test_index_generation.py -- 213 passed. devtools verify --quick -- all 18 steps ok, exit 0. Ref polylogue-uhgm Ref polylogue-6mvg Co-Authored-By: Claude <[email protected]>
Problem: polylogue-6mvg's remaining scope after prior sibling beads (planner stats #3141, byte-skip #3146, batched commits #3147, pool floor #3149, dedup #3151) was durable phase-timing telemetry -- a live full rebuild spent ~86s of one CPU core choosing which raws to replay before the inactive generation held a single session, with nothing durable recording where that time went. The prior commit on this branch already added RebuildPassCost.selection_s and the matching "selection_s" key on the terminal/materialized receipt's timings_s (extending the existing timings_s/RebuildPassCost vocabulary rather than inventing a parallel one -- see polylogue-o56w's terminal.* stages and the existing parse_s/apply_s split it sits beside). What changed: this commit adds the dedicated test coverage for that addition, both on the one-shot "replayed" receipt path and on a resumable pass's "deferred" pass-cost receipt, proving the same key appears on every receipt shape. Anti-vacuity: the production caller exercised is rebuild_index_from_source_sync (the real offline-rebuild orchestrator, no reimplementation). The mutation that makes these tests fail is reverting the selection_elapsed_s measurement or its selection_s=selection_elapsed_s threading into RebuildPassCost / terminal_timings_s in rebuild_index.py -- the "selection_s" key would then be absent from receipt.timings_s entirely (KeyError via dict indexing), not merely zero. Verification: devtools test tests/unit/maintenance/test_rebuild_index_phase_timing.py -- 2 passed. Ref polylogue-6mvg Co-Authored-By: Claude <[email protected]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
Next review available in: 25 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Rebased perf/sharded-from-empty-rebuild onto origin/master (merged since branch point: #3494 deadline_check, #3496 pipeline-decode pin, #3497 conversational-evidence gate, #3498 lineage fixture fix). Conflicts resolved in rebuild_index.py by keeping both #3494's mid-replay deadline checkpointing (non-sharded path) and this PR's shard dispatch, with shard_count>1 + pass_deadline_seconds explicitly rejected (validator and CLI) since the sharded path has no deadline_check seam yet. Ref polylogue-pzxm Co-Authored-By: Claude <[email protected]>
Summary
Two beads, one branch: (1) a P1 correctness-of-contract bug fix — a resumable
rebuild pass's deadline is now checked between REPLAY-phase cohorts, not
only after the whole page's replay call returns — and (2) the small remaining
slice of a P2 observability bead, a durable "selection phase" timing on every
rebuild receipt.
Problem
polylogue-uhgm (P1): live recovery evidence — operation
3f8fa7b0configured
pass_deadline_ms=300000(5 minutes), yet 100-row pages ranroughly 8-9 minutes, because
rebuild_index_from_source(
maintenance/rebuild_index.py) checked elapsed time only afterreplay_source(...)— the whole page's census + every cohort + planner-statsrefresh — returned. A page can also expand into a much larger authority
cohort, making the overshoot unbounded in the worst case. The advertised
bounded-pass contract was not enforced at the work boundary.
polylogue-6mvg (P2, remaining slice): a live full index rebuild spent
~86s of one CPU core selecting which raws to replay before the inactive
generation held a single session, and nothing durable recorded where that
time went. Most of this bead's original scope (planner stats #3141,
byte-skip #3146, batched commits #3147, pool floor #3149, dedup #3151) had
already shipped via sibling beads; the bead's own notes call out durable
phase-timing telemetry — specifically a selection-phase breakdown — as the
one still-unowned piece.
Solution
polylogue/sources/revision_backfill.py:backfill_historical_revision_evidencegains an optional
deadline_check: Callable[[], None] | Noneparameter,called at the top of every REPLAY-phase cohort iteration (both the
byte-cohort and membership-cohort loops) — between cohorts, not only after
the whole call returns. New
RebuildDeadlineExceededErroris the expectedinterrupt signal.
polylogue/maintenance/replay.py: threadsdeadline_checkthroughunchanged to
backfill_historical_revision_evidence.polylogue/maintenance/rebuild_index.py:_check_pass_deadlineclosure from the resumable transaction'spass_deadline_ms, wired only when a durable transaction exists (neverfor non-resumable
--raw-id/--only-missing/--max-blob-mbselections,matching existing validation).
RebuildDeadlineExceededErroraround thereplay_source()call.On interrupt it deliberately does not advance the transaction's
cursor/processed counters — the next pass re-derives from the exact same
source-order position, safe by the existing content-hash idempotency
invariant (re-applying an already-committed cohort is a no-op upsert,
never a duplicate) and consistent with the crash-recovery contract an
open replay batch already has.
replaydict is zero-shaped like a normal replaydict (not a bare marker), so every existing consumer that indexes
replay-dict keys unconditionally (the daemon-mode CLI text formatter, the
daemon HTTP route's raw
receipt.to_dict()response) keeps working.RebuildPassCost.selection_s(polylogue-6mvg) extends the existingtimings vocabulary — the same
RebuildPassCost/timings_sshape everyother phase (
parse_s/apply_s, theterminal.*stages frompolylogue-o56w) already uses — rather than inventing a parallel one.
Measured around
next_raw_page(resumable path) andselect_rebuild_raw_ids(one-shot path), threaded into all threereceipt-building call sites (deadline-interrupt, deferred/paused
post-hoc, final materialized).
Alternatives rejected: proactive page-sizing against a throughput
estimate (checks only before a page starts, so still misses a
mid-page-expanding cohort — the exact worst case the bead names) and
per-raw interruption inside
apply_raw_revision_replayitself (wouldinterrupt mid-transaction-write, risking corruption of the owned-inactive-
generation state). Both were flagged and deliberately deferred in the
bead's own prior investigation as too risky for the intended scope.
Conflict avoidance note for the merge conductor
polylogue/sources/revision_backfill.pyis flagged as owned by another livelane. This PR's addition there is the smallest possible: one new exception
class, one new optional parameter, and two
if deadline_check is not None: deadline_check()call sites at the top of the two existing cohort loops —no other line touched. The branch was rebased onto current
master(
90d4df67e, which includes the sibling lane'spolylogue-5q2ulineage-orderreplay change,
_lineage_aware_replay_order); the rebase conflict wasexactly the loop header line and was resolved by keeping the new
ordered_logical_keysiteration and re-adding thedeadline_check()call —verified with a fresh test run post-rebase.
Acceptance-criteria matrix
polylogue-uhgm:
would exceed its remaining budget, commits a valid cursor, and reports
deadline deferral." — Satisfied.
test_rebuild_index_deadline_stops_mid_page_and_resumes_without_omission_or_duplication.duplicates or omissions." — Satisfied (by construction: the cursor is
never advanced on interrupt, and resumption re-derives from the identical
keyset position; idempotent re-application makes any redone cohort safe).
Proven end-to-end by the same test (interrupted pass → resume → exact final
session count).
production replay work rather than only after the outer call returns." —
Satisfied.
test_deadline_check_invoked_between_replay_cohorts_not_only_after_returnproves the interrupt fires after exactly one cohort durably commits (not
zero, not all three) at the direct
backfill_historical_revision_evidenceboundary.
bounded receipt or are explicitly separately scheduled." — Unchanged:
no terminal-stage code was touched;
terminal.*timings (polylogue-o56w)remain as before.
polylogue-6mvg (remaining slice only — full bead AC already satisfied by
sibling beads per its own 2026-07-19/27 notes):
RebuildPassCost.selection_s/terminal_timings_s["selection_s"],present on every receipt shape (deferred, paused, replayed).
work (
stage_timings_sfromrevision_backfill.py,parse_s/apply_ssplit,
terminal.*stages) — not re-touched here.daemon/bulk_rebuild.py'srun_daemon_bulk_rebuild_passreturns the exactsame
RebuildIndexReceiptfrom the samerebuild_index_from_source_syncengine, so both the deadline fix and the selection-phase timing apply
automatically with zero daemon-side changes needed.
Anti-vacuity
rebuild_index_from_source_sync(thereal offline/daemon rebuild engine, no reimplementation). The mutation that
makes
test_deadline_check_invoked_between_replay_cohorts_not_only_after_returnfail is moving the
deadline_check()calls out of the cohort loops back toa single call after
backfill_historical_revision_evidencereturns (thepre-fix bug shape) — the mock would then never be called before the
function's own natural completion. The mutation that makes
test_rebuild_index_deadline_stops_mid_page_and_resumes_without_omission_or_duplicationfail is either reverting to the post-hoc-only check (the fake clock would
never interrupt anything mid-replay, so the first pass would report
status="replayed"instead of"deferred") or advancing the transaction'scursor on interrupt (the resumed pass would then skip or duplicate a raw,
and the final session count would not equal 3).
rebuild_index_from_source_sync.The mutation that makes
test_rebuild_index_phase_timing.py's two testsfail is reverting
selection_elapsed_s's measurement or itsselection_s=selection_elapsed_sthreading — the"selection_s"key wouldthen be absent from
receipt.timings_s(a dict-key assertion failure, notmerely a zero value).
Verification
Known pre-existing failure, unrelated to this branch — filed as
polylogue-s0ug:tests/unit/storage/test_incremental_rebuild_equivalence.py::test_incremental_restart_and_fresh_generation_rebuild_are_equivalentfails deterministically on current
origin/master(90d4df67e) in isolation,introduced by
0d27e3da7(#3484, tightened Codex CONTINUATION lineageevidence) landing after this branch was cut — the test's hand-built fixture
predates that tightening.
git diff origin/master...HEADfor this branchtouches neither
codex.py,revision_backfill.py's classification logic,nor that test file, confirming it is not caused by this change.
Not run:
devtools verify(full testmon suite) — the coordinator directed asynchronous quick-gate + focused-test path instead of the backgrounded
testmon seed, since a fresh worktree has no testmon seed to run the default
affected-selection path against.
Ref polylogue-uhgm
Ref polylogue-6mvg
Co-Authored-By: Claude [email protected]