+
    -pÐj5  ã                  óP  € R t ^ RIHt ^ RIHt ^ RIHt ^ RIt^ RIt^ RI	H
t
HtHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHtHt ^ R
IHtHt ]P2                  ! R4      tR R lt] ! R R4      4       tR R ltR R lt] ! R R4      4       tR R lt ] ! R R4      4       t!R R lt"R# )añ  The provider call for the conversation turn's retry loop: ``nous_rate_limit_guard`` (skip
the attempt while another session's Nous Portal rate limit is active), ``perform_api_call``
(streaming decision, MoA prepared-request handshake, LLM execution middleware wrapper, the
redirect ``_model_request_active`` bracket and the response-vs-redirect crossing check) and
``handle_api_interrupt`` (``InterruptedError`` mid-call). Nothing here imports
``agent.conversation_loop`` at module level (cycle).
)Úannotations)Únullcontext)Ú	dataclassN)ÚAnyÚDictÚOptional)ÚFailoverReason)Ú hidden_interrupt_placeholder_row)Úappend_message)ÚREPETITION_LOOP_INTERRUPTEDÚis_runaway_repetition)Ú	site_copyÚstamp_failurezagent.conversation_loopc               ó$   € V ^8„  d   QhRRRRRR/# )é   Úagentr   Úthinking_spinnerÚreturnÚNone© )Úformats   "Ú"/opt/hermes/agent/turn_api_call.pyÚ__annotate__r      s!   € ÷ $ñ $ ð $¸ð $Àñ $ó    c                ó‚   € V'       d   VP                  R4       V P                  '       d   V P                  R4       R# R# )z¨Stop the spinner silently and clear the thinking callback; returns ``None`` so
callers can rebind ``thinking_spinner = stop_thinking_spinner(agent, thinking_spinner)``.Ú N)ÚstopÚthinking_callback©r   r   s   &&r   Ústop_thinking_spinnerr      s4   € ÷ Ø×Ñ˜bÔ!Ø××ÐØ×Ñ Ö#ñ r   c                  óD   € ] tR t^#t$ RtR]R&   R]R&   R]R&   R]R&   RtR	# )
ÚApiCallVerdictu«   ``action``: ``"fallthrough"`` (``response`` is ready for verification) or ``"break"``
(a redirect crossed the response â€” rebuild armed on ``_retry`` or ``interrupted``).ÚstrÚactionr   Úresponser   Úinterruptedr   N©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__Ú__static_attributes__r   r   r   r!   r!   #   s    ‡ ñ]ð ƒKØƒMØÓØ×r   r!   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   Úboolr   )r   s   "r   r   r   .   s   € ÷ ñ ˜#ð  $ñ r   c                ó”  € \        V RR4      '       d   R# \        V P                  ;'       g    R4      P                  4       p^ RIHp VP                  R
4      '       g   V! V P                  4      '       d   R# V P                  4       '       g9   V P                  R8X  d   R# ^ RI	H
p \        \        V RR4      V4      '       d   R# R	# )a$  Streaming is preferred even without consumers (stale-stream / read-timeout health
checks); disabled on provider signal, ACP providers (``acp://`` scheme or an
external-process provider profile), MoA without a display consumer, or Mock clients in
tests (SimpleNamespace, not stream iterators).Ú_disable_streamingFr   )Ú_is_external_process_providerÚmoa)ÚMockÚclientNT)zacp://z
acp+tcp://)Úgetattrr"   Úbase_urlÚlowerÚ$hermes_cli.runtime_provider_backendsr2   Ú
startswithÚproviderÚ_has_stream_consumersÚunittest.mockr4   Ú
isinstance)r   Ú_baser2   r4   s   &   r   Ú_should_streamr@   .   s�   € ô
 ˆuÐ*¨E×2Ò2ÙÜ�—‘×$Ð$ "Ó%×+Ñ+Ó-€EÝRà×ÑÐ0×1Ò1Ñ5RÐSX×SaÑSa×5bÒ5bÙØ×&Ñ&×(Ò(Ø�>‰>˜UÔ"ÙÝ&Ü”g˜e X¨tÓ4°d×;Ò;ÙÙr   c               óP   € 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   Ú
