Skip to main content

Module user_sync

Module user_sync 

Source
Expand description

One sync layer for a user’s own data (docs/specs/SPEC_USER_DATA_SYNC_LAYER_2026_11_01.md): a change log of signed, versioned records and the rules that make a collection’s copies on several machines converge. This is the core (build step S0): the clock, the record, the log and its state, and the adapter a kind of collection implements. Nothing here sends anything; the transports come after it.

Modules§

adapter
What a kind of collection says about merging (SPEC_USER_DATA_SYNC_LAYER §3.3): the layer calls its adapter with the record’s current header and the incoming one, and the adapter answers. The layer never decides what a conflict means.
chain_store
A membership chain kept in the store, so a restart does not forget a collection’s members, its key epoch’s removals or a fork not yet outgrown. What is read back is checked again, entry by entry, as a chain received from a peer is, and against the length and head stored beside it: a stored chain that was altered (an entry gone from its middle or its end) is an error, never an empty or a shorter collection.
hlc
The hybrid logical clock that orders a collection’s records (SPEC_USER_DATA_SYNC_LAYER §3.2): wall milliseconds, a counter for changes in the same millisecond, and the machine that made the change, so any two versions are totally ordered. A version far ahead of the receiver’s own clock is refused, so a machine with a wrong clock cannot win every merge.
kanban
The Kanban’s side of the sync layer (SPEC_SHARED_KANBAN_ACROSS_MACHINES §3.3, build step K1): what a board’s tasks look like as records in the change log, and the merge rule for each kind of record.
keys
A collection’s keys on a machine, and how they reach another member (SPEC_USER_DATA_SYNC_LAYER §3.1). A collection has one key for each key epoch it has been in. A key is made on the machine that founds the collection or removes a member, kept by a KeyStore, and handed to another member sealed to that member’s box key (the key its install offers for sealing, derived from its WAN seed and signed into the membership chain), one sealed key for each epoch, so a new member can open what was written before it joined. A key is never sent unsealed, and a machine that is not a current member is given none.
lock
The computer-wide sync lock (SPEC_USER_DATA_SYNC_LAYER §3.1). The identity store, and with it the Kanban and the Vaults, is shared by every AgentMux on a computer, so only one install may act as the computer’s member: sign records as it, run the sync loop and answer sync.* ops. That install holds this lock, a single row in the shared store taken by compare-and-set with a lease. An install that does not hold it never signs as the member and never runs the loop; a crashed holder is replaced when its lease runs out. Each taking raises a generation, so a holder that was replaced finds its renewal refused and stops (a fence, like a task claim’s attempt).
log
The change log and the state tables beside it (SPEC_USER_DATA_SYNC_LAYER §3.2, §5). Every change, made here or taken from another machine, is one row of db_sync_log, keyed by its origin machine and that machine’s sequence number, so a catch-up is “everything after these sequences” and applying a record twice changes nothing. db_sync_state points at the log row that is each record’s current state. db_sync_mark is the high-water mark of the head last accepted from a store, so an older head is refused (rollback).
members
A collection’s members (SPEC_USER_DATA_SYNC_LAYER §3.1): one signed, hash-chained log of membership changes. The first entry founds the collection and is signed by the founder; every later entry is signed by a machine that is a member at the point it extends. A client takes an entry only if it verifies against the signer’s key as the chain lists it and it extends the chain it holds, so a change that no member signed is refused, wherever it came from.
ops
The bodies of the sync.* ops as pure functions (SPEC_USER_DATA_SYNC_LAYER §3.1, §4): hello, pull, push, members and join, over the log, the membership chains and the caller’s proven machine id. Nothing here opens a connection or sends anything: a request comes in, an answer goes out, and what a function may touch is what Ctx hands it. The transport (the remote-ops route on the LAN) is wired to these separately.
record
A record as it leaves a machine (SPEC_USER_DATA_SYNC_LAYER §3.2): a fixed, signed header in the clear and a sealed body. The header is signed by the origin machine’s key over every field and a digest of the body, so no other member and no relay can write a record “from” a machine. The clear header is a fixed list (CLEAR_FIELDS); a collection’s name and everything descriptive lives in the sealed body.
round
One round of sync with one peer, for one collection (SPEC_USER_DATA_SYNC_LAYER §4.2, the loop’s body): the asking side of the sync.* ops. A round learns the membership changes each side lacks (sync.members), takes the keys of any epoch this machine lacks (sync.keys), pulls the records it lacks, and pushes the ones the peer lacks. It is pure over a PeerCalls (the calls to the peer) and a Session (this machine’s own state): a transport hands it both and it never opens a connection, so it is tested against in-memory machines. A Session lends the round this machine’s Ctx only for the length of one closure, so a transport that keeps the store locked while a closure runs never keeps it locked across a call to the peer (which is itself asking this machine for the same store). Which peers, when, and what wakes the loop belong to the transport.
seal
Sealing a record’s body (SPEC_USER_DATA_SYNC_LAYER §3.1). A body leaves a machine sealed with the collection’s key at the record’s key epoch: ChaCha20-Poly1305, with the collection id, record id, version, base version and key epoch as associated data, so a body cannot be replayed under another record, version, base or epoch, nor moved into another collection. The seal is the Vault’s (vault::sync_seal) under this module’s associated-data prefix, so there is one implementation. The layer treats a body as opaque bytes: the caller seals before a record is written (sealer with log::append_local_with) and opens after one is applied (open).