gateway_profile_lifecycle.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. """Named-profile lifecycle inside the host gateway, never a service-manager action."""
  2. from __future__ import annotations
  3. import logging
  4. from contextlib import contextmanager
  5. from hermes_constants import get_hermes_home, get_default_hermes_root
  6. from hermes_cli.profiles import parked_marker_path, profile_is_parked, profile_is_standalone, profiles_to_serve
  7. logger = logging.getLogger(__name__)
  8. def ack_confirmed(answer, key, name):
  9. return isinstance(answer, dict) and answer.get(key) == name and not answer.get("error")
  10. def ack_failure(answer):
  11. if isinstance(answer, dict):
  12. return answer.get("error") or "host operation is still pending"
  13. return "host control socket did not answer"
  14. def mark_planned_stop(pid: int | None = None) -> None:
  15. """Best-effort planned-stop marker for ``pid`` (default: the recorded gateway PID)."""
  16. try:
  17. from gateway.status import get_running_pid, write_planned_stop_marker
  18. if pid is None:
  19. pid = get_running_pid(cleanup_stale=False)
  20. if pid is not None:
  21. write_planned_stop_marker(pid)
  22. except Exception:
  23. pass
  24. def host_scope_for_all_verb(owner):
  25. """Home a ``--all`` verb acts as: the live host owner's, else the default root.
  26. ``--all`` names the ONE host gateway, whichever profile invoked the CLI — but the active home
  27. decides the service name, the Windows task/launcher and the detached child's ``--profile``, so
  28. with the host DOWN ``-p X gateway start|restart --all`` re-launched X's own gateway, which the
  29. named-profile guard then refused (exit 78): the host stayed down after a Desktop update.
  30. """
  31. from hermes_cli.gateway_migrate import _home_env
  32. return _home_env(owner.home if owner is not None else get_default_hermes_root())
  33. @contextmanager
  34. def restart_all_verb_scope(owner):
  35. """``host_scope_for_all_verb`` plus the owed online notice: ``restart --all`` restarts the host.
  36. The verb stops the host with a service call or a SIGTERM, so the old process never runs the
  37. in-band restart path that stamps the planned-restart marker; without it the next boot reads a
  38. plain stop and every home channel stays silent — #142515's gap, on every OS. Yields the stamp
  39. for the caller to run once the old host is gone and before the start: a marker written while the
  40. old host still drains can be replayed and unlinked by it, and the new host then boots silent.
  41. """
  42. from gateway.restart_notice import write_planned_restart_marker
  43. with host_scope_for_all_verb(owner):
  44. yield write_planned_restart_marker
  45. def profile_lifecycle(command: str, args) -> bool:
  46. """True when a named-profile command was handled (including an unconfirmed request)."""
  47. from hermes_cli import gateway as gw
  48. from gateway.control_socket import request_unserve_profile, request_serve_profile_hot
  49. name = gw._current_profile_name()
  50. if not name or name == "default" or getattr(args, "all", False) or getattr(args, "force", False):
  51. return False
  52. home = get_hermes_home()
  53. if profile_is_standalone(home):
  54. # gateway.standalone wins: the host never serves (or parks) an opted-out profile, so its
  55. # verbs keep addressing its own gateway process even while a stale host record lists it.
  56. return False
  57. marker = parked_marker_path(home)
  58. if command == "restart" and profile_is_parked(home) and not gw.find_gateway_pids():
  59. # `stop` parked the profile AND dropped it from the host's served set, so the unserve/serve
  60. # path below finds no host serving it and would fall through to a standalone restart.
  61. # Unpark like `start` does; a live --force gateway keeps its own restart (pids non-empty).
  62. command = "start"
  63. if command == "start":
  64. if not profile_is_parked(home):
  65. return False
  66. marker.unlink()
  67. owner = gw._host_multiplexer_for_all_verb()
  68. if owner is None:
  69. from hermes_cli.gateway_multiplex_served import live_default_gateway_pid
  70. if live_default_gateway_pid() is None:
  71. return False # Unpark even when today's normal start path must start the host first.
  72. host_home = owner.home if owner is not None else get_default_hermes_root()
  73. answer = request_serve_profile_hot(host_home, name)
  74. if ack_confirmed(answer, "served", name):
  75. print(f"Profile '{name}' served by the host gateway.")
  76. else:
  77. print(f"Profile '{name}' unparked, but serving was not confirmed: {ack_failure(answer)}.")
  78. print("The host retries on its next rescan (within 30s). Check gateway status.")
  79. return True
  80. # A separate --force gateway still owns its normal process/service lifecycle.
  81. if gw.find_gateway_pids():
  82. return False
  83. owner = gw._served_by_another_host_gateway()
  84. if owner is None and not gw.named_profile_served_by_running_multiplexer():
  85. return False
  86. host_home = owner.home if owner is not None else get_default_hermes_root()
  87. # Unserving stops this profile's executing turns: the live-work policy applies before the teardown.
  88. from hermes_cli.gateway_lifecycle_guards import refuse_while_sessions_live
  89. refuse_while_sessions_live(command, args, host_home=host_home, profile=name)
  90. if command == "stop":
  91. # Persist intent BEFORE teardown: the periodic rescan must not re-add this profile.
  92. marker.touch()
  93. answer = request_unserve_profile(host_home, name)
  94. if ack_confirmed(answer, "unserved", name):
  95. print(f"Profile '{name}' parked; its bots and cron are stopped. "
  96. f"Start again with: hermes -p {name} gateway start")
  97. else:
  98. print(f"Profile '{name}' parked, but immediate stop was not confirmed: {ack_failure(answer)}.")
  99. print("The host drops it on its next rescan (within 30s).")
  100. return True
  101. answer = request_unserve_profile(host_home, name)
  102. if not ack_confirmed(answer, "unserved", name):
  103. print(f"Profile '{name}' restart was not confirmed: {ack_failure(answer)}.")
  104. return True
  105. answer = request_serve_profile_hot(host_home, name)
  106. if ack_confirmed(answer, "served", name):
  107. print(f"Profile '{name}' restarted by the host gateway.")
  108. else:
  109. print(f"Profile '{name}' stopped, but serving was not confirmed: {ack_failure(answer)}.")
  110. print("The host retries on its next rescan (within 30s). Check gateway status.")
  111. return True
  112. def print_parked_status() -> bool:
  113. """A parked satellite is still installed, but is not a running gateway."""
  114. from hermes_cli import gateway as gw
  115. name = gw._current_profile_name()
  116. if name and name != "default" and profile_is_parked(get_hermes_home()):
  117. print(f"Profile '{name}': parked (hermes -p {name} gateway start)")
  118. return True
  119. if not name or name == "default":
  120. parked = [profile for profile, home in profiles_to_serve(True, include_parked=True)
  121. if profile != "default" and profile_is_parked(home)]
  122. if parked:
  123. owner = gw.host_multiplexer_serving()
  124. if owner is not None:
  125. print(f"Served profiles: {', '.join(owner.profiles)}")
  126. for profile in parked:
  127. print(f"Profile '{profile}': parked (hermes -p {profile} gateway start)")
  128. return False
  129. def owned_gateway_pid() -> int | None:
  130. """This profile's own gateway pid, only on positive, fresh ownership evidence; else None.
  131. A served profile's runtime record carries the host multiplexer's pid (#141133), so the recorded
  132. pid alone never authorizes a stop. Every ``find_gateway_pids()`` caller (stop, uninstall's kill,
  133. ``service_manager.is_running``, cron readiness, Windows start polls) reads it through here.
  134. """
  135. from gateway.status import get_running_pid, _get_process_hermes_home
  136. try:
  137. pid = get_running_pid()
  138. if pid is None or not _pid_owned_by_home(pid, _get_process_hermes_home()):
  139. return None
  140. # The runtime may have switched owners while the OS/control probes ran.
  141. return pid if get_running_pid() == pid else None
  142. except Exception:
  143. # An inaccessible rendezvous or process is never permission to kill it.
  144. logger.debug("Could not prove gateway process ownership", exc_info=True)
  145. return None
  146. def _pid_owned_by_home(pid: int, home) -> bool:
  147. """Ownership ladder, cheapest proof first; the control socket is dialled only for the host's pid.
  148. ``gateway status`` asks once per profile and every served profile's record names the host, so the
  149. host record (a file read plus an incarnation check) settles those without a dial.
  150. """
  151. from gateway import host_attach, host_rendezvous as hr
  152. from gateway.status import _same_hermes_home
  153. from hermes_cli.dashboard_procs import _hermes_home_for_pid
  154. record = hr.read_record(hr.ROLE_GATEWAY)
  155. is_host = record is not None and record.pid == pid and hr.record_names_live_owner(record)
  156. if is_host and not _same_hermes_home(host_attach.record_home(record), home):
  157. return False # the live host, launched from another home
  158. if _launch_record_owns(pid, home):
  159. return True
  160. if is_host:
  161. host_attach.invalidate_host_gateway_cache()
  162. owner = host_attach.host_gateway()
  163. # A verified identity answer from the host's own home needs no argv profile (bare launch).
  164. if owner is not None and owner.pid == pid and owner.served_known:
  165. return _same_hermes_home(owner.home, home)
  166. process_home = _hermes_home_for_pid(pid, require_launch_identity=True)
  167. return process_home is not None and _same_hermes_home(process_home, home)
  168. def _launch_record_owns(pid: int, home) -> bool:
  169. """``<home>/gateway.pid`` names *pid*'s exact incarnation as launched from *home*.
  170. A gateway writes that file once, about its own launch home (the host never writes one into a
  171. served profile), so it pins the profile resolved at launch: a ``gateway run --force`` that relied
  172. on the sticky ``active_profile`` stays stoppable after the sticky choice changes, which argv and
  173. environment alone cannot prove.
  174. """
  175. from gateway.status import (
  176. _GATEWAY_KIND, _get_process_start_time, _pid_from_record, _read_pid_record, _same_hermes_home)
  177. record = _read_pid_record() or {}
  178. started, recorded_home = record.get("start_time"), record.get("hermes_home")
  179. return (record.get("kind") == _GATEWAY_KIND and _pid_from_record(record) == pid
  180. and started is not None and started == _get_process_start_time(pid)
  181. and isinstance(recorded_home, str) and _same_hermes_home(recorded_home, home))
  182. def report_profile_stop_failure() -> None:
  183. """Do not report absence when a live PID was refused or the probe failed."""
  184. from gateway.status import get_running_pid
  185. from hermes_cli import gateway as gw
  186. try:
  187. absent = get_running_pid() is None and not gw.find_gateway_pids()
  188. except Exception:
  189. logger.debug("Could not confirm gateway absence after stop", exc_info=True)
  190. absent = False
  191. if absent:
  192. print("✗ No gateway running for this profile")
  193. return
  194. gw.print_error("Gateway stop was not confirmed; a gateway may still be running. "
  195. "Check gateway status and process ownership.")
  196. _print_host_hint()
  197. raise SystemExit(1)
  198. def _print_host_hint() -> None:
  199. """Name the refused pid when it is the host gateway serving this profile (#141133)."""
  200. from gateway import host_rendezvous as hr
  201. from gateway.status import get_running_pid
  202. try:
  203. pid, record = get_running_pid(), hr.read_record(hr.ROLE_GATEWAY)
  204. except Exception:
  205. logger.debug("Could not read the host record for the stop hint", exc_info=True)
  206. return
  207. if pid is not None and record is not None and record.pid == pid:
  208. print(f" PID {pid} is the host gateway serving this profile; stopping it would take every "
  209. f"served profile offline. Stop the host instead: hermes gateway stop --all")