Skip to content

The agent that grows with you

fix(update): report a symlinked other-install profile gateway as external, not unaccounted

Austin Pickett0f5068732742
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.py
index 49a30b827a5..2c374ecb193 100644
--- a/hermes_cli/update_cmd_fleet.py
+++ b/hermes_cli/update_cmd_fleet.py
@@ -1989,6 +1989,7 @@ def _verify_fleet_after_update(restart, *, _pre_update_plan, _windows_gateway_re
# #91277.
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
_runtime_outcomes = match_runtime_outcomes(
_pre_update_plan,
restarted_services=restart.restarted_services,
@@ -2004,6 +2005,8 @@ def _verify_fleet_after_update(restart, *, _pre_update_plan, _windows_gateway_re
else None
),
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)},
)
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 c95da524a5c..7eadeed7d27 100644
--- a/hermes_cli/update_inventory.py
+++ b/hermes_cli/update_inventory.py
@@ -388,13 +388,14 @@ def match_runtime_outcomes(
plan: "UpdatePlan", *, restarted_services: list, relaunched_profiles: list,
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,
) -> list[dict[str, Any]]:
"""Reconcile the plan's runtimes against what the restart phase DID.
The platform restart branches each re-discover their own targets, so a runtime the plan saw can
be missed with no signal. Returns one ``{kind, profile, pid, mechanism, outcome}`` row per
- planned runtime; outcome is ``restarted``, ``stopped``, ``failed``, ``deferred`` or
- ``unaccounted`` (no bookkeeping mentions it — the blind-spot tripwire). Never raises.
+ planned runtime; outcome is ``restarted``, ``stopped``, ``failed``, ``deferred``, ``external``
+ or ``unaccounted`` (no bookkeeping mentions it — the blind-spot tripwire). Never raises.
Serve/dashboard runtimes are reconciled in their OWN vocabulary and never borrow the gateway's
outcome: with ``stale_serve_pids`` a pre-update serve whose incarnation is gone counts as
``restarted``, one still alive is ``unaccounted``; without the probe an untouched serve stays
@@ -410,6 +411,11 @@ def match_runtime_outcomes(
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.
+
+ ``external_gateway_pids``: gateway pids the post-restart fleet probe verified as serving a
+ DIFFERENT checkout than this updater (a ``profiles/<name>`` symlinked to a separate install).
+ An untouched gateway in that set is ``external`` — another install owns its restart — instead
+ of ``unaccounted``. No evidence keeps the tripwire armed. See #120240.
"""
outcomes: list[dict[str, Any]] = []
try:
@@ -419,6 +425,7 @@ def match_runtime_outcomes(
killed = {int(p) for p in (killed_pids or set())}
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)}
def _outcome(r: RuntimeRecord) -> str:
killed_here = r.pid is not None and r.pid in killed
@@ -446,7 +453,9 @@ def match_runtime_outcomes(
return "stopped"
if _gateway_named_in(r, failed_set):
return "failed"
- return "restarted" if _gateway_named_in(r, restarted_set) else "unaccounted"
+ if _gateway_named_in(r, restarted_set):
+ return "restarted"
+ return "external" if r.pid in external else "unaccounted"
for r in plan.runtimes:
if isinstance(r, RuntimeRecord):
@@ -481,6 +490,13 @@ def report_unaccounted_runtimes(outcomes: list[dict[str, Any]]) -> bool:
action = ("owned by a Desktop connected over SSH; it picks up the update when that Desktop reconnects"
if o.get("mechanism") == "desktop-ssh" else "relaunch the Desktop app to pick up the update")
print(f" • {o['kind']} [{o['profile']}] pid {o['pid']} — {action}")
+ external = [o for o in outcomes if o.get("outcome") == "external"]
+ if external:
+ # Surfaced but not escalated: another install's updater owns these restarts. See #120240.
+ print()
+ print(" ℹ Left to their own install (separate checkout, not restarted by this update):")
+ for o in external:
+ print(f" • {o['kind']} [{o['profile']}] pid {o['pid']}")
missed = [o for o in outcomes if o.get("outcome") == "unaccounted"]
if not missed:
return False
diff --git a/tests/hermes_cli/test_update_external_profile_reconciliation.py b/tests/hermes_cli/test_update_external_profile_reconciliation.py
new file mode 100644
index 00000000000..4ce7666af1e
--- /dev/null
+++ b/tests/hermes_cli/test_update_external_profile_reconciliation.py
@@ -0,0 +1,133 @@
+"""A symlinked profile that is really another install must not fail `hermes update` (#120240).
+
+``~/.hermes/profiles/work -> ~/.hermes-work`` where ``~/.hermes-work`` has its own checkout and
+gateway: the plan inventories that gateway, the restart phase correctly leaves it alone, and the
+fleet matrix already classifies it ``external``. Reconciliation must agree instead of reporting it
+``unaccounted`` and exiting 1.
+"""
+
+import json
+import time
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from hermes_cli import update_cmd_fleet as fleet
+from hermes_cli import update_receipt
+from hermes_cli.update_inventory import (
+ RuntimeRecord,
+ UpdatePlan,
+ match_runtime_outcomes,
+ report_unaccounted_runtimes,
+)
+
+SHA = "a" * 40
+
+
+def _checkout(root: Path) -> Path:
+ (root / "hermes_cli").mkdir(parents=True)
+ (root / "hermes_cli" / "main.py").write_text("", encoding="utf-8")
+ return root
+
+
+def _gateway_state(home: Path, pid: int, entrypoint: Path, sha: str) -> None:
+ home.mkdir(parents=True, exist_ok=True)
+ (home / "gateway_state.json").write_text(json.dumps({
+ "gateway_state": "running", "kind": "hermes-gateway", "pid": pid,
+ "argv": [str(entrypoint), "gateway", "run"], "code_sha": sha, "code_version": "1.0",
+ }), encoding="utf-8")
+
+
+def _gateway(profile: str, pid: int) -> RuntimeRecord:
+ return RuntimeRecord(kind="gateway", profile=profile, pid=pid, supervisor="launchd", restart_via="launchd")
+
+
+def external_profile_host(tmp_path, monkeypatch):
+ """Root install with its own gateway plus ``profiles/work`` symlinked to a separate install."""
+ root = tmp_path / "root_home"
+ updater_main = update_receipt._updater_code_root() / "hermes_cli" / "main.py"
+ _gateway_state(root, 1111, updater_main, SHA)
+
+ other_checkout = _checkout(tmp_path / "other_install" / "hermes-agent")
+ other_home = tmp_path / "other_install" / "home"
+ _gateway_state(other_home, 4242, other_checkout / "hermes_cli" / "main.py", "f" * 40)
+ (root / "profiles").mkdir()
+ (root / "profiles" / "work").symlink_to(other_home, target_is_directory=True)
+
+ monkeypatch.setenv("HERMES_HOME", str(root))
+ monkeypatch.setattr(update_receipt, "_code_identity", lambda refresh=False: {"sha": SHA, "version": "1.0"})
+ monkeypatch.setattr(update_receipt, "_socket_identity", lambda _home: None)
+ live = {root.resolve(): 1111, other_home.resolve(): 4242}
+ monkeypatch.setattr("gateway.status.live_gateway_pid_for_home", lambda home: live.get(Path(home).resolve()))
+ monkeypatch.setattr(fleet, "_time", SimpleNamespace(sleep=lambda _s: None, monotonic=time.monotonic))
+ monkeypatch.setattr(fleet, "_print_legacy_units_warning", lambda: None)
+ monkeypatch.setattr("hermes_cli.update_cmd._finish_dashboard_update_cleanup", lambda *a, **k: None)
+ monkeypatch.setattr("hermes_cli.gateway_migrate.maybe_auto_migrate_after_update", lambda: None)
+ return SimpleNamespace(other_checkout=other_checkout)
+
+
+def _restart_outcome() -> "fleet._GatewayRestartOutcome":
+ # The root's own LaunchAgent was restarted; the other install's gateway was left alone.
+ return fleet._GatewayRestartOutcome(
+ False, [], [1111, 4242], ["ai.hermes.gateway"], [], [], [], set(),
+ )
+
+
+def test_symlinked_external_profile_gateway_does_not_fail_update(external_profile_host, capsys):
+ plan = UpdatePlan(runtimes=[_gateway("default", 1111), _gateway("work", 4242)])
+ update_receipt.begin_update_receipt()
+
+ fleet._verify_fleet_after_update(
+ _restart_outcome(), _pre_update_plan=plan, _windows_gateway_resume=None, update_complete=True,
+ )
+
+ receipt = update_receipt.read_latest_receipt()
+ assert receipt["outcome"] == "success"
+ assert {o["profile"]: o["outcome"] for o in receipt["runtime_outcomes"]} == {
+ "default": "restarted", "work": "external",
+ }
+ out = capsys.readouterr().out
+ assert "never touched" not in out
+ assert "gateway [work] pid 4242" in out # still surfaced, informationally
+
+
+def test_external_profile_does_not_mask_a_missed_own_gateway(external_profile_host):
+ """The root's own gateway missed by the restart phase still fails the update."""
+ plan = UpdatePlan(runtimes=[_gateway("default", 1111), _gateway("work", 4242)])
+ restart = _restart_outcome()
+ restart.restarted_services.clear()
+ update_receipt.begin_update_receipt()
+
+ with pytest.raises(SystemExit) as exc:
+ fleet._verify_fleet_after_update(
+ restart, _pre_update_plan=plan, _windows_gateway_resume=None, update_complete=True,
+ )
+
+ assert exc.value.code == 1
+ outcomes = {o["profile"]: o["outcome"] for o in update_receipt.read_latest_receipt()["runtime_outcomes"]}
+ assert outcomes == {"default": "unaccounted", "work": "external"}
+
+
+def test_external_requires_verified_pid_evidence():
+ """Fail closed: no external evidence, or evidence for another pid, stays unaccounted."""
+ plan = UpdatePlan(runtimes=[_gateway("work", 4242)])
+ common = dict(
+ restarted_services=[], relaunched_profiles=[], externally_supervised_profiles=[],
+ killed_pids=set(), failed_units=[],
+ )
+ assert match_runtime_outcomes(plan, **common)[0]["outcome"] == "unaccounted"
+ assert match_runtime_outcomes(plan, **common, external_gateway_pids={9999})[0]["outcome"] == "unaccounted"
+ outcomes = match_runtime_outcomes(plan, **common, external_gateway_pids={4242})
+ assert outcomes[0]["outcome"] == "external"
+ assert report_unaccounted_runtimes(outcomes) is False
+
+
+def test_external_evidence_never_covers_a_serve_runtime():
+ plan = UpdatePlan(runtimes=[RuntimeRecord(kind="serve", profile="work", pid=4242, supervisor="manual-serve")])
+ outcomes = match_runtime_outcomes(
+ plan, restarted_services=[], relaunched_profiles=[], externally_supervised_profiles=[],
+ killed_pids=set(), failed_units=[], external_gateway_pids={4242},
+ )
+ assert outcomes[0]["outcome"] == "unaccounted"