Files
hermes-agent/scripts/check_detached_spawns.py
T
kshitijk4poor d1cb5d5cbd ci(process): no new detached spawn outside spawn_owned without a reason
A child started in its own session or group (start_new_session, os.setsid /
setpgid / setpgrp, windows_detach_popen_kwargs) does not die with a SIGKILLed
Hermes: it reparents to init and runs forever. That is how every family this PR
migrates leaked. With the families on spawn_owned, this keeps the class closed.

scripts/check_detached_spawns.py (AST) flags those shapes in the production trees
unless the line, or the line above it, says `detached-spawn: ok — <why>`. It runs
in scripts/check ("detached-spawns") and as a blocking lint.yml step. The baseline
is empty: every remaining site is on spawn_owned / own_process_groups, or carries
its reason. The reasons this commit adds are the census verdicts:
- must outlive the starter on purpose: restart watchers, /update and the updater
  hand-offs, dashboard/desktop/service launches, cron and kanban workers
  (restart-safe), the update-custody watchdog itself;
- awaited with a group kill on timeout: cron scripts, rg search, the Browser Use
  CLI, update completion (holds the checkout lock fd);
- covered by its own parent guard: the compute host (compute_host._parent_guard_loop
  flushes sessions; a supervisor KILL could cut that short);
- not a spawn: the wake-word `start_new_session` config key and event field.

tests/scripts/test_check_detached_spawns.py: each shape flagged, non-detached and
commented/string shapes not, the marker opts out on its line or the line above
and nowhere else, tests and the helper are out of scope, main() fails on a new
unmarked spawn, and the repository scan is empty. Teeth: appending an unmarked
start_new_session Popen to tools/file_operations_search.py turns
`scripts/check --only detached-spawns` red (rc 1); restoring it turns it green.
2026-10-29 16:58:09 +05:30

185 lines
8.5 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 with a non-False value
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)
Opt out of one line with ``detached-spawn: ok — <why>`` on that line or the line directly above
it. Legitimate reasons are the ones that stay valid after the owner dies: the child must OUTLIVE
its owner on purpose (an updater relaunching the gateway, a restart watcher, a detached service
launch), or the child is short-lived and awaited with a hard timeout that kills its group (the
group exists only so that kill reaches grandchildren).
``_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 subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
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")
SCAN_ROOT_FILES = ("cli.py", "run_agent.py", "hermes_state.py", "utils.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", "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 _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 node.arg == "start_new_session" and not _is_false(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 key.value == "start_new_session" and not _is_false(value):
lines.add(key.lineno)
elif _is_os_detach(node) or _is_detach_helper_call(node):
lines.add(node.lineno)
return sorted(lines)
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 MARKER in here or MARKER in above
def _tracked_files(root: Path) -> list[Path]:
try:
out = subprocess.run(["git", "-C", str(root), "ls-files", "-z", "--", *SCAN_TREES, *SCAN_ROOT_FILES],
capture_output=True, text=True, check=True, stdin=subprocess.DEVNULL, timeout=60).stdout
except (OSError, subprocess.SubprocessError):
return []
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 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(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)
hits = scan(args.paths or None)
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(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])
advisories.extend(f"{rel}: listed in _BASELINE but clean (or gone)" for rel in sorted(baseline) if rel not in hits)
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())