Workflow SDK 5 取消支持:长时间 AI 任务需要“中止契约”
长时间 AI 任务的难点不只是等待,而是取消之后还能解释什么仍在运行。用户取消了,模型调用可能继续;超时发生了,上传仍可能完成;配额超了,重试又继续花钱。
Vercel 在 2026 年 6 月 16 日宣布 Workflow SDK 5 beta 支持 AbortController 和 AbortSignal 跨 workflow 与 step 边界工作。随着 Sandbox 运行时间变长,agent 和 durable job 也需要更精确地停止。
发生了什么
workflow 可以创建 AbortController,将 signal 传给 step,并在 timeout、race、hook 或 quota monitor 触发时调用 abort()。文档说明 signal 可跨 suspension、deterministic replay 和单独 invocation 保持 durable。
取消是协作式的。step 必须把 signal 传给 fetch 或支持它的 API,调用 signal.throwIfAborted(),或检查 signal.aborted。
为什么重要
OCR、报表生成、浏览器自动化、多模型 agent、测试流水线都受益于长运行窗口。但没有取消契约,就会出现浪费 token、额外 API 调用、资源锁和模糊状态。
社区关于 production-ready、cancel/kill/cleanup 的问题说明,真正缺口是运行模型。
运维影响
timeout 可以变成产品策略;并行任务可以取消输家;abort error 跳过 retry,有助于区分主动取消和故障。
检查清单
定义 user cancel、timeout、admin stop、quota exceeded、parent request close。
高成本 step 接收 signal 并传给支持的 API。
区分 run.cancel() 与 AbortSignal。
保存 cancelled_by_user、timed_out、quota_exceeded。
测试 partial upload、外部 job、邮件与 webhook 清理。
风险
Workflow SDK 5 仍是 beta/pre-release。关键业务应小规模验证、锁定版本并准备回滚。
Signal 不能撤销远端副作用,也不能停止忽略 signal 的代码。
现在该做什么
先画出取消边界:哪些步骤花钱,哪些修改外部状态,哪些不能重试。再决定是否接入 SDK。
30 分钟导入检查
✓ 取消原因有产品化命名
✓ 高成本步骤接收或检查 signal
✓ abort 与 retry 策略不冲突
✓ 外部副作用具备 idempotency key
✓ 监控区分 cancelled 与 failed
const controller = new AbortController();
const result = await Promise.race([
expensiveStep(controller.signal),
sleep("30s").then(() => null),
]);
if (result === null) controller.abort();
来源与参考
- Vercel Changelog: Workflow SDK now supports inflight cancellation
- Workflow SDK v5 docs: Cancellation
- MDN: AbortController
- MDN: AbortSignal.any()
- Vercel Changelog: Vercel Sandbox can now run for up to 24 hours
- Reddit r/vercel: Is Workflow Development Kit production ready?
- Reddit r/vercel: Workflow devkit cleanup/cancel discussion
- Reddit r/vercel: 30 minute Functions and 24 hour Sandbox