
    iH$                    
   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   e! ej"        dd	                    Z# e$ ej"        d
d                    Z%d Z&da'e(e)d<   da*da+d Z,d Z-ddl.m/Z0 de(de(de1fdZ2 ej3        d          Z4de(de(dz  fdZ5de(de(de(fdZ6dde!de(fdZ7ddede!de(fd Z8d!e(de9fd"Z:de(d#e!de;e(e!f         fd$Z<de(de;e(e9f         fd%Z=de(dz  de;e(dz  e(dz  f         fd&Z>dd'l?m@ZA dd(lmBZC dd)lDmEZF dd*lGmHZI dd+lJmKZL dd,lMmNZO dd-lPmQZQ d.ZRi ZSee(ef         e)d/<   i ZTee(e$f         e)d0<    e	jU                    ZVi ZWee(e	jU        f         e)d1<    e	jU                    ZXdaYd2aZi Z[ee(ee(ef         f         e)d3<   d4e(d5ee(ef         fd6Z\d4e(fd7Z]e!d8fd9e(d:e(d;e(fd<Z^dee(ef         fd=Z_d>e`dz  dee(ef         fd?Za	 	 	 	 dde(d@e(dAe(dBe!dCe1dDe1dEe1d4e(dFe(fdGZbddIe!fdJZcdK ZddL ZedM Zfd4e(fdNZgd4e(de9fdOZhdP Zid4e(fdQZjdR Zk e
jl        ek           de(dSe!de(dz  fdTZmde(de9fdUZn	 	 	 	 	 	 	 	 dde(dVe9dBee!         d4ee(         dWe9dee(         dXe9dYe9dZeee(                  de(fd[Zode9fd\Zped]k    r\ eqd^            eqd_            e_            Zr eqd`            eqdaerd                      eqdberdc                      eqdderde                      eqdferdA                      eqdgerdB          dh            eqdierdI          dh            ep            s eqdj            esdk            eqdl            eqdm            eqdn            eqdo            eqdp            eqdq            eqdr            eqds            eqdt            eqdu           dvZt eqdw ej"        dxdy           dz            eqd{ ej"        d|et                       eqd} ej"        d~det                        eqd ej"        det                       eqd ej"        det                       eqd ej"        d eju                                          ddlvmwZx  eqd ej"        d ex             d                       eqd ej"        dd                       eqd ej"        dd                      ddlymzZz deRddddddd2dd8de# de# ddkddddddd2dddd2ddddiddddgddZ{d Z| ezjl        dde{e|epdd           dS )af  
Terminal Tool Module

A terminal tool that executes commands in local, Docker, Modal, SSH, Singularity, and Daytona environments.
Supports local execution, containerized backends, and Modal cloud sandboxes, including managed gateway 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)

Features:
- Multiple execution backends (local, docker, modal)
- 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TERMINAL_MAX_FOREGROUND_TIMEOUT600TERMINAL_DISK_WARNING_GB500c                  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OSErrorloggerdebugDISK_USAGE_WARNING_THRESHOLD_GBwarning	Exception)scratch_dirtotal_bytesr   pathfetotal_gbs          ;/home/agentuser/.hermes/hermes-agent/tools/terminal_tool.py_check_disk_usage_warningr+   R   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/ _cached_sudo_passwordc                 
    | a dS )z<Register a callback for sudo password prompts (used by CLI).N)_sudo_password_callbackcbs    r*   set_sudo_password_callbackr2   {   s     !    c                 
    | a dS )zIRegister a callback for dangerous command approval prompts (used by CLI).N)_approval_callbackr0   s    r*   set_approval_callbackr6      s     r3   )check_all_command_guardscommandenv_typereturnc                 0    t          | |t                    S )zJDelegate to consolidated guard (tirith + dangerous cmd) with CLI callback.)approval_callback)_check_all_guards_implr5   )r8   r9   s     r*   _check_all_guardsr>      s$    !'84FH H H Hr3   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_workdirrE      s      t!!'** B 	 	B#))"-- Qd2hh Q Q Q  
 BA4r3   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.)osgetenvhermes_constantsrJ   )rF   r9   