api_kwargsÚ_original_api_kwargsÚ_llm_middleware_traceÚ_moa_prepared_requestÚ_retryr   Úretry_countÚapi_call_countÚapi_request_idÚeffective_task_idÚturn_idr%   r   r!   r   )r   s   "r   r   r   C   sœ   € ÷ Y#ñ Y#ØðY#Ø"ðY#Ø:=ðY#ØVYðY#àðY#à(+ðY#à?BðY#àQTðY#ð ðY#ð *-ðY#ð BEðY#ð PSðY#ð ð	Y#ð
 ñY#r   c               ó8  a aaa	aaaa€ RoR VVV3R llpV V3R lo\        S 4      oVVV V	V3R lp^ RIHp \        S RR4      p\        S RR4      pVf   \	        4       MTpT;_uu_ 4        Ve   VP                  4        RRR4        T! YTY«S	S P                  ;'       g    R	S P                  ;'       g    R	S P                  S P                  S P                  S P                  V\        V4      R
7      oT;_uu_ 4        Ve   VP                  4        Ve   \        S P                  4      MS P!                  4       pRRR4       X'       d7   \#        S S4      oS P%                  RR7      '       d	   RTn        MRoT! R4      # T! R4      #   + '       g   i     EL; i  + '       g   i     Lj; i  T;_uu_ 4        Te   TP                  4        Te   \        S P                  4      MS P!                  4       pRRR4       i   + '       g   i     i ; i; i)zFIssue the request (see ``_should_stream`` for the streaming decision).Nc               ó    € V ^8„  d   QhRRRR/# )r   r#   r"   r   r!   r   )r   s   "r   r   Ú&perform_api_call.<locals>.__annotate__L   s   € ÷ 
ñ 
˜ð 
 ñ 
