Skip to content

The agent that grows with you

test: restored Windows live tests follow the verified gateway-identity contract

teknium14adb4d5395fb
diff --git a/tests/gateway/test_control_socket_windows_live.py b/tests/gateway/test_control_socket_windows_live.py
index d2c4fdf32d7..723a98fef5a 100644
--- a/tests/gateway/test_control_socket_windows_live.py
+++ b/tests/gateway/test_control_socket_windows_live.py
@@ -13,7 +13,8 @@ Proves, on windows-latest:
3. ``collect_fleet_versions()`` prefers the socket (``source: socket``).
4. After the server process is force-killed, the client returns None
(FileNotFoundError on the pipe — no stale-file hazard on Windows) and
- consumers fall back to the state-file/scan layer.
+ consumers fall back to the state-file layer, which classifies only a
+ live-verified gateway PID (#110420).
"""
from __future__ import annotations
@@ -118,6 +119,7 @@ def test_named_pipe_identify_status_and_fleet_consumer(live_server, monkeypatch)
assert fleet[0]["pid"] == server_pid
[email protected]_gateway_lookalike
def test_pipe_gone_after_kill_falls_back(live_server, monkeypatch):
proc, home, server_pid = live_server
from gateway.control_socket import identify_gateway
@@ -128,15 +130,13 @@ def test_pipe_gone_after_kill_falls_back(live_server, monkeypatch):
assert identify_gateway(home, timeout=2.0) is None
- # Consumer falls back to the state file (live pid = this test process)
+ # Consumer falls back to the state file. That file is a claim, not an
+ # identity: its sha classifies a row only when live_gateway_pid_for_home
+ # verifies the PID as this home's gateway via its live command line
+ # (#110420). A real process wearing a `gateway run` argv stands in for
+ # a gateway that lost its pipe.
import hermes_cli.update_receipt as ur
- (home / "gateway_state.json").write_text(
- json.dumps(
- {"pid": os.getpid(), "code_sha": "OLD", "kind": "hermes-gateway"}
- ),
- encoding="utf-8",
- )
monkeypatch.setattr(
"hermes_cli.build_info.get_code_identity",
lambda refresh=False: {"sha": "NEW", "version": "t"},
@@ -145,7 +145,42 @@ def test_pipe_gone_after_kill_falls_back(live_server, monkeypatch):
monkeypatch.setattr(
"hermes_cli.profiles._get_profiles_root", lambda: home / "no-profiles"
)
+
+ def _write_state(pid: int) -> None:
+ (home / "gateway_state.json").write_text(
+ json.dumps(
+ {
+ "pid": pid, "gateway_state": "running",
+ "code_sha": "OLD", "kind": "hermes-gateway",
+ }
+ ),
+ encoding="utf-8",
+ )
+
+ standin = subprocess.Popen(
+ [sys.executable, "-c", "import time; time.sleep(120)", "hermes", "gateway", "run"],
+ stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ )
+ try:
+ time.sleep(0.5)
+ assert standin.poll() is None
+ _write_state(standin.pid)
+ fleet = ur.collect_fleet_versions()
+ assert len(fleet) == 1, fleet
+ assert "source" not in fleet[0]
+ assert fleet[0]["pid"] == standin.pid
+ assert fleet[0]["code_sha"] == "OLD"
+ assert fleet[0]["state"] == "stale"
+ finally:
+ _kill_tree(standin)
+
+ # A live NON-gateway writer (this pytest process) stays visible, but its
+ # self-reported sha must never classify the row.
+ _write_state(os.getpid())
fleet = ur.collect_fleet_versions()
assert len(fleet) == 1, fleet
assert "source" not in fleet[0]
- assert fleet[0]["state"] == "stale"
+ assert fleet[0]["pid"] == os.getpid()
+ assert fleet[0]["code_sha"] is None
+ assert fleet[0]["state"] == "unknown"
diff --git a/tests/hermes_cli/test_plan_reconciliation_windows_live.py b/tests/hermes_cli/test_plan_reconciliation_windows_live.py
index a53417750f2..3f6e8825ca9 100644
--- a/tests/hermes_cli/test_plan_reconciliation_windows_live.py
+++ b/tests/hermes_cli/test_plan_reconciliation_windows_live.py
@@ -4,12 +4,14 @@ Real processes with real Hermes-shaped argv, real inventory collection
(PID-file discovery + supervisor detection on REAL Windows), real
reconciliation. No mocks on the components under test.
- 1. Spawn a real process registered as a profile gateway (PID file + state
- file in a temp HERMES_HOME) — real collect_runtime_inventory() must find
- it, classify supervisor=manual, mechanism id 'manual'.
+ 1. Spawn a real process with a `gateway run` command line registered as the
+ profile gateway (state file in a temp HERMES_HOME) — real
+ collect_runtime_inventory() must verify and find it, classify
+ supervisor=manual, mechanism id 'manual' (machine id, not display string).
2. Reconcile with bookkeeping that MISSES it -> unaccounted + escalation.
3. Reconcile with it in killed_pids -> 'stopped', no escalation.
- 4. Machine-id contract holds on Windows (no display strings leak in).
+ 4. A running record naming a live NON-gateway process is not a runtime
+ (verified identity, not bare PID existence — #109680).
"""
import io, contextlib, json, os, subprocess, sys, tempfile, time
from pathlib import Path
@@ -19,7 +21,8 @@ sys.path.insert(0, str(WORKTREE))
import pytest
-pytestmark = pytest.mark.windows_only
+# The stand-in wears a `gateway run` argv; the test spawns and reaps it itself.
+pytestmark = [pytest.mark.windows_only, pytest.mark.spawns_gateway_lookalike]
def test_plan_reconciliation_live_windows(tmp_path, monkeypatch):
@@ -27,14 +30,22 @@ def test_plan_reconciliation_live_windows(tmp_path, monkeypatch):
home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
- # Real live process standing in for a manual gateway
+ # Real live process standing in for a manual gateway. The inventory's
+ # state-file fallback proves identity through live_gateway_pid_for_home,
+ # which requires a live `gateway run` command line (#109680), so the
+ # stand-in wears one; a bare sleeper is recorded too and must NOT count.
child = subprocess.Popen(
+ [sys.executable, "-c", "import time; time.sleep(120)", "hermes", "gateway", "run"],
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
+ )
+ foreign = subprocess.Popen(
[sys.executable, "-c", "import time; time.sleep(120)"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
try:
time.sleep(0.5)
assert child.poll() is None
+ assert foreign.poll() is None
import psutil
@@ -89,7 +100,21 @@ def test_plan_reconciliation_live_windows(tmp_path, monkeypatch):
mine2 = [o for o in outcomes2 if o["pid"] == child.pid]
assert mine2 and mine2[0]["outcome"] == "stopped"
assert report_unaccounted_runtimes(outcomes2) is False
+
+ # 4. The same running record naming a live NON-gateway process (a
+ # recycled PID) is not a runtime: bare liveness is not identity.
+ (home / "gateway_state.json").write_text(json.dumps({
+ "pid": foreign.pid,
+ "create_time": psutil.Process(foreign.pid).create_time(),
+ "gateway_state": "running",
+ "kind": "hermes-gateway",
+ "code_sha": "f" * 40,
+ }), encoding="utf-8")
+ plan2 = collect_runtime_inventory()
+ assert [r for r in plan2.runtimes if r.pid == foreign.pid] == [], plan2.runtimes
finally:
- if child.poll() is None:
- child.kill()
- child.wait()
+ for proc in (child, foreign):
+ if proc.poll() is None:
+ # /T: uv's venv python.exe is a trampoline; kill the real interpreter too.
+ subprocess.run(["taskkill", "/PID", str(proc.pid), "/T", "/F"], capture_output=True)
+ proc.wait()