Skip to content

The agent that grows with you

fix(update): settle a stuck failed receipt once the live fleet serves the checkout code

teknium129c4ec8fa795
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.py
index b61434b2f22..b160ad571fd 100644
--- a/hermes_cli/update_cmd_fleet.py
+++ b/hermes_cli/update_cmd_fleet.py
@@ -446,21 +446,24 @@ def _restart_systemd_gateway_units_best_effort(failed: list, listings) -> None:
failed.extend(f"systemd-{scope} (listing unavailable)" for scope, _ in _SYSTEMD_SCOPES if scope not in answered)
-def _live_fleet_all_current() -> bool:
- """True when the fleet probe finds at least one gateway and every row is ``current`` at the
- checkout SHA (identity known). Any unknown/stale/down row or a failed probe -> False (restart)."""
+def _live_fleet_current_rows() -> list[dict] | None:
+ """The fleet matrix when the probe finds at least one gateway and every row is ``current``
+ at the checkout SHA (identity known); ``None`` on any unknown/stale/down row or a failed
+ probe (restart)."""
checkout_sha = _current_checkout_sha()
if not checkout_sha:
- return False
+ return None
try:
from hermes_cli.update_receipt import collect_fleet_versions
fleet = collect_fleet_versions()
except Exception as exc:
logger.debug("Pending fleet restart: fleet probe failed: %s", exc)
- return False
+ return None
if not fleet or _fleet_covered_gateways(fleet) is None:
- return False
- return all(row.get("state") == "current" and str(row.get("code_sha")) == checkout_sha for row in fleet)
+ return None
+ if all(row.get("state") == "current" and str(row.get("code_sha")) == checkout_sha for row in fleet):
+ return fleet
+ return None
def _run_pending_fleet_restart() -> bool:
@@ -495,7 +498,7 @@ def _run_pending_fleet_restart() -> bool:
# ago) already serves the checkout code; stopping it here re-kills the fleet, and on
# Windows the stop/start pair then reports "No gateway was running" plus a second spawn
# (#117051). Skip when EVERY live gateway is current on the checkout SHA.
- if pids and _live_fleet_all_current():
+ if pids and _live_fleet_current_rows() is not None:
print(" ✓ Every running gateway already serves the checkout code — nothing to restart.")
return True
@@ -604,9 +607,23 @@ def _apply_pending_fleet_restart_catchup(*, defer: bool = False) -> None:
print()
_warn_pending_fleet_restart()
print("→ Running the pending fleet restart...")
- if _run_pending_fleet_restart() and not _pending_fleet_restart_needed():
+ if not _run_pending_fleet_restart():
+ print(" ⚠ Fleet restart incomplete. Recover with: hermes gateway restart")
+ sys.exit(1)
+ if not _pending_fleet_restart_needed():
return
- print(" ⚠ Fleet restart incomplete. Recover with: hermes gateway restart")
+ # The restart itself succeeded, but the receipt still owes gateways it cannot match to a
+ # live row (unknown identity, pre-pull plan SHAs). When every live gateway serves the
+ # checkout code, that matrix is the recovery evidence: settle the receipt with it instead of
+ # failing this run — an exit 1 here writes another failed receipt and the warning never
+ # clears, even after a successful manual `hermes gateway restart` (#117051).
+ fleet = _live_fleet_current_rows()
+ if fleet is not None:
+ from hermes_cli.update_receipt import settle_latest_receipt_fleet
+ if settle_latest_receipt_fleet(fleet) and not _pending_fleet_restart_needed():
+ print(f" ✓ Update receipt settled: {len(fleet)} gateway(s) serve the checkout code.")
+ return
+ print(" ⚠ Fleet restart ran, but gateways are still off the checkout code. Recover with: hermes gateway restart")
sys.exit(1)
diff --git a/hermes_cli/update_receipt.py b/hermes_cli/update_receipt.py
index 0c06c368c2c..453dbfacbe8 100644
--- a/hermes_cli/update_receipt.py
+++ b/hermes_cli/update_receipt.py
@@ -246,6 +246,36 @@ def _prune_old_receipts(directory: Path) -> None:
stale.unlink()
+def settle_latest_receipt_fleet(fleet: list[dict[str, Any]]) -> bool:
+ """Record on ``latest.json`` that the fleet it still reports as owed now serves the checkout.
+
+ A failed receipt whose plan rows cannot be matched to a live gateway (unknown identity,
+ pre-pull SHAs) keeps ``hermes update`` exiting 1 and every CLI start warning about mixed
+ modules, long after the operator's ``hermes gateway restart`` fixed the fleet (#117051). The
+ caller has just verified every live row is current at the checkout SHA; persisting that
+ matrix as the receipt's post-restart ``fleet`` (and un-flagging ``gateway_restart``) is what
+ lets the stale-runtime readers see the recovery. Only the ``latest.json`` pointer is
+ rewritten; the archived per-run file keeps the original outcome. Never raises.
+ """
+ try:
+ path = _receipt_dir() / "latest.json"
+ receipt = json.loads(path.read_text(encoding="utf-8"))
+ if not isinstance(receipt, dict):
+ return False
+ receipt["fleet"] = list(fleet)
+ gateway_restart = receipt.get("gateway_restart")
+ if not isinstance(gateway_restart, dict):
+ gateway_restart = {}
+ gateway_restart.update({"incomplete": False, "phase_error": ""})
+ gateway_restart["settled_from_live_fleet_at"] = _utc_now_iso()
+ receipt["gateway_restart"] = gateway_restart
+ path.write_text(json.dumps(receipt, indent=2, default=str), encoding="utf-8")
+ return True
+ except Exception as exc:
+ logger.debug("Could not settle latest update receipt from the live fleet: %s", exc)
+ return False
+
+
def read_latest_receipt() -> Optional[dict[str, Any]]:
"""Read the most recent update receipt, or None. Never raises."""
with suppress(Exception):
diff --git a/tests/hermes_cli/test_update_fleet_restart_pending.py b/tests/hermes_cli/test_update_fleet_restart_pending.py
index 72fe70a0f55..cc6959048e8 100644
--- a/tests/hermes_cli/test_update_fleet_restart_pending.py
+++ b/tests/hermes_cli/test_update_fleet_restart_pending.py
@@ -1011,3 +1011,47 @@ def test_pending_fleet_restart_cleared_instead_of_exit_1(monkeypatch, tmp_path):
assert seen["ran"] is False
assert not marker.exists()
+
+
+def test_catchup_settles_failed_receipt_from_live_fleet_instead_of_exit_1(monkeypatch, capsys):
+ """A failed receipt whose plan rows carry pre-pull SHAs and an unidentifiable profile can never
+ be matched to a live gateway, so every up-to-date `hermes update` ran the restart, printed
+ "completed" then "incomplete", exited 1 and wrote another failed receipt — even after a manual
+ `hermes gateway restart` put the fleet on the checkout code (#117051). With every live row
+ current at the checkout SHA, the catch-up settles the receipt from that matrix and exits 0."""
+ sha = "d" * 40
+ monkeypatch.setattr(update_cmd, "_current_checkout_sha", lambda: sha)
+ monkeypatch.setattr(update_cmd_fleet, "_current_checkout_sha", lambda: sha)
+ receipt_dir = get_hermes_home() / "logs" / "update_receipts"
+ receipt_dir.mkdir(parents=True)
+ latest = receipt_dir / "latest.json"
+ latest.write_text(
+ json.dumps(
+ {
+ "outcome": "failed",
+ "exit_code": 1,
+ "stop_reason": "sys.exit(1)",
+ "gateway_restart": {},
+ "fleet": [],
+ "plan": {
+ "expected_sha": sha,
+ "runtimes": [{"kind": "gateway", "profile": "unknown", "pid": 42, "code_sha": "0" * 40}],
+ },
+ }
+ ),
+ encoding="utf-8",
+ )
+ live = [{"profile": "default", "pid": 48096, "code_sha": sha, "state": "current"}]
+ monkeypatch.setattr("hermes_cli.update_receipt.collect_fleet_versions", lambda **k: list(live))
+ monkeypatch.setattr(update_cmd, "_run_pending_fleet_restart", lambda: True)
+ assert update_cmd._pending_fleet_restart_needed() is True
+
+ update_cmd._apply_pending_fleet_restart_catchup()
+
+ out = capsys.readouterr().out
+ assert "incomplete" not in out and "still off the checkout code" not in out
+ settled = json.loads(latest.read_text(encoding="utf-8"))
+ assert settled["fleet"] == live
+ assert settled["gateway_restart"]["incomplete"] is False
+ assert update_cmd._pending_fleet_restart_needed() is False
+ assert update_cmd_fleet._update_owes_fleet_restart() is False