Skip to content

refactor(capabilities): eliminate Any and slim explore harness_runtime and issue_fix reward_memory - #8997

Open
loopx-agent wants to merge 1 commit into
mainfrom
codex/capabilities-harness-reward-slim-20261118
Open

loopx-agent wants to merge 1 commit into
mainfrom
codex/capabilities-harness-reward-slim-20261118

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Summary

Eliminate all Any annotations (59 -> 0 and 53 -> 0, dict[str, Any] -> 0) and slim boilerplate/duplication (-24 net lines) across two capability modules without altering runtime behavior, exported symbols, error strings, docstrings, or explanatory comments, and without touching loopx/canary/module_metric_baseline.json:

  • loopx/capabilities/explore/harness_runtime.py (1123 -> 1116 lines, 59 -> 0 Any, 0 dict[str, Any]):

    • Introduced module-level Payload / PayloadView / ExecuteFn type aliases (placed after imports for Ruff E402), @runtime_checkable adapter Protocols (_SeedAdapter, _VariantAdapter, _LegacyExecutor), and small runtime narrowing helpers (_mapping, _optional_payload, _sequence, _payload_list, _as_float, _missing_attr).
    • Consolidated duplicated logic: merged selected_worker_branches / rejected_worker_branches loops in plan_router_no_prune, extracted _first_seed_by_family across select_frontier_work and emit_frontier_requests, extracted _standalone_metric_records across historical and live epoch metric aggregation, reused episode_runtime.build_router_probes instead of repeating standalone probe construction, replaced the nested _execute_episode wrapper with functools.partial, and extracted _normalize_queue_outcome.
    • Resolved pre-existing mypy --strict issues (execute_epoch no-redef and router_state non-None narrowing under use_router).
  • loopx/capabilities/issue_fix/reward_memory.py (883 -> 866 lines, 53 -> 0 Any, 0 dict[str, Any]):

    • Introduced Payload / PayloadView, _RecallItemView @runtime_checkable Protocol, and _mapping narrowing helper.
    • Consolidated duplicated automatic-recall checkpoint construction, hook invocation, and envelope formatting across run_issue_fix_reviewer_artifact_automatic_reward_memory and run_issue_fix_reviewer_notification_automatic_reward_memory into _run_automatic_surface_recall.
    • Extracted shared receipt gate validation into _append_receipt_reasons across reviewer_artifact_notification_gate and reviewer_notification_before_send_gate, and collapsed repeated _strict_object field checks in ingest_issue_fix_reward_memory_event.
    • Resolved the pre-existing mypy --strict str | None .encode() error in ingest_issue_fix_reward_memory_event.

Verification

Tested on exact head ce5744a48b11bfd5daa971fb1f7c44c61c85f554 (off origin/main 28ae33cf7efbfdc1f0c424b061db5af6d0f441db):

  • module_metrics:
    • loopx/capabilities/explore/harness_runtime.py: {'lines': 1116, 'any_count': 0, 'dict_any_count': 0} (baseline 1123, 59 Any)
    • loopx/capabilities/issue_fix/reward_memory.py: {'lines': 866, 'any_count': 0, 'dict_any_count': 0} (baseline 883, 53 Any)
  • uv run --extra test mypy --strict --follow-imports=silent loopx/capabilities/explore/harness_runtime.py loopx/capabilities/issue_fix/reward_memory.py: Success: no issues found in 2 source files
  • uv run --extra test mypy: Success: no issues found in 17 source files
  • uv run --extra test ruff check loopx/capabilities/explore/harness_runtime.py loopx/capabilities/issue_fix/reward_memory.py: All checks passed!
  • git diff --check origin/main...HEAD: clean (0 issues)
  • Direct capability smokes:
    • uv run --extra test python examples/explore-harness-runtime-resume-smoke.py: explore-harness-runtime-resume-smoke ok
    • uv run --extra test python examples/reward-memory-recall-application-smoke.py: reward-memory-recall-application-smoke: ok
    • uv run --extra test python examples/issue-fix-reviewer-notification-sink-smoke.py: issue-fix-reviewer-notification-sink-smoke: ok
  • Pytest (72 clean explore/issue_fix/reward test files + tests/canary/test_maintainability_ratchet.py + tests/architecture/test_semantic_vocabulary_drift.py + tests/test_cli_entrypoint.py): 1865 passed in 230.75s (0 failed, 0 skipped).
    • Pre-existing unrelated origin/main failures (14 failed in tests/control_plane/test_explore_settlement_attachment.py and tests/capabilities/test_issue_fix_maintainer_correction_admission.py::test_t06_stopped_goal_refuses_at_effect_time) were reproduced identically on unmodified origin/main.
  • uv run --extra test loopx canary premerge --tier quick --from-git-diff --git-diff-base origin/main: ok=true (selected=3, executed=3, failures=0, manual_holds=0)
  • loopx --format json check --scan-path loopx/capabilities/explore/harness_runtime.py --scan-path loopx/capabilities/issue_fix/reward_memory.py: ok=true (errors=0, warnings=0, public_boundary_scan=clean)

