Skip to content

The agent that grows with you

fix(logging): line-buffer a piped stdout so headless log streams track the agent loop

liuhao102494c843462a7a
diff --git a/hermes_logging.py b/hermes_logging.py
index a5e8bf8a874..06912bacbaf 100644
--- a/hermes_logging.py
+++ b/hermes_logging.py
@@ -48,6 +48,30 @@ _LOG_FORMAT = "%(asctime)s %(levelname)s%(session_tag)s %(name)s: %(message)s"
_LOG_FORMAT_VERBOSE = "%(asctime)s - %(name)s - %(levelname)s%(session_tag)s - %(message)s"
+def _line_buffer_piped_stdout() -> None:
+ """Best-effort line buffering for a piped stdout (#92281).
+
+ Python block-buffers stdout when it isn't a TTY, so an agent loop
+ driving ``print()`` into a supervisor's pipe delivers its output in
+ large delayed bursts — a headless run looks stalled for minutes while
+ the work is actually progressing (only stderr, which we already give
+ ``line_buffering=True`` in ``_safe_stderr()``, shows up on time).
+ Reconfigure the interpreter's own stdout for line buffering when it is
+ piped; interactive TTY stdout is already line-buffered, and the ACP
+ entry point keeps stdout as a protocol channel it manages itself and
+ never calls ``setup_logging``.
+ """
+ try:
+ stream = sys.stdout
+ if stream is None or stream.isatty():
+ return
+ reconfigure = getattr(stream, "reconfigure", None)
+ if reconfigure is not None:
+ reconfigure(line_buffering=True)
+ except Exception:
+ pass # buffering is observability, not correctness — never crash
+
+
def _safe_stderr(): # type: ignore[return]
"""Return a stderr stream that tolerates Unicode on all platforms.
@@ -218,6 +242,13 @@ def setup_logging(
global _logging_initialized
home = hermes_home or get_hermes_home()
log_dir = mkdir_under_hermes_home(home / "logs")
+
+ # Stdout is block-buffered when piped (no TTY); line-buffer it so a
+ # headless supervisor's log stream tracks the agent loop incrementally
+ # (#92281). Runs before the initialized check so every entry mode that
+ # reaches this function gets it, idempotent by construction.
+ _line_buffer_piped_stdout()
+
# A second Hermes home in a process that already logs for another one — a dashboard or
# ``hermes serve`` backend building agents for several profiles, a multiplexed gateway —
# gets routed by record home. Stacking another file handler here would hand it EVERY
diff --git a/tests/test_hermes_logging.py b/tests/test_hermes_logging.py
index 333662d7980..bb4881ac5cb 100644
--- a/tests/test_hermes_logging.py
+++ b/tests/test_hermes_logging.py
@@ -769,3 +769,47 @@ class TestSafeStderr:
+class TestLineBufferPipedStdout:
+ """A piped stdout is line-buffered so headless log streams track the
+ agent loop incrementally (#92281); a TTY stdout is left alone."""
+
+ def _fake_stdout(self, isatty: bool):
+ from types import SimpleNamespace
+ from unittest.mock import MagicMock
+
+ stream = MagicMock()
+ stream.isatty.return_value = isatty
+ stream.reconfigure = MagicMock()
+ return stream
+
+ def test_piped_stdout_reconfigured_line_buffered(self, monkeypatch):
+ stream = self._fake_stdout(isatty=False)
+ monkeypatch.setattr(sys, "stdout", stream)
+ hermes_logging._line_buffer_piped_stdout()
+ stream.reconfigure.assert_called_once_with(line_buffering=True)
+
+ def test_tty_stdout_untouched(self, monkeypatch):
+ stream = self._fake_stdout(isatty=True)
+ monkeypatch.setattr(sys, "stdout", stream)
+ hermes_logging._line_buffer_piped_stdout()
+ stream.reconfigure.assert_not_called()
+
+ def test_none_or_reconfigure_less_stdout_is_noop(self, monkeypatch):
+ monkeypatch.setattr(sys, "stdout", None)
+ hermes_logging._line_buffer_piped_stdout() # must not raise
+
+ from types import SimpleNamespace
+
+ # A stream without reconfigure() (e.g. a print-redirect shim).
+ monkeypatch.setattr(
+ sys, "stdout", SimpleNamespace(isatty=lambda: False)
+ )
+ hermes_logging._line_buffer_piped_stdout()
+
+ def test_setup_logging_applies_it_to_piped_stdout(self, tmp_path, monkeypatch):
+ stream = self._fake_stdout(isatty=False)
+ monkeypatch.setattr(sys, "stdout", stream)
+ hermes_logging.setup_logging(hermes_home=tmp_path, force=True)
+ stream.reconfigure.assert_called_once_with(line_buffering=True)
+
+