Skip to content

The agent that grows with you

refactor(tui-gateway): restore session /yolo once in session.resume, test in its own file

Teknium44395b717754
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..8653e971ae3
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+aslater3
diff --git a/tests/tui_gateway/test_resume_session_yolo.py b/tests/tui_gateway/test_resume_session_yolo.py
new file mode 100644
index 00000000000..9b1cada0eee
--- /dev/null
+++ b/tests/tui_gateway/test_resume_session_yolo.py
@@ -0,0 +1,39 @@
+"""A session /yolo (TUI Shift+Tab, the Desktop zap) survives a backend restart: persisted on the session
+row and re-armed by ``session.resume`` in a process whose in-memory approval set starts empty."""
+
+import types
+
+from tui_gateway import server
+
+
+def test_session_yolo_toggle_survives_a_backend_restart(monkeypatch, tmp_path):
+ """A session /yolo (Shift+Tab / the Desktop zap) is persisted on the row and re-armed when a fresh
+ backend resumes it, including a toggle made before the row existed; switching it off persists too."""
+ from hermes_state import SessionDB
+ from tools.approval import clear_session, is_session_yolo_enabled
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ monkeypatch.setattr(server, "_get_db", lambda: db)
+ monkeypatch.setattr(server, "_enable_gateway_prompts", lambda: None)
+ monkeypatch.setattr(server, "_schedule_agent_build", lambda sid: None)
+ try:
+ # Toggled before the first message: the lazily created row must still record it.
+ server._sessions["sid"] = {"agent": types.SimpleNamespace(), "session_key": "row-a"}
+ server.handle_request({"id": "1", "method": "config.set", "params": {"session_id": "sid", "key": "yolo"}})
+ assert server._ensure_session_db_row(server._sessions["sid"]) is not False
+ assert SessionDB.session_yolo_enabled(db.get_session("row-a"))
+
+ server._sessions.clear()
+ clear_session("row-a") # a new backend process starts with an empty in-memory set
+ resp = server.handle_request({"id": "2", "method": "session.resume", "params": {"session_id": "row-a"}})
+ assert resp["result"]["session_key"] == "row-a"
+ assert is_session_yolo_enabled("row-a") is True
+
+ server.handle_request(
+ {"id": "3", "method": "config.set", "params": {"session_id": resp["result"]["session_id"], "key": "yolo"}})
+ assert is_session_yolo_enabled("row-a") is False
+ assert not SessionDB.session_yolo_enabled(db.get_session("row-a"))
+ finally:
+ server._sessions.clear()
+ clear_session("row-a")
+ db.close()
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index dfc32cbb5cf..41572ed70bc 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -8843,39 +8843,6 @@ def test_config_set_yolo_stale_session_id_is_refused_not_process_scoped(monkeypa
assert os.environ["HERMES_YOLO_MODE"] == "0"
-def test_session_yolo_toggle_survives_a_backend_restart(monkeypatch, tmp_path):
- """A session /yolo (Shift+Tab / the Desktop zap) is persisted on the row and re-armed when a fresh
- backend resumes it, including a toggle made before the row existed; switching it off persists too."""
- from hermes_state import SessionDB
- from tools.approval import clear_session, is_session_yolo_enabled
-
- db = SessionDB(db_path=tmp_path / "state.db")
- monkeypatch.setattr(server, "_get_db", lambda: db)
- monkeypatch.setattr(server, "_enable_gateway_prompts", lambda: None)
- monkeypatch.setattr(server, "_schedule_agent_build", lambda sid: None)
- try:
- # Toggled before the first message: the lazily created row must still record it.
- server._sessions["sid"] = _session(session_key="row-a")
- server.handle_request({"id": "1", "method": "config.set", "params": {"session_id": "sid", "key": "yolo"}})
- assert server._ensure_session_db_row(server._sessions["sid"]) is not False
- assert SessionDB.session_yolo_enabled(db.get_session("row-a"))
-
- server._sessions.clear()
- clear_session("row-a") # a new backend process starts with an empty in-memory set
- resp = server.handle_request({"id": "2", "method": "session.resume", "params": {"session_id": "row-a"}})
- assert resp["result"]["session_key"] == "row-a"
- assert is_session_yolo_enabled("row-a") is True
-
- server.handle_request(
- {"id": "3", "method": "config.set", "params": {"session_id": resp["result"]["session_id"], "key": "yolo"}})
- assert is_session_yolo_enabled("row-a") is False
- assert not SessionDB.session_yolo_enabled(db.get_session("row-a"))
- finally:
- server._sessions.clear()
- clear_session("row-a")
- db.close()
-
-
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_session.py b/tui_gateway/methods_session.py
index b82b824cd88..3e1681f9cf1 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -132,6 +132,8 @@ def _session_row_summary(row: dict, *, tip_row: dict | None = None, resolved_id=
from hermes_state_sessions import INTERNAL_LISTING_SOURCES
+from .methods_session_model_guard import restore_session_yolo as _restore_session_yolo
+
# Hidden from human listings (kanban workers, tool integrations, one-shot runs); see INTERNAL_LISTING_SOURCES.
_LISTING_DENY_SOURCES = frozenset(INTERNAL_LISTING_SOURCES)
@@ -615,16 +617,6 @@ def _(rid, params: dict) -> dict:
return _ok(rid, {"verification": {"status": "unknown", "evidence": None}})
-def _restore_session_yolo(session_key: str, session_meta: dict | None) -> None:
- """Re-arm a persisted session /yolo on resume: a new backend process starts with an empty in-memory
- approval set. Keyed on the stored id (``ctx.target``), the key approvals are checked under, never
- the freshly minted runtime sid. Mirrors ``cli_session_mixin._restore_session_yolo``."""
- from hermes_state import SessionDB
- from tools.approval import enable_session_yolo
- if session_key and SessionDB.session_yolo_enabled(session_meta):
- enable_session_yolo(session_key)
-
-
# ── session.resume ───────────────────────────────────────────────────
class _Resume:
"""Per-call ``session.resume`` state. ``owns_db``: the DEDICATED profile handle is ours
@@ -971,7 +963,6 @@ def _resume_deferred(ctx: _Resume) -> dict:
resume_message_count=int(ctx.found.get("message_count") or 0))
if (reused := ctx.claim(sid, record)) is not None:
return reused
- _restore_session_yolo(ctx.target, ctx.found)
# Desktop owns the visible transcript through bounded REST pages, not this model-history restore.
_schedule_resume_hydration(
sid, ctx.target, ctx.db, close_db=ctx.owns_db,
@@ -997,7 +988,6 @@ def _resume_cold(ctx: _Resume) -> dict:
todo_state=_todo_state_from_history(history))
if (reused := ctx.claim(sid, record)) is not None:
return reused
- _restore_session_yolo(ctx.target, ctx.found)
_schedule_agent_build(sid)
_schedule_session_cap_enforcement() # trim detached idle sessions over the cap
return _resume_response(ctx, sid, record, info=ctx.info(cwd, overrides), display=display_history,
@@ -1039,7 +1029,6 @@ def _resume_eager(ctx: _Resume) -> dict:
if ctx.owns_db:
_transfer_db_to_agent(agent, ctx.db)
ctx.owns_db = False
- _restore_session_yolo(ctx.target, ctx.found)
if (session := _sessions.get(sid)) is not None:
if stored_runtime_overrides.get("model_override") is not None:
session["model_override"] = stored_runtime_overrides["model_override"]
@@ -1084,11 +1073,11 @@ def _(rid, params: dict) -> dict:
return resp
ctx.profile_resume_cwd = (_resumable_stored_cwd(_str_param(ctx.found, "cwd"), ctx.profile_home)
or _profile_workspace_cwd(ctx.profile_home))
- # Fast path: reuse a session live IN THIS PROFILE (never another profile's runtime).
- with _session_resume_lock:
+ with _session_resume_lock: # fast path: reuse a session live IN THIS PROFILE, never another's
live = _find_live_session_by_key(ctx.target, ctx.profile_home)
if live is not None:
return _resume_reuse_live(ctx, *live)
+ _restore_session_yolo(ctx.target, ctx.found) # a new backend starts with an empty approval set
if ctx.lazy:
return _resume_lazy(ctx)
if ctx.eager_build:
@@ -2217,8 +2206,6 @@ def _(rid, params: dict) -> dict:
return _ok(rid, {"closed": _teardown_popped_session(session, end_reason="tui_close")})
-
-
@_session_method("session.branch", live=True)
def _(rid, params: dict, session: dict) -> dict:
return _branch_live(rid, params, session)
@@ -2230,10 +2217,6 @@ def _(rid, params: dict, session: dict) -> dict:
return _branch_live(rid, params, session, omit_messages=True)
-
-
-
-
# ── delegation / spawn trees ─────────────────────────────────────────
@method("delegation.status")
def _(rid, params: dict) -> dict:
diff --git a/tui_gateway/methods_session_model_guard.py b/tui_gateway/methods_session_model_guard.py
index 69510126168..01b7e348f06 100644
--- a/tui_gateway/methods_session_model_guard.py
+++ b/tui_gateway/methods_session_model_guard.py
@@ -1,4 +1,5 @@
-"""``session.create`` composer overrides and the model×provider coherence gate (#96817).
+"""``session.create`` composer overrides, the model×provider coherence gate (#96817), and the
+per-session runtime state ``session.resume`` restores from the stored row.
A composer, script or older client can pin a model the selected provider cannot serve
(``gpt-5.5`` on ``anthropic``); the session used to be minted fine and the FIRST turn died with
@@ -49,3 +50,13 @@ def create_overrides(params: dict) -> tuple:
elif "fast" in params:
service_tier_override = "priority" if is_truthy_value(params.get("fast")) else ""
return model_override, reasoning_override, service_tier_override
+
+
+def restore_session_yolo(session_key: str, session_meta: dict | None) -> None:
+ """Re-arm a persisted session /yolo on ``session.resume``: a new backend process starts with an empty
+ in-memory approval set. Keyed on the stored id (``ctx.target``), the key approvals are checked under,
+ never the freshly minted runtime sid. Mirrors ``cli_session_mixin._restore_session_yolo``."""
+ from hermes_state import SessionDB
+ from tools.approval import enable_session_yolo
+ if session_key and SessionDB.session_yolo_enabled(session_meta):
+ enable_session_yolo(session_key)