+
    iHàj+  ã                   ó€  a € R t0 t R t^ RIt^ RIt^ RIHtHt ^ RIHt ^ RI	H
t
 ]P                  ! ]4      t]! 4       t] ^ k ]! R]R7      t] ^k ]! ]4      P%                  4       P&                  P&                  tR R ltR	 R
 ltR R ltR R ltR R ltR R ltR R ltRR/R R lltR R ltRR/R R lltR# )a¿  Single source of truth for the agent working directory.

`TERMINAL_CWD` is the runtime carrier for the configured working directory (`terminal.cwd`
is bridged to it once at gateway/cron startup; the local CLI leaves it unset and relies on
the launch dir). Reading it in one place keeps the system prompt, tool surfaces, and
context-file discovery agreeing on where the agent lives. Multi-session gateways can pin a
logical cwd via `_SESSION_CWD`.
N)Ú
ContextVarÚToken©ÚPath)ÚAnyÚHERMES_SESSION_CWD)Údefaultc                ó0   € V ^8„  d   QhR\         R\        /# )é   ÚpÚreturn)r   Úbool)Úformats   "Ú6/home/ubuntu/.hermes/hermes-agent/agent/runtime_cwd.pyÚ__annotate__r      s   € ÷ <ñ <œð <¤ñ <ó    c                ó�   €  V P                  4       p T \        8H  ;'       g    \        T P                  9   #   \         d     R# i ; i)uˆ   True only when ``p`` IS the package root or sits inside it â€” ancestors
(a home dir containing the checkout) are legitimate workspaces.F)ÚresolveÚ	ExceptionÚ_PACKAGE_ROOTÚparents)r   s   &r   Ú_is_install_treer      sD   € ðØ�I‰I‹Kˆð ”Ñ×;Ð;¤°!·)±)Ñ!;Ð;øô ô Úðús   ‚6 ¶AÁAc                ó>   € V ^8„  d   QhR\         R,          R\        /# )r
   ÚcwdNr   )Ústrr   )r   s   "r   r   r   &   s   € ÷ 1ñ 1œ˜t�ð 1¬ñ 1r   c                ó\   € \         P                  T ;'       g    RP                  4       4      # )z,Pin the logical cwd for the current context.Ú )Ú_SESSION_CWDÚsetÚstrip)r   s   &r   Úset_session_cwdr    &   s"   € ä×Ñ˜SŸY˜Y B×-Ñ-Ó/Ó0Ð0r   c                ó   € V ^8„  d   QhRR/# )r
   r   N© )r   s   "r   r   r   +   s   € ÷ ñ ˜4ñ r   c                  ó0   € \         P                  R 4       R# )r   N)r   r   r"   r   r   Úclear_session_cwdr$   +   s   € Ü×Ñ�RÖr   c                ó$   € V ^8„  d   QhR\         /# ©r
   r   ©r   )r   s   "r   r   r   /   s   € ÷ 9ñ 9œCñ 9r   c                 ót   € \         P                  4       p V \        J d   R# \        V 4      P	                  4       # )zõReturn the current session's declared cwd without local path validation.

Remote and container paths may not exist on the Hermes host. Callers that only need
logical workspace identity should preserve the declared value instead of resolving it.
r   )r   ÚgetÚ_UNSETr   r   )Úvalues    r   Úscoped_session_cwdr,   /   s0   € ô ×ÑÓ€EØœ&“ˆ2Ð8¤c¨%£j×&6Ñ&6Ó&8Ð8r   c                ó(   € V ^8„  d   QhR\         RR/# )r
   Útokenr   N)r   )r   s   "r   r   r   9   s   € ÷ ñ œUð  tñ r   c                ó0   € \         P                  V 4       R# )zPRestore the logical cwd that was active before the matching ``set_session_cwd``.N)r   Úreset)r.   s   &r   Úreset_session_cwdr1   9   s   € ä×Ñ�uÖr   c                ó$   € V ^8„  d   QhR\         /# r&   r'   )r   s   "r   r   r   >   s   € ÷ ,ñ ,œCñ ,r   c                 ó„   €  ^ RI Hp  T ! RR4      #   \         d$    \        P                  P                  RR4      u # i ; i)u]  Scope-aware TERMINAL_CWD value (may be empty) â€” every cwd consumer reads through this.

