status.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. """Status command for hermes CLI."""
  2. import json
  3. import os
  4. import sys
  5. import time
  6. import importlib.util
  7. from pathlib import Path
  8. from types import SimpleNamespace
  9. PROJECT_ROOT = Path(__file__).parent.parent.resolve()
  10. from hermes_cli.auth import AuthError, resolve_provider
  11. from hermes_cli.colors import Colors, color
  12. from hermes_cli.config import get_env_path, get_env_value, get_hermes_home, load_config
  13. from hermes_cli.config_defaults import DEFAULT_SANDBOX_IMAGE, DEFAULT_VERCEL_IMAGE
  14. from hermes_cli.models import provider_label
  15. from hermes_cli.runtime_provider import resolve_requested_provider
  16. from hermes_cli.vercel_auth import describe_vercel_auth
  17. from hermes_cli.status_auth import ( # renderers wired into _SECTIONS below
  18. _render_api_keys, _render_apikey_providers, _render_auth_providers, _render_nous_gateway)
  19. from hermes_constants import OPENROUTER_MODELS_URL
  20. def check_mark(ok: bool) -> str:
  21. return color("✓", Colors.GREEN) if ok else color("✗", Colors.RED)
  22. def _section(title: str) -> None:
  23. """Print a blank line followed by a bold cyan ``◆`` section heading."""
  24. print()
  25. print(color(f"◆ {title}", Colors.CYAN, Colors.BOLD))
  26. def _row(name: str, ok: bool, text: str, width: int = 12, sep: str = " ") -> None:
  27. """Print one ``name ✓/✗ text`` status row."""
  28. print(f" {name:<{width}}{sep}{check_mark(ok)} {text}")
  29. def _detail(label: str, value) -> None:
  30. """Print an indented ``label: value`` detail line under a status row."""
  31. _kv(label, value, " ", 12)
  32. def _kv(label: str, value, indent: str = " ", width: int = 14) -> None:
  33. """Print a `` Label: value`` line (label padded to the 14-col status layout)."""
  34. print(f"{indent}{label:<{width}}{value}")
  35. def _kv_flag(label: str, ok, on: str, off: str) -> None:
  36. """``_kv`` with a ✓/✗ mark followed by ``on`` or ``off`` text."""
  37. _kv(label, f"{check_mark(bool(ok))} {on if ok else off}")
  38. def _configured(ok) -> str:
  39. return "configured" if ok else "not configured"
  40. def _first_env_value(names) -> str:
  41. """Return the first non-empty env value among ``names`` (a str or tuple of names)."""
  42. return next((v for v in (get_env_value(n) or "" for n in ((names,) if isinstance(names, str) else names)) if v), "")
  43. def _configured_model_label(config: dict) -> str:
  44. """Return the configured default model from config.yaml."""
  45. model_cfg = config.get("model")
  46. if isinstance(model_cfg, dict):
  47. model_cfg = model_cfg.get("default") or model_cfg.get("name") or ""
  48. return (model_cfg.strip() if isinstance(model_cfg, str) else "") or "(not set)"
  49. def _effective_provider_label() -> str:
  50. """Return the provider label matching current CLI runtime resolution."""
  51. requested = resolve_requested_provider()
  52. try:
  53. effective = resolve_provider(requested)
  54. except AuthError:
  55. effective = requested or "auto"
  56. if effective == "openrouter":
  57. # A custom endpoint may live in config.yaml (model.base_url, the canonical location) or
  58. # the legacy OPENAI_BASE_URL env var; either way labeling it "OpenRouter" is misleading.
  59. try:
  60. model_cfg = load_config().get("model")
  61. except Exception:
  62. model_cfg = None
  63. config_base_url = (model_cfg.get("base_url") or "").strip() if isinstance(model_cfg, dict) else ""
  64. if config_base_url or get_env_value("OPENAI_BASE_URL"):
  65. effective = "custom"
  66. return provider_label(effective)
  67. def _estop_status_line():
  68. """One-line pause banner for `hermes status`, or None when not paused."""
  69. try:
  70. from agent.estop import get_state
  71. except ImportError:
  72. return None
  73. state = get_state()
  74. if state is None:
  75. return None
  76. reason = state.get("reason")
  77. return f"⏸️ PAUSED (global emergency stop{f' — reason: {reason}' if reason else ''}; `hermes resume` to lift)"
  78. # --- Data tables driving the per-section renderers -------------------------
  79. # Simple env-driven terminal backends: (label, env var, default, empty-counts-as-unset).
  80. _TERMINAL_ENV_ROWS = {
  81. "ssh": (("SSH Host:", "TERMINAL_SSH_HOST", "(not set)", True), ("SSH User:", "TERMINAL_SSH_USER", "(not set)", True)),
  82. "docker": (("Docker Image:", "TERMINAL_DOCKER_IMAGE", DEFAULT_SANDBOX_IMAGE, False),),
  83. "daytona": (("Daytona Image:", "TERMINAL_DAYTONA_IMAGE", DEFAULT_SANDBOX_IMAGE, False),),
  84. }
  85. # Gateway manager label when the runtime snapshot is unavailable, keyed by platform.
  86. _GATEWAY_FALLBACK = {"termux": ("unknown", "Termux / manual process"), "linux": ("unknown", "systemd/manual"),
  87. "darwin": ("unknown", "launchd")}
  88. def _banner(lines, *styles) -> None:
  89. """Blank line, then each line in ``styles``."""
  90. print()
  91. for line in lines:
  92. print(color(line, *styles))
  93. def _render_header(ctx):
  94. _banner(("┌─────────────────────────────────────────────────────────┐",
  95. "│ ☤ Hermes Agent Status │",
  96. "└─────────────────────────────────────────────────────────┘"), Colors.CYAN)
  97. paused = _estop_status_line()
  98. if paused:
  99. _banner((paused,), Colors.YELLOW, Colors.BOLD)
  100. def _load_ctx_config(ctx) -> None:
  101. """Load config.yaml into the shared context (fail-soft)."""
  102. try:
  103. ctx.config = load_config()
  104. except Exception:
  105. ctx.config = {}
  106. def _render_environment(ctx):
  107. _section("Environment")
  108. _kv("Project:", PROJECT_ROOT)
  109. _kv("Python:", sys.version.split()[0])
  110. _kv_flag(".env file:", get_env_path().exists(), "exists", "not found")
  111. _load_ctx_config(ctx)
  112. _kv("Model:", _configured_model_label(ctx.config))
  113. _kv("Provider:", _effective_provider_label())
  114. def _render_terminal(ctx):
  115. _section("Terminal Backend")
  116. terminal_cfg = ctx.config.get("terminal", {}) if isinstance(ctx.config.get("terminal"), dict) else {}
  117. terminal_env = os.getenv("TERMINAL_ENV", "") or terminal_cfg.get("backend", "local")
  118. _kv("Backend:", terminal_env)
  119. if terminal_env in _TERMINAL_ENV_ROWS:
  120. for label, var, default, empty_is_unset in _TERMINAL_ENV_ROWS[terminal_env]:
  121. value = (os.getenv(var, "") or default) if empty_is_unset else os.getenv(var, default)
  122. print(f" {label:<13} {value}")
  123. elif terminal_env == "vercel_sandbox":
  124. persist = os.getenv("TERMINAL_CONTAINER_PERSISTENT")
  125. persist_enabled = (bool(terminal_cfg.get("container_persistent", True)) if persist is None
  126. else persist.lower() in {"1", "true", "yes", "on"})
  127. auth_status = describe_vercel_auth()
  128. _kv("Image:", os.getenv('TERMINAL_VERCEL_RUNTIME') or terminal_cfg.get('vercel_runtime')
  129. or os.getenv('TERMINAL_VERCEL_IMAGE') or terminal_cfg.get('vercel_image') or DEFAULT_VERCEL_IMAGE)
  130. _kv_flag("SDK:", importlib.util.find_spec("vercel") is not None, "installed",
  131. "missing (run hermes setup terminal and select Vercel Sandbox, then restart Hermes)")
  132. _kv("Auth:", f"{check_mark(auth_status.ok)} {auth_status.label}")
  133. for line in auth_status.detail_lines:
  134. _kv("Auth detail:", line)
  135. _kv("Persistence:", 'snapshot filesystem' if persist_enabled else 'ephemeral filesystem')
  136. _kv("Processes:", "live processes do not survive cleanup, snapshots, or sandbox recreation")
  137. else:
  138. # Plugin-registered terminal backends: show availability via the provider's doctor rows
  139. # (fail-soft — never break `hermes status`).
  140. try:
  141. from hermes_cli.plugins import discover_plugins
  142. discover_plugins()
  143. from agent.terminal_env_registry import get_provider
  144. provider = get_provider(terminal_env)
  145. if provider is not None:
  146. for ok, label, text in provider.doctor_checks():
  147. print(f" {label}: {check_mark(bool(ok))} {text}")
  148. except Exception:
  149. pass
  150. _kv_flag("Sudo:", os.getenv("SUDO_PASSWORD", ""), "enabled", "disabled")
  151. def _render_platforms(ctx):
  152. """One row per platform, judged by the gateway (same verdict as the summary's Platforms line)."""
  153. _section("Messaging Platforms")
  154. try:
  155. from gateway.config import load_gateway_config
  156. from gateway.platform_registry import platform_registry
  157. from hermes_cli.plugins import discover_plugins
  158. discover_plugins() # labels come from the plugin entries
  159. cfg = load_gateway_config()
  160. except Exception:
  161. return
  162. connected = {p.value for p in cfg.get_connected_platforms()}
  163. homes = {p.value: pc.home_channel for p, pc in cfg.platforms.items() if pc.home_channel}
  164. labels = {e.name: e.label for e in platform_registry.plugin_entries()}
  165. for value, label in sorted(labels.items(), key=lambda kv: kv[1].lower()):
  166. home = homes.get(value)
  167. _row(label, value in connected,
  168. _configured(value in connected) + (f" (home: {home.chat_id})" if home else ""))
  169. def _render_gateway(ctx):
  170. _section("Gateway Service")
  171. try:
  172. from hermes_cli.gateway import (
  173. get_gateway_runtime_snapshot, _format_gateway_pids, named_profile_served_by_running_multiplexer)
  174. from hermes_cli.gateway_multiplex_served import multiplexer_served_secondaries
  175. snapshot = get_gateway_runtime_snapshot()
  176. # A satellite profile has no gateway.pid of its own; the default multiplexer is its live process.
  177. if not snapshot.running and named_profile_served_by_running_multiplexer():
  178. _kv_flag("Status:", True, "running (via the default-profile multiplexer)", "stopped")
  179. _kv("Manage with:", "hermes gateway status # from the default profile")
  180. return
  181. _kv_flag("Status:", snapshot.running, "running", "stopped")
  182. _kv("Manager:", snapshot.manager)
  183. if snapshot.gateway_pids:
  184. _kv("PID(s):", _format_gateway_pids(snapshot.gateway_pids))
  185. if snapshot.running and (served := multiplexer_served_secondaries()):
  186. _kv("Serves:", ", ".join(served))
  187. from hermes_cli.gateway_multiplex_served import served_profile_ingress_urls
  188. for name, per_platform in sorted(served_profile_ingress_urls().items()):
  189. for platform, url in sorted(per_platform.items()):
  190. _kv(f" {name}/{platform}:", url)
  191. if snapshot.has_process_service_mismatch:
  192. _kv("Service:", "installed but not managing the current running gateway")
  193. elif snapshot.service_installed and not snapshot.service_running:
  194. _kv("Service:", "installed but stopped")
  195. except Exception:
  196. platform = "linux" if sys.platform.startswith("linux") else sys.platform
  197. status_text, manager = _GATEWAY_FALLBACK.get(platform, ("N/A", "(not supported on this platform)"))
  198. _kv("Status:", color(status_text, Colors.DIM))
  199. _kv("Manager:", manager)
  200. def _load_json(path: Path, encoding: str = "utf-8"):
  201. with open(path, encoding=encoding) as f:
  202. return json.load(f)
  203. def _cron_summary() -> str:
  204. """``N active, M total`` jobs line, shared by the full and short status renderers."""
  205. jobs_file = get_hermes_home() / "cron" / "jobs.json"
  206. if not jobs_file.exists():
  207. return "0"
  208. try:
  209. # utf-8-sig: same dialect as cron/jobs.load_jobs — Windows editors may leave a UTF-8 BOM
  210. # that plain utf-8 json.load rejects.
  211. jobs = _load_json(jobs_file, "utf-8-sig").get("jobs", [])
  212. return f"{sum(1 for j in jobs if j.get('enabled', True))} active, {len(jobs)} total"
  213. except Exception:
  214. return "(error reading jobs file)"
  215. def _render_cron(ctx):
  216. _section("Scheduled Jobs")
  217. _kv("Jobs:", _cron_summary())
  218. def _render_sessions(ctx):
  219. _section("Sessions")
  220. # Gateway session count: state.db is the source of truth; fall back to sessions.json for
  221. # pre-migration installs.
  222. try:
  223. from hermes_state import SessionDB
  224. db = SessionDB(db_path=get_hermes_home() / "state.db", read_only=True)
  225. try:
  226. gateway_rows = db.list_gateway_sessions(active_only=True) or []
  227. finally:
  228. db.close()
  229. except Exception:
  230. gateway_rows = []
  231. if gateway_rows:
  232. _kv("Active:", f"{len(gateway_rows)} session(s)")
  233. freshest = max((float(r.get("last_active") or 0) for r in gateway_rows), default=0.0)
  234. if freshest > 0:
  235. from hermes_cli.timefmt import relative_time
  236. print(f" Last activity:{relative_time(freshest):>13}")
  237. elif not (sessions_file := get_hermes_home() / "sessions" / "sessions.json").exists():
  238. _kv("Active:", 0)
  239. else:
  240. try:
  241. data = _load_json(sessions_file, encoding="utf-8-sig")
  242. entries = [k for k in data if not str(k).startswith("_")] if isinstance(data, dict) else []
  243. _kv("Active:", f"{len(entries)} session(s)")
  244. except Exception:
  245. _kv("Active:", "(error reading sessions file)")
  246. # Slot usage, only when max_concurrent_sessions is set. The cap is shared across CLI,
  247. # desktop/TUI and the messaging gateway, so the surface that gets rejected is rarely the one
  248. # holding the slots — without this the only way to find out is reading
  249. # runtime/active_sessions.json by hand.
  250. try:
  251. from hermes_cli.active_sessions import (
  252. active_session_registry_snapshot, format_age, resolve_max_concurrent_sessions)
  253. cap = resolve_max_concurrent_sessions(ctx.config)
  254. except Exception:
  255. cap = None
  256. if cap:
  257. try:
  258. held = active_session_registry_snapshot()
  259. except Exception:
  260. held = []
  261. _kv("Slots:", color(f"{len(held)}/{cap} in use", Colors.YELLOW if len(held) >= cap else Colors.GREEN))
  262. now = time.time()
  263. for entry in sorted(held, key=lambda e: e.get("started_at") or 0):
  264. age = format_age(now - float(entry.get("started_at") or now))
  265. print(f" {entry.get('surface') or 'unknown':<17} {entry.get('session_id') or '?':<24} {age}")
  266. def _render_deep(ctx):
  267. if not ctx.deep:
  268. return
  269. _section("Deep Checks")
  270. openrouter_key = os.getenv("OPENROUTER_API_KEY", "")
  271. if openrouter_key:
  272. try:
  273. import httpx
  274. response = httpx.get(OPENROUTER_MODELS_URL, headers={"Authorization": f"Bearer {openrouter_key}"}, timeout=10)
  275. _kv_flag("OpenRouter:", response.status_code == 200, "reachable", f"error ({response.status_code})")
  276. except Exception as e:
  277. _kv("OpenRouter:", f"{check_mark(False)} error: {e}")
  278. try: # gateway port, informational: in use == gateway likely running
  279. import socket
  280. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  281. sock.settimeout(1)
  282. port_in_use = sock.connect_ex(('127.0.0.1', 18789)) == 0
  283. sock.close()
  284. _kv("Port 18789:", 'in use' if port_in_use else 'available')
  285. except OSError:
  286. pass
  287. def _render_footer(ctx):
  288. _banner(("─" * 60, " Run 'hermes doctor' for detailed diagnostics", " Run 'hermes setup' to configure"),
  289. Colors.DIM)
  290. print()
  291. def _connected_platforms() -> list:
  292. """Platforms the gateway would start (``GatewayConfig.get_connected_platforms``)."""
  293. from gateway.config import load_gateway_config
  294. return load_gateway_config().get_connected_platforms()
  295. def _connected_platform_labels() -> list:
  296. from gateway.platform_registry import platform_registry
  297. return [getattr(platform_registry.get(p.value), "label", p.value) for p in _connected_platforms()]
  298. def _authenticated_provider_names() -> list:
  299. """The providers the /model picker offers: cached catalogs only, no endpoint probes."""
  300. from hermes_cli.inventory import load_picker_context
  301. from hermes_cli.model_switch import list_authenticated_providers
  302. pick = load_picker_context()
  303. return [row["name"] for row in list_authenticated_providers(
  304. user_providers=pick.user_providers, custom_providers=pick.custom_providers,
  305. excluded_providers=pick.excluded_providers, max_models=0, non_blocking_catalogs=True,
  306. probe_custom_providers=False)]
  307. def _gateway_state() -> tuple:
  308. from hermes_cli.gateway import get_gateway_runtime_snapshot, named_profile_served_by_running_multiplexer
  309. if get_gateway_runtime_snapshot().running:
  310. return True, "running"
  311. if named_profile_served_by_running_multiplexer(): # satellite profile: no gateway.pid of its own
  312. return True, "running (via the default-profile multiplexer)"
  313. return False, "stopped"
  314. def _summary_row(label: str, probe, empty: str) -> None:
  315. """One summary row; a failing probe degrades only its own row."""
  316. try:
  317. values = list(dict.fromkeys(probe()))
  318. except Exception:
  319. values = None
  320. _kv(label, "unknown" if values is None else ", ".join(values) or empty)
  321. def _render_summary(ctx):
  322. """Default ``hermes status``: one line per component; ``--full`` prints every section."""
  323. _render_header(ctx)
  324. print()
  325. _load_ctx_config(ctx)
  326. _kv("Model:", _configured_model_label(ctx.config))
  327. _kv("Provider:", _effective_provider_label())
  328. _summary_row("Providers:", _authenticated_provider_names, "none connected")
  329. try:
  330. running, text = _gateway_state()
  331. _kv_flag("Gateway:", running, text, text)
  332. except Exception:
  333. _kv("Gateway:", "unknown")
  334. _summary_row("Platforms:", _connected_platform_labels, "none configured")
  335. _kv("Jobs:", _cron_summary())
  336. _banner((" Run 'hermes status --full' for every section",), Colors.DIM)
  337. print()
  338. # Print order of `hermes status`; each renderer takes the shared _StatusContext.
  339. _SECTIONS = (
  340. _render_header, _render_environment, _render_api_keys, _render_auth_providers, _render_nous_gateway,
  341. _render_apikey_providers, _render_terminal, _render_platforms, _render_gateway, _render_cron,
  342. _render_sessions, _render_deep, _render_footer)
  343. def show_status(args):
  344. """One-screen summary by default; ``--full``/``--deep`` print every section."""
  345. # Shared by section renderers: config, --deep, and the Nous login facts Auth Providers derives
  346. # for the later Nous Tool Gateway section.
  347. ctx = SimpleNamespace(deep=getattr(args, 'deep', False), config={}, nous_logged_in=False,
  348. nous_inference_present=False, nous_account_info=None)
  349. for render in _SECTIONS if getattr(args, 'full', False) or ctx.deep else (_render_summary,):
  350. render(ctx)