+
    lHàjÅ+  ã                   ó¶   € R t ^ RIt^ RIt^ RIHt Rt]P                  ! R4      tRtRt	R R lt
R	 R
 ltRR R lltR R ltR R ltRR/R R lltR R ltR# )a"  Bash session-snapshot scripting for ``tools.environments.base``.

Pure string builders: the CWD marker, the ``export -p`` dump that strips
per-session vars, the ``init_session`` bootstrap, and the per-command wrapper.
No module state lives here; ``BaseEnvironment`` supplies quoting hooks.
N)ÚIterablezµ^declare -x (HERMES_SESSION_|HERMES_UI_SESSION_ID|HERMES_CRON_AUTO_DELIVER_|HERMES_CRON_SESSION|HERMES_BROWSER_CONTROL_|HERMES_DELEGATED_CHILD_CONTEXT|HERMES_RPC_|HERMES_KERNEL_DIR)z^[A-Za-z_][A-Za-z0-9_]*$z.tmp.XXXXXXXXXXz"$__hermes_snap_tmp"c                ó0   € V ^8„  d   QhR\         R\         /# )é   Ú
session_idÚreturn©Ústr)Úformats   "ÚH/home/ubuntu/.hermes/hermes-agent/tools/environments/base_session_env.pyÚ__annotate__r   (   s   € ÷ *ñ *œCð *¤Cñ *ó    c                 ó   € R V  R2# )Ú__HERMES_CWD_Ú__© )r   s   &r
   Ú_cwd_markerr   (   s   € Ø˜:˜, bÐ)Ð)r   c                ó0   € V ^8„  d   QhR\         R\         /# )r   Úmarkerr   r   )r	   s   "r
   r   r   ,   s   € ÷ >ñ >œsð >¤sñ >r   c                ó   € RV  RV  R2# )z•Emit the CWD marker on its own line (leading ``\n`` guards against a
command whose output lacks a trailing newline; ``_split_cwd_marker`` strips it).z
printf '\nz%sz\n' "$(pwd -P)"r   )r   s   &r
   Ú_cwd_marker_printfr   ,   s   € ð ˜˜  6 (Ð*<Ð=Ð=r   c                óR   € V ^8„  d   QhR\         R\        \         ,          R\         /# )r   Útmp_pathÚexcluded_namesr   ©r   r   )r	   s   "r
   r   r   2   s'   € ÷ $ñ $´#ð $ÄxÔPSÅ}ð $Ô^añ $r   c                óÄ   € V Uu0 uF'  p\        V\        4      '       g   K  V'       g   K%  VkK)  	  ppRP                  R \        V4       4       4      pRV RV  2# u upi )uè  Shell snippet dumping ``export -p`` to *tmp_path* minus the per-session bridged vars (see
``_SNAPSHOT_EXCLUDED_ENV_REGEX``) and *excluded_names*. The vars are ``unset`` in a subshell
BEFORE ``export -p``: a line-based ``grep -vE`` is unsafe because bash 3.2 prints a value
containing a newline as a multi-line ``declare -x`` block, so smuggled continuation lines would
survive into the snapshot and execute on the next ``source``. ``|| true`` keeps the success
contract. The dump is a brace group with the redirection on the group: *tmp_path* is usually a
shell-variable expansion, and a redirect on a pipeline segment would expand it inside that
segment's subshell, inconsistently with the parent that expands the follow-up ``mv``.

``curl â€¦ | bash #`` smuggled into a Matrix room/display name via ``HERMES_SESSION_CHAT_NAME``) land in
the snapshot and execute on the next ``source`` (issue #71296). Unsetting first means ``export -p``
never emits those vars â€” including any continuation lines.
Ú c              3   óT   "  € T F  pR \         P                  ! V4       2x € K   	  R# 5i)Ú N)ÚshlexÚquote)Ú.0Únames   & r
   Ú	<genexpr>Ú6_export_dump_excluding_session_vars.<locals>.<genexpr>D   s#   é € ÐQÑ>P°d˜AœeŸkšk¨$Ó/Ð0Õ1Ó>Pùs   ‚&(zÛ{ ( unset ${!HERMES_SESSION_*} ${!HERMES_CRON_AUTO_DELIVER_*} ${!HERMES_BROWSER_CONTROL_*} AI_AGENT HERMES_AGENT HERMES_DELEGATED_CHILD_CONTEXT HERMES_CRON_SESSION ${!HERMES_RPC_*} HERMES_KERNEL_DIR HERMES_UI_SESSION_IDz( 2>/dev/null; export -p; ) || true; } > )Ú
isinstancer   ÚjoinÚsorted)r   r   r!   Ú
safe_namesÚextra_unsets   &&   r
   Ú#_export_dump_excluding_session_varsr)   2   sf   € ñ" $2ÓT¡>˜4´ZÀÄc×5J”$Ìt—$�$¡>€JÐTØ—'‘'ÑQ¼fÀZÔ>PÓQÓQ€Kð	ð  +˜mð ,àˆJð!	ðùò Us   …A¢A¬Ac                óv   € V ^8„  d   QhR\         R\         R\         R\        \         ,          R\         R\         /# )r   Ú
quoted_cwdÚquoted_snapÚsnap_tmp_templater   Ú
cwd_markerr   r   )r	   s   "r
   r   r   Y   sB   € ÷ /ñ /Üð/Ü%(ð/Ü=@ð/ÜRZÔ[^ÕR_ð/Ümpð/äñ/r   c                ó¶   € RV R\        \        V4       R\         R\         R\         R\         R\         R\         R	V R
\         RV  R\        V4       R2# )aç  Login-shell bootstrap that captures env/functions/aliases into the snapshot. Atomic publish:
assemble in a ``mktemp`` file, then ``mv`` over the final path so a concurrent ``source`` never
reads a half-written snapshot (``$$`` is the parent PID in ``&``-launched subshells and macOS
bash 3.2 lacks ``$BASHPID``, so only ``mktemp`` is portable). Functions are filtered by NAME via
``declare -F`` (a line-based ``declare -f | grep -v`` strips the header and leaves an orphaned
body that breaks every sourced command); the non-empty guard matters because bare ``declare -f``
dumps ALL functions. The trailing ``cd`` restores the configured cwd after profile scripts (e.g.
``cd ~``) so ``pwd -P`` reports terminal.cwd, not the profile's directory.z%umask 077
__hermes_snap_tmp=$(mktemp z) || exit 1
z�
__hermes_fns=$(declare -F | awk '{print $3}' | grep -vE '^_[^_]') || true
[ -n "$__hermes_fns" ] && declare -f $__hermes_fns >> z! 2>/dev/null || true
alias -p >> z#
echo 'shopt -s expand_aliases' >> z
echo 'set +e' >> z
echo 'set +u' >> z
mv -f r   z
 || rm -f z
builtin cd -- z 2>/dev/null || true
Ú
)r)   Ú	_SNAP_TMPr   )r+   r,   r-   r   r.   s   $$$$$r
   Ú_snapshot_bootstrap_scriptr2   Y   s‹   € ð	&Ø&7Ð%8¸Ü.¬y¸.ÓIÐ
Jð KCäCLÀ+ð NÜ �kð "-Ü-6¨Kð 8Ü%˜;ð 'Ü%˜;ð 'ä�˜1˜[˜M¨´I°;ð ?Ø#˜Ð$:Ü˜jÓ)Ð
*¨"ð	.ð/r   c                ó”   € V ^8„  d   QhR\         \        ,          R\        \        \        ,          \        \        ,          3,          /# )r   Únamesr   )r   r   ÚtupleÚlist)r	   s   "r
   r   r   t   s0   € ÷ ñ ¤X¬c¥]ð ´u¼TÄ#½YÌÌSÍ	Ð=QÕ7Rñ r   c                ó¦   € . p. pV  FE  pRV 2pV R2V R2reW RV R2V RV R2.,          pVRV RV R	V R
V R2	RV RV 2.,          pKG  	  W3# )uþ   Shell lines that save profile-scoped passthrough vars before the snapshot is sourced
and restore (or unset) them afterwards â€” a shared snapshot may hold the previous
profile's value. Values stay in environment memory and never enter the command string.Ú_HERMES_RUNTIME_PASSTHROUGH_Ú_PRESENTÚ_VALUEz=${z+x}z-}zif [ "$z" = x ]; then export z="$z"; else unset z; fizunset r   r   )r4   ÚsaveÚrestorer!   r   ÚpresentÚvalues   &      r
   Ú_passthrough_save_restorer?   t   s«   € ð €DØ€GÛˆØ/°¨vÐ6ˆØ"˜8 8Ð,°°¸Ð.?�Ø�9˜D   dÐ+°¨w°d¸4¸&ÀÐ-DÐEÕEˆØØ�g�YÐ3°D°6¸¸U¸GÀ>ÐRVÐQWÐW[Ð\Ø�W�I˜Q˜u˜gÐ&ð(õ 	(Šñ	 ð ˆ=Ðr   Úpersist_envTc                óš   € V ^8„  d   QhR\         R\         R\         R\         R\        \         ,          R\        R\         R\        R	\         /	# )
r   Úcommandr+   r,   r-   Úpassthrough_namesÚsnapshot_readyr.   r@   r   )r   r   Úbool)r	   s   "r
   r   r   „   s\   € ÷ %ñ %Üð%Ü!$ð%Ü36ð%ÜKNð%ä¤•}ð%ä6:ð%äHKð%ô ð%ô "%ñ%r   c               ó¦  € V P                  RR4      p\        V4      w  rš\        V	4      pV'       d   VP                  RV R24       Wº,          pVRRRV R2R	V R2R
R.,          pV'       d@   V'       d8   VP                  RV R\	        \
        V4       R\
         RV R\
         R24       V\        V4      R.,          pRP                  V4      # )a†  Per-command bash script: source snapshot, cd, run, re-dump env, emit CWD marker.
``persist_env=False`` skips the re-dump, so every command starts from the login-time
snapshot and exports from one command never reach the next (the CWD marker still runs).
``source`` stdout goes to /dev/null because macOS bash 3.2 / some Homebrew builds echo
``declare -x`` lines when sourcing. AI_AGENT/HERMES_AGENT advertise the harness to remote
backends (whose env is not inherited); ``${VAR:-default}`` never clobbers an outer harness.
GIT_PAGER/PAGER=cat stop pager-happy tools hanging a PTY-backed command. The env re-dump
uses the same mktemp+mv atomic publish as the bootstrap and chains ``mv`` on the dump
succeeding so a failed dump never replaces a good snapshot. ``umask 077`` is applied after
the user's command so snapshot files (which may carry secrets) are private without
changing the command's umask.
Ú'z'\''zsource z >/dev/null 2>&1 || truezPexport AI_AGENT="${AI_AGENT:-hermes-agent}" HERMES_AGENT="${HERMES_AGENT:-true}"z:export GIT_PAGER="${GIT_PAGER:-cat}" PAGER="${PAGER:-cat}"zbuiltin cd -- z || exit 126zeval 'z__hermes_ec=$?z	umask 077z__hermes_snap_tmp=$(mktemp z) && { z
 && mv -f r   z; } 2>/dev/null || rm -f z 2>/dev/null || truezexit $__hermes_ecr0   )Úreplacer?   r6   Úappendr)   r1   r   r%   )rB   r+   r,   r-   rC   rD   r.   r@   Úescapedr;   r<   Úpartss   &$$$$$$$    r
   Ú_wrap_command_scriptrL   „   sù   € ð  �o‰o˜c 7Ó+€GÜ-Ð.?Ó@�M€DÜ�‹J€EßØ�‰�w˜{˜mÐ+CÐDÔEØ	Õ€EØ	ØZØDà
˜˜ LÐ1Ø
��	˜ÐØØðõ €E÷ Ÿ+Ø�‰Ø)Ð*;Ð)<ð =Ü5´iÐARÓSÐTð UÜ!�{ ! K =ð 1$Ü$- ;Ð.BðDô	Eð
 
Ô  Ó,Ð.AÐBÕB€EØ�9‰9�UÓÐr   c                óz   € V ^8„  d   QhR\         R\         R\        \         R,          \         3,          R,          /# )r   Úoutputr   r   N)r   r5   )r	   s   "r
   r   r   ¬   s3   € ÷ =ñ =œcð =¬3ð =´5¼¸t½ÄS¸Õ3IÈDÕ3Pñ =r   c                óÆ  € V P                  V4      pVR8X  d   R# \        ^ VR,
          4      pV P                  WV4      pVR8X  g   WB8X  d   R# W\        V4      ,           V P                  4       ;'       g    RpV P                  R^ V4      pVR8X  d   TpV P	                  RV\        V4      ,           4      pVR8w  d
   V^,           M
\        V 4      pWPRV WR ,           3# )a  Locate the last ``marker<path>marker`` pair in *output*. Returns
``(cwd_path_or_None, output_without_marker_line)``, or ``None`` when no complete pair
exists. The stripped span runs from the ``\n`` the wrapper injected before the marker
through the end of the marker line.Ni   r0   éÿÿÿÿ)ÚrfindÚmaxÚlenÚstripÚfind)rN   r   ÚlastÚsearch_startÚfirstÚcwd_pathÚ
line_startÚline_ends   &&      r
   Ú_split_cwd_markerr\   ¬   sÕ   € ð
 �<‰<˜Ó€DØˆr„zÙÜ�q˜$ �+Ó&€LØ�L‰L˜¨tÓ4€EØ�„{�e”mÙØœc &›kÕ)¨DÐ1×7Ñ7Ó9×AÐA¸T€HØ—‘˜d A uÓ-€JØ�RÔØˆ
Ø�{‰{˜4 ¬¨F«Õ!3Ó4€HØ'¨2œ~ˆx˜!Ž|´3°v³;€HØ˜K˜ZÐ(¨6°)Ð+<Õ<Ð<Ð<r   )r   )Ú__doc__Úrer   Útypingr   Ú_SNAPSHOT_EXCLUDED_ENV_REGEXÚcompileÚ_SHELL_ENV_NAME_REÚ_SNAP_TMP_SUFFIXr1   r   r   r)   r2   r?   rL   r\   r   r   r
   Ú<module>rd      sm   ðñó 
Û Ý ð(%ð ð —Z’ZÐ ;Ó<Ð ð %Ð Ø"€	õ*õ>÷$õN/õ6ð %ð ÷%÷P=r   