r   c                ó"   <€ \        V SSSR 7      # ))r#   r$   r   r%   )r!   )r#   r%   r$   r   s   &€€€r   Ú_verdictÚ"perform_api_call.<locals>._verdictL   s   ø€ ÜØ HÐ?OØ#ô
ð 	
r   c                 ó    <€ \        S S4      oR # ©N)r   r   s   €€r   Ú_stop_spinnerÚ'perform_api_call.<locals>._stop_spinnerR   s   ø€ ä0°Ð8HÓIÒr   c                ót  <€ SP                   R 8X  d@   SP                  4       P                  V RSP                  4       SP	                  4       R7      p S'       d   SP                  V SR7      # ^ RIHp TP                  T SP                  \        SP                  ;'       g    R4      \        SP                  ;'       g    R4      \        SP                  ;'       g    R4      RSP                   RSR	\        SR
R4      '       d   RM(\        \        SR^ 4      ;'       g    ^ 4      ^ 8”  d   RMRRS/RR7      # )Úcodex_responsesF)Úallow_streamÚis_github_responsesÚsanitize_harmony_tokens)Úon_first_delta)Ú	relay_llmr   r;   Úapi_moderI   Ú	call_roleÚis_subagentÚ	delegatedÚ_fallback_indexÚfallbackÚprimaryrG   T)Ú
session_idÚnameÚ
model_nameÚmetadataÚdefer_logical_completion)r]   Ú_get_transportÚpreflight_kwargsÚ_is_copilot_urlÚ_is_codex_backendÚ!_interruptible_streaming_api_callr   r\   ÚexecuteÚ_interruptible_api_callr"   rd   r;   Úmodelr6   Úint)Únext_api_kwargsr\   rT   Ú_use_streamingr   rI   rG   s   & €€€€€r   Ú_perform_api_callÚ+perform_api_call.<locals>._perform_api_callX   s1  ø€ Ø�>‰>Ð.Ô.Ø#×2Ñ2Ó4×EÑEØ¨eÈ×I^ÑI^ÓI`Ø(-×(?Ñ(?Ó(Að Fó ˆO÷ Ø×:Ñ:Ø°ð ;ó ð õ 	$à× Ñ ØØ×)Ñ)Ü˜5×+Ñ+×1Ð1¨rÓ2Ü�U—^‘^×1Ð1 zÓ2Ü˜5Ÿ;™;×,Ð,¨"Ó-à˜EŸN™NØ  .Øä˜u m°U×;Ò;ñ  ô œ7 5Ð*;¸QÓ?×DÐDÀ1ÓEÈÔIñ $à"à˜{ðð &*ð% !ó 
ð 	
r   )Úrun_llm_execution_middlewareÚ_model_request_activeÚ_pending_redirect_lockr   )Úoriginal_requestÚtask_idrK   rI   rd   Úplatformrp   r;   r7   r]   rH   Úmiddleware_traceT©Úpreserve_redirectÚbreakÚfallthrough)r@   Úhermes_cli.middlewarerv   r6   r   Úsetrd   r{   rp   r;   r7   r]   ÚlistÚclearr/   Ú_pending_redirectÚ_has_pending_redirectr   Úclear_interruptÚ restart_with_redirected_messages)r   rB   rC   rD   rE   rF   r   rG   rH   rI   rJ   rK   r%   rP   rt   rv   rw   Ú_redirect_lockÚ_bracketÚ_redirect_crossed_responserT   rs   r$   s   f$$$$$dd$d$$d       @@@r   Úperform_api_callrŒ   C   sÈ  ÿ€ ð €H÷
ò 
öJô $ EÓ*€N÷
ñ 
õB Cô $ EÐ+BÀDÓIÐÜ˜UÐ$<¸dÓC€NØ .Ò 6Œ{Œ}¸N€Hß	ŠØ Ò,Ø!×%Ñ%Ô'÷ 
ðÙ/ØÐ<PØ%À~Ø×'Ñ'×-Ð-¨2¸¿¹×8LÐ8LÈ"ÐTY×T_ÑT_Ø—^‘^¨e¯n©nÀuÇ~Á~Ø)¼DÐAVÓ<Wô
ˆ÷ ŠXØ$Ò0Ø%×+Ñ+Ô-à1?Ò1K”�U×,Ñ,Ô-Ø×0Ñ0Ó2ð '÷ ÷ "ô 1°Ð8HÓIÐØ× Ñ °4Ð ×8Ò8Ø6:ˆFÕ3àˆKÙ˜Ó Ð Ù�MÓ"Ð"÷9 
�ˆú÷ �Xû�XŠXØ$Ò0Ø%×+Ñ+Ô-à1?Ò1K”�U×,Ñ,Ô-Ø×0Ñ0Ó2ð '÷ �X�XˆXýsH   Á,FÂ
F4 Â#F4 Â8?F4 Ä>F!ÆF	Æ!F1	Æ4HÆ?>H	Ç=	HÈHÈHc                  óD   € ] tR t^Ÿt$ RtR]R&   R]R&   R]R&   R]R&   RtR	# )
ÚApiInterruptVerdictz¢Always ``action == "break"`` (leave the retry loop): either a redirect restart was
armed on ``_retry`` or the turn is ``interrupted`` with ``final_response`` set.r"   r#   r   r   r%   Úfinal_responser   Nr&   r   r   r   rŽ   rŽ   Ÿ   s!   ‡ ñWð ƒKØÓØÓØ×r   rŽ   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   rF   r   ÚmessagesÚconversation_historyÚapi_start_timer%   r�   r   rŽ   r   )r   s   "r   r   r   ª   sa   € ÷ (Wñ (WØð(WØð(WØ25ð(WØADð(WØ\_ð(Wàð(Wà&)ð(Wà;>ð(Wð ñ(Wr   c          
     ó   € ^ RI Hp \        W4      pV P                  4       '       d-   V P	                  RR7      '       d   RVn        \        RW&V4      # \        P                  ! 4       V,
          p	V P                  V P                   R2RR7       RpV P                  V4       T P                  \        V RR4      ;'       g    R4      P                  4       p
V
'       d-   \        V
4      '       d   \        V\!        4       4       \"        pM'V
'       d   \        VR	R
RV
RRR//4       T
pMV V	R R2pV P%                  W44       \        RW&V4      # )zÜ``InterruptedError`` during the provider call: a pending redirect keeps its correction
queued for the outer-loop rebuild; otherwise keep any streamed partial text so the next
turn has a record of the half-finished reply.)Ú"INTERRUPT_WAITING_FOR_MODEL_PREFIXTr}   r   u    âš¡ Interrupted during API call.)ÚforceÚ _current_streamed_assistant_textr   ÚroleÚ	assistantÚcontentÚdisplay_metadatar%   z.1fzs elapsed).)Úagent.conversation_loopr•   r   r†   r‡   rˆ   rŽ   ÚtimeÚ_vprintÚ
log_prefixÚ)_drop_trailing_empty_response_scaffoldingÚ_strip_think_blocksr6   Ústripr   r
   r	   r   Ú_persist_session)r   rF   r   r‘   r’   r“   r%   r�   r•   Úapi_elapsedÚ_partials   &$$$$$$$   r   Úhandle_api_interruptr¦   ª   sH  € õ Kä,¨UÓEÐð ×"Ñ"×$Ò$¨×)>Ñ)>ÐQUÐ)>×)VÒ)VØ26ˆÔ/Ü" 7Ð,<È>ÓZÐZÜ—)’)“+ Õ.€KØ	‡M�M�U×%Ñ%Ð&Ð&FÐGÈt€MÔTØ€Kð 
×3Ñ3°HÔ=Ø×(Ñ(Ü�Ð9¸2Ó>×DÐDÀ"óç�eƒgð ÷ Ô)¨(×3Ò3ô 	�xÔ!AÓ!CÔDÜ4‰ß	Ü�xØ�K ¨HÐ6HÈ=ÐZ^ÐJ_ð"
ô 	ð "‰à>Ð?ÀÈCÐ?PÐP[Ð\ˆØ	×Ñ˜8Ô:Ü˜wÐ(8À~ÓVÐVr   c                  óR   € ] tR t^Õt$ RtR]R&   R]R&   R]R&   R]R&   RtR	]R
&   RtR# )ÚNousRateGuardVerdictu¢   ``action``: ``"fallthrough"`` (no active limit â€” make the call), ``"break"``
(fallback armed on ``_retry``) or ``"return"`` (``result``: no fallback available).r"   r#   r   Úactive_system_promptrG   Úcompression_attemptsNúOptional[dict[str, Any]]Úresultr   )r'   r(   r)   r*   r+   r,   r¬   r-   r   r   r   r¨   r¨   Õ   s,   ‡ ñ[ð ƒKØÓØÓØÓØ'+€FÐ$×+r   r¨   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   rF   Úapi_messagesr‘   r’   r©   rG   rª   rH   r   r¨   r   )r   s   "r   r   r   á   s^   € ÷ ?#ñ ?#Øð?#Øð?#Ø.1ð?#Ø=@ð?#ØX[ð?#àð?#à,/ð?#àGJð?#à\_ð?#ð ñ?#r   c               óÀ  aaa€ ^ RI Hp	 RR VVV3R lllp
V P                  R8X  Ed�    ^ RIHp V! V 4        ^ RIHpHp ^ RI	H
p VP                  V 4      pV! VR7      pVEeP   V^ 8”  EdH   V! V4      pV'       d-   VP                  P                  VP                  V4      R	7      pMR
V R2pV P                  RV R24       V P!                  RV 24       V P#                  4       '       d   V	! WSV4      o^ o^ oV
! R4      # V P%                  4        V P'                  W44       T
! R\)        RV'       d   RV 2MRV R\+        R4       2RVRVRRRRRV/V'       d2   RRRRVP,                  P                  VP                  V4      R	7      //M/ C\.        P0                  P2                  R4      4      # V
! R4      #   \
         d     EL“i ; i  \
         d     L'i ; i) z«Skip the call if another session recorded a Nous Portal rate limit: every attempt (incl.
SDK retries) counts against RPH. Never lets the guard itself break the agent loop.)Ú_arm_fallback_restartc               ó$   € V ^8„  d   QhRRRRRR/# )r   r#   r"   r¬   r«   r   r¨   r   )r   s   "r   r   Ú+nous_rate_limit_guard.<locals>.__annotate__é   s#   € ÷ 
ñ 
˜ð 
Ð&>ð 
ÐJ^ñ 
r   c                ó$   <€ \        V SSSVR 7      # ))r#   r©   rG   rª   r¬   )r¨   )r#   r¬   r©   rª   rG   s   &&€€€r   rP   Ú'nous_rate_limit_guard.<locals>._verdicté   s   ø€ Ü#ØÐ0DÐR]Ø!5¸fô
ð 	
r   Únous)Úapply_model_switch)Únous_rate_limit_remainingÚformat_remaining)Ú	anon_auth)Ú	anonymous)Úresetz7Your Nous account has hit its rate limit; it resets in Ú.u   â�³ z Trying fallback...r   r   r�   z

