Files
pi2d/AGENTS.md
T
yuez a57c874095 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.
2026-10-27 10:44:08 +08:00

4.4 KiB

AGENTS.md — pi2d

pi2 daemon: the web-console daemon for pi2.nvim. A browser talks HTTP/WS to it; it relays JSONL to one nvim per workspace. It is a router and supervisor — it never runs pi and never owns session state.

This file is an index and must stay under 100 lines. When something needs more than a line or two, put the depth in docs/ or a skill under .agents/skills/ and leave a pointer here. Adding bulk to this file is a mistake, not a contribution.

Quick reference

Task Command
Install pnpm install && pnpm --dir web install
Typecheck pnpm typecheck && pnpm --dir web typecheck
Unit tests pnpm test (daemon) · pnpm --dir web exec vitest run (web)
Build pnpm build → dist/ · pnpm build:web → web/dist/
Smoke bash scripts/smoke.sh — isolated tmp dirs, asserts HTTP, static UI, tokens, clean SIGTERM
E2E bash scripts/e2e/run-all.sh — API suite then Playwright; needs a real pi, nvim, curl
Dev loop pnpm dev (:7676) · pnpm --dir web dev (:5173, proxies /api + /ws)
Release skill .agents/skills/release/

Layout

Path Contents
src/protocol/ wire types, command/event vocabulary, error codes, canonical reducer
src/daemon/ HTTP/WS/SSE, auth, static assets, routing, event relay
src/instances/ discovery, lazy spawn, one-instance-per-workspace, crash handling
src/workspaces/ registry, disk discovery, aliases/trust, visibility policy
src/cli/ pi2 — daemon, open, attach, status, doctor, pairing, install-service
src/config.ts env-only configuration (PI2_*, PI2_HOST, PI2_WORKSPACE_*)
src/core/network.ts reachable addresses: kind classification, bases[] order, CORS origins
src/core/qr.ts zero-dependency QR encoder (pairing links; rows, no quiet zone)
web/ React console (own lockfile + workspace); built into web/dist and served by the daemon
fixtures/ golden event streams + expected state, shared with pi2.nvim conformance tests
test/ vitest, hermetic, injectable I/O
scripts/ smoke.sh, e2e/ drivers, gen-expected.ts

Hard rules

  1. docs/protocol.md is the contract. Any wire change lands with the daemon, the web client and the pi2.nvim bridge in view; fixtures/ are regenerated via scripts/gen-expected.ts.
  2. The daemon is not an agent runtime. No pi spawning, no JSONL session parsing, no session state. Snapshots and mutations are relayed to the plugin, which stays the single authority.
  3. dist/ and web/dist/ are build output — gitignored, never committed. They ship inside the npm tarball through prepack + the files allowlist.
  4. Two lockfiles. Root pnpm-lock.yaml and web/pnpm-lock.yaml are independent: use pnpm --dir web … for anything web-side, never a root install.
  5. Tests are hermetic. No real sockets, spawns or networks outside scripts/e2e/; inject probes (DoctorContext, WorkspaceStoreOptions.env, fake instance managers) and keep fixtures/ byte-identical.
  6. Local sign-in is loopback-only. POST /api/v1/console/session requires the CLI token from the runtime dir, the browser token travels in the URL fragment only, device tokens are stored hashed, and lan/tailscale modes never auto-sign-in.
  7. Conventional commits (feat(daemon):, fix(web):, docs:, ci:), scoped to the area.

Naming, so you do not get lost

npm package @yuez/pi2d (scoped — the unscoped pi2d is rejected by the registry's similarity filter), executable pi2, GitHub repo zgs225/pi2d (the trusted publisher matches on owner/repo/workflow filename, not the npm name).

Where to read deeper

Doc Contents
docs/protocol.md the browser ↔ daemon ↔ plugin wire contract
docs/deployment.md env vars, runtime files, ports, auth, services, troubleshooting
docs/e2e.md scenario ids, isolation, skip conditions, report paths
docs/releasing.md npm Trusted Publishing, the bootstrap phases, verification
README.md install paths, :PiWeb, binding & pairing
.agents/skills/release/ how to cut and verify a release

Before you say "done"

pnpm typecheck && pnpm test && pnpm --dir web typecheck && pnpm --dir web exec vitest run && bash scripts/smoke.sh green; docs updated where behaviour changed; no build output staged.