is_gatewaysudo_failuresfailure_dhhs         r*   _handle_sudo_failurerR      s     344J   M ! E EfDDDDDD  Eaeaeagag  E  E  E  E  E  E  E  Mr3   -   timeout_secondsc                 F   ddl }ddl}t          "	 t                      pdS # t          $ r Y dS w xY wdddfd}	 dt          j        d<   |                    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)password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,   rV   z/dev/tty      )   
   r3   zutf-8replace)errorsz)Failed to restore terminal attributes: %szFailed to close tty fd: %srW   )platformsystemmsvcrtgetwchKeyboardInterruptappendjointermiosrK   openO_RDONLY	tcgetattrECHO	tcsetattr	TCSAFLUSHreaddecodeEOFErrorr   r#   r   r    close)tty_fd	old_attrsre   charscrj   	new_attrsb_termiosr(   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timeoutrW   rV   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
)systimer/   r#   rK   environsleepprint	threadingThreadstartri   stdoutr   rs   rg   )rT   r   time_moduler}   password_threadrV   r(   r|   s          @r*   _prompt_for_sudo_passwordr      s    JJJ *	*,,22 	 	 	22	 ..F." ." ." ." ."`03-0
)*#()))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   " 
00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><>)
isinstancer   rC   r#   type__name__)r8   r   s     r*   _safe_command_previewr   I  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splitboolrerB   )r   name_values      r*   _looks_like_env_assignmentr   T  sW    
%5++C00u;;sA&&LD&4d;;<<<r3   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)r8   r   inrD   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r3   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^   #Nz&&z||z;;r   z;|&()sudozsudo -S -p ''r,   )r   r   rh   findr   r   r   ri   )
r8   outr   r   command_startfoundrD   comment_endr   next_is
             r*   _rewrite_real_sudo_invocationsr     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<<r3   c                    | dS t          |           \  }}|s| dfS dt          j        v }|r t          j                            dd          nt          }|s*|s(t          j        d          rt          d          }|r|a|s|r||dz   fS | dfS )	a[  
    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) 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_INTERACTIVErS   )rT   r[   )r   rK   r   getr-   rL   r   )r8   transformedhas_real_sudohas_configured_passwordsudo_passwords        r*   _transform_sudo_commandr     s    H z!?!H!HK }-;;RmBJNN?B777XmM" 2= 2RYG[=\=\ 21"EEE 	2$1! 1- 1MD000D=r3   )LocalEnvironment)SingularityEnvironment)SSHEnvironment)DockerEnvironment)ModalEnvironment)ManagedModalEnvironment)is_managed_tool_gateway_readyu  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.
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_overridesr   )  s      $-   r3   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   popr   s    r*   clear_task_env_overridesr   <  s      GT*****r3   integerr   default
type_labelc           
          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.)rK   rL   
