Plan: the phone reads the session’s history (#154, part 4)

Relates to: #154 (closes its last part). Branch: feat/remote-rpc-history. Status: Done (2026-09-19) — shipped as pi-remote 0.7.0; #154 complete

TL;DR

A phone that reconnects sees nothing of a turn that ran while it was away (pi-pivot#61). pi’s RPC has the two commands for that: get_entries (every session entry in append order, with a since cursor and the current leafId) and get_messages (the live context as AgentMessage[]). Both are reachable from an extension - sessionManager.getEntries(), getLeafId(), and buildSessionContext(), the function pi itself uses to rebuild the context after a compaction, exported from the package. The one thing pi’s RPC never had to think about is the frame: a sealed record is at most 65 519 bytes (#185), and a real session’s entries are far more. get_entries already carries a cursor, so it pages; get_messages has none, so a context that does not fit is refused with the reason and the cursor form named, never cut into a context that lies.

Decisions

pi’s shapes. get_entries { since? } → { entries, leafId }; an unknown since → success:false, “Entry not found: <since>” (pi’s text). get_messages → { messages }.

get_entries pages by pi’s own cursor. The answer carries as many entries from the requested point, in order, as fit under the frame’s budget - whole entries only, at least one - and leafId as pi’s does. The device asks again with since = the id of the last entry it received; an empty entries means it has everything up to the moment of asking. That is the cursor pi documents ("an entry id works as a durable cursor"), used more than once. pi’s RPC returns everything in one answer; the difference is stated on the page. No new field: the device’s stop rule is the empty page. One entry larger than the budget on its own is the one case a page cannot hold whole; it goes through the #185 fit (cut and marked) rather than block the cursor forever.

get_messages is whole or refused. The live context is one list the device renders as a conversation; a cut list is a wrong conversation. Over budget it answers success:false with the size, the budget and “use get_entries with a cursor”. Under budget it is pi’s answer.

Budget, not limit. PAGE_BYTES = 60_000: the frame less headroom for the envelope, ids and the leafId, measured as the record will be encoded.

Which entries. getEntries() - all of them, every branch, as pi’s get_entries does (the header excluded, as pi excludes it). get_messages is the active branch’s context: buildSessionContext(entries, leafId).messages. A device that wants only the active branch from get_entries follows parentId from leafId, as pi’s own clients do.

Live vs persisted. get_messages from an extension is built from the persisted entries; the assistant message of a turn in flight is not among them until message_end writes it. pi’s own get_messages reads the agent’s live state and would include the partial. Stated on the page; the device has the stream for the turn in flight.

Design

Parse (mirror.ts)

get_messages joins PLAIN_COMMANDS. get_entries { since? } is its own variant: since a string when present, else unknown.

Dispatch (commands.ts)

  • get_entries: entries = sm.getEntries(); with since, find its index (not found → pi’s error) and slice after it; then page(entries) - accumulate whole entries while the encoded size of { entries, leafId } stays under PAGE_BYTES, always at least one; data: { entries: page, leafId: sm.getLeafId() }.

  • get_messages: messages = buildSessionContext(sm.getEntries(), sm.getLeafId()).messages; measure; over PAGE_BYTES → success:false, error “the context is <n> bytes; a frame carries <budget>; use get_entries with a cursor”; else data: { messages }.

Both are pure over ctx.sessionManager; page is a small function with its own tests.

Tests

  • commands.test.ts: get_entries returns all when small, with leafId; since slices after the cursor; unknown since is pi’s error; a large session pages - the first page fits the budget, the next since continues exactly after it, the pages concatenate to the whole, the final page is empty; a single oversize entry is returned alone (the #185 fit handles it downstream). get_messages returns the context when it fits and refuses with the size when it does not; the context is the active branch (an entry off the leaf path is not in it).

  • units.test.ts: the parse variants.

  • extension.test.ts (real wire): get_entries through the wire on a session of many entries comes back paged under the seal limit, and the pages join to the whole; get_messages on a small session returns the messages.

Docs

  • remote.adoc § What the phone can send: the two commands, the paging rule and stop rule, the whole-or-refused rule, the live-vs-persisted note.

  • CHANGELOG.adoc: @gadhs/pi-remote 0.7.0, Added.

  • On #154: part 4 landed, the issue can close; on pi-pivot#61: the shapes and the paging rule.

Scope

In

The two commands, the paging, the refusal, tests and docs above; pi-remote 0.7.0.

Out

  • Whole delivery of a big record in one answer: pivot#101 (frame continuation). When it lands, PAGE_BYTES can grow or go; the cursor contract stays valid.

  • Any device-side rendering of entries: pi-pivot#61.

  • A since that names an entry on another branch: pi slices the flat append-order list the same way; matched.

Risks

  • A device that never sends the empty-page probe. It sees a prefix and believes it complete. The page says the stop rule plainly and pivot#61’s note carries it; pi’s own cursor semantics make the same demand of any incremental client.

  • buildSessionContext cost on a big session per call. pi does the same work on every compaction; a phone asks once per reconnect. Acceptable.

Edit this page · latest