Skip to content

refactor(work-items): modularize autonomous_replan_ack with zero Any and linear writeback indexing - #9462

Open
loopx-agent wants to merge 1 commit into
mainfrom
codex/slim-autonomous-replan-ack-zero-any-20261126
Open

loopx-agent wants to merge 1 commit into
mainfrom
codex/slim-autonomous-replan-ack-zero-any-20261126

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Summary

Modularizes loopx/control_plane/work_items/autonomous_replan_ack.py (793 -> 492 LOC, <= 550), eliminates all AST Any references (52 -> 0), and removes redundant scans/allocations while keeping behavior identical across 57c4c43b52.

Changes

  • loopx/control_plane/work_items/autonomous_replan_ack.py (492 LOC, 0 AST Any, 0 # type: ignore):
    • Preserves all 21 public exports and explicit effect_runtime_result re-export for test monkeypatching and package-kernel import pins (packages/loopx-jev/src/loopx_jev/sentinel_compare.py).
    • Consolidates ACK semantic contract verification via _accepted_semantic_delta, removing duplicate dict(ack) copies in autonomous_replan_ack_recorded and compact_autonomous_replan_ack.
    • Preserves exact key-presence vs None semantics (todo_source), removes dead trailing return None, and skips duplicate GoalRef filtering in historical_ack_candidates_with_writeback_order.
  • loopx/control_plane/work_items/autonomous_replan_ack_history.py (400 LOC, 0 AST Any, 0 # type: ignore):
    • Extracts GoalRef ownership helpers (drop_unowned_recorded_replan_acks) and rollout writeback order binding (attach_historical_ack_writeback_order).
    • Unifies duplicated quota_should_run index checks in _admission_event_index and replaces quadratic rollout scans in attach_historical_ack_writeback_order with single-pass indexing and lazy per-turn receipt caching.
  • loopx/control_plane/goals/goal_frontier/semantic_history.py (+4/-1 LOC):
    • Narrows ack.get("semantic_delta") with isinstance(..., dict) so callers compile cleanly against strict dict[str, object] returns (+0 mypy delta across all 448 transitive files).
  • tests/control_plane/test_autonomous_replan_ack_contract.py (341 LOC, 0 AST Any, 0 # type: ignore):
    • Verifies size budgets, 0 AST Any, export preservation, paired origin/main vs HEAD equivalence, single-pass attach_historical_ack_writeback_order event read efficiency, read-only MappingProxyType inputs, and malformed index/container rejections.

Verification (clean detached worktree @ 6cfb1aebde)

  • ruff check: 4/4 files clean (All checks passed!).
  • mypy --strict --follow-imports=silent: 3/3 files clean (autonomous_replan_ack.py, autonomous_replan_ack_history.py, test_autonomous_replan_ack_contract.py).
  • Repo [tool.mypy]: 17/17 clean; caller mypy across 14 direct loopx/ + packages/loopx-jev/ callers (5 -> 5) and 448 transitive files (4376 -> 4376): +0 error delta.
  • TypeScript: npm run typecheck:control-plane clean; 271/271 TS tests (content_digest_single_owner.test.ts, replan_semantics.test.ts, replan_context.test.ts, quota_settlement_readback.test.ts, delivery_continuity.test.ts) passed.
  • Smokes (7/7 passed): todo-readmodel-boundary-smoke.py, autonomous-replan-obligation-smoke.py, quota-replan-decision-plane-smoke.py, work-lane-contract-smoke.py, monitor-scheduler-contract-smoke.py, attention-queue-readmodel-smoke.py, refresh-state-shared-runtime-projection-smoke.py.
  • pytest -q -n 4 -rs across 16 suites (test_autonomous_replan_ack_contract.py, test_autonomous_replan_ack.py, test_autonomous_replan_periodic_lookback.py, test_final_outcome_diagnostics.py, test_goal_frontier_replan_rules.py, test_goal_vision_blocked_successor.py, test_replan_successor_durable_ack.py, test_refresh_state_replan_gate.py, test_replan_checkpoint_compatibility_cli.py, test_external_progress_review.py, test_goal_amendment_proposal.py, test_goal_amendment_proposal_lifecycle.py, test_state_refresh_projections.py, packages/loopx-jev/tests/test_receipts.py, tests/architecture/, tests/canary/): 2,376 passed, 6 skipped (6 APFS non-UTF-8/root filename skips in test_git_path_display.py, 0 failed).
  • loopx check: errors=0, warnings=0 (4/4 files); Goal CQR cqr_5a17d74c3e74e26f9419 (decision=pass); canary premerge: ok=true, merge_gate_passed=true, catalog_canaries=9/9, risk_profile_smokes=8/8, manual_hold_count=0.

…and linear writeback indexing

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 · gpt-6.2-sol · OpenAI · runtime_reported · xhigh

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

动机 / Motivation

把历史 ACK 来源过滤、事件位置和回执绑定拆到同域模块。减少重复扫描,保留历史写回恢复资格及同秒顺序。 基线为每个候选重复遍历同一批 rollout 事件,并在 Todo 回执 fallback 再扫描源事件。 依赖历史重规划 ACK 的 managed Turn、Goal frontier、quota 和状态读取。

同一秒的原始准入、独立完成轮和准入升级共存。原 admission event ID 优先,按日志位置读回已匹配的写回。直接 obligation 绑定与用 Todo、effect 补证的旧 receipt 均由 TypeScript 返回 historical_ack_recorded。固定 1,033 条事件、8 个候选的字段读取从 8,237 次降至 1,033 次。 历史恢复继续只消费正确原义务的写回,不会因同秒准入升级、退役实例或另一个 effect 误关当前重规划。 基线和当前各运行 5 个独立的持久历史样例,真实读取 Run index 与 rollout 日志并调用 TypeScript 决策。当前 44 项定向测试通过,其中含隔离 CLI 历史和结算重放。基线同一 CLI 文件 11 项通过。改动文件 Ruff、严格 mypy 和 git diff --check 通过。 实验使用精确两版的隔离历史文件、真实 TypeScript 判定和 CLI 测试夹具。8 候选统计为 event_kind 字段读取次数。已安装版、远程 CI 和生产延迟未核验。 基线契约

改动思路 / Approach

该改动沿用 load_receipt_replan_ack_candidates → history index 与 rollout JSONL → TypeScript receipt_bound_obligation。一次按 owner 和 Turn 分组,保留原始索引次序。同一候选的 Todo/effect 回执延迟计算并复用,判定留在现有 typed owner。 用 GoalRef、goal_id、agent_id、Turn、义务或 Todo/effect 和原 admission event ID 匹配。原始事件位置决定同秒前后,归并时保留第一个匹配事件。 只缓存与候选 Turn 有关的准入和写回。继续保留外部开口、读回 schema、原 first-match 顺序,无额外持久 cache。

具体改动 / Changes

精确提交 6cfb1ae,分叉基线 57c4c43。4 个文件 +921 / −478。拆出历史绑定 helper,保留 ACK 门面和现有 TypeScript 义务。新增契约与扫描测试。

关键代码讲解

  • _attach_writeback_order_owned。单遍分组,保留 admission event ID 与原日志位置。 源码

  • load_receipt_replan_ack_candidates。重读 Run index 和当前来源的 rollout 记录。 源码

  • compact_autonomous_replan_ack。保留录制、已接受 ACK 和显式 source 语义。 源码

  • qualify_settlement_history。把候选传输给 TypeScript settlement 规则并校验返回索引。 源码

  • bind-historical-ack-to-the-original-admission-and-ownership(implemented)。跨轮历史 ACK 要绑定真实原义务和当前 Goal/Agent 来源,并能证明写回晚于当前原准入。

对主干的风险 / Risks

同秒先于原 admission 的首条匹配写回被保留为早到并拒绝。只有退休 Goal 实例、alias 或别的 Agent 写回,以及 Todo 一致但 effect 错误时,当前和基线都拒绝。五组 Run index 和事件文件字节均保持。 基线和当前各运行 5 个独立的持久历史样例,真实读取 Run index 与 rollout 日志并调用 TypeScript 决策。当前 44 项定向测试通过,其中含隔离 CLI 历史和结算重放。基线同一 CLI 文件 11 项通过。改动文件 Ruff、严格 mypy 和 git diff --check 通过。 实验使用精确两版的隔离历史文件、真实 TypeScript 判定和 CLI 测试夹具。8 候选统计为 event_kind 字段读取次数。已安装版、远程 CI 和生产延迟未核验。

我的整体评价 / Verdict

APPROVE。 同秒顺序、精确来源、回执 fallback 和幂等路径保持。此次拆分实际减少该样例中重复事件扫描。 历史恢复继续只消费正确原义务的写回,不会因同秒准入升级、退役实例或另一个 effect 误关当前重规划。

English verdict: APPROVE at 6cfb1ae. Paired persisted histories preserve original admission order, exact Goal and Agent ownership, and Todo/effect fallback. The indexed traversal examines each event kind once in the measured workload.

@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

本 PR 面向控制面维护者以及依赖复盘确认回执(autonomous_replan_ack,即调度或状态刷新写回的复盘确认结构)的下游运行时调用方(quota、status、goal_frontier、periodic_report 以及 loopx-jev)。

  • 触发任务与前后对比(Before / After):在基线 57c4c43b52 上,若仅原地删除 Any 注解仍无法把主模块压回 <= 550 LOC 维护上限,也无法消除多遍回放扫描与重复校验分支。改造前,autonomous_replan_ack.py 已膨胀至 793 LOC 并包含 52 处 AST Any 注解,semantic_history.py 等 13 处调用点缺乏严格静态约束,且 attach_historical_ack_writeback_order 每次调用都会在回放日志 rollout-event-log.jsonl 上重复线性扫描。改造后,核心编解码收拢至 492 LOC,GoalRef 实例归属与回放索引逻辑拆入 400 LOC 的 autonomous_replan_ack_history.py,两者均为 0 AST Any 与 0 # type: ignore,并将 10 候选回放扫描从多次遍历降至单遍(1 次)预索引。
  • 可观测结果:完整保留全部 22 个公开导出符号与 effect_runtime_result 重导出,在全部 dict 与真实 JSONL 回放载荷上保持与基线 57c4c43 100% 等价,并通过 2677 项 pytest、271 项 TS 测试及 17/17 premerge 金丝雀门禁。
  • 非目标边界:非目标:不修改 autonomous_replan_ack_v0 等协议线格式、不改动 TypeScript 结算内核、不放宽 module_metric_baseline.json 指标且不改动 packages/loopx-jev 源码。

改动思路 / Approach

整体采用“纯编解码留核、历史归属拆出、原有导出原地兼容”的分层思路,TypeScript effect_runtime_result 结算内核继续作为权威语义所有者。将纯编解码/窗口匹配留在 autonomous_replan_ack.py,并将 GoalRef 实例归属与单遍回放日志索引拆入 autonomous_replan_ack_history.py,契合 work_items 职责边界且不改动 TypeScript 结算内核。当前 PR 边界收敛在 Python 侧 work_items/autonomous_replan_ack* 模块重整、semantic_history.py 3 行类型收窄及契约 parity 测试,未扩展协议字段或内核语义。

  • 控制流与现有能力复用:纯校验、字段压缩与 20 轮持久窗口筛选留在 autonomous_replan_ack.py;涉及 GoalRef 解析(唯一标识目标实例的结构)与 rollout-event-log.jsonl 事件索引的函数抽离至 autonomous_replan_ack_history.py 并通过原地重导出兼容全部 13 个调用方,合并了重复的 _accepted_semantic_delta 校验与 _admission_event_index 准入查找,继续复用既有 load_rollout_events / load_goal_runs、effect_runtime_result 与 parse_timestamp。
  • 失败与拦截语义:对损坏的 JSONL 行安全跳过,对非法 qualify_settlement_history 返回下标(bool/负数/越界)抛出 RuntimeError,对不匹配 goal_id 的 GoalRef 抛出 ValueError,确保异常场景 fail closed。

下表对照说明(仅依据 9462@6cfb1aebdef81caad4a73b8742948a68372fa56d 源码整理,用作结构说明而非独立测试证据):

维度 基线 57c4c43b52 本 PR 6cfb1aebde
模块规模与 Any 单文件 793 LOC,52 AST Any 拆分为 492 + 400 LOC,0 AST Any / 0 # type: ignore
回放事件检索 每个候选及收据匹配重复扫描 events 与 runs _attach_writeback_order_owned 单遍预索引 + 按 turn 缓存
调用方 semantic_history.py 依赖松散链式 (ack or {}).get(...) 显式 isinstance(ack, dict) / isinstance(semantic_delta, dict) 收窄
对外接口与兼容 22 项公开符号 + effect_runtime_result 逐一保留全部导出与包 pin,只读 MappingProxyType 一致接纳

具体改动 / Changes

本次共改动 4 个文件(+921/-478):核心模块 loopx/control_plane/work_items/autonomous_replan_ack.py(+176/-477,现 492 LOC)、新建 loopx/control_plane/work_items/autonomous_replan_ack_history.py(+400/0 LOC)、调用方收窄 loopx/control_plane/goals/goal_frontier/semantic_history.py(+4/-1)及契约/等价测试 tests/control_plane/test_autonomous_replan_ack_contract.py(+341/0 LOC)。

关键代码讲解

  • _accepted_semantic_delta(loopx/control_plane/work_items/autonomous_replan_ack.py#L62-L68):统一检查 ack 是否为 Mapping、recorded is True 及 semantic_delta["accepted"] is True,消除原 autonomous_replan_ack_recorded 与 compact_autonomous_replan_ack 之间重复的浅拷贝和校验分支,并在非法输入时 fail closed 返回 None。
  • compact_autonomous_replan_ack(loopx/control_plane/work_items/autonomous_replan_ack.py#L177-L236):借助 _str_list 与 _mapping_items 完成零 Any 类型收窄,严格保留 todo_source 键存在性(run.get("todo_source", raw_ack.get("todo_source"))),并按 fresh_vision_path_outcome 是否在校验通过的 outcomes 中决定是否透传 path_disposition。
  • _attach_writeback_order_owned(loopx/control_plane/work_items/autonomous_replan_ack_history.py#L266-L372):先通过 _admission_event_index 优先按 admission_event_id、回退按 guard_recorded_at 解析唯一准入下标,再单遍扫描 events 按 turn_instance_id 归档 quota_should_run 与 refresh_state 写回记录,并缓存每轮的 expected_todo_receipt,消除多轮回放重扫。
  • receipt_bound_replan_source_acks(loopx/control_plane/goals/goal_frontier/semantic_history.py#L239-L272):将原链式 or 改写为显式 isinstance(ack, dict) 与 isinstance(semantic_delta, dict) 守卫,使严格静态检查下返回类型 dict[str, object] | None 的调用完全闭环而不改变运行时行为。

规范核对(Specification Mapping)

依据已验收协议文档 docs/reference/protocols/goal-vision-replan-contract-v0.md(审阅基线提交 9c0942be2b72381d0ff53131965d83ca736ac7ed)逐条核对:

  • GVR-ACK-1:implemented — compact_autonomous_replan_ack 保留 fresh_vision_path_outcome、path_disposition(仅允许 continue/no_change/replan)及 todo_source 键存在性语义。
  • GVR-ACK-2:implemented — latest_autonomous_replan_ack_for_projection 保留带 trigger_checkpoints 的 ACK 跨越 20 轮实效窗口,同时跳过中性分类。
  • GVR-ACK-3:implemented — autonomous_replan_ack_history.py 对 GoalRef 实例归属、异常下标(bool/负数/越界)及同秒多实例写回顺序保持严格 fail-closed 隔离。

对主干的风险 / Risks

  • 反例与兼容性边界:基线 57c4c43b52 与本 PR 6cfb1aebde 在真实 dict 载荷、边界空值/空白字符串、重复 turn_instance_id 及完整 rollout-event-log.jsonl 文件上运行对偶反事实对比,DICT/REAL MISMATCHES: 0、EDGE MISMATCHES: 0;唯一刻意差异是对只读包装 MappingProxyType 的支持——基线 isinstance(..., dict) 会忽略 MappingProxyType(返回 False/None),本 PR 统一改用 isinstance(..., Mapping) 后已按只读契约接纳并通过专门单测验证,而 JSON/磁盘读写路径均为 dict,对生产行为无副作用。
  • 调用面、隔离与权限边界:scope_fit 命中 live_decision、blocked_retry、status、goal_frontier、periodic_report 与 sentinel_compare 全部活跃调用路径;本 PR 无 opt-in/default-off 开关(default_off_isolation: not_applicable),无新增执行/调度权威(authority_semantics: aligned),无特定领域耦合(domain_neutrality 保持中立),无提示词 prose 改动,未触及公共状态词表,且与 main 及在途 PR #9213、#9395、#9417 的 git merge-tree 均无文本冲突。
  • 验证矩阵(wait_for_ci: false,ci_policy: not_consulted):
    • ruff check(4 个改动文件):PASS。
    • mypy(仓库 [tool.mypy] 17/17 文件无错;13 个直接调用方 base-vs-head --follow-imports=silent 与全量传递检查对比:+0/-0 新增错误):PASS。
    • npm run typecheck:control-plane 与 5 组控制面 TS 测试(271/271):PASS。
    • 对偶反事实及单遍扫描验证(10 候选回放从多次遍历降为 1 次;种子变异注入 todo_source 失败即刻被契约测试拦截):PASS。
    • pytest -q -n 4 -rs(覆盖 autonomous_replan_ack*、goal_frontier、replan_successor*、quota_settlement_cli、packages/loopx-jev/tests 及 tests/architecture//tests/canary/ 共 22 个目标):PASS(2677 passed, 6 skipped,6 项均为 test_git_path_display.py 在 APFS 文件系统上的非 UTF-8/根文件名预期跳过,0 失败);goal-frontier-replan-rules-smoke + test_goal_vision_succession.py(35 passed):PASS。
    • canary premerge(standard 档位:4/4 direct checks、9/9 catalog canaries、8/8 risk profile smokes,manual_hold_count=0):PASS。
    • 远端 GitHub Actions CI:UNVERIFIED(按 wait_for_ci=false 策略未查询/等待远端 CI)。

我的整体评价 / Verdict

  • 长程与用户体验影响(outcome_impact):long_horizon: preserved,user_experience: preserved;视觉审阅 visual_review: not_applicable(原因:纯内部 Python 控制面重构与静态类型收敛,不涉及 UI 渲染),unverified_scope: none。code_volume 评价为 necessary 且 compatibility_assessment: retain(保留包重导出以零侵入守住 package_kernel_import_pins.json 与 13 处下游调用)。change_proportionality 评价为 proportionate(architecture_assessment: retain)。
  • 语义词表与残留风险:本 PR 完全复用既有状态与词表(semantic_alignment: aligned / reuse_existing),observable_semantics 在全部 dict/JSONL 路径上等价,唯一极小残余差异为对非 dict 只读 MappingProxyType 输入的兼容性放开。已基于最新精确提交 6cfb1aebdef81caad4a73b8742948a68372fa56d 完成复核,符合合入前独立评审质量标准。

English Verdict: APPROVE

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