Claude Code 的 SubagentStop 在 subagent 结束当前 turn 且无 pending tool call 时触发,不等同于任务完成。subagent 用 run_in_background: true 启动后台 Bash 进程后结束 turn,会触发 SubagentStop,但后台进程仍在运行。Claude Code 的 task-notification 文档明确说明了这一点:"A task-notification fires each time this agent stops... The user can send it another message and resume it, so the same task-id may notify more than once."
问题概述
executor_report_completehook 假设一次 SubagentStop 即为 agent 终态,据此立即判定 task 成功或失败。实际运行中,Claude Code subagent 可能在任务未完成时触发 SubagentStop(例如:subagent 用run_in_background: true启动长时间运行的 evaluator 后结束当前 turn),orchestrator 还可能自主通过 SendMessage 恢复该 agent。这种重入导致 task 被误标 failed、agent_index 中的 task_id 被覆盖为 None,最终在第二次 SubagentStop 时报错。环境信息
2f257c38b71828fcf807277243d872fcfb4e1e1d1(refactor/hooks_and_scheduler)txn_scheduling_20260825152605a3363342d3a0a3e7f(task_001)预期行为
completed_tasks.jsonl:找到记录则complete_task,未找到则fail_taskcompleted_tasks.jsonl一致实际行为
task_001 在 evotrias task_pool 中被标记为 failed,但
completed_tasks.jsonl中为 success(fitness=4149.38)。两个数据源不一致。根因是同一 agent_id 上发生了两次 SubagentStop,中间夹着一次 SubagentStart。时间线
start_task→ task_001 进入 running。remember_agent写入 agent_index[agent]={task_id: task_001}run_in_background: true启动 evaluator(600s)stop_reason: "end_turn")_find_completed_task返回 None(evaluator 仍在运行)→fail_task(running → failed)start_task在 queued 中找不到该 agent(已 failed)→ 返回 None。remember_agent用task_id: None覆盖了 agent_index 中原本正确的记录completed_tasks.jsonl,调用record_attemptexecutor_report_complete读 agent_index 发现task_id: None→ ERROR,无法 complete_task 也无法 fail_task根因分析
两个设计假设被违反:
假设一:SubagentStop = 终态
Claude Code 的 SubagentStop 在 subagent 结束当前 turn 且无 pending tool call 时触发,不等同于任务完成。subagent 用
run_in_background: true启动后台 Bash 进程后结束 turn,会触发 SubagentStop,但后台进程仍在运行。Claude Code 的 task-notification 文档明确说明了这一点:"A task-notification fires each time this agent stops... The user can send it another message and resume it, so the same task-id may notify more than once."hook 在 SubagentStop 时未找到完成记录即调用
fail_task,是过早的终态判定。假设二:一个 agent_id 不会重入
orchestrator在检测到 subagent 提前停止后,有一定概率通过 SendMessage 恢复该 subagent。resume 会触发同一 agent_id 的第二次 SubagentStart。
remember_agent(scheduler_hooks.py:76)在每次 SubagentStart 时无条件用新建的 dict 覆盖 agent_index。重入时start_task返回 None(task 已离开 queued),agent_info 被重建为task_id: None,破坏了之前正确的绑定关系。