gateway_runtime_start.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """One detached-process primitive; callers must first prove owner/service absence."""
  2. from __future__ import annotations
  3. import os
  4. from pathlib import Path
  5. import subprocess
  6. import sys
  7. from hermes_cli._subprocess_compat import windows_detach_popen_kwargs, _WINDOWS_GATEWAY_BREAKAWAY_ENV
  8. from hermes_cli.gateway_launchd_runtime import GatewayJob, start_gateway_job
  9. from hermes_cli.gateway_runtime_service import RuntimeStartError, remaining
  10. # One launch's policy and pins, never the shared daemon's: hook consent would become daemon-lifetime
  11. # auto-approval for messaging/cron, and a Kanban worker's or terminal's pins would retarget every later
  12. # session's board, cwd and source. The behaviour values main's CLI read per process (ephemeral prompt,
  13. # prefill file, provider request, project plugins) ride each session's ``client_env`` instead
  14. # (agent.session_client_env); an operator-started gateway keeps its own environment.
  15. DAEMON_STRIPPED_ENV = (
  16. "HERMES_PROFILE", "HERMES_YOLO_MODE", "HERMES_IGNORE_RULES", "HERMES_SAFE_MODE",
  17. "HERMES_IGNORE_USER_CONFIG", "HERMES_ACCEPT_HOOKS", "HERMES_SESSION_SOURCE", "TERMINAL_CWD",
  18. "HERMES_KANBAN_TASK", "HERMES_KANBAN_WORKSPACE", "HERMES_KANBAN_BRANCH", "HERMES_KANBAN_RUN_ID",
  19. "HERMES_KANBAN_CLAIM_LOCK", "HERMES_KANBAN_CLAIM_TTL_SECONDS", "HERMES_KANBAN_GOAL_MODE",
  20. "HERMES_KANBAN_GOAL_MAX_TURNS", "HERMES_KANBAN_DB", "HERMES_KANBAN_WORKSPACES_ROOT",
  21. "HERMES_KANBAN_BOARD", "HERMES_TENANT", "HERMES_SESSION_SOURCE_EXPLICIT", "HERMES_TURN_AUTHOR",
  22. # Human-presence/one-shot markers: inherited, every messaging/cron turn would read as attended or -q.
  23. "HERMES_INTERACTIVE", "HERMES_GATEWAY_SESSION", "HERMES_SINGLE_QUERY_SESSION",
  24. "HERMES_EPHEMERAL_SYSTEM_PROMPT", "HERMES_PREFILL_MESSAGES_FILE", "HERMES_INFERENCE_PROVIDER",
  25. "HERMES_ENABLE_PROJECT_PLUGINS", "HERMES_TUI_CLIENT_ENV",
  26. # A terminal's shell under a systemd service (gnome-terminal-server, a user unit) carries these; the
  27. # unmanaged daemon would then identify as "systemd" and maintenance would refuse it as supervised.
  28. "INVOCATION_ID", "JOURNAL_STREAM",
  29. # Same for any process below a launchd/--external-supervisor gateway (its agents' tools, scripts they
  30. # run): the daemon would identify as "launchd", never arm --idle-exit, and outlive every client.
  31. # HERMES_S6_SUPERVISED_CHILD stays: without it `gateway run` in an s6 container redirects to s6.
  32. "XPC_SERVICE_NAME", "LAUNCHD_SOCKET", "HERMES_LAUNCHD_LABEL",
  33. "HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "HERMES_SUPERVISED_CHILD",
  34. # systemd's notify socket and watchdog name the unit's main process: the daemon would feed them.
  35. "NOTIFY_SOCKET", "WATCHDOG_USEC", "WATCHDOG_PID",
  36. )
  37. def spawn_unmanaged_gateway(profile_home: Path, *, deadline: float, idle_exit: bool = False,
  38. force: bool = False) -> subprocess.Popen | GatewayJob:
  39. """Request a daemon, not readiness. Refuse Windows no-breakaway fallback.
  40. No --replace, persistence, login changes, elevation, shell, or inherited stdio.
  41. The gateway runtime's own exclusive ownership fence arbitrates racing starts.
  42. ``idle_exit``: the daemon ends itself once idle (a chat/TUI/cron client's own start; never
  43. Desktop's ``gateway ensure``, which keeps its attached gateway for the app's lifetime).
  44. ``force``: a ``gateway restart --force --detach`` replacement keeps the ``--force`` its old owner
  45. needed (a named profile under a multiplexer), or it exits on the run-side guard.
  46. """
  47. home = profile_home.resolve()
  48. root = Path(__file__).resolve().parent.parent
  49. # A root home is only pinned by an explicit selector: without one the child's
  50. # _apply_profile_override follows the sticky active_profile and boots the wrong
  51. # profile's daemon. A <root>/profiles/<name> home is already trusted as-is.
  52. selector = [] if home.parent.name == "profiles" else ["--profile", "default"]
  53. command = [sys.executable, "-m", "hermes_cli.main", *selector, "gateway", "run", "--quiet",
  54. *(["--idle-exit"] if idle_exit else []), *(["--force"] if force else [])]
  55. # The target profile's own secrets, never the launching client's: a `hermes -p work` client's
  56. # dotenv would otherwise become the default daemon's frozen launch env (host_gateway_child_env).
  57. from tools.environments.local import served_profile_child_env
  58. env = served_profile_child_env(base=dict(os.environ), target_home=home, inherit_credentials=True)
  59. if sys.platform == "win32":
  60. from hermes_cli.gateway_windows import windowless_gateway_restart_spec
  61. command, _, overlay = windowless_gateway_restart_spec(command)
  62. if not overlay:
  63. raise RuntimeStartError("windows_interpreter_unavailable")
  64. env.update(overlay)
  65. env[_WINDOWS_GATEWAY_BREAKAWAY_ENV] = "1"
  66. env.update(HERMES_HOME=str(home), HERMES_GATEWAY_DETACHED="1", PYTHONIOENCODING="utf-8")
  67. # Profile selection is the explicit home, not the invoking client's display
  68. # name. Launch-only approval/context/config flags ride the launching session's frozen
  69. # policy, not the shared daemon or every later session.
  70. from gateway.session_context import _VAR_MAP # the launching turn's session identity, too
  71. from tools.environments.local_env_policy import carry_gateway_profile_pin
  72. carry_gateway_profile_pin(env) # the supervisor markers go; the daemon's tools keep their pin
  73. for key in (*DAEMON_STRIPPED_ENV, *_VAR_MAP):
  74. env.pop(key, None)
  75. remaining(deadline)
  76. logs = home / "logs"
  77. logs.mkdir(parents=True, exist_ok=True)
  78. if sys.platform == "darwin": # LAN access must outlive the app that asked for the gateway
  79. if job := start_gateway_job(command, env=env, cwd=root, log_path=logs / "gateway-stdio.log",
  80. deadline=deadline):
  81. return job
  82. try:
  83. with (logs / "gateway-stdio.log").open("ab", buffering=0) as output:
  84. remaining(deadline)
  85. return subprocess.Popen(command, cwd=root, env=env, close_fds=True,
  86. stdin=subprocess.DEVNULL, stdout=output, stderr=output,
  87. # detached-spawn: ok — a gateway daemon launch must outlive its launcher
  88. **windows_detach_popen_kwargs())
  89. except OSError as exc:
  90. reason = "windows_breakaway_unavailable" if sys.platform == "win32" else "spawn_failed"
  91. raise RuntimeStartError(reason) from exc
  92. def start_detached_replacement(profile_home: Path, *, force: bool = False) -> int:
  93. """``gateway restart --detach`` with no service: start the stopped owner's replacement the way
  94. ``gateway ensure`` starts an absent one. In the foreground, the restarting client (Desktop's
  95. bounded, piped call) would itself become the long-lived gateway it only meant to restart."""
  96. import time
  97. try:
  98. spawn_unmanaged_gateway(profile_home, deadline=time.monotonic() + 30, force=force)
  99. except RuntimeStartError as exc:
  100. print(f"✗ Could not start the gateway in the background: {exc}", file=sys.stderr)
  101. return 1
  102. print("✓ Started the gateway in the background")
  103. return 0
  104. def stdio_log_path(profile_home: Path) -> Path:
  105. return profile_home / "logs" / "gateway-stdio.log"
  106. def stdio_log_size(profile_home: Path) -> int:
  107. """Where this launch's output will start in the append-only stdio log."""
  108. try:
  109. return stdio_log_path(profile_home).stat().st_size
  110. except OSError:
  111. return 0
  112. _BANNER_PREFIXES = ("┌", "│", "├", "└")
  113. _TAIL_LINES = 15
  114. _TAIL_BYTES = 64 * 1024
  115. def startup_failure_report(profile_home: Path, offset: int, status: int, pid: int | None = None) -> str:
  116. """What a client prints when the gateway it started died before serving: the exit status, the
  117. redacted tail of THIS launch's stdio (the traceback lives there, written before logging is up and
  118. before the detached-stdio redactor is installed) and the command that shows the whole log."""
  119. path = stdio_log_path(profile_home)
  120. try:
  121. with path.open("rb") as log:
  122. log.seek(max(offset, path.stat().st_size - _TAIL_BYTES))
  123. text = log.read().decode("utf-8", errors="replace")
  124. except OSError:
  125. text = ""
  126. lines = [line.rstrip() for line in text.splitlines()
  127. if line.strip() and not line.lstrip().startswith(_BANNER_PREFIXES)][-_TAIL_LINES:]
  128. if not lines:
  129. # A clean refusal (config/credential verdict, exit 78) is logged, not printed: the runtime
  130. # status file names it.
  131. from gateway.status import read_runtime_status
  132. status_file = read_runtime_status(profile_home / "gateway_state.json") or {}
  133. if status_file.get("exit_reason") and pid is not None and status_file.get("pid") == pid:
  134. lines = [str(status_file["exit_reason"])[:2000]]
  135. path = profile_home / "logs" / "gateway.log"
  136. from agent.redact import redact_sensitive_text
  137. tail = redact_sensitive_text("\n".join(lines), force=True, redact_url_credentials=True)
  138. see = f'type "{path}"' if sys.platform == "win32" else f"tail -n 200 '{path}'"
  139. body = "\n".join(" " + line for line in tail.splitlines()) if tail else " (it wrote no output)"
  140. return f"the gateway exited with status {status} before it could serve:\n{body}\nFull log: {see}"