Skip to content

The agent that grows with you

fix(tui): sandbox resume ignores legacy desktop launch dirs; compute-host fallback record resolves its source

Ben Barclaya079bc6e1c3f
diff --git a/tests/tui_gateway/test_remote_backend_launch_cwd.py b/tests/tui_gateway/test_remote_backend_launch_cwd.py
index 12c812b620c..754b2c131f6 100644
--- a/tests/tui_gateway/test_remote_backend_launch_cwd.py
+++ b/tests/tui_gateway/test_remote_backend_launch_cwd.py
@@ -185,6 +185,33 @@ def test_sandbox_resume_drops_a_stored_hermes_home_cwd(launch_dir, monkeypatch):
assert server._resumable_stored_cwd(str(hermes_home), None) == str(hermes_home)
[email protected]("launch", ["os-home", "root"])
+def test_sandbox_resume_drops_a_stored_desktop_launch_dir(launch_dir, monkeypatch, tmp_path, launch):
+ """Legacy rows holding a desktop launch dir (the OS home, ``/``) are not adopted as a sandbox workspace, by
+ hydration or ``session.resume`` (both go through ``_resumable_stored_cwd``); local still adopts them."""
+ from hermes_state import SessionDB
+
+ stored = str(server.Path.home()) if launch == "os-home" else "/"
+ _backend(monkeypatch, "modal")
+ assert server._resumable_stored_cwd(stored, None) == ""
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ try:
+ db.create_session("legacy", source="desktop", model="m", cwd=stored)
+ monkeypatch.setitem(server._sessions, "s-legacy", {"session_key": "legacy", "source": "desktop",
+ "cwd": str(launch_dir), "profile_home": None})
+ monkeypatch.setattr(server, "_persist_session_cwd_and_schedule_git_meta", lambda *a, **k: None)
+ server._hydrate_session_cwd("s-legacy", "legacy", db, None)
+ session = server._sessions["s-legacy"]
+ assert not session.get("explicit_cwd")
+ assert server._terminal_task_cwd_with_source(session) == ("", "process")
+ finally:
+ db.close()
+
+ monkeypatch.setenv("TERMINAL_ENV", "local")
+ assert server._resumable_stored_cwd(stored, None) == stored
+
+
def test_sandbox_resume_keeps_a_workspace_picked_under_the_hermes_root(launch_dir, monkeypatch):
"""A folder the user picked under the Hermes root is a workspace, not a launch dir: it persists and resumes."""
picked = server.get_hermes_home() / "projects" / "chosen"
@@ -257,6 +284,34 @@ def test_compute_host_carries_workspace_provenance(launch_dir, monkeypatch, tmp_
assert server._terminal_task_cwd_with_source(child) == (str(project), "session")
+def test_compute_host_minimal_record_keeps_workspace_provenance(launch_dir, monkeypatch, tmp_path):
+ """When ``_init_session`` fails, the compute host's minimal record still carries the picked workspace."""
+ import io
+ import types
+
+ from tui_gateway.compute_host import ComputeHost
+
+ project = tmp_path / "project"
+ project.mkdir()
+ _backend(monkeypatch, "modal")
+
+ def failing_init_session(*_a, **_k):
+ raise RuntimeError("side machinery unavailable")
+
+ monkeypatch.setattr(server, "_make_agent", lambda sid, key, **kw: types.SimpleNamespace(session_id=key))
+ monkeypatch.setattr(server, "_transfer_db_to_agent", lambda agent, db: False)
+ monkeypatch.setattr(server, "_init_session", failing_init_session)
+ server._sessions.pop("child-min", None)
+ frame = {"sid": "child-min", "session_key": "picked-min", "cwd": str(project), "explicit_cwd": True,
+ "history": [], "source": "desktop"}
+ try:
+ child = ComputeHost(stdout=io.StringIO(), heartbeat_secs=0)._build_server_session(server, frame, "child-min")
+ assert child["explicit_cwd"] is True and child["source"] == "desktop"
+ assert server._terminal_task_cwd_with_source(child) == (str(project), "session")
+ finally:
+ server._sessions.pop("child-min", None)
+
+
def test_sandbox_path_completion_lists_the_backend_cwd(launch_dir, monkeypatch):
"""``complete.path`` with no terminal cwd lists relative to the backend's own cwd, never the host dir."""
_backend(monkeypatch, "modal")
diff --git a/tui_gateway/compute_host.py b/tui_gateway/compute_host.py
index e395353602b..c3a3ea05d77 100644
--- a/tui_gateway/compute_host.py
+++ b/tui_gateway/compute_host.py
@@ -410,7 +410,7 @@ class ComputeHost:
"slash_worker": None, "show_reasoning": server._load_show_reasoning(),
"tool_progress_mode": server._load_tool_progress_mode(), "edit_snapshots": {},
"tool_started_at": {}, "model_override": frame.get("model_override"),
- "source": server._sanitize_client_source(frame.get("source")),
+ "source": server._resolve_session_source(frame.get("source")),
"transport": self._transport}
session = server._sessions[sid]
session["transport"] = self._transport
diff --git a/tui_gateway/session_workdir.py b/tui_gateway/session_workdir.py
index df42cb60ea8..8271fd86e6f 100644
--- a/tui_gateway/session_workdir.py
+++ b/tui_gateway/session_workdir.py
@@ -323,12 +323,13 @@ def _is_hermes_owned_cwd(cwd: str, profile_home) -> bool:
return any(path == root or root in path.parents for root in roots) or _is_install_tree(path)
-def _is_hermes_launch_dir(cwd: str, profile_home) -> bool:
- """Whether ``cwd`` IS a directory the Hermes process is launched from: the Hermes root (``/opt/data`` in the
- Docker image), a subprocess home (``/opt/data/home``), the bound profile's home, or the install tree. Folders
- below the root and the homes are not: a user may keep a workspace there."""
+def _is_launch_dir(cwd: str, profile_home) -> bool:
+ """Whether ``cwd`` IS a directory a Hermes process is launched from rather than a folder someone picked: the
+ Hermes root (``/opt/data`` in the Docker image), a subprocess home (``/opt/data/home``), the bound profile's
+ home, the install tree, the OS user's home or ``/`` (the desktop's launch dirs). Folders below them are not: a
+ user may keep a workspace there."""
from agent.runtime_cwd import _is_install_tree
- from hermes_constants import get_default_hermes_root
+ from hermes_constants import get_default_hermes_root, get_real_home
if not os.path.isabs(cwd):
return False
@@ -336,9 +337,11 @@ def _is_hermes_launch_dir(cwd: str, profile_home) -> bool:
path = Path(cwd).resolve()
home = Path(profile_home or get_hermes_home()).expanduser().resolve()
root = get_default_hermes_root(home=home).resolve()
+ launch = {home, home / "home", root, root / "home", Path(path.anchor),
+ Path(get_real_home()).resolve(), Path.home().resolve()}
except (OSError, RuntimeError):
return False
- return path in {home, home / "home", root, root / "home"} or _is_install_tree(path)
+ return path in launch or _is_install_tree(path)
def _is_other_profiles_cwd(cwd: str, profile_home) -> bool:
@@ -363,14 +366,15 @@ def _is_other_profiles_cwd(cwd: str, profile_home) -> bool:
def _resumable_stored_cwd(cwd, profile_home) -> str:
"""A session row's stored cwd as a resume may adopt it: empty when an ssh session's row holds a path in Hermes's
- own host tree (a host launch directory, never a remote workspace), when a sandbox session's row holds one of the
- launch directories themselves (rows written before launch dirs stopped being persisted), or when a named
- profile's row holds another profile's (the resume then lands in :func:`_profile_default_cwd` and re-persists
- it). A folder a sandbox user picked under the Hermes root stays."""
+ own host tree (a host launch directory, never a remote workspace), when a sandbox session's row holds a launch
+ directory itself (:func:`_is_launch_dir`: rows written before launch dirs stopped being persisted), or when a
+ named profile's row holds another profile's (the resume then lands in :func:`_profile_default_cwd` and
+ re-persists it). Hydration and ``session.resume`` both adopt through here, then treat the result as the
+ session's workspace. A folder a sandbox user picked (even under the Hermes root or ``$HOME``) stays."""
cwd = str(cwd or "")
if cwd and _cwd_is_remote(profile_home) and _is_hermes_owned_cwd(cwd, profile_home):
return ""
- if cwd and not _host_cwd_is_terminal_cwd(profile_home) and _is_hermes_launch_dir(cwd, profile_home):
+ if cwd and not _host_cwd_is_terminal_cwd(profile_home) and _is_launch_dir(cwd, profile_home):
return ""
if cwd and _is_other_profiles_cwd(cwd, profile_home):
return ""