Skip to content

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

Open
loopx-agent wants to merge 1 commit into
mainfrom
codex/slim-quota-slot-accounting-zero-any-20261126
Open

loopx-agent wants to merge 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.

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