Skip to content

The agent that grows with you

fix(fs): resolve an unmounted Docker session's files in the profile sandbox

kshitijk4poorb623ded86d38
diff --git a/gateway/platforms/docker_media_paths.py b/gateway/platforms/docker_media_paths.py
index 22be9267c41..cb2f523ef4b 100644
--- a/gateway/platforms/docker_media_paths.py
+++ b/gateway/platforms/docker_media_paths.py
@@ -15,9 +15,16 @@ _WORKSPACE_KEY_RE = re.compile(r"^[0-9a-f]{16}$")
def _scope():
- from gateway.platforms.base import _TRUTHY, _docker_persistent_active, _tenv
+ from gateway.platforms.base import _docker_persistent_active, _tenv
- return _tenv, _TRUTHY, _docker_persistent_active
+ return _tenv, _docker_persistent_active
+
+
+def _cwd_mount_enabled() -> bool:
+ """The terminal's own predicate, so the file panel agrees with what the container mounted."""
+ from tools.terminal_tool_config import _tenv_bool
+
+ return _tenv_bool("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false")
def _docker_sandbox_dir_candidates(session_key: str = "", workspace_key: str = "") -> list[str]:
@@ -26,10 +33,12 @@ def _docker_sandbox_dir_candidates(session_key: str = "", workspace_key: str = "
Mirrors ``_resolve_container_task_id``: profile scope (``default``, else
``profile:<name>``), then the legacy ``session:<key>`` layout. A session
that bound a host workspace also has ``<base>:ws:<digest>`` — a different
- directory from the shared profile sandbox. The digest is explicit because
+ directory from the shared profile sandbox — but only with the cwd mount on:
+ without it every session keeps the profile sandbox, and a ``*_ws_*`` dir
+ left by an older build is not this session's. The digest is explicit because
an ambient lookup after the turn would collapse onto ``default``.
"""
- tenv, _truthy, _persistent = _scope()
+ tenv, _persistent = _scope()
try:
from tools.environments.path_utils import sanitize_task_id_for_path
except Exception: # health: allow BLE001 -- path translation fails closed; a miss is a 404, not a 500
@@ -44,7 +53,7 @@ def _docker_sandbox_dir_candidates(session_key: str = "", workspace_key: str = "
# key is not a per-workspace container, so a :ws: digest does not apply.
if shared := tenv("TERMINAL_DOCKER_SHARED_CONTAINER_KEY", "").strip():
candidates.append(sanitize_task_id_for_path(f"shared:{shared}"))
- elif _WORKSPACE_KEY_RE.fullmatch(workspace_key or ""):
+ elif _WORKSPACE_KEY_RE.fullmatch(workspace_key or "") and _cwd_mount_enabled():
base = "default" if profile == "default" else f"profile:{profile}"
candidates.append(sanitize_task_id_for_path(f"{base}:ws:{workspace_key}"))
if profile != "default":
@@ -63,7 +72,7 @@ def _docker_persistent_sandbox_roots(
Empty unless Docker + persistent. The translator tries each until the file
resolves.
"""
- _tenv, _truthy, persistent = _scope()
+ _tenv, persistent = _scope()
if not persistent():
return []
try:
@@ -76,7 +85,7 @@ def _docker_persistent_sandbox_roots(
def _mounted_workspace_cwd(workspace_cwd: str = "") -> list[Path]:
- tenv, _truthy, _persistent = _scope()
+ tenv, _persistent = _scope()
cwd = workspace_cwd or tenv("TERMINAL_CWD") or os.getcwd()
try:
host = Path(os.path.expanduser(cwd)).resolve(strict=False)
@@ -114,10 +123,10 @@ def _shared_cwd_mount_guess(workspace_key: str) -> list[Path]:
cross-process attach never inspects the container's mounts. Reads keep the
profile cwd, as before; a session Save there is a guess the caller refuses.
"""
- tenv, truthy, persistent = _scope()
+ tenv, persistent = _scope()
if not persistent() or not _WORKSPACE_KEY_RE.fullmatch(workspace_key or ""):
return []
- if tenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false").strip().lower() not in truthy:
+ if not _cwd_mount_enabled():
return []
if not tenv("TERMINAL_DOCKER_SHARED_CONTAINER_KEY", "").strip():
return []
@@ -138,12 +147,11 @@ def _default_docker_workspace_host_roots(
``/workspace`` is still the sandbox directory, because the host cwd was
not bind-mounted when the container was created.
"""
- tenv, truthy, persistent = _scope()
+ tenv, persistent = _scope()
if not persistent():
return []
roots: list[Path] = []
- mounted = tenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false").strip().lower() in truthy
- if mounted:
+ if _cwd_mount_enabled():
if not _WORKSPACE_KEY_RE.fullmatch(workspace_key or ""):
return _mounted_workspace_cwd()
if tenv("TERMINAL_DOCKER_SHARED_CONTAINER_KEY", "").strip():
diff --git a/tests/hermes_cli/test_web_server_fs.py b/tests/hermes_cli/test_web_server_fs.py
index 59fe0b1a37c..e3b9858b11b 100644
--- a/tests/hermes_cli/test_web_server_fs.py
+++ b/tests/hermes_cli/test_web_server_fs.py
@@ -1056,7 +1056,7 @@ def _session_detail(cwds: dict):
def test_fs_download_maps_workspace_container_to_its_sandbox(client, monkeypatch, tmp_path):
- """A persistent per-workspace container stores ``/workspace/note.txt`` under
+ """A persistent per-workspace container (cwd mount on) stores ``/workspace/note.txt`` under
``sandboxes/docker/default_ws_<digest>-<hash>/workspace``, not the shared
``default`` sandbox. The download must serve that session's file, and must
not scan every other workspace for the same relative path."""
@@ -1096,7 +1096,14 @@ def test_fs_download_maps_workspace_container_to_its_sandbox(client, monkeypatch
assert bare.content == b"shared-file"
only = client.get("/api/fs/download", params={"path": "/workspace/only.txt"})
assert only.status_code == 404
+ # Without the cwd mount a session runs in the profile container: its copy is the shared one.
+ unmounted = client.get(
+ "/api/fs/download", params={"path": "/workspace/note.txt", "session_id": "sess-ws"},
+ )
+ assert (unmounted.status_code, unmounted.content) == (200, b"shared-file")
+ # Per-workspace containers exist only with the cwd mount on.
+ monkeypatch.setenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "true")
response = client.get(
"/api/fs/download", params={"path": "/workspace/note.txt", "session_id": "sess-ws"},
)
diff --git a/tests/hermes_cli/test_web_server_fs_session_workspace.py b/tests/hermes_cli/test_web_server_fs_session_workspace.py
index cde401b110b..16521c84f11 100644
--- a/tests/hermes_cli/test_web_server_fs_session_workspace.py
+++ b/tests/hermes_cli/test_web_server_fs_session_workspace.py
@@ -1,5 +1,7 @@
"""WA-5: a Webapp file route that names a session resolves ``/workspace`` in THAT session's
-per-workspace Docker sandbox (#143655's mapping), and refuses a session it cannot see."""
+Docker sandbox (#143655's mapping), and refuses a session it cannot see. A session has its own
+per-workspace sandbox only with ``docker_mount_cwd_to_workspace`` on; without it every session
+shares the profile sandbox, and a ``*_ws_*`` copy an older build left behind is not its own."""
import shutil
from pathlib import Path
@@ -42,7 +44,8 @@ def _workspace_sandbox(sandbox: Path, cwd: Path) -> Path:
@pytest.fixture
def sandboxes(monkeypatch, tmp_path):
"""A persistent Docker backend with three ``/workspace`` copies: this session's, another
- session's, and the shared profile sandbox. ``sess-ws`` belongs to the default profile,
+ session's, and the shared profile sandbox. The cwd mount is off, so the per-workspace copies
+ are leftovers until a test turns it on (:func:`_mount_cwd`). ``sess-ws`` belongs to the default profile,
``sess-work`` to the ``work`` profile, so the default profile's lookup cannot see it."""
project, other = tmp_path / "project", tmp_path / "other"
project.mkdir()
@@ -111,7 +114,18 @@ def test_each_file_route_looks_its_session_up_once(client, sandboxes, monkeypatc
# Part 1: media streams
-def test_stream_plays_the_sessions_own_workspace_clip(client, sandboxes):
+def _mount_cwd(monkeypatch):
+ """Per-workspace sandboxes exist only with the cwd mount on. The session folders here hold no
+ files, so ``/workspace`` falls through to the sandbox copies whose ordering these tests pin;
+ Part 4 covers a real mounted folder."""
+ monkeypatch.setenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "true")
+
+
+def test_stream_plays_the_sessions_own_workspace_clip(client, sandboxes, monkeypatch):
+ # No session keeps the shared profile sandbox, as before.
+ bare = client.get("/api/files/stream", params={"path": "/workspace/clip.mp4"})
+ assert (bare.status_code, bare.content) == (200, b"shared-clip")
+ _mount_cwd(monkeypatch)
params = {"path": "/workspace/clip.mp4", "session_id": "sess-ws"}
full = client.get("/api/files/stream", params=params)
@@ -121,12 +135,10 @@ def test_stream_plays_the_sessions_own_workspace_clip(client, sandboxes):
assert (full.status_code, full.content) == (200, b"session-clip")
assert (partial.status_code, partial.content) == (206, b"session")
assert (other.status_code, other.content) == (200, b"foreign-clip")
- # No session keeps the shared profile sandbox, as before.
- bare = client.get("/api/files/stream", params={"path": "/workspace/clip.mp4"})
- assert (bare.status_code, bare.content) == (200, b"shared-clip")
def test_a_stream_ticket_for_a_session_plays_that_sessions_clip(client, sandboxes, monkeypatch):
+ _mount_cwd(monkeypatch)
monkeypatch.setattr(web_server.app.state, "ui_surface", "webapp", raising=False)
query = {"path": "/workspace/clip.mp4", "session_id": "sess-ws"}
minted = client.post("/api/files/ticket", json={"route": "stream", **query})
@@ -159,13 +171,14 @@ def test_stream_refuses_a_clip_outside_the_sessions_workspace(client, sandboxes,
@pytest.mark.parametrize("path", ["/workspace/note.txt", "file:///workspace/note.txt"])
-def test_read_text_previews_the_sessions_own_workspace_file(client, sandboxes, path):
+def test_read_text_previews_the_sessions_own_workspace_file(client, sandboxes, monkeypatch, path):
def read(**params):
return client.get("/api/fs/read-text", params={"path": path, **params})
+ bare = read()
+ _mount_cwd(monkeypatch)
own = read(session_id="sess-ws")
other = read(session_id="sess-other")
- bare = read()
assert own.status_code == 200, own.text
assert own.json()["text"] == "session note"
@@ -176,6 +189,7 @@ def test_read_text_previews_the_sessions_own_workspace_file(client, sandboxes, p
def test_a_file_url_clip_streams_from_the_sessions_sandbox(client, sandboxes, monkeypatch):
+ _mount_cwd(monkeypatch)
query = {"path": "file:///workspace/clip.mp4", "session_id": "sess-ws"}
direct = client.get("/api/files/stream", params=query)
@@ -217,7 +231,8 @@ def _notes(sandboxes):
@pytest.mark.parametrize("path", ["/workspace/note.txt", "file:///workspace/note.txt"])
-def test_save_writes_the_file_the_preview_read(client, sandboxes, path):
+def test_save_writes_the_file_the_preview_read(client, sandboxes, monkeypatch, path):
+ _mount_cwd(monkeypatch)
read = client.get("/api/fs/read-text", params={"path": path, "session_id": "sess-ws"})
saved = client.post("/api/fs/write-text", json={"path": path, "content": "edited", "session_id": "sess-ws"})
@@ -227,7 +242,10 @@ def test_save_writes_the_file_the_preview_read(client, sandboxes, path):
@pytest.mark.parametrize("path", ["/workspace/note.txt", "file:///workspace/note.txt"])
-def test_save_never_falls_back_to_the_shared_copy_once_the_sessions_copy_is_gone(client, sandboxes, path):
+def test_save_never_falls_back_to_the_shared_copy_once_the_sessions_copy_is_gone(
+ client, sandboxes, monkeypatch, path,
+):
+ _mount_cwd(monkeypatch)
(sandboxes["shared"] / "note.txt").write_text("session note", encoding="utf-8") # the same text
(sandboxes["own"] / "note.txt").unlink()
@@ -242,7 +260,10 @@ def test_save_never_falls_back_to_the_shared_copy_once_the_sessions_copy_is_gone
@pytest.mark.parametrize("path", ["/workspace/note.txt", "file:///workspace/note.txt"])
-def test_save_never_falls_back_to_the_shared_copy_once_the_sessions_sandbox_is_gone(client, sandboxes, path):
+def test_save_never_falls_back_to_the_shared_copy_once_the_sessions_sandbox_is_gone(
+ client, sandboxes, monkeypatch, path,
+):
+ _mount_cwd(monkeypatch)
read = client.get("/api/fs/read-text", params={"path": path, "session_id": "sess-ws"})
assert read.json()["text"] == "session note"
shutil.rmtree(sandboxes["own"].parent) # the session's whole sandbox, not just its file
@@ -265,6 +286,24 @@ def test_save_without_a_session_writes_where_a_bare_read_reads(client, sandboxes
assert _notes(sandboxes) == {"own": "session note", "foreign": "foreign note", "shared": "edited"}
[email protected]("flag", [None, "on"]) # "on" is not a value the terminal mounts on
[email protected]("path", ["/workspace/note.txt", "file:///workspace/note.txt"])
+def test_without_the_cwd_mount_a_session_reads_and_saves_the_profile_sandbox(
+ client, sandboxes, monkeypatch, path, flag,
+):
+ """No cwd mount: the session's container is the profile's, so a leftover ``*_ws_*`` copy is
+ neither previewed nor counted as its own (a Save there would be refused or lost)."""
+ if flag:
+ monkeypatch.setenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", flag)
+ read = client.get("/api/fs/read-text", params={"path": path, "session_id": "sess-ws"})
+ saved = client.post("/api/fs/write-text", json={"path": path, "content": "edited", "session_id": "sess-ws"})
+
+ assert (read.status_code, read.json()["text"]) == (200, "shared note"), read.text
+ assert saved.status_code == 200, saved.text
+ assert saved.json()["path"] == read.json()["path"] == str(sandboxes["shared"] / "note.txt")
+ assert _notes(sandboxes) == {"own": "session note", "foreign": "foreign note", "shared": "edited"}
+
+
@pytest.mark.parametrize("session_id", ["sess-work", "missing-session", " "])
def test_save_refuses_a_session_this_profile_does_not_own(client, sandboxes, session_id):
for path in ("/workspace/note.txt", "file:///workspace/note.txt"):