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`.
This commit is contained in:
yuez committed 2026-10-22 15:47:48 +08:00
1 parent b02f4e9070
commit 7e780515ac
38 files changed
+295 -232

No files matched your search

+73 -38
View File
@@ -1,14 +1,15 @@
# Deployment and operations
`pi2d` is the pi2 web-console daemon. It owns the HTTP/WS surface, the
`pi2 daemon` is the pi2 web-console daemon. It owns the HTTP/WS surface, the
workspace registry, the instance supervisor (one managed nvim per workspace)
and the device/CLI auth. This document covers running it, its runtime files,
the `pi2` CLI and the day-to-day ops commands.
and the device/CLI auth. It is **a subcommand of the single shipped `pi2`
binary**, not a separate executable. This document covers running it, its
runtime files, the `pi2` CLI and the day-to-day ops commands.
```
browser ──HTTP + WS/SSE──▶ pi2d ──JSONL over unix socket──▶ plugin (nvim)
▲
pi2 CLI │ (HTTP, local admin token)
browser ──HTTP + WS/SSE──▶ pi2 daemon ──JSONL over unix socket──▶ plugin (nvim)
▲
pi2 CLI │ (HTTP, local admin token)
```
Contents
@@ -21,6 +22,7 @@ Contents
6. [Ports and the singleton lock](#6-ports-and-the-singleton-lock)
7. [Authentication](#7-authentication)
8. [The `pi2` CLI](#8-the-pi2-cli)
- [`pi2 daemon [start]`](#80-pi2-daemon-start)
9. [Services (systemd / launchd)](#9-services-systemd--launchd)
10. [Logs and health](#10-logs-and-health)
11. [Restart and shutdown semantics](#11-restart-and-shutdown-semantics)
@@ -44,41 +46,55 @@ Contents
```bash
pnpm install
pnpm build # tsc -> dist/ (dist/cli/pi2d.js, dist/cli/pi2.js)
pnpm build # tsc -> dist/ (dist/cli/pi2.js)
pnpm --dir web build # optional: web/dist for the browser console
```
`pi2d` serves the browser console from `<repo>/web/dist` (or `PI2_STATIC_DIR`).
The daemon serves the browser console from `<repo>/web/dist` (or `PI2_STATIC_DIR`).
Without it `/` returns 404/SPA fallback behaviour and `/api/v1/health` reports
`static=FAIL`; the API and `pi2` keep working.
Binaries: `dist/cli/pi2d.js` (daemon, `pi2d`) and `dist/cli/pi2.js` (client,
`pi2`). Both are ESM and start with a `#!` line.
There is exactly one built binary, `dist/cli/pi2.js` (client commands **and** the
`daemon` subcommand). It is ESM and starts with a `#!` line.
## 3. Running the daemon
The daemon is a subcommand of the single `pi2` binary (there is no separate
executable to install or ship):
```bash
node dist/cli/pi2d.js --foreground # or: pnpm dev (tsx watch)
node dist/cli/pi2.js daemon --foreground # or: pnpm dev (tsx watch)
```
`pi2 daemon`, `pi2 daemon start` (alias) and `pi2 daemon --foreground` all start
the same foreground process — `main` is dispatched from `src/cli/pi2.ts` to
`src/cli/daemon.ts` and the `start` positional is dropped before parsing. There
is no detaching mode; that is what the systemd/launchd units below rely on.
`pi2 daemon --help` prints the usage block (`Usage: pi2 daemon [start] [options]`),
`pi2 daemon --version` prints `pi2 <version>`, and an unknown daemon positional
exits `2` without starting anything.
Exactly one line goes to **stdout**:
```
pi2d listening on http://127.0.0.1:7676
pi2 daemon listening on http://127.0.0.1:7676
```
Every log record is JSONL on **stderr** (that is what systemd/launchd capture).
Every daemon error is one line on stderr prefixed `pi2: `.
Flags:
Flags (`pi2 daemon [start] …`):
| Flag | Meaning |
|---|---|
| `--port <n>` | TCP port; `0` = ephemeral (the real port lands in `daemon.json`) |
| `--port <n>`, `-p <n>` | TCP port; `0` = ephemeral (the real port lands in `daemon.json`) |
| `--host <addr>` | bind address (default `127.0.0.1`) |
| `--static-dir <p>` | built web UI directory |
| `--force` | take the lock even if another `pi2d` holds it |
| `--force`, `-f` | take the lock even if another `pi2 daemon` holds it |
| `--foreground` | stay attached to the terminal (default; used by unit files) |
| `--version`, `--help` | print and exit |
| `--version`, `-V` | print `pi2 <version>` and exit |
| `--help`, `-h` | print the usage block and exit |
Flags are applied to the environment before the config is loaded, so
`--port 8000` is equivalent to `PI2_PORT=8000`.
@@ -131,8 +147,8 @@ overwritten on the next start.
## 6. Ports and the singleton lock
- One daemon per runtime dir. A second `pi2d` exits `1` with
`pi2d: another pi2d is running (pid …, port …); use --force to override`.
- One daemon per runtime dir. A second `pi2 daemon` exits `1` with
`pi2: another pi2 daemon is running (pid …, port …); use --force to override`.
- `--force` takes over the lock; the displaced daemon keeps serving until it
exits, and it will *not* delete the new lock on shutdown (ownership is
checked by pid + `daemonId`).
@@ -150,8 +166,8 @@ Two token kinds:
1. **Device tokens** (paired phones/laptops): created by `POST /api/v1/pair`
(redeem) or `pair.create`; only SHA-256 hashes are stored in
`PI2_DATA_DIR/devices.json`.
2. **Local CLI token**: `PI2_RUNTIME_DIR/cli.token`, minted by `pi2d` on each
start. Any request carrying it authenticates as the synthetic device
2. **Local CLI token**: `PI2_RUNTIME_DIR/cli.token`, minted by `pi2 daemon` on
each start. Any request carrying it authenticates as the synthetic device
`{id:"local-cli", name:"local CLI", scopes:["read","steer","control","admin"]}`
(constant-time compare, never persisted, not revocable, not listed by
`GET /api/v1/devices`). Rotating the file invalidates the old value
@@ -168,6 +184,22 @@ authenticated (auth runs before routing).
`PI2_RUNTIME_DIR`, so it needs no flags. Every command exits non-zero with a
one-line `pi2: …` message on failure.
### 8.0 `pi2 daemon [start]`
```bash
pi2 daemon # foreground; the plugin/browser needs this running
pi2 daemon start # same thing (explicit subcommand alias)
pi2 daemon --foreground # same thing (the flag is the default)
pi2 daemon -p 0 # ephemeral port
pi2 daemon --help # Usage: pi2 daemon [start] [options]
```
All three forms start the identical foreground process; the `start` positional is
dropped before the flag parser runs, and an unknown daemon positional exits `2`.
The daemon is dispatched *inside* `pi2` (`src/cli/pi2.ts` → `src/cli/daemon.ts`):
it is not a separate executable, nothing is re-exec'd, and there is no detaching
mode. Flags, stdout contract and startup order: [§3](#3-running-the-daemon).
### 8.1 `pi2 attach [path]`
```bash
@@ -242,21 +274,21 @@ commands. The CLI never executes `systemctl`/`launchctl`.
```bash
pi2 install-service --dry-run # inspect, nothing written
pi2 install-service --write # ~/.config/systemd/user/pi2d.service
pi2 install-service --write # ~/.config/systemd/user/pi2.service
systemctl --user daemon-reload
systemctl --user enable --now pi2d.service
systemctl --user enable --now pi2.service
```
The unit is rendered with absolute paths:
```ini
[Unit]
Description=pi2 daemon (pi2d)
Description=pi2 daemon
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/node /repo/dist/cli/pi2d.js --foreground
ExecStart=/usr/bin/node /repo/dist/cli/pi2.js daemon --foreground
Environment=PATH=/usr/local/bin:/usr/bin:/bin
Restart=on-failure
RestartSec=2
@@ -268,13 +300,14 @@ WantedBy=default.target
```
`XDG_CONFIG_HOME` is honoured for the target directory. On macOS the same
command writes `~/Library/LaunchAgents/com.pi2.pi2d.plist` (`RunAtLoad`,
`KeepAlive`, absolute node + script paths, `StandardOutPath`/`StandardErrorPath`
under `PI2_STATE_DIR`) and prints:
command writes `~/Library/LaunchAgents/com.pi2.daemon.plist` (`RunAtLoad`,
`KeepAlive`, absolute node + script paths, `ProgramArguments`
`node /repo/dist/cli/pi2.js daemon --foreground`, `StandardOutPath`/
`StandardErrorPath` under `PI2_STATE_DIR`) and prints:
```bash
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.pi2.pi2d.plist
launchctl enable gui/$(id -u)/com.pi2.pi2d
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.pi2.daemon.plist
launchctl enable gui/$(id -u)/com.pi2.daemon
```
`uninstall-service --write` removes the file and prints the disable commands.
@@ -283,9 +316,10 @@ For a login-less service to keep running on Linux enable linger
## 10. Logs and health
- `pi2d` logs JSONL to stderr: `systemctl --user status pi2d` /
`journalctl --user -u pi2d -f` on Linux, `~/Library/Logs/pi2/pi2d.log` on
macOS. Raise verbosity with `PI2_LOG_LEVEL=debug` (in the unit's
- `pi2 daemon` logs JSONL to stderr: `systemctl --user status pi2` /
`journalctl --user -u pi2 -f` on Linux (`pi2.service`), and
`~/Library/Logs/pi2/pi2-daemon.log` (`pi2-daemon.err.log` for stderr) on macOS.
Raise verbosity with `PI2_LOG_LEVEL=debug` (in the unit's
`Environment=` line) .
- Managed nvim output: `PI2_STATE_DIR/instances/<instanceId>.log`.
- `GET /api/v1/health` → `{ok, items:[{name, ok, detail}]}` (`instances`,
@@ -301,7 +335,7 @@ For a login-less service to keep running on Linux enable linger
## 11. Restart and shutdown semantics
- **SIGINT/SIGTERM/SIGHUP** (`pi2d`): close HTTP + WS, close the plugin socket,
- **SIGINT/SIGTERM/SIGHUP** (`pi2 daemon`): close HTTP + WS, close the plugin socket,
release `daemon.lock`/`daemon.json`, exit `0`. **Managed nvim processes are
not stopped** — they keep running and reconnect (exponential backoff,
200 ms → 5 s) the next time the daemon starts.
@@ -319,22 +353,23 @@ For a login-less service to keep running on Linux enable linger
bash scripts/smoke.sh
```
Builds, starts `pi2d` on an ephemeral port with throwaway
Builds, starts `pi2 daemon` on an ephemeral port with throwaway
`PI2_RUNTIME_DIR`/`PI2_DATA_DIR`/`PI2_STATE_DIR`, waits for `daemon.json`, then
asserts: `/api/v1/info` (200, `v=1`), `/api/v1/health` (200 ok), `/` (200 html
from `web/dist`), `/api/v1/nope` (404 with token, 401 without),
`/api/v1/workspaces` (200 array with the CLI token, 401 without/with a bogus
token), the `cli.token` format/mode, the lock payload, and a clean `SIGTERM`
exit with lock + `daemon.json` removed. Prints one `PASS` line per assertion.
`PI2_SMOKE_KEEP=1` keeps the temp dir (and `pi2d` log) for debugging.
`PI2_SMOKE_KEEP=1` keeps the temp dir (and the `pi2-daemon.log` daemon log) for
debugging.
## 13. Troubleshooting
| Symptom | Cause / fix |
|---|---|
| `pi2d: another pi2d is running (pid …)` | a live daemon owns this runtime dir. Stop it, or `--force` |
| `pi2: no running pi2d (…/daemon.json missing)` | daemon not started, or started with a different `PI2_RUNTIME_DIR` |
| `pi2: missing CLI token (…/cli.token)` | token deleted or daemon restarted; restart `pi2d` |
| `pi2: another pi2 daemon is running (pid …)` | a live daemon owns this runtime dir. Stop it, or `--force` |
| `pi2: no running pi2 daemon (…/daemon.json missing)` | daemon not started, or started with a different `PI2_RUNTIME_DIR` |
| `pi2: missing CLI token (…/cli.token)` | token deleted or daemon restarted; restart `pi2 daemon` |
| `401 E_UNAUTHORIZED` from `curl` | send `Authorization: Bearer $(cat $PI2_RUNTIME_DIR/cli.token)` |
| `EADDRINUSE` on an explicit port | pick another `--port` (only the default port retries) |
| `403` with the browser console | origin not in `PI2_ORIGINS` (derived from the port) |
+4 -4
View File
@@ -12,7 +12,7 @@
| node | v22.23.1 |
| nvim | 0.12.6 |
| pi | 1.3.0 |
| daemon | `pi2d` @ `31f3630` + working-tree fixes |
| daemon | `pi2 daemon` @ `31f3630` + working-tree fixes |
| plugin | `pi2.nvim` `feat/web-console` @ `ffec47c` + working-tree fixes |
| browser | Chrome (Playwright 1.63, headless) |
| model | `cliproxyapi` (real turns; `deepseek-v4-pro` in the browser run) |
@@ -73,7 +73,7 @@ C3 dark/light screenshots · C4 zero unexpected console errors/pageerrors.
is implemented; `subagent.dispatch` accepts the canonical `items[]` shape;
`attention.respond` accepts the structured response table.
### Daemon (`pi2d`)
### Daemon (`pi2 daemon`)
- Workspaces outside `PI2_WORKSPACE_ROOTS` returned `500 E_INTERNAL`; now
`403 E_FORBIDDEN`.
@@ -86,7 +86,7 @@ C3 dark/light screenshots · C4 zero unexpected console errors/pageerrors.
- `/api/v1/info` gained additive `hostname`, `os`, `version`, `uptimeMs`, `pid`,
`port`; `/health` reflects instance/workspace/auth state.
### Web (`pi2d/web`)
### Web (`web/`)
- Pairing posted `pair.redeem` to the bearer-gated `/api/v1/cmd` → every pairing
401. The manual/scan flows now redeem the public `POST /api/v1/pair`
@@ -124,7 +124,7 @@ C3 dark/light screenshots · C4 zero unexpected console errors/pageerrors.
## Reproduction
```bash
cd /home/yuez/Workspace/Neovim/pi2d
cd <repo> # the pi2 CLI/daemon checkout (this repository)
bash scripts/e2e/run-all.sh # builds daemon + web, runs API then browser
# individual suites
+8 -8
View File
@@ -9,8 +9,8 @@ processes that make the console real, and describes the harness that implements
browser (React in web/dist)
│ HTTP + WS
▼
pi2d (src/) ── JSONL unix socket ──▶ headless nvim + pi2.nvim web layer
└── pi --mode rpc (real model)
pi2 daemon (src/) ── JSONL unix socket ──▶ headless nvim + pi2.nvim web layer
└── pi --mode rpc (real model)
```
Authority, highest first:
@@ -83,7 +83,7 @@ markers, so the diff review does not depend on the model choosing to call the to
Rules:
- Before anything starts, `e2e_init` requires `node`, `nvim`, `pi`, `jq` and `curl`, a plugin
worktree with `lua/pi/web/commands.lua`, a built `dist/cli/pi2d.js`, and `host-init.lua`.
worktree with `lua/pi/web/commands.lua`, a built `dist/cli/pi2.js`, and `host-init.lua`.
Missing pieces exit 2 with the artifacts path.
- Never touch a daemon already running in the default runtime dir. `e2e_stop` terminates the
daemon it started, then reaps every process whose command line contains `$E2E_ROOT`
@@ -334,8 +334,8 @@ For every failed assertion:
→ fix the owning layer.
3. Fix in one layer at a time, with exclusive file ownership per subagent; never let two
agents edit the same file concurrently.
4. Re-run the failed scenario, then the full suite. Unit tests (`vitest` in `pi2d` and
`pi2d/web`) must stay green after every fix.
4. Re-run the failed scenario, then the full suite. Unit tests (`vitest` in the daemon repo and
in `web/`) must stay green after every fix.
5. Do not mark a scenario PASS from a static grep or a screenshot alone; assert the
observable behavior described above.
@@ -355,7 +355,7 @@ the Playwright module/Chrome actually used, and one section per scenario with ev
assertion. `ui-e2e.mjs` also copies its markdown to `/tmp/pi2-e2e-recon/e2e-ui-run.md`.
Logs live in `$E2E_ROOT/logs/`: `<suite>.log` (build output + driver stderr) and
`pi2d.log` / `pi2d.stdout` for the daemon.
`daemon.log` / `daemon.stdout` for the `pi2 daemon` process.
The summary must state which scenarios passed, which failed with observed vs expected,
which were skipped and why (non-blocking only), and the command that reproduces the run.
@@ -363,9 +363,9 @@ which were skipped and why (non-blocking only), and the command that reproduces
## 7. Commands
```bash
cd /home/yuez/Workspace/Neovim/pi2d
cd <repo> # the pi2 CLI/daemon checkout (this repository)
# backend only (A0–A12): tsc, then daemon + headless nvim + real pi
# backend only (A0–A12): tsc, then `pi2 daemon` + headless nvim + real pi
bash scripts/e2e/run-api.sh
# browser (B1–B14, C1–C4): also builds web/dist (pnpm), then Playwright
+2 -2
View File
@@ -3,10 +3,10 @@
Single vocabulary, three surfaces. **The plugin inside nvim is the only authority for session state.**
```
browser <──HTTP + WS/SSE──> pi2d <──JSONL over unix socket──> plugin (nvim)
browser <──HTTP + WS/SSE──> pi2 daemon <──JSONL over unix socket──> plugin (nvim)
```
- `pi2d` never spawns `pi`, never parses session JSONL, never stores session state.
- `pi2 daemon` never spawns `pi`, never parses session JSONL, never stores session state.
- The browser keeps a **render cache** only; a `snapshot` replaces it wholesale.
- One workspace maps to exactly one managed nvim instance.