自主阶段契约
以下为中文参考翻译,权威版本以 English 节为准 / The English section above is authoritative. 中文节标题降一级(###)是刻意的:守护测试 tests/unit/test_phase_b_acceptance.py 按二级标题解析英文契约,中文节不得引入同形态二级标题。 / Chinese headings are deliberately one level lower (###): the guard test parses the English contract by level-2 headings, so the Chinese half must not introduce same-shaped H2s.
目的(Purpose)
Issue #2044(Phase A)。本文档是
AutonomousOrchestrator中每个工作流阶段的契约规范, 是"阶段何时提交、以何种结果提交"的权威来源。代码必须与本文档一致;此处的变更需要 同步更新 orchestrator 以及tests/unit/test_orchestrator_characterization.py中的特征化测试。
AutonomousOrchestrator(约 13k 行)历史上允许每个 _do_* 阶段方法通过
_update_workflow 内联修改 current_phase / status。这使得一个阶段可能在自身
副作用尚未确认之前就部分成功并写入下一阶段的状态——这正是 #2044 要解决的
核心风险。
Phase A 在不移动阶段方法的前提下引入了一套契约:
WorkflowContext—— 阶段接收的只读快照。PhaseResult—— 阶段返回的结构化结果。_commit_phase_result—— 阶段/状态迁移的唯一权威路径。只有outcome="completed"才能推进current_phase。
全部八个阶段现在都返回经由 _commit_phase_result 提交的 PhaseResult;没有任何
阶段再内联提交。其中四个位于 phases/*.py、挂在 PHASE_HANDLERS 注册表之后
(development、pr_review、merge、acceptance_verification —— 通过
resolve_phase_handler 解析);另外四个(preparation、planning、report、wait)
仍是 orchestrator 中的 _do_* 方法,但已经遵循 (ctx, deps) -> PhaseResult
契约。调度器中的 _legacy(...) 包装分支仅作为防御性兜底,实际永远不会触发。
契约字段(Contract fields)
每个阶段都记录以下八个属性:
| 字段 | 含义 |
|---|---|
| preconditions(前置条件) | 阶段运行前必须成立的工作流/DB/git 状态。 |
| inputs(输入) | 阶段从工作流 dict 中读取的字段。 |
| authoritative evidence(权威证据) | 其状态决定结果的里程碑(milestone)。 |
| side effects(副作用) | 外部变更:git、GitHub API、agent 子进程、DB 写入。 |
| postconditions(后置条件) | 成功运行后保证的状态。 |
| re-entry point(重入点) | 重启时从何处恢复(持久化的 current_phase + 里程碑)。 |
| recovery behavior(恢复行为) | 阶段中途重试/重启/关机时的行为。 |
| terminal outcomes(终态结果) | 本阶段可能产生的 PhaseResult 结果。 |
阶段顺序(Phase ordering)
preparation → planning → development → pr_review → report → merge
↓
acceptance_verification
↓
(completed)
PHASE_ORDER 与 PHASE_STATUS_MAP(位于 orchestrator.py)是权威迁移表。
merge 的后继阶段是 acceptance_verification(#2335);
_next_phase("acceptance_verification") == "acceptance_verification"
(终态);_next_phase(<unknown>) == "planning"(恢复默认值)。提交入口只放行
PHASE_ORDER 中的 phase 以及它同样放行的两个伪 phase(wait、completed),
其余 next_phase 一律拒绝。
准备阶段(preparation)
创建 GitHub 仓库(新项目)、issue、分支和 worktree。它是唯一创建 worktree 的
阶段,因此 advance() 会对它跳过 worktree 自愈。
- preconditions(前置条件):
current_phase == "preparation"、status == "preparing"。此时尚不存在 worktree(主仓库是唯一有效的 repo_path)。 - inputs(输入):
project_path、is_new_project、requirements_text/requirements_issue_url、branch_strategy、parent_workflow_id(fork)。 - authoritative evidence(权威证据):
branch_created里程碑 (status == "completed")。新项目还包括repo_setup与issue_created。 - side effects(副作用):
gh.create_repo、gh.create_issue、gh.create_worktree/gh.add_worktree,DB 里程碑/工作流写入。 - postconditions(后置条件) :
worktree_path、branch_name、branch_strategy已设置;current_phase == "planning"、status == "planning"。 - re-entry point(重入点):
current_phase == "preparation"。fork 快速路径 会探测是否有存活的分支(本地或远端),并通过add_worktree重新挂载而非重建, 因此此前不完整的尝试是幂等的(#814)。 - recovery behavior(恢复行为):重新抛出异常前会记录
status == "failed"的branch_created里程碑;advance()将工作流标记为失败(网络错误则做 临时性重试)。 - terminal outcomes(终态结果):
completed→ planning;git/GitHub 错误时failed。
规划阶段(planning)
运行"先规划后审查"的轮次,直到计划获批或达到 max_plan_rounds。使用主会话线
(main session line),只读工具且带超时上限。
- preconditions(前置条件):worktree 已存在(
advance()会先对其自愈);current_phase == "planning"。 - inputs(输入):
requirements_text、current_round、max_plan_rounds、github_issue_number、此前的plan_created/plan_refined/plan_reviewed里程碑、user_feedback。 - authoritative evidence(权威证据) :
plan_finalized里程碑(获批)或max_plan_rounds耗尽。每轮:plan_created/plan_refined+plan_reviewed里程碑。 - side effects(副作用):每轮两次 agent 运行(规划、审查);附带计划的
issue 评论;将
user_feedback注入提示词后清除。 - postconditions(后置条件):获批时
current_phase == "development"、status == "developing"、current_round == 0。超时时status == "planning_timeout"(用户可延长)。其他失败时status == "failed"。 - re-entry point(重入点):
current_round已持久化;重启后从下一轮恢复。round_num由current_round + 1推导,绝不依赖内存。 - recovery behavior(恢复行为):临时性错误通过
advance()重试;WorkflowPaused(关机)不改动状态。 - terminal outcomes(终态结果):
completed→ development;规划超时时pause(planning_timeout 状态);规划失败时failed。
开发阶段(development)
实现变更并运行针对性测试。驱动主会话线(main session line)。测试失败时最多
循环 MAX_DEV_RETRIES_ON_TEST_FAIL 次,并可选用 CI 修复与重新开发。
- preconditions(前置条件):worktree 存在且位于
branch_name上;已存在 获批计划(plan_finalized里程碑);current_phase == "development"。 - inputs(输入):定稿计划内容、
dev_round、重试计数器(test_retries、dev_retries_on_test_fail、skip_retries)、测试里程碑证据。 - authoritative evidence(权威证据):
dev_completed里程碑 + 测试结果 里程碑(带result_summary的tests_run)。测试由结构化命令证据判定 (#2046),而非自由文本启发式。 - side effects(副作用):agent 运行(写代码)、测试执行、CI 修复 agent 运行、分支推送、issue 评论。
- postconditions(后置条件):成功时
current_phase == "pr_review"、status == "pr_review"、current_round == 0、重试计数器清零。不可恢复的 测试失败时status == "failed"。 - re-entry point(重入点):重试计数器与
dev_round已持久化;重启后在 同一开发轮次重入。重入防护避免进程重启后重复启动开发。 - recovery behavior(恢复行为):测试失败 → 开发重试(达上限为止)→
CI 修复 → 硬失败。关机时抛出
WorkflowPaused。 - terminal outcomes(终态结果):
completed→ pr_review;不可恢复的测试 失败时failed;循环内为retry。