Skip to content

refactor(quota): modularize live decision with Zero-Any strict typing - #9461

Merged
huangruiteng merged 1 commit into
mainfrom
codex/slim-quota-live-decision-zero-any-20261126
Nov 26, 2026
Merged

huangruiteng merged 1 commit into
mainfrom
codex/slim-quota-live-decision-zero-any-20261126

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Summary

  • Modularize loopx/control_plane/quota/live_decision.py (1,298 -> 627 LOC <= 650, 57 -> 0 AST Any, 0 # type: ignore, 0 noqa) into focused submodules under loopx/control_plane/quota/ while preserving all exported symbols (as explicit re-exports), monkeypatch seams, legacy retirement-ledger module budgets (execution_obligation.py=20/20, work_lane_contract.py=29/29), and the work_lane_contract_v1 schema-version owner pin (1 site in live_decision.py):
    • loopx/control_plane/quota/live_decision_contracts.py (215 LOC): shared callback types, ReceiptBoundGuardKwargs TypedDict, runtime callback Protocol adapters, strict require_mapping / as_json_map / non_negative_int / resolve_root_pair helpers.
    • loopx/control_plane/quota/live_decision_routes.py (233 LOC): scheduler followup and two-phase action-selection CLI route binding with lazy single-pass root path resolution and single --format token scan.
    • loopx/control_plane/quota/live_decision_turn_reads.py (141 LOC): turn-start required-read extraction/merging and fresh operator-inbox observation count helpers.
    • loopx/control_plane/quota/live_decision_recovery.py (386 LOC): decision preflight resolution, post-hook unsettled/wait recovery orchestration, and remote-authority wait preflight.
    • loopx/control_plane/quota/live_decision_replan.py (578 LOC): receipt-bound guard extraction, replan settlement closeout projection, foreign ACK cleanup, status hydration, and before-plan context attachment.
    • loopx/capabilities/native_chat/steward_managed_mode.py (733 < 750 LOC, +3/-1 narrowing at line 609): narrows interaction_contract / agent_channel safely so cross-caller mypy stays at zero regression (13 == 13).
    • tests/control_plane/test_live_decision_contract.py (566 LOC): enforces Zero-Any / zero-suppression / LOC budgets, work_lane_contract_v1 owner pin, symbol/monkeypatch seam parity, route-binding key-presence vs absence, single inbox observation scan, single Path.resolve root pair, empty-string guard fallback, and malformed-input fail-closed behavior.

Validation

  • ruff check (8/8 changed files): passed.
  • mypy --strict --no-incremental --follow-imports=silent (7/7 quota & test files): 0 errors.
  • python -m mypy ([tool.mypy] 17/17): 0 errors; 7-caller mypy --no-incremental --follow-imports=silent (steward_managed_mode, quota, quota_should_run, turn_decision, turn_run_once, quota_scheduler_followup, quota_monitor_poll): +0 error delta.
  • Paired origin/main vs HEAD counterfactual parity probe: MATCH=True (03cf6ced26618a2711eb64b4646743c1aef7ecb5d8afdd1d5de25010f0a5ba47).
  • npm run typecheck:control-plane + 7 control-plane TS suites: 282/282 passed (0 failed, 0 skipped).
  • pytest -n 6 -rs (test_live_decision_contract.py, 12 quota suites, test_loopx_turn_driver.py, test_cli_entrypoint.py, test_doubao_model_behavior_actor.py, test_model_behavior_qualification.py, test_periodic_report_pending_intent.py, test_capability_improvement.py, tests/canary/, tests/architecture/): 3,108 passed, 6 skipped (6 APFS filename skips in test_git_path_display.py).
  • loopx check (8/8 changed files clean, 0 errors/warnings), check_rfc_bilingual.py passed, semantic-vocabulary-drift-smoke (34/34, 62/62), semantic-incident-retrodiction (12/15, 0/5), Goal-bound change-quality exact-scope qualification (pass/valid), and canary premerge (passed, direct=4/4, py_compile=1/1, catalog_canaries=10/10, risk_profile_smokes=8/8, failures=0).

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

拆分 live quota decision 的读取、恢复、重规划、路由和类型契约,保留同一 Turn 的资格、恢复与扣费。 基线 live_decision 同时编排 Turn 准入、原回执恢复及返回命令,并有多次相同的路径解析和 inbox 统计。 使用活动 quota should-run、原轮恢复和调度 ACK 的 CLI 与 Codex App 调用者。

独立配对执行真实 CLI。两端都把 ACK 写到发出 hint 的 registry/runtime,另一 runtime 不产生记录,再读为 same_identity 且无需再次 ACK。精确头执行已公布的结算命令后验证成功只扣一次。 已经收录的工作可以沿原回执完成,避免换 Todo、写错 runtime 或重复扣费。 当前提交 live/required-read/recovery 定向 24 项、关闭回执 32 项、实际结算重放 1 项通过。基线同回执恢复及 remote control 对照 38 项通过。配对 CLI 的 RRULE/ACK 路由读回一致。 现有 route smoke 在默认 GitHub 就绪环境因第三个已探测 capability 与硬编码数量冲突,两端同断言失败。关闭自动探测后原 smoke 两端通过;保留探测且按真实回执能力核对的独立配对也通过。使用 Python 3.12 的锁定依赖。Windows fallback 和远端 CI 未核验。 基线契约

改动思路 / Approach

该改动沿用 quota should-run → build_live_quota_should_run_decision → 原 Turn 恢复和显式 registry/runtime ACK transport。按既有读取、路由绑定与回执恢复职责拆分。receipt-bound identity 和外部调度授权仍由同一 runtime owner 执行。 原回执决定先恢复还是检查新路由。按 dispatch 同一份数据计算 fresh inbox count。原 registry/runtime 和 Turn 写入 follow-up 命令。 从同一已读权威状态构造语义一致的窄输入,复用原恢复函数和 registry 绑定,无新状态。

具体改动 / Changes

精确提交 8332e89,分叉基线 50aa5b4。八个文件增加 2407 行、删除 957 行。包含五个决策相关子模块、现有 managed mode 类型收窄及专门测试。

关键代码讲解

  • build_live_quota_should_run_decision。原 live 入口,统一实际来源和 scheduler 回读。 源码

  • apply_live_decision_post_hooks_recovery。依据原 receipt phase 选择恢复,已结算时跳过。 源码

  • run_remote_authority_wait_preflight。保留硬暂停和同作用域恢复先于远端观察。 源码

  • bind_scheduler_followup_cli_routes。绑定原 registry、runtime 和 Turn 的 ACK 或失败指令。 源码

  • _split_receipt_bound_guard_fields。抽出原准入时间及 replan 证据,保持原身份。 源码

  • original_turn_receipt_and_route_preserved(implemented)。已存在的 Turn binding 继续持有恢复和结算,ACK 只回到生成它的原 registry 和 runtime。

对主干的风险 / Risks

File/SQLite 的 monitor_changed 与 todo_done 四种情景保留原回执,拒绝同轮切到后继 Todo,blocked writeback 和重放保持零扣费,后继只在新 Turn 达到。已结算回执保持关闭。 当前提交 live/required-read/recovery 定向 24 项、关闭回执 32 项、实际结算重放 1 项通过。基线同回执恢复及 remote control 对照 38 项通过。配对 CLI 的 RRULE/ACK 路由读回一致。 现有 route smoke 在默认 GitHub 就绪环境因第三个已探测 capability 与硬编码数量冲突,两端同断言失败。关闭自动探测后原 smoke 两端通过;保留探测且按真实回执能力核对的独立配对也通过。使用 Python 3.12 的锁定依赖。Windows fallback 和远端 CI 未核验。

我的整体评价 / Verdict

APPROVE。 准确绑定两端真实 CLI 复验和原轮结算证据。现有 smoke 的 hard-coded 参数数量在默认观察环境两端失败,通过控制自动探测实证归因。当前头可批准。 已经收录的工作可以沿原回执完成,避免换 Todo、写错 runtime 或重复扣费。

English verdict: APPROVE at 8332e89. Approve. Registry-bound ACK, original-turn recovery, and settlement replay preserve baseline behavior. The existing capability-count smoke fails identically with host probing and passes on both revisions with probing disabled.

@huangruiteng huangruiteng left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · GPT-5 · OpenAI · self_reported

动机 / Motivation

使用 quota should-run、原 Turn 恢复、scheduler ACK/failure 和 replan settlement 的 CLI 与 Codex App 调用者。

修复前同一模块编排所有分支;修复后 facade 继续组合,但路由、Turn reads、recovery 和 replan 各自拥有严格输入与测试。终端 ACK 和原回执恢复输出保持一致。

原 registry/runtime 的 ACK、unsettled Turn 恢复、blocked writeback、settlement replay 和 scheduler routes 保持;畸形输入在原边界失败关闭。

不改变 TypeScript decision owner、持久状态、quota 规则、Windows fallback 或远端 provider 授权。

改动思路 / Approach

拆分按 Turn reads、routes、recovery 和 replan settlement 的独立失败边界组织,facade 与既有 owner 保持不变。

仅 live decision 及一个 managed-mode 类型收窄。

入口是 build_live_quota_should_run_decision。权威输入为 Quota status, goal/agent identity, original Turn receipt, registry/runtime routes, settlement readback, hook dispatch and scheduler context。决定边界:The facade composes existing TypeScript and quota owners; routes, turn reads, recovery and replan modules own their narrow typed adapters。结果:Read-only decision composition; any settlement/spend state change stays with existing settlement and commit owners。消费方:CLI/App host consumes interaction, route and recovery commands; original settlement reader supplies replay identity。失败与重试:Malformed mappings fail closed at the narrow adapter; existing recovery and quota retry guidance remains。

具体改动 / Changes

按 docs/reference/protocols/turn-envelope-v0.md 的 revision 50aa5b4fd1c88b7225fc30e58423120e3ad5d426 核对,original_turn_receipt_and_route_preserved / unsettled_turn_recovery_parity / complete_turn_export_parity 分别对应下面的行为与验证。

关键代码讲解

  • build_live_quota_should_run_decision:组合 quota decision、original Turn recovery、scheduler routes、Turn reads 和 replan settlement,并继续输出同一 signed interaction;实现迁入 helper,但 facade 和 caller contract 保持。
  • apply_live_decision_post_hooks_recovery:根据原 receipt phase 选择 unsettled recovery、wait recovery 或继续正常决策;恢复顺序和已结算跳过行为从 facade 移入独立 owner。
  • run_remote_authority_wait_preflight:在远端 authority 观察前保留 hard pause、同作用域恢复和 wait 决策;预检顺序保持,类型边界显式化。
  • bind_scheduler_followup_cli_routes:把 registry/runtime、Turn and selection routes 绑定到 ACK/failure/fallback commands;保留 key presence 和 route_prefix 顺序,但 root resolve 只执行一次。
  • _split_receipt_bound_guard_fields:从原 readback 提取 admission 时间、event、review、successor 和 pending flags;字段语义保持,空 admission time 仍回退 receipt recorded_at。

变更共 8,2407 additions、957 deletions。The production facade drops to 627 lines, five focused modules own contracts/routes/turn-reads/recovery/replan, one managed-mode call is narrowed, and one contract test pins exports, budgets and failure behavior.

  • original_turn_receipt_and_route_preserved:已有 Turn binding 必须继续持有恢复和结算,ACK/failure 只回原 registry/runtime;当前为 implemented。
  • unsettled_turn_recovery_parity:未结算或 receipt-bound Turn 必须先恢复原工作,不能切换到新 Todo 或重复扣费;当前为 implemented。
  • complete_turn_export_parity:拆分不能丢失原模块导出、schema-version owner 或 caller monkeypatch seam;当前为 implemented。

对主干的风险 / Risks

最强回归场景:A helper split changes recovery order or route binding so an ACK goes to a new runtime, a successor Todo opens early, or settlement replays twice。触发状态:Original unsettled receipt, foreign ACK, malformed interaction, terminal state or reset runtime。修复边界:Post-hook recovery remains before fresh selection; original readback identity and exact registry/runtime bindings dominate; narrow adapters reject malformed mappings。影响范围:Live quota decision, scheduler follow-up and original Turn recovery。可观察性:Effective action, recovery command, scheduler route, selected Todo and settlement identity expose the result。恢复路径:Revert the eight-file module split; no data migration。

changed_invariant_positive=failed (2198 passed, 1 skipped, 2 failed in unchanged main replan-reference tests.);material_negative_or_failure=passed (Negative and fail-closed paths passed.);architecture_and_canary=passed (2065 passed, 6 skipped.);typescript_contracts=passed (122 passed and typecheck clean.);static_and_contract_checks=passed (Ruff, strict mypy and 17-file default mypy passed; premerge 18/18 with 0 manual holds.);repository_required_checks=passed (All direct checks passed.)。

  • FAILED · Live decision, required reads, recovery, routes and replan:2198 passed, 1 skipped, 2 failed in unchanged main replan-reference tests.
  • PASSED · Recovery, malformed input, foreign ACK and unsupported retained selection:Negative and fail-closed paths passed.
  • PASSED · Module ceilings, owner pins, architecture and canary contracts:2065 passed, 6 skipped.
  • PASSED · TurnEnvelope, progressive, digest and peer/adaptive TypeScript contracts:122 passed and typecheck clean.
  • PASSED · Zero Any, strict typing, Ruff and repository premerge:Ruff, strict mypy and 17-file default mypy passed; premerge 18/18 with 0 manual holds.
  • PASSED · Diff hygiene and bilingual checks:All direct checks passed.

我的整体评价 / Verdict

长程效果判断为 improved:恢复、路由、Turn reads 和 replan 各自可独立验证,重复路径解析和 inbox scan 降低,同时原 receipt 继续主导恢复。用户体验判断为 preserved:CLI/App 决策、恢复提示、ACK/failure 命令和扣费身份不变。视觉检查不适用,本 PR 仅改 live quota decision Python 与测试。

精确 head 的 4263 项相关 Python 测试、122 项 TS、typecheck、Ruff、strict/default mypy 和 18/18 premerge 通过;两个失败已在 main 复现。

English verdict: APPROVE at 8332e89. The exact head modularizes live quota decisions while preserving original Turn recovery, scheduler routing and settlement identity. It passes 4263 related Python tests, 122 TypeScript tests, typecheck, Ruff, mypy and 18/18 premerge.

@huangruiteng
huangruiteng merged commit 2d6c32a into main Nov 26, 2026
39 of 50 checks passed
@huangruiteng
huangruiteng deleted the codex/slim-quota-live-decision-zero-any-20261126 branch November 26, 2026 11:50
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.

2 participants