# Transcript rows and ownership

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 model

Defined once in `.../transcript/schema.py`. TypeScript is generated from it.

### View

Key: `session_id`, or `<session_id>#sub-agent:<first job id>`. Two panes on one conversation share one view.

- `turns`: loaded Turns in order. The server decides order, the client holds the span.
- `window`: `{ head_id, revision, before, after, start, end, total }`. Same names the history hooks read, minus `snapshot`, plus `revision`. Counted in turns. `start` and `end` are absolute positions in the turn index, stable while the head only advances.
- A view has a window only after a server read or a chat ack that carries one. A pane can never mistake leftover rows for a loaded chat.

### TranscriptView (server)

Per view, ids only, no bodies: ordered turns, owner map (node id to turn, row and slot), open rows with a block crc, child maps, the derived-field table, compaction slots, goal-verification anchors, and either `head_id` (main view) or `tip` (sub-agent view).

| Part | Rule |
| --- | --- |
| Input set | Exactly the rows the transcript shows: `get_messages`' filter (no `display=root`, `rewound`, `structural` or hidden `context/*`), minus `execution_control` rows and the closure of their caller descendants. Role=tool rows with an assistant caller are folded into their parent as `aggregate_tool_messages` does. |
| Chain and failures | The chain is the head's branch in branch order. A head folded away by aggregation resolves through the `_resolve_head` walk; a broken predecessor chain falls back to the predecessor walk over the same input set. Both are named cases in `legacy.py`. Any other read failure returns a retryable error. |

**Turn membership** (`turn_index.py`). Rows are classified in this order and the first match decides:

1. Side rows join the row they hang from and never open a turn: `function=attach` (predecessor-only or both edges), runtime placeholders (`type=status`, `display=runtime`, with a predecessor; a rebuilt function run has none and opens its own turn), tool rows, caller descendants, summaries with `covers_ids`.
2. A compaction `{id}_card` row stays at its exact position inside the surrounding turn. It is the only row a compaction adds; nothing marks where the compaction ran.
3. Steers (`steering`) and restart prompts (`trigger=restart`) join the turn they sit in, as do cut replies and continuations.
4. These open a turn: a visible user row, including `job_followup` and `display=runtime` prompts; a top-level function run; an assistant row whose predecessor is not a user row of the current turn (proactive always-on replies); a system row with no prompt.

`spawn_branch_root` rows are never adopted into a parent turn by a caller edge. Caller descendants match only the tool and leaf rule of `_collect_exec_rows`.

The index is extended as the head advances and rebuilt on a structure change. Views no socket holds sit in an LRU of 16. A view a socket holds is never evicted.

The store's transcript state moves on what the index keeps instead of reading fresh: a reparent, and a metadata patch to a summary, whose compaction rows are drawn from its metadata. Compaction stamps `tokens_before`, `tokens_after` and `compacted_at` after the insert. Other patches change row bodies, which every read takes fresh.

Everything non-local is computed from index metadata, so a turn can be projected alone: compaction slots and `superseded_summary` from summary nodes, `spawned_from` from attach nodes, sibling navigation from child maps, goal marks from one scan of the session's executions held as `{assistant id: execution id}` and extended as new turns appear.

### Turn

`{ id, rows }`. `id` is the opener row's id. A compaction row placed before the first opener joins that turn but never becomes its id, so the id is stable.

### Row

The existing `ChatMsg`, same camelCase fields, built on the server. Additions:

| Field | Meaning |
| --- | --- |
| `rowKind` | `user \| assistant \| function_run \| system_text \| system_event \| compaction_card \| agent_notice \| received_message \| spawned_root \| sub_agent_step \| external_reference \| silent`. One value per branch of `dispatch()`, which has no default. `silent` covers `display=runtime` prompts and internal goal turns. |
| `kind`, `slot` | Not additions; they keep their existing meaning: `kind` is `compaction \| snip \| event`, `slot` is `card \| event`. A `system_event` row carries its sub-kind there; a finished compaction produces none. A `compaction_card` row is always `kind: compaction`, `slot: card`. Only `SystemEventRow` reads them; covering reads `coveredBy`. |
| `status` | `pending \| running \| streaming \| paused \| cancelling \| done \| error \| cancelled \| interrupted`, from one mapping in `status.py`. |
| `steeringReplyId`, `continuedBy` | Stamped by the server, because the bot view still draws those rows. |
| `continues` | Ids of the cut replies a continuation joins, oldest first. |
| `attachCards`, `runtimeChildren`, `callRoots` | Names unchanged. Each child gains `blockId`, the resolved owning block, or null when unpaired. An unpaired child is drawn, never lost. |
| `coveredBy` | Compaction id. |
| `origin` | On user rows: `person \| agent_delivery`. |
| `clientRequestId` | `metadata.client_request_id`, or `metadata.command_id` on a steer row. |

