Workflow · 工作流编排、自动化与任务编排引擎
1. 介绍
1.1. 背景
当一个 AI 应用从"一问一答"走向"完成一项工作",它必然变成多步骤的:检索、调用工具、等待人工、处理异常、重试、汇总。此时决定成败的不再是单次模型调用的质量,而是整个执行过程的可靠性。
传统工作流引擎(Airflow、Camunda 等)面向的是确定性任务:输入确定、步骤确定、失败可重放。AI 工作流则引入了新的不确定性:
| 维度 | 传统工作流 | AI 工作流 |
|---|---|---|
| 步骤是否预定义 | 是 | 部分由模型在运行时决定 |
| 单步输出 | 结构化、可校验 | 自然语言,需 schema 约束 |
| 失败模式 | 明确异常 | 包括"输出格式正确但语义错误" |
| 循环 | 次数确定 | 可能不收敛,需预算上限 |
| 人工介入 | 少见 | 常见且必须 |
因此,AI 工作流需要的不是"另一个工作流引擎",而是在既有编排能力之上叠加上下文预算、结构化输出契约、预算护栏与人在回路节点。
1.2. 定义
Workflow(工作流编排):以代码或 DSL 预定义控制流,把多个任务按既定顺序、条件与并发关系组织起来,并提供状态持久化、重试、补偿与可观测能力的工程活动。
关键判据(R1,Anthropic《Building Effective Agents》官方表述):workflow 的控制流由代码预定义,agent 的控制流由 LLM 自主决定。这一句话是 Workflow 与 Agent 的分水岭,也是本文最重要的概念澄清点。
1.3. 在 AI Harness 体系中的定位
图 1-1|Workflow 在 Harness 六层能力模型中的定位
数据来源:基于本文分析绘制的示意图。
Workflow 在 Harness 六层能力模型中的定位为:主层 L3 编排与控制层,次层 L4 记忆与状态层、L5 评估与观测层(该映射为本文分析)。
| 层 | 在 Workflow 中的体现 | 关键机制 |
|---|---|---|
| L1 上下文工程 | 每步的上下文装配与预算控制 | 上下文预算、压缩策略 |
| L2 工具与执行 | 服务调用、脚本执行、沙箱 | Function Calling、MCP、沙箱执行器 |
| L3 编排与控制(主) | 控制流定义、并发、条件、容错、补偿 | DSL / DAG / 状态机 |
| L4 记忆与状态(次) | 工作流状态持久化、事件历史与重放 | Event History、Replay、Checkpoint |
| L5 评估与观测(次) | 执行轨迹追踪、耗时与成功率度量 | Trace、Web UI、指标看板 |
| L6 治理与安全 | 权限、密钥、审计、成本控制 | RBAC、Budget Guardrail、审计日志 |
瓶颈所在层:Workflow 的瓶颈在 L3 与 L4 的交界——即"状态持久化与可重放"。
AI 任务往往长耗时(数分钟到数月)、多外部依赖、中间状态复杂。若状态不可持久化、不可重放,则一次网络抖动就会导致整条链路重跑,成本与体验都不可接受。这正是持久执行(Durable Execution)范式在 AI 时代重新变得重要的原因。
价值:Workflow 是 L3 编排控制层的工程载体。没有它,Agent 只是一串不可靠的 API 调用;有了它,Agent 才能成为可交付、可运维、可审计的生产系统。
1.4. 编排模式与标准
1.4.1. CNCF Serverless Workflow DSL(R1)
来源:https://github.com/serverlessworkflow/specification
- 定位:"vendor-neutral, open-source, and entirely community-driven ecosystem tailored for defining and executing DSL-based workflows in the realm of Serverless technology"。
- 治理:2020 年 7 月 14 日成为 CNCF Sandbox 项目,归属 CNCF Serverless Working Group,位于 CNCF Landscape 的 "App Definition and Development"。
- 12 种默认任务类型:Call、Do、Emit、For、Fork、Listen、Raise、Run、Set、Switch、Try、Wait。
- 调度属性:
every(固定间隔)、cron(CRON 表达式)、after(完成后延迟重启)、on(事件驱动)。 - 执行转移:continue(继续下一任务)/ fault(未捕获错误,转入 faulted)/ end(优雅终止)。
- 数据四段式流转:Validate Workflow Input → Transform Workflow Input → Validate Task Input → Transform Task Input;校验失败抛
ValidationError(URI:https://serverlessworkflow.io/spec/1.0.0/errors/validation)。 - 密钥:运行时必须提供 secret;越权或不存在时抛
https://serverlessworkflow.io/spec/1.0.0/errors/authorization,对应 HTTP 403。 - 一致性:CTK(Conformance Test Kit,Gherkin 特征)+ 七种语言 SDK(.NET / Go / Java / PHP / Python / Rust / TypeScript)。
- 运行时:Apache KIE SonataFlow、Lemline、Synapse;配套 VS Code 扩展。
适配 AI 场景的意义(本文分析):Switch / For / Fork / Try / Wait 五类任务恰好覆盖 Agent 工作流中的路由、批量扇出、并行、容错与人工等待——这是该 DSL 可被直接复用于 AI 编排的原因。
1.4.2. Anthropic 五种工作流模式(R1)
来源:https://www.anthropic.com/research/building-effective-agents
| 模式 | 机制 | 适用场景 | 官方提示 |
|---|---|---|---|
| Prompt Chaining | 任务拆为固定序列,每步处理上一步输出,中间加程序化检查点(gate) | 任务可干净拆解 | 代价是延迟上升,但每步更简单更可靠 |
| Routing | 先分类再分派到专门的下游处理/提示/工具 | 类别差异明显、分开处理更优 | 官方例子:把简单/常见问题路由到 Claude Haiku 4.5,困难/罕见问题路由到 Claude Sonnet 4.5 |
| Parallelization | Sectioning(拆为独立子任务并行后汇总)与 Voting(同一任务多次运行取多样输出) | 可并行提速或需多视角校验 | 官方例子:一个模型处理用户查询、另一个并行做护栏筛查,"tends to perform better than having the same LLM call handle both guardrails and the core response" |
| Orchestrator-workers | 中央 LLM 在运行时动态拆解任务、分派给 worker、汇总结果 | 子任务无法预先确定 | 与 Parallelization 的关键差别是子任务不预先定义;应为 worker 设定预算以约束 fan-out |
| Evaluator-optimizer | 一个 LLM 生成、另一个评估反馈,循环迭代 | 有明确评估标准且迭代带来可测量提升 | — |
官方建议:"从简单方案开始,只在确有必要时增加复杂度。"
1.4.3. 持久执行(Durable Execution)范式(R2)
来源:Temporal 官网 https://temporal.io/
- 核心承诺:"Workflows automatically capture state at every step, and in the event of failure, can pick up exactly where they left off."
- 关键能力:持久状态、内建重试、任务队列、signal、timer、Event History 与 Replay、可观测的 Web UI。
- 典型用例:Agents / MCP / AI Pipelines、Human-in-the-Loop、Saga 补偿、长周期工作流(数天至数月)、订单履约、持久账本、CI/CD、DAG。
- 背书用户(官网自述):NVIDIA(跨云 GPU 集群管理)、Salesforce(单体迁移)、Twilio、Descript、OpenAI、HashiCorp;自述"100% open-source MIT-licensed"、"9 years in production"。
1.4.4. 生产级编排的可靠性模式(R2)
依据 Xgrid 案例总结:
- 每个 Activity 必须幂等:先问"这一步是否已执行完"再执行;用数据库唯一约束、upsert、外部 API 幂等令牌。
- 长耗时 Activity 必须发心跳(heartbeat):心跳停止则快速重试,而非等待执行超时;心跳载荷可带进度信息,重试可从上一检查点续跑。
- Worker 按负载类型分队列:CPU 密集型高核低并发、I/O 密集型高并发、GPU 工作负载独占队列限并发;用 HPA 绑定 schedule-to-start latency 自动扩缩。
- Payload 需加密:Temporal 默认明文存 payload,需三层安全栈——Data Converter 加密 payload、Codec Server 受控解密用于调试、mTLS 保护网络流量;密钥不出客户端基础设施。
- 自托管代价:Cassandra 集群、Elasticsearch 索引、分片架构、多区域故障转移与灾备;文中提到某企业花 6 个月解决分片限制最终仍需整体迁移。
1.4.5. AI 工作流特有的编排要求(本文分析)
- 每步输出必须结构化(schema),供下一步消费;
- 每个循环必须有步数/Token 预算上限;
- 人工介入点(human-in-the-loop)应作为一等节点而非旁路;
- 长周期任务需要持久状态与可重放轨迹(对应 L4 + L5)。
2. 名词解释
| 术语 | 英文/缩写 | 释义 |
|---|---|---|
| 工作流 | Workflow | 控制流由代码预定义的多步骤执行过程 |
| 智能体 | Agent | 控制流由 LLM 自主决定的执行单元 |
| 持久执行 | Durable Execution | 每步自动捕获状态、失败后从断点精确恢复的执行范式 |
| 事件历史 | Event History | 工作流执行过程的完整事件序列,可重放 |
| 重放 | Replay | 依据事件历史重建工作流状态的能力 |
| 活动 | Activity | 工作流中的单个可执行单元(如一次外部调用) |
| 幂等 | Idempotency | 同一操作重复执行不改变结果的性质 |
| 心跳 | Heartbeat | 长耗时 Activity 定期上报存活与进度的机制 |
| 信号 | Signal | 向运行中的工作流外部注入事件的机制 |
| 定时器 | Timer | 工作流内的时间等待与超时机制 |
| 补偿 | Saga / Compensation | 失败时按逆向操作撤销已完成步骤的模式 |
| 扇出 | Fan-out | 一个节点派生出多个并行子任务的模式 |
| 任务队列 | Task Queue | 按类型隔离 Worker 的消息队列 |
| 调度延迟 | Schedule-to-start Latency | 任务入队到开始执行的等待时长,用于自动扩缩 |
| 一致性测试套件 | CTK | Conformance Test Kit,Serverless Workflow 的合规测试集 |
| 域特定语言 | DSL | Domain-Specific Language |
| 提示链 | Prompt Chaining | 把任务拆为固定序列、逐步传递的工作流模式 |
| 路由 | Routing | 先分类再分派的工作流模式 |
| 主管-工人 | Orchestrator-workers | 中央模型运行时动态拆解并分派的编排模式 |
| 评估-优化 | Evaluator-optimizer | 生成与评估交替迭代的工作流模式 |
| 人工介入 | Human-in-the-loop | 流程中设置人工审阅与批准节点 |
3. 案例
3.1. Trendyol:父-子工作流编排跨区域数据库供给
3.1.1. 背景
Trendyol 是电商平台,服务 20 万商家、日订单 100 万+。其数据库供给流程原本是潜在的可靠性瓶颈:需要跨区域创建数据库并建立复制,而底层 Terraform 供给可能耗时很久。
3.1.2. 方案
依据 Temporal 官方案例(R1):
- 父 Workflow 编排整体流程,为每个区域派生子 Workflow。
- 子 Workflow 不阻塞等待,而是用"check database is created" Activity 轮询 +
workflow.sleep()等待——因为 Terraform 供给可能耗时很久,长阻塞会占用 Worker 资源。 - 两个区域就绪后,父 Workflow 建立复制。
3.1.3. 效果
依据 Temporal 官方案例:数据库供给从可靠性瓶颈变为韧性系统。具体收益来自两点:
- Event History + Replay 保证中断后从确切断点恢复,无需重跑已完成的步骤;
- Temporal Web UI 使原本的黑盒流程变为可观测。
该案例的工程启示是:长耗时外部供给不应阻塞 Worker,应改为"轮询 + sleep"的异步等待模式。
3.2. Coalition:用长周期工作流替换 cron job
3.2.1. 背景
Coalition 是网络保险公司,其续保(renewal)流程需要在保单到期前 120 天启动。改造前使用每日 cron job 处理,存在四类问题:
| 问题 | 表现 |
|---|---|
| 可见性 | 无法追踪单张保单发生了什么 |
| 可靠性 | 次日重试无法满足 SLA 法定义务("we have legal obligations to meet certain service-level agreements") |
| 可测试性 | 无法做端到端集成测试 |
| 可扩展性 | 单次执行内存吃紧 |
3.2.2. 方案
依据 Coalition 工程博客(R1):改用 Temporal 后,以单个长周期 Workflow + 多个子 Workflow + Activities 替代 cron job。
3.2.3. 效果
依据同一来源,改造后获得的能力:
- 内建指数退避重试;
- OpenTracing / Datadog / Sentry 集成的可观测性;
- 可单元测试与端到端集成测试;
- 模块化可复用。
该案例是"cron job → durable workflow"迁移的标准范式:当任务的可靠性影响到法定义务时,定时任务就不再够用。
3.3. ServiceNow:Agentic Workflows 的调用量放大效应
3.3.1. 背景
ServiceNow 在推进 agentic workflows 时发现,一旦客户启用智能体工作流,其调用量结构发生显著变化。
3.3.2. 方案
依据 ServiceNow FY2025 Q3 财报电话会(R2),公司把 agentic workflows 作为 Now Assist 的核心增长引擎,并据此调整容量与计价模型。
3.3.3. 效果
依据同一次电话会(R2,高管表述):一旦客户启用 agentic workflows,其 assist 调用量是原来的 5~12 倍(原文:"the volume you require for those agentic use cases is like 10x, 5x, 12x");Now Assist ACV 有望年内超过 5 亿美元。
该案例的工程含义是:工作流化会让模型调用量放大一个数量级,因此成本护栏(Budget Guardrail)与容量规划必须从设计阶段就介入,而不能事后补救。
4. 实践标准
4.1. AGENTS.md 规范
以下为 Workflow 方向的行业标准 AGENTS.md 完整可复制原文,体现工作流 DSL、持久执行引擎、任务队列、signal/timer、事件历史与重放、幂等与心跳、CTK 与 SDK 等专有工具链。
# AGENTS.md —— Workflow(工作流编排与任务编排引擎)
## 角色与边界
- 你是工作流编排智能体,负责设计、生成、校验与优化工作流定义。
- 你可以:生成工作流定义草案(DSL / DAG)、设计人工介入与补偿节点、
生成 CTK 风格的一致性用例、执行静态校验、生成可观测性埋点方案。
- 你不可以:在生产环境启动未审批的工作流、修改正在运行的工作流定义、
删除事件历史、跳过补偿逻辑、把无超时的人工等待写进定义。
- 判定原则:控制流能预定义的写成 workflow;必须由 LLM 运行时决定的才写成 agent。
## 环境假设
- 运行环境提供:工作流引擎(持久执行)、DSL 解析器与校验器、任务队列与 Worker、
signal / timer 能力、Event History 与 Replay、Web UI 与指标看板、密钥服务、审计日志。
- 引擎支持幂等键、心跳、指数退避重试与 Saga 补偿。
- 生产与非生产环境隔离;生产启动需审批。
## 上下文加载顺序(Context Budget)
1. 工作流目标与 SLA(常驻,不压缩)
2. 既有工作流定义与版本(避免重复造轮子)
3. 引擎能力与约束(任务类型、超时上限、并发限制)
4. 依赖服务的契约(接口 schema、幂等性、错误码)
5. 参考模式(Anthropic 五种模式 + 12 种任务类型映射)
6. 历史故障与复盘(按需加载)
## 工具契约
- DSL 校验器:定义变更必须通过静态校验与 CTK 风格用例。
- 引擎 API:启动、signal、终止、重放;生产启动需审批;终止须留补偿动作记录。
- 任务队列:按负载类型分队列(CPU / IO / GPU),不得混用。
- 密钥服务:密钥由密钥服务注入,不得写入工作流定义或日志。
- 脚本:静态校验、用例生成、重放验证、跑分必须调用 scripts/,不得用生成方式替代。
## 任务执行流程(SOP)
1. 识别控制流:能预定义的步骤 → workflow;需运行时决策的 → agent 节点。
2. 选模式:Prompt Chaining / Routing / Parallelization / Orchestrator-workers /
Evaluator-optimizer;优先简单方案,确有必要时才增加复杂度。
3. 定义任务:用 12 种任务类型映射(Switch=路由、For=批量、Fork=并行、
Try=容错、Wait=人工等待)。
4. 设契约:每步输出必须结构化(schema),供下一步消费。
5. 设护栏:每个循环设步数与 Token 预算上限;每个外部调用设超时与重试策略。
6. 设可靠性:每个 Activity 幂等;长耗时 Activity 发心跳;失败路径写补偿(Saga)。
7. 设人工节点:审阅/批准作为一等节点,配 timer 与超时升级路径。
8. 校验:静态校验 + 一致性用例 + 重放验证。
9. 上线:审批后发布;记录版本号与变更日志。
10. 观测:埋点(耗时、成功率、重试次数、schedule-to-start latency),形成看板。
## 验证与证据要求
- 证据包:工作流定义与版本、静态校验报告、一致性用例与结果、
重放验证记录、人工节点清单、补偿路径说明、变更日志。
- 每个外部调用必须说明:幂等方式、超时、重试策略、失败后的补偿动作。
- 每个人工节点必须说明:审阅人角色、超时时长、超期升级路径。
- 每个循环必须说明:步数上限、Token 上限、超限后的行为。
## 失败与升级策略
| 失败 | 处置 |
|---|---|
| 静态校验失败 | 回退定义,修正后重校验;不得绕过校验发布 |
| Activity 非幂等导致重复生效 | 立即停止,评估影响,补幂等键后重跑 |
| 长耗时 Activity 无心跳超时 | 改为带心跳的实现;重试从上一检查点续跑 |
| 人工节点超时 | 按既定路径升级提醒;不得自动放行 |
| 补偿失败 | 记录并升级;不得静默忽略 |
| 队列积压 | 按 schedule-to-start latency 扩缩;GPU 队列限并发 |
- 每个循环必须有步数上限与 Token 预算上限;超限即停并升级。
## 安全与合规红线
- 不得把密钥写入工作流定义、参数或日志(引擎对越权密钥抛 authorization 错误,HTTP 403)。
- 未加密的 payload 不得包含敏感数据(默认明文存储需配 Data Converter 加密)。
- 不得删除或篡改 Event History 与审计日志。
- 不得在生产环境启动未审批的工作流定义。
- 不得修改正在运行的工作流定义。
## 禁止事项
- 禁止把必须由 LLM 运行时决策的控制流硬编码为 workflow(反之亦然)。
- 禁止写出无超时的人工等待节点。
- 禁止省略补偿路径;禁止静默忽略补偿失败。
- 禁止混用 CPU / IO / GPU 队列。
- 禁止编造任务类型、错误码 URI、SDK 语言与版本号。
- 禁止使用 XX / XXX / ___ 等非标准占位符(统一用 [待填写] / [待核实])。
- 禁止使用 emoji 与署名。
## 输出格式
- 工作流定义:带语言标记的 DSL 代码块 + 版本号 + 变更日志。
- 节点清单:节点 / 任务类型 / 输入 schema / 输出 schema / 超时 / 重试 / 幂等键 / 补偿。
- 人工节点清单:节点 / 审阅角色 / 超时 / 升级路径。
- 数值带单位;范围用 ~ 连接;中文全角标点;中英文之间加空格。
## 评估与自检
- 九项自检:控制流归属正确 / 每步有 schema / 每循环有预算上限 /
每个 Activity 幂等 / 长耗时有心跳 / 人工节点有超时 / 补偿路径完整 /
编号与错误码可核实 / 信息缺口已声明。
- 每次定义变更后跑一致性用例与重放验证;失败视为缺陷。 4.2. SKILL.md 规范
以下为 Workflow 方向的行业标准 SKILL.md 完整可复制原文。
---
name: workflow-author
description: 设计、生成与校验工作流定义——选择编排模式、映射任务类型、设定结构化输出契约与预算护栏、设计人工介入节点与 Saga 补偿路径,并输出一致性用例与可观测性埋点方案。当用户要求"编排这个流程""把这个多步任务写成工作流""加一个人工审批节点""设计重试与补偿""工作流为什么卡住"时触发。
version: 1.0
created: 2026-09-12
---
# 工作流编排定义(Workflow Authoring)
## 适用场景
- 把多步骤任务编排为可持久执行的工作流。
- 为既有工作流增加人工介入、重试、补偿或并行分支。
- 工作流卡死或重试风暴的归因与修复。
- 生成一致性测试用例与可观测性埋点方案。
## 前置条件
- 已加载本方向 AGENTS.md;目标引擎与 DSL 版本已确定。
- 已明确 SLA / 超时要求与依赖服务契约(接口、幂等性、错误码)。
- 已明确人工介入节点的审批人与超时策略。
- 生产发布审批路径已确定。
## 输入
- 任务描述与步骤清单、目标引擎与 DSL 版本
- 依赖服务契约(schema、幂等、错误码)
- SLA / 超时 / 并发要求
- 可选:既有工作流定义、故障复盘记录
## 输出
- 工作流定义(DSL 代码块 + 版本号)
- 节点清单:任务类型 / 输入 schema / 输出 schema / 超时 / 重试 / 幂等键 / 补偿
- 人工节点清单:审阅角色 / 超时 / 升级路径
- 护栏说明:循环步数上限、Token 上限、并发限制
- 一致性用例与埋点方案
## 执行步骤
1. 判定控制流归属:可预定义 → workflow;需运行时决策 → agent 节点。
2. 选模式:优先 Prompt Chaining;需要分类用 Routing;需要提速或多视角用
Parallelization(sectioning / voting);子任务不可预知用 Orchestrator-workers;
有明确评估标准且迭代有效用 Evaluator-optimizer。
3. 映射任务类型:Switch(路由)、For(批量)、Fork(并行)、Try(容错)、
Wait(人工等待)、Listen(事件)、Run(脚本/服务)、Emit(事件输出)等。
4. 设契约:每步输入输出必须有 schema;无 schema 的步骤不予发布。
5. 设护栏:循环设步数与 Token 上限;外部调用设超时与指数退避重试。
6. 设可靠性:Activity 幂等(唯一约束 / upsert / 幂等令牌);长耗时发心跳并带进度。
7. 设人工节点:一等节点 + timer + 超时升级路径,禁止无超时等待。
8. 设补偿:为每个不可逆外部调用写 Saga 补偿动作。
9. 校验:静态校验 + 一致性用例 + 重放验证;通过后提交审批。
10. 埋点:耗时、成功率、重试次数、schedule-to-start latency。
## 质量标准(DoD)
- 控制流归属判定正确,未把 agent 问题硬编码为 workflow。
- 每个步骤有结构化 schema;每个循环有预算上限。
- 每个 Activity 幂等;长耗时 Activity 有心跳。
- 每个不可逆调用有补偿动作;补偿失败有升级路径。
- 每个人工节点有审阅角色与超时升级路径。
- 一致性用例与重放验证通过;证据包齐备。
## 常见失败与处理
| 失败 | 根因 | 处置 |
|---|---|---|
| 重试风暴 | 无退避或重试无上限 | 改指数退避 + 最大次数 + 熔断 |
| 重复生效 | Activity 非幂等 | 补幂等键 / 唯一约束 / upsert |
| 长任务误判失败 | 无心跳,等待执行超时 | 改心跳上报,重试从检查点续跑 |
| 卡在人工节点 | 无 timer 与升级 | 补 timer 与到期升级路径 |
| 补偿缺失 | 不可逆调用未设计补偿 | 补 Saga 补偿;无法补偿的步骤前置审批 |
| 队列互相拖累 | CPU/IO/GPU 混用同一队列 | 按负载类型分队列,GPU 独占限并发 |
## 示例
用户请求:把"合同审批"编排为工作流,需支持人工审批与超时升级。
执行:
1. 控制流可预定义 → workflow;"判断是否需法务介入"由模型决定 → 用 Switch + agent 节点。
2. 模式:Prompt Chaining + Routing(按合同金额路由到不同审批人)。
3. 任务映射:Set(装配上下文)→ Switch(金额分级)→ Wait(人工审批)→ Try(归档+通知)。
4. schema:每步输出 JSON,含 contract_id、amount、approver、decision、timestamp。
5. 护栏:审批循环最多 3 轮;单轮 Token 上限 8K。
6. 可靠性:归档步骤用 contract_id 作幂等键;通知步骤发心跳。
7. 人工节点:审批人角色 + 48 小时 timer + 到期升级至其上级。
8. 补偿:归档失败则删除已写记录并回滚状态;通知失败仅记录不回滚。
9. 校验:静态校验 + 3 条一致性用例(正常通过 / 驳回 / 超时升级)+ 重放验证。
约束:Wait 节点必须配 timer;不得无超时等待;生产发布需审批。 4.3. 落地检查清单
| # | 检查项 | 判定标准 | 频次 |
|---|---|---|---|
| 1 | 控制流归属正确 | 可预定义的为 workflow,运行时决策的为 agent | 每次设计 |
| 2 | 模式选择有依据 | 五种模式中选一种并说明理由;优先简单方案 | 每次设计 |
| 3 | 任务类型映射正确 | 使用引擎支持的 12 种任务类型,未臆造 | 每次设计 |
| 4 | 每步有 schema | 输入与输出均结构化,可被下一步消费 | 每次设计 |
| 5 | 循环有预算上限 | 步数上限与 Token 上限均已设定 | 每次设计 |
| 6 | Activity 幂等 | 每个写操作有幂等键或唯一约束 | 每次设计 |
| 7 | 长耗时有心跳 | 长任务定期上报进度,重试可续跑 | 每次设计 |
| 8 | 重试策略合理 | 指数退避 + 最大次数 + 熔断 | 每次设计 |
| 9 | 不可逆调用有补偿 | Saga 补偿动作已定义且可验证 | 每次设计 |
| 10 | 人工节点有超时 | 一等节点 + timer + 升级路径 | 每次设计 |
| 11 | 队列按负载分离 | CPU / IO / GPU 分队列,GPU 独占限并发 | 持续 |
| 12 | payload 已加密 | 敏感数据经 Data Converter 加密;mTLS 保护流量 | 持续 |
| 13 | 密钥未落定义 | 密钥由密钥服务注入,未写入定义或日志 | 每次设计 |
| 14 | 一致性用例通过 | CTK 风格用例与重放验证通过 | 每次变更 |
| 15 | 埋点完整 | 耗时、成功率、重试次数、schedule-to-start latency | 持续 |
5. 总结
Workflow 方向的核心判断是:AI 应用的可靠性来自编排层,而不是模型层。
五条最重要的结论:
- 分清 workflow 与 agent。Anthropic 官方判据是:控制流由代码预定义的叫 workflow,由 LLM 自主决定的叫 agent(R1)。混淆二者会导致"该确定的不确定、该灵活的不灵活"。
- 持久执行是 AI 工作流的刚需。长耗时、多外部依赖的 AI 任务若没有状态持久化与重放能力,一次抖动就会导致整链重跑。Temporal 的核心承诺——"capture state at every step... pick up exactly where they left off"——正是这一需求的答案。
- AI 工作流需要四条额外约束:结构化输出、循环预算上限、人工介入作为一等节点、持久状态与可重放轨迹。这四条是传统工作流不具备的。
- 可靠性模式可枚举:Activity 幂等、长任务心跳、队列按负载分离、payload 加密与 mTLS、Saga 补偿。这些不是优化项,而是上线前置条件。
- 工作流化会放大调用量一个数量级。ServiceNow 高管表述 agentic workflows 场景下 assist 调用量为原来的 5~12 倍(R2),因此成本护栏必须从设计阶段介入。
需要说明的信息缺口:当前没有 AI Agent 工作流的正式国际标准。CNCF Serverless Workflow 是通用工作流标准而非 AI 专用,但因其 12 种任务类型恰好覆盖路由、批量扇出、并行、容错与人工等待,可作为可复用的工程基线。
信息缺口声明
| # | 缺口 | 状态 |
|---|---|---|
| 1 | "AI Agent 工作流"的正式国际标准 | 暂无权威标准/规范(Serverless Workflow 为通用工作流标准,非 AI 专用) |
| 2 | Temporal 官方 SLA / 可用性承诺数值 | 无结果 |
| 3 | Serverless Workflow 当前稳定版本号与发布日期 | 规范版本以其官方仓库为准,本次未逐版核实 |
| 4 | Airflow / n8n / Camunda 等引擎的专项对比 | 未做专项检索(避免与软件工程组重复) |
| 5 | ServiceNow agentic workflows 5~12 倍调用量的统计口径 | 财报电话会高管表述,R2,无方法论披露 |
| 6 | Anthropic 五种模式的量化选型阈值 | 官方未给出阈值,仅给出适用判据 |
6. 参考资料
- Serverless Workflow Specification — CNCF / serverlessworkflow。https://github.com/serverlessworkflow/specification
- Building Effective Agents — Anthropic Research。https://www.anthropic.com/research/building-effective-agents
- Temporal — Durable Execution 官网。https://temporal.io/
- Trendyol Database Scaling with Temporal — Temporal 官方案例。https://temporal.io/resources/case-studies/trendyol-database-scaling-temporal
- Scaling Renewals Engine with Temporal Workflows — Coalition 工程博客。https://www.coalitioninc.com/fr-ca/blog/broker-education/scaling-renewals-engine-temporal-workflows
- ServiceNow FY2025 Q3 Earnings Call — The Globe and Mail。https://www.theglobeandmail.com/investing/markets/stocks/NOW/pressreleases/35791955/
- 生产级编排可靠性模式 — Xgrid。https://www.xgrid.co/resources?p=11487
- Apache KIE SonataFlow — Serverless Workflow 运行时实现。https://sonataflow.org/
- Lemline — Serverless Workflow 运行时实现。https://github.com/lemline/lemline
- Model Context Protocol Specification Changelog — MCP。https://modelcontextprotocol.io/specification/2025-06-18/changelog
- AGENTS.md — A simple, open format for guiding coding agents。https://agents.md/
- Celonis Process Intelligence / Forrester TEI(流程侧对照)— Celonis。https://www.celonis.com/news/press/celonis-customers-saw-payback-in-6-months-and-383-roi
Workflow · Workflow Orchestration, Automation and Task Orchestration Engine
1. Introduction
1.1. Background
When an AI application moves from "question-and-answer" to "getting a job done", it inevitably becomes multi-step: retrieval, tool calls, waiting on humans, handling exceptions, retries, and summarizing. At that point, success is no longer decided by the quality of a single model call, but by the reliability of the entire execution process.
Traditional workflow engines (Airflow, Camunda, etc.) are built for deterministic tasks: defined inputs, defined steps, and replayable failures. AI workflows introduce new kinds of uncertainty:
| Dimension | Traditional Workflow | AI Workflow |
|---|---|---|
| Are steps predefined? | Yes | Partly decided by the model at runtime |
| Per-step output | Structured, verifiable | Natural language, needs schema constraints |
| Failure modes | Explicit exceptions | Includes "correct format but wrong semantics" |
| Loops | Fixed count | May not converge; needs a budget cap |
| Human involvement | Rare | Common and necessary |
Therefore, what AI workflows need is not "another workflow engine", but context budgets, structured output contracts, budget guardrails, and human-in-the-loop nodes layered on top of existing orchestration capabilities.
1.2. Definition
Workflow: an engineering activity that predefines the control flow in code or DSL, organizes multiple tasks according to a fixed sequence, conditions, and concurrency relationships, and provides state persistence, retry, compensation, and observability.
Key criterion (R1, from Anthropic's official statement in Building Effective Agents): a workflow's control flow is predefined by code, while an agent's control flow is decided by the LLM itself. This single sentence is the dividing line between Workflow and Agent, and the most important conceptual clarification in this document.
1.3. Position in the AI Harness Architecture
图 1-1|Workflow 在 Harness 六层能力模型中的定位
数据来源:基于本文分析绘制的示意图。
In the Harness six-layer capability model, Workflow is positioned as follows: primary layer L3 (Orchestration and Control), with secondary layers L4 (Memory and State) and L5 (Evaluation and Observability) (this mapping is this document's own analysis).
| Layer | Representation in Workflow | Key Mechanism |
|---|---|---|
| L1 Context Engineering | Per-step context assembly and budget control | Context budget, compression strategies |
| L2 Tools and Execution | Service calls, script execution, sandbox | Function Calling, MCP, sandbox executor |
| L3 Orchestration and Control (Primary) | Control-flow definition, concurrency, conditions, fault tolerance, compensation | DSL / DAG / state machine |
| L4 Memory and State (Secondary) | Workflow state persistence, event history and replay | Event History, Replay, Checkpoint |
| L5 Evaluation and Observability (Secondary) | Execution trace tracking, latency and success-rate measurement | Trace, Web UI, metrics dashboard |
| L6 Governance and Security | Permissions, secrets, auditing, cost control | RBAC, Budget Guardrail, audit log |
Bottleneck layer: Workflow's bottleneck is at the boundary between L3 and L4 — that is, "state persistence and replayability".
AI tasks are often long-running (minutes to months), with many external dependencies and complex intermediate state. If state cannot be persisted or replayed, a single network hiccup causes the entire chain to re-execute, making both cost and experience unacceptable. This is exactly why the Durable Execution paradigm is becoming important again in the AI era.
Value: Workflow is the engineering vehicle for the L3 orchestration and control layer. Without it, an Agent is just a string of unreliable API calls; with it, an Agent can become a deliverable, operable, auditable production system.
1.4. Orchestration Patterns and Standards
1.4.1. CNCF Serverless Workflow DSL (R1)
Source: https://github.com/serverlessworkflow/specification
- Positioning: a "vendor-neutral, open-source, and entirely community-driven ecosystem tailored for defining and executing DSL-based workflows in the realm of Serverless technology".
- Governance: became a CNCF Sandbox project on July 14, 2020, under the CNCF Serverless Working Group, located in the "App Definition and Development" area of the CNCF Landscape.
- 12 default task types: Call, Do, Emit, For, Fork, Listen, Raise, Run, Set, Switch, Try, Wait.
- Scheduling attributes:
every(fixed interval),cron(CRON expression),after(delayed restart after completion),on(event-driven). - Execution transitions: continue (proceed to next task) / fault (uncaught error, moving to faulted) / end (graceful termination).
- Four-stage data flow: Validate Workflow Input → Transform Workflow Input → Validate Task Input → Transform Task Input; on validation failure, a
ValidationErroris thrown (URI:https://serverlessworkflow.io/spec/1.0.0/errors/validation). - Secrets: a secret must be provided at runtime; on unauthorized access or absence,
https://serverlessworkflow.io/spec/1.0.0/errors/authorizationis thrown, corresponding to HTTP 403. - Conformance: CTK (Conformance Test Kit, Gherkin features) plus seven-language SDKs (.NET / Go / Java / PHP / Python / Rust / TypeScript).
- Runtimes: Apache KIE SonataFlow, Lemline, Synapse; with a companion VS Code extension.
Relevance to AI scenarios (this document's analysis): the five task types Switch / For / Fork / Try / Wait precisely cover the routing, bulk fan-out, parallelism, fault tolerance, and human waiting found in Agent workflows — which is why this DSL can be directly reused for AI orchestration.
1.4.2. Anthropic's Five Workflow Patterns (R1)
Source: https://www.anthropic.com/research/building-effective-agents
| Pattern | Mechanism | Use Case | Official Tip |
|---|---|---|---|
| Prompt Chaining | Split the task into a fixed sequence; each step processes the previous step's output, with a programmatic checkpoint (gate) in between | Tasks that can be cleanly decomposed | The cost is higher latency, but each step is simpler and more reliable |
| Routing | Classify first, then dispatch to a specialized downstream processor/prompt/tool | Category differences are clear and separate handling is better | Official example: route simple/common questions to Claude Haiku 4.5 and hard/rare questions to Claude Sonnet 4.5 |
| Parallelization | Sectioning (split into independent subtasks, run in parallel, then merge) and Voting (run the same task multiple times to obtain diverse outputs) | Can be sped up through parallelism, or needs multi-perspective validation | Official example: one model handles the user query while another runs a parallel guardrail check; it "tends to perform better than having the same LLM call handle both guardrails and the core response" |
| Orchestrator-workers | A central LLM dynamically at runtime decomposes the task, dispatches to workers, and merges results | Sub-tasks cannot be predetermined | The key difference from Parallelization is that sub-tasks are not predefined; a budget should be set for workers to constrain fan-out |
| Evaluator-optimizer | One LLM generates, another evaluates and gives feedback, iterating in a loop | There is a clear evaluation criterion and iteration yields measurable improvement | — |
Official advice: "Start with a simple solution, and only increase complexity when it is truly necessary."
1.4.3. The Durable Execution Paradigm (R2)
Source: Temporal official site https://temporal.io/
- Core promise: "Workflows automatically capture state at every step, and in the event of failure, can pick up exactly where they left off."
- Key capabilities: persistent state, built-in retry, task queues, signal, timer, Event History and Replay, and an observable Web UI.
- Typical use cases: Agents / MCP / AI Pipelines, Human-in-the-Loop, Saga compensation, long-cycle workflows (days to months), order fulfillment, durable ledgers, CI/CD, DAG.
- Endorsed users (per the official site): NVIDIA (cross-cloud GPU cluster management), Salesforce (monolith migration), Twilio, Descript, OpenAI, HashiCorp; it describes itself as "100% open-source MIT-licensed" and "9 years in production".
1.4.4. Reliability Patterns for Production-Grade Orchestration (R2)
Summarized from the Xgrid case study:
- Every Activity must be idempotent: ask "has this step already completed?" before executing; use database unique constraints, upserts, and external API idempotency tokens.
- Long-running Activities must send heartbeats: if the heartbeat stops, retry quickly rather than waiting for the execution timeout; the heartbeat payload can carry progress information, so a retry can resume from the previous checkpoint.
- Workers are queued separately by load type: CPU-intensive work uses high-core/low-concurrency, I/O-intensive work uses high concurrency, and GPU workloads get a dedicated queue with limited concurrency; use HPA bound to schedule-to-start latency for autoscaling.
- Payloads must be encrypted: Temporal stores payloads in plaintext by default, so a three-layer security stack is needed — Data Converter encrypts payloads, a Codec Server does controlled decryption for debugging, and mTLS protects network traffic; keys never leave the client infrastructure.
- The cost of self-hosting: a Cassandra cluster, Elasticsearch indexing, a sharding architecture, multi-region failover, and disaster recovery; the article mentions one company that spent 6 months resolving sharding limits and still had to do a full migration in the end.
1.4.5. Orchestration Requirements Specific to AI Workflows (This Analysis)
- Each step's output must be structured (schema) so the next step can consume it;
- Every loop must have a step/Token budget cap;
- Human-in-the-loop intervention points should be first-class nodes rather than bypasses;
- Long-cycle tasks require persistent state and a replayable trace (corresponding to L4 + L5).
2. Glossary
| Term | English / Abbreviation | Definition |
|---|---|---|
| Workflow | Workflow | Multi-step execution process whose control flow is predefined by code |
| Agent | Agent | Execution unit whose control flow is decided autonomously by the LLM |
| Durable Execution | Durable Execution | Execution paradigm that captures state automatically at every step and resumes precisely from the breakpoint after a failure |
| Event History | Event History | Complete sequence of events during workflow execution, replayable |
| Replay | Replay | The ability to rebuild workflow state from the event history |
| Activity | Activity | A single executable unit in a workflow (e.g., an external call) |
| Idempotency | Idempotency | The property that repeating the same operation does not change the result |
| Heartbeat | Heartbeat | Mechanism by which a long-running Activity periodically reports liveness and progress |
| Signal | Signal | Mechanism for injecting external events into a running workflow |
| Timer | Timer | Time-wait and timeout mechanism within a workflow |
| Compensation | Saga / Compensation | Pattern that undoes completed steps with reverse operations on failure |
| Fan-out | Fan-out | Pattern where one node spawns multiple parallel sub-tasks |
| Task Queue | Task Queue | Message queue that isolates Workers by type |
| Schedule-to-start Latency | Schedule-to-start Latency | Wait time from task enqueuement to execution start, used for autoscaling |
| Conformance Test Kit | CTK | Conformance Test Kit, the compliance test suite for Serverless Workflow |
| Domain-Specific Language | DSL | Domain-Specific Language |
| Prompt Chaining | Prompt Chaining | Workflow pattern that splits a task into a fixed sequence and passes it step by step |
| Routing | Routing | Workflow pattern that classifies first, then dispatches |
| Orchestrator-workers | Orchestrator-workers | Orchestration pattern where a central model dynamically decomposes and dispatches at runtime |
| Evaluator-optimizer | Evaluator-optimizer | Workflow pattern alternating generation and evaluation in iteration |
| Human-in-the-loop | Human-in-the-loop | Human review and approval nodes set in the process |
3. Case Studies
3.1. Trendyol: Parent-Child Workflow Orchestration for Cross-Region Database Provisioning
3.1.1. Background
Trendyol is an e-commerce platform serving 200,000 merchants with over 1 million daily orders. Its database provisioning flow was originally a potential reliability bottleneck: databases had to be created across regions with replication set up, while the underlying Terraform provisioning could take a long time.
3.1.2. Solution
Based on Temporal's official case study (R1):
- A parent Workflow orchestrates the overall flow and spawns a child Workflow for each region.
- The child Workflow does not block waiting; instead it uses a "check database is created" Activity for polling plus
workflow.sleep()to wait — because Terraform provisioning can take a long time and long blocking would tie up Worker resources. - Once both regions are ready, the parent Workflow establishes replication.
3.1.3. Results
Based on Temporal's official case study: database provisioning went from a reliability bottleneck to a resilient system. The concrete gains come from two points:
- Event History + Replay guarantee resume from the exact breakpoint after an interruption, without re-running completed steps;
- Temporal Web UI turns what was a black-box process into something observable.
The engineering lesson of this case is: long-running external provisioning should not block Workers; it should use the "polling + sleep" asynchronous waiting pattern instead.
3.2. Coalition: Replacing cron Jobs with Long-Cycle Workflows
3.2.1. Background
Coalition is a cyber insurance company whose renewal flow needs to start 120 days before a policy expires. Before the change, it used daily cron jobs, which had four kinds of problems:
| Problem | Manifestation |
|---|---|
| Visibility | Unable to trace what happened to an individual policy |
| Reliability | Next-day retry cannot meet legally required SLAs ("we have legal obligations to meet certain service-level agreements") |
| Testability | Unable to run end-to-end integration tests |
| Scalability | Memory pressure during a single execution |
3.2.2. Solution
Based on the Coalition engineering blog (R1): after switching to Temporal, a single long-cycle Workflow + multiple child Workflows + Activities replaced the cron jobs.
3.2.3. Results
Based on the same source, capabilities gained after the change:
- Built-in exponential backoff retry;
- Observability integrated with OpenTracing / Datadog / Sentry;
- Unit-testable and end-to-end integration testable;
- Modular and reusable.
This case is the standard paradigm for the "cron job → durable workflow" migration: when a task's reliability affects legal obligations, scheduled jobs are no longer sufficient.
3.3. ServiceNow: The Volume-Amplification Effect of Agentic Workflows
3.3.1. Background
When pushing agentic workflows, ServiceNow found that once customers enabled intelligent-agent workflows, their call-volume structure changed significantly.
3.3.2. Solution
Based on ServiceNow's FY2025 Q3 earnings call (R2), the company treats agentic workflows as the core growth engine for Now Assist and has adjusted capacity and pricing models accordingly.
3.3.3. Results
Based on the same earnings call (R2, executive statements): once customers enable agentic workflows, their assist call volume is 5~12 times what it was (original quote: "the volume you require for those agentic use cases is like 10x, 5x, 12x"); Now Assist ACV is expected to exceed 500 million USD within the year.
The engineering implication of this case is: workflow-ization amplifies model call volume by an order of magnitude, so cost guardrails (Budget Guardrail) and capacity planning must be involved from the design stage, not patched in afterward.
4. Practical Standards
4.1. AGENTS.md Specification
The following is the complete, copy-ready industry-standard AGENTS.md text for the Workflow direction, reflecting the specialized toolchain of workflow DSLs, durable execution engines, task queues, signal/timer, event history and replay, idempotency and heartbeat, CTK and SDKs.
# AGENTS.md —— Workflow(工作流编排与任务编排引擎)
## 角色与边界
- 你是工作流编排智能体,负责设计、生成、校验与优化工作流定义。
- 你可以:生成工作流定义草案(DSL / DAG)、设计人工介入与补偿节点、
生成 CTK 风格的一致性用例、执行静态校验、生成可观测性埋点方案。
- 你不可以:在生产环境启动未审批的工作流、修改正在运行的工作流定义、
删除事件历史、跳过补偿逻辑、把无超时的人工等待写进定义。
- 判定原则:控制流能预定义的写成 workflow;必须由 LLM 运行时决定的才写成 agent。
## 环境假设
- 运行环境提供:工作流引擎(持久执行)、DSL 解析器与校验器、任务队列与 Worker、
signal / timer 能力、Event History 与 Replay、Web UI 与指标看板、密钥服务、审计日志。
- 引擎支持幂等键、心跳、指数退避重试与 Saga 补偿。
- 生产与非生产环境隔离;生产启动需审批。
## 上下文加载顺序(Context Budget)
1. 工作流目标与 SLA(常驻,不压缩)
2. 既有工作流定义与版本(避免重复造轮子)
3. 引擎能力与约束(任务类型、超时上限、并发限制)
4. 依赖服务的契约(接口 schema、幂等性、错误码)
5. 参考模式(Anthropic 五种模式 + 12 种任务类型映射)
6. 历史故障与复盘(按需加载)
## 工具契约
- DSL 校验器:定义变更必须通过静态校验与 CTK 风格用例。
- 引擎 API:启动、signal、终止、重放;生产启动需审批;终止须留补偿动作记录。
- 任务队列:按负载类型分队列(CPU / IO / GPU),不得混用。
- 密钥服务:密钥由密钥服务注入,不得写入工作流定义或日志。
- 脚本:静态校验、用例生成、重放验证、跑分必须调用 scripts/,不得用生成方式替代。
## 任务执行流程(SOP)
1. 识别控制流:能预定义的步骤 → workflow;需运行时决策的 → agent 节点。
2. 选模式:Prompt Chaining / Routing / Parallelization / Orchestrator-workers /
Evaluator-optimizer;优先简单方案,确有必要时才增加复杂度。
3. 定义任务:用 12 种任务类型映射(Switch=路由、For=批量、Fork=并行、
Try=容错、Wait=人工等待)。
4. 设契约:每步输出必须结构化(schema),供下一步消费。
5. 设护栏:每个循环设步数与 Token 预算上限;每个外部调用设超时与重试策略。
6. 设可靠性:每个 Activity 幂等;长耗时 Activity 发心跳;失败路径写补偿(Saga)。
7. 设人工节点:审阅/批准作为一等节点,配 timer 与超时升级路径。
8. 校验:静态校验 + 一致性用例 + 重放验证。
9. 上线:审批后发布;记录版本号与变更日志。
10. 观测:埋点(耗时、成功率、重试次数、schedule-to-start latency),形成看板。
## 验证与证据要求
- 证据包:工作流定义与版本、静态校验报告、一致性用例与结果、
重放验证记录、人工节点清单、补偿路径说明、变更日志。
- 每个外部调用必须说明:幂等方式、超时、重试策略、失败后的补偿动作。
- 每个人工节点必须说明:审阅人角色、超时时长、超期升级路径。
- 每个循环必须说明:步数上限、Token 上限、超限后的行为。
## 失败与升级策略
| 失败 | 处置 |
|---|---|
| 静态校验失败 | 回退定义,修正后重校验;不得绕过校验发布 |
| Activity 非幂等导致重复生效 | 立即停止,评估影响,补幂等键后重跑 |
| 长耗时 Activity 无心跳超时 | 改为带心跳的实现;重试从上一检查点续跑 |
| 人工节点超时 | 按既定路径升级提醒;不得自动放行 |
| 补偿失败 | 记录并升级;不得静默忽略 |
| 队列积压 | 按 schedule-to-start latency 扩缩;GPU 队列限并发 |
- 每个循环必须有步数上限与 Token 预算上限;超限即停并升级。
## 安全与合规红线
- 不得把密钥写入工作流定义、参数或日志(引擎对越权密钥抛 authorization 错误,HTTP 403)。
- 未加密的 payload 不得包含敏感数据(默认明文存储需配 Data Converter 加密)。
- 不得删除或篡改 Event History 与审计日志。
- 不得在生产环境启动未审批的工作流定义。
- 不得修改正在运行的工作流定义。
## 禁止事项
- 禁止把必须由 LLM 运行时决策的控制流硬编码为 workflow(反之亦然)。
- 禁止写出无超时的人工等待节点。
- 禁止省略补偿路径;禁止静默忽略补偿失败。
- 禁止混用 CPU / IO / GPU 队列。
- 禁止编造任务类型、错误码 URI、SDK 语言与版本号。
- 禁止使用 XX / XXX / ___ 等非标准占位符(统一用 [待填写] / [待核实])。
- 禁止使用 emoji 与署名。
## 输出格式
- 工作流定义:带语言标记的 DSL 代码块 + 版本号 + 变更日志。
- 节点清单:节点 / 任务类型 / 输入 schema / 输出 schema / 超时 / 重试 / 幂等键 / 补偿。
- 人工节点清单:节点 / 审阅角色 / 超时 / 升级路径。
- 数值带单位;范围用 ~ 连接;中文全角标点;中英文之间加空格。
## 评估与自检
- 九项自检:控制流归属正确 / 每步有 schema / 每循环有预算上限 /
每个 Activity 幂等 / 长耗时有心跳 / 人工节点有超时 / 补偿路径完整 /
编号与错误码可核实 / 信息缺口已声明。
- 每次定义变更后跑一致性用例与重放验证;失败视为缺陷。 4.2. SKILL.md Specification
The following is the complete, copy-ready industry-standard SKILL.md text for the Workflow direction.
---
name: workflow-author
description: 设计、生成与校验工作流定义——选择编排模式、映射任务类型、设定结构化输出契约与预算护栏、设计人工介入节点与 Saga 补偿路径,并输出一致性用例与可观测性埋点方案。当用户要求"编排这个流程""把这个多步任务写成工作流""加一个人工审批节点""设计重试与补偿""工作流为什么卡住"时触发。
version: 1.0
created: 2026-09-12
---
# 工作流编排定义(Workflow Authoring)
## 适用场景
- 把多步骤任务编排为可持久执行的工作流。
- 为既有工作流增加人工介入、重试、补偿或并行分支。
- 工作流卡死或重试风暴的归因与修复。
- 生成一致性测试用例与可观测性埋点方案。
## 前置条件
- 已加载本方向 AGENTS.md;目标引擎与 DSL 版本已确定。
- 已明确 SLA / 超时要求与依赖服务契约(接口、幂等性、错误码)。
- 已明确人工介入节点的审批人与超时策略。
- 生产发布审批路径已确定。
## 输入
- 任务描述与步骤清单、目标引擎与 DSL 版本
- 依赖服务契约(schema、幂等、错误码)
- SLA / 超时 / 并发要求
- 可选:既有工作流定义、故障复盘记录
## 输出
- 工作流定义(DSL 代码块 + 版本号)
- 节点清单:任务类型 / 输入 schema / 输出 schema / 超时 / 重试 / 幂等键 / 补偿
- 人工节点清单:审阅角色 / 超时 / 升级路径
- 护栏说明:循环步数上限、Token 上限、并发限制
- 一致性用例与埋点方案
## 执行步骤
1. 判定控制流归属:可预定义 → workflow;需运行时决策 → agent 节点。
2. 选模式:优先 Prompt Chaining;需要分类用 Routing;需要提速或多视角用
Parallelization(sectioning / voting);子任务不可预知用 Orchestrator-workers;
有明确评估标准且迭代有效用 Evaluator-optimizer。
3. 映射任务类型:Switch(路由)、For(批量)、Fork(并行)、Try(容错)、
Wait(人工等待)、Listen(事件)、Run(脚本/服务)、Emit(事件输出)等。
4. 设契约:每步输入输出必须有 schema;无 schema 的步骤不予发布。
5. 设护栏:循环设步数与 Token 上限;外部调用设超时与指数退避重试。
6. 设可靠性:Activity 幂等(唯一约束 / upsert / 幂等令牌);长耗时发心跳并带进度。
7. 设人工节点:一等节点 + timer + 超时升级路径,禁止无超时等待。
8. 设补偿:为每个不可逆外部调用写 Saga 补偿动作。
9. 校验:静态校验 + 一致性用例 + 重放验证;通过后提交审批。
10. 埋点:耗时、成功率、重试次数、schedule-to-start latency。
## 质量标准(DoD)
- 控制流归属判定正确,未把 agent 问题硬编码为 workflow。
- 每个步骤有结构化 schema;每个循环有预算上限。
- 每个 Activity 幂等;长耗时 Activity 有心跳。
- 每个不可逆调用有补偿动作;补偿失败有升级路径。
- 每个人工节点有审阅角色与超时升级路径。
- 一致性用例与重放验证通过;证据包齐备。
## 常见失败与处理
| 失败 | 根因 | 处置 |
|---|---|---|
| 重试风暴 | 无退避或重试无上限 | 改指数退避 + 最大次数 + 熔断 |
| 重复生效 | Activity 非幂等 | 补幂等键 / 唯一约束 / upsert |
| 长任务误判失败 | 无心跳,等待执行超时 | 改心跳上报,重试从检查点续跑 |
| 卡在人工节点 | 无 timer 与升级 | 补 timer 与到期升级路径 |
| 补偿缺失 | 不可逆调用未设计补偿 | 补 Saga 补偿;无法补偿的步骤前置审批 |
| 队列互相拖累 | CPU/IO/GPU 混用同一队列 | 按负载类型分队列,GPU 独占限并发 |
## 示例
用户请求:把"合同审批"编排为工作流,需支持人工审批与超时升级。
执行:
1. 控制流可预定义 → workflow;"判断是否需法务介入"由模型决定 → 用 Switch + agent 节点。
2. 模式:Prompt Chaining + Routing(按合同金额路由到不同审批人)。
3. 任务映射:Set(装配上下文)→ Switch(金额分级)→ Wait(人工审批)→ Try(归档+通知)。
4. schema:每步输出 JSON,含 contract_id、amount、approver、decision、timestamp。
5. 护栏:审批循环最多 3 轮;单轮 Token 上限 8K。
6. 可靠性:归档步骤用 contract_id 作幂等键;通知步骤发心跳。
7. 人工节点:审批人角色 + 48 小时 timer + 到期升级至其上级。
8. 补偿:归档失败则删除已写记录并回滚状态;通知失败仅记录不回滚。
9. 校验:静态校验 + 3 条一致性用例(正常通过 / 驳回 / 超时升级)+ 重放验证。
约束:Wait 节点必须配 timer;不得无超时等待;生产发布需审批。 4.3. Landing Checklist
| # | Check Item | Pass Criteria | Frequency |
|---|---|---|---|
| 1 | Correct control-flow ownership | Predefinable steps are workflows, runtime-decided steps are agents | Every design |
| 2 | Pattern choice is justified | Pick one of the five patterns and state the reason; prefer simple solutions | Every design |
| 3 | Correct task-type mapping | Uses one of the engine's 12 task types, none fabricated | Every design |
| 4 | Every step has a schema | Both input and output are structured and consumable by the next step | Every design |
| 5 | Loops have a budget cap | Both a step cap and a Token cap are set | Every design |
| 6 | Activity idempotency | Every write has an idempotency key or unique constraint | Every design |
| 7 | Heartbeat for long-running tasks | Long tasks report progress periodically; retries can resume | Every design |
| 8 | Reasonable retry strategy | Exponential backoff + max attempts + circuit breaker | Every design |
| 9 | Irreversible calls have compensation | Saga compensation actions defined and verifiable | Every design |
| 10 | Human nodes have timeouts | First-class node + timer + escalation path | Every design |
| 11 | Queues separated by load | CPU / IO / GPU on separate queues, GPU dedicated with limited concurrency | Continuous |
| 12 | Payloads encrypted | Sensitive data encrypted via Data Converter; mTLS protects traffic | Continuous |
| 13 | Secrets not in definitions | Secrets injected by a secrets service, not written into definitions or logs | Every design |
| 14 | Conformance cases pass | CTK-style cases and replay verification pass | Every change |
| 15 | Complete instrumentation | Latency, success rate, retry count, schedule-to-start latency | Continuous |
5. Summary
The core judgment of the Workflow direction is: the reliability of AI applications comes from the orchestration layer, not the model layer.
The five most important conclusions:
- Distinguish workflow from agent. Anthropic's official criterion: control flow predefined by code is a workflow, control flow decided autonomously by the LLM is an agent (R1). Confusing the two leads to "uncertain where it should be certain, and rigid where it should be flexible".
- Durable execution is a hard requirement for AI workflows. Long-running AI tasks with many external dependencies, without state persistence and replay, will re-run the whole chain on a single hiccup. Temporal's core promise — "capture state at every step... pick up exactly where they left off" — is precisely the answer to this need.
- AI workflows need four extra constraints: structured output, loop budget caps, human-in-the-loop as a first-class node, and persistent state with a replayable trace. These four are not present in traditional workflows.
- Reliability patterns are enumerable: Activity idempotency, heartbeats for long tasks, queues separated by load, payload encryption with mTLS, and Saga compensation. These are not optimization items; they are prerequisites for going live.
- Workflow-ization amplifies call volume by an order of magnitude. A ServiceNow executive stated that assist call volume in agentic-workflow scenarios is 5~12 times what it was (R2), so cost guardrails must be involved from the design stage.
Information gaps to note: there is currently no formal international standard for AI Agent workflows. CNCF Serverless Workflow is a general workflow standard rather than one specific to AI, but because its 12 task types happen to cover routing, bulk fan-out, parallelism, fault tolerance, and human waiting, it can serve as a reusable engineering baseline.
Information Gap Statement
| # | Gap | Status |
|---|---|---|
| 1 | Formal international standard for "AI Agent workflows" | No authoritative standard/specification yet (Serverless Workflow is a general workflow standard, not AI-specific) |
| 2 | Temporal official SLA / availability commitment figures | No results |
| 3 | Serverless Workflow current stable version and release date | Specification version follows the official repository; not verified release-by-release this time |
| 4 | Dedicated comparison of Airflow / n8n / Camunda engines | No dedicated search performed (to avoid duplication with the software engineering group) |
| 5 | Statistical basis for ServiceNow's 5~12x agentic-workflow call volume | Executive statement on the earnings call, R2, no methodology disclosed |
| 6 | Quantitative selection thresholds for Anthropic's five patterns | Official thresholds not given; only applicability criteria |
6. References
- Serverless Workflow Specification — CNCF / serverlessworkflow. https://github.com/serverlessworkflow/specification
- Building Effective Agents — Anthropic Research. https://www.anthropic.com/research/building-effective-agents
- Temporal — Durable Execution official site. https://temporal.io/
- Trendyol Database Scaling with Temporal — Temporal official case study. https://temporal.io/resources/case-studies/trendyol-database-scaling-temporal
- Scaling Renewals Engine with Temporal Workflows — Coalition engineering blog. https://www.coalitioninc.com/fr-ca/blog/broker-education/scaling-renewals-engine-temporal-workflows
- ServiceNow FY2025 Q3 Earnings Call — The Globe and Mail. https://www.theglobeandmail.com/investing/markets/stocks/NOW/pressreleases/35791955/
- Production-grade orchestration reliability patterns — Xgrid. https://www.xgrid.co/resources?p=11487
- Apache KIE SonataFlow — Serverless Workflow runtime implementation. https://sonataflow.org/
- Lemline — Serverless Workflow runtime implementation. https://github.com/lemline/lemline
- Model Context Protocol Specification Changelog — MCP. https://modelcontextprotocol.io/specification/2025-06-18/changelog
- AGENTS.md — A simple, open format for guiding coding agents. https://agents.md/
- Celonis Process Intelligence / Forrester TEI (process-side reference) — Celonis. https://www.celonis.com/news/press/celonis-customers-saw-payback-in-6-months-and-383-roi