diff --git a/README.md b/README.md index 8e7d39a..b6f99fe 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ pi2 daemon # foreground daemon pi2 stop # stop running daemon (alias: pi2 daemon stop) pi2 open # open browser console signed in (loopback only) 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 status # daemon status, active instances, and health pi2 doctor # verify node/runtime, nvim, pi, curl, sockets, protocol diff --git a/docs/adr/0002-install-and-run.md b/docs/adr/0002-install-and-run.md index 3af7a5b..316acb0 100644 --- a/docs/adr/0002-install-and-run.md +++ b/docs/adr/0002-install-and-run.md @@ -105,34 +105,40 @@ Using `npx @yuez/pi2d@latest` or `npx` auto-execution is explicitly rejected: daemon versions simultaneously, corrupting singleton socket expectations. The plugin's `:PiWebInstall` becomes an explicit convenience command that prompts for -confirmation before installing a pinned version matching the plugin into -`/pi2d`. It never runs `npx`. +confirmation before installing `@yuez/pi2d@latest` (an optional `` argument +pins one) into `/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` → `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` | | **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: -1. **Attach if running**: Check if a daemon is already active (via `daemon.json` / `d.sock`). - If running, connect immediately — **never** spawn a second daemon. -2. **Start user service**: If an installed user service unit is found (`systemctl --user` or - `launchctl`), trigger unit start (`systemctl --user start pi2.service` or `launchctl kickstart`). -3. **Spawn detached**: If no service is installed, spawn a detached `pi2 daemon` process and - notify the user once in the editor. +1. **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 resolvable `pi2` binary just to use one. +2. **Start user service**: If an installed user service unit is found (via + `pi2 service status`), run `pi2 service start`. +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 -When the daemon is managed by a service manager (`systemd` or `launchd`), `:PiWebStop` -**must refuse** to stop the process. If a client terminates a service-managed daemon directly -via SIGTERM, `Restart=on-failure` (systemd) or `KeepAlive` (launchd) will immediately -attempt to restart it or enter an inconsistent state. The plugin must advise the user to -use `systemctl --user stop pi2` or `:PiWebService disable`. +`: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 @@ -144,7 +150,6 @@ winner: 3. `/pi2d/node_modules/.bin/pi2` (only if `:PiWebInstall` created it) 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 - Commands: `:PiWeb` (ensure + open), `:PiWebStatus`, `:PiWebStart`, `:PiWebStop`, `:PiWebPair`, `:PiWebInstall`, `:PiWebService`, `:PiWebLog`. `:checkhealth pi` grows a `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 rewritten to emphasize the CLI-first model and the compiled binary options. - **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 `` 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 diff --git a/docs/deployment.md b/docs/deployment.md index 90fa9de..5c51027 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -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 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 -and the follow-up commands; `--write` writes the file (`0644`) and prints the -commands. The CLI never executes `systemctl`/`launchctl`. +See [§9](#9-services-systemd--launchd). `install-service --dry-run` (default) +prints the file and the follow-up commands; `--write` writes the file (`0644`) +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) ```bash pi2 install-service --dry-run # inspect, nothing written 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 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 or via shell environment. -`uninstall-service --write` removes the file and prints the disable commands. -For a login-less service to keep running on Linux enable linger -(`loginctl enable-linger $USER`, `pi2 doctor` warns when it is off). +`uninstall-service --write` removes the file and prints the disable commands +(`pi2 service disable` runs the `--now` variant when you want the daemon stopped +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 diff --git a/docs/install.md b/docs/install.md index 308f68c..2f4fc3f 100644 --- a/docs/install.md +++ b/docs/install.md @@ -173,7 +173,7 @@ The CLI inspects how it was installed: 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`) @@ -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). -- 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`. ---