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

+21 -11
View File
@@ -139,7 +139,7 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s
- `GET /api/v1/health` → `ok=true`, `static` item ok (web/dist served).
- `GET /` → 200 HTML containing the app root.
- unauthenticated `GET /api/v1/workspaces` → 401 `E_UNAUTHORIZED`; bogus bearer → 401.
- `POST /api/v1/pair` redeem: valid code → `{token, device}`; same code again → 401;
- `POST /api/v1/pair/redeem`: valid code → `{token, device}`; same code again → 401;
the token authorizes the API; `DELETE /api/v1/devices/:id` revokes it (next call 401).
**A1 Workspaces**
@@ -222,7 +222,7 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s
**A13 Console login handoff**
- `POST /api/v1/console/session` with the CLI token → 200, one `local browser`/`laptop`
device with every scope, and a `http://127.0.0.1:<port>/#t=<token>` URL (fragment only,
device, and a `http://127.0.0.1:<port>/#t=<token>` URL (fragment only,
never `?t=`).
- the handed-off token authenticates; a second login returns the **same** token so a second
`:PiWeb` does not log the open tab out.
@@ -244,8 +244,8 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s
allow-listed `next`.
**B2 Pairing (manual)**
- enter host/port/pairing code/device name → `POST /api/v1/pair` succeeds; the summary
shows server/endpoint/device/scopes; localStorage keys are written; redirect to `next`.
- enter host/port/pairing code/device name → `POST /api/v1/pair/redeem` succeeds; the summary
shows server/endpoint/device; localStorage keys are written; redirect to `next`.
- wrong code → inline localized error, still unpaired, form stays usable.
- while paired, `/connect` shows the summary, not the form; disconnect asks for
confirmation and clears the connection (returns to `/connect`).
@@ -254,6 +254,12 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s
- paste a `pi2://pair?host=…&port=…&code=…` link in the scan fallback → confirmation
modal shows the masked token → confirm pairs; cancel changes nothing.
- the daemon's own `http://host:port/pair/<code>` URL is accepted by the parser.
- opening that URL directly is **zero-input**: `/pair/<code>` redirects to
`/connect?code=<code>&auto=1`, which redeems against its own origin with the
localized default device name and lands on `/workspaces` (or `next`) without the
form ever being shown; the device then appears on the status page.
- a rejected/expired code falls back to the manual form with host, port and code
prefilled from the link, plus the inline pairing error.
**B4 Workspaces**
- seeded W1/W2 render as `[data-workspace-id]` rows, pinned/last-active order, context
@@ -284,11 +290,14 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s
- change the thinking select to `low` → API command sent, state/toast update, value
survives a reload; change the model select → the same, and the tree subtitle updates.
**B9 Chat: panels (Subagents / Todo / Tasks)**
- panel tabs switch; each renders either real data or its empty state; no mock copy.
- Dispatch modal creates a real batch (`items[]`), the subagent row/batch appears, and
the preview modal opens (real tail or an explicit empty state); “Back to parent” works.
- Tasks tab shows real tasks or the empty state; the task output modal shows the real
**B9 Chat: session tray (Todo / Tasks) + subagent child view**
- the stream-head chips (`计划` / `任务`) open the session tray; the two tabs switch and
each renders either real data or its empty state; no mock copy; the right panel is gone
(`.panel-pane` count is 0) and Esc returns focus to the chip.
- the row `…` popover dispatches a subagent: a real batch (`items[]`) appears, the row
shows the child in the tree, and the in-place child view renders the synthesized
timeline (`sub-timeline`) or an explicit empty state; `← 父级` / Esc works.
- the Tasks tab shows real tasks or the empty state; the task output modal shows the real
output tail or an explicit unsupported state (no fake pid).
**B10 Attention**
@@ -296,8 +305,9 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s
title/options; Accept writes the file; the dialog closes; a second client cannot answer.
**B11 Status**
- `/status` renders info/health/resources/models/tasks/devices/connection cards from the
API; no server/workspace selector; resources refresh; health items show pass/fail.
- `/status` renders the four metric tiles + info/health/models/tasks/devices/connection
cards from the API; no server/workspace selector; metrics refresh; health items show
pass/fail. The page has zero primary actions.
- paired devices come from the API and can be revoked with confirmation; a revoked
device token stops working.
- disconnect clears connection and returns to `/connect`.