Skip to main content

Module crdt_sync

Module crdt_sync 

Source
Expand description

State-vector sync protocol primitive (#crdtauth1sv).

The deferred half of #crdtauth1. Where crate::crdt::merge_by_component rebuilds throwaway yrs docs from text and runs a whole-.yrs-snapshot merge every cycle, this module models the incremental per-replica state-vector exchange the CRDT-authority plan calls for (tasks/agent-doc/plan-crdt-authority-model.md, phase 2):

  • Each ReplicaState keeps its yrs Doc across cycles (a durable replica), instead of being rebuilt from text each merge.
  • A sync exchanges only the updates the other side is missing: peer.encode_state_as_update(my_state_vector). The state vector is a compact per-client causal summary, not the whole document — so a sync ships a delta, not a snapshot.
  • Convergence is commutative, associative, idempotent, and yrs buffers an update whose causal dependencies have not arrived yet (causal buffering), so out-of-order / lagged delivery self-heals once the gap is filled — never a permanent conflict, never data loss.

This is the protocol primitive + tests only. Rewiring the live merge_by_component call sites onto it and wiring the FFI-node ↔ supervisor channel are follow-up rungs. The authority gate (sync only under CrdtAuthority::MultiReplica; the GitAuthoritative / ephemeral path keeps rebuilding from git) lives in agent-doc-orchestration over this primitive, since the authority layer lives there.

Structs§

ReplicaState
A durable per-replica CRDT state — one participant (an editor’s FFI node, or the supervisor) in a multi-replica document session.

Functions§

commit_barrier_ready
State-vector commit barrier (#crdtauth3): is canonical a consistent cut — has it integrated every live editor’s ops up to that editor’s current state vector?
flush_to_commit_barrier
Drive the commit barrier: flush every live editor’s missing ops into canonical (“flush all live editors to SV=N”), then confirm the barrier is satisfied. After this returns Ok(true), a snapshot of canonical (ReplicaState::encode_state) is a consistent cut safe to write to git.
sync
One incremental sync round between two replicas: each announces its state vector, receives only the updates it is missing, and applies them. After the round both replicas have converged. This models a single FFI-node ↔ supervisor exchange; repeated rounds (or one round per delivered op) keep live replicas in sync without ever shipping a whole-document snapshot after the first contact.