
    ih                    ~   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
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  SSKJrJrJrJr  SSKJr  SSKJrJr  SS	KJr  SS
KJ r J!r!  SSK"J#r#  SSK$J%r%J&r&J'r'J(r(J)r)  SSK*J+r+J,r,J-r-J.r.  SSK/J0r0J1r1J2r2  \(       a  SSK3J4r4J5r5  SSK6J7r7  SSK8J9r9  SSK/J:r:  \Rv                  " \<5      r=Sr>Sr?        S'S jr@\ " S S5      5       rA " S S\15      rB\" SS9 " S S5      5       rC " S S5      rD " S  S!\ 5      rE " S" S#\5      rF " S$ S%\0\B\4   5      rG/ S&QrHg)(z:Middleware that exposes a persistent shell tool to agents.    )annotationsN)	dataclassfield)Path)TYPE_CHECKING	AnnotatedAnyLiteral)ToolMessage)BaseToolToolException)UntrackedValue)	BaseModelmodel_validator)NotRequired)SHELL_TEMP_PREFIXBaseExecutionPolicyCodexSandboxExecutionPolicyDockerExecutionPolicyHostExecutionPolicy)PIIDetectionErrorPIIMatchRedactionRuleResolvedRedactionRule)AgentMiddleware
AgentStatePrivateStateAttr)MappingSequence)Runtime)Command)ToolCallRequest__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.ShellSessionc                   [         R                  " [        5         U R                  U5        S S S 5        Ub4  [         R                  " [        5         UR	                  5         S S S 5        g g ! , (       d  f       NF= f! , (       d  f       g = fN)
contextlibsuppress	Exceptionstopcleanup)sessiontempdirtimeouts      `/home/james-whalen/.local/lib/python3.13/site-packages/langchain/agents/middleware/shell_tool.py_cleanup_resourcesr0   @   s`     
		Y	'W 
(  +OO ,+  
(	' ,+s   A-A>-
A;>
Bc                  Z    \ rS rSr% SrS\S'   S\S'   S\S'   \" S	S	S
9rS\S'   SS jrSr	g)_SessionResourcesJ   z&Container for per-run shell resources.r$   r,   'tempfile.TemporaryDirectory[str] | Noner-   r   policyF)initreprzweakref.finalize
_finalizerc                    [         R                  " U [        U R                  U R                  U R
                  R                  5      U l        g r&   )weakreffinalizer0   r,   r-   r5   termination_timeoutr8   selfs    r/   __post_init___SessionResources.__post_init__S   s5    !**LLLLKK++
    )r8   NreturnNone)
__name__
__module____qualname____firstlineno____doc____annotations__r   r8   r?   __static_attributes__ rA   r/   r2   r2   J   s.    044#(e%#@J @
rA   r2   c                  $    \ rS rSr% SrS\S'   Srg)ShellToolState]   z;Agent state extension for tracking shell session resources.zRNotRequired[Annotated[_SessionResources | None, UntrackedValue, PrivateStateAttr]]shell_session_resourcesrL   NrE   rF   rG   rH   rI   rJ   rK   rL   rA   r/   rN   rN   ]   s    E rA   rN   T)frozenc                  `    \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   S\S'   Srg)CommandExecutionResulte   z)Structured result from command execution.stroutput
int | None	exit_codebool	timed_outtruncated_by_linestruncated_by_bytesinttotal_linestotal_bytesrL   NrQ   rL   rA   r/   rT   rT   e   s-    3KOrA   rT   c                      \ rS rSrSr          SS jrSS jrSS jrSS jrSS jr	        SS jr
SS	 jrSS
 jrSS jr\SS j5       rSrg)r$   r   zDPersistent shell session that supports sequential command execution.c                    Xl         X l        X0l        [        U5      U l        S U l        S U l        [        R                  " 5       U l	        [        R                  " 5       U l        S U l        S U l        SU l        g )NF)
