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

+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 |