Implements ADR 0001 (docs/adr/0001-phone-first-pairing.md): the console is
meant to be opened from a phone, so reachability stops being a mode and the
pairing link becomes the only access control that matters.
Binding / CORS
- `PI2_HOST` defaults to `0.0.0.0`; `PI2_ACCESS_MODE` (local|lan|tailscale) is
deleted, together with its parser, `AccessMode` type and LAN probe.
- `src/core/network.ts` is the single source of truth for reachable addresses:
kind classification, ordering (public → lan → other → tailscale → loopback,
physical NICs first, link-local skipped), bind-aware `networkBases()`,
`primaryBase()` and `localOrigins()`. Config, the pairing route, doctor and
the CLI all consume it, so the CORS allowlist always matches the pairing URL.
- `/api/v1/info` and `daemon.json` report `host` instead of `access`; doctor's
`access` check becomes `bind` (loopback ok, wildcard warns unless an `https:`
public URL is configured).
Pairing
- `POST /api/v1/pair` only mints, requires a token, and returns the code, the
chosen URL, the expiry, the nonce, the QR matrix and every usable base.
`base` may pin one of those bases; anything else is a 400.
- `POST /api/v1/pair/redeem` is the public half, rate limited per client
(5/min) and globally (50/h) → 429 `E_RATE_LIMITED` with `retry-after`.
- `GET /pair/:code` answers 302 to `/connect?code=…&auto=1`, so a scanned link
needs no JavaScript to reach the console.
- `src/core/qr.ts` (moved out of the CLI) is the one encoder: byte mode, EC
level M, versions 1–10, rows without a quiet zone, shared by the HTTP mint
response and the terminal renderer.
- `pi2 pairing` prints a human report plus a half-block terminal QR and keeps
`--base`; `--json` and `--public-url` are gone (the Neovim client calls the
HTTP mint directly).
Device scopes
- `Scope`, `SCOPES`, `DEFAULT_SCOPES`, `DeviceRecord.scopes` and the `scopes`
field in device/hello payloads are removed: nothing enforced them, and a
paired device now holds the same rights as the local browser. A permission
model, if wanted, gets its own ADR.
Breaking: `PI2_ACCESS_MODE`, `info.access`, `pi2 status --json`'s `access`,
`pi2 pairing --json`/`--public-url`, `POST /api/v1/pair {code}`, and the
`scopes` field. All in-repo callers and the e2e drivers are updated; the plugin
side (`:PiWebAccess`, `env.PI2_ACCESS_MODE`, text-format parsing) follows in
pi2.nvim.
Verification: root typecheck clean; 369/370 tests (the one failure is the
pre-existing supervisor timing flake, 24/24 in isolation ×3); `smoke.sh` green;
web suite 299/299; visual matrix 24/24.
17 KiB
pi2 wire protocol (v1)
Single vocabulary, three surfaces. The plugin inside nvim is the only authority for session state.
browser <──HTTP + WS/SSE──> pi2 daemon <──JSONL over unix socket──> plugin (nvim)
pi2 daemonnever spawnspi, never parses session JSONL, never stores session state.- The browser keeps a render cache only; a
snapshotreplaces it wholesale. - One workspace maps to exactly one managed nvim instance.
1. Envelope
Every record is one JSON object per line (plugin↔daemon) or one WS message (browser↔daemon):
{
"v": 1,
"type": "hello | hello_ack | instance_state | workspace_update | subscribe | sub.subscribe | sub.unsubscribe
| snapshot | event | cmd | ack | error | resync_required | presence | bye",
"id": "req-…", // request/response correlation for cmd/ack
"workspaceId": "ws-…",
"sessionId": "…",
"epoch": "instance-uuid", // restart detection; seq is only valid within an epoch
"seq": 42, // per session, monotonic, produced by the plugin
"ts": 1733234400123, // wall clock, produced by the plugin
"mono": 918273, // monotonic ms, produced by the plugin (durations)
"origin": { "clientId": "…", "kind": "nvim|web|cli|daemon|system" },
"payload": { … }
}
Rules:
seqis per session and assigned by the plugin. The daemon never renumbers.epochchanges when the plugin process (re)starts or the session is re-opened by a different instance. A client holding a(epoch, seq)pair that no longer exists mustresync.ts/monoare authoritative; browsers never compare clocks across hosts.
2. Plugin ↔ daemon
The plugin dials the daemon (<runtime>/d.sock), sends hello first.
// plugin → daemon
{"v":1,"type":"hello","instanceId":"i-…","workspaceId":"ws-…","cwd":"/repo","pid":1234,
"hasUI":true,"listenAddr":"/run/user/1000/pi2/i/ab12.sock","pluginVersion":"1.8.0","capabilities":["snapshot","attention"]}
// daemon → plugin
{"v":1,"type":"hello_ack","daemonId":"d-…","limits":{"maxFrameBytes":8388608},"policy":{"attention":"all"}}
// plugin → daemon
{"v":1,"type":"instance_state","payload":{"status":"ready","sessions":[{"sessionId":"s1","status":"busy","name":"Refactor","tab":2,"model":{"provider":"anthropic","id":"claude-sonnet-4-5"}}]}}
{"v":1,"type":"snapshot","sessionId":"s1","epoch":"e1","seq":10,"payload":{"state":{ /* SessionState */ },"entries":[]}}
{"v":1,"type":"event","sessionId":"s1","epoch":"e1","seq":11,"ts":1733234400123,"payload":{"event":{ /* SessionEvent */ }}}
{"v":1,"type":"ack","id":"req-1","seq":12,"payload":{"disposition":"started"}}
{"v":1,"type":"error","id":"req-1","payload":{"code":"E_LEASE","message":"session is owned by instance i-2","retryable":false}}
// daemon → plugin
{"v":1,"type":"cmd","id":"req-1","sessionId":"s1","payload":{"method":"prompt.send","params":{"text":"hi"},"clientRequestId":"c-9"}}
{"v":1,"type":"sub.subscribe","sessionId":"s1"}
{"v":1,"type":"sub.unsubscribe","sessionId":"s1"}
{"v":1,"type":"instance.shutdown","payload":{"mode":"drain"}}
{"v":1,"type":"workspace.query","id":"req-2","payload":{"kind":"sessions"}}
Plugin obligations:
- Subscribe before prompt: the daemon calls
sub.subscribebefore forwarding a command; the plugin answers every new subscription with asnapshot(state + a tail of entries). - Throttle deltas:
text_delta/thinking_deltacoalesced to ≤30 fps (50 ms),tool_execution_updateto 100 ms. - Write-queue cap 4 MB per connection; on overflow drop low-priority events and emit one
droppedmarker. - Reconnect forever with exponential backoff (200 ms → 5 s). Never exit because the daemon is down.
3. Browser ↔ daemon
3.1 REST (reads)
| Endpoint | Notes |
|---|---|
GET /api/v1/info |
{v, daemonId, machineId, platform, hostname, os, version, uptimeMs, pid, port, capabilities, limits} |
GET /api/v1/health |
{ok, items:[{name, ok, detail}]} |
GET /api/v1/workspaces |
WorkspaceInfo[] (registry + disk index + instance state) |
GET /api/v1/workspaces/:id/sessions |
relayed workspace.query{kind:"sessions"} (row fields below) |
GET /api/v1/sessions/:sid/entries?before=&limit= |
relayed; plugin paginates |
GET /api/v1/attention?workspaceId= |
pending attention for one workspace |
GET /api/v1/devices |
paired devices (daemon-owned; {id, name, kind, createdAt, lastSeenAt, revokedAt}) |
POST /api/v1/pair |
authenticated; mints {code, url, expiresAt, nonce, qr, bases} |
POST /api/v1/pair/redeem |
public + rate limited (E_RATE_LIMITED); {code, kind, deviceName} → {token, device} |
GET /pair/:code |
public; 302 /connect?code=<code>&auto=1 (scanned links) |
GET /api/v1/models?workspaceId= |
relayed to the instance |
GET /api/v1/resources |
instances, sessions, RSS totals for the soft-limit notice |
GET /api/v1/diagnostics |
connections, event rates, slow queries, instance table |
Workspace row (GET /api/v1/workspaces) — registry entry ∪ disk discovery ∪ live instance state. Every known workspace is returned; rows are never filtered, so /workspace/<workspaceId> deep links always resolve:
| Field | Notes |
|---|---|
workspaceId |
sha256(realpath(cwd)).slice(0,16) |
cwd, name, alias |
name falls back to alias, then basename(cwd) |
pinned, hidden, trusted |
persisted user intent from workspaces.json |
exists |
false = ghost (the cwd is gone; the session files are not) |
sessionCount, liveCount, lastActiveAt |
disk projection (lastActiveAt may be null) |
instance |
live InstanceState or null |
visibility?: "default" | "secondary" |
derived, never persisted: the workspace-noise policy. Missing ⇒ default |
visibilityReason? |
hidden | ghost | temp | outside-roots | stale; absent for default rows |
The classification is advisory and client-side only (precedence, first match wins):
hidden: true→secondary/hidden— explicit user intent always wins, no rescue.- A live instance (
instance != nullorliveCount > 0),pinnedortrusted→default(rescue). PI2_WORKSPACE_HIDE_TEMP=1(default1) andcwdis under$TMPDIR,/tmp,/var/tmp,/var/foldersor/private/var/folders→secondary/temp.PI2_WORKSPACE_HIDE_GHOSTS=1(default1) andexists === false→secondary/ghost.PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS=1(default0) andcwdis outsidePI2_WORKSPACE_ROOTS→secondary/outside-roots.PI2_WORKSPACE_STALE_DAYS=<n>(default0= off) andlastActiveAtolder thanndays →secondary/stale(lastActiveAt: nullnever counts).- Otherwise
default.
A client that lets the user choose a workspace should list only default rows by
default; secondary rows belong behind a “show hidden” toggle. Lookups by
workspaceId must ignore visibility entirely.
Session row (GET /api/v1/workspaces/:id/sessions) — a subset of SessionState plus list metadata (usage, flags, entries and attention arrive via snapshot/events, not on this row):
| Field | Notes |
|---|---|
sessionId, sessionFile?, name?, cwd?, timestamp?, modified?, firstMessage? |
identity + disk projection |
tab?, live? |
live projection |
status? |
live status when live (busy, compacting, idle, exited); otherwise the child's manifest status (dormant, done, error, interrupted, exited). Absent on a non-live root. |
model?, thinking? |
per-session model pin; the child's manifest thinking level |
visible: boolean |
always present; the plugin's :PiSessions visibility decision |
parentId?: string |
sub-session rows only: the parent session id (normally present in the same list) |
task?, agentSpawned?, reported? |
sub-session rows only, carried through from the spawn request |
createdAt?, lastActiveAt? |
epoch ms; ordering/aging hints, never required |
Sub-session rows sit in the same flat list as their parent: build the tree from parentId
(an unknown parentId, or one whose row was pruned, promotes the row to a root so a row is
never lost). status of a non-live child is one of dormant|done|error|interrupted|exited;
a live child reports its live status.
Visibility follows the plugin's :PiSessions policy (visible: false = hidden), but the
field is advisory: a client may still render hidden rows on request (pi2 web's “show hidden
sub-sessions” toggle) or when the row is the routed/selected session. An active search filter
takes precedence over the routed row in pi2 web (a non-matching row stays out, even when it is
selected). A manifest pi2:subagents entry that also has a session row (same id) must render
as that row only — the session row wins. pi2:subagents entries may additionally carry
parentId?, visible?, createdAt?, lastActiveAt?.
3.2 Commands (writes)
POST /api/v1/cmd with {method, params, sessionId?, clientRequestId?} → 202 {accepted, disposition}.
Routing:
| Method | Routed to |
|---|---|
instance.ensure, instance.shutdown, workspace.*, pair.*, device.revoke, system.* |
daemon |
everything else (session.*, prompt.*, queue.*, attention.*, subagent.*, task.*) |
plugin of the workspace that owns sessionId (or of params.workspaceId for session.create/session.open) |
Idempotency: clientRequestId is deduplicated for 5 minutes; replays return the original ack.
3.3 WS events
Client → server: {"type":"subscribe","payload":{"workspaceId":"ws-1"}} (workspace scope) or
{"type":"subscribe","payload":{"sessionIds":["s1"]}}; {"type":"unsubscribe",...}; {"type":"ping"}.
Server → client (same envelope as above): hello_ack, instance_state, workspace_update, snapshot,
event, presence, resync_required, dropped, error.
A session subscribe is answered with a snapshot as soon as the plugin's reply
arrives (the daemon waits briefly for the plugin's sub.subscribe snapshot and also
forwards every later plugin snapshot to current subscribers).
GET /events (SSE) emits the same event records for degraded transports.
4. Session state
interface SessionState {
sessionId: string; workspaceId: string; cwd: string; name: string | null;
status: "starting" | "busy" | "compacting" | "idle" | "exited" | "interrupted";
flags: { attention: boolean; done: boolean; error: boolean };
model: { provider: string; id: string } | null;
thinking: "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max" | null;
usage: { input: number; output: number; cacheRead: number; cacheWrite: number; cost: number };
contextUsage: { tokens: number; window: number; percent: number } | null;
queue: { id: string; text: string; steer: boolean }[];
todos: { content: string; status: "pending" | "in_progress" | "completed"; activeForm?: string }[];
tasks: { id: string; command: string; status: "running" | "completed" | "failed" | "stopped";
startTime: number; durationMs?: number; exitCode?: number | null; outputFile?: string }[];
batches: { batchId: string; status: "running" | "completed" | "partial" | "failed" | "cancelled";
items: { ref: string; name?: string; status: string; error?: string }[];
summary?: { total: number; done: number; ok: number; failed: number; cancelled: number; running: number } }[];
subagents: { id: string; name?: string; status: "active" | "completed" | "failed" | "interrupted" | "dormant";
model?: string; thinking?: string; task?: string; children?: unknown[];
parentId?: string; visible?: boolean }[];
attention: { requestId: string; method: string; title?: string; message?: string;
options?: { id: string; label: string }[]; payload?: unknown; timeoutSec?: number;
createdAt: number; receivedAt: number }[];
entries: Entry[];
// List metadata — enriched by the session list (`§3.1`) / snapshot from the
// web client's cached row, never emitted by the reducer.
parentId?: string | null; // null = root (client-side only; the plugin omits the field)
visible?: boolean; // plugin `:PiSessions` visibility decision
task?: string; // subagent task description from the spawn request
}
type Block =
| { type: "text"; text: string }
| { type: "thinking"; text: string; redacted?: boolean }
| { type: "tool"; toolCallId: string; name: string; args?: unknown; output?: { text?: string; details?: unknown; isError?: boolean };
state: "pending" | "running" | "done" | "error"; durationMs?: number }
| { type: "error"; text: string };
interface Entry {
id: string; role: "user" | "assistant" | "system" | "notice";
ts: number; blocks: Block[]; done: boolean;
messageId?: string; images?: { mimeType: string; data: string }[];
}
4.1 Reducer rules (canonical, src/protocol/reducer.ts)
| Event | Effect |
|---|---|
agent_start |
status=busy, flags.done=false, flags.error=false |
agent_settled{aborted} |
status=idle (unless exited/interrupted), flags.done=!aborted, flags.error |= aborted |
compaction_start / compaction_end |
status=compacting / back to idle |
session_info_changed{name} |
name = name ?? null |
thinking_level_changed{level} |
thinking = level |
queue_update{steering,followUp} |
queue = [{id:"q:steer:N",steer:true}, {id:"q:follow:N",steer:false}] |
message_start{message} |
user/assistant entry created (m1, m2, …), blocks from content, done=false |
message_update{assistantMessageEvent} |
text/thinking deltas by contentIndex; toolcall_* creates/updates a tool block; error appends an error block |
tool_execution_start/update/end |
updates the tool block by toolCallId (running → `done |
message_end{message} |
replaces the entry blocks with authoritative content (tool blocks keep execution state), done=true, assistant usage is accumulated into session totals (cost.total → usage.cost) |
pi2:session{session} |
shallow patch of status/name/model/thinking/usage/contextUsage/flags |
pi2:todo{todos} |
replaces todos |
pi2:batch{batchId,status,items,summary} |
upserts into batches |
pi2:task{task} |
upserts into tasks by id |
pi2:subagents{subagents} |
replaces subagents |
pi2:attention_request{…} |
upserts into attention, flags.attention=true |
pi2:attention_resolved{requestId} |
removes from attention, flags.attention = attention.length>0 |
pi2:process_exit{code} |
status = code===0 ? exited : interrupted, flags.done=true |
pi2:error{message} |
appends an error block, flags.error=true |
Fixtures: fixtures/<case>/events.jsonl + expected.json (generated by pnpm tsx scripts/gen-expected.ts,
reviewed, committed). Both the TypeScript and Lua reducers must reproduce expected.json byte-for-byte.
5. Routing and lifecycle
browser cmd(sessionId=s1)
└─ daemon: find session owner
├─ known session → instance of that workspace → relay
├─ unknown → resolve workspace from session file → ensure instance (spawn if needed) → relay
└─ instance missing/degraded → start it (once), else 503 E_INSTANCE_UNAVAILABLE
instance.ensure{workspaceId}is idempotent and returns the currentinstance_state.- The daemon enforces one managed instance per workspace; a second nvim for the same cwd is reported
as
unmanagedand never receives browser commands. - Instances are never stopped automatically. Only
instance.shutdown(user action, confirm in UI) stops one. - On disconnect the daemon marks the instance
degraded; sessions becomeinterruptedwhen the instance reports process exit, or when the instance is restarted (epoch change).
6. Attention
pi2:attention_requestis broadcast to every client subscribed to that session and to the workspace inbox. The local nvim dialog is shown only when#nvim_list_uis() > 0.- The first successful
attention.respondwins; the plugin emitspi2:attention_resolved{requestId,by,response}. Everyone else closes and shows "resolved by ". - A second response returns
E_ATTENTION_RESOLVED. - Requests without a pi-level timeout are cancelled by the plugin after 120 s (
cancelled).
7. Errors
| Code | HTTP | Meaning |
|---|---|---|
E_BAD_REQUEST |
400 | malformed JSON / missing field |
E_UNAUTHORIZED |
401 | missing/invalid device token |
E_FORBIDDEN |
403 | bad Origin, cwd outside PI2_WORKSPACE_ROOTS |
E_RATE_LIMITED |
429 | too many pairing attempts (per client and globally) |
E_NOT_FOUND |
404 | unknown workspace/session/instance |
E_PROTOCOL_VERSION |
426 | v unsupported |
E_PAYLOAD_TOO_LARGE |
413 | body/frame > limits |
E_RATE_LIMITED |
429 | per-IP or per-room limit |
E_INSTANCE_STARTING |
409 (retryable) | instance is booting |
E_INSTANCE_UNAVAILABLE |
503 (retryable) | no instance and spawn failed |
E_INSTANCE_UNMANAGED |
409 | workspace served by an unmanaged nvim |
E_LEASE |
409 | session owned by another instance |
E_EXTERNAL_SESSION |
409 | session written by a process outside pi2 |
E_SESSION_NOT_FOUND |
404 | |
E_ATTENTION_RESOLVED |
409 | already answered elsewhere |
E_UNSUPPORTED |
501 | capability missing |
E_INTERNAL |
500 |
message is English/debug only; the browser maps code to localized text.