The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): discharge inventory-less fleet-restart markers on live-fleet evidence
Yagna Vudathu58ca94178553
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.pyindex 52a4a83a838..ec187507dbe 100644--- a/hermes_cli/update_cmd_fleet.py+++ b/hermes_cli/update_cmd_fleet.py@@ -215,7 +215,8 @@ def _live_fleet_covers_receipt(expected_sha: str | None, receipt: dict, owed: sedef _marker_only_restart_obsolete() -> bool:"""Settle only the inventory stored with this marker's target SHA.- Historical receipts cannot narrow this obligation. Legacy, malformed or unsupported inventories stay fail-closed; empty discovery never proves a stopped gateway recovered.+ Historical receipts cannot narrow this obligation. Malformed or unsupported inventories stay fail-closed; empty discovery never proves a stopped gateway recovered.+ An inventory-less marker records no obligation (#115638), so it discharges when the live fleet provably serves the marker's expected SHA."""from hermes_cli.update_serve_obligations import defer_manual_serve@@ -228,23 +229,27 @@ def _marker_only_restart_obsolete() -> bool:fields[key] = valueexpected_sha = fields.get("expected_sha", "").strip()inventory = json.loads(fields.get("inventory", "null"))- if not isinstance(inventory, dict) or inventory.get("version") != 1:- return False- runtimes = inventory.get("runtimes")- if not isinstance(runtimes, list) or not runtimes:- return False- owed = set()- for runtime in runtimes:- if not isinstance(runtime, dict):- return False- if runtime.get("kind") in ("serve", "dashboard") and defer_manual_serve(runtime):- continue- if runtime.get("kind") != "gateway":+ owed: set[tuple[str, str]] | None = None+ if inventory is None:+ pass # inventory-less marker: no recorded obligation; live-fleet evidence alone settles it+ else:+ if not isinstance(inventory, dict) or inventory.get("version") != 1:return False- profile = runtime.get("profile")- if not isinstance(profile, str) or not profile.strip() or profile == "unknown":+ runtimes = inventory.get("runtimes")+ if not isinstance(runtimes, list) or not runtimes:return False- owed.add(("gateway", profile))+ owed = set()+ for runtime in runtimes:+ if not isinstance(runtime, dict):+ return False+ if runtime.get("kind") in ("serve", "dashboard") and defer_manual_serve(runtime):+ continue+ if runtime.get("kind") != "gateway":+ return False+ profile = runtime.get("profile")+ if not isinstance(profile, str) or not profile.strip() or profile == "unknown":+ return False+ owed.add(("gateway", profile))except (OSError, UnicodeError, ValueError):return Falseif not expected_sha:@@ -266,7 +271,7 @@ def _marker_only_restart_obsolete() -> bool:for row in fleet:if row.get("state") != "current" or str(row.get("code_sha")) != expected_sha:return False # stale / down / unknown-identity row still owes the restart- if not owed <= covered:+ if owed is not None and not owed <= covered:return False # A gateway this marker owns is absent (down) or unidentifiable._clear_fleet_restart_pending_marker()logger.debug(diff --git a/tests/hermes_cli/test_manual_serve_deferral.py b/tests/hermes_cli/test_manual_serve_deferral.pyindex fdb0a5797ea..a58199cc4f8 100644--- a/tests/hermes_cli/test_manual_serve_deferral.py+++ b/tests/hermes_cli/test_manual_serve_deferral.py@@ -85,8 +85,9 @@ def test_historical_manual_obligation_does_not_block_healthy_gateway(monkeypatchmonkeypatch.setattr(update_receipt, "collect_fleet_versions", lambda **k: [{"profile": "default", "state": "current", "code_sha": "new"}] if gateway_present else [])if marker:fleet._write_fleet_restart_pending_marker(expected_sha="new")- # Legacy markers cannot inherit inventory from any historical receipt, even with a nonempty current fleet.- pending = marker+ # An inventory-less marker never inherits inventory from a historical receipt, but it+ # discharges when the live fleet provably serves its expected SHA (#115638).+ pending = marker and not gateway_presentassert fleet._pending_fleet_restart_needed() is pendingfleet._warn_pending_fleet_restart_on_startup()warning = capsys.readouterr().errdiff --git a/tests/hermes_cli/test_update_fleet_restart_pending.py b/tests/hermes_cli/test_update_fleet_restart_pending.pyindex 41c63d28254..d5576dadf71 100644--- a/tests/hermes_cli/test_update_fleet_restart_pending.py+++ b/tests/hermes_cli/test_update_fleet_restart_pending.py@@ -722,7 +722,8 @@ def test_startup_warn_discharged_when_fleet_current(monkeypatch, capsys):def test_startup_warn_discharged_when_multiplexer_covers_owed_profiles(monkeypatch, capsys):"""A current multiplexer discharges every profile named in its live record (#113350)."""disk_sha = "e" * 40- # The marker owns its inventory (two gateways owed); a marker without one stays fail-closed.+ # The marker owns its inventory (two gateways owed); an inventory-less marker+ # discharges on live-fleet evidence alone (#115638).update_cmd._write_fleet_restart_pending_marker(expected_sha=disk_sha,runtimes=[{"kind": "gateway", "profile": p} for p in ("default", "coder")],@@ -898,3 +899,47 @@ def test_startup_warn_silent_when_completed_update_fleet_restarted_onto_moved_chupdate_cmd._warn_pending_fleet_restart_on_startup()assert capsys.readouterr().err == ""+++# ── Inventory-less markers discharge on live-fleet evidence (#115638) ──+#+# A marker written without an inventory line (legacy markers, or a pull that raced+# a missing pre-update plan) records no owed set, so the inventory check stays+# fail-closed forever and every CLI call warns. With no recorded obligation the+# marker discharges when the live fleet provably serves expected_sha — same+# evidence bar as an inventoried marker, minus the owed-coverage check.+++def test_startup_warn_discharged_when_inventory_less_marker_fleet_current(monkeypatch, capsys):+ disk_sha = "e" * 40+ update_cmd._write_fleet_restart_pending_marker(expected_sha=disk_sha)+ assert "inventory=" not in update_cmd._fleet_restart_pending_marker_path().read_text(encoding="utf-8")+ _patch_marker_sha(monkeypatch, disk_sha)+ monkeypatch.setattr(+ "hermes_cli.update_receipt.collect_fleet_versions",+ lambda **kwargs: [+ {"profile": "default", "pid": 42, "code_sha": disk_sha, "code_version": "0.21.0", "state": "current"}+ ],+ )++ update_cmd._warn_pending_fleet_restart_on_startup()++ assert capsys.readouterr().err == ""+ assert not update_cmd._fleet_restart_pending_marker_path().exists()+++def test_startup_warn_kept_when_inventory_less_marker_fleet_stale(monkeypatch, capsys):+ disk_sha = "e" * 40+ update_cmd._write_fleet_restart_pending_marker(expected_sha=disk_sha)+ _patch_marker_sha(monkeypatch, disk_sha)+ 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_restart_pending_marker_path().exists()diff --git a/tests/hermes_cli/test_update_scoped_reconciliation.py b/tests/hermes_cli/test_update_scoped_reconciliation.pyindex 237b1b081c2..3ee2c12b897 100644--- a/tests/hermes_cli/test_update_scoped_reconciliation.py+++ b/tests/hermes_cli/test_update_scoped_reconciliation.py@@ -124,8 +124,7 @@ def test_probe_exception_does_not_hide_manual_warning(monkeypatch, capsys, marke@pytest.mark.parametrize("completed_restart", [False, True])[email protected]("legacy", [False, True], ids=["owned-inventory", "legacy-marker"])-def test_new_marker_cannot_borrow_old_alpha_receipt(monkeypatch, capsys, legacy, completed_restart):+def test_new_marker_cannot_borrow_old_alpha_receipt(monkeypatch, capsys, completed_restart):"""N owns alpha; N+1 owns alpha and beta but dies before writing its receipt."""old = {"outcome": "failed", "plan": {"runtimes": [GATEWAY]}}if completed_restart:@@ -133,9 +132,8 @@ def test_new_marker_cannot_borrow_old_alpha_receipt(monkeypatch, capsys, legacy,live = [CURRENT]target = seed(monkeypatch, old, "new", live)marker = fleet._fleet_restart_pending_marker_path()- if not legacy:- with marker.open("a") as stream:- stream.write("inventory=" + json.dumps({"version": 1, "runtimes": [GATEWAY, dict(GATEWAY, profile="beta")]}) + "\n")+ with marker.open("a") as stream:+ stream.write("inventory=" + json.dumps({"version": 1, "runtimes": [GATEWAY, dict(GATEWAY, profile="beta")]}) + "\n")receipt_before, marker_before = target.read_bytes(), marker.read_bytes()fleet._warn_pending_fleet_restart_on_startup()assert "hermes gateway restart" in capsys.readouterr().err@@ -145,12 +143,35 @@ def test_new_marker_cannot_borrow_old_alpha_receipt(monkeypatch, capsys, legacy,assert marker.read_bytes() == marker_beforeassert target.read_bytes() == receipt_beforelive.append(dict(CURRENT, profile="beta"))- assert fleet._pending_fleet_restart_needed() is legacy- assert marker.exists() is legacy+ assert not fleet._pending_fleet_restart_needed()+ assert not marker.exists()+ assert target.read_bytes() == receipt_before++[email protected]("completed_restart", [False, True])+def test_legacy_marker_discharges_on_live_fleet_evidence_without_receipt(monkeypatch, capsys, completed_restart):+ """An inventory-less N+1 marker settles on live-fleet evidence alone (#115638).++ It never borrows the old receipt's ownership: the receipt is left intact and the+ marker discharges only because every live row is current at its expected SHA.+ """+ old = {"outcome": "failed", "plan": {"runtimes": [GATEWAY]}}+ if completed_restart:+ old.update(post_update={"sha": "new"}, gateway_restart={"incomplete": False})+ live = [CURRENT]+ target = seed(monkeypatch, old, "new", live)+ marker = fleet._fleet_restart_pending_marker_path()+ receipt_before = target.read_bytes()+ fleet._warn_pending_fleet_restart_on_startup()+ assert "hermes gateway restart" not in capsys.readouterr().err+ assert not fleet._pending_fleet_restart_needed()+ assert not marker.exists()assert target.read_bytes() == receipt_before[email protected]("inventory", [None, {}, [], {"version": 2, "runtimes": [GATEWAY]}, {"version": 1, "runtimes": []}, {"version": 1, "runtimes": [GATEWAY, dict(MANUAL, detail={})]}, {"version": 1, "runtimes": [None]}, {"version": 1, "runtimes": [{"kind": "gateway", "profile": "unknown"}]}, {"version": 1, "runtimes": [{"kind": "gateway", "profile": []}]}])+# Explicit `inventory=null` records no obligation either, so it settles on live-fleet+# evidence like a missing line (#115638); the variants below stay fail-closed.[email protected]("inventory", [{}, [], {"version": 2, "runtimes": [GATEWAY]}, {"version": 1, "runtimes": []}, {"version": 1, "runtimes": [GATEWAY, dict(MANUAL, detail={})]}, {"version": 1, "runtimes": [None]}, {"version": 1, "runtimes": [{"kind": "gateway", "profile": "unknown"}]}, {"version": 1, "runtimes": [{"kind": "gateway", "profile": []}]}])def test_unverified_marker_inventory_stays_pending(monkeypatch, inventory):seed(monkeypatch, {"outcome": "success", "plan": {"runtimes": [GATEWAY]}}, "new", [CURRENT])marker = fleet._fleet_restart_pending_marker_path()