Files
hermes-agent/scripts/check_detached_spawns.py
alt-glitch 42866787a1 refactor(speech): speech files move from tools/ to a new speech/ package
Text-to-speech engines, transcription, voice mode and wake word register no
tool, so 28 files leave tools/ for a root package with four sub-packages:
speech/synthesis, speech/transcription, speech/voice and speech/wake_word.
tools/tts_tool.py, which registers text_to_speech, stays, and so does
tools/tts_elevenlabs_voices.py, which builds part of that tool's schema. Each
__init__.py is a docstring only.

File names keep their stem and drop the prefix the folder makes redundant.
transcription_tools becomes transcription/transcriber, transcription_common
becomes transcription/settings, wake_word becomes wake_word/listener, and
tts_tool_openai becomes synthesis/openai_compatible: neutts_synth.py runs by
file path from that folder, where an openai.py would shadow the openai package.
The NeuTTS samples and the bundled wake-word model move with the code that
reads them through __file__.

Function bodies are unchanged and every import stays where it was. Logger
names follow the modules: a moved module logs under its speech.* path, and a
literal that named a moved module names its new path. speech joins the tools
log component, so `hermes logs --component tools` shows the same lines. The
seven synthesis files that share the logger of tools/tts_tool.py keep it.

tools.tts_streaming keeps a generated file at its old path. Every release from
v0.21.6 to v0.23.5 imports it inside the CLI and TUI turn, outside any try, and
an idle process has not loaded it yet, so a process that outlives `hermes
update` would fail its first turn. No other moved name gets a file.

speech joins pyproject's packages.find, FIRST_PARTY_MODULE_ROOTS, the plugin
isolation audit, the known logger roots of `hermes debug share`, six check
scripts, the P33 profile-scope pattern, the subprocess env guard test, the
packaging test and three fixtures that copy a source tree. Tests whose subject
is a moved module move to tests/speech/ with the fixtures only they use.

Pinned catalog plugins import these moved modules by the old name, so each has a key in
hermes_agent/compat/moves.py and the plugin guide lists it with the release
that may remove it (no earlier than v0.27.0):
  tools.transcription_cloud
  tools.transcription_tools
  tools.tts_text_normalize
  tools.tts_tool_providers
  tools.voice_live
  tools.voice_mode
  tools.voice_mode_transcript
2026-11-30 16:59:14 +05:30

220 lines
11 KiB
Python

