diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py index 7c3fc553c5..e4f1102a26 100644 --- a/hermes_cli/web_server.py +++ b/hermes_cli/web_server.py @@ -545,6 +545,7 @@ _SESSION_TOKEN = _resolve_session_token() _SESSION_HEADER_NAME = "X-Hermes-Session-Token" _SSH_OWNER_NONCE: Optional[str] = None _SSH_RUNTIME_PURELIB: Optional[Tuple[str, int, int]] = None +_SSH_RUNTIME_MARKER: Optional[str] = None def _apply_ssh_session_token(token: str) -> None: @@ -554,27 +555,52 @@ def _apply_ssh_session_token(token: str) -> None: def _apply_ssh_owner_nonce(nonce: Optional[str]) -> None: - global _SSH_OWNER_NONCE, _SSH_RUNTIME_PURELIB + global _SSH_OWNER_NONCE, _SSH_RUNTIME_PURELIB, _SSH_RUNTIME_MARKER _SSH_OWNER_NONCE = nonce _SSH_RUNTIME_PURELIB = None + _SSH_RUNTIME_MARKER = None if nonce: try: purelib = sysconfig.get_paths()["purelib"] - stat = os.stat(purelib) - _SSH_RUNTIME_PURELIB = (purelib, stat.st_dev, stat.st_ino) except (KeyError, OSError): + return + # Primary identity: a marker FILE written into site-packages now. + # A replaced venv (rm -rf && recreate — same OR different Python + # version) loses the marker deterministically, while pip installs + # into the live venv leave it untouched (no false stales). A bare + # (dev, ino) snapshot of the directory is NOT sufficient on its + # own: ext4 reuses directory inodes immediately, so the exact + # reported repro (`rm -rf venv && uv venv`) can land on the same + # inode and pass undetected (proven live during salvage). + try: + marker = os.path.join(purelib, f".hermes-ssh-runtime-{nonce}") + with open(marker, "w", encoding="utf-8") as fh: + fh.write(f"pid={os.getpid()}\n") + _SSH_RUNTIME_MARKER = marker + except OSError: + pass # read-only site-packages — fall back to the stat snapshot + try: + st = os.stat(purelib) + _SSH_RUNTIME_PURELIB = (purelib, st.st_dev, st.st_ino) + except OSError: pass def _ssh_runtime_intact() -> bool: + # Marker file is the deterministic signal when we managed to write one. + if _SSH_RUNTIME_MARKER is not None: + return os.path.isfile(_SSH_RUNTIME_MARKER) + # Fallback (read-only site-packages): directory identity snapshot. + # Weaker — inode reuse can mask a same-filesystem recreate — but still + # catches cross-device moves and version-bump path changes. if _SSH_RUNTIME_PURELIB is None: return True purelib, device, inode = _SSH_RUNTIME_PURELIB try: - stat = os.stat(purelib) + st = os.stat(purelib) except OSError: return False - return (stat.st_dev, stat.st_ino) == (device, inode) + return (st.st_dev, st.st_ino) == (device, inode) # In-browser Chat tab (/chat, /api/pty, /api/ws, …). Always enabled: the # desktop app and the dashboard's own Chat tab both drive the agent over the diff --git a/tests/hermes_cli/test_ssh_ownership_endpoint.py b/tests/hermes_cli/test_ssh_ownership_endpoint.py index a8bb58d5e5..ad8d21d447 100644 --- a/tests/hermes_cli/test_ssh_ownership_endpoint.py +++ b/tests/hermes_cli/test_ssh_ownership_endpoint.py @@ -29,6 +29,7 @@ def test_ssh_ownership_reports_replaced_runtime(monkeypatch): token = "t" * 64 monkeypatch.setattr(web_server, "_SESSION_TOKEN", token) monkeypatch.setattr(web_server, "_SSH_OWNER_NONCE", "0123456789abcdef") + monkeypatch.setattr(web_server, "_SSH_RUNTIME_MARKER", None) monkeypatch.setattr(web_server, "_SSH_RUNTIME_PURELIB", ("/venv/site-packages", 10, 20)) monkeypatch.setattr(web_server.os, "stat", lambda _path: type("Stat", (), {"st_dev": 10, "st_ino": 21})()) client = TestClient(web_server.app) @@ -39,6 +40,84 @@ def test_ssh_ownership_reports_replaced_runtime(monkeypatch): assert response.json()["runtimeIntact"] is False +def test_ssh_runtime_marker_detects_recreated_venv_even_with_reused_inode( + tmp_path, monkeypatch +): + """The exact #82429 repro: rm -rf venv && recreate. On ext4 the new + site-packages directory routinely REUSES the old inode (proven live + during salvage), so the stat snapshot alone reports intact. The marker + file is the deterministic tier: it dies with the old tree.""" + purelib = tmp_path / "venv" / "lib" / "site-packages" + purelib.mkdir(parents=True) + monkeypatch.setattr( + web_server.sysconfig, + "get_paths", + lambda *a, **k: {"purelib": str(purelib)}, + ) + + web_server._apply_ssh_owner_nonce("0123456789abcdef") + try: + assert web_server._ssh_runtime_intact() is True + + # Replace the venv; the recreated directory may reuse the inode. + import shutil + + shutil.rmtree(tmp_path / "venv") + purelib.mkdir(parents=True) + + assert web_server._ssh_runtime_intact() is False, ( + "marker tier must catch a recreated venv regardless of inode reuse" + ) + finally: + web_server._apply_ssh_owner_nonce(None) + + +def test_ssh_runtime_marker_survives_in_place_installs(tmp_path, monkeypatch): + """pip/uv installs INTO the live venv must not read as a replacement.""" + purelib = tmp_path / "venv" / "lib" / "site-packages" + purelib.mkdir(parents=True) + monkeypatch.setattr( + web_server.sysconfig, + "get_paths", + lambda *a, **k: {"purelib": str(purelib)}, + ) + + web_server._apply_ssh_owner_nonce("0123456789abcdef") + try: + (purelib / "newpkg").mkdir() # a package landing in the live venv + assert web_server._ssh_runtime_intact() is True + finally: + web_server._apply_ssh_owner_nonce(None) + + +def test_ssh_runtime_readonly_purelib_falls_back_to_stat(tmp_path, monkeypatch): + """When site-packages is unwritable the marker can't be placed; the + stat-snapshot fallback still arms (weaker, never a false stale).""" + purelib = tmp_path / "venv" / "lib" / "site-packages" + purelib.mkdir(parents=True) + monkeypatch.setattr( + web_server.sysconfig, + "get_paths", + lambda *a, **k: {"purelib": str(purelib)}, + ) + real_open = open + + def refuse_marker(path, *a, **k): + if ".hermes-ssh-runtime-" in str(path): + raise OSError(30, "Read-only file system") + return real_open(path, *a, **k) + + monkeypatch.setattr("builtins.open", refuse_marker) + + web_server._apply_ssh_owner_nonce("0123456789abcdef") + try: + assert web_server._SSH_RUNTIME_MARKER is None + assert web_server._SSH_RUNTIME_PURELIB is not None + assert web_server._ssh_runtime_intact() is True + finally: + web_server._apply_ssh_owner_nonce(None) + + def test_ssh_ownership_endpoint_is_absent_without_owner_nonce(monkeypatch): token = "t" * 64 monkeypatch.setattr(web_server, "_SESSION_TOKEN", token)