Skip to content

The agent that grows with you

fix(scratch): owner lookup follows the configured home; orphaned MCP helpers are not owned

Tekniumb76610b7c1ae
diff --git a/hermes_constants_scratch.py b/hermes_constants_scratch.py
index 3991b8d38ea..81b1c0e2f8c 100644
--- a/hermes_constants_scratch.py
+++ b/hermes_constants_scratch.py
@@ -170,6 +170,7 @@ def _registered_live_pids(home: Path) -> set[int]:
matches is a reused pid, not ownership."""
import psutil
+ from hermes_cli.process_identity import spawner_is_dead
from hermes_constants import get_default_hermes_root
root = get_default_hermes_root(home=home)
@@ -185,6 +186,11 @@ def _registered_live_pids(home: Path) -> set[int]:
pid, stamp = (entry.get("pid"), entry.get(stamp_key)) if isinstance(entry, dict) else (None, None)
if not isinstance(pid, int) or pid <= 0 or entry.get("pid_scope", "host") != "host":
continue
+ # An MCP helper whose recorded spawner is provably gone is an orphan, not owned: the
+ # rule reap_orphaned_mcp_helpers applies. Long-lived entry points (gateway, serve) that
+ # outlive their launcher stay owned; an unprovable spawner keeps ownership.
+ if entry.get("purpose") == "mcp-helper" and spawner_is_dead(entry) is True:
+ continue
try:
if stamp is None:
alive = psutil.pid_exists(pid)
@@ -250,7 +256,9 @@ def reap_processes_rooted_in(scratch_root: Path, doomed: list[Path], audit: logg
continue
if deleted or not os.path.exists(cwd_path) or any(_under(cwd_path, t) for t in targets):
(lineage if proc.pid in skip else victims).append((proc, cwd_path))
- registered = _registered_live_pids(Path(root).parent.parent) if victims else set()
+ # The owning home comes from the scratch path as configured, not its resolved target:
+ # a cache/scratch symlinked onto another disk still belongs to <home>.
+ registered = _registered_live_pids(Path(scratch_root).absolute().parent.parent) if victims else set()
owned = lineage + [(proc, cwd_path) for proc, cwd_path in victims if _owned_by_hermes(proc, registered)]
for proc, cwd_path in owned:
for entry in [e for e in doomed if _under(cwd_path, os.path.realpath(str(e)))]:
diff --git a/tests/test_scratch_dir.py b/tests/test_scratch_dir.py
index aa8ade9a663..cb6de8b23c8 100644
--- a/tests/test_scratch_dir.py
+++ b/tests/test_scratch_dir.py
@@ -622,3 +622,55 @@ def test_prune_keeps_entry_the_pruning_sessions_own_lineage_works_in(tmp_path, m
assert prune_scratch_dir(scratch) == 0
assert session_dir.exists()
+
[email protected]_system_guard_bypass # the helper outlives the spawner the test killed
+def test_prune_owner_lookup_survives_symlinked_scratch_and_drops_orphaned_mcp_helpers(tmp_path, monkeypatch):
+ """#132401 review: the owning home comes from the configured scratch path even when it is a
+ symlink onto another disk, and an MCP helper whose recorded spawner died is an orphan again
+ (reaped, entry removed) while a live background worker keeps its entry."""
+ from hermes_cli import process_identity
+ from tools.process_registry import ProcessRegistry
+
+ home = tmp_path / "home"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ elsewhere = tmp_path / "other-disk" / "scratch"
+ elsewhere.mkdir(parents=True)
+ (home / "cache").mkdir()
+ (home / "cache" / "scratch").symlink_to(elsewhere, target_is_directory=True)
+ scratch = home / "cache" / "scratch"
+ owned_dir, helper_dir = scratch / "live-worker", scratch / "orphan-helper"
+ owned_dir.mkdir()
+ helper_dir.mkdir()
+ sleeper = [sys.executable, "-c", "import time; time.sleep(60)"]
+ registry = ProcessRegistry()
+ worker = registry.spawn_local(" ".join(shlex.quote(a) for a in sleeper), cwd=str(owned_dir))
+ helper = subprocess.Popen(sleeper, cwd=str(helper_dir), stdin=subprocess.DEVNULL)
+ # A real spawner registers the helper the way the MCP client does, then dies.
+ repo = str(Path(__file__).resolve().parents[1])
+ spawner = subprocess.Popen(
+ [sys.executable, "-c",
+ f"import sys; sys.path.insert(0, {repo!r}); from hermes_cli.process_identity import register_child; "
+ f"assert register_child({helper.pid}, 'mcp-helper'); import time; time.sleep(60)"],
+ stdin=subprocess.DEVNULL, env={**os.environ, "HERMES_HOME": str(home)})
+ wait_until(lambda: helper.pid in {e.get("pid") for e in process_identity.ledger_entries(all_installs=True)},
+ timeout=20, what="spawner registered the mcp-helper")
+ spawner.kill()
+ spawner.wait(timeout=10)
+ assert process_identity.spawner_is_dead(next(
+ e for e in process_identity.ledger_entries(all_installs=True) if e.get("pid") == helper.pid)) is True
+ ancient = time.time() - 30 * 3600
+ for path in (owned_dir, helper_dir):
+ os.utime(path, (ancient, ancient))
+ try:
+ assert prune_scratch_dir(scratch) == 1
+ assert owned_dir.exists() and not helper_dir.exists()
+ assert worker.process.poll() is None, "registered worker under a symlinked scratch was reaped"
+ assert helper.wait(timeout=10) is not None, "orphaned mcp-helper was kept"
+ finally:
+ registry.kill_process(worker.id)
+ if helper.poll() is None:
+ helper.kill()
+ helper.wait(timeout=10)
+