Ë
    ãß²j   ã            	       ó–  — d Z ddlZddlZddlZddlZddlmZ ddlmZ  ej                  d«      Z
eeej                  dœdedefd	„«       Zed
fdedededefd„Zdefd„ZdZ ej&                  d«      Zdedefd„Z eh d£«      Zeh d£z  Zdedefd„Zd#dedefd„Zdedefd„Zdefd„Zdedefd „Zd$dededefd!„Zdededefd"„Z y)%aƒ  Terminal backend configuration: scope-aware TERMINAL_* reads, env-var parsing,
container-cwd sanity checks, plugin-backend classification, and the ``_quiet``
best-effort block shared by the terminal_tool_* modules.

Split out of ``tools/terminal_tool.py``; every public/patched name is re-imported there,
so ``tools.terminal_tool.<name>`` keeps resolving (and monkeypatching) as before.
é    N)Úcontextmanager)ÚAnyztools.terminal_tool)ÚexcÚlevelÚlabelr   c             '   ó`   K  — 	 d–— y# |$ r t        j                  || g|¢­ddiŽ Y yw xY w­w)z¹Best-effort block: swallow *exc*, log *label* (``%``-formatted with *args*)
    at *level* with the traceback. For janitor/advisory work that must never
    take the terminal tool down.NÚexc_infoT)ÚloggerÚlog)r   r   r   Úargss       ú?/home/ubuntu/.hermes/hermes-agent/tools/terminal_tool_config.pyÚ_quietr      s4   è ø€ ð
7ÜøØò 7Ü�
‰
�5˜%Ð6 $Ò6°Ô6ð7üs   ‚.„	 ˆ.‰+¨.ª+«.ÚintegerÚnameÚdefaultÚ	converterÚ
type_labelc           
      óÞ   — | j                  d«      rt        | |«      nt        j                  | |«      }	  ||«      S # t        t
        j                  f$ r t	        d| › d|›d|› d�«      ‚w xY w)zÃParse an env var with *converter*, raising a clear ValueError on bad
    values (e.g. TERMINAL_TIMEOUT=5m) instead of an opaque crash. TERMINAL_*
    names are read scope-aware via :func:`_tenv`.Ú	TERMINAL_zInvalid value for z: z (expected z1). Check ~/.hermes/.env or environment variables.)Ú
startswithÚ_tenvÚosÚgetenvÚ
ValueErrorÚjsonÚJSONDecodeError)r   r   r   r   Úraws        r   Ú_parse_env_varr      s}   € ð #'§/¡/°+Ô">Œ%��gÔ
ÄBÇIÁIÈdÐT[ÓD\€Cð
Ù˜‹~ÐøÜœ×,Ñ,Ð-ò 
ÜØ    b¨¨¨{¸:¸,ð G=ð >ó
ð 	
ð
ús	   µ= ½/A,Úreturnc                  ó®   — 	 t        j                  «       S # t        t        f$ r/ t	        d«      xs t         j
                  j                  d«      cY S w xY w)zÃ``os.getcwd()`` tolerant of a deleted cwd (FileNotFoundError) or a macOS
    TCC-protected one without Full Disk Access (PermissionError); falls back
    to TERMINAL_CWD, then the home directory.ÚTERMINAL_CWDÚ~)r   ÚgetcwdÚFileNotFoundErrorÚPermissionErrorr   ÚpathÚ
