Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from sandbox_executor import local_llm
from sandbox_executor.agent_defaults import AGENT_IMAGE_MAPPING, normalise_agent_id, normalise_model_and_effort
from sandbox_executor.preflight_quota_scope import QuotaScope, classify_quota_scope
from sandbox_executor.preflight_result import PreflightResult, PreflightStatus, redact_output

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -320,13 +321,20 @@ def active_cooldown_result(
return build_cooldown_result(marker, agent, model, resolved_image, now=now)


def _record_quota_cooldown(result: PreflightResult, now: float | None) -> None:
if result.model and classify_quota_scope(result.output_excerpt) is QuotaScope.MODEL:
record_model_cooldown(result.agent, result.model, result.output_excerpt, now=now)
else:
record_cooldown(result.agent, result.model, result.output_excerpt, now=now)


def sync_cooldown_for_result(result: PreflightResult, *, now: float | None = None) -> None:
if not isinstance(result, PreflightResult):
return
if result.status is PreflightStatus.QUOTA_EXHAUSTED:
if result.hint.startswith(COOLDOWN_HINT_PREFIX):
return
record_cooldown(result.agent, result.model, result.output_excerpt, now=now)
_record_quota_cooldown(result, now)
elif result.ok and not result.skipped:
clear_cooldown(result.agent)
clear_model_cooldown(result.agent, result.model)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

import re
from enum import StrEnum


class QuotaScope(StrEnum):
PROVIDER = "provider"
MODEL = "model"


_ACCOUNT_WIDE_LIMIT_RE = re.compile(
r"(?:\borg\b|\borg's|organi[sz]ation|\baccount\b|\bworkspace\b|\bbilling\b)[^\n]{0,40}?"
r"(?:limit|quota|spend|spending|budget)",
re.IGNORECASE,
)
_EXPLICIT_MODEL_LIMIT_RE = re.compile(
r"for\s+this\s+model"
r"|\bmodel(?:'s)?\s+(?:spend|spending|rate|usage|token)\s+limit"
r"|per-model\s+limit",
re.IGNORECASE,
)
_SWITCH_MODEL_HINT_RE = re.compile(r"switch\s+to\s+(?:another|a\s+different)\s+model", re.IGNORECASE)


def classify_quota_scope(output: str) -> QuotaScope:
text = output or ""
# why: explicit per-model wording names the exhausted scope even when the sentence also mentions the account.
if _EXPLICIT_MODEL_LIMIT_RE.search(text):
return QuotaScope.MODEL
# why: an org or account limit outranks a "switch model" hint because no other model on the provider can succeed.
if _ACCOUNT_WIDE_LIMIT_RE.search(text):
return QuotaScope.PROVIDER
if _SWITCH_MODEL_HINT_RE.search(text):
return QuotaScope.MODEL
return QuotaScope.PROVIDER
73 changes: 73 additions & 0 deletions apps/sandbox-executor/tests/test_preflight_cooldown.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
record_model_cooldown,
sync_cooldown_for_result,
)
from sandbox_executor.preflight_quota_scope import QuotaScope, classify_quota_scope
from sandbox_executor.preflight_result import PreflightResult


Expand Down Expand Up @@ -323,3 +324,75 @@ def test_ok_preflight_clears_the_model_cooldown(isolated_cooldown_home):
)
sync_cooldown_for_result(ok)
assert not os.path.exists(model_marker_path("claude-agent", "claude-opus-5-5"))


FABLE_MONTHLY_SPEND_LIMIT = "You've hit your monthly spend limit. Switch to another model to continue."
ORG_SPEND_LIMITS = ["You've hit your org's monthly spend limit", "Organization spending limit reached"]


def _quota_exhausted(model: str, output: str) -> PreflightResult:
return PreflightResult(
status=PreflightStatus.QUOTA_EXHAUSTED,
agent="claude-agent",
model=model,
image="holon/agent-claude",
exit_code=1,
output_excerpt=output,
hint="",
)


@pytest.mark.parametrize(
("output", "expected"),
[
(FABLE_MONTHLY_SPEND_LIMIT, QuotaScope.MODEL),
("Rate limit for this model exceeded", QuotaScope.MODEL),
("Your account has reached the spend limit for this model", QuotaScope.MODEL),
(ORG_SPEND_LIMITS[0], QuotaScope.PROVIDER),
(ORG_SPEND_LIMITS[1], QuotaScope.PROVIDER),
("org's monthly spend limit. Switch to another model to continue.", QuotaScope.PROVIDER),
("HTTP 429 Too Many Requests", QuotaScope.PROVIDER),
("", QuotaScope.PROVIDER),
],
)
def test_classify_quota_scope_separates_model_limits_from_account_limits(output, expected):
assert classify_quota_scope(output) is expected


def test_model_spend_limit_blocks_only_that_model(isolated_cooldown_home):
sync_cooldown_for_result(_quota_exhausted("claude-fable-5-2", FABLE_MONTHLY_SPEND_LIMIT))

