Skip to content

The agent that grows with you

fix(webapp): log why the SSH workspace is unavailable and drop the dead mapping

Adolaniume749ca3638d3
diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.py
index d61f6a3a375..2ee8edf6fae 100644
--- a/hermes_cli/web_routers/files.py
+++ b/hermes_cli/web_routers/files.py
@@ -8,6 +8,7 @@ import asyncio
import base64
import binascii
import contextlib
+import logging
import mimetypes
import os
import re
@@ -44,6 +45,7 @@ from hermes_cli.web_models import (
)
router = APIRouter()
+logger = logging.getLogger(__name__)
# Late-bound so a test's monkeypatch on the owning module wins at call time.
_profile_scope = late("_profile_scope", "hermes_cli.web_server_profiles")
@@ -296,10 +298,12 @@ 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.
- 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.
+ Only an uncached adapter dials the host: the profile's first file request since the server
+ started, or the first after a setting the adapter is built from changes. When that dial fails,
+ or the settings cannot dial at all (no host or user, no ``ssh`` on PATH), the request answers
+ 503 ``SSH workspace is unavailable`` and the log names the cause the 503 hides. A cached
+ adapter is not re-dialed here: a host that goes down after it connected fails the read itself,
+ which each route answers on its own.
"""
from hermes_cli.ssh_workspace_fs import SshWorkspaceFsError, get_ssh_workspace_fs
@@ -318,6 +322,7 @@ def _fs_backend(profile: Optional[str] = None):
try:
return get_ssh_workspace_fs(profile_key, terminal)
except SshWorkspaceFsError as exc:
+ logger.warning("SSH workspace for profile %s is unavailable: %s", profile_key, exc.__cause__ or exc)
_raise_fs_backend_error(exc)
@@ -354,15 +359,10 @@ def _is_host_file(path: str, profile: Optional[str]) -> bool:
def _remote_fs_backend(path: str, profile: Optional[str]):
"""The SSH workspace reader for *path*, or None when this host owns it. The host's pastes and
staged uploads are checked before the SSH machine is dialed, so they read back while that
- machine is down; other paths answer 503 until it is back."""
- from hermes_cli.ssh_workspace_fs import SshWorkspaceFsError
-
+ machine is down; every other path is the SSH machine's to answer (see ``_fs_backend``)."""
if _is_host_file(path, profile):
return None
- try:
- return _fs_backend(profile)
- except SshWorkspaceFsError as exc:
- _raise_fs_backend_error(exc)
+ return _fs_backend(profile)
def _raise_fs_backend_error(exc: Exception) -> None:
diff --git a/tests/hermes_cli/test_web_server_fs.py b/tests/hermes_cli/test_web_server_fs.py
index 7876f1b912a..42317fc7490 100644
--- a/tests/hermes_cli/test_web_server_fs.py
+++ b/tests/hermes_cli/test_web_server_fs.py
@@ -685,20 +685,34 @@ 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")
+def test_a_down_ssh_host_is_unavailable_on_workspace_file_routes(client, monkeypatch, caplog):
+ """The profile's first file request opens its SSH adapter, which dials the host. While the host
+ refuses, list, read-text, write-text, git-root and the default folder answer 503, the error the
+ image route and download already give, and the log names the refusal. Once the host answers,
+ the next request dials again, and a folder that is not there or a file the SSH user cannot read
+ keeps its own answer instead of that 503."""
+ from tools.environments.base import EnvironmentConnectionError
+
+ class SshHost:
+ """The SSH machine: refuses the dial while ``down``; once up, it has no /srv/missing and
+ its SSH user cannot read /srv/secret."""
+
+ down = True
+ cwd, timeout, _remote_home = "/srv/repos", 30, "/home/dev"
- class UnreachableSsh:
def __init__(self, **_options):
- raise ConnectionRefusedError("ssh: connect to host 127.0.0.1 port 1: Connection refused")
+ if SshHost.down:
+ raise EnvironmentConnectionError(
+ "SSH connection failed: ssh: connect to host 127.0.0.1 port 1: Connection refused")
- monkeypatch.setattr(ssh_workspace_fs, "SSHEnvironment", UnreachableSsh)
+ def execute(self, command, **_options):
+ code, rc = ("EACCES", 46) if "/srv/secret" in command else ("ENOENT", 44)
+ return {"returncode": rc, "output": f"__HERMES_FS_ERROR__:{code}\n"}
+
+ def cleanup(self):
+ pass
+
+ monkeypatch.setattr(ssh_workspace_fs, "SSHEnvironment", SshHost)
monkeypatch.setattr(ssh_workspace_fs, "_BACKENDS", {})
monkeypatch.setattr(
file_routes, "load_config",
@@ -707,32 +721,24 @@ def test_a_down_ssh_host_is_unavailable_on_workspace_file_routes(client, monkeyp
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())
+ with caplog.at_level("WARNING", logger=file_routes.__name__):
+ for response in (
+ client.get("/api/fs/list", params={"path": "/srv/repos"}),
+ client.get("/api/fs/read-text", params={"path": "/srv/repos/README.md"}),
+ client.post("/api/fs/write-text", json={"path": "/srv/repos/README.md", "content": "x"}),
+ client.get("/api/fs/git-root", params={"path": "/srv/repos"}),
+ client.get("/api/fs/default-cwd"),
+ ):
+ assert (response.status_code, response.json()) == unavailable, response.request.url
+ assert "Connection refused" in caplog.text
+
+ SshHost.down = False
missing = client.get("/api/fs/list", params={"path": "/srv/missing"})
denied = client.get("/api/fs/list", params={"path": "/srv/secret"})
+ missing_text = client.get("/api/fs/read-text", params={"path": "/srv/missing/a.txt"})
+ denied_text = client.get("/api/fs/read-text", 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"})