scheduler_diagnostics.py 1.5 KB

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