Ë
    áß²jÆ  ã                   óˆ  — U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	  ej                  e«      Z e«       Ze	ed<    ede¬«      Zeed	<    ee«      j%                  «       j&                  j&                  Zd
edefd„Zdedz  defd„Zdd„Zdeddfd„Zdefd„Zdedededz  fd„Zdededz  fd„Zdefd„Zdedz  fd„Zy)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Ú_UNSETÚHERMES_SESSION_CWD)ÚdefaultÚ_SESSION_CWDÚpÚreturnc                 ó|   — 	 | j                  «       } | t        k(  xs t        | j                  v S # t        $ r Y yw xY w)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    ú6/home/ubuntu/.hermes/hermes-agent/agent/runtime_cwd.pyÚ_is_install_treer      sA   € ðØ�I‰I‹Kˆð ”ÑÒ;¤°!·)±)Ð!;Ð;øô ò Ùðús   ‚/ ¯	;º;Úcwdc                 óP   — t         j                  | xs dj                  «       «      S )z,Pin the logical cwd for the current context.Ú )r
   ÚsetÚstrip)r   s    r   Úset_session_cwdr   &   s    € ä×Ñ˜SšY B×-Ñ-Ó/Ó0Ð0ó    c                  ó.   — t         j                  d«       y )Nr   )r
   r   © r   r   Úclear_session_cwdr   +   s   € Ü×Ñ�RÕr   Útokenc                 ó.   — t         j                  | «       y)zPRestore the logical cwd that was active before the matching ``set_session_cwd``.N)r
   Úreset)r   s    r   Úreset_session_cwdr!   /   s   € ä×Ñ�uÕr   c                  ó€   — 	 ddl m}   | dd«      S # t        $ r# t        j                  j                  dd«      cY S w xY w)um  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.
    r   ©Úterminal_envÚTERMINAL_CWDr   )Útools.terminal_scoper$   ÚImportErrorÚosÚenvironÚgetr#   s    r   Úscope_terminal_cwdr+   4   s?   € ð2Ý5ñ ˜¨Ó+Ð+øô ò 2Ü�z‰z�~‰~˜n¨bÓ1Ò1ð2ús   ‚ ‘)=¼=ÚrawÚlabelc                 óˆ   — t        | «      j                  «       }|j                  «       r|S t        j	                  d|| «       y )Nz%s does not exist: %s)r   Ú
expanduserÚis_dirÚloggerÚwarning)r,   r-   r   s      r   Ú_existing_dirr3   B   s6   € ÜˆS‹	×ÑÓ€AØ‡x�x„zØˆÜ
‡N�NÐ*¨E°3Ô7Ør   Úoverride_is_finalc                 óè   — t         j                  «       }|t        u rdnt        |«      j	                  «       }|rt        |d«      }|€| r|S t        «       j	                  «       }|rt        |d«      S dS )zÈ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.
    r   zconfigured working directoryNr%   )r
   r*   r   Ústrr   r3   r+   )r4   Úoverrider   r,   s       r   Ú_resolve_configured_cwdr8   J   so   € ô ×ÑÓ!€HØ¤Ñ'‰r¬S°«]×-@Ñ-@Ó-B€HÙÜ˜(Ð$BÓCˆØˆ=Ñ-ØˆHÜ
Ó
×
$Ñ
$Ó
&€CÙ14Œ=˜˜nÓ-Ð>¸$Ð>r   c                  óX   — t        d¬«      xs t        t        j                  «       «      S )zeConfigured cwd, else the launch dir (os.getcwd()'s OSError on a deleted cwd deliberately propagates).F©r4   )r8   r   r(   Úgetcwdr   r   r   Úresolve_agent_cwdr<   Z   s   € ä"°UÔ;ÒP¼tÄBÇIÁIÃKÓ?PÐPr   c                  ó   — t        d¬«      S )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.Tr:   )r8   r   r   r   Úresolve_context_cwdr>   _   s   € ô #°TÔ:Ð:r   )r   N) Ú__doc__Úloggingr(   Úcontextvarsr   r   Úpathlibr   Útypingr   Ú	getLoggerÚ__name__r1   Úobjectr   Ú__annotations__r
   Ú__file__r   Úparentr   Úboolr   r6   r   r   r!   r+   r3   r8   r<   r>   r   r   r   ú<module>rK      s  ðòó Û 	ß )Ý Ý à	ˆ×	Ñ	˜8Ó	$€á‹h€ˆÓ á%Ð&:ÀFÔK€ˆjÓ Kñ
 �X“×&Ñ&Ó(×/Ñ/×6Ñ6€ð<˜ð < ó <ð1˜˜t™ð 1¨ó 1ó
ð˜Uð  tó ð
,˜Có ,ð�sð  3ð ¨4°$©;ó ð?°$ð ?¸4À$¹;ó ?ð Q˜4ó Qð
;˜T D™[ô ;r   