Ë
    ãß²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k(  Z
 ej                  d«      Zd„ Zded	ed
efd„Zdededed
eedf   fd„Zded
edz  fd„Zded
ee   fd„Zded
dfd„Zded
dfd„Zy)u+  Hermes-owned PYTHONPATH stripping for child processes. Launchers prepend the repo
root and the Hermes venv's site-packages so the backend can ``import tools``; leaked
into a child Python of a DIFFERENT version they load the backend's C extensions and
crash. Only entries proven Hermes-owned by *path provenance* are removed â€” never by a
cross-version heuristic. Module state (``_hermes_repo_root_aliases``, ``_in_venv``,
``_hermes_site_packages``) lives in ``tools.environments.local`` (via :func:`_state`)
so tests monkeypatching it there keep working.é    N©ÚPath)Ú_ACTIVE_VENV_MARKER_VARSÚWindowsztools.environments.localc                  ó   — ddl m}  | S )zEReturn the ``tools.environments.local`` module (owner of the caches).r   ©Úlocal)Útools.environmentsr	   r   s    úH/home/ubuntu/.hermes/hermes-agent/tools/environments/local_pythonpath.pyÚ_stater      s
   € å(à€Ló    ÚleftÚrightÚreturnc                 óô   — | j                   D �cg c]!  }t        j                  j                  |«      ‘Œ# c}|j                   D �cg c]!  }t        j                  j                  |«      ‘Œ# c}k(  S c c}w c c}w )z;Compare path spellings with host filesystem case semantics.)ÚpartsÚosÚpathÚnormcase)r   r   Úps      r   Ú
_same_pathr      sS   € à)-¯©Ö4 AŒB�G‰G×Ñ˜QÕÒ4ÐV[×VaÑVaÖ8bÐQR¼¿¹×9IÑ9IÈ!Õ9LÒ8bÑbÐbùÒ4ùÒ8bs   �&A0Á&A5Úresolved_rootÚlexical_rootÚconfigured_home.c           
      ó‚  ‡
— | |g}|g}|j                   j                  dk(  r%|j                  |j                   j                   «       |D ]É  }	 |j                  «       }t        j
                  j                  t        |«      «      }t        j
                  j                  |t        j
                  j                  t        | «      «      g«      |k(  rD|j                  |t        j
                  j                  t        | «      t        |«      «      z  «       ŒË |D ]H  }|| j                  z  }	 |j                  d¬«      | j                  d¬«      k(  r|j                  |«       ŒJ g }	|D ](  Š
t        ˆ
fd„|	D «       «      rŒ|	j                  ‰
«       Œ* t        |	«      S # t        t        f$ r Y �Œew xY w# t        $ r Y Œ¦w xY w)aÅ  Exact repo-root spellings emitted by Hermes launchers. Mirrors
    ``gateway_windows._preserve_hermes_home_path`` (physical path under the resolved
    HERMES_HOME -> configured spelling) so a junction-backed install matches without
    treating arbitrary HERMES_HOME descendants as Hermes-owned. A repo-level junction
    (possibly cross-drive) is accepted only when a strict resolve proves
    <root>/<repo dirname> is the physical root (fail-closed).ÚprofilesT)Ústrictc              3   ó6   •K  — | ]  }t        ‰|«      –— Œ y ­w©N©r   )Ú.0ÚexistingÚ	candidates     €r   ú	<genexpr>z2_build_hermes_repo_root_aliases.<locals>.<genexpr>D   s   øè ø€ ÒK°x”:˜i¨×2ÑKùó   ƒ)ÚparentÚnameÚappendÚresolver   r   r   ÚstrÚ
commonpathÚrelpathÚOSErrorÚ
ValueErrorÚanyÚtuple)r   r   r   Ú
candidatesÚhome_candidatesÚhomeÚresolved_homeÚhome_keyÚrepo_candidateÚaliasesr#   s             @r   Ú_build_hermes_repo_root_aliasesr8   "   s   ø€ ð   Ð.€Jð 'Ð'€OØ×Ñ×"Ñ" jÒ0Ø×Ñ˜×5Ñ5×<Ñ<Ô=Øò ˆð	Ø ŸL™L›NˆMÜ—w‘w×'Ñ'¬¨MÓ(:Ó;ˆHÜ�w‰w×!Ñ! 8¬R¯W©W×-=Ñ-=¼cÀ-Ó>PÓ-QÐ"RÓSÐW_Ò_Ø×!Ñ! $¬¯©¯©¼¸]Ó9KÌSÐQ^ÓM_Ó)`Ñ"`Ôaøðð  ò ˆØ × 2Ñ 2Ñ2ˆð	Ø×%Ñ%¨TÐ%Ó2°m×6KÑ6KÐSWÐ6KÓ6XÒXØ×!Ñ! .Ô1øð	ð €GØò &ˆ	ÜÓKÀ7ÔKÕKØ�N‰N˜9Õ%ð&ô �‹>Ðøô œÐ$ò 	Úð	ûô ò 	Ùð	ús%   ÁCFÄ*6F2ÆF/Æ.F/Æ2	F>Æ=F>Úenvc                 óþ   ‡— t        | j                  d«      xs d«      Š| j                  d«      r&t        ˆfd„t        «       j                  D «       «      sy	 ‰dz  j                  «       r‰S dS # t        $ r Y yw xY w)zõProducer-owned runtime venv identified by VIRTUAL_ENV, or None. The variable
    alone is not provenance (users carry unrelated venvs): require the legacy Windows
    base-Python producer's exact ``<repo>/venv`` layout AND a real ``pyvenv.cfg``.ÚVIRTUAL_ENVÚ c              3   ó<   •K  — | ]  }t        ‰|d z  «      –— Œ y­w)ÚvenvNr    )r!   Úrootr#   s     €r   r$   z*_validated_runtime_venv.<locals>.<genexpr>N   s#   øè ø€ ò -aØ59ŒJ�y $¨¡-×0ñ-aùs   ƒNz
pyvenv.cfg)r   Úgetr/   r   Ú_hermes_repo_root_aliasesÚis_filer-   )r9   r#   s    @r   Ú_validated_runtime_venvrC   I   s}   ø€ ô �S—W‘W˜]Ó+Ò1¨rÓ2€IØ�7‰7�=Ô!¬ó -aÜ=C»X×=_Ñ=_ô-aô *aàðØ&¨Ñ5×>Ñ>Ô@ˆyÐJÀdÐJøÜò Ùðús   ÁA0 Á.A0 Á0	A<Á;A<c                 ó†  ‡— t        «       }|j                  €Ëg }|j                  r­	 ddl}|j	                  d„ |j                  «       D «       «       |s€dt        j                  d   › dt        j                  d   › �}|j                  t        rt        t        j                  «      dz  dz  n!t        t        j                  «      d	z  |z  dz  «       t        |«      |_        t        |j                  «      }t        | «      }|�-|dz  dz  Št        ˆfd
„|D «       «      s|j                  ‰«       |S # t        $ r Y Œîw xY w)z×Exact site-packages dirs owned by the Hermes runtime (cached):
    ``site.getsitepackages()`` with a ``sys.prefix`` fallback, plus a validated
    Windows base-interpreter launch's ``VIRTUAL_ENV/Lib/site-packages``.Nr   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   r   )r!   Úsps     r   r$   z,_get_hermes_site_packages.<locals>.<genexpr>a   s   è ø€ ÒH¨2œd 2ŸhÑHùs   ‚Úpythonú.é   ÚLibzsite-packagesÚlibc              3   ó6   •K  — | ]  }t        ‰|«      –— Œ y ­wr   r    )r!   r"   Úruntime_site_packagess     €r   r$   z,_get_hermes_site_packages.<locals>.<genexpr>n   s   øè ø€ ÒVÀ8”:Ð3°X×>ÑVùr%   )r   Ú_hermes_site_packagesÚ_in_venvÚsiteÚextendÚgetsitepackagesÚ	ExceptionÚsysÚversion_infor(   Ú_IS_WINDOWSr   ÚprefixÚlistrC   r/   )r9   r	   ÚresultrP   ÚpyverÚruntime_venvrM   s         @r   Ú_get_hermes_site_packagesr\   W   s%  ø€ ô ‹H€EØ×"Ñ"Ð*ØˆØ�>Š>ðÛØ—‘ÑH°×1EÑ1EÓ1GÔHÔHñ Ø ¤×!1Ñ!1°!Ñ!4Ð 5°Q´s×7GÑ7GÈÑ7JÐ6KÐL�Ø—‘ÍKœd¤3§:¡:Ó.°Ñ6¸ÒHÜ#'¬¯
