# The document, and what the node asks of it A design, built stage by stage ([Stages](#stages), below). So far: what is written here of the node as it is; and of the second stage, what was the engine's in other modules on the engine's side (`document::own`, `dropping`, `screen`), the rules judging a unit at the state its writer saw (`access::rules::judge_seen`), values read and written through a view any kind gives (`document::view`, `AppState::mutate_doc`), what goes to a peer next and what is made of what it sent on the document's side (`document::passing`), and a wire version in which what is a kind's is bytes, with the kind said in handshakes (`transport::wire`, 53). The node's document is an Automerge document, and that is all it can be; this says what the rest of the node asks of it, which of that is Automerge's shape and which is continuum's, and where the seam goes behind which a swarm's document may be of another kind (a *kind* of document, a kind for short). Why: every write to an Automerge document is a change kept forever, and what Automerge does grows with their number, so a swarm takes no frequent writing; only a remake, which a majority must want, drops the history. What is written often, a pointer's place, a node's address as it moves, has one value of interest, the newest, and a history of changes is the most expensive way to hold it. And what a swarm holds needn't be merged JSON at all: a federation's store asks of its document a set of events, no more ([What the federation asks](#what-the-federation-asks)). ## What the node asks today Automerge itself is named only in [`document/`](../src/document/) and in five modules of [`remake/`](../src/remake/) (`snapshot`, `delta`, `catchup`, `kind`, `quarantine`): a test keeps it so (`document::tests::automerge_stays_behind_document`). The rest of the node goes through `document`'s functions. But those functions speak of changes, their hashes, heads, writers (actors), transactions and sync sessions, and the rest of the node speaks so with them: the names are behind the seam, the shape is not. What is asked, by what asks it: ### Values Asked by the API ([`api/store.rs`](../src/api/store.rs), `users`, `login`, `peers`), access control's reads ([`access/`](../src/access/): `registry`, `token`, `bans`, `envelope`, `sealed`, `policy`), the member registry and the topology ([`swarm/members.rs`](../src/swarm/members.rs), `topology`, `stability`, `working_links`, `peer/forget`), and remakes' records. * **Read** the JSON at a path, and a map's keys (`store::read_path`, `read_plain`, `keys_at`): of the document as it is, of a write in progress, or of the document as it was (below). No more is asked by most of the node. * **Write** as one unit, all of it or none (`AppState::mutate_doc*`: a closure over a transaction): reads inside see what the write has done so far, a check there may end it, and what it made is judged as every node would judge it before it leaves, and taken back where refused (`state::judged_here`, `roll_back`). The unit carries who made it, the node and the user. * **What changed**: the paths a batch of writes or of received data touched (`store::ChangeSet`, by `swarm::changes`), for streams, sessions and the dashboard. * **Conflicts**: values written at once at one place, shown on request with what was chosen (`?conflicts=true`), and all of them read where a rule needs them (a remake decided twice, a member acking twice: `store::read_candidates`). * **Types** beyond JSON's: counters, timestamps, bytes, text, and a value's type kept when written again (`document::typed`). ### Versions * **Heads** name a state of the document: sent to peers, compared with theirs, given to API clients with every write (`x-continuum-heads`), kept to tell what changed since. * **The document as it was at given heads** (`document::at::At`): for a client (`?at=`), and for judging, below. ### Passing it on Asked by [`swarm/sync.rs`](../src/swarm/sync.rs), `relay`, `blob`, the dissemination (`tree`, `plumtree`, `flood`) and the peer workers. * **A session per peer** (`document::sync::SyncSession`, Automerge's sync state and the repairs around it): the next message for the peer; a received one taken; what the peer is known to have, by what it said elsewhere and by what others bring it; what to send again when a message was lost. * **Changes as they are**, pushed along a writer's tree with the sender's heads. * **Many changes at once** as a blob: those between two sets of heads, the same bytes on every node holding them, fetched in chunks from several nodes; and one change by its hash. * **Which of these heads do I lack**, and does a peer's history hold mine (`changes::missing`, `holds_all`, `covers`). * **What came since given heads**: each change's hash, the node that made it and its time, for passing on by writer and for measuring delays (`changes::since`). ### Judging What is received is read, put in order and applied by [`document/screen.rs`](../src/document/screen.rs), which has each change judged before it applies it; `state` has the node's own writes judged alike. The rules ([`access/rules.rs`](../src/access/rules.rs), `holding`) judge a unit, who made it, for whom, and what it writes where, reading the document as its writer saw it (`rules::judge_seen`), and know of no change; what is Automerge's of judging one is beside them (`rules::judge`). The unit judged is one change. Judging it takes: * its hash and the changes it builds on, one building on none being an epoch's root; * **the document as it was at what it builds on**: who was a member, what a user was allowed, what the policy was, are read there, so that what was allowed when written stays taken; * the node that made it and the user it was made for, which the change itself carries; * **the paths it writes and what it does at each**, read from its operations before it is applied, the same on every node (`document::inspect`); * whether a signature vouches for it: a node signs a change's hash, and a signed change vouches for those of its writer it builds on (`document::signatures`). A change refused is kept out with all that builds on it, its hash remembered; one taken and found wrong later is dropped with what builds on it, by a walk of the history and a document made anew without them (`document::dropping`, for `AppState::drop_taken`). ### Whole documents * **Saved and loaded**: the document file, its appends since given heads, a backup (`document::file`, `save`, `save_since`, `load`). * **Replaced**: a remake's switch and a rejoin put another document in the live one's place, the one left kept beside it for a while (`state/host.rs`, `state/read.rs`); every peer's session starts over. * **Its root**: the one change that builds on none, named in handshakes so that nodes of different documents don't sync (`swarm::roots`). * **Its epoch**, which a remake changes: in every message, in the document file, in the API. A later epoch's ID is the hash of its first change. * **Its history**, for people: who wrote what and when (`GET /admin/changes`). ### What remakes ask A remake moves a swarm to a new document ([`remakes.md`](remakes.md)). It asks the most: a document built byte for byte alike on every node, the document at given heads, the changes after them in their order with their times, writers and what they touched, a commit taken back, a change fetched by its hash. And it holds what isn't a remake's: the writes kept aside when a change is dropped (`remake::quarantine_taken`). Of its reasons, these come of the history: compacting, leaving writers behind, keeping what a rejoining node fetches small, and ending writes made at a history from before. These are wanted of any document: replacing what it holds, restoring a backup, and a swarm turning strict, at one point for every node. ### What the federation asks The federation's fold judges events by their own bytes, their hashes and the signatures in them, and reads nothing of Automerge's: no merge, no conflict, no writer, no change's hash, no heads but the store's own ([`store.md`](store.md): "The store asks nothing of what carries its events"). Of the document it asks: * bytes added under their hash, never replaced nor removed; * what is held, listed, and one of them fetched; * to be told when something came; * the set passed on among the store's members; * the store's root, and a checkpoint where the document compacts. What surrounds it today makes an Automerge document behave so, or outlasts a remake: the rules for what a change may write (`access::holding`), the changes that found a swarm built byte for byte (`document::events`), events read out of changes that were refused, the store read anew after a switch or a rejoin, and the fixed bytes of Automerge's encoding (`federation::vectors`). A document that is such a set by itself needs none of it, and no remake. ## Whose shape it is | Asked | Automerge's shape | Any document's | |---|---|---| | Read at a path, a map's keys | | yes | | Write as one unit, checked, taken back | the transaction, the change | the unit | | Paths that changed | patches | yes | | Conflicts shown | operation IDs | that there are several values | | Types beyond JSON's | its scalars, text | | | Heads | change hashes | a name for a state | | The document as it was | any heads | | | A session per peer | all of it | that peers catch each other up | | Changes pushed, fetched, in blobs | all of it | | | Judging | the change, its dependencies, its operations | who wrote what where, read at the state the writer saw | | Signatures | over a change's hash, vouching per writer | that what is taken is vouched for | | Dropping what was taken | the walk, the document made anew | | | Root, epoch | hashes of changes | that documents are told apart | | History for people | all of it | | | Remakes | most of it | replace, restore, turn strict | ## What users see of it The API speaks of the engine in these, which a document of another kind answers otherwise or not at all: * `x-continuum-heads` on every write and on reads asked for it, and `?at=` to read at them; * `?conflicts=true`, whose candidates carry Automerge's operation IDs; * `x-continuum-type` and what a value is cast to; * `GET /admin/changes`, `GET /admin/refused`: changes by hash; * `GET /admin/document`: an Automerge save; * `heads`, `writers` and sizes in `GET /peers` and `GET /admin/stats`; * remakes and epochs. ## On the wire SYNC carries an Automerge sync message, or changes as they are with heads, or the heads a blob of changes lies between; with it, signatures by change hash. STATUS carries heads, or a digest of them. BLOB names a change by its hash or a delta by its heads. REFUSALS names changes. HELLO names the root. PRUNE and GRAFT name a writer. Every message but a few carries the epoch ([`transport/mod.rs`](../src/transport/mod.rs)). ## The seam A document isn't storage alone. How it is passed on, what a unit of it is and how one is judged, how it is kept on disk and whether it ever needs remaking are all of a piece with how it holds its values: the seam is around all of that, and a *kind* of document is what is behind it. **Above it**, the same for every kind: the transports and the wire's envelope, node identities, peers, the member registry, topology and dissemination (who is sent what, not what it is), access control's rules as rules, the API, the configuration. **Behind it**, a kind's own: * **Values**: reading at a path and a map's keys, writing as one unit with reads of the write in progress, a check ending it, and the paths that changed. Every kind has these, and they are all most of the node uses. * **A name for a state**: opaque above the seam, compared for sameness, summed up for STATUS, and asked whether one holds another. Automerge's is its heads. * **Passing on**: what to send a peer next and what to make of what it sent, as bytes the wire carries without reading; what a peer lacks; whatever a kind sends in bulk. The repairs of Automerge's sync are its kind's. * **Units and their judging**: a kind turns what it receives into units, each with the node that made it, the user, what it writes where, and a view of the state its writer saw; the rules judge that, and the kind keeps out or drops what they refuse. What a unit is, a change or one writer's update of some places, is the kind's. * **Vouching**: that a unit is its node's. Automerge's kind signs changes' hashes. * **Disk**: saving, appending, loading. * **What it can do beyond that**, asked for where wanted and answered "not this kind" otherwise: a history to list, a state read as it was, types beyond JSON's, remakes. A swarm is of one kind from its first document on, as it is of one policy: the kind is named where nodes meet, and nodes of different kinds refuse each other saying so. ### Kinds * **Automerge**, as now: every change kept, values merged within (lists, text, counters), a history to list and read at, and remakes to shed it. * **Merged state, without a history** ([below](#merged-state-without-a-history)): at each place its value, or the several written at once, each write named by its writer and that writer's count, and replacing what its writer had seen. Of writes at once to one place none is chosen, by a clock, a writer's ID or anything else: all are kept, and a reader gets them all, till a write that saw them all. A writer is the node, not one run of it. Lists and text are whole values. * **Ordered by clocks** ([below](#ordered-by-clocks)): at each place its newest write, by a stamp of its node's clock; no conflicts, nothing kept per writer. * **A set of events** ([below](#a-set-of-events)): bytes by their hash, added, and all but what a checkpoint keeps dropped when the fold moves to one ([`store.md`](store.md)), which is what a federation's store asks. What each is for: | | Automerge | Merged | By clocks | Events | |---|---|---|---|---| | Nodes | honest, or judged by a policy | honest, or judged by a policy | honest, clocks about right | parties that don't trust each other | | Writes at once to one place | merged within, or one chosen | all kept, the reader decides | the later wins, the other gone | the fold's rule | | Written often | no: every write is history | yes | yes | no: every write is an event | | Its size | grows, shed by a remake | what it holds, a count per member | what it holds | grows, shed by a checkpoint | | Deleted for good | yes | yes, among members | within the time markers are kept | the fold's | | A history to read | yes | no | no | back to its checkpoint | | Needs | a majority, to remake | nothing | clocks | the fold's votes, to move | Merged state and the one ordered by clocks name a write by its writer's word, a count or a stamp, which a node that lies can use twice ([What others do](#what-others-do)): they are for swarms whose nodes are honest. The set of events names by hash, and is for those whose aren't. ## Passing on How a document of any kind is passed on, and what the wire carries of it. Built: the node asking Automerge's side for it (`document::passing`), and the wire version below. Not built: the names above the seam as a kind's own in the node's memory, where a stamp, a message's body and an offer in bulk are still Automerge's types. ### Whose it is **The swarm's**, for every kind: whom a node syncs with, and when (handshakes, who is ready, who is level with whom, backing off); along whose tree a write is pushed and to whom it is only announced (`swarm::tree`, `plumtree`, `flood`); that neighbours tell each other where they stand and pull when behind (`swarm::relay`); moving many bytes in chunks, from several nodes at once where they hold the same (`swarm::blob`); telling a peer what was refused of it (`swarm::refusals`); the epoch a message is of. **A kind's**: what a message says, what a peer is known to have and what goes to it next, what is done when one is lost, what the bytes moved in bulk are, and what a unit is. The swarm hands these on without reading them. ### What a kind gives Above the seam, three names, each opaque: * **A stamp**: where a document stands, in a few bytes a keepalive carries (Automerge's: its heads, or a digest of many). Two are the same or not; and a document says of a peer's whether it lacks something the peer has, has all of it, or can't tell. * **A unit's name**: 32 bytes (Automerge's: a change's hash). What is announced, asked for, refused and counted is named so. * **A writer**: the node a unit is of, by its node ID, as now: a tree is a writer's. And a session per peer, the kind's own, with the document: | Asked of a kind | When | Automerge's | |---|---|---| | The next message for this peer, if there is any: its bytes, what vouches for what it carries, and the units it carries, each with its writer | the document changed, the peer answered, a reply is overdue | a sync message, or changes as they are, or an offer of a delta | | Take a message: what it made of it | one came | changes screened and applied, the rest fed to the sync state | | The peer got that message; it didn't | the transport says | what was carried counts as sent, or goes again | | The peer stands here | its stamp came in a keepalive | everything in those heads' history is the peer's | | The peer gets these units from another | the tree has another bring them | counted as sent, with a time | | Start over | a handshake, another document | a new sync state | | Units since this stamp, each with its writer and its time | after anything was taken, and for the node's own writes | the changes since those heads | | The bytes of something offered in bulk, and whether every node holding it gives the same bytes; and take such bytes | a peer asks for a blob it was offered; one was fetched | a delta between heads; one change by its hash | What a kind makes of a message it took, it says in terms the swarm knows: the units taken, with their writers and times; the paths that changed; the units refused, and whose they were; whether the peer is now known to hold what this node holds, and this node what the peer does; whether to answer; and what to fetch in bulk. Each unit taken was judged first: the kind turns what came into units, and asks the rules (`access::rules::judge_seen`). ### On the wire Wire version 53, beside 52, in which what is a kind's is bytes (`transport::wire`, `kinds`): | Message | In 52 | In 53 | |---|---|---| | SYNC | an Automerge sync message, or changes with heads, or a delta's heads; signatures of changes; `delivered` | the kind's bytes; what vouches, the kind's bytes | | STATUS | heads, or a digest and some of them | a stamp, the kind's bytes, no more than a keepalive may carry | | BLOB | a change by its hash, a delta by its heads, content by its hash | a unit by its name, something offered in bulk by its name with what the kind says of it, content by its hash | | REFUSALS | changes by their hashes | units by their names: the same bytes | | HELLO | the root's hash | the same, and the kind (`document::KIND`) | What stays as it is: the epoch in every message, `ready`, the writer a push is along whose tree, when a message was sent, PRUNE and GRAFT, and everything that is no document's. Automerge's bytes are what 52 carries in its messages' own fields, so a node speaks both to its peers as each can (versions side by side): a swarm of Automerge documents upgrades node by node. A node that speaks 52 alone says no kind, and is of the kind there was then; nodes of different kinds refuse each other in the handshake, saying so, and sync nothing. What another kind says goes in the same two messages (`SyncBody::Kind`, `HeadsSummary::Kind`): a SYNC's body is one of Automerge's, or another kind's bytes; a STATUS's stamp is Automerge's heads, or another kind's bytes after a byte no Automerge stamp begins with. Version 52 says neither. What a SYNC and a STATUS say beside, that the sender is ready, when it sent, whose tree a push is along, whom it reaches, is for every kind; the epoch, a body taken as delivered, heads announced and what vouches for changes are Automerge's, and a node of another kind says one epoch and leaves the rest. ### As merged state without a history would give it To see that what is asked isn't Automerge's in other words: * **A stamp**: a fingerprint of all it holds, the same on two nodes where they hold the same. * **A unit**: a write, its name the hash of its writer and count. * **A session**: which ranges of places the two have found the same, and which they are still narrowing down. The next message is fingerprints of ranges, or the writes of a small one, as the document holds them now: a place written ten times since is sent once. A message lost is nothing to repair: the range is compared again. * **In bulk**: all it holds, for a node that has nothing. * **Units since a stamp**: what was taken since, kept in the order it came for as long as a neighbour may ask. ## Merged state, without a history Not built: the second kind, `merged`. A design to be gone over before it is. It is a kind for any swarm, not for one use: what it gives and what it doesn't is said here for everything a swarm can be asked to do, a policy and a strict one among it. A kind differs from another by what it guarantees, not by what is built on it. ### What it holds * **Writes.** A write is a value at a place, by a writer, with that writer's count: the node's ID and a number it raises by one with every write, so that the pair names the write for good. A value is any JSON but a map: a map is the places under it. A write of nothing is a deletion (below). * **At each place**, the writes that count there: one, where one writer writes or each saw the last; several, where writers wrote at once. * **Per member, the count seen up to**: every write of that writer up to it has been taken, or replaced by one that has. An entry per member, however much was written; a member forgotten (`swarm::peer::forget`) is forgotten here too, once markers of its time are ([Deleting](#deleting), below). Nothing else: no write that was replaced, no order of what came when. Its size is by what it holds and how many members there are, not by how often they wrote, nor by who ever did. ### A write, and what it replaces A write says what its writer had seen where it writes: for each writer that had written at or under the place, the count seen there. It replaces those writes, every node alike, whatever of them a node holds: * a writer's later write at a place replaces its earlier ones there, also those a node never got: so a place written a hundred times a second is passed on as its newest write alone, and a slow link skips the rest; * a write by another that saw it replaces it; one that didn't stays beside it, and both count (a conflict, below); * a write at a place replaces what its writer saw under it: writing a value where a map was, or deleting a map, takes what was in it, and keeps what was written in it at once by someone who hadn't seen; * and a value its writer saw above it: writing under a place that held a value makes a map of it. What a write saw at its place it has from the writes it replaces, with what they saw: so what was seen at a place is not forgotten as its writes are replaced, and stays as small as the number of those who ever wrote there. ### Deleting A deletion is a write of nothing: it replaces what its writer saw, and stays, as a marker that the place was emptied. A node keeps a marker for a day since it took it, by its own clock (as long as it keeps a peer that is no member), and then drops it: nobody is asked whether they have seen it, and nothing is written to find out. Waiting for every member to have seen it would keep markers for as long as one member is silent ([What others do](#what-others-do)). A marker is what tells a node that is behind a little, as a slow link makes it all the time, what went, with the write that says so. It isn't what keeps a deletion true: the counts are. ### Seen, and gone A node that has seen a write and holds it no more never takes it again, whoever offers it, marker or none: its count for the writer covers the write. And of two nodes comparing what they hold ([Passing on, and catching up](#passing-on-and-catching-up), below): * a write one holds that the other has seen and holds no more is gone, replaced or deleted: the one drops it; * a write one holds that the other hasn't seen is a write like any other, however old: passed on, replacing what it saw, and beside whatever was written at its place meanwhile (a conflict, as below). So nothing deleted comes back and nothing written is lost, however long a node was away, with markers or without, for as long as the writer is a member. The counts of a forgotten member go, a day after it was forgotten: what it wrote and nobody replaced stays where it is; what of it was deleted, and a node away since before brings again, is back. Forgetting a member is saying it wrote its last. ### Reading A place with one write reads as its value; a map, as its places that hold something. With several writes at once at a place, the document chooses none: a reader gets them all, each with its writer and count, and decides. The next write by one who saw them all replaces them all: through the API, a write at the place replaces all its node holds there. So a read of such a place answers its values, not a value, and says so (`300`); a read of a tree with such a place in it leaves the place out and says that it did, and `?conflicts=true` lists them. What the node itself reads, a rule's who may write where among it, takes such a place for holding nothing: the same on every node holding the same writes, and giving no right that only one of the writes gives. A value where a map also holds something (one wrote a value there as another wrote under it, neither seeing the other) is such a conflict too. Lists and text are values: written whole, and two edits at once conflict as two values. Counters are not this kind's: a count is a value, written whole. ### Passing on, and catching up * **A unit is a write**: taken or refused by itself. * **Signed where the swarm asks for it**, as a change is today ([`api.md`](api.md), [Signed changes](api.md#signed-changes)): by its writer's node, where it writes a place the swarm lists as signed, or protocol data, so that whoever passes it on can't change it and whoever takes it knows whose it is. Elsewhere a write goes unsigned, taken on the word of the neighbour bringing it: in a swarm of one operator's nodes a signature on every write buys nothing, and a pointer written a hundred times a second pays 64 bytes and a check for each. Unlike a change, an unsigned write is vouched for by no later one: there is no history a signature could cover. * **As written**, a write goes to the writer's neighbours along its tree and on, as a change does now. * **Where it stands**, a node says by a fingerprint of all it holds: the sum of a hash of each write's name. Two that say the same hold the same. * **Behind**, or ahead, or both: the two say their counts, and compare what they hold by ranges of places ([What others do](#what-others-do)). One sends the fingerprint of a range; where the other's is the same, the range is done; where not, it answers with the fingerprints of the range's parts, or with the writes themselves where they are few. A number of rounds by the logarithm of what they hold, bytes by how much they differ; nothing kept per neighbour, nothing to know of each other before. Once through, each has what the other held, and takes the other's counts for seen. No log is kept to answer from, and nothing is sent twice for having been written twice. * **A node with nothing** is behind on everything, and gets it in bulk. What can't be had so: a writer's counts must not have holes a peer can't account for. A node counts on from what its document file holds, which it writes ahead of the writes it lets out; one that lost its file takes its own count from its peers before it writes again (writes are held till then, as a kept writer's are now). ### Partitions Each side reads and writes on, as long as it lasts: nothing waits for a majority, and no side is the one that counts. When the sides meet again, each sends what the other lacks, and at each place: * written on one side only: that write, as if there had been no partition; * written on both: both writes count, a conflict, and a reader gets both, until someone who saw both writes again; * deleted on one side, written on the other by one who hadn't seen the deletion: the write stays. A deletion takes what its writer saw, no more; * deleted on one side, untouched on the other: deleted; * a value on one side, a map written under it on the other: a conflict too. A partition longer than markers are kept ends no differently ([Seen, and gone](#seen-and-gone)): the sides come out the same. ### Judging A swarm of this kind says what its members are trusted with in the same three steps as any ([`trust.md`](trust.md), [What members are trusted with](trust.md#what-members-are-trusted-with)), by the same rules ([`access.md`](access.md), [Changes judged on every node](access.md#changes-judged-on-every-node)): who wrote, for which user, what, where. What differs is one thing. The rules read who may write where *as the document stood when a change was written*, and this document holds no past. What decides who may write where is itself written in the document: a user's entry, a member's, a ban, the policy, the entries of `_continuum/access`. Call a write at one of those a write of rights. What stands in for the past: * **A write of rights says all its writer had seen**: of every writer, the count seen up to. (Any other write says that only of its place.) And it is kept, a deletion too, for as long as nothing replaces it: no marker of rights is dropped after a day. * **A write is judged by the rights as they are**, whenever it comes. Where they refuse it, it still stands if what refuses it had seen it: the entry that no longer gives the right, the ban, the member's entry gone, each had seen that writer up to a count, and the writer's writes up to that count were written before it. * **So taking a right away holds for every write the one taking it hadn't seen**, and what it had seen stays. Every node holding the same writes judges alike, in whatever order they came; one that took a write before what refuses it came drops it then (as a node drops what it took unjudged now). No rule does better: that nobody writes once its right is gone, and that a write made without knowing stays, can't both hold where nodes write without asking each other ([What others do](#what-others-do)). A swarm chooses: without a policy, the node a write is made through checks it and the others take it; with one, the cut above. Against a document read as it was, that is the same wherever one write came after the other. It differs for writes made at once: * a write made without having seen its right taken away, on the other side of a partition say, is refused. Read as it was, it stands: and so does everything its writer makes later at a history from before, as far as it likes, which a document with a history ends only by a remake ([`access.md`](access.md), [Values signed by their users](access.md#values-signed-by-their-users)). Here nothing is to end: a writer can't write before a count others have seen; * a write made before its right was given is taken once the right is there. Its writer could write it again then anyway. A write refused so is out of the document, not lost: the node that made it keeps it aside (`GET /admin/quarantine`, as a node does today with its own writes among what it drops), for a user who may write there to put again or discard. Nobody can show that a write was made without having seen its right taken away, the writer's word apart; so the document doesn't take it, and a person may. The three steps, then: * **Without a policy**, nothing is judged: any member writes anywhere, a write is taken on the word of the neighbour bringing it, signed or not. * **With a policy**, every node judges every write as above: a member writes its own entry; the rest as the registry lets the user the write names; a banned node nothing; one that is no member only its own entry. With signatures required, every write is signed by its node, and two different writes of one name prove their writer made both. * **Strict**, the values at the places that decide who may do what, and at the paths the swarm lists, are envelopes their users signed, as now: each names its path, its user, the one it replaces and its number, so it needs no history to be checked, and of several at a place the highest number counts, before anything that isn't one. That is the one conflict the document decides, by what the users signed. The signer's key and right are judged as above. What every member is trusted with whatever the step stays: reading everything, and what it says of itself. What needed a majority of members only because of remakes (votes, the electorate, a coordinator) isn't there: nothing in this kind waits for a majority. A swarm turns strict at its start. Turning a running one strict, which a remake does for a document with a history, would be one write of rights here, signed by a key every node was given (`--strict-keys`), naming what is kept at the signed places: it had seen what it keeps, and what it hadn't seen is judged strictly. ### What it doesn't do No history to list, no reading as it was, no change taken back but by writing again; lists and text not merged within; no remake, as nothing gathers that one would shed. Replacing all it holds and restoring a backup are writes. Asked for what it doesn't do, it says so. ### Open * **A tree with a conflict in it**, as under Reading: left out and said, or the whole read refused. * **Rights taken away**, as under Judging: that it holds for whatever the one taking it hadn't seen, a write made at once on the other side of a partition among it. * **Turning a running swarm strict** by one write, as under Judging, or at a start only. * **What only grows** under a policy: the rights taken away that nothing replaced, each with all its writer had seen. By how many users and members there ever were, not by writes or time; and nothing sheds them yet. ## Ordered by clocks Not built: a kind, `clocked`, for swarms whose nodes are honest and whose clocks are about right. Beside `merged`, not instead: it decides what `merged` shows. * **A stamp on every write**: the later of its node's time and the highest stamp the node has seen, and one more (a hybrid logical clock), then the writer's node ID. The node's time is its clock set right by what it measures of its peers' ([`measurements.md`](measurements.md), [Clocks](measurements.md#clocks)). So a write made having seen another is after it, whatever the clocks say; of two made at once, the later by the clocks is, as far as they agree; and the node ID parts only two of one stamp. * **At each place the newest write**, and nothing else: a write at a place replaces what is older at it, under it, and a value above it. No conflicts, no choice left to a reader, and of two writes at once one is gone without a word. * **Nothing per writer, nothing per neighbour.** A write goes to the neighbours as it is made. Where a node stands is a fingerprint of all it holds, the sum of a hash of each place with its stamp; two nodes that differ compare ranges of places by their fingerprints, as `merged` does ([Passing on, and catching up](#passing-on-and-catching-up)), and each takes the newer of what differs. * **A deletion's marker is kept for a set time**, long (a month, say), then dropped. After a partition longer than that, a value one side holds and the other doesn't is kept: something deleted may be back, and nothing written is lost. * **A write stamped further ahead** than clocks may differ is refused, saying so; one from a node long away is as old as it is, and loses to what was written since. Its size is by what it holds and the markers of that time: nothing in it only grows. What is written often and matters only as it is now, where a pointer is, where a node is reached, is what it is for: other systems keep such apart from their documents ([What others do](#what-others-do)). A write made at once with another and lost to it is lost without a trace; where that matters, `merged`. And a stamp is its node's word: among nodes that don't trust each other, no cut by time holds. ## A set of events A kind, `events`, for a swarm holding a store ([`store.md`](store.md)): built. A node is of it where its operator says so ([As built](#as-built), below), and a swarm on Automerge moves to it node by node ([From an Automerge document to this](#from-an-automerge-document-to-this)). The store asks nothing of what carries its events, and what makes an Automerge document carry them is most of what a swarm holding a store has to get right ([On Automerge](store.md#on-automerge)): the changes that may write an event, the changes that found a swarm built byte for byte, events put again where a document lacks them, remakes and the epochs they part, a writer's two changes of one number, the Automerge version every node must share. This kind is the store's own home, and has none of it. ### What it holds * **The store's checkpoint, the events it keeps, and the events above its heads**, each as its bytes, named by its ID. A swarm's ID is its root's, as now. * **Each member's entry**, written by that member alone, the one with its highest count kept, signed with its node key: where it is reached, its group, what it speaks. An entry of a node the fold doesn't name a member is held by nobody. * **For a while, what its checkpoint forgot** (below). And nothing else: no users, no bans, no tokens of other nodes, no topology written down, as a swarm holding a store has none today; no epochs, no records of remakes, no acks. ### What is taken An event is taken into the document where its bytes hash to its name, are no more than the fold allows, name what they build on as the fold reads it, and all it builds on is held; and where the fold takes it at its history, which every node judges alike. One the fold refuses there is held by nobody. One whose history takes it and that the node itself refuses or doesn't read (a node's own: what it took before, its operator's drops, where it stopped) is held and passed on all the same, as the document holds it today: so that what one node doesn't read still reaches the nodes that do. What builds on such an event, the node can't judge: it holds no history to judge it at. Where nodes took different ones of two versions that changed the same, or an operator dropped an event the others hold, what is written after it on the other side is of that sort. The node holds it unjudged and passes it on, for the nodes that can judge it; and as whoever can make an event can make one that names such an event to build on, it holds no more of them than a bound (`store::set::UNJUDGED`), the oldest going first. A node that judges them holds none the fold refuses, so they fill up only nodes that refuse what they build on, and cost those the bound and no more. Who brought an event, and who put it, is nothing: no node signs for an event, and nothing is judged by a writer. An event that waits for what it builds on waits a bounded while, among a bounded number. ### Passing on * **A unit is an event**, its name its ID; or a member's entry. * **A stamp**: the checkpoint's ID and a digest of the heads. * **Behind**, a node says its checkpoint and its heads: the same heads, the same events, exactly. Heads the peer holds, it answers with what builds on them. Else the two find what differs by ranges of the events' names and their fingerprints, as `merged` does ([Passing on, and catching up](#passing-on-and-catching-up)), the fingerprint a hash of the names' sum and their number ([What others do](#what-others-do)): nothing kept per peer, and nothing a party can make slow by the names it mints. Where the heads still differ after that, someone made fingerprints collide, and the node asks by name, from the heads it lacks back to what both hold. Whatever a peer answers, a node takes only events whose history it holds whole, each after what it builds on: a peer that lies withholds, and adds nothing. * **Across a move to a checkpoint**: a node keeps what its checkpoint forgot for a time its operator sets, after moving, and gives it to one that is still before it, which then builds the checkpoint itself, as every node does. After that time, a node still before it takes the checkpoint where its operator pins it, as today after a remake ([Pinned checkpoints](store.md#pinned-checkpoints)). * **A node with nothing** starts from the root its operator names, or a checkpoint its operator pins. ### What bounds it Checkpoints, which the fold moves to (the federation: by a version its parties pass, which nodes voting by themselves propose and back). What a checkpoint forgot goes after the set time; what was refused is remembered by name only until the store moves; what waits is bounded. Without checkpoints passed it grows, as the store does anywhere: that is the fold's to see to, and the federation's limits on what a party writes are still to come ([`federation.md`](federation.md)). ### What goes with Automerge | Today, on Automerge | In this kind | |---|---| | A change judged by what it writes (`access::holding`), signed by its node | an event taken by its bytes | | The genesis change and a fork's founding changes, byte for byte (`document::events`) | the root, and the events it keeps, as they are | | Events put again (a change refused, a switch, a rejoin) | nothing takes an event away | | Remakes, acks, epochs, of two epochs the lower | none: checkpoints bound it | | A writer's two changes of one number parting documents | no writers | | Every writer's cost in memory, a fresh one each start | no writers | | One Automerge version for all, its bytes pinned (`federation::vectors`) | none | | A snapshot holding the store at a base | the checkpoint is the document's | What the store promises ([What is promised](store.md#what-is-promised)) it promises as now; "nothing underneath adds an event or takes one away" is then true by what the document is, not by what is put again. ### As built A node holds its store in a set of events where it is given `--store-document`, the set's file, beside `--store-root` or `--store-id`. It then says `events` for its kind in its handshakes, and syncs with nodes that say so and no others. * **The set** ([`federation/store/set.rs`](../src/federation/store/set.rs)): the store, and beside it what the store lets go of and the node still gives; the members' entries ([`entry.rs`](../src/federation/store/entry.rs)); its names, kept as it changes; its file, written where it changed, every five seconds and as the node stops. * **Passing on** ([`federation/store/passing.rs`](../src/federation/store/passing.rs), [`swarm/set.rs`](../src/swarm/set.rs)): where the node stands goes in its STATUSes, the checkpoint's and the heads' digest with the fingerprint of all its names; the rest in SYNCs' bodies: ranges ([`document/ranges.rs`](../src/document/ranges.rs)), heads, the names it wants, events and entries. What it took goes on to its other neighbours at once. Nothing is kept of a peer between messages. * **Starting**: from the file; without one, from the root, where given and no checkpoint is pinned; else with nothing, asking the members it reaches for the checkpoint named by the store's ID or by `--store-checkpoint`, and for the events that one keeps, each taken as what its name says. * **The node's own entry** is what `swarm::members` writes of a member, put as the node's next entry; its other writes of protocol data are refused. What reads members, the policy and the swarm's ID of a document reads them of the set (`Holding::entries`). * **The document the node holds beside** is an Automerge document with nothing in it: nothing syncs it, remakes it or writes to it, and the API's `/store` holds nothing. ### From an Automerge document to this A store is its root and its events, whatever carries them: a node of this kind reads the same state from the same events, and builds the same checkpoints. So a swarm moves without a new federation and without a vote, by **a node of both kinds for a while**: a release whose node can hold the store in both at once, a member of both, and takes into each what the other holds, as it puts events again today. Operators move their nodes one at a time; once none is of the old kind, the next release drops it for a swarm holding a store. Built: a node given `--store-both` beside `--store-document` holds its store in the Automerge document and in a set ([`swarm/set.rs`](../src/swarm/set.rs), `Holding::of_both`). It takes handshakes of either kind and says to each peer the kind that peer holds; passes the Automerge document on to the ones and its set to the others; puts into the document what its set took of peers on sets, and passes on to them what its set took of the document; and writes its entry in both. Its set, without a file yet, is made of the store as the node reads it of its document, so it begins where the document does, a checkpoint a remake left among it. Nodes on the document alone and nodes on a set alone take none of each other's handshakes: what goes between them goes through the nodes of both ([`operations.md`](operations.md), [Moving to sets of events](operations.md#moving-to-sets-of-events)). Not built: that no swarm holding a store is started on Automerge any more. One that runs there runs on, until it has moved. ## What others do What the three kinds take from what is published, and where they part from it. ### Naming by hash, for nodes that lie Kleppmann, *Making CRDTs Byzantine Fault Tolerant* (PaPoC 2022, [doi:10.1145/3517209.3524042](https://doi.org/10.1145/3517209.3524042)), and Kleppmann and Howard, *Byzantine Eventual Consistency and the Fundamental Limits of Peer-to-Peer Databases* ([arXiv:2012.00472](https://arxiv.org/abs/2012.00472)): * An update named by the hash of its bytes, naming those it builds on, applied after them, and judged by them alone, is taken or refused by every correct node alike, however many nodes lie. That is an event of the store, judged at its history. * A name made of a node and a count is the node's word: one that lies gives two updates one name, and two correct nodes whose counts are the same hold different things, signed or not. So `merged` and `clocked` are for honest nodes. * Catching up: heads, then asking back by name; and a Bloom filter of what came since the last time with that peer, which brought their measure to 1.03 round trips. `events` keeps the heads and the asking back, and finds what differs by ranges instead (below): a filter is state per peer, and one who mints names can fill it. * What can be kept without nodes agreeing, however many lie, is what two writes that each keep it also keep together. That a name is given once, that a balance stays above nothing, and that nobody writes once its right is gone, are not: they need agreement, which is the federation's votes, or a cut one side loses by. * History may go where every node of a known set has delivered what follows it. They say nothing of a node that never says so; the store's checkpoint is voted instead. Brocco, *Decoupling Trust in Byzantine CRDTs* ([arXiv:2606.31759](https://arxiv.org/abs/2606.31759)), on a key that was honest and then leaked: shutting out the identity takes what it wrote before with it, and what others built on that; a cut by time needs a clock all trust; so what was taken before stays, named by its content, and what comes after is judged anew. How nodes come to the same judgement it leaves open. The store has both halves: what a node took it holds, an operator's drops and a party's keys changed by votes apart ([`store.md`](store.md), [A node's own](store.md#a-nodes-own)). ### Until every node has seen it Olivier, Bauwens, Didonet Del Fabro and Gérard, *The Cost of Purity: Scalability Challenges in the Pure Operation-based CRDT Framework* (ICSOFT 2026, [cea-05739385](https://cea.hal.science/cea-05739385)), of a document that is a log of operations, pruned of what later ones make redundant and compacted once every node has delivered: * One silent node holds that off for good: "we do not recommend relying on causal stability" where nodes crash. So markers are kept for a time, members are forgotten, and a store moves by its fold's votes. * What is pruned from the document still waits to be sent to whoever hasn't acknowledged it, unless the one sent in its place says what it stands for: `merged`'s write saying what it saw. * A stamp of one entry per node on every operation, and a matrix of them to find what all have seen. `clocked` has neither; a `merged` write names those who wrote at its place. * A graph of operations by hash can't be pruned: what is gone leaves holes in what the rest builds on. A kind keeps a history or it doesn't. ### Catching up, knowing nothing of each other Auvolat and Taïani, *Merkle Search Trees: Efficient State-Based CRDTs in Open Networks* (SRDS 2019, [hal-02303490](https://inria.hal.science/hal-02303490)): a map as a search tree whose shape its keys' hashes fix, each block named by its hash, so that two nodes compare roots and fetch the blocks that differ, at a cost by the difference and not by how many nodes there are or ever were. Against vector clocks it wins where writes are rare and loses where they are many, and suggests sending each write as it is made with the tree as the repair: push along trees, and compare when behind. It says nothing of nodes that lie, nor of dropping the markers of what was deleted. Meyer, *Range-Based Set Reconciliation* ([arXiv:2212.13567](https://arxiv.org/abs/2212.13567), a draft): the same end without a tree the two must share. Items in one order; a range's fingerprint sent; the same, done; else the range's parts, or its items where few. Rounds by the logarithm of what is held in the worst case, where a tree's depth is its keys' to decide. Fingerprints that add up come from any sorted structure; a sum of hashes can be made to collide by one who chooses the items, which honest nodes don't. `merged` and `clocked` catch up so. Hoyte, *Range-Based Set Reconciliation* ([logperiodic.com/rbsr.html](https://logperiodic.com/rbsr.html)), and Negentropy, which Nostr relays run over tens of millions of items: items by a timestamp and a 32-byte name; a range split in 16, so three round trips for a million; the fingerprint the names added up modulo 2^256, their number appended, hashed, 16 bytes of it; no state on either side between messages, as the ranges are each node's to choose. On fingerprints: XOR collides in seconds, a plain sum in about a day of eight cores, a multiset hash on an elliptic curve not at all but slowly; the number of items in the fingerprint and boundaries chosen at random are what it takes instead. On the rest: a Bloom filter leaves what it wrongly takes for held, and can be filled on purpose (shown against Automerge's sync); a tree whose shape the items fix can be made to send everything. `events` takes that fingerprint, with the heads to tell when it was fooled. ### Newest wins, in each writer's own space Willow ([willowprotocol.org](https://willowprotocol.org): its data model, Meadowcap, its reconciliation, and *Timestamps, really?*): an entry is a namespace, a subspace (a writer's key), a path, a timestamp its writer claims, and the length and digest of a payload. Of two entries of one subspace, the newer at a path removes the older at it and under it (of one timestamp, the greater digest): the newer one stays, standing for all it removed, and nothing else marks a deletion. Who may write where is a capability (Meadowcap): a namespace is anyone's to write in, each in its own subspace, or one key's, which hands out parts of it, a subspace, a path, a span of time. Nothing takes one back: it runs out, or the owner writes over what was written with a stamp later than the capability reaches. Nodes catch up by ranges in three dimensions, with a fingerprint that must hold against items made to collide. Of its timestamps it says what holds for `clocked`: a system that relies on them being right "is a centralised system in disguise"; this one doesn't, as any stamps give one state on every node, and right ones give the state people expect. Exact order would cost space by how much is written at once, and still leave a winner to pick; and any rule that picks one can be gamed, a lowest hash by trying until one is low. What it warns of is a stamp far ahead by mistake: it removes what is right, and nothing right can be written under it until that time comes; so a node refuses entries stamped more than some ten minutes ahead. `clocked` refuses alike, and its stamps follow the highest seen, so that whoever has seen a write can always write after it. It is `clocked` with one thing more: a stamp competes only with its own writer's, so a clock that lies harms its own places and no others. `clocked` has that wherever a place is one node's to write, as in a mesh each node's is. For a federation it is no home. Its authority is one key or none, where a federation's is two thirds of its parties; it has no history, so nothing is judged by the version current when it was written, and a key that was taken away writes on with a later stamp; votes, versions and forks would have to stand beside it, which is the fold. ### Libraries Read as they stand on crates.io, none built on: | Crate | What it is | For us | |---|---|---| | `crdts` 7.3 | maps of values that merge, a clock per map and per entry, removal without markers by "the other has seen it and holds it no more", writes at once kept | `merged`'s shape, and what to test it against; its clocks never forget a writer, an operation can't be skipped, and nothing is between sending all and sending each | | `crdt-kit` 0.5 | a last-writer-wins map by a hybrid clock | `clocked`'s shape; its stamps name a node by 16 bits, so two nodes can stamp alike, and its markers stay | | `merkle-search-tree` 0.8 | the tree of that paper, over keys and hashes of values | a reference; it doesn't remove, and hashes weakly unless told | | `loro` 1.19 | a document of maps, lists, text, trees, with its history | where Automerge stands, not another kind: a history, a clock entry per peer, last writer wins in maps, nothing named by hash; its trimmed snapshots shut out whoever is behind. It keeps presence apart from the document, as `clocked` is apart here | | `diamond-types` 1.0 | text | not ours | | `negentropy` 0.5 | Negentropy's protocol, 1 800 lines, no dependency, over a sorted list | a reference for catching up by ranges; its items are a timestamp and a name, ours a place or a name | | `willow25` 0.7 | Willow: entries, capabilities, stores, reconciliation | a reference for `clocked`; no home for a federation | ## Stages 1. **The seam written down**: this. 2. **Automerge behind it**, the node behaving as it does: what is the engine's moves to the engine's side (the writers a node wrote under, the walk that drops a change, the reading of received changes apart from the rules that judge them); what crosses the seam gets names of its own for a state, a unit and a writer; the closures that take a document or a transaction take a view to read or to write; passing on is asked of a kind ([Passing on](#passing-on)), first in the node, then on the wire; remakes become something a kind can do. Each step leaves the tests as they are. 3. **The kind named** where nodes meet, with one kind to name: built, with the wire version (`HelloRequest::kind`). 4. **The set of events** ([above](#a-set-of-events)), and the federation on it: catching up by ranges; the document, its file, its passing on; the node of either kind; a node of both, to move by. 5. **Merged state, and the one ordered by clocks**, with what is written often as their test. 6. **A WireGuard mesh** built on one of those two, as any application is on a swarm: each node's key and where it is reached, written by itself.