Skip to content

The agent that grows with you

fix(webapp): refuse a session Save once the session's whole sandbox is gone

unsupportedpastelsd8a472d04a2f
diff --git a/hermes_cli/web_server_files.py b/hermes_cli/web_server_files.py
index 68d5959091d..01757b1a084 100644
--- a/hermes_cli/web_server_files.py
+++ b/hermes_cli/web_server_files.py
@@ -158,16 +158,17 @@ SANDBOX_COPY_MISSING = "File is no longer in this session's workspace"
def _in_fallback_sandbox(target: Path, workspace_key: str) -> bool:
- """Whether *target* is in a Docker sandbox after the first existing one in the translator's
- order (the session's own, then the profile's shared ones). Reads fall through to those copies
- when the session's own is missing; a Save that did would change another sandbox's file."""
+ """Whether *target* is in a Docker sandbox other than the session's own, the first in the
+ translator's order (then the profile's shared ones). The session's own is that first name
+ whether or not its directory exists: reads fall through to the shared copies when the session's
+ file or its whole sandbox is gone, and a Save that did would change another sandbox's file."""
from gateway.platforms.docker_media_paths import _docker_sandbox_dir_candidates
from tools.environments.base import get_sandbox_dir
base = get_sandbox_dir() / "docker"
- sandboxes = [d for d in ((base / name).resolve(strict=False)
- for name in _docker_sandbox_dir_candidates("", workspace_key)) if d.is_dir()]
- return any(_path_is_under(sandbox, target) for sandbox in sandboxes[1:])
+ names = _docker_sandbox_dir_candidates("", workspace_key)
+ fallbacks = [(base / name).resolve(strict=False) for name in names[1:]]
+ return any(_path_is_under(sandbox, target) for sandbox in fallbacks)
@contextmanager
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 f8ab33dc464..fe7fe3a1030 100644
--- a/tests/hermes_cli/test_web_server_fs_session_workspace.py
+++ b/tests/hermes_cli/test_web_server_fs_session_workspace.py
@@ -1,6 +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."""
+import shutil
from pathlib import Path
import pytest
@@ -239,6 +240,21 @@ def test_save_never_falls_back_to_the_shared_copy_once_the_sessions_copy_is_gone
assert not (sandboxes["own"] / "note.txt").exists()
[email protected]("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):
+ 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
+
+ reread = 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 (reread.status_code, reread.json()["text"]) == (200, "shared note") # reads still fall back
+ assert (saved.status_code, saved.json()) == (409, {"detail": "File is no longer in this session's workspace"})
+ assert (sandboxes["shared"] / "note.txt").read_text(encoding="utf-8") == "shared note"
+ assert not sandboxes["own"].parent.exists()
+
+
def test_save_without_a_session_writes_where_a_bare_read_reads(client, sandboxes):
read = client.get("/api/fs/read-text", params={"path": "/workspace/note.txt"})
saved = client.post("/api/fs/write-text", json={"path": "/workspace/note.txt", "content": "edited"})