Repository navigation
refactor(work-items): modularize autonomous_replan_ack with zero Any and linear writeback indexing - #9462
refactor(work-items): modularize autonomous_replan_ack with zero Any and linear writeback indexing#9462loopx-agent wants to merge 1 commit into
Conversation
…and linear writeback indexing Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
loopx-agent
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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注解仍无法把主模块压回<= 550LOC 维护上限,也无法消除多遍回放扫描与重复校验分支。改造前,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与本 PR6cfb1aebde在真实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/4direct checks、9/9catalog canaries、8/8risk 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
Summary
Modularizes
loopx/control_plane/work_items/autonomous_replan_ack.py(793 -> 492LOC,<= 550), eliminates all ASTAnyreferences (52 -> 0), and removes redundant scans/allocations while keeping behavior identical across57c4c43b52.Changes
loopx/control_plane/work_items/autonomous_replan_ack.py(492LOC,0ASTAny,0 # type: ignore):effect_runtime_resultre-export for test monkeypatching and package-kernel import pins (packages/loopx-jev/src/loopx_jev/sentinel_compare.py)._accepted_semantic_delta, removing duplicatedict(ack)copies inautonomous_replan_ack_recordedandcompact_autonomous_replan_ack.Nonesemantics (todo_source), removes dead trailingreturn None, and skips duplicateGoalReffiltering inhistorical_ack_candidates_with_writeback_order.loopx/control_plane/work_items/autonomous_replan_ack_history.py(400LOC,0ASTAny,0 # type: ignore):drop_unowned_recorded_replan_acks) and rollout writeback order binding (attach_historical_ack_writeback_order).quota_should_runindex checks in_admission_event_indexand replaces quadratic rollout scans inattach_historical_ack_writeback_orderwith single-pass indexing and lazy per-turn receipt caching.loopx/control_plane/goals/goal_frontier/semantic_history.py(+4/-1LOC):ack.get("semantic_delta")withisinstance(..., dict)so callers compile cleanly against strictdict[str, object]returns (+0mypy delta across all 448 transitive files).tests/control_plane/test_autonomous_replan_ack_contract.py(341LOC,0ASTAny,0 # type: ignore):0ASTAny, export preservation, pairedorigin/mainvsHEADequivalence, single-passattach_historical_ack_writeback_orderevent read efficiency, read-onlyMappingProxyTypeinputs, and malformed index/container rejections.Verification (clean detached worktree @
6cfb1aebde)ruff check:4/4files clean (All checks passed!).mypy --strict --follow-imports=silent:3/3files clean (autonomous_replan_ack.py,autonomous_replan_ack_history.py,test_autonomous_replan_ack_contract.py).[tool.mypy]:17/17clean; callermypyacross 14 directloopx/+packages/loopx-jev/callers (5 -> 5) and 448 transitive files (4376 -> 4376):+0error delta.npm run typecheck:control-planeclean;271/271TS 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.7/7passed):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 -rsacross 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 intest_git_path_display.py, 0 failed).loopx check:errors=0, warnings=0(4/4files); Goal CQRcqr_5a17d74c3e74e26f9419(decision=pass);canary premerge:ok=true,merge_gate_passed=true,catalog_canaries=9/9,risk_profile_smokes=8/8,manual_hold_count=0.