auxiliary_call_outcome.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """How one auxiliary logical call ended, for its Relay ``hermes.logical_llm_call`` scope.
  2. The scope's ``outcome`` + ``error_class`` become the shared-metrics ``model_route`` row of an
  3. auxiliary call, under the same contract as a primary call: a failure carries the classifier's
  4. ``FailoverReason`` for the error that ended it, a Hermes-side abort (``/stop``, Ctrl+C, an
  5. interrupt, shutdown) is ``cancelled`` rather than ``failed``, and a success keeps the last
  6. attempt error it recovered from (a retried timeout, a fallback past a 429).
  7. """
  8. from __future__ import annotations
  9. import logging
  10. from typing import Any
  11. logger = logging.getLogger(__name__)
  12. # Attribute an aux "invalid response" error carries when the HTTP-200 body held a provider
  13. # ``error`` object instead of choices (aggregators relay upstream failures this way).
  14. PROVIDER_ERROR_ATTR = "provider_error"
  15. class _EmbeddedProviderError(Exception):
  16. """An HTTP-200 ``error`` object reshaped so ``classify_api_error`` reads it as an SDK body."""
  17. def __init__(self, error: dict[str, Any]) -> None:
  18. super().__init__(str(error.get("message") or ""))
  19. self.body = {"error": error}
  20. def embedded_provider_error(response: Any) -> dict[str, Any] | None:
  21. """The ``error`` object of an HTTP-200 body that carried no choices, else None."""
  22. error = response.get("error") if isinstance(response, dict) else getattr(response, "error", None)
  23. if isinstance(error, dict):
  24. return error or None
  25. if error is None or not (hasattr(error, "message") or hasattr(error, "code")):
  26. return None
  27. fields = {"message": getattr(error, "message", None), "code": getattr(error, "code", None)}
  28. return {k: v for k, v in fields.items() if v is not None} or None
  29. def is_cancellation(error: BaseException) -> bool:
  30. """Hermes aborted the call (explicit cancel, interrupt, Ctrl+C, task cancel, shutdown):
  31. everything that is not an ``Exception``, plus the ``InterruptedError`` aux streams raise."""
  32. return not isinstance(error, Exception) or isinstance(error, InterruptedError)
  33. def error_class(error: BaseException, *, provider: str = "", model: str = "") -> str:
  34. """The classifier's reason for one aux attempt error; ``unknown`` when it cannot say.
  35. Never raises: telemetry must not replace the caller's exception."""
  36. if not isinstance(error, Exception):
  37. return "unknown"
  38. try:
  39. from agent.error_classifier import classify_api_error
  40. embedded = getattr(error, PROVIDER_ERROR_ATTR, None)
  41. target = _EmbeddedProviderError(embedded) if isinstance(embedded, dict) else error
  42. return classify_api_error(target, provider=provider, model=model).reason.value
  43. except Exception:
  44. logger.debug("Auxiliary error classification failed", exc_info=True)
  45. return "unknown"