assert read_active_model_cooldown("claude-agent", "claude-fable-5-2") is not None
assert read_active_cooldown("claude-agent") is None
assert active_cooldown_result("claude-agent", "claude-fable-5-2") is not None
assert active_cooldown_result("claude-agent", "claude-opus-5-5") is None


@pytest.mark.parametrize("output", ORG_SPEND_LIMITS)
def test_org_spend_limit_blocks_every_model_of_the_provider(isolated_cooldown_home, output):
sync_cooldown_for_result(_quota_exhausted("claude-fable-5-2", output))

assert os.path.exists(marker_path("claude-agent"))
for model in ("claude-fable-5-2", "claude-opus-5-5", "claude-sonnet-4-6"):
assert active_cooldown_result("claude-agent", model) is not None


def test_run_preflight_probe_model_spend_limit_leaves_fallback_model_probeable(isolated_cooldown_home, monkeypatch):
monkeypatch.setattr(preflight_cooldown.time, "time", lambda: 1_700_000_000.0)
failing_runner = MagicMock(
return_value=subprocess.CompletedProcess(args=[], returncode=1, stdout="", stderr=FABLE_MONTHLY_SPEND_LIMIT)
)
first = run_preflight_probe("claude-agent", "claude-fable-5-2", timeout=5, runner_fn=failing_runner)
assert first.status is PreflightStatus.QUOTA_EXHAUSTED
assert not os.path.exists(marker_path("claude-agent"))

ok_runner = MagicMock(return_value=subprocess.CompletedProcess(args=[], returncode=0, stdout="OK\n", stderr=""))
fallback = run_preflight_probe("claude-agent", "claude-opus-5-5", timeout=5, runner_fn=ok_runner)
assert fallback.ok
assert ok_runner.call_count == 1

blocked_runner = MagicMock()
retry = run_preflight_probe("claude-agent", "claude-fable-5-2", timeout=5, runner_fn=blocked_runner)
assert retry.status is PreflightStatus.QUOTA_EXHAUSTED
assert retry.hint.startswith(COOLDOWN_HINT_PREFIX)
assert blocked_runner.call_count == 0
44 changes: 44 additions & 0 deletions executions/E-1796254161-CLA-CO5.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Execution Record: E-1796254161-CLA-CO5.5

## Summary

Implements plan `P-1796254006-CLA-CO5.5` for intent `I-1796253983-hac-model-scoped-cooldown`: quota-exhausted preflight
results are classified by scope, so a per-model spend limit writes a `(provider, model)` cooldown marker while org or
account limits and ambiguous text keep the provider-wide marker.

## Actual Metrics

| metric | value |
| --------------------- | ----- |
| success_actual | 1.0 |
| entropy_actual | 3.0 |
| impact_actual | 70.0 |
| cost_actual | 10.0 |
| learning_value_actual | 3.0 |

## Steps Executed

1. Step 1: added `apps/sandbox-executor/src/sandbox_executor/preflight_quota_scope.py` with `QuotaScope` and
`classify_quota_scope`. Account-wide wording (`org`, `organization`, `account`, `workspace`, `billing` near a limit
word) is checked first and wins; model-scoped wording ("switch to another model", "for this model", "model spend
limit", "per-model limit") yields `MODEL`; anything else stays `PROVIDER`. The module imports only `re` and `enum`.
2. Step 2: `sync_cooldown_for_result` now calls `_record_quota_cooldown`, which writes a model marker through
`record_model_cooldown` for model-scoped text with a non-empty model and the provider marker otherwise. No public
signature changed; `preflight_cooldown.py` is 340 lines (budget 350).
3. Step 3: added regression tests to `apps/sandbox-executor/tests/test_preflight_cooldown.py`: a parametrised classifier
test, a fable monthly spend limit that blocks only `claude-fable-5-2` (not `claude-opus-5-5`), org spend limits that
block every claude model, and an end-to-end `run_preflight_probe` test where the fallback `claude-opus-5-5` probe
runs while a repeat `claude-fable-5-2` probe short-circuits. `claude-fable-5-2` is accepted unchanged by the model
normaliser.
4. Step 4: verification below.

## Tests Run

| command | result |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
| `uv run pytest apps/sandbox-executor/tests/test_preflight_cooldown.py apps/sandbox-executor/tests/test_preflight_outage.py` (after step 2, before new tests) | 67 passed, 0 failed in 1.13s |
| `uv run pytest apps/sandbox-executor/tests/test_preflight_cooldown.py apps/sandbox-executor/tests/test_preflight_outage.py apps/sandbox-executor/tests/test_preflight.py` | 369 passed, 0 failed in 1.43s |
| `uv run task lint` | exit 0 (module budgets, self-explaining check passed) |
| `uv run task test` (full suite, once) | 11342 passed, 8 skipped, 0 failed in 58.68s |

No timing requirement in the intent, so each command ran once; median equals the single recorded run.
Loading
Loading