Skip to content

The agent that grows with you

fix(tui-gateway): unavailable profile param is JSON-RPC 4064, not a ws dispatch crash

teknium1105e27f962d0
diff --git a/tests/tui_gateway/test_default_profile_session_name.py b/tests/tui_gateway/test_default_profile_session_name.py
index 953d19d56f1..16767354a4c 100644
--- a/tests/tui_gateway/test_default_profile_session_name.py
+++ b/tests/tui_gateway/test_default_profile_session_name.py
@@ -168,16 +168,3 @@ def test_custom_default_root_real_session_db_owner_stamping(tmp_path, monkeypatc
assert launch_db.get_session("lazy-default") is None
assert launch_db.get_session("seeded-default") is None
assert launch_db.get_session("branched-default") is None
-
-
-def test_deleted_profile_falls_back_to_current_profile(tmp_path, monkeypatch):
- """Deleted/non-existent profiles must not raise FileNotFoundError in _response_profile_name."""
- from tui_gateway import server
-
- default_home, launch_home = _profile_layout(tmp_path)
- monkeypatch.setattr(Path, "home", lambda: tmp_path)
- monkeypatch.setenv("HERMES_HOME", str(launch_home))
- monkeypatch.setattr(server, "_hermes_home", launch_home)
-
- assert server._response_profile_name("non-existent-profile-123") == server._current_profile_name()
-
diff --git a/tests/tui_gateway/test_profile_target_unavailable.py b/tests/tui_gateway/test_profile_target_unavailable.py
index f55f3b17f60..a4ea6da0ae4 100644
--- a/tests/tui_gateway/test_profile_target_unavailable.py
+++ b/tests/tui_gateway/test_profile_target_unavailable.py
@@ -78,3 +78,23 @@ def test_profile_param_traversal_fails_closed(tmp_path, monkeypatch, name):
with pytest.raises(FileNotFoundError):
with server._profile_db({"profile": name}):
pass
+
+
+def test_unavailable_profile_is_a_typed_rpc_error_not_a_dispatch_crash(tmp_path, monkeypatch):
+ """A client still holding a deleted profile gets JSON-RPC 4064 from every profile-scoped
+ method (#107829) — the method itself keeps raising, the dispatcher chokepoint maps it."""
+ from tui_gateway import server
+
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ (home / "config.yaml").write_text("terminal:\n cwd: /launch\n")
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(server, "_hermes_home", home)
+
+ for method, params in (("session.create", {"profile": "gone"}),
+ ("config.get", {"profile": "gone", "key": "full"})):
+ resp = server.handle_request({"jsonrpc": "2.0", "id": 7, "method": method, "params": params})
+ assert resp["error"]["code"] == 4064, resp
+ assert "gone" in resp["error"]["message"]
+ assert server._response_profile_name("gone") == server._current_profile_name()
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index 195f572a207..bafa526aa29 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -478,7 +478,7 @@ def _response_profile_name(profile: str | None = None) -> str:
return _current_profile_name()
try:
return name if _profile_home(name) is not None else _current_profile_name()
- except FileNotFoundError:
+ except ProfileUnavailableError:
return _current_profile_name()
@@ -489,6 +489,12 @@ def _db_unavailable_error(rid, *, code: int):
# ── Per-session profile scoping: the desktop's app-global remote mode points every profile at this
# backend, so calls carry ``profile`` → open that profile's db and bind its HERMES_HOME (ContextVar
# override) so config/skills/model/persistence resolve to it. Omitted/own profile → launch profile.
+class ProfileUnavailableError(FileNotFoundError):
+ """An explicit ``profile`` param names no live profile on this host. Raised out of the method
+ (never a silent fall-back to the launch profile); ``handle_request`` turns it into JSON-RPC 4064
+ so a client holding a deleted profile gets a typed error instead of a ws dispatch crash (#107829)."""
+
+
def _profile_home(profile: str | None) -> Path | None:
"""Resolve a named profile's home on THIS host, or None for the launch profile."""
if not (name := _canonical_profile_request((profile or "").strip())):
@@ -499,7 +505,7 @@ def _profile_home(profile: str | None) -> Path | None:
except ValueError:
home = None
if home is None or not home.is_dir():
- raise FileNotFoundError(f"Profile '{name}' does not exist.")
+ raise ProfileUnavailableError(f"Profile '{name}' does not exist.")
if home.resolve() == Path(_hermes_home).resolve():
return None # already the launch profile (no override needed)
if home not in _served_profile_homes:
@@ -769,6 +775,8 @@ def handle_request(req: dict) -> dict | None:
token = _current_rpc_method.set(method)
try:
return fn(rid, params)
+ except ProfileUnavailableError as exc:
+ return _err(rid, 4064, str(exc))
finally:
_current_rpc_method.reset(token)