Skip to content

The agent that grows with you

fix(cli): read multiplex topology from the default root in enroll warning

Ben Barclay6360113a8b16
diff --git a/hermes_cli/gateway_enroll.py b/hermes_cli/gateway_enroll.py
index a4ba9d0e869..9f529e625ce 100644
--- a/hermes_cli/gateway_enroll.py
+++ b/hermes_cli/gateway_enroll.py
@@ -271,11 +271,6 @@ def cmd_gateway_enroll(args) -> None:
if explicit_wake_url:
print(f" GATEWAY_RELAY_WAKE_URL={explicit_wake_url.rstrip('/')}")
print()
- print(
- " The gateway now authenticates its relay WS upgrade with the per-gateway\n"
- " secret and verifies signed inbound deliveries with the tenant delivery\n"
- " key. Restart the gateway to pick up the new env."
- )
# GATEWAY_RELAY_URL / GATEWAY_RELAY_WAKE_URL are process-global deployment
# stamps (agent/secret_scope.py): a multiplexed gateway resolves them from
# the PROCESS environment only, never from a secondary profile's .env
@@ -283,33 +278,73 @@ def cmd_gateway_enroll(args) -> None:
# write above works for a single-profile gateway and for the profile the
# process is launched under (load_hermes_dotenv exports that .env), so
# warn rather than refuse — but don't let a secondary-profile enroll claim
- # a config that will silently never activate.
+ # a config that will silently never activate. Emitted BEFORE the generic
+ # restart line so the two don't contradict each other.
+ warned_secondary = False
if explicit_url or explicit_wake_url:
- _warn_if_secondary_multiplex_profile()
+ warned_secondary = _warn_if_secondary_multiplex_profile()
+ if not warned_secondary:
+ print(
+ " The gateway now authenticates its relay WS upgrade with the per-gateway\n"
+ " secret and verifies signed inbound deliveries with the tenant delivery\n"
+ " key. Restart the gateway to pick up the new env."
+ )
+
+def _warn_if_secondary_multiplex_profile() -> bool:
+ """Warn when relay routing stamps were written to a secondary profile's
+ .env that a multiplexed gateway will never read them from. Returns True
+ when the warning fired (the caller suppresses the generic restart text).
-def _warn_if_secondary_multiplex_profile() -> None:
- """Warn when relay routing stamps were written to a profile .env that a
- multiplexed gateway will never read them from. Best-effort — any failure
- to determine the topology stays silent (the write itself succeeded)."""
+ The topology decision is owned by the DEFAULT root, not the active
+ profile home: ``multiplex_profiles`` normally lives in
+ ``<default_root>/config.yaml`` (or the GATEWAY_MULTIPLEX_PROFILES env
+ override), and the secondary check is the resolved-path relationship to
+ ``<default_root>/profiles/`` — mirroring the multiplexer-conflict guard
+ in hermes_cli/gateway.py. Best-effort: any failure to determine the
+ topology stays silent (the credential write itself succeeded).
+ """
try:
- from gateway.config import load_gateway_config
+ from hermes_constants import get_default_hermes_root
from hermes_cli.config import get_hermes_home
+ default_root = Path(get_default_hermes_root()).resolve()
home = Path(get_hermes_home()).resolve()
- is_secondary = home.parent.name == "profiles"
- if not is_secondary:
- return
- if not getattr(load_gateway_config(), "multiplex_profiles", False):
- return
- print()
+ try:
+ home.relative_to(default_root / "profiles")
+ except ValueError:
+ return False # default profile or custom layout — not a secondary
+
+ # Multiplex flag precedence mirrors gateway.config: recognized env
+ # override wins, else the DEFAULT root's config.yaml (raw read — the
+ # active profile's load_gateway_config() is the wrong owner AND runs
+ # the full enablement pass, including the relay-exclusive sweep's own
+ # log output, which has no place in enroll output).
+ from gateway.config import _env_multiplex_profiles_override
+ env_multiplex = _env_multiplex_profiles_override()
+ if env_multiplex is False:
+ return False
+ if env_multiplex is not True:
+ cfg_path = default_root / "config.yaml"
+ if not cfg_path.exists():
+ return False
+ from hermes_cli.config import read_user_config_raw
+ cfg = read_user_config_raw(cfg_path) or {}
+ if not bool(
+ cfg.get("multiplex_profiles")
+ or (cfg.get("gateway", {}) or {}).get("multiplex_profiles")
+ ):
+ return False
+
print(
" ⚠ This profile is a SECONDARY profile of a multiplexed gateway.\n"
" GATEWAY_RELAY_URL / GATEWAY_RELAY_WAKE_URL are process-level\n"
" deployment settings: the gateway reads them from the process\n"
" environment (or the default profile's .env), not from this\n"
" profile's .env. Set them in the environment the gateway process\n"
- " is launched with, or enroll from the default profile."
+ " is launched with, or enroll from the default profile. The\n"
+ " relay credentials written above are valid either way."
)
+ return True
except Exception:
- pass
+ return False
diff --git a/tests/hermes_cli/test_gateway_enroll_multiplex_warning.py b/tests/hermes_cli/test_gateway_enroll_multiplex_warning.py
new file mode 100644
index 00000000000..31a4d539b50
--- /dev/null
+++ b/tests/hermes_cli/test_gateway_enroll_multiplex_warning.py
@@ -0,0 +1,117 @@
+"""Gateway enroll: secondary-multiplex-profile warning for relay routing stamps.
+
+``hermes gateway enroll --connector-url/--wake-url`` persists GATEWAY_RELAY_URL /
+GATEWAY_RELAY_WAKE_URL into the active profile's .env. Those are process-global
+deployment stamps (agent/secret_scope.py): a multiplexed gateway reads them from
+the process environment only, never from a secondary profile's isolated secret
+scope — so an enroll run from a secondary profile must WARN that the written
+URLs will not activate from there (sol-reviewer round-4: the first cut read
+``multiplex_profiles`` from the SECONDARY profile's config.yaml, where it never
+lives, and the warning silently never fired in the real topology).
+
+The topology decision is owned by the DEFAULT root: path relationship to
+``<default_root>/profiles/`` plus the default root's config.yaml (or the
+GATEWAY_MULTIPLEX_PROFILES env override).
+"""
+
+from __future__ import annotations
+
+import io
+from contextlib import redirect_stdout
+
+import pytest
+
+import hermes_constants
+from hermes_cli.gateway_enroll import _warn_if_secondary_multiplex_profile
+
+
+def topology(tmp_path, monkeypatch):
+ """Standard multiplex layout: default root + one secondary profile."""
+ root = tmp_path / "root"
+ (root / "profiles" / "alice").mkdir(parents=True)
+ monkeypatch.setattr(hermes_constants, "get_default_hermes_root", lambda: root)
+ monkeypatch.delenv("GATEWAY_MULTIPLEX_PROFILES", raising=False)
+ return root
+
+
+def _run() -> tuple[bool, str]:
+ buf = io.StringIO()
+ with redirect_stdout(buf):
+ fired = _warn_if_secondary_multiplex_profile()
+ return fired, buf.getvalue()
+
+
+def test_fires_for_secondary_when_default_root_has_multiplex_on(topology, monkeypatch):
+ """The regression case: flag in the DEFAULT root's config.yaml, absent from
+ the secondary profile's own config — the warning must still fire."""
+ (topology / "config.yaml").write_text(
+ "gateway:\n multiplex_profiles: true\n", encoding="utf-8"
+ )
+ monkeypatch.setenv("HERMES_HOME", str(topology / "profiles" / "alice"))
+
+ fired, out = _run()
+
+ assert fired is True
+ assert "SECONDARY profile" in out
+
+
+def test_silent_when_multiplex_off_in_default_root(topology, monkeypatch):
+ (topology / "config.yaml").write_text(
+ "gateway:\n multiplex_profiles: false\n", encoding="utf-8"
+ )
+ monkeypatch.setenv("HERMES_HOME", str(topology / "profiles" / "alice"))
+
+ fired, out = _run()
+
+ assert fired is False
+ assert out == ""
+
+
+def test_silent_for_default_profile_even_with_multiplex_on(topology, monkeypatch):
+ (topology / "config.yaml").write_text(
+ "gateway:\n multiplex_profiles: true\n", encoding="utf-8"
+ )
+ monkeypatch.setenv("HERMES_HOME", str(topology))
+
+ fired, _ = _run()
+
+ assert fired is False
+
+
+def test_env_override_forces_multiplex_on_without_config_flag(topology, monkeypatch):
+ (topology / "config.yaml").write_text("{}\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(topology / "profiles" / "alice"))
+ monkeypatch.setenv("GATEWAY_MULTIPLEX_PROFILES", "true")
+
+ fired, _ = _run()
+
+ assert fired is True
+
+
+def test_env_override_off_wins_over_config_flag(topology, monkeypatch):
+ (topology / "config.yaml").write_text(
+ "gateway:\n multiplex_profiles: true\n", encoding="utf-8"
+ )
+ monkeypatch.setenv("HERMES_HOME", str(topology / "profiles" / "alice"))
+ monkeypatch.setenv("GATEWAY_MULTIPLEX_PROFILES", "false")
+
+ fired, _ = _run()
+
+ assert fired is False
+
+
+def test_silent_for_unrelated_dir_named_profiles(topology, tmp_path, monkeypatch):
+ """A directory whose parent happens to be named 'profiles' but is OUTSIDE
+ the default root is not a secondary profile (sol-reviewer round-4 MINOR:
+ name-based heuristics false-positive on unrelated layouts)."""
+ (topology / "config.yaml").write_text(
+ "gateway:\n multiplex_profiles: true\n", encoding="utf-8"
+ )
+ other = tmp_path / "elsewhere" / "profiles" / "x"
+ other.mkdir(parents=True)
+ monkeypatch.setenv("HERMES_HOME", str(other))
+
+ fired, _ = _run()
+
+ assert fired is False