### Block

As stored, plus `id` and `index`.

| Field | Rule |
| --- | --- |
| Tool block id | `id = tool_call_id`; a legacy tool block with no id uses `tool:N`. |
| Text and thinking id | `id = <type>:<n>`, where `n` counts runs of that type, a run being consecutive stored blocks of one type. The first block of a run keeps `<type>:<n>`. A later block in the same run is `<type>:<n>.<k>`. The stream tap merges consecutive deltas into one block, so its block and the first block of the final segmentation share an id, and an open step keeps its place when the final write re-segments. |
| `index` | Stored position in `blocks`. Blocks are always held sorted by `index`, so block order never depends on arrival order. |
| `fullOutput` | Names the real row: `<reply>_t_<tid>`. |
| `task`, `step` | On a text, thinking or tool block: names the task and step it belongs to, from its position in the reply; absent when it belongs to none ([Task blocks](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/chat/task-blocks.html)). |

### Local rows and overlays (browser only)

**Local rows** are rows with no server copy: pending send, failed send, optimistic function placeholder, `local_command` and no-op notices, compaction notices. They belong to no Turn and carry an anchor:

- `tail` (sends and placeholders): drawn after every turn, in creation order. A `turn_upsert` from elsewhere inserts before them. Removed by acknowledgement or by a server row with the same `clientRequestId`.
- `afterRowId` (notices): drawn directly after the row that was last when the notice appeared. Not drawn when that row is outside the window.

Every local row carries a `rowKind`. A pending or failed send is `user`, an optimistic function placeholder `function_run`, a `local_command` or no-op notice `system_text`, a compaction notice `system_event` with `kind: compaction`. The typed local append requires the field, so dispatch never meets a row without one.

| Part | Rule |
| --- | --- |
| Overlays | **Overlays** are the one way the client changes what a server row shows before the server says so. Per view: row id, a closed field set (`status`, `siblingIndex`, `siblingTotal`, `contextTree`, `chapterPinnedAt`, `reverted`, `turnFiles`, `goalVerification`), a clear condition (`until_terminal`, `until_field_matches`, `until_read`, `until_row_gone`, `until_command_rejected`) and an optional rollback snapshot. |
| Merge | `selectRow` merges row and overlay. Reads and upserts leave an overlay in place until its condition holds. |
| Stop overlay | A Stop overlay is `status: cancelled` over an open row. It clears on any terminal server status, so a later `cancelling` cannot pull a stopped bubble back to thinking, and on rejection of its cancel command ([Stop](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/transcript-model/live.html#stop)). |

Frame-only fields a server row never clears: `retryStatus`, `autoPreviewFiles`. Expansion state lives per view, keyed by (row id, block id).

## Ownership of each fact

| Fact | Owner | Readers |
| --- | --- | --- |
| Text, tools, status, cards, order | Store nodes, read through the projection | Everything. Deltas are disposable. |
| Turn membership, owner of each node | `TranscriptView` | Reader, publisher |
| `rowKind`, block ids, `blockId` pairing | `transcript/` projection | Components |
| Revision | One process-wide monotonic counter; `TranscriptView` holds the view's last value | Client cursor |
| Terminal status and pause of a run's reply | Store row, written or repaired from the canonical execution by `event_effects` | Projection |
| `run_active`, `run_task`, `run_seq`, open waits, goal, todos, context stats | Unchanged, outside the transcript model. The run slot writes no row. | Run-level UI |
| Local rows, overlays, `retryStatus`, `autoPreviewFiles`, expansion | Browser | Components |
| Follow position | `useChatAreaStick` only | The transcript viewport |
| Anchors | `history-viewport` only | The transcript viewport |

[Reads and client rules](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/transcript-model/reads.html)
