
    Ki	                     j    d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	 defdZ
 G d d	e          ZdS )
z9Base class for all Hermes execution environment backends.    )ABCabstractmethodN)Path)get_hermes_homereturnc                      t          j        d          } | rt          |           }nt                      dz  }|                    dd           |S )zReturn the host-side root for all sandbox storage (Docker workspaces,
    Singularity overlays/SIF cache, etc.).

    Configurable via TERMINAL_SANDBOX_DIR. Defaults to {HERMES_HOME}/sandboxes/.
    TERMINAL_SANDBOX_DIR	sandboxesT)parentsexist_ok)osgetenvr   r   mkdir)customps     4/home/ubuntu/hermes-agent/tools/environments/base.pyget_sandbox_dirr      sT     Y-..F ,LL+GGD4G(((H    c                       e Zd ZdZddededefdZeddddd	edededz  d
edz  def
d            Z	ed             Z
d Zd Zd	edeeedz  f         fdZ	 ddedz  d
edz  defdZdedz  defdZdS )BaseEnvironmentzCommon interface for all Hermes execution backends.

    Subclasses implement execute() and cleanup(). Shared helpers eliminate
    duplicated subprocess boilerplate across backends.
    Ncwdtimeoutenvc                 4    || _         || _        |pi | _        d S N)r   r   r   )selfr   r   r   s       r   __init__zBaseEnvironment.__init__!   s    9"r    )r   
stdin_datacommandr   r   c                    dS )z=Execute a command, return {"output": str, "returncode": int}.N )r   r    r   r   r   s        r   executezBaseEnvironment.execute&   s	    
 	r   c                     dS )z<Release backend resources (container, instance, connection).Nr"   r   s    r   cleanupzBaseEnvironment.cleanup-   s	     	r   c                 .    |                                   dS )z.Alias for cleanup (compat with older callers).N)r&   r%   s    r   stopzBaseEnvironment.stop2   s    r   c                 R    	 |                                   d S # t          $ r Y d S w xY wr   )r&   	Exceptionr%   s    r   __del__zBaseEnvironment.__del__6   s:    	LLNNNNN 	 	 	DD	s    
&&c                 $    ddl m}  ||          S )u  Transform sudo commands if SUDO_PASSWORD is available.

        Returns:
            (transformed_command, sudo_stdin) — see _transform_sudo_command
            for the full contract.  Callers that drive a subprocess directly
            should prepend sudo_stdin (when not None) to any stdin_data they
            pass to Popen.  Callers that embed stdin via heredoc (modal,
            daytona) handle sudo_stdin in their own execute() method.
        r   )_transform_sudo_command)tools.terminal_toolr-   )r   r    r-   s      r   _prepare_commandz BaseEnvironment._prepare_command@   s(     	@?????&&w///r   c                 |    d|p| j         ddt          j        t          j        d}|||d<   nt          j        |d<   |S )zABuild common subprocess.run kwargs for non-interactive execution.Tzutf-8replace)textr   encodingerrorsstdoutstderrNinputstdin)r   
subprocessPIPESTDOUTDEVNULL)r   r   r   kws       r   _build_run_kwargsz!BaseEnvironment._build_run_kwargsM   sR     .$, o '
 
 !$BwKK$,BwK	r   c                 "    d|p| j          dddS )z.Standard return dict when a command times out.zCommand timed out after s|   )output
returncode)r   )r   r   s     r   _timeout_resultzBaseEnvironment._timeout_result^   s,     L1HDLKKK
 
 	
r   r   )r   )__name__
__module____qualname____doc__strintdictr   r   r#   r&   r(   r+   tupler/   r>   rD   r"   r   r   r   r      sz         C # D    
 &*)-  s  t$J26   ^   ^    0 0c3:o0F 0 0 0 0 48 t &)Dj<@   "
sTz 
d 
 
 
 
 
 
r   r   )rH   abcr   r   r   r9   pathlibr   hermes_constantsr   r   r   r"   r   r   <module>rP      s    ? ? # # # # # # # # 				           , , , , , ,    I
 I
 I
 I
 I
c I
 I
 I
 I
 I
r   