From e82b86a53005afe54b2c5ec37ce1bfbbedbcaf41 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:13:29 +0800 Subject: [PATCH] fix(quota): report spend index conflicts on the CLI Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/cli_commands/quota_failure_report.py | 12 ++++ loopx/control_plane/quota/error_codes.py | 8 +++ loopx/control_plane/quota/spend_commit.py | 9 +-- ...test_quota_authority_settlement_journey.py | 55 ++++++++++++++++++- 4 files changed, 75 insertions(+), 9 deletions(-) diff --git a/loopx/cli_commands/quota_failure_report.py b/loopx/cli_commands/quota_failure_report.py index 1de65a1b0b..f3c267af7d 100644 --- a/loopx/cli_commands/quota_failure_report.py +++ b/loopx/cli_commands/quota_failure_report.py @@ -41,6 +41,7 @@ QuotaCommandValidationError, QuotaDecisionTransportError, QuotaIdentityPreconditionError, + QuotaRunIndexConflict, quota_error_code, ) from ..file_lock import lock_timeout_error_fields @@ -168,6 +169,17 @@ def quota_failure_payload( diagnostic_code = getattr(error, "diagnostic_code", None) if isinstance(error, EffectRuntimeRemoteError) and isinstance(diagnostic_code, str): payload["diagnostic_code"] = diagnostic_code + if isinstance(error, QuotaRunIndexConflict): + payload.update( + status=error.reason_code, + reason="the quota run index changed before the spend could commit", + appended=False, + recommended_action=( + "for a bound spend, rerun quota should-run with the same Turn " + "identity and follow its returned settlement command. For an " + "unbound spend, obtain a fresh preview before retrying." + ), + ) if isinstance(error, HeartbeatReceiptIdentityConflictError): payload.update({ "status": "heartbeat_receipt_identity_conflict", diff --git a/loopx/control_plane/quota/error_codes.py b/loopx/control_plane/quota/error_codes.py index e2e9e37641..f1aa270762 100644 --- a/loopx/control_plane/quota/error_codes.py +++ b/loopx/control_plane/quota/error_codes.py @@ -32,6 +32,12 @@ class HeartbeatReceiptIdentityConflictError(ValueError): """Public-safe diagnostic for a same-turn settlement identity conflict.""" +class QuotaRunIndexConflict(ValueError): + """A quota spend was definitively rejected by the run-index CAS.""" + + reason_code = "index_digest_conflict" + + class QuotaIdentityPrecondition(StrEnum): """Typed identity admission preconditions for scoped quota decisions.""" @@ -233,6 +239,8 @@ def quota_error_code(exc: BaseException) -> str: return exc.error_code if isinstance(exc, HeartbeatReceiptIdentityConflictError): return "heartbeat_receipt_identity_conflict" + if isinstance(exc, QuotaRunIndexConflict): + return exc.reason_code if isinstance(exc, PermissionError): return "quota_state_permission_denied" if isinstance(exc, OSError): diff --git a/loopx/control_plane/quota/spend_commit.py b/loopx/control_plane/quota/spend_commit.py index 5ddc370ba6..b8b5e2f677 100644 --- a/loopx/control_plane/quota/spend_commit.py +++ b/loopx/control_plane/quota/spend_commit.py @@ -17,8 +17,9 @@ ) from ..runtime.time import now_local_iso from ..todos.contract import normalize_todo_claimed_by -from .decision_summary import compact_quota_decision, quota_decision_agent_id from .accounting_admission import quota_accounting_admission +from .decision_summary import compact_quota_decision, quota_decision_agent_id +from .error_codes import QuotaRunIndexConflict from .spend_sources import DEFAULT_SLOT_SPEND_SOURCE @@ -26,12 +27,6 @@ QUOTA_SPEND_COMMIT_RESULT_SCHEMA = "loopx_quota_spend_commit_result_v0" -class QuotaRunIndexConflict(ValueError): - """A quota spend was definitively rejected by the run-index CAS.""" - - reason_code = "index_digest_conflict" - - def quota_spend_index_digest(index_path: Path) -> str | None: try: content = index_path.read_bytes() diff --git a/tests/control_plane/test_quota_authority_settlement_journey.py b/tests/control_plane/test_quota_authority_settlement_journey.py index 6d6e0c259c..4051c6f680 100644 --- a/tests/control_plane/test_quota_authority_settlement_journey.py +++ b/tests/control_plane/test_quota_authority_settlement_journey.py @@ -16,10 +16,12 @@ from codex_checkpoint_fixture import selected_context_values from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection -from loopx.control_plane.todos.active_state_todo_parser import parse_active_state_todos +from loopx.control_plane.quota import slot_accounting from loopx.control_plane.quota.settlement import read_heartbeat_settlement -from loopx.rollout_event_log import rollout_event_log_path +from loopx.control_plane.todos.active_state_todo_parser import parse_active_state_todos +from loopx.entrypoint import main as cli_main from loopx.presentation.renderers.quota_event_markdown import render_quota_slot_preview_markdown +from loopx.rollout_event_log import rollout_event_log_path from loopx.state_refresh import render_state_refresh_markdown from loopx.todos import list_goal_todos @@ -901,3 +903,52 @@ def test_returned_command_settles_and_repairs_receipts_without_another_debit(tmp assert code == 0, next_wake assert next_wake["effective_action"] != "unsettled_host_turn_recovery" assert cli._spend_run_count(runtime) == 1 + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_spend_index_conflict_keeps_its_diagnostic_and_retries_the_same_turn( + tmp_path, monkeypatch, capsys, provider, +): + project, runtime, registry = cli._write_fixture(tmp_path) + cli._initialize_fixture_authority(project, runtime, provider=provider) + code, guard = _guard(project, runtime, registry) + assert code == 0, guard + code, refreshed = _refresh(project, runtime, registry) + assert code == 0, refreshed + command = refreshed["settlement_owed"]["command"] + + commit = slot_accounting.record_quota_slot_spend_from_preview + + def spend_with_competing_write(preview, status, **kwargs): + # Commit another real spend after the original preview captured its basis. + code, competing = cli._run_cli( + registry, runtime, "quota", "spend-slot", "--execute", "--source", "controller", + "--goal-id", cli.GOAL_ID, "--agent-id", cli.AGENT_ID, cwd=project, + ) + assert code == 0 and competing["appended"] is True, competing + return commit(preview, status, **kwargs) + + with monkeypatch.context() as patch: + patch.chdir(project) + patch.setattr( + slot_accounting, "record_quota_slot_spend_from_preview", spend_with_competing_write, + ) + code = cli_main(shlex.split(command)[1:]) + rejected = json.loads(capsys.readouterr().out) + assert code == 1 and rejected["ok"] is False, rejected + assert cli._spend_run_count(runtime) == 1 + assert rejected["error_code"] == "index_digest_conflict", rejected + assert rejected["status"] == "index_digest_conflict" + assert rejected["appended"] is False + assert "same Turn" in rejected["recommended_action"] + + code, still_owed = _refresh(project, runtime, registry) + assert code == 0 and still_owed["settlement_owed"]["command"] == command, still_owed + code, recovered = _execute(command, project, runtime, registry) + assert code == 0 and recovered["settlement_progress"]["state"] == "settled", recovered + assert recovered["settlement_identity"] == refreshed["settlement_identity"] + assert recovered["appended"] is True + code, replay = _execute(command, project, runtime, registry) + assert code == 0 and replay["appended"] is False, replay + assert replay["settlement_identity"] == recovered["settlement_identity"] + assert cli._spend_run_count(runtime) == 2