debug.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. """``hermes debug`` debug tools for Hermes Agent."""
  2. import contextlib
  3. import datetime
  4. import gzip
  5. import io
  6. import json
  7. import logging
  8. import sys
  9. import threading
  10. import time
  11. import urllib.request
  12. from dataclasses import dataclass
  13. from pathlib import Path
  14. from types import SimpleNamespace
  15. from typing import Optional
  16. from hermes_constants import get_hermes_home
  17. from hermes_cli.debug_redaction import redact_debug_support_text
  18. from utils import atomic_json_write
  19. logger = logging.getLogger(__name__)
  20. # Prepended to upload-bound content when redaction is enabled so paste reviewers know.
  21. _REDACTION_BANNER = (
  22. "[hermes debug share: log content redacted at upload time. "
  23. "run with --no-redact to disable]\n")
  24. _PASTE_RS_URL = "https://paste.rs/" # primary; dpaste.com is the fallback
  25. _DPASTE_COM_URL = "https://dpaste.com/api/"
  26. _USER_AGENT = "hermes-agent/debug-share"
  27. _MAX_LOG_BYTES = 512_000 # per log file for upload (paste.rs caps at ~1 MB)
  28. _AUTO_DELETE_SECONDS = 21600 # 6 hours
  29. # Pending-deletion tracking: the gateway cron ticker calls ``_sweep_expired_pastes`` hourly and
  30. # ``hermes debug`` sweeps on entry (CLI-only users). Replaced a fork-and-sleep subprocess that
  31. # leaked ~20 MB per share.
  32. _PENDING_LOCK_HOLDER = threading.local()
  33. def _pending_file() -> Path:
  34. return get_hermes_home() / "pastes" / "pending.json"
  35. def _load_pending() -> list[dict]:
  36. try:
  37. data = json.loads(_pending_file().read_text(encoding="utf-8-sig"))
  38. except (OSError, ValueError):
  39. return []
  40. if not isinstance(data, list):
  41. return []
  42. return [e for e in data if isinstance(e, dict) and {"url", "expire_at"} <= e.keys()]
  43. def _save_pending(entries: list[dict]) -> None:
  44. try:
  45. atomic_json_write(_pending_file(), entries)
  46. except OSError:
  47. pass # non-fatal — worst case the user runs ``hermes debug delete`` manually
  48. @contextlib.contextmanager
  49. def _pending_lock():
  50. """Cross-process lock for pending.json read-modify-write (gateway sweep vs CLI/gateway share)."""
  51. from hermes_cli.auth import _file_lock
  52. path = _pending_file()
  53. with _file_lock(path.with_name(path.name + ".lock"), _PENDING_LOCK_HOLDER, 10.0,
  54. f"Timed out waiting for the pending paste list lock ({path})"):
  55. yield
  56. def _sweep_expired_pastes(now: Optional[float] = None) -> tuple[int, int]:
  57. """Synchronously DELETE pending pastes whose ``expire_at`` has passed → (deleted, remaining).
  58. Best-effort and silent: failed deletes stay pending for the next sweep, up to 24h past
  59. expiration (paste.rs GCs them eventually).
  60. """
  61. entries = _load_pending()
  62. if not entries:
  63. return (0, 0)
  64. current = time.time() if now is None else now
  65. reaped: set[str] = set()
  66. for entry in entries:
  67. try:
  68. expire_at = float(entry.get("expire_at", 0))
  69. except (TypeError, ValueError):
  70. continue # malformed entries are dropped below
  71. if expire_at > current:
  72. continue
  73. try:
  74. gone = delete_paste(entry.get("url", ""))
  75. except Exception:
  76. gone = False # network hiccup, 404 (already gone), ...
  77. if gone or expire_at + 86400 <= current:
  78. reaped.add(entry.get("url", "")) # deleted, or given up on → count as reaped
  79. # DELETEs can take minutes; a share may have scheduled new pastes meanwhile, so
  80. # re-read under the lock and drop only what this sweep reaped (and malformed rows).
  81. with _pending_lock():
  82. latest = _load_pending()
  83. remaining = [e for e in latest if _keep_after_sweep(e, reaped, current)]
  84. if len(remaining) != len(latest):
  85. _save_pending(remaining)
  86. return (len(reaped), len(remaining))
  87. def _keep_after_sweep(entry: dict, reaped: set[str], current: float) -> bool:
  88. try:
  89. expire_at = float(entry.get("expire_at", 0))
  90. except (TypeError, ValueError):
  91. return False
  92. # A URL re-scheduled during the sweep has a future expiry and must stay.
  93. return not (entry.get("url", "") in reaped and expire_at <= current)
  94. def _best_effort_sweep_expired_pastes() -> None:
  95. """Pending-paste cleanup that never lets /debug fail offline."""
  96. with contextlib.suppress(Exception):
  97. _sweep_expired_pastes()
  98. _PRIVACY_NOTICE = """\
  99. ⚠️ This will upload system info + logs to a PUBLIC paste service.
  100. Cryptographic secrets (API keys, tokens, passwords) are redacted before
  101. upload, but the following personal data is NOT redacted and will be public:
  102. • Your display name and persistent platform user ID
  103. • Verbatim content of your recent messages (prompts, responses, tool output)
  104. • Local filesystem paths
  105. • Any other PII present in the logs
  106. The resulting URL is public to anyone who has the link. Pastes auto-delete
  107. after 6 hours, but may be archived by third parties in the meantime.
  108. If paste.rs is unreachable, uploads fall back to dpaste.com: those pastes
  109. stay public for the --expire window (default: 1 day) and CANNOT be deleted
  110. with `hermes debug delete`.
  111. Use --local to view the report without uploading.
  112. """
  113. _GATEWAY_PRIVACY_NOTICE = (
  114. "⚠️ **Privacy notice:** This uploads system info + recent log tails "
  115. "(may contain conversation fragments) to a public paste service. "
  116. "Full logs are NOT included from the gateway — use `hermes debug share` "
  117. "from the CLI for full log uploads.\n"
  118. "Pastes auto-delete after 6 hours (dpaste.com fallback pastes: kept for "
  119. "1 day, cannot be deleted).")
  120. def _extract_paste_id(url: str) -> Optional[str]:
  121. """Paste ID from a paste.rs URL (dpaste.com pastes have no deletable ID)."""
  122. url = url.strip().rstrip("/")
  123. for prefix in ("https://paste.rs/", "http://paste.rs/"):
  124. if url.startswith(prefix):
  125. return url[len(prefix):]
  126. return None
  127. def _is_dpaste_url(url: str) -> bool:
  128. """Whether *url* is a dpaste.com paste (no unauthenticated delete exists for those)."""
  129. return url.strip().startswith(("https://dpaste.com/", "http://dpaste.com/"))
  130. def delete_paste(url: str) -> bool:
  131. """Delete a paste.rs paste (the only service with unauthenticated DELETE). True on success."""
  132. paste_id = _extract_paste_id(url)
  133. if not paste_id:
  134. if _is_dpaste_url(url):
  135. raise ValueError(
  136. "Cannot delete: dpaste.com pastes cannot be deleted (anonymous "
  137. "uploads have no owner token); they expire on their own. Got: " + url)
  138. raise ValueError(f"Cannot delete: only paste.rs URLs are supported. Got: {url}")
  139. req = urllib.request.Request(f"{_PASTE_RS_URL}{paste_id}", method="DELETE",
  140. headers={"User-Agent": _USER_AGENT})
  141. with urllib.request.urlopen(req, timeout=30) as resp:
  142. return 200 <= resp.status < 300
  143. def _schedule_auto_delete(urls: list[str], delay_seconds: int = _AUTO_DELETE_SECONDS):
  144. """Record paste.rs *urls* for deletion ``delay_seconds`` from now (merged into pending.json)."""
  145. paste_rs_urls = [u for u in urls if _extract_paste_id(u)]
  146. if not paste_rs_urls:
  147. return
  148. try:
  149. with _pending_lock():
  150. # Dedupe by URL, keeping the later expire_at.
  151. by_url: dict[str, float] = {e["url"]: float(e["expire_at"]) for e in _load_pending()}
  152. expire_at = time.time() + delay_seconds
  153. for u in paste_rs_urls:
  154. by_url[u] = max(expire_at, by_url.get(u, 0.0))
  155. _save_pending([{"url": u, "expire_at": ts} for u, ts in by_url.items()])
  156. except (OSError, TimeoutError):
  157. # The pastes are already uploaded; a lock failure must not lose the share result.
  158. logger.debug("could not schedule auto-delete for %s", paste_rs_urls, exc_info=True)
  159. def _post_paste(service: str, endpoint: str, body: bytes, content_type: str) -> str:
  160. """POST *body* to a paste service and return the paste URL it echoes back."""
  161. req = urllib.request.Request(endpoint, data=body, method="POST",
  162. headers={"Content-Type": content_type, "User-Agent": _USER_AGENT})
  163. with urllib.request.urlopen(req, timeout=30) as resp:
  164. url = resp.read().decode("utf-8").strip()
  165. if not url.startswith("http"):
  166. safe_response = redact_debug_support_text(url, max_chars=200)
  167. raise ValueError(f"Unexpected response from {service}: {safe_response}")
  168. return url
  169. def _upload_paste_rs(content: str) -> str:
  170. return _post_paste("paste.rs", _PASTE_RS_URL, content.encode("utf-8"), "text/plain; charset=utf-8")
  171. def _upload_dpaste_com(content: str, expiry_days: int = 1) -> str:
  172. boundary = "----HermesDebugBoundary9f3c"
  173. fields = (("content", content), ("syntax", "text"), ("expiry_days", str(expiry_days)))
  174. body = ("".join(f'--{boundary}\r\nContent-Disposition: form-data; name="{n}"\r\n\r\n{v}\r\n'
  175. for n, v in fields) + f"--{boundary}--\r\n").encode("utf-8")
  176. return _post_paste("dpaste.com", _DPASTE_COM_URL, body,
  177. f"multipart/form-data; boundary={boundary}")
  178. def upload_to_pastebin(content: str, expiry_days: int = 1) -> str:
  179. """Upload *content* to a paste service, trying paste.rs then dpaste.com.
  180. ``expiry_days`` applies only to the dpaste.com fallback (paste.rs pastes are
  181. swept after 6h); dpaste.com's minimum is 1 day and anonymous pastes cannot
  182. be deleted afterwards, so callers must not promise a shorter retention.
  183. """
  184. errors: list[str] = []
  185. for service, upload in (
  186. ("paste.rs", lambda: _upload_paste_rs(content)),
  187. ("dpaste.com", lambda: _upload_dpaste_com(content, expiry_days=expiry_days))):
  188. try:
  189. return upload()
  190. except Exception as exc:
  191. errors.append(f"{service}: {exc}") # every caller scrubs the RuntimeError
  192. raise RuntimeError("Failed to upload to any paste service:\n " + "\n ".join(errors))
  193. @dataclass
  194. class LogSnapshot:
  195. """Single-read snapshot of a log file used by debug-share."""
  196. path: Optional[Path]
  197. tail_text: str
  198. full_text: Optional[str]
  199. def _primary_log_path(log_name: str) -> Optional[Path]:
  200. """Where *log_name* would live if present. Doesn't check existence."""
  201. from hermes_cli.logs import log_file_path
  202. return log_file_path(log_name) # update/handoff: the ROOT home's, whatever profile is active
  203. # Logs written by a client process, invisible to a remote/docker/SSH backend running `debug
  204. # share`; a bare "(file not found)" would read as "the app logged nothing" and misdirect triage.
  205. _CLIENT_SIDE_LOGS = {
  206. "desktop": (
  207. "written by Hermes Desktop on the machine running the app, not by this "
  208. "backend. If the desktop connects to a remote/docker/SSH backend, collect "
  209. "it on that client machine")}
  210. def _missing_log_note(log_name: str) -> str:
  211. """Explain a missing log instead of stating a bare absence."""
  212. reason = _CLIENT_SIDE_LOGS.get(log_name)
  213. if reason is None:
  214. return "(file not found)"
  215. primary = _primary_log_path(log_name)
  216. return f"(not on this host: {reason}{f' — expected at {primary}' if primary else ''})"
  217. def _resolve_log_path(log_name: str) -> Optional[Path]:
  218. """First non-empty candidate for *log_name* (primary, then the .1 rotation), or None."""
  219. primary = _primary_log_path(log_name)
  220. if primary is None:
  221. return None
  222. for candidate in (primary, primary.parent / f"{primary.name}.1"):
  223. if candidate.exists() and candidate.stat().st_size > 0:
  224. return candidate
  225. return None
  226. def _read_tail_bytes(
  227. log_path: Path, size: int, max_bytes: int, tail_lines: int
  228. ) -> tuple[bytes, int, bool]:
  229. """Return a bounded tail, its file offset, and whether it starts on a line boundary."""
  230. with open(log_path, "rb") as f:
  231. if size <= max_bytes:
  232. return f.read(), 0, True
  233. chunk_size = 8192
  234. pos = size
  235. chunks: list[bytes] = []
  236. total = 0
  237. newline_count = 0
  238. while (pos > 0 and total < max_bytes * 2
  239. and (total < max_bytes or newline_count <= tail_lines + 1)):
  240. read_size = min(chunk_size, pos)
  241. pos -= read_size
  242. f.seek(pos)
  243. chunk = f.read(read_size)
  244. chunks.insert(0, chunk)
  245. total += len(chunk)
  246. newline_count += chunk.count(b"\n")
  247. chunk_size = min(chunk_size * 2, 65536)
  248. starts_on_boundary = True
  249. if pos > 0:
  250. f.seek(pos - 1)
  251. starts_on_boundary = f.read(1) == b"\n"
  252. return b"".join(chunks), pos, starts_on_boundary
  253. def _truncate_complete_lines(text: str, max_bytes: int) -> tuple[str, bool]:
  254. """Keep at most the last ``max_bytes`` without publishing a partial first line."""
  255. raw = text.encode("utf-8")
  256. if len(raw) <= max_bytes:
  257. return text, False
  258. cut = len(raw) - max_bytes
  259. on_boundary = raw[cut - 1:cut] == b"\n"
  260. kept = raw[cut:]
  261. if not on_boundary:
  262. kept = kept.split(b"\n", 1)[1] if b"\n" in kept else b""
  263. return kept.decode("utf-8", errors="replace"), True
  264. def _capture_log_snapshot(
  265. log_name: str, *, tail_lines: int, max_bytes: int = _MAX_LOG_BYTES, redact: bool = True,
  266. ) -> LogSnapshot:
  267. """Capture a log once and derive the summary tail and full-log views from that single read
  268. (a rotation between two reads would make the report look newer than the uploaded log)."""
  269. log_path = _resolve_log_path(log_name)
  270. if log_path is None:
  271. primary = _primary_log_path(log_name)
  272. tail = "(file empty)" if primary and primary.exists() else _missing_log_note(log_name)
  273. return LogSnapshot(path=None, tail_text=tail, full_text=None)
  274. try:
  275. size = log_path.stat().st_size
  276. if size == 0: # truncated between _resolve_log_path and stat
  277. return LogSnapshot(path=log_path, tail_text="(file empty)", full_text=None)
  278. raw, start_offset, starts_on_boundary = _read_tail_bytes(
  279. log_path, size, max_bytes, tail_lines
  280. )
  281. all_text = raw.decode("utf-8", errors="replace")
  282. if redact:
  283. # The support scrub needs the complete logical field before either
  284. # the summary-line cap or full-log byte cap can remove its key.
  285. all_text = redact_debug_support_text(all_text)
  286. if start_offset and not starts_on_boundary:
  287. all_text = all_text.split("\n", 1)[1] if "\n" in all_text else ""
  288. tail_text = "".join(all_text.splitlines(keepends=True)[-tail_lines:]).rstrip("\n")
  289. full_text, capped = _truncate_complete_lines(all_text, max_bytes)
  290. truncated = capped or size > max_bytes
  291. if truncated:
  292. full_text = f"[... truncated — showing last ~{max_bytes // 1024}KB ...]\n{full_text}"
  293. return LogSnapshot(path=log_path, tail_text=tail_text, full_text=full_text)
  294. except Exception as exc:
  295. error = redact_debug_support_text(exc)
  296. return LogSnapshot(path=log_path, tail_text=f"(error reading: {error})", full_text=None)
  297. # Logs the debug report tails, in output order. ``agent`` gets the full ``--lines`` budget;
  298. # the rest are capped at 100 lines. Every log but ``errors`` is also uploaded in full.
  299. _REPORT_LOGS = ("agent", "errors", "gateway", "gui", "desktop", "update", "handoff")
  300. _FULL_LOGS = ("agent", "gateway", "gui", "desktop", "update", "handoff")
  301. def _tail_budget(name: str, log_lines: int) -> int:
  302. return log_lines if name == "agent" else min(log_lines, 100)
  303. def _capture_default_log_snapshots(
  304. log_lines: int, *, redact: bool = True) -> dict[str, LogSnapshot]:
  305. """Capture all logs used by debug-share exactly once."""
  306. return {
  307. name: _capture_log_snapshot(name, tail_lines=_tail_budget(name, log_lines), redact=redact)
  308. for name in _REPORT_LOGS}
  309. def _capture_dump(redact: bool = True) -> str:
  310. """Run ``hermes dump`` and return its stdout, force-redacted unless *redact* is False: the dump
  311. is upload-bound and quotes config values (e.g. ``fallback_providers``), so URL credentials are
  312. redacted too, as in the uploaded logs."""
  313. from hermes_cli.dump import run_dump
  314. capture = io.StringIO()
  315. with contextlib.redirect_stdout(capture), contextlib.suppress(SystemExit):
  316. run_dump(SimpleNamespace(show_keys=False))
  317. text = capture.getvalue()
  318. return redact_debug_support_text(text) if redact else text
  319. def collect_debug_report(
  320. *, log_lines: int = 200, dump_text: str = "",
  321. log_snapshots: Optional[dict[str, LogSnapshot]] = None) -> str:
  322. """Build the summary debug report (system dump + log tails) as upload-ready text.
  323. ``dump_text`` is pre-captured dump output; when empty, ``hermes dump`` is run internally.
  324. """
  325. buf = io.StringIO()
  326. buf.write(dump_text or _capture_dump())
  327. if log_snapshots is None:
  328. log_snapshots = _capture_default_log_snapshots(log_lines)
  329. # In-process sanitiser heal counters: populated only inside a process that ran agent turns
  330. # (gateway /debug share); a fresh CLI's errors.log tail carries the same escalation lines.
  331. with contextlib.suppress(Exception):
  332. # See #96870.
  333. from agent.agent_runtime_helpers import get_sanitizer_heal_stats
  334. heal_stats = get_sanitizer_heal_stats()
  335. if heal_stats:
  336. buf.write("\n\n--- transcript sanitiser heal counters ---\n")
  337. for sess, st in sorted(heal_stats.items()):
  338. buf.write(f"session {sess}: {st['heal_events']} heal events, "
  339. f"{st['messages_healed']} messages healed, escalated={st['escalated']}\n")
  340. buf.write("\n")
  341. from hermes_cli.logs import LOG_FILES
  342. for name in _REPORT_LOGS:
  343. snap = log_snapshots.get(name)
  344. if snap is None:
  345. continue
  346. filename = LOG_FILES.get(name, f"{name}.log")
  347. buf.write(f"\n--- {filename} (last {_tail_budget(name, log_lines)} lines) ---\n"
  348. f"{snap.tail_text}\n")
  349. return buf.getvalue()
  350. # Nous-S3 envelope format id; the discord-support viewer keys off it.
  351. _NOUS_BUNDLE_FORMAT = "hermes-debug-share/1"
  352. def collect_share_bundle(log_lines: int = 200, redact: bool = True) -> dict[str, str]:
  353. """Collect the debug report + full logs as a label→text mapping.
  354. The dump header is prepended to each full log so every file is self-contained, and the
  355. redaction banner is prepended when ``redact`` is True.
  356. """
  357. dump_text = _capture_dump(redact=redact)
  358. log_snapshots = _capture_default_log_snapshots(log_lines, redact=redact)
  359. report = collect_debug_report(log_lines=log_lines, dump_text=dump_text,
  360. log_snapshots=log_snapshots)
  361. banner = _REDACTION_BANNER if redact else ""
  362. bundle: dict[str, str] = {"report": banner + report}
  363. from hermes_cli.logs import LOG_FILES
  364. for name in _FULL_LOGS:
  365. if full := log_snapshots[name].full_text:
  366. filename = LOG_FILES.get(name, f"{name}.log")
  367. bundle[filename] = banner + dump_text + f"\n\n--- full {filename} ---\n" + full
  368. return bundle
  369. def build_nous_bundle(bundle: dict[str, str], redact: bool = True) -> bytes:
  370. """Gzip a :func:`collect_share_bundle` mapping into the Nous envelope (shape parsed by the
  371. discord-support viewer — keep it stable)."""
  372. envelope = {"format": _NOUS_BUNDLE_FORMAT, "redacted": bool(redact),
  373. "created": datetime.datetime.now(datetime.UTC).isoformat(),
  374. "files": bundle}
  375. return gzip.compress(json.dumps(envelope).encode("utf-8"))
  376. @dataclass
  377. class DebugShareResult:
  378. """Outcome of a ``debug share`` upload, so non-CLI callers can render real links."""
  379. urls: dict # label -> paste URL (e.g. {"Report": "...", "agent.log": "..."})
  380. failures: list # human-readable "label: error" strings for optional uploads
  381. redacted: bool # whether force-mode redaction was applied before upload
  382. auto_delete_seconds: int # how long until the pastes auto-delete (paste.rs only;
  383. # dpaste.com fallbacks outlive this and cannot be deleted)
  384. report: str = "" # the summary report text (kept for local fallback)
  385. def build_debug_share(
  386. *, log_lines: int = 200, expiry: int = 1, redact: bool = True) -> DebugShareResult:
  387. """Collect the debug report + full logs, upload each, return the URLs.
  388. Shared by ``hermes debug share`` and the dashboard ``POST /api/ops/debug-share``. Blocking
  389. network I/O — callers inside an event loop must run it in a worker thread.
  390. """
  391. _best_effort_sweep_expired_pastes()
  392. bundle = collect_share_bundle(log_lines=log_lines, redact=redact)
  393. if redact:
  394. logger.info(
  395. "hermes debug share: applied force-mode redaction to log snapshots before upload")
  396. report = bundle["report"]
  397. failures: list[str] = []
  398. # The summary report is required (raises so callers can fall back); full logs are optional.
  399. urls = {"Report": upload_to_pastebin(report, expiry_days=expiry)}
  400. # Full logs to upload (the source of this list is checked by tests to ensure
  401. # new logs aren't silently skipped).
  402. for label in (
  403. "agent.log",
  404. "gateway.log",
  405. "gui.log",
  406. "desktop.log",
  407. # Update-failure diagnostics: the only files holding the root cause
  408. # of a failed update/Desktop rebuild (#100874).
  409. "update.log",
  410. "desktop-update-handoff.log",
  411. ):
  412. content = bundle.get(label)
  413. if not content:
  414. continue
  415. try:
  416. urls[label] = upload_to_pastebin(content, expiry_days=expiry)
  417. except Exception as exc:
  418. failures.append(f"{label}: {redact_debug_support_text(exc)}")
  419. _schedule_auto_delete(list(urls.values()))
  420. return DebugShareResult(urls=urls, failures=failures, redacted=redact,
  421. auto_delete_seconds=_AUTO_DELETE_SECONDS, report=report)
  422. def _confirm_upload(args) -> bool:
  423. """Gate the actual upload: ``--yes`` proceeds unprompted, else ask an interactive [y/N]."""
  424. if bool(getattr(args, "yes", False)):
  425. return True
  426. if not sys.stdin.isatty():
  427. print("ERROR: Non-interactive mode requires --yes to confirm upload.\n"
  428. " This prevents accidental exposure of personal data.\n"
  429. " Use --local to view the report without uploading.", file=sys.stderr)
  430. sys.exit(1)
  431. try:
  432. answer = input("Upload debug report? [y/N] ").strip().lower()
  433. except (EOFError, KeyboardInterrupt):
  434. answer = ""
  435. if answer in ("y", "yes"):
  436. return True
  437. print("Aborted.")
  438. return False
  439. def run_debug_share(args):
  440. """Collect debug report + full logs, upload each, print URLs."""
  441. log_lines = getattr(args, "lines", 200)
  442. expiry = getattr(args, "expire", 1)
  443. redact = not getattr(args, "no_redact", False)
  444. if getattr(args, "local", False):
  445. # Same collector as the upload path so the output matches exactly; no network I/O.
  446. _best_effort_sweep_expired_pastes()
  447. print("Collecting debug report...")
  448. bundle = collect_share_bundle(log_lines=log_lines, redact=redact)
  449. print(bundle["report"])
  450. for title, label in (
  451. ("FULL agent.log", "agent.log"),
  452. ("FULL gateway.log", "gateway.log"),
  453. ("FULL gui.log", "gui.log"),
  454. ("FULL desktop.log", "desktop.log"),
  455. ("FULL update.log", "update.log"),
  456. ("FULL desktop-update-handoff.log", "desktop-update-handoff.log"),
  457. ):
  458. body = bundle.get(label)
  459. if body:
  460. print(f"\n\n{'=' * 60}")
  461. print(title)
  462. print(f"{'=' * 60}\n")
  463. print(body)
  464. return
  465. if getattr(args, "nous", False):
  466. return _run_debug_share_nous(args, log_lines=log_lines, redact=redact)
  467. print(_PRIVACY_NOTICE)
  468. if not _confirm_upload(args):
  469. return
  470. print("Collecting debug report...\nUploading...")
  471. try:
  472. result = build_debug_share(log_lines=log_lines, expiry=expiry, redact=redact)
  473. except RuntimeError as exc:
  474. print(f"\nUpload failed: {redact_debug_support_text(exc)}", file=sys.stderr)
  475. print("\nRun `hermes debug share --local` to print the report instead.\n")
  476. sys.exit(1)
  477. label_width = max(len(k) for k in result.urls)
  478. print("\nDebug report uploaded:")
  479. for label, url in result.urls.items():
  480. print(f" {label:<{label_width}} {url}")
  481. if result.failures:
  482. print(f"\n (failed to upload: {', '.join(result.failures)})")
  483. dpaste_urls = [u for u in result.urls.values() if _is_dpaste_url(u)]
  484. if dpaste_urls:
  485. print(f"\n⏱ paste.rs pastes will auto-delete in "
  486. f"{result.auto_delete_seconds // 3600} hours.")
  487. print(f"⚠️ {len(dpaste_urls)} of {len(result.urls)} upload(s) fell back to "
  488. f"dpaste.com: those pastes stay public for {expiry} day(s) and CANNOT be "
  489. "deleted with `hermes debug delete`.\n"
  490. "\nShare these links with the Hermes team for support.")
  491. else:
  492. print(f"\n⏱ Pastes will auto-delete in {result.auto_delete_seconds // 3600} hours.\n"
  493. "To delete now: hermes debug delete <url>\n"
  494. "\nShare these links with the Hermes team for support.")
  495. _NOUS_PRIVACY_NOTICE = """\
  496. ⚠️ --nous: This uploads your debug bundle to Nous-INTERNAL storage (AWS S3),
  497. NOT a public paste service. The following is included:
  498. • System info (OS, Python/Hermes version, provider, which API keys are
  499. configured — NOT the actual keys)
  500. • Full agent.log, gateway.log, and desktop.log (up to 512 KB each — likely
  501. contains conversation content, tool outputs, and file paths), plus
  502. update.log and desktop-update-handoff.log when present (update/hand-off
  503. output — the root cause of update failures)
  504. • The bundle is viewable only by Nous staff (and allowlisted Discord mods)
  505. via a Google-login-gated viewer.
  506. • It is NOT a public paste — there is no public URL to the contents.
  507. • It auto-deletes after 14 days.
  508. """
  509. def _run_debug_share_nous(args, *, log_lines: int, redact: bool) -> None:
  510. """``hermes debug share --nous``: gzip the same bundle into the Nous envelope → Nous-S3."""
  511. from hermes_cli.diagnostics_upload import share_to_nous
  512. print(_NOUS_PRIVACY_NOTICE)
  513. if not _confirm_upload(args):
  514. return
  515. if not redact:
  516. print("⚠️ --no-redact is set: secrets in your logs will NOT be redacted before upload.\n")
  517. print("Collecting debug report...")
  518. _best_effort_sweep_expired_pastes()
  519. bundle = collect_share_bundle(log_lines=log_lines, redact=redact)
  520. if redact:
  521. logger.info("hermes debug share --nous: applied force-mode redaction before upload")
  522. print("Uploading to Nous diagnostics storage...")
  523. try:
  524. res = share_to_nous(build_nous_bundle(bundle, redact=redact))
  525. except Exception as exc:
  526. from pm.network import edge_block
  527. error = redact_debug_support_text(exc)
  528. cause = edge_block(exc) or "The Nous diagnostics service may be unavailable or not yet provisioned"
  529. print(f"\nNous upload failed: {error}\n\n{cause}.\n"
  530. "Run `hermes debug share --local` to print the report instead, "
  531. "or `hermes debug share` to upload to a public paste service.\n", file=sys.stderr)
  532. sys.exit(1)
  533. view_url = res.get("viewUrl") or res.get("view_url")
  534. expires_at = res.get("expiresAt") or res.get("expires_at")
  535. print("\nDebug bundle uploaded to Nous (private):")
  536. print(f" View URL {view_url}" if view_url
  537. else f" (no view URL returned; upload id: {res.get('id', '?')})")
  538. print(f"\n⏱ Auto-deletes at {expires_at} (14-day retention)." if expires_at
  539. else "\n⏱ Auto-deletes after 14 days.")
  540. print("\nShare this private link with the Nous team — only Nous staff "
  541. "(via Google login) can open it.\n"
  542. "\nPick up the discussion in:\n"
  543. " GitHub Issues https://github.com/NousResearch/hermes-agent/issues\n"
  544. " Nous Portal Support https://portal.nousresearch.com/help\n"
  545. " Discord https://discord.gg/NousResearch")
  546. def run_debug_delete(args):
  547. """Delete one or more paste URLs uploaded by /debug."""
  548. urls = getattr(args, "urls", [])
  549. if not urls:
  550. print("Usage: hermes debug delete <url> [<url> ...]\n"
  551. " Deletes paste.rs pastes uploaded by 'hermes debug share'.")
  552. return
  553. for url in urls:
  554. try:
  555. if delete_paste(url):
  556. print(f" ✓ Deleted: {url}")
  557. else:
  558. print(
  559. " ✗ Failed to delete: "
  560. f"{redact_debug_support_text(url)} (unexpected response)"
  561. )
  562. except ValueError as exc:
  563. print(f" ✗ {redact_debug_support_text(exc)}")
  564. except Exception as exc:
  565. safe_url = redact_debug_support_text(url)
  566. print(f" ✗ Could not delete {safe_url}: {redact_debug_support_text(exc)}")
  567. def run_debug(args):
  568. """Route debug subcommands (sweeping expired pastes opportunistically on every call)."""
  569. _best_effort_sweep_expired_pastes()
  570. handler = {"share": run_debug_share, "delete": run_debug_delete}.get(
  571. getattr(args, "debug_command", None))
  572. if handler is None:
  573. print(_DEBUG_USAGE)
  574. else:
  575. handler(args)
  576. _DEBUG_USAGE = """\
  577. Usage: hermes debug <command>
  578. Commands:
  579. share Upload debug report to a paste service and print URL
  580. delete Delete a previously uploaded paste
  581. Options (share):
  582. --lines N Number of log lines to include (default: 200)
  583. --expire N dpaste.com fallback retention in days (default: 1)
  584. --local Print report locally instead of uploading
  585. --nous Upload to Nous-internal storage (private, staff-only,
  586. auto-deletes in 14 days) instead of a public paste
  587. --no-redact Disable upload-time secret redaction (default: redact)
  588. Options (delete):
  589. <url> ... One or more paste URLs to delete"""