Skip to content

refactor(quota): modularize slot_accounting with zero Any and lazy index scan - #9465

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

huangruiteng merged 1 commit into
mainfrom
codex/slim-quota-slot-accounting-zero-any-20261126

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Summary

Modularizes loopx/control_plane/quota/slot_accounting.py (998 -> 323 LOC, 54 -> 0 AST Any) into cohesive, strict Zero-Any TypeScript/Python-boundary modules while preserving all architecture pins, re-export identities, error/key-presence semantics, and monkeypatch seams:

  • loopx/control_plane/quota/slot_accounting.py (323 LOC, 0 AST Any, 0 # type: ignore, 0 # noqa, max 36 statements / 33 decision points):
    • Retains the compatibility facade (build_quota_slot_preview_for_decision, _latest_unspent_turn_settlement_run, DELIVERY_COMPLETION_SPEND_STATES, TERMINAL_NO_FOLLOWUP_DECISION_STATE) and preserves exact object-identity re-exports for spend_commit, void_commit, ledger_readback, and paths.validate_goal_id_path_segment (_validate_goal_id_path_segment).
    • Keeps _latest_unspent_turn_settlement_run defined at top level in slot_accounting.py so loopx/semantics/goal_instance_binding_inventory_v1.json and direct callers/monkeypatch seams remain intact.
  • loopx/control_plane/quota/slot_preview_settlement.py (399 LOC, 0 AST Any, 0 # type: ignore, 0 # noqa, max 29 statements / 18 decision points):
    • Extracts PreviewSettlementResolution, resolve_preview_settlement, todo_binding_error, receipt_committed, unbound_visible_goal_settlement_failure, admits_delivery_completion_spend_state, and run-index selection helpers.
    • Adds iter_reversed_goal_run_index_records so _latest_unspent_turn_settlement_run parses runs/index.jsonl lazily from newest records first and stops at the latest spend/qualifying run without JSON-decoding older historical entries, while preserving missing/unreadable/corrupt/non-dict line skipping.
  • loopx/control_plane/quota/slot_preview_workspace.py (520 LOC, 0 AST Any, 0 # type: ignore, 0 # noqa, max 19 statements / 14 decision points):
    • Extracts DeliveryWorkspacePreviewState, evaluate_delivery_workspace_preview, repair_settlement_workspace_causality, settlement_workspace_requirement, missing_delivery_workspace_preview, workspace_repair_blocks_spend, queue_item_for_goal, set_quota_for_goal, and build_preview_success_payload.
    • Unifies repeated spend-preview refusal payloads through preview_failure_payload while preserving exact key-presence vs None separation across all refusal branches (including quota_rejection_action, settlement_result, and workspace resolution fields).
  • tests/control_plane/test_slot_accounting_contract.py (618 LOC, 0 AST Any, 0 # type: ignore, 0 # noqa):
    • Verifies LOC budgets, 0 AST Any / # type: ignore / # noqa, per-function AST complexity ceilings, single-owner validate_goal_id_path_segment and re-export identities, goal-id traversal fail-closed behavior, key-presence vs None across all refusal modes, terminal_no_followup / workspace_repair_blocks_spend gating, lazy tail-scan JSON parse bounds and malformed-input handling, monkeypatch seams, and status_payload deepcopy mutation isolation.

Boundary & Compatibility Checks

  • slot_accounting._validate_goal_id_path_segment is paths.validate_goal_id_path_segment (tests/architecture/test_goal_id_path_segment_owner.py)
  • render_quota_slot_preview_markdown absent from slot_accounting.py (tests/architecture/test_control_plane_import_boundaries.py)
  • loopx/canary/module_metric_baseline.json untouched; top-level loopx/*.py == 112
  • 0 retirement-ledger legacy tokens added in new or changed modules

Verification

  • Static checks (74bd2406ff):
    • ruff check (4/4 changed/added files): passed
    • mypy --strict --no-incremental --follow-imports=silent (4/4 changed/added files): 0 errors
    • mypy --no-incremental ([tool.mypy], 17 files): 0 errors
    • Quota caller mypy --no-incremental --follow-imports=silent (36 Python callers): 213 -> 213 (+0 regressions)
    • npm run typecheck:control-plane: passed
    • scripts/check_rfc_bilingual.py --base origin/main --head HEAD: passed
    • git diff --check origin/main...HEAD & git merge-tree --write-tree origin/main HEAD: clean
  • Test & smoke runs (74bd2406ff):
    • node --no-warnings --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/content_digest_single_owner.test.ts: 17/17 passed
    • examples/control_plane/quota-spend-agent-identity-smoke.py & examples/control_plane/quota-spend-workspace-causality-smoke.py: passed
    • pytest -n 6 -rs (test_slot_accounting_contract.py, test_quota_slot_accounting.py, test_quota_void_commit_runtime.py, test_quota_spend_commit_runtime.py, tests/architecture/, tests/canary/, tests/test_cli_entrypoint.py): 2,466 passed, 6 skipped (6 APFS filename skips in tests/canary/test_git_path_display.py, 0 failed)
    • loopx canary premerge --from-git-diff --git-diff-base origin/main (standard): ok=true, merge_gate_passed=true, direct_checks=4/4, python_compile=1/1, catalog_run=7/7, risk_profile_run=8/8, manual_hold_count=0

…dex scan

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

把扣额预览拆为结算、工作区和显示投影,减少候选位于索引尾部时的 JSON 解码。 基线门面承担结算和工作区校验,并在选候选前解码完整 Run index。 经 quota slot preview 和 refresh-state 读取扣额与交付回执的 Goal 执行者。

当有效候选靠近尾部,历次索引条目仍被解码。若 owner 或终止语义漂移,则可能消费另一轮的交付或误报可扣额度。 独立双版本样例中,匹配候选和接下来的额度预览相同。别的 Agent、退休 GoalRef 和中性事件夹在其后时,尾部候选的选择器 JSON 解码由 3,208 次降到 8 次。预览在内存中把三个额度视图从 2 调至 3,源字节保持。 Head 101 项定向测试通过,基线已有测试 92 项。精确基线与 Head 各通过 4 项 File/SQLite 真实 CLI 回归。另有 5 个双版本文件索引用例。 解码数只统计候选选择器。实现仍整文件读取并切分行,严格结算读回独立。未测生产耗时、当前安装或远程 CI。 基线契约

改动思路 / Approach

该改动沿用 quota slot preview → slot_accounting → 结算读回、工作区校验和投影。Python 门面保留原入口和重导出。结算的查询和候选选择与工作区规则按既有职责拆开,失败继续使用原有 typed 回执及调用级字段形状。 先绑定原 Goal/Agent/Todo/Turn,再判断 workspace 因果性和可扣额资格。未绑定的候选选择保留原 owner 过滤、终止事件和原文件行序。 只在已有尾部选择器延迟解码。保留整文件入口、回滚顺序与既有回执接口,不新增索引或数据迁移。

具体改动 / Changes

精确提交 74bd240,分叉基线 57c4c43。4 个文件,新增 1,722 行、删除 860 行。其中 618 行是契约测试,生产重构净增 244 行。

关键代码讲解

  • select_latest_unspent_turn_settlement_run。按反向文件行序和相同身份识别当下的合法候选及停止事件。 源码

  • resolve_preview_settlement。保留原 typed 结算结果、工作区因果性与已提交步骤读回。 源码

  • evaluate_delivery_workspace_preview。沿用工作区的要求、修复和错误结果,并保持失败 JSON 字段的显式空值与缺失。 源码

  • build_quota_slot_preview_for_decision。复用原门面协调结算、资格检查和内存中的额度投影。 源码

  • retain-quota-preview-settlement-identity-and-replay(implemented)。写回和额度回执必须绑定原执行身份,重复读回不能提供第二次扣额资格。

对主干的风险 / Risks

同一来源已有扣额或明确不可结算写回时,两版均不再选中旧候选。只有退休 GoalRef 时,两版均扫完 3,208 条,且拒绝候选。严格结算读回继续拒绝损坏行。隔离 CLI 对只读结算的非因果工作区及拒绝在规范检出路径执行必需写入的断言保持一致。 为追求尾扫而绕过已消费行或用旧候选复用另一个 GoalRef。用双版持久索引反例验证。 保留原索引顺序与同 owner 的终止规则,维持原失败字段和结算回执绑定。

我的整体评价 / Verdict

APPROVE。 在经验证的入口,拆分保留结算绑定、读回与拒绝行为,且候选选择器少解码旧行。 后继轮替换已选 Todo 后,合法交付仍靠原绑定回执恢复。旧扣额、其他 Agent 或退休实例继续隔离。

English verdict: APPROVE at 74bd240. Exact-base/head checks preserve quota preview selection, workspace gates, and File/SQLite CLI settlement behavior. The measured tail lookup decodes 8 of 3,208 rows.

@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 slot preview 与 refresh-state 读取额度、扣额和交付回执的 Goal 执行者。

在尾部存在匹配候选时,修复前解码全部历史行;修复后只解码从尾部到终止候选的记录。文件仍整体读取,改动优化的是 JSON decode 和选择流程。

同一 Goal/Agent/Todo/Turn 的候选、扣额预览、workspace 拒绝和失败字段形状保持;尾部场景减少旧行解码。

不改变 quota 规则、settlement 写回、持久索引格式、TypeScript 展示 owner、权限或数据库 schema。

改动思路 / Approach

职责按 settlement invariant、workspace invariant 和 preview facade 分离,仍复用既有 commit/readback/ledger owner。

仅 quota slot accounting 与其契约测试。

入口是 build_quota_slot_preview_for_decision。权威输入为 Quota decision, runtime_root run index, settlement identity/readback, workspace causality and original Goal/Agent/Todo/Turn binding。决定边界:The existing facade coordinates; slot_preview_settlement owns settlement selection and slot_preview_workspace owns workspace/resolution policy。结果:Read-only preview; durable spend/writeback happens only through existing commit owners after preview。消费方:Quota CLI consumes the preview and existing spend/void commit paths consume its identity。失败与重试:Missing/unreadable index returns no candidate; workspace or settlement refusal returns the existing typed preview failure。

具体改动 / Changes

按 docs/reference/protocols/loopx-turn-v0.md 的 revision 57c4c43b5243003c6da1988730c49ac2267626f7 核对,retain-quota-preview-settlement-identity-and-replay / preserve-preview-refusal-shape / preserve-workspace-causality 分别对应下面的行为与验证。

关键代码讲解

  • select_latest_unspent_turn_settlement_run:从尾部记录中识别第一个同身份、同 GoalRef、非 neutral、且仍可结算的候选或终止记录;判定顺序不变,但只解码到候选/终止行为止。
  • resolve_preview_settlement:读取原 Turn settlement,保留 identity、delivery、workspace、writeback/spend committed 与 closeout reasons;逻辑从门面移动但回执类别、reason 和字段形状不变。
  • evaluate_delivery_workspace_preview:判断 delivery workspace 是否缺失、需修复或已验证,并决定是否阻止 spend;工作区因果、repair gating 和 refusal shape 由独立 owner 生成,语义保持。
  • build_quota_slot_preview_for_decision:验证 Goal path、绑定和 workspace,再调用 settlement selection 与 quota preview,最后构造明确 field presence 的失败或成功 payload;门面变薄,但调用顺序、原身份、扣额预览和错误分支保持。

变更共 4,1722 additions、860 deletions。One facade shrinks to 323 lines; two modules own settlement and workspace concerns; one contract test file pins complexity, identity re-exports, refusal shape and tail decode.

  • retain-quota-preview-settlement-identity-and-replay:候选、写回和扣额回执必须绑定原 Goal/Agent/Todo/Turn 身份,旧回执不得再次提供扣额资格;当前为 implemented。
  • preserve-preview-refusal-shape:拒绝分支必须保持 key presence 与 None 的区别,以及 quota rejection/settlement result 字段;当前为 implemented。
  • preserve-workspace-causality:缺失、未知或修复中的 delivery workspace 因果必须继续阻止不符合条件的 spend;当前为 implemented。

对主干的风险 / Risks

最强回归场景:Tail scan stops on an earlier valid row or refuses on a later neutral row, changing quota eligibility。触发状态:A later void/monitor/ACK/refresh, mismatched GoalRef, malformed line or existing spend。修复边界:The original ordered classification loop is moved unchanged into the tail iterator; spent remains terminal and neutral rows are skipped。影响范围:Quota slot preview and settlement selection。可观察性:Selected run, preview status, refusal reason and commit identity expose the result。恢复路径:Revert the four-file modularization; persisted index and receipts are unchanged。

changed_invariant_positive=passed (2466 passed, 6 skipped.);material_negative_or_failure=passed (All negative and refusal cases passed.);typescript_digest_owner=passed (17 passed.);static_and_contract_checks=passed (Ruff and mypy passed; premerge 15/15 with 0 manual holds.);repository_required_checks=passed (All direct checks passed.)。

  • PASSED · Slot preview, settlement replay, workspace and tail selection:2466 passed, 6 skipped.
  • PASSED · Wrong binding, later spend, malformed index, missing workspace and repeated receipt:All negative and refusal cases passed.
  • PASSED · Canonical digest owner remains stable:17 passed.
  • PASSED · Zero Any, strict types, identity re-exports and premerge:Ruff and mypy passed; premerge 15/15 with 0 manual holds.
  • PASSED · Diff hygiene and bilingual/RFC checks:All direct checks passed.

我的整体评价 / Verdict

长程效果判断为 improved:模块 owner 更清晰,尾部候选场景减少历史 JSON 解码而不改变持久语义。用户体验判断为 preserved:扣额预览、拒绝原因、回执和恢复命令保持兼容。视觉检查不适用,本 PR 仅改 quota slot accounting 与测试。

精确 head 的 2466 项相关测试、17 项 TS、Ruff、strict mypy 和 15/15 premerge 全部通过,未发现阻塞问题。

English verdict: APPROVE at 74bd240. The exact head modularizes slot accounting and delays historical JSON decoding while preserving settlement identity, refusal shape and workspace causality. It passes 2466 related tests, 17 TypeScript tests, Ruff, strict mypy and 15/15 premerge.

@huangruiteng
huangruiteng merged commit c897ad6 into main Nov 26, 2026
37 of 48 checks passed
@huangruiteng
huangruiteng deleted the codex/slim-quota-slot-accounting-zero-any-20261126 branch November 26, 2026 11:30
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