
    iX                       U d 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 ddlmZmZmZmZ  ej        e          ZddlmZmZ ddlmZ ddlmZmZmZm Z  de!d	ed
e!fdZ" e"dde#d          Z$ e"dde%d          Z&dZ'dZ(de!de)fdZ*de+e!ef         de)fdZ,d Z-i Z.e+e!e!f         e/d<    e	j0                    Z1ddl	Z	 e	j2                    Z3d Z4d Z5d Z6d Z7de!fdZ8de!fd Z9d!e!ddfd"Z:dd#Z;dd$l<m=Z> d%e!d&e!de+fd'Z? ej@        d(          ZAd)e!de!dz  fd*ZBd+e!d&e!de!fd,ZCdd.e#de!fd/ZDdd%ed1e#de!fd2ZEd3e!de)fd4ZFd%e!d5e#deGe!e#f         fd6ZHd%e!deGe!e)f         fd7ZIde)fd8ZJd%e!de!fd9ZKd%e!dz  deGe!dz  e!dz  f         fd:ZLdd;lMmNZO dd<lmPZQ dd=lRmSZT dd>lUmVZW dd?lXmYZZ dd@l[m\Z] ddAl^m_Z_ dBZ`i Zaee!ef         e/dC<   i Zbee!e%f         e/dD<    e	j0                    Zci Zdee!e	j0        f         e/dE<    e	j0                    ZedafdFagi Zhee!ee!ef         f         e/dG<   dHe!dIee!ef         fdJZidHe!fdKZjdHee!         de!fdLZke#dfde!d	e!d
e!fdMZldee!ef         fdNZmdOendz  dee!ef         fdPZo	 	 	 	 dd&e!dQe!dRe!dSe#dTe+dUe+dVe+dHe!dWe!fdXZpddZe#fd[Zqd\ Zrd] Zsd^ ZtdHe!fd_ZudHe!de)fd`Zvda ZwdHe!fdbZxdc Zy e
jz        ey           d%e!dde#de!dz  fdeZ{d%e!de)fdfZ| ej@        dgej}                  Z~ ej@        dh          Z ej@        di          Z ej@        djej}                   ej@        dkej}                   ej@        dlej}                   ej@        dmej}                   ej@        dnej}                   ej@        doej}                   ej@        dpej}                   ej@        dqej}                  fZd%e!de)fdrZd%e!de!dz  fdsZdte)due)deGfdvZ	 	 	 	 	 	 	 	 dd%e!due)dSee#         dHee!         dwe)d)ee!         dxe)dte)dyeee!                  de!fdzZde)fd{Zed|k    r\ ed}            ed~            em            Z ed            eded&                      eded                      eded                      ededR                      ededS          d            ededZ          d            e            s ed            ed            ed            ed            ed            ed            ed            ed            ed            ed            ed            ed           dZ ed ej        dd           d            ed ej        de                       ed ej        dde                        ed ej        de                       ed ej        de                       ed ej        d ej                                          ddlmZ  ed ej        d e             d                       ed ej        dd                       ed ej        dd                      ddlmZ de`dddddddFddde$ de$ dddddddddFddddFddddidddÜd%gdĜdŜZdƄ Z ejz        ddeeeddȬɦ           dS )a  
Terminal Tool Module

A terminal tool that executes commands in local, Docker, Modal, SSH,
Singularity, Daytona, and Vercel Sandbox environments. Supports local
execution, containerized backends, and cloud sandboxes, including managed
Modal mode.

Environment Selection (via TERMINAL_ENV environment variable):
- "local": Execute directly on the host machine (default, fastest)
- "docker": Execute in Docker containers (isolated, requires Docker)
- "modal": Execute in Modal cloud sandboxes (direct Modal or managed gateway)
- "vercel_sandbox": Execute in Vercel Sandbox cloud sandboxes

Features:
- Multiple execution backends (local, docker, modal, vercel_sandbox)
- Background task support
- VM/container lifecycle management
- Automatic cleanup after inactivity

Cloud sandbox note:
- Persistent filesystems preserve working state across sandbox recreation
- Persistent filesystems do NOT guarantee the same live sandbox or long-running processes survive cleanup, idle reaping, or Hermes exit

Usage:
    from terminal_tool import terminal_tool

    # Execute a simple command
    result = terminal_tool("ls -la")

    # Execute in background
    result = terminal_tool("python server.py", background=True)
    N)Path)OptionalDictAnyList)is_interrupted_interrupt_event)_get_scratch_dir)coerce_modal_modehas_direct_modal_credentialsmanaged_nous_tools_enabledresolve_modal_backend_statenamedefault
type_labelc                     t          j        |           }||dk    r|S 	  ||          S # t          t          f$ r# t                              d| |||           |cY S w xY w)zParse module-level numeric env vars without breaking import.

    Terminal tool is imported by CLI, ACP, tests, and tool discovery. A single
    malformed env var must not make the whole module unloadable at import time.
    N z;Invalid value for %s: %r (expected %s). Falling back to %r.)osgetenv	TypeError
ValueErrorloggerwarningr   r   	converterr   raws        8/home/ubuntu/.hermes/hermes-agent/tools/terminal_tool.py_safe_parse_import_envr   N   s     )D//C
{cRii
y~~z"   I	
 	
 	
 s   
+ 1AATERMINAL_MAX_FOREGROUND_TIMEOUTiX  integerTERMINAL_DISK_WARNING_GBg     @@numberz/vercel/sandbox)node24node22z
python3.13runtimereturnc                     |  p| t           v S N)_SUPPORTED_VERCEL_RUNTIMES)r%   s    r   _is_supported_vercel_runtimer*   |   s    ;?'%???    configc                 b   |                      d          pd                                }t          |          s8d                    t                    }t
                              d||           dS |                      dd          }|dvrt
                              d	|           dS t          j        	                    d
          t
                              d           dS t          t          j        d                    }t          t          j        d                    }t          t          j        d                    }t          t          j        d                    }|rdS |s|s|r$|r|r|rdS t
                              d           dS t
                              d           dS )z6Validate Vercel Sandbox terminal backend requirements.vercel_runtimer   z, zVVercel Sandbox runtime %r is not supported. Set TERMINAL_VERCEL_RUNTIME to one of: %s.Fcontainer_disk   )r   r0   zmVercel Sandbox does not support custom TERMINAL_CONTAINER_DISK=%s. Use the default shared setting (51200 MB).vercelNzNvercel is required for the Vercel Sandbox terminal backend: pip install vercelVERCEL_OIDC_TOKENVERCEL_TOKENVERCEL_PROJECT_IDVERCEL_TEAM_IDTzVercel Sandbox backend selected with token auth, but VERCEL_TOKEN, VERCEL_PROJECT_ID, and VERCEL_TEAM_ID must all be set together. VERCEL_OIDC_TOKEN is supported for one-off local development only.zVercel Sandbox backend selected but no supported auth configuration was found. Set VERCEL_TOKEN, VERCEL_PROJECT_ID, and VERCEL_TEAM_ID for normal use. VERCEL_OIDC_TOKEN is supported for one-off local development only.)getstripr*   joinr)   r   error	importlibutil	find_specboolr   r   )r,   r%   	supporteddiskhas_oidc	has_tokenhas_projecthas_teams           r   "_check_vercel_sandbox_requirementsrD      s   zz*++1r88::G'00 II899	9		
 	
 	
 u::&..D:9	
 	
 	

 u~))1\	
 	
 	
 uBI12233HRY~..//Iry!45566KBI.//00H t 	K 	8 	 	 	 	4&	
 	
 	
 u
LL	   5r+   c                  d   	 t                      } d}ddl}|                    t          | dz                      D ]}t          |                              d          D ]g}|                                rQ	 ||                                j        z  }4# t          $ r&}t          
                    d||           Y d}~_d}~ww xY wh|dz  }|t          k    r#t                              d|t                     dS d	S # t          $ r(}t          
                    d
|d           Y d}~d	S d}~ww xY w)z4Check if total disk usage exceeds warning threshold.r   Nhermes-**zCould not stat file %s: %si   @z\Disk usage (%.1fGB) exceeds threshold (%.0fGB). Consider running cleanup_all_environments().TFz#Disk usage warning check failed: %sexc_info)r
   globstrr   rglobis_filestatst_sizeOSErrorr   debugDISK_USAGE_WARNING_THRESHOLD_GBr   	Exception)scratch_dirtotal_bytesrJ   pathfetotal_gbs          r   _check_disk_usage_warningrZ      s}   &(( IIc+
":;;<< 	I 	ID$ZZ%%c** I I99;; II#qvvxx'77" I I I%A1aHHHHHHHHIII ),555NNy#%DF F F4u   :AMMMuuuuusB   A3C= 6BC= 
CB>9C= >C6C= =
D/D**D/_sudo_password_cachec                  .    t          t          dd           S )Nsudo_passwordgetattr_callback_tls r+   r   _get_sudo_password_callbackrb      s    =/4888r+   c                  .    t          t          dd           S )Napprovalr^   ra   r+   r   _get_approval_callbackre      s    =*d333r+   c                     | t           _        dS )u   Register a callback for sudo password prompts (used by CLI).

    Per-thread scope — ACP sessions that run concurrently in a
    ThreadPoolExecutor each have their own callback slot.
    N)r`   r]   cbs    r   set_sudo_password_callbackri      s     #%Mr+   c                     | t           _        dS )u   Register a callback for dangerous command approval prompts.

    Per-thread scope — ACP sessions that run concurrently in a
    ThreadPoolExecutor each have their own callback slot. See
    GHSA-qg5c-hvr5-hjgr.
    N)r`   rd   rg   s    r   set_approval_callbackrk      s      Mr+   c                     	 ddl m}   | dd          }n%# t          $ r t          j        dd          }Y nw xY w|rd| S t                      }|Zt          |dd          }t          |dd          }|$|"d	t          |           d