Únous_rate_limitr‘   Ú	api_callsÚ	completedFÚfailedTÚerrorÚ	free_tierÚkindÚrate_limitedÚmessager€   rS   )rœ   r°   r;   Úhermes_cli.anon_authr¶   Ú	ExceptionÚagent.nous_rate_guardr·   r¸   Ú
hermes_clir¹   Úis_anonymous_agentÚFREE_TIER_RATE_LIMIT_CHATr   Úfriendly_waitÚ_buffer_vprintÚ_buffer_diagnostic_statusÚ_try_activate_fallbackÚ_flush_status_bufferr£   r   r   ÚFREE_TIER_RATE_LIMIT_CARDr   Ú
rate_limitÚvalue)r   rF   r®   r‘   r’   r©   rG   rª   rH   r°   rP   r¶   r·   Ú_fmt_nous_remainingr¹   Ú
_anonymousÚ_nous_remainingr»   Ú	_nous_msgs   &$$$$ddd$          r   Únous_rate_limit_guardrØ   á   s&  ú€ õ >÷
ô 
ð ‡~�~˜Õð	Ý?Ù˜uÔ%ð(	÷õ -Ø"×5Ñ5°eÓ<ˆJÙ7À*ÔMˆOØÓ*¨ÀÕ/BÙ+¨OÓ<�ßØ )× CÑ C× JÑ JØ'×5Ñ5°oÓFð !Kó !H‘Ið #ZÐZ_ÐY`Ð`aÐ b�IØ×$Ñ$ t¨I¨;Ð6IÐ%JÔKØ×/Ñ/°$°y°kÐ0BÔCØ×/Ñ/×1Ò1Ù+@ØÐ-AÀ6ó,KÐ(à"#�KØ+,Ð(Ù# GÓ,Ð,à×*Ñ*Ô,Ø×&Ñ& xÔFñ   ¬-Ø$¿Z¨¨i¨[Ñ'9Ø.2°9°+¸TÄ)ÐL]ÓB^ÐA_Ð,`Ø Ø Ø Ø˜dØ˜Yð9÷ NXð $ f¨n¸iÈ×IlÑIl×IsÑIsØ'×5Ñ5°oÓFð Jtó JHð &Iñ JØ]_ð9ô "×,Ñ,×2Ñ2°Dó+:ó ;ð ;ñ �MÓ"Ð"øôW ô 	Úð	ûôR ô 	Ùð	ús>   ªF= ¹AG Á>A0G Ã/G ÄAG ÅAG Æ=GÇGÇGÇG)#r+   Ú
__future__r   Ú
contextlibr   Údataclassesr   Úloggingr�   Útypingr   r   r   Úagent.error_classifierr   Ú(agent.agent_runtime_helpers_placeholdersr	   Úagent.message_metadatar
   Úagent.repetition_guardr   r   Úagent.turn_failure_copyr   r   Ú	getLoggerÚloggerr   r!   r@   rŒ   rŽ   r¦   r¨   rØ   r   r   r   Ú<module>rå      s©   ðñõ #å "Ý !Û Û ß &Ñ &å 1Ý UÝ 1ß Uß <à	×	Ò	Ð4Ó	5€õ$ð ÷ð ó ðõõ*Y#ðx ÷ð ó ðõ(WðV ÷,ð ,ó ð,÷?#r   