gateway_runtime_multiplex.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """Which daemon a named-profile client must start when nothing serves its home.
  2. A ``<root>/profiles/<name>`` home that the default multiplexer serves has no daemon of its own.
  3. When the multiplexer is down (restart, update, crash) the profile looks ``absent`` and the ensure
  4. loop would spawn a per-profile gateway for it — a second owner that answers this client and then
  5. blocks the multiplexer's next all-or-nothing reserve (``Cannot reserve gateway profiles``). The
  6. client must start the multiplexer instead, so the start target is decided here from the same facts
  7. the multiplexer boots on (``gateway_multiplex_mode.host_serves_named_profile``): a profile's own
  8. ``gateway.standalone: true``, the default profile's explicit ``true``, or — when the flag is unset or
  9. the retired ``false`` that boot settles like unset — the served set the last multiplexer recorded in
  10. the root's ``gateway_state.json`` (a standalone default gateway clears it).
  11. """
  12. from __future__ import annotations
  13. from pathlib import Path
  14. def multiplexer_root_for(home: Path) -> Path | None:
  15. """Default root that may multiplex *home*, when *home* is a named profile under it."""
  16. from hermes_constants import named_profile_home
  17. if named_profile_home(home) is None:
  18. return None
  19. root = home.parent.parent
  20. return root if root != home else None
  21. def implied_host_root(home: Path) -> Path | None:
  22. """The root whose gateway must serve named profile *home* when nothing records a verdict.
  23. ``hermes gateway run`` refuses a ``profiles/<name>`` home a gateway of its own unless it authored
  24. ``gateway.standalone: true`` (or passes ``--force``); the retired ``multiplex_profiles: false``
  25. no longer opts out, so only a standalone profile keeps its own daemon.
  26. """
  27. root = multiplexer_root_for(home)
  28. if root is None:
  29. return None
  30. from hermes_cli.gateway_multiplex_mode import host_serves_named_profile
  31. return None if host_serves_named_profile(root, home) is False else root
  32. def multiplexer_serves_home(home: Path) -> Path | None:
  33. """The root whose (possibly stopped) multiplexer serves *home*, else None.
  34. ``hermes_cli.gateway.named_profile_served_by_running_multiplexer`` answers the live question;
  35. this is its offline twin for the moment no gateway runs, deciding from the boot policy
  36. (:func:`hermes_cli.gateway_multiplex_mode.host_serves_named_profile`): a standalone secondary is
  37. never served, an explicit ``true`` serves the rest, and an unset or retired ``false`` flag falls
  38. back to the ``served_profiles`` the last multiplexer recorded. No evidence keeps it unanswered.
  39. """
  40. root = multiplexer_root_for(home)
  41. if root is None:
  42. return None
  43. from hermes_cli.gateway_multiplex_mode import host_serves_named_profile
  44. verdict = host_serves_named_profile(root, home)
  45. if verdict is not None:
  46. return root if verdict else None
  47. from gateway.status import read_runtime_status
  48. from hermes_cli.profiles import normalize_profile_name
  49. served = (read_runtime_status(root / "gateway_state.json") or {}).get("served_profiles")
  50. if not isinstance(served, list):
  51. return None
  52. names = {normalize_profile_name(str(p)) for p in served if p}
  53. return root if normalize_profile_name(home.name) in names else None