turn_api_error.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. """API-call exception handler for the conversation turn's retry loop: pre-/post-classification
  2. recovery, interpreter-shutdown abandon, classified-error routing, overflow recovery, the
  3. non-retryable client-error exit, max-retries exhaustion (primary transport recovery →
  4. fallback → terminal result) and the interruptible backoff. Nothing here imports
  5. ``agent.conversation_loop`` at module level (cycle) — loop-internal helpers resolve lazily so
  6. ``patch("agent.conversation_loop.X")`` sites keep intercepting.
  7. """
  8. from __future__ import annotations
  9. from dataclasses import dataclass
  10. import json
  11. import logging
  12. import ssl
  13. import time
  14. from typing import Any, Dict, Optional
  15. from agent.api_error_summary import is_provider_stream_parse_error
  16. from agent.error_classifier import RETRYABLE_CLIENT_REASONS, FailoverReason, classify_api_error
  17. from agent.turn_overflow import recover_from_overflow
  18. from agent.turn_recovery import (
  19. _NONRETRYABLE_LABELS, abort_turn_on_interrupt, compute_error_backoff, free_tier_cooldown_ends_turn,
  20. interruptible_backoff_sleep, log_api_error_attempt,
  21. max_retries_exhausted_result, nonretryable_client_error_result, recover_after_classification,
  22. recover_before_classification, route_classified_error, settle_delivered_partial,
  23. )
  24. logger = logging.getLogger("agent.conversation_loop")
  25. @dataclass
  26. class ApiErrorVerdict:
  27. """``action``: ``"continue"`` (retry the API call), ``"break"`` (leave the retry loop:
  28. fallback armed / redirect pending) or ``"return"`` (``result`` is the turn's result dict);
  29. ``"fallthrough"`` never happens — the handler always ends in an exit. The other fields
  30. are the retry-loop locals the handler rebinds; ``_provider_overflow_recovery_pending`` is
  31. merge-only (caller sets True when set)."""
  32. action: str
  33. thinking_spinner: Any
  34. messages: Any
  35. active_system_prompt: Any
  36. conversation_history: Any
  37. approx_tokens: Any
  38. retry_count: Any
  39. max_retries: Any
  40. compression_attempts: Any
  41. _provider_overflow_recovery_pending: Any
  42. result: Optional[dict[str, Any]] = None
  43. def handle_api_error(
  44. agent: Any, *, api_error: Any, _retry: Any, thinking_spinner: Any, messages: Any,
  45. api_messages: Any, api_kwargs: Any, system_message: Any, active_system_prompt: Any,
  46. conversation_history: Any, approx_tokens: Any, retry_count: Any, max_retries: Any,
  47. compression_attempts: Any, max_compression_attempts: Any, api_call_count: Any,
  48. api_request_id: Any, api_start_time: Any, effective_task_id: Any, turn_id: Any,
  49. current_turn_user_idx: Any = None,
  50. ) -> ApiErrorVerdict:
  51. """Recover from ``api_error`` in the original order. Every fallback activation must leave
  52. the retry loop with ``restart_with_rebuilt_messages`` armed (``"break"``) so the pre-API
  53. preflight re-runs against the fallback's context window (#84733)."""
  54. _provider_overflow_recovery_pending = False
  55. def _verdict(action: str, result: Optional[dict[str, Any]] = None) -> ApiErrorVerdict:
  56. return ApiErrorVerdict(
  57. action=action, thinking_spinner=thinking_spinner, messages=messages,
  58. active_system_prompt=active_system_prompt, conversation_history=conversation_history,
  59. approx_tokens=approx_tokens, retry_count=retry_count, max_retries=max_retries,
  60. compression_attempts=compression_attempts,
  61. _provider_overflow_recovery_pending=_provider_overflow_recovery_pending, result=result,
  62. )
  63. # Stop spinner silently — retry status is buffered and only flushed when every
  64. # retry+fallback is exhausted.
  65. if thinking_spinner:
  66. thinking_spinner.stop("")
  67. thinking_spinner = None
  68. if agent.thinking_callback:
  69. agent.thinking_callback("")
  70. _recovered, active_system_prompt = recover_before_classification(
  71. agent, api_error, messages=messages, api_messages=api_messages, api_kwargs=api_kwargs,
  72. active_system_prompt=active_system_prompt,
  73. )
  74. if _recovered:
  75. return _verdict("continue")
  76. status_code = getattr(api_error, "status_code", None)
  77. error_context = agent._extract_api_error_context(api_error)
  78. # A Nous 402/403 carries the same x-nous-credits-* headers as a success, and a usage-window plan's
  79. # "ran out for this model" / "payment paused" state is only visible on the blocking response itself
  80. # (the stream path captures headers after the stream opens, which a 402 never does). Fail-open.
  81. # Usage-window responses only: every other account keeps main's behaviour (no capture here).
  82. if status_code in (402, 403) and getattr(agent, "provider", "") == "nous":
  83. try:
  84. from agent.credits_tracker import response_is_usage_window
  85. response = getattr(api_error, "response", None)
  86. if response_is_usage_window(response):
  87. agent._capture_credits(response, refused=True)
  88. except Exception:
  89. logger.debug("credits ▸ header capture on %s failed (fail-open)", status_code, exc_info=True)
  90. # Process is exiting mid-flight: retries/rotation/fallbacks are futile and the
  91. # retry trace spams the shell. One log line.
  92. from tools.interpreter_shutdown import interpreter_shutting_down
  93. if interpreter_shutting_down(api_error):
  94. logger.warning(
  95. "%sInterpreter is shutting down — abandoning turn "
  96. "during API call #%d (%s)",
  97. agent.log_prefix, api_call_count, api_error,
  98. )
  99. _shutdown_summary = "Turn abandoned: the process was shutting down before the model call could complete."
  100. return _verdict("return", {
  101. "final_response": _shutdown_summary, "messages": messages, "api_calls": api_call_count,
  102. "completed": False, "failed": True, "error": _shutdown_summary,
  103. "failure_reason": "interpreter_shutdown", "failure_retryable": False,
  104. })
  105. _compressor = getattr(agent, "context_compressor", None)
  106. _ctx_len = getattr(_compressor, "context_length", 200000) if _compressor else 200000
  107. classified = classify_api_error(
  108. api_error, provider=getattr(agent, "provider", "") or "",
  109. model=getattr(agent, "model", "") or "", approx_tokens=approx_tokens,
  110. context_length=_ctx_len, num_messages=len(api_messages) if api_messages else 0,
  111. base_url=str(getattr(agent, "base_url", "") or ""),
  112. api_key=getattr(agent, "api_key", None),
  113. )
  114. logger.debug(
  115. "Error classified: reason=%s status=%s retryable=%s compress=%s rotate=%s fallback=%s",
  116. classified.reason.value, classified.status_code,
  117. classified.retryable, classified.should_compress,
  118. classified.should_rotate_credential, classified.should_fallback,
  119. )
  120. agent._invoke_api_request_error_hook(
  121. task_id=effective_task_id, turn_id=turn_id, api_request_id=api_request_id,
  122. api_call_count=api_call_count, api_start_time=api_start_time, api_kwargs=api_kwargs,
  123. error_type=type(api_error).__name__, error_message=str(api_error), status_code=status_code,
  124. retry_count=retry_count, max_retries=max_retries, retryable=classified.retryable,
  125. reason=classified.reason.value,
  126. )
  127. _recovered, recovered_with_pool = recover_after_classification(
  128. agent, api_error, classified, _retry, status_code=status_code, error_context=error_context,
  129. messages=messages, api_messages=api_messages,
  130. )
  131. if _recovered:
  132. return _verdict("continue")
  133. retry_count += 1
  134. elapsed_time = time.time() - api_start_time
  135. # Liveness/watchdog label only (never shown in chat), so the classifier's
  136. # "not retryable" verdict is named on the logged attempt line below instead.
  137. agent._touch_activity(f"API error recovery (attempt {retry_count}/{max_retries})")
  138. error_type, error_msg, _provider, _base, _model = log_api_error_attempt(
  139. agent, api_error, retry_count=retry_count, max_retries=max_retries, status_code=status_code,
  140. elapsed_time=elapsed_time, api_messages=api_messages, approx_tokens=approx_tokens,
  141. retryable=bool(classified.retryable),
  142. )
  143. if agent._interrupt_requested:
  144. # Preserve a pending redirect: the user is steering, not stopping — rebuild the
  145. # turn from the correction instead of aborting.
  146. if agent.clear_interrupt(preserve_redirect=True):
  147. _retry.restart_with_redirected_messages = True
  148. return _verdict("break")
  149. return _verdict("return", abort_turn_on_interrupt(
  150. agent, messages, conversation_history, api_call_count,
  151. abort_message="Interrupt detected during error handling, aborting retries.",
  152. interrupt_text=f"Operation interrupted: handling API error ({error_type}: {agent._clean_error_message(str(api_error))}).",
  153. ))
  154. _ce = route_classified_error(
  155. agent, api_error, classified, _retry, error_msg=error_msg, error_context=error_context,
  156. recovered_with_pool=recovered_with_pool, base_url=_base, model=_model, messages=messages,
  157. api_messages=api_messages, system_message=system_message,
  158. active_system_prompt=active_system_prompt, conversation_history=conversation_history,
  159. retry_count=retry_count, max_retries=max_retries, compression_attempts=compression_attempts,
  160. max_compression_attempts=max_compression_attempts, api_call_count=api_call_count,
  161. effective_task_id=effective_task_id,
  162. )
  163. status_code = _ce.status_code
  164. messages = _ce.messages
  165. active_system_prompt = _ce.active_system_prompt
  166. conversation_history = _ce.conversation_history
  167. retry_count = _ce.retry_count
  168. max_retries = _ce.max_retries
  169. compression_attempts = _ce.compression_attempts
  170. is_rate_limited = _ce.is_rate_limited
  171. _wrapped_output_cap_budget = _ce.wrapped_output_cap_budget
  172. _is_zai_coding_overload = _ce.is_zai_coding_overload
  173. if _ce.provider_overflow_recovery_pending:
  174. _provider_overflow_recovery_pending = True
  175. if _ce.action != "fallthrough":
  176. return _verdict(_ce.action, _ce.result)
  177. _ov = recover_from_overflow(
  178. agent, api_error, classified, _retry, status_code=status_code, error_msg=error_msg,
  179. wrapped_output_cap_budget=_wrapped_output_cap_budget, messages=messages,
  180. api_messages=api_messages, system_message=system_message,
  181. active_system_prompt=active_system_prompt, conversation_history=conversation_history,
  182. approx_tokens=approx_tokens, compression_attempts=compression_attempts,
  183. max_compression_attempts=max_compression_attempts, api_call_count=api_call_count,
  184. effective_task_id=effective_task_id,
  185. )
  186. messages = _ov.messages
  187. active_system_prompt = _ov.active_system_prompt
  188. conversation_history = _ov.conversation_history
  189. approx_tokens = _ov.approx_tokens
  190. compression_attempts = _ov.compression_attempts
  191. is_context_length_error = _ov.is_context_length_error
  192. if _ov.provider_overflow_recovery_pending:
  193. _provider_overflow_recovery_pending = True
  194. if _ov.action != "fallthrough":
  195. return _verdict(_ov.action, _ov.result)
  196. _ue = settle_unrecovered_error(
  197. agent, api_error=api_error, classified=classified, _retry=_retry, status_code=status_code,
  198. error_msg=error_msg, error_context=error_context,
  199. is_context_length_error=is_context_length_error,
  200. is_rate_limited=is_rate_limited, _is_zai_coding_overload=_is_zai_coding_overload,
  201. _provider=_provider, _base=_base, _model=_model, messages=messages,
  202. api_messages=api_messages, api_kwargs=api_kwargs, active_system_prompt=active_system_prompt,
  203. conversation_history=conversation_history, approx_tokens=approx_tokens,
  204. retry_count=retry_count, max_retries=max_retries, compression_attempts=compression_attempts,
  205. api_call_count=api_call_count, current_turn_user_idx=current_turn_user_idx,
  206. )
  207. active_system_prompt = _ue.active_system_prompt
  208. retry_count = _ue.retry_count
  209. compression_attempts = _ue.compression_attempts
  210. return _verdict(_ue.action, _ue.result)
  211. def _is_local_validation_error(api_error: Any) -> bool:
  212. """ValueError/TypeError are local bugs, except: UnicodeEncodeError (surrogate recovery
  213. path), json.JSONDecodeError / provider stream-parse ValueErrors (transient provider/network failure, must retry),
  214. ssl.SSLError (inherits OSError *and* ValueError — a TLS failure is not a local bug)
  215. and "NoneType is not iterable" TypeErrors (upstream shape mismatches, e.g. Codex
  216. response.completed.output=null — retryable so the fallback path runs)."""
  217. if not isinstance(api_error, (ValueError, TypeError)):
  218. return False
  219. if isinstance(api_error, (UnicodeEncodeError, json.JSONDecodeError, ssl.SSLError)):
  220. return False
  221. _text = str(api_error).lower()
  222. if is_provider_stream_parse_error(api_error):
  223. return False # corrupted provider SSE chunk (jiter), transient — not a local bug (#65147)
  224. return not (isinstance(api_error, TypeError) and "nonetype" in _text and "not iterable" in _text)
  225. @dataclass
  226. class UnrecoveredErrorVerdict:
  227. """``action``: ``"continue"`` (retry), ``"break"`` (fallback armed / redirect pending) or
  228. ``"return"`` (``result`` is the terminal result dict). Rebinds ``active_system_prompt``,
  229. ``retry_count`` and ``compression_attempts``."""
  230. action: str
  231. active_system_prompt: Any
  232. retry_count: Any
  233. compression_attempts: Any
  234. result: Optional[dict[str, Any]] = None
  235. def settle_unrecovered_error(
  236. agent: Any, *, api_error: Any, classified: Any, _retry: Any, status_code: Any, error_msg: Any,
  237. is_context_length_error: Any, is_rate_limited: Any, _is_zai_coding_overload: Any,
  238. _provider: Any, _base: Any, _model: Any, messages: Any, api_messages: Any, api_kwargs: Any,
  239. active_system_prompt: Any, conversation_history: Any, approx_tokens: Any, retry_count: Any,
  240. max_retries: Any, compression_attempts: Any, api_call_count: Any, error_context: Any = None,
  241. current_turn_user_idx: Any = None,
  242. ) -> UnrecoveredErrorVerdict:
  243. """Decide the fate of an API error that every recovery chain declined: local validation /
  244. non-retryable client errors (provider stale-credential self-heal first, then fallback, then a
  245. terminal result), max-retries exhaustion (primary transport recovery -> fallback -> terminal
  246. result), else the interruptible error backoff. ``FailoverReason.billing`` (402) is deliberately
  247. treated as non-retryable (#31273)."""
  248. from agent.conversation_loop import _arm_fallback_restart, _credential_refresh_profile
  249. from agent.provider_route_hooks import ask_on_error_path
  250. def _verdict(action: str, result: Optional[dict[str, Any]] = None) -> UnrecoveredErrorVerdict:
  251. return UnrecoveredErrorVerdict(
  252. action=action, active_system_prompt=active_system_prompt, retry_count=retry_count,
  253. compression_attempts=compression_attempts, result=result,
  254. )
  255. # ``FailoverReason.billing`` (402) is deliberately NOT excluded: pool rotation and
  256. # eager fallback already gave up, so retrying only burns paid requests on a depleted
  257. # balance. Mirrors 401/403.
  258. is_local_validation_error = _is_local_validation_error(api_error)
  259. # ``recover_after_classification`` sets ``image_shrink_retry_attempted`` BEFORE it runs the shrink,
  260. # so an image-size rejection reaching this point with the flag set had nothing left to shrink (the
  261. # excess is text on a host whose cap is payload-scoped, or an unshrinkable image). Re-sending the
  262. # byte-identical body ``max_retries`` times changes nothing: treat it as a client error and try
  263. # the fallback chain now, as the format_error verdict these 400s carried before did (#112473).
  264. shrink_spent = classified.reason == FailoverReason.image_too_large and bool(
  265. getattr(_retry, "image_shrink_retry_attempted", False)
  266. )
  267. # Same shape for the reasoning-disable rung: the retry already went out without the disable,
  268. # so a second reasoning-field rejection means the route refuses the configured reasoning
  269. # controls themselves — nothing left to drop, so take the fallback chain now instead of
  270. # replaying the identical request ``max_retries`` times (#114460).
  271. reasoning_spent = classified.reason == FailoverReason.reasoning_mandatory and bool(
  272. getattr(_retry, "reasoning_mandatory_retry_attempted", False)
  273. )
  274. is_client_error = (
  275. is_local_validation_error
  276. or shrink_spent
  277. or reasoning_spent
  278. or (
  279. not classified.retryable
  280. and not classified.should_compress
  281. and classified.reason not in RETRYABLE_CLIENT_REASONS
  282. )
  283. ) and not is_context_length_error
  284. if is_client_error:
  285. # A Codex ChatGPT-account entitlement 400 names the model: with nothing to rotate the
  286. # slug is dead for this account, so record it before the fallback walk runs (#106475).
  287. from agent.fallback_cooldown import _mark_entitlement_rejected_model
  288. _mark_entitlement_rejected_model(agent, api_error)
  289. # Provider self-heal BEFORE fallback: some providers surface a stale credential as a
  290. # non-auth error (the profile's ``is_stale_credential_error``), not a 401. Fresh
  291. # credential + client rebuild, one retry, SAME provider.
  292. if (
  293. not _retry.provider_stale_refresh_attempted
  294. and (profile := _credential_refresh_profile(agent)) is not None
  295. and ask_on_error_path(
  296. profile, "is_stale_credential_error", False,
  297. status_code=status_code, message=str(getattr(api_error, "message", "") or api_error),
  298. )
  299. ):
  300. _retry.provider_stale_refresh_attempted = True
  301. if agent._try_refresh_provider_runtime_credentials(reason="stale"):
  302. agent._buffer_vprint("🔐 " + (ask_on_error_path(profile, "credential_refresh_notice", "", reason="stale") or (
  303. f"{profile.display_name or profile.name} credential renewed after a "
  304. "stale-credential error. Retrying request...")))
  305. retry_count = 0
  306. return _verdict("continue")
  307. # ``should_fallback=False`` marks a deterministic failure no other provider can fix (the
  308. # model's own malformed tool-call JSON, #12770; MoA preset/adapter faults, #55933): skip
  309. # the cascade. An UNCLASSIFIED local ValueError/TypeError keeps its historical fallback;
  310. # a recognised verdict that opts out wins even when the exception is a ValueError subclass.
  311. _unclassified_local = is_local_validation_error and classified.reason == FailoverReason.unknown
  312. if classified.should_fallback or _unclassified_local or shrink_spent or reasoning_spent:
  313. # Announce the fallback only when a chain exists, else "trying fallback..." lies
  314. # before a silent abort.
  315. if agent._has_pending_fallback():
  316. _label = _NONRETRYABLE_LABELS.get(classified.reason, f"Non-retryable error (HTTP {status_code})")
  317. agent._buffer_diagnostic_status(f"⚠️ {_label} — trying fallback...")
  318. reset_at = error_context.get("reset_at") if isinstance(error_context, dict) else None
  319. if agent._try_activate_fallback(reason=classified.reason, reset_at=reset_at):
  320. # Direct ``return _verdict("break")`` is load-bearing: the restart handler
  321. # re-runs the pre-API preflight against the fallback's context window.
  322. active_system_prompt = _arm_fallback_restart(agent, api_messages, active_system_prompt, _retry)
  323. retry_count = compression_attempts = 0
  324. return _verdict("break")
  325. # Terminal from here: collapse the continuation trail once, before the persist.
  326. _delivered = settle_delivered_partial(agent, messages, current_turn_user_idx)
  327. return _verdict("return", nonretryable_client_error_result(
  328. agent, api_error, classified, status_code=status_code, api_kwargs=api_kwargs,
  329. api_messages=api_messages, messages=messages, conversation_history=conversation_history,
  330. api_call_count=api_call_count, approx_tokens=approx_tokens, provider=_provider,
  331. base_url=_base, model=_model, delivered=_delivered,
  332. ))
  333. # An attended session on the free model does not sit through a long cooldown: end the attempt
  334. # cycle now (fallback, else the reset time and the ways forward); the copy counts attempts made.
  335. attempts_made = min(retry_count, max_retries)
  336. if is_rate_limited and retry_count < max_retries and free_tier_cooldown_ends_turn(agent, api_error, _base):
  337. logger.info("%sFree-tier cooldown outlasts the attended wait — ending retries after attempt %s",
  338. agent.log_prefix, retry_count)
  339. retry_count = max_retries
  340. if retry_count >= max_retries:
  341. # Before fallback, rebuild the primary client once per API call block for
  342. # transient transport errors (stale pool, TCP reset).
  343. if not _retry.primary_recovery_attempted and agent._try_recover_primary_transport(
  344. api_error, retry_count=retry_count, max_retries=max_retries,
  345. ):
  346. _retry.primary_recovery_attempted = True
  347. retry_count = 0
  348. # Fresh attempt cycle: re-open fallback state so a follow-on 429 can still
  349. # activate fallback_providers.
  350. _retry.has_retried_429 = False
  351. agent._fallback_index = 0
  352. agent._fallback_activated = False
  353. return _verdict("continue")
  354. if agent._has_pending_fallback():
  355. agent._buffer_diagnostic_status(f"⚠️ Max retries ({max_retries}) exhausted — trying fallback...")
  356. reset_at = error_context.get("reset_at") if isinstance(error_context, dict) else None
  357. if agent._try_activate_fallback(reason=classified.reason, reset_at=reset_at):
  358. # Direct ``return _verdict("break")`` is load-bearing: the restart handler
  359. # re-runs the pre-API preflight against the fallback's context window.
  360. active_system_prompt = _arm_fallback_restart(agent, api_messages, active_system_prompt, _retry)
  361. retry_count = compression_attempts = 0
  362. return _verdict("break")
  363. # Fallback first (above); only with nothing left to move to does the bounded auto-recovery
  364. # ladder park the turn on a transient outage instead of ending it (#85426, #107307).
  365. from agent.turn_recovery_autorecover import auto_recover_after_exhaustion
  366. _ladder = auto_recover_after_exhaustion(
  367. agent, api_error, classified, _retry, messages=messages,
  368. conversation_history=conversation_history, api_call_count=api_call_count,
  369. )
  370. if _ladder is not None:
  371. if _ladder["action"] == "continue":
  372. retry_count = 0
  373. return _verdict(_ladder["action"], _ladder.get("result"))
  374. _delivered = settle_delivered_partial(agent, messages, current_turn_user_idx)
  375. return _verdict("return", max_retries_exhausted_result(
  376. agent, api_error, classified, attempts=attempts_made, is_rate_limited=is_rate_limited,
  377. error_msg=error_msg, api_kwargs=api_kwargs, api_messages=api_messages,
  378. messages=messages, conversation_history=conversation_history,
  379. api_call_count=api_call_count, approx_tokens=approx_tokens, provider=_provider,
  380. base_url=_base, model=_model, delivered=_delivered,
  381. ))
  382. wait_time = compute_error_backoff(
  383. agent, api_error, retry_count=retry_count, max_retries=max_retries,
  384. is_rate_limited=is_rate_limited, is_zai_coding_overload=_is_zai_coding_overload,
  385. base_url=_base, model=_model,
  386. )
  387. # Same preserve-redirect rule as the invalid-response wait: a steering correction
  388. # must survive backoff, not die as "Operation interrupted".
  389. _interrupted = interruptible_backoff_sleep(
  390. agent, wait_time, _retry, messages=messages, conversation_history=conversation_history,
  391. api_call_count=api_call_count,
  392. abort_message="Interrupt detected during retry wait, aborting.",
  393. interrupt_text=f"Operation interrupted: retrying API call after error (retry {retry_count}/{max_retries}).",
  394. activity_label=f"error retry backoff ({retry_count}/{max_retries})",
  395. )
  396. if _interrupted is not None:
  397. return _verdict("return", _interrupted)
  398. if _retry.restart_with_redirected_messages:
  399. # Leave the retry loop — the caller rebuilds this iteration from the correction
  400. # instead of re-firing the stale request.
  401. return _verdict("break")
  402. return _verdict("fallthrough")