Skip to content

The agent that grows with you

test: cover the slash-worker watchdog parent choice without faking the host OS

kshitijk4poorc9a92dda67a5
diff --git a/tests/tui_gateway/test_slash_worker_watchdog.py b/tests/tui_gateway/test_slash_worker_watchdog.py
index 5394aa88495..a996726ffc9 100644
--- a/tests/tui_gateway/test_slash_worker_watchdog.py
+++ b/tests/tui_gateway/test_slash_worker_watchdog.py
@@ -3,7 +3,6 @@ import importlib.util
import io
import subprocess
import sys
-import types
from pathlib import Path
from tui_gateway import slash_worker
@@ -67,11 +66,6 @@ def test_main_arms_watchdog_then_imports_cli_before_runtime_prep(monkeypatch):
lambda: events.append(("runtime",)),
)
- def unexpected_getppid():
- raise AssertionError("spawn parent PID must come from argv")
-
- monkeypatch.setattr(slash_worker.os, "getppid", unexpected_getppid)
- monkeypatch.setattr(sys, "platform", "linux")
monkeypatch.setattr(
sys,
"argv",
@@ -88,26 +82,26 @@ def test_main_arms_watchdog_then_imports_cli_before_runtime_prep(monkeypatch):
slash_worker.main()
assert events == [
- ("watchdog", parent_pid),
+ ("watchdog", slash_worker._watchdog_parent(parent_pid, is_windows=sys.platform == "win32")),
("cli-import",),
("runtime",),
("cli", "session-1"),
]
-def test_windows_watchdog_keeps_observed_parent_over_spawn_pid(monkeypatch):
+def test_watchdog_parent_uses_spawn_pid_off_windows():
+ def unexpected_getppid():
+ raise AssertionError("spawn parent PID must come from argv")
+
+ assert slash_worker._watchdog_parent(424242, is_windows=False, getppid=unexpected_getppid) == 424242
+
+
+def test_watchdog_parent_falls_back_to_observed_parent_without_spawn_pid():
+ assert slash_worker._watchdog_parent(0, is_windows=False, getppid=lambda: 777) == 777
+
+
+def test_watchdog_parent_keeps_observed_parent_on_windows():
# A venv python.exe redirector sits between the gateway and this interpreter on
# Windows, so the gateway PID never equals os.getppid() there; arming on it
# would make the watchdog kill a healthy worker on its first poll.
- armed = []
- monkeypatch.setitem(sys.modules, "cli", types.SimpleNamespace(HermesCLI=lambda **kw: types.SimpleNamespace()))
- monkeypatch.setattr(slash_worker, "_start_parent_death_watchdog", armed.append)
- monkeypatch.setattr(slash_worker, "_prepare_slash_worker_runtime", lambda: None)
- monkeypatch.setattr(slash_worker.os, "getppid", lambda: 777)
- monkeypatch.setattr(sys, "platform", "win32")
- monkeypatch.setattr(sys, "argv", ["slash_worker", "--session-key", "s", "--parent-pid", "424242"])
- monkeypatch.setattr(sys, "stdin", io.StringIO(""))
-
- slash_worker.main()
-
- assert armed == [777]
+ assert slash_worker._watchdog_parent(424242, is_windows=True, getppid=lambda: 777) == 777
diff --git a/tui_gateway/slash_worker.py b/tui_gateway/slash_worker.py
index 2649103674f..6149479421f 100644
--- a/tui_gateway/slash_worker.py
+++ b/tui_gateway/slash_worker.py
@@ -43,6 +43,21 @@ def _is_orphaned(original_ppid, getppid=os.getppid) -> bool:
return getppid() != original_ppid
+def _watchdog_parent(parent_pid: int, *, is_windows: bool, getppid=os.getppid) -> int:
+ """Return the PID the parent-death watchdog should treat as our parent.
+
+ The gateway passes its PID at spawn so a fast exit cannot make this child
+ mistake a subreaper for its original parent before the watchdog starts.
+ The watchdog compares the kernel's live PPID against it, so a reused PID
+ can never pass for the parent. Windows never reparents, and a venv
+ python.exe redirector makes the launcher (not the gateway) our direct
+ parent there, so keep the observed PPID or the worker would exit at once.
+ """
+ if is_windows or not parent_pid:
+ return getppid()
+ return parent_pid
+
+
def _prepare_slash_worker_runtime() -> None:
"""Start bounded MCP discovery before HermesCLI snapshots tools: each slash_worker child is its
own process — the parent ``hermes serve`` discovery thread does not populate this registry.
@@ -153,14 +168,7 @@ def main():
args = p.parse_args()
os.environ["HERMES_SESSION_KEY"] = args.session_key
os.environ["HERMES_INTERACTIVE"] = "1"
- # The gateway passes its PID at spawn so a fast exit cannot make this child
- # mistake a subreaper for its original parent before the watchdog starts.
- # The watchdog compares the kernel's live PPID against it, so a reused PID
- # can never pass for the parent. Windows never reparents, and a venv
- # python.exe redirector makes the launcher (not the gateway) our direct
- # parent there, so keep the observed PPID or the worker would exit at once.
- orig_ppid = os.getppid() if sys.platform == "win32" else (args.parent_pid or os.getppid())
- _start_parent_death_watchdog(orig_ppid)
+ _start_parent_death_watchdog(_watchdog_parent(args.parent_pid, is_windows=sys.platform == "win32"))
# Keep the heavyweight CLI import behind the watchdog (importing it at module
# load left a reparenting window before main() could snapshot PPID), but ahead
# of MCP discovery: importing cli loads ~/.hermes/.env and sets HERMES_QUIET,