…e and issue_fix reward_memory

- Eliminate all 59 Any annotations in loopx/capabilities/explore/harness_runtime.py (1123 -> 1116 lines, 0 Any, 0 dict[str, Any]) using Payload/PayloadView aliases, adapter Protocols, runtime narrowing helpers, and shared probe/branch/seed helpers.

- Eliminate all 53 Any annotations in loopx/capabilities/issue_fix/reward_memory.py (883 -> 866 lines, 0 Any, 0 dict[str, Any]) using Payload/PayloadView aliases, _RecallItemView Protocol, _mapping narrowing, shared _append_receipt_reasons gate checks, and unified _run_automatic_surface_recall orchestration.

- Preserve all exported symbols, error messages, docstrings, explanatory comments, and leave loopx/canary/module_metric_baseline.json untouched.

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)

[VERDICT: APPROVE]

已针对 PR #8997 exact head ce5744a48b11bfd5daa971fb1f7c44c61c85f554 完成完整审查与本地验证(审查结论:APPROVE,无 P0/P1/P2 阻塞问题)。

动机 / Motivation

受影响调用方与运维者:所有调用 Explore Harness 预算臂执行、前沿变体调度与指标聚合(run_budget_arm、run_queue_epoch、select_frontier_work、emit_frontier_requests、plan_router_no_prune、aggregate_arms)以及 Issue Fix 奖励记忆事件入库、补丁规划、Reviewer 摘要门控与通知前策略召回(ingest_issue_fix_reward_memory_event、run_issue_fix_patch_planning_reward_memory、run_issue_fix_reviewer_artifact_reward_memory、run_issue_fix_reviewer_artifact_automatic_reward_memory、run_issue_fix_reviewer_notification_automatic_reward_memory 等)的运行时与测试调用方。

