Skip to content

The agent that grows with you

fix: a same-SHA catch-up adds newly stopped unmapped gateways to the owed inventory

Teknium72717c15c159
diff --git a/hermes_cli/update_cmd_fleet_verify.py b/hermes_cli/update_cmd_fleet_verify.py
index 59f092d95fe..6097bce476d 100644
--- a/hermes_cli/update_cmd_fleet_verify.py
+++ b/hermes_cli/update_cmd_fleet_verify.py
@@ -6,6 +6,7 @@ follow-up, never a failed update — contract C3) or clear the obligation, and f
Restart-phase helpers stay in ``update_cmd_fleet`` and are read through it so their patches apply.
"""
+import json
import logging
import subprocess
import time as _time
@@ -129,7 +130,7 @@ def _live_gateway_pids_from_fleet(fleet_rows: list) -> dict:
def _record_owed_gateway_inventory(plan, stopped_unmapped_pids=()) -> None:
- """Name the pre-update gateways on an inventory-less obligation once their restart is owed.
+ """Name the pre-update gateways on the obligation once their restart is owed.
The pull arms the obligation before the restart phase knows what it owes. An inventory-less
record on a host whose gateway then died at boot is settled by the gateway-less discharge
@@ -137,12 +138,22 @@ def _record_owed_gateway_inventory(plan, stopped_unmapped_pids=()) -> None:
failed update under contract C3. A named inventory keeps it until those gateways run HEAD.
A gateway stopped with no known profile is recorded as an unmapped row for the same reason: it
has no ``gateway_state.json`` the gateway-less probe could see, so absence would settle it.
+ A readable inventory (a same-SHA catch-up keeps the standing record) only GROWS: rows this run
+ newly owes are appended, so a mapped successor cannot discharge a newly stopped unmapped one.
"""
from hermes_cli import update_cmd_fleet as fleet
armed = fleet._fleet_restart_obligation_armed()
fields = fleet._obligation_fields() if armed else {}
- if fields is None or fields.get("inventory") not in (None, "", "null"):
- return # unreadable terms stay fail-closed; a recorded inventory is never rewritten
+ if fields is None:
+ return # unreadable terms stay fail-closed
+ recorded: list = []
+ if fields.get("inventory") not in (None, "", "null"):
+ try:
+ inventory = json.loads(fields["inventory"])
+ fleet._marker_owed_gateways(inventory) # validates; malformed/unsupported stays untouched
+ except ValueError:
+ return
+ recorded = inventory["runtimes"]
from dataclasses import asdict
rows = [
asdict(runtime) for runtime in getattr(plan, "runtimes", ()) or ()
@@ -151,11 +162,12 @@ def _record_owed_gateway_inventory(plan, stopped_unmapped_pids=()) -> None:
and runtime.profile.strip() and runtime.profile != "unknown"
] + [{"kind": "gateway", "profile": None, "pid": pid, "stopped_unmapped": True}
for pid in sorted(stopped_unmapped_pids)]
+ rows = [row for row in rows if row not in recorded]
if rows:
# Not armed any more (a pre-restart probe settled an inventory-less record): re-arm, the
# restart is owed. The SHA is the code the fleet must be proven current on.
sha = fields.get("expected_sha", "") if armed else (fleet._current_checkout_sha() or "")
- fleet._write_fleet_restart_pending_marker(expected_sha=sha, runtimes=rows)
+ fleet._write_fleet_restart_pending_marker(expected_sha=sha, runtimes=recorded + rows)
def _named_gateways_still_owed() -> bool:
diff --git a/tests/hermes_cli/test_update_fleet_check_fail_closed.py b/tests/hermes_cli/test_update_fleet_check_fail_closed.py
index c6de5605ef1..179c46f2d5a 100644
--- a/tests/hermes_cli/test_update_fleet_check_fail_closed.py
+++ b/tests/hermes_cli/test_update_fleet_check_fail_closed.py
@@ -23,6 +23,7 @@ not a runtime inventory, and its entries have no corresponding
from __future__ import annotations
+import json
import types
import pytest
@@ -196,3 +197,44 @@ def test_unmapped_stop_debt_is_not_settled_by_the_mapped_gateways_restart(monkey
fleet_rows.append({"profile": "work", "state": "current", "code_sha": "head", "pid": 9})
assert fleet._update_owes_fleet_restart(receipt={}, pending_manual=[]) is False
assert not fleet._fleet_restart_obligation_armed()
+
+
+def test_same_sha_retry_adds_newly_stopped_unmapped_debt_to_an_existing_inventory(monkeypatch, tmp_path):
+ # A same-SHA catch-up keeps the standing inventory (gateway `default`, still down). If that retry
+ # also stops an unmapped gateway, its debt must join the inventory: otherwise `default` coming
+ # back settles the obligation while the unmapped gateway still has no successor.
+ from hermes_cli import update_cmd, update_cmd_fleet as fleet, update_cmd_fleet_verify as fleet_verify
+ from hermes_cli import update_host_obligation as host, update_receipt
+ from hermes_cli.update_inventory import UpdatePlan
+
+ monkeypatch.setattr(host, "host_obligation_path", lambda: tmp_path / "host-update-restart.json")
+ monkeypatch.setattr(fleet, "_current_checkout_sha", lambda: "head")
+ monkeypatch.setattr(update_cmd, "_current_checkout_sha", lambda: "head")
+ monkeypatch.setattr(fleet_verify, "_print_legacy_units_warning", lambda: None)
+ monkeypatch.setattr("hermes_cli.update_cmd_maint._refresh_dashboard_after_update", lambda **kw: None)
+ monkeypatch.setattr(update_cmd, "_surviving_pre_update_serve_runtimes", lambda plan: [])
+ monkeypatch.setattr(fleet_verify, "_FLEET_PROBE_SETTLE_TIMEOUT_SECONDS", 0) # A stays down: one poll
+ # Signal delivery is the external boundary: the down row has no process to drain.
+ monkeypatch.setattr("hermes_cli.update_cmd_stale_survivors.signal_stale_fleet_survivors", lambda *a: None)
+ fleet_rows = [{"profile": "default", "state": "down", "code_sha": "old", "pid": 7}]
+ monkeypatch.setattr(update_receipt, "collect_fleet_versions", lambda **kw: list(fleet_rows))
+ fleet._write_fleet_restart_pending_marker(expected_sha="head", runtimes=[
+ {"kind": "gateway", "profile": "default", "pid": 7}])
+ out = fleet._GatewayRestartOutcome(
+ incomplete=False, phase_errors=[], pre_restart_gateway_pids=[7999], restarted_services=[],
+ failed_or_stale_units=[], relaunched_profiles=[], externally_supervised_profiles=[],
+ killed_pids={7999}, stopped_unmapped_pids={7999},
+ )
+ update_receipt.begin_update_receipt()
+ fleet_verify._verify_fleet_after_update(
+ out, _pre_update_plan=UpdatePlan(runtimes=[RuntimeRecord(kind="gateway", profile="default", pid=7)]),
+ _windows_gateway_resume=None, update_complete=True)
+
+ rows = json.loads(fleet._obligation_fields()["inventory"])["runtimes"]
+ assert {"kind": "gateway", "profile": "default", "pid": 7} in rows # earlier debt kept
+ assert {"kind": "gateway", "profile": None, "pid": 7999, "stopped_unmapped": True} in rows
+ fleet_rows[:] = [{"profile": "default", "state": "current", "code_sha": "head", "pid": 8}]
+ assert fleet._marker_only_restart_obsolete() is False # `default`'s successor is not 7999's
+ assert fleet._fleet_restart_obligation_armed()
+ fleet_rows.append({"profile": "work", "state": "current", "code_sha": "head", "pid": 9})
+ assert fleet._marker_only_restart_obsolete() is True