Skip to content

The agent that grows with you

fix(bot-relay): classify the named-secret spelling of a scope refusal too

kshitijk4poora8022121719b
diff --git a/tests/tools/test_bot_failure_reasons.py b/tests/tools/test_bot_failure_reasons.py
index 2d626ee1406..65d206610f0 100644
--- a/tests/tools/test_bot_failure_reasons.py
+++ b/tests/tools/test_bot_failure_reasons.py
@@ -7,6 +7,7 @@ real-world fixtures from live bot runs, and the auto-retryable set.
import pytest
+from agent.secret_scope import UnscopedSecretError
from tools import bot_failure_reasons as fr
# Real error text captured from live bot turns.
@@ -19,8 +20,7 @@ FIXTURE_NO_PROVIDER = (
"a provider, or run `hermes setup` for first-time configuration."
)
FIXTURE_NO_TOKEN = "agent init failed: No access token found for Nous Portal login."
-# The two spellings of a target-scope spawn refusal, captured verbatim from the relay ledger after a
-# relayed DM into a PEER's default profile (the raising process is the TARGET's, not the sender's).
+# Target-scope spawn refusals, verbatim from a relay ledger (the named-secret spelling is built live).
FIXTURE_TARGET_SCOPE = (
"Hermes could not read this profile's API key (an internal profile-scoping bug on the "
"multiplexed gateway, not your configuration). Run `hermes gateway restart`; if it keeps "
@@ -57,6 +57,7 @@ FIXTURE_TARGET_SCOPE_DETAILED = (
("upstream server error", fr.PROVIDER_SERVER_ERROR),
(FIXTURE_TARGET_SCOPE, fr.TARGET_SCOPE_UNRESOLVED),
(FIXTURE_TARGET_SCOPE_DETAILED, fr.TARGET_SCOPE_UNRESOLVED),
+ (str(UnscopedSecretError("OPENROUTER_API_KEY")), fr.TARGET_SCOPE_UNRESOLVED),
# bare numbers WITHOUT a status-code context must not classify —
# they feed AUTO_RETRYABLE and a misfire could auto-retry a
# permanent local failure (review finding on #93101).
@@ -92,20 +93,6 @@ def test_fixture_no_access_token_is_missing_config():
assert fr.classify_agent_error(FIXTURE_NO_TOKEN) == fr.MISSING_CONFIG
-def test_target_scope_refusal_wins_over_the_api_key_wording_and_never_auto_retries():
- """The copy says "API key" and names a gateway restart, but nothing was ever sent to a provider:
- the delivery turn was never created because a spawn site could not resolve the target profile's
- secret scope. Reading it as auth would put it on the wrong ladder and send the reader to a
- restart on the wrong machine; it must classify as its own class and never auto-retry."""
- assert "api key" in FIXTURE_TARGET_SCOPE.lower() # the word that must not win
- for text in (FIXTURE_TARGET_SCOPE, FIXTURE_TARGET_SCOPE_DETAILED):
- code = fr.classify_agent_error(text)
- assert code == fr.TARGET_SCOPE_UNRESOLVED
- assert code in fr.ALL_REASONS
- assert not fr.is_auto_retryable(code)
- assert fr.retry_action(code) == fr.RETRY_NONE
-
-
def test_auto_retryable_set_and_predicate():
for code in fr.AUTO_RETRYABLE:
assert fr.is_auto_retryable(code)
diff --git a/tools/bot_failure_reasons.py b/tools/bot_failure_reasons.py
index c9c0b030f71..bf1d0f1d930 100644
--- a/tools/bot_failure_reasons.py
+++ b/tools/bot_failure_reasons.py
@@ -28,9 +28,7 @@ PROVIDER_SERVER_ERROR = "provider_server_error"
CONTEXT_OVERFLOW = "context_overflow"
MISSING_CONFIG = "missing_config"
MODEL_UNAVAILABLE = "model_unavailable"
-# A relay delivery (or a local Bot Chat spawn) could not resolve the target PROFILE's secret scope,
-# so the delivery turn was never created. Distinct from a provider failure: nothing was ever sent to
-# a model, and the remedy is at the spawn site, not on the sender's gateway.
+# A spawn site could not resolve the target profile's secret scope: no turn ran, no provider was called.
TARGET_SCOPE_UNRESOLVED = "target_scope_unresolved"
UNKNOWN = "unknown"
@@ -75,12 +73,11 @@ _STATUS = r"(?:error code:?\s*|status(?:\s*code)?:?\s*|http\s*)"
_RULES: tuple[tuple[re.Pattern[str], str], ...] = tuple(
(re.compile(pat, re.IGNORECASE), code)
for pat, code in (
- # FIRST: the message text itself mentions an "API key" and a restart, so it must be claimed
- # before the auth/status rules can read it as a provider verdict. Two spellings reach here —
- # the generic ``UnscopedSecretError`` copy (secret name empty, so the lede is a stand-in) and
- # ``served_profile_child_env``'s detailed one.
- (r"could not read this profile's api key|served_profile_child_env\(inherit_credentials=true\)",
- TARGET_SCOPE_UNRESOLVED),
+ # FIRST: the copy says "API key" and "restart", so it must be claimed before the auth rules.
+ # ``UnscopedSecretError`` names the secret when it has one ("this profile's OPENROUTER_API_KEY");
+ # the ``served_profile_child_env`` detail rides in tracebacks.
+ (r"could not read this profile's [^(]+\(an internal profile-scoping bug"
+ r"|served_profile_child_env\(inherit_credentials=true\)", TARGET_SCOPE_UNRESOLVED),
(rf"authentication_error|invalid api key|{_STATUS}(?:401|403)\b", PROVIDER_AUTH_OR_ACCESS),
(rf"{_STATUS}402\b|out of funds|quota|balance", PROVIDER_QUOTA_LIMIT),
(rf"{_STATUS}429\b|rate.?limit", PROVIDER_RATE_LIMIT),