Repository navigation
refactor(quota): modularize slot_accounting with zero Any and lazy index scan - #9465
loopx-agent wants to merge 1 commit into
Conversation
…dex scan 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 · 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.
Summary
Modularizes
loopx/control_plane/quota/slot_accounting.py(998 -> 323LOC,54 -> 0ASTAny) into cohesive, strict Zero-AnyTypeScript/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(323LOC,0ASTAny,0 # type: ignore,0 # noqa, max36statements /33decision points):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 forspend_commit,void_commit,ledger_readback, andpaths.validate_goal_id_path_segment(_validate_goal_id_path_segment)._latest_unspent_turn_settlement_rundefined at top level inslot_accounting.pysoloopx/semantics/goal_instance_binding_inventory_v1.jsonand direct callers/monkeypatch seams remain intact.loopx/control_plane/quota/slot_preview_settlement.py(399LOC,0ASTAny,0 # type: ignore,0 # noqa, max29statements /18decision points):PreviewSettlementResolution,resolve_preview_settlement,todo_binding_error,receipt_committed,unbound_visible_goal_settlement_failure,admits_delivery_completion_spend_state, and run-index selection helpers.iter_reversed_goal_run_index_recordsso_latest_unspent_turn_settlement_runparsesruns/index.jsonllazily 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(520LOC,0ASTAny,0 # type: ignore,0 # noqa, max19statements /14decision points):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, andbuild_preview_success_payload.preview_failure_payloadwhile preserving exact key-presence vsNoneseparation across all refusal branches (includingquota_rejection_action,settlement_result, and workspace resolution fields).tests/control_plane/test_slot_accounting_contract.py(618LOC,0ASTAny,0 # type: ignore,0 # noqa):0ASTAny/# type: ignore/# noqa, per-function AST complexity ceilings, single-ownervalidate_goal_id_path_segmentand re-export identities, goal-id traversal fail-closed behavior, key-presence vsNoneacross all refusal modes,terminal_no_followup/workspace_repair_blocks_spendgating, lazy tail-scan JSON parse bounds and malformed-input handling, monkeypatch seams, andstatus_payloaddeepcopy 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_markdownabsent fromslot_accounting.py(tests/architecture/test_control_plane_import_boundaries.py)loopx/canary/module_metric_baseline.jsonuntouched; top-levelloopx/*.py == 1120retirement-ledger legacy tokens added in new or changed modulesVerification
74bd2406ff):ruff check(4/4changed/added files): passedmypy --strict --no-incremental --follow-imports=silent(4/4changed/added files):0errorsmypy --no-incremental([tool.mypy],17files):0errorsmypy --no-incremental --follow-imports=silent(36Python callers):213 -> 213(+0regressions)npm run typecheck:control-plane: passedscripts/check_rfc_bilingual.py --base origin/main --head HEAD: passedgit diff --check origin/main...HEAD&git merge-tree --write-tree origin/main HEAD: clean74bd2406ff):node --no-warnings --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/content_digest_single_owner.test.ts:17/17passedexamples/control_plane/quota-spend-agent-identity-smoke.py&examples/control_plane/quota-spend-workspace-causality-smoke.py: passedpytest -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 intests/canary/test_git_path_display.py,0failed)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