修改前(origin/main 28ae33cf7efbfdc1f0c424b061db5af6d0f441db):(1) loopx/capabilities/explore/harness_runtime.py 共 1123 行、存在 59 处 Any 注解,mypy --strict --follow-imports=silent 报 2 处错误(execute_epoch 在 if/else 分支间触发 [no-redef],observe_epoch(router_state, ...) 接收 dict[str, Any] | None),且存在多处重复逻辑(plan_router_no_prune 中 selected_worker_branches/rejected_worker_branches 双重循环、run_budget_arm 中重复的 standalone 记录规范化与独立探针字典构造、select_frontier_work 与 emit_frontier_requests 中重复的首个族种子抽取);(2) loopx/capabilities/issue_fix/reward_memory.py 共 883 行、存在 53 处 Any 注解,mypy --strict --follow-imports=silent 报 1 处错误(public_safe_compact_text 返回 str | None 后直接调用 .encode("utf-8") 触发 [union-attr]),且 run_issue_fix_reviewer_artifact_automatic_reward_memory 与 run_issue_fix_reviewer_notification_automatic_reward_memory 重复实现了近百行配置路由、checkpoint 构造、自动召回调用与结果信封组装,两个门控函数亦重复实现相同的 receipt 校验序列。
修改后(ce5744a48b11bfd5daa971fb1f7c44c61c85f554):(1) harness_runtime.py 降至 1116 行(净 -7 行)、Any 清零(59 -> 0,dict[str, Any] = 0),通过导入后置的 Payload/PayloadView/ExecuteFn、_SeedAdapter/_VariantAdapter/_LegacyExecutor @runtime_checkable Protocol 及窄化辅助函数(_mapping、_optional_payload、_sequence、_payload_list、_as_float、_missing_attr)消除全部 Any 与全部 mypy --strict 报错,并以 functools.partial、_normalize_queue_outcome、_first_seed_by_family、_standalone_metric_records 与复用 build_router_probes 收拢重复逻辑;(2) reward_memory.py 降至 866 行(净 -17 行)、Any 清零(53 -> 0,dict[str, Any] = 0),通过 _RecallItemView Protocol、Payload/PayloadView、_mapping 窄化、_append_receipt_reasons 共享门控校验、_run_automatic_surface_recall 统一自动召回编排,以及 _strict_object 统一字段循环校验与 summary or "" 修复 .encode() 类型错误。

可观测结果:exact head 对应工作树上 module_metrics 返回:harness_runtime.py 为 {'lines': 1116, 'any_count': 0, 'dict_any_count': 0}(基线 1123 行、59 Any),reward_memory.py 为 {'lines': 866, 'any_count': 0, 'dict_any_count': 0}(基线 883 行、53 Any);uv run --extra test mypy --strict --follow-imports=silent loopx/capabilities/explore/harness_runtime.py loopx/capabilities/issue_fix/reward_memory.py 与全仓 17 文件 uv run --extra test mypy 均 0 报错;ruff check 通过(All checks passed!);git diff --check origin/main...HEAD 零问题且零本地用户绝对路径;3 项直连冒烟脚本(examples/explore-harness-runtime-resume-smoke.py、examples/reward-memory-recall-application-smoke.py、examples/issue-fix-reviewer-notification-sink-smoke.py)全部通过;pytest 覆盖 72 份无基线阻塞的 explore/issue_fix/reward 套件及 tests/canary/test_maintainability_ratchet.py、tests/architecture/test_semantic_vocabulary_drift.py、tests/test_cli_entrypoint.py 共 1865 passed in 230.75s(0 failed, 0 skipped;另行复现验证 test_explore_settlement_attachment.py 12 例与 test_issue_fix_maintainer_correction_admission.py::test_t06_stopped_goal_refuses_at_effect_time 2 例为当前 origin/main 既有无关失败);uv run --extra test loopx canary premerge --tier quick --from-git-diff --git-diff-base origin/main 返回 ok=true(selected=3, executed=3, failures=0, manual_holds=0);loopx check --scan-path ... 返回 ok=true(errors=0, warnings=0, public_boundary_scan=clean)。

非目标边界:本 PR 刻意不修改 loopx/canary/module_metric_baseline.json(与并发基线注册 PR 保持完全不相交),不更改任何导出符号名称、异常错误文本、文档字符串或解释性注释,亦不扩展修复 origin/main 既有无关测试失败(test_explore_settlement_attachment.py 与 test_t06_stopped_goal_refuses_at_effect_time)。

改动思路 / Approach

全部改动严格收敛于 loopx/capabilities/explore/harness_runtime.py 与 loopx/capabilities/issue_fix/reward_memory.py 两个目标能力模块内,复用了同目录既有 episode_runtime.build_router_probes 与 reward_memory 共享核心契约,未改动共享基线文件或其他控制面模块。

