Plan: pi-remote speaks pivot-wire 0.4.0 — a record may span frames (#199)

Closes: #199. Branch: feat/wire-0-4. Status: Done (2026-09-23) — pi-remote 0.9.0; the live proof on the operator’s phone follows the release

TL;DR

#199 is the design; this plan records it and settles what it leaves open. pivot-wire 0.4.0 (the group registry’s latest since 2026-09-23; the PWA is on it) changes every transport plaintext: a record is split by recordChunks at chunkBytes() = 65 518 into chunks under a one-byte tag (0x00 last, 0x01 more follow), one chunk per sealed frame, all of a record’s chunks before any other record’s on that session; the receiver keeps one Reassembler per admitted session and gets the record at its last chunk. Cap maxRecordBytes() = 4 MiB both ways. Frame header, envelope, relay, handshake and pairing are untouched. Verified against the 0.4.0 tarball (index.d.ts, pkg/pivot_wire.d.ts, README § Send/Receive, vectors/vectors.json records section: chunk_bytes 65518, max_record_bytes 4194304, rekey_every 65536, three cases).

Today a phone on the new PWA handshakes, fails on the first record (record_tag), shows the "older wire" notice and churns; the box logs send_failed for nothing. The operator’s phone is in that state now.

The four obligations (pivot protocol.adoc § Records), where each lands

Obligation Where in pi-remote

Name a session before its first frame on it

Peer.sendControl: hello admits each sessions[i].session; session_opened admits info.session; session_ended forgets it. Every control the box sends passes through sendControl (link.ts broadcastControl/sendControlTo, the hub’s uplink, the pairing paired), so admission follows the box’s own word. Session 0 is admitted when the map is made at intoSession().

Open every frame before asking whether its session is admitted

Peer.receiveRecord: session.open(body) first, always; a decrypt failure keeps today’s drop-and-keep (snow does not advance the nonce on a failed open); an unknown session is dropped after the open, peer kept.

Any record_* error on receive ends the connection

Reassembler.push throwing record_tag → log pi_remote.peer_older_wire (the remedy in the line: reload the phone’s tab / update the PWA) and close; record_empty / record_size → log and close. A seal or send that throws mid-record → send_failed then close.

Never reuse a session id within one relay connection; reassemblers fresh per handshake

The hub retires ids and never reuses them (hub.ts); the map is made in intoSession() per peer, and close() frees every reassembler.

Design

U1 — the dependency and the numbers

packages/pi-remote/package.json: @gadhs/pivot-wire 0.3.0 → 0.4.0. wire.ts exposes the type Reassembler = InstanceType<Wire["Reassembler"]> and the numbers through the loaded wire (chunkBytes, maxRecordBytes, rekeyEvery) — the module has no wire of its own, so the constants that were literals become reads of the loaded module where a caller has one, and mirror.ts’s `MAX_RECORD_BYTES becomes 4_194_304 with a unit pin that it equals wire.maxRecordBytes(). ELISION_MARK stops saying "the frame carries 64 KiB" and says "the wire carries 4 MiB". commands.ts PAGE_BYTES stays 60 000: the reason is the phone’s memory now (pi-pivot#61), not the frame; the doc comment says so.

U2 — Peer: send

sendRecord: fitForWire(rec, MAX_RECORD_BYTES) stays as the last resort (#185’s elision, now at 4 MiB — a record_dropped is a record over 4 MiB with nothing to cut); then for (const chunk of wire.recordChunks(bytes)) deps.send(session, session.seal(chunk)). A throw inside the loop is send_failed and close("send failed mid-record") — the nonce has moved and the peer would splice the next record onto the half one. A throw from recordChunks itself (record_size, before any seal) is record_dropped, peer kept — nothing went out.

U3 — Peer: receive and admission

receiveRecord(session, body): chunk = this.session.open(body) (drop and keep on failure, as today); r = this.reassemblers.get(session) — none → frame_dropped "session N not admitted", keep, return; whole = r.push(chunk) — undefined → return (more to come); a WireError record_tag → pi_remote.peer_older_wire + close; other record_* → frame_dropped with the code + close; a whole record → decodeRecord and dispatch exactly as today. sendControl(rec) admits/forgets before the send, so the phone’s first frame on a session the box just named finds its reassembler. close() calls free() on every reassembler and clears the map. peer_older_wire joins LIFECYCLE_EVENTS in index.ts so it reaches link.log for the operator whose phone just went quiet.

U4 — the hub and the members

hub.ts MAX_LINE_BYTES 4 MiB → 8 MiB: a member’s line is the record plus the member envelope, and the comment "fitForWire bounds real records far below" must stay true with the record cap at 4 MiB. MAX_BACKLOG_BYTES (8 MiB) stays: it bounds queued lines per member socket, two records' worth. member.ts fits at MAX_RECORD_BYTES through the same fitForWire (it already does). Nothing else in the hub moves: the local socket carries JSON lines, not Noise frames; chunking is the relay link’s.

U5 — tests (the category named per case)

test/fake-relay.ts FakeDevice speaks the new wire: sendRecord chunks and seals per chunk; read opens every frame and feeds one Reassembler per session, admitted from the box’s hello / session_opened as it reads them (the fake is the phone’s side of the same contract). Then:

  • contract — test/vectors.test.ts: the records section — the three numbers against chunkBytes(), maxRecordBytes(), rekeyEvery(), and each case rebuilt (hex, or byte[i] = i mod 251 for the long ones), chunked, compared byte for byte, reassembled;

  • contract — a ~70 000-byte tool_execution_end reaches the device whole (no wireElided; first and last bytes asserted, not the length alone); a ~70 KB command reaches the box whole the same way;

  • regression (the symptom) — a 0.3.0 device (bare JSON sealed) → peer_older_wire logged, peer closed;

  • contract (obligation 2) — a frame on a session the box never named → dropped, peer kept, and the next frame on an admitted session still opens (the nonce advanced on the open);

  • contract (the nonce claim) — a tampered frame fails to open and is dropped, and the next good frame still opens: snow does not advance the nonce on a failed open. link.test.ts:429 covers the drop today; the "next frame opens" half is added;

  • contract (obligation 1) — session_ended forgets: a late chunk on that id is dropped, not spliced;

  • regression — MAX_RECORD_BYTES pinned to wire.maxRecordBytes(); a record of exactly that size chunks and reassembles; one byte more throws record_size from recordChunks and is record_dropped, peer kept;

  • contract (obligation 3) — a send that throws on the second chunk closes the peer (a fake send that fails once), and a send that throws on a one-chunk record closes it too;

  • `units.test.ts:427’s seal-boundary case moves from "MAX_RECORD_BYTES seals" to "chunkBytes() seals, one more does not";

  • contract — the hub e2e: a ~70 KB record from a member reaches the phone whole.

Coverage bars stand (lines 98 / branches 88).

U6 — docs and notes

remote.adoc: the wire paragraph (~74) says what a record is on the wire now and the 4 MiB cap; the paging sentence (~198) gives the phone’s memory as the reason; the "several sessions" section’s wire-limit sentence unchanged (idle/waiting carry no session id — that is pivot#102, not this). packages/pi-remote/README.md if it names the frame limit. CHANGELOG.adoc == Unreleased: pi-remote 0.9.0, Changed, breaking against a phone still on the 0.3.0 PWA (a stale tab handshakes and its greeting never reads as one until the tab reloads; pairings, keys, tokens and the handshake are untouched). The plan ledger records what the tarball showed.

Risks

Behaviour Mode Mitigation

A phone on the 0.3.0 PWA against this box

fail-closed, loud

peer_older_wire in link.log names the remedy; the phone shows its own notice; pairings survive, a tab reload fixes it.

A record_* error on receive (a chunk the box cannot place)

fail-closed

The peer is closed rather than resynchronised — there is no point in a Noise stream to resynchronise at. The device reconnects; the reassemblers are fresh at the handshake.

A send that throws mid-record

fail-closed

Close: the nonce has moved and the peer would splice the next record onto the half one. Logged as send_failed with the chunk index.

A frame on a session the box never named

silent to the phone, said in the log

Dropped after the open (so the nonce advances), frame_dropped with the session; the peer stays. This is the phone’s error, not the box’s, and pi-pivot owns the notice for it.

A record over 4 MiB

degrade, visible

fitForWire elides the longest string and marks the record wireElided, as #185 does today at 64 KiB; nothing to cut → record_dropped, logged.

The hub line cap

fail-closed

MAX_LINE_BYTES 8 MiB keeps a 4 MiB record plus envelope on one line; a longer line is skipped and logged as today, the connection kept.

A stale MAX_RECORD_BYTES literal

silent

Pinned equal to wire.maxRecordBytes(); a pivot-wire bump that moves the cap fails the suite.

Decisions the issue left open

  • pi-remote 0.9.0, not 0.8.2: the wire changed under every record; a phone on the old PWA stops working against this box. Minor, pre-1.0.

  • No hang-up verb: after close the device may sit deaf on an open socket until the tab reloads — pi-pivot#114 owns which hang-up the box should have; this change closes the peer and logs, no more.

  • Release: 0.59.0 was reserved for #196 U3 (the roles), which waits on the operator’s battery fill; this ships first as 0.59.0 on its own, and the roles take 0.60.0. Cut at once: the operator’s phone is down.

Verification and close

validate green (the bars unchanged). The vectors section byte for byte. Then the live proof, the operator’s: /remote-control from a session on 0.59.0 with the phone on the current PWA — the greeting reads, a long tool result arrives whole (no "truncated"), and link.log shows no send_failed. A phone on a stale tab shows the "older wire" notice once more, reloads, and reads. Close: MR Closes #199; a note on #199 with the merge SHA and the release; a note on pi-pivot#114 that the box now closes the peer and logs on a record error, the hang-up verb still theirs.

Scope

In: U1–U6, the release.

Out (named): the hang-up verb (pi-pivot#114); an overlay of PAGE_BYTES now that the frame is not the bound (pi-pivot#61 decides the phone’s budget); rekey handling (rekeyEvery is the wire’s, inside Session); any change to the local hub protocol’s framing (JSON lines stay).

Erratum ledger:

  • Erratum (2026-09-23): the seam is Peer and Link.sendRouted. The link caught a socket’s refusal and only logged it, so a send failing mid-record never reached Peer’s loop and the device would have been left holding half a record. `sendRouted now returns whether the frame went, PeerDeps.send returns that boolean, and Peer ends the link on false exactly as on a throw.

  • Erratum (2026-09-23): a seal that throws and recordChunks refusing a record the fit let through cannot be produced by the real wasm under fitForWire’s bound; their cases run through a test wire whose `seal and recordChunks are armed, everything else the real module. The session N drop at dispatch (a named session no member takes) is its own case now that an unnamed session drops at admission.

Edit this page · latest