Skip to content

The agent that grows with you

fix(tui_gateway): foreign-profile RPCs open state.db read-only

yoniebans31e0300b5287
diff --git a/tests/tui_gateway/test_profile_db_readonly.py b/tests/tui_gateway/test_profile_db_readonly.py
new file mode 100644
index 00000000000..da9b551269b
--- /dev/null
+++ b/tests/tui_gateway/test_profile_db_readonly.py
@@ -0,0 +1,45 @@
+"""Foreign-profile _profile_db handles must not write-lock another profile's live store.
+
+A named-profile backend serving RPCs about a different profile (desktop app-global remote
+mode, profile switcher) used to acquire() a WRITER on that profile's state.db per RPC and
+close it in the handler's finally. Reads never need that lock, and the writer's close
+participated in the deleted-WAL incident class. Read paths now open read-only, mirroring
+hermes_cli.web_routers.profiles._read_profile_db; the few RPCs that genuinely write
+(move-cwd, delete, set_hidden, foreign import) opt in with writer=True.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import tui_gateway.server as server
+from hermes_state import SessionDB
+
+
+def _seed_store(home: Path) -> Path:
+ home.mkdir(parents=True, exist_ok=True)
+ db = SessionDB(db_path=home / "state.db")
+ db.create_session(session_id="seed", source="cli", model="m")
+ db.close()
+ return home
+
+
+def _bind_foreign(monkeypatch, tmp_path: Path) -> None:
+ foreign = _seed_store(tmp_path / "profiles" / "code")
+ monkeypatch.setattr(server, "_profile_home", lambda name: foreign if (name or "").strip() == "code" else None)
+
+
+def test_foreign_profile_db_is_read_only(monkeypatch, tmp_path):
+ _bind_foreign(monkeypatch, tmp_path)
+ with server._profile_db({"profile": "code"}) as db:
+ assert db is not None
+ assert db.read_only is True
+ assert db.get_session("seed") is not None
+
+
+def test_foreign_profile_db_writer_opt_in(monkeypatch, tmp_path):
+ _bind_foreign(monkeypatch, tmp_path)
+ with server._profile_db({"profile": "code"}, writer=True) as db:
+ assert db is not None
+ assert db.read_only is False
+ assert db.set_session_title("seed", "renamed") is True
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index b744e3ca6b7..8465eec67d5 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -15350,7 +15350,10 @@ def test_session_list_honors_params_profile_opens_profile_db(monkeypatch, tmp_pa
monkeypatch.setattr(server, "_profile_home", lambda p: profile_home if p == "mlperf" else None)
monkeypatch.setattr(server, "_get_db", lambda: LaunchDB())
- monkeypatch.setattr("hermes_state_registry.acquire", ProfileDB)
+ monkeypatch.setattr(
+ "hermes_cli.web_server_sessions._open_session_db_at_path",
+ lambda db_path, *, read_only: ProfileDB(db_path=db_path),
+ )
resp = server.handle_request(
{
@@ -15391,7 +15394,10 @@ def test_session_most_recent_honors_params_profile(monkeypatch, tmp_path):
monkeypatch.setattr(server, "_profile_home", lambda p: profile_home if p == "mlperf" else None)
monkeypatch.setattr(server, "_get_db", lambda: LaunchDB())
- monkeypatch.setattr("hermes_state_registry.acquire", ProfileDB2)
+ monkeypatch.setattr(
+ "hermes_cli.web_server_sessions._open_session_db_at_path",
+ lambda db_path, *, read_only: ProfileDB2(db_path=db_path),
+ )
resp = server.handle_request(
{
@@ -22528,7 +22534,7 @@ def test_workspace_move_rehomes_running_session(monkeypatch, tmp_path):
import contextlib
@contextlib.contextmanager
- def _fake_db(_params):
+ def _fake_db(_params, *, writer=False):
yield FakeDB()
monkeypatch.setattr(server, "_profile_db", _fake_db)
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index efc946d44cd..b14d20594be 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -884,7 +884,7 @@ def _(rid, params: dict) -> dict:
live_sid, live = next(
((sid, sess) for sid, sess in list(_sessions.items()) if sess.get("session_key") == target), ("", None))
branch, root = git_probe.branch(resolved), git_probe.common_repo_root(resolved)
- with _profile_db(params) as db:
+ with _profile_db(params, writer=True) as db:
if db is None:
return _db_unavailable_error(rid, code=5007)
# A draft has no row yet; the live re-home still applies (row inherits cwd on write).
@@ -945,7 +945,7 @@ def _(rid, params: dict) -> dict:
if any(s.get("session_key") == target for _sid, s in snapshot):
return _err(rid, 4023, "cannot delete an active session")
profile_home = _profile_home((params.get("profile") or "").strip() or None)
- with _profile_db(params) as db:
+ with _profile_db(params, writer=True) as db:
if db is None:
return _db_unavailable_error(rid, code=5036)
try:
@@ -1013,7 +1013,7 @@ def _(rid, params: dict) -> dict:
LIVE runtime id first (unpersisted drafts via ``pending_hidden``), then a stored id/key in the profile db."""
hidden = is_truthy_value(params.get("hidden", True))
session, err = _sess_nowait(params, rid)
- with (_profile_db(params) if session is None else _session_db(session)) as db:
+ with (_profile_db(params, writer=True) if session is None else _session_db(session)) as db:
if db is None:
return _db_unavailable_error(rid, code=5007)
try:
diff --git a/tui_gateway/methods_session_foreign.py b/tui_gateway/methods_session_foreign.py
index ba37ad85801..c6f37ac4f92 100644
--- a/tui_gateway/methods_session_foreign.py
+++ b/tui_gateway/methods_session_foreign.py
@@ -20,7 +20,7 @@ def _foreign_list(rid, params):
def _foreign_history_request(rid, params, importing):
from hermes_cli.foreign_sessions_browser import import_browser_session, preview_foreign_session
try:
- with _profile_db(params) as db:
+ with _profile_db(params, writer=importing) as db:
if db is None:
return _db_unavailable_error(rid, code=-32000)
result = (import_browser_session(params.get("id"), db, _response_profile_name(params.get("profile")))
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index cc60857233c..b4cf0bc4a85 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -432,9 +432,13 @@ def _open_profile_session_db(profile_home):
@contextlib.contextmanager
-def _profile_db(params: dict | None = None):
+def _profile_db(params: dict | None = None, *, writer: bool = False):
"""Yield the SessionDB for ``params['profile']`` (None when unavailable); closes dedicated
- profile handles, leaves the launch-profile shared handle open."""
+ profile handles, leaves the launch-profile shared handle open.
+
+ Foreign-profile handles are read-only unless ``writer=True``: that store belongs to ITS
+ gateway/dashboard, and a writer here would take its write lock per RPC. Mirrors
+ hermes_cli.web_routers.profiles._read_profile_db."""
profile = (params.get("profile") or "").strip() or None if isinstance(params, dict) else None
# Launch/own profile → the shared _get_db() handle (left open); another profile → a dedicated
# handle closed below (app-global remote mode). db is None when unavailable.
@@ -442,8 +446,13 @@ def _profile_db(params: dict | None = None):
db, owns = _get_db(), False
else:
try:
- from hermes_state_registry import acquire
- db, owns = acquire(Path(profile_home) / "state.db"), True
+ if writer:
+ from hermes_state_registry import acquire
+ db = acquire(Path(profile_home) / "state.db")
+ else:
+ from hermes_cli.web_server_sessions import _open_session_db_at_path
+ db = _open_session_db_at_path(Path(profile_home) / "state.db", read_only=True)
+ owns = True
except Exception as exc:
logger.warning("TUI profile session store unavailable for %s: %s", profile, exc)
db, owns = None, False