Skip to content

The agent that grows with you

fix(gateway): standalone gateway binds the launch profile's own scope after hosted activation

teknium151c4b6ba9dbb
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.py
index 9f422d07143..ca506335d79 100644
--- a/gateway/run_adapters.py
+++ b/gateway/run_adapters.py
@@ -1438,14 +1438,26 @@ class GatewayAdapterLifecycleMixin:
def _primary_message_handler(self):
"""Return the correctly scoped handler for a primary adapter."""
- return self._make_default_profile_message_handler() if self._multiplex_on() else self._handle_message
+ if self._multiplex_on():
+ return self._make_default_profile_message_handler()
+ return self._standalone_scoped(self._handle_message)
def _primary_busy_session_handler(self):
"""Return the correctly scoped busy-session handler for a primary adapter."""
- return (
- self._make_default_profile_busy_session_handler()
- if self._multiplex_on() else self._handle_active_session_busy_message
- )
+ if self._multiplex_on():
+ return self._make_default_profile_busy_session_handler()
+ return self._standalone_scoped(self._handle_active_session_busy_message)
+
+ def _standalone_scoped(self, handler):
+ """Standalone twin of the ``_make_default_profile_*`` wrappers: run ``handler`` under
+ ``_standalone_launch_scope`` so slash commands and turns keep resolving the launch profile's
+ credentials after a hosted room flipped the process-wide guard (#112878). Decided per event:
+ activation happens after the adapters were wired."""
+ async def _handler(*args):
+ with self._standalone_launch_scope():
+ return await handler(*args)
+
+ return _handler
def _multiplex_on(self) -> bool:
return bool(getattr(self.config, "multiplex_profiles", False))
@@ -1486,7 +1498,7 @@ class GatewayAdapterLifecycleMixin:
def _primary_platform_event_handler(self):
if self._multiplex_on():
return self._make_default_profile_platform_event_handler()
- return self._handle_gateway_platform_event
+ return self._standalone_scoped(self._handle_gateway_platform_event)
@staticmethod
def _adapter_credential_claim(platform: Platform, adapter: Any) -> Optional[tuple]:
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 910582a00b6..1490ecf7561 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -2166,17 +2166,32 @@ class GatewayTurnMixin:
"""``_profile_runtime_scope`` for ``source``'s profile when a secret scope is required.
Under multiplexing config/skills/memory resolve to the source profile's home AND credentials
- come from its secret scope (never process-global ``os.environ``). Hosted-room activity can
- activate the same process-wide credential guard for a standalone gateway, which must bind
- its default profile rather than leave a later credential read unscoped."""
- from agent.secret_scope import is_multiplex_active
+ come from its secret scope (never process-global ``os.environ``). A standalone gateway
+ (``multiplex_profiles`` off) still binds once a hosted room has flipped the process-wide
+ credential guard — see ``_standalone_launch_scope``."""
from gateway.run import _profile_runtime_scope
- if (
- getattr(getattr(self, "config", None), "multiplex_profiles", False)
- or is_multiplex_active()
- ):
+ if getattr(getattr(self, "config", None), "multiplex_profiles", False):
return _profile_runtime_scope(self._resolve_profile_home_for_source(source))
- return nullcontext()
+ return self._standalone_launch_scope()
+
+ @staticmethod
+ def _standalone_launch_scope():
+ """Scope for a standalone gateway's own (launch-profile) work: a no-op until the process hosts
+ another profile home, then the launch profile's OWN runtime scope.
+
+ A native hosted room running a second profile calls
+ ``tui_gateway.launch_profile_policy.activate_multi_profile_hosting`` inside the gateway process,
+ so ``get_secret`` fails closed for every unscoped read afterwards — including the standalone
+ gateway's ordinary turns, which never bound a scope because ``multiplex_profiles`` is off
+ (#112878). The launch profile is a profile too: bind its ``.env`` over the env frozen at
+ activation (a key injected by systemd / ``op run`` has no file to rebuild it from), never a
+ secondary's scope and never live ``os.environ``."""
+ from agent.secret_scope import is_multiplex_active
+ if not is_multiplex_active():
+ return nullcontext()
+ from hermes_constants import get_process_hermes_home
+ from tui_gateway.launch_profile_policy import launch_profile_runtime_scope
+ return launch_profile_runtime_scope(get_process_hermes_home())
def _media_delivery_scope_for_source(self, source: SessionSource):
"""Home + terminal-policy scope for validating a turn's MEDIA / local-file paths on the
diff --git a/tests/gateway/test_multiplex_background_task_scope.py b/tests/gateway/test_multiplex_background_task_scope.py
index 3e32361a007..bf5c70e7241 100644
--- a/tests/gateway/test_multiplex_background_task_scope.py
+++ b/tests/gateway/test_multiplex_background_task_scope.py
@@ -9,7 +9,6 @@ import asyncio
from pathlib import Path
from unittest import mock
-from agent import secret_scope
from gateway.config import GatewayConfig
from gateway.run import GatewayRunner
@@ -48,26 +47,3 @@ class TestBackgroundTaskProfileScope:
inner.assert_awaited_once()
-def test_standalone_gateway_binds_default_scope_after_hosted_activation(
- tmp_path, monkeypatch
-):
- """Regression for #112878: hosted rooms can activate fail-closed scopes globally.
-
- A gateway configured without multiplexing must still bind its default profile
- when a hosted room has already activated the process-wide secret guard.
- """
- from tui_gateway import launch_profile_policy
-
- home = tmp_path / "default"
- home.mkdir()
- (home / ".env").write_text("OPENAI_API_KEY=default-key\n", encoding="utf-8")
- monkeypatch.setattr(secret_scope, "_MULTIPLEX_ACTIVE", False)
- monkeypatch.setattr(launch_profile_policy, "_snapshot", None)
-
- runner = _make_runner(multiplex=False)
- source = mock.MagicMock()
- with mock.patch.object(runner, "_resolve_profile_home_for_source", return_value=home):
- launch_profile_policy.activate_multi_profile_hosting()
- with runner._profile_scope_for_source(source):
- assert secret_scope.get_secret("OPENAI_API_KEY") == "default-key"
-
diff --git a/tests/gateway/test_standalone_gateway_launch_scope.py b/tests/gateway/test_standalone_gateway_launch_scope.py
new file mode 100644
index 00000000000..e47d5fc7c47
--- /dev/null
+++ b/tests/gateway/test_standalone_gateway_launch_scope.py
@@ -0,0 +1,72 @@
+"""A standalone gateway (``multiplex_profiles`` off) keeps resolving the launch profile's credentials
+after a native hosted room activated the process-wide secret guard (#112878).
+
+``tui_gateway.launch_profile_policy.activate_multi_profile_hosting`` runs inside the messaging
+gateway process when a hosted room serves a second profile; ``get_secret`` then fails closed for
+every unscoped read. The standalone gateway's turns and handler entry points must bind the launch
+profile's OWN scope (``.env`` over the env frozen at activation) — not skip binding because the
+config flag is off, and not rebuild from ``.env`` alone (systemd / ``op run`` injection has no file).
+"""
+import asyncio
+from contextlib import nullcontext
+from unittest import mock
+
+import pytest
+
+from agent import secret_scope
+from agent.secret_scope import UnscopedSecretError, current_secret_scope, get_secret
+from gateway.config import GatewayConfig
+from gateway.run import GatewayRunner
+from tui_gateway import launch_profile_policy
+
+INJECTED = "HOSTEDROOM_TEST_INJECTED_KEY"
+
+
+def standalone(tmp_path, monkeypatch):
+ launch = tmp_path / "launch"
+ launch.mkdir()
+ (launch / ".env").write_text("OPENAI_API_KEY=launch-dotenv-key\n", encoding="utf-8")
+ secondary = tmp_path / "profiles" / "roomie"
+ secondary.mkdir(parents=True)
+ (secondary / ".env").write_text("OPENAI_API_KEY=secondary-key\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(launch))
+ monkeypatch.setenv(INJECTED, "launch-env-injected") # systemd / `op run` style injection
+ monkeypatch.setattr(secret_scope, "_MULTIPLEX_ACTIVE", False)
+ monkeypatch.setattr(launch_profile_policy, "_snapshot", None)
+ runner = GatewayRunner.__new__(GatewayRunner)
+ runner.config = GatewayConfig(multiplex_profiles=False)
+ return runner, secondary
+
+
+def _keys():
+ return get_secret("OPENAI_API_KEY"), get_secret(INJECTED)
+
+
+def test_standalone_turn_binds_launch_profile_scope_after_hosted_activation(standalone):
+ runner, secondary = standalone
+ from tui_gateway.server import _session_profile_runtime_scope
+
+ # A native hosted room ran a second profile: real activation, real secondary scope entered and left.
+ launch_profile_policy.activate_multi_profile_hosting()
+ with _session_profile_runtime_scope({"profile_home": str(secondary)}):
+ assert get_secret("OPENAI_API_KEY") == "secondary-key"
+ assert get_secret(INJECTED) is None # the launch env never leaks into a secondary
+ assert secret_scope.is_multiplex_active()
+
+ source = mock.MagicMock(profile=None)
+ with runner._profile_scope_for_source(source):
+ assert _keys() == ("launch-dotenv-key", "launch-env-injected")
+ runner._handle_message = mock.AsyncMock(side_effect=lambda event: _keys())
+ assert asyncio.run(runner._primary_message_handler()(mock.MagicMock(source=source))) == (
+ "launch-dotenv-key", "launch-env-injected")
+ with pytest.raises(UnscopedSecretError): # the guard itself is not weakened
+ get_secret("OPENAI_API_KEY")
+
+
+def test_standalone_without_hosted_activation_stays_unscoped(standalone):
+ runner, _secondary = standalone
+ source = mock.MagicMock(profile=None)
+ assert isinstance(runner._profile_scope_for_source(source), nullcontext)
+ runner._handle_message = mock.AsyncMock(side_effect=lambda event: current_secret_scope())
+ assert asyncio.run(runner._primary_message_handler()(mock.MagicMock(source=source))) is None
diff --git a/tui_gateway/launch_profile_policy.py b/tui_gateway/launch_profile_policy.py
index a84b948ac0b..42883f33ce8 100644
--- a/tui_gateway/launch_profile_policy.py
+++ b/tui_gateway/launch_profile_policy.py
@@ -17,10 +17,11 @@ Two facts anchor this module:
from __future__ import annotations
+import contextlib
import os
import threading
from pathlib import Path
-from typing import Dict, Optional
+from typing import Dict, Iterator, Optional
_lock = threading.Lock()
_snapshot: Optional[Dict[str, str]] = None
@@ -76,3 +77,22 @@ def launch_secret_scope(launch_home: "str | Path") -> Dict[str, str]:
scope = {k: v for k, v in _launch_env().items() if not _is_global_env(k)}
scope.update(build_profile_secret_scope(Path(launch_home)))
return scope
+
+
+def launch_profile_runtime_scope(launch_home: "str | Path") -> Iterator[None]:
+ """Bind the launch profile's own runtime scope for one body: ``launch_secret_scope`` plus its
+ terminal policy over the frozen launch ``TERMINAL_*`` overlay. No HERMES_HOME override — the
+ launch home IS the process home. For hosts whose launch-profile bodies are not RPC sessions
+ (the standalone messaging gateway after a hosted room activated multiplexing, #112878)."""
+ from agent.secret_scope import reset_secret_scope, set_secret_scope
+ from tools.terminal_scope import install_profile_terminal_scope, reset_terminal_scope
+
+ home = Path(launch_home)
+ secret_token = set_secret_scope(launch_secret_scope(home))
+ terminal_token = install_profile_terminal_scope(home, env_overlay=launch_terminal_env())
+ try:
+ yield
+ finally:
+ reset_terminal_scope(terminal_token)
+ reset_secret_scope(secret_token)