# Backup and sync The person's data lives in the vault (`roadmap.md`, encrypted storage), and there is no server. So far a lost or broken phone took everything with it. This is how Pinnacle keeps a copy: **on the person's other devices**, kept in step over the p2p layer, and **on a backup disk**, encrypted, for a person with one device. Both restore onto a new device. Written 2026-11-29 (session s912) as the design for `full-os.md`'s Need-1 item "Backup and restore of the vault, and sync between the person's own devices over p2p". ## What a person sees **Settings → Devices** lists the person's own devices: this one, and every device linked to it, each with when it was last in step. - **Link a device.** On the device that has the data, *Link a device* shows a code of twelve letters and digits (`7KQM-2XHD-9WPA`) for ten minutes. On the other device, *Link to my device* asks for it. Both then show the other's name and fingerprint, and say they are linked. Nothing else is asked: the code is the proof that both are in one person's hands. - **In step by themselves.** Linked devices keep the vault's files the same whenever both are online: a note written on the laptop is on the phone a few seconds later, and the other way round. A device that was off catches up when it is on again. - **Restore a lost phone.** A new phone starts as any new device (its own vault, its own password), then *Link to my device* with a code from the laptop: everything the laptop has comes over. *Forget* on the laptop drops the lost phone, so it gets nothing more. The same page's **Backup** writes the vault's files to a backup disk (a USB stick, an SD card; in QEMU a third disk) whose file system is labelled `backup`, encrypted, and restores them from one. - **Back up now** copies what changed since the last backup. The backup is sealed with the vault's password at the time it is made; the disk holds no names and no contents in the clear. - **Restore** on a new device, after its first start: the password of the device the backup was made on, then every file comes back. A wrong one opens nothing. ## What is kept in step Every file in the vault (`data:/`), but what belongs to one device: | Not synced | Why | |---|---| | `data:/p2p/identity` | Each device keeps its own identity key, so a lost one can be dropped alone and its key never travels. | | `data:/bt/bonds` | A Bluetooth bond is the key of one radio with one other radio. | | `data:/bt/identity` | This device's Bluetooth identity key: devices paired with know it by it, and a second device with it would pass for this one. | | `data:/sync/` | The sync service's own state: what each device last agreed on. | | `data:/display/` | The display server's: the past notifications this device showed, and how many were seen here. | So the apps' folders (`data:/apps/notes`, …), the address book and the contacts' cards, conversations, texts, Wi-Fi networks and their passwords, the agent's key and its log all go to every linked device. The system disk (`system:/settings`, installed apps) does not: that is the device's, and the Store reinstalls apps. ## How it works ### Own devices A person's devices are not one identity: each keeps its own (`p2p`), and the person's devices are contacts of each other, marked **own** in `data:/sync/devices` (their keys, 32 bytes each). Only an own device may open a sync stream, and only to a device that has it as own too: the p2p service checks the list before it connects a stream tagged `sync` to the sync service. Being a contact or being offered services is not enough. ### Linking, through the DHT The two devices do not know each other yet, and the p2p layer opens sessions only between contacts. The code carries them there: 1. Device A draws a random code `c` (60 bits: twelve characters of Crockford's base 32). From it: a key `k = HKDF(c, "pinnacle link key")` and two places in the DHT, `offer = SHA-256("pinnacle link offer" ‖ c)` and `answer = SHA-256("pinnacle link answer" ‖ c)`. 2. A stores a **link record** at `offer`: its card, sealed under `k` (ChaCha20-Poly1305), and keeps storing it every 30 s while the code shows. 3. B, given `c`, looks up `offer`, opens the seal (a wrong code finds nothing, or a record that does not open), checks the card's signature, and stores its own card sealed under `k` at `answer`. 4. A looks up `answer` every three seconds while the code shows; when it finds B's card, both have the other's card, add it as a contact and as own, and A's code is spent. A link record is the DHT's second kind of record beside presence: a kind byte, the sealed card, valid ten minutes, at most 64 held by a node. The nodes that hold it cannot open it. Someone who learns the code in those ten minutes could link a device of theirs: the code is shown on the screen and nowhere else, as a Bluetooth pairing number is. ### Sync, between two devices The **sync service** (`system-sync`, a system program, so the kernel keeps installed apps from reaching it) walks the vault, and whenever an own device is reachable, every 30 s and after a change, runs a sync with it over a p2p stream. The logic is a pure state machine in the crate `sync`, like `net` and `p2p`: messages in, messages out, files read and written through a trait, so two devices are tested in memory. 1. **Manifests.** Each side lists its files: path, size, time of change and SHA-256. The service keeps what it hashed (`data:/sync/index`) and hashes again only a file whose size or time changed, or whose time is this very second (a second write in the same second would otherwise be missed). 2. **The base.** For every own device, each keeps the **base**: every path and its hash and size as the two last agreed (`data:/sync/base/`). 3. **Three ways.** The device with the lower key decides, for each path, from its own version `L`, the other's `R` and the base `B`: | Case | Done | |---|---| | `L = R` | nothing | | `L = B`, `R` changed (or new) | take `R` | | `R = B`, `L` changed (or new) | send `L` | | `L` gone, `R = B` | remove it there | | `R` gone, `L = B` | remove it here | | one gone, the other changed | the changed one stays, and goes to both | | both changed, each starting with `B` | **merged**: `B`, then what was added here, then what was added there | | both changed otherwise | **both kept**: the newer under its name, the other beside it as `name (from ).ext` | The merge is what keeps logs whole: a conversation, the texts, the agent's log only ever grow at their end, so two devices that both added to one keep all of it. Nothing is lost in any case: a conflict leaves both versions for the person. 4. **Transfer.** Files go in pieces that fit one message (3 KiB), each file written whole when its last piece is in (one transaction of the file system, so a file is the old one or the new one, never half). 5. **The new base.** When every step is done the deciding device sends the paths and hashes they now share; both keep it as the new base. A sync cut off halfway keeps the old base, and the next one finds again what is left: what was already copied is equal on both sides and costs nothing. A program that holds a file in memory has to hear that it changed. The p2p service (the address book, contacts, conversations) is told by the sync service after a sync changed `data:/p2p/` and reads them again; an app sees a change the next time it reads the file. ### Backup to a disk A backup disk is a disk whose plain file system is labelled `backup`. The backup of a device is `backup:/pinnacle//`: - `key`: a random backup key, sealed under the vault password stretched by PBKDF2 (as a vault's key slot); - `index`: every path, its hash and size, sealed under the backup key; - `blobs/`: each file's contents, sealed under the backup key, named by a keyed hash of its contents, so a file already there is not written again and the names say nothing. A backup writes the blobs that are missing, then the index, then removes blobs no index names. A restore reads the index and writes each file into the vault, a file there already with the same hash left alone. The same `sync` crate lists the files, hashes them and decides what to copy. ## Limits, and what is next - **Contacts know devices, not people.** Bo has Ada's phone as a contact. A new phone restored from the laptop has Bo's card and the conversation, but Bo's device does not yet accept a session from the new phone: Ada has to exchange cards again. Next: a device that links another tells its contacts, signed with its own key, that the new device is the same person's, and their address books add it to Ada's entry. - **A lost device is not wiped.** *Forget* stops it getting more; what it had stays on it, in its vault, behind its password. - **Deleted is deleted everywhere.** A file removed on one device is removed on all; only the backup disk keeps it, until the next backup. Kept versions (the last replaced or removed version of each file for 30 days) are next. - **Big files.** A file is held in memory while it comes; files above 16 MiB are not synced yet (none of the apps writes one). - **Pairs, not groups.** Three own devices sync as three pairs, each with its own base, so a change goes from one to the next a sync at a time. Linking a third to one of the others links it to that one only; link it to each. - **One backup disk**, the one labelled `backup`, and a backup only when the person asks for one. - No backup to a friend's device, encrypted for them to hold, yet: it needs a recovery code, since a new device has no key that the friend knows.