Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

chore: auto-save WIP via Claude stop-hook safety net (2026-06-30 13:25)

Ruben Lütke05ddbe082c5f
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index 5c1365ad97..1666efa0ab 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -70,6 +70,10 @@ from jarvis.voice.contextual_readback import render_readback
from jarvis.memory import CoreMemory, PersonStore, RecallStore, Soul, UserProfile
from jarvis.memory.curator import Curator
from jarvis.safety.tool_executor import ToolExecutor
+# ONE canonical billing/budget/quota marker list, shared with the test-badge
+# classifier (provider_test.classify_provider_error) so the live fallback chain and
+# the API-Keys badge can never disagree on "out of credits / over budget" (AP-22).
+from jarvis.brain.provider_test import BILLING_LIMIT_MARKERS
from .dispatcher import BrainDispatcher
from .healthcheck import BrainConfigError
@@ -6154,7 +6158,7 @@ class BrainManager:
# On missing_key: remove provider from the chain for the rest
# of the session. Prevents each voice turn from running 8x
# sequentially against the same missing keys.
- if kind in ("missing_key", "account_blocked") and prov_name not in self._dead_providers:
+ if kind in _DEAD_LIST_KINDS and prov_name not in self._dead_providers:
self._dead_providers.add(prov_name)
if kind == "missing_key":
log.warning(
@@ -6430,7 +6434,7 @@ class BrainManager:
else:
log.warning("Brain %s(%s) fehlgeschlagen: %s", prov_name, model, exc)
if (
- kind in ("missing_key", "account_blocked")
+ kind in _DEAD_LIST_KINDS
and prov_name not in self._dead_providers
):
self._dead_providers.add(prov_name)
@@ -7322,43 +7326,56 @@ def _is_account_blocked_exc(msg: str) -> bool:
- OpenAI 403: ``The model `o1-pro` is not available on your tier.``
- Gemini 403: ``Quota exceeded for ...`` (unlike 429 — terminal).
+ - OpenRouter 403: ``Key limit exceeded (total limit).`` (a funded account
+ whose per-key spend cap is used up — live probe 2026-06-30, AP-22).
+
These providers are dead for the session (a simple retry won't help).
BrainManager pushes them immediately into _dead_providers and emits a
user-actionable setup message instead of "provider unreachable".
"""
m = msg.lower()
- return any(k in m for k in (
- "credit balance is too low",
- "credit balance too low",
- "plans & billing",
- "billing required",
- "your team",
- "your team does not have access",
- "team does not have access",
- "team_does_not_have_access",
- "not available on your tier",
- "subscription required",
- "upgrade plan",
- "upgrade your plan",
- "exceeded your quota", # Gemini-style, terminal vs. 429
- "exceeded your current quota", # OpenAI insufficient_quota wording
- "quota exceeded for",
- "insufficient_quota",
- "insufficient quota",
- "billing not active",
- "payment required",
- "account is suspended",
- # Terminal-billing 429s that carry "429" in the message and would
- # otherwise be mis-read as a transient rate-limit (live forensic
- # 2026-06-28: depleted Gemini bricked the whole chain — AP-22).
- "credits are depleted",
- "credits depleted",
- "prepayment credits",
- "out of credits",
- "no credits remaining",
- "check your plan and billing",
- "plan and billing",
- ))
+ # Billing / budget / quota wording is the SHARED canonical list (one source of
+ # truth with the test-badge classifier) — covers credit-balance, spend/key/total
+ # limit, insufficient_quota, depleted prepayment, out-of-credits, etc.
+ if any(k in m for k in BILLING_LIMIT_MARKERS):
+ return True
+ # Access / tier / subscription gates — terminal too, but not strictly "money".
+ return any(k in m for k in _ACCOUNT_ACCESS_MARKERS)
+
+
+# Terminal access/tier/subscription wordings (NOT money — kept beside the shared
+# billing markers so both flavours of "account blocked" live in one classifier).
+_ACCOUNT_ACCESS_MARKERS: tuple[str, ...] = (
+ "your team", # xAI "your team ... does not have access"
+ "team does not have access",
+ "team_does_not_have_access",
+ "not available on your tier", # OpenAI tier gate
+ "subscription required",
+ "upgrade plan",
+ "upgrade your plan",
+ "billing required",
+ "billing not active",
+ "account is suspended",
+)
+
+
+def _http_status_code(msg: str) -> int | None:
+ """First 4xx/5xx HTTP status embedded in a provider error string, else None.
+
+ The SDK serializes the numeric code into ``str(exc)`` ("Error code: 403 - …"
+ for the OpenAI/Anthropic families; "403 … PERMISSION_DENIED" for Gemini), so a
+ code-first decision works uniformly without importing any provider SDK — the
+ same approach the test-badge classifier uses.
+ """
+ match = re.search(r"\b([45]\d\d)\b", msg)
+ return int(match.group(1)) if match else None
+
+
+# The chain loop dead-lists exactly these kinds: a terminal credential/account state
+# (no key stored, blocked/over-budget account, invalid key) must cross to another
+# available provider family for the rest of the session. A transient ``rate_limit``
+# is deliberately NOT here — it takes the 30s cooldown path and keeps the provider.
+_DEAD_LIST_KINDS: frozenset[str] = frozenset({"missing_key", "account_blocked", "bad_key"})
# User-friendly labels per provider — what the user needs to do.
@@ -7400,6 +7417,18 @@ def _classify_provider_error(msg: str, *, default: str) -> str:
if _is_invalid_model_exc(msg):
return "invalid_model"
m = msg.lower()
+ # Code-first terminal fallback for a 401/402 that carries the numeric HTTP code
+ # but NONE of the known wordings (a bare live "Error code: 401 - invalid x-api-key"
+ # / "Error code: 402 - Payment Required"). A 401 = invalid/expired/wrong-account
+ # key, a 402 = Payment-Required — both terminal, so the provider is dead-listed and
+ # stops leading the chain every turn (live log 2026-06-30: claude-api 401 with no
+ # Anthropic account was retried on every turn). 403/429 stay word-driven on purpose:
+ # a transient Gemini 403 "CachedContent not found" must NOT dead-list (BUG-019).
+ code = _http_status_code(m)
+ if code == 401:
+ return "bad_key"
+ if code == 402:
+ return "account_blocked"
if any(s in m for s in ("429", "rate_limit", "rate-limit",
"rate limit", "too many requests")):
return "rate_limit"
diff --git a/jarvis/brain/provider_test.py b/jarvis/brain/provider_test.py
index 1ba49e6cfc..3602ac3a6d 100644
--- a/jarvis/brain/provider_test.py
+++ b/jarvis/brain/provider_test.py
@@ -56,17 +56,46 @@ INTEGRATION_OK_STATUSES: frozenset[str] = frozenset(
_HTTP_CODE_RE = re.compile(r"\b([45]\d\d)\b")
-# Money / quota markers — distinguish a billing 403/429 ("no credits") from a
-# plain auth 403 ("bad key") or a transient 429 ("rate limited").
-_BILLING_MARKERS = (
- "credit", # "used all available credits", "purchase more credits"
+# Money / quota / budget markers — a credential that AUTHENTICATES but is refused
+# for a billing/budget/quota reason (an *account* state, not a bad key). This is
+# the SINGLE canonical list shared with the runtime dead-list classifier
+# (jarvis/brain/manager.py::_is_account_blocked_exc) so the test-badge and the live
+# fallback chain can never drift apart on what "out of credits / over budget" means.
+# Every entry is a phrase observed live or documented from a real provider 4xx body.
+#
+# HARD INVARIANT: none of these may be a substring of a transient
+# "rate limit exceeded" message (that must stay rate_limited / take the cooldown,
+# never dead-list) — so there is deliberately NO bare "limit exceeded" / "limit"
+# here, only qualified forms ("key limit", "spend limit", "total limit", …).
+BILLING_LIMIT_MARKERS = (
+ "credit", # "credit balance too low", "used all available credits"
"spending limit",
+ "spend limit",
+ "key limit", # OpenRouter per-key cap: "Key limit exceeded (total limit)"
+ "total limit", # OpenRouter per-key cap (the parenthetical)
+ "credit limit",
+ "usage limit",
+ "monthly limit",
+ "budget", # "monthly budget exceeded for this key"
"billing", # OpenAI "check your plan and billing details"
"insufficient_quota",
+ "insufficient quota",
"out of funds",
- "payment",
+ "out of credits",
+ "no credits",
+ "payment", # HTTP 402 Payment Required bodies
+ "quota exceeded",
+ "exceeded your quota",
+ "exceeded your current quota",
+ "prepayment", # Gemini "prepayment credits are depleted"
+ "depleted",
+ "plan and billing",
+ "plans & billing",
)
+# Back-compat alias (anything importing the old private name keeps working).
+_BILLING_MARKERS = BILLING_LIMIT_MARKERS
+
# "No key stored" shapes from the plugins' own _ensure_client guards.
_MISSING_KEY_MARKERS = (
("kein", "gefunden"), # "Kein OpenAI-API-Key gefunden ..."
diff --git a/tests/unit/brain/test_depleted_credits_classification.py b/tests/unit/brain/test_depleted_credits_classification.py
index 3ef45cf60e..ef58549fb8 100644
--- a/tests/unit/brain/test_depleted_credits_classification.py
+++ b/tests/unit/brain/test_depleted_credits_classification.py
@@ -11,13 +11,28 @@ crosses to whatever provider family actually has a usable key (AP-22).
"""
from __future__ import annotations
-from jarvis.brain.manager import _classify_provider_error, _is_account_blocked_exc
+from jarvis.brain.manager import (
+ _DEAD_LIST_KINDS,
+ _classify_provider_error,
+ _is_account_blocked_exc,
+)
GEMINI_DEPLETED = (
"Your prepayment credits are depleted. Please go to AI Studio at "
"https://ai.studio/projects to manage your project and billing."
)
+# Live probe 2026-06-30: a funded OpenRouter account whose per-key spend cap is
+# used up. Returns 403 for every PAID model while free models still answer. The
+# old word-list knew "credits"/"spending limit" but not "key limit"/"total limit",
+# so this slipped to call_fail → the spend-capped provider was never dead-listed →
+# it kept leading the chain every turn and bricked voice ("can't reach my model")
+# even though Gemini was READY. AP-22, the OpenRouter twin of the Gemini case above.
+OPENROUTER_KEY_LIMIT = (
+ "Error code: 403 - {'error': {'message': 'Key limit exceeded (total limit). "
+ "Manage it using https://openrouter.ai/settings/keys', 'code': 403}}"
+)
+
def test_depleted_prepayment_credits_is_account_blocked():
assert _is_account_blocked_exc(GEMINI_DEPLETED) is True
@@ -36,3 +51,34 @@ def test_openai_style_insufficient_quota_is_account_blocked():
# different vendor must also dead-list, never read as a transient 429.
msg = "Error code: 429 - You exceeded your current quota, please check your plan and billing."
assert _classify_provider_error(msg, default="call_fail") == "account_blocked"
+
+
+def test_openrouter_key_limit_exceeded_is_account_blocked():
+ # Runtime half of the spend-cap bug: the capped key must dead-list OpenRouter so
+ # the chain crosses to another funded family instead of leading every turn.
+ assert _is_account_blocked_exc(OPENROUTER_KEY_LIMIT) is True
+ assert _classify_provider_error(OPENROUTER_KEY_LIMIT, default="call_fail") == "account_blocked"
+
+
+def test_bare_401_invalid_key_is_terminal_bad_key():
+ # A live 401 (invalid/expired/wrong-account key) carries ONLY the numeric code,
+ # none of the "missing key" wording. Word-list-first classification let it fall
+ # through to call_fail → a dead key (e.g. claude-api with no Anthropic account,
+ # 401 in the live log) was retried EVERY turn instead of dead-listed. Code-first:
+ # a 401 is terminal and must dead-list.
+ msg = (
+ "AuthenticationError: Error code: 401 - {'type': 'error', 'error': "
+ "{'type': 'authentication_error', 'message': 'invalid x-api-key'}}"
+ )
+ assert _classify_provider_error(msg, default="call_fail") == "bad_key"
+
+
+def test_dead_list_kinds_cover_every_terminal_state():
+ # The chain-loop dead-lists exactly these kinds. A terminal credential/account
+ # state (missing key, blocked account, invalid key) must cross-family fallback;
+ # a transient rate_limit must NOT (it takes the cooldown path instead).
+ assert "missing_key" in _DEAD_LIST_KINDS
+ assert "account_blocked" in _DEAD_LIST_KINDS
+ assert "bad_key" in _DEAD_LIST_KINDS
+ assert "rate_limit" not in _DEAD_LIST_KINDS
+ assert "call_fail" not in _DEAD_LIST_KINDS
diff --git a/tests/unit/brain/test_provider_test.py b/tests/unit/brain/test_provider_test.py
index 9180f512b1..03b76cfc94 100644
--- a/tests/unit/brain/test_provider_test.py
+++ b/tests/unit/brain/test_provider_test.py
@@ -62,6 +62,28 @@ def test_english_missing_key_is_not_configured() -> None:
assert classify_provider_error("No API key found for provider") == "not_configured"
+def test_openrouter_key_spend_limit_403_is_no_credits() -> None:
+ # Live probe 2026-06-30: an OpenRouter key with a per-key spend cap that is
+ # used up returns 403 "Key limit exceeded (total limit)" for EVERY paid model,
+ # while the SAME key still answers free models. It is a funded-account budget
+ # block, NOT an invalid key — labeling it "Key invalid" (bad_key) sent the user
+ # hunting the wrong problem. Must read as out-of-credits.
+ msg = (
+ "PermissionDeniedError: Error code: 403 - {'error': {'message': "
+ "'Key limit exceeded (total limit). Manage it using "
+ "https://openrouter.ai/settings/keys', 'code': 403}}"
+ )
+ assert classify_provider_error(msg) == "no_credits"
+
+
+def test_generic_spend_limit_without_credit_word_is_no_credits() -> None:
+ # Robustness across providers: a 403 whose wording is a budget/limit block but
+ # does not contain the literal word "credit" must still read as no_credits, not
+ # bad_key. (The old marker list keyed on "credit"/"spending limit" only.)
+ msg = "PermissionDeniedError: Error code: 403 - monthly budget exceeded for this key"
+ assert classify_provider_error(msg) == "no_credits"
+
+
def test_openai_insufficient_quota_429_is_no_credits() -> None:
msg = (
"RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your "