+
    iHàj0h  ã                  óŠ  € R t ^ RIHt ^ RIt^ RIt^ RIt^ RIHt ^ RIH	t	H
t
HtHtHt ^ RIHtHtHtHtHtHt ^ RIHt ^ RIHt ^ RIHtHtHtHt ^ R	IHtH t  ^ R
I!H"t" ^ RI#H$t$ ]PJ                  ! R4      t&Rt'Rt(Rt)R t*] ! R R4      4       t+]! RR7       ! R R]+4      4       t,R R lt-R R lt.R R lt/R R lt0R R lt1R# )!aÔ  Overflow recovery for the conversation turn loop: 413 payload-too-large and
context-length errors after ``classify_api_error``.

Each path either compresses and signals a restart, defers softly (compression lock /
transient block), or ends the turn with a typed result. Nothing here imports
``agent.conversation_loop`` at module level (cycle); loop-internal helpers and the token
estimators that tests patch on the loop module are imported lazily inside the handlers.
)ÚannotationsN)Ú	dataclass)ÚAnyÚDictÚListÚOptionalÚTuple)Ú*COMPRESSION_RETRY_MESSAGES_STATUS_TEMPLATEÚ(COMPRESSION_RETRY_TOKENS_STATUS_TEMPLATEÚ+COMPRESSION_RETRY_TOO_LARGE_STATUS_TEMPLATEÚcompression_blocked_transientlyÚcompression_skipped_due_to_lockÚcontext_compression_timed_out)ÚFailoverReason)Úserialized_messages_bytes)Ú&get_context_length_from_provider_errorÚis_local_endpointÚis_output_cap_errorÚ(parse_available_output_tokens_from_error)Ú	site_copyÚstamp_failure)ÚTurnRetryState)Úbase_url_host_matcheszagent.conversation_loopuR      ðŸ’¡ Try /new to start a fresh conversation, or /compress to retry compression.g      à?c                  óv   € ] tR t^5t$ RtR]R&   R]R&   R]R&   R]R	&   R]R
&   R]R&   R]R&   R]R&   R]R&   RtR# )ÚOverflowVerdictuœ  Outcome of ``recover_from_overflow``.

``action`` is one of ``"return"`` (end the turn with ``result``), ``"break"``
(restart the API call â€” a ``_retry.restart_with_*`` flag is set), ``"continue"``
(retry the call immediately) or ``"fallthrough"`` (not an overflow error, or
overflow recovery declined â€” continue generic error handling). The remaining
fields are the loop locals the handler may have rebound.ÚstrÚactionúOptional[dict[str, Any]]Úresultúlist[dict[str, Any]]Úmessagesr   Úactive_system_promptÚconversation_historyÚintÚapprox_tokensÚcompression_attemptsÚboolÚ"provider_overflow_recovery_pendingÚis_context_length_error© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__Ú__static_attributes__r)   ó    Ú8/home/ubuntu/.hermes/hermes-agent/agent/turn_overflow.pyr   r   5   s?   ‡ ñ@ð ƒKØ$Ó$Ø"Ó"ØÓØÓØÓØÓØ(,Ó,Ø!×!r1   r   T)Úkw_onlyc            
      ó  € ] tR t^Jt$ RtR]R&   R]R&   R]R&   R]R&   R]R&   R]R	&   R
