Skip to content

The agent that grows with you

fix(update): credit a gateway restart by successor incarnation, not service name

DavidMetcalfed5c8097909a7
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.py
index 2c374ecb193..21023ca9fa9 100644
--- a/hermes_cli/update_cmd_fleet.py
+++ b/hermes_cli/update_cmd_fleet.py
@@ -1899,6 +1899,24 @@ def _restarted_units_gone(scoped_units) -> bool:
return True
+def _live_gateway_pids_from_fleet(fleet_rows: list) -> dict:
+ """Profile -> gateway PIDs alive after the restart phase, from the fleet snapshot.
+
+ Incarnation evidence for gateway reconciliation (``match_runtime_outcomes``'s
+ ``live_gateway_pids``): the plan identifies a gateway by the profile it SERVES while the restart
+ bookkeeping names the SERVICE, and a service can serve a profile its name does not encode — the
+ profile-scoped name matcher then never credits the planned runtime. A ``down`` row reports the
+ PRE-restart PID (nothing replaced it), so it is never a successor; rows without a usable
+ profile/PID are skipped.
+ """
+ live: dict = {}
+ for row in fleet_rows:
+ pid, profile = row.get("pid"), row.get("profile")
+ if profile and isinstance(pid, int) and row.get("state") != "down":
+ live.setdefault(str(profile), set()).add(pid)
+ return live
+
+
def _verify_fleet_after_update(restart, *, _pre_update_plan, _windows_gateway_resume, update_complete):
"""Post-restart verification: legacy-unit warning, dashboard cleanup, stale serve
probe, fleet version matrix, plan-vs-execution reconciliation, receipt finalize.
@@ -1990,6 +2008,10 @@ def _verify_fleet_after_update(restart, *, _pre_update_plan, _windows_gateway_re
if _pre_update_plan is not None and _pre_update_plan.runtimes:
from hermes_cli.update_inventory import (match_runtime_outcomes, report_unaccounted_runtimes)
from hermes_cli.update_receipt import row_is_external
+ # Gateway incarnation evidence: a service can serve a profile its own name does not
+ # encode (root-home launchd label + sticky active profile, hashed custom HERMES_HOME),
+ # so the bookkeeping's service names alone cannot credit the planned runtime.
+ _live_gateway_pids = _live_gateway_pids_from_fleet(_fleet_snapshot)
_runtime_outcomes = match_runtime_outcomes(
_pre_update_plan,
restarted_services=restart.restarted_services,
@@ -2007,6 +2029,7 @@ def _verify_fleet_after_update(restart, *, _pre_update_plan, _windows_gateway_re
failed_respawn_pids=_dashboards_down,
# A symlinked profile served by another install's checkout (#120240).
external_gateway_pids={row.get("pid") for row in _fleet_snapshot if row_is_external(row)},
+ live_gateway_pids=_live_gateway_pids,
)
from dataclasses import asdict
from hermes_cli.update_serve_obligations import defer_manual_serve
diff --git a/hermes_cli/update_inventory.py b/hermes_cli/update_inventory.py
index 7eadeed7d27..5483ca5e520 100644
--- a/hermes_cli/update_inventory.py
+++ b/hermes_cli/update_inventory.py
@@ -389,6 +389,7 @@ def match_runtime_outcomes(
externally_supervised_profiles: list, killed_pids: set, failed_units: list,
stale_serve_pids: "set | None" = None, failed_respawn_pids: "set | None" = None,
external_gateway_pids: "set | None" = None,
+ live_gateway_pids: "dict[str, set[int]] | None" = None,
) -> list[dict[str, Any]]:
"""Reconcile the plan's runtimes against what the restart phase DID.
@@ -408,6 +409,16 @@ def match_runtime_outcomes(
(#109290). The probe itself fails closed (unreadable ledger -> every planned serve is listed as
surviving), so ``deferred`` means "not shown to be gone", not "observed alive". See #111494.
+ ``live_gateway_pids`` (profile -> gateway PIDs alive for that profile AFTER the restart phase:
+ control-socket identity, or a runtime-status record whose PID is still live) carries the same
+ incarnation evidence for gateways: the plan identifies a gateway by the
+ profile it SERVES, while the restart bookkeeping names the SERVICE it runs under, and those two
+ disagree whenever a service serves a profile its name does not encode — a root-home launchd
+ label running the sticky active profile, a hash-suffixed systemd unit for a custom
+ ``HERMES_HOME``. No name rule can bridge that, so a planned PID that is gone while a gateway
+ answers for the same profile counts as ``restarted``. A missing successor, or the planned PID
+ still answering, stays ``unaccounted`` — the tripwire keeps its teeth.
+
See #91277.
They never borrow the gateway's outcome: ``relaunched_profiles`` and ``hermes-gateway*`` name a
different process that shares the profile, nothing more. See #100479.
@@ -426,6 +437,11 @@ def match_runtime_outcomes(
stale_serves = {int(p) for p in stale_serve_pids} if stale_serve_pids is not None else None
failed_respawns = {int(p) for p in (failed_respawn_pids or set())}
external = {p for p in (external_gateway_pids or ()) if isinstance(p, int)}
+ successors = (
+ {str(profile): {p for p in pids if isinstance(p, int)} for profile, pids in live_gateway_pids.items()}
+ if live_gateway_pids is not None
+ else None
+ )
def _outcome(r: RuntimeRecord) -> str:
killed_here = r.pid is not None and r.pid in killed
@@ -455,6 +471,14 @@ def match_runtime_outcomes(
return "failed"
if _gateway_named_in(r, restarted_set):
return "restarted"
+ if successors is not None and r.pid is not None:
+ # Incarnation-verified last resort: the planned process is gone and a gateway answers
+ # for the same profile now. Name-independent on purpose — the supervising service's
+ # label/unit may encode the install root (or a hashed home) instead of the served
+ # profile, which the profile-scoped name matcher above can never credit.
+ live = successors.get(r.profile)
+ if live and r.pid not in live:
+ return "restarted"
return "external" if r.pid in external else "unaccounted"
for r in plan.runtimes:
diff --git a/tests/hermes_cli/test_restart_plan_reconciliation.py b/tests/hermes_cli/test_restart_plan_reconciliation.py
index 017a47e4bae..48cbdc02bdc 100644
--- a/tests/hermes_cli/test_restart_plan_reconciliation.py
+++ b/tests/hermes_cli/test_restart_plan_reconciliation.py
@@ -393,3 +393,119 @@ def test_mixed_fleet_only_the_missed_one_escalates(capsys):
assert "ghost" in missed_block
assert "[default]" not in missed_block
assert "[work]" not in missed_block
+
+
+def test_gateway_credited_by_successor_incarnation_not_service_name():
+ """A service can serve a profile its own name does not encode: with a sticky
+ active profile, the root-home ``ai.hermes.gateway`` LaunchAgent supervises the
+ ``coder`` gateway, so the plan row (profile ``coder``) and the restarted label
+ never match by name. The planned PID being gone while a gateway answers for the
+ same profile is the evidence that has to credit it — otherwise every update
+ exits 1 (receipt outcome=partial) after a successful launchd restart."""
+ outcomes = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=["ai.hermes.gateway"], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(), failed_units=[],
+ live_gateway_pids={"coder": {76796}},
+ )
+ assert outcomes[0]["outcome"] == "restarted"
+ assert report_unaccounted_runtimes(outcomes) is False
+
+
+def test_gateway_successor_credit_requires_a_live_replacement():
+ """The tripwire keeps its teeth: no successor (the gateway was stopped and
+ nothing replaced it) or the planned PID still answering (never restarted)
+ stays unaccounted and escalates."""
+ no_successor = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=["ai.hermes.gateway"], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(), failed_units=[],
+ live_gateway_pids={},
+ )
+ assert no_successor[0]["outcome"] == "unaccounted"
+ assert report_unaccounted_runtimes(no_successor) is True
+
+ never_restarted = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=[], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(), failed_units=[],
+ live_gateway_pids={"coder": {76508}},
+ )
+ assert never_restarted[0]["outcome"] == "unaccounted"
+
+ other_profile_only = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=[], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(), failed_units=[],
+ live_gateway_pids={"default": {76796}},
+ )
+ assert other_profile_only[0]["outcome"] == "unaccounted"
+
+ empty_evidence = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=["ai.hermes.gateway"], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(), failed_units=[],
+ live_gateway_pids={"coder": set()},
+ )
+ assert empty_evidence[0]["outcome"] == "unaccounted"
+
+
+def test_successor_evidence_never_outranks_stopped_or_failed():
+ """Bookkeeping verdicts stay authoritative when the incarnation evidence is
+ also passed: the successor branch is reached only after them, so a killed or
+ name-failed gateway cannot be promoted to ``restarted``."""
+ stopped = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=[], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids={76508}, failed_units=[],
+ live_gateway_pids={"coder": {76796}},
+ )
+ assert stopped[0]["outcome"] == "stopped"
+
+ failed = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=[], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(),
+ failed_units=["ai.hermes.gateway-coder"],
+ live_gateway_pids={"coder": {76796}},
+ )
+ assert failed[0]["outcome"] == "failed"
+
+
+def test_live_gateway_pids_from_fleet_skips_down_and_unusable_rows():
+ """The fleet snapshot is the successor evidence. A ``down`` row carries the
+ PRE-restart PID (nothing replaced it) so it must never count as a successor;
+ rows without a usable profile/PID are skipped; a ``stale`` row is a live
+ successor (the fleet matrix escalates staleness on its own)."""
+ from hermes_cli.update_cmd_fleet import _live_gateway_pids_from_fleet
+
+ rows = [
+ {"profile": "coder", "pid": 76796, "state": "current"},
+ {"profile": "coder", "pid": 76508, "state": "down"},
+ {"profile": "default", "pid": 4242, "state": "stale"},
+ {"profile": "default", "pid": None, "state": "unknown"},
+ {"profile": "", "pid": 7, "state": "current"},
+ {"profile": "researcher", "pid": "not-a-pid", "state": "current"},
+ ]
+ assert _live_gateway_pids_from_fleet(rows) == {"coder": {76796}, "default": {4242}}
+ assert _live_gateway_pids_from_fleet([]) == {}
+
+
+def test_successor_evidence_is_gateway_only_and_stays_optional():
+ """Serve/dashboard rows reconcile in their own vocabulary, and callers that
+ pass no ``live_gateway_pids`` keep the bookkeeping-only verdict."""
+ outcomes = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd"), _serve("coder", 900)),
+ restarted_services=[], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(), failed_units=[],
+ live_gateway_pids={"coder": {76796}},
+ )
+ by_pid = {o["pid"]: o["outcome"] for o in outcomes}
+ assert by_pid == {76508: "restarted", 900: "unaccounted"}
+
+ without_probe = match_runtime_outcomes(
+ _plan(_rt("coder", 76508, supervisor="launchd")),
+ restarted_services=["ai.hermes.gateway"], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(), failed_units=[],
+ )
+ assert without_probe[0]["outcome"] == "unaccounted"