Skip to content

The agent that grows with you

fix(update): discharge an SHA-less host restart obligation on live-fleet evidence

kokhloe57f6d0f2d60
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index f2af68d7612..47b40154ac1 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -724,7 +724,10 @@ def _complete_source_update(request: dict | None) -> None:
current = _completion_receipt._current.get()
if current is not None:
request["receipt"] = deepcopy(current.data)
- _write_fleet_restart_pending_marker(expected_sha=request.get("expected_sha") or "")
+ _write_fleet_restart_pending_marker(
+ expected_sha=request.get("expected_sha")
+ or _completion_receipt._receipt_post_update_sha(request["receipt"])
+ )
result = run_completion(request)
_accept_completion_pm_receipt(result.get("pm_receipt"), request["receipt"]["update_id"])
token = request["windows_resume"]
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.py
index 8602b7695c6..080efe89b9a 100644
--- a/hermes_cli/update_cmd_fleet.py
+++ b/hermes_cli/update_cmd_fleet.py
@@ -387,7 +387,10 @@ def _marker_only_restart_obsolete() -> bool:
that died before its inventory was recorded, #115638) clears once every live gateway is
current on the checkout — there is no recorded owed set, so the fleet running the code on disk
is the whole of the evidence the marker's warning can be about, even after HEAD moved past
- ``expected_sha`` by an out-of-band pull. With no live gateway at all, the inventory-less marker
+ ``expected_sha`` by an out-of-band pull. An inventory-less record armed with no SHA at all
+ (a no-op update whose head capture failed, #125952) settles by the same live-fleet evidence
+ once the process that armed it is gone: a status call must not discharge a restart phase
+ that is still running. With no live gateway at all, the inventory-less marker
asks the host instead (``update_cmd_fleet_gatewayless``): it clears when no profile left a
gateway that should be running and every live runtime is supervisor-owned or handed off, so a
Desktop-only install stops failing every later update (#118742).
@@ -419,9 +422,22 @@ def _marker_only_restart_obsolete() -> bool:
logger.debug("Fleet-restart-pending marker discharged: no gateway obligation recorded")
return True
if not expected_sha:
- return False
+ if owed is not None:
+ return False # an inventoried obligation without its SHA can never be proven
+ # An inventory-less record armed with no SHA (a no-op update whose head capture
+ # failed, #125952) has no equality gate to hold it to; the fleet running the
+ # checkout code is the whole of the evidence, but never discharge an update
+ # whose arming process may still be mid-restart-phase.
+ from hermes_cli._subprocess_compat import pid_exists_stdlib
+
+ try:
+ armer_pid = int(fields.get("pid", "") or 0)
+ except ValueError:
+ return False
+ if armer_pid > 0 and pid_exists_stdlib(armer_pid):
+ return False
checkout_sha = _current_checkout_sha()
- if owed is not None and checkout_sha != expected_sha and not checkout_contains(expected_sha):
+ if expected_sha and owed is not None and checkout_sha != expected_sha and not checkout_contains(expected_sha):
return False # a newer pull moved HEAD; it owns a fresh obligation
# HEAD may sit past ``expected_sha`` by a carried local commit (a cherry-picked hotfix) that no
# pull made and no fresh obligation covers; the fleet is held to the code it actually runs, which
diff --git a/hermes_cli/update_host_obligation.py b/hermes_cli/update_host_obligation.py
index 23a6ae62c3d..cc9ec15658d 100644
--- a/hermes_cli/update_host_obligation.py
+++ b/hermes_cli/update_host_obligation.py
@@ -154,6 +154,10 @@ def obligation_fields() -> Optional[dict[str, str]]:
if record is None:
return None
fields = {"expected_sha": str(record.get("expected_sha") or "")}
+ pid = record.get("pid")
+ if pid is not None:
+ # The arming process, for readers that must not discharge a still-running update.
+ fields["pid"] = str(pid)
inventory = record.get("inventory")
if inventory is not None:
fields["inventory"] = json.dumps(inventory)
diff --git a/hermes_cli/update_receipt.py b/hermes_cli/update_receipt.py
index c1d6ddf4664..aad20505512 100644
--- a/hermes_cli/update_receipt.py
+++ b/hermes_cli/update_receipt.py
@@ -172,6 +172,24 @@ class UpdateReceipt:
self.data["post_update"] = _code_identity(refresh=True)
+def _receipt_post_update_sha(receipt: Any) -> str:
+ """The SHA the completed receipt says the checkout ended on, else ``""``.
+
+ Arming the host restart obligation with an empty SHA leaves a record the reader can
+ never prove discharged (#125952); the receipt carried in the same completion request
+ already names the code a no-op update stood on.
+ """
+ if not isinstance(receipt, dict):
+ return ""
+ post_update = receipt.get("post_update")
+ if isinstance(post_update, dict) and post_update.get("sha"):
+ return str(post_update["sha"])
+ pre_update = receipt.get("pre_update")
+ if isinstance(pre_update, dict) and pre_update.get("sha"):
+ return str(pre_update["sha"])
+ return ""
+
+
def _receipt_dir() -> Path:
# ``hermes_constants`` (stdlib-only), never ``hermes_cli.config``: the receipt must be
# writable from the refused/failed paths where config loading itself may be what broke
diff --git a/tests/hermes_cli/test_update_fleet_restart_pending.py b/tests/hermes_cli/test_update_fleet_restart_pending.py
index a0098f2f227..68bd9c675ec 100644
--- a/tests/hermes_cli/test_update_fleet_restart_pending.py
+++ b/tests/hermes_cli/test_update_fleet_restart_pending.py
@@ -1068,3 +1068,101 @@ def test_second_profile_attaches_to_completed_host_restart(monkeypatch):
# A stamp for other code proves nothing about this checkout.
host_obligation.mark_host_restart_completed("def456")
assert update_cmd_fleet._fleet_restart_skip_reason(None) is None
+
+
+# ── SHA-less inventory-less obligations discharge on live-fleet evidence (#125952) ──
+#
+# A no-op update whose head capture failed armed the host record with expected_sha=""
+# and no inventory. The reader bailed out on the empty SHA before the fleet check, so
+# the warning could never clear even with every live gateway current on the checkout.
+
+
+def _write_sha_less_obligation(pid: int = 424242) -> None:
+ """Arm the poisoned shape verbatim: no SHA, no inventory, a known arming pid."""
+ path = host_obligation.host_obligation_path()
+ assert path is not None
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(json.dumps({
+ "version": 1, "started": 0.0, "pid": pid, "armed_by_profile": "default",
+ "expected_sha": "",
+ }), encoding="utf-8")
+
+
+def _patch_armer_liveness(monkeypatch, alive_pids: set[int]) -> None:
+ monkeypatch.setattr(
+ "hermes_cli._subprocess_compat.pid_exists_stdlib", lambda pid: int(pid) in alive_pids
+ )
+
+
+def test_startup_warn_discharged_when_sha_less_marker_fleet_current(monkeypatch, capsys):
+ checkout = "e" * 40
+ _write_sha_less_obligation()
+ _patch_armer_liveness(monkeypatch, set()) # the update that armed it is gone
+ _patch_marker_sha(monkeypatch, checkout)
+ monkeypatch.setattr(
+ "hermes_cli.update_receipt.collect_fleet_versions",
+ lambda **kwargs: [
+ {"profile": "default", "pid": 42, "code_sha": checkout, "code_version": "0.21.5", "state": "current"}
+ ],
+ )
+
+ update_cmd._warn_pending_fleet_restart_on_startup()
+
+ assert capsys.readouterr().err == ""
+ assert not update_cmd_fleet._fleet_restart_obligation_armed()
+
+
+def test_startup_warn_kept_when_sha_less_marker_armer_still_alive(monkeypatch, capsys):
+ """A status call must not discharge an update that is still mid-restart-phase."""
+ checkout = "e" * 40
+ _write_sha_less_obligation()
+ _patch_armer_liveness(monkeypatch, {424242}) # the arming update is still running
+ _patch_marker_sha(monkeypatch, checkout)
+ monkeypatch.setattr(
+ "hermes_cli.update_receipt.collect_fleet_versions",
+ lambda **kwargs: [
+ {"profile": "default", "pid": 42, "code_sha": checkout, "code_version": "0.21.5", "state": "current"}
+ ],
+ )
+
+ update_cmd._warn_pending_fleet_restart_on_startup()
+
+ assert "did not restart running gateways" in capsys.readouterr().err
+ assert update_cmd_fleet._fleet_restart_obligation_armed()
+
+
+def test_startup_warn_kept_when_sha_less_marker_fleet_stale(monkeypatch, capsys):
+ checkout = "e" * 40
+ _write_sha_less_obligation()
+ _patch_armer_liveness(monkeypatch, set())
+ _patch_marker_sha(monkeypatch, checkout)
+ monkeypatch.setattr(
+ "hermes_cli.update_receipt.collect_fleet_versions",
+ lambda **kwargs: [
+ {"profile": "default", "pid": 42, "code_sha": "7" * 40, "code_version": "0.20.0", "state": "stale"}
+ ],
+ )
+
+ update_cmd._warn_pending_fleet_restart_on_startup()
+
+ assert "did not restart running gateways" in capsys.readouterr().err
+ assert update_cmd_fleet._fleet_restart_obligation_armed()
+
+
+def test_startup_warn_kept_when_inventory_present_but_sha_less(monkeypatch, capsys):
+ """An inventoried obligation without its SHA can never be proven: fail closed."""
+ checkout = "e" * 40
+ _write_sha_less_obligation()
+ host_obligation.amend_host_obligation(inventory={"version": 1, "runtimes": [{"kind": "gateway", "profile": "default"}]})
+ _patch_marker_sha(monkeypatch, checkout)
+ monkeypatch.setattr(
+ "hermes_cli.update_receipt.collect_fleet_versions",
+ lambda **kwargs: [
+ {"profile": "default", "pid": 42, "code_sha": checkout, "code_version": "0.21.5", "state": "current"}
+ ],
+ )
+
+ update_cmd._warn_pending_fleet_restart_on_startup()
+
+ assert "did not restart running gateways" in capsys.readouterr().err
+ assert update_cmd_fleet._fleet_restart_obligation_armed()