auxiliary_fallback_recovery.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Parameter-rejection recovery for auxiliary fallback candidates.
  2. The primary auxiliary request runs the full recovery ladder in ``auxiliary_client``; a fallback
  3. candidate (per-task chain, main fallback chain, discovery) used to get a single shot, so a fallback
  4. model that rejects ``temperature``, ``max_tokens`` or a reasoning field failed the whole task even
  5. though the same parameter rungs would have recovered it on the primary path (#78273, #72351).
  6. This module runs those rungs — and only those — around a candidate's request.
  7. """
  8. from typing import Any, Awaitable, Callable, Dict, Optional
  9. def _parameter_ladder(first_err: Exception, client: Any, kwargs: dict[str, Any], *,
  10. task: Optional[str], tag: str):
  11. from agent.auxiliary_client import _LadderRoute, _ladder_parameter_rungs
  12. # Keyword construction: the route tuple grows with every new ladder rung (a positional 13-tuple
  13. # broke the moment a sibling PR added ``timeout``); fields this ladder never reads stay None.
  14. route = _LadderRoute(**{**dict.fromkeys(_LadderRoute._fields), "client": client, "task": task,
  15. "tag": tag, "async_mode": bool(tag), "resolved_provider": "",
  16. "base_info": str(getattr(client, "base_url", "") or "")})
  17. max_tokens = kwargs.get("max_tokens") or kwargs.get("max_completion_tokens")
  18. resp, err, _ = yield from _ladder_parameter_rungs(first_err, route, kwargs, max_tokens)
  19. if err is None:
  20. return resp
  21. raise err
  22. def send_with_parameter_rungs(
  23. send: Callable[[Any, dict[str, Any]], Any], client: Any, kwargs: dict[str, Any], *, task: Optional[str],
  24. ) -> Any:
  25. """``send(client, kwargs)``; on a parameter 400, retry through the parameter rungs. Any other
  26. error (auth, payment, connection) propagates unchanged for the caller's own handling."""
  27. from agent.auxiliary_client import _drive_ladder
  28. try:
  29. return send(client, kwargs)
  30. except Exception as first_err:
  31. ladder = _parameter_ladder(first_err, client, kwargs, task=task, tag="")
  32. return _drive_ladder(ladder, lambda step: send(*step.args))
  33. async def send_with_parameter_rungs_async(
  34. send: Callable[[Any, dict[str, Any]], Awaitable[Any]], client: Any, kwargs: dict[str, Any], *,
  35. task: Optional[str],
  36. ) -> Any:
  37. """Async twin of :func:`send_with_parameter_rungs`."""
  38. from agent.auxiliary_client import _drive_ladder_async
  39. try:
  40. return await send(client, kwargs)
  41. except Exception as first_err:
  42. ladder = _parameter_ladder(first_err, client, kwargs, task=task, tag=" (async)")
  43. return await _drive_ladder_async(ladder, lambda step: send(*step.args))