Repository navigation
refactor(quota): eliminate Any typing and redundant allocations in task_orchestration - #9468
loopx-agent wants to merge 1 commit into
Conversation
…sk_orchestration 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 · Gemini · Google · self_reported
Approval conclusion (author-owned PR; GitHub blocks formal self-approval)
动机 / Motivation
本 PR 面向配额与多智能体任务编排(task_orchestration)调度准备与决策发包调用方(should_run_prepare、should_run_packet 及只读 MappingProxyType 上下文构建方),消除类型擦除、冗余运行时分配与布尔值配置穿透隐患。
- 前后对比(Before / After):改造前,task_orchestration.py 含 51 处 AST Any 标注与多处 cast 调用,不仅掩盖只读 MappingProxyType 与非法非映射入参的边界差异,还会在无效编排目标上重复归一化能力集合、在逐键推导时分配多余临时字典,且因 Python 中 bool 继承自 int 而让 max_children=True 穿透校验进入自适应子任务准入(按 max_children=1 返回 task_orchestration_contract_v2)。改造后,模块实现零 Any、零忽略注释并压缩在 454 行以内,统一使用 Mapping[str, object] 入口与集中窄化辅助函数,对只读上下文与非映射响应均安全收口,显式排除布尔 max_children,并在配对反事实与生产级校验中保持既有行为严格等价。
- 可观测收益:task_orchestration.py(454 行)与新增契约测试(427 行)均达成 0 AST Any 与 0 静态忽略注释且未新增顶层模块;严格 mypy、6 个主干/调用方跨版本 mypy(+0/-0 增量)、57c4c43b52 与本提交配对反事实(除刻意拦截 max_children=True 外其余 100% 字节级一致)、78 项 TS 测试、10 项清净环境 smoke、3674 项 pytest 及 standard 档位 premerge 门禁全部通过。
- 非目标边界:非目标:不改动下游 task_orchestration_admission.py、should_run_prepare.py 与 should_run_packet.py 源码,不改变 TypeScript 决策所有者(peer_orchestration.ts / adaptive_children.ts)的规则语义,也不放宽工作道(work_lane)版本单源约束或新增兼容层包装。
改动思路 / Approach
保持业务规则继续归属 TypeScript 决策所有者与 work_lane 单一版本源,Python 侧仅在 task_orchestration.py 内原位收敛类型契约、只读映射窄化与配置边界防护,不新增顶层模块或兼容层。当前 PR 边界仅覆盖 task_orchestration.py 的原位零 Any 改造、冗余分配清理与配套单元测试,不扩展到下游 task_orchestration_admission.py 或 should_run_* 调用方重构。
| 路径 / 边界场景 | 基线 57c4c43b52 行为 |
本 PR 9548e47e33 行为 |
|---|---|---|
| 静态类型与只读上下文 | 51 处 AST Any + cast(...);部分入口仅认 dict |
0 AST Any / 0 忽略注释;内层与辅助函数统一支持 Mapping / MappingProxyType |
orchestration.max_children=True |
isinstance(True, int) 穿透,按 max_children=1 准入自适应子任务 |
not isinstance(max_children, bool) 拦截,安全回退为 (None, fallback) |
无效编排或非映射 work_lane_contract |
无前置短路仍执行能力归一化;非 dict 直接原样透传 |
前置短路退出;_as_dict_or_none / Mapping 校验 fail-closed 返回 None |
| 合法对等体 / 自适应编排 / 阻塞诊断 | 既有 assignment_key、[] 优先、peer_activation_diagnostic |
与基线逐字段一致(配对反事实全量通过) |
具体改动 / Changes
本次改动共 2 个文件(+669/-155):loopx/control_plane/quota/task_orchestration.py(+242/-155,现 454 <= 550 LOC)与新增契约测试 tests/control_plane/test_task_orchestration_contract.py(+427/-0,427 LOC),顶层模块数维持 loopx/*.py == 112 不变。
关键代码讲解
_as_dict_or_none(loopx/control_plane/quota/task_orchestration.py#L45-L53)与_peer_lane_refs(#L60-L80):统一将Mapping安全复制为dict[str, object]并屏蔽非映射运行时返回值;通过_PEER_SOURCE_ITEM_FIELDS与itertools.chain消除{**{...}, ...}二次构 dict 与中间合并列表分配。payload_work_lane_contract(loopx/control_plane/quota/task_orchestration.py#L162-L188):接收只读Mapping[str, object],保持OUTCOME_FLOOR_RECOVERY抢占与AGENT_SCOPE延迟推演,对非法非映射返回None而对普通dict原样返回零拷贝。_task_orchestration_contract(loopx/control_plane/quota/task_orchestration.py#L280-L360):提前判定peer_active与adaptive_eligible(显式not isinstance(max_children, bool))并在双否时跳过能力归一化;对等体阻塞时保留peer_activation_diagnostic供自适应回退携带。_registered_peer_task_orchestration_contract(loopx/control_plane/quota/task_orchestration.py#L363-L429)与_task_orchestration_work_lane_contract(#L432-L454):严格保持agent_todo_source_items=[]优先于过期显示摘要、quota.peer_orchestration.project非映射返回值安全收口以及WORK_LANE_CONTRACT_SCHEMA_VERSION单一真源导出。
规范核对(Specification Mapping)
依据已验收协议文档 docs/reference/protocols/peer-agent-runtime-v1.md(审阅基线提交 1af7dbd43a1629b0356ecd5ee28a426ad45d1d7a)逐条核对:
PAR-TOC-1:implemented — 任务级对等体编排按排序后的_peer_lane_refs哈希生成确定性assignment_key,不受清单或注册顺序影响且不引入持久代理等级。PAR-TOC-2:implemented — 严格保留agent_todo_source_items=[]优先权,且对缺失能力、阻塞就绪态、畸形运行时返回值及max_children=True均做到 fail-closed 回退。PAR-TOC-3:implemented —_task_orchestration_work_lane_contract继续引用单源WORK_LANE_CONTRACT_SCHEMA_VERSION常量,且混合模式完整保留peer_activation_diagnostic与自适应子任务准入语义。
对主干的风险 / Risks
- 反例探针与边界跟踪(非阻塞):
_task_orchestration_contract#L292的最外层goal_boundary与agent_identity仍沿用dict检查(与尚未重构的task_orchestration_admission.py对齐),但对已核查的全部生产调用方(should_run_prepare.py、should_run_packet.py)完全兼容,内层映射与其余辅助接口均已全面支持MappingProxyType。无新阻塞缺陷。 - 调用面、隔离与合并兼容:
default_off_isolation: isolated(双否目标在normalize_required_capabilities前直接返回None)、authority_semantics: aligned、领域中立术语保留不变;与main及同批在审 PR(#9463、#9467、#9469、#9470)零文件重叠。 - 验证矩阵(
wait_for_ci: false,ci_policy: not_consulted):ruff check(2/2)与mypy --strict --no-incremental --follow-imports=silent(2/2):PASS(0ASTAny,0 # type: ignore,0 # noqa)。- 6 个调用方/关联模块(
task_orchestration、should_run_prepare、should_run_packet、should_run、task_orchestration_admission、quota_fixtures)基线57c4c43b52vs9548e47e33mypy(silent 与 transitive):PASS(+0/-0,均为 0 错误)。 - 配对反事实(
57c4c43b52vs9548e47e33,连同真实 Node TS handler)与MappingProxyType一致性及两例源码变异敏感性测试:PASS(合法路径 100% 一致;max_children=True基线穿透为max_children=1,本 PR 安全拦截;两例变异均精准在断言处rc=42失败)。 - TypeScript:
npm run -s typecheck:control-plane与 4 套control_plane_ts测试(peer_orchestration、content_digest_single_owner、adaptive_children、quota_run_decision):PASS(78/78)。 - 10 项清净
HOMEsmoke(词表漂移、可维护性棘轮、work-lane、quota-work-lane-policy、quota-replan、quota-scheduler-registry-route、quota-action-scope-guard、quota-scheduler-hint-compaction、task-orchestration、peer-agent-runtime-v1):PASS(10/10)。 pytest -q -n 4 -rs(控制面编排/准入/配额/信封/边界/生命周期 +tests/architecture/+tests/canary/):PASS(3674 passed, 7 skipped,其中 1 项 Windows PowerShell 与 6 项 APFS 文件名预期跳过,0失败)。check_rfc_bilingual、loopx check(errors=0)与canary premerge --profile standard(direct=6/6、catalog=7/7、risk_profile=8/8、failures=0、manual_hold_count=0):PASS。- 远端 GitHub Actions CI:UNVERIFIED(依
wait_for_ci=false策略未查询/等待远端 CI)。
我的整体评价 / Verdict
- 长程与用户体验影响(
outcome_impact):long_horizon: improved(拦截布尔max_children=True误准入并维持全部已有调度语义不漂移),user_experience: preserved;视觉审阅visual_review: not_applicable(原因:纯 Python 控制面类型收紧、内存分配简化与契约单元测试,不涉及前端界面或渲染页面改动),unverified_scope: none。 - 精简度与词表对齐:
code_volume: necessary且compatibility_assessment: retain,change_proportionality: proportionate(architecture_assessment: retain),default_off_isolation: isolated,authority_semantics: aligned,semantic_alignment: aligned / reuse_existing,observable_semantics: equivalent(合法路径完全等价且布尔配置防护严格收紧)。已基于精确提交9548e47e33b0d35b8c72d92dc56adaf6923dcfaa完成独立核验,无阻塞缺陷,建议通过。
English Verdict: APPROVE
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
收紧 Python quota/Work Lane 的 orchestration 输入与投影,保留已有 registered-peer 和 adaptive 两类准入。 基线在发现可选编排未启用时仍规整 capability 列表,路由合并和 peer assignment 使用临时字典、列表。准入判断已由 TypeScript 拥有。 通过 quota should-run 获取当前 Agent 或 Task Bundle 行动建议的 Codex host 和其他现有调用者。
准入变化可能把未授权的候选显示为可执行,或让当前 Agent 失去本应继续的独立 lane。 双版本 File 和 SQLite 的真实 CLI 输出一致。启用 adaptive 并声明 subagent_spawn 时,主 Todo 保持原绑定,独立 Todo 成为唯一可用 child,缺 network 的候选带 capability_unavailable 留在 blocked_lanes。恢复该能力后,同一候选入队,域外候选仍拒绝。新增 canonical Todo 按同一授权进入容量。 基线 176、Head 183 项定向测试通过。两版 task-orchestration-smoke 通过。独立真实 CLI 覆盖 File 和 SQLite 各六种 gate 场景,读路径保持 canonical provider 和 Markdown。额外反例核对 registry 与内部 helper 的布尔容量,见下文。git diff --check 通过。 CLI 试验声明的是隔离环境的模拟能力。只验证准入与投影。实际子 Agent 执行、生产耗时、当前安装和远程 CI 未核验。 基线契约
改动思路 / Approach
该改动沿用 quota should-run → apply_task_orchestration_contract → TypeScript peer/adaptive admission → Work Lane。沿用原 orchestration bridge 与 TypeScript admit/project。只在可能编排时准备 capability,组装 Work Lane 时保留原状态、assignment 与 fallback。 显式 peer authority、runtime capability 和 canonical candidate 必须同时满足。Blocked peer 诊断无法授权另一条 lane,合法 adaptive 仍可按独立规则进入。canonical 空列表优先于显示行。 复用已存在的 peer_work_key、work_lane schema 和 capability decoder。减少局部中间容器,维持原返回字段和控制面 owner。
具体改动 / Changes
精确提交 9548e47,分叉基线 57c4c43。2 个文件,新增 669 行、删除 155 行。427 行为测试。保留原模块入口并缩窄动态输入。
关键代码讲解
-
_task_orchestration_contract。按 Goal 与 agent 资格筛出 peer/adaptive,再读取相应的 host capability。 源码 -
_registered_peer_task_orchestration_contract。读原 canonical 候选与已观察的 Agent 状态,交给 TypeScript 后生成排序稳定的 assignment_key。 源码 -
apply_task_orchestration_contract。只为可执行 contract 投影 bundle lane,blocked 时保留已有 fallback。 源码 -
payload_work_lane_contract。保留 recovery preemption、Agent wait 降级与原工作 lane 签名。 源码 -
preserve-observed-capability-and-coordination-gates(implemented)。只有明确配置与运行时能力同时满足,符合授权范围的 Todo 才能进入对应编排。阻断项保留 typed reason,未开路径保持原动作。
对主干的风险 / Risks
两版在 Goal 未开启、当前 host 未声明 spawn 或 registry 把 max_children 配成布尔值时,都保留 normal_run 和 advancement_task。直接传给 Python helper 的 True 在基线会变成容量 1,Head 拒绝;实际 registry 原有归一化在两版都先将其映射为 0。配对烟测在无 peer activation 时保留原 dispatcher 门槛,有显式 coordinator 和能力才组织可用 peers。 没有先验有效的 runtime capability 时仍输出可执行 bundle,会让 host 消费错误的下一步。 从观察到的能力、明确配置和当前 Todo 推导,只消费现有结构化 contract。
我的整体评价 / Verdict
APPROVE。 实际双后端 CLI、反例和同版烟测共同支持当前版本的准入、fallback 和字段行为。 保留缺能力和候选不可用的原因,Agent 能沿正确的 fallback 继续或等待,原 canonical 结果便于后续轮次读回。
English verdict: APPROVE at 9548e47. Exact-base/head File and SQLite quota decisions preserve default-off behavior, adaptive admission, and typed blocked lanes. Paired peer/adaptive smokes passed.
Summary
Eliminates all
Anyannotations (51 -> 0ASTAny,0# type: ignore,0# noqa) and removes redundant runtime allocations inloopx/control_plane/quota/task_orchestration.py, keeping the module compact in-place (367 -> 454LOC, well under550LOC) with zero top-level module additions (loopx/*.py == 112).Changes
AnyTyped Contracts & Read-Only Mapping Safety (loopx/control_plane/quota/task_orchestration.py):dict[str, Any]/Anyannotations andcast(...)calls withMapping[str, object]/dict[str, object]and explicit narrowing helpers (_as_dict_or_none,_list_or_empty,_peer_lane_refs), handlingtypes.MappingProxyTypeand malformed/non-mapping runtime responses cleanly.WORK_LANE_CONTRACT_SCHEMA_VERSIONsingle-owner import (tests/architecture/test_work_lane_contract_schema_version_single_owner.py) and exact_task_orchestration_work_lane_contractcontract/behavior, as well as strict boolean exclusion onmax_children(not isinstance(max_children, bool)).apply_task_route_hint_override: evaluatesomit_current_agent_next_actiononce rather than per key and merges overrides without allocating an intermediate filtered dict._agent_scope_payload_work_lane_contract: cachesreason_codesand_DEFERRED_WORK_LANE_FIELDSlookups once per key while preserving key-presence vsNonesemantics indeferred_work_lane._registered_peer_task_orchestration_contract&_task_orchestration_contract: hoists_PEER_SOURCE_ITEM_FIELDS, avoids{**{...}, ...}double-dict allocation per item and intermediatelanesconcatenation viaitertools.chain, preserves explicitagent_todo_source_items=[]empty-list precedence, and short-circuits_task_orchestration_contractbefore normalizing capabilities when neither peer coordination nor adaptive subagent orchestration is eligible.tests/control_plane/test_task_orchestration_contract.py,427LOC,0ASTAny):0-Any/0-ignore/LOC/single-owner invariants,MappingProxyTypesupport across all read-only helpers,apply_task_route_hint_overridenon-mutation/omission, material-change stop gates,OUTCOME_FLOOR_RECOVERYpreemption and deferredwork_laneshape,agent_todo_source_items=[]precedence,assignment_keyorder invariance, hybrid blocked-peer adaptive fallback (peer_activation_diagnostic), boolmax_children, and malformed runtime response resilience.Verification
ruff check(2/2touched files): passed.mypy --strict --no-incremental --follow-imports=silent(task_orchestration.py,test_task_orchestration_contract.py):0issues.mypy --no-incrementalacrosstask_orchestration.py,should_run_prepare.py,should_run_packet.py,should_run.py, andtest_task_orchestration_contract.py:0issues.57c4c43b52vs9548e47e33counterfactual across all public helpers,_task_orchestration_work_lane_contract, empty-source-items precedence, hybrid blocked-peer adaptive fallback, and inactive short-circuit: exact JSON parity.npm run typecheck:control-planeclean;node --experimental-strip-types --test tests/control_plane_ts/peer_orchestration.test.ts tests/control_plane_ts/content_digest_single_owner.test.ts:21/21passed.scripts/check_rfc_bilingual.py --base origin/main --head HEADpassed;git merge-tree --write-tree origin/main HEADclean (6f153334c83382136b81fbdce9fdb8520819500c).HOMEsmokes:semantic-vocabulary-drift-smoke.py+7/7quota/work-lane/maintainability smokes (control-plane-maintainability-ratchet-smoke.py,work-lane-contract-smoke.py,quota-work-lane-policy-smoke.py,quota-replan-decision-plane-smoke.py,quota-scheduler-registry-route-smoke.py,quota-action-scope-guard-smoke.py,quota-scheduler-hint-compaction-smoke.py) passed.pytest -n 4 -rs(tests/control_plane/test_task_orchestration_contract.py,test_task_orchestration_admission.py,test_adaptive_child_admission_characterization.py,test_quota_*.py,test_agent_lifecycle_state.py,test_goal_boundary_resolution.py,tests/test_turn_envelope.py,tests/test_quota*.py,tests/architecture/,tests/canary/):3,674 passed, 7 skipped(1 Windows PowerShell + 6 APFS filename skips,0failures).standardtier): passed (direct_checks=4/4,python_compile=1/1,catalog_run=7/7,risk_profile_run=8/8,failures=0,manual_hold_count=0).