feat(daemon): serve a live-only prompt-history read

The web composer's ↑/↓ recall needs the plugin's prompt history, so the browser
and nvim walk one list instead of two. The daemon relays it and stores nothing:
it is a router, and the plugin stays the authority (AGENTS.md rule 2).

- plugin `hello` advertises the new `promptHistory` capability; the daemon never
  relays the read to a plugin that did not advertise it
- `workspace.query{kind:"promptHistory",limit}` is answered by the plugin from
  its own `prompt.history` archive (workspace cwd), oldest-first
- `GET /api/v1/workspaces/:id/prompt-history?limit=` → `{v, workspaceId, live,
  supported, entries}`; `limit` defaults to 200 and clamps to 500, an unknown
  workspace is a 404, and malformed entries are dropped
- the read deliberately does **not** call `instances.ensure()`: recall is a
  keystroke, not a lifecycle event, so a workspace with no connected plugin
  answers `{live:false, supported:false, entries:[]}` instead of booting an nvim
- `PluginConnection.capabilities` joins the interface the daemon sees, and the
  fake connection grows a writable one so the gate is testable

No event or reducer change: `fixtures/` are untouched.
This commit is contained in:
yuez committed 2026-11-02 11:12:21 +08:00
1 parent c24f30fe71
commit 32292873f9
6 files changed
+204

No files matched your search

+40
View File
@@ -67,8 +67,19 @@ The plugin dials the daemon (`<runtime>/d.sock`), sends `hello` first.
{"v":1,"type":"workspace.query","id":"req-2","payload":{"kind":"sessions"}}
// a session-scoped read keeps the sid on the envelope (see §3.1 entries)
{"v":1,"type":"workspace.query","id":"req-3","sessionId":"s1","payload":{"kind":"entries","limit":50}}
// workspace-scoped read, no sid: the answering instance *is* the workspace (see §3.1.1)
{"v":1,"type":"workspace.query","id":"req-4","payload":{"kind":"promptHistory","limit":200}}
```
`hello.capabilities` is how a plugin announces optional behaviour; the daemon never
relays a feature-gated command to a plugin that has not advertised it. Known strings:
| Capability | Meaning |
|---|---|
| `snapshot` | answers `sub.subscribe` with a `snapshot` |
| `attention` | reports pending attention (§6) |
| `promptHistory` | answers `workspace.query{kind:"promptHistory"}` with the workspace's prompt input history (§3.1.1) |
Plugin obligations:
- **Say `bye` when leaving on purpose.** A plugin that stops the bridge (`:PiWebStop`, `VimLeavePre`)
@@ -100,6 +111,7 @@ Plugin obligations:
| `GET /api/v1/sessions/recent?limit=` | disk-projected recent sessions across every workspace (below); never spawns an instance |
| `GET /api/v1/sessions/:sid/entries?before=&limit=&workspaceId=&sessionFile=` | relayed `workspace.query{kind:"entries"}`; plugin paginates |
| `GET /api/v1/attention?workspaceId=` | pending attention for one workspace |
| `GET /api/v1/workspaces/:id/prompt-history?limit=` | **live-only** `workspace.query{kind:"promptHistory"}`, never spawns; `{v, workspaceId, live, supported, entries}` (§3.1.1) |
| `GET /api/v1/devices` | paired devices (daemon-owned; `{id, name, kind, createdAt, lastSeenAt, revokedAt}`) |
| `POST /api/v1/pair` | **authenticated**; mints `{code, url, expiresAt, nonce, qr, bases}` |
| `POST /api/v1/pair/redeem` | public + rate limited (`E_RATE_LIMITED`); `{code, kind, deviceName}` → `{token, device}` |
@@ -116,6 +128,34 @@ does not know. A session that exists but has no JSONL yet (`pi` creates the file
the first entry) answers an **empty page** (`{entries:[], cursor:{hasMore:false}}`),
never an error: a new session has no history to fail on.
#### Prompt input history (`/workspaces/:id/prompt-history`)
The composer's ↑/↓ recall reads the workspace's prompt history from the plugin, which
owns it (pi2.nvim's `prompt.history`, one archive per workspace cwd). The daemon is a
relay here and stores nothing: a browser that loses this read still works, it just
cannot see what nvim recorded.
```jsonc
GET /api/v1/workspaces/ws-1/prompt-history?limit=200
{ "v": 1, "workspaceId": "ws-1", "live": true, "supported": true,
"entries": ["oldest first", "newest last"] }
```
Three rules make this read safe to call from a keystroke handler:
- **It never spawns an instance.** A workspace with no connected plugin answers
`200 {live:false, supported:false, entries:[]}` instead of `instances.ensure()`, so
pressing ↑ on an idle workspace cannot boot an nvim. Every other relayed read spawns.
- **It is capability-gated.** A connected plugin that did not advertise `promptHistory`
answers `{live:true, supported:false, entries:[]}` and receives no command at all.
- **The client owns the fallback.** When `supported` is false the console recalls from its
browser-local mirror (`docs/prompt-history.md`). The cursor (position + stashed draft) is
always client state; only the text of the entries comes from the plugin.
`limit` defaults to 200 and is clamped to 500; the plugin answers oldest-first and the
daemon trims to the newest `limit` entries. Unknown workspace → `404`, exactly like
`/sessions` and `/attention`.
The `system` object on `GET /api/v1/resources` is additive (absent on daemons
older than 0.1.5, so clients must tolerate its absence) and reports **host**
figures sampled by the daemon itself: