- README/install: the plugin attaches → `pi2 service start` → spawn ladder, `:PiWebStop` detaches only the bridge, `:PiWebService`/`:PiWebLog` exist. - deployment §8.6/§9: the new subcommand, its exit-code contract and the one-command `pi2 service enable` after `install-service --write`. - ADR 0002: service-manager commands belong to the CLI (`pi2 service …`), the plugin's ladder gains $PI2_DAEMON_BIN, `:PiWebStop` never signals a service-managed daemon, and `:PiWebInstall` installs `@yuez/pi2d@latest` (optionally pinned by a <version> argument) after confirmation.
19 KiB
ADR 0002 — Installation and run model: CLI first, service-owned daemon, plugin attaches
- Status: accepted (implementing)
- Date: 2026-10-28
- Supersedes: the implicit "the plugin installs and spawns the daemon" default in
README.md§Install and the resolution table (explicit path →PATH→ plugin data dir → repo checkout →npx @latest)
Context
pi2d is two things at once, and that is why its install story drifted:
- a CLI (
pi2+pi2 daemon) that serves the web console and supervises one headless nvim per workspace, and - a dependency of a Neovim plugin (
:PiWebis the only way most users will ever meet it).
Today the documented path is the second one: the plugin finds no daemon, npm installs @yuez/pi2d into <stdpath("data")>/pi2d, spawns it detached, and mints
a loopback token. It works, but it makes the plugin an installer and a supervisor,
which is the exact shape the rest of the ecosystem avoids. Nine reference
implementations were surveyed (see §1).
1. How the reference implementations do it
| Agent | Install channels | Long-lived process | Self-update | How editors attach |
|---|---|---|---|---|
pi (@earendil-works/pi-coding-agent) |
curl -fsSL https://pi.dev/install.sh | sh → managed install: its own Node under ~/.local/share/pi-node, launcher in ~/.local/bin, managed-install.json marker; npm i -g --ignore-scripts (Node ≥22.19); nix profile add; bun-compiled single binary in releases |
foreground CLI; pi --mode rpc is the long-lived JSONL process a client drives |
pi update [self] (pi update cannot update Nix installs) |
the plugin spawns pi --mode rpc and speaks JSONL; the SDK embeds it in-process |
| dsh (DeepSeek Harness) | npm package under a version manager (mise npm-deepseek-ai-dsh), dsh launcher; profile plugins via dsh plugin … pnpm |
dsh web = foreground HTTP+WS server (3080, --host/--port/--no-open/--public-url/--tls-*); also dsh tui, dsh headless |
package manager | dsh.nvim spawns a node bridge (bridge/bridge.mjs) that dials dsh web; auto_start spawns the server, never installs it |
| Claude Code | native installer curl -fsSL https://claude.ai/install.sh | bash (auto-updates); brew cask; winget; apt/dnf/apk; npm (legacy) |
foreground CLI; IDE extensions spawn it and connect over a WS MCP socket | background auto-update (native); manual for brew/winget | claudecode.nvim runs the WS MCP server inside nvim; the claude binary on PATH is a hard requirement |
| Codex CLI | curl -fsSL https://chatgpt.com/codex/install.sh | sh → standalone binaries (releases.openai.com, GitHub fallback); npm i -g @openai/codex; brew install --cask codex; raw tarballs |
foreground CLI; codex app-server for IDE clients |
package manager | IDE extension |
| opencode | curl -fsSL https://opencode.ai/install | bash; npm/bun/pnpm/yarn; brew tap; pacman/AUR; choco/scoop; mise use -g github:…; Docker; raw releases |
TUI and a server: opencode serve (default 127.0.0.1:4096, OpenAPI 3.1 at /doc, --mdns, --cors, basic auth via OPENCODE_SERVER_PASSWORD) |
package manager | IDE plugins drive a running server (/tui endpoint); the TUI itself is just a client |
| gemini-cli | npx @google/gemini-cli; npm i -g (latest/preview/nightly); brew; macports; conda |
foreground CLI | npm dist-tag | (retired in favour of Antigravity) |
| aider | curl -LsSf https://aider.chat/install.sh | sh (uv-backed, brings Python 3.12); aider-install; uv tool install; pipx; Docker |
foreground CLI | --upgrade / package manager |
— |
| crush | brew tap; npm i -g @charmland/crush; pacman; nix/NUR; apt repo; winget/scoop; FreeBSD pkg |
foreground TUI (single Go binary) | package manager | — |
| mason.nvim (editor-side counter-example) | the editor installs into stdpath("data"), one bin/ added to $PATH, registry-driven |
n/a | :MasonUpdate |
n/a |
Common denominators, all nine agreeing:
- The agent is a first-class CLI installed by a package manager or a
curl | shinstaller — never by the editor plugin. Plugins that do install (mason.nvim) are infrastructure whose entire purpose is installing, with a registry behind them. - Installers that bypass the OS package manager manage their own runtime (pi pins Node, aider pins Python via uv, claude/codex ship native binaries). "Your distro's Node is too old" is never allowed to block the install.
- Self-update is an explicit command or an explicit policy (
pi update,claude's background updater, gemini dist-tags, brew upgrades), because agents move faster than distros. - The long-lived process is an explicit, separately-documented mode (
pi --mode rpc,dsh web,opencode serve,codex app-server) with a stable wire contract, and the editor is a client of it. - Server modes bind loopback by default and take auth seriously (opencode password env, dsh accepts only a concrete interface, our loopback CLI token).
- Nobody silently auto-updates the server behind the editor's back. Version skew is handled by a version field in the handshake or a startup compatibility check.
pi2d's one genuine asymmetry: the daemon is not the agent. It routes to nvim,
which owns sessions. So a daemon alone does nothing (/workspaces would be empty of
live instances), and a plugin without a daemon has no browser console. Neither side
can be "the" entry point — but the install and the process can each have exactly
one owner, and today the plugin wrongly claims both.
2. Decision
2.1 Install: Tier-1 distribution channels and Node floor
The primary distribution shifts from plugin-orchestrated npm installs to first-class CLI distribution channels.
Node engine floor: lowered to >= 20
The package.json engines requirement and pi2 doctor checks are lowered to
node >= 20 (from >= 22). Nothing in src/ requires Node 22-only features (no
node:sqlite, no styleText, no import.meta.dirname, no --experimental-*).
Lowering to Node 20 broadens compatibility for npm users (including enterprise LTS
and Debian/Ubuntu environments). When running as a compiled binary, no Node runtime
is needed at all.
Tier-1 distribution channels (shipped as compiled binaries)
Instead of the installer downloading and managing a separate Node runtime, all three
primary channels ship identical precompiled standalone binaries (bun build --compile):
| Channel | Method | Target audience | Runtime dependency |
|---|---|---|---|
| curl installer | curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh | sh |
Generic Linux & macOS | None (zero external dependencies) |
| Homebrew tap | brew install zgs225/tap/pi2d |
macOS & Linux | None (standalone binary formula) |
Arch AUR (-bin) |
paru -S pi2d-bin / yay -S pi2d-bin |
Arch Linux, EndeavourOS, Manjaro | None (standalone binary package) |
| npm global | npm i -g @yuez/pi2d (or pnpm add -g, bun add -g) |
Node developers | Node >= 20 |
| repo checkout | git clone … && pnpm install && pnpm build |
Contributors, offline development | Node >= 20, pnpm |
The curl installer, Homebrew tap, and Arch AUR package are launched simultaneously (T1).
Because they distribute compiled binaries, the installer no longer needs to pin or install
Node under ~/.local/share/pi2-node.
Rejection of npx as an install or execution path
Using npx @yuez/pi2d@latest or npx auto-execution is explicitly rejected:
- Network overhead on startup:
npxqueries npm registries on first use or tag resolution, introducing multi-second latency to editor startup. - Phantom dependencies: creates ephemeral cache directories or uncontrolled
node_modulesunderstdpath("data"). - Supply chain exposure:
@latestresolution opens unpinned supply chain attack vectors. - Version drift: different editor sessions or background runs can resolve different daemon versions simultaneously, corrupting singleton socket expectations.
The plugin's :PiWebInstall becomes an explicit convenience command that prompts for
confirmation before installing @yuez/pi2d@latest (an optional <version> argument
pins one) into <stdpath("data")>/pi2d. It never runs npx.
2.2 Run: the daemon owns its own lifecycle
| Mode | Who starts it | When to use |
|---|---|---|
| user service (default) | pi2 install-service --write + pi2 service enable; :PiWeb only uses it (pi2 service start) |
daily use, especially phone/another-browser access — the console has to be up when nvim is not |
| plugin spawn | :PiWeb spawns a detached pi2 daemon if nothing runs (web.autostart = "spawn") |
a machine where the user does not want a service; logs to PI2_STATE_DIR/daemon.log |
| foreground | the user, in a terminal | debugging, pi2 daemon --foreground |
Service-manager commands belong to the CLI, not the editor: pi2 service status|start|stop|enable|disable is the only thing that talks to
systemctl/launchd (and only when the user asks for it). install-service
stays pure file rendering.
web.autostart = "auto" semantics
The plugin setting web.autostart = "auto" follows strict three-step semantics:
- Attach if running: Check if a daemon is already active (
daemon.json). If running, connect immediately — never spawn a second daemon, and never require a resolvablepi2binary just to use one. - Start user service: If an installed user service unit is found (via
pi2 service status), runpi2 service start. - Spawn detached: If no service is installed, spawn a detached
pi2 daemonprocess and notify the user once in the editor.
:PiWebStop behavior in service mode
:PiWebStop only stops the editor-side bridge (the WebSocket to the daemon);
it never signals the daemon process, so a service-managed daemon is not restarted
by Restart=on-failure/KeepAlive and an in-flight browser session is not broken.
Stopping the daemon is an explicit CLI action: pi2 service stop, pi2 service disable, or pi2 stop.
2.3 The plugin is a client
pi2.nvim resolution order becomes, in order, with :PiWebStatus reporting the
winner:
web.binary/$PI2_DAEMON_BIN(explicit path or command name)pi2on$PATH(the normal case — the CLI install of §2.1)<stdpath("data")>/pi2d/node_modules/.bin/pi2(only if:PiWebInstallcreated it)- repo checkout
dist/cli/pi2.js/node_modules/.bin/pi2(developing pi2d) - nothing → one-line error with the install command, no
npxfallback Commands::PiWeb(ensure + open),:PiWebStatus,:PiWebStart,:PiWebStop,:PiWebPair,:PiWebInstall,:PiWebService,:PiWebLog.:checkhealth pigrows awebsection: binary source, version, daemon reachability, protocol match, service state, autostart mode.
2.4 Configuration stays environment-only
The daemon keeps PI2_* env as the single config surface (src/config.ts, no file
parser, no precedence rules). Two additions: pi2 config prints the resolved
configuration plus the paths that produced it, and the systemd unit carries
EnvironmentFile=-%h/.config/pi2/env (the leading - instructs systemd to ignore missing
files). A pi2.toml is rejected: ~20 knobs, all deployment-time, all naturally expressed
as environment variables.
2.5 Packaging: standalone binary contract
Artifact contract
Stand-alone binary releases are compiled with Bun (bun build --compile) and packaged
by scripts/build-binary.sh for six cross-compilation targets:
linux-x64linux-arm64linux-x64-musllinux-arm64-musldarwin-x64darwin-arm64
Each release produces a tarball named pi2d-<version>-<target>.tar.gz and a top-level
SHA256SUMS manifest.
Archive layout contract
Every tarball unpacks with the exact layout:
bin/
pi2
share/
pi2d/
web/
dist/
index.html
...
LICENSE
Filesystem prefix rule: <prefix>/bin + <prefix>/share/pi2d/web/dist
This directory structure conforms to standard Unix filesystem hierarchies (FHS). It is
the exact path probed at runtime by src/core/install.ts:
resolve(dirname(executablePath()), "..", "share", "pi2d", "web", "dist")
Every distribution channel preserves this relationship under its installation prefix:
- curl installer: prefix
~/.local/lib/pi2d(with symlink~/.local/bin/pi2) - Homebrew: prefix
#{prefix}(/opt/homebrewor/usr/local) - Arch AUR: prefix
/usr
npm package remains JS-only for Node >= 20
The npm package (@yuez/pi2d) deliberately remains pure JavaScript/TypeScript output
in dist/ and web/dist/, running under Node >= 20. Packaging native platform binaries
into npm optional dependencies (e.g. @yuez/pi2d-linux-x64) is intentionally avoided
for now (Decision 3 '先不修改') to prevent publishing complexity across npm and GitHub
releases before justified by demand.
2.6 Upgrade and restart semantics
- npm upgrade: running
npm i -g @yuez/pi2d@latest(orpnpm/bun) updates the script in place. The running daemon process continues until restarted viasystemctl --user restart pi2orpi2 stop && pi2 daemon. - Binary upgrade (installer / brew / AUR): package managers or
install.shreplace the binary on disk. Under Unix filesystem semantics, an open file descriptor to the running executable remains valid; existing daemon operations continue uninterrupted until the service is restarted. - Systemd
Restart=on-failure: does not fight intentional shutdowns. A cleanSIGTERM(e.g. viapi2 stop) causespi2 daemonto exit with code0. Systemd considers exit 0 a clean stop and does not trigger restart. - Node path staleness: units created for an npm install hardcode the absolute node
path (
process.execPath). Upgrading Node via a version manager (mise,nvm,fnm) invalidates that path.pi2 doctordetects when node cannot run, and re-runningpi2 install-service --writere-renders the unit with the updated path. - Daemon restart & client reconnection:
- Managed Neovim instances: spawned nvim processes do not terminate when the daemon
stops. They dial
d.sockwith exponential backoff (200 ms to 5 s) and reconnect automatically when the daemon restarts. - Web browsers: WebSocket disconnects trigger client-side reconnection. During the
initial handshake (
hello), the daemon presents its newly minteddaemonId. If thedaemonIdhas changed, the browser recognizes a restarted daemon and resynchronizes state.
- Managed Neovim instances: spawned nvim processes do not terminate when the daemon
stops. They dial
2.7 Compatibility and protocol handshake
- Protocol v1 handshake: Every connection over WebSocket or unix socket begins with
a
helloexchange containing:daemonVersion: semantic version of the running daemon (e.g.0.1.5)protocolVersion: integer protocol version (currently1, defined insrc/protocol/types.ts)daemonId: unique UUID minted per daemon process run
- Version skew handling:
- The plugin compares
protocolVersionagainst its supported range. - If the protocol version does not match, the plugin refuses to proceed, prints an actionable error message, and displays the exact upgrade command for the user's platform.
pi2 doctorprobes the live daemon (/api/v1/info) and verifies CLI/daemon protocol parity.
- The plugin compares
2.8 Security hardening
- Service environment file:
EnvironmentFile=-%h/.config/pi2/envuses the leading-to ensure systemd boots cleanly even if the file is missing. Recommended permissions are0600(chmod 0600 ~/.config/pi2/env) to protect tokens and configuration from other users. - No tokens in logs:
- Local admin token (
cli.token) is randomly generated per boot, mode0600. - Device tokens are stored only as SHA-256 hashes in
devices.json. - Browser console login (
POST /api/v1/console/session) passes the session token strictly within the URL fragment (#t=<token>), preventing tokens from leaking into server logs, HTTP headers, orRefererfields.
- Local admin token (
- Pairing rate limits:
POST /api/v1/pair/redeemenforces strict rate limits: 5 attempts/minute/IP and 50 attempts/hour globally. Exceeding limits returns429 Too Many Requests.- Pairing codes expire in 10 minutes and are single-use.
- Service bind warnings:
pi2 install-servicechecks if the daemon is configured to bind to all interfaces (0.0.0.0) without TLS and prints an explicit security warning. - Doctor posture checks:
pi2 doctorverifies file permissions (0700for runtime dir,0600forcli.token), warns when linger is disabled on Linux, and requires explicit acknowledgement when running non-loopback without anhttps:PI2_PUBLIC_URL.
2.9 Deferred features and acceptance criteria
The following items are deferred to future revisions:
pi2 updatein-place self-updater:- Rationale: Package managers (Homebrew, AUR, npm) already provide robust upgrade mechanisms.
- Acceptance criteria: Detects if installed via standalone installer (
~/.local/lib/pi2d); queries GitHub Releases API for newer version; verifies releaseSHA256SUMS; performs atomic replacement ofbin/andshare/; signals running systemd/launchd service to restart; refuses self-update when managed by Homebrew, AUR, or npm with instructions to use the native package manager.
- npm platform-binary packages (
@yuez/pi2d-<os>-<arch>):- Rationale: The npm package works well as pure JS for Node >= 20. Adding platform-specific
npm packages in
optionalDependenciescomplicates CI/CD and release publishing. - Acceptance criteria: CI builds and publishes target-specific packages containing only the
compiled binary; root
@yuez/pi2dpackage delegates to platform binary if present, falling back to Node JS entrypoint.
- Rationale: The npm package works well as pure JS for Node >= 20. Adding platform-specific
npm packages in
- Upstream distribution repositories (
homebrew-core, Alpineaports,winget):- Rationale: A custom tap (
zgs225/tap) and AUR package provide fast release iteration without third-party review bottlenecks. - Acceptance criteria: Stable v1.0 release, official submission to
homebrew-core(requires source-based build), official Alpine community package, and signed Windows MSIX/winget package.
- Rationale: A custom tap (
Consequences
- Documentation order changes: install the CLI → (optionally) enable the service →
install the plugin →
:PiWeb.:PiWebstops being the whole install path. - Migration for existing users: users who relied on the legacy auto-install into
<stdpath("data")>/pi2dmust install thepi2CLI via one of the Tier-1 channels (curl, brew, AUR, or npm).:PiWebstops silently installing or executing vianpx. - Documentation rewrite:
README.md,docs/deployment.md, anddocs/install.mdare rewritten to emphasize the CLI-first model and the compiled binary options. - Explicit
:PiWebInstall::PiWebInstallremains available as an explicit fallback, installing@yuez/pi2d@latest(optionally pinned with a<version>argument) after user confirmation. - Node floor lowered to >= 20: broader compatibility for npm users without compromising runtime stability.
- Standalone binaries eliminate Node dependencies: users of curl, Homebrew, and AUR require no Node installation, eliminating "distro Node is too old" issues entirely.
- Service is optional:
pi2 daemonruns standalone in the foreground, andpi2 doctortreats missing service managers or linger as warnings, never fatal errors.