The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tts): a command TTS/STT provider dies with the Hermes process that ran it (orphans after kill -9: 3 -> 0)
kshitijk4poor118712327d3d
diff --git a/tests/hermes_cli/test_owned_agent_subprocs.py b/tests/hermes_cli/test_owned_agent_subprocs.pyindex 06eab9775dd..506443f7d3b 100644--- a/tests/hermes_cli/test_owned_agent_subprocs.py+++ b/tests/hermes_cli/test_owned_agent_subprocs.py@@ -62,6 +62,11 @@ _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"),+ # 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"+ "threading.Thread(target=run_command_provider, args=(child_cmd, 120.0), daemon=True).start()\n"+ ),}@@ -203,3 +208,14 @@ def test_verify_start_phase_owns_then_releases_the_app_group(monkeypatch, tmp_paresult = runner._run_start_phase(Recipe(name="x", start="sleep 30", port=1), tmp_path, 0.3)assert not result.readyassert len(owned) == 1 and released == [owned]+++def test_tts_command_owns_then_releases_its_group_after_success_and_idle_kill(monkeypatch, owned):+ from tools import tts_command_provider as tts++ released = []+ monkeypatch.setattr(tts, "release_process_groups", lambda pgids: released.extend(pgids))+ assert tts.run_command_provider("echo hi", 10.0).stdout.strip() == "hi"+ with pytest.raises(subprocess.TimeoutExpired):+ tts.run_command_provider("sleep 30", 0.3)+ assert len(owned) == 2 and released == owneddiff --git a/tests/tools/test_transcription_tools.py b/tests/tools/test_transcription_tools.pyindex b4402001d6f..8c84350b456 100644--- a/tests/tools/test_transcription_tools.py+++ b/tests/tools/test_transcription_tools.py@@ -282,17 +282,21 @@ class TestTranscribeLocalCommand:class Proc:returncode = 0+ pid = 4242stdout = _Stream()stderr = _Stream()def wait(self, timeout=None):return 0+ def poll(self):+ return 0+def fake_popen(command, **kwargs):captured["env"] = kwargs["env"]return Proc()- monkeypatch.setattr("tools.tts_command_provider.subprocess.Popen", fake_popen)+ monkeypatch.setattr("tools.tts_command_provider.spawn_owned", fake_popen)from tools.transcription_command import _run_command_sttdiff --git a/tests/tools/test_tts_command_providers.py b/tests/tools/test_tts_command_providers.pyindex c4b446e9a00..3b03013cca7 100644--- a/tests/tools/test_tts_command_providers.py+++ b/tests/tools/test_tts_command_providers.py@@ -117,17 +117,21 @@ class TestCommandTtsEnv:class Proc:returncode = 0+ pid = 4242stdout = _Stream()stderr = _Stream()def wait(self, timeout=None):return 0+ def poll(self):+ return 0+def fake_popen(command, **kwargs):captured["env"] = kwargs["env"]return Proc()- monkeypatch.setattr("tools.tts_command_provider.subprocess.Popen", fake_popen)+ monkeypatch.setattr("tools.tts_command_provider.spawn_owned", fake_popen)result = _run_command_tts("echo hi", timeout=1)@@ -487,17 +491,21 @@ class TestCommandTtsEnvPassthrough:class Proc:returncode = 0+ pid = 4242stdout = _Stream()stderr = _Stream()def wait(self, timeout=None):return 0+ def poll(self):+ return 0+def fake_popen(command, **kwargs):captured["env"] = kwargs["env"]return Proc()- monkeypatch.setattr("tools.tts_command_provider.subprocess.Popen", fake_popen)+ monkeypatch.setattr("tools.tts_command_provider.spawn_owned", fake_popen)result = _run_command_tts("echo hi", timeout=1, env_passthrough=["MY_TTS_API_KEY"]@@ -552,11 +560,11 @@ class TestCommandProviderSpawnGroupKwargs:_subprocess_compat.windows_detach_flags_without_breakaway())- def test_posix_branch_uses_start_new_session(self):+ def test_posix_branch_leaves_the_group_to_spawn_owned(self):from tools.tts_command_provider import provider_popen_group_kwargs- kwargs = provider_popen_group_kwargs("posix")- assert kwargs == {"start_new_session": True}+ # spawn_owned makes the child a session/group leader (and ties the group to our life).+ assert provider_popen_group_kwargs("posix") == {}@pytest.mark.platforms("windows")def test_popen_kwargs_hide_console_window_windows(self, monkeypatch):@@ -568,12 +576,16 @@ class TestCommandProviderSpawnGroupKwargs:class Proc:returncode = 0+ pid = 4242stdout = _Stream()stderr = _Stream()def wait(self, timeout=None):return 0+ def poll(self):+ return 0+def fake_popen(command, **kwargs):captured.update(kwargs)return Proc()@@ -589,7 +601,7 @@ class TestCommandProviderSpawnGroupKwargs:assert "start_new_session" not in captured@pytest.mark.platforms("posix")- def test_popen_kwargs_start_new_session_posix(self, monkeypatch):+ def test_posix_spawn_is_owned_and_has_no_console_flags(self, monkeypatch):captured = {}class _Stream:@@ -598,22 +610,26 @@ class TestCommandProviderSpawnGroupKwargs:class Proc:returncode = 0+ pid = 4242stdout = _Stream()stderr = _Stream()def wait(self, timeout=None):return 0- def fake_popen(command, **kwargs):- captured.update(kwargs)+ def poll(self):+ return 0++ def fake_spawn_owned(command, *, purpose, **kwargs):+ captured.update(kwargs, purpose=purpose)return Proc()- monkeypatch.setattr("tools.tts_command_provider.subprocess.Popen", fake_popen)+ monkeypatch.setattr("tools.tts_command_provider.spawn_owned", fake_spawn_owned)result = _run_command_tts("echo hi", timeout=1)assert result.returncode == 0- assert captured["start_new_session"] is True+ assert captured["purpose"] == "tts-command"assert "creationflags" not in captureddiff --git a/tools/tts_command_provider.py b/tools/tts_command_provider.pyindex ee4df322a7a..41953b6d337 100644--- a/tools/tts_command_provider.py+++ b/tools/tts_command_provider.py@@ -23,6 +23,7 @@ from functools import partialfrom pathlib import Pathfrom typing import Any, Dict, FrozenSet, Optional+from hermes_cli.process_identity import release_process_groups, spawn_ownedfrom utils import is_truthy_value@@ -146,13 +147,15 @@ def provider_popen_group_kwargs(os_name: str) -> dict[str, Any]:windows_detach_flags_without_breakaway) adds CREATE_NO_WINDOW so the childowns a hidden console its descendants inherit instead — deliberately withoutCREATE_BREAKAWAY_FROM_JOB: this child is foreground-owned and killed by us on- idle timeout, so job teardown propagation must keep working. POSIX keeps the- session detach (setsid) so the tree can be signalled on idle timeout.+ idle timeout, so job teardown propagation must keep working. POSIX: the child+ leads its own group (``spawn_owned``) so the tree can be signalled on idle+ timeout, and the group dies with this process: the idle timer lives here, so+ a stalled command used to outlive a SIGKILLed Hermes forever."""from hermes_cli._subprocess_compat import windows_detach_flags_without_breakawayif os_name == "nt":return {"creationflags": windows_detach_flags_without_breakaway()}- return {"start_new_session": True}+ return {}def run_command_provider(@@ -177,9 +180,9 @@ def run_command_provider(# flashes a window on windowless hosts). Lossy UTF-8 decode: locale-mismatched bytes# must not raise in the reader threads.group = provider_popen_group_kwargs(os.name)- proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,- text=True, encoding="utf-8", errors="replace", env=delegated_child_subprocess_env(scrubbed),- stdin=subprocess.DEVNULL, **group)+ proc = spawn_owned(command, purpose="tts-command", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,+ text=True, encoding="utf-8", errors="replace", env=delegated_child_subprocess_env(scrubbed),+ stdin=subprocess.DEVNULL, **group)output_queue: queue.Queue[tuple[str, Optional[str]]] = queue.Queue()chunks: dict[str, list[str]] = {"stdout": [], "stderr": []}open_streams = {"stdout", "stderr"}@@ -229,6 +232,8 @@ def run_command_provider(name, chunk = output_queue.get_nowait()if chunk:chunks[name].append(chunk)+ if proc.poll() is not None:+ release_process_groups([proc.pid]) # a tree we failed to stop stays coveredstdout, stderr = "".join(chunks["stdout"]), "".join(chunks["stderr"])if timed_out:raise subprocess.TimeoutExpired(command, timeout, output=stdout, stderr=stderr) from (