Skip to content

fix(turn_driver): materialize autonomous_replan successor and handle writeback rejections - #9587

Open
loopx-agent wants to merge 1 commit into
mainfrom
codex/fix-turn-replan-successor-writeback-20261129
Open

loopx-agent wants to merge 1 commit into
mainfrom
codex/fix-turn-replan-successor-writeback-20261129

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Summary

Fixes two coupled bugs causing infinite dead-loops and missing outer-adapter successor materialization on Todo-less autonomous_replan Turns:

  1. Safe writeback rejection handling & composed replan validation (turn_todo_writeback.py, turn_run_once.py):
    • Catches (ReplanWritebackRejected, LocalCoordinationAuthorityRejection) inside refresh_turn_candidate (when not dry_run) so settlement.py cleanly aborts durable_writeback and marks the Turn failed (result_kind="writeback_failed") rather than crashing mid-prepared.
    • Composes native validate_replan on Todo-less autonomous_replan Turns even when an external --validation-command-json validator is supplied.
  2. Typed outer-adapter replan_successor_json contract (replan_successor.py, executor.py, recovery.py, codex_cli.py, host_candidate.py, todo_terminal_reentry.ts):
    • Adds turn_replan_successor_v0 (replan_successor_json) with "create_successor" (creating an open advancement_task Todo bound to replan_obligation_id and idempotently linking uncovered Goal outcomes when applicable) and "settle_no_followup" (closing trigger Todos with --no-follow-up via lifecycle_reentry while preserving original Turn settlement bindings).
    • Enforces result_kind="replan_required" and required replan_successor at host_result_contract (enabling reinvoke_host=True retry with remediation prompt rather than failing in durable_writeback).
    • Updates verifyReentrySettlement in todo_terminal_reentry.ts so lifecycle_reentry closeout recognizes Turn-recorded completion_turn_key (turn_instance_id / run_id) alongside settlement_effect_id while still verifying exact settlementIdentity and settled quota/writeback receipts.

Verification

  • pytest: 517 passed across tests/test_loopx_turn_replan_successor.py, tests/test_loopx_turn_executor.py, tests/test_loopx_turn_codex_cli.py, tests/test_loopx_turn_driver.py, tests/test_steward_report_recovery_and_validator.py, tests/control_plane/test_refresh_state_replan_gate.py, and tests/control_plane/test_turn_replan_writeback.py.
  • node --experimental-strip-types --test: 34 passed across tests/control_plane_ts/todo_completion_transaction.test.ts and tests/control_plane_ts/turn_journal.test.ts.
  • ruff check, loopx check --scan-path ..., and loopx canary premerge --from-git-diff --git-diff-base origin/main: all passed (18/18 checks, 0 failures).

…writeback rejections

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · Gemini · Google · self_reported
Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

动机 / Motivation

当一个 Goal 下的初始 agent advancement Todo 全部完成(触发 completed_advancement_without_successor)后,在 C1 turn run-once 模式下进入下一轮时,调度面会生成一个不带 selected_todo 的 autonomous_replan Turn,并要求宿主产出一个满足 new_runnable_successor 语义的结果来推进后续工作或收口生命周期。

  • 受影响调用方:使用 loopx turn run-once --host codex-cli(或 --host dsh)并传入 --validation-command-json 外部验证器的调度器与操作员,在 Goal 进入 Todo-less autonomous_replan 轮次时受影响。
  • 修复前后场景:修复前,外部验证器导致 if task_validator is None 跳过 validate_replan,随后 refresh_state_run 抛出未捕获的 ReplanWritebackRejected,使 journal 卡在 in_progress + prepared 并使后续重试以 reinvoke_host: false 无限死循环,且 C1 无法落地新后继或收口;修复后,host_result_contract 先校验 replan_successor_json 并支持带引导重调 Host,写回前幂等物化后继或关闭触发 Todo,写回拒绝也可被安全捕获并触发 abort_prepared。
  • 可观测结果:Todo-less autonomous_replan 轮次要么在 host_result_contract 阶段显式拒绝并支持恢复重试,要么成功物化 advancement_task 后继(及关联未覆盖 outcome)或完成 --no-follow-up 收口后提交,要么在拒绝时干净落入 status=failed(result_kind=writeback_failed)。
  • 非目标边界:不改变已有 Todo 绑定 Turn 的既有行为,不放松权限/租约/回执边界,也不允许 C1 Host 直接修改 LoopX 状态。