expanduser© ó    r   Ú_safe_getcwdr*   -   sI   € ð@Ü�y‰y‹{ÐøÜœÐ/ò @Ü�^Ó$Ò?¬¯©×(:Ñ(:¸3Ó(?Ò?ð@ús   ‚ –;AÁA)z/Users/z/home/z^[A-Za-z]:[\\/]r&   c                 ól   — | j                  t        «      xs t        t        j	                  | «      «      S )N)r   Ú_HOST_CWD_PREFIXESÚboolÚ_WINDOWS_DRIVE_REÚmatch)r&   s    r   Ú_is_host_cwdr0   =   s(   € Ø�?‰?Ô-Ó.ÒU´$Ô7H×7NÑ7NÈtÓ7TÓ2UÐUr)   >   ÚmodalÚdockerÚdaytonaÚsingularityÚvercel_sandbox>   ÚsshÚlocalÚmanaged_modalÚenv_typeÚfn_namec                 óp   — | r| t         v r|S 	 ddlm}  t        ||«      | g|¢­Ž S # t        $ r |cY S w xY w)u  Call ``agent.terminal_env_registry.<fn_name>(env_type, *args)`` for a
    plugin backend. Fail-soft: *default* for built-in/empty backends, when the
    registry is unavailable, or when the provider raises â€” a misbehaving plugin
    must never take the terminal tool down.r   N)Ú_BUILTIN_BACKENDSÚagent.terminal_env_registryÚterminal_env_registryÚgetattrÚ	Exception)r9   r:   r   r   Úregs        r   Ú_plugin_registry_lookuprB   D   sI   € ñ
 �xÔ#4Ñ4ØˆðÝ1à$Œw�s˜GÓ$ XÐ5°Ò5Ð5øÜò ØŠðús   Ž' §5´5Úattrc                 ó    — t        | d|||«      S )zJClassification flag of a plugin-registered backend (fail-soft, see above).Úprovider_flag©rB   )r9   rC   r   s      r   Ú_plugin_env_flagrG   S   s   € ä" 8¨_¸gÀtÈWÓUÐUr)   c                 ó.   — | t         v xs t        | d«      S )zLTrue for built-in container backends and plugins declaring ``is_container``.Úis_container)Ú_CONTAINER_BACKENDSrG   ©r9   s    r   Ú_is_container_backendrL   X   s   € àÔ*Ð*ÒXÔ.>¸xÈÓ.XÐXr)   c                 ó   — t        | dd«      S )z>Return the registered plugin provider for *env_type*, or None.Úget_providerNrF   rK   s    r   Ú_get_plugin_env_providerrO   ]   s   € ä" 8¨^¸TÓBÐBr)   Úcwdc                 óv   — t        | «      xr- t        | «      xs  t        j                  j	                  | «       S )a  True if *cwd* is a host or relative path that can't be a container
    workdir: ``docker run -w`` needs an absolute in-sandbox path, otherwise the
    container fails to start (exit 125). Windows drive paths aren't ``isabs``
    on POSIX, so they're caught by the prefix check.)r-   r0   r   r&   Úisabs)rP   s    r   Ú_is_unusable_container_cwdrS   b   s-   € ô
 �‹9ÒFœ, sÓ+ÒE´2·7±7·=±=ÀÓ3EÐ/EÐFr)   c                 ó    — ddl m}  || |«      S )a*  Scope-aware read of a ``TERMINAL_*`` variable. Every terminal setting
    must go through this: under gateway multiplexing the active profile's
    config arrives via a per-turn scope, and a raw ``os.getenv`` would read
    whatever a previous turn pinned into the process env (cross-profile leak).r   )Úterminal_env)Útools.terminal_scoperU   )r   r   rU   s      r   r   r   j   s   € õ
 2á˜˜gÓ&Ð&r)   c                 ó:   — t        | |«      j                  «       dv S )zGScope-aware boolean ``TERMINAL_*`` read: true/1/yes (case-insensitive).>   Ú1ÚyesÚtrue)r   Úlower)r   r   s     r   Ú
_tenv_boolr\   t   s   € ä��wÓ×%Ñ%Ó'Ð+?Ð?Ð?r)   )F)Ú )!Ú__doc__Úloggingr   r   ÚreÚ
contextlibr   Útypingr   Ú	getLoggerr
   r@   ÚDEBUGÚstrÚintr   r   r*   r,   Úcompiler.   r-   r0   Ú	frozensetrJ   r<   rB   rG   rL   rO   rS   r   r\   r(   r)   r   ú<module>ri      s{  ðñó Û Û 	Û 	Ý %Ý ð 
ˆ×	Ñ	Ð0Ó	1€ð Ø"+¸'¿-¹-ò 7�#ð 7°Cò 7ó ð7ð >AÐT]ñ 
˜ð 
 sð 
°sð 
Ècó 
ð@�có @ð +Ð Ø�B—J‘JÐ1Ó2Ð ðV�sð V˜tó Vñ  Ò _Ó`Ð Ø'Ò*KÑKÐ ð cð °Có ñV˜sð V¨#ó Vð
Y Cð Y¨Dó Yð
C só Cð
G Cð G¨Dó Gñ'�ð '˜cð '¨3ó 'ð@�Sð @ 3ð @¨4ô @r)   