§
    ÷žyjG  ã                  ó¾   — U d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	 dgZ
 ej        ¦   «         Zi Zded<    G d„ d	¦  «        Zdd„Zej        dd„¦   «         ZdS )u!  Thread-scoped stdout/stderr silencing for background worker threads.

``contextlib.redirect_stdout``/``redirect_stderr`` reassign the *process-global*
``sys.stdout``/``sys.stderr``.  When a daemon worker thread (e.g. the background
memory/skill review) wraps its whole body in those context managers, every other
thread in the process â€” including a gateway's asyncio event-loop thread driving a
Telegram long-poll â€” sees ``sys.stdout``/``sys.stderr`` pointing at ``devnull``
for the full duration.  Any bare ``print`` / ``sys.stderr.write`` from those other
threads is silently lost during that window (see issue #55769 / #55925).

This module installs a thin proxy as ``sys.stdout``/``sys.stderr`` that routes
writes per-thread: threads registered as "silenced" go to a sink; every other
thread passes through to the *original* stream.  The proxy is installed once,
idempotently, and is never uninstalled (uninstalling would race other threads
mid-write), so the only observable effect for unregistered threads is one extra
attribute lookup per write.
é    )ÚannotationsN)ÚIteratorÚTextIOÚthread_scoped_silencez!dict[str, '_ThreadRoutingStream']Ú
_installedc                  óX   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zd„ Zd„ Z	d„ Z
dd„Zd„ Zd„ ZdS )Ú_ThreadRoutingStreamaÇ  A ``sys.stdout``/``sys.stderr`` stand-in that routes writes per-thread.

    Threads whose ident is in ``_silenced`` write to ``_sink``; all other
    threads write to ``_passthrough`` (the original stream captured at install
    time).  Attribute access for anything other than the methods we override
    is delegated to the *current* target so things like ``.encoding`` /
    ``.fileno()`` behave like the underlying stream for the calling thread.
    Úpassthroughr   ÚsinkÚreturnÚNonec                ó`   — || _         || _        i | _        t          j        ¦   «         | _        d S ©N)Ú_passthroughÚ_sinkÚ	_silencedÚ	threadingÚLockÚ_lock)Úselfr
   r   s      ú@/home/ragecks/.hermes/hermes-agent/agent/thread_scoped_output.pyÚ__init__z_ThreadRoutingStream.__init__-   s-   € Ø'ˆÔØˆŒ
ð *,ˆŒÝ”^Ñ%Ô%ˆŒ
ˆ
ˆ
ó    c                ó~   — | j                              t          j        ¦   «         d¦  «        dk    r| j        S | j        S ©Nr   )r   Úgetr   Ú	get_identr   r   ©r   s    r   Ú_targetz_ThreadRoutingStream._target6   s9   € ØŒ>×Ò�iÔ1Ñ3Ô3°QÑ7Ô7¸!Ò;Ð;Ø”:ÐØÔ Ð r   ÚidentÚintc                ó’   — | j         5  | j                             |d¦  «        dz   | j        |<   d d d ¦  «         d S # 1 swxY w Y   d S ©Nr   é   )r   r   r   )r   r    s     r   Úsilencez_ThreadRoutingStream.silence<   s¥   € ØŒZð 	Eð 	EØ$(¤N×$6Ò$6°u¸aÑ$@Ô$@À1Ñ$DˆDŒN˜5Ñ!ð	Eð 	Eð 	Eñ 	Eô 	Eð 	Eð 	Eð 	Eð 	Eð 	Eð 	Eð 	Eøøøð 	Eð 	Eð 	Eð 	Eð 	Eð 	Es   ˆ'<¼A ÁA c                óÚ   — | j         5  | j                             |d¦  «        dz
  }|dk    r|| j        |<   n| j                             |d ¦  «         d d d ¦  «         d S # 1 swxY w Y   d S r#   )r   r   r   Úpop)r   r    Údepths      r   Ú	unsilencez_ThreadRoutingStream.unsilence@   sº   € ØŒZð 	0ð 	0Ø”N×&Ò& u¨aÑ0Ô0°1Ñ4ˆEØ�qŠyˆyØ(-�”˜uÑ%Ð%à”×"Ò" 5¨$Ñ/Ô/Ð/ð	0ð 	0ð 	0ñ 	0ô 	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	0øøøð 	0ð 	0ð 	0ð 	0ð 	0ð 	0s   ˆAA Á A$Á'A$c                ó¾   — 	 |                       ¦   «                              |¦  «        S # t          $ r) t          |t          ¦  «        rt          |¦  «        ndcY S w xY wr   )r   ÚwriteÚ	ExceptionÚ
isinstanceÚstrÚlen)r   Údatas     r   r+   z_ThreadRoutingStream.writeI   sc   € ð	=Ø—<’<‘>”>×'Ò'¨Ñ-Ô-Ð-øÝð 	=ð 	=ð 	=Ý *¨4µÑ 5Ô 5Ð<•3�t‘9”9�9¸1Ð<Ð<Ð<ð	=øøøs   ‚&) ©0AÁAc                ór   — 	 |                       ¦   «                              ¦   «         S # t          $ r Y d S w xY wr   )r   Úflushr,   r   s    r   r2   z_ThreadRoutingStream.flushO   sC   € ð	Ø—<’<‘>”>×'Ò'Ñ)Ô)Ð)øÝð 	ð 	ð 	Ø�4�4ð	øøøs   ‚%( ¨
6µ6c                óx   — |                       ¦   «         }	 |                     |¦  «        S # t          $ r Y d S w xY wr   )r   Ú
writelinesr,   )r   ÚlinesÚtargets      r   r4   z_ThreadRoutingStream.writelinesU   sJ   € Ø—’‘”ˆð	Ø×$Ò$ UÑ+Ô+Ð+øÝð 	ð 	ð 	Ø�4�4ð	øøøs   –+ «
9¸9Úboolc                óŒ   — 	 t          |                      ¦   «                              ¦   «         ¦  «        S # t          $ r Y dS w xY w)NF)r7   r   Úisattyr,   r   s    r   r9   z_ThreadRoutingStream.isatty\   sK   € ð	Ý˜Ÿš™œ×-Ò-Ñ/Ô/Ñ0Ô0Ð0øÝð 	ð 	ð 	Ø�5�5ð	øøøs   ‚25 µ
AÁAc                óN   — |                       ¦   «                              ¦   «         S r   )r   Úfilenor   s    r   r;   z_ThreadRoutingStream.filenob   s   € Ø�|Š|‰~Œ~×$Ò$Ñ&Ô&Ð&r   c                óF   — t          |                      ¦   «         |¦  «        S r   )Úgetattrr   )r   Únames     r   Ú__getattr__z _ThreadRoutingStream.__getattr__e   s   € õ �t—|’|‘~”~ tÑ,Ô,Ð,r   N)r
   r   r   r   r   r   )r   r   )r    r!   r   r   )r   r7   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r)   r+   r2   r4   r9   r;   r?   © r   r   r	   r	   #   sÑ   € € € € € ðð ð&ð &ð &ð &ð!ð !ð !ð !ðEð Eð Eð Eð0ð 0ð 0ð 0ð=ð =ð =ðð ð ðð ð ðð ð ð ð'ð 'ð 'ð-ð -ð -ð -ð -r   r	   Úattrr.   r
   r   r   ú'_ThreadRoutingStream'c                ót  — t           5  t                               | ¦  «        }t          t          | d¦  «        }|�||u r|cddd¦  «         S |�|n|}t          t          j        dd¬¦  «        }t          ||¦  «        }t          t          | |¦  «         |t          | <   |cddd¦  «         S # 1 swxY w Y   dS )zGInstall (idempotently) a routing proxy as ``sys.<attr>`` and return it.NÚwzutf-8)Úencoding)
Ú_install_lockr   r   r=   ÚsysÚopenÚosÚdevnullr	   Úsetattr)rE   r
   ÚproxyÚcurrentr   s        r   Ú_ensure_installedrR   k   s  € å	ð ð Ý—’˜tÑ$Ô$ˆÝ�#˜t TÑ*Ô*ˆØÐ ¨EÐ!1Ð!1Øð	ð ð ð ñ ô ð ð ð ")Ð!4�g�g¸+ˆÝ•B”J ¨gÐ6Ñ6Ô6ˆÝ$ [°$Ñ7Ô7ˆÝ•�T˜5Ñ!Ô!Ð!Ø �
�4ÑØðð ð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð ð s   ˆ8B-ÁAB-Â-B1Â4B1úIterator[None]c               #  óØ  K  — t          j        ¦   «         } t          dt          j        pt          j        ¦  «        }t          dt          j        pt          j        ¦  «        }|                     | ¦  «         |                     | ¦  «         	 dV — | 	                    | ¦  «         | 	                    | ¦  «         dS # | 	                    | ¦  «         | 	                    | ¦  «         w xY w)a-  Silence ``stdout``/``stderr`` for the *current thread only*.

    Other threads keep writing to the real streams.  Use this around a worker
    thread's body instead of ``contextlib.redirect_stdout(devnull)`` when the
    process is multi-threaded and another thread must keep its console output.
    ÚstdoutÚstderrN)
r   r   rR   rK   Ú
__stdout__rU   Ú
__stderr__rV   r%   r)   )r    Ú	out_proxyÚ	err_proxys      r   r   r   }   sÛ   è è € õ ÔÑ!Ô!€EÝ! (­C¬NÐ,H½c¼jÑIÔI€IÝ! (­C¬NÐ,H½c¼jÑIÔI€IØ×Ò�eÑÔÐØ×Ò�eÑÔÐð#Øˆˆˆà×Ò˜EÑ"Ô"Ð"Ø×Ò˜EÑ"Ô"Ð"Ð"Ð"øð 	×Ò˜EÑ"Ô"Ð"Ø×Ò˜EÑ"Ô"Ð"Ð"øøøs   ÂB= Â=,C))rE   r.   r
   r   r   rF   )r   rS   )rC   Ú
__future__r   Ú
contextlibrM   rK   r   Útypingr   r   Ú__all__r   rJ   r   Ú__annotations__r	   rR   Úcontextmanagerr   rD   r   r   ú<module>ra      s  ððð ð ð$ #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø 	€	€	€	Ø 
€
€
€
Ø Ð Ð Ð Ø #Ð #Ð #Ð #Ð #Ð #Ð #Ð #à"Ð
#€à�	”Ñ Ô €ð 13€
Ð 2Ð 2Ð 2Ñ 2ðE-ð E-ð E-ð E-ð E-ñ E-ô E-ð E-ðPð ð ð ð$ Ôð#ð #ð #ñ Ôð#ð #ð #r   