[client] F22a: snapshot and restore; staged calls that send nothing before the app has saved #15

Closed
mnovak wants to merge 0 commits from feature/client-state into main
Owner

F22a: the client survives a restart, and sends nothing before the app has saved. Part of F22 (chat in Amicitia). Analysis: .agents/features/client-state.md.

  • Client::snapshot() / Client::restore(bytes, transport): the whole state as versioned JSON, OpenMLS's storage included. All or nothing: a bad snapshot is refused (ClientError::State).
  • Staged calls: compose, accept, receive do the local work and return what still has to reach the relay (Outgoing, serialisable for an outbox). deliver and finish send it after the app has saved. send, accept_invite and poll stay as the one-step forms.
  • Event::Joined says which invite it came through (via), so the app knows which person joined.
  • receive returns what it processed when the relay fails part-way (interrupted), instead of losing it.
  • forget_invite(id): stop fetching an unclaimed invite's mailbox.
  • No change to PROTOCOL.md: the relay sees exactly what it saw before.
**F22a: the client survives a restart, and sends nothing before the app has saved.** Part of F22 (chat in Amicitia). Analysis: `.agents/features/client-state.md`. - `Client::snapshot()` / `Client::restore(bytes, transport)`: the whole state as versioned JSON, OpenMLS's storage included. All or nothing: a bad snapshot is refused (`ClientError::State`). - **Staged calls**: `compose`, `accept`, `receive` do the local work and return what still has to reach the relay (`Outgoing`, serialisable for an outbox). `deliver` and `finish` send it after the app has saved. `send`, `accept_invite` and `poll` stay as the one-step forms. - `Event::Joined` says which invite it came through (`via`), so the app knows which person joined. - `receive` returns what it processed when the relay fails part-way (`interrupted`), instead of losing it. - `forget_invite(id)`: stop fetching an unclaimed invite's mailbox. - No change to `PROTOCOL.md`: the relay sees exactly what it saw before.
mnovak added 2 commits 2026-11-16 01:03:19 +01:00
mnovak reviewed 2026-11-16 01:03:19 +01:00
mnovak left a comment
Author
Owner

Adversarial self-review — F22a, client state

Base: main at 47ed9ee. Findings R1–R7 are on their lines.

Evidence

What Result
cargo test --workspace --locked (Windows) client 24 (2 unit + 8 pairing + 1 real relay + 13 state), protocol 14, relay 17: 55 passed, 0 failed
The client tests on the Android emulator amicitia_test, Android 15, x86_64, NDK r27b over adb: 24 passed, 0 failed
cargo build -p tabellarius-client for aarch64- and armv7- Android passes
Clippy (-D warnings), fmt, check-deps.sh clean / ok / ok
New dependency none new to the tree: base64 0.22, already used by protocol
Planted bugs 10/11 caught; the 11th is equivalent (R4)
Planted bug Caught by
receive acknowledges inside the call 18 tests, the staged calls post nothing and acknowledge nothing among them
compose posts the staged calls…
accept posts the Welcome at once the staged calls…, finish acknowledges nothing when delivering fails
restore skips OpenMLS's storage 6 tests, every restart test
restore starts next_contact at 0 a restored client gives a new contact a new id, equal states give equal snapshots
finish acknowledges before delivering finish acknowledges nothing when delivering fails
finish never drops a used welcome mailbox a used welcome mailbox is no longer fetched
receive gives up on a relay failure a relay failing part-way returns what was processed
the snapshot not sorted equal states give equal snapshots
forget_invite forgets nothing a forgotten invite is no longer fetched
no guard against the same Welcome twice missed, equivalent (R4)

Not verified

  • A real phone; iOS.
  • Snapshot size with many contacts and long conversations: not measured (R2).
  • Amicitia's side (saving snapshot, events and outbox in one transaction) is F22b.

Verdict

R3 fixed on this branch; R1, R2, R5–R7 stated, with their answers; R4 kept as defence in depth. Merging.

