docs: opening a project from the console needs no :PiWebStart

`workspace.add`/`instance.ensure` now spawn a host-capable headless nvim on
demand, so the documented flow is the browser, not the editor.

protocol.md gains the missing normative bodies for the three daemon-owned writes
(including `~` expansion, `name`-as-`alias`, and the ordered error codes), states
that `hello` requires `lua/pi/host.lua`, and records that any relayed read
implicitly ensures. deployment.md documents `PI2_NVIM_INIT`, `PI2_PLUGIN_DIR`,
`PI2_NVIM_BIN` and `PI2_ENSURE_TIMEOUT_MS`, where spawn output goes, and a
symptom-ordered "opening a project fails" section. README says it out loud.
This commit is contained in:
yuez committed 2026-10-28 17:32:53 +08:00
1 parent d5d205a7eb
commit e775b09037
3 files changed
+69 -4

No files matched your search

+25 -4
View File
@@ -174,13 +174,20 @@ Everything is environment-driven (`src/config.ts` + `src/core/paths.ts`):
| `PI2_WORKSPACE_HIDE_GHOSTS` | `1` | hide workspaces whose `cwd` no longer exists (session files are kept) |
| `PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS` | `0` | hide workspaces outside `PI2_WORKSPACE_ROOTS` (opt-in: hides real repos outside `$HOME`) |
| `PI2_WORKSPACE_STALE_DAYS` | `0` (= off) | hide workspaces idle for more than `n` days (opt-in: hides real repos) |
| `PI2_PLUGIN_DIR` | `~/.local/share/nvim/lazy/pi2.nvim` | plugin location for `pi2 doctor` |
| `PI2_NVIM_BIN` | `nvim` | binary used for managed instances |
| `PI2_NVIM_INIT` | – | extra `-u <init>` for managed instances |
| `PI2_PLUGIN_DIR` | `~/.local/share/nvim/lazy/pi2.nvim` | pi2.nvim checkout: `pi2 doctor` probes it and spawned instances prepend it to `runtimepath`; a leading `~/` is expanded |
| `PI2_NVIM_BIN` | `nvim` | nvim binary for managed instances, resolved on the **daemon's** `PATH` (read once, at daemon start) |
| `PI2_NVIM_INIT` | – (daemon-generated init) | unset ⇒ a generated minimal init that prepends `PI2_PLUGIN_DIR`/the discovered plugin; `user` ⇒ load the user's own nvim config (no `-u`); any other value ⇒ that file via `-u` |
| `PI2_ENSURE_TIMEOUT_MS` | `10000` | how long `instance.ensure` waits for a spawned nvim to say `hello`; expiry answers `E_INSTANCE_STARTING` |
| `PI2_CODING_AGENT_DIR`, falling back to `PI_CODING_AGENT_DIR` | `~/.pi/agent` | pi session storage (discovery) |
There is no config file: the environment *is* the configuration.
A spawned instance inherits the **daemon's** environment, not your shell's, so `PATH` is the
service's. Each spawn writes the child's stdout/stderr to
`PI2_STATE_DIR/instances/<instanceId>.log` (`0600`, appended per start); the last lines of that
file are folded into `instance_state.reason`, which is what the console and `pi2 status` show when
a workspace fails to open.
### Service environment file (`~/.config/pi2/env`)
For background services, the systemd unit automatically includes:
@@ -396,7 +403,7 @@ object `{daemon, instances, resources, health}`.
| `nvim` | missing, or < 0.9 | version unparsable |
| `pi` | – | not on `PATH` |
| `curl` | – | missing → `not found on PATH (:PiWeb cannot sign the browser in automatically; pair instead)`; present but unparsable → `could not parse version of <path>` |
| `plugin` | `lua/pi/web/bridge.lua` not found | – |
| `plugin` | `lua/pi/web/bridge.lua` not found | bridge present but `lua/pi/host.lua` missing — the daemon cannot spawn a workspace from the console |
| `runtime-dir` | missing | mode not `0700` |
| `cli-token` | mode not `0600` | missing (daemon not started yet) |
| `socket` | `d.sock` missing (and no daemon) | missing but daemon answers |
@@ -599,3 +606,17 @@ debugging.
| `pi2 doctor` → `linger warn` | services stop at logout; `loginctl enable-linger $USER` |
| unit points at a node path that no longer exists | mise/nvm/fnm or system Node upgrade changed the binary path; re-run `pi2 install-service --write && systemctl --user daemon-reload` |
| Instances gone after a wifi/laptop sleep | daemon restarted: nvim reconnects automatically; check `pi2 status` |
### Opening a project fails
Opening a workspace from the web console spawns a headless nvim on demand, so a broken spawn is
what a failed “open” looks like. Work the symptoms in this order:
1. **`pi2 doctor` → `plugin warn`/`plugin fail`** (or a spawn error naming the plugin): install or
update pi2.nvim. The daemon needs `lua/pi/host.lua`; the bridge alone is not enough.
2. **`E_INSTANCE_UNAVAILABLE … failed to spawn nvim`**: the binary is not on the daemon's `PATH`
(a service PATH is not your shell's). Point `PI2_NVIM_BIN` at an absolute nvim path and
restart the daemon.
3. **`… did not say hello within 10000ms`** (`E_INSTANCE_STARTING`): nvim started but the plugin
never loaded. Read `$PI2_STATE_DIR/instances/<instanceId>.log` — `instance_state.reason`
quotes its tail.
+37
View File
@@ -69,6 +69,10 @@ The plugin dials the daemon (`<runtime>/d.sock`), sends `hello` first.
Plugin obligations:
- **`hello` requires `lua/pi/host.lua`.** A daemon-spawned instance is started as
`nvim --headless … -u <init> -c 'lua require("pi.host").start()'`, so the host module is what
answers on a spawned workspace. Without it the daemon cannot start a workspace from the web
console (`pi2 doctor` reports `warn` when `bridgeFound` is true and `hostFound` is false).
- **Subscribe before prompt**: the daemon calls `sub.subscribe` before forwarding a command; the plugin
answers every new subscription with a `snapshot` (state + a tail of entries).
- Throttle deltas: `text_delta`/`thinking_delta` coalesced to ≤30 fps (50 ms), `tool_execution_update` to 100 ms.
@@ -231,6 +235,34 @@ Routing:
Idempotency: `clientRequestId` is deduplicated for 5 minutes; replays return the original `ack`.
The daemon-owned write commands have fixed bodies:
| Command | Params | Response |
|---|---|---|
| `workspace.add` | `{cwd, alias?, name?, trust?}` | `{accepted, workspaceId, workspace}` |
| `instance.ensure` | `{workspaceId}` | `{accepted, instance}` (`InstanceState`) |
| `instance.shutdown` | `{workspaceId, mode?}` | `{accepted, workspaceId, mode}` |
`workspace.add` registers `cwd`, clears `hidden` and answers the same `WorkspaceInfo` row the list
serves; `workspaceId` is `sha256(realpath(cwd)).slice(0,16)`. `name` is an alias for `alias` — when
both are present `alias` wins, and a whitespace-only value of either is ignored, not stored. `cwd`
must be an absolute path or begin with `~`/`~/`: the daemon expands exactly those two forms against
its own home and never expands `~user/…`. Validation is ordered and each failure maps to one code:
non-empty string, no NUL byte, absolute-or-`~` → `E_BAD_REQUEST`; `realpath` → `E_NOT_FOUND`;
inside `PI2_WORKSPACE_ROOTS` → `E_FORBIDDEN`; a directory → `E_BAD_REQUEST`; readable, writable and
executable → `E_FORBIDDEN`. The allowlist is applied to the canonical path, so a symlink inside the
roots cannot escape them.
`instance.ensure` is the only way the daemon starts an nvim: it **spawns on demand** and is
idempotent per `workspaceId` (concurrent calls coalesce into one spawn). A spawn that cannot happen
answers `E_INSTANCE_UNAVAILABLE`; one that stays silent past `PI2_ENSURE_TIMEOUT_MS` (default
10000) answers `E_INSTANCE_STARTING`. Both are retryable and repeated failures back off, so a client
retries rather than treating them as permanent, and `instance_state.reason` carries the failure text
(the summary plus the tail of the instance log) to show the user.
`instance.shutdown` stops the one managed instance for `workspaceId`; `mode` defaults to `drain`
(`stop` is the abrupt form) and no session file is touched.
### 3.3 WS events
Client → server: `{"type":"subscribe","payload":{"workspaceId":"ws-1"}}` (workspace scope) or
@@ -337,6 +369,11 @@ browser cmd(sessionId=s1)
- On disconnect the daemon marks the instance `degraded`; sessions become `interrupted` when the instance
reports process exit, or when the instance is restarted (epoch change).
Every relayed read also ensures: `workspace.query` (`sessions`, `entries`, `attention`, and
`models` with a `workspaceId`) routes through the same `instance.ensure`, so a browser read can
spawn nvim. `GET /api/v1/workspaces`, `GET /api/v1/sessions/recent` and the daemon-resolved
`GET /api/v1/models` (no `workspaceId`) are the reads that never spawn.
## 6. Attention
- `pi2:attention_request` is broadcast to **every** client subscribed to that session and to the