Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions loopx/cli_commands/quota_failure_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
QuotaCommandValidationError,
QuotaDecisionTransportError,
QuotaIdentityPreconditionError,
QuotaRunIndexConflict,
quota_error_code,
)
from ..file_lock import lock_timeout_error_fields
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions loopx/control_plane/quota/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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):
Expand Down
9 changes: 2 additions & 7 deletions loopx/control_plane/quota/spend_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,16 @@
)
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


QUOTA_SPEND_COMMIT_REQUEST_SCHEMA = "loopx_quota_spend_commit_request_v0"
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()
Expand Down
55 changes: 53 additions & 2 deletions tests/control_plane/test_quota_authority_settlement_journey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Loading