# 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`), and the rules judging a unit at the state its writer saw (`access::rules::judge_seen`). 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. 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**: at each place its value, or the several written at once, each write named by its writer and that writer's count; a document knows per writer the count it has seen up to, and a write replaces what its writer had seen. Its size is by what it holds, not by how often it was written; a slow link skips to the newest; nothing needs remaking. Of writes at once to one place none is chosen by a clock or by its writer's ID: all are kept and shown, and a reader wanting one gets the one whose write has the lowest hash, till a write that saw them all. A writer is the node, not one run of it. Lists and text are whole values at first. * **A set of events**: bytes by their hash, added and never removed, which is what a federation's store asks. ## 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; SYNC's body becomes the kind's bytes; 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. 4. **Merged state without a history**, as a second kind, with what is written often as its test. 5. **A WireGuard mesh** as its first use: each node's key and where it is reached, written by itself. 6. **The federation on a set of events**, where that proves the better home.