Skip to content

The agent that grows with you

refactor(hermes_cli): auth_nous — extract _NousRuntimeResolve phases, shared JWT/status helpers, compact

Tekniuma6f75130386d
diff --git a/hermes_cli/auth_nous.py b/hermes_cli/auth_nous.py
index 3877dda854c..da0811e3f0e 100644
--- a/hermes_cli/auth_nous.py
+++ b/hermes_cli/auth_nous.py
@@ -1,9 +1,8 @@
"""Nous Portal OAuth: device-code login, refresh, shared-store mirroring, JWT selection, status.
-Split out of ``hermes_cli/auth.py``; every moved name is re-imported there, so
-``hermes_cli.auth.<name>`` keeps resolving (and monkeypatching) as before. Origin-internal
-helpers are imported lazily inside each function (no import cycle; patches on
-``hermes_cli.auth.<helper>`` still intercept).
+Split out of ``hermes_cli/auth.py``; every name is re-exported there so ``hermes_cli.auth.<name>``
+keeps resolving (and monkeypatching). Origin-internal helpers are imported lazily inside each
+function (no import cycle; patches on ``hermes_cli.auth.<helper>`` still intercept).
"""
from __future__ import annotations
@@ -18,27 +17,16 @@ import uuid
from contextlib import contextmanager
from datetime import datetime, timezone
from pathlib import Path
-from typing import Any, Callable, Dict, FrozenSet, List, Optional
+from typing import TYPE_CHECKING, Any, Callable, Dict, FrozenSet, List, Optional
from urllib.parse import urlparse
from hermes_cli.auth_codex import _pool_entries
from hermes_cli.auth_constants import (
- _decode_jwt_claims,
- AUTH_LOCK_TIMEOUT_SECONDS,
- AuthError,
- DEFAULT_NOUS_CLIENT_ID,
- DEFAULT_NOUS_INFERENCE_URL,
- DEFAULT_NOUS_PORTAL_URL,
- DEFAULT_NOUS_SCOPE,
- DEVICE_AUTH_POLL_INTERVAL_CAP_SECONDS,
- NOUS_AUTH_PATH_INVOKE_JWT,
- NOUS_BILLING_MANAGE_SCOPE,
- NOUS_DEVICE_CODE_SOURCE,
- NOUS_INFERENCE_INVOKE_SCOPE,
- NOUS_INVOKE_JWT_MIN_TTL_SECONDS,
- _nous_err,
- httpx,
+ _decode_jwt_claims, AUTH_LOCK_TIMEOUT_SECONDS, AuthError, DEFAULT_NOUS_CLIENT_ID,
+ DEFAULT_NOUS_INFERENCE_URL, DEFAULT_NOUS_PORTAL_URL, DEFAULT_NOUS_SCOPE,
+ DEVICE_AUTH_POLL_INTERVAL_CAP_SECONDS, NOUS_AUTH_PATH_INVOKE_JWT, NOUS_BILLING_MANAGE_SCOPE,
+ NOUS_DEVICE_CODE_SOURCE, NOUS_INFERENCE_INVOKE_SCOPE, NOUS_INVOKE_JWT_MIN_TTL_SECONDS,
+ _nous_err, httpx,
)
-from typing import TYPE_CHECKING
if TYPE_CHECKING: # annotation-only; the runtime import would be a cycle
from hermes_cli.auth import ProviderConfig
@@ -46,20 +34,27 @@ if TYPE_CHECKING: # annotation-only; the runtime import would be a cycle
# Log-record parity with the origin module (caplog tests pin "hermes_cli.auth").
logger = logging.getLogger("hermes_cli.auth")
+_UNUSABLE_JWT_RELOGIN = "Re-authenticate with: hermes auth add nous"
+
+
+def _unusable_invoke_jwt_error(reason: str, *, no_refresh_token: bool = False) -> AuthError:
+ """Shared ``relogin=True`` error for an access token that is not a usable inference JWT."""
+ detail = " and no refresh token is available" if no_refresh_token else ""
+ return _nous_err(
+ f"Nous Portal access token is not a usable inference JWT ({reason}){detail}. "
+ f"{_UNUSABLE_JWT_RELOGIN}",
+ reason, relogin=True,
+ )
+
def _token_fingerprint(token: Any) -> Optional[str]:
"""Return a short hash fingerprint for telemetry without leaking token bytes."""
- if not isinstance(token, str):
- return None
- cleaned = token.strip()
- if not cleaned:
- return None
- return hashlib.sha256(cleaned.encode("utf-8")).hexdigest()[:12]
+ cleaned = token.strip() if isinstance(token, str) else ""
+ return hashlib.sha256(cleaned.encode("utf-8")).hexdigest()[:12] if cleaned else None
def _oauth_trace_enabled() -> bool:
- raw = os.getenv("HERMES_OAUTH_TRACE", "").strip().lower()
- return raw in {"1", "true", "yes", "on"}
+ return os.getenv("HERMES_OAUTH_TRACE", "").strip().lower() in {"1", "true", "yes", "on"}
def _oauth_trace(event: str, *, sequence_id: Optional[str] = None, **fields: Any) -> None:
@@ -79,32 +74,25 @@ def _iso_after(now: datetime, ttl_seconds: int) -> str:
# Nous agent-key slots; a fresh login persists them as None, quarantine strips them.
_NOUS_EMPTY_AGENT_KEY_FIELDS: Dict[str, Any] = {
- "agent_key": None,
- "agent_key_id": None,
- "agent_key_expires_at": None,
- "agent_key_expires_in": None,
- "agent_key_reused": None,
- "agent_key_obtained_at": None,
+ "agent_key": None, "agent_key_id": None, "agent_key_expires_at": None,
+ "agent_key_expires_in": None, "agent_key_reused": None, "agent_key_obtained_at": None,
}
+_NOUS_STALE_PORTAL_HOSTS: FrozenSet[str] = frozenset({"api.nousresearch.com"})
-_NOUS_STALE_PORTAL_HOSTS: FrozenSet[str] = frozenset({
- "api.nousresearch.com",
-})
+
+def _portal_entitlement_message(capability: str) -> str:
+ """Portal entitlement notice for *capability* (fresh account data), "" when unavailable."""
+ from hermes_cli.nous_account import (
+ format_nous_portal_entitlement_message, get_nous_portal_account_info,
+ )
+ account_info = get_nous_portal_account_info(force_fresh=True)
+ return format_nous_portal_entitlement_message(account_info, capability=capability) or ""
def _format_nous_entitlement_auth_error(error: AuthError) -> str:
try:
- from hermes_cli.nous_account import (
- format_nous_portal_entitlement_message,
- get_nous_portal_account_info,
- )
-
- account_info = get_nous_portal_account_info(force_fresh=True)
- message = format_nous_portal_entitlement_message(
- account_info,
- capability="Nous model access",
- )
+ message = _portal_entitlement_message("Nous model access")
if message:
return message
except Exception:
@@ -117,39 +105,27 @@ def _migrate_stale_nous_portal_url(providers: Dict[str, Any]) -> None:
if not isinstance(nous, dict):
return
stored = (nous.get("portal_base_url") or "").strip()
- if stored:
- parsed = urlparse(stored)
- if parsed.hostname in _NOUS_STALE_PORTAL_HOSTS:
- logger.warning(
- "auth: migrating stale nous portal_base_url %s -> %s",
- stored, DEFAULT_NOUS_PORTAL_URL,
- )
- nous["portal_base_url"] = DEFAULT_NOUS_PORTAL_URL
+ if stored and urlparse(stored).hostname in _NOUS_STALE_PORTAL_HOSTS:
+ logger.warning(
+ "auth: migrating stale nous portal_base_url %s -> %s", stored, DEFAULT_NOUS_PORTAL_URL,
+ )
+ nous["portal_base_url"] = DEFAULT_NOUS_PORTAL_URL
-# Allowlist of hosts the Nous Portal proxy is willing to forward inference
-# JWTs to. Sending a bearer anywhere else would leak it.
-#
-# This is consulted only for URLs coming from the NETWORK side (Portal
-# refresh responses). User-controlled env-var overrides
-# (NOUS_INFERENCE_BASE_URL) bypass validation — that's the documented
-# dev/staging escape hatch and the env source is already trusted (the
+# Allowlist of hosts the Nous Portal proxy will forward inference JWTs to — a bearer sent anywhere
+# else would leak. Consulted only for URLs from the NETWORK side (Portal refresh responses);
+# the NOUS_INFERENCE_BASE_URL env override bypasses it (documented dev/staging escape hatch, the
# user set it themselves).
-_ALLOWED_NOUS_INFERENCE_HOSTS: FrozenSet[str] = frozenset({
- "inference-api.nousresearch.com",
-})
+_ALLOWED_NOUS_INFERENCE_HOSTS: FrozenSet[str] = frozenset({"inference-api.nousresearch.com"})
def _validate_nous_inference_url_from_network(url: Optional[str]) -> Optional[str]:
"""Validate a Portal-returned inference URL against the host allowlist.
- Defense-in-depth: a compromised refresh response from the Portal API (MITM, malicious response
- injection) could otherwise redirect every subsequent proxy request — bearing the user's
- inference JWT — to an attacker-controlled endpoint.
+ Defense-in-depth: a compromised refresh response (MITM, response injection) could otherwise
+ redirect every proxy request — bearing the user's inference JWT — to an attacker endpoint.
"""
- if not isinstance(url, str):
- return None
- cleaned = url.strip()
+ cleaned = url.strip() if isinstance(url, str) else ""
if not cleaned:
return None
try:
@@ -158,8 +134,7 @@ def _validate_nous_inference_url_from_network(url: Optional[str]) -> Optional[st
return None
if parsed.scheme != "https":
logger.warning(
- "nous: refusing non-https inference URL scheme %r from Portal response",
- parsed.scheme,
+ "nous: refusing non-https inference URL scheme %r from Portal response", parsed.scheme,
)
return None
if parsed.hostname not in _ALLOWED_NOUS_INFERENCE_HOSTS:
@@ -173,23 +148,21 @@ def _validate_nous_inference_url_from_network(url: Optional[str]) -> Optional[st
def _nous_inference_env_override() -> Optional[str]:
- """Return the user-set ``NOUS_INFERENCE_BASE_URL`` override, if any.
+ """User-set ``NOUS_INFERENCE_BASE_URL`` override (trailing slash stripped) or None.
- Documented dev/staging escape hatch. The env source is trusted (the OS user set it), so unlike
- Portal-returned URLs it is intentionally NOT gated by the network host allowlist.
- Returns a trailing-slash-stripped string, or ``None`` when unset/blank.
+ Documented dev/staging escape hatch; the env source is trusted, so unlike Portal-returned URLs
+ it is intentionally NOT gated by the network host allowlist.
"""
from hermes_cli.auth import _optional_base_url
return _optional_base_url(os.getenv("NOUS_INFERENCE_BASE_URL"))
def _nous_portal_env_override() -> Optional[str]:
- """Return the user/deployment-set Portal base URL override, if any.
+ """``HERMES_PORTAL_BASE_URL`` / ``NOUS_PORTAL_BASE_URL`` override or None.
- ``HERMES_PORTAL_BASE_URL`` / ``NOUS_PORTAL_BASE_URL`` are the documented dev/staging escape
- hatch (e.g. hosted agents on the staging Portal). Like the inference override, the env source
- is trusted and must NOT be gated by ``_NOUS_PORTAL_ALLOWED_HOSTS``: that allowlist rejects an
- untrusted NETWORK-provided value persisted to auth.json, not one the operator configured.
+ Documented dev/staging escape hatch (e.g. hosted agents on the staging Portal). Trusted env
+ source: must NOT be gated by ``_NOUS_PORTAL_ALLOWED_HOSTS``, which rejects untrusted
+ NETWORK-provided values persisted to auth.json, not operator config.
"""
from hermes_cli.auth import _optional_base_url
return _optional_base_url(
@@ -198,14 +171,11 @@ def _nous_portal_env_override() -> Optional[str]:
def _scope_values(raw_scope: Any) -> set[str]:
- # OAuth token responses normally return a space-separated string. Keep
- # collection support for JWT ``scp`` claims and older stored test fixtures.
+ # OAuth token responses return a space-separated string; collections are kept for JWT ``scp``
+ # claims and older stored fixtures.
scopes: set[str] = set()
if isinstance(raw_scope, str):
- for part in raw_scope.replace(",", " ").split():
- cleaned = part.strip()
- if cleaned:
- scopes.add(cleaned)
+ scopes.update(part for part in raw_scope.replace(",", " ").split() if part.strip())
elif isinstance(raw_scope, (list, tuple, set, frozenset)):
for item in raw_scope:
if isinstance(item, str):
@@ -226,21 +196,15 @@ def _nous_invoke_jwt_status(
if not claims:
return "access_token_not_jwt"
scopes = (
- _scope_values(scope)
- | _scope_values(claims.get("scope"))
- | _scope_values(claims.get("scp"))
+ _scope_values(scope) | _scope_values(claims.get("scope")) | _scope_values(claims.get("scp"))
)
if NOUS_INFERENCE_INVOKE_SCOPE not in scopes:
return "missing_inference_invoke_scope"
exp = claims.get("exp")
skew = max(0, int(min_ttl_seconds))
if isinstance(exp, (int, float)):
- if float(exp) <= (time.time() + skew):
- return "invoke_jwt_expiring"
- return None
- if _is_expiring(expires_at, skew):
- return "invoke_jwt_expiry_unknown_or_expiring"
- return None
+ return "invoke_jwt_expiring" if float(exp) <= (time.time() + skew) else None
+ return "invoke_jwt_expiry_unknown_or_expiring" if _is_expiring(expires_at, skew) else None
def _nous_invoke_jwt_is_usable(
@@ -251,14 +215,16 @@ def _nous_invoke_jwt_is_usable(
min_ttl_seconds: int = NOUS_INVOKE_JWT_MIN_TTL_SECONDS,
) -> bool:
from hermes_cli.auth import _nous_invoke_jwt_status
- return (
- _nous_invoke_jwt_status(
- token,
- scope=scope,
- expires_at=expires_at,
- min_ttl_seconds=min_ttl_seconds,
- )
- is None
+ return _nous_invoke_jwt_status(
+ token, scope=scope, expires_at=expires_at, min_ttl_seconds=min_ttl_seconds,
+ ) is None
+
+
+def _state_invoke_jwt_status(state: Dict[str, Any], token: Any) -> Optional[str]:
+ """``_nous_invoke_jwt_status`` for *token* using *state*'s scope / expires_at (patchable)."""
+ from hermes_cli.auth import _nous_invoke_jwt_status
+ return _nous_invoke_jwt_status(
+ token, scope=state.get("scope"), expires_at=state.get("expires_at"),
)
@@ -267,35 +233,29 @@ def _assert_nous_inference_jwt_usable(
*,
access_token: Any = None,
) -> None:
- from hermes_cli.auth import _nous_invoke_jwt_status
token = state.get("access_token") if access_token is None else access_token
- reason = _nous_invoke_jwt_status(
- token,
- scope=state.get("scope"),
- expires_at=state.get("expires_at"),
- )
- if reason is None:
- return
- raise _nous_err(
- "Nous Portal access token is not a usable inference JWT "
- f"({reason}). Re-authenticate with: hermes auth add nous",
- reason, relogin=True,
- )
+ reason = _state_invoke_jwt_status(state, token)
+ if reason is not None:
+ raise _unusable_invoke_jwt_error(reason)
-def _log_nous_invoke_jwt_selected(
- *,
- access_token: Any,
- sequence_id: Optional[str] = None,
-) -> None:
+def _log_nous_invoke_jwt_selected(*, access_token: Any, sequence_id: Optional[str] = None) -> None:
logger.debug("Nous inference auth: using NAS invoke JWT")
_oauth_trace(
- "nous_invoke_jwt_selected",
- sequence_id=sequence_id,
+ "nous_invoke_jwt_selected", sequence_id=sequence_id,
access_token_fp=_token_fingerprint(access_token),
)
+def _remaining_ttl(expires_at: Any, fallback_expires_in: Any) -> int:
+ """Seconds until *expires_at* (ISO), else *fallback_expires_in* coerced to a TTL."""
+ from hermes_cli.auth import _coerce_ttl_seconds, _parse_iso_timestamp
+ expires_epoch = _parse_iso_timestamp(expires_at)
+ if expires_epoch is not None:
+ return max(0, int(expires_epoch - time.time()))
+ return _coerce_ttl_seconds(fallback_expires_in)
+
+
def _nous_jwt_expires_at(token: Any, fallback_expires_at: Any = None) -> Optional[str]:
claims = _decode_jwt_claims(token)
exp = claims.get("exp")
@@ -312,29 +272,19 @@ def _set_nous_agent_key_from_invoke_jwt(
*,
obtained_at: Optional[str] = None,
) -> None:
- from hermes_cli.auth import _coerce_ttl_seconds, _nonempty_str, _parse_iso_timestamp
+ from hermes_cli.auth import _nonempty_str
access_token = state.get("access_token")
if not _nonempty_str(access_token):
return
- now = datetime.now(timezone.utc)
existing_obtained_at = state.get("agent_key_obtained_at")
if obtained_at:
effective_obtained_at = obtained_at
- elif (
- state.get("agent_key") == access_token
- and isinstance(existing_obtained_at, str)
- and existing_obtained_at.strip()
- ):
+ elif state.get("agent_key") == access_token and _nonempty_str(existing_obtained_at):
effective_obtained_at = existing_obtained_at
else:
- effective_obtained_at = now.isoformat()
+ effective_obtained_at = datetime.now(timezone.utc).isoformat()
expires_at = _nous_jwt_expires_at(access_token, state.get("expires_at"))
- expires_epoch = _parse_iso_timestamp(expires_at)
- expires_in = (
- max(0, int(expires_epoch - time.time()))
- if expires_epoch is not None
- else _coerce_ttl_seconds(state.get("expires_in"))
- )
+ expires_in = _remaining_ttl(expires_at, state.get("expires_in"))
if expires_at:
state["expires_at"] = expires_at
state["expires_in"] = expires_in
@@ -356,46 +306,29 @@ def _select_nous_invoke_jwt(
if _nonempty_str(access_token):
state["access_token"] = access_token
_set_nous_agent_key_from_invoke_jwt(state)
- _log_nous_invoke_jwt_selected(
- access_token=state.get("access_token"),
- sequence_id=sequence_id,
- )
+ _log_nous_invoke_jwt_selected(access_token=state.get("access_token"), sequence_id=sequence_id)
-_NOUS_EFFECTIVE_STATE_IGNORED_KEYS = frozenset({
- # These are derived from expires_at/JWT exp and naturally tick down between
- # reads. Persisting only these changes makes auth.json noisy and defeats
- # the mtime-keyed auth-status cache.
- "expires_in",
- "agent_key_expires_in",
-})
+# Derived from expires_at/JWT exp and tick down between reads; persisting only these changes makes
+# auth.json noisy and defeats the mtime-keyed auth-status cache.
+_NOUS_EFFECTIVE_STATE_IGNORED_KEYS = frozenset({"expires_in", "agent_key_expires_in"})
def _nous_effective_provider_state(state: Dict[str, Any]) -> Dict[str, Any]:
- return {
- key: value
- for key, value in state.items()
- if key not in _NOUS_EFFECTIVE_STATE_IGNORED_KEYS
- }
+ return {k: v for k, v in state.items() if k not in _NOUS_EFFECTIVE_STATE_IGNORED_KEYS}
NOUS_SHARED_STORE_FILENAME = "nous_auth.json"
-
-
_nous_shared_lock_holder = threading.local()
def _nous_shared_auth_dir() -> Path:
- """Resolve the directory that holds the shared Nous token store.
-
- Honors ``HERMES_SHARED_AUTH_DIR`` so tests can redirect it. Defaults to
- ``<hermes-root>/shared/`` (``~/.hermes/shared/`` on POSIX, ``%LOCALAPPDATA%\\hermes\\shared\\``
- on Windows), outside any named profile so all profiles under one root share the store.
+ """Directory of the shared Nous token store: ``HERMES_SHARED_AUTH_DIR`` or ``<root>/shared/``.
- The store lets ``hermes --profile <name> auth add nous --type oauth`` one-tap import instead
- of re-running device-code. It is written on login AND on every runtime refresh so the stored
- refresh_token stays current when one profile rotates it; a server-side stale token just makes
- the import fail gracefully and falls back to the device-code flow.
+ It sits outside any named profile so all profiles under one root share it, letting
+ ``hermes --profile <name> auth add nous --type oauth`` one-tap import instead of re-running
+ device-code. Written on login AND every runtime refresh so the stored refresh_token stays
+ current when one profile rotates it; a stale token makes the import fall back to device-code.
"""
override = os.getenv("HERMES_SHARED_AUTH_DIR", "").strip()
if override:
@@ -406,12 +339,9 @@ def _nous_shared_auth_dir() -> Path:
def _nous_shared_store_path() -> Path:
path = _nous_shared_auth_dir() / NOUS_SHARED_STORE_FILENAME
- # Seat belt: if pytest is running and this resolves to a path under the
- # real user's Hermes root, refuse rather than silently corrupt cross-profile
- # state. Tests must set HERMES_SHARED_AUTH_DIR to a tmp_path (conftest
- # does not do this automatically — mirror the _auth_file_path() guard
- # so forgetting to set it fails loudly instead of writing to the real
- # shared store).
+ # Seat belt (mirrors the _auth_file_path() guard): under pytest, refuse a path under the real
+ # user's Hermes root so a test that forgot HERMES_SHARED_AUTH_DIR fails loudly instead of
+ # corrupting cross-profile state.
if os.environ.get("PYTEST_CURRENT_TEST"):
from hermes_constants import get_default_hermes_root
real_home_shared = (
@@ -440,30 +370,23 @@ def _nous_shared_store_lock(timeout_seconds: float = AUTH_LOCK_TIMEOUT_SECONDS):
try:
lock_path = _nous_shared_store_path().with_suffix(".lock")
except RuntimeError:
- # No HERMES_HOME yet (pre-setup): fall through without locking.
- yield
+ yield # No HERMES_HOME yet (pre-setup): fall through without locking.
return
-
with _file_lock(
- lock_path,
- _nous_shared_lock_holder,
- timeout_seconds,
+ lock_path, _nous_shared_lock_holder, timeout_seconds,
"Timed out waiting for shared Nous auth lock",
):
yield
+def _shared_lock_timeout(timeout_seconds: float) -> float:
+ return max(timeout_seconds + 5.0, AUTH_LOCK_TIMEOUT_SECONDS)
+
+
# OAuth fields mirrored between a profile's Nous state and the shared cross-profile store.
_NOUS_SHARED_STATE_KEYS = (
- "access_token",
- "refresh_token",
- "token_type",
- "scope",
- "client_id",
- "portal_base_url",
- "inference_base_url",
- "obtained_at",
- "expires_at",
+ "access_token", "refresh_token", "token_type", "scope", "client_id", "portal_base_url",
+ "inference_base_url", "obtained_at", "expires_at",
)
@@ -473,19 +396,14 @@ def _merge_shared_nous_oauth_state(state: Dict[str, Any]) -> bool:
shared = _read_shared_nous_state()
if not shared:
return False
-
shared_refresh = shared.get("refresh_token")
if not _nonempty_str(shared_refresh):
return False
-
- local_refresh = state.get("refresh_token")
shared_access_exp = _parse_iso_timestamp(shared.get("expires_at")) or 0.0
local_access_exp = _parse_iso_timestamp(state.get("expires_at")) or 0.0
- refresh_changed = shared_refresh.strip() != str(local_refresh or "").strip()
- fresher_access = shared_access_exp > local_access_exp
- if not refresh_changed and not fresher_access:
+ refresh_changed = shared_refresh.strip() != str(state.get("refresh_token") or "").strip()
+ if not refresh_changed and not shared_access_exp > local_access_exp:
return False
-
for key in _NOUS_SHARED_STATE_KEYS:
value = shared.get(key)
if value not in {None, ""}:
@@ -511,19 +429,15 @@ def _nous_shared_shape(src: Dict[str, Any]) -> Dict[str, Any]:
def _write_shared_nous_state(state: Dict[str, Any]) -> None:
"""Persist a minimal copy of the Nous OAuth state to the shared store.
- Best-effort: any failure is swallowed after logging. The shared store is a convenience layer;
- the per-profile auth.json remains the source of truth.
+ Best-effort: failures are logged and swallowed; per-profile auth.json stays the source of truth.
"""
from hermes_cli.auth import _nonempty_str, _write_private_file_atomic
refresh_token = state.get("refresh_token")
- access_token = state.get("access_token")
# No refresh_token = nothing worth sharing across profiles
- if not (_nonempty_str(refresh_token) and _nonempty_str(access_token)):
+ if not (_nonempty_str(refresh_token) and _nonempty_str(state.get("access_token"))):
return
-
shared = {
- "_schema": 1,
- **_nous_shared_shape(state),
+ "_schema": 1, **_nous_shared_shape(state),
"updated_at": datetime.now(timezone.utc).isoformat(),
}
try:
@@ -533,8 +447,7 @@ def _write_shared_nous_state(state: Dict[str, Any]) -> None:
path, json.dumps(shared, indent=2, sort_keys=True), replace=os.replace,
)
_oauth_trace(
- "nous_shared_store_written",
- path=str(path),
+ "nous_shared_store_written", path=str(path),
refresh_token_fp=_token_fingerprint(refresh_token),
)
except Exception as exc:
@@ -542,17 +455,15 @@ def _write_shared_nous_state(state: Dict[str, Any]) -> None:
def _read_shared_nous_state() -> Optional[Dict[str, Any]]:
- """Return the shared Nous OAuth state if present and well-formed.
+ """Shared Nous OAuth state when present and well-formed, else None.
- Returns ``None`` when the file is missing, unreadable, malformed, or lacks required fields;
- callers treat that as "no shared credentials, fall through to device-code".
+ None (missing / unreadable / malformed / lacking tokens) means "fall through to device-code".
"""
from hermes_cli.auth import _nonempty_str
try:
path = _nous_shared_store_path()
except RuntimeError:
- # Test seat belt tripped — treat as missing
- return None
+ return None # Test seat belt tripped — treat as missing
if not path.is_file():
return None
try:
@@ -562,9 +473,8 @@ def _read_shared_nous_state() -> Optional[Dict[str, Any]]:
return None
if not isinstance(payload, dict):
return None
- if not (_nonempty_str(payload.get("refresh_token")) and _nonempty_str(payload.get("access_token"))):
- return None
- return payload
+ has_tokens = _nonempty_str(payload.get("refresh_token")) and _nonempty_str(payload.get("access_token"))
+ return payload if has_tokens else None
def _clear_shared_nous_state(reason: str) -> None:
@@ -581,39 +491,20 @@ def _clear_shared_nous_state(reason: str) -> None:
logger.debug("Failed to clear shared Nous auth store: %s", exc)
-# Error codes per provider for which retrying the SAME refresh token cannot succeed.
-# ``*_refresh_failed`` covers HTTP 400/401/403 from the token endpoint (invalid_grant, token
-# revoked, refresh_token_reused); ``*_auth_missing_refresh_token`` means the pool entry has no
-# refresh token at all. All must also carry ``relogin_required=True``; transient failures
-# (429, 5xx) do not.
-def _quarantine_nous_oauth_state(
- state: Dict[str, Any],
- error: AuthError,
- *,
- reason: str,
-) -> None:
- """Keep routing metadata but remove dead OAuth material so it is not replayed."""
- from hermes_cli.auth import _FLAT_OAUTH_TOKEN_KEYS, _auth_file_path, _last_auth_error_marker, invalidate_nous_auth_status_cache
- # Forensic logging BEFORE we clear the token material. A hosted agent
- # can take a terminal invalid_grant and get quarantined here silently: the
- # only downstream signal is a "No access token found" WARNING once the pool
- # is already empty, which is too late to root-cause. A managed log drain may
- # be WARNING-only, so this MUST be logger.warning (INFO never reaches it).
- #
- # Redaction safety: emit ONLY the 12-char SHA-256 hex prefix of the refresh
- # token (correlates to NAS's refreshTokenHash without leaking the secret) plus
- # sizes/booleans. NEVER pass a raw token/agent_key into the log call — Hermes
- # has a known bug class where credential-shaped literals get corrupted in logs.
+def _quarantine_forensics(state: Dict[str, Any], error: AuthError, reason: str) -> Dict[str, Any]:
+ """Redaction-safe forensic record for a quarantine: fingerprints, sizes and booleans only.
+
+ NEVER include a raw token/agent_key — Hermes has a known bug class where credential-shaped
+ literals get corrupted in logs. The 12-char SHA-256 prefix correlates to NAS's
+ refreshTokenHash without leaking the secret. Provenance is client_id + agent_key_id (no
+ session_id exists on Nous state).
+ """
+ from hermes_cli.auth import _auth_file_path
forensic: Dict[str, Any] = {
- "reason": reason,
- "error_code": error.code,
- # No session_id field exists on Nous state; provenance is client_id +
- # agent_key_id (both non-secret routing identifiers).
- "client_id": state.get("client_id"),
+ "reason": reason, "error_code": error.code, "client_id": state.get("client_id"),
"agent_key_id": state.get("agent_key_id"),
"refresh_token_fp": _token_fingerprint(state.get("refresh_token")),
}
-
# On-disk integrity of the auth store at the moment of quarantine.
try:
auth_path = _auth_file_path()
@@ -627,7 +518,6 @@ def _quarantine_nous_oauth_state(
forensic["auth_json_exists"] = False
except Exception as exc: # pragma: no cover - never let logging break quarantine
forensic["auth_json_stat_error"] = repr(exc)
-
# Was the token already past its own expiry when it was rejected?
already_expired: Optional[bool] = None
expires_at_raw = state.get("expires_at")
@@ -640,10 +530,25 @@ def _quarantine_nous_oauth_state(
except ValueError:
already_expired = None
forensic["token_already_expired"] = already_expired
+ return forensic
+
+
+def _quarantine_nous_oauth_state(state: Dict[str, Any], error: AuthError, *, reason: str) -> None:
+ """Keep routing metadata but remove dead OAuth material so it is not replayed.
+ Only for terminal errors (``*_refresh_failed`` = HTTP 400/401/403 invalid_grant / revoked /
+ refresh_token_reused; ``*_auth_missing_refresh_token``), all ``relogin_required=True`` —
+ transient 429/5xx never quarantine.
+ """
+ from hermes_cli.auth import (
+ _FLAT_OAUTH_TOKEN_KEYS, _last_auth_error_marker, invalidate_nous_auth_status_cache,
+ )
+ # Forensics BEFORE clearing token material: a hosted agent quarantined here is otherwise only
+ # visible as a later "No access token found" WARNING, too late to root-cause. Managed log
+ # drains may be WARNING-only, so this MUST be logger.warning.
logger.warning(
"Nous OAuth state quarantined (terminal auth death): %s",
- json.dumps(forensic, sort_keys=True, ensure_ascii=False),
+ json.dumps(_quarantine_forensics(state, error, reason), sort_keys=True, ensure_ascii=False),
)
for key in (*_FLAT_OAUTH_TOKEN_KEYS, *_NOUS_EMPTY_AGENT_KEY_FIELDS):
@@ -654,78 +559,52 @@ def _quarantine_nous_oauth_state(
def _quarantine_nous_pool_entries(
- auth_store: Dict[str, Any],
- error: AuthError,
- *,
- reason: str,
+ auth_store: Dict[str, Any], error: AuthError, *, reason: str,
) -> bool:
"""Remove singleton-seeded Nous pool entries that contain dead OAuth state."""
entries = _pool_entries(auth_store, "nous")
if entries is None:
return False
- pool = auth_store["credential_pool"]
-
- retained = []
- removed = False
singleton_sources = {NOUS_DEVICE_CODE_SOURCE, f"manual:{NOUS_DEVICE_CODE_SOURCE}"}
- for entry in entries:
- if isinstance(entry, dict) and entry.get("source") in singleton_sources:
- removed = True
- continue
- retained.append(entry)
-
+ retained = [
+ e for e in entries if not (isinstance(e, dict) and e.get("source") in singleton_sources)
+ ]
+ removed = len(retained) != len(entries)
if removed:
- pool["nous"] = retained
- _oauth_trace(
- "nous_pool_device_code_quarantined",
- reason=reason,
- error_code=error.code,
- )
+ auth_store["credential_pool"]["nous"] = retained
+ _oauth_trace("nous_pool_device_code_quarantined", reason=reason, error_code=error.code)
return removed
-def _try_import_shared_nous_state(
- *,
- timeout_seconds: float = 15.0,
-) -> Optional[Dict[str, Any]]:
- """Attempt to rehydrate Nous OAuth state from the shared store.
-
- Runs a forced refresh with the stored refresh_token to mint a fresh inference JWT scoped to
- this profile and returns the auth_state dict ready for ``persist_nous_credentials()``.
- Returns ``None`` on any failure (expired token, portal unreachable) so the caller falls
- through to the normal device-code flow.
+def _try_import_shared_nous_state(*, timeout_seconds: float = 15.0) -> Optional[Dict[str, Any]]:
+ """Rehydrate Nous OAuth state from the shared store via a forced refresh.
+
+ Mints a fresh inference JWT scoped to this profile and returns the auth_state ready for
+ ``persist_nous_credentials()``. None on any failure (expired token, portal unreachable) so the
+ caller falls through to the normal device-code flow.
"""
- from hermes_cli.auth import _read_shared_nous_state, _write_shared_nous_state, refresh_nous_oauth_from_state, _is_terminal_nous_refresh_error
+ from hermes_cli.auth import (
+ _read_shared_nous_state, _write_shared_nous_state, refresh_nous_oauth_from_state,
+ _is_terminal_nous_refresh_error,
+ )
try:
- with _nous_shared_store_lock(timeout_seconds=max(timeout_seconds + 5.0, AUTH_LOCK_TIMEOUT_SECONDS)):
+ with _nous_shared_store_lock(timeout_seconds=_shared_lock_timeout(timeout_seconds)):
shared = _read_shared_nous_state()
if not shared:
return None
-
- # Build a full state dict so refresh_nous_oauth_from_state has every
- # field it needs. force_refresh=True gets us a fresh access_token
- # for this profile.
+ # Full state dict so refresh_nous_oauth_from_state has every field it needs.
state: Dict[str, Any] = {
- **_nous_shared_shape(shared),
- "agent_key": None,
- "agent_key_expires_at": None,
+ **_nous_shared_shape(shared), "agent_key": None, "agent_key_expires_at": None,
"tls": {"insecure": False, "ca_bundle": None},
}
-
- def _persist_shared_refresh(updated_state: Dict[str, Any], _reason: str) -> None:
- _write_shared_nous_state(updated_state)
-
refreshed = refresh_nous_oauth_from_state(
- state,
- timeout_seconds=timeout_seconds,
- force_refresh=True,
- on_state_update=_persist_shared_refresh,
+ state, timeout_seconds=timeout_seconds, force_refresh=True,
+ on_state_update=lambda updated, _reason: _write_shared_nous_state(updated),
)
_write_shared_nous_state(refreshed)
except AuthError as exc:
_oauth_trace(
- "nous_shared_import_failed",
- error_type=type(exc).__name__,
+ "nous_shared_import_failed", error_type=type(exc).__name__,
error_code=getattr(exc, "code", None),
)
if _is_terminal_nous_refresh_error(exc):
@@ -733,32 +612,20 @@ def _try_import_shared_nous_state(
logger.debug("Shared Nous import failed: %s", exc)
return None
except Exception as exc:
- _oauth_trace(
- "nous_shared_import_failed",
- error_type=type(exc).__name__,
- )
+ _oauth_trace("nous_shared_import_failed", error_type=type(exc).__name__)
logger.debug("Shared Nous import failed: %s", exc)
return None
-
return refreshed
def _refresh_access_token(
- *,
- client: httpx.Client,
- portal_base_url: str,
- client_id: str,
- refresh_token: str,
+ *, client: httpx.Client, portal_base_url: str, client_id: str, refresh_token: str,
) -> Dict[str, Any]:
response = client.post(
f"{portal_base_url}/api/oauth/token",
headers={"x-nous-refresh-token": refresh_token},
- data={
- "grant_type": "refresh_token",
- "client_id": client_id,
- },
+ data={"grant_type": "refresh_token", "client_id": client_id},
)
-
if response.status_code == 200:
payload = response.json()
if "access_token" not in payload:
@@ -773,16 +640,10 @@ def _refresh_access_token(
code = str(error_payload.get("error", "invalid_grant"))
description = str(error_payload.get("error_description") or "Refresh token exchange failed")
relogin = code in {"invalid_grant", "invalid_token", "refresh_token_reused"}
-
- # Detect the OAuth 2.1 "refresh token reuse" signal from the Nous portal
- # server and surface an actionable message. This fires when an external
- # process (health-check script, monitoring tool, custom self-heal hook)
- # called POST /api/oauth/token with Hermes's refresh_token without
- # persisting the rotated token back to auth.json — the server then
- # retires the original RT, Hermes's next refresh uses it, and the whole
- # session chain gets revoked as a token-theft signal (#15099).
- lowered = description.lower()
- if code == "refresh_token_reused" or "reuse" in lowered or "reuse detected" in lowered:
+ # OAuth 2.1 "refresh token reuse": an external process (health check, monitoring tool, custom
+ # self-heal hook) redeemed Hermes's refresh_token without persisting the rotated token, so the
+ # server retired the original and revoked the whole session chain as a token-theft signal.
+ if code == "refresh_token_reused" or "reuse" in description.lower():
description = (
"Nous Portal detected refresh-token reuse and revoked this session.\n"
"This usually means an external process (monitoring script, "
@@ -800,23 +661,15 @@ def _refresh_access_token(
def _refresh_nous_or_quarantine(
- *,
- client: httpx.Client,
- auth_store: Dict[str, Any],
- state: Dict[str, Any],
- portal_base_url: str,
- client_id: str,
- refresh_token: str,
- reason: str,
+ *, client: httpx.Client, auth_store: Dict[str, Any], state: Dict[str, Any],
+ portal_base_url: str, client_id: str, refresh_token: str, reason: str,
persist: Callable[[], None],
) -> Dict[str, Any]:
- """Redeem the Nous refresh token; on a terminal failure quarantine state + pool, persist, re-raise."""
+ """Redeem the Nous refresh token; on terminal failure quarantine state + pool, persist, re-raise."""
from hermes_cli.auth import _refresh_access_token, _is_terminal_nous_refresh_error
try:
return _refresh_access_token(
- client=client,
- portal_base_url=portal_base_url,
- client_id=client_id,
+ client=client, portal_base_url=portal_base_url, client_id=client_id,
refresh_token=refresh_token,
)
except AuthError as exc:
@@ -828,10 +681,7 @@ def _refresh_nous_or_quarantine(
def _apply_nous_refreshed_tokens(
- state: Dict[str, Any],
- refreshed: Dict[str, Any],
- refresh_token: str,
- *,
+ state: Dict[str, Any], refreshed: Dict[str, Any], refresh_token: str, *,
inference_base_url: Optional[str] = None,
) -> None:
"""Write a successful Nous token-refresh payload into *state* (tokens + expiry fields).
@@ -856,10 +706,9 @@ def _apply_nous_refreshed_tokens(
def _healed_nous_inference_url(refreshed: Dict[str, Any]) -> str:
"""Validated network-provenance inference URL from a refresh payload, healed to the default.
- When the Portal-returned URL is rejected by the allowlist (returns None), reset to the
- production default instead of leaving a previously-persisted bad host (e.g. a stale staging
- URL) in place — otherwise a poisoned auth.json keeps re-validating to None on every refresh
- and silently re-uses the dead endpoint.
+ A Portal URL rejected by the allowlist resets to the production default instead of leaving a
+ previously-persisted bad host (e.g. a stale staging URL) in place — otherwise a poisoned
+ auth.json re-validates to None on every refresh and silently re-uses the dead endpoint.
"""
return (
_validate_nous_inference_url_from_network(refreshed.get("inference_base_url"))
@@ -867,22 +716,36 @@ def _healed_nous_inference_url(refreshed: Dict[str, Any]) -> str:
)
+def _nous_http_client(timeout_seconds: float, verify: Any) -> httpx.Client:
+ return httpx.Client(
+ timeout=httpx.Timeout(timeout_seconds), headers={"Accept": "application/json"},
+ verify=verify,
+ )
+
+
+def _model_priority(mid: str) -> tuple:
+ """Sort key: opus > pro > haiku/flash > sonnet (sonnet is cheap/fast; best model first)."""
+ low = mid.lower()
+ if "opus" in low:
+ return (0, mid)
+ if "pro" in low and "sonnet" not in low:
+ return (1, mid)
+ if "sonnet" in low:
+ return (3, mid)
+ return (2, mid)
+
+
def fetch_nous_models(
- *,
- inference_base_url: str,
- api_key: str,
- timeout_seconds: float = 15.0,
+ *, inference_base_url: str, api_key: str, timeout_seconds: float = 15.0,
verify: bool | str = True,
) -> List[str]:
"""Fetch available model IDs from the Nous inference API."""
from hermes_cli.auth import _nonempty_str
- timeout = httpx.Timeout(timeout_seconds)
- with httpx.Client(timeout=timeout, headers={"Accept": "application/json"}, verify=verify) as client:
+ with _nous_http_client(timeout_seconds, verify) as client:
response = client.get(
f"{inference_base_url.rstrip('/')}/models",
headers={"Authorization": f"Bearer {api_key}"},
)
-
if response.status_code != 200:
description = f"/models request failed with status {response.status_code}"
try:
@@ -892,35 +755,17 @@ def fetch_nous_models(
logger.debug("Could not parse error response JSON: %s", e)
raise _nous_err(description, "models_fetch_failed")
- payload = response.json()
- data = payload.get("data")
+ data = response.json().get("data")
if not isinstance(data, list):
return []
-
model_ids: List[str] = []
for item in data:
- if not isinstance(item, dict):
- continue
- model_id = item.get("id")
+ model_id = item.get("id") if isinstance(item, dict) else None
if _nonempty_str(model_id):
mid = model_id.strip()
- # Skip Hermes models — they're not reliable for agentic tool-calling
- if "hermes" in mid.lower():
+ if "hermes" in mid.lower(): # Hermes models aren't reliable for agentic tool-calling
continue
model_ids.append(mid)
-
- # Sort: prefer opus > pro > haiku/flash > sonnet (sonnet is cheap/fast,
- # users who want the best model should see opus first).
- def _model_priority(mid: str) -> tuple:
- low = mid.lower()
- if "opus" in low:
- return (0, mid)
- if "pro" in low and "sonnet" not in low:
- return (1, mid)
- if "sonnet" in low:
- return (3, mid)
- return (2, mid)
-
model_ids.sort(key=_model_priority)
return list(dict.fromkeys(model_ids))
@@ -928,87 +773,54 @@ def fetch_nous_models(
def _agent_key_is_usable(state: Dict[str, Any], min_ttl_seconds: int) -> bool:
from hermes_cli.auth import _nonempty_str
key = state.get("agent_key")
- if not _nonempty_str(key):
- return False
- return _nous_invoke_jwt_is_usable(
- key,
- scope=state.get("scope"),
- expires_at=state.get("agent_key_expires_at"),
+ return _nonempty_str(key) and _nous_invoke_jwt_is_usable(
+ key, scope=state.get("scope"), expires_at=state.get("agent_key_expires_at"),
min_ttl_seconds=max(0, int(min_ttl_seconds)),
)
def refresh_nous_oauth_pure(
- access_token: str,
- refresh_token: str,
- client_id: str,
- portal_base_url: str,
- inference_base_url: str,
- *,
- token_type: str = "Bearer",
- scope: str = DEFAULT_NOUS_SCOPE,
- obtained_at: Optional[str] = None,
- expires_at: Optional[str] = None,
- agent_key: Optional[str] = None,
- agent_key_expires_at: Optional[str] = None,
- timeout_seconds: float = 15.0,
- insecure: Optional[bool] = None,
- ca_bundle: Optional[str] = None,
+ access_token: str, refresh_token: str, client_id: str, portal_base_url: str,
+ inference_base_url: str, *, token_type: str = "Bearer", scope: str = DEFAULT_NOUS_SCOPE,
+ obtained_at: Optional[str] = None, expires_at: Optional[str] = None,
+ agent_key: Optional[str] = None, agent_key_expires_at: Optional[str] = None,
+ timeout_seconds: float = 15.0, insecure: Optional[bool] = None, ca_bundle: Optional[str] = None,
force_refresh: bool = False,
on_state_update: Optional[Callable[[Dict[str, Any], str], None]] = None,
) -> Dict[str, Any]:
"""Refresh Nous OAuth state without mutating auth.json directly.
- ``on_state_update`` is called after a successful access-token refresh. Callers that own
- persistent state can use it to save the newly rotated refresh token before later validation can
- fail.
+ ``on_state_update`` fires after a successful access-token refresh so callers owning persistent
+ state can save the rotated refresh token before later validation can fail.
"""
- from hermes_cli.auth import _assert_nous_inference_jwt_usable, _nous_invoke_jwt_status, _refresh_access_token, _resolve_verify, _select_nous_invoke_jwt
+ from hermes_cli.auth import (
+ _assert_nous_inference_jwt_usable, _refresh_access_token, _resolve_verify,
+ _select_nous_invoke_jwt,
+ )
state: Dict[str, Any] = {
- "access_token": access_token,
- "refresh_token": refresh_token,
+ "access_token": access_token, "refresh_token": refresh_token,
"client_id": client_id or DEFAULT_NOUS_CLIENT_ID,
"portal_base_url": (portal_base_url or DEFAULT_NOUS_PORTAL_URL).rstrip("/"),
"inference_base_url": (inference_base_url or DEFAULT_NOUS_INFERENCE_URL).rstrip("/"),
- "token_type": token_type or "Bearer",
- "scope": scope or DEFAULT_NOUS_SCOPE,
- "obtained_at": obtained_at,
- "expires_at": expires_at,
- "agent_key": agent_key,
+ "token_type": token_type or "Bearer", "scope": scope or DEFAULT_NOUS_SCOPE,
+ "obtained_at": obtained_at, "expires_at": expires_at, "agent_key": agent_key,
"agent_key_expires_at": agent_key_expires_at,
- "tls": {
- "insecure": bool(insecure),
- "ca_bundle": ca_bundle,
- },
+ "tls": {"insecure": bool(insecure), "ca_bundle": ca_bundle},
}
verify = _resolve_verify(insecure=insecure, ca_bundle=ca_bundle, auth_state=state)
- timeout = httpx.Timeout(timeout_seconds if timeout_seconds else 15.0)
-
- with httpx.Client(timeout=timeout, headers={"Accept": "application/json"}, verify=verify) as client:
- current_invoke_jwt_status = _nous_invoke_jwt_status(
- state.get("access_token"),
- scope=state.get("scope"),
- expires_at=state.get("expires_at"),
- )
+ with _nous_http_client(timeout_seconds or 15.0, verify) as client:
+ current_invoke_jwt_status = _state_invoke_jwt_status(state, state.get("access_token"))
if force_refresh or current_invoke_jwt_status is not None:
refresh_token_value = state.get("refresh_token")
if not isinstance(refresh_token_value, str) or not refresh_token_value:
if current_invoke_jwt_status is not None:
- raise _nous_err(
- "Nous Portal access token is not a usable inference JWT "
- f"({current_invoke_jwt_status}) and no refresh token is available. "
- "Re-authenticate with: hermes auth add nous",
- current_invoke_jwt_status, relogin=True,
+ raise _unusable_invoke_jwt_error(
+ current_invoke_jwt_status, no_refresh_token=True,
)
- raise _nous_err(
- "No refresh token is available for Nous Portal.",
- relogin=True,
- )
+ raise _nous_err("No refresh token is available for Nous Portal.", relogin=True)
refreshed = _refresh_access_token(
- client=client,
- portal_base_url=state["portal_base_url"],
- client_id=state["client_id"],
- refresh_token=refresh_token_value,
+ client=client, portal_base_url=state["portal_base_url"],
+ client_id=state["client_id"], refresh_token=refresh_token_value,
)
_apply_nous_refreshed_tokens(
state, refreshed, refresh_token_value,
@@ -1016,82 +828,54 @@ def refresh_nous_oauth_pure(
)
if on_state_update is not None:
on_state_update(dict(state), "post_refresh_access_token")
-
_assert_nous_inference_jwt_usable(state)
_select_nous_invoke_jwt(state)
-
return state
def refresh_nous_oauth_from_state(
- state: Dict[str, Any],
- *,
- timeout_seconds: float = 15.0,
- force_refresh: bool = False,
+ state: Dict[str, Any], *, timeout_seconds: float = 15.0, force_refresh: bool = False,
on_state_update: Optional[Callable[[Dict[str, Any], str], None]] = None,
) -> Dict[str, Any]:
"""Refresh Nous OAuth from a state dict. Thin wrapper around refresh_nous_oauth_pure."""
tls = state.get("tls") or {}
return refresh_nous_oauth_pure(
- state.get("access_token", ""),
- state.get("refresh_token", ""),
- state.get("client_id", "hermes-cli"),
- state.get("portal_base_url", DEFAULT_NOUS_PORTAL_URL),
+ state.get("access_token", ""), state.get("refresh_token", ""),
+ state.get("client_id", "hermes-cli"), state.get("portal_base_url", DEFAULT_NOUS_PORTAL_URL),
state.get("inference_base_url", DEFAULT_NOUS_INFERENCE_URL),
- token_type=state.get("token_type", "Bearer"),
- scope=state.get("scope", DEFAULT_NOUS_SCOPE),
- obtained_at=state.get("obtained_at"),
- expires_at=state.get("expires_at"),
- agent_key=state.get("agent_key"),
- agent_key_expires_at=state.get("agent_key_expires_at"),
- timeout_seconds=timeout_seconds,
- insecure=tls.get("insecure"),
+ token_type=state.get("token_type", "Bearer"), scope=state.get("scope", DEFAULT_NOUS_SCOPE),
+ obtained_at=state.get("obtained_at"), expires_at=state.get("expires_at"),
+ agent_key=state.get("agent_key"), agent_key_expires_at=state.get("agent_key_expires_at"),
+ timeout_seconds=timeout_seconds, insecure=tls.get("insecure"),
ca_bundle=tls.get("ca_bundle"),
- force_refresh=force_refresh,
- on_state_update=on_state_update,
+ force_refresh=force_refresh, on_state_update=on_state_update,
)
-def persist_nous_credentials(
- creds: Dict[str, Any],
- *,
- label: Optional[str] = None,
-):
- """Persist Nous OAuth credentials as the singleton provider state
-
- Nous credentials are read from two places: ``providers.nous`` (401 recovery, pool seeding) and
- ``credential_pool.nous`` (runtime ``pool.select()``). Writing only a pool entry left the
- singleton empty and made expiry recovery fail silently, so this writes the singleton and then
- ``load_pool("nous")`` upserts the canonical ``device_code`` entry in place (never duplicates).
- ``label`` is embedded in the singleton so re-seeding keeps the user's display name.
+def persist_nous_credentials(creds: Dict[str, Any], *, label: Optional[str] = None):
+ """Persist Nous OAuth credentials as the singleton provider state.
+
+ Nous credentials are read from ``providers.nous`` (401 recovery, pool seeding) AND
+ ``credential_pool.nous`` (runtime ``pool.select()``); writing only a pool entry left the
+ singleton empty and broke expiry recovery. So: write the singleton, mirror to the shared store,
+ then ``load_pool("nous")`` upserts the canonical ``device_code`` entry in place (never
+ duplicates). ``label`` is embedded in the singleton so re-seeding keeps the display name.
"""
from hermes_cli.auth import _save_active_provider_state, _write_shared_nous_state
from agent.credential_pool import load_pool
-
state = dict(creds)
if label and str(label).strip():
state["label"] = str(label).strip()
-
_save_active_provider_state("nous", state)
-
- # Mirror to the shared store so a new profile can one-tap import
- # these credentials via `hermes auth add nous --type oauth`. Best-
- # effort: any I/O failure is logged and swallowed (the per-profile
- # auth.json is still the source of truth).
_write_shared_nous_state(state)
-
pool = load_pool("nous")
- return next(
- (e for e in pool.entries() if e.source == NOUS_DEVICE_CODE_SOURCE),
- None,
- )
+ return next((e for e in pool.entries() if e.source == NOUS_DEVICE_CODE_SOURCE), None)
def _sync_nous_pool_from_auth_store() -> None:
"""Best-effort pool reseed after providers.nous changes; never fail login."""
try:
from agent.credential_pool import load_pool
-
load_pool("nous")
except Exception as exc:
logger.debug("Failed to sync Nous credential pool from auth store: %s", exc)
@@ -1107,10 +891,7 @@ class _NousStatePersister:
"""
def __init__(
- self,
- auth_store: Dict[str, Any],
- state: Dict[str, Any],
- state_source_path: Optional[Path],
+ self, auth_store: Dict[str, Any], state: Dict[str, Any], state_source_path: Optional[Path],
sequence_id: str,
) -> None:
self._auth_store = auth_store
@@ -1123,30 +904,20 @@ class _NousStatePersister:
def persist(self, reason: str) -> None:
from hermes_cli.auth import _save_provider_state_to_source, _write_shared_nous_state
state = self._state
- if (
- _nous_effective_provider_state(state)
- == _nous_effective_provider_state(self._persisted_state)
- ):
- _oauth_trace(
- "nous_state_persist_skipped",
- sequence_id=self._sequence_id,
- reason=reason,
- )
+ persisted = _nous_effective_provider_state(self._persisted_state)
+ if _nous_effective_provider_state(state) == persisted:
+ _oauth_trace("nous_state_persist_skipped", sequence_id=self._sequence_id, reason=reason)
return
try:
_save_provider_state_to_source(self._auth_store, "nous", state, self._source_path)
except Exception as exc:
_oauth_trace(
- "nous_state_persist_failed",
- sequence_id=self._sequence_id,
- reason=reason,
+ "nous_state_persist_failed", sequence_id=self._sequence_id, reason=reason,
error_type=type(exc).__name__,
)
raise
_oauth_trace(
- "nous_state_persisted",
- sequence_id=self._sequence_id,
- reason=reason,
+ "nous_state_persisted", sequence_id=self._sequence_id, reason=reason,
refresh_token_fp=_token_fingerprint(state.get("refresh_token")),
access_token_fp=_token_fingerprint(state.get("access_token")),
)
@@ -1170,10 +941,9 @@ def _nous_effective_routing(state: Dict[str, Any]) -> tuple[str, str, str, str]:
or DEFAULT_NOUS_PORTAL_URL
).rstrip("/")
- # A persisted/stale portal_base_url is where the refresh token gets
- # POSTed on refresh — reject any host outside the allowlist so a
- # poisoned value can't exfiltrate the bearer, healing to the default.
- # Trusted operator env overrides bypass this network-value gate.
+ # A persisted/stale portal_base_url is where the refresh token gets POSTed — reject any host
+ # outside the allowlist so a poisoned value can't exfiltrate the bearer, healing to the
+ # default. Trusted operator env overrides bypass this network-value gate.
env_portal_override = _nous_portal_env_override()
if env_portal_override:
portal_url = env_portal_override.rstrip("/")
@@ -1181,20 +951,14 @@ def _nous_effective_routing(state: Dict[str, Any]) -> tuple[str, str, str, str]:
parsed_portal_url = urlparse(portal_url)
portal_host = parsed_portal_url.hostname
loopback_http = (
- parsed_portal_url.scheme == "http"
- and portal_host in {"localhost", "127.0.0.1"}
+ parsed_portal_url.scheme == "http" and portal_host in {"localhost", "127.0.0.1"}
)
trusted_scheme = parsed_portal_url.scheme == "https" or loopback_http
- if (
- not portal_host
- or portal_host not in _NOUS_PORTAL_ALLOWED_HOSTS
- or not trusted_scheme
- ):
+ if not portal_host or portal_host not in _NOUS_PORTAL_ALLOWED_HOSTS or not trusted_scheme:
logger.warning(
"auth: ignoring invalid portal_base_url %r "
"(host %r or scheme not allowed), using default",
- portal_url,
- portal_host,
+ portal_url, portal_host,
)
portal_url = DEFAULT_NOUS_PORTAL_URL
@@ -1205,242 +969,199 @@ def _nous_effective_routing(state: Dict[str, Any]) -> tuple[str, str, str, str]:
or DEFAULT_NOUS_INFERENCE_URL
)
return (
- portal_url,
- stored_inference_url,
- _nous_inference_env_override() or stored_inference_url,
+ portal_url, stored_inference_url, _nous_inference_env_override() or stored_inference_url,
str(state.get("client_id") or DEFAULT_NOUS_CLIENT_ID),
)
-def resolve_nous_runtime_credentials(
- *,
- timeout_seconds: float = 15.0,
- insecure: Optional[bool] = None,
- ca_bundle: Optional[str] = None,
- force_refresh: bool = False,
- stale_access_token: Optional[str] = None,
-) -> Dict[str, Any]:
- """Resolve Nous inference credentials for runtime use.
+class _NousRuntimeResolve:
+ """Mutable working set for one ``resolve_nous_runtime_credentials`` call.
- Ensures access_token is a valid inference-scoped JWT, refreshing it when
- needed. Concurrent processes coordinate through the auth store file lock.
-
- ``stale_access_token`` is the bearer that just failed upstream (401). When
- set together with ``force_refresh``, the refresh POST is skipped if the
- store — re-read under the lock — already holds a *different*, usable
- token: another process won the rotation, so this caller adopts it instead
- of rotating the shared grant again (otherwise N concurrent processes at the
- same expiry issue N refreshes, each invalidating a sibling's fresh token).
+ Holds the token pair and routing tuple that shared-store merges and refreshes replace
+ mid-flight, so the phase helpers below can update them in place.
"""
- from hermes_cli.auth import _assert_nous_inference_jwt_usable, _auth_file_path, _coerce_ttl_seconds, _nous_invoke_jwt_status, _parse_iso_timestamp, _provider_state_transaction, _resolve_verify, _select_nous_invoke_jwt, _sync_nous_pool_from_auth_store, _tls_state_from_verify
- sequence_id = uuid.uuid4().hex[:12]
- with _provider_state_transaction("nous") as (
- auth_store,
- state,
- state_source_path,
- ):
+ def __init__(
+ self, auth_store: Dict[str, Any], state: Dict[str, Any], state_source_path: Optional[Path],
+ *, force_refresh: bool, stale_access_token: Optional[str], timeout_seconds: float,
+ ) -> None:
+ self.auth_store, self.state = auth_store, state
+ self.force_refresh, self.stale_access_token = force_refresh, stale_access_token
+ self.timeout_seconds = timeout_seconds
+ self.sequence_id = uuid.uuid4().hex[:12]
+ self.persister = _NousStatePersister(auth_store, state, state_source_path, self.sequence_id)
+ self.access_token = state.get("access_token")
+ self.refresh_token = state.get("refresh_token")
+ self._reload_routing()
+
+ def _reload_routing(self) -> None:
+ (
+ self.portal_base_url, self.stored_inference_base_url, self.inference_base_url,
+ self.client_id,
+ ) = _nous_effective_routing(self.state)
- if not state:
- raise _nous_err("Hermes is not logged into Nous Portal.", relogin=True)
+ def persist(self, reason: str) -> None:
+ self.persister.persist(reason)
+
+ def shared_lock(self):
+ return _nous_shared_store_lock(timeout_seconds=_shared_lock_timeout(self.timeout_seconds))
+
+ def has_access_token(self) -> bool:
+ return isinstance(self.access_token, str) and bool(self.access_token)
+
+ def invoke_jwt_status(self) -> Optional[str]:
+ return _state_invoke_jwt_status(self.state, self.access_token)
+
+ def merge_shared(self) -> bool:
+ """Adopt fresher shared-store tokens (caller holds the shared lock). True when merged."""
+ if not _merge_shared_nous_oauth_state(self.state):
+ return False
+ self.access_token = self.state.get("access_token")
+ self.refresh_token = self.state.get("refresh_token")
+ self._reload_routing()
+ return True
+
+ def skip_refresh_if_peer_rotated(self) -> None:
+ """Skip the refresh when a peer already rotated the grant.
+
+ Under the store lock: if the bearer that failed upstream is no longer the one on disk and
+ the on-disk one is usable, adopt it — never re-POST the shared grant.
+ """
+ token = self.access_token
+ rotated = bool(
+ self.force_refresh and self.stale_access_token and isinstance(token, str) and token
+ and token != self.stale_access_token and self.invoke_jwt_status() is None
+ )
+ if rotated:
+ _oauth_trace(
+ "refresh_skipped_peer_rotated", sequence_id=self.sequence_id,
+ access_token_fp=_token_fingerprint(token),
+ )
+ self.force_refresh = False
- def _already_rotated_by_peer(token: Any) -> bool:
- return bool(
- force_refresh
- and stale_access_token
- and isinstance(token, str)
- and token
- and token != stale_access_token
- and _nous_invoke_jwt_status(
- token,
- scope=state.get("scope"),
- expires_at=state.get("expires_at"),
- ) is None
+ def refresh(self, client: httpx.Client, invoke_jwt_status: Optional[str]) -> None:
+ """Redeem the refresh token, apply + persist the rotated pair (caller holds both locks)."""
+ if not isinstance(self.refresh_token, str) or not self.refresh_token:
+ raise _unusable_invoke_jwt_error(
+ invoke_jwt_status or "force_refresh", no_refresh_token=True,
)
+ refresh_reason = (
+ "force_refresh" if self.force_refresh else (invoke_jwt_status or "access_unusable")
+ )
+ _oauth_trace(
+ "refresh_start", sequence_id=self.sequence_id, reason=refresh_reason,
+ refresh_token_fp=_token_fingerprint(self.refresh_token),
+ )
+ refreshed = _refresh_nous_or_quarantine(
+ client=client, auth_store=self.auth_store, state=self.state,
+ portal_base_url=self.portal_base_url, client_id=self.client_id,
+ refresh_token=self.refresh_token, reason="runtime_access_refresh_failure",
+ persist=lambda: self.persist("terminal_runtime_access_refresh_failure"),
+ )
+ previous_refresh_token = self.refresh_token
+ # The validated, network-provenance URL is what gets persisted (with the rotated tokens,
+ # so a later JWT validation failure cannot leave the stores on stale metadata). The
+ # NOUS_INFERENCE_BASE_URL env override is layered on for the client/return value only.
+ self.stored_inference_base_url = _healed_nous_inference_url(refreshed)
+ self.inference_base_url = _nous_inference_env_override() or self.stored_inference_base_url
+ _apply_nous_refreshed_tokens(
+ self.state, refreshed, self.refresh_token,
+ inference_base_url=self.stored_inference_base_url,
+ )
+ self.access_token = self.state["access_token"]
+ self.refresh_token = self.state["refresh_token"]
+ _oauth_trace(
+ "refresh_success", sequence_id=self.sequence_id, reason=refresh_reason,
+ previous_refresh_token_fp=_token_fingerprint(previous_refresh_token),
+ new_refresh_token_fp=_token_fingerprint(self.refresh_token),
+ )
+ # Persist immediately so validation failures cannot drop rotated refresh tokens.
+ self.persist("post_refresh_access_token")
+
+ def ensure_usable_access_token(self, client: httpx.Client) -> None:
+ """Merge from the shared store / refresh until the access token is a usable invoke JWT."""
+ if not self.has_access_token():
+ with self.shared_lock():
+ if self.merge_shared():
+ self.persist("runtime_shared_merge_missing_access_token")
+ if not self.has_access_token():
+ raise _nous_err("No access token found for Nous Portal login.", relogin=True)
+
+ invoke_jwt_status = self.invoke_jwt_status()
+ self.skip_refresh_if_peer_rotated()
+ if not (self.force_refresh or invoke_jwt_status is not None):
+ return
+ with self.shared_lock():
+ if self.merge_shared():
+ invoke_jwt_status = self.invoke_jwt_status()
+ self.persist("post_shared_merge_access_unusable")
+ self.skip_refresh_if_peer_rotated()
+ if self.force_refresh or invoke_jwt_status is not None:
+ self.refresh(client, invoke_jwt_status)
- persister = _NousStatePersister(auth_store, state, state_source_path, sequence_id)
- _persist_state = persister.persist
- (
- portal_base_url,
- stored_inference_base_url,
- inference_base_url,
- client_id,
- ) = _nous_effective_routing(state)
+def resolve_nous_runtime_credentials(
+ *, timeout_seconds: float = 15.0, insecure: Optional[bool] = None,
+ ca_bundle: Optional[str] = None, force_refresh: bool = False,
+ stale_access_token: Optional[str] = None,
+) -> Dict[str, Any]:
+ """Resolve Nous inference credentials for runtime use.
+
+ Ensures access_token is a valid inference-scoped JWT, refreshing it when needed. Concurrent
+ processes coordinate through the auth store file lock.
+ ``stale_access_token`` is the bearer that just failed upstream (401). With ``force_refresh``,
+ the refresh POST is skipped if the store — re-read under the lock — already holds a
+ *different*, usable token: another process won the rotation, so this caller adopts it instead
+ of rotating the shared grant again (otherwise N concurrent processes at the same expiry issue N
+ refreshes, each invalidating a sibling's fresh token).
+ """
+ from hermes_cli.auth import (
+ _assert_nous_inference_jwt_usable, _auth_file_path, _provider_state_transaction,
+ _resolve_verify, _select_nous_invoke_jwt, _sync_nous_pool_from_auth_store,
+ _tls_state_from_verify,
+ )
+ with _provider_state_transaction("nous") as (auth_store, state, state_source_path):
+ if not state:
+ raise _nous_err("Hermes is not logged into Nous Portal.", relogin=True)
+ run = _NousRuntimeResolve(
+ auth_store, state, state_source_path, force_refresh=force_refresh,
+ stale_access_token=stale_access_token, timeout_seconds=timeout_seconds,
+ )
verify = _resolve_verify(insecure=insecure, ca_bundle=ca_bundle, auth_state=state)
- timeout = httpx.Timeout(timeout_seconds if timeout_seconds else 15.0)
_oauth_trace(
- "nous_runtime_credentials_start",
- sequence_id=sequence_id,
+ "nous_runtime_credentials_start", sequence_id=run.sequence_id,
refresh_token_fp=_token_fingerprint(state.get("refresh_token")),
)
-
- with httpx.Client(timeout=timeout, headers={"Accept": "application/json"}, verify=verify) as client:
- access_token = state.get("access_token")
- refresh_token = state.get("refresh_token")
-
- if not isinstance(access_token, str) or not access_token:
- with _nous_shared_store_lock(
- timeout_seconds=max(timeout_seconds + 5.0, AUTH_LOCK_TIMEOUT_SECONDS)
- ):
- if _merge_shared_nous_oauth_state(state):
- access_token = state.get("access_token")
- refresh_token = state.get("refresh_token")
- (
- portal_base_url,
- stored_inference_base_url,
- inference_base_url,
- client_id,
- ) = _nous_effective_routing(state)
- _persist_state("runtime_shared_merge_missing_access_token")
-
- if not isinstance(access_token, str) or not access_token:
- raise _nous_err(
- "No access token found for Nous Portal login.",
- relogin=True,
- )
-
- invoke_jwt_status = _nous_invoke_jwt_status(
- access_token,
- scope=state.get("scope"),
- expires_at=state.get("expires_at"),
- )
- # Under the store lock: if the bearer that failed upstream is no
- # longer the one on disk and the on-disk one is usable, a peer
- # already rotated — adopt, never re-POST the shared grant.
- if _already_rotated_by_peer(access_token):
- _oauth_trace(
- "refresh_skipped_peer_rotated",
- sequence_id=sequence_id,
- access_token_fp=_token_fingerprint(access_token),
- )
- force_refresh = False
- if force_refresh or invoke_jwt_status is not None:
- with _nous_shared_store_lock(timeout_seconds=max(timeout_seconds + 5.0, AUTH_LOCK_TIMEOUT_SECONDS)):
- if _merge_shared_nous_oauth_state(state):
- access_token = state.get("access_token")
- refresh_token = state.get("refresh_token")
- (
- portal_base_url,
- stored_inference_base_url,
- inference_base_url,
- client_id,
- ) = _nous_effective_routing(state)
- invoke_jwt_status = _nous_invoke_jwt_status(
- access_token,
- scope=state.get("scope"),
- expires_at=state.get("expires_at"),
- )
- _persist_state("post_shared_merge_access_unusable")
- if _already_rotated_by_peer(access_token):
- _oauth_trace(
- "refresh_skipped_peer_rotated",
- sequence_id=sequence_id,
- access_token_fp=_token_fingerprint(access_token),
- )
- force_refresh = False
-
- if force_refresh or invoke_jwt_status is not None:
- if not isinstance(refresh_token, str) or not refresh_token:
- reason = invoke_jwt_status or "force_refresh"
- raise _nous_err(
- "Nous Portal access token is not a usable inference JWT "
- f"({reason}) and no refresh token is available. "
- "Re-authenticate with: hermes auth add nous",
- reason, relogin=True,
- )
-
- refresh_reason = "force_refresh" if force_refresh else (invoke_jwt_status or "access_unusable")
- _oauth_trace(
- "refresh_start",
- sequence_id=sequence_id,
- reason=refresh_reason,
- refresh_token_fp=_token_fingerprint(refresh_token),
- )
- refreshed = _refresh_nous_or_quarantine(
- client=client,
- auth_store=auth_store,
- state=state,
- portal_base_url=portal_base_url,
- client_id=client_id,
- refresh_token=refresh_token,
- reason="runtime_access_refresh_failure",
- persist=lambda: _persist_state("terminal_runtime_access_refresh_failure"),
- )
- previous_refresh_token = refresh_token
- # The validated, network-provenance URL is what gets persisted to
- # auth.json (with the rotated tokens, so a later JWT validation
- # failure cannot leave the stores on stale metadata). The
- # NOUS_INFERENCE_BASE_URL env override is layered on for the
- # client/return value only — it is never persisted.
- stored_inference_base_url = _healed_nous_inference_url(refreshed)
- inference_base_url = (
- _nous_inference_env_override() or stored_inference_base_url
- )
- _apply_nous_refreshed_tokens(
- state, refreshed, refresh_token,
- inference_base_url=stored_inference_base_url,
- )
- access_token = state["access_token"]
- refresh_token = state["refresh_token"]
- _oauth_trace(
- "refresh_success",
- sequence_id=sequence_id,
- reason=refresh_reason,
- previous_refresh_token_fp=_token_fingerprint(previous_refresh_token),
- new_refresh_token_fp=_token_fingerprint(refresh_token),
- )
- # Persist immediately so validation failures cannot drop rotated refresh tokens.
- _persist_state("post_refresh_access_token")
-
- _assert_nous_inference_jwt_usable(
- state,
- access_token=access_token,
- )
+ with _nous_http_client(timeout_seconds or 15.0, verify) as client:
+ run.ensure_usable_access_token(client)
+ _assert_nous_inference_jwt_usable(state, access_token=run.access_token)
_select_nous_invoke_jwt(
- state,
- access_token=access_token,
- sequence_id=sequence_id,
+ state, access_token=run.access_token, sequence_id=run.sequence_id,
)
-
- # Persist routing and TLS metadata for non-interactive refresh.
- # Persist the validated, network-provenance URL — NEVER the env
- # override (which is a runtime-only overlay; persisting it would
- # leak a dev/staging host into auth.json and survive unsetting it).
- state["portal_base_url"] = portal_base_url
- state["inference_base_url"] = stored_inference_base_url
- state["client_id"] = client_id
+ # Persist routing and TLS metadata for non-interactive refresh — the validated,
+ # network-provenance URL, NEVER the env override (a runtime-only overlay; persisting
+ # it would leak a dev/staging host into auth.json and survive unsetting it).
+ state["portal_base_url"] = run.portal_base_url
+ state["inference_base_url"] = run.stored_inference_base_url
+ state["client_id"] = run.client_id
state["tls"] = _tls_state_from_verify(verify)
+ run.persist("resolve_nous_runtime_credentials_final")
- _persist_state("resolve_nous_runtime_credentials_final")
-
- if persister.persisted_any:
+ if run.persister.persisted_any:
_sync_nous_pool_from_auth_store()
api_key = state.get("agent_key")
if not isinstance(api_key, str) or not api_key:
raise _nous_err("Failed to resolve a Nous inference API key", "server_error")
-
expires_at = state.get("agent_key_expires_at")
- expires_epoch = _parse_iso_timestamp(expires_at)
- expires_in = (
- max(0, int(expires_epoch - time.time()))
- if expires_epoch is not None
- else _coerce_ttl_seconds(state.get("agent_key_expires_in"))
- )
-
return {
- "provider": "nous",
- "base_url": inference_base_url,
- "api_key": api_key,
- "key_id": state.get("agent_key_id"),
- "expires_at": expires_at,
- "expires_in": expires_in,
+ "provider": "nous", "base_url": run.inference_base_url, "api_key": api_key,
+ "key_id": state.get("agent_key_id"), "expires_at": expires_at,
+ "expires_in": _remaining_ttl(expires_at, state.get("agent_key_expires_in")),
"source": NOUS_AUTH_PATH_INVOKE_JWT,
- # Preserve the public semantic source label while exposing the concrete
- # store separately for diagnostics. Refresh persistence uses
- # state_source_path internally and must not overload this field.
+ # Public semantic source label; the concrete store is exposed separately for diagnostics.
+ # Refresh persistence uses state_source_path internally and must not overload this field.
"auth_path": NOUS_AUTH_PATH_INVOKE_JWT,
"state_path": str(state_source_path or _auth_file_path()),
}
@@ -1448,31 +1169,24 @@ def resolve_nous_runtime_credentials(
def _empty_nous_auth_status() -> Dict[str, Any]:
return {
- "logged_in": False,
- "portal_base_url": None,
- "inference_base_url": None,
- "access_expires_at": None,
- "agent_key_expires_at": None,
- "has_refresh_token": False,
- "inference_credential_present": False,
- "credential_source": None,
+ "logged_in": False, "portal_base_url": None, "inference_base_url": None,
+ "access_expires_at": None, "agent_key_expires_at": None, "has_refresh_token": False,
+ "inference_credential_present": False, "credential_source": None,
}
def _snapshot_nous_pool_status() -> Dict[str, Any]:
"""Best-effort status from the credential pool.
- This is a fallback only. The auth-store provider state is the runtime source of truth because it
- is what ``resolve_nous_runtime_credentials()`` refreshes.
+ Fallback only: the auth-store provider state is the runtime source of truth because it is what
+ ``resolve_nous_runtime_credentials()`` refreshes.
"""
from hermes_cli.auth import _parse_iso_timestamp
try:
from agent.credential_pool import load_pool
-
pool = load_pool("nous")
if not pool or not pool.has_credentials():
return _empty_nous_auth_status()
-
entries = list(pool.entries())
if not entries:
return _empty_nous_auth_status()
@@ -1480,12 +1194,10 @@ def _snapshot_nous_pool_status() -> Dict[str, Any]:
def _entry_sort_key(entry: Any) -> tuple[float, float, int]:
agent_exp = _parse_iso_timestamp(getattr(entry, "agent_key_expires_at", None)) or 0.0
access_exp = _parse_iso_timestamp(getattr(entry, "expires_at", None)) or 0.0
- priority = int(getattr(entry, "priority", 0) or 0)
- return (agent_exp, access_exp, -priority)
+ return (agent_exp, access_exp, -int(getattr(entry, "priority", 0) or 0))
entry = max(entries, key=_entry_sort_key)
- runtime_key = getattr(entry, "runtime_api_key", None)
- if not runtime_key:
+ if not getattr(entry, "runtime_api_key", None):
return _empty_nous_auth_status()
access_token = getattr(entry, "access_token", None)
auth_type = str(getattr(entry, "auth_type", "") or "").strip().lower()
@@ -1494,13 +1206,10 @@ def _snapshot_nous_pool_status() -> Dict[str, Any]:
auth_type.startswith("oauth") or bool(refresh_token)
)
label = getattr(entry, "label", "unknown")
- portal_status_url = None
- if is_portal_oauth:
- portal_status_url = (
- getattr(entry, "portal_base_url", None)
- or DEFAULT_NOUS_PORTAL_URL
- )
-
+ portal_status_url = (
+ (getattr(entry, "portal_base_url", None) or DEFAULT_NOUS_PORTAL_URL) if is_portal_oauth
+ else None
+ )
return {
"logged_in": is_portal_oauth,
"portal_base_url": portal_status_url,
@@ -1519,7 +1228,9 @@ def _snapshot_nous_pool_status() -> Dict[str, Any]:
return _empty_nous_auth_status()
-def _nous_status_from_state(state: Dict[str, Any], *, logged_in: bool, source: str) -> Dict[str, Any]:
+def _nous_status_from_state(
+ state: Dict[str, Any], *, logged_in: bool, source: str,
+) -> Dict[str, Any]:
"""Auth-store-backed Nous status snapshot (shared by the live and refresh-free variants)."""
access_token = state.get("access_token")
return {
@@ -1540,157 +1251,121 @@ def _compute_nous_auth_status() -> Dict[str, Any]:
"""Uncached implementation of get_nous_auth_status(). See that function."""
from hermes_cli.auth import get_provider_auth_state, resolve_nous_runtime_credentials
state = get_provider_auth_state("nous")
- if state:
- base_status = _nous_status_from_state(
- state, logged_in=bool(state.get("access_token")), source="auth_store",
- )
- try:
- creds = resolve_nous_runtime_credentials()
- refreshed_state = get_provider_auth_state("nous") or state
- base_status.update(
- {
- "logged_in": True,
- "portal_base_url": refreshed_state.get("portal_base_url") or base_status.get("portal_base_url"),
- "inference_base_url": creds.get("base_url")
- or refreshed_state.get("inference_base_url")
- or base_status.get("inference_base_url"),
- "access_expires_at": refreshed_state.get("expires_at") or base_status.get("access_expires_at"),
- "agent_key_expires_at": creds.get("expires_at")
- or refreshed_state.get("agent_key_expires_at")
- or base_status.get("agent_key_expires_at"),
- "has_refresh_token": bool(refreshed_state.get("refresh_token")),
- "inference_credential_present": True,
- "credential_source": "auth_store",
- "source": f"runtime:{creds.get('source', 'portal')}",
- "key_id": creds.get("key_id"),
- }
- )
- return base_status
- except AuthError as exc:
- base_status.update({
- "logged_in": False,
- "error": str(exc),
- "relogin_required": bool(getattr(exc, "relogin_required", False)),
- "error_code": getattr(exc, "code", None),
- })
- return base_status
+ if not state:
+ return _snapshot_nous_pool_status()
+ base_status = _nous_status_from_state(
+ state, logged_in=bool(state.get("access_token")), source="auth_store",
+ )
+ try:
+ creds = resolve_nous_runtime_credentials()
+ refreshed_state = get_provider_auth_state("nous") or state
+ base_status.update({
+ "logged_in": True,
+ "portal_base_url": refreshed_state.get("portal_base_url")
+ or base_status.get("portal_base_url"),
+ "inference_base_url": creds.get("base_url")
+ or refreshed_state.get("inference_base_url")
+ or base_status.get("inference_base_url"),
+ "access_expires_at": refreshed_state.get("expires_at")
+ or base_status.get("access_expires_at"),
+ "agent_key_expires_at": creds.get("expires_at")
+ or refreshed_state.get("agent_key_expires_at")
+ or base_status.get("agent_key_expires_at"),
+ "has_refresh_token": bool(refreshed_state.get("refresh_token")),
+ "inference_credential_present": True,
+ "credential_source": "auth_store",
+ "source": f"runtime:{creds.get('source', 'portal')}",
+ "key_id": creds.get("key_id"),
+ })
+ except AuthError as exc:
+ base_status.update({
+ "logged_in": False, "error": str(exc),
+ "relogin_required": bool(getattr(exc, "relogin_required", False)),
+ "error_code": getattr(exc, "code", None),
+ })
+ return base_status
+
- return _snapshot_nous_pool_status()
+def _terminal_quarantine_marker(state: Dict[str, Any]) -> Optional[Dict[str, Any]]:
+ """The persisted ``last_auth_error`` when it is a terminal quarantine with no credential left.
+
+ Only terminal while there is no usable credential: if a later login repopulated tokens the stale
+ marker must not keep reporting terminal.
+ """
+ last_err = state.get("last_auth_error")
+ if (
+ isinstance(last_err, dict) and last_err.get("relogin_required")
+ and not (state.get("access_token") or state.get("refresh_token"))
+ ):
+ return last_err
+ return None
def get_nous_auth_status_local() -> Dict[str, Any]:
"""Refresh-free Nous auth snapshot for read-only display surfaces.
- Unlike :func:`get_nous_auth_status`, this NEVER calls ``resolve_nous_runtime_credentials()`` and
- therefore never performs an OAuth refresh POST or consumes a single-use refresh token. It
- reports the persisted auth-store state, classifying the access token with a local invoke-JWT
- decode only.
-
- ``logged_in`` here means "a persisted login exists that the runtime can use or refresh": a
- currently-usable invoke JWT, or a refresh token that has not been terminally quarantined. It
- does not prove the refresh token is still accepted server-side — only a live resolve can do
- that.
+ Unlike :func:`get_nous_auth_status`, this NEVER calls ``resolve_nous_runtime_credentials()``
+ and so never performs an OAuth refresh POST or consumes a single-use refresh token; it
+ classifies the persisted access token with a local invoke-JWT decode only. ``logged_in`` means
+ "a persisted login exists that the runtime can use or refresh" (a usable invoke JWT, or a
+ refresh token not terminally quarantined) — not that the server still accepts the refresh token.
"""
- from hermes_cli.auth import _nous_invoke_jwt_status, get_provider_auth_state
+ from hermes_cli.auth import get_provider_auth_state
try:
state = get_provider_auth_state("nous")
except Exception:
state = None
-
if not state:
return _snapshot_nous_pool_status()
-
- access_token = state.get("access_token")
- jwt_reason = _nous_invoke_jwt_status(
- access_token,
- scope=state.get("scope"),
- expires_at=state.get("expires_at"),
- )
- last_err = state.get("last_auth_error")
- terminal = bool(
- isinstance(last_err, dict)
- and last_err.get("relogin_required")
- and not (access_token or state.get("refresh_token"))
- )
- logged_in = (jwt_reason is None) or (
- bool(state.get("refresh_token")) and not terminal
- )
-
+ jwt_reason = _state_invoke_jwt_status(state, state.get("access_token"))
+ last_err = _terminal_quarantine_marker(state)
+ logged_in = (jwt_reason is None) or (bool(state.get("refresh_token")) and last_err is None)
status = _nous_status_from_state(state, logged_in=logged_in, source="auth_store_local")
- if terminal and isinstance(last_err, dict):
+ if last_err is not None:
status["relogin_required"] = True
status["error_code"] = last_err.get("code")
status["error"] = last_err.get("message") or "re-login required"
return status
-# Enum values reported on the dashboard /api/status as ``nous_session_valid``.
-# NAS's health sweep re-mints the bootstrap session ONLY on "terminal"; "valid"
-# and "unknown" are no-ops. Keep this set small and stable — NAS parses it with
-# a permissive schema, so new members are non-breaking but should stay rare.
+# Enum values reported on the dashboard /api/status as ``nous_session_valid``. NAS's health sweep
+# re-mints the bootstrap session ONLY on "terminal"; "valid" and "unknown" are no-ops. Keep this
+# set small and stable — NAS parses it permissively, so new members are non-breaking but rare.
NOUS_SESSION_VALID = "valid"
-
-
NOUS_SESSION_TERMINAL = "terminal"
-
-
NOUS_SESSION_UNKNOWN = "unknown"
def get_nous_session_validity() -> str:
"""Classify the Nous bootstrap session for the dashboard /api/status probe.
- Determinable with NO working token — it reads local auth-store state only, which is exactly the
- condition a dead hosted box is in. This function is called by the frequently-polled public
- ``/api/status`` endpoint, so it must never resolve credentials or perform an OAuth refresh.
-
- ANTI-FLAP CONTRACT: only a *terminal* failure maps to "terminal". A normal mid-rotation blip, a
- transient network error, or a merely-expiring token must NOT report "terminal" (that would
- trigger a spurious NAS re-mint on a healthy box).
+ Reads local auth-store state only (exactly what a dead hosted box still has) and is called by
+ the frequently-polled public ``/api/status`` endpoint, so it must never resolve credentials or
+ refresh. ANTI-FLAP CONTRACT: only a *terminal* failure maps to "terminal" — a mid-rotation
+ blip, transient network error, or merely-expiring token must NOT (that would trigger a
+ spurious NAS re-mint on a healthy box).
"""
- from hermes_cli.auth import _nous_invoke_jwt_status, get_provider_auth_state
- # A persisted quarantine marker is the strongest, most stable terminal
- # signal: the refresh path writes `last_auth_error.relogin_required=True`
- # into the Nous provider state when it clears dead tokens (the exact path
- # that produced the incident's "No access token found"). Read it directly
- # so we report "terminal" even after the in-memory AuthError is long gone.
+ from hermes_cli.auth import get_provider_auth_state
try:
state = get_provider_auth_state("nous")
except Exception:
return NOUS_SESSION_UNKNOWN
-
if not state:
return NOUS_SESSION_UNKNOWN
-
- last_err = state.get("last_auth_error")
- # Only terminal while there is no usable credential left. If a later
- # successful login repopulated tokens, the stale marker must not
- # keep reporting terminal.
- if (
- isinstance(last_err, dict)
- and last_err.get("relogin_required")
- and not (state.get("access_token") or state.get("refresh_token"))
- ):
+ # The persisted quarantine marker (`last_auth_error.relogin_required=True`, written when the
+ # refresh path clears dead tokens) is the strongest, most stable terminal signal — report
+ # "terminal" even after the in-memory AuthError is long gone.
+ if _terminal_quarantine_marker(state) is not None:
return NOUS_SESSION_TERMINAL
-
- if _nous_invoke_jwt_status(
- state.get("access_token"),
- scope=state.get("scope"),
- expires_at=state.get("expires_at"),
- ) is None:
+ if _state_invoke_jwt_status(state, state.get("access_token")) is None:
return NOUS_SESSION_VALID
-
- # Missing, malformed, expired, or merely expiring credentials are not proof
- # of a terminal session. Runtime inference/keepalive paths own refreshes;
- # the health endpoint remains side-effect free and reports indeterminate.
+ # Missing, malformed, expired, or merely expiring credentials are not proof of a terminal
+ # session. Runtime paths own refreshes; the health endpoint stays side-effect free.
return NOUS_SESSION_UNKNOWN
def _pool_first_oauth_status(
- provider_id: str,
- *,
- is_expiring: Callable[[str, int], bool],
- auth_mode: str,
+ provider_id: str, *, is_expiring: Callable[[str, int], bool], auth_mode: str,
resolve: Callable[[], Dict[str, Any]],
on_pool_miss: Optional[Callable[[], Optional[Dict[str, Any]]]] = None,
) -> Dict[str, Any]:
@@ -1703,23 +1378,19 @@ def _pool_first_oauth_status(
from hermes_cli.auth import _auth_file_path
try:
from agent.credential_pool import load_pool
-
pool = load_pool(provider_id)
if pool and pool.has_credentials():
entry = pool.select()
if entry is not None:
api_key = (
- getattr(entry, "runtime_api_key", None)
- or getattr(entry, "access_token", "")
+ getattr(entry, "runtime_api_key", None) or getattr(entry, "access_token", "")
)
if api_key and not is_expiring(api_key, 0):
return {
- "logged_in": True,
- "auth_store": str(_auth_file_path()),
+ "logged_in": True, "auth_store": str(_auth_file_path()),
"last_refresh": getattr(entry, "last_refresh", None),
"auth_mode": auth_mode,
- "source": f"pool:{getattr(entry, 'label', 'unknown')}",
- "api_key": api_key,
+ "source": f"pool:{getattr(entry, 'label', 'unknown')}", "api_key": api_key,
}
if on_pool_miss is not None:
degraded = on_pool_miss()
@@ -1727,56 +1398,41 @@ def _pool_first_oauth_status(
return degraded
except Exception:
pass
-
try:
creds = resolve()
return {
- "logged_in": True,
- "auth_store": str(_auth_file_path()),
+ "logged_in": True, "auth_store": str(_auth_file_path()),
"last_refresh": creds.get("last_refresh"),
- "auth_mode": creds.get("auth_mode"),
- "source": creds.get("source"),
+ "auth_mode": creds.get("auth_mode"), "source": creds.get("source"),
"api_key": creds.get("api_key"),
}
except AuthError as exc:
- return {
- "logged_in": False,
- "auth_store": str(_auth_file_path()),
- "error": str(exc),
- }
+ return {"logged_in": False, "auth_store": str(_auth_file_path()), "error": str(exc)}
def _nous_device_code_login(
- *,
- portal_base_url: Optional[str] = None,
- inference_base_url: Optional[str] = None,
- client_id: Optional[str] = None,
- scope: Optional[str] = None,
- open_browser: bool = True,
- timeout_seconds: float = 15.0,
- insecure: bool = False,
- ca_bundle: Optional[str] = None,
+ *, portal_base_url: Optional[str] = None, inference_base_url: Optional[str] = None,
+ client_id: Optional[str] = None, scope: Optional[str] = None, open_browser: bool = True,
+ timeout_seconds: float = 15.0, insecure: bool = False, ca_bundle: Optional[str] = None,
on_verification: Optional[Callable[[str, str], None]] = None,
) -> Dict[str, Any]:
"""Run the Nous device-code flow and return full OAuth state without persisting."""
- from hermes_cli.auth import PROVIDER_REGISTRY, _coerce_ttl_seconds, _is_remote_session, _optional_base_url, _poll_for_token, _print_device_code_instructions, _request_device_code, _tls_state_from_verify, format_auth_error, refresh_nous_oauth_from_state
+ from hermes_cli.auth import (
+ PROVIDER_REGISTRY, _coerce_ttl_seconds, _is_remote_session, _optional_base_url,
+ _poll_for_token, _print_device_code_instructions, _request_device_code,
+ _tls_state_from_verify, format_auth_error, refresh_nous_oauth_from_state,
+ )
pconfig = PROVIDER_REGISTRY["nous"]
portal_base_url = (
- portal_base_url
- or os.getenv("HERMES_PORTAL_BASE_URL")
- or os.getenv("NOUS_PORTAL_BASE_URL")
+ portal_base_url or os.getenv("HERMES_PORTAL_BASE_URL") or os.getenv("NOUS_PORTAL_BASE_URL")
or pconfig.portal_base_url
).rstrip("/")
requested_inference_url = (
- inference_base_url
- or os.getenv("NOUS_INFERENCE_BASE_URL")
- or pconfig.inference_base_url
+ inference_base_url or os.getenv("NOUS_INFERENCE_BASE_URL") or pconfig.inference_base_url
).rstrip("/")
client_id = client_id or pconfig.client_id
scope = scope or pconfig.scope
- timeout = httpx.Timeout(timeout_seconds)
verify: bool | str = False if insecure else (ca_bundle if ca_bundle else True)
-
if _is_remote_session():
open_browser = False
@@ -1787,55 +1443,40 @@ def _nous_device_code_login(
elif ca_bundle:
print(f"TLS verification: custom CA bundle ({ca_bundle})")
- with httpx.Client(timeout=timeout, headers={"Accept": "application/json"}, verify=verify) as client:
+ with _nous_http_client(timeout_seconds, verify) as client:
device_data = _request_device_code(
- client=client,
- portal_base_url=portal_base_url,
- client_id=client_id,
- scope=scope,
+ client=client, portal_base_url=portal_base_url, client_id=client_id, scope=scope,
)
-
verification_url = str(device_data["verification_uri_complete"])
user_code = str(device_data["user_code"])
expires_in = int(device_data["expires_in"])
interval = int(device_data["interval"])
-
_print_device_code_instructions(
verification_url, user_code, open_browser=open_browser, failure_dash="—",
)
-
- # Surface the verification URL/code to an out-of-band consumer (e.g. the
- # TUI gateway, whose stdout is a JSON-RPC pipe — a plain print() there is
- # dropped). Fired AFTER the print/browser block and BEFORE polling blocks,
- # so the consumer can render the link while we wait. Best-effort.
+ # Surface the URL/code to an out-of-band consumer (e.g. the TUI gateway, whose stdout is
+ # a JSON-RPC pipe where print() is dropped). Fired AFTER the print/browser block and
+ # BEFORE polling blocks so the consumer can render the link while we wait. Best-effort.
if on_verification is not None:
try:
on_verification(verification_url, user_code)
except Exception:
pass
-
effective_interval = max(1, min(interval, DEVICE_AUTH_POLL_INTERVAL_CAP_SECONDS))
print(f"Waiting for approval (polling every {effective_interval}s)...")
-
token_data = _poll_for_token(
- client=client,
- portal_base_url=portal_base_url,
- client_id=client_id,
- device_code=str(device_data["device_code"]),
- expires_in=expires_in,
+ client=client, portal_base_url=portal_base_url, client_id=client_id,
+ device_code=str(device_data["device_code"]), expires_in=expires_in,
poll_interval=interval,
)
now = datetime.now(timezone.utc)
token_expires_in = _coerce_ttl_seconds(token_data.get("expires_in", 0))
- expires_at = now.timestamp() + token_expires_in
resolved_inference_url = (
- _optional_base_url(token_data.get("inference_base_url"))
- or requested_inference_url
+ _optional_base_url(token_data.get("inference_base_url")) or requested_inference_url
)
if resolved_inference_url != requested_inference_url:
print(f"Using portal-provided inference URL: {resolved_inference_url}")
-
auth_state = {
"portal_base_url": portal_base_url,
"inference_base_url": resolved_inference_url,
@@ -1845,25 +1486,20 @@ def _nous_device_code_login(
"access_token": token_data["access_token"],
"refresh_token": token_data.get("refresh_token"),
"obtained_at": now.isoformat(),
- "expires_at": datetime.fromtimestamp(expires_at, tz=timezone.utc).isoformat(),
+ "expires_at": _iso_after(now, token_expires_in),
"expires_in": token_expires_in,
"tls": _tls_state_from_verify(verify),
**_NOUS_EMPTY_AGENT_KEY_FIELDS,
}
try:
return refresh_nous_oauth_from_state(
- auth_state,
- timeout_seconds=timeout_seconds,
- force_refresh=False,
+ auth_state, timeout_seconds=timeout_seconds, force_refresh=False,
)
except AuthError as exc:
if exc.code == "subscription_required":
- portal_url = auth_state.get(
- "portal_base_url", DEFAULT_NOUS_PORTAL_URL
- ).rstrip("/")
- message = format_auth_error(exc)
+ portal_url = auth_state.get("portal_base_url", DEFAULT_NOUS_PORTAL_URL).rstrip("/")
print()
- print(message)
+ print(format_auth_error(exc))
print(f" Subscribe here: {portal_url}/billing")
print()
print("After subscribing, run `hermes model` again to finish setup.")
@@ -1885,24 +1521,24 @@ def _mirror_nous_state_best_effort(auth_state: Dict[str, Any]) -> None:
def step_up_nous_billing_scope(
- *,
- open_browser: bool = True,
- timeout_seconds: float = 15.0,
+ *, open_browser: bool = True, timeout_seconds: float = 15.0,
on_verification: Optional[Callable[[str, str], None]] = None,
) -> bool:
"""Re-run the device flow requesting ``billing:manage`` and persist the result.
Lazy step-up triggered by ``403 insufficient_scope``. The user must be ADMIN/OWNER and select
- "Allow Remote Spending" in the portal, otherwise the server silently downscopes and this returns
- False. Reuses the held credential's portal/inference URLs + client_id so the step-up targets the
- same deployment, and persists like ``_login_nous`` but WITHOUT the model picker.
+ "Allow Remote Spending" in the portal, otherwise the server silently downscopes and this
+ returns False. Reuses the held credential's portal/inference URLs + client_id so the step-up
+ targets the same deployment, and persists like ``_login_nous`` but WITHOUT the model picker.
"""
- from hermes_cli.auth import PROVIDER_REGISTRY, _nous_device_code_login, _save_active_provider_state, get_provider_auth_state
+ from hermes_cli.auth import (
+ PROVIDER_REGISTRY, _nous_device_code_login, _save_active_provider_state,
+ get_provider_auth_state,
+ )
prior = get_provider_auth_state("nous") or {}
pconfig = PROVIDER_REGISTRY["nous"]
-
- # Build the step-up scope: existing scopes (if any) + billing:manage, deduped,
- # order-stable. Fall back to the standard inference+tool+billing set.
+ # Step-up scope: existing scopes (if any) + billing:manage, deduped, order-stable. Falls back
+ # to the standard inference+tool+billing set.
_raw_scope = prior.get("scope")
prior_scope = _raw_scope if isinstance(_raw_scope, str) else ""
requested: list[str] = []
@@ -1911,26 +1547,22 @@ def step_up_nous_billing_scope(
requested.append(tok)
if NOUS_BILLING_MANAGE_SCOPE not in requested:
requested.append(NOUS_BILLING_MANAGE_SCOPE)
- scope = " ".join(requested)
auth_state = _nous_device_code_login(
portal_base_url=prior.get("portal_base_url") or None,
inference_base_url=prior.get("inference_base_url") or None,
- client_id=prior.get("client_id") or pconfig.client_id,
- scope=scope,
- open_browser=open_browser,
- timeout_seconds=timeout_seconds,
- on_verification=on_verification,
+ client_id=prior.get("client_id") or pconfig.client_id, scope=" ".join(requested),
+ open_browser=open_browser, timeout_seconds=timeout_seconds, on_verification=on_verification,
)
-
_save_active_provider_state("nous", auth_state)
_mirror_nous_state_best_effort(auth_state)
-
granted = auth_state.get("scope")
return isinstance(granted, str) and NOUS_BILLING_MANAGE_SCOPE in granted.split()
-def _pick_nous_model_after_login(auth_state: Dict[str, Any], inference_base_url: str) -> Optional[str]:
+def _pick_nous_model_after_login(
+ auth_state: Dict[str, Any], inference_base_url: str,
+) -> Optional[str]:
"""Fetch the curated Nous model list (tier/policy-filtered) and run the interactive picker.
Returns the selected model id, or None when the user skipped / nothing was selectable.
@@ -1949,74 +1581,52 @@ def _pick_nous_model_after_login(auth_state: Dict[str, Any], inference_base_url:
union_with_portal_paid_recommendations,
)
model_ids = get_curated_nous_model_ids()
+ _portal = auth_state.get("portal_base_url", "")
print()
unavailable_models: list = []
unavailable_message = ""
+ _policy_narrowed = False
if model_ids:
pricing = get_pricing_for_provider("nous")
- # Force fresh account data for model selection so recent credit
- # purchases are reflected immediately.
+ # Force fresh account data so recent credit purchases are reflected immediately.
free_tier = check_nous_free_tier(force_fresh=True)
- _portal_for_recs = auth_state.get("portal_base_url", "")
- # Narrow before the tier split, so a rescued id still has to
- # pass the free/paid predicate.
+ # Narrow before the tier split, so a rescued id still has to pass the free/paid predicate.
_policy_allowed = nous_policy_allowed_ids()
- _policy_narrowed = False
if free_tier:
try:
- from hermes_cli.nous_account import (
- format_nous_portal_entitlement_message,
- get_nous_portal_account_info,
- )
-
- _account_info = get_nous_portal_account_info(force_fresh=True)
- unavailable_message = (
- format_nous_portal_entitlement_message(
- _account_info,
- capability="paid Nous models",
- )
- or ""
- )
+ unavailable_message = _portal_entitlement_message("paid Nous models")
except Exception:
unavailable_message = ""
- # The Portal's free/paidRecommendedModels endpoint is the source of
- # truth for what's available *right now*. Augment the curated list with
- # anything new the Portal flags so users on older Hermes builds still
+ # The Portal's free/paidRecommendedModels endpoint is the source of truth for what's
+ # available *right now*: augment the curated list so users on older Hermes builds still
# see newly-launched models without a CLI release.
union = (
- union_with_portal_free_recommendations
- if free_tier
+ union_with_portal_free_recommendations if free_tier
else union_with_portal_paid_recommendations
)
- model_ids, pricing = union(model_ids, pricing, _portal_for_recs)
+ model_ids, pricing = union(model_ids, pricing, _portal)
_before_policy = model_ids
- model_ids = restrict_to_nous_policy(
- model_ids, _policy_allowed, rescue_empty=True,
- )
+ model_ids = restrict_to_nous_policy(model_ids, _policy_allowed, rescue_empty=True)
_policy_narrowed = model_ids != _before_policy
if free_tier:
model_ids, unavailable_models = partition_nous_models_by_tier(
model_ids, pricing, free_tier=True,
)
- _portal = auth_state.get("portal_base_url", "")
if model_ids:
from hermes_cli.nous_account import nous_policy_notice
-
_policy_notice = nous_policy_notice(removed=_policy_narrowed)
if _policy_notice:
print(_policy_notice)
- print(f"Showing {len(model_ids)} curated models — use \"Enter custom model name\" for others.")
+ print(
+ f"Showing {len(model_ids)} curated models — use \"Enter custom model name\" for others."
+ )
return _prompt_model_selection(
- model_ids, pricing=pricing,
- unavailable_models=unavailable_models,
- portal_url=_portal,
- unavailable_message=unavailable_message,
- confirm_provider="nous",
- confirm_base_url=inference_base_url,
- confirm_api_key=runtime_key,
+ model_ids, pricing=pricing, unavailable_models=unavailable_models, portal_url=_portal,
+ unavailable_message=unavailable_message, confirm_provider="nous",
+ confirm_base_url=inference_base_url, confirm_api_key=runtime_key,
)
- elif unavailable_models:
+ if unavailable_models:
_url = (_portal or DEFAULT_NOUS_PORTAL_URL).rstrip("/")
print("No free models currently available.")
print(unavailable_message or f"Upgrade at {_url} to access paid models.")
@@ -2053,17 +1663,30 @@ def _offer_shared_nous_import(timeout_seconds: float) -> Optional[Dict[str, Any]
return auth_state
+def _restore_active_provider(prior_active_provider: Any) -> None:
+ """Undo the ``active_provider="nous"`` that ``_save_provider_state`` wrote during login."""
+ from hermes_cli.auth import _auth_store_lock, _load_auth_store, _save_auth_store
+ with _auth_store_lock():
+ auth_store = _load_auth_store()
+ if prior_active_provider:
+ auth_store["active_provider"] = prior_active_provider
+ else:
+ auth_store.pop("active_provider", None)
+ _save_auth_store(auth_store)
+
+
def _login_nous(args, pconfig: ProviderConfig) -> None:
"""Nous Portal device authorization flow."""
- from hermes_cli.auth import _auth_store_lock, _load_auth_store, _nous_device_code_login, _save_active_provider_state, _save_auth_store, _save_model_choice, _sync_nous_pool_from_auth_store, _update_config_for_provider, _write_shared_nous_state, format_auth_error
+ from hermes_cli.auth import (
+ _auth_store_lock, _load_auth_store, _nous_device_code_login, _save_active_provider_state,
+ _save_model_choice, _sync_nous_pool_from_auth_store, _update_config_for_provider,
+ _write_shared_nous_state, format_auth_error,
+ )
timeout_seconds = getattr(args, "timeout", None) or 15.0
- insecure = bool(getattr(args, "insecure", False))
ca_bundle = (
- getattr(args, "ca_bundle", None)
- or os.getenv("HERMES_CA_BUNDLE")
+ getattr(args, "ca_bundle", None) or os.getenv("HERMES_CA_BUNDLE")
or os.getenv("SSL_CERT_FILE")
)
-
try:
auth_state = _offer_shared_nous_import(timeout_seconds)
if auth_state is None:
@@ -2073,26 +1696,17 @@ def _login_nous(args, pconfig: ProviderConfig) -> None:
client_id=getattr(args, "client_id", None) or pconfig.client_id,
scope=getattr(args, "scope", None),
open_browser=not getattr(args, "no_browser", False),
- timeout_seconds=timeout_seconds,
- insecure=insecure,
+ timeout_seconds=timeout_seconds, insecure=bool(getattr(args, "insecure", False)),
ca_bundle=ca_bundle,
)
-
inference_base_url = auth_state["inference_base_url"]
- # Snapshot the prior active_provider BEFORE _save_provider_state
- # overwrites it to "nous". If the user picks "Skip (keep current)"
- # during model selection below, we restore this so the user's previous
- # provider (e.g. openrouter) is preserved.
+ # Snapshot the prior active_provider BEFORE _save_provider_state overwrites it to "nous",
+ # so "Skip (keep current)" in the model picker can restore the user's previous provider.
with _auth_store_lock():
- _prior_store = _load_auth_store()
- prior_active_provider = _prior_store.get("active_provider")
-
+ prior_active_provider = _load_auth_store().get("active_provider")
saved_to = _save_active_provider_state("nous", auth_state)
-
- # Mirror to the shared store so other profiles can one-tap import
- # these credentials. Best-effort: any I/O failure is logged and
- # swallowed inside the helper.
+ # Mirror to the shared store so other profiles can one-tap import (best-effort inside).
_write_shared_nous_state(auth_state)
_sync_nous_pool_from_auth_store()
@@ -2100,10 +1714,8 @@ def _login_nous(args, pconfig: ProviderConfig) -> None:
print("Login successful!")
print(f" Auth state: {saved_to}")
- # Resolve model BEFORE writing provider to config.yaml so we never
- # leave the config in a half-updated state (provider=nous but model
- # still set to the previous provider's model, e.g. opus from
- # OpenRouter). The auth.json active_provider was already set above.
+ # Resolve the model BEFORE writing the provider to config.yaml so config is never
+ # half-updated (provider=nous but model still the previous provider's).
selected_model = None
try:
selected_model = _pick_nous_model_after_login(auth_state, inference_base_url)
@@ -2112,23 +1724,11 @@ def _login_nous(args, pconfig: ProviderConfig) -> None:
print()
print(f"Login succeeded, but could not fetch available models. Reason: {message}")
- # Write provider + model atomically so config is never mismatched.
- # If no model was selected (user picked "Skip (keep current)",
- # model list fetch failed, or no curated models were available),
- # preserve the user's previous provider — don't silently switch
- # them to Nous with a mismatched model. The Nous OAuth tokens
- # stay saved for future use.
+ # No model selected (Skip, fetch failed, or nothing curated): preserve the previous
+ # provider rather than silently switching to Nous with a mismatched model. config.yaml
+ # model.provider is left untouched; the Nous OAuth tokens stay saved for future use.
if not selected_model:
- # Restore the prior active_provider that _save_provider_state
- # overwrote to "nous". config.yaml model.provider is left
- # untouched, so the user's previous provider is fully preserved.
- with _auth_store_lock():
- auth_store = _load_auth_store()
- if prior_active_provider:
- auth_store["active_provider"] = prior_active_provider
- else:
- auth_store.pop("active_provider", None)
- _save_auth_store(auth_store)
+ _restore_active_provider(prior_active_provider)
print()
print("No provider change. Nous credentials saved for future use.")
print(" Run `hermes model` again to switch to Nous Portal.")
@@ -2141,7 +1741,6 @@ def _login_nous(args, pconfig: ProviderConfig) -> None:
_save_model_choice(selected_model)
print(f"Default model set to: {selected_model}")
print(f" Config updated: {config_path} (model.provider=nous)")
-
except KeyboardInterrupt:
print("\nLogin cancelled.")
raise SystemExit(130)