feat(daemon)!: phone-first binding and a two-step pairing flow

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.
This commit is contained in:
yuez committed 2026-10-27 10:44:08 +08:00
1 parent 3789394c85
commit a57c874095
34 files changed
+2157 -488

No files matched your search

+16 -10
View File
@@ -79,23 +79,29 @@ The plugin looks for the daemon in this order, and `:PiWebStatus` reports which
## Remote access (phone, LAN, Tailscale)
The daemon binds `127.0.0.1` by default and never exposes itself to the network on its own.
`:PiWeb` asks once which access mode you want and remembers the answer; `web.access = "lan"`
(or `"tailscale"`, or `"local"`) skips the prompt.
The daemon binds `0.0.0.0` by default, so a phone on the same network can open the console;
access is gated by pairing codes and per-device tokens, never by the bind address. Set
`web.host = "127.0.0.1"` (or `PI2_HOST=127.0.0.1`) for a loopback-only daemon, and
`web.public_url` when a `tailscale serve` portal is in front.
| Mode | Bind | Browser access | Login |
|---|---|---|---|
| `local` | `127.0.0.1` | same machine | automatic (one-time token handed to the browser) |
| `lan` | `0.0.0.0` | `http://<lan-ip>:7676` | pairing code / QR, **never** automatic |
| `tailscale` | `127.0.0.1` + `tailscale serve` | `https://<host>.<tailnet>.ts.net` | pairing code / QR, **never** automatic |
| Bind (`PI2_HOST`) | Browser access | Login |
|---|---|---|
| unset → `0.0.0.0` | `http://127.0.0.1:7676` on the desktop, `http://<lan-ip>:7676` from a phone, or a `tailscale serve` URL | desktop: automatic one-time token; other devices: pairing code / QR |
| `127.0.0.1` | this machine only (put `tailscale serve` in front if you need remote access) | automatic one-time token |
Pair a device from the CLI:
```bash
pi2 pairing # loopback URL + code + QR payload
pi2 pairing --public-url https://myhost.tail1234.ts.net
pi2 pairing # phone-reachable URL + code + terminal QR
pi2 pairing --base https://myhost.tail1234.ts.net # pin the interface to advertise
```
The link is built on the primary LAN address by default, so scanning it from a phone
actually works — the link carries host, port and code, so opening it pairs the device with
no further input; the mint response additionally carries the QR module matrix (`qr.rows`) and every
selectable base (`bases`), which is what `:PiWebPair` in Neovim uses: pick an interface
(name + IP), then draw the code.
Or from the web console's device page. Tokens are per-device, revocable, and stored
hashed; `pi2 doctor` reports the active mode and warns when a non-loopback bind has no TLS.