Skip to content

The agent that grows with you

fix(tui): _init_session cwd hydration must not fall back to the launch DB

Teknium4896cab0ebc9
diff --git a/tests/hermes_state/test_named_profile_session_db.py b/tests/hermes_state/test_named_profile_session_db.py
index 038c496c231..68238e27623 100644
--- a/tests/hermes_state/test_named_profile_session_db.py
+++ b/tests/hermes_state/test_named_profile_session_db.py
@@ -164,3 +164,48 @@ def test_deferred_build_fails_closed_when_profile_store_unopenable(homes, monkey
with server._sessions_lock:
server._sessions.pop(sid, None)
launch.close()
+
+
+def test_init_session_skips_launch_db_when_profile_store_unopenable(homes, monkeypatch):
+ """Sibling site: _init_session's cwd hydration must not fall back either.
+
+ Before the fix, ``except Exception: db = _get_db()`` hydrated/persisted a
+ named-profile session's cwd row against the launch state.db. _get_db is
+ patched to a tripwire: any call proves the launch store was touched.
+ """
+ root, profile = homes
+ from tui_gateway import server
+
+ _make_store_unopenable(profile)
+ sid = "sid-worker-init"
+
+ def tripwire():
+ raise AssertionError("launch _get_db() must not be touched for a named-profile session")
+
+ monkeypatch.setattr(server, "_get_db", tripwire)
+ monkeypatch.setattr(server, "_wire_callbacks", lambda _sid: None)
+ monkeypatch.setattr(server, "_start_notification_poller", lambda _sid, _s: threading.Event())
+ monkeypatch.setattr(server, "_notify_session_boundary", lambda *a, **kw: None)
+ monkeypatch.setattr(server, "_emit", lambda *a, **kw: None)
+ monkeypatch.setattr(server, "_session_info", lambda _agent, _s=None: {})
+ monkeypatch.setattr(server, "_schedule_mcp_late_refresh", lambda _sid, _agent: None)
+ monkeypatch.setattr(server, "_register_session_cwd", lambda _s: None)
+ monkeypatch.setattr(server, "_load_show_reasoning", lambda: False)
+ monkeypatch.setattr(server, "_load_tool_progress_mode", lambda: "off")
+ monkeypatch.setattr(server, "_load_memory_notifications", lambda: "off")
+
+ try:
+ server._init_session(
+ sid,
+ "key-worker-init",
+ SimpleNamespace(),
+ [],
+ cwd=str(root),
+ profile_home=str(profile),
+ )
+ with server._sessions_lock:
+ assert sid in server._sessions
+ assert _ids(root / "state.db") == set()
+ finally:
+ with server._sessions_lock:
+ server._sessions.pop(sid, None)
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index 8bc77155fda..f86e588e7e4 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -7920,12 +7920,21 @@ def _init_session(
db = session_db
elif profile_home:
try:
- from hermes_state import SessionDB
-
- db = SessionDB(db_path=Path(profile_home) / "state.db")
+ db = _open_profile_session_db(profile_home)
_init_owns_db = True
except Exception:
- db = _get_db()
+ # FAIL CLOSED — same class as the deferred-build bind: a
+ # named-profile session must never read/write the launch
+ # ``state.db``. Skip the cwd hydration/persist (the row lands on
+ # the agent's own lazy-create once the store recovers) rather
+ # than writing this session's row into the wrong profile's store.
+ logger.warning(
+ "profile session store unavailable for %s — skipping cwd "
+ "hydration instead of touching the launch state.db",
+ profile_home,
+ exc_info=True,
+ )
+ db = None
else:
db = _get_db()
try: