
    g3fiy                    f   d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddl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mZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lm Z m!Z! ddl"m#Z# ddl$m%Z%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8 erddl9m:Z:m;Z; ddl<m=Z=  ej|                  e?      Z@dZAdZBdZC	 	 	 	 	 	 	 	 d&dZDe G d d             ZE G d de4      ZF ed       G d d             ZG G d  d      ZH G d! d"e       ZI G d# d$e3eFef         ZJg d%ZKy)'z:Middleware that exposes a persistent shell tool to agents.    )annotationsN)	dataclassfield)Path)TYPE_CHECKING	AnnotatedAnyLiteralcast)ToolMessage)ToolException)UntrackedValue)	BaseModelmodel_validator)SkipJsonSchema)NotRequiredoverride)SHELL_TEMP_PREFIXBaseExecutionPolicyCodexSandboxExecutionPolicyDockerExecutionPolicyHostExecutionPolicy)PIIDetectionErrorPIIMatchRedactionRuleResolvedRedactionRule)AgentMiddleware
AgentStatePrivateStateAttr)ToolRuntimetool)MappingSequence)Runtime__LC_SHELL_DONE__aG  Execute a shell command inside a persistent session. Before running a command, confirm the working directory is correct (e.g., inspect with `ls` or `pwd`) and ensure any parent directories exist. Prefer absolute paths and quote paths containing spaces, such as `cd "/path/with spaces"`. Chain multiple commands with `&&` or `;` instead of embedding newlines. Avoid unnecessary `cd` usage unless explicitly required so the session remains stable. Outputs may be truncated when they become very large, and long running commands will be terminated once their configured timeout elapses.shellShellSessionc                   t        j                  t              5  | j                  |       d d d        |3t        j                  t              5  |j	                          d d d        y y # 1 sw Y   ?xY w# 1 sw Y   y xY wN)
contextlibsuppress	Exceptionstopcleanup)sessiontempdirtimeouts      d/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain/agents/middleware/shell_tool.py_cleanup_resourcesr3   @   sp     
		Y	' W  + 	OO	 	  	 	s   A*A6*A36A?c                  V    e Zd ZU dZded<   ded<   ded<    edd	      Zd
ed<   ddZy)_SessionResourcesz&Container for per-run shell resources.r'   r/   'tempfile.TemporaryDirectory[str] | Noner0   r   policyF)initreprzweakref.finalize	finalizerc                    t        j                  | t        | j                  | j                  | j
                  j                        | _        y r)   )weakreffinalizer3   r/   r0   r7   termination_timeoutr:   selfs    r2   __post_init__z_SessionResources.__post_init__S   s5     ))LLLLKK++
    NreturnNone)__name__
