Plan: a paired phone reads the box’s stored agent messages
Closes: #863. Relates to: pi-pivot #206 and its ADR-010 (the decision and the wire), #563 (spaces on the device session), #564 (the store), #185 (records cut to fit the wire). Branch: feat/phone-reads-messages. Status: Done (2026-11-20) — as planned, with the erratum below
Erratum (2026-11-20): the measured rows. The read-only run over real stored state: the Linux box, 4 channels and 2,097 messages; a copy of the Windows box’s, 20 channels and 4,097 messages; on both every message paged back once at limits 50, 7 and 1, no file changed, and the largest page was 146 KiB. Reading the largest channel’s file took 14.5 ms on the first and 43 to 49 ms on the second. Not run: a real phone, and the held-spaces rule on real state (the Windows store holds two revoked spaces' files).
Erratum (2026-11-20): D4 named one test that pins the greeting’s last record. Six assertions pin the greeting’s exact sequence, four in packages/pi-remote/test/link.test.ts and two in packages/pi-remote/test/extension.test.ts, which Scope did not list; each gains msg_channels after spaces.
Context
Agents on a box post to each other over the relay; pi-remote opens each message and keeps it in a store, one file a channel (packages/pi-remote/msg-store.ts, bounded at 16 to 20 MiB a file). The operator can read those messages only at a terminal.
pivot’s ADR-010 (accepted 2026-11-19 with the operator’s approval) lets the phone read them from a paired box’s store, over the Noise session the phone already has with the box. The phone holds no space secret, posts nothing and marks nothing read. pivot has published the wire as @gadhs/pivot-wire 0.11.0 and built the phone’s page; a pi box does not answer yet, so the page shows "does not report messages".
The wire, three control records on session 0 (pivot’s protocol.adoc, Control records):
box -> device msg_channels { channels: [{ space_id, channel, count, newest?: { id, sent } }] }
device -> box msg_fetch { space_id, channel, before?, limit }
box -> device msg_page { space_id, channel, messages: [StoredMsg], more }
StoredMsg is { id, re?, agent, handle?, from, subject, tags, body, sent }.
The operator’s decision for pi (2026-11-20, asked in session): every trusted paired device may read, with no switch. A paired device can already prompt the agent, which can read every message, and can already export a space’s secret (space_export), so this gives it no new authority.
Measured on the Linux box’s own store, read-only: four channel files, 4.3 MiB in all. The largest (3.7 MiB, 1,577 messages) reads and parses in 27 ms (median of seven). Message bodies: median 1.7 KiB, largest 16.5 KiB; the newest 50 together 60 KiB.
Design
D1. The wire dependency moves to 0.11.0
@gadhs/pivot-wire is pinned exactly in packages/pi-remote/package.json and packages/pi/package.json (it is vendored into the distribution). Both move from 0.10.0 to 0.11.0. pivot’s changelog says nothing in it breaks: it adds the three records and their types. The existing vectors test (packages/pi-remote/test/vectors.test.ts) runs against the new version unchanged.
D2. A new module answers from the store: packages/pi-remote/msg-serve.ts
Functions over MsgStore, with no socket and no timer. All are pure but MsgSummaries, which keeps the cache of D3:
-
toStoredMsg(entry): a stored message as the wire’sStoredMsg. It copies the nine named fields and nothing else, soseq,epoch,receivedand the read marks stay on the box. -
pageOf(messages, before, limit): one page.messagesis the channel oldest first, one per id (whatMsgStore.messages()returns). The page ends just before the message whose id isbefore, or at the newest message whenbeforeis absent or not held. It walks back from there and stops atlimitmessages, or before the message that would take the bodies pastPAGE_BODY_BYTES(1 MiB), or before the one that would take the encoded record pastPAGE_RECORD_BYTES. The first message is always taken.moreis true when an older message remains. The page is returned oldest first. -
fetchProblem(rec, held, channelNameOk): why a fetch is refused, or undefined. Aspace_idthat is not a string or is not held: "this box holds no such space". Achannelthat is not a string or fails the wire’schannelNameOk: "not a channel name". Alimitthat is not an integer from 1 to 50: "limit must be 1 to 50". Abeforethat is present and not a string: "malformed msg_fetch". -
MsgSummaries: the channel list.list(held)returns oneMsgChannelfor each channel the store holds in a held space: its count of messages and, when there is one, the newest message’sidandsent.
PAGE_RECORD_BYTES is 3 MiB, under the wire’s 4 MiB record cap (MAX_RECORD_BYTES in packages/pi-remote/mirror.ts). It exists because a record over the cap is cut to fit by fitForWire (#185), which shortens strings: a page of bodies full of characters JSON escapes sixfold could pass 4 MiB with 1 MiB of bodies, and its text would be cut. With the bound, no page is ever cut. One message alone always fits: a post is refused past 262,144 sealed bytes (SEALED_MAX_BYTES), and that escapes to at most about 1.6 MiB.
A channel the store does not hold, in a space the box does hold, answers an empty page with more false: no message remains.
D3. The channel list is cheap to recompute
The store has no index, so a count means reading a channel’s file. MsgStore gains one public member for this, stamp(spaceId, channel): the channel file’s size and modification time, or undefined when there is no file. The store keeps its paths private; this is the only new thing it exposes, and it reads no message.
MsgSummaries keeps, per channel, the summary it last computed and the stamp it was computed at. list() asks every channel’s stamp and re-reads only one whose stamp changed. A new message therefore costs one file read, and a file another process shortened (the store drops its oldest lines at its bound, and any session’s read mark can trigger that) is read again the next time it is asked, so a fallen count is reported as fallen. Nothing is cached across a restart.
D4. When msg_channels is sent
From the session that holds the box’s link, to each device that is ready and trusted (not a pairing connection):
-
in the greeting, after
spaces(Peer.greet, which also answers the device’srefresh).spaceswas the greeting’s last record, and a test inlink.test.tssays so; that test changes to expectmsg_channelslast for a trusted device andspaceslast for a pairing connection; -
after the store gains a message: when the link’s
Messagingkeeps one that arrived (onMessage) or one this box posted (publish); -
after the box’s spaces change (
Link.spacesChanged), since the list covers held spaces only.
The second and third are coalesced. The link holds one timer of MSG_CHANNELS_SETTLE_MS (500 ms): the first change starts it, later changes inside it do nothing, and when it fires one list goes to every ready trusted device. A replay of fifty messages is one record, inside pivot’s "within about a second". The timer is cleared in Link.stop() and is not started while no trusted device is ready.
A member session has no devices and sends nothing; the hub’s link is the only sender, and it is also the only writer of messages to the store.
D5. Answering msg_fetch
In Peer.onControl, a new case beside space_export, with the same gate: a pairing connection is answered refused { what: "msg_fetch", reason: "not yet a trusted device" }. Otherwise fetchProblem decides a refusal, and a fetch that passes is answered msg_page built by pageOf over MsgStore.messages().
The peer reaches the store through one new member of PeerDeps, messages, with channels() and page(…); the link supplies it. The handler reads and never writes: no read mark, no cursor. One log line records a served page: pi_remote.msg_page_served with exactly five fields, the device’s routing id, the space id, the channel, the count sent and more. A refused fetch logs pi_remote.msg_fetch_refused with the routing id and the reason. Neither carries a subject, a body, a tag or a sender.
Scope
One unit, one branch, one MR:
-
packages/pi-remote/msg-serve.ts(new),packages/pi-remote/msg-store.ts(stamp),packages/pi-remote/peer.ts,packages/pi-remote/link.ts,packages/pi-remote/messaging.ts(a callback when a message is kept),packages/pi-remote/package.json,packages/pi/package.json,pnpm-lock.yaml. -
Tests:
packages/pi-remote/test/msg-serve.test.ts(new),packages/pi-remote/test/msg-store.test.ts,packages/pi-remote/test/link.test.ts,packages/pi-remote/test/vectors.test.ts. -
Docs:
docs/modules/ROOT/pages/remote.adoc,docs/modules/ROOT/pages/security.adoc,docs/modules/ROOT/pages/public-surface.adoc,CHANGELOG.adoc.
Not in scope: posting or marking read from the phone; serving from a member session; a switch to turn serving off; an index in the store.
Risks
| Risk | Mode | Mitigation |
|---|---|---|
A device that is not trusted reads messages. |
fail-closed |
Only a ready peer that is not a pairing connection is sent |
A fetch reaches a space the box no longer holds, whose files are still in the store. |
fail-closed |
The held spaces are read at each fetch and each list. Tests: |
A channel name from the device is used as a path. |
fail-closed |
|
A page over the wire’s cap is cut by |
silent |
|
Reading marks something read, or moves a cursor, so an agent is told less. |
silent |
The handler calls only |
Message text reaches the box’s log through the new log lines. |
silent |
The two lines carry named fields only. Test: |
A burst of stored messages sends a list per message and floods the device. |
silent |
One timer coalesces. Test (concurrency): |
The timer fires after the link stopped and sends on a closed link. |
fail-closed |
Cleared in |
A cached summary is stale after another process shortened the file. |
silent |
The cache is keyed by size and modification time. Test: |
A trusted device floods fetches, each a file read on the hub’s thread (27 ms for the largest file measured). |
silent |
Accepted: the same device can already send prompts without bound. |
pivot-wire 0.11.0 changes something the box relies on. |
fail-closed |
The vectors test and the whole pi-remote suite run on 0.11.0; the distribution’s wasm check runs in consumer-sim and on Windows before merge. |
Tests
-
Contract (wire): the
msg_channelsandmsg_pagerecords the box builds carry exactly the fields of pivot-wire 0.11.0’s published vectors for those records;toStoredMsgof a stored message has the vector message’s nine keys and no other. -
Property (fast-check,
pageOf): for any channel,beforeandlimit: the page is never empty while a message remains beforebefore; it holds at mostlimit; its bodies are within 1 MiB unless it holds one message; it is oldest first and contiguous, ending just beforebefore; an unknownbeforegives the newest page; and walking back by each page’s oldest id returns every message exactly once. -
Unit:
fetchProblem’s four refusals and the limits 0, 1, 50, 51, 1.5 and a string; `MsgSummarieson an empty store, a channel with no message, and the shortened file;MsgStore.stampfor a file, a missing file and a name it refuses. -
Through the link, with a real Noise device (
link.test.ts): a trusted device is sentmsg_channelsafterhello; a fetch answers the page; one message over 1 MiB of body goes alone; and the named cases of the risk table. -
Concurrency: the two timer cases above, on fake timers.
-
Against real stored state, before merge, read-only: on the Linux box’s store and on a copy of the Windows box’s, every channel paged back from its newest page returns its messages exactly once, and the summary’s counts equal the store’s. The prediction is written before each run.
Not tested here: a real phone. That is pivot’s browser spec against a released pi (its unit U3), which looks for msg_fetch in the loaded pi-remote.
Delivery
-
The canonical page:
docs/modules/ROOT/pages/remote.adoc, a section "What the phone can read" beside "What the phone can send".docs/modules/ROOT/pages/security.adoc, A paired device, gains the sentence that a trusted device reads the stored messages of held spaces. `docs/modules/ROOT/pages/public-surface.adoc’s pivot-wire row moves to 0.11.0. -
CHANGELOG.adocunder Unreleased, Added. -
Before merge: validate as the user and as root, the full consumer-sim, and the Windows seat’s suite and stored-state run.
-
This plan is committed as
docs/modules/ROOT/pages/plans/phone-reads-messages.adocwith itsnav.adocline before implementation starts. -
Close: the MR carries
Closes #863; the merge SHA is noted on the issue, and pivot is told the release that carries it.