The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): a stopped unmapped gateway keeps its restart owed across startups (review F7-unmapped-stop-debt-lost)
Teknium06eabea794ee
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.pyindex 3e297e48c63..3d5cd4d1dc8 100644--- a/hermes_cli/update_cmd_fleet.py+++ b/hermes_cli/update_cmd_fleet.py@@ -325,6 +325,11 @@ def _live_fleet_covers_receipt(expected_sha: str | None, receipt: dict, owed: sereturn False+# A gateway the restart phase stopped without knowing its profile: no live row can be matched to it,+# so only a live, current gateway of this checkout settles it, never an empty host (""=no profile).+_UNMAPPED_GATEWAY = ("gateway", "")++def _marker_owed_gateways(inventory: object) -> set[tuple[str, str]] | None:"""The ``("gateway", profile)`` set a marker's inventory owes; None when it recorded none.@@ -345,6 +350,9 @@ def _marker_owed_gateways(inventory: object) -> set[tuple[str, str]] | None:raise ValueError("fleet-restart inventory row is not an object")if runtime_outside_gateway_evidence(runtime):continue+ if runtime.get("kind") == "gateway" and runtime.get("stopped_unmapped"):+ owed.add(_UNMAPPED_GATEWAY)+ continueprofile = runtime.get("profile")if runtime.get("kind") != "gateway" or not isinstance(profile, str) or not profile.strip() or profile == "unknown":raise ValueError("fleet-restart inventory row is not an identified gateway")@@ -447,8 +455,10 @@ def _marker_only_restart_obsolete() -> bool:continueif row.get("state") != "current" or str(row.get("code_sha")) != target_sha:return False # stale / down / unknown-identity row still owes the restart- if owed is not None and not owed <= covered:+ if owed is not None and not owed - {_UNMAPPED_GATEWAY} <= covered:return False # A gateway this marker owns is absent (down) or unidentifiable.+ if owed and _UNMAPPED_GATEWAY in owed and all(row_is_external(row) for row in fleet):+ return False # another checkout's gateway is no successor for the one the update stopped_clear_fleet_restart_pending_marker()logger.debug("Fleet-restart-pending marker discharged: %d gateway(s) already serve %s",diff --git a/hermes_cli/update_cmd_fleet_verify.py b/hermes_cli/update_cmd_fleet_verify.pyindex 4197ad1b32b..9275e0f0660 100644--- a/hermes_cli/update_cmd_fleet_verify.py+++ b/hermes_cli/update_cmd_fleet_verify.py@@ -128,17 +128,17 @@ def _live_gateway_pids_from_fleet(fleet_rows: list) -> dict:return live-def _record_owed_gateway_inventory(plan) -> None:+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.The pull arms the obligation before the restart phase knows what it owes. An inventory-lessrecord on a host whose gateway then died at boot is settled by the gateway-less discharge(nothing live, nothing named), which would silence the owed-restart warning that replaces afailed 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."""from hermes_cli import update_cmd_fleet as fleet- if plan is None:- returnarmed = fleet._fleet_restart_obligation_armed()fields = fleet._obligation_fields() if armed else {}if fields is None or fields.get("inventory") not in (None, "", "null"):@@ -149,7 +149,8 @@ def _record_owed_gateway_inventory(plan) -> None:if getattr(runtime, "kind", None) == "gateway"and isinstance(getattr(runtime, "profile", None), str)and runtime.profile.strip() and runtime.profile != "unknown"- ]+ ] + [{"kind": "gateway", "profile": None, "pid": pid, "stopped_unmapped": True}+ for pid in sorted(stopped_unmapped_pids)]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.@@ -324,7 +325,7 @@ def _verify_fleet_after_update(restart, *, _pre_update_plan, _windows_gateway_re+ "; recover with `hermes gateway restart`",)with _best_effort('Fleet restart inventory not recorded: %s'):- _record_owed_gateway_inventory(_pre_update_plan)+ _record_owed_gateway_inventory(_pre_update_plan, stopped)with _best_effort('Update receipt finalize failed: %s'):from hermes_cli.update_receipt import finalize_update_receipt# A False ``update_complete`` (unsafe SQLite runtime, owed tail work) never fails thediff --git a/tests/hermes_cli/test_update_fleet_check_fail_closed.py b/tests/hermes_cli/test_update_fleet_check_fail_closed.pyindex cfdd26d8b6c..8b10342837f 100644--- a/tests/hermes_cli/test_update_fleet_check_fail_closed.py+++ b/tests/hermes_cli/test_update_fleet_check_fail_closed.py@@ -138,3 +138,38 @@ def test_unmapped_stop_keeps_the_restart_owed(monkeypatch, tmp_path):assert [f["step"] for f in receipt["followups"]] == ["gateway_restart"]assert "101" in receipt["followups"][0]["reason"]assert cleared == []+++def test_unmapped_stop_debt_survives_startup_until_a_current_gateway_runs(monkeypatch, tmp_path):+ # An unmapped gateway leaves no gateway_state.json, so on the next start the empty host looked+ # gateway-less and the inventory-less obligation was discharged: the stopped gateway's restart+ # debt vanished. Only a live gateway serving the checkout may settle it.+ from hermes_cli import update_cmd, update_cmd_fleet as fleet, update_cmd_fleet_verify as fleet_verify+ from hermes_cli import update_cmd_fleet_gatewayless as gatewayless, update_host_obligation as host, update_receipt++ 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(gatewayless, "host_owes_no_gateway_restart", lambda: True)+ fleet_rows: list = []+ monkeypatch.setattr(update_receipt, "collect_fleet_versions", lambda **kw: list(fleet_rows))+ fleet._write_fleet_restart_pending_marker(expected_sha="head") # armed by the pull, inventory-less+ out = fleet._GatewayRestartOutcome(+ incomplete=False, phase_errors=[], pre_restart_gateway_pids=[101], restarted_services=[],+ failed_or_stale_units=[], relaunched_profiles=[], externally_supervised_profiles=[],+ killed_pids={101}, stopped_unmapped_pids={101},+ )+ update_receipt.begin_update_receipt()+ fleet_verify._verify_fleet_after_update(out, _pre_update_plan=_plan([]), _windows_gateway_resume=None,+ update_complete=True)++ assert fleet._update_owes_fleet_restart(receipt={}, pending_manual=[]) is True+ assert fleet._pending_fleet_restart_needed(receipt={}, pending_manual=[]) is True # already-current update+ assert fleet._fleet_restart_obligation_armed()++ fleet_rows.append({"profile": "default", "state": "current", "code_sha": "head"}) # `hermes gateway run`+ assert fleet._update_owes_fleet_restart(receipt={}, pending_manual=[]) is False+ assert not fleet._fleet_restart_obligation_armed()