__module____qualname____doc____annotations__r   r:   rA    rB   r2   r5   r5   J   s.    044"'U"?I?
rB   r5   c                      e Zd ZU dZded<   y)ShellToolStatez;Agent state extension for tracking shell session resources.zRNotRequired[Annotated[_SessionResources | None, UntrackedValue, PrivateStateAttr]]shell_session_resourcesNrF   rG   rH   rI   rJ   rK   rB   r2   rM   rM   ]   s    E rB   rM   T)frozenc                  X    e Zd ZU dZded<   ded<   ded<   ded<   ded	<   d
ed<   d
ed<   y)CommandExecutionResultz)Structured result from command execution.stroutput
int | None	exit_codebool	timed_outtruncated_by_linestruncated_by_bytesinttotal_linestotal_bytesNrO   rK   rB   r2   rR   rR   e   s-    3KOrB   rR   c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZddZddZddZddZ	 	 	 	 	 	 	 	 ddZ	ddZ
dd	Zdd
ZddZ	 d	 	 	 	 	 	 	 ddZedd       Zy)r'   zDPersistent shell session that supports sequential command execution.c                    || _         || _        || _        t        |      | _        d | _        d | _        t        j                         | _	        t        j                         | _        d | _        d | _        d| _        y )NF)
_workspace_policy_commanddict_environment_process_stdinqueueQueue_queue	threadingLock_lock_stdout_thread_stderr_thread_terminated)r@   	workspacer7   commandenvironments        r2   __init__zShellSession.__init__u   sg     $ -6:;@;;=^^%
7;7; rB   c                D   | j                   r| j                   j                         y| j                  j                  | j                  | j
                  | j                        | _         | j                   j                  ,| j                   j                  | j                   j                  d}t        |      | j                   j                  | _        d| _        t        j                         | _        t!        j"                  | j$                  | j                   j                  dfd      | _        t!        j"                  | j$                  | j                   j                  dfd      | _        | j&                  j+                          | j(                  j+                          y)	z.Start the shell subprocess and reader threads.N)rp   envrq   z)Failed to initialize shell session pipes.FstdoutT)targetargsdaemonstderr)re   pollra   spawnr`   rd   rb   stdinrv   rz   RuntimeErrorrf   ro   rg   rh   ri   rj   Thread_enqueue_streamrm   rn   startr@   msgs     r2   r   zShellSession.start   sB   ==T]]//19**oo!!MM + 
 MM'}}##+}}##+=Cs##mm)) kkm'..''--&&1

 (..''--&&1

 	!!#!!#rB   c                n    | j                  | j                  j                         | j                          y)zRestart the shell process.N)r-   ra   r>   r   r?   s    r2   restartzShellSession.restart   s!    		$,,223

rB   c                X   | j                   sy| j                   j                         B| j                  s6	 | j                  j	                  d       | j                  j                          	 | j                   j                  |      | j                          d| _        t        j                  t               5  | j                  j#                          ddd       d| _         y# t        t        f$ r t        j                  dd       Y w xY w# t        j                  $ r | j                          Y w xY w# 1 sw Y   d| _         yxY w# d| _        t        j                  t               5  | j                  j#                          ddd       d| _         w # 1 sw Y   d| _         w xY wxY w)zStop the shell subprocess.Nzexit
z8Failed to write exit command; terminating shell session.Texc_infor1   )re   r{   ro   rf   writeflushBrokenPipeErrorOSErrorLOGGERdebugwait_kill_process
subprocessTimeoutExpiredr*   r+   r,   close)r@   r1   s     r2   r-   zShellSession.stop   sR   }}=='0@0@!!(+!!#		!}}!!'!2:""$  $D$$Y/ $!!#$ DM $W- N!   (( 	! 	!$ DM  $D$$Y/ $!!#$ DM$ DMsY   5C" +,D 7D7"&D
D#D41E
 3D44E
 7E
!F)+FF)F&F)c                  | j                   r| j                   j                         d}t        |      t         t	        j
                         j                   }t        j                         |z   }| j                  5  | j                          |j                  d      r|n| d}	 | j                  j                  |       | j                  j                  d| d       | j                  j                          | j%                  |||      cddd       S # t        t         f$ r | j#                  |      cY cddd       S w xY w# 1 sw Y   yxY w)z*Execute a command in the persistent shell.NzShell session is not running.
zprintf 'z
 %s\n' $?
)re   r{   r~   _DONE_MARKER_PREFIXuuiduuid4hextime	monotonicrl   _drain_queueendswithrf   r   r   r   r   _collect_output_after_exit_collect_output)r@   rq   r1   r   markerdeadlinepayloads          r2   executezShellSession.execute   s2   }} 2 2 4 @1Cs##'()9)9(:;>>#g-ZZ 	C!(!1!1$!7gy^GA!!'*!!HVHL"AB!!# '''B	C 	C $W- A 66x@@	C 	CA	C 	Cs1   =)E'AD;E E7EEEEc           	        g }d}d}d}d}d }	d}
	 |t        j                         z
  }|dk  rd}
nT	 | j                  j                  |      \  }}|D|dk(  rX|j                  |      rG|j                  d      \  }}}| j                  |j                               }	| j                  ||       n|dz  }|j                  dd	      }|t        |      z  }|| j                  j                  kD  rd}| j                  j                  || j                  j                  kD  rd}|d
k(  rH|j!                  d      }|j#                  d|        |j%                  d      r#|j#                  d       n|j#                  |       t|
r8t&        j)                  d|       | j+                          t-        dd d||||      S dj/                  |      }t-        ||	d||||      S # t        j
                  $ r d}
Y tw xY w)Nr   FTr   rv       utf-8replacerz   r   	[stderr] z?Command timed out after %.2f seconds; restarting shell session. rT   rV   rX   rY   rZ   r\   r]   )r   r   ri   getrg   Empty
startswith	partition	_safe_intstrip_drain_remaining_stderrencodelenra   max_output_linesmax_output_bytesrstripappendr   r   warningr   rR   join)r@   r   r   r1   	collectedr\   r]   rY   rZ   rV   rX   	remainingsourcedata_statusencodedstrippedrT   s                      r2   r   zShellSession._collect_output   s!     "	"" $		 4>>#33IA~ 	#{{yA
 |!doof&=#~~c21f NN6<<>:	 ,,YA1Kkk'95G3w<'KT\\:::%)" --9$,,"?"??%)"!;;t,  9XJ!78==&$$T*  &Y \ NNQ LLN)#5#5''  #%11##
 	
q ;;  	s   G! !G98G9c           	     L   g }d}d}d}d}d}t        t        j                         |z   |      }	 |t        j                         z
  }	|	dk  rn	 | j                  j	                  |	      \  }
}|A|dz  }|j                  dd	      }|t        |      z  }|| j                  j                  kD  rd}| j                  j                  || j                  j                  kD  rd}|
d
k(  rH|j                  d      }|j                  d|        |j                  d      r#|j                  d       n|j                  |       d}| j                  r| j                  j!                         }dj#                  |      }t%        ||d||||      S # t
        j                  $ r Y `w xY w)a  Collect output after the shell exited unexpectedly.

        Called when a `BrokenPipeError` occurs while writing to stdin, indicating the
        shell process terminated (e.g., due to an 'exit' command).

        Args:
            deadline: Absolute time by which collection must complete.

        Returns:
            `CommandExecutionResult` with collected output and the process exit code.
        r   Fg?Tr   Nr   r   r   rz   r   r   r   r   )minr   r   ri   r   rg   r   r   r   ra   r   r   r   r   r   re   r{   r   rR   )r@   r   r   r\   r]   rY   rZ   drain_timeoutdrain_deadliner   r   r   r   r   rV   rT   s                   r2   r   z'ShellSession._collect_output_after_exit8  s     "	"" T^^-=xH&)99IA~#{{yA |1Kkk'95G3w<'KT\\:::%)" --9$,,"?"??%)"!;;t,  9XJ!78==&$$T*  &E J !%	==**,I#%11##
 	