t          |           S dt          |           S dt          j	                     S )z6Return the cache scope for interactive sudo passwords.r   get_session_envHERMES_SESSION_KEYr   zsession:N__self____func__zcallback-owner::z	callback:zthread:)
gateway.session_contextrn   rS   r   r   rb   r_   id	threading	get_ident)rn   session_keycallbackownerfuncs        r   _get_sudo_password_cache_scoper{   	  s    :;;;;;;%o&:B?? : : :i 4b99: ('+'''*,,H*d33xT22!1;RYY;;D;;;)2h<<))),Y(**,,,s    77c                      t                      } t          5  t                              | d          cddd           S # 1 swxY w Y   dS )z6Return the cached sudo password for the current scope.r   N)r{   _sudo_password_cache_lockr[   r6   )scopes    r   _get_cached_sudo_passwordr     s    *,,E	" 3 3#''r223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3s   >AApasswordc                     t                      }t          5  | r| t          |<   nt                              |d           ddd           dS # 1 swxY w Y   dS )z.Persist a sudo password for the current scope.N)r{   r}   r[   pop)r   r~   s     r   _set_cached_sudo_passwordr   &  s    *,,E	" 2 2 	2*2 '' $$UD111	2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s   )AAAc                  x    t           5  t                                           ddd           dS # 1 swxY w Y   dS )z`Clear all cached sudo passwords.

    Internal helper for tests and process teardown paths.
    N)r}   r[   clearra   r+   r   _reset_cached_sudo_passwordsr   0  s    
 
# % %""$$$% % % % % % % % % % % % % % % % % %s   /33)check_all_command_guardscommandenv_typec                 >    t          | |t                                S )zJDelegate to consolidated guard (tirith + dangerous cmd) with CLI callback.)approval_callback)_check_all_guards_implre   )r   r   s     r   _check_all_guardsr   B  s*    !'84J4L4LN N N Nr+   z^[A-Za-z0-9/\\:_\-.~ +@=,]+$workdirc                     | sdS t                               |           s6| D ]1}t                               |          sdt          |           dc S 2dS dS )zReject workdir values that don't look like a filesystem path.

    Uses an allowlist of safe characters rather than a deny-list, so novel
    shell metacharacters can't slip through.

    Returns None if safe, or an error message string if dangerous.
    Nz/Blocked: workdir contains disallowed character z<. Use a simple filesystem path without shell metacharacters.z0Blocked: workdir contains disallowed characters.)_WORKDIR_SAFE_REmatchrepr)r   chs     r   _validate_workdirr   O  s      t!!'** B 	 	B#))"-- Qd2hh Q Q Q  
 BA4r+   outputc                     t          j        d          }|s| S g d}|D ]}|| v rddlm} | d |             dz   c S  | S )z
    Check for sudo failure and add helpful message for messaging contexts.
    
    Returns enhanced output if sudo failed in messaging context, else original.
    HERMES_GATEWAY_SESSION)zsudo: a password is requiredzsudo: no tty presentzsudo: a terminal is requiredr   display_hermes_homeu@   

💡 Tip: To enable sudo over messaging, add SUDO_PASSWORD to z/.env on the agent machine.)r   r   hermes_constantsr   )r   r   
is_gatewaysudo_failuresfailure_dhhs         r   _handle_sudo_failurer   e  s     344J   M ! E EfDDDDDD  Eaeaeagag  E  E  E  E  E  E  E  Mr+   -   timeout_secondsc                 F   ddl }t                      }|	  |            pdS # t          $ r Y dS w xY wdddfd}	 dt          j        d<   t          j        d	           t                       t          d
           t          d           t          d           t          d           t          d           t          d|  ddz   dz              t          d           t                       t          ddd           t          j	        |d          }|
                                 |                    |            d         r~d         pd}t                       |rt          d           nt          d           t                       |j                                         |dt          j        v rt          j        d= S S t          d           t          d           t                       |j                                         	 dt          j        v rt          j        d= dS dS # t          t          f$ re t                       t          d           t                       |j                                         Y dt          j        v rt          j        d= dS dS t          $ rT}t          d | d!           |j                                         Y d}~dt          j        v rt          j        d= dS dS d}~ww xY w# dt          j        v rt          j        d= w xY w)"a  
    Prompt user for sudo password with timeout.
    
    Returns the password if entered, or empty string if:
    - User presses Enter without input (skip)
    - Timeout expires (45s default)
    - Any error occurs
    
    Only works in interactive mode (HERMES_INTERACTIVE=1).
    If a _sudo_password_callback is registered (by the CLI), delegates to it
    so the prompt integrates with prompt_toolkit's UI.  Otherwise reads
    directly from /dev/tty with echo disabled.
    r   Nr   F)r   donec                     d} d}	 t          j                    dk    r\ddl}g }	 |                                }|dv rn#|dk    rt          |                    |           <d                    |          
d<   nddl}t          j	        d	t          j
                  } |                    |           }|                    |           }|d
         |j         z  |d
<   |                    | |j        |           g }	 t          j        | d          }|r|dv rn|                    |           2d                    |                              dd          
d<   n2# t"          t          t$          f$ r d
d<   Y nt&          $ r d
d<   Y nw xY w| V|T	 ddl}|                    | |j        |           n2# t&          $ r%}	t(                              d|	           Y d}	~	nd}	~	ww xY w| H	 t          j        |            n2# t&          $ r%}	t(                              d|	           Y d}	~	nd}	~	ww xY wd
d<   dS # | V|T	 ddl}|                    | |j        |           n2# t&          $ r%}	t(                              d|	           Y d}	~	nd}	~	ww xY w| H	 t          j        |            n2# t&          $ r%}	t(                              d|	           Y d}	~	nd}	~	ww xY wd
d<   w xY w)zKRead password with echo disabled. Uses msvcrt on Windows, /dev/tty on Unix.NWindowsr   T)
r   r   z/dev/tty      )   
   r+   zutf-8replace)errorsz)Failed to restore terminal attributes: %szFailed to close tty fd: %sr   )platformsystemmsvcrtgetwchKeyboardInterruptappendr8   termiosr   openO_RDONLY	tcgetattrECHO	tcsetattr	TCSAFLUSHreaddecodeEOFErrorrP   rS   r   rQ   close)tty_fd	old_attrsr   charscr   	new_attrsb_termiosrX   results             r   read_password_threadz7_prompt_for_sudo_password.<locals>.read_password_thread  s   	*	"  I--$AL((F{{//LLOOO$ &(WWU^^z""R[99#--f55	#--f55	(|w|m;	!!!&'*;YGGG$**A ^ 3 3LLOOO	$
 &)XXe__%;%;GI%;%V%Vz"+W5 	$ 	$ 	$!#F: 	$ 	$ 	$!#F:	$ !i&;Q....&&vx/A9MMMM  Q Q QLL!LaPPPPPPPPQ!BHV$$$$  B B BLL!=qAAAAAAAAB!F6NNN !i&;Q....&&vx/A9MMMM  Q Q QLL!LaPPPPPPPPQ!BHV$$$$  B B BLL!=qAAAAAAAAB!F6N!!!!s   EE H5 F	6H5 8F	H5 F		H5  F2 2
G!<GG!'G< <
H+H&&H+5K; IK
J&JKJKJ&%K&
K0KKK	K1HERMES_SPINNER_PAUSEg?u   ┌──────────────────────────────────────────────────────────┐uA   │  🔐 SUDO PASSWORD REQUIRED                              │u   ├──────────────────────────────────────────────────────────┤u?   │  Enter password below (input is hidden), or:            │uA   │    • Press Enter to skip (command fails gracefully)     │u   │    • Wait zs to auto-skipz                           u   │u   └──────────────────────────────────────────────────────────┘z  Password (hidden): T)endflushtargetdaemontimeoutr   r   u1     ✓ Password received (cached for this session)u'     ⏭ Skipped - continuing without sudou(   
  ⏱ Timeout - continuing without sudoz    (Press Enter to dismiss)u)     ⏭ Cancelled - continuing without sudoz
  [sudo prompt error: z] - continuing without sudo
)sysrb   rS   r   environtimesleepprintru   Threadstartr8   stdoutr   r   r   )r   r   _sudo_cbr   password_threadr   rX   r   s          @r   _prompt_for_sudo_passwordr     s    JJJ +,,H	8::## 	 	 	22	 ..F." ." ." ." ."`03-0
)*
3()))CDDD()))OPPPQRRR@@@@8KeSTTT()))%2T::::#*2FtTTT_555&> 	j)/RHGGG AIJJJJ?@@@GGGJ& "RZ//
122 0# =>>>0111GGGJ "RZ//
1222 0/ '(   9:::
 "RZ//
1222 0/    IIIIJJJ
rrr!RZ//
1222 0/
 "RZ//
122222sJ   # 
11E-H. 	AH. .AL L #	L ,,K;L ;L  L L    limitc                     | dS t          | t                    r
| d|         S 	 t          |           d|         S # t          $ r dt	          |           j         dcY S w xY w)z>Return a log-safe preview for possibly-invalid command values.Nz<None><>)
isinstancerK   r   rS   type__name__)r   r   s     r   _safe_command_previewr     s    x'3 vv-G}}VeV$$ - - -,4==),,,,,,-s   < "A! A!tokenc                     d| vs|                      d          rdS |                     dd          \  }}t          t          j        d|                    S )zCReturn True when *token* is a leading shell environment assignment.=Fr   z^[A-Za-z_][A-Za-z0-9_]*$)
startswithsplitr=   rer   )r   r   _values      r   _looks_like_env_assignmentr     sW    
%5++C00u;;sA&&LD&4d;;<<<r+   r   c                    |}t          |           }||k     r| |         }|                                s|dv rn|dk    r:|dz  }||k     r#| |         dk    r|dz  }||k     r| |         dk    ||k     r|dz  }g|dk    r@|dz  }||k     r4| |         }|dk    r|dz   |k     r|dz  }#|dk    r|dz  }n|dz  }||k     4|dk    r|dz   |k     r|dz  }|dz  }||k     | ||         |fS )zERead one shell token, preserving quotes/escapes, starting at *start*.z;|&()'r   "\   )lenisspace)r   r   inr   inners         r   _read_shell_tokenr     s]   AGA
a%%QZ::<< 	2==99FAa%%GAJ#--Q a%%GAJ#--1uuQ99FAa%%
D==QUQYYFAC<<FAQ a%% ::!a%!))FA	Q5 a%%8 57Qr+   c                    g }d}t          |           }d}d}||k     r| |         }|                                r#|                    |           |dk    rd}|dz  }F|dk    r]|r[|                     d|          }|dk    r|                    | |d                    n8|                    | ||                    |}|                     d	|          s,|                     d
|          s|                     d|          r)|                    | ||dz                       |dz  }d}|dv r|                    |           |dz  }d}6|dk    r|                    |           |dz  }d}Zt          | |          \  }}	|r|dk    r|                    d           d}n|                    |           |rt          |          rd}nd}|	}||k     d                    |          |fS )zGRewrite only real unquoted sudo command words, not plain text mentions.r   TFr   r   #N&&||z;;r   z;|&()sudozsudo -S -p ''r   )r   r   r   findr   r   r   r8   )
r   outr   r   command_startfoundr   comment_endr   next_is
             r   _rewrite_real_sudo_invocationsr  2  s<   C	AGAME
