# Transcript live updates

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).

## Live text

| Part | Rule |
| --- | --- |
| Order | `stream_tap` updates its blocks, persists through the quiet path, then emits the same `stream_event` frame plus `row_id`, `item_id`, `item_index`, `offset`. A text frame that opens a passage after a tool call also carries `separator`, which the page puts before the delta in the row's `content`, never in a block. |
| Task stamps | A text or thinking frame carries `task` and `step` when its block belongs to a task; the page writes them on the block it makes. A tool block's stamps arrive with its `item_upsert`, again when a task call that returned changes them. |
| Retry frames | `retry` and `structured_output_retry` stay on this frame and set the client-only `retryStatus`. |
| Citations | `text_end` stays, gains `row_id` and `item_id`, and sets citations on that block without touching its text. |
| Quiet path | The quiet path (`update_stream_progress`, `stream_node(quiet=True)`) is `durable=False` and does not fire the observer. After the reply's first write, each write is a `node.stream` line with only what was added since. A process crash loses nothing already written; a power loss can lose the tail of the reply in progress. |
| Durable writes | Tool, steering and citation writes stay durable and fire the observer. |
| Latency | The callback is synchronous, so each delta already waits on the previous delta's persist; the reorder adds at most one persist of latency to the first frame of a burst. |
| Persist failure | A persist failure does not block the emit. The offset rule repairs it. |

## Everything else: observer to publisher

| Part | Rule |
| --- | --- |
| Observer | `SessionLog._append` calls one observer at its end, gated by `is_main_writer()` so a forked child never fires it, for `node.put`, `node.patch`, `node.delete` and `meta.patch`. The observer only enqueues (session, record type, id, changed keys). |
| Layering | `state` exposes the registration hook; the Server registers on load. Runtime and state import nothing from interfaces. |
| Draining | One publisher thread drains **all** queued records before resolving a tick, so a steer and its reparent are seen together. |
| Ticks | It coalesces per view on a 50 ms leading-edge tick. A status change goes at once. |
| Numbering | Numbering and enqueueing happen under the view lock. A view the publisher rebuilds while publishing, the main view or a sub-agent view, keeps its revision, so a steer's reparent skips no number and forces no repair read. |
| New id | For a new id, the node is read and resolved in this order: caller in the owner map (call rows, under the spawn-root exclusion), then predecessor in the owner map (attach, placeholder, steer, restart prompt, continuation), then sub-agent tip rule. |
| Head patch wait | Only a caller-less chain row that will receive a head patch waits: it has no owner, and the `meta.patch` that follows dirties the new head. This covers `write_history` running before `set_head`. |
| Steers | A row with `metadata.steering` waits one tick for its reply's reparent. If the reparent never comes it is sent as it stands. |

