Commit Graph
13 Commits
Author SHA1 Message Date
yuez 8fb535f7f3 feat(cli): add pi2 update with a versioned install layout
The standalone (curl installer) channel had no upgrade path of its own: the
documented one was 're-run the installer', which the Neovim plugin cannot do
safely and which re-downloads 35 MB without version arithmetic, provenance or
rollback. ADR 0003 records the design; this is the implementation.

- install.sh now installs one immutable tree per version
  (`<prefix>/versions/<v>/{bin,share,LICENSE}`) behind a single atomically
  swapped `current` symlink, writes `<prefix>/install.json` as the install
  receipt, and migrates a pre-0.1.7 flat layout in place. Keeping `current` plus
  one previous tree gives a one-step rollback for free.
- `pi2 update` (native TypeScript, no `curl | sh` subprocess) resolves the
  release with an ETag-cached GitHub lookup, picks the asset from the
  build-time `PI2_BUILD_TARGET` define instead of re-probing libc, verifies
  SHA256SUMS, verifies SLSA provenance with `gh attestation verify`, runs the
  new binary once, then swaps `current` and rewrites the receipt. Flags:
  --check/--dry-run/--version/--force/--json/--require-attestation/
  --skip-attestation. Downgrades need --force; restarts stay manual.
- the receipt is the only channel detection: npm, Homebrew and the AUR are
  refused with the command that owns their tree, and a flat install is told to
  re-run the installer once. No heuristics on `process.execPath`.
- `pi2 install-service` renders `<prefix>/current/bin/pi2`, so an update no
  longer invalidates the unit.
- `scripts/build-binary.sh` injects `PI2_BUILD_TARGET`; `tarball.ts` is a
  dependency-free ustar/PAX reader with traversal and symlink rejection.

Verified against the real release: install.sh into a scratch prefix, update
--check/--dry-run, a local 0.1.7 mirror upgrade (attestation warn path), then
`pi2 update --version 0.1.6 --force` (real download, real attestation
verified), plus boot-check and the refusal paths. typecheck + 567 daemon tests
+ web typecheck + 537 web tests + smoke are green.
2026-10-29 16:42:57 +08:00
yuez 8a39e65def feat(cli): add --json to pi2 pairing
The Neovim plugin needs the mint payload (URL, code, expiry, QR matrix, every
advertised base) to draw a scannable code, but the CLI only printed a human
report — so `:PiWebPair` spoke HTTP itself, duplicating the CLI token handling
and requiring `curl` in the editor.

`pi2 pairing --json` prints the normalised payload instead:

  { url, code, expiresAt?, qr: { version, ecc, mask, size, rows }, bases[] }

Same fields the human report renders, so the terminal QR and the float always
agree, and `--base` still pins the link.
2026-10-29 14:11:10 +08:00
yuez 4eda2b9420 feat(cli): add pi2 service status|start|stop|enable|disable
`install-service` only renders the unit file; nothing in the CLI talked to
systemd/launchd. The editor plugin had no supported way to start a
service-managed daemon (it would have had to shell out to `systemctl` itself),
so add the explicit control surface ADR 0002 §2.2 assumes:

  pi2 service status [--json]   manager, unit path, installed/enabled/active, linger
  pi2 service start|stop        systemctl --user start|stop / launchctl kickstart|kill
  pi2 service enable|disable    systemctl --user enable|disable --now / launchctl bootstrap|bootout

`status` never fails: no manager, no unit and a dead service are reports, and
`--json` carries the same shape the plugin decodes. The four mutating actions
exit 1 with the manager's first stderr line, and refuse to run when the unit
file is missing. Every probe is injectable (exec/exists/platform/paths/uid/user)
so the tests never touch a real service manager.
2026-10-29 14:00:41 +08:00
yuez b95ff6055b feat(cli): tell users when pi2.nvim cannot host a spawned instance
`checkPlugin` only looked for `lua/pi/web/bridge.lua`, so a checkout that can
drive an existing nvim but cannot be spawned reported `ok` and the failure
surfaced later as an unexplained "did not say hello".

The probe now also requires `lua/pi/host.lua`: both present is `ok`, bridge only
is `warn` with the update hint, neither is `fail`. `pi2 daemon --help` lists the
instance knobs (`PI2_NVIM_BIN`, `PI2_NVIM_INIT`, `PI2_PLUGIN_DIR`,
`PI2_ENSURE_TIMEOUT_MS`) it previously kept to itself.
2026-10-28 17:32:41 +08:00
yuez d2e20d428a feat: session-first workspace activity and installable daemon
Workspaces page:
- list the newest sessions across workspaces in the activity column; the
  daemon exposes GET /api/v1/sessions/recent as a disk projection and the
  browser enriches live rows from the per-workspace session relay
