---
title: "Agent 写入超时或中断，从 pi-durable 看运行时能兜住哪一半 | 行开心的颠倒世界"
description: "UndoBench 54% 的故障试验因超时盲目重试造出重复副作用。pi-durable 能靠 unsafe 与 api.memo 兜住崩溃重跑，却救不了进程存活的 HTTP 超时。"
canonical: "https://xingkaixin.me/posts/agent-timeout-retry/"
language: "zh-CN"
---

# Agent 写入超时或中断，从 pi-durable 看运行时能兜住哪一半

UndoBench 54% 的故障试验因超时盲目重试造出重复副作用。pi-durable 能靠 unsafe 与 api.memo 兜住崩溃重跑，却救不了进程存活的 HTTP 超时。

2026年12月1日·1,400 字·4 分钟[AI 英文版](https://xingkaixin.me/en/posts/agent-timeout-retry/)

[Agent](https://xingkaixin.me/tags/Agent/)pi-durable幂等性运行时

![Agent 写入超时或中断，从 pi-durable 看运行时能兜住哪一半](https://xingkaixin.me/cover/agent-timeout-retry-800.2d3f9046.webp)

服务端已经建好了工单 781，发给 Agent 的确认回执却在半路丢了。Agent 等到超时，又提交了一次创建请求，同一件事最后多出一张工单。

这是 Tanmay Sah 和 Dolly Sah 在 2026 年 11 月 11 日 AI Engineer Code Summit [演讲](https://ai.engineer/code/2026/talks/online-undo-button)里举的例子。他们展示的 UndoBench 跑了 2,880 组试验，正常条件下成功率约 84%，把故障注进原本能做完的任务里，成功恢复的只有约 47%。两个比例分母不同，不能直接做减法，而把超时当失败去天真重试，在 **54%** 的故障试验里造出了重复副作用。

问题出在超时本身给不出结论。调用超时时，请求可能没到达服务端，可能还在处理，也可能已经写完只丢了回执；只要工具返回里没附上可查线索，模型为了继续任务，下一步往往只有再调一次接口。

把超时阈值调大只对单纯响应慢有效，回执丢在半路等再久也拿不到确认；在 Prompt 里写上“重试前先核对”也没有用，工具不提供查询入口，模型只能重读同一条报错。

代码改坏了还能用 `git revert` 退回，已经建出的工单和发出的通知却收不回来。[RFC 9110 第 9.2.2 节](https://www.rfc-editor.org/rfc/rfc9110.html#section-9.2.2)规定，客户端不应自动重试非幂等请求，除非确知接口允许安全重复，或确定原请求没执行。

## pi-durable 先兜住进程中断

在终端跑 `pi` 这类交互式 Coding Agent 时，工具执行到一半按两次 Ctrl+C，这轮调用就当没发生，人重新打开会话敲一句 `continue` 就能接着做。

Mario Zechner 在 2026 年 11 月 10 日的[演讲](https://www.youtube.com/watch?v=EreOxs9Vvrg)里对比过另一类场景：把 Agent 放进常驻 Slack 的机器人，或者部署到 15 分钟强制回收 V8 isolate 的 Cloudflare Durable Objects 上，没有开发者坐在终端前替它敲 `continue`。Pi 团队为此拆出了持久化运行时 [`@earendil-works/pi-durable`](https://github.com/earendil-works/pi/tree/747c217b81980206063a89e46645b362de7ac8b1/packages/durable)。

消息入口层遇到 Slack 重复投递时，会按 [`requestId`](https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/submissions.ts#L229-L237)（演讲里口头称为 idempotency key）去重，直接返回原有提交的运行句柄，不重开第二轮任务。

到了工具执行层，调度策略则保守得多。[`ToolTask`](https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/tool.ts#L204-L205) 在调用 `tool.execute()` 之前，会先调 `commitIntent()` 将 `checkpoint: { phase: "execute" }` 写入存储，并把重放策略 `replay` 默认设为 `"unsafe"`。

源码里，连内置的 `read`、`write`、`edit`、`bash`、`powershell` 五个编码工具都没有设成 `"safe"`，全包只有 `tool_search` 和 `codemode` 设了。

进程崩溃或被宿主回收后，`harness.resume()` 看到任务停在 `execute` 阶段且策略为 `"unsafe"`，就会拒绝静默重跑，直接把 `"Tool <name> was interrupted and may have partially run"` 写回上下文（见[中断处理源码](https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/tool.ts#L259-L263)），把未知状态的副作用诚实交给模型或人去处理。

## 崩溃后要重跑，幂等键必须先落盘

写工具如果确实要在崩溃后自动重跑，发远端请求前必须先把幂等键钉在本地任务记录里。[官方示例](https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/test/examples/40-tool-waits-for-webhook.ts#L101-L104)只写了两句核心代码：

```
const runId = await api.memo("ci.run", crypto.randomUUID(), ctx);
const build = await ci.trigger({ ref, idempotencyKey: runId });
```

[`api.memo`](https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/scheduler.ts#L2866-L2881) 采用先写者胜出策略，把首次生成的键持久存在 `TaskRecord.memos` 里；哪怕工具触发 CI 后崩溃重跑，`api.memo` 也会读回旧值，而不会生成新的 UUID。

同一笔创建或发送动作在重发时必须沿用同一个键，服务端才认得出这是同一笔请求。

只标 `replay: "safe"` 却不用 `api.memo` 存键，崩溃重跑从头执行会生成新键，远端照样再建出一张工单；反过来，客户端记好了键值，但服务端不校验，或者重发时参数变动、超过了键的有效期，传了也等于白传。

## 进程没挂、只是 HTTP 超时，运行时拦不住

即便写工具接好 `api.memo` 并标成 `"safe"`，`pi-durable` 仍救不了所有工单 781。`GenerationTask` 对模型请求内置了指数退避重试，但 `ToolTask` 的 `replay` 分支只在进程崩溃或实例回收后恢复 `phase: "execute"` 时才触发。

当宿主进程还活着、只是工具里的 HTTP 请求超时抛错，`ToolTask` 会捕获异常并直接按 [`tool_error`](https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/tool.ts#L571-L581) 结单，工具本身没有内置重试循环。

模型下一轮读到报错后若再调一次建单工具，生成的是全新的 `ToolCall` 和 `taskId`；因为 `TaskRecord.memos` 绑在单次任务记录上，新任务里的 `api.memo` 也会生成另一把键，远端依然会建出第二张工单。

**持久化运行时能拦住进程重启后的盲目重跑，却替不了工具层面对超时未决状态的幂等键传递与远端反查。**

![允许重放且已存键时，恢复同一任务会读回 K1；HTTP 超时后重新调用会创建新任务，未跨任务传键时会生成 K2](https://xingkaixin.me/posts/images/agent-timeout-retry/agent-timeout-retry-01.db5dc116.webp)

## 工具层要补上的另一半防线

补上另一半防线，工具层仍要做好三件事。超时后别只扔普通报错，要返回明确的“结果尚未确认”状态并附带可核对标识或跨轮幂等键。

同时给 Agent 配好只读反查工具，不过单次“未查到”不代表原请求没执行，可能还在服务端排队或写入尚未可见，立刻重发照样写重。

状态实在查不清时就要停下来等人工确认。在 `pi-durable` 的 `main` 分支（`Unreleased`）里，工具已能通过 `ToolPark` 与 `defineSignal` 主动挂起任务、释放进程，等外部信号或人工确认抵达再继续。

跨轮次与跨重启的幂等键和远端反查接通前，把写工具保持在默认的 `replay: "unsafe"` 更稳妥。上线前专门测试两种故障：服务端写完后把响应丢掉，以及工具执行到一半直接杀掉进程，确认下一轮或重启恢复后不会再多写一笔。

文中链接

1.  演讲https://ai.engineer/code/2026/talks/online-undo-button
2.  RFC 9110 第 9.2.2 节https://www.rfc-editor.org/rfc/rfc9110.html#section-9.2.2
3.  演讲https://www.youtube.com/watch?v=EreOxs9Vvrg
4.  @earendil-works/pi-durablehttps://github.com/earendil-works/pi/tree/747c217b81980206063a89e46645b362de7ac8b1/packages/durable
5.  requestIdhttps://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/submissions.ts#L229-L237
6.  ToolTaskhttps://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/tool.ts#L204-L205
7.  中断处理源码https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/tool.ts#L259-L263
8.  官方示例https://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/test/examples/40-tool-waits-for-webhook.ts#L101-L104
9.  api.memohttps://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/scheduler.ts#L2866-L2881
10.  tool\_errorhttps://github.com/earendil-works/pi/blob/747c217b81980206063a89e46645b362de7ac8b1/packages/durable/src/harness/tool.ts#L571-L581

放大查看插图

## 版权声明

作者

XingKaiXin

标题

Agent 写入超时或中断，从 pi-durable 看运行时能兜住哪一半

发布时间

2026年12月1日

文章链接

[https://xingkaixin.me/posts/agent-timeout-retry/](https://xingkaixin.me/posts/agent-timeout-retry/)

本作品采用 [CC BY-NC-ND 4.0 DEED](https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh-hans) 许可。

XingKaiXin

我的划线 · 0

展开导览

我的划线 · 0

## 目录

我的划线 · 0