I ;; s   F F#"F#c                   | j                   sy t        t        d      rnt        j                  t
              5  t        j                  t        j                  | j                   j                        t        j                         d d d        y t        j                  t
              5  | j                   j                          d d d        y # 1 sw Y   y xY w# 1 sw Y   y xY w)Nkillpg)re   hasattrosr*   r+   ProcessLookupErrorr   getpgidpidsignalSIGKILLkillr?   s    r2   r   zShellSession._kill_process  s    }}2x $$%78 I		"**T]]%6%67HI I $$%78 %""$% %I I% %s   AC	%C	CCc                    t        |j                  d      D ]  }| j                  j                  ||f       ! | j                  j                  |d f       y )Nr   )iterreadlineri   put)r@   streamlabellines       r2   r   zShellSession._enqueue_stream  sD    "- 	+DKKOOUDM*	+&rB   c                n    	 	 | j                   j                          # t        j                  $ r Y y w xY wr)   )ri   
get_nowaitrg   r   r?   s    r2   r   zShellSession._drain_queue  s7    &&(  ;; s    44c                   t        t        j                         |z   |      }	 |t        j                         z
  }|dk  ry	 | j                  j	                  |      \  }}||dk7  rF|j                  d      }|j                  d|        |j                  d      r|j                  d       # t
        j                  $ r Y yw xY w)a\  Drain any stderr output that arrived concurrently with the done marker.

        The stdout and stderr reader threads run independently. When a command writes to
        stderr just before exiting, the stderr output may still be in transit when the
        done marker arrives on stdout. This method briefly polls the queue to capture
        such output.

        Args:
            collected: The list to append collected stderr lines to.
            deadline: The original command deadline (used as an upper bound).
            drain_timeout: Maximum time to wait for additional stderr output.
        r   r   Nrz   r   r   )
r   r   r   ri   r   rg   r   r   r   r   )	r@   r   r   r   r   r   r   r   r   s	            r2   r   z$ShellSession._drain_remaining_stderr  s     T^^-=xH&)99IA~#{{yA |v1{{4(Hy
34}}T"  &  ;; s   B0 0CCc                v    t        j                  t              5  t        |       cd d d        S # 1 sw Y   y xY wr)   )r*   r+   
ValueErrorr[   )values    r2   r   zShellSession._safe_int  s/      , 	u:	 	 	s   /8N)
rp   r   r7   r   rq   tuple[str, ...]rr   zMapping[str, str]rD   rE   rC   )r1   floatrD   rE   )rq   rS   r1   r   rD   rR   )r   rS   r   r   r1   r   rD   rR   )r   r   rD   rR   )r   r	   r   rS   rD   rE   )g?)r   z	list[str]r   r   r   r   rD   rE   )r   rS   rD   rU   )rF   rG   rH   rI   rs   r   r   r-   r   r   r   r   r   r   r   staticmethodr   rK   rB   r2   r'   r'   r   s    N!! $! !	!
 '! 
!&!$F
!4C.U
U
 U
 	U

 
 U
nH
T	%'
 MQ'"'.3'DI'	'>  rB   c                  b    e Zd ZU dZdZded<   	 dZded<   	 dZded<   	  ed	
      dd       Z	y)_ShellToolInputz+Input schema for the persistent shell tool.N
str | Nonerq   zbool | Noner   z Annotated[Any, SkipJsonSchema()]runtimeafter)modec                    | j                   | j                  sd}t        |      | j                   | j                  rd}t        |      | S )Nz2Shell tool requires either 'command' or 'restart'.z+Specify only one of 'command' or 'restart'.)rq   r   r   r   s     r2   validate_payloadz _ShellToolInput.validate_payload  sE    <<FCS/!<<#?CS/!rB   )rD   r   )
rF   rG   rH   rI   rq   rJ   r   r   r   r   rK   rB   r2   r   r     sH    5GZ'G[/04G-4 '" #rB   r   c            
      @    e Zd ZdZeZ	 ddddddeddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZe	 	 	 	 dd       Z	e	 	 	 	 dd       Z
edd       Zedd       Zdd	Zedd
       ZddZddZddZddZddZddZ	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 	 	 d dZ xZS )!ShellToolMiddlewareu  Middleware that registers a persistent shell tool for agents.

    The middleware exposes a single long-lived shell session. Use the execution policy
    to match your deployment's security posture:

    * `HostExecutionPolicy` – full host access; best for trusted environments where the
        agent already runs inside a container or VM that provides isolation.
    * `CodexSandboxExecutionPolicy` – reuses the Codex CLI sandbox for additional
        syscall/filesystem restrictions when the CLI is available.
    * `DockerExecutionPolicy` – launches a separate Docker container for each agent run,
        providing harder isolation, optional read-only root filesystems, and user
        remapping.

    When no policy is provided the middleware defaults to `HostExecutionPolicy`.
    N)startup_commandsshutdown_commandsexecution_policyredaction_rulestool_description	tool_nameshell_commandru   c                   t                    |rt        |      nd _        | _         j                  |       _         j                  |	       _        || _	        nt                _	        |xs d}
t        d |
D               _         j                  |       _         j                  |       _        |xs t         }t#         j                  t$        |      ddd	 	 	 	 	 	 	 d fd       }| _         j&                  g _        y)	a  Initialize an instance of `ShellToolMiddleware`.

        Args:
            workspace_root: Base directory for the shell session.

                If omitted, a temporary directory is created when the agent starts and
                removed when it ends.
            startup_commands: Optional commands executed sequentially after the session
                starts.
            shutdown_commands: Optional commands executed before the session shuts down.
            execution_policy: Execution policy controlling timeouts, output limits, and
                resource configuration.

                Defaults to `HostExecutionPolicy` for native execution.
            redaction_rules: Optional redaction rules to sanitize command output before
                returning it to the model.

                !!! warning
                    Redaction rules are applied post execution and do not prevent
                    exfiltration of secrets or sensitive data when using
                    `HostExecutionPolicy`.

            tool_description: Optional override for the registered shell tool
                description.
            tool_name: Name for the registered shell tool.

                Defaults to `"shell"`.
            shell_command: Optional shell executable (string) or argument sequence used
                to launch the persistent session.

                Defaults to an implementation-defined bash command.
            env: Optional environment variables to supply to the shell session.

                Values are coerced to strings before command execution. If omitted, the
                session inherits the parent process environment.
        NrK   c              3  <   K   | ]  }|j                           y wr)   )resolve).0rules     r2   	<genexpr>z/ShellToolMiddleware.__init__.<locals>.<genexpr>(  s      I
#DLLNI
s   )args_schemadescriptionFrq   r   c                |    j                  | j                        }j                  |||d| j                        S )Nr   )tool_call_id)_get_or_create_resourcesstate_run_shell_toolr   )r   rq   r   	resourcesr@   s       r2   
shell_toolz0ShellToolMiddleware.__init__.<locals>.shell_tool1  sE     55gmmDI''#8$11 (  rB   )r   z!ToolRuntime[None, ShellToolState]rq   r   r   rW   rD   ToolMessage | str)superrs   r   _workspace_root
_tool_name_normalize_shell_command_shell_command_normalize_envrd   _execution_policyr   tuple_redaction_rules_normalize_commands_startup_commands_shutdown_commandsDEFAULT_TOOL_DESCRIPTIONr!   r   _shell_tooltools)r@   workspace_rootr   r   r   r   r   r   r   ru   rulesr   r  	__class__s   `            r2   rs   zShellToolMiddleware.__init__  s.   b 	7EtN34#";;MJ //4'%5D"%8%:D"%2CH I
',I
 D
 "&!9!9:J!K"&":":;L"M 'B*B	doo?	T #'!		6	  	 		
 	 
U	 &&&'
rB   c                D    | yt        | t              r| fS t        |       S )NrK   )
isinstancerS   r  )commandss    r2   r  z'ShellToolMiddleware._normalize_commandsB  s)     h$;XrB   c                f    | yt        | t              r| fn
t        |       }|sd}t        |      |S )N)z	/bin/bashz1Shell command must contain at least one argument.)r  rS   r  r   )r   
normalizedr   s      r2   r  z,ShellToolMiddleware._normalize_shell_commandL  s>      !)3M3)Gm%US`Ma
ECS/!rB   c                    | y i }| j                         D ]0  \  }}t        |t              sd}t        |      t        |      ||<   2 |S )Nz+Environment variable names must be strings.)itemsr  rS   	TypeError)ru   r  keyr   r   s        r2   r	  z"ShellToolMiddleware._normalize_envX  sV    ;%'
))+ 	)JCc3'Cn$!%jJsO		)
 rB   c                ,    | j                  |      }d|iS )z1Start the shell session and run startup commands.rN   )r   r@   r   r   r  s       r2   before_agentz ShellToolMiddleware.before_agentd  s     11%8	)955rB   c                .   K   | j                  ||      S w)z7Async start the shell session and run startup commands.)r!  r@   r   r   s      r2   abefore_agentz!ShellToolMiddleware.abefore_agentj  s       00   c                    |j                  d      }t        |t              sy	 | j                  |j                         |j                          y# |j                          w xY w)zDRun shutdown commands and release resources when an agent completes.rN   N)r   r  r5   _run_shutdown_commandsr/   r:   r   s       r2   after_agentzShellToolMiddleware.after_agentn  sS     II78	)%67	"''	(9(9:!I!s   A A"c                .   K   | j                  ||      S w)zJAsync run shutdown commands and release resources when an agent completes.)r(  r#  s      r2   aafter_agentz ShellToolMiddleware.aafter_agentz  s     w//r%  c                    |j                  d      }t        |t              r|S | j                         }|t	        d|      d<   |S )a  Get existing resources from state or create new ones if they don't exist.

        This method enables resumability by checking if resources already exist in the state
        (e.g., after an interrupt), and only creating new resources if they're not present.

        Args:
            state: The agent state which may contain shell session resources.

        Returns:
            Session resources, either retrieved from state or newly created.
        rN   dict[str, Any])r   r  r5   _create_resourcesr   )r@   r   r  new_resourcess       r2   r   z,ShellToolMiddleware._get_or_create_resources~  sK     II78	i!23..0CPu%&?@rB   c                `   | j                   }d }|0t        j                  t              }t	        |j
                        }n|}|j                  dd       t        || j                  | j                  | j                  xs i       }	 |j                          t        j                  d|       | j                  |       t)        ||| j                        S # t        $ rN t        j!                  d       |j#                  | j                  j$                         ||j'                           w xY w)N)prefixT)parentsexist_okzStarted shell session in %sz5Starting shell session failed; cleaning up resources.)r/   r0   r7   )r  tempfileTemporaryDirectoryr   r   namemkdirr'   r
  r  rd   r   r   info_run_startup_commandsBaseException	exceptionr-   r>   r.   r5   )r@   rp   r0   workspace_pathr/   s        r2   r-  z%ShellToolMiddleware._create_resources  s	   ((	;?119JKG!',,/N&N   =""#	
		MMOKK5~F&&w/ !'$J`J`aa  	TULL//CCD"!	s   7C AD-c                    | j                   sy | j                   D ]`  }|j                  || j                  j                        }|j                  s|j
                  dvsEd| d|j
                   }t        |       y )Nr      Nr   zStartup command 'z' failed with exit code )r  r   r
  startup_timeoutrX   rV   r~   )r@   r/   rq   resultr   s        r2   r8  z)ShellToolMiddleware._run_startup_commands  s|    %%-- 	(G__Wd6L6L6\6\_]FF$4$4I$E)'2J6K[K[J\]"3''		(rB   c                   | j                   sy | j                   D ]|  }	 |j                  || j                  j                        }|j                  rt
        j                  d|       n/|j                  dvr!t
        j                  d||j                         ~ y # t        t        t        f$ r#}t
        j                  d||d       Y d }~d }~ww xY w)Nr   z Shutdown command '%s' timed out.r=  z%Shutdown command '%s' exited with %s.z'Failed to run shutdown command '%s': %sTr   )r  r   r
  command_timeoutrX   r   r   rV   r~   r   r   )r@   r/   rq   r?  excs        r2   r'  z*ShellToolMiddleware._run_shutdown_commands  s    &&.. 	G $:P:P:`:`a##NN#EwO%%Y6NN?&JZJZ	 !-9 =wVZ   s   A9BC.CCc                    i }|}| j                   D ]D  }|j                  |      \  }}|s|j                  |j                  g       j	                  |       F ||fS )z3Apply configured redaction rules to command output.)r  apply
setdefaultpii_typeextend)r@   contentmatches_by_typeupdatedr   matchess         r2   _apply_redactionsz%ShellToolMiddleware._apply_redactions  se    57)) 	ND#zz'2GW**4=="=DDWM	N ''rB   c          
         |j                   }|j                  d      rMt        j                  d       	 |j	                          | j                  |       d}| j                  ||d      S |j                  d      }|rt        |t              sd	}t        |      t        j                  d
|       |j                  || j                  j                        }	|	j                  r5| j                  j                  }
d|
dd}| j                  ||ddd d      S 	 | j!                  |	j"                        \  }}|xs d}|	j.                  r7|j1                          d| j                  j2                   d|	j4                   d}|	j6                  rM| j                  j8                  7|j1                          d| j                  j8                   d|	j:                   d}|	j*                  dvr"|j1                          d|	j*                   }d}nd}d|	j*                  |	j.                  |	j6                  |	j4                  |	j:                  |d}| j                  ||||      S # t        $ r(}t        j                  d       d}t        |      |d }~ww xY w# t$        $ rs}t        j'                  d|j(                         d|j(                   d}| j                  ||dd|	j*                  |j(                  |j,                  id      cY d }~S d }~ww xY w) Nr   z$Restarting shell session on request.z=Restarting shell session failed; session remains unavailable.z Failed to restart shell session.zShell session restarted.success)r   rq   zDShell tool expects a 'command' string when restart is not requested.zExecuting shell command: %sr   zError: Command timed out after z.1fz	 seconds.errorT)rX   rV   )r   artifactz+Blocking command output due to detected %s.zOutput blocked: detected .F)rX   rV   rK  z<no output>z

... Output truncated at z lines (observed z).z bytes (observed r=  z

Exit code: )rX   rV   rY   rZ   r\   r]   redaction_matches)r/   r   r   r7  r   r8  r9  r:  r   _format_tool_messager  rS   r   r
  rA  rX   rL  rT   r   r   rF  rV   rK  rY   r   r   r\   rZ   r   r]   )r@   r  r   r   r/   errr   messagerq   r?  timeout_secondssanitized_outputrK  rO  final_statusrP  s                   r2   r   z#ShellToolMiddleware._run_shell_tool  s6    ##;;y!KK>?2!**73
 1G,,Wl9,UU++i(j#6XC$$17;$2H2H2X2XY"44DDO77LIVG,,!%!%	 -  	(,(>(>v}}(M%g ,<}$$#**,- .++/+A+A+R+R*S T#//04 
 $$)?)?)P)P)\#**,- .++/+A+A+R+R*S T#//04  9,"2"9"9";!<OFL\L\K]^8?L$L ))"(";";"(";";!--!--!(
 ((	 ) 
 	
U ! 2  !`a8#C(c12< ! 	NNH%..Y1%..1ACG,,!&!'!1!1 %>	 - 	 		s6   !I J 	J&#J		J	LA(LLL)rP  c               J    |xs i }||S t        ||| j                  ||      S )N)rH  r   r5  r   rP  )r   r  )r@   rH  r   r   rP  s        r2   rS  z(ShellToolMiddleware._format_tool_message3  s8     >rN%
 	
rB   r)   )r  zstr | Path | Noner   (tuple[str, ...] | list[str] | str | Noner   rZ  r   zBaseExecutionPolicy | Noner   z6tuple[RedactionRule, ...] | list[RedactionRule] | Noner   r   r   rS   r   Sequence[str] | str | Noneru   Mapping[str, Any] | NonerD   rE   )r  rZ  rD   r   )r   r[  rD   r   )ru   r\  rD   zdict[str, str] | None)r   rM   r   r$   rD   dict[str, Any] | None)r   rM   r   r$   rD   rE   )r   rM   rD   r5   )rD   r5   )r/   r'   rD   rE   )rH  rS   rD   z%tuple[str, dict[str, list[PIIMatch]]])r  r5   r   r,  r   r   rD   r	   )
rH  rS   r   r   r   zLiteral['success', 'error']rP  r]  rD   r  )rF   rG   rH   rI   rM   state_schemaSHELL_TOOL_NAMErs   r   r  r  r	  r   r!  r$  r(  r*  r   r-  r8  r'  rL  r   rS  __classcell__)r  s   @r2   r   r     s     "L -1S( FJFJ7;RV'+(48(,S()S( C	S(
 DS( 5S( PS( %S( S( 2S( &S( 
S(j :	  	1			 	 	 	 6 6
1 	" 	"0*b:("(]
$]
  ]

 !]
 
]
J +/

 !

 ,
 (
 

rB   r   )r   r   r   r   r   )r/   r'   r0   r6   r1   r   rD   rE   )LrI   
__future__r   r*   loggingr   rg   r   r   r3  rj   r   r   r<   dataclassesr   r   pathlibr   typingr   r   r	   r
   r   langchain_core.messagesr   langchain_core.tools.baser   "langgraph.channels.untracked_valuer   pydanticr   r   pydantic.json_schemar   typing_extensionsr   r   &langchain.agents.middleware._executionr   r   r   r   r   &langchain.agents.middleware._redactionr   r   r   r   !langchain.agents.middleware.typesr   r   r   langchain.toolsr    r!   collections.abcr"   r#   langgraph.runtimer$   	getLoggerrF   r   r   r  r_  r3   r5   rM   rR   r'   r   r   __all__rK   rB   r2   <module>rt     s@   @ "   	         (  ? ? / 3 = / / 3   \ [ -1) 
		8	$) Q  $KV[	 
 
 
$Z  $	 	 	J JZ
i 6j
/.#*=> j
ZrB   