tR]R&   RtR]R&   R
tR]R&   Rt	R]R&   Rt
R]R&   R,R R lltRR-RR
RRRRRR/R R  lltR!R/R" R# lltR$R/R% R& lltR$R/R' R( lltR) R* ltR+tR
# ).Ú	_Recoveryu²   Working state shared by the overflow sub-handlers â€” the verdict itself, plus the
read-only call context; handlers mutate the loop-local fields and ``done()`` stamps
the action.r   ÚagentÚapi_messagesÚsystem_messageÚeffective_task_idr#   Úapi_call_countÚmax_compression_attemptsNzOptional[str]Úoverflow_reasonÚfallthroughr   r   r   r   Fr&   r'   r(   c               ó$   € V ^8„  d   QhRRRRRR/# )é   r   r   r   r   Úreturnr   r)   )Úformats   "r2   Ú__annotate__Ú_Recovery.__annotate__\   s"   € ÷ ñ ˜3ð Ð(@ð ÈOñ r1   c                	ó"   € WuV n         V n        V # ©N)r   r   )Úselfr   r   s   &&&r2   ÚdoneÚ_Recovery.done\   s   € Ø#)Ð ˆŒ�T”[Øˆr1   ÚnoticesÚlogÚcompression_exhaustedTÚreasonÚcontext_overflowÚ	retryablec               ó8   € V ^8„  d   QhRRRRRRRRR	RR
RRRRR/# )r?   Úfinal_responser   rI   ÚtuplerJ   zOptional[tuple]rK   r&   rL   rN   Úextrar   r@   r   r)   )rA   s   "r2   rB   rC   `   sR   € ÷ +ñ +Ø!ð+Ø/4ð+Ø@Oð+à#ð+à47ð+ð ð+ð +.ð+ð 
ñ	+r1   c               óà  € V P                   pV'       d<   VP                  4        V F%  p	VP                  VP                   V	 2RRR7       K'  	  V'       d   \        P
                  ! V!   VP                  V P                  V P                  4       \        RVRV P                  RRRV P                  RVR	RR
R/WV4      p
V'       d   RV
R&   V
P                  V4       V P                  RV
4      # )z†End the turn as failed/partial. ``notices`` flush the buffered retry trace
first so the user sees what compression attempts were made.T©ÚforceÚ
diagnosticrP   r    Ú	completedFÚ	api_callsÚerrorÚpartialÚfailedrK   r@   )r6   Ú_flush_status_bufferÚ_vprintÚ
log_prefixÚloggerrY   Ú_persist_sessionr    r"   r   r:   ÚupdaterG   )rF   rP   rI   rJ   rK   rL   rN   rR   r6   Úliner   s   &&$$$$$,   r2   Ú	fail_turnÚ_Recovery.fail_turn`   sâ   € ð —
‘
ˆßØ×&Ñ&Ô(Û�Ø—‘ ×!1Ñ!1Ð 2°4°&Ð9ÀÐRV�ÖWñ  çÜ�LŠL˜#ÒØ×Ñ˜tŸ}™}¨d×.GÑ.GÔHÜØ˜nØ˜Ÿ™Ø˜Ø˜×,Ñ,Ø�^Ø�tØ�dð 
ð óˆ÷ !ð /3ˆFÐ*Ñ+Ø�‰�eÔØ�y‰y˜ 6Ó*Ð*r1   Úpayload_too_largec               ó    € V ^8„  d   QhRRRR/# )r?   re   r&   r@   úOptional[OverflowVerdict]r)   )rA   s   "r2   rB   rC   €   s   € ÷ 
ñ 
°$ð 
ÐC\ñ 
r1   c               óÒ  € V ;P                   ^,          un         V P                  pV P                   V8:  d   R# V'       dU   V P                  \        RV P                  P
                  R7      RV R2\        3RV P                  P                  V3R7      # V P                  \        RV P                  P
                  R7      RV R	2\        3R
V P                  P                  V3R7      # )zMBump ``compression_attempts``; the terminal verdict once the cap is exceeded.Nre   ©Úmodelu   â�Œ Max compression attempts (z&) reached for payload-too-large error.z+%s413 compression failed after %d attempts.©rI   rJ   rM   z
) reached.z/%sContext compression failed after %d attempts.)r%   r;   rc   r   r6   rj   Ú_RETRY_HINTr^   )rF   re   Úcaps   &$ r2   Úcount_attemptÚ_Recovery.count_attempt€   sÝ   € à×!Ò! QÕ&Õ!Ø×+Ñ+ˆØ×$Ñ$¨Ô+ÙßØ—>‘>ÜÐ-°T·Z±Z×5EÑ5EÔFà4°S°EÐ9_Ð`Üðð CÀDÇJÁJ×DYÑDYÐ[^Ð_ð "ó ð ð �~‰~ÜÐ(°·
±
×0@Ñ0@ÔAØ5°c°U¸*ÐEÄ{ÐSØBÀDÇJÁJ×DYÑDYÐ[^Ð_ð ó 
ð 	
r1   Úfail_on_timeoutc               ó$   € V ^8„  d   QhRRRRRR/# )r?   Úrequest_tokensr#   rp   r&   r@   rg   r)   )rA   s   "r2   rB   rC   •   s"   € ÷ &ñ & sð &Àð &ÐQjñ &r1   c          
     óþ  € ^ RI Hp ^ RIHpHp V P
                  pV P                  pVP                  WpP                  VV P                  RRV P                  R7      w  V n        V n        V P                  VJ d·   Rp\        V4      '       d   V! W`P                  V P                  4      pM0\        V4      '       d    V! W`P                  V P                  RR7      pVeR   V ;P                  ^,          un        VP!                  V P                  V P"                  4       V P%                  R	V4      # V'       d%   \'        V4      '       d   V P)                  VR
