fix(web_server): detect replaced venvs with a marker file — inode snapshots miss ext4 inode reuse

Follow-up on the cherry-picked #82644: the (st_dev, st_ino) snapshot of
site-packages does not survive contact with ext4 — a recreated directory
routinely REUSES the freed inode, so the exact reported repro
(rm -rf venv && uv venv) passed the intact check undetected. Proven live
during salvage: the E2E's replaced venv came back with the identical
inode and runtimeIntact stayed true.

Primary identity is now a marker file written into site-packages when
the SSH owner nonce activates: it deterministically dies with the old
tree on ANY replacement (same or different Python version) and survives
in-place pip/uv installs (no false stales). The stat snapshot remains as
the fallback for read-only site-packages, where it still catches
cross-device moves and version-bump path changes. Client classifier
semantics unchanged: only an explicit runtimeIntact:false rejects, so
older remotes stay compatible.

Three new tests: recreated-venv-with-reused-inode (the live-proven
case), in-place-install stays intact, read-only fallback arms the stat
tier.
This commit is contained in:
Teknium
2026-08-26 05:06:38 -07:00
parent 8624c1e8f7
commit cddb908aab
2 changed files with 110 additions and 5 deletions
+31 -5
View File
@@ -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
@@ -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)