改动思路 / Approach

整体思路遵循控制面与 CLI 适配器分层边界:将纯协议校验与提示词契约留在 loopx/control_plane/turn_driver/,将外层状态写入与安全刷新包装放在 loopx/cli_commands/turn_todo_writeback.py 与 turn_run_once.py,并遵守现有模块行数棘轮限制(executor.py < 2000 LOC、turn_run_once.py < 1000 LOC)。

阶段 旧路径(修复前) 新路径(精确 head 779d1aa60ec6)
host_result_contract 不校验 Todo-less autonomous_replan 是否提供后继声明 当需要 new_runnable_successor 且 result_kind 为实质性结果时,强制要求 result_kind="replan_required" 与合法 replan_successor_json,失败时标记 host_result_contract 以便重调 Host
task_postcondition 提供 --validation-command-json 时跳过 validate_replan 对 Todo-less autonomous_replan Turn 始终先执行调用方验证器,再串联组合 validate_replan 进行干跑预览
durable_writeback 直接调用 refresh_state_run,拒绝异常穿透并卡死在 prepared;lifecycle_reentry 不识别 Turn 写入的 turn_instance_id 先幂等执行 create_successor(创建绑定 Todo 并按需关联未覆盖 Goal outcome)或 settle_no_followup;refresh_turn_candidate 捕获拒绝并交由 settlement.py 走 abort_prepared,同时 verifyReentrySettlement 兼容已校验回执的 Turn completion_turn_key

架构边界评估(architecture_assessment: retain):将纯契约校验放入 replan_successor.py,将外层状态写入与安全刷新包装放入 turn_todo_writeback.py,并在 todo_terminal_reentry.ts 兼容 Turn 写入的 completion_turn_key,既满足控制面分层与行数棘轮约束,又完整打通重试、物化与中止恢复闭环。 当前 PR 严格限定在修复 Todo-less autonomous_replan 写回死循环与 C1 后继物化缺失的最小完整边界内,未夹带无关重构。

具体改动 / Changes

依据问题与测试基线(spec_basis: none,基于复现故障与既有 turn_driver / todo_terminal_reentry 事务不变量推导需求,无独立外部 RFC 文档),具体实现覆盖 9 个文件(+1289 / -172 行):

关键代码讲解

  1. replan_successor.py — validate_autonomous_replan_host_contract & parse_host_replan_successor
    负责在 executor.py 的 validate_loopx_turn_host_result 中解析并校验 turn_replan_successor_v0:支持 create_successor(校验公共安全 text、优先级、replan_successor_semantic_binding(action_kind, target_key) 与可选 outcome_id)和 settle_no_followup(强制要求 agent_vision.state == "no_followup" 与 path_delta.outcome == "stop"),并在缺少必需后继声明或 result_kind != "replan_required" 时立即在 host_result_contract 阶段拒绝。
  2. executor.py & recovery.py & codex_cli.py & host_candidate.py — _normalize_host_path_delta & host_result_contract_retry_remediation
    在 HOST_RESULT_FIELDS 和 codex_cli._result_schema 中注册 replan_successor_json(maxLength=2400);当 settle_no_followup 时放行 material_replan 下的 path_delta.outcome == "stop";更新提示词说明 path_delta_mode="unchanged" 与 material_replan 的适用条件,并在 host_result_contract 失败后通过 retry_remediation 在重试提示中指导 Host 补全或修正 replan_successor_json。
  3. turn_todo_writeback.py & turn_run_once.py — refresh_turn_candidate & materialize_replan_successor_before_refresh & build_replan_task_validator
    将原位于 turn_run_once.py 的校验与刷新包装外提至 turn_todo_writeback.py(使 turn_run_once.py 保持 960 LOC、executor.py 保持 1996 LOC)。build_replan_task_validator 确保外部 --validation-command-json 与原生 validate_replan 叠加执行;materialize_replan_successor_before_refresh 在 refresh_state_run 前幂等创建/复用 advancement_task Todo 并链接未覆盖 outcome,或在 settle_no_followup 时以 lifecycle_reentry 关闭触发 Todo 并补齐 no_followup progress_observation;refresh_turn_candidate 在 not dry_run 时捕获 ReplanWritebackRejected 与 LocalCoordinationAuthorityRejection 返回结构化失败,同时 turn_run_once.py 在 writeback_failed 时同步透传 "error" 字段以保持 CLI 契约兼容。
  4. todo_terminal_reentry.ts — verifyReentrySettlement
    在保留 actor_agent_id、todo_id、settlementIdentity(...).effect_id 及 readQuotaSettlementFromSnapshot 双回执校验的前提下,允许识别由 turn run-once 写入 todo.completion_turn_key 的原始 turn_instance_id(event.run_id),从而打通 C1 Turn 完成 Todo 后的 --no-follow-up 生命周期重入闭环。