Under gateway multiplexing the per-turn terminal scope carries the active profile's cwd;
the process-global env var may hold another profile's. Only an ImportError falls back: an
active refusal scope must raise, not silently resolve the launch profile's cwd.
©Úterminal_envÚTERMINAL_CWDr   )Útools.terminal_scoper5   ÚImportErrorÚosÚenvironr)   r4   s    r   Úscope_terminal_cwdr;   >   s?   € ð2Ý5ñ ˜¨Ó+Ð+øô ô 2Ü�z‰z�~‰~˜n¨bÓ1Ò1ð2ús   ‚ ‘+?¾?c                óJ   € V ^8„  d   QhR\         R\         R\        R,          /# )r
   ÚrawÚlabelr   N)r   r   )r   s   "r   r   r   L   s%   € ÷ ñ ”sð ¤3ð ¬4°$­;ñ r   c                 ó”   € \        V 4      P                  4       pVP                  4       '       d   V# \        P	                  R W4       R# )z%s does not exist: %sN)r   Ú
expanduserÚis_dirÚloggerÚwarning)r=   r>   r   s   && r   Ú_existing_dirrD   L   s6   € ÜˆS‹	×ÑÓ€AØ‡x�x‡z‚zØˆÜ
‡N�NÐ*¨EÔ7Ùr   Úinclude_session_overrideTc                óJ   € V ^8„  d   QhR\         R\         R\        R,          /# )r
   Úoverride_is_finalrE   r   N©r   r   )r   s   "r   r   r   T   s(   € ÷ ?ñ ?Üð?Ü:>ð?ä	ˆD…[ñ?r   c                ó$  € V'       d\   \         P                  4       pV\        J d   RM\        V4      P	                  4       pV'       d   \        VR4      pVf	   V '       d   V# \        4       P	                  4       pV'       d   \        VR4      # R# )aE  Session override, then TERMINAL_CWD; each validated as a real directory.

``override_is_final``: a set-but-missing session override yields None
instead of falling through to TERMINAL_CWD.
``include_session_override``: skip a session cwd known to be a launch artifact
while still consulting the active profile's TERMINAL_CWD.
r   zconfigured working directoryNr6   )r   r)   r*   r   r   rD   r;   )rG   rE   Úoverrider   r=   s   $$   r   Ú_resolve_configured_cwdrK   T   sr   € ÷  Ü×#Ñ#Ó%ˆØ!¤VÓ+‘2´°X³×1DÑ1DÓ1FˆßÜ˜hÐ(FÓGˆAØŠ}× 1Ø�Ü
Ó
×
$Ñ
$Ó
&€Cß14Œ=˜˜nÓ-Ð>¸$Ð>r   c                ó$   € V ^8„  d   QhR\         /# r&   r   )r   s   "r   r   r   i   s   € ÷ Qñ Qœ4ñ Qr   c                 óf   € \        RR7      ;'       g    \        \        P                  ! 4       4      # )zeConfigured cwd, else the launch dir (os.getcwd()'s OSError on a deleted cwd deliberately propagates).F)rG   )rK   r   r9   Úgetcwdr"   r   r   Úresolve_agent_cwdrO   i   s    € ä"°UÔ;×PÐP¼tÄBÇIÂIÃKÓ?PÐPr   c                ó>   € V ^8„  d   QhR\         R\        R,          /# )r
   rE   r   NrH   )r   s   "r   r   r   n   s   € ÷ 
ñ 
´Tð 
ÄTÈDÅ[ñ 
r   c                ó   € \        RV R7      # )u–  Configured cwd for context-file discovery, or None (build_context_files_prompt then falls back to the
launch dir). An existing configured path is honored verbatim â€” including the Hermes source tree, a
legitimate workspace when developing Hermes; fallback-directory policy lives in the caller.

Launch-artifact callers can skip the session override while still honoring the active profile's
TERMINAL_CWD.
T)rG   rE   )rK   )rE   s   $r   Úresolve_context_cwdrR   n   s   € ô #ØÐ9Qôð r   c                óh   € V ^8„  d   Qh/ ^ \         9   d
   \        ;R&   ^\         9   d
   \        ;R&   # )r
   r*   r   )Ú__conditional_annotations__r   r   )r   s   "r   r   r      s1   € ÷ ñ ÷" Ò ŒÑ ñ#÷& LÒ KŒjÑ Kò'r   ) rT   Ú__doc__Úloggingr9   Úcontextvarsr   r   Úpathlibr   Útypingr   Ú	getLoggerÚ__name__rB   Úobjectr*   r   Ú__file__r   Úparentr   r   r    r$   r,   r1   r;   rD   rK   rO   rR   r   )rT   s   @r   Ú<module>r_      s­   øðöó Û 	ß )Ý Ý à	×	Ò	˜8Ó	$€á‹h€Ó á%Ð&:ÀFÔK€Ó Kñ
 �X“×&Ñ&Ó(×/Ñ/×6Ñ6€õ<õ1õ
õ9õõ
,õð?ØAE÷?õ*Qð

¸D÷ 
ñ 
r   