# Nested LLM streaming

Each `llm()` or `runtime.exec(..., execution_kind="llm")` inside an Agent method streams over `execution_stream.v1`, in-process or from a subprocess, with the same result on screen. The node's expanded layout is in [Nested LLM node content](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/dag/nested-llm-node-content.html).

1. **Provider** text, thinking, tool and retry events
2. **`CallStreamState`** merges deltas, numbers revisions, checkpoints the node
3. **Transport** subprocess frames are checked by the parent
4. **Browser store** one state per node, applied by revision; a gap or refresh subscribes for a snapshot

## Channels

| Carrier | Holds | Limit |
| --- | --- | --- |
| `execution_stream` | Attempts, blocks and text of one LLM node. Driven per frame. | Never touches the assistant reply content |
| `tree_update` | Tree structure, status and `stream_preview` from the checkpoint. Sent when the run writes a node, at most once a second. | A terminal status is a critical frame |
| `Call.output` | Final output, written when the node closes | Stream state is a preview, never the final result |

Only `result`, `error` and `retry_result` frames finalize chat bookkeeping. A nested stream frame never clears the parent run, overwrites reply text or refreshes branch metadata.

## Frames

```
{"type": "chat_response", "data": {
  "type": "execution_stream", "schema_version": 1,
  "session_id": "...", "execution_id": "...", "node_id": "...",
  "generation": 1, "display_msg_id": "...",
  "op": "block_delta", "base_revision": 11, "revision": 12,
  "attempt_id": "...", "message_id": "...", "block_id": "...",
  "kind": "text", "delta": "Hello"}}
```

|  |  |
| --- | --- |
| `attempt_started` | A provider attempt begins. A running predecessor becomes `superseded`. |
| `block_started`, `block_delta`, `block_finished` | One ordered block of an attempt |
| `attempt_finished` | Status `completed`, `failed`, `cancelled` or `superseded`, with validation |
| `snapshot` | Full state; replaces what the client holds |
| `node_finished` | Terminal decision with a full snapshot |
| `sync_required` | The client must resynchronize |

**Block kinds, phases and budgets**

|  |  |
| --- | --- |
| Block kinds | `text`, `reasoning_summary`, `tool_ref`, `tool_arguments`, `parallel_group`, `refusal`, `unsupported`. Opaque signatures are never accumulated as text. |
| Node phases | `created`, `running`, `retry_wait`, `validating`, `finalizing`, `completed`, `cancelling`, `cancelled`, `failed`, `paused`, `reconciliation_required` |
| Delta merge | Same block, within 25 ms and under 16 KiB. Never across nodes or blocks. |
| Checkpoint | To `Call.metadata.stream` every 0.25 s or 64 KiB of new content, skipped for ephemeral streams |
| Frame size | Target 64 KiB, inline preview 32 KiB. The parent rejects a child frame over 96 KiB. |
| Send queue | 256 frames and 4 MiB per connection. Adjacent `block_delta` frames of one block with continuous revisions merge; other operations are critical. |

## Applying a frame

| Frame | Result |
| --- | --- |
| Older generation | Rejected |
| Newer generation with a snapshot | Replaces the state even with a smaller revision, unless the node is terminal |
| Revision not above the current one | Ignored as a duplicate |
| `base_revision` differs from the current revision | Requests a snapshot; never appends blindly |
| Any running update on a terminal node | Rejected; terminal nodes never return to syncing |
| `sync_required` with reason `owner_unavailable` | Ends this recovery and keeps the content. It cannot trigger another identical request; a later stream event may. |

The browser store is a display replica keyed by session, execution and node, so parallel calls do not mix. `subscribe_execution_stream` answers from the live owner, else from the checkpoint.

## Execution tree row

|  |  |
| --- | --- |
| Running | Growing reply from the stream, else `stream_preview`, else output. Reasoning shows from `reasoning_summary`. Prompt and parameters stay in the short note; the full prompt is in details. |
| Finished | `output` or `raw_reply`, with duration |
| Syncing | Note reads "resyncing…" |
| Retrying | Note reads "retrying (…)"; older attempts stay available |

## Failure and reconnection

|  |  |
| --- | --- |
| User cancel | Phase `cancelled`; partial text kept; later deltas rejected |
| Provider error | Phase `failed`; partial text and the error stay visible |
| Transport retry or structured-output repair | The attempt ends and a new one starts |
| Refresh or reconnect | Saved snapshot renders first; live frames continue. Deltas are not replayed. |
| Owner gone | Checkpoint snapshot, or `owner_unavailable` when there is none |

**Subprocess path**

The child never opens a WebSocket. It puts frames on the existing `event_queue`; the parent checks schema version, known operation, session, execution, a generation not ahead of the launch record, and size, then forwards them. The parent never reallocates a revision or writes the child's checkpoint.

## Not covered

- Token-level audit logs or replayable delta recordings
- Changes to top-level `stream_event.text`; full terminal-interface parity
- Per-field highlights for structured output; image deltas
- Cross-session stream subscriptions
- Replacing `tree_update`

[Activity and control](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/execution/control.html)
