Skip to content

The agent that grows with you

fix(desktop): don't reap the healthy standalone gateway on Windows desktop startup

butbutbutbutbutbut1ec4b569f253
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index f8d6c0d153a..b20b1731b85 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -2288,10 +2288,19 @@ def _reap_unsupervised_gateway_orphans(extra_exclude: set | None = None) -> bool
# the Scheduled-Task bootstrap's argv (``gateway run``) matches the
# gateway scan — killing that bootstrap takes the detached gateway it
# spawned down with it.
+ #
+ # Probe the record with cleanup_stale=False so the sweep never deletes
+ # the registration it is about to use as exclusion evidence. With the
+ # default cleanup, a record that fails liveness validation is unlinked
+ # mid-sweep; the recorded PID then never joins the exclusion set, and a
+ # healthy standalone gateway (no service supervisor — e.g. `hermes
+ # gateway run` on Windows) is hard-killed by the sweep. On Windows
+ # SIGTERM is TerminateProcess, so the gateway's own planned-stop watcher
+ # never gets a chance to shut down gracefully.
try:
from gateway.status import get_running_pid
- recorded = get_running_pid()
+ recorded = get_running_pid(cleanup_stale=False)
if recorded and recorded > 0:
own.add(recorded)
try:
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index d0e8911d394..6e9f56e8306 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -471,10 +471,23 @@ async def _lifespan(app: "FastAPI"):
# the old gateway to launchd (PPID=1). It keeps holding the QQ
# WebSocket, and a newly forked gateway then races the same credential,
# splitting messages across parallel session trees (#77276).
+ #
+ # Only reap when no live, registered gateway is in the record: a
+ # healthy standalone gateway (launched via `hermes gateway run`, no
+ # service supervisor) is not an orphan, and the sweep would kill it.
+ # On Windows every layer of the launcher chain (stub -> venv python ->
+ # runtime python) carries "gateway run" in its command line, so
+ # find_gateway_pids() matches processes the pidfile exclusion cannot
+ # see, and os.kill(SIGTERM) is a hard TerminateProcess — the
+ # gateway's planned-stop watcher (0.5s poll) has no time to drain.
+ # cleanup_stale=False keeps the probe from deleting the registration
+ # record it is checking.
try:
+ from gateway.status import get_running_pid
from hermes_cli.gateway import _reap_unsupervised_gateway_orphans
- _reap_unsupervised_gateway_orphans()
+ if get_running_pid(cleanup_stale=False) is None:
+ _reap_unsupervised_gateway_orphans()
except Exception:
_log.exception("Desktop startup: orphan gateway reap failed")
diff --git a/tests/hermes_cli/test_gateway.py b/tests/hermes_cli/test_gateway.py
index 53c014b641c..ef749899fac 100644
--- a/tests/hermes_cli/test_gateway.py
+++ b/tests/hermes_cli/test_gateway.py
@@ -677,7 +677,9 @@ class TestReapUnsupervisedGatewayOrphansWindows:
self._install_fake_psutil(monkeypatch, [recorded, bootstrap])
# get_running_pid() returns the recorded healthy gateway PID.
- monkeypatch.setattr("gateway.status.get_running_pid", lambda: recorded_pid)
+ monkeypatch.setattr(
+ "gateway.status.get_running_pid", lambda cleanup_stale=True: recorded_pid
+ )
# find_gateway_pids returns the recorded PID, its bootstrap parent
# and a real orphan. The reaper should only kill the orphan.
@@ -720,7 +722,9 @@ class TestReapUnsupervisedGatewayOrphansWindows:
recorded = SimpleNamespace(pid=recorded_pid, parent=lambda: bootstrap)
self._install_fake_psutil(monkeypatch, [recorded, bootstrap])
- monkeypatch.setattr("gateway.status.get_running_pid", lambda: recorded_pid)
+ monkeypatch.setattr(
+ "gateway.status.get_running_pid", lambda cleanup_stale=True: recorded_pid
+ )
# find_gateway_pids would return the recorded PID and its bootstrap
# parent, but both are excluded.