ValueErrorjsonJSONDecodeError)r   r   	converterr   raws        r*   _parse_env_varr   F  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	}t          j        d
|          
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%
d&|d'|d(t          d)d*          d+t          d,d-          d.t          j        d/d0          d1t          j        d2d0          d3t          d4d5          d6t          j        d7d0          d8t          j        d9t          j        d:d;                                                    dv t          j        d<d                                          dv t          d=d>t"          d?          t          d@dA          t          dBdC          t          j        dDd;                                          dv t          dEdt          j        d          dFS )GzBGet terminal environment configuration from environment variables.*nikolaik/python-nodejs:python3.11-nodejs20TERMINAL_ENVlocal&TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACEfalse)truer~   yesssh~/rootTERMINAL_CWDN)z/Users/z/home/zC:\zC:/dockerc              3   B   K   | ]}                     |          V  d S Nr   ).0p	candidates     r*   	<genexpr>z"_get_env_config.<locals>.<genexpr>s  s1      ??A	$$Q''??????r3   )
/workspacer   r   )modalr   singularitydaytonac              3   B   K   | ]}                     |          V  d S r   r   )r   r   cwds     r*   r   z"_get_env_config.<locals>.<genexpr>z  s/      DD3>>!,,DDDDDDr3   zeIgnoring TERMINAL_CWD=%r for %s backend (host/relative path won't work in sandbox). Using %r instead.r9   
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   host_cwddocker_mount_cwd_to_workspacer   TERMINAL_TIMEOUT180lifetime_secondsTERMINAL_LIFETIME_SECONDS300ssh_hostTERMINAL_SSH_HOSTr,   ssh_userTERMINAL_SSH_USERssh_portTERMINAL_SSH_PORT22ssh_keyTERMINAL_SSH_KEYssh_persistentTERMINAL_SSH_PERSISTENTTERMINAL_PERSISTENT_SHELLr   TERMINAL_LOCAL_PERSISTENTTERMINAL_CONTAINER_CPUr~   numberTERMINAL_CONTAINER_MEMORY5120TERMINAL_CONTAINER_DISK51200TERMINAL_CONTAINER_PERSISTENTTERMINAL_DOCKER_VOLUMES)local_persistentcontainer_cpucontainer_memorycontainer_diskcontainer_persistentdocker_volumes)rK   rL   lowergetcwdr&   abspath
expanduseranyisabsisdirr   r   infor   r   r   loadsfloat)default_imager9   mount_docker_cwddefault_cwdr  host_prefixesdocker_cwd_sourceis_host_pathis_relativer   r   s            @@r*   _get_env_configr?  V  s.    AMy11Hy!I7SSYY[[_ss
 7ikk	U		 )NK
0
0CH8M8 0In55DGOOBG$6$67H$I$IJJ	?????????	i((	-/W]]9-E-E	NWNbNbczN{N{	 !HC	B	B	Bs	B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  	s  	H  	()9  	>"4e<<  	N+FNN  	BI1266  	BI1266   	N#6==! " 	29/44# * 	")%I16::
 
 %'')*+ 2 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?       r3   r   c                 X    t          | t                      t          d                    S )z2Resolve direct vs managed Modal backend selection.r   )
has_directmanaged_ready)r   r   r   )r   s    r*   _get_modal_backend_staterC    s0    &/113G<<   r3   imager   r   
ssh_configcontainer_configlocal_configr  c	                    |pi }	|	                     dd          }
|	                     dd          }|	                     dd          }|	                     dd          }|	                     d	g           }|	                     d
g           }|	                     di           }| dk    rt          ||          S | dk    r0t          ||||
|||||||	                     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|r*|                     d)          r|                     d*          st          d+          t'          |d)         |d*         |                     d,d-          |                     d.d/          ||0          S t          d1|  d2          )3a  
    Create an execution environment for sandboxed command execution.
    
    Args:
        env_type: One of "local", "docker", "singularity", "modal", "daytona", "ssh"
        image: Docker/Singularity/Modal image name (ignored for local/ssh)
        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+     r,  Tr-  r  
docker_envr   )r   r   r   r  F)rD  r   r   cpumemorydiskpersistent_filesystemr   volumesr  auto_mount_cwdforward_envenvr   )rD  r   r   rL  rM  rN  rO  r   r   r   rL  rM  Nephemeral_diskr   selected_backendmanaged)rD  r   r   modal_sandbox_kwargsrO  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.r   )DaytonaEnvironmentr   hostuserz?SSH environment requires ssh_host and ssh_user to be configuredport   keyr,   )r\  r]  r^  key_pathr   r   zUnknown environment type: zD. Use 'local', 'docker', 'singularity', 'modal', 'daytona', or 'ssh')r   _LocalEnvironment_DockerEnvironment_SingularityEnvironmentinspectr   	signatureSandboxcreate
parametersr#   rC  _ManagedModalEnvironmentr   r   _ModalEnvironmenttools.environments.daytonar[  int_SSHEnvironment)r9   rD  r   r   rE  rF  rG  r   r  ccrL  rM  rN  
persistentrP  r  rK  sandbox_kwargsre  r   modal_statemessage_DaytonaEnvironments                          r*   _create_environmentru    s7   * 
	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*	
 	
 	
 		
 
]	"	"&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
 
 
 	
 
U		 	`!7!7 	`z~~f?U?U 	`^___F#F#++^^E2..
 
 
 	
   Eh  E  E  E  F  F  	Fs   /3E# #
E0/E0,  r  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_registryry  listr   keyshas_active_processesImportError	_env_lockitemsr   r   rh   _creation_locks_lockr   tools.file_toolsr{  hasattrr|  r}  r~  r   r5  r#   r   r.  r"   )r  current_timery  r   envs_to_stop	last_timerS  _r{  r(   	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.r  zError in cleanup thread: %sTr   N<   r^   )	_cleanup_runningr?  r  r#   r   r"   ranger   r   )configr(   r  s      r*   _cleanup_thread_workerr  m  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  r   r   r  r    r3   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  ri   
SystemExitrg   r  r3   r*   _stop_cleanup_threadr    sb     "	   +++++-. 	 	 	DD	 #"s   ( ==c                 x    t           5  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)r  r   r   r   s    r*   get_active_envr    s}    	 1 1#''001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1s   /33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   getattr)r   rS  s     r*   is_persistent_envr    s5     
!
!C
{u]E22333r3   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: %sTr   Nr   )ignore_errorszRemoved orphaned: %sz%Failed to remove orphaned path %s: %szCleaned %d environments)r  r   r  
cleanup_vmr#   r   errorr
   r   r   shutilrmtreer5  r   r    )task_idscleanedr   r(   r$   r   r&   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   rz  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   r5  r#   r   r.  r"   )r   rS  r{  r(   r  s        r*   r  r    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   r5  r  )counts    r*   _atexit_cleanupr    sU     #())?GGG """""# #r3   	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   stripr   r   )	r8   r  segmentslast_segmentwordsbase_cmdw	semanticscmd_semanticss	            r*   _interpret_exit_coder    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''4r3   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)ri   r.  r   r   )r8   
normalizeds     r*   _command_requires_pipe_stdinr  C  sL     '--////1122Jo.. 	)j(r3   
backgroundforceptynotify_on_completewatch_patternsc	                 L   	 t          | t                    s]t                              dt	          |           j                   t          j        dddt	          |           j         ddd          S t                      }	|	d	         }
|pd
}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 t                       t          5  |t          v r+t          j                    t           |<   t          |         }d}nd}ddd           n# 1 swxY w Y   |r t"          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   |rA|
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.g           |	
                    d/d          d0}d}|
d1k    rd2|	
                    d3d          i}t/          |
||||||||	
                    d4          5	  	        }nB# t0          $ r5}t          j        ddd6| d7d8d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                              d9|
|dd                    ddd           n# 1 swxY w Y   d}|sMt3          | |
          }|d:         s|
                    d;          d<k    rnt          j        dd|
                    d=d>          d<|
                    d?|           |
                    d@dA          |
                    dBd          dCd          S |
                    d@dA          }dD| dE}t          j        dd|
                    d=|          dFdd          S |
                    dG          r|
                    d@dH          }dI| dJ}n1|
                    dK          r|
                    d@dH          }dL| dM}|r]t5          |          }|rLt                              dN|ddO         t7          |                      t          j        dd|dFdd          S d}|}|rt9          |           rd}dP}|rdQdRlm}  dQdSlm }!  | dT          }"|p|}#	 |
d1k    r3|!!                    | |#||"tE          |dU          r|j#        nd|V          }$n|!$                    || |#||"W          }$dX|$j%        |$j&        dQddY}%|r||%dZ<   |r||%d[<   |rk|s|rgdQd\l'm(}&  |&d]d          }'|'rS |&d^d          }( |&d_d          }) |&d`d          }* |&dad          }+|'|$_)        |(|$_*        |*|$_+        |+|$_,        |)|$_-        |r^|r\d|$_.        d|%db<   |$j)        rIdc|$_/        |!j0        1                    |$j%        dc|"|$j)        |$j*        |$j+        |$j,        |$j-        ddd	           |r |rte          |          |$_3        |$j3        |%de<   t          j        |%d          S # th          $ r4}t          j        dddft          |           dgd          cY d}~S d}~ww xY wdh},dQ}-d}.|-|,k    re	 d|i}/|r||/d<    |j5        | fi |/}.nH# th          $ r:}t          |          6                                }0d|0v r#t          j        ddidj| dkdgd          cY d}~S |-|,k     rd|-d&z  }-dl|-z  }1t                              dm|1|-|,t7          |           t	          |          j        |||
	  	         t          j7        |1           Y d}~t          8                    dn|,t7          |           t	          |          j        |||
           t          j        dddot	          |          j         dpt          |           dgd          cY d}~S d}~ww xY w	 |.
                    dqd          }2|.
                    drdQ          }3ts          |2|
          }2ds}4tu          |2          |4k    r[tw          |4dtz            }5|4|5z
  }6tu          |2          |5z
  |6z
  }7du|7 dvtu          |2           dw}8|2d|5         |8z   |2|6 d         z   }2dQdxl<m=}9  |9|2          }2dQdyl>m?}: |2r |:|2@                                          nd}2t          | |3          };|2|3ddg}<|r||<dZ<   |;r|;|<dz<   t          j        |<d          S # th          $ ri}dQdlB}=|=C                                }>t          8                    d{|>           t          j        ddd|t          |           |>dd}d          cY d}~S d}~ww xY w)~a+  
    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, auto-notify the agent when the process exits
        watch_patterns: List of strings to watch for in background output; triggers notification on match

    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 r  )rF   r  r  statusF)ensure_asciir9   r   r   r  r   r  r   r  r   r
  r   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...   r   r  r  r  r_  r  r  )r\  r]  r^  r`  rp  )r   r   r   r   r)  r^   r*  rI  r+  rJ  r,  r   r   r-  r  )r)  r*  r+  r,  r   r-  r  r   rp  r(  r  )	r9   rD  r   r   rE  rF  rG  r   r  z5Terminal tool disabled: environment creation failed (r   disabledz %s environment ready for task %sapprovedr  approval_requiredrs  zWaiting for user approvalr8   descriptionzcommand flaggedpattern_key)rF   r  r  r  r8   r  r  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_keyrx  )r   rS  )r8   r   r   session_keyenv_varsuse_pty)rS  r8   r   r   r  zBackground process started)rF   
session_idpidr  r  approvalpty_note)get_session_envHERMES_SESSION_PLATFORMHERMES_SESSION_CHAT_IDHERMES_SESSION_THREAD_IDHERMES_SESSION_USER_IDHERMES_SESSION_USER_NAMEr  r  )	r  check_intervalr  rc   chat_iduser_id	user_name	thread_idr  r  z$Failed to start background process: )rF   r  r  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: r   rF   
returncodeiP  g?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: )rF   r  r  	tracebackr  )Dr   r   r   r"   r   r   r   dumpsr?  r   r   FOREGROUND_MAX_TIMEOUTr  r  r   r   r   r  r   r   Lockr+   r5  ru  r  r>   rE   r   r  tools.approvalr  r  ry  spawn_localr  rS  spawn_via_envidr  gateway.session_contextr  watcher_platformwatcher_chat_idwatcher_user_idwatcher_user_namewatcher_thread_idr  watcher_intervalpending_watchersrh   r  r  r#   executer.  r   r  rR   r   rm  tools.ansi_stripr  agent.redactr  r  r  r  
format_exc)?r8   r  r   r   r  r?   r  r  r  r  r9   effective_task_idr   rD  r   default_timeouteffective_timeoutrS  needs_creation	task_lockrE  rF  rG  new_envr(   approval_noter  descfallback_msgworkdir_errorpty_disabled_reasoneffective_ptyr  ry  r  effective_cwdproc_sessionresult_data_gse_gw_platform_gw_chat_id_gw_thread_id_gw_user_id_gw_user_namemax_retriesretry_countr|   execute_kwargsr  	wait_timerF   r  MAX_OUTPUT_CHARS
head_chars
tail_charsomittedtruncated_noticer  r  	exit_noteresult_dictr  tb_strs?                                                                  r*   terminal_toolr3  R  sI   RC'3'' 
	#NN=W&   :Z$w--BXZZ!	 
 "# # # # !""*% $0y (++,=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 "# # # # 	  	& 	& $88848IKK01*+<=!&!%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&  G	e% ? ?$O;;9B9I9IO$56+,=>	? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
  @e @e / /(,@@@<@IKK'8923DE).	/ / / / / / / / / / / / / / / " 8e=001333KK LhXijlkljlXmnnn./%)
#u,,(.

:r(B(B(.

:r(B(B(.

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

CY[_8`8`.4jjv.N.N28**=Mr2R2RAGLkmrAsAs0 0, (,#w.. ,fjj9KU.S.S,L #6%-"' #$5'1-=)5$5%+ZZ
%;%;
# 
# 
# ' / / /#z&()+%a]^%a%a%a&0	+ +
 ). /  /  / / / / / /k@e @e @e @e @e @e @e @eh/ # & &BI,->?<@IKK'89%& & & & & & & & & & & & & & & KK BHN_`bab`bNcdddA@e @e @e @e @e @e @e @e @e @e @e @e @e @e @eH   	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    n	? ?>>>>>??????11"===K#NsMQ'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 & * 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  %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  A1l0 4D"l0 l0 ,7G/#l0 /G33l0 6G37l0 2I6l0 Il0 	I
l0 S24JS2J	S2J	>S2EP:9S2:
Q9Q4"Q9#S2'l0 4Q99
S2(R7+S27R;	;S2>R;	?'S2&l0 2S66l0 9S6:B&l0 !A
l0 ,Cl0 .8l0 'E7a 
b))bbl0 bl0 .c	 l0 	hAh	hl0 A%h	l0 A=h	hl0 	hD!l0 0
n#:Ann#n#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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.r9   r   Tr   r   )find_dockerz?Docker executable not found in PATH or common install locationsFversionr  )capture_outputr   r   	apptainerz	--versionr   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   r   rU  rV  rX  rY  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.rZ  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 modalr   )DaytonaDAYTONA_API_KEYzWUnknown TERMINAL_ENV '%s'. Use one of: local, docker, singularity, modal, daytona, ssh.z&Terminal requirements check failed: %sr   )r?  tools.environments.dockerr5  r   r  
subprocessrunr  r  whichr   rC  r   	importlibutil	find_specr   r9  rK   rL   r#   )	r  r9   r5  r   r|   
executablerr  r9  r(   s	            r*   check_terminal_requirementsrC    s   Fj!Ha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""''''''9.//t;; LL'  
 5   =q4PPPuuuuusb   J= 2J= #J= 8AJ= A
J= 5J= .J= &J= +AJ= <AJ= 9J= >!J=  J= =
K/K**K/__main__zTerminal Tool Modulez2==================================================z
Current Configuration:z  Environment type: z  Docker image: r  z  Modal image: r  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:r   z  TERMINAL_ENV: r   r   z- (local/docker/singularity/modal/daytona/ssh)z  TERMINAL_DOCKER_IMAGE: r  z  TERMINAL_SINGULARITY_IMAGE: r  r  z  TERMINAL_MODAL_IMAGE: r	  z  TERMINAL_DAYTONA_IMAGE: r  z  TERMINAL_CWD: r   rI   z  TERMINAL_SANDBOX_DIR: TERMINAL_SANDBOX_DIRz
/sandboxesz  TERMINAL_TIMEOUT: r  60z  TERMINAL_LIFETIME_SECONDS: r  r  )registryterminalobjectstringz The command to execute on the VM)r   r  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   r  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   r  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 when the process finishes — no polling needed. Use this for tasks that take a while (tests, builds, deployments) so you can keep working on other things in the meantime.arrayr   uT  List of strings to watch for in background process output. When any pattern matches a line of output, you'll be notified with the matching text — like notify_on_complete but triggers mid-process on specific output. Use for monitoring logs, watching for errors, or waiting for specific events (e.g. ["ERROR", "FAIL", "listening on port"]).)r   r  r  )r8   r  r   r?   r  r  r  )r   
propertiesrequired)r   r  ri  c                 f   t          |                     d          |                     dd          |                     d          |                    d          |                     d          |                     dd          |                     dd          |                     d	          
          S )Nr8   r  Fr   r   r?   r  r  r  )r8   r  r   r   r?   r  r  r  )r3  r   )argskws     r*   _handle_terminalrT    s    ##88L%00##y!!##HHUE""88$8%@@xx 011	 	 	 	r3   u   💻i )r   toolsetschemahandlercheck_fnemojimax_result_size_chars)rS   )r   )NNNr   N)rv  )FNNFNFFN)}__doc__importlib.utilr?  r   loggingrK   rc   r   r   r   atexitr  r<  pathlibr   typingr   r   r   r   	getLoggerr   r   tools.interruptr   r	   tools.environments.singularityr
   tools.tool_backend_helpersr   r   r   r   rm  rL   r   r7  r!   r+   r-   r   __annotations__r/   r5   r2   r6   r  r7   r=   dictr>   compilerA   rE   rR   r   r   r   r   tupler   r   r   tools.environments.localr   rb  r   rd  tools.environments.sshr   rn  r;  r   rc  tools.environments.modalr   rk   tools.environments.managed_modalr   rj  tools.managed_tool_gatewayr   TERMINAL_TOOL_DESCRIPTIONr   r   r  r  r   r  r  r  r   r   r   r   r?  rJ  rC  ru  r  r  r  r  r  r  r  r  r  registerr  r  r3  rC  r   r  exitdefault_imgr/  rM   rJ   rQ   tools.registryrH  TERMINAL_SCHEMArT  r  r3   r*   <module>rt     s    >       				  				                  , , , , , , , , , , , ,		8	$	$ = < < < < < < < < ; ; ; ; ;            YRY'H%PPQQ  #(%		2Le(T(T"U"U   <   s      ! ! !       
Hs Hc Hd H H H H 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6S4Z 6E#*cDj:P4Q 6 6 6 6t 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 + + + + 7:Y 
 
 
s 
s 
 
 
 
 Lc3h L L L L^$ 4S>     KO-1'0(,	~F ~F# ~Fc ~F ~Fc ~F$(~FCG~F&*~F "%~F #&	~F ~F ~F ~FB;\ ;\S ;\ ;\ ;\ ;\|  $ $ $  1C 1 1 1 14s 4t 4 4 4 4&  6'X 'X 'X 'X 'XT# # #       =# =# =#* = = = =@# $    " !!!$*.l lll c]l c]	l
 l c]l 
l l T#Y'l 	l l l l^fT f f f fR 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
nYRY~w??
n
n
nooo	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   |   |   |  ! 
 "  ~   "  O # #   (+  | ;"
 "
F KK& &* *Z
 
 
  	(
!     r3   