Skip to content

The agent that grows with you

fix(tui-gateway): a stale session_id no longer turns config.set fast/yolo global

John Paul Solivaba5bec3f2c9a
diff --git a/tests/tui_gateway/test_fast_session_scope.py b/tests/tui_gateway/test_fast_session_scope.py
index c46b41a1ea0..81ab30182d4 100644
--- a/tests/tui_gateway/test_fast_session_scope.py
+++ b/tests/tui_gateway/test_fast_session_scope.py
@@ -13,7 +13,8 @@ Contract under test:
1. ``config.set key=fast`` with a session must NOT write config.yaml; it pins
``create_service_tier_override`` ("priority" / "" for explicit normal) so
lazily-built sessions and rebuilds keep the choice.
-2. Without a session it persists globally, unchanged.
+2. Without a session it persists globally, unchanged. A session_id the
+ backend no longer holds is refused with 4001, not treated as "no session".
3. ``config.get key=fast`` must read a pre-build session's pin.
"""
@@ -107,6 +108,15 @@ class TestConfigSetFastSessionScope:
assert resp["result"]["value"] == "normal"
write_key.assert_called_once_with("agent.service_tier", "normal")
+ def test_stale_session_id_is_refused_not_persisted_globally(self) -> None:
+ """A runtime id the backend no longer holds (reaped / re-minted) is not "no session": it
+ must 4001 so the client resumes, not rewrite the profile's tier for every surface."""
+ with patch.dict(server._sessions, {}, clear=True), \
+ patch.object(server, "_write_config_key") as write_key:
+ resp = _set({"key": "fast", "session_id": "reaped-sid", "value": "normal"})
+ assert resp.get("error", {}).get("code") == 4001, resp
+ write_key.assert_not_called()
+
class TestConfigGetFastSessionScope:
def test_reads_prebuild_pin(self) -> None:
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 5cc7c044d0a..b61c9d0dbd5 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -8203,6 +8203,24 @@ def test_config_set_yolo_toggles_session_scope():
server._sessions.clear()
+def test_config_set_yolo_stale_session_id_is_refused_not_process_scoped(monkeypatch):
+ """A runtime id the backend no longer holds must answer 4001 so the client resumes, not flip
+ the process HERMES_YOLO_MODE that every child spawned afterwards inherits."""
+ monkeypatch.setenv("HERMES_YOLO_MODE", "0") # setenv, not delenv: undo must also drop a leaked "1"
+
+ with patch.dict(server._sessions, {}, clear=True):
+ resp = server.handle_request(
+ {
+ "id": "1",
+ "method": "config.set",
+ "params": {"session_id": "reaped-sid", "key": "yolo", "value": "1"},
+ }
+ )
+
+ assert resp.get("error", {}).get("code") == 4001, resp
+ assert os.environ["HERMES_YOLO_MODE"] == "0"
+
+
def test_config_set_yolo_global_scope_writes_approvals_mode(tmp_path, monkeypatch):
"""Shift+click the desktop zap -> scope="global" flips persistent approvals.mode."""
import yaml
diff --git a/tui_gateway/methods_config_set.py b/tui_gateway/methods_config_set.py
index 8034a869df6..be03c00edb5 100644
--- a/tui_gateway/methods_config_set.py
+++ b/tui_gateway/methods_config_set.py
@@ -162,6 +162,10 @@ _FAST_WORDS = {"fast": "fast", "on": "fast", "normal": "normal", "off": "normal"
def _set_fast(rid, params, key, value, session):
+ if session is None and params.get("session_id"):
+ # A runtime id this backend no longer holds (reaped / re-minted) must not reach the sessionless
+ # branch: it persists agent.service_tier for every surface. 4001 lets the client resume it.
+ return _sess_nowait(params, rid)[1]
raw = _word(value)
agent = session.get("agent") if session else None
if agent is not None:
@@ -278,6 +282,10 @@ def _set_yolo(rid, params, key, value, session):
enable = _BOOL_WORDS.get(raw, not is_session_yolo_enabled(skey))
(enable_session_yolo if enable else disable_session_yolo)(skey)
_emit_session_info(params.get("session_id", ""), session)
+ elif params.get("session_id"):
+ # Stale runtime id: the process flag below never reaches this session's approvals, yet every
+ # child spawned afterwards (compute host, a terminal's `hermes`) inherits the flip.
+ return _sess_nowait(params, rid)[1]
else:
enable = _BOOL_WORDS.get(raw, not is_truthy_value(os.environ.get("HERMES_YOLO_MODE")))
if enable: