
    OKiv7                       S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
JrJr  SSKJrJr  SSKJr   SSKrSrS	r            SS
 jr\	R.                  (       a  SSK
JrJr  SSKJr  \ " S S\R0                  5      5       r\ " S S\5      5       r\ " S S\5      5       r\ " S S\5      5       r/ SQrg! \ a    Sr Nf = f)z7Execution policies for the persistent shell middleware.    )annotationsN)MappingSequence)	dataclassfield)PathTFzlangchain-shell-c                   [         R                  " [        U 5      [         R                  [         R                  [         R                  USSSSUUUS9$ )NTzutf-8replace   )stdinstdoutstderrcwdtextencodingerrorsbufsizeenv
preexec_fnstart_new_session)
subprocessPopenlistPIPE)commandr   r   r   r   s        h/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langchain/agents/middleware/_execution.py_launch_subprocessr      sK     Woo+     c                      \ rS rSr% SrSrS\S'   SrS\S'   SrS\S'   S	r	S
\S'   Sr
S\S'   SS jr\R                          SS j5       rSrg)BaseExecutionPolicy8   a  Configuration contract for persistent shell sessions.

Concrete subclasses encapsulate how a shell process is launched and constrained.

Each policy documents its security guarantees and the operating environments in
which it is appropriate. Use `HostExecutionPolicy` for trusted, same-host execution;
`CodexSandboxExecutionPolicy` when the Codex CLI sandbox is available and you want
additional syscall restrictions; and `DockerExecutionPolicy` for container-level
isolation using Docker.
g      >@floatcommand_timeoutstartup_timeoutg      $@termination_timeoutd   intmax_output_linesN
int | Nonemax_output_bytesc                >    U R                   S::  a  Sn[        U5      eg )Nr   z"max_output_lines must be positive.)r(   
ValueErrorselfmsgs     r   __post_init__!BaseExecutionPolicy.__post_init__K   s$      A%6CS/! &r   c                   g)z$Launch the persistent shell process.N )r.   	workspacer   r   s       r   spawnBaseExecutionPolicy.spawnP   s    r   r3   returnNoner4   r   r   Mapping[str, str]r   Sequence[str]r8   subprocess.Popen[str])__name__
__module____qualname____firstlineno____doc__r#   __annotations__r$   r%   r(   r*   r0   abcabstractmethodr5   __static_attributes__r3   r   r   r    r    8   s    	 "OU!!OU!!%%c#'j'"
 	3 3 	3
 3 
3 3r   r    c                     ^  \ rS rSr% SrSrS\S'   SrS\S'   SrS\S	'   \	" S
S
S
S9r
S\S'   SU 4S jjr        SS jrSS jrSS jr\SS j5       rSrU =r$ )HostExecutionPolicy[   a  Run the shell directly on the host process.

This policy is best suited for trusted or single-tenant environments (CI jobs,
developer workstations, pre-sandboxed containers) where the agent must access the
host filesystem and tooling without additional isolation. Enforces optional CPU and
memory limits to prevent runaway commands but offers **no** filesystem or network
sandboxing; commands can modify anything the process user can reach.

On Linux platforms resource limits are applied with `resource.prlimit` after the
shell starts. On macOS, where `prlimit` is unavailable, limits are set in a
`preexec_fn` before `exec`. In both cases the shell runs in its own process group
so timeouts can terminate the full subtree.
Nr)   cpu_time_secondsmemory_bytesTboolcreate_process_groupF)initreprdefault_limits_requestedc                x  > [         TU ]  5         U R                  b  U R                  S::  a  Sn[        U5      eU R                  b  U R                  S::  a  Sn[        U5      e[        S U R                  U R                  4 5       5      U l        U R                  (       a  [        (       d  Sn[        U5      eg g )Nr   z.cpu_time_seconds must be positive if provided.*memory_bytes must be positive if provided.c              3  (   #    U  H  oS Lv   M
     g 7fNr3   ).0values     r   	<genexpr>4HostExecutionPolicy.__post_init__.<locals>.<genexpr>y   s      %
+U%+Us   zHostExecutionPolicy cpu/memory limits require the Python 'resource' module. Either remove the limits or run on a POSIX platform.)	superr0   rJ   r,   rK   anyrQ   _HAS_RESOURCERuntimeErrorr.   r/   	__class__s     r   r0   !HostExecutionPolicy.__post_init__q   s      ,1F1F!1KBCS/!(T->->!-C>CS/!!$ %
,0,A,A4CTCT+U%
 "
 !!--G  s## +8!r   c                   [        [        U5      UUU R                  5       U R                  S9nU R	                  U5        U$ )Nr   r   r   r   )r   r   _create_preexec_fnrM   _apply_post_spawn_limits)r.   r4   r   r   processs        r   r5   HostExecutionPolicy.spawn   sE     %M..0"77
 	%%g.r   c                f   ^  T R                   (       a  T R                  5       (       a  g SU 4S jjnU$ )Nc                   > TR                   b=  TR                   TR                   4n [        R                  " [        R                  U 5        TR                  b  TR                  TR                  4n [        [        S5      (       a&  [        R                  " [        R                  U 5        g [        [        S5      (       a&  [        R                  " [        R                  U 5        g g g )N	RLIMIT_ASRLIMIT_DATA)rJ   resource	setrlimit
RLIMIT_CPUrK   hasattrri   rj   )limitr.   s    r   
_configure:HostExecutionPolicy._create_preexec_fn.<locals>._configure   s    $$0..0E0EF""8#6#6>  ,**D,=,=>8[11&&x'9'95AX}55&&x';';UC 6	 -r   r7   )rQ   _can_use_prlimit)r.   rp   s   ` r   rc   &HostExecutionPolicy._create_preexec_fn   s+    %%)>)>)@)@		D r   c                   U R                   (       a  U R                  5       (       d  g [        (       d  g UR                  n [        R
                  " S[        5      R                  nU R                  b.  U" U[        R                  U R                  U R                  45        U R                  bu  U R                  U R                  4n[        [        S5      (       a  U" U[        R                  U5        g [        [        S5      (       a  U" U[        R                  U5        g g g ! [         a  nSn[        U5      UeS nAff = f)N
typing.Anyri   rj   z,Failed to apply resource limits via prlimit.)rQ   rr   r\   pidtypingcastrk   prlimitrJ   rm   rK   rn   ri   rj   OSErrorr]   )r.   re   rv   ry   ro   excr/   s          r   rd   ,HostExecutionPolicy._apply_post_spawn_limits   s   %%T-B-B-D-D}kk	-kk,9AAG$$0X0043H3H$J_J_2`a  ,**D,=,=>8[11C!3!3U;X}55C!5!5u= 6	 -  	-@Cs#,	-s   B2D$ 4-D$ $
E.D<<Ec                     [         =(       a6    [        [        S5      =(       a    [        R                  R                  S5      $ )Nry   linux)r\   rn   rk   sysplatform
startswithr3   r   r   rr   $HostExecutionPolicy._can_use_prlimit   s)    b9!=b#,,BYBYZaBbbr   )rQ   r7   r:   )r8    typing.Callable[[], None] | None)re   r=   r8   r9   )r8   rL   )r>   r?   r@   rA   rB   rJ   rC   rK   rM   r   rQ   r0   r5   rc   rd   staticmethodrr   rF   __classcell__r_   s   @r   rH   rH   [   s     $(j'#L*#!%$%#UEJtJ$$  	
  
""-( c cr   rH   c                      \ rS rSr% SrSrS\S'   SrS\S'   \" \	S	9r
S
\S'           SS jrSS jrSS jrSS jr\SS j5       rSrg)CodexSandboxExecutionPolicy   a  Launch the shell through the Codex CLI sandbox.

Ideal when you have the Codex CLI installed and want the additional syscall and
filesystem restrictions provided by Anthropic's Seatbelt (macOS) or Landlock/seccomp
(Linux) profiles. Commands still run on the host, but within the sandbox requested by
the CLI. If the Codex binary is unavailable or the runtime lacks the required
kernel features (e.g., Landlock inside some containers), process startup fails with a
`RuntimeError`.

Configure sandbox behavior via `config_overrides` to align with your Codex CLI
profile. This policy does not add its own resource limits; combine it with
host-level guards (cgroups, container resource limits) as needed.
codexstrbinaryautoz(typing.Literal['auto', 'macos', 'linux']r   )default_factoryzMapping[str, typing.Any]config_overridesc               >    U R                  U5      n[        UUUS SS9$ NFrb   )_build_commandr   )r.   r4   r   r   full_commands        r   r5   !CodexSandboxExecutionPolicy.spawn   s0     **73!#
 	
r   c           	     L   U R                  5       nU R                  5       nUSU/n[        [        U R                  5      R                  5       5       H,  u  pVUR                  SU SU R                  U5       3/5        M.     UR                  S5        UR                  U5        U$ )Nsandboxz-c=z--)	_resolve_binary_determine_platformsorteddictr   itemsextend_format_overrideappend)r.   r   r   platform_argr   keyrW   s          r   r   *CodexSandboxExecutionPolicy._build_command   s    %%'//1#)9l"C d&;&;!<!B!B!DEJC#a0E0Ee0L/M'N OP FD!G$r   c                ~    [         R                  " U R                  5      nUc  Sn[        X R                  -  5      eU$ )NzQCodex sandbox policy requires the '%s' CLI to be installed and available on PATH.shutilwhichr   r]   r.   pathr/   s      r   r   +CodexSandboxExecutionPolicy._resolve_binary   s:    ||DKK(<c  s[[011r   c                    U R                   S:w  a  U R                   $ [        R                   R                  S5      (       a  g[        R                   S:X  a  gSn[        U5      e)Nr   r~   darwinmacoszYCodex sandbox policy could not determine a supported platform; set 'platform' explicitly.)r   r   r   r]   r-   s     r   r   /CodexSandboxExecutionPolicy._determine_platform   sV    ==F"== <<""7++<<8#) 	 3r   c                f     [         R                  " U 5      $ ! [         a    [        U 5      s $ f = frU   )jsondumps	TypeErrorr   )rW   s    r   r   ,CodexSandboxExecutionPolicy._format_override  s/    	::e$$ 	u:	s    00r3   Nr:   )r   r<   r8   	list[str]r8   r   )rW   ru   r8   r   )r>   r?   r@   rA   rB   r   rC   r   r   r   r   r5   r   r   r   r   r   rF   r3   r   r   r   r      s     FC9?H6?16t1L.L
 
 	

 
 

    r   r   c                    ^  \ rS rSr% SrSrS\S'   SrS\S'   SrS	\S
'   Sr	S	\S'   Sr
S\S'   SrS\S'   SrS\S'   SrS\S'   SrS	\S'   SrS\S'   SU 4S jjr        SS jr        S S jr\S!S j5       rS"S jrSrU =r$ )#DockerExecutionPolicyi
  a>  Run the shell inside a dedicated Docker container.

Choose this policy when commands originate from untrusted users or you require
strong isolation between sessions. By default the workspace is bind-mounted only
when it refers to an existing non-temporary directory; ephemeral sessions run
without a mount to minimise host exposure. The container's network namespace is
disabled by default (`--network none`) and you can enable further hardening via
`read_only_rootfs` and `user`.

The security guarantees depend on your Docker daemon configuration. Run the agent on
a host where Docker is locked down (rootless mode, AppArmor/SELinux, etc.) and
review any additional volumes or capabilities passed through ``extra_run_args``. The
default image is `python:3.12-alpine3.19`; supply a custom image if you need
preinstalled tooling.
dockerr   r   zpython:3.12-alpine3.19imageTrL   remove_container_on_exitFnetwork_enabledNzSequence[str] | Noneextra_run_argsr)   rK   ztyping.Any | NonerJ   z
str | Nonecpusread_only_rootfsuserc                  > [         TU ]  5         U R                  b  U R                  S::  a  Sn[        U5      eU R                  b  Sn[        U5      eU R                  b,  U R                  R                  5       (       d  Sn[        U5      eU R                  b,  U R                  R                  5       (       d  Sn[        U5      e[        U R                  =(       d    S5      U l
        g )Nr   rS   zxDockerExecutionPolicy does not support cpu_time_seconds; configure CPU limits using Docker run options such as '--cpus'.z.cpus must be a non-empty string when provided.z.user must be a non-empty string when provided.r3   )rZ   r0   rK   r,   rJ   r]   r   stripr   tupler   r^   s     r   r0   #DockerExecutionPolicy.__post_init__'  s    (T->->!-C>CS/!  ,=  s##99 ):):BCS/!99 ):):BCS/!#D$7$7$=2>r   c               |    U R                  XU5      n[        R                  R                  5       n[	        UUUS SS9$ r   )r   osenvironcopyr   )r.   r4   r   r   r   host_envs         r   r5   DockerExecutionPolicy.spawn:  sB     **97C::??$!#
 	
r   c                   U R                  5       nUSS/nU R                  (       a  UR                  S5        U R                  (       d  UR	                  SS/5        U R
                  b&  UR	                  S[        U R
                  5      /5        U R                  U5      (       a7  [        U5      nUR	                  SU SU 3/5        UR	                  S	U/5        OUR	                  S	S
/5        U R                  (       a  UR                  S5        UR                  5        H  u  pxUR	                  SU SU 3/5        M     U R                  b  UR	                  SU R                  /5        U R                  b  UR	                  SU R                  /5        U R                  (       a  UR	                  U R                  5        UR                  U R                  5        UR	                  U5        U$ )Nrunz-iz--rmz	--networknonez--memoryz-v:z-w/z--read-onlyz-er   z--cpusz--user)r   r   r   r   r   rK   r   _should_mount_workspacer   r   r   r   r   r   )	r.   r4   r   r   r   r   	host_pathr   rW   s	            r   r   $DockerExecutionPolicy._build_commandK  s    %%'#)5$"7(('##f 56(S1B1B-C DE''	22II)Ai['A BCy 12s,  .))+JC#aw'7 89 &99 499 5699 499 56 3 34DJJ'G$r   c                J    U R                   R                  [        5      (       + $ rU   )namer   SHELL_TEMP_PREFIX)r4   s    r   r   -DockerExecutionPolicy._should_mount_workspacem  s    >>,,->???r   c                ~    [         R                  " U R                  5      nUc  Sn[        X R                  -  5      eU$ )NzTDocker execution policy requires the '%s' CLI to be installed and available on PATH.r   r   s      r   r   %DockerExecutionPolicy._resolve_binaryq  s<    ||DKK(<*  s[[011r   )r   r7   r:   )r4   r   r   r;   r   r<   r8   r   )r4   r   r8   rL   r   )r>   r?   r@   rA   rB   r   rC   r   r   r   r   rK   rJ   r   r   r   r0   r5   r   r   r   r   rF   r   r   s   @r   r   r   
  s      FC)E3)%)d)!OT!+/N(/#L*#*.'.D*"d"D*?&
 
 	

 
 

"     	 
 
 D @ @ r   r   )r    r   r   rH   )r   r<   r   r;   r   r   r   r   r   rL   r8   r=   )rB   
__future__r   rD   r   r   r   r   r   rw   collections.abcr   r   dataclassesr   r   pathlibr   rk   r\   ImportErrorr   r   TYPE_CHECKINGABCr    rH   r   r   __all__r3   r   r   <module>r      s/   = " 
  	   
  - ( M
 '  
 
	
 1  0 
1 3#'' 3 3D _c- _c _cD H"5 H HV n/ n nbQ  Ms   C CC