turn_retry_state.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Per-attempt recovery bookkeeping (``TurnRetryState``) for the conversation turn loop.
  2. Dependency-free so it imports without a cycle."""
  3. from __future__ import annotations
  4. from dataclasses import dataclass
  5. @dataclass
  6. class TurnRetryState:
  7. """One-shot recovery guards + restart signals for a single API-call attempt.
  8. A fresh instance is created per ``api_call_count`` iteration; each guard fires at
  9. most once, and ``restart_with_*`` signals tell the loop to rebuild and retry.
  10. Loop-control (``retry_count``, ``max_retries``) stays as plain loop locals."""
  11. # Per-provider OAuth / credential refresh guards
  12. codex_auth_retry_attempted: bool = False
  13. anthropic_auth_retry_attempted: bool = False
  14. nous_auth_retry_attempted: bool = False
  15. nous_paid_entitlement_refresh_attempted: bool = False
  16. # Nous free tier: one model move onto the tier's own model after a ``model_not_free``
  17. # refusal, and one route re-read after a wrong-host refusal (``anon_on_paid_host``).
  18. welcome_model_switch_attempted: bool = False
  19. welcome_route_heal_attempted: bool = False
  20. # Provider-profile runtime refresh (``refresh_runtime_credentials``): one 401 refresh, and a
  21. # separate one for a non-auth error the profile reads as a stale credential.
  22. provider_auth_refresh_attempted: bool = False
  23. provider_stale_refresh_attempted: bool = False
  24. vertex_auth_retry_attempted: bool = False
  25. # Format / payload recovery guards
  26. thinking_sig_retry_attempted: bool = False
  27. invalid_encrypted_content_retry_attempted: bool = False
  28. native_compaction_reject_retry_attempted: bool = False
  29. image_shrink_retry_attempted: bool = False
  30. multimodal_tool_content_retry_attempted: bool = False
  31. reasoning_mandatory_retry_attempted: bool = False
  32. oauth_1m_beta_retry_attempted: bool = False
  33. llama_cpp_grammar_retry_attempted: bool = False
  34. # Transport / rate-limit recovery
  35. primary_recovery_attempted: bool = False
  36. has_retried_429: bool = False
  37. # Credit-limited 402 ("can only afford N tokens") already retried with a lowered output cap.
  38. affordable_402_clamp_attempted: bool = False
  39. # Persistent 401/403 already escalated to the fallback chain once this attempt.
  40. auth_failover_attempted: bool = False
  41. # Post-exhaustion auto-recovery cycles spent on this API call (agent.auto_recovery_cycles caps it).
  42. auto_recovery_cycles_used: int = 0
  43. # Restart signals (read by the outer loop after the attempt)
  44. restart_with_compressed_messages: bool = False
  45. restart_with_length_continuation: bool = False
  46. # A fallback activation (incl. content-filter stream stalls) rolled partial content
  47. # off ``messages``; re-issue the call against the new provider.
  48. restart_with_rebuilt_messages: bool = False
  49. # A user correction cancelled the in-flight request: append a role-safe checkpoint +
  50. # user message, rebuild the payload, and retry the same logical iteration.
  51. restart_with_redirected_messages: bool = False