## Adversarial self-review — F22a, client state Base: `main` at 47ed9ee. Findings R1–R7 are on their lines. ### Evidence | What | Result | |:--|:--| | `cargo test --workspace --locked` (Windows) | client **24** (2 unit + 8 pairing + 1 real relay + 13 state), protocol **14**, relay **17**: **55 passed, 0 failed** | | The client tests **on the Android emulator** | `amicitia_test`, Android 15, x86_64, NDK r27b over `adb`: **24 passed, 0 failed** | | `cargo build -p tabellarius-client` for `aarch64-` and `armv7-` Android | passes | | Clippy (`-D warnings`), fmt, `check-deps.sh` | clean / ok / ok | | New dependency | none new to the tree: `base64` 0.22, already used by `protocol` | | Planted bugs | **10/11 caught**; the 11th is equivalent (R4) | | Planted bug | Caught by | |:--|:--| | `receive` acknowledges inside the call | 18 tests, *the staged calls post nothing and acknowledge nothing* among them | | `compose` posts | *the staged calls…* | | `accept` posts the Welcome at once | *the staged calls…*, *finish acknowledges nothing when delivering fails* | | `restore` skips OpenMLS's storage | 6 tests, every restart test | | `restore` starts `next_contact` at 0 | *a restored client gives a new contact a new id*, *equal states give equal snapshots* | | `finish` acknowledges before delivering | *finish acknowledges nothing when delivering fails* | | `finish` never drops a used welcome mailbox | *a used welcome mailbox is no longer fetched* | | `receive` gives up on a relay failure | *a relay failing part-way returns what was processed* | | the snapshot not sorted | *equal states give equal snapshots* | | `forget_invite` forgets nothing | *a forgotten invite is no longer fetched* | | no guard against the same Welcome twice | **missed**, equivalent (R4) | ### Not verified - A real phone; iOS. - Snapshot size with many contacts and long conversations: not measured (R2). - Amicitia's side (saving snapshot, events and outbox in one transaction) is F22b. ### Verdict R3 fixed on this branch; R1, R2, R5–R7 stated, with their answers; R4 kept as defence in depth. Merging.
@@ -94,0 +113,4 @@
/// `Debug` shows neither the post token nor the bytes.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Outgoing {
Author
Owner

R7 — an Outgoing carries a post token. An app that keeps an outbox must keep it in encrypted storage too. Debug prints only the size. The blob itself is MLS ciphertext.

