Skip to content

The agent that grows with you

test(pm): re-drop the legacy shim-quarantine live test the merge resurrected

ethernet5c2ee8972d75
diff --git a/tests/hermes_cli/test_shim_fail_closed_windows_live.py b/tests/hermes_cli/test_shim_fail_closed_windows_live.py
deleted file mode 100644
index c72587e3582..00000000000
--- a/tests/hermes_cli/test_shim_fail_closed_windows_live.py
+++ /dev/null
@@ -1,185 +0,0 @@
-"""LIVE Windows E2E for the fail-closed shim quarantine (#87331).
-
-Runs ONLY on a real Windows host (the on-demand ``windows-venv-e2e.yml``
-lane). Reproduces the REAL lock shape from the field report: a process
-holding ``hermes.exe`` open WITHOUT FILE_SHARE_DELETE, exactly like a
-running launcher — then proves the strict quarantine refuses before any
-installer runs, and that the non-contended path still installs.
-
-No mocks: real files, a real child process holding a real Windows handle,
-the real rename attempt hitting the real sharing violation.
-"""
-
-from __future__ import annotations
-
-import os
-import queue
-import subprocess
-import sys
-import threading
-import time
-from pathlib import Path
-
-import pytest
-from hermes_cli import main_install_repair
-
-pytestmark = pytest.mark.platforms("windows")
-
-PROJECT_ROOT = Path(__file__).resolve().parents[2]
-
-
-def _wait_until(predicate, timeout: float = 15.0, interval: float = 0.05) -> bool:
- deadline = time.monotonic() + timeout
- while time.monotonic() < deadline:
- if predicate():
- return True
- time.sleep(interval)
- return bool(predicate())
-
-
-def _readline(stream, timeout: float = 60.0) -> str:
- """``stream.readline()`` bounded by *timeout* (a hung child fails, not hangs)."""
- got: queue.Queue = queue.Queue()
- threading.Thread(target=lambda: got.put(stream.readline()), daemon=True).start()
- try:
- return got.get(timeout=timeout)
- except queue.Empty:
- return ""
-
-
-def _rename_round_trips(path: Path) -> bool:
- """True once no handle blocks a rename of *path* (the holder is gone)."""
- probe = path.with_name(path.name + ".probe")
- try:
- os.rename(path, probe)
- except OSError:
- return False
- os.rename(probe, path)
- return True
-
-# Child that opens a file with GENERIC_READ and NO FILE_SHARE_DELETE —
-# the exact sharing mode a running .exe image / desktop backend exhibits.
-_HOLDER_CODE = r"""
-import ctypes, sys, time
-GENERIC_READ = 0x80000000
-FILE_SHARE_READ = 0x1 # note: NO FILE_SHARE_DELETE
-OPEN_EXISTING = 3
-h = ctypes.windll.kernel32.CreateFileW(
- sys.argv[1], GENERIC_READ, FILE_SHARE_READ, None, OPEN_EXISTING, 0, None
-)
-if h == -1 or h == 0xFFFFFFFF:
- print("OPEN_FAILED", flush=True)
- sys.exit(1)
-print("HOLDING", flush=True)
-time.sleep(120)
-"""
-
-
-def held_shim(tmp_path: Path):
- scripts = tmp_path / "venv" / "Scripts"
- scripts.mkdir(parents=True)
- shim = scripts / "hermes.exe"
- shim.write_bytes(b"MZ fake shim")
- (scripts / "hermes-gateway.exe").write_bytes(b"MZ fake shim")
- holder = subprocess.Popen(
- [sys.executable, "-c", _HOLDER_CODE, str(shim)],
- stdout=subprocess.PIPE,
- text=True,
- )
- line = _readline(holder.stdout).strip()
- if line != "HOLDING":
- holder.kill()
- pytest.fail(f"lock-holder child failed: {line!r}")
- yield scripts, shim
- holder.kill()
- holder.wait()
-
-
-def test_locked_shim_really_cannot_be_renamed(held_shim):
- """Premise check: the no-FILE_SHARE_DELETE handle blocks rename."""
- _scripts, shim = held_shim
- with pytest.raises(OSError):
- os.rename(shim, shim.with_name("hermes.exe.old.premise"))
-
-
-def test_strict_quarantine_refuses_against_real_lock(held_shim, monkeypatch):
- import hermes_cli.main as cli_main
- import hermes_cli.main_install_repair as hermes_cli_main_install_repair
-
- scripts, _shim = held_shim
- install_ran: list = []
- monkeypatch.setattr(
- cli_main,
- "_run_install_with_heartbeat",
- lambda cmd, env=None: install_ran.append(cmd),
- )
- monkeypatch.setattr(
- hermes_cli_main_install_repair,
- "_run_install_with_heartbeat",
- lambda cmd, env=None: install_ran.append(cmd),
- )
-
- with pytest.raises(main_install_repair.ShimQuarantineError) as exc_info:
- main_install_repair._run_quarantined_install(
- ["would-be", "uv", "pip", "install"],
- scripts_dir=scripts,
- strict_quarantine=True,
- )
-
- assert install_ran == [], "installer ran against a contended venv"
- assert "hermes.exe" in exc_info.value.failed_shims
- # The unlocked sibling's rename was rolled back — venv untouched.
- assert (scripts / "hermes-gateway.exe").exists()
- assert not list(scripts.glob("*.old.*"))
-
-
-def test_recovery_installer_refuses_against_real_lock(held_shim, monkeypatch):
- import hermes_cli._install_repair as ir
-
- scripts, _shim = held_shim
- monkeypatch.setattr(ir, "_venv_scripts_dir", lambda root: scripts)
- run_calls: list = []
- monkeypatch.setattr(ir.subprocess, "run", lambda *a, **k: run_calls.append(a))
-
- with pytest.raises(ir.ShimQuarantineError):
- ir._run_install_cmd(["fake"], env=None, root=scripts.parent.parent)
- assert run_calls == []
-
-
-def test_release_then_strict_quarantine_succeeds(tmp_path, monkeypatch):
- """After the holder exits, the same strict path proceeds normally."""
- import hermes_cli.main as cli_main
- import hermes_cli.main_install_repair as hermes_cli_main_install_repair
-
- scripts = tmp_path / "venv" / "Scripts"
- scripts.mkdir(parents=True)
- (scripts / "hermes.exe").write_bytes(b"MZ fake shim")
-
- holder = subprocess.Popen(
- [sys.executable, "-c", _HOLDER_CODE, str(scripts / "hermes.exe")],
- stdout=subprocess.PIPE,
- text=True,
- )
- assert _readline(holder.stdout).strip() == "HOLDING"
- holder.kill()
- holder.wait()
- # The handle is released when the kernel tears the (possibly trampolined)
- # holder down, not when wait() returns.
- assert _wait_until(lambda: _rename_round_trips(scripts / "hermes.exe")), "holder handle never released"
-
- install_ran: list = []
- monkeypatch.setattr(
- cli_main,
- "_run_install_with_heartbeat",
- lambda cmd, env=None: install_ran.append(cmd),
- )
- monkeypatch.setattr(
- hermes_cli_main_install_repair,
- "_run_install_with_heartbeat",
- lambda cmd, env=None: install_ran.append(cmd),
- )
- main_install_repair._run_quarantined_install(
- ["fake"], scripts_dir=scripts, strict_quarantine=True
- )
- assert install_ran == [["fake"]]