Skip to content

The agent that grows with you

refactor(gateway-windows): one death predicate, no second process scan

kshitijk4poor7e7641561b9d
diff --git a/hermes_cli/gateway_windows.py b/hermes_cli/gateway_windows.py
index 75171ea2c00..91cc7bafa4d 100644
--- a/hermes_cli/gateway_windows.py
+++ b/hermes_cli/gateway_windows.py
@@ -919,25 +919,23 @@ def _attested_pid_exited_cleanly(pid: int) -> bool:
return isinstance(data, dict) and data.get("phase") == "exited" and data.get("pid") == pid
-def attested_gateway_died(current_pids: list[int] | None = None) -> bool:
+def _attested_dead(attested: list[int], current_pids: list[int]) -> bool:
+ """The liveness rule shared by the consuming and read-only probes: attested PIDs are dead when
+ no gateway runs now and the lifecycle ledger shows no clean exit for any of them."""
+ return not current_pids and not any(_attested_pid_exited_cleanly(pid) for pid in attested)
+
+
+def attested_gateway_died(current_pids: list[int]) -> bool:
"""True when the start attestation vouches for gateway PID(s) that are gone without a clean exit.
Read-only twin of :func:`check_start_attestation` for callers that must not consume the
one-shot marker — ``hermes update`` consults it to decide whether a Desktop-owned install
- still owes a gateway cold-start (#109538). ``False`` for anything undecidable (no marker, a
- gateway running now, a clean ledger exit, or discovery failure): "unknown" must never read
+ still owes a gateway cold-start (#109538). Callers pass the liveness they already established
+ (``[]`` after their own discovery came back empty) so the process table is not scanned twice.
+ ``False`` for anything undecidable (no marker, a clean ledger exit): "unknown" must never read
as "dead"."""
attested = _attested_pids_from(_read_start_attestation())
- if not attested:
- return False
- if current_pids is None:
- try:
- from hermes_cli.gateway import find_gateway_pids
-
- current_pids = list(find_gateway_pids())
- except Exception:
- return False
- return not current_pids and not any(_attested_pid_exited_cleanly(pid) for pid in attested)
+ return bool(attested) and _attested_dead(attested, current_pids)
def check_start_attestation(current_pids: list[int] | None = None) -> str | None:
@@ -960,7 +958,7 @@ def check_start_attestation(current_pids: list[int] | None = None) -> str | None
return None
_clear_start_attestation()
- if current_pids or any(_attested_pid_exited_cleanly(pid) for pid in attested):
+ if not _attested_dead(attested, current_pids):
return None
return _format_attestation_warning(attested, data)
diff --git a/hermes_cli/update_cmd_windows.py b/hermes_cli/update_cmd_windows.py
index a314971c766..2751cf36492 100644
--- a/hermes_cli/update_cmd_windows.py
+++ b/hermes_cli/update_cmd_windows.py
@@ -758,7 +758,7 @@ def _windows_cold_start_plan() -> dict | None:
if not gateway_windows.is_installed():
return None
with _best_effort('Could not check Desktop gateway-lifecycle ownership before update: %s'):
- if _desktop_owns_gateway_lifecycle() and not gateway_windows.attested_gateway_died():
+ if _desktop_owns_gateway_lifecycle() and not gateway_windows.attested_gateway_died(current_pids=[]):
logger.debug("Skipping Windows gateway cold-start plan: Desktop owns gateway lifecycle")
return None
return {"resume_needed": True, "profiles": {}, "unmapped_pids": [], "unmapped": [], "cold_start_if_installed": True}
@@ -942,7 +942,7 @@ def _cold_start_windows_gateway_after_update() -> bool:
if list(find_gateway_pids(all_profiles=True)):
return True
with _abort_on_error("Could not re-check Desktop gateway-lifecycle ownership before cold-start"):
- if _desktop_owns_gateway_lifecycle() and not gateway_windows.attested_gateway_died():
+ if _desktop_owns_gateway_lifecycle() and not gateway_windows.attested_gateway_died(current_pids=[]):
logger.debug("Skipping Windows gateway cold-start: Desktop owns gateway lifecycle")
return True
with _abort_on_error("Could not cold-start Windows gateway after update"):
diff --git a/tests/hermes_cli/test_gateway_start_attestation.py b/tests/hermes_cli/test_gateway_start_attestation.py
index 9922f20b765..2f83db97773 100644
--- a/tests/hermes_cli/test_gateway_start_attestation.py
+++ b/tests/hermes_cli/test_gateway_start_attestation.py
@@ -210,7 +210,6 @@ def test_attested_probe_fails_closed_without_a_well_formed_dead_attestation(atte
for malformed in ('{"pids": null}', '{"pids": 555}', '["not", "a", "dict"]', "not json"):
marker.write_text(malformed, encoding="utf-8")
assert gateway_windows.attested_gateway_died(current_pids=[]) is False, malformed
- assert gateway_windows._attested_pids_from({"pids": [1, "x", 2]}) == [1, 2]
gateway_windows._write_start_attestation([555], "cold-start after update")
assert gateway_windows.attested_gateway_died(current_pids=[555]) is False # alive
diff --git a/tests/hermes_cli/test_windows_gateway_cold_start_desktop_lifecycle.py b/tests/hermes_cli/test_windows_gateway_cold_start_desktop_lifecycle.py
index 580bd0ab8bb..6b8de2486c4 100644
--- a/tests/hermes_cli/test_windows_gateway_cold_start_desktop_lifecycle.py
+++ b/tests/hermes_cli/test_windows_gateway_cold_start_desktop_lifecycle.py
@@ -101,7 +101,7 @@ def test_pause_skips_cold_start_plan_when_desktop_owns_lifecycle(monkeypatch):
hermes_gateway, "find_windows_gateway_services", lambda **_k: []
)
monkeypatch.setattr(gateway_windows, "is_installed", lambda: True)
- monkeypatch.setattr(gateway_windows, "attested_gateway_died", lambda: False)
+ monkeypatch.setattr(gateway_windows, "attested_gateway_died", lambda **_k: False)
monkeypatch.setattr(update_cmd, "_desktop_owns_gateway_lifecycle", lambda: True)
monkeypatch.setattr(update_cmd_windows, "_desktop_owns_gateway_lifecycle", lambda: True)
@@ -135,7 +135,7 @@ def test_cold_start_aborts_when_desktop_owns_lifecycle(monkeypatch):
monkeypatch.setattr(cli_main, "_is_windows", lambda: True)
monkeypatch.setattr(main_install_repair, "_is_windows", lambda: True)
monkeypatch.setattr(hermes_gateway, "find_gateway_pids", lambda **_k: [])
- monkeypatch.setattr(gateway_windows, "attested_gateway_died", lambda: False)
+ monkeypatch.setattr(gateway_windows, "attested_gateway_died", lambda **_k: False)
monkeypatch.setattr(update_cmd, "_desktop_owns_gateway_lifecycle", lambda: True)
monkeypatch.setattr(update_cmd_windows, "_desktop_owns_gateway_lifecycle", lambda: True)
monkeypatch.setattr(
@@ -186,4 +186,4 @@ def test_attested_dead_gateway_survives_desktop_ownership_and_marker_is_consumed
assert spawned == [1]
assert "Gateway started via cold-start after update (PID: 4242)" in capsys.readouterr().out
assert not marker.exists() # consumed by the spawn
- assert gateway_windows.attested_gateway_died() is False
+ assert gateway_windows.attested_gateway_died(current_pids=[]) is False