Skip to content

The agent that grows with you

refactor(hermes_cli): debug._capture_dump via contextlib; container_boot argv probe as one loop

Tekniuma4daa0b78041
diff --git a/hermes_cli/container_boot.py b/hermes_cli/container_boot.py
index 9348aae6434..038ff5bf888 100644
--- a/hermes_cli/container_boot.py
+++ b/hermes_cli/container_boot.py
@@ -179,26 +179,22 @@ def _read_container_argv() -> tuple[str, ...]:
``s6-svscan`` and the real command lives on another PID, so after the PID 1 fast path we
scan ``/proc/*/cmdline`` for a process whose argv contains ``main-wrapper.sh``.
"""
- try:
- argv = _cmdline_argv(Path("/proc/1/cmdline"))
+ def _cmdlines():
+ yield Path("/proc/1/cmdline")
+ try:
+ for entry in Path("/proc").iterdir():
+ if entry.name.isdigit():
+ yield entry / "cmdline"
+ except OSError:
+ return
+
+ for cmdline in _cmdlines():
+ try:
+ argv = _cmdline_argv(cmdline)
+ except OSError:
+ continue
if any("main-wrapper.sh" in part for part in argv):
return argv
- except OSError:
- pass
-
- try:
- for entry in Path("/proc").iterdir():
- if not entry.name.isdigit():
- continue
- try:
- argv = _cmdline_argv(entry / "cmdline")
- except OSError:
- continue
- if any("main-wrapper.sh" in part for part in argv):
- return argv
- except OSError:
- pass
-
return ()
diff --git a/hermes_cli/debug.py b/hermes_cli/debug.py
index 64447bfbcb1..420eb225dcb 100644
--- a/hermes_cli/debug.py
+++ b/hermes_cli/debug.py
@@ -1,5 +1,6 @@
"""``hermes debug`` debug tools for Hermes Agent."""
+import contextlib
import datetime
import gzip
import io
@@ -11,6 +12,7 @@ import time
import urllib.request
from dataclasses import dataclass
from pathlib import Path
+from types import SimpleNamespace
from typing import Optional
from hermes_constants import get_hermes_home
@@ -396,19 +398,9 @@ def _capture_default_log_snapshots(
def _capture_dump() -> str:
"""Run ``hermes dump`` and return its stdout as a string."""
from hermes_cli.dump import run_dump
-
- class _FakeArgs:
- show_keys = False
-
- old_stdout = sys.stdout
- sys.stdout = capture = io.StringIO()
- try:
- run_dump(_FakeArgs())
- except SystemExit:
- pass
- finally:
- sys.stdout = old_stdout
-
+ capture = io.StringIO()
+ with contextlib.redirect_stdout(capture), contextlib.suppress(SystemExit):
+ run_dump(SimpleNamespace(show_keys=False))
return capture.getvalue()
@@ -448,9 +440,8 @@ def collect_debug_report(
buf.write("\n")
for name in _REPORT_LOGS:
- buf.write(f"\n--- {name}.log (last {_tail_budget(name, log_lines)} lines) ---\n")
- buf.write(log_snapshots[name].tail_text)
- buf.write("\n")
+ buf.write(f"\n--- {name}.log (last {_tail_budget(name, log_lines)} lines) ---\n"
+ f"{log_snapshots[name].tail_text}\n")
return buf.getvalue()