Skip to content

The agent that grows with you

fix(dashboard): the WhatsApp pairing bridge dies with the dashboard that started it

kshitijk4poorbcf275a67517
diff --git a/hermes_cli/web_routers/messaging.py b/hermes_cli/web_routers/messaging.py
index b62875a229b..8c061ef6065 100644
--- a/hermes_cli/web_routers/messaging.py
+++ b/hermes_cli/web_routers/messaging.py
@@ -25,6 +25,7 @@ from fastapi import APIRouter, HTTPException
from gateway.status import resolve_gateway_liveness, retained_gateway_state
from hermes_cli._subprocess_compat import windows_hide_flags
from hermes_cli.config import OPTIONAL_ENV_VARS, get_env_path
+from hermes_cli.process_identity import release_process_groups, spawn_owned
from hermes_constants import get_process_hermes_home
from hermes_cli.web_deps import LateState, late
from hermes_cli.web_server_gateway import _restart_gateway_after
@@ -434,24 +435,28 @@ def _spawn_whatsapp_pairing_process(session_path: Path, mode: str) -> subprocess
env = with_hermes_node_path()
env["WHATSAPP_MODE"] = mode
env["WHATSAPP_DM_POLICY"] = "pairing"
- return subprocess.Popen(
+ # Owned: a SIGKILLed dashboard must not leave a pairing bridge holding the session dir.
+ return spawn_owned(
[node, str(bridge_script), "--pair-only", "--pair-json", "--session", str(session_path)],
- cwd=str(bridge_dir), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
- encoding="utf-8", errors="replace", start_new_session=True, env=env,
- creationflags=windows_hide_flags(),
+ purpose="whatsapp-pairing", cwd=str(bridge_dir), stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+ text=True, encoding="utf-8", errors="replace", env=env, creationflags=windows_hide_flags(),
)
def _terminate_whatsapp_pairing(proc: subprocess.Popen | None) -> None:
- if proc is None or proc.poll() is not None:
+ if proc is None:
return
- try:
- proc.terminate()
- proc.wait(timeout=3)
- except Exception:
- with contextlib.suppress(Exception):
- proc.kill()
+ if proc.poll() is None:
+ try:
+ proc.terminate()
proc.wait(timeout=3)
+ except Exception:
+ with contextlib.suppress(Exception):
+ proc.kill()
+ proc.wait(timeout=3)
+ if proc.poll() is not None:
+ # Stopped; one we failed to stop stays covered by the owner-death supervisor.
+ release_process_groups([proc.pid])
def _fail_whatsapp_pairing(pairing_id: str, error: str, *, proc=None, unless=_WHATSAPP_ONBOARDING_TERMINAL_STATUSES) -> None:
@@ -505,6 +510,7 @@ def _watch_whatsapp_pairing(pairing_id: str, proc: subprocess.Popen) -> None:
if record.status not in {"cancelled", "expired"}:
_apply_pairing_event(record, payload)
returncode = proc.wait()
+ release_process_groups([proc.pid])
except Exception as exc:
_fail_whatsapp_pairing(pairing_id, str(exc), proc=proc)
return
diff --git a/tests/hermes_cli/test_owned_agent_subprocs.py b/tests/hermes_cli/test_owned_agent_subprocs.py
index 506443f7d3b..abd37accbaf 100644
--- a/tests/hermes_cli/test_owned_agent_subprocs.py
+++ b/tests/hermes_cli/test_owned_agent_subprocs.py
@@ -62,6 +62,20 @@ _OWNERS = {
"threading.Thread(target=runner._run_start_phase, daemon=True,\n"
" args=(Recipe(name='x', start=child_cmd, port=1), out, 120.0)).start()\n"
),
+ # The dashboard's pairing bridge: `node bridge.js --pair-only ...`, with python as node and a
+ # bridge.js that execs the child.
+ "whatsapp-pairing": (
+ "bridge = out / 'bridge'; bridge.mkdir()\n"
+ "(bridge / 'bridge.js').write_text('import os, sys\\nos.execv(sys.executable, ' + repr(child) + ')\\n',\n"
+ " encoding='utf-8')\n"
+ "import hermes_constants\n"
+ "from gateway.platforms import whatsapp_common\n"
+ "from hermes_cli.web_routers import messaging\n"
+ "whatsapp_common.resolve_whatsapp_bridge_dir = lambda: bridge\n"
+ "hermes_constants.find_node_executable = lambda name: sys.executable\n"
+ "messaging._ensure_whatsapp_bridge_dependencies = lambda bridge_dir: None\n"
+ "messaging._spawn_whatsapp_pairing_process(out / 'session', 'bot')\n"
+ ),
# A command provider that stalls: the owner dies inside its 120 s idle timeout.
"tts-command": (
"from tools.tts_command_provider import run_command_provider\n"
@@ -219,3 +233,17 @@ def test_tts_command_owns_then_releases_its_group_after_success_and_idle_kill(mo
with pytest.raises(subprocess.TimeoutExpired):
tts.run_command_provider("sleep 30", 0.3)
assert len(owned) == 2 and released == owned
+
+
+def test_whatsapp_pairing_terminate_releases_the_process_it_stopped(monkeypatch):
+ from hermes_cli.web_routers import messaging
+
+ released = []
+ monkeypatch.setattr(messaging, "release_process_groups", lambda pgids: released.append(list(pgids)))
+ pairing = _sleeper()
+ try:
+ messaging._terminate_whatsapp_pairing(pairing)
+ assert pairing.poll() is not None, "_terminate_whatsapp_pairing() left the process running"
+ assert released == [[pairing.pid]]
+ finally:
+ _reap(pairing)