a%%QZ::<< 	JJrNNNTzz $FA999!,,tQ//Kb  

7122;'''JJwq}-...AdA&& 	'*<*<T1*E*E 	I[I[\`bcIdId 	JJwqQw'(((FA M<<JJrNNNFA M99JJrNNNFA!M)'155v 	Uf__JJ'''EEJJu 	"7>> 	" MM!Ma a%%d 773<<r+   c                  6   t          j        dd                                                                          pd} | dk    rdS 	 t	          j        g dt          j        t          j        t          j        dd          }|j        dk    S # t          $ r Y dS w xY w)aN  Return True when local sudo currently works without prompting.

    Only probes for the `local` terminal backend; Docker/SSH/Modal/etc. must
    not inherit the host's sudo state. Re-probes every call (no process-level
    cache) so an expired sudo timestamp cannot make a later command silently
    block waiting for a password.
    TERMINAL_ENVlocalF)r   z-ntruer   )stdinr   stderrr   checkr   )	r   r   r7   lower
subprocessrunDEVNULL
returncoderS   )terminal_envprobes     r   _sudo_nopasswd_worksr  o  s     9^W55;;==CCEEPLwu"""$%%
 
 
 1$$   uus   AB
 

BBc                 T   t          |           }d}d}d}d}g }||k     rv| |         }|dk    r|dk    r|dk    rd}|dz  })|                                r|dz  }C|dk    r!|                     d|          }|dk    rn|}j|dk    r|dz   |k     r|dz  }|dv r't          | |          \  }	}
t	          |
|dz             }|d	k    r|dz  }|dz  }|d
k    rt	          d|dz
            }|dz  }|dk    rA|dz   |k     r8| |dz                                            s| |dz            dk    r|dz  }|dz  }!|dk    r|dk    r|dz  }d}|dz  };|dk    s|dk    r|dz  }N|                     d|          s|                     d|          r|dz   }|dz  }|dk    r	d}|dz  }|dk    r	d}|dz  }|dk    r|dz   |k     r| |dz            dk    r|dz  }|dz
  }|dk    r?| |                                         r%|dz  }|dk    r| |                                         %|dk    r| |         dv r|dz  }*|dk    r|                    ||f           d}|dz  }Pt          | |          \  }	}
t	          |
|dz             }||k     v|s| S | }t          |          D ]{\  }}|}||k     r?||                                         r%|dz  }||k     r||                                         %|d|         }|||         }||dz   d         }|dz   |z   dz   |z   }||S )u  Wrap `A && B &` (or `A || B &`) to `A && { B & }` at depth 0.

    Bash parses ``A && B &`` with `&&` tighter than `&`, so it forks a
    subshell for the whole `A && B` compound and backgrounds it. Inside
    the subshell, `B` runs foreground, so the subshell waits for `B` to
    finish. When `B` is a long-running process (`python3 -m http.server`,
    `yes > /dev/null`, anything that doesn't naturally exit), the subshell
    never exits. It leaks as a process stuck in ``wait4`` forever — and
    on the way, its open stdout pipe can prevent the terminal tool from
    returning promptly.

    Rewriting the tail to `A && { B & }` preserves `&&`'s error semantics
    (skip B if A fails) while replacing the subshell with a brace group.
    The brace group runs in the current shell (no fork), backgrounds B as
    a simple command (bash doesn't wait for it in non-interactive mode),
    and exits immediately. B runs as a normal backgrounded child, orphaned
    when the parent shell exits.

    Handles redirects (``&>``, ``2>&1``) and skips content inside quoted
    strings and parenthesised subshells. Leaves simple ``cmd &`` alone —
    that construct doesn't have the subshell-wait bug.
    r   r   r   r   r   r   r   )r   r   (r   {}r   r   ;|&r   z<>Nz{ z& })r   r   r   r   maxr   r   reversed)r   r   r   paren_depthbrace_depthlast_chain_op_endrewritesr   nl_r  jr   	chain_endamp_pos
insert_posprefixmiddlesuffixs                      r   _rewrite_compound_backgroundr,    sg   . 	GA	AKK &(H
a%%QZ ::+**{a/?/? "FA::<< 	FA
 99dA&&BRxxA::!a%!))FA )'155IAvFAE""A991KFA99aq11KFA 99QA(>(>(@(@GAPQENVZDZDZ1KFA99q1K "FA
 ??kAooFA dA&& 	'*<*<T1*E*E 	 !AFA 99 "FA 99 "FA 991uqyyWQU^s22QAAq&&WQZ//11&Q q&&WQZ//11&Avv'!*,,Q A%%!2A 6777 "FA &gq11	6A] a%%`   F&x00 9 9	7 
7""vj'9'A'A'C'C"!OJ 7""vj'9'A'A'C'C"$
7*+!& $'%/&8Mr+   c                 r   | dS t          |           \  }}|s| dfS dt          j        v }|r t          j                            dd          nt	                      }|s|st                      r| dfS |s7|s5t          j        d          r!t          d          }|rt          |           |s|r||dz   fS | dfS )	ao  
    Transform sudo commands to use -S flag if SUDO_PASSWORD is available.

    This is a shared helper used by all execution environments to provide
    consistent sudo handling across local, SSH, and container environments.

    Returns:
        (transformed_command, sudo_stdin) where:
        - transformed_command has every bare ``sudo`` replaced with
          ``sudo -S -p ''`` so sudo reads its password from stdin.
        - sudo_stdin is the password string with a trailing newline that the
          caller must prepend to the process's stdin stream.  sudo -S reads
          exactly one line (the password) and passes the rest of stdin to the
          child command, so prepending is safe even when the caller also has
          its own stdin_data to pipe.
        - If no password is available, sudo_stdin is None and the command is
          returned unchanged so it fails gracefully with
          "sudo: a password is required".

    Callers that drive a subprocess directly (local, ssh, docker, singularity)
    should prepend sudo_stdin to their stdin_data and pass the merged bytes to
    Popen's stdin pipe.

    Callers that cannot pipe subprocess stdin (modal, daytona,
    vercel_sandbox) must embed the password in the command string
    themselves; see their execute() methods for how they handle the
    non-None sudo_stdin case.

    If SUDO_PASSWORD is not set and in interactive mode (HERMES_INTERACTIVE=1):
      Prompts user for password with 45s timeout, caches for session.

    If SUDO_PASSWORD is not set and NOT interactive:
      Command runs as-is (fails gracefully with "sudo: a password is required").
    N)NNSUDO_PASSWORDr   HERMES_INTERACTIVEr   )r   r   )	r  r   r   r6   r   r  r   r   r   )r   transformedhas_real_sudohas_configured_passwordr]   s        r   _transform_sudo_commandr3  .  s   F z!?!H!HK }-; #	)
+++&((  # = =Q=S=S }" 5= 5RYG[=\=\ 51"EEE 	5%m444 1- 1MD000D=r+   )LocalEnvironment)SingularityEnvironment)SSHEnvironment)DockerEnvironment)ModalEnvironment)ManagedModalEnvironment)is_managed_tool_gateway_readyu6  Execute shell commands on a Linux environment. Filesystem usually persists between calls.

Do NOT use cat/head/tail to read files — use read_file instead.
Do NOT use grep/rg/find to search — use search_files instead.
Do NOT use ls to list directories — use search_files(target='files') instead.
Do NOT use sed/awk to edit files — use patch instead.
Do NOT use echo/cat heredoc to create files — use write_file instead.
Reserve terminal for: builds, installs, git, processes, scripts, network, package managers, and anything that needs a shell.

Foreground (default): Commands return INSTANTLY when done, even if the timeout is high. Set timeout=300 for long builds/scripts — you'll still get the result in seconds if it's fast. Prefer foreground for short commands.
Background: Set background=true to get a session_id. Two patterns:
  (1) Long-lived processes that never exit (servers, watchers).
  (2) Long-running tasks with notify_on_complete=true — you can keep working on other things and the system auto-notifies you when the task finishes. Great for test suites, builds, deployments, or anything that takes more than a minute.
For servers/watchers, do NOT use shell-level background wrappers (nohup/disown/setsid/trailing '&') in foreground mode. Use background=true so Hermes can track lifecycle and output.
After starting a server, verify readiness with a health check or log signal, then run tests in a separate terminal() call. Avoid blind sleep loops.
Use process(action="poll") for progress checks, process(action="wait") to block until done.
Working directory: Use 'workdir' for per-command cwd.
PTY mode: Set pty=true for interactive CLI tools (Codex, Claude Code, Python REPL).

Do NOT use vim/nano/interactive tools without pty=true — they hang without a pseudo-terminal. Pipe git output to cat if it might page.
_active_environments_last_activity_creation_locksF_task_env_overridestask_id	overridesc                     |t           | <   dS )a#  
    Register environment overrides for a specific task/rollout.

    Called by Atropos environments before the agent loop to configure
    per-task sandbox settings (e.g., a custom Dockerfile for the Modal image).

    Supported override keys:
        - modal_image: str -- Path to Dockerfile or Docker Hub image name
        - docker_image: str -- Docker image name
        - cwd: str -- Working directory inside the sandbox

    Args:
        task_id: The rollout's unique task identifier
        overrides: Dict of config keys to override
    Nr>  )r?  r@  s     r   register_task_env_overridesrC    s      $-   r+   c                 <    t                               | d           dS )z
    Clear environment overrides for a task after rollout completes.

    Called during cleanup to avoid stale entries accumulating.
    N)r>  r   r?  s    r   clear_task_env_overridesrF    s      GT*****r+   c                      | r| t           v r| S dS )a  
    Map a tool-call ``task_id`` to the container/sandbox key used by
    ``_active_environments``.

    The top-level agent passes ``task_id=None`` and lands on ``"default"``.
    ``delegate_task`` children pass their own subagent ID so that
    file-state tracking, the active-subagents registry, and TUI events stay
    distinct per child -- but we deliberately collapse that ID back to
    ``"default"`` here so subagents share the parent's long-lived container
    (one bash, one /workspace, one set of installed packages).

    Exception: RL / benchmark environments (TerminalBench2, HermesSweEnv, ...)
    call ``register_task_env_overrides(task_id, {...})`` to request a
    per-task Docker/Modal image. When an override is registered for a
    task_id, we honour it by returning the task_id unchanged -- those
    rollouts need their own isolated sandbox, which is the whole point of
    the override.
    r   rB  rE  s    r   _resolve_container_task_idrH    s!    &  71119r+   c           
          t          j        | |          }	  ||          S # t          t          j        f$ r t          d|  d|d| d          w xY w)zParse an environment variable with *converter*, raising a clear error on bad values.

    Without this wrapper, a single malformed env var (e.g. TERMINAL_TIMEOUT=5m)
    causes an unhandled ValueError that kills every terminal command.
    zInvalid value for : z (expected z1). Check ~/.hermes/.env or environment variables.)r   r   r   jsonJSONDecodeErrorr   s        r   _parse_env_varrM    s     )D'
"
"C
y~~,- 
 
 
> > > > >: > > >
 
 	

s	   
" 0Ac                    	
 d} t          j        dd          }t          j        dd                                          dv }|dk    rt          j                    }n|dk    rd}n|d	k    rt          }nd
}t          j        d|          

rt           j                            
          
d}d}|dk    r|rt          j        d          pt          j                    }t           j                            t           j                            |                    	t          	fd|D                       sSt           j        	                    	          r8t           j        
                    	          r	                    d          s	}d
nj|dv rf
rdt          
fd|D                       }t           j        	                    
           }|s|r%
|k    rt                              d
||           |
i d|dt          t          j        dd                    dt          j        d|           dt          ddt           j        d          dt          j        d d!|            d"t          j        d#|           d$t          j        d%|           d&t          j        d'd(                                          d)
d*|d+|d,t          d-d.          d/t          d0d1          d2t          j        d3d(          d4t          j        d5d(          d6t          d7d8          d9t          j        d:d(          t          j        d;t          j        d<d=                                                    dv t          j        d>d                                          dv t          d?d@t&          dA          t          dBdC          t          dDdE          t          j        dFd=                                          dv t          dGdt           j        d          t          j        dHd                                          dv dIS )JzBGet terminal environment configuration from environment variables.*nikolaik/python-nodejs:python3.11-nodejs20r  r	  &TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACEfalse)r
  r   yesssh~vercel_sandbox/rootTERMINAL_CWDN)z/Users/z/home/zC:\zC:/dockerc              3   B   K   | ]}                     |          V  d S r(   r   ).0p	candidates     r   	<genexpr>z"_get_env_config.<locals>.<genexpr>  s1      ??A	$$Q''??????r+   )
/workspacerV  r_  )modalrX  singularitydaytonarU  c              3   B   K   | ]}                     |          V  d S r(   rZ  )r[  r\  cwds     r   r^  z"_get_env_config.<locals>.<genexpr>  s/      DD3>>!,,DDDDDDr+   zeIgnoring TERMINAL_CWD=%r for %s backend (host/relative path won't work in sandbox). Using %r instead.r   
modal_modeTERMINAL_MODAL_MODEautodocker_imageTERMINAL_DOCKER_IMAGEdocker_forward_envTERMINAL_DOCKER_FORWARD_ENVz[]z
valid JSONsingularity_imageTERMINAL_SINGULARITY_IMAGE	docker://modal_imageTERMINAL_MODAL_IMAGEdaytona_imageTERMINAL_DAYTONA_IMAGEr.   TERMINAL_VERCEL_RUNTIMEr   rd  host_cwddocker_mount_cwd_to_workspacer   TERMINAL_TIMEOUT180lifetime_secondsTERMINAL_LIFETIME_SECONDS300ssh_hostTERMINAL_SSH_HOSTssh_userTERMINAL_SSH_USERssh_portTERMINAL_SSH_PORT22ssh_keyTERMINAL_SSH_KEYTERMINAL_SSH_PERSISTENTTERMINAL_PERSISTENT_SHELLr
  TERMINAL_LOCAL_PERSISTENTTERMINAL_CONTAINER_CPUr   r"   TERMINAL_CONTAINER_MEMORY5120TERMINAL_CONTAINER_DISK51200TERMINAL_CONTAINER_PERSISTENTTERMINAL_DOCKER_VOLUMES TERMINAL_DOCKER_RUN_AS_HOST_USER)ssh_persistentlocal_persistentcontainer_cpucontainer_memoryr/   container_persistentdocker_volumesdocker_run_as_host_user)r   r   r  getcwd_VERCEL_SANDBOX_DEFAULT_CWDrV   
expanduserabspathanyisabsisdirr   r   infor   rM  rK  loadsr7   float)default_imager   mount_docker_cwddefault_cwdrt  host_prefixesdocker_cwd_sourceis_host_pathis_relativer]  rd  s            @@r   _get_env_configr    s    AMy11Hy!I7SSYY[[_ss
 7ikk	U			%	%	%1 )NK
0
0C
 &g  %%H8M8 0In55DGOOBG$6$67H$I$IJJ	?????????	i((	-/W]]9-E-E	NWNbNbczN{N{	 !HC	T	T	TY\	TDDDDmDDDDD'--,,, 	K 	SK-?-?KK XX{4 4 4 C#H#'	2G(P(PQQ# 		"9=II# 	n-JDRVR\^jkk	#
 	RY'CE`Q^E`E`aa# 	ry!7GG# 	#;]KK# 	")$=rBBHHJJ# 	s# 	H# 	()9# 	>"4e<<# 	N+FNN# 	BI1266#  	BI1266!#" 	N#6==##$ 	29/44%#, )%I16::
 
 %'')* I&A7KKQQSSWkk ((@#uhWW*+FOO()BGLL "	*I6 R R X X Z Z^r r()BD$*Vbcc#%9-OQX#Y#Y#_#_#a#aey#yE# # # #r+   re  c                 X    t          | t                      t          d                    S )z2Resolve direct vs managed Modal backend selection.r`  )
has_directmanaged_ready)r   r   r:  )re  s    r   _get_modal_backend_stater  D  s0    &/113G<<   r+   imagerd  r   
ssh_configcontainer_configlocal_configrt  c	                    |pi }	|	                     dd          }
|	                     dd          }|	                     dd          }|	                     dd          }|	                     d	g           }|	                     d
g           }|	                     di           }| dk    rt          ||          S | dk    rEt          ||||
|||||||	                     dd          |||	                     dd                    S | dk    rt          ||||
||||          S | dk    r5i }|
dk    r|
|d<   |dk    r||d<   |dk    rE	 ddl}ddl}d|                    |j        j                  j	        v r||d<   n# t          $ r Y nw xY wt          |	                     d                    }|d         dk    rt          ||||||          S |d         dk    rn|d          rt          d!          |d"         dk    rt          d#          |d"         dk    rt          d$          d%}t                      rd&}t          |          t          ||||||          S | d'k    r&dd(lm}  ||||t%          |
          ||||          S | d)k    r.dd*lm}  ||	                     d+          pd|||
||||,          S | d-k    r|r*|                     d.          r|                     d/          st          d0          t+          |d.         |d/         |                     d1d2          |                     d3d4          ||5          S t          d6|  d7          )8a  
    Create an execution environment for sandboxed command execution.
    
    Args:
        env_type: One of "local", "docker", "singularity", "modal",
            "daytona", "vercel_sandbox", "ssh"
        image: Docker/Singularity/Modal image name (ignored for local/ssh/vercel)
        cwd: Working directory
        timeout: Default command timeout
        ssh_config: SSH connection config (for env_type="ssh")
        container_config: Resource config for container backends (cpu, memory, disk, persistent)
        task_id: Task identifier for environment reuse and snapshot keying
        host_cwd: Optional host working directory to bind into Docker when explicitly enabled
        
    Returns:
        Environment instance with execute() method
    r  r   r     r/   r0   r  Tr  rj  
docker_envr	  )rd  r   rX  ru  Fr  )r  rd  r   cpumemoryr?   persistent_filesystemr?  volumesrt  auto_mount_cwdforward_envenvrun_as_host_userra  )r  rd  r   r  r  r?   r  r?  r`  r   r  r  Nephemeral_diskre  selected_backendmanaged)r  rd  r   modal_sandbox_kwargsr  r?  directmanaged_mode_blockedzModal backend is configured for managed mode, but a paid Nous subscription is required for the Tool Gateway and no direct Modal credentials/config were found. Log in with `hermes model` or choose TERMINAL_MODAL_MODE=direct/auto.modezZModal backend is configured for managed mode, but the managed tool gateway is unavailable.z_Modal backend is configured for direct mode, but no direct Modal credentials/config were found.zHModal backend selected but no direct Modal credentials/config was found.z`Modal backend selected but no direct Modal credentials/config or managed tool gateway was found.rb  )DaytonaEnvironmentrU  )VercelSandboxEnvironmentr.   )r%   rd  r   r  r  r?   r  r?  rS  hostuserz?SSH environment requires ssh_host and ssh_user to be configuredport   keyr   )r  r  r  key_pathrd  r   zUnknown environment type: zV. Use 'local', 'docker', 'singularity', 'modal', 'daytona', 'vercel_sandbox', or 'ssh')r6   _LocalEnvironment_DockerEnvironment_SingularityEnvironmentinspectr`  	signatureSandboxcreate
parametersrS   r  _ManagedModalEnvironmentr   r   _ModalEnvironmenttools.environments.daytonar  int!tools.environments.vercel_sandboxr  _SSHEnvironment)r   r  rd  r   r  r  r  r?  rt  ccr  r  r?   
persistentr  rj  r  sandbox_kwargsr  r`  modal_statemessage_DaytonaEnvironment_VercelSandboxEnvironments                           r   _create_environmentr  M  s   , 
	RB
&&!
$
$CVV&--F66"E**D.55Jff%r**G 4b99b))J7 S'::::	X		!S'F",g66"A5II*VV$=uEE

 

 

 
	
 
]	"	"&S'F",g
 
 
 	
 
W		77$'N5!A::'-N8$!88%%%%%%%%#w'8'89M'N'N'YYY7;N#34    /rvvl/C/CDD)*i77+g%3&0'    )*h6612  >   6"i// p   6"h.. u   aG)++ v  W%%% S'!/",g
 
 
 	
 
Y		XXXXXX""S'Cd",g
 
 
 	
 
%	%	%	
 	
 	
 	
 	
 	
 )(FF+,,4",	
 	
 	
 		
 
U		 	`!7!7 	`z~~f?U?U 	`^___F#F#++^^E2..
 
 
 	
 M M M M
 
 	
s   3E8 8
FF,  rx  c                    t          j                     }	 ddlm} t          t                                                    D ]!}|                    |          r
|t          |<   "n# t          $ r Y nw xY wg }t          5  t          t          	                                          D ]]\  }}||z
  | k    rOt                              |d          }t                              |d           ||                    ||f           ^t          5  |D ] \  }}t                              |d           !	 ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |D ]+\  }}	 ddlm}  ||           n# t          $ r Y nw xY w	 t#          |d          r|                                 nIt#          |d          r|                                 n$t#          |d          r|                                 t*                              d|           # t.          $ rl}	t1          |	          }
d	|
v sd
|
                                v rt*                              d|           nt*                              d||	           Y d}	~	%d}	~	ww xY wdS )zOClean up environments that have been inactive for longer than lifetime_seconds.r   process_registryNclear_file_ops_cachecleanupstop	terminatez,Cleaned up inactive environment for task: %s404	not found*Environment for task %s already cleaned up-Error cleaning up environment for task %s: %s)r   tools.process_registryr  listr<  keyshas_active_processesImportError	_env_lockitemsr;  r   r   _creation_locks_lockr=  tools.file_toolsr  hasattrr  r  r  r   r  rS   rK   r  r   )rx  current_timer  r?  envs_to_stop	last_timer  r$  r  rX   	error_strs              r   _cleanup_inactive_envsr    sq   9;;L;;;;;;N//1122 	7 	7G44W== 7*6w'	7     L	 3 3"&~';';'='=">"> 	8 	8GYi'*:::*..w==""7D111? ''#777 " 	3 	3* 3 3
##GT22223	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 % \ \	======  )))) 	 	 	D		\sI&&  f%%  



k**  KKFPPPP 	\ 	\ 	\AI	!![IOO4E4E%E%EH'RRRRNPWYZ[[[	\'\ \su   AA$ $
A10A1=BE	$D:.E:D>	>ED>	EEE%E77
FFB	H
JA!JJc                  6   t           r	 t                      } t          | d                    n4# t          $ r'}t                              d|d           Y d}~nd}~ww xY wt          d          D ]}t           s nt          j        d            t           dS dS )zKBackground thread worker that periodically cleans up inactive environments.rx  zError in cleanup thread: %sTrH   N<   r   )	_cleanup_runningr  r  rS   r   r   ranger   r   )r,   rX   r$  s      r   _cleanup_thread_workerr      s    
 
	L$&&F"6*<#=>>>> 	L 	L 	LNN8!dNKKKKKKKK	L r 	 	A# JqMMMM  
 
 
 
 
s   #- 
AAAc                      t           5  t          t                                          s6dat	          j        t          d          at                                           ddd           dS # 1 swxY w Y   dS )z;Start the background cleanup thread if not already running.NTr   )r  _cleanup_threadis_aliver  ru   r   r   r   ra   r+   r   _start_cleanup_threadr  /  s     
 $ $"/*B*B*D*D"#'.6LUYZZZO!!###	$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s   AA,,A03A0c                      da t          6	 t                              d           dS # t          t          f$ r Y dS w xY wdS )z#Stop the background cleanup thread.FN   r   )r  r  r8   
SystemExitr   ra   r+   r   _stop_cleanup_threadr  :  sb     "	   +++++-. 	 	 	DD	 #"s   ( ==c                     t          |           }t          5  t                              |          pt                              |           cddd           S # 1 swxY w Y   dS )z9Return the active BaseEnvironment for *task_id*, or None.N)rH  r  r;  r6   )r?  lookups     r   get_active_envr  E  s    '00F	 U U#''//T3G3K3KG3T3TU U U U U U U U U U U U U U U U U Us   4AAAc                 d    t          |           }|dS t          t          |dd                    S )a  Return True if the active environment for task_id is configured for
    cross-turn persistence (``persistent_filesystem=True``).

    Used by the agent loop to skip per-turn teardown for backends whose whole
    point is to survive between turns (docker with ``container_persistent``,
    daytona, modal, etc.). Non-persistent backends (e.g. Morph) still get torn
    down at end-of-turn to prevent leakage. The idle reaper
    (``_cleanup_inactive_envs``) handles persistent envs once they exceed
    ``terminal.lifetime_seconds``.
    NF_persistent)r  r=   r_   )r?  r  s     r   is_persistent_envr  L  s5     
!
!C
{u]E22333r+   c                  t   t          t                                                    } d}| D ]L}	 t          |           |dz  }# t          $ r(}t
                              d||d           Y d}~Ed}~ww xY wt                      }ddl}|                    t          |dz                      D ]g}	 t          j        |d           t
                              d	|           5# t          $ r&}t
                              d
||           Y d}~`d}~ww xY w|dk    rt
                              d|           |S )z3Clean up ALL active environments. Use with caution.r   r   zError cleaning %s: %sTrH   NrF   )ignore_errorszRemoved orphaned: %sz%Failed to remove orphaned path %s: %szCleaned %d environments)r  r;  r  
cleanup_vmrS   r   r9   r
   rJ   rK   shutilrmtreer  rP   rQ   )task_idscleanedr?  rX   rT   rJ   rV   s          r   cleanup_all_environmentsr  _  s   (--//00HG M M	MwqLGG 	M 	M 	MLL0'1tLLLLLLLLL	M #$$KKKK		#kJ67788 K K	KM$d3333KK.5555 	K 	K 	KLL@$JJJJJJJJ	K {{-w777Ns/   A
A5A00A521C$$
D.DDc                    d}t           5  t                              | d          }t                              | d           ddd           n# 1 swxY w Y   t          5  t
                              | d           ddd           n# 1 swxY w Y   	 ddlm}  ||            n# t          $ r Y nw xY w|dS 	 t          |d          r|
                                 nIt          |d          r|                                 n$t          |d          r|                                 t                              d|            dS # t          $ rr}t!          |          }d|v sd	|                                v rt                              d
|            n"t                              d| |           Y d}~dS Y d}~dS d}~ww xY w)z4Manually clean up a specific environment by task_id.Nr   r  r  r  r  z,Manually cleaned up environment for task: %sr  r  r  r  )r  r;  r   r<  r  r=  r  r  r  r  r  r  r  r   r  rS   rK   r  r   )r?  r  r  rX   r  s        r   r  r  z  s   
 C	 * *"&&w557D)))* * * * * * * * * * * * * * *
 
 + +GT***+ + + + + + + + + + + + + + +999999W%%%%    {X3	"" 	KKMMMMS&!! 	HHJJJJS+&& 	MMOOOBGLLLLL X X XFF	I	0A0A!A!AKKDgNNNNNNJGUVWWWWWWWWW ONNNNNXsO   7AAABBBB% %
B21B2:B	E 
GA!F<<Gc                      t                       t          r?t          t                    } t                              d|            t                       dS dS )zBStop cleanup thread and shut down all remaining sandboxes on exit.z)Shutting down %d remaining sandbox(es)...N)r  r;  r   r   r  r  )counts    r   _atexit_cleanupr    sU     #())?GGG """""# #r+   	exit_codec                    |dk    rdS t          j        d|           }|r|d         n|                                 }|                                }d}|D ]7}d|v r|                    d          s|                    d          d         } |sdS d	d
id	d
id	d
id	d
id	d
id	d
id	did	did	did	did	didddddd	did}|                    |          }|r||v r||         S dS )a
  Return a human-readable note when a non-zero exit code is non-erroneous.

    Returns None when the exit code is 0 or genuinely signals an error.
    The note is appended to the tool result so the model doesn't waste
    turns investigating expected exit codes.
    r   Nz\s*(?:\|\||&&|[|;])\s*r   r   r   -/r   zNo matches found (not an error)z%Files differ (expected, not an error)zGSome directories were inaccessible (partial results may still be valid)z5Condition evaluated to false (expected, not an error)zCould not resolve hostzFailed to connect to hostz2HTTP response code indicated error (e.g. 404, 500)zOperation timed out)      r     uL   Non-zero exit (often normal — e.g. 'git diff' returns 1 when files differ))grepegrepfgreprgagackdiff	colordiffr   test[curlgit)r   r   r7   r   r6   )	r   r  segmentslast_segmentwordsbase_cmdw	semanticscmd_semanticss	            r   _interpret_exit_coder5    st    A~~t
 x17;;H$,9HRLL'@@BBL   EH  !88ALL--8773<<# t
 676767676767<=@A^_LMLM (*D%	
 
 cd1, ,I6 MM(++M (m33Y''4r+   c                     d                     |                                                                           }|                    d          od|v S )au  Return True when PTY mode would break stdin-driven commands.

    Some CLIs change behavior when stdin is a TTY. In particular,
    `gh auth login --with-token` expects the token to arrive via piped stdin and
    waits for EOF; when we launch it under a PTY, `process.submit()` only sends a
    newline, so the command appears to hang forever with no visible progress.
     zgh auth loginz--with-token)r8   r  r   r   r   
