host_topology.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """Who owns the gateway role on THIS host, and which profiles does that one process serve?
  2. Multiplex-only (Teknium ruling): exactly ONE ``hermes gateway run`` per host, multiplexing every
  3. profile. Every reporting surface (doctor, ``cron status``, ``claw``, the dashboard liveness ladder)
  4. used to ask a per-PROFILE question instead — "does *my* profile own a gateway process?" — and a
  5. profile that is SERVED by the host gateway answered "no". That produced three user-visible lies:
  6. "not running", "Per-profile gateways: 0/3 up", and a silently skipped destructive-action warning.
  7. :func:`host_gateway_topology` answers the real question once, from
  8. :mod:`gateway.host_rendezvous` (the authoritative host record the winning process publishes),
  9. falling back to the default home's recorded ``served_profiles`` for a gateway that predates the
  10. record. ``default`` is just another served profile here, never a special owner.
  11. """
  12. from __future__ import annotations
  13. from dataclasses import dataclass
  14. from pathlib import Path
  15. from typing import Optional
  16. def _normalized(name: Optional[str]) -> str:
  17. if not name:
  18. return ""
  19. # Late import: ``hermes_cli.profiles`` imports gateway modules back.
  20. from hermes_cli.profiles import normalize_profile_name
  21. return normalize_profile_name(name)
  22. @dataclass(frozen=True)
  23. class HostGatewayTopology:
  24. """The single gateway process on this host plus the profile roster it multiplexes.
  25. ``source`` names the rung that answered (``host_record`` / ``served_record``) — for logs and
  26. tests only; never branch product behaviour on it.
  27. """
  28. pid: int
  29. profiles: tuple[str, ...]
  30. source: str
  31. #: Home the process was launched from (its ``gateway_state.json`` lives there); None when the
  32. #: rung cannot tell, which readers resolve to the default root.
  33. home: Optional[Path] = None
  34. def serves(self, profile_name: Optional[str]) -> bool:
  35. """True when this host process ticks/serves ``profile_name`` (``default`` included)."""
  36. wanted = _normalized(profile_name)
  37. return bool(wanted) and wanted in {_normalized(p) for p in self.profiles}
  38. def describe(self) -> str:
  39. """``the host gateway (PID 42) serving profiles default, coder`` — one shared phrasing so
  40. doctor, cron status and the state.db holder lines cannot drift apart."""
  41. roster = ", ".join(self.profiles) if self.profiles else "an unknown profile set"
  42. return f"the host gateway (PID {self.pid}) serving profiles {roster}"
  43. def _from_host_record() -> Optional[HostGatewayTopology]:
  44. from gateway import host_rendezvous as hr
  45. record = hr.read_record(hr.ROLE_GATEWAY) # already drops stale/PID-reused records
  46. if record is None or not record.pid:
  47. return None
  48. # A record whose (pid, createTime) cannot be POSITIVELY matched is a candidate, never an owner
  49. # (``host_rendezvous`` contract): reporting an unprovable record as the live host gateway would
  50. # turn a leftover record into a permanent "running" lie on every surface. A record with no
  51. # recorded createTime is exactly that — ``_same_incarnation`` treats ``None`` as "matches", so
  52. # liveness_is_proven() would bless ANY process that happens to hold the recorded PID today.
  53. if record.create_time is None or not hr.record_names_live_owner(record):
  54. return None
  55. from gateway.host_attach import launched_by_other_tenant, record_home
  56. from hermes_constants import get_hermes_home
  57. # Another tenant root's gateway is a name collision, not this tenant's host process (#121352).
  58. if launched_by_other_tenant(record.home, get_hermes_home()):
  59. return None
  60. return HostGatewayTopology(pid=int(record.pid), profiles=tuple(record.profiles), source="host_record",
  61. home=record_home(record))
  62. def _from_served_record() -> Optional[HostGatewayTopology]:
  63. """A gateway started before the host record existed still publishes ``served_profiles`` into
  64. the default home's ``gateway_state.json``; that plus a proven-live PID is the same fact."""
  65. from hermes_cli.gateway_multiplex_served import live_default_gateway_pid, recorded_served_profiles
  66. pid = live_default_gateway_pid()
  67. if pid is None:
  68. return None
  69. served = recorded_served_profiles() or []
  70. roster = ["default"] + [str(p) for p in served if _normalized(p) != "default"]
  71. return HostGatewayTopology(pid=int(pid), profiles=tuple(roster), source="served_record")
  72. def host_gateway_topology() -> Optional[HostGatewayTopology]:
  73. """The one live host gateway and its served profiles, or None when no gateway owns the role."""
  74. for rung in (_from_host_record, _from_served_record):
  75. topology = rung()
  76. if topology is not None:
  77. return topology
  78. return None
  79. def host_gateway_serving(profile_name: Optional[str] = None) -> Optional[HostGatewayTopology]:
  80. """The host gateway when it serves ``profile_name`` (default: the active profile), else None."""
  81. topology = host_gateway_topology()
  82. if topology is None:
  83. return None
  84. if profile_name is None:
  85. from hermes_cli.profiles import get_active_profile_name
  86. profile_name = get_active_profile_name()
  87. return topology if topology.serves(profile_name) else None