Skip to content

The agent that grows with you

fix(relay): refresh the Nous login in the launch profile's scope at startup

Calvin Ng48d47cc00c31
diff --git a/gateway/relay/__init__.py b/gateway/relay/__init__.py
index cf670500a92..81e97dfd20a 100644
--- a/gateway/relay/__init__.py
+++ b/gateway/relay/__init__.py
@@ -12,6 +12,7 @@ unless the effective relay platform configuration explicitly disables it.
from __future__ import annotations
+import contextlib
import json
import logging
import os
@@ -59,6 +60,25 @@ def _gateway_cfg() -> dict:
return block if isinstance(block, dict) else {}
+def _launch_profile_scope():
+ """The launch profile's runtime scope once the gateway multiplexes, ``nullcontext`` before.
+
+ Self-provision runs at startup, before any turn binds a profile, and under multiplexing the
+ Nous Portal and inference routing overrides only resolve inside a profile scope. The scope is
+ built from the profile's files like a turn's (``gateway.run._profile_runtime_scope``), not via
+ ``launch_profile_scope_if_multiplexed``, whose first entry freezes the launch env snapshot
+ before startup has written ``GATEWAY_RELAY_*`` into ``os.environ``.
+ """
+ from agent.secret_scope import is_multiplex_active
+
+ if not is_multiplex_active():
+ return contextlib.nullcontext()
+ from gateway.run import _profile_runtime_scope # late import to avoid cycle
+ from hermes_constants import get_process_hermes_home
+
+ return _profile_runtime_scope(get_process_hermes_home())
+
+
def _env_or_cfg(env_var: str, cfg_key: str) -> str:
"""Env var first (Docker/NAS stamp), then ``gateway.<cfg_key>``; stripped, ``""`` when unset."""
value = os.environ.get(env_var, "").strip()
@@ -427,7 +447,10 @@ def _resolve_relay_identity_token() -> str:
if not token_url:
from hermes_cli.auth import resolve_nous_access_token
- return resolve_nous_access_token()
+ # Only the Nous refresh is scoped: the gateway config read above and the IdP modes below
+ # keep resolving ``${VAR}`` refs from the launch env, as they always have.
+ with _launch_profile_scope():
+ return resolve_nous_access_token()
if not client_id and not client_secret:
# Mode 1b — plain GET; the body is the token, raw or JSON-enveloped.
diff --git a/tests/gateway/relay/test_self_provision.py b/tests/gateway/relay/test_self_provision.py
index ca9499b1dc6..d413b67cfc3 100644
--- a/tests/gateway/relay/test_self_provision.py
+++ b/tests/gateway/relay/test_self_provision.py
@@ -12,11 +12,15 @@ managed, which is False on a NAS-hosted Fly agent). The real gate is
from __future__ import annotations
+import json
+import logging
import os
import pytest
+from agent import secret_scope
from gateway import relay
+from gateway.run_startup import GatewayStartupMixin
@pytest.fixture(autouse=True)
@@ -239,3 +243,111 @@ def test_relay_display_name_suppresses_stock_brand(monkeypatch):
assert relay.relay_display_name() is None
+# ───────────────── boot refresh routing on a multiplexed gateway ─────────────────
+
+
+def _boot_with_expired_login(tmp_path, monkeypatch, *, env_file: str, stored_portal: str) -> list[str]:
+ """Launch home whose Nous access token expired while the box slept, so the startup
+ self-provision is the first caller to refresh it. Returns the Portal URLs the refresh used."""
+ launch_home = tmp_path / ".hermes"
+ launch_home.mkdir()
+ (launch_home / ".env").write_text(env_file, encoding="utf-8")
+ (launch_home / "auth.json").write_text(json.dumps({
+ "active_provider": "nous",
+ "providers": {"nous": {
+ "portal_base_url": stored_portal,
+ "access_token": "expired-access-token", "refresh_token": "refresh-token",
+ "expires_at": "2000-01-01T00:00:00+00:00",
+ }},
+ }), encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(launch_home))
+ for name in ("HERMES_PORTAL_BASE_URL", "NOUS_PORTAL_BASE_URL", "NOUS_INFERENCE_BASE_URL"):
+ monkeypatch.delenv(name, raising=False)
+ monkeypatch.setattr(relay, "relay_url", lambda: "wss://connector.example/relay")
+ monkeypatch.setattr(relay, "register_relay_adapter", lambda: False)
+ portals: list[str] = []
+
+ def _refresh(**kwargs):
+ portals.append(kwargs["portal_base_url"])
+ raise RuntimeError("stop before any network call")
+
+ monkeypatch.setattr("hermes_cli.auth._refresh_nous_or_quarantine", _refresh)
+ return portals
+
+
+def test_startup_refresh_uses_launch_profile_portal_when_multiplexed(tmp_path, monkeypatch, caplog):
+ """A Portal override in the launch profile's .env survives the startup refresh."""
+ portals = _boot_with_expired_login(
+ tmp_path, monkeypatch, env_file="HERMES_PORTAL_BASE_URL=https://portal.example\n",
+ stored_portal="https://portal.example")
+ secret_scope.set_multiplex_active(True)
+
+ with caplog.at_level(logging.WARNING):
+ GatewayStartupMixin._start_register_plugins_relay_hooks()
+
+ assert portals == ["https://portal.example"]
+ assert not [r for r in caplog.records if "no profile secret scope" in r.getMessage()]
+
+
+def test_startup_refresh_without_override_stays_on_default_portal(tmp_path, monkeypatch):
+ """With no override anywhere, the startup refresh keeps the default Portal."""
+ from hermes_cli.auth_nous import DEFAULT_NOUS_PORTAL_URL
+
+ portals = _boot_with_expired_login(
+ tmp_path, monkeypatch, env_file="", stored_portal=DEFAULT_NOUS_PORTAL_URL)
+ secret_scope.set_multiplex_active(True)
+
+ GatewayStartupMixin._start_register_plugins_relay_hooks()
+
+ assert portals == [DEFAULT_NOUS_PORTAL_URL]
+
+
+def test_startup_refresh_single_profile_reads_process_env_override(tmp_path, monkeypatch):
+ """Single-profile hosts (multiplexing off) still take the override from the process env."""
+ portals = _boot_with_expired_login(
+ tmp_path, monkeypatch, env_file="", stored_portal="https://portal.example")
+ monkeypatch.setenv("HERMES_PORTAL_BASE_URL", "https://portal.example")
+
+ GatewayStartupMixin._start_register_plugins_relay_hooks()
+
+ assert portals == ["https://portal.example"]
+
+
+def test_startup_idp_secret_ref_keeps_launch_env_value_when_multiplexed(tmp_path, monkeypatch):
+ """Only the Nous token step runs in the launch profile's scope: a ``${VAR}`` IdP secret injected
+ into the launch env (systemd, ``op run``) still reaches the IdP, as it did unscoped."""
+ import io
+ from unittest.mock import Mock
+
+ launch_home = tmp_path / ".hermes"
+ launch_home.mkdir()
+ (launch_home / ".env").write_text("", encoding="utf-8")
+ (launch_home / "config.yaml").write_text(
+ "gateway:\n idp:\n token_url: https://idp.example/token\n"
+ " client_id: relay-client\n client_secret: ${RELAY_IDP_SECRET}\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(launch_home))
+ # The autouse fixture stubs config reads; this test needs the real ``${VAR}`` expansion.
+ from hermes_cli.config_effective import load_user_config_effective
+ monkeypatch.setattr("gateway.run._load_gateway_config",
+ lambda config_path=None: load_user_config_effective(launch_home / "config.yaml"))
+ monkeypatch.setenv("RELAY_IDP_SECRET", "injected-launch-secret")
+ for name in ("GATEWAY_RELAY_IDP_TOKEN_URL", "GATEWAY_RELAY_IDP_CLIENT_ID",
+ "GATEWAY_RELAY_IDP_CLIENT_SECRET", "GATEWAY_RELAY_IDP_SCOPE"):
+ monkeypatch.delenv(name, raising=False)
+ monkeypatch.setattr(relay, "relay_url", lambda: "wss://connector.example/relay")
+ monkeypatch.setattr(relay, "register_relay_adapter", lambda: False)
+ monkeypatch.setattr(relay, "_post_provision", Mock(return_value={}))
+ bodies: list[str] = []
+
+ def _idp(req, timeout=None):
+ bodies.append(req.data.decode())
+ return io.BytesIO(b'{"access_token": "idp-workload-token"}')
+
+ monkeypatch.setattr("urllib.request.urlopen", _idp)
+ secret_scope.set_multiplex_active(True)
+
+ GatewayStartupMixin._start_register_plugins_relay_hooks()
+
+ assert len(bodies) == 1
+ assert "client_secret=injected-launch-secret" in bodies[0]
+