# In-flight state recovery

| What happens | What the chat shows | Source |
| --- | --- | --- |
| Page refresh, worker alive | The running row with its latest saved state, then numbered updates and text frames placed by offset | The row as stored, `running_task` frame, `transcript_update` |
| Run ended during the load | The running slot closes | `running_task_clear` |
| Worker restart | Row marked `interrupted`, amber, with “Worker restarted before this turn finished” | `reconcile_interrupted_runs` |
| Open question or approval | The card is drawn again | `_replay_open_waits` |

## Placeholders

Work that runs for a while writes its node before it starts. Status lives in `metadata`; the `Call` dataclass does not change.

| Producer | Written by | Saved while running |
| --- | --- | --- |
| Assistant reply | `turn_lifecycle.py`: empty `role="llm"` row with `status="running"`, `worker_id`, `started_at` | Text and thinking blocks as they stream, through `stream_tap`'s quiet path (`update_stream_progress`, not fsynced) before each frame goes out; the full row at the end of the turn, partial output on cancel |
| Agent method run | Placeholder `<msg_id>_reply` | The live reader in `execution/projections/dag.py` writes `context_tree`, `status` and `last_update_at` |
| Agent method called by the model | `runtime_attach.py`: `display=runtime` row | Same tree |
| `/task --async`, merge | `slash_actions.py`: attach pointer | Job status on the card |
| Sub-agent | `sub_agent_run.py` | Job status on the card |

The tree is read again when the run writes a node, at most once a second. It flushes once more at the end, so a refresh after the run shows it done.

**Status values**

- **Waiting**: `pending`, `paused`
- **Producing**: `running`, `streaming`, `cancelling`
- **Finished**: `completed`, `done`, `error`, `cancelled`, `interrupted`
- **No status**: `_node_to_msg` reads an older node as `done`

## Reconnect

A session load returns the saved rows, then `running_task` for the live run:

```
{ "type": "running_task",
  "data": { "session_id", "msg_id", "func_name", "execution_id", "status_version",
            "started_at", "display_params", "partial_tree": null,
            "stream_events", "status", "reason_code", "run_seq" } }
```

Frames are per session; `run_seq` orders them so a late frame cannot revive a closed slot.

Rows and live trees arrive as numbered `transcript_update` changes: a function run's tree as a `row_patch`, a model-issued call's tree in its reply's `callRoots`. A reloaded tab reads first and then follows the updates; a missed update or a text frame past the row's text makes it read once more.

## Worker restart

A new worker has nothing running, so startup reconciles sessions on a background thread, archived ones included: those written since the last finished pass, and those that pass left open ([the restart pass](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/reliability/worker-memory.html#restart)). There is no age threshold.

| Node | Result |
| --- | --- |
| `running` | Becomes `interrupted` |
| `cancelling`, owner dead | Becomes `cancelled` |
| `cancelling`, owner alive | Cancellation resumes |
| Has an open durable wait | Stays resumable |
| Execution still active | Left to the execution |

Continuing an execution after a restart is the job of `openprogram/runtime/execution/recovery/restart.py`, within `execution.auto_resume_window_seconds`.

## Implementation status

| Item | State |
| --- | --- |
| Built | Placeholders, tree persistence, saving a reply's partial text while it streams, status rendering, reconnect frames, restart reconcile |
| Not built | Subscribing to one message; a panel of active work across sessions |
