The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): preserve slash worker parent identity
embwl0xa63a8e9918ba
diff --git a/tests/tui_gateway/test_slash_worker_watchdog.py b/tests/tui_gateway/test_slash_worker_watchdog.pyindex 6051854a622..0854afa01b3 100644--- a/tests/tui_gateway/test_slash_worker_watchdog.py+++ b/tests/tui_gateway/test_slash_worker_watchdog.py@@ -1,9 +1,90 @@+import inspect+import io+import subprocess+import sys+import types+from pathlib import Path+from tui_gateway import slash_worker++def test_module_import_does_not_load_cli_before_watchdog_can_start():+ subprocess.run(+ [+ sys.executable,+ "-c",+ "import sys; assert 'cli' not in sys.modules; "+ "import tui_gateway.slash_worker; assert 'cli' not in sys.modules",+ ],+ cwd=Path(__file__).resolve().parents[2],+ check=True,+ timeout=10,+ )++def test_is_orphaned_true_when_ppid_changes():# Our parent went away and we were reparented to a subreaper/init.assert slash_worker._is_orphaned(1234, getppid=lambda: 999999) is True+def test_is_orphaned_false_when_direct_parent_is_unchanged():original_ppid = 1234assert slash_worker._is_orphaned(original_ppid, getppid=lambda: original_ppid) is False+++def test_parent_death_watchdog_contract_has_no_create_time_plumbing():+ assert list(inspect.signature(slash_worker._is_orphaned).parameters) == [+ "original_ppid",+ "getppid",+ ]+ assert list(inspect.signature(slash_worker._start_parent_death_watchdog).parameters) == [+ "original_ppid",+ ]+++def test_main_arms_watchdog_for_spawn_parent_before_cli_startup(monkeypatch):+ parent_pid = 424242+ events = []++ class FakeHermesCLI:+ def __init__(self, **kwargs):+ events.append(("cli", kwargs["resume"]))++ cli_module = types.ModuleType("cli")+ setattr(cli_module, "HermesCLI", FakeHermesCLI)+ monkeypatch.setitem(sys.modules, "cli", cli_module)+ monkeypatch.setattr(+ slash_worker,+ "_start_parent_death_watchdog",+ lambda pid: events.append(("watchdog", pid)),+ )+ monkeypatch.setattr(+ slash_worker,+ "_prepare_slash_worker_runtime",+ 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,+ "argv",+ [+ "slash_worker",+ "--session-key",+ "session-1",+ "--parent-pid",+ str(parent_pid),+ ],+ )+ monkeypatch.setattr(sys, "stdin", io.StringIO(""))++ slash_worker.main()++ assert events == [+ ("watchdog", parent_pid),+ ("runtime",),+ ("cli", "session-1"),+ ]diff --git a/tui_gateway/server.py b/tui_gateway/server.pyindex a6868375706..94bc00c0be4 100644--- a/tui_gateway/server.py+++ b/tui_gateway/server.py@@ -255,7 +255,8 @@ class _SlashWorker:# ``--provider`` pins the child to the parent agent's virtual provider: without it the# worker re-resolves provider from config, so a MoA session (provider=moa, model=<preset>)# dispatched its preset NAME to the configured real provider and 402/503'd (#57283).- argv = [sys.executable, "-m", "tui_gateway.slash_worker", "--session-key", session_key] \+ argv = [sys.executable, "-m", "tui_gateway.slash_worker", "--session-key", session_key,+ "--parent-pid", str(os.getpid())] \+ (["--model", model] if model else []) \+ (["--provider", provider] if provider else [])self._closed = Falsediff --git a/tui_gateway/slash_worker.py b/tui_gateway/slash_worker.pyindex b2507c7090e..54a87638f70 100644--- a/tui_gateway/slash_worker.py+++ b/tui_gateway/slash_worker.py@@ -23,12 +23,13 @@ import osimport sysimport threadingimport time+from typing import TYPE_CHECKING-import cli as cli_mod-from cli import HermesCLIfrom tui_gateway._env import env_floatfrom tui_gateway._stdin_recovery import handle_spurious_eof-from rich.console import Console++if TYPE_CHECKING:+ from cli import HermesCLI# Env-overridable so the integration test can drive sub-second timing._WATCHDOG_POLL_S = max(0.05, env_float("HERMES_SLASH_WATCHDOG_POLL_S", 2.0))@@ -97,7 +98,7 @@ def _refuse_skill_slash(command: str) -> None:raise SkillSlashRefused(base)-def _run(cli: HermesCLI, command: str) -> str:+def _run(cli: "HermesCLI", command: str) -> str:"""Run one command; return its captured, ANSI-stripped output.A command like /prompt or /blueprint parks the composed text on the one-shot@@ -105,6 +106,9 @@ def _run(cli: HermesCLI, command: str) -> str:worker has no REPL, so the seed is harvested here onto ``cli._harvested_seed``and routed back to the gateway, which sends it as the next turn (#107800)."""+ import cli as cli_mod+ from rich.console import Console+cli._harvested_seed = "" # one-shot: a fresh run never re-sends a stale seedcmd = (command or "").strip()if not cmd:@@ -145,13 +149,20 @@ def main():p.add_argument("--session-key", required=True)p.add_argument("--model", default="")p.add_argument("--provider", default="")+ p.add_argument("--parent-pid", type=int, default=0)args = p.parse_args()os.environ["HERMES_SESSION_KEY"] = args.session_keyos.environ["HERMES_INTERACTIVE"] = "1"- # Start before the (hundreds-of-ms) HermesCLI build — that window is itself an orphan risk if the- # gateway dies mid-spawn.- _start_parent_death_watchdog(os.getppid())+ # 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.+ orig_ppid = args.parent_pid or os.getppid()+ _start_parent_death_watchdog(orig_ppid)_prepare_slash_worker_runtime()++ # Keep the heavyweight CLI import behind the watchdog. Importing it at+ # module load left a reparenting window before main() could snapshot PPID.+ from cli import HermesCLI+with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):# --provider pins the CLI to the parent agent's resolved provider (a MoA session's virtual# "moa" provider included). Without it HermesCLI re-resolves from config and dispatches the