当前 PR 边界:仅包含 loopx/capabilities/explore/harness_runtime.py 与 loopx/capabilities/issue_fix/reward_memory.py 的零 Any 强类型化、mypy --strict 修复与重复模板收拢,不包含 module_metric_baseline.json 变更。

在实现路径上:(1) harness_runtime.py 将模块级类型别名(Payload、PayloadView、ExecuteFn)与 @runtime_checkable Protocol(_SeedAdapter、_VariantAdapter、_LegacyExecutor)置于全部顶层导入之后以确保 Ruff E402 零告警,通过 _mapping/_optional_payload/_sequence/_payload_list/_as_float/_missing_attr 在运行时边界完成安全类型窄化,保持适配器鸭子类型契约与 AttributeError 失败语义不变,同时用 episode_runtime.build_router_probes 与 _standalone_metric_records 消除重复探针和指标归一化逻辑;(2) reward_memory.py 通过 _RecallItemView 与 _mapping 安全收窄 object/Mapping 输入,将两条自动召回分支合并至 _run_automatic_surface_recall 并将两条门控 receipt 校验合并至 _append_receipt_reasons,严格维持所有异常类型、错误字符串、词表常量归属(test_reward_memory_field_vocabulary_owner.py 与 test_issue_fix_local_time_owner.py)与 fail-open 不变量。

具体改动 / Changes

本次 diff 共涉及 2 个文件(543 insertions(+), 567 deletions(-),净 -24 行);本任务无独立书面规范文档(spec_basis: no_spec,依据仓库静态类型棘轮与现有行为语义等价性评审)。

  1. loopx/capabilities/explore/harness_runtime.py(1123 -> 1116 行,净 -7 行,59 -> 0 Any,0 dict[str, Any])。
  2. loopx/capabilities/issue_fix/reward_memory.py(883 -> 866 行,净 -17 行,53 -> 0 Any,0 dict[str, Any])。

关键代码讲解

  • harness_runtime.py::run_budget_arm(L713-1053):入口通过 isinstance(adapter, _SeedAdapter/_VariantAdapter/_LegacyExecutor) 与 _missing_attr 对不同生命周期适配器按需窄化检查并保留原有 AttributeError 语义;在 episode_mode is not None 分支改用 functools.partial(execute_episode_group, ...) 替代内嵌 def execute_epoch 消除 [no-redef],并在 use_router 分支断言非空 router_state 与复用 build_router_probes(metric_records) 消除 15 行重复探针字典构造。
  • harness_runtime.py::_normalize_queue_outcome & plan_router_no_prune(L285-311, L612-680):将队列结果类型校验(Mapping vs Sequence、非空检查与 setdefault 附加元信息)抽取为 _normalize_queue_outcome,并将 plan_router_no_prune 中 selected_worker_branches 与 rejected_worker_branches 合并为单循环,同时用 _first_seed_by_family 去重 select_frontier_work 与 emit_frontier_requests 的种子分组逻辑。
  • reward_memory.py::_run_automatic_surface_recall(L220-309):统一 run_issue_fix_reviewer_artifact_automatic_reward_memory 与 run_issue_fix_reviewer_notification_automatic_reward_memory 中 resolve_reward_memory_surface_config、checkpoint 映射生成、run_reward_memory_automatic_recall_hook 调用及标准信封构造,并通过 include_peer_ref 精确保留摘要面含 peer_ref、通知面不含 peer_ref 的既有行为差异。
  • reward_memory.py::_append_receipt_reasons & ingest_issue_fix_reward_memory_event(L195-210, L312-370):抽取 reviewer_artifact_notification_gate 与 reviewer_notification_before_send_gate 末尾 5 项 receipt 校验为 _append_receipt_reasons(保持各门控前置 reason 顺序不变);在事件入库函数中以循环执行三组 _strict_object 校验,并将 summary 窄化为 str(or "")修复 .encode("utf-8") 在 mypy --strict 下的 [union-attr] 报错。