| Dirty node | Change sent |
| --- | --- |
| New opener | `turn_upsert` |
| New top-level row in a held turn | `row_upsert` with `afterRowId`, then derived-field recompute |
| Attach, tool or code node | `item_upsert` into `attachCards`, `callRoots` or `runtimeChildren` |
| Non-text block of an open reply, including steering | `item_upsert` into `blocks` with `index`, when its crc changed |
| Other fields of an open reply | `row_patch` without content or text blocks |
| Terminal reply | One `row_upsert` with every field a reload gives |
| Any other change to a row that is not open (chapter pins, `reverted`, execution-effect rewrites, late tool rows) | One `row_upsert` from the projection, position preserved |
| A write to a row that is not open that changes nothing the row shows (a turn's start patches `memory_policy`, `turn_settings` and `turn_context` onto the sent message) | Nothing. The publisher keeps the crc of each ended row as it last went out whole (`_Sent.whole`) and skips a projection with the same crc, so a sent message reaches its holders once, not three times in the first half second. |
| Summary with `covers_ids`, written or patched, `node.delete`, rewound or structural patch, reparent outside the last turn | `window_stale { reason: structure }` |

| Part | Rule |
| --- | --- |
| Derived fields | When a top-level row joins a held turn or a member's predecessor changes, the publisher recomputes that whole turn's order, `steeringReplyId`, `continuedBy` and `continues` against the stored table and sends `row_patch` for every row that changed. If member order changed beyond a single insertion it sends `turn_upsert`. |
| Version arrows | A turn that ends on a branch the view does not show changes where a drawn fork row's arrow leads, or how many versions it counts. The publisher sends `row_patch` with the changed `siblingIndex`, `siblingTotal`, `prevSiblingId` and `nextSiblingId`. The change is counted from the oldest read no update has answered, so a later read that already shows the new arrow does not hide it from an earlier holder. |
| Size | A single `transcript_update` over 512 KiB is replaced by `transcript_gap`, because reads fragment and updates do not. |
| Goal annotation | Runs on reads, new turns and terminal rows only. |

## Run-end status

`event_effects` gains one rule: when an execution reaches a terminal status and its anchor row is still open, it writes that terminal status to the row. The write goes through the store, so the row settles live and cold. A terminal store write that failed in `finalize` is thereby repaired when the execution ends, with no display-side fallback.

`event_effects` also writes the pause and the way back, on a lifecycle event whose snapshot is still the execution's current version:

- Execution `paused`, row status `None`, `pending`, `running` or `streaming`: writes `status: paused` and `pause_reason`. `continuation_contract_mismatch` is excluded, because `_project_failed_assistant` already writes it as an error.
- Execution `running`, row status `paused`: writes `status: running` and clears `pause_reason`.

The target is the anchor row, `assistant_message_id`, which for a function run is its pre-created node. Each write goes through the store, and the publisher sends it at once as a `row_patch`. `reconcile_session_projection` applies the same mapping at hydration, so live and cold agree. New text on a paused row is appended and leaves the status to this rule.

## Stop

| Part | Rule |
| --- | --- |
| Publisher | Text frames stay gated by `_broadcast_chat_response`, which keeps post-stop silence. In the publisher, while `is_cancelled(session, execution_id=<row's execution>)` holds, non-terminal changes to rows of that execution are dropped, since the terminal row covers them. Terminal `row_upsert`s are never held. Background job rows in the same session keep updating. |
| Overlay and proof | The browser sets the Stop overlay with `until_terminal`, plus `until_command_rejected` naming the cancel's command id. A rejection whose `latest_snapshot` proves the execution still runs passes the checks in `handleExecutionCommandUpdated` (same session, not replaced by a newer execution); it sends `overlay_command_rejected`, which clears the overlay, and restores the run slot unless the slot already holds the turn at a newer version. |
| One send | A stop goes out once (`lib/execution/cancel-execution.ts`). The Worker takes it at the current version, so an old version is not a refusal. A stop of a turn that has ended answers `already_terminal`. "Stopped." follows the execution's cancelled frame. |
| Before the ack | A Stop pressed before the ack sets the same overlay and sends the cancel as soon as the ack names the execution, with or without a version. |
| Row status | The row then shows its server status, because the overlay never changed the row. |
| Other cases | A rejection without that proof leaves the overlay until the terminal row. Text frames dropped under the overlay leave a gap, and the next frame's `offset > length` repairs it with one read at the view's anchor. |

## Function-run trees

`live_progress` keeps building trees and persisting `context_tree` on the row. Its `tree_update` is the input for live delivery, because in the Agent method subprocess the store write is foreign to the parent. At the Server's fan-out, a `tree_update` calls `publisher.notice_tree(session_id, msg_id, tree, function)`. The publisher finds the row from `msg_id`, then `<msg_id>_reply`, and the target from that row:

| `msg_id` names | Change |
| --- | --- |
| An assistant reply that is not `display=runtime` (a model-issued function) | `item_upsert` into that reply's `callRoots`, without the default `expose: "full"`, as a read shapes it. Item identity is `tree.path`. A tree with no path replaces the running root of the same function name, otherwise it is added. A flat tree with no children sends nothing: the reply's tool step already shows that call, and a read draws no tree for it. |
| A `function_run` row | `row_patch { contextTree, function, status }` |
| A child in a reply's `runtimeChildren` | `item_upsert` of that child into `runtimeChildren` with `contextTree`, `function`, `status` |

| Part | Rule |
| --- | --- |
| Status | `status` comes from the tree root through `status.py`: `running`, `completed` as `done`, `error`. Any other root status sends none. |
| Open rows and ticks | A `running` tree never changes a row the view no longer records as open. Ticks coalesce. |
| Terminal tree | A terminal tree goes at once and carries the status, so the card settles even while the store write is foreign and not yet rebuilt. The terminal `row_upsert` follows with the next foreign rebuild. |
| Cold and unowned | Cold reads get the same `_exec_tnode` shape from `context_tree` or `build_exec_dag_by_id`. A tree whose row has no owner yet marks the session foreign-dirty once. |

## Foreign writes

| Part | Rule |
| --- | --- |
| Absorption | Absorbed at the two existing points, the stale rebuild in `_open` and `invalidate_cache`. These report a foreign change and mark that view's open rows dirty, and nodes with `seq` above the view's maximum are resolved like new ids. |
| Rebuild rate | The publisher reads the cached index without a stale check and takes a foreign rebuild at most once per 2 s per session, so a running subprocess cannot cause an O(N) rebuild per tick. A mark inside that window defers the rebuild to the window's end and is never skipped, so a run's last foreign write always gets its rebuild. |
| Consistency | `memory_index.rebuild` fills fresh containers and swaps them under the lock, so a lock-free reader never sees a part-filled index. Cron children have no poller; their writes appear on the next read. |

## Head moves

| Case | Rule |
| --- | --- |
| Descendant | `head_id` in a `meta.patch` is compared with the old head. A descendant extends the index and sends nothing extra. Attach and notice writers use `advance_head=False` and no longer flip the head, so a spawn cannot reach this path. |
| Branch and structure | A head that is not a descendant sends `window_stale { head_id, reason: branch }`, and every holder does one read at latest. `window_stale { reason: structure }` reads at the anchor, replaces in place and restores by message id. These reads run hydration, so session metadata refreshes too. |

## DAG panel

`branches_changed { session_id }` (replace class) goes out on attach writes, head moves and node deletes. The client runs its existing `list_branches` send. This is the only graph refresh trigger once reloads go.

## Send

| Part | Rule |
| --- | --- |
| Identity | A local row tagged `clientRequestId`. The chat action stores the same id as `metadata.client_request_id` on the user node, and function dispatch stores it on the pre-created function-run node, so a read or update names the send it belongs to. |
| Replacement | `chat_ack` re-ids the local row to the server id, or removes it when the server's row already arrived. A `turn_upsert` or read carrying that `clientRequestId` replaces it even if it beats the ack, and a reconnect after a lost ack replaces the row `settleSendsOnDisconnect` marked Not sent. Function runs work the same; the placeholder goes at ack or upsert. |
| One bubble | The row list keys a row by its `clientRequestId` when it has one (`rowRenderKeys` in `row-render-key.ts`), so the re-id is a change of fields on a row that stays mounted: the bubble is one DOM node from Enter on, and its appear animation plays once. |
| Steady height | The action row under the bubble is drawn from the first frame and hidden until the ack (`.is-pending`), so the ack does not make the row taller; the bubble's fade from 0.6 to full opacity is a transition. The standalone "Thinking…" indicator holds the empty reply's action row too, unseen, so the reply row replacing it moves nothing. |
| What an ack redraws | Only what it changes. `markSessionReady` returns the tab state untouched unless a draft tab became a conversation. The composer reads the running task as two flags (running, cancelling), not the task object that every status frame replaces. A message's action bar reads the running state in its Rewind button and through one fork selector, so a run starting or ending redraws the last prompt's actions and not every bar on screen. |
| What an ack redraws: sidebar and projects | A sidebar row (`ConvRow`) renders only when a field it draws changed and calls the list's latest handlers through a shared source. Readers of the conversation's project list that ask at the same moment share one `list_projects` request (`listSessionProjects`); a `project-changed` event ends the sharing. |
| What a run frame redraws | The message list reads the running task as what it draws from it: running, start time, the operation's words. A reply reads the row order through `useMessageIds`, and only when its conversation has tasks (`useRowTasks`); the store's own order array is replaced by every write, so reading it redrew every reply on each streamed token. `setStatusBadge` keeps the stored badge when no field changed. The backdrop preference tells its readers only of a different value. |
| What a shell change redraws | `AppShell` redraws on a tab or route change. Its children that take no props are memoized: tab strip, view controls, right panel, welcome screen, page shell, focused composer. The composer's environment row and controls cluster are memoized too, and the composer keeps their props stable (callbacks, effort levels, web-search origin), so a keystroke redraws the input and not the chips. The file tree is memoized under the right panel. |
| First send of a New chat | The draft keeps its conversation id when it is sent, and its project is resolved again as a conversation's. The right panel keeps the tree of that conversation's project meanwhile, so the tree is not reloaded and its folders stay open. Another conversation's project is never shown. |
| Pre-ack error | A pre-ack error marks only that local row, records the server's text on it as `errorDetail` and mints no `_reply`. The `NotSent` line under the failed bubble draws that text after "Not sent". |
| No local row | A rejection with no local row keeps its toast, and `run_active` keeps its requeue path. |
| Steer | The steer row carries the command id as `clientRequestId`. When a read or update delivers a row with that id, the reducer returns a `settleSteers` effect and the caller runs `settleDeliveredSteer`. This covers reads, which a frame cannot. |

## Optimistic writes

Every write the browser makes ahead of the server, and what ends it. Nothing else writes a row: server rows come only from reads, numbered updates and text frames.

| Call site | Class | Clear |
| --- | --- | --- |
| `use-chat-submit.ts` `stopSession`; `chat-handlers.ts` `wsHandleChatAck` (a Stop pressed before the ack) | overlay `status` (`stopOverlay`) | `until_terminal`, `until_command_rejected` from `handleExecutionCommandUpdated` |
| `message-actions.tsx` (version switch) | overlay `siblingIndex`; `overlay_clear` on failure | `until_read` |
| `runtime-block.tsx` (retry, version switch) | overlay `status`, `contextTree`, `siblingIndex`, `siblingTotal`; `overlay_clear` on failure | `until_row_gone` |
| `chapter-pin.ts` `setTurnChapter` | overlay `chapterPinnedAt` | `until_field_matches`, and `overlay_clear` when the server replies, because the stored pin time never equals the browser's |
| `turn-files-chips.tsx` | overlay `turnFiles`, `reverted` | `until_field_matches`, `until_row_gone` |
| `goal-state.ts` | overlay `goalVerification` | `until_field_matches` |
| `send-chat-message.ts` and `chat-stream.ts` (`appendLocalUserTurn`, the ack), `chat-handlers.ts` (pre-ack error, local command), `unacked-sends.ts`, `user-bubble.tsx` | local user row | typed local change; a read or `turn_upsert` naming its `clientRequestId` replaces it |
| `use-function-dispatch.ts` | local `function_run` row | a read naming its request id replaces it; removed at the HTTP response when no read follows, or on failure |
| `use-slash-menu.ts`, `chat-handlers.ts` `handleChatResponse` (`appendLocalNotice`) | local notice after the row then last | survives reads; the compaction notice goes when a compaction lands with a real result |
| `chat-stream.ts` (`patchHeldRow`), `assistant-file-cards.tsx` | frame-only fields `retryStatus`, `autoPreviewFiles` | typed local change; kept across reads (`withFrameOnlyFields`) |
| `session-history-loader.ts`, `conversations.ts`, `sub-agent-transcripts.ts`, `transcript-updates.ts` `applyFirstRead` | read application | `applyReadToView` |

## Sub-agent

| Part | Rule |
| --- | --- |
| Placeholder | Every spawn writes a placeholder attach row from `on_accepted`, so it carries `job.id` and `tool_call_id` and `delivery=tool_result` from the first write. The job carries `attach_pointer_id`, so every path that ends a job patches the card. |
| End write | The end of a sync spawn patches the row, because `append_message` is idempotent on id; failure paths and a result with no head patch to errored. If the placeholder write fails the end write appends the full row. |
| Delivery and reads | `notices.queue` is already a no-op for `wait=True` jobs. The card is an ordinary store write the observer delivers as `item_upsert`. `head_id` is null during the run; opening the row reads by `job_id` through `get_execution_input`. |
| Startup | Startup reconcile closes attach rows whose job is not active. A collapsed row loads nothing. |
| Expanded view | The expanded view reads through the same reader under its view key, in branch order, without moving HEAD. |
| Tip rule and text | It has its own tip rule: a `node.put` whose predecessor is the view's tip, under the `_branch_tip` filter (`user` or `assistant`, not `display=runtime`, not `function=attach`), extends the view and emits `turn_upsert`. Text frames apply to whichever loaded views hold `row_id`, so job text cannot enter the parent. |

[Wire protocol and modules](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/transcript-model/wire.html)
