docs: document pi2 service and the CLI-owned service control

- 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.
This commit is contained in:
yuez committed 2026-10-29 14:00:48 +08:00
1 parent 4eda2b9420
commit f854ba8d4f
4 files changed
+58 -26

No files matched your search

+2
View File
@@ -121,6 +121,8 @@ pi2 daemon # foreground daemon
pi2 stop # stop running daemon (alias: pi2 daemon stop) pi2 stop # stop running daemon (alias: pi2 daemon stop)
pi2 open # open browser console signed in (loopback only) pi2 open # open browser console signed in (loopback only)
pi2 install-service --write # render and write user service file pi2 install-service --write # render and write user service file
pi2 service status # unit, enabled, active, linger (--json for machines)
pi2 service enable # daemon-reload + enable --now (start|stop|disable too)
pi2 uninstall-service --write # remove user service file pi2 uninstall-service --write # remove user service file
pi2 status # daemon status, active instances, and health pi2 status # daemon status, active instances, and health
pi2 doctor # verify node/runtime, nvim, pi, curl, sockets, protocol pi2 doctor # verify node/runtime, nvim, pi, curl, sockets, protocol
+22 -16
View File
@@ -105,34 +105,40 @@ Using `npx @yuez/pi2d@latest` or `npx` auto-execution is explicitly rejected:
daemon versions simultaneously, corrupting singleton socket expectations. daemon versions simultaneously, corrupting singleton socket expectations.
The plugin's `:PiWebInstall` becomes an explicit convenience command that prompts for The plugin's `:PiWebInstall` becomes an explicit convenience command that prompts for
confirmation before installing a pinned version matching the plugin into confirmation before installing `@yuez/pi2d@latest` (an optional `<version>` argument
`<stdpath("data")>/pi2d`. It never runs `npx`. pins one) into `<stdpath("data")>/pi2d`. It never runs `npx`.
### 2.2 Run: the daemon owns its own lifecycle ### 2.2 Run: the daemon owns its own lifecycle
| Mode | Who starts it | When to use | | Mode | Who starts it | When to use |
|---|---|---| |---|---|---|
| **user service (default)** | `pi2 install-service --write` → `systemctl --user` / `launchd`; `:PiWeb` only *uses* it | daily use, especially phone/another-browser access — the console has to be up when nvim is not | | **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` | | **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` | | **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 #### `web.autostart = "auto"` semantics
The plugin setting `web.autostart = "auto"` follows strict three-step semantics: The plugin setting `web.autostart = "auto"` follows strict three-step semantics:
1. **Attach if running**: Check if a daemon is already active (via `daemon.json` / `d.sock`). 1. **Attach if running**: Check if a daemon is already active (`daemon.json`).
If running, connect immediately — **never** spawn a second daemon. If running, connect immediately — **never** spawn a second daemon, and never
2. **Start user service**: If an installed user service unit is found (`systemctl --user` or require a resolvable `pi2` binary just to use one.
`launchctl`), trigger unit start (`systemctl --user start pi2.service` or `launchctl kickstart`). 2. **Start user service**: If an installed user service unit is found (via
3. **Spawn detached**: If no service is installed, spawn a detached `pi2 daemon` process and `pi2 service status`), run `pi2 service start`.
notify the user once in the editor. 3. **Spawn detached**: If no service is installed, spawn a detached `pi2 daemon`
process and notify the user once in the editor.
#### `:PiWebStop` behavior in service mode #### `:PiWebStop` behavior in service mode
When the daemon is managed by a service manager (`systemd` or `launchd`), `:PiWebStop` `:PiWebStop` **only stops the editor-side bridge** (the WebSocket to the daemon);
**must refuse** to stop the process. If a client terminates a service-managed daemon directly it never signals the daemon process, so a service-managed daemon is not restarted
via SIGTERM, `Restart=on-failure` (systemd) or `KeepAlive` (launchd) will immediately by `Restart=on-failure`/`KeepAlive` and an in-flight browser session is not broken.
attempt to restart it or enter an inconsistent state. The plugin must advise the user to Stopping the daemon is an explicit CLI action: `pi2 service stop`, `pi2 service
use `systemctl --user stop pi2` or `:PiWebService disable`. disable`, or `pi2 stop`.
### 2.3 The plugin is a client ### 2.3 The plugin is a client
@@ -144,7 +150,6 @@ winner:
3. `<stdpath("data")>/pi2d/node_modules/.bin/pi2` (only if `:PiWebInstall` created it) 3. `<stdpath("data")>/pi2d/node_modules/.bin/pi2` (only if `:PiWebInstall` created it)
4. repo checkout `dist/cli/pi2.js` / `node_modules/.bin/pi2` (developing pi2d) 4. repo checkout `dist/cli/pi2.js` / `node_modules/.bin/pi2` (developing pi2d)
5. **nothing** → one-line error with the install command, no `npx` fallback 5. **nothing** → one-line error with the install command, no `npx` fallback
Commands: `:PiWeb` (ensure + open), `:PiWebStatus`, `:PiWebStart`, `:PiWebStop`, Commands: `:PiWeb` (ensure + open), `:PiWebStatus`, `:PiWebStart`, `:PiWebStop`,
`:PiWebPair`, `:PiWebInstall`, `:PiWebService`, `:PiWebLog`. `:checkhealth pi` grows a `:PiWebPair`, `:PiWebInstall`, `:PiWebService`, `:PiWebLog`. `:checkhealth pi` grows a
`web` section: binary source, version, daemon reachability, protocol match, service `web` section: binary source, version, daemon reachability, protocol match, service
@@ -300,7 +305,8 @@ The following items are deferred to future revisions:
- **Documentation rewrite**: `README.md`, `docs/deployment.md`, and `docs/install.md` are - **Documentation rewrite**: `README.md`, `docs/deployment.md`, and `docs/install.md` are
rewritten to emphasize the CLI-first model and the compiled binary options. rewritten to emphasize the CLI-first model and the compiled binary options.
- **Explicit `:PiWebInstall`**: `:PiWebInstall` remains available as an explicit fallback, - **Explicit `:PiWebInstall`**: `:PiWebInstall` remains available as an explicit fallback,
installing a pinned version strictly matching the plugin release after user confirmation. 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 - **Node floor lowered to >= 20**: broader compatibility for npm users without compromising
runtime stability. runtime stability.
- **Standalone binaries eliminate Node dependencies**: users of curl, Homebrew, and AUR - **Standalone binaries eliminate Node dependencies**: users of curl, Homebrew, and AUR
+28 -7
View File
@@ -475,17 +475,36 @@ symbol. A URL too long for version 10 is printed without a QR, with a note.
Machines call `POST /api/v1/pair` directly — that is what `:PiWebPair` does — so the CLI Machines call `POST /api/v1/pair` directly — that is what `:PiWebPair` does — so the CLI
keeps only this human report; there is no `--json`. keeps only this human report; there is no `--json`.
### 8.6 `pi2 install-service` / `pi2 uninstall-service` ### 8.6 `pi2 install-service` / `pi2 service`
See [§9](#9-services-systemd--launchd). `--dry-run` (default) prints the file See [§9](#9-services-systemd--launchd). `install-service --dry-run` (default)
and the follow-up commands; `--write` writes the file (`0644`) and prints the prints the file and the follow-up commands; `--write` writes the file (`0644`)
commands. The CLI never executes `systemctl`/`launchctl`. and prints the commands — it never executes `systemctl`/`launchctl`.
`pi2 service …` is the explicit control surface the editor plugin calls, so the
plugin never talks to a service manager itself:
```bash
pi2 service status [--json] # manager, unit path, installed/enabled/active, linger
pi2 service start # systemctl --user start pi2.service / launchctl kickstart -k
pi2 service stop # systemctl --user stop pi2.service / launchctl kill TERM
pi2 service enable # … enable --now / launchctl bootstrap + enable
pi2 service disable # … disable --now / launchctl bootout
```
`service status` always exits `0` and reports the world as it is (no manager,
no unit and a dead service are reports, not errors). The four mutating actions
exit `1` with the manager's first stderr line when it refuses; they refuse to
run at all when the unit file is missing (run `pi2 install-service --write`
first).
## 9. Services (systemd / launchd) ## 9. Services (systemd / launchd)
```bash ```bash
pi2 install-service --dry-run # inspect, nothing written pi2 install-service --dry-run # inspect, nothing written
pi2 install-service --write # ~/.config/systemd/user/pi2.service pi2 install-service --write # ~/.config/systemd/user/pi2.service
pi2 service enable # daemon-reload + enable --now (one explicit command)
# or, by hand:
systemctl --user daemon-reload systemctl --user daemon-reload
systemctl --user enable --now pi2.service systemctl --user enable --now pi2.service
``` ```
@@ -534,9 +553,11 @@ Note: `launchd` on macOS has no direct equivalent for systemd's `EnvironmentFile
Environment variables on macOS must be set in the plist `EnvironmentVariables` dictionary Environment variables on macOS must be set in the plist `EnvironmentVariables` dictionary
or via shell environment. or via shell environment.
`uninstall-service --write` removes the file and prints the disable commands. `uninstall-service --write` removes the file and prints the disable commands
For a login-less service to keep running on Linux enable linger (`pi2 service disable` runs the `--now` variant when you want the daemon stopped
(`loginctl enable-linger $USER`, `pi2 doctor` warns when it is off). too). For a login-less service to keep running on Linux enable linger
(`loginctl enable-linger $USER`, `pi2 doctor` warns when it is off;
`pi2 service status --json` reports `linger`).
## 10. Logs and health ## 10. Logs and health
+6 -3
View File
@@ -173,7 +173,7 @@ The CLI inspects how it was installed:
systemctl --user stop pi2.service # stop daemon systemctl --user stop pi2.service # stop daemon
``` ```
> **Important**: When running under systemd with `Restart=on-failure`, do **not** use `:PiWebStop` inside Neovim or process kill signals to shut down the daemon. Always use `systemctl --user stop pi2.service` so systemd does not interpret the shutdown as a failure and immediately restart it. > **Important**: When running under systemd with `Restart=on-failure`, stop the daemon with `pi2 service stop` (or `systemctl --user stop pi2.service`), never by signalling the process directly. `:PiWebStop` in Neovim only detaches the editor-side bridge and deliberately leaves the daemon alone.
#### macOS (`launchd`) #### macOS (`launchd`)
@@ -241,9 +241,12 @@ require("pi").setup({
}) })
``` ```
When you run `:PiWeb`, the plugin discovers `pi2` on `$PATH`, spawns a detached `pi2 daemon` process, and monitors its health. When you run `:PiWeb`, the plugin attaches to a running daemon, otherwise starts
the installed user service (`pi2 service start`), otherwise spawns a detached
`pi2 daemon` process — the three steps of `web.autostart = "auto"`.
- Logs are written to `$PI2_STATE_DIR/daemon.log` (`~/.local/state/pi2/daemon.log` on Linux, `~/Library/Logs/pi2/daemon.log` on macOS). - Logs are written to `$PI2_STATE_DIR/daemon.log` (`~/.local/state/pi2/daemon.log` on Linux, `~/Library/Logs/pi2/daemon.log` on macOS).
- Stop the spawned daemon with `:PiWebStop` in Neovim or `pi2 stop` from your terminal. - `:PiWebStop` stops only the editor-side bridge; the daemon keeps running. Stop it with `pi2 service stop` (service mode) or `pi2 stop` (any mode).
- `:PiWebService` reports the unit and runs `pi2 service enable|disable|start|stop`; `:PiWebLog` shows the service manager's log or `daemon.log`.
--- ---