- put recent activity first below 1100px and fix the mobile page-header
  flex-basis gap

Smoke:
- pin PI2_HOST=127.0.0.1 in both smoke scripts and make the startup-line
  assertion follow the configured host; the daemon default stays 0.0.0.0

Install:
- standalone binary build/install scripts, packaging templates, CI/release
  hooks, CLI-first install docs and the supporting config/doctor changes
2026-10-28 10:53:48 +08:00
yuez 48411dccd2 feat(cli): add stop command to shut down running daemon 2026-10-27 18:37:21 +08:00
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
yuez 0193230cd3 feat(cli): flag a missing curl in pi2 doctor
`:PiWeb` signs the local browser in through curl (Neovim has no HTTP client), so
a missing binary breaks the primary onboarding path. The new `curl` check sits
with the other binary probes, warns instead of failing, and can never flip
`doctor`'s exit code.
2026-10-23 10:08:15 +08:00
yuez 9ab0830223 feat(cli): add pi2 open, pi2 pairing --public-url and the access check
`pi2 open [--print] [--no-browser]` mints a loopback browser session and hands
the URL to the platform opener without waiting for it, so "open the console" is
one command that never hangs. `pi2 pairing --public-url` (or `daemon.json.publicUrl`
/ `PI2_PUBLIC_URL`) rebuilds the printed URL and QR payload for a tailnet or
reverse-proxy base.

`pi2 doctor` gains an `access` check: ok on loopback, warn for `lan`/`tailscale`,
fail when a non-loopback bind has neither a public URL nor TLS.
2026-10-23 08:34:48 +08:00
yuez 01db49fb38 feat(daemon): add access modes and the local-browser login handshake
`PI2_ACCESS_MODE` (local|lan|tailscale) picks the bind address and the derived
CORS origins, `PI2_PUBLIC_URL` adds the external origin, and `PI2_ORIGINS` is now
additive instead of replacing the derived list. `daemon.json` and
`/api/v1/info` carry the mode and the public URL.

`POST /api/v1/console/session` is the `:PiWeb` handshake: loopback + the local
CLI token only, it mints or reuses the single "local browser" device and answers
`http://127.0.0.1:<port>/#t=<token>`. The token is stored plaintext next to
`devices.json` (0600) so repeated logins return the same credentials instead of
logging the already-open tab out; revoking the device drops it.
2026-10-23 08:34:39 +08:00
yuez 7e780515ac feat(cli)!: merge the daemon into pi2 as the daemon subcommand
`pi2 daemon [start] [flags]` now starts the web-console daemon. The
`pi2d` executable, its bin entry and its entry module are removed: the
daemon keeps its lock -> token -> plugin socket -> listen -> daemon.json
lifecycle, its single stdout line (`pi2 daemon listening on ...`) and the
signal handling that leaves managed nvim instances running.

Service files are renamed to `pi2.service` / `com.pi2.daemon`, and the
smoke and e2e harnesses start the daemon through `pi2 daemon`.

BREAKING CHANGE: the `pi2d` command no longer exists; use `pi2 daemon`
(or `pi2 daemon start`). Existing systemd/launchd units installed under
the old names must be reinstalled with `pi2 install-service --write`.
2026-10-22 15:47:48 +08:00
yuez 5572ac0d84 fix(daemon): forward snapshot entries and map workspace errors
- Preserve the snapshot entries tail in the cache and WS frames (cold,
  cached and bus paths), so the browser receives {state, entries}.
- Await a plugin snapshot on the first subscribe of a fresh session.
- Return E_FORBIDDEN (403) for workspace paths outside PI2_WORKSPACE_ROOTS.
- Mint cli.token only after the singleton lock is held, so a second daemon
  cannot invalidate the running one.
- Add hostname/os/version/uptimeMs/pid/port to /api/v1/info and make
  /health reflect instance, workspace and auth state.
2026-10-21 20:42:02 +08:00
yuez 18e3e0ab4e feat(cli): runnable daemon, local CLI, doctor, service files and smoke test
- composition root wiring config/auth/workspaces/instances/http
- singleton lock + daemon.json + port retry + local cli.token (admin scope)
- pi2 attach/status/doctor/pairing/install-service
- systemd user unit + launchd plist rendering (never auto-enabled)
- smoke.sh boots the real daemon on an ephemeral port and asserts auth/static/lifecycle
- 93 new tests (263 total), tsc + build + smoke green
2026-10-21 12:45:11 +08:00