100 lines
6.7 KiB
Markdown
100 lines
6.7 KiB
Markdown
# F22a — The client survives a restart, and never sends what it has not saved
|
|
|
|
Analysis, 2026-11-16. Part of F22 (chat in Amicitia, `../amicitia/.agents/features/chat.md`). It
|
|
builds on F21 (`client.md`), whose state lived in memory only (its review, finding F6).
|
|
|
|
## 1. Why and what
|
|
An app must be able to close and reopen without losing a conversation, and must never get into a
|
|
state that MLS cannot recover from. Two things are needed:
|
|
1. **A snapshot.** `Client::snapshot()` gives the whole client state as bytes, and
|
|
`Client::restore(bytes, transport)` brings it back. The app keeps the bytes in its own encrypted
|
|
store (Amicitia: the SQLCipher database). The library never writes a file.
|
|
2. **Save before the network sees it.** Everything that reaches the relay after a state change goes
|
|
out only after the app has saved the snapshot. Otherwise a crash can leave the relay ahead of the
|
|
saved state.
|
|
|
|
### What goes wrong without step 2
|
|
| Crash between… | Then, after restoring the older snapshot |
|
|
|:--|:--|
|
|
| a text posted and the snapshot saved | the next text reuses the same MLS generation; the other side cannot decrypt it, and every later one fails too |
|
|
| the blobs acknowledged and the snapshot saved | the messages are gone from the relay and from the app |
|
|
| the inviter's `hello` posted and the snapshot saved | the inviter joins again from the same Welcome and sends a second `hello` with a new mailbox; the other side refuses it (F21's *second hello* rule) and posts to a mailbox the inviter no longer knows |
|
|
|
|
## 2. Design
|
|
### The staged calls
|
|
Each call that changes state does its **local** work and returns what still has to reach the relay:
|
|
|
|
| Call | Network inside the call | Returned for the app to deliver after saving |
|
|
|:--|:--|:--|
|
|
| `compose(contact, text)` | none | one `Outgoing` |
|
|
| `accept(invite)` | claim the KeyPackage; create our mailbox | `ContactId`, and the Welcome and `hello` as `Outgoing`s |
|
|
| `receive()` | fetch; create our mailbox when a `hello` asks us to answer | a `Received`: the events, our answering `hello`s as `Outgoing`s, and the blobs to acknowledge |
|
|
| `create_invite()` | create the welcome mailbox and the invite | the `Invite` (the app shows the link only after saving) |
|
|
|
|
Then `deliver(&Outgoing)` posts one blob, and `finish(&Received)` delivers its `Outgoing`s, then
|
|
acknowledges its blobs, then drops each welcome mailbox whose contact has said hello. The app saves
|
|
the snapshot again after `finish`. The drop waits for `finish` on purpose. A snapshot saved between
|
|
`receive` and `finish` must still know the welcome mailbox, so that a restored client fetches its
|
|
blobs again and acknowledges them. Dropping it inside `receive` left the Welcome and the `hello` on
|
|
the relay for 30 days; test 3 found that while this was being built. `Outgoing` is serialisable, so an app can keep it in an outbox and deliver
|
|
it again after a crash.
|
|
|
|
The F21 calls stay, for apps and tests without persistence: `send` = `compose` + `deliver`,
|
|
`accept_invite` = `accept` + deliver, `poll` = `receive` + `finish`.
|
|
|
|
### Why a crash is now harmless
|
|
| Crash after saving, before… | What happens on the next start |
|
|
|:--|:--|
|
|
| delivering an `Outgoing` | the app delivers it from its outbox. If the crash came after the post, the blob arrives twice; MLS refuses the second copy as a replay, and it is dropped |
|
|
| acknowledging | the blobs are fetched again; MLS refuses them as already processed, and the client drops them and acknowledges them. The events were saved with the snapshot, so nothing is shown twice |
|
|
| saving at all | nothing reached the relay, apart from a mailbox or an invite nobody knows about: no harm. A claimed invite is used up (the link says *ask for a new one*) |
|
|
|
|
### The snapshot
|
|
- JSON, versioned (`"v": 1`). It contains:
|
|
- our name and the 16 identity bytes;
|
|
- the signature public key; the private key is in OpenMLS's storage;
|
|
- each contact: id, MLS group id, name, our mailbox and fetch token, their mailbox and post token;
|
|
- the welcome mailboxes;
|
|
- **all of OpenMLS's storage** (`MemoryStorage::values`, a public map of keys and values,
|
|
base64url).
|
|
- Restoring rebuilds the provider with that map. It loads each group with `MlsGroup::load` and the
|
|
signer with `SignatureKeyPair::read`. A snapshot that does not parse, has another version, or
|
|
names a group that is not in the storage is refused (`ClientError::State`). A half-restored client
|
|
is never returned.
|
|
- **It holds every secret of the identity**: MLS private keys, fetch tokens, post tokens. It must
|
|
live only in encrypted storage. The library never logs it, and its `Debug` prints no bytes.
|
|
- **Size:** a few KB per contact (two MLS epochs of secrets). Writing it whole after each change is
|
|
fine for 1:1 chats with dozens of people. A table per key would scale better; that is for later,
|
|
when measured.
|
|
|
|
## 3. Tests — written first
|
|
1. **Restart both sides mid-conversation:** pair, talk, snapshot both, restore both into new clients,
|
|
then talk both ways.
|
|
2. **Restart with an invite waiting:** create an invite, snapshot, restore; the invitee accepts; the
|
|
restored inviter joins.
|
|
3. **Crash between receive and finish** (on the inviter's side, when the `hello` is answered): save
|
|
the snapshot with the `Received`, drop the client, restore, deliver the saved `Outgoing`s, receive
|
|
again. The pairing completes, and no event is shown twice.
|
|
4. **Crash after deliver, before marking it delivered:** the blob is posted twice; the receiver shows
|
|
the text once.
|
|
5. **Nothing reaches the relay before the app saves.** `compose` and `receive` post nothing (the
|
|
relay's blob count is unchanged), and `receive` acknowledges nothing.
|
|
6. **A bad snapshot is refused:** truncated, wrong version, or a contact whose group is missing.
|
|
7. **The snapshot carries no name in clear on the relay:** not a relay test. The relay never sees a
|
|
snapshot, and the F21 tests still hold.
|
|
|
|
8. **The relay fails:** `finish` acknowledges nothing when delivering fails (an app without an
|
|
outbox depends on that). A `receive` whose relay fails part-way returns what it processed,
|
|
because the client's state already includes it.
|
|
9. **A used welcome mailbox is no longer fetched**, even after a restart.
|
|
|
|
Planted bugs, for the review: `receive` acknowledges inside the call; `compose` posts; `restore`
|
|
skips the storage map; `finish` acknowledges before it delivers; a restored client starts
|
|
`next_contact` at 0, so two contacts share an id.
|
|
|
|
## 4. Not in this part
|
|
- How Amicitia stores the snapshot, events and outbox in one transaction (F22b).
|
|
- Several devices per identity (Phase 2 has one; HANDOVER).
|
|
- Encrypting the snapshot inside the library: the app's store is already encrypted, and a second
|
|
key would have to live somewhere too.
|