Skip to content

The agent that grows with you

fix(tui_gateway): bind the session's profile scope around slash side-effect mirrors (#122655, salvage #122698)

ahisblessed332424ce450a
diff --git a/tests/tui_gateway/test_multi_profile_hosting_fail_closed.py b/tests/tui_gateway/test_multi_profile_hosting_fail_closed.py
index 8ae08691453..e5e16ca7379 100644
--- a/tests/tui_gateway/test_multi_profile_hosting_fail_closed.py
+++ b/tests/tui_gateway/test_multi_profile_hosting_fail_closed.py
@@ -265,6 +265,62 @@ def test_live_review_binds_runtime_scope_under_multiplex(two_homes, monkeypatch)
assert os.environ["HERMES_CODEX_BASE_URL"] == A_CODEX_URL
+def test_model_switch_mirror_binds_runtime_scope_under_multiplex(two_homes, monkeypatch):
+ """Desktop ``/model`` live sync is off-turn; the slash side-effect mirror must still resolve
+ the switch's credentials from the session's own profile (#122655). Unbound, ``switch_model``
+ reads ``get_secret`` with no scope active and the mirror reports "live session sync failed:
+ ... UnscopedSecretError" while the session stays on its previous route."""
+ from agent.secret_scope import UnscopedSecretError, get_secret
+ from hermes_constants import get_hermes_home
+
+ root, b = two_homes
+ seen = []
+
+ def fake_apply_model_switch(sid, session, arg, **kwargs):
+ # What switch_model → resolve_runtime does with get_secret for the target provider.
+ seen.append((
+ Path(get_hermes_home()),
+ get_secret("A_ONLY_TOKEN"),
+ get_secret("B_ONLY_TOKEN"),
+ get_secret("HERMES_API_KEY"),
+ ))
+ return {"warning": ""}
+
+ monkeypatch.setattr(server, "_apply_model_switch", fake_apply_model_switch)
+ monkeypatch.setattr(server, "_session_uses_compute_host", lambda value: False)
+
+ def invoke(profile_home):
+ sid = f"model-mirror-{len(seen)}"
+ session = {
+ "agent": object(),
+ "profile_home": str(profile_home) if profile_home else None,
+ "history": [],
+ "history_lock": threading.Lock(),
+ "running": False,
+ "session_key": sid,
+ }
+ server._sessions[sid] = session
+ try:
+ return server._mirror_slash_side_effects(sid, session, "/model stealth/model --provider openrouter")
+ finally:
+ server._sessions.pop(sid, None)
+
+ assert invoke(None) == "" # no warning — the switch resolved its credential
+ _probe("b") # activate multiplexing and freeze the launch profile's own secret scope
+ with pytest.raises(UnscopedSecretError):
+ get_secret("HERMES_API_KEY")
+ assert invoke(b) == ""
+ assert invoke(None) == ""
+
+ assert seen == [
+ (root, A_VAL, None, A_API_KEY),
+ (b, None, B_VAL, B_API_KEY),
+ (root, A_VAL, None, A_API_KEY),
+ ]
+ assert os.environ["HERMES_API_KEY"] == A_API_KEY
+ assert "B_ONLY_TOKEN" not in os.environ
+
+
def test_config_show_keeps_each_profiles_values_after_multiplex_activation(two_homes):
"""A→B→A config.show calls resolve the requested profile instead of running unscoped."""
root, b = two_homes
diff --git a/tui_gateway/methods_slash.py b/tui_gateway/methods_slash.py
index 38d6e01df44..33946acf0af 100644
--- a/tui_gateway/methods_slash.py
+++ b/tui_gateway/methods_slash.py
@@ -398,7 +398,10 @@ def _mirror_slash_side_effects(sid: str, session: dict, command: str) -> str:
if (mirror := _SLASH_MIRRORS.get(name)) is None:
return ""
try:
- return mirror(sid, session, agent, arg) or ""
+ # Mirrors run OFF-turn (slash.exec RPC pool / compute-host control reader): bind the session's
+ # profile scope or /model's credential read raises UnscopedSecretError under multiplex (#122655).
+ with _session_profile_runtime_scope(session):
+ return mirror(sid, session, agent, arg) or ""
except Exception as e:
if name == "compress" and agent:
from agent.conversation_compression import finalize_context_engine_compression_notification