update_cmd_fleet_gatewayless.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Gateway-less host evidence for the update-restart obligation (``update_cmd_fleet`` sibling).
  2. An inventory-less obligation (the pre-inventory writer, or a tail that died before recording one)
  3. has no owed set, so the live gateway matrix is its whole evidence. On a host that runs no gateway
  4. (the Desktop app alone, #118742) that matrix is empty forever and the obligation could never
  5. settle. An empty probe cannot tell that host from one whose gateway the dying update stopped, so
  6. these readers ask the live host directly. A historical receipt is never consulted: it can belong
  7. to an older update and says nothing about what runs now.
  8. """
  9. from __future__ import annotations
  10. from dataclasses import asdict
  11. def runtime_outside_gateway_evidence(runtime: dict) -> bool:
  12. """A serve/dashboard row the gateway matrix neither covers nor needs to.
  13. Its supervisor owns the restart (Desktop backend, launchd/systemd unit, Windows service), or it
  14. is a manual serve whose restart ``defer_manual_serve`` has handed to its own durable reminder.
  15. Unclassified backends and failed transfers stay evidence against settlement (#115090, #111494).
  16. """
  17. from hermes_cli.update_cmd_fleet import _SUPERVISOR_OWNED_SERVE_BACKENDS
  18. from hermes_cli.update_serve_obligations import defer_manual_serve
  19. return runtime.get("kind") in ("serve", "dashboard") and (
  20. defer_manual_serve(runtime) or runtime.get("supervisor") in _SUPERVISOR_OWNED_SERVE_BACKENDS
  21. )
  22. def host_owes_no_gateway_restart() -> bool:
  23. """True when no profile expects a gateway to be running and every live runtime is outside the matrix.
  24. A ``gateway_state.json`` that does not say ``stopped``/``startup_failed`` belongs to a gateway
  25. that went away without a clean stop, which is what an update that died mid-restart leaves
  26. behind; it keeps the obligation. Profiles that never ran a gateway have no record at all.
  27. """
  28. from gateway.status import read_runtime_status
  29. from hermes_cli.update_inventory import collect_runtime_inventory
  30. from hermes_cli.update_receipt import _NOT_EXPECTED_STATES, _profile_homes
  31. for _profile, home in _profile_homes():
  32. record = read_runtime_status(home / "gateway_state.json")
  33. if record is None:
  34. continue
  35. state = record.get("gateway_state") if isinstance(record, dict) else None
  36. if not (isinstance(state, str) and state in _NOT_EXPECTED_STATES):
  37. return False
  38. return all(runtime_outside_gateway_evidence(asdict(runtime)) for runtime in collect_runtime_inventory().runtimes)