Skip to content

fix(persistence): allocate collision-free checkpoint snapshot IDs - #39

Merged
4444J99 merged 2 commits into
mainfrom
ranked-queue/issue-38-checkpoint-collision
Nov 15, 2026
Merged

4444J99 merged 2 commits into
mainfrom
ranked-queue/issue-38-checkpoint-collision

Conversation

@4444J99

@4444J99 4444J99 commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

Closes #38.

Defect (reproduced pre-fix at exact base)

CheckpointManager.create_checkpoint() derived snapshot_id to whole-second precision (SNAP_{now.strftime('%Y%m%d_%H%M%S')}). With datetime.now() fixed to one instant for two calls:

  • both checkpoints returned the same ID (SNAP_20261023_231500)
  • only one snapshot file remained on disk
  • CHECKPOINT_REGISTRY.json held two entries with that ID
  • get_checkpoint_by_name("first") loaded the SECOND checkpoint's state (data loss)

Fix

  • rege/persistence/checkpoint.py: new _allocate_snapshot_id() — keeps the SNAP_YYYYMMDD_HHMMSS base with a zero-padded _NNN sequence suffix (the layout the module docstring already documents; mirrors rege/protocols/recovery.py). The sequence is derived from IDs claimed in the checkpoint registry plus snapshot files on disk, so same-second creates get distinct IDs even when the clock never advances, across manager instances and restarts.
  • Save-then-register ordering unchanged: a failed snapshot write raises PersistenceError before any registry entry is created (fail closed).
  • rege/tests/test_checkpoint_collision.py (new): 5 regression tests — same-second distinct IDs/files, per-entry state resolution, cross-instance sequence continuation, fail-closed write failure, legacy unsuffixed-ID coexistence.

Scope honored: only checkpoint.py + the new regression test; no workflow, packaging, or archive changes. models.py's StateSnapshot fallback keeps its existing behavior (out of scope, noted for follow-up).

Verification (local, base 685e682 worktree)

  • Issue repro post-fix: distinct IDs _001/_002, two files, each name loads its own state — PASS
  • python -m pytest -q rege/tests/test_checkpoint_collision.py → 5 passed
  • python -m pytest -q rege/tests/test_persistence.py rege/tests/test_persistence_coverage.py → 47 passed, 4 failed — the 4 failures are pre-existing environment failures (permission-denied tests running as root); verified identical on pristine base, untouched assertions.

Base / head

Draft per the issue's delivery boundary (no merge/release authority granted). CI must be honestly green at the exact head before this counts as done.

Summary by Sourcery

Allocate collision-free checkpoint snapshot IDs to prevent same-second saves from causing data loss.

Bug Fixes:

  • Prevent checkpoint snapshots created within the same second from sharing IDs and overwriting each other.
  • Preserve checkpoint creation failure semantics by avoiding registry entries when snapshot persistence fails.

Enhancements:

  • Ensure snapshot ID allocation remains collision-free across existing registry entries, on-disk snapshots, manager instances, and restarts, while retaining compatibility with legacy unsuffixed IDs.

Tests:

  • Add regression coverage for same-second ID uniqueness, state resolution, cross-instance sequence continuation, failed writes, and legacy snapshot coexistence.

Closes #38.

CheckpointManager.create_checkpoint() derived snapshot_id to whole-second
precision, so two checkpoints created within the same second shared one ID:
the second write replaced the first snapshot file while CHECKPOINT_REGISTRY.json
kept two entries, and loading the first name returned the second state
(reproducible data loss).

The fix allocates IDs as SNAP_YYYYMMDD_HHMMSS_NNN (the layout already
documented in the module docstring, mirroring rege/protocols/recovery.py),
deriving the sequence from IDs claimed in the checkpoint registry and
snapshot files on disk so allocation stays correct across manager instances
and process restarts, even when the clock does not advance between calls.

Save-then-register ordering is unchanged: a failed snapshot write raises
PersistenceError before any registry entry is created (fail closed).

Tests: rege/tests/test_checkpoint_collision.py (5 regression tests:
same-second distinct IDs/files, per-entry state resolution, cross-instance
sequence continuation, fail-closed write failure, legacy unsuffixed ID
coexistence). Verified: new file 5 passed; rege/tests/test_persistence.py +
test_persistence_coverage.py 47 passed with 4 pre-existing environment
failures (root-user permission tests, identical on pristine base).
@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@sourcery-ai

sourcery-ai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Allocate a timestamp-based snapshot ID with the next sequence unused by the registry and on-disk snapshots, and use it when creating checkpoints to prevent same-second overwrites.

  • rege/persistence/checkpoint.py L61, L84-105

Tests

Add regression tests covering same-second ID uniqueness and state lookup, allocation across manager instances, failed writes, and coexistence with legacy IDs.

  • rege/tests/test_checkpoint_collision.py L1-116

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

4444J99 commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Green progress receipt (exact-head 8bde311): Python CI workflow completed with success on this draft PR. Core tests for the collision-free snapshot ID allocation are green. Awaiting remaining checks before any further progress.

@4444J99
4444J99 marked this pull request as ready for review November 15, 2026 03:07
@sourcery-ai

sourcery-ai Bot commented Nov 15, 2026

Copy link
Copy Markdown

Sorry @4444J99, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 14 hours and 55 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T03:08:10.346431Z cc40c59 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@4444J99
4444J99 merged commit 89b6d8f into main Nov 15, 2026
8 of 10 checks passed
@4444J99
4444J99 deleted the ranked-queue/issue-38-checkpoint-collision branch November 15, 2026 03:08
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent same-second checkpoint ID collisions from overwriting snapshots

1 participant