Skip to content

The agent that grows with you

fix(webapp): return a clear error when the SSH file host is down

Adolaniumdbe74c61df35
diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.py
index 938c5eaa4d6..d61f6a3a375 100644
--- a/hermes_cli/web_routers/files.py
+++ b/hermes_cli/web_routers/files.py
@@ -294,8 +294,14 @@ def _fs_git_branch(cwd: str) -> str:
def _fs_backend(profile: Optional[str] = None):
- """Return the profile's SSH workspace adapter, or None for host-local FS."""
- from hermes_cli.ssh_workspace_fs import get_ssh_workspace_fs
+ """Return the profile's SSH workspace adapter, or None for host-local FS.
+
+ A refused connection fails while the adapter is opened, before any path is
+ read. That is the same 503 the image route returns (``SSH workspace is
+ unavailable``), not an escaped 500. A missing file or a permission error
+ comes from an adapter that already connected, and keeps its own status.
+ """
+ from hermes_cli.ssh_workspace_fs import SshWorkspaceFsError, get_ssh_workspace_fs
with _profile_scope(profile):
terminal = dict(load_config().get("terminal") or {})
@@ -309,7 +315,10 @@ def _fs_backend(profile: Optional[str] = None):
if not terminal.get(key) and profile_env.get(env_name):
terminal[key] = profile_env[env_name]
profile_key = (profile or "current").strip().lower() or "current"
- return get_ssh_workspace_fs(profile_key, terminal)
+ try:
+ return get_ssh_workspace_fs(profile_key, terminal)
+ except SshWorkspaceFsError as exc:
+ _raise_fs_backend_error(exc)
def _chat_image_dir(home: Path) -> Path:
diff --git a/tests/hermes_cli/test_web_server_fs.py b/tests/hermes_cli/test_web_server_fs.py
index d58bf54ae87..7876f1b912a 100644
--- a/tests/hermes_cli/test_web_server_fs.py
+++ b/tests/hermes_cli/test_web_server_fs.py
@@ -685,6 +685,58 @@ def test_a_browser_paste_reads_back_while_the_ssh_machine_is_unreachable(client,
assert (response.status_code, response.json()) == (503, {"detail": "SSH workspace is unavailable"})
+def test_a_down_ssh_host_is_unavailable_on_workspace_file_routes(client, monkeypatch, tmp_path):
+ """List, read, git-root and the default folder answer 503 when the SSH host is down, the same
+ error the image route already returns. A file that is missing or unreadable on a host that did
+ connect is not that error, and a workspace that is not SSH still reads this machine."""
+ note = tmp_path / "note.txt"
+ note.write_text("on this machine")
+ local = client.get("/api/fs/read-text", params={"path": str(note)})
+ assert (local.status_code, local.json()["text"]) == (200, "on this machine")
+
+ class UnreachableSsh:
+ def __init__(self, **_options):
+ raise ConnectionRefusedError("ssh: connect to host 127.0.0.1 port 1: Connection refused")
+
+ monkeypatch.setattr(ssh_workspace_fs, "SSHEnvironment", UnreachableSsh)
+ monkeypatch.setattr(ssh_workspace_fs, "_BACKENDS", {})
+ monkeypatch.setattr(
+ file_routes, "load_config",
+ lambda: {"terminal": {"backend": "ssh", "ssh_host": "127.0.0.1", "ssh_user": "dev", "ssh_port": 1}},
+ )
+ monkeypatch.setattr(file_routes, "load_env", dict)
+
+ unavailable = (503, {"detail": "SSH workspace is unavailable"})
+ for path, params in (
+ ("/api/fs/list", {"path": "/srv/repos"}),
+ ("/api/fs/read-text", {"path": "/srv/repos/README.md"}),
+ ("/api/fs/git-root", {"path": "/srv/repos"}),
+ ("/api/fs/default-cwd", {}),
+ ):
+ response = client.get(path, params=params)
+ assert (response.status_code, response.json()) == unavailable, path
+ assert "Traceback" not in response.text
+
+ class Reachable:
+ def list_dir(self, path, _hidden):
+ code = "EACCES" if path.endswith("secret") else "ENOENT"
+ return {"entries": [], "error": code}
+
+ def read_bytes(self, path, *, max_bytes, read_limit=None):
+ code = "EACCES" if path.endswith("secret") else "ENOENT"
+ raise ssh_workspace_fs.SshWorkspaceFsError(code)
+
+ monkeypatch.setattr(file_routes, "_fs_backend", lambda profile=None: Reachable())
+ missing = client.get("/api/fs/list", params={"path": "/srv/missing"})
+ denied = client.get("/api/fs/list", params={"path": "/srv/secret"})
+ assert (missing.status_code, missing.json()) == (200, {"entries": [], "error": "ENOENT"})
+ assert (denied.status_code, denied.json()) == (200, {"entries": [], "error": "EACCES"})
+ missing_text = client.get("/api/fs/read-text", params={"path": "/srv/missing.txt"})
+ denied_text = client.get("/api/fs/read-text", params={"path": "/srv/secret"})
+ assert (missing_text.status_code, missing_text.json()) == (404, {"detail": "File not found"})
+ assert (denied_text.status_code, denied_text.json()) == (403, {"detail": "File is not readable"})
+
+
class _NotOnTheSshMachine:
"""An SSH workspace that has none of the files a test asks it for; it records each ask."""