# 运行中状态恢复

| 发生的事 | 聊天里显示 | 来源 |
| --- | --- | --- |
| 刷新页面，worker 存活 | 运行中的行及其最新已保存状态，随后接收编号更新和按偏移放置的文字帧 | 已存的行、`running_task` 帧、`transcript_update` |
| 加载期间运行结束 | 运行槽关闭 | `running_task_clear` |
| Worker 重启 | 行标为 `interrupted`，琥珀色，并显示 “Worker restarted before this turn finished” | `reconcile_interrupted_runs` |
| 有待答问题或待批准项 | 重新绘制卡片 | `_replay_open_waits` |

## 占位

会运行一段时间的工作在开始前先写节点。状态放在 `metadata`；`Call` dataclass 不变。

| 产生者 | 写入位置 | 运行中保存 |
| --- | --- | --- |
| Assistant 回复 | `turn_lifecycle.py`：空的 `role="llm"` 行，带 `status="running"`、`worker_id`、`started_at` | 文本和思考块边流边存：每帧发出前经 `stream_tap` 的静默路径写入（`update_stream_progress`，不 fsync）；轮次结束时写完整行，取消时写入已有部分 |
| Agent 方法运行 | 占位 `<msg_id>_reply` | `execution/projections/dag.py` 的实时读取写入 `context_tree`、`status`、`last_update_at` |
| 模型调用的 Agent 方法 | `runtime_attach.py`：`display=runtime` 行 | 同一棵树 |
| `/task --async`、merge | `slash_actions.py`：attach 指针 | 卡片上的任务状态 |
| 子 Agent | `sub_agent_run.py` | 卡片上的任务状态 |

运行写入节点时重新读树，最多每秒一次。结束时再写一次，所以运行结束后刷新会显示已完成。

**状态值**

- **等待**: `pending`、`paused`
- **产出中**: `running`、`streaming`、`cancelling`
- **结束**: `completed`、`done`、`error`、`cancelled`、`interrupted`
- **无状态**: `_node_to_msg` 把旧节点读作 `done`

## 重连

加载会话先返回已保存的行，再为正在运行的任务发送 `running_task`：

```
{ "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" } }
```

帧按会话下发；`run_seq` 决定先后，迟到的帧不能让已关闭的运行槽复活。

行和实时树以编号的 `transcript_update` 变化到达：函数运行的树是 `row_patch`，模型调用的树在其回复的 `callRoots` 里。重新加载的标签页先读取，再跟随更新；漏掉一次更新，或文字帧超过行里已有的文字，就再读一次。

## Worker 重启

新 worker 没有在运行的工作，所以启动时在后台线程核对会话，包括已归档的：上一次完成的对账之后有写入的，以及那次对账结束时仍未结束的（[重启对账](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/reliability/worker-memory.zh.html#restart)）。没有时间阈值。

| 节点 | 结果 |
| --- | --- |
| `running` | 变为 `interrupted` |
| `cancelling`，所有者已退出 | 变为 `cancelled` |
| `cancelling`，所有者存活 | 继续取消 |
| 有未结束的持久等待 | 保持可恢复 |
| execution 仍在活动 | 交给 execution |

重启后继续某个 execution 由 `openprogram/runtime/execution/recovery/restart.py` 负责，受 `execution.auto_resume_window_seconds` 限制。

## 实现状态

| 项目 | 状态 |
| --- | --- |
| 已实现 | 占位、树持久化、回复流式过程中保存部分文本、状态渲染、重连帧、重启核对 |
| 未实现 | 订阅单条消息；跨会话的活跃工作面板 |
