跳转到主要内容
Lifecycle Handler 由 Flow.spec.lifecycle[] 声明,用有限事件枚举连接观察、上报和清理能力。它不是顶层 kind,当前仓库也不提供 lifecycle event dispatcher。 Handler 通过 requires.capability 描述能力,通过 requires.roleRun.spec.bindings 中选择 Skill 或 Adapter。外部文件、MCP、凭证等环境依赖仍由被绑定对象的 requirements[] 声明。

Handler 字段

字段要求Schema 语义
id必填匹配对象名正则的 handler id。
on必填有限 lifecycle event 枚举之一。
delivery必填bestEffortrequired
match可选stage selector。
match.stagesmatch 存在时必填至少一个、不重复的非空字符串。
requires.capability必填合法 capability。
requires.role必填非空字符串,对应 Run binding key。
timeoutSeconds必填大于等于 1 的整数。
retry.maxAttempts必填大于等于 1 的整数。
match 存在时,on 只能是 stage.startedstage.succeededstage.failed。当 onrun.succeededrun.failedrun.cancelled 时,delivery 必须为 bestEffort;required 收尾使用 run.finalizing 事件枚举固定为:
不得增加任意 hook 字符串。状态改变属于 stage,决策属于 Checkpoint,权限属于 Policy,外部能力属于 Adapter。

合法示例

对应 Run 必须把 stage-reporter 绑定到提供 huoban.dev/stageReporting 的 Skill 或 Adapter,例如:

当前 Preflight 行为

  • 检查 Flow 内重复 handler id。
  • 检查 match.stages[] 是否引用 Flow 中存在的 stage。
  • requires.role 查找锁定的 Run binding,并检查目标是否提供 requires.capability
  • 继续检查被绑定 Skill/Adapter 的 Requirement 和当前已实现的关联引用。
  • delivery: bestEffort 会把 binding 及其依赖标记为 optional;required 会参与 required gate。
  • run scope 检查所有 handler;stage scope 只选择 stage.* 事件且 selector 包含该 stage 的 handler。
timeoutSecondsretry.maxAttempts 目前只被 schema 和 explain 读取。没有 dispatcher 执行 timeout、retry 或 handler 调用。

Lifecycle Event Envelope

Lifecycle Event Envelope 是独立 fragment,不是 Huoban 对象。
字段要求说明
id必填非空事件 id。
type必填上述有限事件枚举。
occurredAt必填时间字符串;schema 不校验格式。
runRef必填local object reference。
stagestage event 必填;checkpoint event 可选;run event 禁止stage id。
checkpointRefcheckpoint event 必填;其他 event 禁止关联的 Checkpoint 引用。
artifactRefs可选Artifact 引用数组。
conditionTypes可选不重复的 condition type 字符串数组。
当前 CLI 不生成、投递或消费 Event Envelope;validate 可以直接校验 JSON/YAML envelope fragment。该 schema 只定义互操作形状。

Run invocation 状态

Run.status.lifecycleInvocations[] 记录 handler 调用状态:
字段要求说明
handlerId必填对应 Flow handler。
eventId必填对应事件 id。
phase必填PendingRunningSucceededFailed
attempts必填Pending 时必须为 0;其他 phase 至少为 1。
eventType必填有限事件枚举。
stagestage event 必填;checkpoint event 可选;run event 禁止stage id。
checkpointRefcheckpoint event 必填;其他 event 禁止local object reference。
lastAttemptTime非 Pending phase 必填时间字符串。
Pending 必须使用 attempts: 0 且不能有 lastAttemptTimeRunningSucceededFailed 必须至少有一次 attempt 并记录 lastAttemptTime 这是状态 schema,不代表当前仓库已经执行 invocation。

验证边界

  • huoban validate 会通过 Flow/Run schema 检查嵌入的 Handler 与 invocation,但不解析 binding;也可以校验没有 apiVersion/kind 的 Event Envelope fragment。
  • stage event 必须带 stage 且不能带 checkpointRef;checkpoint event 必须带 checkpointRef;run event 不能带二者。
  • Preflight 检查 handler binding、capability、selector 和依赖,不触发事件。
  • schema 限制 match 只能搭配三类 stage 事件,并限制三类 terminal Run event 只能使用 bestEffort
  • handler 适用于观察、上报和清理,不应承载状态变更、决策、授权或外部能力定义。

相邻概念边界

容易混淆精确边界
Flow stage承载工作与状态变化。
Lifecycle Handler对有限事件做观察、上报或清理集成。
Checkpoint承载人类或系统决策。
Policy承载副作用、审批与权限规则。
Adapter描述具体外部能力;Handler 只声明 capability 与 role。

常见错误

  • 写成顶层 kind: LifecycleHandler
  • 使用 afterStageonSuccess 等任意 hook 名称代替有限事件。
  • 把 Handler 的 requires 写成 Requirement target。
  • 忘记在 Run 中绑定 handler role。
  • 认为 bestEffort 会让 schema 错误通过;它只影响当前 Preflight 的 optional gate。
  • run.succeededrun.failedrun.cancelled 上声明 required delivery;必须完成的收尾应使用 run.finalizing
  • 声称当前 CLI 会分发事件、执行 retry 或调用 MCP reporting tool。