Skip to content

The agent that grows with you

fix(update): classify launchd-owned serve/dashboard rows as launchd, not manual-serve

liuhao1024ca7200fc407b
diff --git a/hermes_cli/update_abort_recovery.py b/hermes_cli/update_abort_recovery.py
index 0e3e31fb88d..26acc1f6c04 100644
--- a/hermes_cli/update_abort_recovery.py
+++ b/hermes_cli/update_abort_recovery.py
@@ -230,6 +230,8 @@ def _warn_stale_serve_runtimes(rows) -> None:
print(" Ask their owner to relaunch `hermes serve` / `hermes dashboard`, or reconnect Desktop for an SSH backend.")
if sys.platform == "linux" and any(row.get("supervisor") == "systemd" for row in rows):
print(" For unit-managed backends: `systemctl --user restart hermes-serve.service`.")
+ if sys.platform == "darwin" and any(row.get("supervisor") == "launchd" for row in rows):
+ print(" For launchd-managed backends: `launchctl kickstart -k gui/$UID/<label>`.")
def _owed_stale_serve_rows(rows) -> list[dict]:
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.py
index c97e0dc2109..585d03a3e16 100644
--- a/hermes_cli/update_cmd_fleet.py
+++ b/hermes_cli/update_cmd_fleet.py
@@ -889,6 +889,13 @@ _SERVE_SKIP_REASON = (
" systemd unit pass when it owns a hermes-serve* unit, else left running for explicit"
" operator restart"
)
+# A launchd-owned backend (#116503): the fresh child has no per-label kickstart for serve/dashboard
+# jobs, but the post-update dashboard cleanup pass kickstarts the loaded job — never a detached
+# argv respawn, which would fight the job's own KeepAlive.
+_LAUNCHD_SERVE_SKIP_REASON = (
+ "launchd job owns this backend; the post-update dashboard cleanup kickstarts the job through"
+ " launchd, never a detached argv respawn that would fight its KeepAlive"
+)
def _gateway_recovery_partition(plan, *, skip_profiles: set[str] | None = None) -> tuple[dict[str, str], list[dict]]:
@@ -920,7 +927,12 @@ def _gateway_recovery_partition(plan, *, skip_profiles: set[str] | None = None)
continue
reason = _MANUAL_GATEWAY_SKIP_REASON
elif kind in ("serve", "dashboard"):
- reason = _DESKTOP_SERVE_SKIP_REASON if supervisor == "desktop" else _SERVE_SKIP_REASON
+ if supervisor == "desktop":
+ reason = _DESKTOP_SERVE_SKIP_REASON
+ elif supervisor == "launchd":
+ reason = _LAUNCHD_SERVE_SKIP_REASON
+ else:
+ reason = _SERVE_SKIP_REASON
else:
continue
skipped.append({"profile": profile, "kind": str(kind), "supervisor": str(supervisor), "reason": reason})
diff --git a/hermes_cli/update_inventory.py b/hermes_cli/update_inventory.py
index 0c46d232455..f20ec076765 100644
--- a/hermes_cli/update_inventory.py
+++ b/hermes_cli/update_inventory.py
@@ -8,6 +8,7 @@ side-effect-free probe, so ``hermes update --plan`` is safe on a live fleet.
from __future__ import annotations
import logging
+import shlex
import sys
from contextlib import contextmanager, suppress
from dataclasses import dataclass, field, asdict, fields as dataclass_fields
@@ -217,14 +218,49 @@ def _collect_gateway_runtimes(plan: UpdatePlan, profile_homes: list, seen: set[i
plan.runtimes.append(_runtime("gateway", proc.profile, proc.pid, supervisor(proc.pid)))
+def _loaded_backend_launchd_jobs() -> list:
+ """Loaded launchd dashboard/serve jobs for supervisor classification.
+
+ The probe itself is darwin-gated (``[]`` on every other host); here any failure also degrades
+ to ``[]`` — classification falls back to the spawner probe and never aborts the inventory.
+ See #116503."""
+ with suppress(Exception):
+ from hermes_cli import main_dashboard as _dash
+
+ return _dash._loaded_launchd_backend_jobs()
+ return []
+
+
+def _launchd_owner_for_ledger_entry(entry: dict, pid: int, jobs: list) -> "tuple[str, str, int | None] | None":
+ """``(domain, label, live_pid)`` of the loaded launchd job owning this ledger row, if any.
+
+ A KeepAlive LaunchAgent backend's recorded spawner (the bootstrap shell) is long dead, so the
+ spawner probe alone misreads the row as ``manual-serve`` — and a respawn-argv restart then
+ fights the job's own KeepAlive respawn. The loaded-job match (live PID, an ancestor, or the
+ normalized ``ProgramArguments``) is the authoritative classification. See #116503."""
+ with suppress(Exception):
+ from hermes_cli import main_dashboard as _dash
+ from hermes_cli.dashboard_procs import _process_ancestors
+
+ try:
+ cmdline = shlex.split(str(entry.get("argv") or "")) or None
+ except ValueError:
+ cmdline = None
+ return _dash._launchd_job_owning_backend(pid, cmdline, jobs, ancestors=_process_ancestors(pid))
+ return None
+
+
def _collect_ledger_runtimes(plan: UpdatePlan, seen: set[int]) -> None:
"""Serve/dashboard backends from the spawn ledger — runtimes the gateway collectors can never see
(a manual `hermes serve --host <ip>` for a remote Desktop, a long-lived `hermes dashboard`).
ledger_entries() live-verifies (pid, create_time) so PID reuse never fabricates a row. Desktop-
- supervised backends (spawner still alive) restart via the Desktop's own respawn, not ours."""
+ supervised backends (spawner still alive) restart via the Desktop's own respawn, not ours.
+ A backend owned by a loaded launchd job is classified ``launchd`` (kickstart restart, never a
+ detached argv respawn) — the spawner probe cannot see that (#116503)."""
with _probe("Serve/dashboard ledger inventory"):
from hermes_cli.process_identity import ledger_entries, spawner_is_dead
+ launchd_jobs = _loaded_backend_launchd_jobs()
for entry in ledger_entries():
purpose, pid = entry.get("purpose"), entry.get("pid")
if purpose not in _SERVE_KINDS or not isinstance(pid, int) or pid in seen:
@@ -232,13 +268,17 @@ def _collect_ledger_runtimes(plan: UpdatePlan, seen: set[int]) -> None:
seen.add(pid)
# detail.create_time: process incarnation, not just the numeric PID — a post-update
# survivor probe comparing PIDs alone calls a NEW serve that reused the number a survivor.
+ detail = {
+ "argv": entry.get("argv") or "", "host": entry.get("host") or "",
+ "port": entry.get("port"), "create_time": entry.get("create_time"),
+ }
+ job = _launchd_owner_for_ledger_entry(entry, pid, launchd_jobs) if launchd_jobs else None
+ if job:
+ supervisor, detail["launchd_domain"], detail["launchd_label"] = "launchd", job[0], job[1]
+ else:
+ supervisor = "desktop" if spawner_is_dead(entry) is False else "manual-serve"
plan.runtimes.append(_runtime(
- str(purpose), str(entry.get("profile") or "default"), pid,
- "desktop" if spawner_is_dead(entry) is False else "manual-serve",
- detail={
- "argv": entry.get("argv") or "", "host": entry.get("host") or "",
- "port": entry.get("port"), "create_time": entry.get("create_time"),
- },
+ str(purpose), str(entry.get("profile") or "default"), pid, supervisor, detail=detail,
))
diff --git a/tests/hermes_cli/test_manual_serve_deferral.py b/tests/hermes_cli/test_manual_serve_deferral.py
index c2671b4edfe..aea484fff8c 100644
--- a/tests/hermes_cli/test_manual_serve_deferral.py
+++ b/tests/hermes_cli/test_manual_serve_deferral.py
@@ -213,3 +213,13 @@ def test_unreadable_create_time_warning_names_identity_not_storage(monkeypatch,
assert "could not read the process creation time" in out
assert "storage permissions" not in out
assert "relaunch" in out
+
+
+def test_launchd_serve_row_never_pessimize_gateway_coverage():
+ """#116503: a launchd-owned serve/dashboard row is the post-update dashboard cleanup pass's
+ to kickstart, so it must not make the receipt's gateway coverage unverified (owed=None keeps
+ ``fleet_restart_pending`` armed with nothing gateway-side left to restart)."""
+ launchd = asdict(RuntimeRecord(
+ kind="serve", profile="work", pid=900, supervisor="launchd", restart_via="launchd", detail={}))
+ receipt = {"plan": {"runtimes": [{"kind": "gateway", "profile": "default"}, launchd]}, "fleet": []}
+ assert fleet._receipt_owed_gateways(receipt, []) == {("gateway", "default")}
diff --git a/tests/hermes_cli/test_serve_runtime_inventory.py b/tests/hermes_cli/test_serve_runtime_inventory.py
index 553ba4d410b..5e3e71ec437 100644
--- a/tests/hermes_cli/test_serve_runtime_inventory.py
+++ b/tests/hermes_cli/test_serve_runtime_inventory.py
@@ -245,3 +245,73 @@ def test_inventory_records_the_serve_process_incarnation(monkeypatch):
plan = update_inventory.collect_runtime_inventory()
serves = [r for r in plan.runtimes if r.kind == "serve"]
assert serves and serves[0].detail["create_time"] == 1712345678.5
+
+
+# ---------------------------------------------------------------------------
+# update_inventory: launchd-owned serve/dashboard classification (#116503)
+# ---------------------------------------------------------------------------
+
+def test_inventory_classifies_launchd_job_owned_serve(monkeypatch):
+ """A KeepAlive LaunchAgent backend's recorded spawner (the bootstrap shell) is long dead,
+ so the spawner probe alone reads manual-serve — and the update plan then restarts it as a
+ detached argv respawn that fights the job's own KeepAlive respawn. A loaded job whose
+ ProgramArguments match the ledger argv must classify the row launchd (kickstart restart)."""
+ entry = _ledger_entry(spawner_pid=999, spawner_create=1.0)
+ fake_pi = SimpleNamespace(
+ ledger_entries=lambda **k: [entry],
+ spawner_is_dead=lambda e: True, # bootstrap shell provably gone
+ )
+ jobs = [("gui/501", "ai.hermes.dashboard",
+ ["hermes", "serve", "--host", "100.94.65.93", "--port", "9119"], None)]
+ monkeypatch.setitem(sys.modules, "hermes_cli.process_identity", fake_pi)
+ with patch.object(main_dashboard, "_loaded_launchd_backend_jobs", return_value=jobs), \
+ patch("hermes_cli.dashboard_procs._process_ancestors", return_value=[]):
+ plan = update_inventory.collect_runtime_inventory()
+ serves = [r for r in plan.runtimes if r.kind == "serve"]
+ assert serves, "launchd-owned serve must appear in the inventory"
+ row = serves[0]
+ assert row.supervisor == "launchd"
+ assert row.restart_via == "launchd"
+ assert row.detail["launchd_domain"] == "gui/501"
+ assert row.detail["launchd_label"] == "ai.hermes.dashboard"
+
+
+def test_inventory_launchd_job_with_other_argv_leaves_manual_classification(monkeypatch):
+ entry = _ledger_entry()
+ fake_pi = SimpleNamespace(
+ ledger_entries=lambda **k: [entry],
+ spawner_is_dead=lambda e: None,
+ )
+ jobs = [("gui/501", "ai.hermes.other", ["hermes", "dashboard", "--port", "8300"], 777)]
+ monkeypatch.setitem(sys.modules, "hermes_cli.process_identity", fake_pi)
+ with patch.object(main_dashboard, "_loaded_launchd_backend_jobs", return_value=jobs), \
+ patch("hermes_cli.dashboard_procs._process_ancestors", return_value=[]):
+ plan = update_inventory.collect_runtime_inventory()
+ serves = [r for r in plan.runtimes if r.kind == "serve"]
+ assert serves and serves[0].supervisor == "manual-serve"
+ assert "launchd_domain" not in serves[0].detail
+
+
+def test_inventory_launchd_probe_failure_degrades_to_spawner_classification(monkeypatch):
+ """The launchd probe is advisory: launchctl failing mid-inventory must degrade to the spawner
+ classification, never abort the (read-only) inventory pass."""
+ entry = _ledger_entry()
+ fake_pi = SimpleNamespace(
+ ledger_entries=lambda **k: [entry],
+ spawner_is_dead=lambda e: None,
+ )
+ monkeypatch.setitem(sys.modules, "hermes_cli.process_identity", fake_pi)
+ with patch.object(main_dashboard, "_loaded_launchd_backend_jobs", side_effect=OSError("launchctl busy")):
+ plan = update_inventory.collect_runtime_inventory()
+ serves = [r for r in plan.runtimes if r.kind == "serve"]
+ assert serves and serves[0].supervisor == "manual-serve"
+
+
+def test_stale_serve_warning_names_the_launchd_kickstart_command(monkeypatch, capsys):
+ from hermes_cli import update_abort_recovery
+
+ monkeypatch.setattr(update_abort_recovery.sys, "platform", "darwin")
+ update_abort_recovery._warn_stale_serve_runtimes(
+ [{"pid": 4321, "kind": "dashboard", "profile": "default", "supervisor": "launchd"}])
+ out = capsys.readouterr().out
+ assert "launchctl kickstart -k gui/$UID/<label>" in out