+
    iHàj>  ã                  óz   € R t ^ RIHt ^ RIt^ RIt^ RIHt ]P                  ! R4      tR R lt	R R lt
R	 R
 ltR# )z²Pre-turn dead-connection sweep over an agent's OpenAI client pools (primary + cached request
client). Sibling of ``agent.agent_runtime_helpers``, which owns the pool-socket walk.)ÚannotationsN)ÚAnyÚ	run_agentc               ó   € V ^8„  d   QhRR/# ©é   ÚreturnÚbool© )Úformats   "ÚQ/home/ubuntu/.hermes/hermes-agent/agent/agent_runtime_helpers_dead_connections.pyÚ__annotate__r      s   € ÷ #ñ #˜Tñ #ó    c           	     óR  € ^ RI p V P                  R4       V P                  ^VP                  VP                  ,          4      R8H  \
        P                  ! \        4      ;_uu_ 4        V P                  R4       RRR4       #   + '       g   i     # ; i  \         dU     \
        P                  ! \        4      ;_uu_ 4        T P                  R4       RRR4       R#   + '       g   i     R# ; i\         dU     \
        P                  ! \        4      ;_uu_ 4        T P                  R4       RRR4       R#   + '       g   i     R# ; ii ; i  \
        P                  ! \        4      ;_uu_ 4        T P                  R4       RRR4       i   + '       g   i     i ; i; i)z2Probe socket health with a non-blocking recv peek.NFr   T)	ÚsocketÚsetblockingÚrecvÚMSG_PEEKÚMSG_DONTWAITÚ
contextlibÚsuppressÚOSErrorÚBlockingIOError)ÚsockÚ_sockets   & r   Ú_socket_is_deadr      s  € ãð	#Ø×Ñ˜ÔØ�y‰y˜˜G×,Ñ,¨w×/CÑ/CÕCÓDÈÑKô × Ò ¤×)Õ)Ø×Ñ˜TÔ"÷ *×)×)Ð)ûô ô Øô × Ò ¤×)Õ)Ø×Ñ˜TÔ"÷ *×)×)Ò)úô ô Øä× Ò ¤×)Õ)Ø×Ñ˜TÔ"÷ *×)×)Ò)úðûô × Ò ¤×)Õ)Ø×Ñ˜TÔ"÷ *×)×)Ð)ýsr   †A B Á(BÂBÂEÂ!E ÃC Ã C1	Ã4EÃ=EÃ>E Ä!D=Ä=E	ÅEÅE Å#F&Å8F	Æ
	F&ÆF#ÆF&c               ó    € V ^8„  d   QhRRRR/# )r   Úclientr   r   Úintr
   )r   s   "r   r   r      s   € ÷ Rñ R Sð R¨Sñ Rr   c                ó>   € ^ RI Hp \        R V! V 4       4       4      # )zUReturn the number of sockets in ``client``'s pool that have reached EOF (CLOSE-WAIT).)Ú_iter_pool_socketsc              3  óL   "  € T F  p\        V4      '       g   K  ^x € K  	  R# 5i)é   N)r   )Ú.0r   s   & r   Ú	<genexpr>Ú+_count_dead_pool_sockets.<locals>.<genexpr>   s   é € ÐQÑ7�T¼?È4×;P�qŠqÓ7ùs   ‚$š
$)Úagent.agent_runtime_helpersr    Úsum)r   r    s   & r   Ú_count_dead_pool_socketsr(      s   € å>ÜÑQÑ/°Ô7ÓQÓQÐQr   c               ó   € V ^8„  d   QhRR/# r   r
   )r   s   "r   r   r   "   s   € ÷ ;ñ ; tñ ;r   c                ó(  € \        V RR4      p\        V RR4      p\        V\        4      '       d   VP                  R4      MRp. pVe   VP	                  RV34       Ve   W1Jd   VP	                  RV34       V'       g   R# . pV F.  w  rg \        V4      pV'       d   VP	                  WgV34       K.  K0  	  V'       g   R# \        R
 V 4       4      p	RP                  R V 4       4      p
\        P                  RV	V
4       V F4  w  rkp VR8X  d   V P                  RR7       K"  V P                  RR7       K6  	  R#   \         d    \        P                  RTRR	7        Kß  i ; i  \         d    \        P                  RTRR	7        KŠ  i ; i)a¼  Detect and clean up dead TCP connections owned by an agent.

Long-lived chat-completion turns use a cached per-request OpenAI client
for sequential calls, while ``agent.client`` remains the primary client
used for runtime rebuilds and provider management.  Inspect both pools:
a provider FIN on the cached client otherwise stays in CLOSE-WAIT until
session teardown and can be handed back to the next request.

Dead sockets on the primary client rebuild that client.  Dead sockets on
the request cache go through the cache's ownership-aware teardown hook so
an in-flight worker is aborted safely and an idle client is closed by its
owner.  Returns True if dead connections were found and cleaned up.
r   NÚ_request_client_cacheÚprimaryÚrequestFz)Dead connection check error for %s clientT)Úexc_infoc              3  ó*   "  € T F	  w  rq3x € K  	  R # 5i©Nr
   )r#   Ú_labelÚ_clientÚcounts   &   r   r$   Ú+cleanup_dead_connections.<locals>.<genexpr>J   s   é € ÐF¹Ñ5˜v°”U»ùó   ‚z, c              3  ó*   "  € T F	  w  rq1x € K  	  R # 5ir0   r
   )r#   Úlabelr2   Ú_counts   &   r   r$   r4   K   s   é € ÐH¹<Ñ!7 °”u»<ùr5   u@   Found %d dead connection(s) in %s client pool(s) â€” cleaning upÚdead_connection_cleanup)Úreasonz+Dead connection cleanup error for %s client)ÚgetattrÚ
isinstanceÚdictÚgetÚappendr(   Ú	ExceptionÚloggerÚdebugr'   ÚjoinÚwarningÚ_replace_primary_openai_clientÚ#_close_cached_request_openai_client)Úagentr,   ÚcacheÚrequest_clientÚclientsÚdead_clientsr7   r   Ú
dead_countÚ
total_deadÚlabelsr2   r8   s   &            r   Úcleanup_dead_connectionsrO   "   s‘  € ô �e˜X tÓ,€GÜ�EÐ2°DÓ9€EÜ,6°u¼d×,CÒ,C�U—Y‘Y˜xÔ(È€Nà%'€GØÒØ�‰˜	 7Ð+Ô,ØÒ! nÓ&CØ�‰˜	 >Ð2Ô3ßÙà/1€LÛ ‰ˆð	\Ü1°&Ó9ˆJßØ×#Ñ# U°JÐ$?Ö@ñ ñ !÷ ÙäÑF¹ÓFÓF€JØ�Y‰YÑH¹<ÓHÓH€FÜ
‡N�NØJØØôó #/Ñˆ˜ð		^Ø˜	Ô!Ø×4Ñ4Ð<UÐ4ÖVð ×9Ñ9ÐAZÐ9Ö[ñ #/ñ øô7 ô 	\ô �L‰LÐDÀeÐVZˆL×[ð	\ûô0 ô 	^ä�L‰LÐFÈÐX\ˆL×]ð	^ús*   Â&E ÄE*Ä(E*Å #E'Å&E'Å*#FÆF)Ú__doc__Ú
__future__r   r   ÚloggingÚtypingr   Ú	getLoggerrA   r   r(   rO   r
   r   r   Ú<module>rU      s9   ðñYõ #ã Û Ý à	×	Ò	˜;Ó	'€õ#õR÷;r   