Skip to content

The agent that grows with you

fix(tui): keep a Desktop cwd record when the profile's terminal policy is unreadable

kshitijk4poor3408c1a23c68
diff --git a/tests/tui_gateway/test_unmounted_docker_cwd.py b/tests/tui_gateway/test_unmounted_docker_cwd.py
index a46a6167aaa..d9ab016529d 100644
--- a/tests/tui_gateway/test_unmounted_docker_cwd.py
+++ b/tests/tui_gateway/test_unmounted_docker_cwd.py
@@ -122,3 +122,31 @@ def test_a_bad_unrelated_terminal_setting_does_not_drop_the_registration(tmp_pat
finally:
terminal_tool.clear_task_env_overrides("bad-timeout")
reset_terminal_scope(token)
+
+
+def test_an_unreadable_profile_keeps_the_desktop_cwd_record(tmp_path, monkeypatch):
+ """The routed profile's refusal scope raises on every terminal read; the host-side record
+ (project tools read it) must still be written."""
+ import hermes_constants as hc
+ from tools import terminal_tool
+ from tui_gateway import server
+
+ home = tmp_path / "home"
+ profile = home / "profiles" / "bad"
+ profile.mkdir(parents=True)
+ (profile / "config.yaml").write_text("terminal: [unclosed", encoding="utf-8")
+ workspace = tmp_path / "host-project"
+ workspace.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setenv("TERMINAL_ENV", "local")
+ vars(server)["_register_session_cwd"]({
+ "session_key": "bad-profile", "cwd": str(workspace), "explicit_cwd": True,
+ "source": "desktop", "profile_home": str(profile),
+ })
+
+ token = hc.set_hermes_home_override(str(profile))
+ try:
+ assert terminal_tool.resolve_task_overrides("bad-profile").get("cwd") == str(workspace)
+ finally:
+ terminal_tool.clear_task_env_overrides("bad-profile")
+ hc.reset_hermes_home_override(token)
diff --git a/tui_gateway/session_workdir.py b/tui_gateway/session_workdir.py
index d1ad5dba417..55f3ca38f73 100644
--- a/tui_gateway/session_workdir.py
+++ b/tui_gateway/session_workdir.py
@@ -518,7 +518,7 @@ def _register_session_cwd(session: dict | None) -> None:
# and the scoped turn (`profile:<p>:<key>`) misses it until the first `cd`. Callers already
# inside the session's scope (the turn) bind nothing: the routed home is theirs already.
import hermes_constants as hc
- from tools.terminal_scope import install_and_reset_profile_terminal_scope
+ from tools.terminal_scope import TerminalPolicyUnavailable, install_and_reset_profile_terminal_scope
with contextlib.ExitStack() as stack:
profile_home = session.get("profile_home")
@@ -527,11 +527,18 @@ def _register_session_cwd(session: dict | None) -> None:
stack.enter_context(install_and_reset_profile_terminal_scope(profile_home))
overrides = {"cwd": cwd, "cwd_source": cwd_source}
_ensure_terminal_env_bridged()
- unmounted = not _tenv_bool("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false")
- if _tenv("TERMINAL_ENV", "local") == "docker" and unmounted:
- # The Desktop workspace is host context, not a directory in this sandbox. An empty cwd
- # leaves its configured cwd and a later in-container cd intact; host-side readers
- # (project tools) still find the session's folder under ``host_workspace``.
+ try:
+ unmounted_docker = _tenv("TERMINAL_ENV", "local") == "docker" and not _tenv_bool(
+ "TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false")
+ except TerminalPolicyUnavailable:
+ # An unreadable profile's terminal refuses every call anyway: register the cwd as usual
+ # so project tools still find the folder. (register_task_env_overrides writes the record
+ # before its live-env update, which raises under this refusal scope.)
+ unmounted_docker = False
+ if unmounted_docker:
+ # A host cwd is not a directory in this sandbox. An empty cwd leaves its configured cwd
+ # and a later in-container cd intact; host-side readers (project tools) still find the
+ # session's own folder under ``host_workspace``.
host = cwd if cwd_source == "session" else ""
overrides = {"cwd": "", "cwd_source": "process", "host_workspace": host}
register_task_env_overrides(session["session_key"], overrides)