
    sj                    z    d Z ddlmZ ddlZddlZddlmZmZmZ  ej	        e
          ZdZdZdZdddZdddZdS )u  Shared config→env bridge for media-delivery policy.

``validate_media_delivery_path`` (gateway/platforms/base.py) reads its policy
from environment variables:

  - ``HERMES_MEDIA_DELIVERY_STRICT``    <- gateway.strict
  - ``HERMES_MEDIA_ALLOW_DIRS``         <- gateway.media_delivery_allow_dirs
  - ``HERMES_MEDIA_TRUST_RECENT_FILES`` <- gateway.trust_recent_files

Historically the config.yaml -> env translation ran ONLY in gateway startup
(gateway/run.py), so any process that delivers media without booting the
gateway — a manual ``hermes cron run`` in the CLI, ``hermes send``, a
standalone cron tick — filtered MEDIA paths under DIFFERENT policy than the
gateway's scheduled deliveries. In strict/allowlisted enterprise deployments
that divergence silently dropped attachments from manual cron runs while
scheduled runs delivered them (text is unaffected — only media goes through
path validation).

``apply_media_policy_env()`` is that same translation as a shared, idempotent
helper. Gateway startup calls it, and every standalone delivery entrypoint
calls it immediately before filtering media paths.

Precedence: an explicitly-set environment variable WINS over config.yaml.
This preserves both the operator contract (env overrides are how deployments
pin behavior) and gateway/run.py's historical shape (it only wrote the env
var when the config key was present; we additionally refuse to overwrite a
pre-existing env value so a shell-exported override survives).
    )annotationsN)AnyDictOptionalHERMES_MEDIA_DELIVERY_STRICTHERMES_MEDIA_ALLOW_DIRSHERMES_MEDIA_TRUST_RECENT_FILESconfigOptional[Dict[str, Any]]returnDict[str, Any]c                    | &	 ddl m}  |            pi } n# t          $ r i cY S w xY w|                     di           }t	          |t
                    r|ni S )Nr   )load_configgateway)hermes_cli.configr   	Exceptionget
isinstancedict)r
   r   gateway_cfgs      8/home/agent/.hermes/hermes-agent/gateway/media_policy.py_load_gateway_cfgr   +   s    ~	555555 []](bFF 	 	 	III	**Y++K$[$77?;;R?s    &&Nonec                >   	 t          |           }|sdS |                    d          }|<t          j                            t                    s|rdndt          j        t          <   |                    d          }|rt          j                            t
                    svt          |t                    r|}nHt          |t          t          f          r*t          j
                            d |D                       }nd}|r|t          j        t
          <   |                    d          }|>t          j                            t                    s|rdndt          j        t          <   dS dS dS # t          $ r  t                              d	d
           Y dS w xY w)u  Bridge gateway media-policy settings from config.yaml into the env.

    Idempotent and env-wins: a variable already present in the environment is
    never overwritten, so gateway startup (which runs this same helper) and
    operator shell exports keep precedence. Never raises — a policy-bridge
    failure must not break delivery; the validator falls back to its
    defaults exactly as before.
    Nstrict10media_delivery_allow_dirsc              3  8   K   | ]}|t          |          V  d S N)str).0ps     r   	<genexpr>z)apply_media_policy_env.<locals>.<genexpr>N   s-      0Q0QAq0QQ0Q0Q0Q0Q0Q0Q     trust_recent_fileszapply_media_policy_env failedT)exc_info)r   r   osenviron_STRICT_ENV_ALLOW_DIRS_ENVr   r!   listtuplepathsepjoin_TRUST_RECENT_ENVr   loggerdebug)r
   r   r   
allow_dirsallow_dirs_strtrust_recents         r   apply_media_policy_envr7   7   s   E'// 	F**bjnn[&A&A-3&<ccBJ{# __%@AA
 	=bjnn_== 	=*c** $!+Ju66 $!#0Q0Q0Q0Q0Q!Q!Q!# =.<
?+"';<<#BJNN;L,M,M#3?,HCCSBJ())) $### E E E4tDDDDDDEs   E2 EE2 2&FFr    )r
   r   r   r   )r
   r   r   r   )__doc__
__future__r   loggingr)   typingr   r   r   	getLogger__name__r2   r+   r,   r1   r   r7    r%   r   <module>r?      s    : # " " " " "  				 & & & & & & & & & &		8	$	$,+5 	@ 	@ 	@ 	@ 	@!E !E !E !E !E !E !Er%   