The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(process): an idle supervisor stays for a 30 s grace instead of exiting with every command
kshitijk4poor2a845db4580d
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.pyindex d020628fcb7..ed9b0dc2cdf 100644--- a/hermes_cli/process_identity.py+++ b/hermes_cli/process_identity.py@@ -23,7 +23,7 @@ import subprocessimport sysimport threadingimport time-from contextlib import ExitStack, contextmanager+from contextlib import ExitStack, contextmanager, suppressfrom contextvars import ContextVarfrom dataclasses import asdict, dataclassfrom pathlib import Path@@ -890,6 +890,47 @@ def _watch_supervisor(proc) -> None:threading.Thread(target=wait_then_restart, name="owner-death-supervisor-waiter", daemon=True).start()+# An idle supervisor (nothing registered) exits this long after the set last emptied, unless a new+# registration arrives first.+_SUPERVISOR_IDLE_EXIT_S = 30.0+_idle_release: Optional[threading.Timer] = None+++def _release_when_idle_locked(proc) -> None:+ """Close ``proc``'s pipe once the set has stayed empty for ``_SUPERVISOR_IDLE_EXIT_S``; the caller+ holds ``_supervisor_lock``. Closing our write end is the same EOF our death sends; with an empty+ set it exits, and its waiter thread reaps it. The next registration respawns and replays."""+ global _idle_release+ _cancel_idle_release_locked()+ if _SUPERVISOR_IDLE_EXIT_S <= 0:+ _close_idle_supervisor_locked(proc)+ return++ def release() -> None:+ with _supervisor_lock:+ _close_idle_supervisor_locked(proc)++ _idle_release = threading.Timer(_SUPERVISOR_IDLE_EXIT_S, release)+ _idle_release.daemon = True+ _idle_release.start()+++def _close_idle_supervisor_locked(proc) -> None:+ global _supervisor+ if _supervisor is not proc or _supervised_pgids:+ return # replaced, or something registered meanwhile+ with suppress(BrokenPipeError, ValueError, OSError):+ proc.stdin.close()+ _supervisor = None+++def _cancel_idle_release_locked() -> None:+ global _idle_release+ if _idle_release is not None:+ _idle_release.cancel()+ _idle_release = None++def _start_supervisor_locked():"""Start a supervisor and replay every live registration to it; the caller holds``_supervisor_lock``. Returns it, or None when it cannot start or its pipe broke at once."""@@ -1042,6 +1083,8 @@ def _update_supervised_pgids(verb: str, pgids) -> None:# mutation and the prune, so pruned groups never reach the replacement)._start_supervisor_locked()return+ if _supervised_pgids:+ _cancel_idle_release_locked()payload = "".join(f"{verb} {pgid}\n" for pgid in wanted)payload += "".join(f"unregister {pgid}\n" for pgid in stale)@@ -1056,11 +1099,8 @@ def _update_supervised_pgids(verb: str, pgids) -> None:returnif not _supervised_pgids:- # Nothing left to reap: release the supervisor rather than keep a process and a pipe- # resident for the life of a gateway. Closing our write end is the same EOF our death- # sends; with an empty set it exits. The next register respawns and replays.- try:- proc.stdin.close()- except (BrokenPipeError, ValueError, OSError):- pass- _supervisor = None # its waiter thread reaps it once it exits+ # Nothing left to reap: release the supervisor after an idle grace rather than keep a+ # process and a pipe resident for the life of a gateway. Not at once: a foreground+ # command owns and releases its group every time, and a supervisor per command costs a+ # /bin/sh spawn (~11 ms) under this lock.+ _release_when_idle_locked(proc)diff --git a/tests/hermes_cli/test_process_identity_supervisor.py b/tests/hermes_cli/test_process_identity_supervisor.pyindex 07eef1c08f4..f86f34f5f13 100644--- a/tests/hermes_cli/test_process_identity_supervisor.py+++ b/tests/hermes_cli/test_process_identity_supervisor.py@@ -393,7 +393,9 @@ class _FakeSupervisor:@pytest.fixture(autouse=True)-def _reset_client_state():+def _reset_client_state(monkeypatch):+ # The protocol tests below assert the release itself; the idle grace has its own test.+ monkeypatch.setattr(process_identity, "_SUPERVISOR_IDLE_EXIT_S", 0.0)yieldprocess_identity._supervisor = Noneprocess_identity._supervised_pgids.clear()@@ -911,3 +913,20 @@ def test_a_supervisor_dropped_on_a_broken_pipe_is_still_restarted_by_its_waiter(finally:_kill(child.pid)child.wait(timeout=10)+++def test_an_idle_supervisor_stays_for_the_next_command_and_exits_after_the_grace(monkeypatch):+ """A foreground command owns and releases its group every time; a supervisor per command cost a+ /bin/sh spawn (~11 ms). The supervisor now outlives an empty set for a grace, then still exits."""+ monkeypatch.setattr(process_identity, "_SUPERVISOR_IDLE_EXIT_S", 1.0)+ for _ in range(2):+ child = subprocess.Popen([sys.executable, "-c", "pass"], start_new_session=True)+ process_identity.own_process_groups([child.pid])+ child.wait(timeout=10)+ process_identity.release_process_group_once_empty(child.pid, proc=child, timeout=5)+ if _ == 0:+ first = process_identity._supervisor+ assert first is not None and first.poll() is None, "the supervisor exited with the first command"+ assert process_identity._supervisor is first, "the second command spawned another supervisor"+ wait_until(lambda: process_identity._supervisor is None, timeout=10, what="the idle supervisor released")+ wait_until(lambda: first.returncode is not None, timeout=10, what="the idle supervisor to exit and be reaped")diff --git a/tests/tools/test_mcp_child_filter_supervisor.py b/tests/tools/test_mcp_child_filter_supervisor.pyindex 90d26f5e683..8ebc42cdb17 100644--- a/tests/tools/test_mcp_child_filter_supervisor.py+++ b/tests/tools/test_mcp_child_filter_supervisor.py@@ -17,6 +17,7 @@ pytestmark = pytest.mark.platforms("posix")def test_an_owned_spawn_racing_mcp_discovery_does_not_hand_mcp_the_supervisor(monkeypatch):+ monkeypatch.setattr(process_identity, "_SUPERVISOR_IDLE_EXIT_S", 0.0)monkeypatch.setattr(process_identity, "_supervisor", None)monkeypatch.setattr(process_identity, "_supervised_pgids", set())monkeypatch.setattr(process_identity, "register_child", lambda *a, **k: True)