Skip to content

fix(sync): stamp each op a device writes after its newest one - #3077

Merged
kunickiaj merged 3 commits into
mainfrom
fix/monotonic-op-stamps
Nov 29, 2026
Merged

kunickiaj merged 3 commits into
mainfrom
fix/monotonic-op-stamps

Conversation

@kunickiaj

@kunickiaj kunickiaj commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Description

A device could permanently miss a change made on a peer. Peers read a device's ops in (created_at, op_id) order and continue after the last op they received. The op ID is random, and created_at came from the wall clock before the write lock was held. So an op could sort before the point a peer had already read to, and that peer never received it. That happened in three cases:

  • an op in the same millisecond as the device's previous one, with an op ID that sorts lower;
  • an op whose process read the clock and then waited for the write lock while another process committed a later op;
  • an op written after the clock stepped back.

It affected coordinator-scoped and coordinator-free pulls and pushes.

What changes:

  • Each op a device writes is stamped while it holds the write lock, at the later of the time it asks for and 1 ms after its newest op and the retained floor. All four insert paths use it: recorded ops, scope moves, access cleanup and backfill. The newest op is read with the existing status index, one lookup per op type, and read back from the newest ops when that index is missing.
  • The memory's conflict clock (revision, updated_at, device) stays on real time. Only the op's position in the stream is stamped.
  • Pruning never moves a scope's retained floor backward. Before, pruning an op received after an earlier prune could lower the floor below ops a peer had already read.
  • When the scope backfill places one of this device's unscoped ops in a scope other than the default, it also writes a copy of the op under a fresh stamp, with the same content, conflict clock and stored copy. Without the copy, the op became eligible at its old position, behind peers that had already read past it, so a deletion could be missed.
  • Cleanup diagnostics report the cleanup's own time as latest_at, instead of the op's stream position, which can be ahead of the clock.

Nothing changes on the wire or in the cursor format, so mixed versions keep syncing (0.50 with 0.49.1, and beta.5 with beta.6). A device that hasn't updated can still serve its own ops out of order until it does.

Known limits:

  • Ops a peer skipped before this change stay skipped until the memory is edited again. Recovering them would mean rewinding each stored pull cursor once and pulling again.
  • The scope backfill doesn't yet send again an op it places in the default scope. Peers are served no unscoped op, so a caught-up peer can miss such an op, for example a deletion. main behaves the same way.

Type of Change

  • 🚀 Feature (new functionality)
  • 🐛 Bug fix (fixes an issue)
  • 📚 Documentation (docs-only change)
  • 🔧 Maintenance (refactor, chore, CI, etc.)
  • 🧪 Testing (test-only changes)

Testing

  • pnpm run tsc and pnpm run lint:delta -- --base main pass.

  • New tests in replication-op-stamp.test.ts (core), replication-op-stamp-sync.test.ts and sync-cleanup-diagnostics.test.ts (viewer server) all fail on main and pass here. They cover:

    • the three cases, through coordinator-scoped pull and push over a channel and coordinator-free pull and push;
    • each of the four insert paths;
    • a second process that reads the clock and waits for the write lock while another commits;
    • pruning that would move the floor back, by age and in bulk, followed by a clock step back;
    • a deletion the scope backfill places behind a peer's cursor;
    • a store without the status index, and timestamps stored without milliseconds;
    • cleanup diagnostics.
  • Disabling each new rule in turn makes a test fail.

  • Three existing tests changed because ops written together now get consecutive milliseconds. The project-resume and scope-move tests check stream order instead of equal timestamps, and the snapshot restart test prunes a real op instead of setting the floor to the year 9999.

  • 181 targeted core, viewer server and CLI test files for sync, replication, pruning, backfill, retention and scopes passed (5,649 tests). The full suite runs in CI.

  • Relevant checks pass locally (pnpm run tsc, pnpm run lint, pnpm run test)

  • Added/updated tests for changes

  • Manually verified changes work as expected

Checklist

  • Code follows project style (pnpm run lint passes for touched files)
  • Self-review completed
  • Documentation updated (if needed)
  • No new warnings introduced

Peers page a device's ops in (created_at, op_id) order and continue after
the last one they received. An op ID is random, and created_at came from
the wall clock before the write lock was held, so an op could sort before
a point a peer had already read to and never reach it: one in the same
millisecond with a lower ID, one whose process read the clock while
another committed, or one written after the clock stepped back. This
affected coordinator-scoped and coordinator-free pulls and pushes.

Each op a device writes is now stamped while it holds the write lock, at
the later of the time it asks for and 1 ms after its newest op and the
retained floor. All four insert paths use it: recorded ops, scope moves,
access cleanup and backfill. The memory's conflict clock stays on real
time. Nothing changes on the wire or in the cursor.
…he scope backfill places

Review of fc1b48d:

1. Pruning set the floor to the last op it deleted. An op received after
   a prune can sort before the floor, and pruning it moved the floor
   back. The stamp's lower bound then fell below an op a peer had read,
   so after a clock step back the next local op sorted before that
   peer's cursor. The floor now only moves forward, in the prune's
   transaction, and both prune paths share one delete.
2. The scope backfill placed an unscoped op in a scope's stream at its
   old position, behind peers that had read past it, so a deletion could
   be missed. When it places one of this device's ops in a scope other
   than the default, it also writes a copy with the same content,
   conflict clock and stored copy under a fresh stamp.
3. Cleanup diagnostics reported the newest cleanup op's stream position
   as latest_at, which can be ahead of the clock. They now report the
   cleanup's clock.
Peers are served no unscoped op, so an op the scope backfill places in
the default scope also lands behind a caught-up peer's cursor. Only ops
placed in other scopes are sent again.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 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-29T05:22:28.568441Z dae045a PR opened
ℹ️ 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.

Copy link
Copy Markdown
Owner Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@kunickiaj
kunickiaj merged commit 0b10b07 into main Nov 29, 2026
23 checks passed
@kunickiaj
kunickiaj deleted the fix/monotonic-op-stamps branch November 29, 2026 05:34
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