Files
hermes-agent/cron/scheduler_diagnostics.py
T
teknium1 e094e25b26 fix(cron): a restart-safe worker that dies before its ack reports its own stderr
`_launch_external_cron_worker` spawned the worker with `stderr=DEVNULL`, so a worker
that crashed before publishing its acknowledgement — an import error, a module missing
from the interpreter it was handed, a bad payload — surfaced only as
"exited before ownership acknowledgement (exit 1)". #112729 shows the cost: the
reporter, a claimant and a reviewer spent three rounds guessing at the mechanism
(sys.executable symlinks — refuted) because the worker's actual traceback was thrown
away. The worker's own log handler is installed only after `cron.scheduler` imports,
so an import-time failure never reaches agent.log either.

Capture stderr into `<home>/cron/external-workers/<execution_id>.stderr` (0o600), and
on a pre-ack exit append a redacted tail of it to the dispatch error (which lands in
`last_error`, the executions ledger and the gateway log). The file rides in
`handoff_files` so every exit path removes it; the tail helper lives in
`scheduler_diagnostics` next to the run-error redaction.

Part of #112729
2026-09-16 17:52:40 -07:00

39 lines
1.5 KiB
Python

"""Private run-document diagnostics, kept separate from delivery summaries."""
from pathlib import Path
from traceback import format_exception
from agent.redact import redact_sensitive_text
# Enough for a Python traceback's last frames plus the exception line.
_WORKER_STDERR_TAIL_CHARS = 1500
def format_run_error(exc: BaseException) -> str:
"""Retain chained causes without capturing locals or exposing URL credentials."""
traceback_text = redact_sensitive_text(
"".join(format_exception(exc)), force=True, redact_url_credentials=True,
)
return f"## Error\n\n```\n{traceback_text}\n```\n"
def external_worker_stderr_tail(path: Path) -> str:
"""Redacted tail of a restart-safe cron worker's captured stderr, as a message suffix.
The gateway used to spawn the worker with ``stderr=DEVNULL``, so a worker that died before
publishing its acknowledgement (an import error, a missing module in the interpreter it was
handed) only ever reported ``exit 1`` and the reporter had to guess the cause (#112729).
Returns an empty string when nothing was captured.
"""
try:
text = path.read_text(encoding="utf-8", errors="replace").strip()
except OSError:
return ""
if not text:
return ""
tail = text[-_WORKER_STDERR_TAIL_CHARS:]
if len(text) > _WORKER_STDERR_TAIL_CHARS:
tail = "…" + tail
tail = redact_sensitive_text(tail, force=True, redact_url_credentials=True)
return f"; worker stderr: {tail}"