turn_overflow.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. """Overflow recovery for the conversation turn loop: 413 payload-too-large and
  2. context-length errors after ``classify_api_error``.
  3. Each path either compresses and signals a restart, defers softly (compression lock /
  4. transient block), or ends the turn with a typed result. Nothing here imports
  5. ``agent.conversation_loop`` at module level (cycle); loop-internal helpers and the token
  6. estimators that tests patch on the loop module are imported lazily inside the handlers.
  7. """
  8. from __future__ import annotations
  9. import logging
  10. import re
  11. import time
  12. from dataclasses import dataclass
  13. from typing import Any, Dict, List, Optional, Tuple
  14. from agent.conversation_compression import (
  15. COMPRESSION_RETRY_MESSAGES_STATUS_TEMPLATE, COMPRESSION_RETRY_TOKENS_STATUS_TEMPLATE,
  16. COMPRESSION_RETRY_TOO_LARGE_STATUS_TEMPLATE, compression_blocked_transiently,
  17. compression_skipped_due_to_lock, context_compression_timed_out,
  18. )
  19. from agent.error_classifier import FailoverReason
  20. from agent.message_sanitization import serialized_messages_bytes
  21. from agent.model_metadata import (
  22. get_context_length_from_provider_error, is_local_endpoint, is_output_cap_error,
  23. parse_available_output_tokens_from_error,
  24. )
  25. from agent.turn_failure_copy import site_copy, stamp_failure
  26. from agent.turn_retry_state import TurnRetryState
  27. logger = logging.getLogger("agent.conversation_loop")
  28. _RETRY_HINT = " 💡 Try /new to start a fresh conversation, or /compress to retry compression."
  29. # A provider "context exceeded" whose request (incl. the output reservation) is under this share
  30. # of the known window is not explained by the transcript; the rough estimator is ±30%, so a real
  31. # overflow (request ≈ window) never lands below half.
  32. _UNEXPLAINED_REJECTION_FRACTION = 0.5
  33. _MINIMAX_ANTHROPIC_URLS = ("https://api.minimax.io/anthropic", "https://api.minimaxi.com/anthropic")
  34. @dataclass
  35. class OverflowVerdict:
  36. """Outcome of ``recover_from_overflow``.
  37. ``action`` is one of ``"return"`` (end the turn with ``result``), ``"break"``
  38. (restart the API call — a ``_retry.restart_with_*`` flag is set), ``"continue"``
  39. (retry the call immediately) or ``"fallthrough"`` (not an overflow error, or
  40. overflow recovery declined — continue generic error handling). The remaining
  41. fields are the loop locals the handler may have rebound."""
  42. action: str
  43. result: Optional[dict[str, Any]]
  44. messages: list[dict[str, Any]]
  45. active_system_prompt: Any
  46. conversation_history: Any
  47. approx_tokens: int
  48. compression_attempts: int
  49. provider_overflow_recovery_pending: bool
  50. is_context_length_error: bool
  51. @dataclass(kw_only=True)
  52. class _Recovery(OverflowVerdict):
  53. """Working state shared by the overflow sub-handlers — the verdict itself, plus the
  54. read-only call context; handlers mutate the loop-local fields and ``done()`` stamps
  55. the action."""
  56. agent: Any
  57. api_messages: Any
  58. system_message: Any
  59. effective_task_id: Any
  60. api_call_count: int
  61. max_compression_attempts: int
  62. overflow_reason: Optional[str] = None # the classifier's reason, recorded on each compression attempt
  63. action: str = "fallthrough"
  64. result: Optional[dict[str, Any]] = None
  65. provider_overflow_recovery_pending: bool = False
  66. is_context_length_error: bool = False
  67. def done(self, action: str, result: Optional[dict[str, Any]] = None) -> OverflowVerdict:
  68. self.action, self.result = action, result
  69. return self
  70. def fail_turn(
  71. self, final_response: str, *, notices: tuple = (), log: Optional[tuple] = None,
  72. compression_exhausted: bool = True, reason: str = "context_overflow",
  73. retryable: bool = False, **extra: Any,
  74. ) -> OverflowVerdict:
  75. """End the turn as failed/partial. ``notices`` flush the buffered retry trace
  76. first so the user sees what compression attempts were made."""
  77. agent = self.agent
  78. if notices:
  79. agent._flush_status_buffer()
  80. for line in notices:
  81. agent._vprint(f"{agent.log_prefix}{line}", force=True, diagnostic=True)
  82. if log:
  83. logger.error(*log)
  84. agent._persist_session(self.messages, self.conversation_history)
  85. result = stamp_failure({
  86. "final_response": final_response,
  87. "messages": self.messages,
  88. "completed": False,
  89. "api_calls": self.api_call_count,
  90. "error": final_response,
  91. "partial": True,
  92. "failed": True,
  93. }, reason, retryable)
  94. if compression_exhausted:
  95. # Reuse the gateway's existing context-recovery contract (#98722, salvaged from #98741). The
  96. # bloated transcript remains intact while future input can move to a clean session instead of
  97. # replaying the summarize-timeout loop.
  98. result["compression_exhausted"] = True
  99. result.update(extra)
  100. return self.done("return", result)
  101. def count_attempt(self, *, payload_too_large: bool = False) -> Optional[OverflowVerdict]:
  102. """Bump ``compression_attempts``; the terminal verdict once the cap is exceeded."""
  103. self.compression_attempts += 1
  104. cap = self.max_compression_attempts
  105. if self.compression_attempts <= cap:
  106. return None
  107. if payload_too_large:
  108. return self.fail_turn(
  109. site_copy("payload_too_large", model=self.agent.model),
  110. notices=(
  111. f"❌ Max compression attempts ({cap}) reached for payload-too-large error.",
  112. _RETRY_HINT,
  113. ),
  114. log=("%s413 compression failed after %d attempts.", self.agent.log_prefix, cap),
  115. )
  116. return self.fail_turn(
  117. site_copy("context_overflow", model=self.agent.model),
  118. notices=(f"❌ Max compression attempts ({cap}) reached.", _RETRY_HINT),
  119. log=("%sContext compression failed after %d attempts.", self.agent.log_prefix, cap),
  120. )
  121. def compress(self, request_tokens: int, *, fail_on_timeout: bool = False) -> Optional[OverflowVerdict]:
  122. """One compression pass with the summary-failure cooldown bypassed (the
  123. provider proved the request doesn't fit). Returns ``None`` when history was
  124. compressed, or a soft-defer verdict when another path holds the compression
  125. lock or a timed guard no-oped the pass: the attempt is refunded and the turn
  126. ends WITHOUT ``compression_exhausted`` so the gateway does not auto-reset. With
  127. ``fail_on_timeout`` a host timeout (recovery spent its wait budget with no
  128. committed summary) ends the turn via the typed contract, since re-sending would
  129. hit the same overflow."""
  130. from agent.conversation_compression import conversation_history_after_compression
  131. from agent.conversation_loop import _COMPRESSION_TIMEOUT_FINAL_RESPONSE, _compression_deferred_result
  132. agent = self.agent
  133. before = self.messages
  134. self.messages, self.active_system_prompt = agent._compress_context(
  135. before, self.system_message, approx_tokens=request_tokens,
  136. task_id=self.effective_task_id, bypass_cooldown=True, trigger="overflow",
  137. overflow_reason=self.overflow_reason,
  138. )
  139. if self.messages is before:
  140. deferred = None
  141. if compression_skipped_due_to_lock(agent):
  142. deferred = _compression_deferred_result(agent, self.messages, self.api_call_count)
  143. elif compression_blocked_transiently(agent):
  144. deferred = _compression_deferred_result(
  145. agent, self.messages, self.api_call_count, reason="transient_block",
  146. )
  147. if deferred is not None:
  148. self.compression_attempts -= 1
  149. agent._persist_session(self.messages, self.conversation_history)
  150. return self.done("return", deferred)
  151. if fail_on_timeout and context_compression_timed_out(agent):
  152. return self.fail_turn(
  153. _COMPRESSION_TIMEOUT_FINAL_RESPONSE, turn_exit_reason="context_compression_timeout"
  154. )
  155. self.conversation_history = conversation_history_after_compression(
  156. agent, self.messages, self.conversation_history
  157. )
  158. return None
  159. def compress_scored_by_tokens(
  160. self, request_tokens: int, *, fail_on_timeout: bool = False,
  161. ) -> tuple[Optional[OverflowVerdict], bool, int]:
  162. """``compress`` scored in message count / tokens (context-overflow errors ARE
  163. token-budget errors). Same-message-count compression (tool-result pruning,
  164. in-place summarization) can shrink the request, so re-estimate rather than trust
  165. the array length. Returns ``(deferred_verdict, shrank, new_tokens)``."""
  166. from agent.model_metadata import estimate_messages_tokens_rough
  167. original_len = len(self.messages)
  168. original_tokens = estimate_messages_tokens_rough(self.messages)
  169. deferred = self.compress(request_tokens, fail_on_timeout=fail_on_timeout)
  170. if deferred is not None:
  171. return deferred, False, original_tokens
  172. messages = self.messages
  173. # Re-measure after compression. Same-message-count compression (tool-result pruning, in-place
  174. # summarization) can materially reduce request size without reducing the message array (#39550), and
  175. # — the image-dominated case — compaction's historical-media aging (#97160) can free megabytes of
  176. # base64 that the token estimate never counted. Bytes are the yardstick for a 413; tokens are kept
  177. # only for status display.
  178. # Re-estimate tokens after compression. Same-message-count compression (tool-result pruning,
  179. # in-place summarization) can materially reduce request size without reducing the message array.
  180. # (#39550)
  181. new_tokens = estimate_messages_tokens_rough(messages)
  182. shrank_tokens = new_tokens > 0 and new_tokens < original_tokens * 0.95
  183. if len(messages) < original_len:
  184. self.agent._buffer_diagnostic_status(COMPRESSION_RETRY_MESSAGES_STATUS_TEMPLATE.format(before=original_len, after=len(messages)))
  185. elif shrank_tokens:
  186. self.agent._buffer_diagnostic_status(COMPRESSION_RETRY_TOKENS_STATUS_TEMPLATE.format(before=original_tokens, after=new_tokens))
  187. return None, len(messages) < original_len or shrank_tokens, new_tokens
  188. def request_tokens(self) -> int:
  189. """Overhead-aware request size (msgs + tools + system) so LCM forced-overflow
  190. recovery arms on the TRUE request, not the tool-blind message count."""
  191. from agent.model_metadata import estimate_request_tokens_rough
  192. return estimate_request_tokens_rough(self.api_messages, tools=self.agent.tools or None)
  193. def _recover_payload_too_large(st: _Recovery, _retry: TurnRetryState) -> OverflowVerdict:
  194. """413: compress and retry. A 413 is a BYTE-size error, so progress is scored in
  195. payload bytes — never the token estimate, which is deliberately byte-blind to images
  196. and wedged sessions on "no progress"."""
  197. from agent.model_metadata import estimate_messages_tokens_rough
  198. agent = st.agent
  199. exhausted = st.count_attempt(payload_too_large=True)
  200. if exhausted is not None:
  201. return exhausted
  202. agent._buffer_diagnostic_status(
  203. f"⚠️ Request payload too large — compression attempt "
  204. f"{st.compression_attempts}/{st.max_compression_attempts}..."
  205. )
  206. messages = st.messages
  207. original_len = len(messages)
  208. # A 413 is a BYTE-size error, so this branch scores progress in BYTES of the serialized messages payload
  209. # — exact and free — never the token estimate. The estimator prices every image at a flat per-image
  210. # token cost (see estimate_messages_tokens_rough) so screenshots don't trigger premature compaction;
  211. # that deliberate byte-blindness means compaction can free megabytes of base64 (real case: two vision
  212. # results = 96.6% of the request body but ~3.7% of the estimate) while the token delta stays under any
  213. # threshold. Token-scored progress here burned all attempts on "no progress" and wedged the session
  214. # permanently. (#88960 / #47339)
  215. original_bytes = serialized_messages_bytes(messages)
  216. deferred = st.compress(st.request_tokens())
  217. if deferred is not None:
  218. return deferred
  219. # Re-measure: same-count compression and media aging can shrink the request
  220. # without shrinking the array. Tokens only for status display.
  221. messages = st.messages
  222. st.approx_tokens = estimate_messages_tokens_rough(messages)
  223. new_bytes = serialized_messages_bytes(messages)
  224. if len(messages) < original_len or (new_bytes > 0 and new_bytes < original_bytes * 0.95):
  225. if len(messages) < original_len:
  226. agent._buffer_diagnostic_status(COMPRESSION_RETRY_MESSAGES_STATUS_TEMPLATE.format(before=original_len, after=len(messages)))
  227. else:
  228. agent._buffer_diagnostic_status(
  229. f"🗜️ Compressed {original_bytes:,} → {new_bytes:,} " f"payload bytes, retrying..."
  230. )
  231. time.sleep(2) # Brief pause between compression retries
  232. _retry.restart_with_compressed_messages = True
  233. return st.done("break")
  234. if agent._try_strip_image_parts_from_tool_messages(st.api_messages, remember_model=False):
  235. agent._buffer_diagnostic_status(
  236. "📐 Compression could not reduce the request further — "
  237. "removed retained vision payloads and retrying..."
  238. )
  239. return st.done("continue")
  240. return st.fail_turn(
  241. site_copy("payload_too_large", model=agent.model),
  242. notices=("❌ Payload too large and cannot compress further.", _RETRY_HINT),
  243. log=("%s413 payload too large. Cannot compress further.", agent.log_prefix),
  244. )
  245. def _clamp_output_cap(st: _Recovery, _retry: TurnRetryState, available_out: int, old_ctx: int) -> OverflowVerdict:
  246. """Output-cap error ("max_tokens too large": input fits but input + max_tokens >
  247. window). The provider's available_tokens is the authoritative bound; also estimate
  248. the real request shape (API-only content) and use the smaller minus a margin."""
  249. agent = st.agent
  250. request_input_estimate = st.request_tokens()
  251. local_available_out = old_ctx - request_input_estimate
  252. if local_available_out > 0:
  253. safe_out = max(1, min(available_out, local_available_out) - 64)
  254. else:
  255. # Local estimate can overshoot; fall back to the provider-reported budget.
  256. safe_out = max(1, available_out - 64)
  257. agent._ephemeral_max_output_tokens = safe_out
  258. agent._buffer_vprint(
  259. f"⚠️ Output cap too large for current prompt — retrying with max_tokens={safe_out:,} "
  260. f"(provider_available={available_out:,}, estimated_request_tokens={request_input_estimate:,}; "
  261. f"context_length unchanged at {old_ctx:,})"
  262. )
  263. # Still count against compression_attempts so a recurring error can't loop forever.
  264. exhausted = st.count_attempt()
  265. if exhausted is not None:
  266. return exhausted
  267. # Also compress history so the retry doesn't spin on max_tokens alone; dropping the
  268. # middle window makes the total fit. Compression must never turn an output-cap error
  269. # fatal — on error, fall through and retry on max_tokens alone.
  270. try:
  271. deferred, _shrank, _new_tokens = st.compress_scored_by_tokens(request_input_estimate)
  272. if deferred is not None:
  273. return deferred
  274. except Exception:
  275. logger.warning(
  276. "%sOutput-cap compression hit an error; retrying on max_tokens only.", agent.log_prefix
  277. )
  278. _retry.restart_with_compressed_messages = True
  279. return st.done("break")
  280. def _adopt_provider_context_limit(st: _Recovery, error_msg: str, old_ctx: int) -> Optional[int]:
  281. """Shrink context_length only when the provider reports the real limit; else keep
  282. the window and compress. Guessed probe tiers can turn a configured 1M window into
  283. 256K/128K/64K. Returns the provider-reported limit, or ``None``."""
  284. from agent.model_metadata import save_provider_context_length
  285. agent = st.agent
  286. compressor = agent.context_compressor
  287. new_ctx = get_context_length_from_provider_error(error_msg, old_ctx)
  288. if new_ctx is not None:
  289. agent._buffer_vprint(f"Context limit detected from API: {new_ctx:,} tokens (was {old_ctx:,})")
  290. compressor.update_model(
  291. model=agent.model, context_length=new_ctx, base_url=agent.base_url,
  292. api_key=getattr(agent, "api_key", ""), provider=agent.provider, api_mode=agent.api_mode,
  293. )
  294. # Persist the provider-reported limit BEFORE compression/retry: rate limit,
  295. # missing usage, or restart must not lose confirmed metadata. Probe flags
  296. # remain a fallback if this write fails.
  297. save_provider_context_length(agent.model, agent.base_url, new_ctx, agent.provider)
  298. # Probe flags only on the built-in compressor (plugin engines manage their
  299. # own); provider-sourced value, so safe to cache.
  300. if hasattr(compressor, "_context_probed"):
  301. compressor._context_probed = True
  302. compressor._context_probe_persistable = True
  303. agent._buffer_vprint(f"⚠️ Context length exceeded — using provider limit: {old_ctx:,} → {new_ctx:,} tokens")
  304. return new_ctx
  305. is_minimax_provider = (
  306. (getattr(agent, "provider", "") or "").lower() in {"minimax", "minimax-cn"}
  307. or (getattr(agent, "base_url", "") or "").rstrip("/").lower().startswith(_MINIMAX_ANTHROPIC_URLS)
  308. )
  309. if is_minimax_provider and "context window exceeds limit (" in error_msg:
  310. agent._buffer_vprint(
  311. f"Provider reported overflow amount only; "
  312. f"keeping context_length at {old_ctx:,} tokens."
  313. )
  314. else:
  315. agent._buffer_vprint(
  316. f"⚠️ Context length exceeded, but provider did not report a max context length; "
  317. f"keeping context_length at {old_ctx:,} tokens."
  318. )
  319. return None
  320. def _recover_context_length(st: _Recovery, _retry: TurnRetryState, error_msg: str) -> OverflowVerdict:
  321. """Context-length error. Two shapes: "prompt too long" = INPUT overflows the window
  322. (shrink context_length + compress); "max_tokens too large" = input fits but
  323. input + max_tokens > window (shrink the OUTPUT cap only)."""
  324. agent = st.agent
  325. old_ctx = agent.context_compressor.context_length
  326. available_out = parse_available_output_tokens_from_error(error_msg)
  327. if available_out is not None:
  328. return _clamp_output_cap(st, _retry, available_out, old_ctx)
  329. # Output-cap error with unparseable budget: compression can't help (input already
  330. # fits) and would death-loop on the same 400. Fail fast.
  331. if is_output_cap_error(error_msg):
  332. return st.fail_turn(
  333. "The requested output length exceeds the provider's output cap for this model, "
  334. "and the error did not state the allowed limit.",
  335. notices=(
  336. "❌ The provider rejected the request because the requested output length exceeds its "
  337. "output cap for this model, and the error did not state the allowed limit.",
  338. " 💡 Hermes has no user setting for the output cap — check the endpoint's default max output "
  339. "(completion) tokens for this model on the server or proxy. "
  340. "(This is an output-cap error, not a context overflow — compression cannot fix it.)",
  341. ),
  342. log=(
  343. f"{agent.log_prefix}Output-cap error not routed into compression "
  344. f"(max_tokens over provider cap): {error_msg[:200]}",
  345. ),
  346. compression_exhausted=False,
  347. )
  348. new_ctx = _adopt_provider_context_limit(st, error_msg, old_ctx)
  349. # A rejection the transcript cannot explain (#114644): the request sits far below the window
  350. # Hermes knows for this model (after adopting any limit the server reported), so compressing
  351. # would destroy history for nothing. Single-slot local servers reject like this while ANOTHER
  352. # request — a background review from an earlier session — holds their context. Name that,
  353. # keep the turn retryable and transient: no "conversation too long", no gateway auto-reset.
  354. # Only when the server quoted NO measurement of its own: "prompt is too long: 233153 tokens
  355. # > 200000" is the server's count and beats the local estimate. Local endpoints only: a hosted
  356. # route has no shared slot, so the same rejection means its real window is smaller than the one
  357. # Hermes assumes, and "wait and /retry" would fail identically forever; compress instead.
  358. window = agent.context_compressor.context_length
  359. request_tokens = st.request_tokens() + max(0, int(getattr(agent, "max_tokens", 0) or 0))
  360. if (
  361. is_local_endpoint(agent.base_url)
  362. and not re.search(r"\d{4,}", error_msg)
  363. and isinstance(window, int) and window > 0
  364. and request_tokens < window * _UNEXPLAINED_REJECTION_FRACTION
  365. ):
  366. return st.fail_turn(
  367. site_copy("server_context_rejection", model=agent.model, tokens=request_tokens, window=window),
  368. notices=(
  369. f"❌ The server rejected the request as too large, but it is only ~{request_tokens:,} "
  370. f"tokens against a {window:,}-token window — not compressing.",
  371. " 💡 Wait a moment and /retry; another request on the same server (e.g. a background "
  372. "review) may have been holding its context.",
  373. ),
  374. log=(
  375. "%sProvider context rejection not explained by request size (~%s of %s tokens); "
  376. "skipping compression: %s", agent.log_prefix, f"{request_tokens:,}", f"{window:,}", error_msg[:200],
  377. ),
  378. compression_exhausted=False, reason=FailoverReason.server_error.value, retryable=True,
  379. )
  380. exhausted = st.count_attempt()
  381. if exhausted is not None:
  382. return exhausted
  383. agent._buffer_diagnostic_status(COMPRESSION_RETRY_TOO_LARGE_STATUS_TEMPLATE.format(
  384. tokens=st.approx_tokens, attempt=st.compression_attempts, cap=st.max_compression_attempts,
  385. ))
  386. deferred, shrank, new_tokens = st.compress_scored_by_tokens(st.request_tokens(), fail_on_timeout=True)
  387. if deferred is not None:
  388. return deferred
  389. st.approx_tokens = new_tokens
  390. if shrank or (new_ctx and new_ctx < old_ctx):
  391. time.sleep(2) # Brief pause between compression retries
  392. # Rebuild the full request and force normal preflight to honor it; message
  393. # count alone doesn't prove system/tool-inclusive pressure fell.
  394. st.provider_overflow_recovery_pending = True
  395. _retry.restart_with_compressed_messages = True
  396. return st.done("break")
  397. # Can't compress further and already at minimum tier.
  398. return st.fail_turn(
  399. site_copy("context_overflow", model=agent.model),
  400. notices=(
  401. f"❌ The conversation is too long for the model ({new_tokens:,} tokens) and cannot be shrunk further.",
  402. _RETRY_HINT,
  403. ),
  404. log=("%sContext length exceeded: %s tokens. Cannot compress further.", agent.log_prefix, f"{new_tokens:,}"),
  405. )
  406. def _request_rejection_hint(status_code: Optional[int], base_url: str) -> tuple[str, ...]:
  407. """Every registered profile's ``request_rejection_hint`` for this endpoint, whatever route reached
  408. it (a free tier is commonly reached as ``custom``)."""
  409. from providers import list_providers
  410. return tuple(line for profile in list_providers()
  411. for line in profile.request_rejection_hint(status_code=status_code, base_url=base_url))
  412. def recover_from_overflow(
  413. agent: Any, api_error: Exception, classified: Any, _retry: TurnRetryState, *,
  414. status_code: Optional[int], error_msg: str, wrapped_output_cap_budget: Optional[int],
  415. messages: list[dict[str, Any]], api_messages: Any, system_message: Any,
  416. active_system_prompt: Any, conversation_history: Any, approx_tokens: int,
  417. compression_attempts: int, max_compression_attempts: int, api_call_count: int,
  418. effective_task_id: Any,
  419. ) -> OverflowVerdict:
  420. """413 payload-too-large and context-length recovery (compress + retry, output-cap
  421. clamp, provider-reported context limit, endpoint hard-cap hints). Order is
  422. load-bearing: 413 is checked BEFORE the generic 4xx handler, and context-length
  423. errors (incl. relay-wrapped output-cap 429s) BEFORE non-retryable client errors.
  424. Compression progress is scored in payload BYTES for 413 (never the byte-blind token
  425. estimate) and in tokens/message count for context overflow."""
  426. st = _Recovery(
  427. agent=agent, api_messages=api_messages, system_message=system_message,
  428. effective_task_id=effective_task_id, api_call_count=api_call_count,
  429. max_compression_attempts=max_compression_attempts, messages=messages,
  430. active_system_prompt=active_system_prompt, conversation_history=conversation_history,
  431. approx_tokens=approx_tokens, compression_attempts=compression_attempts,
  432. overflow_reason=getattr(classified.reason, "value", None),
  433. )
  434. # An endpoint whose hard cap no recovery can meet (a provider profile's
  435. # ``request_rejection_hint``, whatever route reached it): say so.
  436. if isinstance(agent.base_url, str):
  437. for line in _request_rejection_hint(status_code, agent.base_url):
  438. agent._vprint(f"{agent.log_prefix}{line}", force=True, diagnostic=True)
  439. if classified.reason == FailoverReason.payload_too_large:
  440. return _recover_payload_too_large(st, _retry)
  441. # Relay-wrapped output-cap 429s (parsed by the caller) go to the clamp, not
  442. # failover or generic retries. The classifier also covers 400/disconnect +
  443. # large-session heuristics.
  444. st.is_context_length_error = (
  445. # Check for context-length errors BEFORE generic 4xx handler. The classifier detects context
  446. # overflow from: explicit error messages, generic 400 + large session heuristic (#1630), and server
  447. # disconnect + large session pattern (#2153).
  448. classified.reason == FailoverReason.context_overflow
  449. or wrapped_output_cap_budget is not None
  450. )
  451. if st.is_context_length_error:
  452. return _recover_context_length(st, _retry, error_msg)
  453. return st.done("fallthrough")