#!/usr/bin/env python3
"""Fail when production code starts a detached child outside the owned-child helper.
A child started in its own session/process group (``start_new_session=True``, ``os.setsid``,
``os.setpgid``/``os.setpgrp`` in a ``preexec_fn``) no longer dies with the Hermes process that
started it. When that process is SIGKILLed, OOM-killed or crashes, the child reparents to init
(or a ``systemd --user`` subreaper) and runs forever. Every long-lived child that should die with
its owner goes through ``hermes_cli.process_identity.spawn_owned`` (or hands its group to
``own_process_groups`` where something else spawned it), which registers the group with the
per-process owner-death supervisor.
Flags, in Python files of the production trees (tests, evals, scripts and nix checks excluded):
start_new_session=True / start_new_session=<anything but False>, or a ``"start_new_session"``
dict key / ``kwargs["start_new_session"] = ...`` item with a non-False value
process_group=<anything but None> (``process_group=0`` puts the child in a new group), in the
same three shapes
any reference to os.setsid / os.setpgid / os.setpgrp (call or ``preexec_fn=os.setsid``)
a call to ``windows_detach_popen_kwargs()`` (``start_new_session=True`` on POSIX)
Not caught: a PTY spawn (``pty.fork``, ``ptyprocess.PtyProcess.spawn``, which always start a new
session), aliased imports (``from os import setsid``, ``import os as _os``), ``os.posix_spawn(...,
setsid=True)`` and ``kw.setdefault("start_new_session", True)``. None is used to start a long-lived
child in production today; a review still has to catch a new one.
Opt out of one line with ``detached-spawn: ok — <why>`` on that line or the line directly above
it; a marker without a reason after the dash does not count. Legitimate reasons: the child must
OUTLIVE its owner on purpose (an updater relaunching the gateway, a restart watcher, a detached
service launch), or the child is awaited with a hard timeout that kills its group (the group
exists only so that kill reaches grandchildren). That timeout runs in the owner, so an owner killed
mid-wait leaves such a child to finish on its own: mark it only when that worst case is acceptable.
``_BASELINE`` maps a file to a tolerated count of unmarked hits. It is empty: every detached
spawn site is on ``spawn_owned`` or marked with its reason. Keep it that way; an entry is a
temporary burn-down, and an overstated one is an advisory (``--strict-baseline`` fails on it).
Run: python scripts/check_detached_spawns.py [--all] [--print-baseline] [--strict-baseline] [paths...]
Exit 1 on any violation, 0 when clean.
"""
from __future__ import annotations
import argparse
import ast
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.append(str(ROOT))
from hermes_agent.compat.paths import current_path, original_path
MARKER = "detached-spawn: ok"
# The helper itself: the one place a detached spawn is the point.
HELPER_FILES = {"hermes_cli/process_identity.py"}
SCAN_TREES = ("agent", "cron", "gateway", "hermes_cli", "plugins", "tools", "tui_gateway", "acp_adapter", "pm", "utils",
"persistence", "browser", "speech")
SCAN_ROOT_FILES = ("cli.py", "run_agent.py", "hermes_constants.py")
SKIP_PARTS = {"tests", "__pycache__", "evals", "node_modules"}
_DETACH_OS_ATTRS = {"setsid", "setpgid", "setpgrp"}
# Helpers that return detaching Popen kwargs (``start_new_session=True`` on POSIX).
_DETACH_HELPERS = {"windows_detach_popen_kwargs"}
# Cheap text prefilter: only files mentioning one of these are parsed.
_NEEDLES = ("start_new_session", "process_group", "setsid", "setpgid", "setpgrp", *_DETACH_HELPERS)
def _is_false(node: ast.AST) -> bool:
return isinstance(node, ast.Constant) and node.value in (False, None, 0)
def _detaches(name: object, value: ast.AST) -> bool:
"""Whether Popen keyword ``name`` set to ``value`` detaches the child into its own group."""
if name == "start_new_session":
return not _is_false(value)
# process_group=0 is a new group; only the default (None) leaves the child in ours.
return name == "process_group" and not (isinstance(value, ast.Constant) and value.value is None)
def _is_os_detach(node: ast.AST) -> bool:
return (isinstance(node, ast.Attribute) and node.attr in _DETACH_OS_ATTRS
and isinstance(node.value, ast.Name) and node.value.id == "os")
def _is_detach_helper_call(node: ast.AST) -> bool:
return isinstance(node, ast.Call) and (getattr(node.func, "id", None) in _DETACH_HELPERS
or getattr(node.func, "attr", None) in _DETACH_HELPERS)
def detach_lines(text: str) -> list[int]:
"""Line numbers of detached-spawn shapes in ``text`` (a Python module)."""
if not any(needle in text for needle in _NEEDLES):
return []
try:
tree = ast.parse(text)
except (SyntaxError, ValueError):
return []
lines: set[int] = set()
for node in ast.walk(tree):
if isinstance(node, ast.keyword) and _detaches(node.arg, node.value):
lines.add(node.value.lineno)
elif isinstance(node, ast.Dict):
for key, value in zip(node.keys, node.values):
if isinstance(key, ast.Constant) and _detaches(key.value, value):
lines.add(key.lineno)
elif isinstance(node, ast.Assign): # kwargs["start_new_session"] = True
for target in node.targets:
if (isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Constant)
and _detaches(target.slice.value, node.value)):
lines.add(target.lineno)
elif _is_os_detach(node) or _is_detach_helper_call(node):
lines.add(node.lineno)
return sorted(lines)
# The marker counts only with a reason: ``detached-spawn: ok — <why>`` (an em dash or hyphens).
_MARKED = re.compile(re.escape(MARKER) + r"\s*(?:—|-+)\s*\S")
def _marked(src_lines: list[str], lineno: int) -> bool:
here = src_lines[lineno - 1] if lineno - 1 < len(src_lines) else ""
above = src_lines[lineno - 2] if lineno >= 2 else ""
return bool(_MARKED.search(here) or _MARKED.search(above))
def _tracked_files(root: Path) -> list[Path]:
try:
out = subprocess.run(["git", "-C", str(root), "ls-files", "-z", "--", *map(current_path, (*SCAN_TREES, *SCAN_ROOT_FILES))],
capture_output=True, text=True, check=True, stdin=subprocess.DEVNULL, timeout=60).stdout
except (OSError, subprocess.SubprocessError) as exc:
# Fail closed: scanning nothing would report "no detached spawns".
raise RuntimeError(f"cannot list the tracked files under {root}: {exc}") from exc
return [root / rel for rel in out.split("\0") if rel.endswith(".py")]
def _in_scope(rel: str) -> bool:
parts = Path(rel).parts
return not (SKIP_PARTS & set(parts)) and not Path(rel).name.startswith("test_") and original_path(rel) not in HELPER_FILES
def scan(paths=None, root: Path | None = None) -> dict[str, list[tuple[int, str]]]:
"""Repo-relative path -> [(lineno, line)] of unmarked detached spawns."""
root = (root or ROOT).resolve()
hits: dict[str, list[tuple[int, str]]] = {}
for path in (list(paths) if paths else _tracked_files(root)):
path = Path(path).resolve()
try:
rel = path.relative_to(root).as_posix()
except ValueError:
rel = str(path)
if path.suffix != ".py" or not _in_scope(rel):
continue
try:
text = path.read_text(encoding="utf-8-sig", errors="ignore")
except OSError:
continue
src = text.splitlines()
found = [(n, src[n - 1]) for n in detach_lines(text) if not _marked(src, n)]
if found:
hits[rel] = found
return hits
# Files that spawned unmarked detached children when this check landed. Burn-down list:
# migrate to spawn_owned / own_process_groups, or mark the line with a reason, then drop the entry.
_BASELINE: dict[str, int] = {
}
def _format_baseline(hits: dict[str, list]) -> str:
body = "".join(f' "{rel}": {len(found)},\n' for rel, found in sorted((original_path(r), h) for r, h in hits.items()))
return "_BASELINE: dict[str, int] = {\n" + body + "}"
def main(argv: list[str] | None = None) -> int:
ap = argparse.ArgumentParser(description=(__doc__ or "").splitlines()[0])
ap.add_argument("paths", nargs="*", help="files to check (default: the production trees)")
ap.add_argument("--all", action="store_true", help="ignore _BASELINE; report every hit")
ap.add_argument("--print-baseline", action="store_true", help="print a _BASELINE for the current tree")
ap.add_argument("--strict-baseline", action="store_true", help="also fail when _BASELINE overstates a file")
args = ap.parse_args(argv)
try:
hits = scan(args.paths or None)
except RuntimeError as exc:
print(f"check_detached_spawns: {exc}", file=sys.stderr)
return 2
if args.print_baseline:
print(_format_baseline(hits))
return 0
baseline = {} if (args.all or args.paths) else _BASELINE
problems: list[str] = []
advisories: list[str] = []
for rel in sorted(hits):
allowed = baseline.get(original_path(rel))
if allowed is not None and len(hits[rel]) <= allowed:
if len(hits[rel]) < allowed:
advisories.append(f"{rel}: {len(hits[rel])} detached spawn(s) left, _BASELINE says {allowed}")
continue
problems.extend(f"{rel}:{n}: {line.strip()[:160]}" for n, line in hits[rel])
current = {original_path(rel) for rel in hits}
advisories.extend(f"{rel}: listed in _BASELINE but clean (or gone)" for rel in sorted(baseline) if rel not in current)
if advisories:
print("advisory — _BASELINE in scripts/check_detached_spawns.py is stale; regenerate it with "
"--print-baseline (fewer hits than listed is progress, not a failure):")
print("\n".join(" " + a for a in advisories))
if args.strict_baseline:
problems.extend(advisories)
if not problems:
print("no detached spawns outside the owned-child helper and the baseline")
return 0
print("\n".join(problems))
print(f"\n{len(problems)} detached spawn(s) flagged. A child in its own session/group outlives a "
"SIGKILLed Hermes. Start long-lived children with hermes_cli.process_identity.spawn_owned "
"(or hand the group to own_process_groups), or mark a child that must outlive its owner / "
f"is awaited with a group-killing timeout with `{MARKER} — <why>` (same line or the line above).")
return 1
if __name__ == "__main__":
sys.exit(main())