dump.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. """Dump command for hermes CLI."""
  2. import json
  3. import os
  4. import platform
  5. import subprocess
  6. import sys
  7. from pathlib import Path
  8. from hermes_cli.config import get_hermes_home, get_env_path, get_project_root, load_config
  9. from hermes_cli.env_loader import load_hermes_dotenv
  10. from hermes_constants import display_hermes_home
  11. from agent.skill_utils import is_excluded_skill_path
  12. from datetime import UTC
  13. def _dotenv_key_names() -> set[str]:
  14. """Env-var names assigned a non-empty value in ~/.hermes/.env — what the managed backends (launchd /
  15. systemd / desktop ``serve``) load, as opposed to the shell exports ``os.getenv`` reflects here.
  16. ``hermes debug share`` runs in a terminal, so ``os.getenv`` reflects the shell's environment, which can
  17. include exported keys the managed backend never sees. Comparing against this set lets the dump flag that
  18. mismatch (the exact trap behind #48504-style "no web_search" reports: key exported in the shell, absent
  19. from .env, invisible to the launchd backend).
  20. """
  21. try:
  22. env_path = get_env_path()
  23. text = env_path.read_text(encoding="utf-8-sig", errors="ignore")
  24. except (OSError, UnicodeError):
  25. return set()
  26. names: set[str] = set()
  27. for raw in text.splitlines():
  28. line = raw.strip()
  29. if not line or line.startswith("#") or "=" not in line:
  30. continue
  31. if line.lower().startswith("export "):
  32. line = line[len("export "):].lstrip()
  33. name, _, value = line.partition("=")
  34. if name.strip() and value.strip().strip("'\""): # a bare `KEY=` is effectively unset for the backend
  35. names.add(name.strip())
  36. return names
  37. def _git_output(project_root: Path, *args: str) -> str:
  38. """Stripped stdout of ``git <args>`` run in *project_root*, or '' on any failure."""
  39. try:
  40. result = subprocess.run(["git", *args], capture_output=True, text=True, encoding='utf-8', errors='replace',
  41. timeout=5, cwd=str(project_root), check=False)
  42. return result.stdout.strip() if result.returncode == 0 else ""
  43. except Exception:
  44. return ""
  45. def _get_git_commit(project_root: Path) -> str:
  46. """Short git commit hash, or '(unknown)'.
  47. The requested *project_root* is authoritative: when it has no git,
  48. the install-stamp fallback is only valid for the RUNNING install
  49. (``hermes_cli.version_info`` has no per-root API — it reads
  50. HERMES_INSTALL_ROOT / the running code root, never an arbitrary
  51. directory). For any other root the provenance is unknown.
  52. """
  53. value = _git_output(project_root, "rev-parse", "--short=8", "HEAD")
  54. if value:
  55. return value
  56. if project_root.resolve() != get_project_root():
  57. return "(unknown)"
  58. try:
  59. from hermes_cli.version_info import get_code_identity # deferred: keeps dump cheap on non-dump paths
  60. return get_code_identity().get("short_sha") or "(unknown)"
  61. except Exception:
  62. return "(unknown)"
  63. def _get_git_commit_date(project_root: Path) -> str:
  64. """Return the date the HEAD commit was authored (YYYY-MM-DD), or ''.
  65. Same authority rule as :func:`_get_git_commit`: the stamp fallback
  66. describes the running install only.
  67. """
  68. value = _git_output(project_root, "log", "-1", "--format=%cd", "--date=short", "HEAD")
  69. if value:
  70. return value
  71. if project_root.resolve() != get_project_root():
  72. return ""
  73. try:
  74. from datetime import datetime, timezone
  75. from hermes_cli.version_info import get_version_info # deferred: keeps dump cheap on non-dump paths
  76. commit_date = get_version_info().commit_date
  77. return datetime.fromtimestamp(commit_date, tz=UTC).strftime("%Y-%m-%d") if commit_date else ""
  78. except Exception:
  79. return ""
  80. def _redact(value: str) -> str:
  81. """Redact all but first 4 and last 4 chars; ``""`` for an empty value (dump formats empties as blank)."""
  82. from agent.redact import mask_secret
  83. return mask_secret(value)
  84. def _gateway_status() -> str:
  85. """Return a short gateway status string."""
  86. try:
  87. from hermes_cli.gateway import get_gateway_runtime_snapshot
  88. snapshot = get_gateway_runtime_snapshot()
  89. if snapshot.running:
  90. mode = "manual" if snapshot.has_process_service_mismatch else snapshot.manager
  91. # A supervised gateway may have no scannable PID (s6 `python -c` launcher, #125390).
  92. pid = f", pid {snapshot.gateway_pids[0]}" if snapshot.gateway_pids else ""
  93. return f"running ({mode}{pid})"
  94. return f"stopped ({snapshot.manager})"
  95. except Exception:
  96. return "unknown" if sys.platform.startswith(("linux", "darwin")) else "N/A"
  97. def _count_skills(hermes_home: Path) -> int:
  98. """Count installed skills."""
  99. skills_dir = hermes_home / "skills"
  100. return sum(1 for item in skills_dir.rglob("SKILL.md") if not is_excluded_skill_path(item)) if skills_dir.is_dir() else 0
  101. def _cron_summary(hermes_home: Path) -> str:
  102. """Return cron jobs summary."""
  103. jobs_file = hermes_home / "cron" / "jobs.json"
  104. if not jobs_file.exists():
  105. return "0"
  106. try:
  107. # utf-8-sig: same dialect as cron/jobs.load_jobs — Windows editors may leave a UTF-8 BOM.
  108. with open(jobs_file, encoding="utf-8-sig") as f:
  109. jobs = json.load(f).get("jobs", [])
  110. active = sum(1 for j in jobs if j.get("enabled", True))
  111. return f"{active} active / {len(jobs)} total"
  112. except Exception:
  113. return "(error reading)"
  114. _PLATFORM_ENV_VARS = {
  115. "telegram": "TELEGRAM_BOT_TOKEN", "discord": "DISCORD_BOT_TOKEN", "slack": "SLACK_BOT_TOKEN",
  116. "whatsapp": "WHATSAPP_ENABLED", "signal": "SIGNAL_HTTP_URL", "email": "EMAIL_ADDRESS",
  117. "sms": "TWILIO_ACCOUNT_SID", "matrix": "MATRIX_HOMESERVER_URL", "mattermost": "MATTERMOST_URL",
  118. "dingtalk": "DINGTALK_CLIENT_ID", "feishu": "FEISHU_APP_ID",
  119. "wecom": "WECOM_BOT_ID", "wecom_callback": "WECOM_CALLBACK_CORP_ID", "weixin": "WEIXIN_ACCOUNT_ID",
  120. "qqbot": "QQ_APP_ID",
  121. }
  122. def _get_model_and_provider(config: dict) -> tuple[str, str]:
  123. """Extract model and provider from config."""
  124. model_cfg = config.get("model", "")
  125. if isinstance(model_cfg, dict):
  126. return (model_cfg.get("default") or model_cfg.get("model") or model_cfg.get("name") or "(not set)", model_cfg.get("provider") or "(auto)")
  127. return (model_cfg if isinstance(model_cfg, str) else "") or "(not set)", "(auto)"
  128. # Sections with interesting user-facing overrides
  129. _INTERESTING_PATHS = (
  130. ("agent", "max_turns"), ("agent", "gateway_timeout"), ("agent", "session_stall_timeout"),
  131. ("agent", "sanitizer_heal_escalation_threshold"), ("agent", "tool_use_enforcement"),
  132. ("agent", "execution_guidance"), ("terminal", "backend"), ("terminal", "docker_image"),
  133. ("terminal", "persistent_shell"), ("browser", "allow_private_urls"), ("compression", "enabled"),
  134. ("compression", "threshold"), ("compression", "in_place"), ("display", "streaming"),
  135. ("display", "skin"), ("display", "show_reasoning"), ("privacy", "redact_pii"), ("tts", "provider"),
  136. )
  137. def _config_overrides(config: dict) -> dict[str, str]:
  138. """Find non-default config values worth reporting."""
  139. from hermes_cli.config import DEFAULT_CONFIG
  140. overrides = {}
  141. for section, key in _INTERESTING_PATHS:
  142. default_section = DEFAULT_CONFIG.get(section, {})
  143. user_section = config.get(section, {})
  144. if not isinstance(default_section, dict) or not isinstance(user_section, dict):
  145. continue
  146. user_val = user_section.get(key)
  147. if user_val is not None and user_val != default_section.get(key):
  148. overrides[f"{section}.{key}"] = str(user_val)
  149. user_toolsets = config.get("toolsets", [])
  150. if user_toolsets != DEFAULT_CONFIG.get("toolsets", []):
  151. overrides["toolsets"] = str(user_toolsets)
  152. fallbacks = config.get("fallback_providers", [])
  153. if fallbacks:
  154. from hermes_cli.debug_redaction import redact_debug_support_text
  155. overrides["fallback_providers"] = redact_debug_support_text(
  156. str(_fallback_debug_projection(fallbacks))
  157. )
  158. return overrides
  159. _FALLBACK_DEBUG_FIELDS = (
  160. "provider",
  161. "model",
  162. "base_url",
  163. "api_mode",
  164. "key_env",
  165. "api_key_env",
  166. "max_tokens",
  167. )
  168. def _fallback_debug_projection(value) -> list[dict]:
  169. """Render only route metadata useful to support; omit arbitrary containers.
  170. Fallback entries are open mappings consumed by provider-specific code, so a
  171. denylist cannot make their ``headers``, ``env``, command/argv, or future
  172. fields safe to serialize. Credential pointer names remain useful and carry
  173. no values; an inline key is represented only as a source status.
  174. """
  175. entries = [value] if isinstance(value, dict) else value if isinstance(value, list) else []
  176. projected: list[dict] = []
  177. for entry in entries:
  178. if not isinstance(entry, dict):
  179. continue
  180. item: dict = {}
  181. for key in _FALLBACK_DEBUG_FIELDS:
  182. child = entry.get(key)
  183. if key == "max_tokens":
  184. if isinstance(child, (int, float)) and not isinstance(child, bool):
  185. item[key] = child
  186. elif isinstance(child, str):
  187. item[key] = child
  188. if entry.get("api_key"):
  189. item["credential_source"] = "inline"
  190. projected.append(item)
  191. return projected
  192. # (env var, dump label) in display order.
  193. _API_KEYS = [
  194. ("OPENROUTER_API_KEY", "openrouter"), ("OPENAI_API_KEY", "openai"),
  195. ("ANTHROPIC_API_KEY", "anthropic"), ("ANTHROPIC_TOKEN", "anthropic_token"),
  196. ("NOUS_API_KEY", "nous"), ("GOOGLE_API_KEY", "google/gemini"), ("GEMINI_API_KEY", "gemini"),
  197. ("GLM_API_KEY", "glm/zai"), ("ZAI_API_KEY", "zai"), ("KIMI_API_KEY", "kimi"),
  198. ("MINIMAX_API_KEY", "minimax"), ("DEEPSEEK_API_KEY", "deepseek"),
  199. ("DASHSCOPE_API_KEY", "dashscope"), ("HF_TOKEN", "huggingface"), ("NVIDIA_API_KEY", "nvidia"),
  200. ("AI_GATEWAY_API_KEY", "ai_gateway"), ("OPENCODE_ZEN_API_KEY", "opencode_zen"),
  201. ("OPENCODE_GO_API_KEY", "opencode_go"), ("COMMANDCODE_API_KEY", "commandcode"),
  202. ("KILOCODE_API_KEY", "kilocode"), ("FIRECRAWL_API_KEY", "firecrawl"), ("TAVILY_API_KEY", "tavily"), ("PERPLEXITY_API_KEY", "perplexity"),
  203. ("KEENABLE_API_KEY", "keenable"), ("BROWSERBASE_API_KEY", "browserbase"), ("FAL_KEY", "fal"),
  204. ("ELEVENLABS_API_KEY", "elevenlabs"), ("GITHUB_TOKEN", "github"),
  205. ]
  206. def _version_line(project_root: Path) -> str:
  207. """``<version> [<commit>] (<commit date>)`` using the running code identity."""
  208. try:
  209. from hermes_cli.version_info import get_version_info
  210. version = get_version_info().derived_version
  211. except Exception:
  212. version = "(unknown)"
  213. ver_str = f"{version} [{_get_git_commit(project_root)}]"
  214. commit_date = _get_git_commit_date(project_root)
  215. return f"{ver_str} ({commit_date})" if commit_date else ver_str
  216. def _effective_terminal_backend(config: dict) -> str:
  217. """The EFFECTIVE backend: a TERMINAL_ENV set directly in .env / the shell overrides ``terminal.backend`` and
  218. is what terminal_tool uses. run_dump() has already loaded .env, so os.environ reflects the override."""
  219. config_backend = config.get("terminal", {}).get("backend", "local")
  220. env_backend = (os.environ.get("TERMINAL_ENV") or "").strip().lower()
  221. if env_backend and env_backend != str(config_backend).strip().lower():
  222. return f"{env_backend} (TERMINAL_ENV overrides config.yaml terminal.backend={config_backend})"
  223. return config_backend
  224. def _api_key_lines(show_keys: bool) -> list[str]:
  225. dotenv_keys = _dotenv_key_names()
  226. lines = []
  227. for env_var, label in _API_KEYS:
  228. val = os.getenv(env_var, "")
  229. display = _redact(val) if show_keys and val else ("set" if val else "not set")
  230. # Set in this shell but absent from ~/.hermes/.env: a managed backend loads .env, not the login
  231. # shell, so it likely can't see this key — flag it so support doesn't chase a phantom "configured".
  232. if val and env_var not in dotenv_keys:
  233. display += " (shell only — not in .env; managed/desktop backend may not see it)"
  234. # `hermes auth add openrouter` credentials live in the pool, not env — don't read "not set".
  235. # A credential added via `hermes auth add openrouter` lives in the credential pool, not as an env
  236. # var — surface it so the dump doesn't misleadingly read "not set" while `hermes auth list` shows it
  237. # (#42130).
  238. if not val and label == "openrouter":
  239. try:
  240. from agent.credential_pool import load_pool as _load_pool
  241. if _load_pool("openrouter").has_credentials():
  242. display = "set (auth pool)"
  243. except Exception:
  244. pass
  245. lines.append(f" {label:<20} {display}")
  246. return lines
  247. def _openai_version() -> str:
  248. try:
  249. import openai
  250. return openai.__version__
  251. except ImportError:
  252. return "not installed"
  253. def run_dump(args):
  254. """Output a compact, copy-pasteable setup summary."""
  255. show_keys = getattr(args, "show_keys", False)
  256. # Load env from .env file so key checks work
  257. load_hermes_dotenv(hermes_home=get_env_path().parent, project_env=get_project_root() / ".env")
  258. project_root, hermes_home = get_project_root(), get_hermes_home()
  259. try:
  260. config = load_config()
  261. except Exception:
  262. config = {}
  263. model, provider = _get_model_and_provider(config)
  264. try:
  265. from hermes_cli.profiles import get_active_profile_name
  266. profile = get_active_profile_name() or "(default)"
  267. except Exception:
  268. profile = "(default)"
  269. toolsets = config.get("toolsets", ["hermes-cli"])
  270. platforms = [name for name, env in _PLATFORM_ENV_VARS.items() if os.getenv(env)]
  271. lines = [
  272. "--- hermes dump ---",
  273. f"version: {_version_line(project_root)}",
  274. f"os: {platform.system()} {platform.release()} {platform.machine()}",
  275. f"python: {sys.version.split()[0]}",
  276. f"openai_sdk: {_openai_version()}",
  277. f"profile: {profile}",
  278. f"hermes_home: {display_hermes_home()}",
  279. f"model: {model}",
  280. f"provider: {provider}",
  281. f"terminal: {_effective_terminal_backend(config)}",
  282. "", "api_keys:", *_api_key_lines(show_keys),
  283. "", "features:",
  284. f" toolsets: {', '.join(toolsets) if toolsets else '(default)'}",
  285. f" mcp_servers: {len(config.get('mcp', {}).get('servers', {}))}",
  286. f" memory_provider: {config.get('memory', {}).get('provider', '') or 'built-in'}",
  287. f" gateway: {_gateway_status()}",
  288. f" platforms: {', '.join(platforms) if platforms else 'none'}",
  289. f" cron_jobs: {_cron_summary(hermes_home)}",
  290. f" skills: {_count_skills(hermes_home)}",
  291. ]
  292. overrides = _config_overrides(config)
  293. if overrides:
  294. lines += ["", "config_overrides:"] + [f" {key}: {val}" for key, val in overrides.items()]
  295. print("\n".join(lines + ["--- end dump ---"]))