R7      # V! W`P                  V P"                  4      V n        R# )a+  One compression pass with the summary-failure cooldown bypassed (the
provider proved the request doesn't fit). Returns ``None`` when history was
compressed, or a soft-defer verdict when another path holds the compression
lock or a timed guard no-oped the pass: the attempt is refunded and the turn
ends WITHOUT ``compression_exhausted`` so the gateway does not auto-reset. With
``fail_on_timeout`` a host timeout (recovery spent its wait budget with no
committed summary) ends the turn via the typed contract, since re-sending would
hit the same overflow.)Ú&conversation_history_after_compression)Ú#_COMPRESSION_TIMEOUT_FINAL_RESPONSEÚ_compression_deferred_resultTÚoverflow)r$   Útask_idÚbypass_cooldownÚtriggerr<   NÚtransient_block)rL   r@   Úcontext_compression_timeout)Úturn_exit_reason)Úagent.conversation_compressionrt   Úagent.conversation_loopru   rv   r6   r    Ú_compress_contextr8   r9   r<   r!   r   r:   r   r%   r`   r"   rG   r   rc   )	rF   rr   rp   rt   ru   rv   r6   ÚbeforeÚdeferreds	   &&$      r2   ÚcompressÚ_Recovery.compress•   sK  € õ 	Zßmà—
‘
ˆØ—‘ˆØ38×3JÑ3JØ×'Ñ'°~Ø×*Ñ*¸DÈ*Ø ×0Ñ0ð 4Kó 4
Ñ0ˆŒ�tÔ0ð
 �=‰=˜FÓ"ØˆHÜ.¨u×5Ò5Ù7¸¿}¹}Èd×NaÑNaÓb‘Ü0°×7Ò7Ù7ØŸ=™=¨$×*=Ñ*=ÐFWô�ð Ò#Ø×)Ò)¨QÕ.Õ)Ø×&Ñ& t§}¡}°d×6OÑ6OÔPØ—y‘y ¨8Ó4Ð4ßÔ<¸U×CÒCØ—>‘>Ø3ÐFcð "ó ð ñ %KØ—=‘= $×";Ñ";ó%
ˆÔ!ñ r1   c               ó$   € V ^8„  d   QhRRRRRR/# )r?   rr   r#   rp   r&   r@   z+tuple[Optional[OverflowVerdict], bool, int]r)   )rA   s   "r2   rB   rC   ½   s)   € ÷ Oñ OØ!ðOØ7;ðOà	4ñOr1   c               ó&  € ^ RI Hp \        V P                  4      pV! V P                  4      pV P	                  WR7      pVe   VRV3# V P                  pV! V4      pV^ 8„  ;'       d    W…R,          8  p	\        V4      V8  d<   V P
                  P                  \        P                  ! V\        V4      R7      4       M8V	'       d1   V P
                  P                  \        P                  ! WXR7      4       R\        V4      V8  ;'       g    T	V3# )a,  ``compress`` scored in message count / tokens (context-overflow errors ARE
token-budget errors). Same-message-count compression (tool-result pruning,
in-place summarization) can shrink the request, so re-estimate rather than trust
the array length. Returns ``(deferred_verdict, shrank, new_tokens)``.©Úestimate_messages_tokens_rough©rp   NFçffffffî?©r�   Úafter)
Úagent.model_metadatarˆ   Úlenr    rƒ   r6   Ú_buffer_diagnostic_statusr	   rA   r
   )
