Skip to content

The agent that grows with you

fix(updater): require Hermes-owned Windows services

fangliquanflq94d0789cc590
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index dcdec3f6b9b..6ca3c2e66a5 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -788,6 +788,12 @@ def find_profile_gateway_processes(exclude_pids: set | None = None, *, strict: b
return processes
+def _is_hermes_windows_gateway_service(name: str) -> bool:
+ """Return whether an SCM service name declares Hermes ownership."""
+ normalized = "".join(char for char in name.casefold() if char.isalnum())
+ return normalized == "hermes" or normalized.startswith("hermesgateway")
+
+
def find_windows_gateway_services(
*, psutil_module=None, profile_processes: list[ProfileGatewayProcess] | None = None
) -> list[WindowsGatewayService]:
@@ -830,6 +836,8 @@ def find_windows_gateway_services(
continue
if service_pid <= 0:
raise RuntimeError(f"Running SCM service {service_name} has no valid process ID")
+ if not _is_hermes_windows_gateway_service(service_name):
+ continue
service_names_by_pid.setdefault(service_pid, set()).add(service_name)
except Exception as exc:
raise RuntimeError("SCM service enumeration failed") from exc
diff --git a/tests/hermes_cli/test_gateway.py b/tests/hermes_cli/test_gateway.py
index 50aa0fa2e39..8fd83f50cd4 100644
--- a/tests/hermes_cli/test_gateway.py
+++ b/tests/hermes_cli/test_gateway.py
@@ -1322,6 +1322,41 @@ def test_find_windows_gateway_services_rejects_transitional_ancestor(monkeypatch
)
+def test_find_windows_gateway_services_ignores_task_scheduler_ancestor(monkeypatch):
+ """A task-launched gateway is not owned by the Schedule SCM service."""
+ monkeypatch.setattr(gateway.sys, "platform", "win32")
+ profile = SimpleNamespace(profile="default", pid=300, create_time=300.0)
+
+ class FakeService:
+ def as_dict(self):
+ return {"name": "Schedule", "pid": 100, "status": "running"}
+
+ class FakeProcess:
+ def __init__(self, pid):
+ self.pid = pid
+
+ def parents(self):
+ return [FakeProcess(200), FakeProcess(100)]
+
+ def children(self, recursive=False):
+ assert self.pid == 100
+ assert recursive is True
+ return [FakeProcess(200), FakeProcess(300)]
+
+ def create_time(self):
+ return float(self.pid)
+
+ fake_psutil = SimpleNamespace(
+ win_service_iter=lambda: [FakeService()],
+ Process=FakeProcess,
+ )
+
+ assert gateway.find_windows_gateway_services(
+ psutil_module=fake_psutil,
+ profile_processes=[profile],
+ ) == []
+
+
def test_find_windows_gateway_services_rejects_shared_service_host_pid(monkeypatch):
"""A shared host PID cannot prove which service owns the gateway subtree."""
monkeypatch.setattr(gateway.sys, "platform", "win32")
@@ -1348,7 +1383,10 @@ def test_find_windows_gateway_services_rejects_shared_service_host_pid(monkeypat
return float(self.pid)
fake_psutil = SimpleNamespace(
- win_service_iter=lambda: [FakeService("ServiceA"), FakeService("ServiceB")],
+ win_service_iter=lambda: [
+ FakeService("HermesGatewayA"),
+ FakeService("HermesGatewayB"),
+ ],
Process=FakeProcess,
)