agent_init_fallback.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Init-time fallback chain: entry normalization, chain setup, and the fallback's api_mode."""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Any, Dict, List
  5. logger = logging.getLogger("run_agent")
  6. def _fallback_entries(fallback_model) -> list[dict[str, Any]]:
  7. """Normalize legacy single-dict ``fallback_model`` / list ``fallback_providers``."""
  8. if isinstance(fallback_model, dict):
  9. fallback_model = [fallback_model]
  10. if not isinstance(fallback_model, list):
  11. return []
  12. return [
  13. f for f in fallback_model if isinstance(f, dict) and f.get("provider") and f.get("model")
  14. ]
  15. def _init_fallback_chain(agent, fallback_model):
  16. # Stable pool-entry identity: OAuth refreshes can replace the token before a failed
  17. # request is recovered, so the key value alone can't attribute the failure.
  18. from agent.agent_runtime_helpers import sync_credential_pool_entry_id
  19. sync_credential_pool_entry_id(agent)
  20. # Ordered backups tried when the primary is exhausted (legacy single-dict or list).
  21. agent._fallback_chain = _fallback_entries(fallback_model)
  22. agent._fallback_index = 0
  23. agent._fallback_activated = getattr(agent, "_fallback_activated", False)
  24. # Legacy attribute kept for backward compat (tests, external callers)
  25. agent._fallback_model = agent._fallback_chain[0] if agent._fallback_chain else None
  26. chain = agent._fallback_chain
  27. if chain and not agent.quiet_mode:
  28. labels = [f"{f['model']} ({f['provider']})" for f in chain]
  29. if len(chain) == 1:
  30. print(f"🔄 Fallback model: {labels[0]}")
  31. else:
  32. print(f"🔄 Fallback chain ({len(chain)} providers): " + " → ".join(labels))
  33. def recompute_init_fallback_api_mode(agent, fb_client) -> None:
  34. """Give an init-time fallback its own api_mode instead of the unreachable primary's.
  35. A Nous primary configured with ``api_mode: codex_responses`` otherwise leaves a fallback whose
  36. profile serves that model on chat completions (``api_mode_for_model``) on the Responses API,
  37. which silently drops reasoning content (#46527).
  38. The per-turn path (``try_activate_fallback``) already recomputes via
  39. ``_fallback_api_mode_resolved``; this keeps init in agreement, and runs before the
  40. ``_primary_runtime`` snapshot so ``restore_primary_runtime`` replays the recomputed mode.
  41. A detection failure keeps the primary's mode and never blocks the fallback itself.
  42. """
  43. from agent.chat_completion_helpers import _fallback_api_mode_resolved
  44. try:
  45. agent.api_mode = _fallback_api_mode_resolved(
  46. agent, agent.provider, agent.model, str(getattr(fb_client, "base_url", "") or ""))
  47. except Exception:
  48. logger.debug("Init-time fallback api_mode detection failed for %s", agent.provider, exc_info=True)
  49. return
  50. if hasattr(agent, "_transport_cache"):
  51. agent._transport_cache.clear()