normalizeds     r   _command_requires_pipe_stdinr:    sL     '--////1122Jo.. 	)j(r+   z\b(?:nohup|disown|setsid)\bz\s&\sz\s&\s*(?:#.*)?$z@\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start|serve|watch)\bz\bdocker\s+compose\s+up\bz\bnext\s+dev\bz\bvite(?:\s|$)z\bnodemon\bz\buvicorn\bz\bgunicorn\bz$\bpython(?:3)?\s+-m\s+http\.server\bc                     d                     |                                                                           }d|v p-|                    d          pd|v p|                    d          S )zGReturn True for informational invocations that should never be blocked.r7  z --helpz -hz
 --versionz -v)r8   r  r   endswithr8  s     r   #_looks_like_help_or_version_commandr=    so    '--////1122JZ 	&u%%	&:%	& u%%	r+   c                    t          |           rdS t                              |           r	 dS t                              |           st                              |           r	 dS t
          D ]}|                    |           r	  dS dS )zSuggest background mode when a foreground command looks long-lived.

    Prevents workflows that start a server/watch process and then stall before
    follow-up checks or test commands run.
    NzForeground command uses shell-level background wrappers (nohup/disown/setsid). Use terminal(background=true) so Hermes can track the process, then run readiness checks and tests in separate commands.zForeground command uses '&' backgrounding. Use terminal(background=true) for long-lived processes, then run health checks and tests in follow-up terminal calls.zThis foreground command appears to start a long-lived server/watch process. Run it with background=true, verify readiness (health endpoint/log signal), then execute tests in a separate command.)r=  _SHELL_LEVEL_BACKGROUND_REsearch_INLINE_BACKGROUND_AMP_RE_TRAILING_BACKGROUND_AMP_RE_LONG_LIVED_FOREGROUND_PATTERNS)r   patterns     r   _foreground_background_guidancerE     s     +733 t!((11 
?	
 	
 !''00 
4O4V4VW^4_4_ 
W	
 	

 3  >>'"" 	<  	 4r+   notify_on_complete
backgroundc                 "    |r
| r|rd}d|fS |dfS )uK  Decide what to do when both notify_on_complete and watch_patterns are set.

    These flags produce duplicate, delayed notifications when combined — one
    notification per watch-pattern match AND one on process exit, with async
    delivery that can spam the user long after the process ends. When both are
    set, we drop watch_patterns in favor of notify_on_complete (the more useful
    "let me know when it's done" signal) and return a human-readable note.

    Returns:
        (watch_patterns_to_use, conflict_note). conflict_note is "" when there
        is no conflict.
    zuwatch_patterns ignored because notify_on_complete=True; these two flags produce duplicate notifications when combinedNr   ra   )rF  watch_patternsrG  notes       r   #_resolve_notification_flag_conflictrK  A  s=    $  ( ^ L 	 Tz2r+   forceptyrI  c	                    	 t          | t                    s]t                              dt	          |           j                   t          j        dddt	          |           j         ddd          S t                      }	|	d	         }
