§
    øžyj©   ã                  ó˜   — d Z ddlmZ ddlZddlZg d¢Zdd„Zdd	„Zdd„Zdd„Z	dd„Z
dd„Zdd„Zdd„Zdd„Zd d„Zd d„Zdd„Zd!d"d„ZdS )#u’  Pre-import startup fast paths â€” THE canonical lightweight helpers.

This module is imported by ``hermes_cli/main.py`` BEFORE its heavy import
wall (config, argparse tree, logging, providers). Everything here must stay
**stdlib-only and cheap** (os/sys file probes; no yaml, no hermes_cli.config,
no argparse). A guard test (``test_startup_fast_import_weight``) subprocess-
imports this module and fails if any heavy module sneaks into sys.modules.

Why this module exists (the bug class it kills): version-printing kept being
reimplemented as ``*_fast()`` copies at the top of main.py (Termux first,
then globally), each duplicating canonical logic â€” project-root resolution,
container detection, profile detection. The copies drifted: eb4040242
changed the canonical output and referenced ``PROJECT_ROOT`` inside the fast
function, which doesn't exist yet on the fast path â†’ the Termux fast path
NameError'd on --version and nobody noticed. One implementation, imported
by both the fast path and the module constants, makes that drift
structurally impossible; the parity guard test would have caught eb4040242
the day it landed.

``hermes_cli/config.py``'s ``get_container_exec_info()`` reads the same
``.container-mode`` file; keep the file-format assumptions here and there in
sync (this module deliberately only PROBES existence/typos cheaply and errs
toward the slow path, which then does the authoritative parse).
é    )ÚannotationsN)Úproject_root_strÚensure_project_root_on_pathÚis_termux_envÚis_termux_fast_version_argvÚis_global_fast_version_argvÚ is_container_startup_environmentÚ active_profile_may_override_homeÚcontainer_mode_may_be_activeÚread_openai_versionÚread_install_methodÚprint_fast_version_infoÚtry_fast_versionÚreturnÚstrc                 óÔ   — t           j                             t           j                             t           j                             t
          ¦  «        t           j        ¦  «        ¦  «        S )uD   Repo root as a str â€” the single source for main.py's PROJECT_ROOT.)ÚosÚpathÚrealpathÚjoinÚdirnameÚ__file__Úpardir© ó    ú>/home/ragecks/.hermes/hermes-agent/hermes_cli/_startup_fast.pyr   r   /   s8   € åŒ7×Ò�BœGŸLšL­¬¯ª½Ñ)BÔ)BÅBÄIÑNÔNÑOÔOÐOr   ÚNonec                 ó*  ‡— t          ¦   «         } t          j                             t          j                             | ¦  «        ¦  «        Šˆfd„t
          j        D ¦   «         t
          j        dd…<   t
          j                             d| ¦  «         dS )zCPut the project root at sys.path[0], deduping realpath-equivalents.c                ó˜   •— g | ]F}|r@t           j                             t           j                             |¦  «        ¦  «        ‰k    ¯D|‘ŒGS r   )r   r   Únormcaser   )Ú.0ÚentryÚnormalized_roots     €r   ú
<listcomp>z/ensure_project_root_on_path.<locals>.<listcomp>8   s[   ø€ ð ð ð àØðõ Œ7×Ò�BœG×,Ò,¨UÑ3Ô3Ñ4Ô4¸ÒGÐGð 	ð HÐGÐGr   Nr   )r   r   r   r    r   ÚsysÚinsert)Úproject_rootr#   s    @r   r   r   4   s…   ø€ å#Ñ%Ô%€LÝ”g×&Ò&¥r¤w×'7Ò'7¸Ñ'EÔ'EÑFÔF€Oðð ð ð å”Xðñ ô …C„HˆQˆQˆQ�Kõ „H‡O‚O�A�|Ñ$Ô$Ð$Ð$Ð$r   Úboolc                 óÌ   — t           j                             dd¦  «        } t          t           j                             d¦  «        pd| v p|                      d¦  «        ¦  «        S )z3Tiny Termux check for pre-import startup shortcuts.ÚPREFIXÚ ÚTERMUX_VERSIONzcom.termux/files/usrz/data/data/com.termux/)r   ÚenvironÚgetr(   Ú
startswith)Úprefixs    r   r   r   A   s_   € åŒZ�^Š^˜H bÑ)Ô)€FÝÝ
Œ
�ŠÐ'Ñ(Ô(ð 	7Ø! VÐ+ð	7à×ÒÐ5Ñ6Ô6ñô ð r   Úargvú	list[str]c                ó   — | dgdgdgfv S )Nú	--versionú-VÚversionr   ©r1   s    r   r   r   K   s   € Ø�[�M D 6¨I¨;Ð7Ð7Ð7r   c                ó   — | dgdgfv S )Nr4   r5   r   r7   s    r   r   r   O   s   € Ø�[�M D 6Ð*Ð*Ð*r   c                 ó:  — t           j                             d¦  «        st           j                             d¦  «        rdS 	 t          dd¬¦  «        5 } |                      ¦   «         }ddd¦  «         n# 1 swxY w Y   n# t
          $ r Y dS w xY wd	|v pd
|v pd|v S )zDTrue when we're already INSIDE a container (fast path is then safe).z/.dockerenvz/run/.containerenvTz/proc/1/cgroupúutf-8©ÚencodingNFÚdockerÚpodmanz/lxc/)r   r   ÚexistsÚopenÚreadÚOSError)ÚhandleÚcgroups     r   r	   r	   S   sí   € å	„w‡~‚~�mÑ$Ô$ð ­¬¯ªÐ7KÑ(LÔ(Lð ØˆtðÝÐ"¨WÐ5Ñ5Ô5ð 	#¸Ø—[’[‘]”]ˆFð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#øøåð ð ð Øˆuˆuðøøøà�vÐÐH ¨VÐ!3ÐH°wÀ&Ð7HÐHs6   ÁB  ÁA4Á(B  Á4A8Á8B  Á;A8Á<B  Â 
BÂBÚhermes_rootc                ó~  — t           j                             | d¦  «        }	 t           j                             |¦  «        rdt	          |d¬¦  «        5 }|                     ¦   «                              ¦   «         }ddd¦  «         n# 1 swxY w Y   t          |o|dk    ¦  «        S n# t          t          f$ r Y nw xY wdS )zECheap probe: does an active non-default profile redirect HERMES_HOME?Úactive_profiler:   r;   NÚdefaultF)
r   r   r   r?   r@   rA   Ústripr(   rB   ÚUnicodeDecodeError)rE   rG   rC   Úactives       r   r
   r
   _   sû   € å”W—\’\ +Ð/?Ñ@Ô@€NðÝŒ7�>Š>˜.Ñ)Ô)ð 	8Ý�n¨wÐ7Ñ7Ô7ð /¸6ØŸš™œ×,Ò,Ñ.Ô.�ð/ð /ð /ñ /ô /ð /ð /ð /ð /ð /ð /øøøð /ð /ð /ð /å˜Ð6 6¨YÒ#6Ñ7Ô7Ð7ð	8øõ Õ'Ð(ð ð ð Øˆðøøøàˆ5s5   ¢0B& Á'BÁ9B& ÂB	Â	B& ÂB	ÂB& Â&B:Â9B:c                 óè   — t           j                             dd¦  «                             ¦   «         } | r| S t           j                             t           j                             d¦  «        d¦  «        S )NÚHERMES_HOMEr+   ú~ú.hermes)r   r-   r.   rI   r   r   Ú
expanduser)Úhermes_homes    r   Ú_resolved_homerR   l   sW   € Ý”*—.’. °Ñ3Ô3×9Ò9Ñ;Ô;€KØð ØÐÝŒ7�<Š<�œ×*Ò*¨3Ñ/Ô/°Ñ;Ô;Ð;r   c                 óD  — t           j                             d¦  «        dk    rdS t          ¦   «         rdS t           j                             dd¦  «                             ¦   «         } | r­t           j                             t           j                             | d¦  «        ¦  «        rdS t           j                             t           j         	                    t           j         
                    | ¦  «        ¦  «        ¦  «        }|dk    ot          | ¦  «        S t           j                             t           j                             d	¦  «        d
¦  «        }t          |¦  «        rdS t           j                             t           j                             |d¦  «        ¦  «        S )uŽ  Conservative probe for NixOS container-mode routing.

    False positives are fine (we fall through to the slow path, whose
    ``get_container_exec_info()`` does the authoritative check and routes
    into the container). False negatives are NOT fine â€” they'd print the
    host's version instead of the container's. Hence: any profile
    ambiguity â†’ assume container mode may be active.
    Ú
HERMES_DEVÚ1FrM   r+   z.container-modeTÚprofilesrN   rO   )r   r-   r.   r	   rI   r   r?   r   Úbasenamer   Únormpathr
   rP   )rQ   Úparent_nameÚdefault_homes      r   r   r   s   s;  € õ 
„z‡~‚~�lÑ#Ô# sÒ*Ð*ØˆuÝ'Ñ)Ô)ð Øˆuå”*—.’. °Ñ3Ô3×9Ò9Ñ;Ô;€KØð 
ÝŒ7�>Š>�"œ'Ÿ,š, {Ð4EÑFÔFÑGÔGð 	Ø�4Ý”g×&Ò&¥r¤w§¢µr´w×7GÒ7GÈÑ7TÔ7TÑ'UÔ'UÑVÔVˆà˜:Ò%ð >Ý0°Ñ=Ô=ð	
õ
 ”7—<’<¥¤× 2Ò 2°3Ñ 7Ô 7¸ÑCÔC€LÝ'¨Ñ5Ô5ð ØˆtÝŒ7�>Š>�"œ'Ÿ,š, |Ð5FÑGÔGÑHÔHÐHr   ú
str | Nonec                 óD  — t           j        D �]} | st          j        ¦   «         } t          j                             | dd¦  «        }	 t          |d¬¦  «        5 }|D ]™}|                     ¦   «         }|                     d¦  «        sŒ,|                     d¦  «        \  }}}| 	                    dd¦  «        d	                              ¦   «                              d
¦  «        }|pdc cddd¦  «         c S 	 ddd¦  «         n# 1 swxY w Y   �Œ# t          $ r Y �Œw xY wdS )zARead OpenAI SDK version without importing ``importlib.metadata``.Úopenaiz_version.pyr:   r;   Ú__version__ú=ú#é   r   z"'N)r%   r   r   Úgetcwdr   r@   rI   r/   Ú	partitionÚsplitrB   )ÚbaseÚversion_filerC   ÚlineÚstrippedÚ_keyÚ_sepÚvalues           r   r   r   ‘   s”  € å”ð ñ ˆØð 	Ý”9‘;”;ˆDÝ”w—|’| D¨(°MÑBÔBˆð
	Ý�l¨WÐ5Ñ5Ô5ð )¸Ø"ð )ð )�DØ#Ÿzšz™|œ|�HØ#×.Ò.¨}Ñ=Ô=ð !Ø Ø(0×(:Ò(:¸3Ñ(?Ô(?Ñ%�D˜$ Ø!ŸKšK¨¨QÑ/Ô/°Ô2×8Ò8Ñ:Ô:×@Ò@ÀÑGÔG�EØ ˜= DÐ(Ð(ð)ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð)ð)ð )ð )ñ )ô )ð )ð )ð )ð )ð )ð )øøøð )ð )ð )ð )ùøõ ð 	ð 	ð 	Ø‰Hð	øøøàˆ4s=   ÁDÁBDÃ&DÃ6DÄD	ÄDÄ	D	Ä
DÄ
DÄDc                 óH  — t           j                             t          ¦   «         d¦  «        } 	 t	          | d¬¦  «        5 }|                     ¦   «                              ¦   «                              ¦   «         }ddd¦  «         n# 1 swxY w Y   |pdS # t          $ r Y dS w xY w)u   Read the installer's ``.install_method`` stamp, if present.

    Only the stamp (step 1 of ``config.detect_install_method``'s resolution
    order) â€” the managed/git/pip fallbacks need heavier imports and stay on
    the slow path. On the fast path home ambiguity is already excluded:
    ``container_mode_may_be_active()`` bails to the slow path whenever a
    non-default profile might redirect HERMES_HOME.
    z.install_methodr:   r;   N)	r   r   r   rR   r@   rA   rI   ÚlowerrB   )ÚstamprC   Úmethods      r   r   r   ¥   sÛ   € õ ŒG�LŠL�Ñ)Ô)Ð+<Ñ=Ô=€EðÝ�% 'Ð*Ñ*Ô*ð 	3¨fØ—[’[‘]”]×(Ò(Ñ*Ô*×0Ò0Ñ2Ô2ˆFð	3ð 	3ð 	3ñ 	3ô 	3ð 	3ð 	3ð 	3ð 	3ð 	3ð 	3øøøð 	3ð 	3ð 	3ð 	3àˆ~˜ÐøÝð ð ð Øˆtˆtðøøøs4   ®B ¿9BÁ8B ÂBÂB ÂBÂB Â