**R7 — an `Outgoing` carries a post token.** An app that keeps an outbox must keep it in encrypted storage too. `Debug` prints only the size. The blob itself is MLS ciphertext.
@@ -226,0 +297,4 @@
/// Stops fetching an invite's welcome mailbox: the invite was cancelled, or is older than the
/// relay keeps unclaimed invites (7 days; the app has the clock, the library has none).
/// Otherwise an unclaimed invite's mailbox is fetched for ever. Save the snapshot after it.
pub fn forget_invite(&mut self, invite: &InviteId) {
Author
Owner

R8 — the library has no clock. An unclaimed invite's welcome mailbox would be fetched for ever: wasted requests, and one more mailbox linked by timing (PROTOCOL.md section 1). forget_invite lets the app stop it after the relay's 7 days, or when the user cancels.

**R8 — the library has no clock.** An unclaimed invite's welcome mailbox would be fetched for ever: wasted requests, and one more mailbox linked by timing (PROTOCOL.md section 1). `forget_invite` lets the app stop it after the relay's 7 days, or when the user cancels.
@@ -226,1 +305,4 @@
/// creates our mailbox, then makes the group. The Welcome and our `hello` come back for the
/// app to deliver after saving.
pub fn accept(&mut self, invite: &Invite) -> Result<(ContactId, Vec<Outgoing>), ClientError> {
let kp_bytes = self.transport.claim_invite(&invite.id, &invite.secret)?;
Author
Owner

R6 — a claimed invite is used up before anything is saved. If the app dies between the claim and saving, the link no longer works; the person asks for a new one. Unavoidable with a one-time claim, and harmless: nothing readable is exposed.

**R6 — a claimed invite is used up before anything is saved.** If the app dies between the claim and saving, the link no longer works; the person asks for a new one. Unavoidable with a one-time claim, and harmless: nothing readable is exposed.
@@ -351,0 +477,4 @@
let blobs = match self.transport.fetch(&mailbox.id, &mailbox.fetch) {
Ok(blobs) => blobs,
Err(e) => {
got.interrupted = Some(e);
Author
Owner

R5 — a partial receive. If the relay fails after some mailboxes were processed, the client's state already includes them. Returning an error here would lose those messages: the app would not save them, and fetching them again gives MLS replays that are dropped. So receive returns what it has, with interrupted set.

**R5 — a partial receive.** If the relay fails after some mailboxes were processed, the client's state already includes them. Returning an error here would lose those messages: the app would not save them, and fetching them again gives MLS replays that are dropped. So `receive` returns what it has, with `interrupted` set.
@@ -359,0 +550,4 @@
.filter(|c| c.name.is_some())
.filter_map(|c| c.via)
.collect();
self.welcome_boxes.retain(|w| !joined.contains(&w.invite));
Author
Owner

R3 — found while building, fixed. The used welcome mailbox was first dropped inside receive. A snapshot saved between receive and finish then forgot it, so after a crash the Welcome and hello stayed on the relay for 30 days. Test a crash between receive and finish… failed on it (3 blobs waiting instead of 1). The drop now happens in finish, after acknowledging.

**R3 — found while building, fixed.** The used welcome mailbox was first dropped inside `receive`. A snapshot saved between `receive` and `finish` then forgot it, so after a crash the Welcome and `hello` stayed on the relay for 30 days. Test *a crash between receive and finish…* failed on it (3 blobs waiting instead of 1). The drop now happens in `finish`, after acknowledging.
@@ -367,0 +581,4 @@
.map_err(|_| ClientError::Mls)?;
// The same group twice (a Welcome fetched again after a crash): refused.
let group_id = staged.group_context().group_id();
if self.contacts.iter().any(|c| c.group.group_id() == group_id) {
Author
Owner

R4 — an equivalent planted bug. Removing this guard is not caught, because OpenMLS deletes the KeyPackage when it joins from a Welcome, so a second copy of the Welcome already fails. The guard stays as defence in depth, for a last-resort KeyPackage or a change in OpenMLS.

**R4 — an equivalent planted bug.** Removing this guard is not caught, because OpenMLS deletes the KeyPackage when it joins from a Welcome, so a second copy of the Welcome already fails. The guard stays as defence in depth, for a last-resort KeyPackage or a change in OpenMLS.
@@ -0,0 +65,4 @@
impl<T: Transport> Client<T> {
/// The whole state, to keep in the app's encrypted store. Secret: never log it, never send it.
pub fn snapshot(&self) -> Result<Vec<u8>, ClientError> {
Author
Owner

R1 — the snapshot is the identity. It holds every secret: MLS private keys, fetch and post tokens. It must live only in encrypted storage (Amicitia: SQLCipher), never in a log or a backup in the clear. The library writes no file and implements no Debug for it. Encrypting it again inside the library would need a second key stored somewhere; not done, stated in the analysis.

**R1 — the snapshot is the identity.** It holds every secret: MLS private keys, fetch and post tokens. It must live only in encrypted storage (Amicitia: SQLCipher), never in a log or a backup in the clear. The library writes no file and implements no `Debug` for it. Encrypting it again inside the library would need a second key stored somewhere; not done, stated in the analysis.
@@ -0,0 +75,4 @@
.iter()
.map(|(k, v)| (Bytes(k.clone()), Bytes(v.clone())))
.collect();
storage.sort();
Author
Owner

R2 — written whole after every change. A few KB per contact. Fine for 1:1 chats with dozens of people; a table per key would scale better. Not measured with many contacts; for later, when measured.

**R2 — written whole after every change.** A few KB per contact. Fine for 1:1 chats with dozens of people; a table per key would scale better. Not measured with many contacts; for later, when measured.
Author
Owner

Merged into main with --no-ff as 4f78101735 (remote hash verified). The self-review is review 376 on this PR (its summary says R1–R7; there are eight, R8 is on forget_invite).

Merged into `main` with `--no-ff` as 4f78101735ddd9f66c085b27a30844eb48006f9e (remote hash verified). The self-review is review 376 on this PR (its summary says R1–R7; there are eight, R8 is on `forget_invite`).
mnovak closed this pull request 2026-11-16 01:03:35 +01:00
Some checks are pending
ci / check (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.