Skip to content

The agent that grows with you

fix(gateway): a native launch-profile turn keeps its credentials across the switch

Teknium35fba4c84327
diff --git a/gateway/session_ingress.py b/gateway/session_ingress.py
index 992c2a90895..c4ce71d9636 100644
--- a/gateway/session_ingress.py
+++ b/gateway/session_ingress.py
@@ -335,6 +335,19 @@ async def execute_admission(authority, ref, row):
viewer_turn.reset(viewer_token)
+def _launch_body_if_launch_owner(authority):
+ """A standalone owner's turn binds nothing, but the launch profile's is marked at entry: a second
+ profile switching this gateway to multiplexing mid-turn must not fail its later credential reads
+ closed. Any other owner (a hosted named profile) stays unmarked, never reading the launch env."""
+ from pathlib import Path
+ from agent.secret_scope import _is_process_home
+ owner = Path(str(authority.profile_id))
+ if not owner.is_absolute() or not _is_process_home(owner):
+ return nullcontext()
+ from tui_gateway.launch_profile_policy import launch_body
+ return launch_body()
+
+
async def _execution_scope(authority, home):
"""Under multiplex, owner-side execution runs under the OWNING profile's home (agent build,
config, secrets, state.db), never the launch profile's ambient scope; native provenance
@@ -342,7 +355,7 @@ async def _execution_scope(authority, home):
Secret sources (external CLIs) hydrate off the loop, as main's per-message scope does (#99519)."""
multiplex = getattr(getattr(authority.runner, 'config', None), 'multiplex_profiles', False)
if home is None and not multiplex:
- return nullcontext()
+ return _launch_body_if_launch_owner(authority)
from gateway.run import _load_profile_secret_scope, _profile_runtime_scope
def load():
diff --git a/tests/gateway/test_single_profile_flip_keeps_launch_work.py b/tests/gateway/test_single_profile_flip_keeps_launch_work.py
index a14fa33dec4..0859f208399 100644
--- a/tests/gateway/test_single_profile_flip_keeps_launch_work.py
+++ b/tests/gateway/test_single_profile_flip_keeps_launch_work.py
@@ -188,3 +188,61 @@ async def test_activation_rechecks_shutdown_after_the_preflight(standalone_host,
_mkprofile(home, "daily-news-bot")
await runner.reconcile_served_profiles(reason="control-socket")
assert not is_multiplex_active(), "stop() began during the preflight, yet fail-closed secrets were armed"
+
+
+async def _native_turn_across_activation(tmp_path, profile_home):
+ """Drive a Desktop/TUI ``prompt.submit`` through ``AuthorityConnection.dispatch`` on a standalone
+ owner and switch the process to multi-profile hosting while the turn is running."""
+ from gateway.config import GatewayConfig, Platform
+ from gateway.session import SessionSource, SessionStore
+ from gateway.session_authority import LiveSession, initialize_session_authority
+ from gateway.session_controls import AuthorityConnection
+ from tui_gateway.launch_profile_policy import activate_multi_profile_hosting
+
+ started, release, seen = asyncio.Event(), asyncio.Event(), []
+
+ async def answer(event):
+ seen.append(_read())
+ started.set()
+ await release.wait()
+ seen.append(_read())
+ return "ok"
+
+ store = SessionStore(tmp_path / "sessions", GatewayConfig())
+ runner = types.SimpleNamespace(_session_db=store._db, session_store=store, _draining=False,
+ config=GatewayConfig(multiplex_profiles=False),
+ _handle_message=answer, _adapter_for_source=lambda source: None)
+ authority = await initialize_session_authority(runner, profile_id=str(profile_home), instance_id="owner")
+ store._db.create_session("s", source="telegram")
+ authority.sessions["s"] = LiveSession(SessionSource(platform=Platform.TELEGRAM, chat_id="c"), "s")
+ connection = AuthorityConnection(authority, object(), {"user_id": "owner"})
+ try:
+ await connection.dispatch({"id": 1, "method": "session.resume", "params": {"session_id": "s"}})
+ reply = await connection.dispatch({"id": 2, "method": "prompt.submit", "params": {
+ "session_id": "s", "submission_id": "x", "text": "hi"}})
+ assert reply.get("result", {}).get("status") == "queued", reply
+ await asyncio.wait_for(started.wait(), 10)
+ activate_multi_profile_hosting() # a second profile switched the gateway mid-turn
+ release.set()
+ await authority.sessions["s"].task
+ finally:
+ await connection.close()
+ return seen
+
+
+async def test_native_launch_turn_in_flight_across_activation_keeps_its_credentials(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setenv("OPENROUTER_API_KEY", "launch-env-key")
+ seen = await _native_turn_across_activation(tmp_path, tmp_path)
+ assert seen == ["launch-env-key", "launch-env-key"], seen
+
+
+async def test_native_named_profile_turn_never_reads_the_launch_env(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setenv("OPENROUTER_API_KEY", "launch-env-key")
+ bot = tmp_path / "profiles" / "bot"
+ bot.mkdir(parents=True)
+ seen = await _native_turn_across_activation(tmp_path, bot)
+ assert seen[1] == "UnscopedSecretError", seen