t          |          }t                              |i           }|
d
k    r|                    d          p|	d         }nn|
dk    r|                    d          p|	d         }nJ|
dk    r|                    d          p|	d         }n&|
dk    r|                    d          p|	d         }nd}|                    d          p|	d         }|	d         }|p|}|s1|r/|t          k    r$t          j        dd| dt           did          S |s,t          |           }|rt          j        dd|ddd          S t                       t          5  |t           v r+t#          j                    t$          |<   t           |         }d}nd}ddd           n# 1 swxY w Y   |rTt&          5  |t(          vrt+          j                    t(          |<   t(          |         }ddd           n# 1 swxY w Y   |5  t          5  |t           v r*t#          j                    t$          |<   t           |         }d}ddd           n# 1 swxY w Y   |r|
dk    rt/                       t                              d|
|dd                    	 d}|
dk    rl|	                    dd          |	                    dd          |	                    dd          |	                    d d          |	                    d!d          d"}d}|
d#v r|	                    d$d%          |	                    d&d'          |	                    d(d)          |	                    d*d          |	                    d+d,          |	                    d-d          |	                    d.g           |	                    d/d          |	                    d0g           |	                    d1i           |	                    d2d          d3}d}|
d4k    rd5|	                    d6d          i}t3          |
||||||||	                    d7          8	  	        }nB# t4          $ r5}t          j        ddd9| d:d;dd          cY d}~cddd           S d}~ww xY wt          5  |t           |<   t#          j                    t$          |<   |}ddd           n# 1 swxY w Y   t                              d<|
|dd                    ddd           n# 1 swxY w Y   d}|sMt7          | |
          }|d=         s|                    d>          d?k    rnt          j        dd|                    d@dA          d?|                    dB|           |                    dCdD          |                    dEd          dFd          S |                    dCdD          }dG| dH}t          j        dd|                    d@|          dIdd          S |                    dJ          r|                    dCdK          }dL| dM}n1|                    dN          r|                    dCdK          }dO| dP}|r]t9          |          }|rLt                              dQ|ddR         t;          |                      t          j        dd|dIdd          S d}|} |rt=          |           rd} dS}|r-dTdUlm }! dTdVl!m"}"  |!dW          }#|p|}$	 |