rF   rr   rp   rˆ   Úoriginal_lenÚoriginal_tokensr‚   r    Ú
new_tokensÚshrank_tokenss
   &&$       r2   Úcompress_scored_by_tokensÚ#_Recovery.compress_scored_by_tokens½   s  € õ 	Hä˜4Ÿ=™=Ó)ˆÙ8¸¿¹ÓGˆØ—=‘= �=ÓQˆØÒØ˜U OÐ3Ð3Ø—=‘=ˆñ 4°HÓ=ˆ
Ø" Q™×NÐN¨:È$Õ8NÑ+NˆÜˆx‹=˜<Ô'Ø�J‰J×0Ñ0Ô1[×1bÒ1bÐjvô  Bð  CKó  Lô  2Mõ  NßØ�J‰J×0Ñ0Ô1Y×1`Ò1`Ðhwô  2Kô  LØ”S˜“] \Ñ1×BÐB°]ÀJÐNÐNr1   c               ó   € V ^8„  d   QhRR/# )r?   r@   r#   r)   )rA   s   "r2   rB   rC   Ü   s   € ÷ `ñ ` ñ `r1   c                ór   € ^ RI Hp T! V P                  V P                  P                  ;'       g    RR7      # )z�Overhead-aware request size (msgs + tools + system) so LCM forced-overflow
recovery arms on the TRUE request, not the tool-blind message count.)Úestimate_request_tokens_roughN)Útools)r�   r˜   r7   r6   r™   )rF   r˜   s   & r2   rr   Ú_Recovery.request_tokensÜ   s.   € õ 	Gá,¨T×->Ñ->ÀdÇjÁj×FVÑFV×F^ÐF^ÐZ^Ô_Ð_r1   )r   r!   r"   r    r   rE   r)   )r*   r+   r,   r-   r.   r/   r<   r   r   r'   r(   rG   rc   rn   rƒ   r”   rr   r0   r)   r1   r2   r5   r5   J   sÊ   ‡ ñð ƒJØÓØÓØÓØÓØ!Ó!Ø%)€O�]Ó)Ø€FˆCÓØ'+€FÐ$Ó+Ø/4Ð&¨Ó4Ø$)Ð˜TÓ)÷ð+Ø79ð+ØRVð+à&*ð+à:Lð+ð  ÷+ð@
¸÷ 
ð*&Àu÷ &ðPOØ>C÷O÷>`ñ `r1   r5   c               ó$   € V ^8„  d   QhRRRRRR/# )r?   Ústr5   Ú_retryr   r@   r   r)   )rA   s   "r2   rB   rB   ä   s!   € ÷ 8ñ 8 9ð 8°nð 8Èñ 8r1   c                óÞ  € ^ RI Hp V P                  pV P                  RR7      pVe   V# VP	                  RV P
                   RV P                   R24       V P                  p\        V4      p\        V4      pV P                  V P                  4       4      pVe   V# V P                  pV! V4      V n        \        V4      p	\        V4      V8  g   V	^ 8”  d—   W—R,          8  dŠ   \        V4      V8  d2   VP	                  \        P                  ! V\        V4      R7      4       MVP	                  R	VR
 RV	R
 R24       \        P                   ! ^4       RVn        V P%                  R4      # VP'                  V P(                  RR7      '       d#   VP	                  R4       V P%                  R4      # V P+                  \-        RVP.                  R7      R\0        3RVP2                  3R7      # )uÊ   413: compress and retry. A 413 is a BYTE-size error, so progress is scored in
payload bytes â€” never the token estimate, which is deliberately byte-blind to images
and wedged sessions on "no progress".r‡   T)re   u@   âš ï¸�  Request payload too large (413) â€” compression attempt Ú/z...rŠ   r‹   u   ðŸ—œï¸� Compressed Ú,õ    â†’ z payload bytes, retrying...ÚbreakF)Úremember_modeluj   ðŸ“� Compression could not reduce the request further â€” removed retained vision payloads and retrying...Úcontinuere   ri   u2   â�Œ Payload too large and cannot compress further.z1%s413 payload too large. Cannot compress further.rk   )r�   rˆ   r6   rn   r�   r%   r;   r    rŽ   r   rƒ   rr   r$   r	   rA   ÚtimeÚsleepÚ restart_with_compressed_messagesrG   Ú)_try_strip_image_parts_from_tool_messagesr7   rc   r   rj   rl   r^   )
rœ   r�   rˆ   r6   Ú	exhaustedr    r�   Úoriginal_bytesr‚   Ú	new_bytess
   &&        r2   Ú_recover_payload_too_larger¬   ä   så  € õ Dà�H‰H€EØ× Ñ °4Ð Ó8€IØÒØÐØ	×#Ñ#Ø
JØ×"Ñ"Ð
# 1 R×%@Ñ%@Ð$AÀð	Fôð
 �{‰{€HÜ�x“=€Lô /¨xÓ8€NØ�{‰{˜2×,Ñ,Ó.Ó/€HØÒØˆð �{‰{€HÙ5°hÓ?€BÔÜ)¨(Ó3€IÜ
ˆ8ƒ}�|Ô#¨	°A¬¸)ÐW[ÕF[Ô:[Üˆx‹=˜<Ô'Ø×+Ñ+Ô,V×,]Ò,]ÐeqÔy|ð  ~Fó  zGô  -Hõ  Ià×+Ñ+Ø% n°QÐ%7°u¸YÀq¸MÐIhÐiôô 	�
Š
�1ŒØ26ˆÔ/Ø�w‰w�wÓÐà×6Ñ6°r·±ÐW\Ð6×]Ò]Ø×'Ñ'ð?ô	
ð �w‰w�zÓ"Ð"à�<‰<ÜÐ%¨U¯[©[Ô9ØEÄ{ÐSØ@À%×BRÑBRÐSð ó ð r1   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR	/# )
r?   rœ   r5   r�   r   Úavailable_outr#   Úold_ctxr@   r   r)   )rA   s   "r2   rB   rB     s1   € ÷ "ñ "˜)ð "¨^ð "ÈCð "ÐZ]ð "Ðbqñ "r1   c                óþ  € V P                   pV P                  4       pW5,
          pV^ 8”  d   \        ^\        W&4      ^@,
          4      pM\        ^V^@,
          4      pWtn        VP                  RVR RVR RVR RVR R2	4       V P                  4       pVe   V#  V P                  V4      w  ršpV	e   V	#  RVn        V P                  R	4      #   \         d$    \        P                  RTP                  4        LEi ; i)
zîOutput-cap error ("max_tokens too large": input fits but input + max_tokens >
window). The provider's available_tokens is the authoritative bound; also estimate
the real request shape (API-only content) and use the smaller minus a margin.uM   âš ï¸�  Output cap too large for current prompt â€” retrying with max_tokens=r    z (provider_available=z, estimated_request_tokens=z; context_length unchanged at Ú)zC%sOutput-cap compression hit an error; retrying on max_tokens only.Tr¢   )r6   rr   ÚmaxÚminÚ_ephemeral_max_output_tokensÚ_buffer_vprintrn   r”   Ú	Exceptionr_   Úwarningr^   r§   rG   )rœ   r�   r®   r¯   r6   Úrequest_input_estimateÚlocal_available_outÚsafe_outr©   r‚   Ú_shrankÚ_new_tokenss   &&&&        r2   Ú_clamp_output_capr½     s-  € ð �H‰H€EØ×.Ñ.Ó0ÐØ!Õ:ÐØ˜QÔÜ�qœ#˜mÓAÀBÕFÓG‰ô �q˜-¨"Õ,Ó-ˆØ)1Ô&Ø	×ÑØ
WÐX`ÐabÐWcð dØ,¨QÐ/Ð/JÐKaÐbcÐJdð e'Ø'.¨q k°ð	4ôð × Ñ Ó"€IØÒØÐð
Ø)+×)EÑ)EÐF\Ó)]Ñ&ˆ˜;ØÒØˆOð  ð /3€FÔ+Ø�7‰7�7ÓÐøô ô 
Ü�‰ØQÐSX×ScÑScö	
ð
ús   ÂC Ã+C<Ã;C<c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r?   rœ   r5   Ú	error_msgr   r¯   r#   r@   úOptional[int]r)   )rA   s   "r2   rB   rB   D  s)   € ÷ )ñ ) ið )¸Cð )È#ð )ÐR_ñ )r1   c           
     óV  € ^ RI Hp V P                  pVP                  p\	        W4      pVeÈ   VP                  RVR RVR R24       VP                  VP                  WdP                  \        VRR4      VP                  VP                  R	7       V! VP                  VP                  WdP                  4       \        VR
