Skip to content

The agent that grows with you

refactor(auth): share the key-prefix check; reach it via rp in the openrouter resolver

kshitijk4poorc1edc9f1b5cd
diff --git a/hermes_cli/auth.py b/hermes_cli/auth.py
index ee60d71786d..33e017bfc92 100644
--- a/hermes_cli/auth.py
+++ b/hermes_cli/auth.py
@@ -326,12 +326,17 @@ KNOWN_PROVIDER_KEY_PREFIXES: Dict[str, tuple] = {
}
+def _matches_key_prefix(provider_id: str, val: str) -> bool:
+ """True when *val* starts with one of *provider_id*'s declared key prefixes (False when the
+ provider declares none)."""
+ return val.startswith(KNOWN_PROVIDER_KEY_PREFIXES.get(provider_id, ()))
+
+
def looks_like_openrouter_key(value: Any) -> bool:
"""True when *value* carries an OpenRouter key prefix. OPENAI_API_KEY is a legacy home for an
OpenRouter key, so only a value shaped like one may be read as an OpenRouter credential: a real
OpenAI key must never be auto-routed to, or sent to, openrouter.ai."""
- val = str(value or "").strip()
- return any(val.startswith(p) for p in KNOWN_PROVIDER_KEY_PREFIXES["openrouter"])
+ return _matches_key_prefix("openrouter", str(value or "").strip())
def _usable_declared_secret(provider_id: str, value: Any, source: str) -> Optional[str]:
@@ -342,7 +347,7 @@ def _usable_declared_secret(provider_id: str, value: Any, source: str) -> Option
if not has_usable_secret(val):
return None
prefixes = KNOWN_PROVIDER_KEY_PREFIXES.get(provider_id)
- if prefixes and not any(val.startswith(p) for p in prefixes):
+ if prefixes and not _matches_key_prefix(provider_id, val):
logger.warning(
"Ignoring %s for provider %r: value does not match the expected key "
"prefix (%s). Falling back to the next credential source. Fix or "
@@ -1462,7 +1467,7 @@ def _openrouter_auto_detected(scoped_key_env: Callable[[str], str]) -> bool:
# env var). Without this, a key that only lives in the credential pool is invisible to
# auto-detection — the user sees `hermes auth list` showing the credential while requests go out
# with no Authorization header ("HTTP 401: Missing Authentication header"). The env-var check above
- # only covers keys exported as OPENROUTER_API_KEY / OPENAI_API_KEY. See issue #42130.
+ # only covers OPENROUTER_API_KEY and an sk-or- key in OPENAI_API_KEY. See issue #42130.
from agent.credential_pool import load_pool as _load_pool
return bool(_load_pool("openrouter").has_credentials())
except Exception as e:
diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py
index 0bf71e302d2..dc286260ca8 100644
--- a/hermes_cli/runtime_provider.py
+++ b/hermes_cli/runtime_provider.py
@@ -26,7 +26,7 @@ from hermes_cli.auth import ( # resolve_external_process_provider_credentials i
resolve_nous_runtime_credentials, resolve_codex_runtime_credentials, resolve_xai_oauth_runtime_credentials,
resolve_qwen_runtime_credentials, resolve_api_key_provider_credentials,
resolve_external_process_provider_credentials, # noqa: F401
- has_usable_secret, is_actual_local_base_url, normalize_actual_base_url,
+ has_usable_secret, is_actual_local_base_url, looks_like_openrouter_key, normalize_actual_base_url,
)
from hermes_cli import config as _config_mod
from hermes_cli import models as _models # attribute access keeps ``hermes_cli.models.<name>`` patches effective
diff --git a/hermes_cli/runtime_provider_backends.py b/hermes_cli/runtime_provider_backends.py
index 98637a1b5ab..1893371ea74 100644
--- a/hermes_cli/runtime_provider_backends.py
+++ b/hermes_cli/runtime_provider_backends.py
@@ -160,13 +160,12 @@ def _resolve_openrouter_runtime(
# OPENAI_API_KEY is a legacy home for an OpenRouter key. When OPENAI_BASE_URL binds it, it
# goes only to that host. Unbound, openrouter.ai gets it only when it is OpenRouter-shaped
# (sk-or-), so a real OpenAI key never reaches a third party.
- from hermes_cli.auth import looks_like_openrouter_key
openai_key = get_secret_str("OPENAI_API_KEY")
openai_base_host = base_url_hostname(get_secret_str("OPENAI_BASE_URL", "").strip())
if openai_base_host:
openai_key_ok = openai_base_host == base_url_hostname(base_url)
else:
- openai_key_ok = not is_openrouter_url or looks_like_openrouter_key(openai_key)
+ openai_key_ok = not is_openrouter_url or rp.looks_like_openrouter_key(openai_key)
candidates = [explicit_api_key, get_secret_str("OPENROUTER_API_KEY"),
openai_key if openai_key_ok else ""]
else: