Skip to content

The agent that grows with you

fix(gateway): classify relay routing stamps as process-global deployment config

Ben Barclayf8c3635d4046
diff --git a/agent/secret_scope.py b/agent/secret_scope.py
index 919fe3e27bd..86f18f52193 100644
--- a/agent/secret_scope.py
+++ b/agent/secret_scope.py
@@ -114,6 +114,23 @@ _GLOBAL_ENV_EXACT = frozenset({
# profile-scoped.
"API_SERVER_ENABLED", "API_SERVER_HOST", "API_SERVER_PORT",
"API_SERVER_CORS_ORIGINS",
+ # Relay-connector ROUTING stamps — deployment config injected into the
+ # container/process env by managed deploys (the same shape as the
+ # API_SERVER listener settings above). The scoped runner reload and the
+ # relay-exclusive sweep in gateway/config.py must keep seeing them, and
+ # every reader (gateway.config, gateway.relay.relay_url()/registration/
+ # self-provision) must resolve the SAME value — a scope-dependent split
+ # leaves the adapter registered but the platform absent from config (or
+ # vice versa). Mirrors the non-secret/secret line drawn by the terminal
+ # env blocklist (tools/environments/local.py): routing hints are global;
+ # GATEWAY_RELAY_SECRET / GATEWAY_RELAY_ID / GATEWAY_RELAY_DELIVERY_KEY
+ # and the IDP_* credentials are auth material and deliberately NOT here —
+ # they stay profile-scoped with the fail-closed multiplex guard.
+ "GATEWAY_RELAY_URL", "GATEWAY_RELAY_ENDPOINT",
+ "GATEWAY_RELAY_ALLOW_DIRECT_PLATFORMS",
+ "GATEWAY_RELAY_PLATFORMS", "GATEWAY_RELAY_BOT_IDS",
+ "GATEWAY_RELAY_ROUTE_KEYS", "GATEWAY_RELAY_INSTANCE_ID",
+ "GATEWAY_RELAY_WAKE_URL", "GATEWAY_RELAY_DISPLAY_NAME",
})
_GLOBAL_ENV_PREFIXES = (
"HERMES_KANBAN_",
diff --git a/tests/agent/test_secret_scope.py b/tests/agent/test_secret_scope.py
index f0d76fe6543..7e73f12dbca 100644
--- a/tests/agent/test_secret_scope.py
+++ b/tests/agent/test_secret_scope.py
@@ -287,3 +287,63 @@ class TestApiServerListenerGlobals:
finally:
ss.reset_secret_scope(token)
assert not ss._is_global_env("API_SERVER_KEY")
+
+
+class TestRelayRoutingStampGlobals:
+ """GATEWAY_RELAY_* ROUTING stamps are deployment config, not profile
+ secrets: config's relay enablement/sweep and gateway.relay's readers
+ (relay_url(), registration, self-provision) must resolve the same
+ process-env value under any scope, or the gateway enters a split-brain
+ state (adapter registered but Platform.RELAY absent from config, or vice
+ versa). Auth material (GATEWAY_RELAY_SECRET / _ID / _DELIVERY_KEY and the
+ IDP_* credentials) stays profile-scoped with the fail-closed guard —
+ mirroring the API_SERVER_KEY line above and the terminal env blocklist
+ (tools/environments/local.py)."""
+
+ ROUTING_VARS = (
+ "GATEWAY_RELAY_URL",
+ "GATEWAY_RELAY_ENDPOINT",
+ "GATEWAY_RELAY_ALLOW_DIRECT_PLATFORMS",
+ "GATEWAY_RELAY_PLATFORMS",
+ "GATEWAY_RELAY_BOT_IDS",
+ "GATEWAY_RELAY_ROUTE_KEYS",
+ "GATEWAY_RELAY_INSTANCE_ID",
+ "GATEWAY_RELAY_WAKE_URL",
+ "GATEWAY_RELAY_DISPLAY_NAME",
+ )
+ AUTH_VARS = (
+ "GATEWAY_RELAY_SECRET",
+ "GATEWAY_RELAY_ID",
+ "GATEWAY_RELAY_DELIVERY_KEY",
+ "GATEWAY_RELAY_IDP_CLIENT_SECRET",
+ "GATEWAY_RELAY_IDP_CLIENT_ID",
+ "GATEWAY_RELAY_IDP_TOKEN_URL",
+ )
+
+ def test_routing_stamps_read_environ_even_when_scoped_multiplex(self, monkeypatch):
+ for name in self.ROUTING_VARS:
+ monkeypatch.setenv(name, f"deploy-{name.lower()}")
+ ss.set_multiplex_active(True)
+ token = ss.set_secret_scope({"TELEGRAM_BOT_TOKEN": "scoped"})
+ try:
+ for name in self.ROUTING_VARS:
+ assert ss.get_secret(name) == f"deploy-{name.lower()}", name
+ finally:
+ ss.reset_secret_scope(token)
+ ss.set_multiplex_active(False)
+
+ def test_relay_auth_material_stays_profile_scoped(self, monkeypatch):
+ for name in self.AUTH_VARS:
+ monkeypatch.setenv(name, "cross-profile-credential")
+ ss.set_multiplex_active(True)
+ token = ss.set_secret_scope({"OTHER": "x"})
+ try:
+ for name in self.AUTH_VARS:
+ # A scoped miss must NOT borrow the (potentially
+ # cross-profile) environ value: relay auth is a credential.
+ assert ss.get_secret(name) is None, name
+ finally:
+ ss.reset_secret_scope(token)
+ ss.set_multiplex_active(False)
+ for name in self.AUTH_VARS:
+ assert not ss._is_global_env(name), name
diff --git a/tests/gateway/test_config.py b/tests/gateway/test_config.py
index a6daa8d6877..3d7834e15d8 100644
--- a/tests/gateway/test_config.py
+++ b/tests/gateway/test_config.py
@@ -772,10 +772,12 @@ class TestLoadGatewayConfig:
def test_relay_exclusive_reads_profile_scoped_env(self, tmp_path, monkeypatch):
- """Under a multiplexed profile secret scope, the relay-exclusive
- trigger and opt-out must read the PROFILE's values, not process
- globals. A process-wide GATEWAY_RELAY_URL must not disable direct
- platforms in a profile whose own .env carries no relay stamp."""
+ """GATEWAY_RELAY_URL is a process-global deployment stamp (like the
+ 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."""
from agent import secret_scope as ss
hermes_home = tmp_path / ".hermes"
@@ -790,12 +792,12 @@ class TestLoadGatewayConfig:
encoding="utf-8",
)
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
- # Process-global stamp that must be INVISIBLE inside the scope.
- monkeypatch.setenv("GATEWAY_RELAY_URL", "https://global.example/relay")
+ # Managed-deploy stamp in the process env; the profile .env has none.
+ monkeypatch.setenv("GATEWAY_RELAY_URL", "https://deploy.example/relay")
profile_dir = tmp_path / "profile-a"
profile_dir.mkdir()
- (profile_dir / ".env").write_text("", encoding="utf-8") # no relay stamp
+ (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))
@@ -805,11 +807,13 @@ class TestLoadGatewayConfig:
ss.reset_secret_scope(tok)
ss.set_multiplex_active(False)
- # No profile-scoped relay stamp -> no exclusive sweep in this profile.
- assert config.platforms[Platform.TELEGRAM].enabled is True
+ # The deploy stamp survives the profile scope: relay enabled, sweep
+ # ran — matching what register_relay_adapter() sees in os.environ.
+ assert config.platforms[Platform.RELAY].enabled is True
+ assert config.platforms[Platform.TELEGRAM].enabled is False
- # And the inverse: a profile-scoped stamp triggers the sweep even
- # though the process env carries none.
+ # A profile-only stamp does NOT activate relay: globals read only the
+ # process env, so config and the relay transport can never disagree.
monkeypatch.delenv("GATEWAY_RELAY_URL", raising=False)
(profile_dir / ".env").write_text(
"GATEWAY_RELAY_URL=https://profile.example/relay\n", encoding="utf-8"
@@ -822,8 +826,9 @@ class TestLoadGatewayConfig:
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
+ 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
def test_relay_exclusive_sweep_log_levels_and_marker_cleanup(self, tmp_path, monkeypatch, caplog):