Files
pi2d/README.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

7.2 KiB
Raw Blame History

pi2d — pi2 web console daemon

The always-on web side of pi2.nvim. It is a router + supervisor, not an agent runtime:

browser ──HTTP/WS──▶ pi2 daemon ──JSONL unix socket──▶ one nvim per workspace (pi2.nvim)
                                                         └── one pi --mode rpc per active session

Hard rules (see docs/protocol.md):

  1. pi2 daemon never spawns pi, never reads session JSONL, never owns session state.
  2. A workspace maps to exactly one managed nvim instance.
  3. All mutations are commands relayed to the plugin; the plugin (inside nvim) is the single source of truth.
  4. The web client keeps a render cache only; a snapshot from the plugin replaces it wholesale.

Install

Requirements: Node >= 22, Neovim >= 0.9, the pi CLI on PATH.

You do not normally install this by hand. The pi2.nvim plugin detects a missing daemon, offers to install it into its own data directory (no sudo, no global PATH edits) and starts it lazily. The result is one command inside Neovim:

:PiWeb

…which opens your browser already signed in, showing the current workspace.

Status. The npm package, the packaged web console and the lazy-start contract are ready (npm i -g @yuez/pi2d works today). :PiWeb, :PiWebInstall and the access-mode prompt land with the next pi2.nvim release; until then use :PiWebStart plus a manual install.

Package managers (optional, for CLI-first users)

npm i -g @yuez/pi2d   # installs the `pi2` binary
pnpm add -g @yuez/pi2d
bun add -g @yuez/pi2d

Then:

pi2 daemon                 # foreground; the plugin/browser needs this running
pi2 install-service --write   # or run it at login: systemd --user / launchd
pi2 status                 # daemon + instances + health
pi2 doctor                 # node/nvim/pi/curl/plugin/runtime/web-dist/protocol checks

pi2 is the only executable (@yuez/pi2d is the npm package name); pi2 daemon is a subcommand of it, not a separate process. Runtime files, ports, auth, services and troubleshooting: docs/deployment.md.

Neovim plugin

{
    "zgs225/pi2.nvim",
    opts = {},                    -- add `web = { enabled = true }` to start the bridge at setup
    cmd = { "PiWeb", "PiWebStart", "PiWebStatus" },
}

:PiWeb is the whole install path: it resolves the daemon binary, installs it if needed, spawns it, waits for the socket, and hands the browser a one-time login token. Nothing else to configure.

The plugin looks for the daemon in this order, and :PiWebStatus reports which one won:

# Source Where
1 web.daemon / $PI2_DAEMON_BIN explicit path or command name
2 pi2 on PATH a global install
3 plugin data dir <stdpath("data")>/pi2d/node_modules/.bin/pi2
4 repo checkout node_modules/.bin/pi2 (developing pi2d itself)
5 npx -y @yuez/pi2d@latest last resort when nothing is writable

Remote access (phone, LAN, Tailscale)

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.

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:

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.

Developing pi2d

pnpm install
pnpm --dir web install
pnpm dev            # daemon on 127.0.0.1:7676 (tsx watch)
pnpm --dir web dev  # vite dev server on :5173, proxying /api and /ws to the daemon

pnpm build → dist/, pnpm build:web → web/dist/. The published tarball ships both, so pi2 daemon serves the web console from its own package with no extra step.

Releasing: bump version in package.json, tag v<version>, push the tag. That is the whole flow — .github/workflows/release.yml verifies the tag matches package.json, runs the checks, and publishes to npm. Authentication is OIDC trusted publishing: the publish step holds no npm token, and id-token: write lets the npm CLI exchange a short-lived GitHub OIDC token for publish rights (provenance is attached automatically). There is no NPM_TOKEN secret to configure or rotate, and a guard step fails the run if one ever exists. The one-time bootstrap (first publish by hand, then the npmjs.com trusted-publisher setup), the local dry run and troubleshooting: docs/releasing.md.

Layout

Path Purpose
src/protocol/ wire types, event/command vocabulary, error codes, canonical reducer
src/daemon/ HTTP/WS/SSE, auth, static assets, routing, event relay
src/instances/ discovery, lazy spawn, single-instance enforcement, crash handling
src/workspaces/ workspace registry, disk discovery, aliases/trust
src/cli/ pi2 — the daemon subcommand plus attach/status/doctor
web/ the React web console (built into web/dist, served by the daemon)
fixtures/ golden event streams + expected session state (shared with pi2.nvim tests)
docs/protocol.md the wire contract (browser ↔ daemon ↔ plugin)

Testing

Unit contracts are hermetic (pnpm test → vitest run; pnpm lint typechecks). The web-console gate is end-to-end and real: a daemon, one headless nvim per workspace, a real pi --mode rpc behind it and, for the browser layer, Playwright against the built bundle.

pnpm test                              # unit: daemon + web (vitest)
bash scripts/e2e/run-api.sh            # A0–A12: API/WS against a real backend
bash scripts/e2e/run-ui.sh             # B1–B14, C1–C4: Playwright against web/dist
bash scripts/e2e/run-all.sh            # both, API first

Every suite runs in its own mktemp root with isolated runtime/data/state/agent dirs and pi sessions redirected away from ~/.pi/agent; E2E_SKIP_BUILD=1 reuses an existing build and E2E_KEEP=1 keeps the artifacts (report, logs, screenshots). Scenario ids, isolation details, skip conditions and report paths: docs/e2e.md.