Skip to content

The agent that grows with you

fix(computer-use): the private embedded cua-driver daemon dies with the Hermes that started it (survivors after kill -9: 1 -> 0)

kshitijk4pooradf564d01b96
diff --git a/tests/computer_use/test_cua_no_overlay.py b/tests/computer_use/test_cua_no_overlay.py
index 0d69298db3a..5d10c154b51 100644
--- a/tests/computer_use/test_cua_no_overlay.py
+++ b/tests/computer_use/test_cua_no_overlay.py
@@ -248,7 +248,9 @@ class TestEmbeddedDaemonOverlayFlag:
cua_backend.subprocess, "Popen", return_value=process,
) as popen, patch.object(
cua_backend.subprocess, "run", return_value=status,
- ), patch.object(cua_backend_daemon.threading, "Thread"):
+ ), patch.object(cua_backend_daemon.threading, "Thread"), patch(
+ "hermes_cli.process_identity._update_supervised_pgids", # a MagicMock pid never reaches the supervisor
+ ), patch("hermes_cli.process_identity.register_child"):
daemon.start()
command = popen.call_args.args[0]
diff --git a/tests/computer_use/test_cua_pm_selection.py b/tests/computer_use/test_cua_pm_selection.py
index 9b9f34521e1..8971cdcae23 100644
--- a/tests/computer_use/test_cua_pm_selection.py
+++ b/tests/computer_use/test_cua_pm_selection.py
@@ -112,10 +112,14 @@ def test_private_start_uses_post_ensure_binary(cua_home, monkeypatch, permission
def capture_serve(command, **kwargs):
if command[1] == "serve":
launches.append(command)
- return SimpleNamespace(stderr=(), poll=lambda: None, wait=lambda **kw: 0)
+ return SimpleNamespace(pid=0, stderr=(), poll=lambda: None, wait=lambda **kw: 0)
return spawn(command, **kwargs)
monkeypatch.setattr(subprocess, "Popen", capture_serve)
+ # The stand-in daemon has no real process group: keep it away from the owner-death supervisor.
+ from hermes_cli import process_identity
+ monkeypatch.setattr(process_identity, "_update_supervised_pgids", lambda verb, pgids: None)
+ monkeypatch.setattr(process_identity, "register_child", lambda pid, purpose: None)
# Re-start the same backend after PM publishes another version as well.
for _ in range(2):
try:
diff --git a/tests/tools/test_computer_use_cua_0_10_permissions.py b/tests/tools/test_computer_use_cua_0_10_permissions.py
index ebae4c0d197..990559f2335 100644
--- a/tests/tools/test_computer_use_cua_0_10_permissions.py
+++ b/tests/tools/test_computer_use_cua_0_10_permissions.py
@@ -160,6 +160,9 @@ def test_unrestricted_embedded_daemon_uses_private_socket_and_two_part_ack():
cua_backend, "_cua_no_overlay", return_value=False,
), patch.object(cua_backend.subprocess, "Popen", return_value=process) as popen, patch.object(
cua_backend.subprocess, "run", side_effect=[status, stopped]
+ ), patch("hermes_cli.process_identity._update_supervised_pgids") as supervise, patch(
+ # A Mock pid must never reach the real owner-death supervisor or the spawn ledger.
+ "hermes_cli.process_identity.register_child"
):
daemon.start()
command = popen.call_args.args[0]
@@ -167,6 +170,10 @@ def test_unrestricted_embedded_daemon_uses_private_socket_and_two_part_ack():
proxy_command, proxy_args = daemon.proxy_invocation()
daemon.stop()
+ # The private daemon is an owned child: its group is handed to the owner-death supervisor at start
+ # and released once stop() has stopped it.
+ assert popen.call_args.kwargs["start_new_session"] is True
+ assert [c.args for c in supervise.call_args_list] == [("register", [process.pid]), ("unregister", [process.pid])]
assert command[:2] == ["/opt/cua-driver", "serve"]
assert "--embedded" in command
assert command[command.index("--permission-mode") + 1] == "unrestricted"
diff --git a/tools/computer_use/cua_backend_daemon.py b/tools/computer_use/cua_backend_daemon.py
index 508072e6167..c0157513259 100644
--- a/tools/computer_use/cua_backend_daemon.py
+++ b/tools/computer_use/cua_backend_daemon.py
@@ -18,6 +18,7 @@ from typing import Any, Dict, List, Optional, Tuple
from tools.computer_use import cua_backend_driver as _driver
from tools.computer_use.permissions import CUA_DRIVER_BUNDLE_ID
+from hermes_cli.process_identity import release_process_groups, spawn_owned
logger = logging.getLogger("tools.computer_use.cua_backend")
@@ -168,9 +169,16 @@ class _EmbeddedCuaDaemon:
self._command, self._mcp_args = _driver._resolve_mcp_invocation(driver_cmd)
env = self._sanitized_env()
command = _embedded_daemon_spawn_command(self._command, self._serve_args(), platform=sys.platform)
- self._process = subprocess.Popen(command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
- stderr=subprocess.PIPE, text=True, encoding="utf-8", errors="replace",
- env=env)
+ popen_kwargs: dict[str, Any] = dict(stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
+ text=True, encoding="utf-8", errors="replace", env=env)
+ if sys.platform == "darwin":
+ # `open` hands the daemon to LaunchServices: it is launchd's child, never in our tree, so there is no
+ # group of ours to own. The daemon's own --embedded lifetime is the only bound there.
+ self._process = subprocess.Popen(command, **popen_kwargs)
+ else:
+ # Owned: the daemon dies with this process however it dies. It used to be stopped only by stop()
+ # (session release / atexit), so a SIGKILLed or OOM-killed Hermes left a YOLO-mode daemon running.
+ self._process = spawn_owned(command, purpose="cua-driver-embedded", **popen_kwargs)
self._owns_runtime = True
threading.Thread(target=self._drain_stderr, args=(self._process,), name="hermes-cua-daemon-stderr", daemon=True).start()
deadline = time.monotonic() + self._START_TIMEOUT_SECONDS
@@ -207,6 +215,7 @@ class _EmbeddedCuaDaemon:
stderr=subprocess.DEVNULL, env=self._sanitized_env(), swallow=_QUIET_ERRORS)
if process is not None:
_wait_or_kill(process)
+ release_process_groups([process.pid]) # stopped it ourselves (a no-op for the macOS `open` launcher)
if sys.platform != "win32" and os.path.exists(self.socket_path):
with contextlib.suppress(OSError):
os.remove(self.socket_path)