B!Â B!c                 ó�  — ddl m} m} t          d|› d| › d�¦  «         t          dt	          ¦   «         › �¦  «         t          ¦   «         }|rt          d|› �¦  «         t          dt          j                             ¦   «         d         › �¦  «         t          ¦   «         }t          |rd	|› �nd
¦  «         t          d¦  «         d S )Nr   )Ú__release_date__r^   zHermes Agent vz (ú)zInstall directory: zInstall method: zPython: zOpenAI SDK: zOpenAI SDK: Not installedz'Run 'hermes version' for update status.)
Ú
hermes_clirq   r^   Úprintr   r   r%   r6   rd   r   )rq   r^   Úinstall_methodÚopenai_versions       r   r   r   ·   sñ   € Ø8Ð8Ð8Ð8Ð8Ð8Ð8Ð8å	Ð
=˜;Ð
=Ð
=Ð*:Ð
=Ð
=Ð
=Ñ>Ô>Ð>Ý	Ð
4Õ 0Ñ 2Ô 2Ð
4Ð
4Ñ5Ô5Ð5Ý(Ñ*Ô*€NØð 3ÝÐ1 Ð1Ð1Ñ2Ô2Ð2å	Ð
-•S”[×&Ò&Ñ(Ô(¨Ô+Ð
-Ð
-Ñ.Ô.Ð.å(Ñ*Ô*€NÝ	¨^Ð
\Ð
)˜Ð
)Ð
)Ð
)ÐA\Ñ]Ô]Ð]Ý	Ð
3Ñ4Ô4Ð4Ð4Ð4r   úlist[str] | Nonec                ó"  — | €t           j        dd…         } t          ¦   «         }|r%t          j                             d¦  «        dk    rdS |rt          | ¦  «        sdS n!t          | ¦  «        sdS t          ¦   «         rdS t          ¦   «          dS )a¨  Handle ``hermes --version`` before the heavy import wall.

    Termux keeps its historical contract (also accepts the ``version``
    subcommand + the HERMES_TERMUX_DISABLE_FAST_CLI escape hatch). Everywhere
    else: only ``--version``/``-V`` (the ``version`` subcommand stays on the
    slow path for full output incl. update check), and never when container
    mode may need to route the command into the container.
    Nra   ÚHERMES_TERMUX_DISABLE_FAST_CLIrU   FT)
r%   r1   r   r   r-   r.   r   r   r   r   )r1   Ú	is_termuxs     r   r   r   Ç   s¢   € ð €|ÝŒx˜˜˜Œ|ˆÝ‘”€IØð •R”Z—^’^Ð$DÑEÔEÈÒLÐLØˆuØð Ý*¨4Ñ0Ô0ð 	Ø�5ð	å(¨Ñ.Ô.ð ØˆuÝ	%Ñ	'Ô	'ð ØˆuåÑÔÐØˆ4r   )r   r   )r   r   )r   r(   )r1   r2   r   r(   )rE   r   r   r(   )r   r[   )N)r1   rw   r   r(   )Ú__doc__Ú
__future__r   r   r%   Ú__all__r   r   r   r   r   r	   r
   rR   r   r   r   r   r   r   r   r   ú<module>r~      se  ððð ð2 #Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø 
€
€
€
ðð ð €ð Pð Pð Pð Pð

%ð 
%ð 
%ð 
%ðð ð ð ð8ð 8ð 8ð 8ð+ð +ð +ð +ð	Ið 	Ið 	Ið 	Ið
ð 
ð 
ð 
ð<ð <ð <ð <ðIð Ið Ið Ið<ð ð ð ð(ð ð ð ð$5ð 5ð 5ð 5ð ð ð ð ð ð ð r   