_workspace_policy_commanddict_environment_process_stdinqueueQueue_queue	threadingLock_lock_stdout_thread_stderr_thread_terminated)r>   	workspacer5   commandenvironments        r/   __init__ShellSession.__init__u   sa     $ -6:;@;;=^^%
7;7; rA   c                J   U R                   (       a  U R                   R                  5       c  gU R                  R                  U R                  U R
                  U R                  S9U l         U R                   R                  b.  U R                   R                  b  U R                   R                  c  Sn[        U5      eU R                   R                  U l        SU l        [        R                  " 5       U l        [         R"                  " U R$                  U R                   R                  S4SS9U l        [         R"                  " U R$                  U R                   R                  S4SS9U l        U R&                  R+                  5         U R(                  R+                  5         g)	z.Start the shell subprocess and reader threads.N)rt   envru   z)Failed to initialize shell session pipes.FstdoutT)targetargsdaemonstderr)ri   pollre   spawnrd   rh   rf   stdinr{   r   RuntimeErrorrj   rs   rk   rl   rm   rn   Thread_enqueue_streamrq   rr   startr>   msgs     r/   r   ShellSession.start   sB   ==T]]//19**oo!!MM + 
 MM'}}##+}}##+=Cs##mm)) kkm'..''--&&1

 (..''--&&1

 	!!#!!#rA   c                n    U R                  U R                  R                  5        U R                  5         g)zRestart the shell process.N)r*   re   r<   r   r=   s    r/   restartShellSession.restart   s!    		$,,223

rA   c                r   U R                   (       d  gU R                   R                  5       cG  U R                  (       d6   U R                  R	                  S5        U R                  R                  5          U R                   R                  US9c  U R                  5         SU l        [        R                  " [         5         U R                  R#                  5         SSS5        SU l         g! [        [        4 a    [        R                  SSS9   Nf = f! [        R                   a    U R                  5          Nf = f! , (       d  f       Nj= f! SU l        [        R                  " [         5         U R                  R#                  5         SSS5        SU l         f ! , (       d  f       SU l         f = f= f)zStop the shell subprocess.Nzexit
z8Failed to write exit command; terminating shell session.Texc_infor.   )ri   r   rs   rj   writeflushBrokenPipeErrorOSErrorLOGGERdebugwait_kill_process
subprocessTimeoutExpiredr'   r(   r)   close)r>   r.   s     r/   r*   ShellSession.stop   sG   }}=='0@0@!!(+!!#		!}}!!'!2:""$  $D$$Y/!!# 0 DM $W- N!   (( 	! 	! 0/  $D$$Y/!!# 0 DM 0/ DMsZ    5C, 6*D E ,$DD$D=:E <D==E  
E"F63FF6
F3(F6c               f   U R                   (       a  U R                   R                  5       b  Sn[        U5      e[         [        R
                  " 5       R                   3n[        R                  " 5       U-   nU R                     U R                  5         UR                  S5      (       a  UOU S3nU R                  R                  U5        U R                  R                  SU S35        U R                  R                  5         U R                  XEU5      sSSS5        $ ! , (       d  f       g= f)z*Execute a command in the persistent shell.NzShell session is not running.
zprintf 'z
 %s\n' $?
)ri   r   r   _DONE_MARKER_PREFIXuuiduuid4hextime	monotonicrp   _drain_queueendswithrj   r   r   _collect_output)r>   ru   r.   r   markerdeadlinepayloads          r/   executeShellSession.execute   s    }} 2 2 4 @1Cs##'()9)9(:;>>#g-ZZ!(!1!1$!7!7gy^GKKg&KK=>KK'''B ZZs   BD""
D0c           
        / nSnSnSnSnS n	Sn
 U[         R                  " 5       -
  nUS::  a  Sn
GOO U R                  R                  US9u  pUc  MD  US:X  aJ  UR                  U5      (       a4  UR                  S5      u    pU R                  UR                  5       5      n	OUS-  nUR                  SS	5      nU[        U5      -  nXPR                  R                  :  a  SnM  U R                  R                  b  X`R                  R                  :  a  SnGM  US
:X  aM  UR                  S5      nUR!                  SU 35        UR#                  S5      (       a  UR!                  S5        OUR!                  U5        GMq  U
(       a5  [$        R'                  SU5        U R)                  5         [+        SS SUUUUS9$ SR-                  U5      n[+        UU	SUUUUS9$ ! [        R
                   a    Sn
 Mu  f = f)Nr   FTr   r{       zutf-8replacer   r   z	[stderr] z?Command timed out after %.2f seconds; restarting shell session. )rW   rY   r[   r\   r]   r_   r`   )r   r   rm   getrk   Empty
startswith	partition	_safe_intstripencodelenre   max_output_linesmax_output_bytesrstripappendr   r   warningr   rT   join)r>   r   r   r.   	collectedr_   r`   r\   r]   rY   r[   	remainingsourcedata_statusencodedstrippedrW   s                      r/   r   ShellSession._collect_output   s     "	"" $		 4>>#33IA~ 	#{{yA
 |!doof&=&=#~~c21 NN6<<>:	1Kkk'95G3w<'K\\:::%)" --9,,"?"??%)"!;;t,  9XJ!78==&&$$T*  &Q T NNQ LLN)#5#5''  #%11##
 	
i ;;  	s   G G76G7c                   U R                   (       d  g [        [        S5      (       aq  [        R                  " [
        5         [        R                  " [        R                  " U R                   R                  5      [        R                  5        S S S 5        g [        R                  " [
        5         U R                   R                  5         S S S 5        g ! , (       d  f       g = f! , (       d  f       g = f)Nkillpg)ri   hasattrosr'   r(   ProcessLookupErrorr   getpgidpidsignalSIGKILLkillr=   s    r/   r   ShellSession._kill_process/  s    }}2x  $$%78		"**T]]%6%67H 98 $$%78""$ 98 98 98s   AC3C(
C%(
C6c                    [        UR                  S5       H  nU R                  R                  X#45        M!     U R                  R                  US 45        g )Nr   )iterreadlinerm   put)r>   streamlabellines       r/   r   ShellSession._enqueue_stream:  s=    "-DKKOOUM* .&rA   c                r      U R                   R                  5         M  ! [        R                   a     g f = fr&   )rm   
get_nowaitrk   r   r=   s    r/   r   ShellSession._drain_queue?  s7    &&(  ;; s    66c                    [         R                  " [        5         [        U 5      sS S S 5        $ ! , (       d  f       g = fr&   )r'   r(   
ValueErrorr^   )values    r/   r   ShellSession._safe_intF  s&      ,u: -,,s   0
>)rf   rh   rp   re   ri   rm   rr   rj   rq   rs   rd   N)
rt   r   r5   r   ru   tuple[str, ...]rv   zMapping[str, str]rC   rD   rB   )r.   floatrC   rD   )ru   rV   r.   r   rC   rT   )r   rV   r   r   r.   r   rC   rT   )r   r	   r   rV   rC   rD   )r   rV   rC   rX   )rE   rF   rG   rH   rI   rw   r   r   r*   r   r   r   r   r   staticmethodr   rK   rL   rA   r/   r$   r$   r   s    N!! $! !	!
 '! 
!&!$F
!4C$Q
Q
 Q
 	Q

 
 Q
f	%'
  rA   c                  R    \ rS rSr% SrSrS\S'   SrS\S'   \" SS	9SS
 j5       r	Sr
g)_ShellToolInputiM  z+Input schema for the persistent shell tool.N
str | Noneru   zbool | Noner   after)modec                    U R                   c  U R                  (       d  Sn[        U5      eU R                   b  U R                  (       a  Sn[        U5      eU $ )Nz2Shell tool requires either 'command' or 'restart'.z+Specify only one of 'command' or 'restart'.)ru   r   r   r   s     r/   validate_payload _ShellToolInput.validate_payloadS  sE    <<FCS/!<<#?CS/!rA   rL   )rC   r   )rE   rF   rG   rH   rI   ru   rJ   r   r   r   rK   rL   rA   r/   r   r   M  s0    5GZG['" #rA   r   c                  j   ^  \ rS rSr% SrSrS\S'   \rS\S'   \	r
S\S'   SSU 4S	 jjjrSS
 jrSrU =r$ )_PersistentShellTooli^  zBTool wrapper that relies on middleware interception for execution.shellrV   namedescriptionztype[BaseModel]args_schemac                B   > [         TU ]  5         Xl        Ub  X l        g g r&   )superrw   _middlewarer   )r>   
middlewarer   	__class__s      r/   rw   _PersistentShellTool.__init__e  s%    %"* #rA   c                    Sn[        U5      e)NzNPersistent shell tool execution should be intercepted via middleware wrappers.)r   )r>   r   r   s      r/   _run_PersistentShellTool._runk  s    ^3rA   )r   r   r&   )r   ShellToolMiddlewarer   r   rC   rD   )r   r	   rC   r	   )rE   rF   rG   rH   rI   r   rJ   DEFAULT_TOOL_DESCRIPTIONr   r   r   rw   r   rK   __classcell__r   s   @r/   r   r   ^  s8    LD#/K/#2K2+ +   rA   r   c            	        ^  \ rS rSrSr\r SSSSSSSSS.                 SU 4S jjjjr\    SS j5       r	\    SS j5       r
\SS j5       rSS	 jrSS
 jrSS jrSS jrSS jrS S jrS!S jrS!S jrS"S jr        S#S jr      S$S jr      S%S jrSS.         S&S jjrSrU =r$ )'r   ip  a  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shell_commandrz   c                 > [         TU ]  5         U(       a  [        U5      OSU l        U R	                  U5      U l        U R                  U5      U l        Ub  X@l        O[        5       U l        U=(       d    Sn	[        S U	 5       5      U l        U R                  U5      U l        U R                  U5      U l        U=(       d    [        n
[!        X
S9U l        U R"                  /U l        g)a^  Initialize the middleware.

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 :class:`HostExecutionPolicy` for native execution.
    redaction_rules: Optional redaction rules to sanitize command output before
        returning it to the model.
    tool_description: Optional override for the registered shell tool description.
    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.
NrL   c              3  @   #    U  H  oR                  5       v   M     g 7fr&   )resolve).0rules     r/   	<genexpr>/ShellToolMiddleware.__init__.<locals>.<genexpr>  s      I
',tLLNNus   )r   )r   rw   r   _workspace_root_normalize_shell_command_shell_command_normalize_envrh   _execution_policyr   tuple_redaction_rules_normalize_commands_startup_commands_shutdown_commandsr   r   _tooltools)r>   workspace_rootr  r  r  r  r  r  rz   rulesr   r   s              r/   rw   ShellToolMiddleware.__init__  s    < 	7EtN34";;MJ //4'%5"%8%:D"%2CH I
',I
 D
 "&!9!9:J!K"&":":;L"M&B*B)$H
jj\
rA   c                P    U c  g[        U [        5      (       a  U 4$ [        U 5      $ )NrL   )
isinstancerV   r  )commandss    r/   r  'ShellToolMiddleware._normalize_commands  s,     h$$;XrA   c                |    U c  g[        U [        5      (       a  U 4O
[        U 5      nU(       d  Sn[        U5      eU$ )N)z	/bin/bashz1Shell command must contain at least one argument.)r  rV   r  r   )r  
normalizedr   s      r/   r  ,ShellToolMiddleware._normalize_shell_command  sA      !)3M3)G)Gm%US`Ma
ECS/!rA   c                    U c  g 0 nU R                  5        H4  u  p#[        U[        5      (       d  Sn[        U5      e[        U5      X'   M6     U$ )Nz+Environment variable names must be strings.)itemsr  rV   	TypeError)rz   r#  keyr   r   s        r/   r  "ShellToolMiddleware._normalize_env  sP    ;%'
))+JCc3''Cn$!%jJO	 &
 rA   c                *    U R                  5       nSU0$ )z1Start the shell session and run startup commands.rP   )_create_resourcesr>   stateruntime	resourcess       r/   before_agent ShellToolMiddleware.before_agent  s    **,	)955rA   c                ,   #    U R                  X5      $ 7f)z$Async counterpart to `before_agent`.)r0  r>   r-  r.  s      r/   abefore_agent!ShellToolMiddleware.abefore_agent  s       00   c                    U R                  U5      n U R                  UR                  5        UR                  5         g! UR                  5         f = f)zDRun shutdown commands and release resources when an agent completes.N)_ensure_resources_run_shutdown_commandsr,   r8   r,  s       r/   after_agentShellToolMiddleware.after_agent  sD    **51		#''	(9(9:  "I  "s	   ? Ac                ,   #    U R                  X5      $ 7f)z#Async counterpart to `after_agent`.)r:  r3  s      r/   aafter_agent ShellToolMiddleware.aafter_agent  s     //r6  c                |    UR                  S5      nUb  [        U[        5      (       d  S nUc  Sn[        U5      eU$ )NrP   zoShell session resources are unavailable. Ensure `before_agent` ran successfully before invoking the shell tool.)r   r  r2   r   )r>   r-  r/  r   s       r/   r8  %ShellToolMiddleware._ensure_resources  sL    II78	 I?P)Q)QI2   $$rA   c                ^   U R                   nS nUc.  [        R                  " [        S9n[	        UR
                  5      nOUnUR                  SSS9  [        UU R                  U R                  U R                  =(       d    0 5      n UR                  5         [        R                  SU5        U R                  U5        [)        XBU R                  S9$ ! [         aO    [        R!                  S5        UR#                  U R                  R$                  5        Ub  UR'                  5         e f = f)N)prefixT)parentsexist_okzStarted shell session in %sz5Starting shell session failed; cleaning up resources.)r,   r-   r5   )r  tempfileTemporaryDirectoryr   r   r   mkdirr$   r  r  rh   r   r   info_run_startup_commandsBaseException	exceptionr*   r<   r+   r2   )r>   rt   r-   workspace_pathr,   s        r/   r+  %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                   U R                   (       d  g U R                    Hd  nUR                  X R                  R                  S9nUR                  (       d  UR
                  S;  d  MI  SU SUR
                   3n[        U5      e   g )Nr   r   NzStartup command 'z' failed with exit code )r  r   r  startup_timeoutr[   rY   r   )r>   r,   ru   resultr   s        r/   rI  )ShellToolMiddleware._run_startup_commands  su    %%--G__W6L6L6\6\_]FF$4$4I$E)'2J6K[K[J\]"3''	 .rA   c           	        U R                   (       d  g U R                    H  n UR                  X R                  R                  S9nUR                  (       a  [
        R                  SU5        MP  UR                  S;  a"  [
        R                  SX#R                  5        M  M     g ! [        [        [        4 a   n[
        R                  SX$SS9   S nAM  S nAff = f)Nr   z Shutdown command '%s' timed out.rO  z%Shutdown command '%s' exited with %s.z'Failed to run shutdown command '%s': %sTr   )r  r   r  command_timeoutr[   r   r   rY   r   r   r   )r>   r,   ru   rQ  excs        r/   r9  *ShellToolMiddleware._run_shutdown_commands  s    &&..G :P:P:`:`a##NN#EwO%%Y6NN?JZJZ 7 / !-9 =wVZ  s   A
B&/0B&&C;CCc                    0 nUnU R                    HJ  nUR                  U5      u  p5U(       d  M  UR                  UR                  / 5      R	                  U5        ML     X24$ )z3Apply configured redaction rules to command output.)r  apply
setdefaultpii_typeextend)r>   contentmatches_by_typeupdatedr  matchess         r/   _apply_redactions%ShellToolMiddleware._apply_redactions-  s\    57))D#zz'2Gw**4=="=DDWM * ''rA   c          
     L   UR                   nUR                  S5      (       aI  [        R                  S5         UR	                  5         U R                  U5        SnU R                  XsSS9$ UR                  S5      nU(       a  [        U[        5      (       d  S	n[        U5      e[        R                  S
U5        UR                  XR                  R                  S9n	U	R                  (       a2  U R                  R                  n
SU
S S3nU R                  UUSSS S.S9$  U R!                  U	R"                  5      u  pU=(       d    SnU	R.                  (       a7  UR1                  5        SU R                  R2                   SU	R4                   S3nU	R6                  (       aN  U R                  R8                  b7  UR1                  5        SU R                  R8                   SU	R:                   S3nU	R*                  S;  a"  UR1                  5        SU	R*                   3nSnOSnSU	R*                  U	R.                  U	R6                  U	R4                  U	R:                  US.nU R                  UUUUS9$ ! [         a(  n[        R                  S5        Sn[        U5      UeS nAff = f! [$         ap  n[        R'                  SUR(                  5        SUR(                   S3nU R                  UUSSU	R*                  UR(                  UR,                  0S.S9s S nA$ S nAff = f) 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   ru   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)r[   rY   )r   artifactz+Blocking command output due to detected %s.zOutput blocked: detected .F)r[   rY   r_  z<no output>z

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

Exit code: )r[   rY   r\   r]   r_   r`   redaction_matches)r,   r   r   rH  r   rI  rJ  rK  r   _format_tool_messager  rV   r   r  rT  r[   r`  rW   r   r   rZ  rY   r_  r\   r   r   r_   r]   r   r`   )r>   r/  r   tool_call_idr,   errr   messageru   rQ  timeout_secondssanitized_outputr_  rd  final_statusre  s                   r/   _run_shell_tool#ShellToolMiddleware._run_shell_tool7  s6    ##;;y!!KK>?2!**73
 1G,,W9,UU++i(j#66XC$$17;2H2H2X2XY"44DDO77LIVG,,!%!%	 -  	(,(>(>v}}(M% ,<}$$#**,- .++/+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   !I4 #J) 4
J&>#J!!J&)
L#3A%LL#L#c                    [        UR                  [        5      (       aR  U R                  UR                  5      nU R                  UUR                  S   UR                  R                  S5      S9$ U" U5      $ )zJIntercept local shell tool calls and execute them via the managed session.r}   idri  r  toolr   r8  r-  ro  	tool_callr   r>   requesthandlerr/  s       r/   wrap_tool_call"ShellToolMiddleware.wrap_tool_call  st     gll$899..w}}=I''!!&)$..2248 (  
 wrA   c                  #    [        UR                  [        5      (       aR  U R                  UR                  5      nU R                  UUR                  S   UR                  R                  S5      S9$ U" U5      I Sh  vN $  N7f)z:Async interception mirroring the synchronous tool handler.r}   rr  rs  Nrt  rw  s       r/   awrap_tool_call#ShellToolMiddleware.awrap_tool_call  s}      gll$899..w}}=I''!!&)$..2248 (  
 W%%%%s   A=B?B B)re  c               d    U=(       d    0 nUc  U$ [        UUU R                  R                  UUS9$ )N)r\  ri  r   r   re  )r   r  r   )r>   r\  ri  r   re  s        r/   rh  (ShellToolMiddleware._format_tool_message  s<     >rN%
 	
rA   )	rh   r  r  r  r  r  r  r  r  r&   )r  zstr | Path | Noner  (tuple[str, ...] | list[str] | str | Noner  r  r  zBaseExecutionPolicy | Noner  z6tuple[RedactionRule, ...] | list[RedactionRule] | Noner  r   r  Sequence[str] | str | Nonerz   Mapping[str, Any] | NonerC   rD   )r   r  rC   r   )r  r  rC   r   )rz   r  rC   zdict[str, str] | None)r-  rN   r.  r    rC   dict[str, Any] | None)r-  rN   r.  r    rC   rD   )r-  rN   rC   r2   )rC   r2   )r,   r$   rC   rD   )r\  rV   rC   z%tuple[str, dict[str, list[PIIMatch]]])r/  r2   r   zdict[str, Any]ri  r   rC   r	   )rx  r"   ry  z9typing.Callable[[ToolCallRequest], ToolMessage | Command]rC   ToolMessage | Command)rx  r"   ry  zKtyping.Callable[[ToolCallRequest], typing.Awaitable[ToolMessage | Command]]rC   r  )
r\  rV   ri  r   r   zLiteral['success', 'error']re  r  rC   zToolMessage | str)rE   rF   rG   rH   rI   rN   state_schemarw   r   r  r  r  r0  r4  r:  r=  r8  r+  rI  r9  r`  ro  rz  r}  rh  rK   r   r   s   @r/   r   r   p  s    "L -1/" FJFJ7;RV'+48(,/")/" C	/"
 D/" 5/" P/" %/" 2/" &/" 
/" /"b :	  	1			 	 	 	6
1#0
b:("(]
$]
  ]

 !]
 
]
~    K  
	 & & ]& 
	&* +/

 !

 ,
 (
 

 
rA   r   )r   r   r   r   r   )r,   r$   r-   r4   r.   r   rC   rD   )IrI   
__future__r   r'   loggingr   rk   r   r   rE  rn   r   typingr   r:   dataclassesr   r   pathlibr   r   r   r	   r
   langchain_core.messagesr   langchain_core.tools.baser   r   "langgraph.channels.untracked_valuer   pydanticr   r   typing_extensionsr   &langchain.agents.middleware._executionr   r   r   r   r   &langchain.agents.middleware._redactionr   r   r   r   !langchain.agents.middleware.typesr   r   r   collections.abcr   r   langgraph.runtimer    langgraph.typesr!   r"   	getLoggerrE   r   r   r   r0   r2   rN   rT   r$   r   r   r   __all__rL   rA   r/   <module>r     sD   @ "   	          (  9 9 / = = / )   \ [1)'A			8	$) Q $KV[	 
 
 
$Z  $	 	 	X Xvi " 8  $U
/.#*=> U
p
rA   