Repository navigation
fix(sync): stamp each op a device writes after its newest one - #3077
Merged
Merged
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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, andcreated_atcame 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:It affected coordinator-scoped and coordinator-free pulls and pushes.
What changes:
updated_at, device) stays on real time. Only the op's position in the stream is stamped.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:
mainbehaves the same way.Type of Change
Testing
pnpm run tscandpnpm run lint:delta -- --base mainpass.New tests in
replication-op-stamp.test.ts(core),replication-op-stamp-sync.test.tsandsync-cleanup-diagnostics.test.ts(viewer server) all fail onmainand pass here. They cover: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
pnpm run lintpasses for touched files)