# Remote control: answer and stop runs from your phone

<!-- toc -->
- [What it does](#what-it-does)
- [Commands](#commands)
- [How it runs](#how-it-runs)
- [What the computer checks for every request](#what-the-computer-checks-for-every-request)
- [What leaves the machine](#what-leaves-the-machine)
- [Threat model in plain terms](#threat-model-in-plain-terms)
- [Run handles and answers](#run-handles-and-answers)
- [Acknowledging requests](#acknowledging-requests)
- [Status and revocation](#status-and-revocation)
- [Where the code is shown](#where-the-code-is-shown)
- [The pairing secret and the transcript](#the-pairing-secret-and-the-transcript)
- [Transport](#transport)
- [What "unattended" means for a verb](#what-unattended-means-for-a-verb)
- [Requirements](#requirements)
<!-- /toc -->

Optional. Off until two things are true: the site has enabled the feature for
your account, and you have paired a phone with this computer. Until then
nothing runs, polls or listens.

## What it does

From a paired phone, and only these:

| Verb | Effect on this computer |
|------|-------------------------|
| `list_runs` | Lists runs (id, title, state, phase, pending question) |
| `run_status` | One run's state, phase and pending question with its options |
| `answer_question` | Records a list of option ids the run's pending question offered (never free text) |
| `stop_run` | Stops the run's session (`claude stop`). The worktree and branch stay |

There is no shell verb, no file verb and no free-form command. `start_run`
exists in the shared contract for a later phase and is refused here. The
verbs are fixed in `scripts/remote/verbs.json`; the site cannot add one.

## Commands

| Command | Purpose |
|---------|---------|
| `/multi-agent:pair` | Pair a phone (QR code or typed code, then a six-digit confirmation number) |
| `/multi-agent:pair --list` | Phones pinned on this computer |
| `/multi-agent:pair --revoke <device>` | Forget one phone here and remove it on the site when it is reachable |
| `/multi-agent:pair --off` | Stop the listener, forget every phone, remove them on the site when reachable. Works offline |
| `/multi-agent:pair --start` | Start the listener again for pinned phones |
| `/multi-agent:pair --status` | Listener and device count |

Pairing always needs the person and is refused in an unattended run
(`MULTI_AGENT_UNATTENDED=1`). A wrong confirmation number rejects the pairing,
warns that someone may be interfering, and blocks new pairings on this
computer for a cooldown that grows fourfold with each consecutive mismatch
(1 minute, 4, 16, 64, up to 24 hours), with at most 5 mismatches in the 24
hours counted from the first. A match ends the consecutive run, not the daily
count. The record is kept in a 0600 file and survives restarts; the site
cannot relax it.

## How it runs

After a successful pairing a background process (`remote-listener.mjs`) is
started, detached like the office server, so it outlives the terminal. One
record in the state directory names its pid and start time; there is one
instance. It makes outbound HTTPS long-poll requests to the site only; no
port is opened. It ends by itself when no phone is pinned, when the site
answers that the feature or your permission is off, or when the usage token is
refused, and then polls no more until you run `pair --start`. `pair --off`
stops it.

Machine identity is the per-machine usage token that usage registration
stored. The site is the one in `usageLog.endpoint`, trusted under the same
rule as the usage clients (its own host or `usageLog.trustedHosts`), https
only, and a redirect is never followed. The pairing page the QR code opens
(`pairUrl`, the origin the passkey is bound to) must be https on a trusted
host, with the path `/remote/pair` and no query; this computer never sends a
request, or the token, to that host. Machine routes are always on the site's
main API origin. The device id is part of the pairing transcript, so a site that
names a different device than the phone confirmed produces a different number.

## What the computer checks for every request

The site is not trusted. Each request is checked here before anything runs:
the device is one you confirmed locally; the sequence number is newer than the
last accepted (stored on disk, so a restart does not reset it); the passkey
assertion verifies against the key pinned at pairing (rpIdHash, user
presence and verification, type, origin, a challenge recomputed here from the
nonce, verb, run tag, sequence number and the exact ciphertext, ES256
signature, counter); the envelope decrypts with the header as authenticated
data; the plaintext verb equals the clear one and is a contract verb at an
enabled phase this computer implements; the arguments validate against the
schema; the request carries a nonce this computer issued less than 120 seconds ago and has not seen used (it is spent as soon as the request authenticates); the request's issue time is within 10 minutes of this computer's clock; for run verbs the run tag
matches the run named inside the payload. Answer and stop are refused when
this machine is unattended. Anything else is dropped and audited.

## What leaves the machine

- To the site: the usage token (in a header), the pairing lookup token, public
  keys, your answer to "did the numbers match" as accept or reject (never the
  number), and sealed envelopes. The site sees the verb name, the run tag
  (it cannot read the run id), sizes, timing and whether this computer is
  online or unattended.
- To the phone, sealed so only it can read: a handle, a title, state, phase, the
  pending question and its options. Text loses control, bidirectional and
  zero-width characters, passes through the secret and path scrubber, and is
  clipped: title and option labels to 60 characters, state and phase to 32, the
  question to 1000 (200 in the run list). A run list that would not fit one
  reply is cut from the end and carries `omitted`, the number of runs left out. No file contents, diffs, branch names, URLs, costs or keys.
- Locally, an audit log (`audit.jsonl`, 0600, rotated at 256 KiB): time,
  device (8 characters), verb, outcome and, for a verb about one run, that
  run's opaque handle (never the run id). Never content.

State lives under the autopilot root in `remote/` (0700): `devices.json` (the
pinned phones and their session keys), `audit.jsonl`, `lockout.json`,
`listener.json`, `ack.json`.

## Threat model in plain terms

**Pairing trusts the JavaScript the site serves to your phone while you pair.**
That page reads the pairing secret from the URL fragment, creates the phone's
keys and passkey in the browser and shows the six-digit number. A site that is
malicious at pairing time can serve a page that makes its own software keys,
computes the correct number from the secret it read, shows it on your real
phone and so gets a key it controls pinned on this computer. This computer
checks no attestation of the passkey, so it cannot tell a hardware passkey
from a software one. From then on that site can send phase 1 verbs with no
biometric prompt. The six-digit number protects against a network attacker and
against a site that tampers after an honest pairing. It does not protect
against a site that served hostile pairing code.

What limits the damage is the fixed verb list: list runs, read a run's status
and pending question, answer a question with one of the options it offered,
and stop a run's session. There is no shell, no file access and no way to start
a run. The feature is off by default. Enable it only for a site you operate or
trust as much as your own machine. Checking the passkey's attestation would
close this gap and is not done yet.

After an honest pairing:

- The site can stop the service, drop or delay messages, and see metadata:
  your account, device and machine, verb names, run tags, message sizes and
  timing, and whether the computer is online. It cannot read a request or a
  reply, forge, alter or replay one, or send a command without a key only the
  paired phone holds and a passkey assertion this computer checks against the
  key pinned at pairing.
- A device list from the site is used only to forget devices. A device id that
  is already pinned is never replaced: a second pairing with the same id is
  refused until you revoke the first.
- A man in the middle at pairing who substitutes a key or passkey passes the
  six-digit number check with probability 1 in 1,000,000 per attempt; every
  miss is visible, warns you, and locks pairing here.
- A site that serves malicious JavaScript to the phone after an honest pairing
  can use the phone's keys while the page is open, read what the page decrypts
  and show misleading text. It still cannot sign a verb without your biometric
  prompt for that exact request.
- A stolen phone needs your biometric for every verb, including reads. Remove
  it on the site and with `pair --revoke`.
- A site cannot hold a genuine signed request back and deliver it later: every
  poll carries a fresh nonce this computer made, the phone signs the newest one
  into the verb, and the request is accepted only with a nonce issued here in
  the last 120 seconds that has not been used. The computer keeps its last 8
  nonces in memory; a restart voids them. The issue time inside the request
  (`iat`) is still checked within 10 minutes as a second bound.
- A compromised computer has everything this feature has and more.

## Run handles and answers

The phone never sees a run id. `list_runs` returns a random handle per run, valid
for that phone until the computer restarts or the phone is removed; the other
verbs take the handle, and a raw run id or another phone's handle is answered
`unknown_run`. A question is `null` or `{id, text, kind, options}` with kind
`single` or `multi` and at most 8 options. An answer is a list of option ids
checked against the options the run actually offered (`unknown_question` or
`bad_answer` otherwise); the computer applies the ids, not any text.

## Acknowledging requests

A poll hands requests out without removing them from the site's queue. The next
poll lists the ids of the answer just processed in `ack` (at most 10; the list is
kept in `remote/ack.json` until a poll that carried it was answered, so a restart
still sends it). A request the site hands out again is dropped by its sequence
number: it runs once and is answered once. The poll body stays under 1 KiB.

## Status and revocation

Before showing a code, `pair` asks the site's `machine/status` and says which of
three things is true: the site has not enabled the feature, your account lacks
the permission, or the site did not accept this machine's usage token (the site
answers an invalid token with an empty 404, so only a valid token learns the
rest). The listener asks the same when `poll` answers 404, and stops for
"switched off", "no permission" or "token not accepted"; when the status cannot
explain the 404 it keeps trying. `pair --revoke` and `pair --off` also call
`machine/revoke` when the site is reachable and say plainly when they could
only act on this computer. The passkey counter starts at the `signCount` the
site reports for the registration.

## Where the code is shown

`pair begin` writes the QR code and the fallback code straight to the controlling
terminal (`/dev/tty`) and returns to the session only that they were shown and
how many minutes remain, so the code does not pass through the model's context.
The command file therefore tells the person to run `pair begin` themselves, in
a terminal window of their own (the host's `! <command>` prefix runs it from
the prompt; whether it has a terminal there is the host's choice), and the
model continues from `pair wait`.

Inside the host's shell tool `/dev/tty` cannot be opened, so when the model runs
`pair begin` the QR code and the code always land in the session output and are
visible to the session, and are written to its transcript. That is the fallback,
and the command file says so before it uses it. It is bounded by the six-digit
confirmation, which only the phone shows and only the person types, and by the
computer's own lockout: a session that sees the code but not the number cannot
finish a pairing.

## The pairing secret and the transcript

`pair begin` prints the QR code and the code to the terminal. When the command
runs inside an assistant session, that output passes through the assistant's
tool output and is therefore also written to the on-disk session transcript,
and the secret and the ephemeral key sit in `remote/pending.json` while the
pairing runs. That is acceptable because the code is single use, expires after
five minutes and is useless without the confirmation number typed on this
computer. `pending.json` is deleted as soon as the pairing ends, whichever way
it ends.

## Transport

Requests go through Node's https only, with certificate verification forced on
for every request and a dedicated agent, so `NODE_TLS_REJECT_UNAUTHORIZED` and
proxy variables change nothing. The trust store is the one the usage transport uses
(`lib/tls-trust.mjs`): Node's bundled roots plus the file `NODE_EXTRA_CA_CERTS`
names, passed as the agent's `ca`; `SSL_CERT_FILE`, `SSL_CERT_DIR`,
`--use-openssl-ca` and `NODE_USE_SYSTEM_CA` add nothing. A certificate or TLS error fails that poll and
backs off; a 4xx answer with a body that is not JSON (a hosting layer's own
404) is still returned as its status, so the switch-off check runs; there is no retry through another client. The listener starts with a
short list of inherited variables (path, home, state locations, the unattended
flag, `NODE_EXTRA_CA_CERTS` for a network with its own certificate authority);
`NODE_OPTIONS`, `NODE_TLS_*`, proxy variables and the usage token are not
passed on, and the listener reads the token from the credential store itself.

## What "unattended" means for a verb

Answer and stop are refused when the listener itself was started in an
unattended environment (`MULTI_AGENT_UNATTENDED=1`, fixed when it started), and
also when the run they target is unattended: its own state says autopilot, or
it was launched in the background under the unattended log root. The stricter
of the two applies.

## Requirements

- A usage token on this machine (`/multi-agent:setup`) and a site account the
  token maps to.
- The site must have switched the feature on globally and for you.
- Node 20.11 or newer. `openssl` is not needed.
