# Transcript reads and client rules

Part of the [transcript model](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/transcript-model/overview.html) record. Built so far: [implementation status](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/transcript-model/overview.html#status).

## The client rules

| Rule | Behaviour |
| --- | --- |
| Cursor | Each view has a cursor. A `transcript_update` with `revision <= cursor` is ignored. `previous == cursor` is applied. Anything else causes one coalesced read at the reader's anchor. A frame for a view with no window is dropped with no read. A full window replacement sets the cursor to its revision unconditionally. A page or one-turn reply never moves the cursor. |
| Revisions | Revisions come from one process-wide counter, so they only rise. A view rebuilt from the store starts at a fresh number, so its next frame's `previous` matches no cursor and the holder does one read. A view the publisher rebuilds while publishing keeps its revision. A worker restart kills the socket, and the new socket reads. |
| Item or patch miss | `item_upsert` and `row_patch` carry `turnId`. Row not held and turn not in the window: drop silently. Row not held and turn in the window: at most one in-flight read per turn, and if it returns without that row, stop. No row is ever created from a miss. |
| Upsert on an open row | `turn_upsert` and `row_upsert` never replace a held text or thinking block of an open row with a shorter one. A terminal row replaces the whole row. |
| Non-latest window | A `turn_upsert` is appended only when `after` is null. Otherwise only `total` advances. |
| Immediate flush | The reducer flushes a row's pending text before applying any item or status change to that row. |
| Read owner | `createSessionLoads` is the only read owner, keyed by view, with one in flight and one pending. A finished reply is applied, then the pending read is sent, so a stream of invalidations cannot starve it. Repair and page reads go through `exclusive`: a newer one replaces the waiting one, which resolves as skipped, and a repair that finds a read already in flight folds into it. |

**Text frame.** Given `row_id`, `item_id`, `item_index`, `offset`:

- Row not held: drop, no read. Row creation is a numbered change, so a missing row is found by the cursor.
- Row status settled, or a Stop overlay on the row: drop.
- Item absent and `offset == 0`: create it at `item_index`. A frame for an index past the row's last block waits beside the row until the publisher's next change brings the blocks before it.
- `offset == length`: apply. `offset < length`: trim the overlap and apply the rest.
- `offset > length`: one read at the view's anchor, none while a read of that view is in flight. Frames arriving meanwhile are dropped, because they are at or behind the read.

Lengths are in UTF-16 units and include the row's pending rAF text.

## Parking

| Part | Rule |
| --- | --- |
| Scope | Every read that returns rows (first open, page, one-turn, reconnect, chat ack with a window) parks that session's row-writing frames on that socket from before capture until the last fragment of the reply is queued. Row-writing frames are `transcript_update`, `transcript_gap` and `stream_event`. |
| Terminal frames | Terminal `chat_response` frames (`result`, `error`, `cancelled`) carry only run-level effects and write no row, so they do not park. |
| Start and merge | Parking starts synchronously on the event loop, before the read's first thread hop. Parked contiguous text merges, so a normal stream does not fill the buffer. |
| Limits | Over 256 parked frames, the buffer is dropped and one `transcript_gap` is sent after the reply. Nested reads of one session count depth and flush at zero. Disconnect clears it. |
| Why it is enough | Persist-before-emit means every frame sent before capture is reflected in the read. Parking means every frame numbered or emitted after capture arrives after the reply. Text therefore always arrives at or behind the read, and the offset rule trims it. |

## First open

| Part | Rule |
| --- | --- |
| Request and capture | `load_session` with `history_version: 3`. `handle_load_session` runs `reconcile_session_projection` as it does now, joins the view on the socket and starts parking, gets or builds the `TranscriptView` outside any publish lock, then under the view lock captures revision R and projects the newest 6 turns, stopping at 128 KiB of actual bytes. |
| Ordering | Projection and capture share the lock, so frames numbered `<= R` were built before the read and frames `> R` after it. |
| Reply | `session_loaded` carries `turns`, `window` and the existing metadata with `graph: []`. Parked frames follow the last fragment. |

## Brand-new chat

| Part | Rule |
| --- | --- |
| Join and ack | No `load_session` is sent, because the session id is first known at ack. The chat action joins the view on the sending socket and parks. For a protocol 3 socket that does not hold the view, `chat_ack` carries `first_read: { turns, window }`, captured the same way as a first open, then parked frames follow. |
| Contents | The turn is persisted before the ack, so `first_read` holds the user row. The reply placeholder and everything after arrive as numbered updates. |
| Call sites | Applies to `_send_chat_ack`, `retry.py` and `turn_local_command.py`. A pending local row is replaced by `clientRequestId`. |

## Page older or newer

| Part | Rule |
| --- | --- |
| Request and reply | Same `load_session` with `history_before`, `history_after`, `history_around` or `history_latest`, values being any row id. 25 turns or 512 KiB. Bodies come fresh from the store. Reply `session_history_page { id, turns, window, request_id, action }`. |
| Cursor | A cursor no longer on the branch returns `cursor_not_on_branch` and the client does one first read. The row a reader in history was on is forgotten with it, so the first read does not ask for it again. A head that only advanced is not a mismatch. |
| Placement | The client places a page by `window.start`, never at the tail: a page that is neither adjacent to nor overlapping the held span is dropped. Restore by message id before paint. Evict whole pages. |
| Anchors | Anchors are saved only from server rows, never local ones. |

## Reconnect

Transcript read at the anchor, then execution recovery, snapshot-only. Local rows and overlays survive.

## Split pane

Frames are delivered to every socket holding the view and applied regardless of focus. A pane reads when its view has no window. Both panes draw through one `TranscriptRows` and take session scope from the row list, not the focused session.

## View lifecycle

| Part | Rule |
| --- | --- |
| Closing | The browser sends `close_view { session_id, view }` when it evicts a conversation or closes a pane or an expanded sub-agent. |
| Cap | A socket holds at most 16 views. Over the cap the server drops the socket's least recently read view and sends `view_closed`; the client removes that window, and a pane still showing it reads again. |
| Release | Socket stop releases all its views. The four-conversation browser limit is enforced by `trimHistoryWindows` and is what keeps a socket under the cap. |

## Non-web consumers

| Consumer | Rule |
| --- | --- |
| Event path | CLI, ACP, channels, `worker_turn` and `bound_reply` keep `on_event` and `stream_event` untouched. |
| CLI and rehearsal | The CLI (`history_version` absent) and `rehearsal` read `session_loaded.messages` built by `flat_rows.py`: `id`, `role`, `content`, the whole branch, no paging. |
| HTTP route | The HTTP command route (`CommandReply`, protocol 1) gets the same fields capped to the newest 6 turns or 128 KiB, with `history_before` and `history_after` still accepted, because `CommandReply` refuses replies over 4 MiB. |

[Live updates](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/transcript-model/live.html)
