Description 1. Summary
当前系统已具备 ReAct 主链路、Tool Facts、Todo、SubAgent 与 RunState phase,但“任务完成”仍缺少 runtime 级确定性闭环。
本提案将完成判定升级为“双环模型”:内环由 ReAct 自驱执行,外环由 Runtime 强制验证流水线(lint/build/test/typecheck)兜底。
同时引入 Runtime Hook 扩展层,借鉴 Claude Code Hook 思路,但采用强类型、可测试、可控失败策略的 Go 内部机制。
目标是让系统从“会调用工具”升级为“对代码任务结果负责到底”。
2. Background and Problems
2.1 Observed Facts
completion gate 已存在,但当前阻断条件仅覆盖 pending todo / unverified write / post-execute closure。
关联文件:internal/runtime/controlplane/completion.go
RunState 已有 plan/execute/verify,但 verify 目前未绑定 runtime 强制验证流水线。
关联文件:internal/runtime/controlplane/phase.go, internal/runtime/run.go
ToolExecutionFacts 已支持 WorkspaceWrite 与 VerificationPerformed/Passed/Scope,但主要依赖工具返回事实,不是 runtime 硬判定。
关联文件:internal/tools/types.go, internal/tools/bash/tool.go, internal/runtime/turn_control.go
StopReason 当前缺少“验证失败/重试耗尽/闭环熔断”语义。
关联文件:internal/runtime/controlplane/stop_reason.go, internal/runtime/controlplane/decider.go
Todo 领域已具备 RetryCount/RetryLimit/FailureReason 等字段,但 runtime 尚未形成统一“验证失败 -> 回退重做 -> 熔断阻塞”的调度闭环。
关联文件:internal/session/todo.go
Runtime 已存在 ExecuteSystemTool 执行通道,可用于外循环确定性验证。
关联文件:internal/runtime/system_tool.go
当前工具执行含权限审批链路,若直接走普通 bash 自动验证,可能进入 ask 交互阻塞。
关联文件:internal/runtime/permission.go
2.2 Inference
仅依赖 Prompt 约束和模型自我修复,无法稳定保证“完成判定可信、失败策略一致、token 消耗可收敛”。
必须把“是否完成”的最终控制权收归 Runtime。
2.3 Assumption
不同仓库的验证命令差异大,需要配置化命令集合与启停策略。
自动化验证默认不应触发人工审批弹窗,否则闭环被中断。
3. Goals
建立 Runtime 强控的任务完成闭环:写入后必须经过验证流水线才可 DONE。
建立结构化失败归因:验证失败可被机器读取并驱动下一轮修复。
建立统一重试与熔断策略:失败可重试,重试耗尽后进入 BLOCKED 并停止自动消耗。
建立可扩展 Hook 机制:不改主循环即可插入治理策略、观测与审计能力。
保持模块边界:不把策略泄漏到 TUI,不把工具执行散落到 runtime/tui 之外。
4. Non-Goals
不在本 RFC 中引入新的模型厂商适配逻辑。
不在本 RFC 中实现完整 CI 平台替代。
不以“兼容旧路径”为目标保留多套完成判定逻辑。
不把 Hook 设计成任意外部脚本执行平台(避免不可控副作用)。
5. Design
5.1 Main Structure / System Prompt
采用“双环架构”:
内环:现有 ReAct(plan/execute)负责产出修改与工具调用。
外环:Runtime 在 verify 阶段强制执行验证流水线并给出硬结果。
完成判定收敛为:
completion gate 通过(无 pending todo、无未验证写入、无 post-execute closure)
且 verification gate 通过(对本轮写入范围完成验证)
Prompt 仅保留行为约束,不承担最终裁决:
可增加 submit_for_review 作为模型显式提交信号。
即使模型未显式提交,Runtime 在检测到未验证写入时仍会强制触发外循环验证。
5.2 Sub-agent Design
SubAgent 继续用于执行类子任务,不拥有最终完成判定权。
SubAgent 返回结果只作为内环输入,不可直接将 Todo 标记为最终完成。
Runtime 对 SubAgent 产物执行同一套验证与重试规则,避免主/子代理行为分裂。
5.3 Tool Strategy
新增 Runtime 验证编排器(建议走 ExecuteSystemTool 通道),执行配置化命令:
lint
typecheck
build
test
统一输出结构化结果:
stage
command
exit_code
error_class
file
line
summary
raw_excerpt
工具事实约束:
模型工具返回的 VerificationPerformed/Passed 作为参考信号。
最终 verification gate 以 Runtime 验证编排器结论为准。
禁止通过伪造工具事实绕过完成门禁。
5.4 Behavior Constraints
新增 StopReason(建议):
STOP_VERIFICATION_FAILED
STOP_RETRY_EXHAUSTED
STOP_REACT_STEP_EXHAUSTED
Todo 闭环规则:
验证失败:RetryCount++,写入 FailureReason,状态回退到可继续执行态。
达到 RetryLimit:状态转 blocked,Run 终止并等待人工介入。
验证通过:清除未验证写入标记,允许进入完成判定。
熔断规则:
内环超过步数上限触发 STOP_REACT_STEP_EXHAUSTED。
外环重试耗尽触发 STOP_RETRY_EXHAUSTED。
5.5 Hook Mechanism (Claude Hook 思路的本地化落地)
引入 Runtime 内部 Hook Registry(强类型接口 + 固定生命周期点):
BeforeToolExecute
AfterToolExecute
BeforeVerification
AfterVerification
BeforeCompletionDecision
BeforeStopReasonDecide
Hook 分级:
Blocking Hook:失败即阻断(质量门禁、安全策略)。
Non-blocking Hook:失败仅记录(埋点、通知、观测)。
每个 Hook 必须声明:
name
priority
timeout
failure_policy(fail-open / fail-closed)
与 Claude Code Hook 的关系:
借鉴事件拦截思想。
不采用松散脚本化回调,改为 runtime 内部可测试机制,避免不可控副作用。
6. Acceptance Criteria
代码写入后,任务无法直接 DONE,必须通过 runtime 外循环验证。
验证失败时,系统能输出结构化失败归因并驱动下一轮修复提示。
连续失败达到重试上限后,任务进入 blocked,并输出明确 stop reason。
stop_reason_decided 事件能区分“验证失败”“重试耗尽”“普通完成”等终止类型。
Hook 可以在不改 ReAct 主循环代码的情况下启停,且行为可被测试覆盖。
关键路径测试覆盖:
completion gate
stop reason 决议
todo 重试流转
验证编排器成功/失败分支
hook fail-open/fail-closed 语义
7. Dependencies and Impact
配置层:新增 runtime verification 配置项与校验逻辑。
关联文件:internal/config/runtime.go, internal/config/config.go
Runtime 控制面:扩展 stop reason、验证门禁、重试熔断与事件载荷。
关联文件:internal/runtime/controlplane/decider.go, internal/runtime/events.go, internal/runtime/run_lifecycle.go
Runtime 执行面:在 run.go 与 turn_control.go 接入验证编排与回退逻辑。
关联文件:internal/runtime/run.go, internal/runtime/turn_control.go
Session/Todo:消费既有重试字段并补齐流转校验测试。
关联文件:internal/session/todo.go
文档:同步更新 runtime 流程、todo 设计、tools 与 TUI 事件说明。
关联文件:docs/runtime-provider-event-flow.md, docs/session-todo-design.md, docs/tools-and-tui-integration.md
8. Implementation Changes
在 internal/runtime 新增验证编排模块与结构化失败分类模块(建议新目录 verification)。
在 internal/runtime 新增 Hook 抽象与注册中心(建议新目录 hooks)。
扩展 internal/runtime/controlplane/stop_reason.go 与 decider.go 的停止语义。
扩展 internal/runtime/events.go 新增验证相关事件载荷(开始、完成、失败归因)。
在 internal/runtime/run.go 引入“写入后强制验证 -> 失败回退 -> 成功放行”的闭环。
在 internal/config/runtime.go 增加 verification 配置字段及默认值/校验。
补齐单元测试与回归测试:
验证流水线通过/失败/超时
重试计数与阻塞
Hook 顺序、超时、失败策略
stop reason 唯一决议一致性
9. Alternatives
仅做 Prompt 强化 + 工具约束,不做 Runtime 外循环。
优点:实现快。
缺点:完成判定稳定性依赖模型,难以工程化兜底。
纯状态机硬编码,不引入 Hook。
优点:简单直接。
缺点:后续新增治理策略时会持续侵入主循环。
10. Risks and Rollback
风险:验证命令配置不当导致误阻塞。
对策:提供按仓库可配命令集和可观测失败摘要。
风险:Hook 滥用导致隐式副作用。
对策:限制 Hook 生命周期点、超时、失败策略与读写边界。
回滚:关闭 verification gate 与 Hook registry 开关后,退回现有 completion gate 行为。
Reactions are currently unavailable
You can’t perform that action at this time.
1. Summary
2. Background and Problems
2.1 Observed Facts
completion gate已存在,但当前阻断条件仅覆盖 pending todo / unverified write / post-execute closure。关联文件:internal/runtime/controlplane/completion.go
RunState已有plan/execute/verify,但verify目前未绑定 runtime 强制验证流水线。关联文件:internal/runtime/controlplane/phase.go, internal/runtime/run.go
ToolExecutionFacts已支持WorkspaceWrite与VerificationPerformed/Passed/Scope,但主要依赖工具返回事实,不是 runtime 硬判定。关联文件:internal/tools/types.go, internal/tools/bash/tool.go, internal/runtime/turn_control.go
StopReason当前缺少“验证失败/重试耗尽/闭环熔断”语义。关联文件:internal/runtime/controlplane/stop_reason.go, internal/runtime/controlplane/decider.go
RetryCount/RetryLimit/FailureReason等字段,但 runtime 尚未形成统一“验证失败 -> 回退重做 -> 熔断阻塞”的调度闭环。关联文件:internal/session/todo.go
ExecuteSystemTool执行通道,可用于外循环确定性验证。关联文件:internal/runtime/system_tool.go
ask交互阻塞。关联文件:internal/runtime/permission.go
2.2 Inference
2.3 Assumption
3. Goals
DONE。BLOCKED并停止自动消耗。4. Non-Goals
5. Design
5.1 Main Structure / System Prompt
verify阶段强制执行验证流水线并给出硬结果。completion gate通过(无 pending todo、无未验证写入、无 post-execute closure)verification gate通过(对本轮写入范围完成验证)submit_for_review作为模型显式提交信号。5.2 Sub-agent Design
5.3 Tool Strategy
ExecuteSystemTool通道),执行配置化命令:linttypecheckbuildteststagecommandexit_codeerror_classfilelinesummaryraw_excerptVerificationPerformed/Passed作为参考信号。verification gate以 Runtime 验证编排器结论为准。5.4 Behavior Constraints
STOP_VERIFICATION_FAILEDSTOP_RETRY_EXHAUSTEDSTOP_REACT_STEP_EXHAUSTEDRetryCount++,写入FailureReason,状态回退到可继续执行态。RetryLimit:状态转blocked,Run 终止并等待人工介入。STOP_REACT_STEP_EXHAUSTED。STOP_RETRY_EXHAUSTED。5.5 Hook Mechanism (Claude Hook 思路的本地化落地)
BeforeToolExecuteAfterToolExecuteBeforeVerificationAfterVerificationBeforeCompletionDecisionBeforeStopReasonDecidenameprioritytimeoutfailure_policy(fail-open / fail-closed)6. Acceptance Criteria
DONE,必须通过 runtime 外循环验证。blocked,并输出明确 stop reason。stop_reason_decided事件能区分“验证失败”“重试耗尽”“普通完成”等终止类型。7. Dependencies and Impact
关联文件:internal/config/runtime.go, internal/config/config.go
关联文件:internal/runtime/controlplane/decider.go, internal/runtime/events.go, internal/runtime/run_lifecycle.go
run.go与turn_control.go接入验证编排与回退逻辑。关联文件:internal/runtime/run.go, internal/runtime/turn_control.go
关联文件:internal/session/todo.go
关联文件:docs/runtime-provider-event-flow.md, docs/session-todo-design.md, docs/tools-and-tui-integration.md
8. Implementation Changes
internal/runtime新增验证编排模块与结构化失败分类模块(建议新目录verification)。internal/runtime新增 Hook 抽象与注册中心(建议新目录hooks)。internal/runtime/controlplane/stop_reason.go与decider.go的停止语义。internal/runtime/events.go新增验证相关事件载荷(开始、完成、失败归因)。internal/runtime/run.go引入“写入后强制验证 -> 失败回退 -> 成功放行”的闭环。internal/config/runtime.go增加 verification 配置字段及默认值/校验。9. Alternatives
10. Risks and Rollback