d4k    r3|"#                    | |$||#tI          |dX          r|j%        nd| Y          }%n|"&                    || |$||#Z          }%d[|%j'        |%j(        dTdd\}&|r||&d]<   |r||&d^<   |rk|s|rgdTd_l)m*}'  |'d`d          }(|(rS |'dad          }) |'dbd          }* |'dcd          }+ |'ddd          },|(|%_+        |)|%_,        |+|%_-        |,|%_.        |*|%_/        ta          tc          |          |tc          |          e          \  }}-|-r&t                              df|%j'        |-           |-|&dg<   |r^|r\d|%_2        d|&dh<   |%j+        rIdi|%_3        |"j4        5                    |%j'        di|#|%j+        |%j,        |%j-        |%j.        |%j/        ddj	           |r |rtm          |          |%_7        |%j7        |&dk<   t          j        |&d          S # tp          $ r4}t          j        dddlt          |           dmd          cY d}~S d}~ww xY wdn}.dT}/d}0|/|.k    re	 d|i}1|r||1d<    |j9        | fi |1}0nH# tp          $ r:}t          |          :                                }2d|2v r#t          j        ddodp| dqdmd          cY d}~S |/|.k     rd|/d%z  }/dr|/z  }3t                              ds|3|/|.t;          |           t	          |          j        |||
	  	         t#          j;        |3           Y d}~t          <                    dt|.t;          |           t	          |          j        |||
           t          j        dddut	          |          j         dvt          |           dmd          cY d}~S d}~ww xY w	 |0                    dwd          }4|0                    dxdT          }5t{          |4|
          }4	 dTdyl>m?}6  |6dz| |4|5|pd|
{          }7|7D ]}8t          |8t                    r|8}4 nn# tp          $ r Y nw xY wdTd|l@mA}9  |9            }:t          |4          |:k    r[t          |:d}z            };|:|;z
  }<t          |4          |;z
  |<z
  }=d~|= dt          |4           d}>|4d|;         |>z   |4|< d         z   }4dTdlDmE}?  |?|4          }4dTdlFmG}@ |4r |@|4H                                          nd}4t          | |5          }A|4|5ddm}B|r||Bd]<   |Ar|A|Bd<   t          j        |Bd          S # tp          $ ri}dTdlJ}C|CK                                }Dt          <                    d|D           t          j        dddt          |           |Dddd          cY d}~S d}~ww xY w)u.  
    Execute a command in the configured terminal environment.

    Args:
        command: The command to execute
        background: Whether to run in background (default: False)
        timeout: Command timeout in seconds (default: from config)
        task_id: Unique identifier for environment isolation (optional)
        force: If True, skip dangerous command check (use after user confirms)
        workdir: Working directory for this command (optional, uses session cwd if not set)
        pty: If True, use pseudo-terminal for interactive CLI tools (local backend only)
        notify_on_complete: If True and background=True, you'll be notified exactly once when the process exits. The right choice for almost every long task. MUTUALLY EXCLUSIVE with watch_patterns.
        watch_patterns: List of strings to watch for in background output. HARD rate limit: 1 notification per 15s per process. After 3 strike windows in a row, watch_patterns is disabled and the session is auto-promoted to notify_on_complete. Use ONLY for rare, one-shot mid-process signals on long-lived processes (server readiness, migration-done markers). NEVER use in loops/batch jobs — error patterns there will hit the strike limit and get disabled. MUTUALLY EXCLUSIVE with notify_on_complete — set one, not both.

    Returns:
        str: JSON string with output, exit_code, and error fields

    Examples:
        # Execute a simple command
        >>> result = terminal_tool(command="ls -la /tmp")

        # Run a background task
        >>> result = terminal_tool(command="python server.py", background=True)

        # With custom timeout
        >>> result = terminal_tool(command="long_task.sh", timeout=300)
        
        # Force run after user confirmation
        # Note: force parameter is internal only, not exposed to model API
    z+Rejected invalid terminal command value: %sr   r   z&Invalid command: expected string, got r9   )r   r  r9   statusF)ensure_asciir   rX  rh  ra  rl  r`  ro  rb  rq  rd  r   zForeground timeout zs exceeds the maximum of zNs. Use background=true with notify_on_complete=true for long-running commands.TNz*Creating new %s environment for task %s...   rS  r{  r}  r  r  r  r  )r  r  r  r  r  )rX  ra  r`  rb  rU  r  r   r  r  r/   r0   r  re  rg  r.   r  ru  rj  r  r  )r  r  r/   r  re  r.   r  ru  rj  r  r  r	  r  r  rt  )	r   r  rd  r   r  r  r  r?  rt  z5Terminal tool disabled: environment creation failed (r   disabledz %s environment ready for task %sapprovedrO  approval_requiredr  zWaiting for user approvalr   descriptionzcommand flaggedpattern_key)r   r  r9   rO  r   rU  rV  zCommand denied: z?. Use the approval prompt to allow it, or rephrase the command.blockeduser_approvedzflagged as dangerouszCommand required approval (z) and was approved by the user.smart_approvedzCommand was flagged (z&) and auto-approved by smart approval.z+Blocked dangerous workdir: %s (command: %s)r   zPTY disabled for this command because it expects piped stdin/EOF (for example gh auth login --with-token). For local background processes, call process(action='close') after writing so it receives EOF.r   )get_current_session_keyr  )r   r  )r   rd  r?  rw   env_varsuse_pty)r  r   rd  r?  rw   zBackground process started)r   
session_idpidr  r9   rd   pty_noterm   HERMES_SESSION_PLATFORMHERMES_SESSION_CHAT_IDHERMES_SESSION_THREAD_IDHERMES_SESSION_USER_IDHERMES_SESSION_USER_NAME)rF  rI  rG  zbackground proc %s: %swatch_patterns_ignoredrF  r  )	r]  check_intervalrw   r   chat_iduser_id	user_name	thread_idrF  rI  z$Failed to start background process: )r   r  r9   r   |   zCommand timed out after z secondsr   zfExecution error, retrying in %ds (attempt %d/%d) - Command: %s - Error: %s: %s - Task: %s, Backend: %szWExecution failed after %d retries - Command: %s - Error: %s: %s - Task: %s, Backend: %szCommand execution failed: rJ  r   r  )invoke_hooktransform_terminal_output)r   r   r  r?  r   )get_max_bytesg?z

... [OUTPUT TRUNCATED - z chars omitted out of z total] ...

)
strip_ansi)redact_sensitive_textexit_code_meaningzterminal_tool exception:
%szFailed to execute command: )r   r  r9   	tracebackrO  )Lr   rK   r   r   r   r   rK  dumpsr  rH  r>  r6   FOREGROUND_MAX_TIMEOUTrE  r  r  r;  r   r<  r  r=  ru   LockrZ   r  r  r  r   r   r   r:  tools.approvalrZ  r  r  spawn_localr  r  spawn_via_envrt   r^  rs   rn   watcher_platformwatcher_chat_idwatcher_user_idwatcher_user_namewatcher_thread_idrK  r=   rF  watcher_intervalpending_watchersr   r  rI  rS   executer  r   r9   r   hermes_cli.pluginsrl  tools.tool_output_limitsrn  r   r  tools.ansi_stripro  agent.redactrp  r7   r5  rr  
format_exc)Er   rG  r   r?  rL  r   rM  rF  rI  r,   r   effective_task_idr@  r  rd  default_timeouteffective_timeoutguidancer  needs_creation	task_lockr  r  r  new_envrX   approval_noterd   descfallback_msgworkdir_errorpty_disabled_reasoneffective_ptyrZ  r  rw   effective_cwdproc_sessionresult_data_gse_gw_platform_gw_chat_id_gw_thread_id_gw_user_id_gw_user_nameconflict_notemax_retriesretry_countr   execute_kwargsr  	wait_timer   r  rl  hook_resultshook_resultrn  MAX_OUTPUT_CHARS
head_chars
tail_charsomittedtruncated_noticero  rp  	exit_noteresult_dictrr  tb_strsE                                                                        r   terminal_toolr  \  s   R|'3'' 
	#NN=W&   :Z$w--BXZZ!	 
 "# # # # !""*% 7w?? (++,=rBB	 xMM.11KVN5KEE&&MM"566U&AT:UEE  MM-00IF=4IEE""MM/22Mf_6MEEEmmE""3fUm +#6  	#g 	#'4J*J*J:J' J J-J J J "# # # #  	'6w??H 'z !#%%	# #
 !&' ' ' ' 	  	& 	& $88848IKK01*+<=!&!%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&  K	e% ? ?$O;;9B9I9IO$56+,=>	? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
  De De / /(,@@@<@IKK'8923DE).	/ / / / / / / / / / / / / / / " <e=001333KK LhXijlkljlXmnnn2/%)
#u,,(.

:r(B(B(.

:r(B(B(.

:r(B(B'-zz)R'@'@.4jj9I5.Q.Q* *J ,0(#'fff17OQ1O1O4:JJ?QSW4X4X28**=Mu2U2U8>

CY[_8`8`.4jjv.N.N28**=Mr2R2R28**=Mr2R2RAGLkmrAsAs6<jjAUWY6Z6Z.4jjr.J.J;A::F_af;g;g0 0, (,#w.. ,fjj9KU.S.S,L #6%-"' #$5'1-=)5$5%+ZZ
%;%;
# 
# 
# ' / / /#z&()+%a]^%a%a%a&0	+ +
 ). /  /  / / / / / /sDe De De De De De De Dep/ # & &BI,->?<@IKK'89%& & & & & & & & & & & & & & & KK BHN_`bab`bNcdddIDe De De De De De De De De De De De De De DeP   	e((;;HJ' '<<))-@@@:"$%'!)i9T!U!U"5#+<<	7#C#C'/||MCT'U'U'/||M2'F'F' ' %*+ + + +  ||M3DEETt T T T  z !#%\\)\BB'	# #
 !&' ' ' ' ||O,, e||M3IJJ cd c c c.// e||M3IJJ d d d d  
	'-g66M 'L&tt}.CG.L.LN N Nz !#*'	# #
 !&' ' ' ' # 	/88 	!M    T	? ?>>>>>??????11"===K#NsMa'w&&#3#?#? ') 1$/,3C,?,?!IT - $@ $ $LL $4#A#A ') 1$/ $B $ $L ;"./'+!"!  ! <.;K
+& B.AK
+
  G#5 G GOOOOOO#'4(A2#F#FL# 	G&*d+CR&H&H(,-G(L(L&*d+CR&H&H(,-G(L(L8D57B47B49F69F6 1T'+,>'?'?#1#J//1 1 1-
 ! JNN#;\_m\\\<IK 89 & * 6:L38<K 45
 $4 895(9@@*6/./+6(4(E'3'C'3'C)5)G)5)G26
B 
B 
 
 
 " Pj P26~2F2FL/4@4OK 01z+EBBBB ' ' 'z !#LCFFLL# # !&	' ' ' ' ' ' ' ' '' KKF,,+&/1B%CN 807u-(S[CCNCCFF  + + + #AI I--#z&(),%[@Q%[%[%[+ + ).	 /  /  / / / / / / / #[00#q($%$4	  (P'0+{LabiLjLjlpqrlslsl|~  BS  U]^ ^ ^
9--- LL!z!,.CG.L.LdSTggN^`actv~@ @ @:"$%'!Zd1gg>N!Z!ZRUVWRXRX!Z!Z' ' %*	+ + + + + + + + +)+6  ZZ"--FL!44J *&(;;F::::::*{/#!)-3%      $0  K!+s33 !,     ?>>>>>,}6{{--- !1C!788
-
:
f++
2Z?;7 ; ;!&kk; ; ; !  ,/??&*BVV 433333Z''F ;:::::>DL**6<<>>:::"F -WjAAI !' K
  8*7J' =3</0:k>>>> 
 
 
%%''3V<<<z;3q66;;
 
    	 	 	 	 	 		
s  A1q+ 4D-q+ "-q+ q+ %7H(q+ (H,,q+ /H,0q+ =2I;/q+ ;I??q+ I?q+ U?4KU?K	U?K	>U?F/SU?
TT/T0U?4q+ T
U?(U8U?U	U?U	'U?3q+ ?Vq+ VB&q+ .A
q+ 9Cq+ ;8q+ 4Ge 
f)e<6f7q+ <fq+ f- +q+ -k28Ak-:k2;q+  A%k-%q+ *A=k-'k2(q+ -k22A q+ 37m+ *q+ +
m85q+ 7m88C2q+ +
s5Asssc                     	 t                      } | d         }|dk    rdS |dk    rRddlm}  |            }|st                              d           dS t          j        |d	gdd
          }|j        dk    S |dk    rPt          j	        d          pt          j	        d          }|r$t          j        |dgdd
          }|j        dk    S dS |dk    rH| 
                    d          r| 
                    d          st                              d           dS dS |dk    r[t          | 
                    d                    }|d         dk    rdS |d         dk    r|d         rt                              d           dS |d         dk    rt                              d           dS |d         dk    rEt                      rt                              d           nt                              d           dS t                      rt                              d           nt                              d           dS t          j                            d          t                              d!           dS dS |d"k    rt!          |           S |d#k    rdd$lm} t'          j        d%          d uS t                              d&|           dS # t*          $ r(}t                              d'|d(           Y d }~dS d }~ww xY w))z8Check if all requirements for the terminal tool are met.r   r	  TrX  r   )find_dockerz?Docker executable not found in PATH or common install locationsFversionr  )capture_outputr   ra  	apptainerz	--versionrS  r{  r}  zSSH backend selected but TERMINAL_SSH_HOST and TERMINAL_SSH_USER are not both set. Configure both or switch TERMINAL_ENV to 'local'.r`  re  r  r  r  r  zModal backend selected with TERMINAL_MODAL_MODE=managed, but a paid Nous subscription is required for the Tool Gateway and no direct Modal credentials/config were found. Log in with `hermes model` or choose TERMINAL_MODAL_MODE=direct/auto.r  zModal backend selected with TERMINAL_MODAL_MODE=managed, but the managed tool gateway is unavailable. Configure the managed gateway or choose TERMINAL_MODAL_MODE=direct/auto.zModal backend selected with TERMINAL_MODAL_MODE=direct, but no direct Modal credentials/config were found. Configure Modal or choose TERMINAL_MODAL_MODE=managed/auto.zModal backend selected with TERMINAL_MODAL_MODE=direct, but no direct Modal credentials/config were found. Configure Modal or choose TERMINAL_MODAL_MODE=auto.zModal backend selected but no direct Modal credentials/config or managed tool gateway was found. Configure Modal, set up the managed gateway, or choose a different TERMINAL_ENV.z|Modal backend selected but no direct Modal credentials/config was found. Configure Modal or choose a different TERMINAL_ENV.NzFmodal is required for direct modal terminal backend: pip install modalrU  rb  )DaytonaDAYTONA_API_KEYzgUnknown TERMINAL_ENV '%s'. Use one of: local, docker, singularity, modal, daytona, vercel_sandbox, ssh.z&Terminal requirements check failed: %srH   )r  tools.environments.dockerr  r   r9   r  r  r  r  whichr6   r  r   r:   r;   r<   rD   rb  r  r   r   rS   )	r,   r   r  rX  r   
executabler  r  rX   s	            r   check_terminal_requirementsr  D  s   g ""*%w4!!====== []]F ^___u^VY$7VWXXXF$))&&k22Qfl=6Q6QJ .#[(ARV`abbb(A--5::j)) J1G1G Z   u4  26::l3K3KLLK-.);;t-.(::56 !LLE   !5v&)33LL;  
 !5 (H44133 @    8  
 !5133 
B    R   !5~''008efffu4)))5f===""''''''9.//t;; LL7  
 5   =q4PPPuuuuush   K 2K #K 8AK A
K 5K .K &K +AK <AK 9K >K !K 5K 
LK??L__main__zTerminal Tool Modulez2==================================================z
Current Configuration:z  Environment type: z  Docker image: rh  z  Modal image: ro  z  Working directory: z  Default timeout: sz  Lifetime: u;   
❌ Requirements not met. Please check the messages above.r   u   
✅ All requirements met!z
Available Tool:z=  - terminal_tool: Execute commands in sandboxed environmentsz
Usage Examples:z  # Execute a commandz*  result = terminal_tool(command='ls -la')z  z  # Run a background taskzE  result = terminal_tool(command='python server.py', background=True)z
Environment Variables:rO  z  TERMINAL_ENV: r  r	  z< (local/docker/singularity/modal/daytona/vercel_sandbox/ssh)z  TERMINAL_DOCKER_IMAGE: ri  z  TERMINAL_SINGULARITY_IMAGE: rm  rn  z  TERMINAL_MODAL_IMAGE: rp  z  TERMINAL_DAYTONA_IMAGE: rr  z  TERMINAL_CWD: rW  r   z  TERMINAL_SANDBOX_DIR: TERMINAL_SANDBOX_DIRz
/sandboxesz  TERMINAL_TIMEOUT: rv  60z  TERMINAL_LIFETIME_SECONDS: ry  rz  )registryterminalobjectstringz The command to execute on the VM)r   rU  booleanu2  Run the command in the background. Two patterns: (1) Long-lived processes that never exit (servers, watchers). (2) Long-running tasks paired with notify_on_complete=true — you can keep working and get notified when the task finishes. For short commands, prefer foreground with a generous timeout instead.)r   rU  r   z3Max seconds to wait (default: 180, foreground max: u   ). Returns INSTANTLY when command finishes — set high for long tasks, you won't wait unnecessarily. Foreground timeout above z7s is rejected; use background=true for longer commands.)r   rU  minimumz^Working directory for this command (absolute path). Defaults to the session working directory.zRun in pseudo-terminal (PTY) mode for interactive CLI tools like Codex, Claude Code, or Python REPL. Only works with local and SSH backends. Default: false.u  When true (and background=true), you'll be automatically notified exactly once when the process finishes. **This is the right choice for almost every long-running task** — tests, builds, deployments, multi-item batch jobs, anything that takes over a minute and has a defined end. Use this and keep working on other things; the system notifies you on exit. MUTUALLY EXCLUSIVE with watch_patterns — when both are set, watch_patterns is dropped.arrayr   u
  Strings to watch for in background process output. HARD RATE LIMIT: at most 1 notification per 15 seconds per process — matches arriving inside the cooldown are dropped. After 3 consecutive 15-second windows with dropped matches, watch_patterns is automatically disabled for that process and promoted to notify_on_complete behavior (one notification on exit, no more mid-process spam). USE ONLY for truly rare, one-shot mid-process signals on LONG-LIVED processes that will never exit on their own — e.g. ['Application startup complete'] on a server so you know when to hit its endpoint, or ['migration done'] on a daemon. DO NOT use for: (1) end-of-run markers like 'DONE'/'PASS' — use notify_on_complete instead; (2) error patterns like 'ERROR'/'Traceback' in loops or multi-item batch jobs — they fire on every iteration and you'll hit the strike limit fast; (3) anything you'd ever combine with notify_on_complete. When in doubt, choose notify_on_complete. MUTUALLY EXCLUSIVE with notify_on_complete — set one, not both.)r   r  rU  )r   rG  r   r   rM  rF  rI  )r   
propertiesrequired)r   rU  r  c                 f   t          |                     d          |                     dd          |                     d          |                    d          |                     d          |                     dd          |                     dd          |                     d	          
          S )Nr   rG  Fr   r?  r   rM  rF  rI  )r   rG  r   r?  r   rM  rF  rI  )r  r6   )argskws     r   _handle_terminalr  	  s    ##88L%00##y!!##HHUE""88$8%@@xx 011	 	 	 	r+   u   💻i )r   toolsetschemahandlercheck_fnemojimax_result_size_chars)r&   N)r   )r   )NNNr   N)r  )FNNFNFFN)__doc__importlib.utilr:   rK  loggingr   r   r   r   ru   atexitr  r  pathlibr   typingr   r   r   r   	getLoggerr   r   tools.interruptr   r	   tools.environments.singularityr
   tools.tool_backend_helpersr   r   r   r   rK   r   r  rt  r  rR   r  r)   r=   r*   dictrD   rZ   r[   __annotations__ru  r}   r	  r`   rb   re   ri   rk   r{   r   r   r   rv  r   r   r   compiler   r   r   r   r   r   tupler   r  r  r,  r3  tools.environments.localr4  r  r5  r  tools.environments.sshr6  r  r  r7  r  tools.environments.modalr8  r   tools.environments.managed_modalr9  r  tools.managed_tool_gatewayr:  TERMINAL_TOOL_DESCRIPTIONr;  r<  r  r=  r  r  r  r>  rC  rF  rH  rM  r  r  r  r  r  r   r  r  r  r  r  r  r  registerr5  r:  
IGNORECASEr?  rA  rB  rC  r=  rE  rK  r  r  r   r,   exitdefault_imgr   r  r   r   r   tools.registryr  TERMINAL_SCHEMAr  ra   r+   r   <module>r     s       D       				  				                  , , , , , , , , , , , ,		8	$	$ = < < < < < < < < ; ; ; ; ;           
 	   8 0/%	   #9"8			# #  0 ? @# @$ @ @ @ @5tCH~ 5$ 5 5 5 5p  F (* d38n ) ) )*IN,,     	!!9 9 94 4 4% % %     - - - - -,33 3 3 3 32 2 2 2 2 2% % % %     
Ns Nc Nd N N N N 2:=>> s sTz    ,      4z3 z3s z3C z3 z3 z3 z3x	- 	-3 	-s 	-S 	- 	- 	- 	-=c =d = = = =!s !3 !5c? ! ! ! !H:C :E#t)4D : : : :zd    4b# b# b b b bJBS4Z BE#*cDj:P4Q B B B BL K J J J J J \ \ \ \ \ \ D D D D D D M M M M M M J J J J J J ` ` ` ` ` ` D D D D D D . (* d38n ) ) )#%S%Z  % % %IN	-/c9>)* / / /%y~''   24 T#tCH~-. 3 3 3- -c3h - - - -&+c + + + + #    4 7:Y 
 
 
s 
s 
 
 
 
 Sc3h S S S Sl$ 4S>     KO-1'0(,	R
 R
# R
c R
 R
c R
$(R
CGR
&*R
 "%R
 #&	R
 R
 R
 R
j;\ ;\S ;\ ;\ ;\ ;\|  $ $ $  UC U U U U4s 4t 4 4 4 4&  6'X 'X 'X 'X 'XT# # #       =# =# =#* = = = =@# $     (RZ(FVV &BJx00 (bj);<< BJRTVTabbBJ+R];;BJ "-00BJ "-00BJ~r}--BJ~r}--BJ..BJ6FF	#      S S4Z    B 	
    : !!!$*.e eee c]e c]	e
 e c]e 
e e T#Y'e 	e e e ePiT i i i iX z	E
 !!!	E(OOO_F	E
$%%%	E
5
!3
5
5666	E
5VN3
5
5666	E
3F=1
3
3444	E
1&-
1
1222	E
4y 1
4
4
4555	E
6 23
6
6
6777&&(( LMMMQ	E
'(((	E
	E
IJJJ	E
	E
!"""	E
6777	E$KKK	E
%&&&	E
QRRR	E
$%%%>K	E	F29^W--	F 	F 	F  
 
E
Wibi0G&U&U
W
WXXX	E
o9295QSl_jSlSl+m+m
o
oppp	E
UYRY/E{%S%S
U
UVVV	E
Yyry1I;'W'W
Y
YZZZ	E
EYRY~yry{{CC
E
EFFF<<<<<<	E
_YRY/E$$&&G\G\G\%]%]
_
_```	E
F+=t!D!D
F
FGGG	E
Y)")4OQV*W*W
Y
YZZZ $ # # # # # , !A 
 "  T   "  |Uk   |   |  mC   |   |   |  ! 
 "  ~   "  ` # #   (+  l ;"
 "
F KK& &* *Z
 
 
  	(
!     r+   