Skip to content
pi-c Artifact [a604a8aa47]
Login

Artifact [a604a8aa47]

Artifact a604a8aa47331bd101182b749cd8a65d12bb1c3d9d6d3255ab12c32b3767ab73:


# Remote control: the relay side

The wire contract is [the remote protocol](../design/remote-protocol.md), with its Amendments. This
page is the invariants in `src/remote.c`. The user's view is
[remote control](../manual/remote-control.md); the inbox it writes to is [the inbox](inbox.md).

## State

Two tables in `~/.pic/pic.sqlite`, both sealed with the credential store's key:

| Table | Holds | Sealing AAD |
|---|---|---|
| `picu_remote_key` | pic's static key, the mailbox id and owner token, by `name` | `remote-key:NAME` |
| `picu_remote_device` | one paired phone: `id`, sealed JSON, `paired_ms`, `seen_ms` | `remote-device:HEX` |

Unpair deletes a device row and needs no key. Reset deletes both tables' rows in one transaction,
then the mailbox at the relay (best effort; the relay drops an unused mailbox after 90 days).

## The passphrase rule

`pic remote` unlocks the store on the terminal at every start (`rmt_unlock_fresh`): it locks first
and never takes a key from a parent or the platform's cached key. With no terminal it exits. When
the store has no passphrase yet it refuses and tells the user to pair first.

## Process model

One loop and one stream thread. The thread reads the relay's up stream and writes whole records to
a pipe; it never touches SQLite. The loop owns the database, the sessions and the lane posts.
A session is confirmed by the first message that verifies. Nothing is persisted but rows: no
cursor, no session. A restart reconnects and each phone resyncs from its own cursor.

`pic remote` takes no lease and writes no journal rows except inbox rows and keyed admissions.
One per user: `~/.pic/remote.lock`.

## Every command commits before it answers

| Command | Commit |
|---|---|
| `steer`, `follow_up`, `abort` | an inbox row, key `rc:DEVICE:KEY`, sender `remote:DEVICE` |
| `prompt`, conversation idle | keyed admission (`pic submit KEY`), then `pic run` in a `setsid` child holding the handed-down key (`PIC_KEY_FD`), so the request outlives `pic remote` |
| `prompt`, request running | the inbox, as a steer would |
| `get_state`, `get_messages` | answered from the journal; no row |

Retrying a key returns the first answer. A kill before the commit means the phone retries; a kill
after it means the retry is recognised: delivered once either way.

## The pairing channel

pic deletes the pairing channel P only after the relay says the phone is reading it: `Pic-Peer: 1`
on the post of `paired`, or a peer event that it came back, at most 30 s after posting. When the
relay sends no `Pic-Peer`, 2 s. Deleting sooner loses `paired` for a phone that was reconnecting.

## Tests

- `test/pic/fake_relay.c` is a loopback relay implementing protocol section 6 without limits;
  `test/pic/fake_phone.c` plays the phone with the dev build's initiator. `test/pic/remote.sh` runs
  `pic remote` against them, including kill-and-restart crash cases.
- `test/pic/remote-interop.sh` runs the real Worker from `~/pic-relay` under `wrangler dev` with the
  page's protocol module driven from Node. It is skipped when node, wrangler or the checkout is
  absent.
- `PIC_TEST_PAIR_TTL` (seconds) shortens a pairing offer, for tests only.