对主干的风险 / Risks

  • 兼容性与行为等价性:所有导出函数名、常量名、参数默认值、ValueError/TypeError/AssertionError/AttributeError 异常文本、原始 dict 原地写入语义(如 run_budget_arm 对 lane["results"] 中记录附加 novel_value/raw_value)及 test_reward_memory_field_vocabulary_owner.py / test_issue_fix_local_time_owner.py 词表所有权约束均原样保留;对于调用方传入 dict[str, Any] 或测试桩对象,PayloadView(Mapping[str, object])与 @runtime_checkable Protocol 保持协变与鸭子类型兼容。
  • 失败边界与局部范围:无任何公共开关或默认行为变更(default_off_isolation 保持不变);已通过负向用例验证非法参数、空返回序列、坏 JSON checkpoint、不兼容 runtime signature 及门控失败原因码列表与修改前一致。
  • 本地实测结果:
    • module_metrics: PASS — harness_runtime.py lines=1116, any_count=0, dict_any_count=0;reward_memory.py lines=866, any_count=0, dict_any_count=0
    • mypy --strict --follow-imports=silent(2 目标文件)与全仓 mypy(17 文件): PASS(0 errors)
    • ruff check(2 目标文件): PASS(All checks passed!)
    • 3 项直接能力冒烟脚本: PASS(explore-harness-runtime-resume-smoke, reward-memory-recall-application-smoke, issue-fix-reviewer-notification-sink-smoke)
    • Pytest(75 份洁净 explore/issue_fix/reward/棘轮/词表/CLI 套件): PASS — 1865 passed in 230.75s(0 failed, 0 skipped);另外 2 份 origin/main 既有损坏套件中的 14 例已独立复现验证与本 PR 无关
    • loopx canary premerge --tier quick --from-git-diff --git-diff-base origin/main: PASS — ok=true(selected=3, executed=3, failures=0, manual_holds=0)
    • loopx check --scan-path ...: PASS — ok=true(errors=0, warnings=0, public_boundary_scan=clean)

我的整体评价 / Verdict

  • Long-horizon 影响:improved — 彻底清除 explore/harness_runtime.py(59 处)与 issue_fix/reward_memory.py(53 处)共 112 处 Any 注解与 3 处 mypy --strict 隐患,净减 24 行重复样板,显著提升长期演进下的静态契约安全性。
  • User-experience 影响:preserved — 纯内部能力层强类型重构与去重,公共输入/输出、CLI 行为与错误信息完全保持等价。
  • visual_review:not_applicable — 仅涉及后端 Python 能力模块,无前端 UI 改动。
  • 可观测语义 / 复用 / 词表:observable_semantics 为 equivalent,repository_reuse 为 reused,authority_semantics 与 semantic_alignment 均为 aligned(candidate_decision="reuse_existing",不引入任何新状态词表,保持内部辅助类型与既有领域词表所有权不变)。

范围聚焦、向后兼容、本地验证充分;因仅涉及内部能力模块静态类型与去重且由 loopx-agent 自提交,待远端 CI 完成并由维护者授权即可合入(残余工作仅为在并发基线 PR 收拢后单独在 module_metric_baseline.json 中登记棘轮基线)。

English verdict: APPROVE exact head ce5744a (author-owned COMMENTED approval review); eliminates all 112 Any annotations (59->0 in loopx/capabilities/explore/harness_runtime.py, 53->0 in loopx/capabilities/issue_fix/reward_memory.py; 0 dict[str, Any]) and slims net 24 lines (1123->1116, 883->866) while fixing 3 strict mypy errors, leaves module_metric_baseline.json untouched, and passes mypy --strict, 17-file repo mypy, ruff, 3 capability smokes, 1865 pytest cases (0 failures; 14 pre-existing failures verified on unmodified origin/main), quick premerge canaries (3/3), and loopx check.

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