Skip to content

The agent that grows with you

fix(tui-gateway): persist session /yolo toggles to the session row

rlaope807fa8a0a4ce
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 41572ed70bc..b2892776a59 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -8843,6 +8843,119 @@ def test_config_set_yolo_stale_session_id_is_refused_not_process_scoped(monkeypa
assert os.environ["HERMES_YOLO_MODE"] == "0"
+def test_config_set_yolo_session_scope_persists_to_the_session_row(monkeypatch):
+ """A TUI /yolo toggle writes model_config.yolo_mode like the CLI's does.
+
+ Without the persist, the toggle lives only in the in-memory
+ _session_yolo set: --resume never restores it (_restore_session_yolo
+ reads the row back) and nothing on disk reflects the flip.
+ """
+ from contextlib import contextmanager
+
+ from tools.approval import clear_session
+
+ calls = []
+
+ class _RecordingDB:
+ def set_session_yolo(self, session_id, enabled):
+ calls.append((session_id, enabled))
+
+ @contextmanager
+ def _fake_session_db(session):
+ yield _RecordingDB()
+
+ monkeypatch.setattr(server, "_session_db", _fake_session_db)
+ agent = types.SimpleNamespace(session_id="live-session-id")
+ server._sessions["sid"] = _session(agent=agent)
+ try:
+ server.handle_request(
+ {
+ "id": "1",
+ "method": "config.set",
+ "params": {"session_id": "sid", "key": "yolo"},
+ }
+ )
+ server.handle_request(
+ {
+ "id": "2",
+ "method": "config.set",
+ "params": {"session_id": "sid", "key": "yolo"},
+ }
+ )
+ # agent.session_id wins over session_key (post-compression rule).
+ assert calls == [("live-session-id", True), ("live-session-id", False)]
+ finally:
+ clear_session("session-key")
+ server._sessions.clear()
+
+
+def test_compression_rotation_carries_the_persisted_yolo_flag(monkeypatch):
+ """Rotation persists onto the continuation row, like the CLI's rotation.
+
+ The toggle-time persist writes the OLD row; without the rotation carry a
+ later `--resume <new_id>` loses the bypass even though yolo survived the
+ rotation in memory.
+ """
+ from contextlib import contextmanager
+
+ from tools.approval import clear_session, enable_session_yolo
+
+ calls = []
+
+ class _RecordingDB:
+ def set_session_yolo(self, session_id, enabled):
+ calls.append((session_id, enabled))
+
+ @contextmanager
+ def _fake_session_db(session):
+ yield _RecordingDB()
+
+ monkeypatch.setattr(server, "_session_db", _fake_session_db)
+ monkeypatch.setattr(
+ server, "_transfer_active_session_slot", lambda *args, **kwargs: True
+ )
+ agent = types.SimpleNamespace(session_id="new-continuation-id")
+ session = _session(agent=agent)
+ enable_session_yolo("session-key")
+ try:
+ server._sync_session_key_after_compress(
+ "sid", session, restart_slash_worker=False
+ )
+ assert session["session_key"] == "new-continuation-id"
+ assert calls == [("new-continuation-id", True)]
+ finally:
+ clear_session("session-key")
+ clear_session("new-continuation-id")
+
+
+def test_config_set_yolo_session_scope_survives_a_failing_db(monkeypatch):
+ """Persistence is best-effort: a broken DB must not break the toggle."""
+ from contextlib import contextmanager
+
+ from tools.approval import clear_session, is_session_yolo_enabled
+
+ @contextmanager
+ def _broken_session_db(session):
+ raise RuntimeError("db unavailable")
+ yield None
+
+ monkeypatch.setattr(server, "_session_db", _broken_session_db)
+ server._sessions["sid"] = _session()
+ try:
+ resp = server.handle_request(
+ {
+ "id": "1",
+ "method": "config.set",
+ "params": {"session_id": "sid", "key": "yolo"},
+ }
+ )
+ assert resp["result"]["value"] == "1"
+ assert is_session_yolo_enabled("session-key") is True
+ finally:
+ clear_session("session-key")
+ server._sessions.clear()
+
+
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 hermes_yaml as yaml
diff --git a/tui_gateway/methods_config_set.py b/tui_gateway/methods_config_set.py
index 286dc3fb72c..11001ea1755 100644
--- a/tui_gateway/methods_config_set.py
+++ b/tui_gateway/methods_config_set.py
@@ -286,6 +286,21 @@ def _set_yolo(rid, params, key, value, session):
skey = session["session_key"]
enable = _BOOL_WORDS.get(raw, not is_session_yolo_enabled(skey))
(enable_session_yolo if enable else disable_session_yolo)(skey)
+ # Persist to the session row, same contract as the CLI's /yolo
+ # (cli.py _persist_session_yolo): without this the toggle lives only in
+ # the in-memory session-yolo set, so `--resume` never restores it
+ # (_restore_session_yolo reads model_config.yolo_mode back) and nothing
+ # on disk reflects the toggle. Best-effort — the in-memory flag stays
+ # authoritative for this process either way. Prefer agent.session_id
+ # over session_key: after compression the key can be the ended parent
+ # while session_id is the live continuation (session finalize, #20001).
+ row_id = getattr(session.get("agent"), "session_id", None) or skey
+ try:
+ with _session_db(session) as db:
+ if db is not None and row_id:
+ db.set_session_yolo(row_id, enable)
+ except Exception:
+ logger.debug("failed to persist session yolo flag", exc_info=True)
_emit_session_info(params.get("session_id", ""), session)
else:
enable = _BOOL_WORDS.get(raw, not is_truthy_value(os.environ.get("HERMES_YOLO_MODE")))
diff --git a/tui_gateway/session_compression.py b/tui_gateway/session_compression.py
index 3aa7412336a..223309b3ccd 100644
--- a/tui_gateway/session_compression.py
+++ b/tui_gateway/session_compression.py
@@ -336,6 +336,16 @@ def _sync_session_key_after_compress(
if approval.is_session_yolo_enabled(old_key):
approval.enable_session_yolo(new_session_id)
approval.disable_session_yolo(old_key)
+ # Carry the PERSISTED flag onto the continuation row too, same as
+ # the CLI's rotation path: the in-memory swap above alone leaves
+ # a later `--resume <new_id>` without the bypass, because the
+ # toggle-time persist wrote the now-ended parent row.
+ try:
+ with _session_db(session) as db:
+ if db is not None:
+ db.set_session_yolo(new_session_id, True)
+ except Exception:
+ logger.debug("failed to carry session yolo flag", exc_info=True)
with contextlib.suppress(Exception):
approval.register_gateway_notify(new_session_id, lambda data: _emit_approval_request(sid, data))
# Invalidate any in-flight ``_drain_queued_prompt`` claim taken under the pre-rotation key: a raced