©
Ó#3°eÑ#;¸eÑ#CÀoÑ#UôWä&*¨6£lˆÔ#Ü�%×-Ñ-Ó.€Fä*¨3Ó/€LØÐØ ,¨uÑ 4°Ñ FÐÜÓVÈvÔVÔVØ�M‰MÐ/Ô0Ø€Møô ò Ùðús   §*D4 Ä4	E Ä?E c                 óT   — t        | «       t        D ]  }| j                  |d«       Œ y)zúStrip Hermes-owned PYTHONPATH entries, then the runtime marker vars. Order is
    load-bearing: PYTHONPATH filtering runs BEFORE the markers go so a validated Windows
    base-interpreter launch (VIRTUAL_ENV -> <repo>/venv) can still prove ownership.N)Ú_strip_hermes_owned_pythonpathr   Úpop)r9   Ú_markers     r   Ú2_strip_hermes_owned_pythonpath_and_runtime_markersra   s   s)   € ô # 3Ô'Ü+ò ˆØ�‰�˜Õñr   c                 óÚ  ‡— | j                  d«      }|syg t        | «      ¢t        «       j                  ¢}|j	                  t
        j                  «      }|D �‡cg c]  Š‰sŒt        ˆfd„|D «       «      sŒ‰‘Œ }}|D �cg c]	  }||vsŒ|‘Œ }}|r#t
        j                  j                  |«      | d<   n| j                  dd«       |rt        j                  d|«       yyc c}w c c}w )uó  Remove Hermes-owned PYTHONPATH entries: only exact matches of the repo root
    (any launcher spelling) and runtime site-packages â€” never descendants, which are
    user paths. Empty components (= cwd) and everything else are preserved.

    Everything else -- user libs, Nix plugin paths, a pythonX.Y/site-packages entry meant for a DIFFERENT
    child version -- is preserved byte-for-byte: ownership is decided by path provenance, never by a
    cross-version heuristic (#74817 follow-up).
    Ú
PYTHONPATHNc              3   óH   •K  — | ]  }t        t        ‰«      |«      –— Œ y ­wr   )r   r   )r!   r   Úes     €r   r$   z1_strip_hermes_owned_pythonpath.<locals>.<genexpr>Š   s   øè ø€ Ò/\È1´
¼4À»7ÀA×0FÑ/\ùs   ƒ"z1Stripped Hermes-owned entries from PYTHONPATH: %s)r@   r\   r   rA   Úsplitr   Úpathsepr/   Újoinr_   ÚloggerÚdebug)r9   ÚppÚowned_pathsÚentriesre   ÚstrippedÚkepts       `  r   r^   r^   |   sÍ   ø€ ð 
�‰�Ó	€BÙØØXÔ-¨cÓ2ÐX´V³X×5WÑ5WÐX€KØ�h‰h”r—z‘zÓ"€GØ"×]�a¢a¬CÓ/\ÐP[Ô/\Õ,\’Ð]€HÐ]ØÖ4�! !¨8Ò"3ŠAÐ4€DÐ4ÙÜŸJ™JŸO™O¨DÓ1ˆˆLÒà�‰�˜dÔ#ÙÜ�‰ÐHÈ(ÕSð ùò ^ùÚ4s   ÁC#Á#C#Á8C#Â	C(ÂC()Ú__doc__Úloggingr   ÚplatformrT   Úpathlibr   Ú#tools.environments.local_env_policyr   ÚsystemrV   Ú	getLoggerri   r   Úboolr   r0   r8   ÚdictrC   rX   r\   ra   r^   © r   r   ú<module>rz      sí   ðñ2ó Û 	Û Û 
Ý å Hàˆh�o‰oÓ 9Ñ,€à	ˆ×	Ñ	Ð5Ó	6€òðc�Tð c $ð c¨4ó cð
$Øð$Ø'+ð$Ø>Bð$à
ˆ4�ˆ9Ñó$ðN ð ¨$°©+ó ð 4ð ¨D°©Jó ð8¸Dð ÀTó ðT¨ð T°ô Tr   