4      '       d   RVn        RVn        VP                  RVR RVR R24       V# \        VRR4      ;'       g    RP                  4       R9   ;'       gH    \        VRR4      ;'       g    RP!                  R4      P                  4       P#                  \$        4      pV'       d    RV9   d   VP                  RVR R24       R# VP                  RVR R24       R# )záShrink context_length only when the provider reports the real limit; else keep
the window and compress. Guessed probe tiers can turn a configured 1M window into
256K/128K/64K. Returns the provider-reported limit, or ``None``.)Úsave_provider_context_lengthNz!Context limit detected from API: r    z tokens (was r±   Úapi_keyÚ )rj   Úcontext_lengthÚbase_urlrÃ   ÚproviderÚapi_modeÚ_context_probedTu:   âš ï¸�  Context length exceeded â€” using provider limit: r¡   z tokensrÇ   rÆ   rŸ   zcontext window exceeds limit (zBProvider reported overflow amount only; keeping context_length at z tokens.um   âš ï¸�  Context length exceeded, but provider did not report a max context length; keeping context_length at >   Úminimaxú
minimax-cn)r�   rÂ   r6   Úcontext_compressorr   rµ   Úupdate_modelrj   rÆ   ÚgetattrrÇ   rÈ   ÚhasattrrÉ   Ú_context_probe_persistableÚlowerÚrstripÚ
startswithÚ_MINIMAX_ANTHROPIC_URLS)rœ   r¿   r¯   rÂ   r6   Ú
compressorÚnew_ctxÚis_minimax_providers   &&&     r2   Ú_adopt_provider_context_limitrØ   D  s»  € õ Bà�H‰H€EØ×)Ñ)€JÜ4°YÓH€GØÒØ×ÑÐ@ÀÈÀÈ=ÐY`ÐabÐXcÐcdÐeÔfØ×ÑØ—+‘+¨gÇÁÜ˜E 9¨bÓ1¸E¿N¹NÐUZ×UcÑUcð 	 ô 	
ñ 	% U§[¡[°%·.±.À'Ï>É>ÔZô �:Ð0×1Ò1Ø)-ˆJÔ&Ø48ˆJÔ1Ø×ÑÐYÐZaÐbcÐYdÐdiÐjqÐrsÐitÐt{Ð|Ô}Øˆô 
�˜
 BÓ	'×	-Ð	-¨2×4Ñ4Ó6Ð:SÑS÷ 	jð 	jÜ�E˜: rÓ*×0Ð0¨b×8Ñ8¸Ó=×CÑCÓE×PÑPÔQhÓið ÷ Ð?À9ÔLØ×Ñð)Ø)0°¨°8ð=ô	
ñ ð	 	×Ñð)Ø)0°¨°8ð=ô	
ñ r1   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r?   rœ   r5   r�   r   r¿   r   r@   r   r)   )rA   s   "r2   rB   rB   p  s/   € ÷ \ñ \ 	ð \°>ð \Ècð \ÐVeñ \r1   c                óŒ  € V P                   pVP                  P                  p\        V4      pVe   \	        WWT4      # \        V4      '       d-   V P                  RRVP                   RVR,           23RR7      # \        WV4      pVP                  P                  pV P                  4       \        ^ \        \        VR^ 4      ;'       g    ^ 4      4      ,           p\        VP                  4      '       d¶   \        P                   ! RV4      '       g™   \#        V\        4      '       dƒ   V^ 8”  d|   W‡\$        ,          8  dk   V P                  \'        RVP(                  W‡R	7      R
VR RVR R2R3RVP                  VR VR VR,          3R\*        P,                  P.                  RR7      # V P1                  4       p	V	e   V	# VP3                  \4        P6                  ! V P8                  V P:                  V P<                  R7      4       V P?                  V P                  4       RR7      w  r«pV
e   V
# WÀn        V'       g   V'       d<   Wd8  d6   \@        PB                  ! ^4       RV n"        RVn#        V PI                  R4      # V P                  \'        RVP(                  R7      RVR R2\J        3RVP                  VR 3R7      # )zÖContext-length error. Two shapes: "prompt too long" = INPUT overflows the window
(shrink context_length + compress); "max_tokens too large" = input fits but
input + max_tokens > window (shrink the OUTPUT cap only).z|The requested output length exceeds the provider's output cap for this model, and the error did not state the allowed limit.zMOutput-cap error not routed into compression (max_tokens over provider cap): :NéÈ   NF)rI   rJ   rK   Ú
max_tokensz\d{4,}Úserver_context_rejection)rj   ÚtokensÚwindowuB   â�Œ The server rejected the request as too large, but it is only ~r    z tokens against a u"   -token window â€” not compressing.u‚      ðŸ’¡ Wait a moment and /retry; another request on the same server (e.g. a background review) may have been holding its context.zg%sProvider context rejection not explained by request size (~%s of %s tokens); skipping compression: %sT)rI   rJ   rK   rL   rN   )rÞ   Úattemptrm   r‰   r¢   rM   ri   u0   â�Œ The conversation is too long for the model (z& tokens) and cannot be shrunk further.z>%sContext length exceeded: %s tokens. Cannot compress further.rk   )uŸ   â�Œ The provider rejected the request because the requested output length exceeds its output cap for this model, and the error did not state the allowed limit.uñ      ðŸ’¡ Hermes has no user setting for the output cap â€” check the endpoint's default max output (completion) tokens for this model on the server or proxy. (This is an output-cap error, not a context overflow â€” compression cannot fix it.))&r6   rÌ   rÅ   r   r½   r   rc   r^   rØ   rr   r²   r#   rÎ   r   rÆ   ÚreÚsearchÚ
isinstanceÚ_UNEXPLAINED_REJECTION_FRACTIONr   rj   r   Úserver_errorÚvaluern   r�   r   rA   r$   r%   r;   r”   r¥   r¦   r'   r§   rG   rl   )rœ   r�   r¿   r6   r¯   r®   rÖ   rß   rr   r©   r‚   Úshrankr’   s   &&&          r2   Ú_recover_context_lengthrè   p  sÓ  € ð �H‰H€EØ×&Ñ&×5Ñ5€Gä<¸YÓG€MØÒ Ü  ¨]ÓDÐDô ˜9×%Ò%Ø�|‰|ð=ðð ×#Ñ#Ð$ð %3Ø3<¸Tµ?Ð2CðEðð #(ð ó 
ð 	
ô" ,¨B¸7ÓC€Gð ×%Ñ%×4Ñ4€FØ×&Ñ&Ó(¬3¨q´#´g¸eÀ\ÐSTÓ6U×6ZÐ6ZÐYZÓ2[Ó+\Õ\€Nä˜%Ÿ.™.×)Ò)Ü—	’	˜) Y×/Ò/Ü�vœs×#Ò#¨°¬
ØÔ&EÕEÔEà�|‰|ÜÐ0¸¿¹ÈNÔjàTÐUcÐdeÐTfð g$Ø$*¨1 :Ð-OðQð=ðð+Ø,1×,<Ñ,<ÀÐPQÐ@RÐX^Ð_`ÐWaÐdmÐnrÕdsðð #(´×0KÑ0K×0QÑ0QÐ]að ó 
ð 	
ð × Ñ Ó"€IØÒØÐØ	×#Ñ#Ô$O×$VÒ$VØ×Ñ¨×)@Ñ)@Àb×FaÑFaô%ô ð $&×#?Ñ#?À×@QÑ@QÓ@SÐeiÐ#?Ó#jÑ €H�jØÒØˆØ!Ôß—'˜gÔ/Ü�
Š
�1Œð 15ˆÔ-Ø26ˆÔ/Ø�w‰w�wÓÐð �<‰<ÜÐ$¨E¯K©KÔ8à>¸zÈ!¸nÐLrÐsÜð
ð NÈu×O_ÑO_ÐdnÐopÐcqÐsð ó ð r1   c          $     ó`   € V ^8„  d   QhRRRRRRRRRR	R
RRR	RRRRRRRRRRRRRRRRRRRRRR/# )r?   r6   r   Ú	api_errorr¶   Ú
classifiedr�   r   Ústatus_coderÀ   r¿   r   Úwrapped_output_cap_budgetr    r   r7   r8   r!   r"   r$   r#   r%   r;   r:   r9   r@   r   r)   )rA   s   "r2   rB   rB   Ï  s¸   € ÷ 0"ñ 0"Øð0"Ø$ð0"Ø25ð0"Ø?Mð0"àð0"à+.ð0"àKXð0"ð #ð0"ð 36ð0"ð HKð0"ð ð	0"ð 69ð	0"ð JMð	0"ð
 ð0"ð
 :=ð0"ð
 ORð0"ð ð0"ð ñ0"r1   c               óF  € \        WV	VVWçW«WÍ\        VP                  RR4      R7      pVR8X  dl   \        V P                  \
        4      '       dL   \        V P                  R4      '       d0   \         F%  pV P                  V P                   V 2RRR7       K'  	  VP                  \        P                  8X  d   \        VV4      # VP                  \        P                  8H  ;'       g    VRJVn        VP                  '       d   \        VW54      # VP!                  R4      # )	aÐ  413 payload-too-large and context-length recovery (compress + retry, output-cap
clamp, provider-reported context limit, GitHub Models free-tier hint). Order is
load-bearing: 413 is checked BEFORE the generic 4xx handler, and context-length
errors (incl. relay-wrapped output-cap 429s) BEFORE non-retryable client errors.
Compression progress is scored in payload BYTES for 413 (never the byte-blind token
estimate) and in tokens/message count for context overflow.ræ   N)r6   r7   r8   r9   r:   r;   r    r!   r"   r$   r%   r<   i�  zmodels.inference.ai.azure.comTrT   r=   )r5   rÎ   rL   rã   rÆ   r   r   Ú_GITHUB_MODELS_HINTr]   r^   r   re   r¬   rM   r(   rè   rG   )r6   rê   rë   r�   rì   r¿   rí   r    r7   r8   r!   r"   r$   r%   r;   r:   r9   rœ   rb   s   &&&&$$$$$$$$$$$$$  r2   Úrecover_from_overflowrð   Ï  s	  € ô 
Ø¸~Ø+¸NØ!9Ø1Ø#Ü 
× 1Ñ 1°7¸DÓAô
€Bð 	�sÔÜ�u—~‘~¤s×+Ò+Ü! %§.¡.Ð2Q×RÒRç'ˆDØ�M‰M˜U×-Ñ-Ð.¨t¨fÐ5¸TÈdˆMÖSñ (ð ×ÑœN×<Ñ<Ô<Ü)¨"¨fÓ5Ð5ð 	×Ñœ^×<Ñ<Ñ<÷ 	1ð 	1Ø$¨DÐ0ð Ôð 
×!×!Ð!Ü& r¨6Ó=Ð=Ø�7‰7�=Ó!Ð!r1   )uJ      ðŸ’¡ GitHub Models free tier (models.inference.ai.azure.com) caps everyzJ      request at ~8K tokens. Hermes' system prompt + tool schemas baselinezF      exceeds that floor, so this endpoint cannot run an agentic loop.zK      Use the `copilot` provider with a Copilot subscription token (`hermesu=         setup` â†’ GitHub Copilot), or pick any other provider.)z https://api.minimax.io/anthropicz"https://api.minimaxi.com/anthropic)2r.   Ú
__future__r   Úloggingrá   r¥   Údataclassesr   Útypingr   r   r   r   r   r~   r	   r
   r   r   r   r   Úagent.error_classifierr   Úagent.message_sanitizationr   r�   r   r   r   r   Úagent.turn_failure_copyr   r   Úagent.turn_retry_stater   Úutilsr   Ú	getLoggerr_   rl   rä   rï   rÔ   r   r5   r¬   r½   rØ   rè   rð   r)   r1   r2   Ú<module>rû      sÕ   ðñõ #ã Û 	Û Ý !ß 3Õ 3÷÷ õ
 2Ý @÷ó ÷ =Ý 1Ý 'à	×	Ò	Ð4Ó	5€àb€ð
 #&Ð ðÐ ð eÐ ð ÷"ð "ó ð"ñ( �4ÔôV`�ó V`ó ðV`õr8õv"õJ)õX\÷~0"r1   