fix(daemon): forward snapshot entries and map workspace errors

- Preserve the snapshot entries tail in the cache and WS frames (cold,
  cached and bus paths), so the browser receives {state, entries}.
- Await a plugin snapshot on the first subscribe of a fresh session.
- Return E_FORBIDDEN (403) for workspace paths outside PI2_WORKSPACE_ROOTS.
- Mint cli.token only after the singleton lock is held, so a second daemon
  cannot invalidate the running one.
- Add hostname/os/version/uptimeMs/pid/port to /api/v1/info and make
  /health reflect instance, workspace and auth state.
This commit is contained in:
yuez committed 2026-10-21 20:42:02 +08:00
1 parent 31f36302bd
commit 5572ac0d84
15 files changed
+661 -22

No files matched your search

+8 -2
View File
@@ -83,8 +83,10 @@ Flags:
Flags are applied to the environment before the config is loaded, so
`--port 8000` is equivalent to `PI2_PORT=8000`.
Daemon startup order: mint `cli.token` → take `daemon.lock` → start the plugin
Daemon startup order: take `daemon.lock` → mint `cli.token` → start the plugin
unix socket → listen (with port retry) → write `daemon.json` → print the line.
The token is rotated only by the process that owns the lock, so a rejected
second daemon cannot invalidate the running one.
## 4. Configuration
@@ -287,7 +289,11 @@ For a login-less service to keep running on Linux enable linger
`Environment=` line) .
- Managed nvim output: `PI2_STATE_DIR/instances/<instanceId>.log`.
- `GET /api/v1/health` → `{ok, items:[{name, ok, detail}]}` (`instances`,
`workspaces`, `auth`, `static`).
`workspaces`, `auth`, `static`). `instances` fails when a managed instance is
`degraded`; `workspaces` fails when every known workspace path is gone (ghost
discovery entries are normal and only shown in `detail`); `auth` fails when
there is neither an active device nor a local `cli.token`; `static` fails when
`PI2_STATIC_DIR` is missing.
- `GET /api/v1/diagnostics` → uptime, connection count, event rate, slow
queries, device/workspace/instance counts.
- `GET /api/v1/resources` → per-instance RSS, total vs physical memory, soft
+6 -2
View File
@@ -79,7 +79,7 @@ Plugin obligations:
| Endpoint | Notes |
|---|---|
| `GET /api/v1/info` | `{v, daemonId, machineId, platform, capabilities, limits}` |
| `GET /api/v1/info` | `{v, daemonId, machineId, platform, hostname, os, version, uptimeMs, pid, port, capabilities, limits}` |
| `GET /api/v1/health` | `{ok, items:[{name, ok, detail}]}` |
| `GET /api/v1/workspaces` | `WorkspaceInfo[]` (registry + disk index + instance state) |
| `GET /api/v1/workspaces/:id/sessions` | relayed `workspace.query{kind:"sessions"}` |
@@ -111,6 +111,10 @@ Client → server: `{"type":"subscribe","payload":{"workspaceId":"ws-1"}}` (work
Server → client (same envelope as above): `hello_ack`, `instance_state`, `workspace_update`, `snapshot`,
`event`, `presence`, `resync_required`, `dropped`, `error`.
A session `subscribe` is answered with a `snapshot` as soon as the plugin's reply
arrives (the daemon waits briefly for the plugin's `sub.subscribe` snapshot and also
forwards every later plugin snapshot to current subscribers).
`GET /events` (SSE) emits the same `event` records for degraded transports.
## 4. Session state
@@ -212,7 +216,7 @@ browser cmd(sessionId=s1)
|---|---|---|
| `E_BAD_REQUEST` | 400 | malformed JSON / missing field |
| `E_UNAUTHORIZED` | 401 | missing/invalid device token |
| `E_FORBIDDEN` | 403 | bad `Origin`, insufficient scope |
| `E_FORBIDDEN` | 403 | bad `Origin`, insufficient scope, cwd outside `PI2_WORKSPACE_ROOTS` |
| `E_NOT_FOUND` | 404 | unknown workspace/session/instance |
| `E_PROTOCOL_VERSION` | 426 | `v` unsupported |
| `E_PAYLOAD_TOO_LARGE` | 413 | body/frame > limits |