Plan: the phone sends pi’s control commands (#154, part 2 + get_state)
Relates to: #154 (parts 3 and 4 stay open there). Branch: feat/remote-rpc-control. Status: Done (2026-09-16) — shipped as pi-remote 0.2.0; parts 3 and 4 stay on #154
TL;DR
The phone cannot send a prompt while a turn runs. It sends prompt;
pi-remote calls pi.sendUserMessage(message) with no deliverAs; pi
throws "Agent is already processing…"; the ExtensionAPI wrapper swallows
that rejection into emitError, so pi-remote’s .catch never fires; and
the phone hears nothing — the message is gone. In the TUI the same keypress
queues (steer). pivot’s operator report (2026-09-16) names this as the
daily-use gap, and asks for the fix in pi’s own shapes: the phone sends the
RPC command records pi --mode rpc documents, pi-remote answers with pi’s
response records, and the PWA mirrors verbatim. This plan lands the
control half of #154: prompt with streamingBehavior, steer,
follow_up, abort, get_commands, get_state — and says plainly which
of pi’s commands an extension cannot honour (clear_queue, the
queue_update event) rather than faking them.
Decisions
pi’s shapes, verbatim, including the refusals. Every command and every
response is the one in pi’s docs/rpc.md at 0.85.1 (the package’s
installed peer; the shapes used here are unchanged from 0.84.3). Where
pi-remote must decide before pi does — it cannot see pi’s rejection — it
applies pi’s own rule and pi’s own error text: streaming and no
streamingBehavior → success:false with "Agent is already processing.
Specify streamingBehavior ('steer' or 'followUp') to queue the message.";
compacting → success:false with "Cannot submit a prompt while compaction
is in progress. Wait for compaction to finish and retry." Nothing is
invented; a field pi-remote cannot know is omitted, never guessed.
What an extension cannot reach is refused, not imitated. The ExtensionAPI
has sendUserMessage(deliverAs, expandPromptTemplates), getCommands,
getThinkingLevel, ctx.isIdle, ctx.hasPendingMessages, ctx.abort;
it has no clearQueue, no queue contents, no queue_update event (all
AgentSession-only). So clear_queue answers success:false with an
error that says so, and no queue_update is emitted — pivot infers from
responses, as it offered. The upstream ask (queue access on the
ExtensionAPI; sendUserMessage returning its rejection) goes on #11’s
list; when it lands, both become one-line changes here.
Slash commands pass the pre-check. pi runs extension commands (/mode,
/remember) immediately even mid-turn or mid-compaction, before either
check, so a message beginning with / is never refused by pi-remote’s
pre-check and is sent with expandPromptTemplates: true — as pi’s RPC
always does. The one
case the pre-check cannot close: a /-prefixed message that is not an
extension command, sent mid-turn without streamingBehavior, is refused by
pi after acceptance and the phone sees success:true. Named in the docs;
closed by the same upstream ask.
The response goes to the peer that asked, on the same session id. The
box has one session (THIS_SESSION); a response is a session-frame record
like any event, sealed to the sending peer only, never broadcast.
A command without an id is still answered — pi does the same (the
response simply carries no id). Today’s PWA sends prompt without an id
and shows an unknown record as a raw card; pivot asked for this behaviour.
Unknown commands stay on the existing path: dropped and logged, never a
crash and never a response — the session-frame rule pivot wrote.
Design
Parse (mirror.ts)
PhoneCommand grows; each variant carries id?: string:
-
prompt { message, streamingBehavior?: "steer" | "followUp" }— astreamingBehaviorof any other value is ignored (pi’s rule: unknown → none). -
steer { message },follow_up { message }— pi’s shorthands; they dispatch aspromptwith the matching behaviour. -
abort,clear_queue,get_commands,get_state. -
ui_responseunchanged;unknownunchanged.
message must be a non-empty string as today; images is not read (out
of scope, below). MAX_PROMPT_CHARS applies to prompt, steer and
follow_up alike, refused in words as today (#171) — the refusal is a
response success:false on the session frame (pi’s shape) and the
existing control-frame refused record stays for today’s PWA.
Dispatch (commands.ts, new)
dispatch(cmd, host): Promise<RpcResponse | undefined> — pure over a
host seam, so every branch is a unit test:
interface CommandHost {
pi: Pick<ExtensionAPI, "sendUserMessage" | "getCommands" | "getThinkingLevel">;
ctx(): ExtensionContext | undefined; // the last one seen; undefined before session_start
isCompacting(): boolean; // tracked from session_before_compact / session_compact / _failed
}
type RpcResponse = { type: "response"; command: string; id?: string; success: boolean; data?: unknown; error?: string };
-
prompt/steer/follow_up→deliverAsfrom the behaviour; no ctx →success:false"no session"; compacting → pi’s compaction text;!ctx.isIdle()and nodeliverAsand message not/-prefixed → pi’s streaming text,sendUserMessagenot called; otherwisepi.sendUserMessage(message, { deliverAs?, expandPromptTemplates: true })→success:true. -
abort→ctx.abort()→success:true. -
clear_queue→success:false, error "clear_queue is not available from an extension host: pi’s ExtensionAPI has no queue access". -
get_commands→data: { commands: pi.getCommands() }. -
get_state→data: { model: ctx.model ?? null, thinkingLevel, isStreaming: !ctx.isIdle(), isCompacting, sessionFile, sessionId, sessionName? (omitted when unset, as pi does), messageCount }wheremessageCountcountsmessageentries on the active branch (ctx.sessionManager.getBranch()). Omitted because an extension cannot know them:steeringMode,followUpMode,autoCompactionEnabled,pendingMessageCount(onlyhasPendingMessagesexists, a boolean, and pi’s field is a count). -
ui_responseandunknownare not dispatched here (peer keeps them).
The command field of the response is the command’s own type string
(follow_up, not followUp).
Wiring (peer.ts, link.ts, index.ts)
PeerDeps.onPrompt / onAbort become one
onCommand(cmd: PhoneCommand): Promise<RpcResponse | undefined>;
Peer.onCommand awaits it and, when defined, sendRecord(THIS_SESSION,
response) to itself. A rejection from onCommand is logged
(pi_remote.command_failed) and answered success:false with the error’s
message — never a throw out of the socket handler (#171’s fence holds).
LinkDeps carries the same onCommand. index.ts builds the host:
pi from the extension’s API, ctx() from state.lastCtx, and
isCompacting() from a flag set on session_before_compact and cleared on
session_compact / session_compact_failed (three handlers added in the
existing event wiring). RemoteState.pi widens from
Pick<ExtensionAPI, "sendUserMessage"> to the three methods.
Tests
-
commands.test.ts: each command’s response shape; mid-turnpromptwithout behaviour → pi’s text and no send; withsteer/followUpand via the shorthands →deliverAsmatches andexpandPromptTemplatesis true;/mode planmid-turn without behaviour → sent; compacting → pi’s compaction text; no ctx → "no session";abortcallsctx.abort;clear_queuerefused with the reason;get_stateomitssessionNamewhen unset and the four fields it cannot know,messageCountcounts message entries only;get_commandspassesgetCommands()through; a host that throws →success:falsewith the message. -
link.test.ts(the existing wasm-device harness): apromptwith anidon the session frame produces oneresponseframe on the same session id, to the sending peer only (a second paired device sees nothing); asteermid-turn is answeredsuccess:true; a response never goes on the control frame; an over-longsteergets both the controlrefusedand the sessionresponse. -
units.test.ts(whereparseCommandis tested today): the new parse variants,idcarried, a badstreamingBehaviordropped,imagesignored.
Coverage bars unchanged.
Docs
-
remote.adoc§ First-class: a "What the phone can send" list — the six commands, the two refusals and why, the slash-command caveat, theget_statesubset — pointing at pi’sdocs/rpc.mdfor the shapes. -
security.adoc, the pi-remote paragraph (the frame guarantees, #171): the response goes only to the asking peer;MAX_PROMPT_CHARScovers the two new message-bearing commands. -
CHANGELOG.adoc:@gadhs/pi-remote0.2.0, Added. -
On #154: which items landed, the upstream asks, the caveat; on #11: the two ExtensionAPI asks.
Scope
In
-
mirror.tsparse growth;commands.ts; theonCommandseam through peer, link and index; the compaction flag; tests; the docs above; pi-remote 0.2.0.
Out
-
imagesonprompt/steer/follow_up— the PWA does not send them yet; parsing them raises the frame-size question (#171’s bound) and is its own change. -
clear_queuedoing anything, thequeue_updateevent,pendingMessageCount,steeringMode/followUpMode/autoCompactionEnabledinget_state— not reachable from an extension; upstream asks on #11. -
#154 part 3 (models, thinking,
compact, session stats, forwardednotify/setStatus) and part 4 (history:get_messages,get_entries) — each starts when pivot’s side is ready for it; part 4 is pi-pivot#61’s. -
Answering unknown commands with pi’s
success:false— pivot’s session-frame rule says drop and log; changing that is pivot’s call. -
Mirroring more events (
compaction_*,auto_retry_*) — part 3.
Risks
-
A
/-prefixed non-command mid-turn reportssuccess:trueand is refused by pi afterwards. Named in the docs; the upstream ask closes it. Frequency: low — the phone’s/messages are pi-modes' commands. -
Today’s PWA receiving
responserecords for its id-less prompts. It shows an unknown record as a raw card by its own rule; pivot asked for the records and is building the UI that consumes them. -
isCompactingdrifts ifsession_compactnever fires aftersession_before_compact.session_compact_failedclears it too; an aborted compaction reaches one of the two. If neither, the flag stays set and prompts are refused with the compaction text until the next compaction — visible, not silent, and the phone can stillabort.