
    i                        U d Z ddlmZ ddlZddlmZ ddlmZ  ej        e	          Z
 ed          Zded<   ddZdaded<   ddZddZddZddZddZdS )ua  Environment variable passthrough registry.

Skills that declare ``required_environment_variables`` in their frontmatter
need those vars available in sandboxed execution environments (execute_code,
terminal).  By default both sandboxes strip secrets from the child process
environment for security.  This module provides a session-scoped allowlist
so skill-declared vars (and user-configured overrides) pass through.

Two sources feed the allowlist:

1. **Skill declarations** — when a skill is loaded via ``skill_view``, its
   ``required_environment_variables`` are registered here automatically.
2. **User config** — ``terminal.env_passthrough`` in config.yaml lets users
   explicitly allowlist vars for non-skill use cases.

Both ``code_execution_tool.py`` and ``tools/environments/local.py`` consult
:func:`is_env_passthrough` before stripping a variable.
    )annotationsN)
ContextVar)Iterable_allowed_env_varszContextVar[set[str]]_allowed_env_vars_varreturnset[str]c                     	 t                                           S # t          $ r- t                      } t                               |            | cY S w xY w)zGGet or create the allowed env vars set for the current context/session.)r   getLookupErrorset)vals    =/home/agentuser/.hermes/hermes-agent/tools/env_passthrough.py_get_allowedr   !   sZ    $((***   !!#&&&


s    4AAzfrozenset[str] | None_config_passthrough	var_namesIterable[str]Nonec                    | D ]T}|                                 }|r<t                                          |           t                              d|           UdS )zRegister environment variable names as allowed in sandboxed environments.

    Typically called when a skill declares ``required_environment_variables``.
    zenv passthrough: registered %sN)stripr   addloggerdebug)r   names     r   register_env_passthroughr   /   se    
  A Azz|| 	ANNt$$$LL94@@@	A A    frozenset[str]c                    t           t           S t                      } 	 ddlm}  |            }|                    di                               d          }t          |t                    rU|D ]R}t          |t                    r;|                                r'| 	                    |                                           Sn2# t          $ r%}t                              d|           Y d}~nd}~ww xY wt          |           a t           S )z9Load ``tools.env_passthrough`` from config.yaml (cached).Nr   )read_raw_configterminalenv_passthroughz4Could not read tools.env_passthrough from config: %s)r   r   hermes_cli.configr   r   
isinstanceliststrr   r   	Exceptionr   r   	frozenset)resultr   cfgpassthroughitemes         r   _load_config_passthroughr-   ;   s    &""uuF	P555555oggj"--112CDDk4(( 	-# - -dC(( -TZZ\\ -JJtzz||,,, P P PKQOOOOOOOOP $F++s   B#C 
C1C,,C1var_namer%   boolc                F    | t                      v rdS | t                      v S )zCheck whether *var_name* is allowed to pass through to sandboxes.

    Returns ``True`` if the variable was registered by a skill or listed in
    the user's ``tools.env_passthrough`` config.
    T)r   r-   )r.   s    r   is_env_passthroughr1   Q   s*     <>>!!t/1111r   c                 V    t          t                                t                      z  S )zGReturn the union of skill-registered and config-based passthrough vars.)r'   r   r-    r   r   get_all_passthroughr4   \   s     \^^$$'?'A'AAAr   c                 F    t                                                       dS )z9Reset the skill-scoped allowlist (e.g. on session reset).N)r   clearr3   r   r   clear_env_passthroughr7   a   s    NNr   )r   r	   )r   r   r   r   )r   r   )r.   r%   r   r/   )r   r   )__doc__
__future__r   loggingcontextvarsr   typingr   	getLogger__name__r   r   __annotations__r   r   r   r-   r1   r4   r7   r3   r   r   <module>r@      s$    & # " " " " "  " " " " " "      		8	$	$ /9j9L.M.M  M M M M    .2  1 1 1 1	A 	A 	A 	A   ,2 2 2 2B B B B
     r   