Skip to content

feat(maintenance): idempotent resumable replay (#1147) - #1161

Merged
Sinity merged 1 commit into
masterfrom
feature/feat/idempotent-replay-execution
May 17, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/feat/idempotent-replay-execution

Conversation

@Sinity

@Sinity Sinity commented May 17, 2026

Copy link
Copy Markdown
Owner

Summary

Adds the execute half of the typed maintenance planner: a per-target
replay loop with idempotent re-runs, durable resume via an on-disk
cursor, isolated per-target failure handling, and structured progress
reporting. Wires it into polylogue maintenance run behind
--operation-id and --resume.

Problem

#1144 landed the typed BackfillOperation contract (with
resume_cursor, failure_samples, metrics, scope) via PR #1157,
but no executor used those fields end-to-end. The existing
execute_backfill path drove run_selected_maintenance as one
atomic op, which meant:

  • a kill mid-run could not be resumed without re-running every target;
  • one failing target aborted the rest;
  • the resume_cursor field on BackfillOperation was always None.

#1147 closes this gap so that maintenance work survives interruptions
without re-doing committed targets and without skipping uncommitted
ones.

Solution

New module polylogue/maintenance/replay.py:

  • execute_replay(config, targets, *, operation_id, resume_cursor, dry_run, persist_state, progress_callback) runs the resolved
    targets in catalog order, dispatching through a typed
    _REPLAY_DISPATCH table to the existing repair functions
    (repair_session_insights, repair_action_event_read_model,
    repair_dangling_fts, repair_message_type_backfill,
    repair_wal_checkpoint).
  • Cursor is the opaque string target:N (next target index) with
    sentinel done for completion. _decode_cursor clamps invalid
    values to 0 so a corrupt state file cannot silently skip work.
  • State is persisted to
    <archive_root>/.maintenance-state/<operation_id>.json after each
    target. Successful completion clears the file; failure leaves it
    so resume picks up where the operation stopped.
  • Per-target raises become typed FailureSample entries
    (kind=ExceptionClassName, locator=target:<name>); repair
    functions reporting success=False become
    RepairReportedFailure; unsupported targets become
    UnsupportedReplayTargetError. Failure samples remain bounded by
    BoundedFailureSamples.from_samples (MAX_FAILURE_SAMPLES=50).
  • ReplayProgress snapshots are passed to an optional callback,
    including operation_id, target, processed/total, cursor,
    and in_flight_failures.

CLI polylogue maintenance run:

  • new --operation-id (resume by id) and --resume (explicit
    cursor) flags;
  • per-target progress lines emitted on stderr;
  • failure-sample block in the post-run summary including kind,
    locator, and message;
  • replaces the previous execute_backfill call.

Topology manifest updated (docs/plans/topology-target.yaml) to
register polylogue/maintenance/replay.py; render-all regenerated
docs/topology-status.md and docs/verification-catalog.md to match.

Alternatives rejected: persisting state in a new SQLite table —
explicitly out of scope for this PR (polylogue/storage/sqlite/ is
owned by another agent worktree). A JSON state file under
archive_root is sufficient for the AC and inherits free atomic
writes via the tmp + replace pattern.

Verification

nix develop -c pytest tests/unit/maintenance/ \
  tests/unit/cli/test_maintenance_registration.py \
  tests/unit/daemon/test_maintenance_endpoints.py -x
77 passed in 10.62s

nix develop -c mypy polylogue/maintenance/replay.py \
  polylogue/cli/commands/maintenance.py \
  tests/unit/maintenance/test_resume.py \
  tests/unit/maintenance/test_idempotency.py
Success: no issues found in 4 source files

nix develop -c devtools verify --quick
exit_code=0 (34.55s)

AC matrix (issue #1147):

  • Running maintenance run --target X twice converges — pinned by
    test_second_run_makes_no_further_changes (second op reports
    affected_rows == 0).
  • Kill mid-run + same operation_id resumes from stored cursor;
    no target rebuilt twice, none skipped — pinned by
    test_kill_mid_run_resumes_from_persisted_cursor (asserts exact
    per-target call counts across the two invocations).
  • FTS, action events, session insights supported — pinned by
    test_supported_targets_cover_ac_required_set.
  • Per-target failure isolation, bounded samples — pinned by
    test_single_target_failure_does_not_abort_others,
    test_repair_reported_failure_surfaces_as_failure_sample,
    test_failure_samples_remain_bounded,
    test_unsupported_target_is_typed_failure_not_silent.
  • Status output includes operation_id / target / processed-total
    / last cursor / in-flight failures — pinned by
    test_progress_callback_fires_per_target; mirrored by the new
    stderr progress block in the CLI.
  • Clean execute, resume after kill, idempotent re-run, single-
    target failure isolation — covered by the new test files.

Closes #1147

🤖 Generated with Claude Code

Adds polylogue/maintenance/replay.py implementing the execute half of
the typed maintenance planner from #1144. Targets supported in this
PR: session_insights, action_event_read_model, dangling_fts (plus
message_type_backfill and wal_checkpoint, which already converge).

Resume substrate is a per-operation JSON state file under
<archive_root>/.maintenance-state/. The cursor encodes the index of
the next target to run; a successful replay clears the file, an
interrupted replay leaves it for resume. The cursor advances after
both successful and failed targets so resume cannot stack duplicate
failure samples or re-run already-completed work.

Per-target failures are isolated: a raise becomes a typed
FailureSample, repair functions reporting success=False are surfaced
as RepairReportedFailure, and unsupported targets are reported as
UnsupportedReplayTargetError rather than silently succeeding.

`polylogue maintenance run` gains --operation-id and --resume, with
per-target progress emitted on stderr so a caller can checkpoint
externally if it owns its own state substrate.

Closes #1147

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 34 minutes and 25 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0fa64b24-0861-4f6d-a289-dcd7ee7331b7

📥 Commits

Reviewing files that changed from the base of the PR and between e843c16 and 9504255.

📒 Files selected for processing (7)
  • docs/plans/topology-target.yaml
  • docs/topology-status.md
  • docs/verification-catalog.md
  • polylogue/cli/commands/maintenance.py
  • polylogue/maintenance/replay.py
  • tests/unit/maintenance/test_idempotency.py
  • tests/unit/maintenance/test_resume.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/idempotent-replay-execution

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Sinity
Sinity merged commit 2b61ab7 into master May 17, 2026
3 checks passed
@Sinity
Sinity deleted the feature/feat/idempotent-replay-execution branch May 17, 2026 05:00

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9504255b4c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread polylogue/maintenance/replay.py Resolved
@Sinity

Sinity commented Sep 30, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

Only the rows listed above are dispositioned. Held findings remain open.

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.

feat(maintenance): idempotent and resumable replay execution

1 participant