
    [j:                     L   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        Zd
edefdZdedz  defdZddZdefdZdefdZdedz  fdZdS )a>  Single source of truth for the agent working directory.

`TERMINAL_CWD` is the runtime carrier for the configured working directory
(design #19214/#19242: `terminal.cwd` is bridged once to `TERMINAL_CWD` at
gateway/cron startup). The local-CLI backend deliberately leaves it unset and
relies on the launch dir. Reading it in one place keeps the system prompt, the
tool surfaces, and context-file discovery agreeing on where the agent lives.

Multi-session gateways can pin a logical cwd via the `_SESSION_CWD`
contextvar; CLI/cron fall through to `TERMINAL_CWD`/launch cwd.
    N)
ContextVarToken)Path)Any_UNSETHERMES_SESSION_CWD)default_SESSION_CWDpreturnc                     	 |                                  } n# t          $ r Y dS w xY w| t          k    pt          | j        v S )NF)resolve	Exception_PACKAGE_ROOTparents)r   s    3/home/ice/.hermes/hermes-agent/agent/runtime_cwd.py_is_install_treer   !   sO    IIKK   uu;!)!;;s    
%%cwdc                 ^    t                               | pd                                          S )z,Pin the logical cwd for the current context. )r
   setstrip)r   s    r   set_session_cwdr   ,   s&    SYB--//000    c                  :    t                               d           d S Nr   )r
   r    r   r   clear_session_cwdr   1   s    Rr   c                      t                                           } | t          u rdS t          |                                           S r   )r
   getr   strr   )values    r   _session_cwd_overrider#   5   s9    Eru::r   c                     t                      } | rRt          |                                           }|                                r|S t                              d|            t          j                            dd          	                                }|rRt          |                                          }|                                r|S t                              d|           t          t          j
                              S Nz/configured working directory does not exist: %sTERMINAL_CWDr   zTERMINAL_CWD does not exist: %s)r#   r   
expanduseris_dirloggerwarningosenvironr    r   getcwdoverrider   raws      r   resolve_agent_cwdr1   <   s    $&&H TNN%%''88:: 	HH(SSS
*..
,
,
2
2
4
4C
 ?II  ""88:: 	H8#>>>	r   c                     t                      } | rUt          |                                           }|                                st                              d|            n|S d S t          j                            dd          	                                }|rSt          |                                          }|                                st                              d|           n|S d S r%   )
r#   r   r'   r(   r)   r*   r+   r,   r    r   r.   s      r   resolve_context_cwdr3   L   s     %&&H NN%%''xxzz 	NNLhWWWWHt
*..
,
,
2
2
4
4C
 II  ""xxzz 	NN<cBBBBH4r   )r   N)__doc__loggingr+   contextvarsr   r   pathlibr   typingr   	getLogger__name__r)   objectr   __annotations__r
   __file__r   parentr   boolr   r!   r   r   r#   r1   r3   r   r   r   <module>r@      s  
 
 
  				 ) ) ) ) ) ) ) )            		8	$	$fhh   %:&:FKKKj K K K X&&((/6< < < < < <1t 1 1 1 1 1
   s    4     TD[      r   