Skip to content

Reconcile offline edits with a hybrid logical clock - #81

Merged
j1philli merged 2 commits into
mainfrom
t3/store5-offline-conflict-handling
Nov 17, 2026
Merged

j1philli merged 2 commits into
mainfrom
t3/store5-offline-conflict-handling

Conversation

@j1philli

Copy link
Copy Markdown
Owner

Summary

Offline edits from different devices used to be reconciled by device-clock last-writer-wins. Two problems followed: a device with a slow clock could lose an edit it made after seeing a peer's change, and concurrent edits to unrelated fields overwrote each other. This PR replaces that with:

  • Hybrid logical clock (Hlc / HybridLogicalClock in EntityVersions.kt): versions advance past any version a device has seen, so causally later edits win regardless of wall-clock skew.
  • Per-field-group LWW registers: each entity keeps a version per field group, so concurrent edits to different fields both survive.
  • Versioned tombstones: deletes carry an HLC version. An entity exists only while some field version is newer than its delete, so offline deletes reach peers and merges are order-independent.
  • entity_state table (EntityState.sq, migration 2.sqm): stores the last known field values, versions and tombstones per space/entity. It is kept after a delete, cleared by clearAllSyncMetadata (so "Start over" and account deletion wipe it), and claimed with the other legacy-space tables.
  • ChangeMessage gains optional versions / deletedAt (null from older peers). SyncStoreFacade, the Store5 stores, LinkedTransferStore and IncomingChangeRouter merge through the registers.
  • DatabaseBootstrap upgrades existing space-scoped databases that lack entity_state in place (by checking for the table, not trusting user_version) rather than dropping data.

This branch also merges the latest main (#66–#75). It merged with no conflicts; the new account-deletion path wipes entity_state through wipeAll() → clearAllSyncMetadata.

Tests

New: EntityVersionsTest (12 tests, e.g. anEditMadeAfterSeeingAPeersChangeWinsEvenWithASlowClock, mergeIsOrderIndependentAndIdempotent) and OfflineConflictTest (8 tests). Updated: the store, bootstrap (v2 → entity_state upgrade), snapshot and serialization tests.

TeamCity CI steps run locally on the merged branch, all passing:

  • npm run test:security — 4 passed
  • scripts/ci/test-glib-security.sh — passed
  • npm audit (root + smoke) — 0 vulnerabilities
  • scripts/enforce-architecture.sh — passed
  • python3 -m unittest discover -s scripts/ci/tests — 71 OK
  • scripts/ci/check-version.py — 0.1.3 OK
  • scripts/ci/run-jvm-tests.sh — 230 tests, 0 failures, 0 errors (first attempt)
  • scripts/ci/test-web-database.cjs — passed
  • scripts/ci/test-hosted-spaces.sh — passed
  • ./kotlin build -m server — passed

Other compiles: shared for wasmJs / iosSimulatorArm64 / android, web-app, perf-android, and the perf-ios Kotlin compile all succeed. The perf-ios Xcode packaging step was not run locally because this clone doesn't have the gitignored WebRTC.xcframework.

🤖 Generated with Claude Code

j1philli and others added 2 commits November 17, 2026 09:01
Replace device-clock last-writer-wins with a hybrid logical clock (Hlc /
HybridLogicalClock in EntityVersions.kt), per-field-group LWW registers and
versioned tombstones, persisted in a new EntityState table (migration 2.sqm).
Stores, the sync facade and IncomingChangeRouter merge remote changes through
these registers so merges are order-independent and idempotent, and an edit
made after seeing a peer's change wins even when the local clock is slow.

Adds EntityVersionsTest and OfflineConflictTest and updates existing store,
bootstrap and serialization tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@j1philli
j1philli merged commit 9ef4743 into main Nov 17, 2026
1 check passed
@j1philli
j1philli deleted the t3/store5-offline-conflict-handling branch November 17, 2026 15:22
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