Skip to content

refactor(quota): eliminate Any typing and redundant allocations in task_orchestration - #9468

Open
loopx-agent wants to merge 1 commit into
mainfrom
codex/slim-task-orchestration-zero-any-20261126
Open

loopx-agent wants to merge 1 commit into
mainfrom
codex/slim-task-orchestration-zero-any-20261126

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Summary

Eliminates all Any annotations (51 -> 0 AST Any, 0 # type: ignore, 0 # noqa) and removes redundant runtime allocations in loopx/control_plane/quota/task_orchestration.py, keeping the module compact in-place (367 -> 454 LOC, well under 550 LOC) with zero top-level module additions (loopx/*.py == 112).

Changes

  • Zero-Any Typed Contracts & Read-Only Mapping Safety (loopx/control_plane/quota/task_orchestration.py):
    • Replaced all dict[str, Any] / Any annotations and cast(...) calls with Mapping[str, object] / dict[str, object] and explicit narrowing helpers (_as_dict_or_none, _list_or_empty, _peer_lane_refs), handling types.MappingProxyType and malformed/non-mapping runtime responses cleanly.
    • Preserved the architecture-pinned WORK_LANE_CONTRACT_SCHEMA_VERSION single-owner import (tests/architecture/test_work_lane_contract_schema_version_single_owner.py) and exact _task_orchestration_work_lane_contract contract/behavior, as well as strict boolean exclusion on max_children (not isinstance(max_children, bool)).
  • Redundant Allocation & Lookup Removal:
    • apply_task_route_hint_override: evaluates omit_current_agent_next_action once rather than per key and merges overrides without allocating an intermediate filtered dict.
    • _agent_scope_payload_work_lane_contract: caches reason_codes and _DEFERRED_WORK_LANE_FIELDS lookups once per key while preserving key-presence vs None semantics in deferred_work_lane.
    • _registered_peer_task_orchestration_contract & _task_orchestration_contract: hoists _PEER_SOURCE_ITEM_FIELDS, avoids {**{...}, ...} double-dict allocation per item and intermediate lanes concatenation via itertools.chain, preserves explicit agent_todo_source_items=[] empty-list precedence, and short-circuits _task_orchestration_contract before normalizing capabilities when neither peer coordination nor adaptive subagent orchestration is eligible.
  • Contract & Parity Tests (tests/control_plane/test_task_orchestration_contract.py, 427 LOC, 0 AST Any):
    • Covers AST 0-Any/0-ignore/LOC/single-owner invariants, MappingProxyType support across all read-only helpers, apply_task_route_hint_override non-mutation/omission, material-change stop gates, OUTCOME_FLOOR_RECOVERY preemption and deferred work_lane shape, agent_todo_source_items=[] precedence, assignment_key order invariance, hybrid blocked-peer adaptive fallback (peer_activation_diagnostic), bool max_children, and malformed runtime response resilience.

Verification

  • ruff check (2/2 touched files): passed.
  • mypy --strict --no-incremental --follow-imports=silent (task_orchestration.py, test_task_orchestration_contract.py): 0 issues.
  • mypy --no-incremental across task_orchestration.py, should_run_prepare.py, should_run_packet.py, should_run.py, and test_task_orchestration_contract.py: 0 issues.
  • Paired 57c4c43b52 vs 9548e47e33 counterfactual 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-plane clean; node --experimental-strip-types --test tests/control_plane_ts/peer_orchestration.test.ts tests/control_plane_ts/content_digest_single_owner.test.ts: 21/21 passed.
  • scripts/check_rfc_bilingual.py --base origin/main --head HEAD passed; git merge-tree --write-tree origin/main HEAD clean (6f153334c83382136b81fbdce9fdb8520819500c).
  • Clean-HOME smokes: semantic-vocabulary-drift-smoke.py + 7/7 quota/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, 0 failures).
  • Pre-merge canary gate (standard tier): passed (direct_checks=4/4, python_compile=1/1, catalog_run=7/7, risk_profile_run=8/8, failures=0, manual_hold_count=0).

…sk_orchestration

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

本 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(0 AST Any,0 # type: ignore,0 # noqa)。
    • 6 个调用方/关联模块(task_orchestration、should_run_prepare、should_run_packet、should_run、task_orchestration_admission、quota_fixtures)基线 57c4c43b52 vs 9548e47e33 mypy(silent 与 transitive):PASS(+0/-0,均为 0 错误)。
    • 配对反事实(57c4c43b52 vs 9548e47e33,连同真实 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 项清净 HOME smoke(词表漂移、可维护性棘轮、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 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

收紧 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.

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