对主干的风险 / Risks

  • 非目标与已启用路径隔离(default_off_isolation / observable_semantics):replan_successor_json 仅在 Todo-less autonomous_replan Turn 且存在实质性结果时作为必需契约生效;普通 Todo-bound Turn 及 wait/blocked 等非实质性结果保持与基线一致(允许 replan_successor_json="")。turn_run_once.py 仅在 ok=False 且 result_kind="writeback_failed" 时补充 "error" 字段,与原未捕获异常的 CLI 错误读取路径(如 test_turn_replan_writeback.py)保持兼容。
  • 反例与失败恢复边界(failure_analysis):当外部 refresh_state_run 或 refine_goal_outcome 返回拒绝时,materialize_replan_successor_before_refresh 或 refresh_turn_candidate 会返回 {"ok": False, ...},交由 settlement.py 执行 abort_prepared 使 journal 落为 status="failed"、清理 prepared 悬挂态,并保证 --resume-turn-key 在未带 --retry-failed-turn 时按原样回放失败而不重调 Host。
  • 语义对齐(semantic_alignment):复用现有 turn_replan_successor_v0、replan_successor_semantic_binding、todo_lifecycle_settlement 与 lifecycle_reentry 语义词汇,未引入新的公开状态枚举,不触发跨仓库语义对齐违例。
  • 残余风险:本地 TypeScript 单测(todo_completion_transaction.test.ts、turn_journal.test.ts)共 34 项全部通过;test_loopx_turn_replan_successor.py 中对 settle_no_followup、outcome 绑定及写回拒绝恢复做了端到端覆盖(run_codex_cli_host 在测试中以回调桩替换真实外部 Codex 进程)。远端 GitHub Actions CI 按 Goal wait_for_ci=false 策略未等待(UNVERIFIED / diagnostic)。

我的整体评价 / Verdict

本地全量相关验证均已通过(PASS):

  • pytest(7 个相关测试文件:test_loopx_turn_replan_successor.py、test_loopx_turn_executor.py、test_loopx_turn_codex_cli.py、test_loopx_turn_driver.py、test_steward_report_recovery_and_validator.py、test_refresh_state_replan_gate.py、test_turn_replan_writeback.py):517 passed(PASS)。
  • TypeScript 单测(todo_completion_transaction.test.ts、turn_journal.test.ts):34 passed(PASS)。
  • 维护性与边界门禁(ruff check、loopx check --scan-path ...、loopx canary premerge --from-git-diff --git-diff-base origin/main):18/18 检查通过,0 failures;executor.py 1996 LOC、turn_run_once.py 960 LOC 均满足棘轮限制(PASS)。
  • 视觉渲染审查(visual_review):not_applicable(本次改动均为 CLI/控制面/TS 状态逻辑,无前端或渲染 UI 变更;未验证范围为 none)。

对长程自主推进(long_horizon: improved)与 CLI 操作体验(user_experience: improved)均有直接正向收益:消除了无 Todo autonomous_replan Turn 的 prepared 死循环与 C1 无法物化后继/收口的断链。代码结构紧致、无阻断性缺陷。

English verdict: APPROVE at exact head 779d1aa; no blocking findings remain, and local verification passed (517 pytest passed, 34 TS tests passed, 18/18 canary premerge passed, ruff and boundary scans clean; remote CI not consulted under wait_for_ci=false).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant