# Prompt input history (web composer) The Chat composer recalls previously sent prompts with the arrow keys. The text comes from the **plugin** (pi2.nvim owns the archive); the daemon relays a live-only read; the browser keeps a mirror so recall still works when the relay cannot answer. This document is the web-side map of that design — the wire contract lives in `docs/protocol.md` §3.1.1. ## Key map | Key | Behaviour | |---|---| | `↑` (``) | Recall the previous (older) prompt. | | `↓` (``) | Walk back toward the newest entry; past it, restore the draft. | | `Enter` / `Shift+Enter` | Unchanged: send / newline. | `↑` and `↓` only recall when the caret has nowhere left to go — `↑` on the **first** line, `↓` on the **last** line — or, for `↓`, while already browsing history. A collapsed caret is required (a selection means "editing a fragment"), and an IME composition (`isComposing`) keeps the keys to itself, because arrows navigate its candidate window. `` / `` are the readline aliases and always recall. Recalling stashes the in-progress draft, which comes back when you walk past the newest entry; the caret is parked at the end of the recalled text. Any manual edit ends browsing, so a later `↓` can never overwrite what you typed. Sending also ends browsing and records the prompt (trimmed). ## Who owns the text Three tiers, in this precedence order: | Tier | Owns | Lifetime | |---|---|---| | **pi2.nvim** (`prompt.history`, one archive per workspace cwd) | the entries | durable, per workspace, shared with every surface | | **daemon** | nothing | — (relay only) | | **browser mirror** (`localStorage`, `pi2-prompt-history`) | a fallback copy | per browser profile, bounded | 1. **The plugin is the authority.** Every prompt is recorded by pi2.nvim when it is sent — `Store:add` inside `Chat:_send_message`, whichever surface sent it — so nvim's ↑ and the console's ↑ walk the *same* list, and so does every device paired to this daemon. The console writes nothing back: sending through the composer already lands in that store through `prompt.send`. 2. **The daemon relays one read and stores nothing.** `GET /api/v1/workspaces/:id/prompt-history?limit=` → `{live, supported, entries}`. It is **live-only**: with no connected plugin it answers `live: false` instead of `instances.ensure()`, because pressing ↑ must never boot an nvim. It is also **capability-gated**: a plugin that did not advertise `promptHistory` gets no command at all (`supported: false`). Unknown workspace → `404`. 3. **The browser mirror is the fallback.** Written on every send and refreshed from every successful relay read, so losing the plugin (instance restart, an older daemon that 404s the endpoint, an older plugin without the capability, an offline browser) still recalls the last known history instead of only what this browser typed. It is never authoritative: when the relay answers `supported`, the plugin's entries win. Version skew is therefore boring: a new console against an old daemon (404) or an old plugin (no capability) simply keeps using the mirror. ## What is remembered Mirrors `Store:add` in `lua/pi/prompt_history.lua`, on both the plugin and the mirror: * blank / whitespace-only prompts are not recorded; * a prompt identical to the newest entry is skipped (readline consecutive dedupe — a non-consecutive repeat *is* kept); * entries are multi-line safe and stored verbatim; * the newest 500 are kept (`HISTORY_LIMIT`; the plugin's `prompt.history.max` defaults to the same). ## Scope and storage The mirror is one `localStorage` key, `pi2-prompt-history`, holding `{ [workspaceId]: string[] }`, entries oldest-first. * **Scope = workspace id**, so every session in a workspace shares one archive (like nvim's per-cwd store) while another workspace starts clean. A session not yet bound to a workspace uses the `default` bucket rather than losing the send. * **Bounded**: the 40 least-recently-used workspaces are kept (`WORKSPACE_LIMIT`), so a long-lived profile cannot grow without bound. * Every read/write tolerates a missing, corrupt or unwritable storage (private mode, quota): the feature degrades to "no mirror", never to a broken composer. * The **cursor** (position + stashed draft, `PromptHistoryNav`) never leaves the composer. No server sees it, and two tabs never share it. ## Where it lives | Path | Contents | |---|---| | `web/src/features/chat/prompt-history.ts` | the mirror (`readPromptHistory`, `rememberPrompt`, `replacePromptHistory`, `clearPromptHistory`), the readline rules (`appendEntry`), the `PromptHistorySource` seam and `PromptHistoryNav` | | `web/src/features/chat/prompt-history-feed.ts` | `usePromptHistory(workspaceId, enabled)` — the relay read, the optimistic append after a send, the mirror refresh, and the fallback decision (`remote` / `live`) | | `web/src/features/chat/composer.tsx` | key handling, draft stash, caret placement; recalls from and records into the injected source | | `web/src/pages/chat.tsx` | builds the feed and injects it | | `src/daemon/router.ts` | `relayPromptHistory` (live-only, capability-gated, no `ensure()`) | | `src/daemon/http.ts` | `GET /api/v1/workspaces/:id/prompt-history` | | `pi2.nvim` `lua/pi/web/bridge.lua` | advertises the `promptHistory` capability in `hello` | | `pi2.nvim` `lua/pi/web/commands.lua` | answers `workspace.query{kind="promptHistory"}` from the workspace archive, and records every `prompt.send` into it (the TUI records inside `Chat:_send_message`, which a web send never reaches) | | `web/tests/prompt-history.test.ts` | mirror + readline rules + cursor contract (the port of `tests/prompt_history_spec.lua`) | | `web/tests/chat.prompt-history-feed.test.tsx` | relay, fallback, mirror refresh, version skew | | `web/tests/chat.composer.test.tsx` | the keyboard rules | ## Not done (deliberately) * **Cross-host sharing.** One daemon sees one host's plugins. Two machines running their own `pi2d` still have separate archives; sharing them means a shared store directory (Syncthing/NFS) *and* switching the plugin's store from whole-file rewrite to append-only JSONL first, or two nvim processes in the same cwd clobber each other. * **History without nvim.** Because the plugin is the authority, a workspace with no live plugin recalls only the mirror. Making the daemon hold the durable store would lift that, at the cost of a second writer (the plugin must still work standalone), conflict resolution and a daemon-owned user-data lifecycle — an ADR decision, not a patch. * **A "forget this workspace" action.** `clearPromptHistory(scope)` exists and is tested; nothing in the UI calls it yet.