Skip to content

The agent that grows with you

test(gateway): pin config/registration relay-URL agreement under multiplex scope

Ben Barclayd97cb8f253f9
diff --git a/tests/gateway/relay/test_relay_registration.py b/tests/gateway/relay/test_relay_registration.py
index 4aceb269c6c..819d4e80490 100644
--- a/tests/gateway/relay/test_relay_registration.py
+++ b/tests/gateway/relay/test_relay_registration.py
@@ -42,3 +42,99 @@ def test_registers_when_url_configured(monkeypatch):
assert platform_registry.is_registered("relay") is True
+
+
+class TestConfigRegistrationAgreementUnderMultiplexScope:
+ """The config loader and the relay registration path must resolve the SAME
+ GATEWAY_RELAY_URL under an active multiplex profile scope — the routing
+ stamp is process-global (agent/secret_scope.py), so neither side may see a
+ value the other doesn't. Regression for the split-brain states: adapter
+ registered with Platform.RELAY absent from config (process stamp dropped
+ by the scoped reload), and enabled RELAY config with no adapter
+ (profile-only stamp invisible to relay_url())."""
+
+ def test_process_stamp_agrees_on_both_sides_under_scope(
+ self, tmp_path, monkeypatch
+ ):
+ from agent import secret_scope as ss
+ from gateway.config import Platform, load_gateway_config
+
+ hermes_home = tmp_path / ".hermes"
+ hermes_home.mkdir()
+ (hermes_home / "config.yaml").write_text(
+ "gateway:\n"
+ " platforms:\n"
+ " telegram:\n"
+ " enabled: true\n"
+ " bot_token: '123:abc'\n",
+ encoding="utf-8",
+ )
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ monkeypatch.setenv("GATEWAY_RELAY_URL", "wss://deploy.example/relay")
+
+ profile_dir = tmp_path / "profile-a"
+ profile_dir.mkdir()
+ (profile_dir / ".env").write_text("", encoding="utf-8")
+
+ ss.set_multiplex_active(True)
+ tok = ss.set_secret_scope(ss.build_profile_secret_scope(profile_dir))
+ try:
+ config = load_gateway_config()
+ # Registration-side reader agrees with the config side.
+ assert relay_url() == "wss://deploy.example/relay"
+ assert register_relay_adapter() is True
+ finally:
+ ss.reset_secret_scope(tok)
+ ss.set_multiplex_active(False)
+
+ assert config.platforms[Platform.RELAY].enabled is True
+ assert config.platforms[Platform.TELEGRAM].enabled is False
+ # The registered factory actually constructs an adapter with a live
+ # transport dialing the same URL — the connect loop's contract.
+ adapter = platform_registry.create_adapter(
+ "relay", config.platforms[Platform.RELAY]
+ )
+ assert isinstance(adapter, RelayAdapter)
+ # A URL-configured registration builds a live transport (force=True's
+ # transport-less posture is the test-only path) — private attr, no
+ # public accessor on the adapter.
+ assert adapter._transport is not None
+
+ def test_profile_only_stamp_is_inert_on_both_sides_under_scope(
+ self, tmp_path, monkeypatch
+ ):
+ from agent import secret_scope as ss
+ from gateway.config import Platform, load_gateway_config
+
+ hermes_home = tmp_path / ".hermes"
+ hermes_home.mkdir()
+ (hermes_home / "config.yaml").write_text(
+ "gateway:\n"
+ " platforms:\n"
+ " telegram:\n"
+ " enabled: true\n"
+ " bot_token: '123:abc'\n",
+ encoding="utf-8",
+ )
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+
+ profile_dir = tmp_path / "profile-a"
+ profile_dir.mkdir()
+ (profile_dir / ".env").write_text(
+ "GATEWAY_RELAY_URL=wss://profile.example/relay\n", encoding="utf-8"
+ )
+
+ ss.set_multiplex_active(True)
+ tok = ss.set_secret_scope(ss.build_profile_secret_scope(profile_dir))
+ try:
+ config = load_gateway_config()
+ # Both sides agree the stamp is absent: no half-enabled state.
+ assert relay_url() is None
+ assert register_relay_adapter() is False
+ finally:
+ ss.reset_secret_scope(tok)
+ ss.set_multiplex_active(False)
+
+ relay_cfg = config.platforms.get(Platform.RELAY)
+ assert relay_cfg is None or relay_cfg.enabled is False
+ assert config.platforms[Platform.TELEGRAM].enabled is True
diff --git a/tests/gateway/test_config.py b/tests/gateway/test_config.py
index 3d7834e15d8..00ec63e1b53 100644
--- a/tests/gateway/test_config.py
+++ b/tests/gateway/test_config.py
@@ -776,8 +776,11 @@ class TestLoadGatewayConfig:
API_SERVER listener vars, #69379): the scoped runner reload in a
multiplexed gateway must keep seeing it, so config's relay enablement
and sweep agree with gateway.relay.relay_url()/register_relay_adapter()
- (which read os.environ). A relay URL in a profile's .env is NOT a
- supported activation path — the scope is never consulted for globals."""
+ (which read os.environ). A secondary profile's .env stamp does NOT
+ override the shared process stamp — an isolated multiplex scope is
+ never consulted for globals. (A single-profile gateway, or the profile
+ the process is launched under, still activates via its .env because
+ load_hermes_dotenv exports that file into os.environ at startup.)"""
from agent import secret_scope as ss
hermes_home = tmp_path / ".hermes"