Skip to content

The agent that grows with you

fix(cron): a failed bot-chat delivery records both streams, labeled

Halldrix6e89b8f36cf6
diff --git a/cron/scheduler_delivery.py b/cron/scheduler_delivery.py
index f843ee40dbf..dc58dbc4f65 100644
--- a/cron/scheduler_delivery.py
+++ b/cron/scheduler_delivery.py
@@ -653,6 +653,27 @@ def _get_bot_chat_delivery_timeout() -> int:
return 600
+_BOT_CHAT_STREAM_TAIL = 500
+
+
+def _format_failure_streams(result) -> str:
+ """Labeled stderr+stdout tails for a failed delivery turn ("": none).
+
+ ``-Q`` reports the resume banner and ``session_id:`` on stderr while the
+ response rides stdout, so ``stderr or stdout`` discards half the signal —
+ and when stderr is empty and stdout holds only the banner, the recorded
+ error carries zero diagnostics (#104056). Both tails are kept, capped.
+ """
+ err = (getattr(result, "stderr", None) or "").strip()
+ out = (getattr(result, "stdout", None) or "").strip()
+ parts = []
+ if err:
+ parts.append(f"stderr: {err[-_BOT_CHAT_STREAM_TAIL:]}")
+ if out:
+ parts.append(f"stdout: {out[-_BOT_CHAT_STREAM_TAIL:]}")
+ return " | ".join(parts)
+
+
def _deliver_to_bot_chat(job: dict, content: str, profile: str, *, deferred: Optional[dict] = None) -> Optional[str]:
"""Hand output to the live Bot Chat owner, or use the legacy unowned CLI lane.
@@ -784,14 +805,14 @@ def _deliver_to_bot_chat(job: dict, content: str, profile: str, *, deferred: Opt
argv, capture_output=True, text=True, timeout=_get_bot_chat_delivery_timeout(), env=env,
creationflags=windows_hide_flags())
if result.returncode != 0:
- tail = (result.stderr or result.stdout or "").strip()[-500:]
+ tail = _format_failure_streams(result)
logger.warning(
"Job '%s': bot-chat delivery to profile '%s' failed (exit %s) at %s%s",
job_id, profile_label, result.returncode, home, f": {tail}" if tail else "")
return (
f"Hermes could not deliver this result to Bot Chat (profile '{profile_label}'). "
"The result is saved; run `hermes cron runs` to see it, or `hermes doctor` if this keeps happening"
- + (f". Details: {tail[-200:]}" if tail else ""))
+ + (f". Details: {tail}" if tail else ""))
logger.info("Job '%s': delivered to Bot Chat of profile '%s'", job_id, profile_label)
return None
except subprocess.TimeoutExpired:
diff --git a/tests/cron/test_cron_bot_chat_delivery.py b/tests/cron/test_cron_bot_chat_delivery.py
index 7d0ab443011..4ae3f676cc5 100644
--- a/tests/cron/test_cron_bot_chat_delivery.py
+++ b/tests/cron/test_cron_bot_chat_delivery.py
@@ -115,8 +115,8 @@ def test_create_validation_accepts_bare_and_existing():
# ── delivery lane ────────────────────────────────────────────────────────────
-def _completed(returncode=0, stderr=""):
- return subprocess.CompletedProcess(args=[], returncode=returncode, stdout="", stderr=stderr)
+def _completed(returncode=0, stdout="", stderr=""):
+ return subprocess.CompletedProcess(args=[], returncode=returncode, stdout=stdout, stderr=stderr)
def test_deliver_runs_canonical_bot_chat_lane():
@@ -156,6 +156,33 @@ def test_deliver_failure_returns_error_string():
assert "boom" in err
+def test_deliver_failure_reports_both_streams_labeled():
+ """A failed turn must keep stderr AND stdout, labeled — ``stderr or
+ stdout`` discarded half the signal (#104056)."""
+ with mock.patch.object(
+ sched.subprocess, "run",
+ return_value=_completed(returncode=1, stdout="banner out", stderr="boom-err"),
+ ), mock.patch.object(sched_delivery.shutil, "which", return_value="/usr/bin/hermes"):
+ err = _deliver_to_bot_chat({"id": "j1", "name": "n"}, "out", "")
+ assert err is not None
+ assert "stderr: boom-err" in err
+ assert "stdout: banner out" in err
+
+
+def test_deliver_failure_stdout_only_when_stderr_empty():
+ """The reported shape: empty stderr, stdout holding only the resume
+ banner — the recorded error must still carry it (#104056)."""
+ banner = 'Resumed session 20260905_121420_8084c7 "Bot Chat" (1 user message, 1 total messages)'
+ with mock.patch.object(
+ sched.subprocess, "run",
+ return_value=_completed(returncode=1, stdout=banner, stderr=""),
+ ), mock.patch.object(sched_delivery.shutil, "which", return_value="/usr/bin/hermes"):
+ err = _deliver_to_bot_chat({"id": "j1", "name": "n"}, "out", "")
+ assert err is not None
+ assert "stdout: " + banner in err
+ assert "stderr:" not in err
+
+
def test_deliver_timeout_returns_error_string():
with mock.patch.object(
sched.subprocess, "run",