# Interaction feedback

A click that starts anything slower than about 100ms changes something visible at once.

## The three layers

| Layer | Behaviour |
| --- | --- |
| 0ms optimistic | Before any network I/O the client shows a transitional state: spinner card, highlighted target, stopping, pending bubble. It is written into the session store as a typed local row, an overlay on a server row, or a flag; never as a change to a server row. |
| Fast server confirm | The 0ms row is a typed local row carrying the send's `clientRequestId`. The server stores the same id on the user node or on the function run's pre-created node, so the first read or `turn_upsert` naming it replaces the local row in place, whichever arrives first, without flicker. An ack to a socket holding no view of the chat carries that read as `first_read`. |
| Streaming backfill | Numbered `transcript_update` changes fill the row (tree, steps, status) and text frames add text by offset; the terminal `row_upsert` finishes it. A field shown ahead of the server is an overlay that ends on a stated event: the terminal status, the next read, the server's matching value, the row leaving, or the request failing. |
| Failure rollback (required) | Backfill supersedes the state, or a timeout reverts it and shows an error toast: 10s for control actions, `OPTIMISTIC_TIMEOUT_MS`. |

## Shared helper

`optimisticAction({apply, settled, subscribe, revert, onTimeoutMessage})` in `apps/web/lib/runtime-bridge/optimistic-action.ts`.

| Argument | Role |
| --- | --- |
| `apply` | Sets the 0ms state |
| `settled()` | True once real data supersedes it: message gone from the store, tree repopulated, `branch.active` flipped |
| `subscribe` | The store `settled()` reads; it is asked on each change of that store, never on a timer. One 10 s deadline is the only timer |
| `revert` | Runs with the toast on timeout while `settled()` is false |

Used where the confirm path is a `load_session` reload (retry, version switcher). A purely local transient (stop, fn-form placeholder, branch checkout) inlines the pattern.

## Per-surface behaviour

| Surface | First 100ms after the click |
| --- | --- |
| Chat send | Welcome hides at 0ms; the user bubble lands as a pending local row once the socket takes the send, and `chat_ack` gives it the server id about one round trip later; the reply arrives with the server's turn |
| Stop button | `runningTask` cleared and a cancelled overlay set on the reply at 0ms, streamed text kept; the send queue drains at the same moment |
| Function-call Retry | Card flips to a spinner body and running, switcher to N+1/N+1, at 0ms; reload backfills; 10s revert |
| fn-form or welcome submit | Pending runtime card inserted at 0ms as a local row; the read that names its request id replaces it; a POST failure removes it and toasts |
| Runtime `< N/M >` switcher | Current card becomes a spinner body with the target sibling index at 0ms; reload swaps content; 10s revert |
| Chat message `< N/M >` switcher | Label advances to the target at 0ms; reload backfills; failure reverts the label and toasts |
| Branches panel checkout | Clicked row highlighted active at 0ms; real `branch.active` supersedes it; 10s self-clear |
| Chat Retry, Edit, Branch, Rewind | `setBusy(true)` dims the buttons and `setRunActive` greys Edit and Retry |
| Session switch in the sidebar | `router.push`; cached messages render on route change |
| Enable or disable models, tools, toggles | Local store flip |

New optimistic states use the store's own machinery: typed local changes (`applyLocalChange`) and overlays, `siblingIndex`, `setRunningTaskFor`. No parallel state.
