
    Ki                       d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ  e	d	
           G d d                      Z e	d	
           G d d                      ZddZddZ G d de          ZdS )an  Shared Hermes-side execution flow for Modal transports.

This module deliberately stops at the Hermes boundary:
- command preparation
- cwd/timeout normalization
- stdin/sudo shell wrapping
- common result shape
- interrupt/cancel polling

Direct Modal and managed Modal keep separate transport logic, persistence, and
trust-boundary decisions in their own modules.
    )annotationsN)abstractmethod)	dataclass)Any)BaseEnvironment)is_interruptedT)frozenc                  @    e Zd ZU dZded<   ded<   ded<   dZded	<   dS )
PreparedModalExeczCNormalized command data passed to a transport-specific exec runner.strcommandcwdinttimeoutN
str | None
stdin_data)__name__
__module____qualname____doc____annotations__r        </home/ubuntu/hermes-agent/tools/environments/modal_common.pyr   r      sD         MMLLLHHHLLL!J!!!!!!r   r   c                  0    e Zd ZU dZdZded<   dZded<   dS )ModalExecStartz*Transport response after starting an exec.Nz
Any | Nonehandledict | Noneimmediate_result)r   r   r   r   r   r   r   r   r   r   r   r   %   s;         44F$(((((((r   r   r   r   r   returnc                    dt          j                    j        dd          }||v r'dt          j                    j        dd          }||v '|  d| d| d| S )zDAppend stdin as a shell heredoc for transports without stdin piping.HERMES_EOF_N   z << 'z'

)uuiduuid4hex)r   r   markers      r   wrap_modal_stdin_heredocr)   -   s|    14:<<+BQB/11F
J

5tz||/355 J

==F==z==V===r   
sudo_stdinc                Z    dt          j        |                                           d|  S )zFFeed sudo via a shell pipe for transports without direct stdin piping.zprintf '%s\n' z | )shlexquoterstrip)r   r*   s     r   wrap_modal_sudo_piper/   5   s.    KU[):):)<)<==KK'KKKr   c                      e Zd ZU dZdZdZdZded<   dZdZ		 d(ddd
d)dZ
d*dZd	dddd+dZd,dZd-dZd.dZed/d"            Zed0d&            Zed1d'            ZdS )2BaseModalExecutionEnvironmentz>Common execute() flow for direct and managed Modal transports.payloadg      ?Nzfloat | None_client_timeout_grace_secondsz[Command interrupted]zModal execution error )r   r   r   r   r   r   
int | Noner   r   r    dictc                  |                                   |                     ||||          }	 |                     |          }n6# t          $ r)}|                     | j         d|           cY d }~S d }~ww xY w|j        |j        S |j        |                     | j         d          S d }| j        #t          j
                    |j        z   | j        z   }	 t                      rG	 |                     |j                   n# t          $ r Y nw xY w|                     | j        d          S 	 |                     |j                  }	n6# t          $ r)}|                     | j         d|           cY d }~S d }~ww xY w|	|	S |]t          j
                    |k    rF	 |                     |j                   n# t          $ r Y nw xY w|                     |j                  S t          j        | j                   %)Nr   r   r   z: z): transport did not return an exec handleT   )_before_execute_prepare_modal_exec_start_modal_exec	Exception_error_result_unexpected_error_prefixr   r   r3   time	monotonicr   r   _cancel_modal_exec_result_interrupt_output_poll_modal_exec_timeout_result_for_modalsleep_poll_interval_seconds)
selfr   r   r   r   preparedstartexcdeadlineresults
             r   executez%BaseModalExecutionEnvironment.executeC   s    	++!	 , 
 
	Q**844EE 	Q 	Q 	Q%%)F&O&O#&O&OPPPPPPPP	Q !-))<%%0[[[   -9~''(*::T=__H	4 A++EL9999    D||D$:C@@@U..u|<< U U U))T-J*S*Sc*S*STTTTTTTTU !#(8(8H(D(D++EL9999    D55h6FGGGJt2333/	4s_   A 
A8A3-A83A8)D 
DD0E 
E>E93E>9E>F: :
GGNonec                    dS )z8Hook for backends that need pre-exec sync or validation.Nr   )rI   s    r   r:   z-BaseModalExecutionEnvironment._before_execute}   s    tr   r8   r   c                   |p| j         }|p| j        }|}| j        dk    r|nd }|| j        dk    rt          ||          }|                     |          \  }}	|	t          ||	          }t          ||||          S )Nr2   heredoc)r   r   r   r   )r   r   _stdin_moder)   _prepare_commandr/   r   )
rI   r   r   r   r   effective_cwdeffective_timeoutexec_command
exec_stdinr*   s
             r   r;   z1BaseModalExecutionEnvironment._prepare_modal_exec   s     tx#3t|#'#3y#@#@ZZd
!d&6)&C&C3L*MML#'#8#8#F#F j!/jIIL  %!	
 
 
 	
r   output
returncoder   c                    ||dS )N)rZ   r[   r   )rI   rZ   r[   s      r   rC   z%BaseModalExecutionEnvironment._result   s    $
 
 	
r   c                .    |                      |d          S )N   rC   )rI   rZ   s     r   r>   z+BaseModalExecutionEnvironment._error_result   s    ||FA&&&r   c                6    |                      d| dd          S )NzCommand timed out after s|   r_   )rI   r   s     r   rF   z7BaseModalExecutionEnvironment._timeout_result_for_modal   s"    ||AwAAA3GGGr   rJ   r   c                    dS )z Begin a transport-specific exec.Nr   )rI   rJ   s     r   r<   z/BaseModalExecutionEnvironment._start_modal_exec         r   r   r   r   c                    dS )z8Return a final result dict when complete, else ``None``.Nr   rI   r   s     r   rE   z.BaseModalExecutionEnvironment._poll_modal_exec   rd   r   c                    dS )z.Cancel or terminate the active transport exec.Nr   rf   s     r   rB   z0BaseModalExecutionEnvironment._cancel_modal_exec   rd   r   )r4   )
r   r   r   r   r   r5   r   r   r    r6   )r    rP   )
r   r   r   r   r   r5   r   r   r    r   )rZ   r   r[   r   r    r6   )rZ   r   r    r6   )r   r   r    r6   )rJ   r   r    r   )r   r   r    r   )r   r   r    rP   )r   r   r   r   rT   rH   r3   r   rD   r?   rO   r:   r;   rC   r>   rF   r   r<   rE   rB   r   r   r   r1   r1   :   sW        HHK!26!6666/6
 84
 #!%84 84 84 84 84 84t    "!%
 
 
 
 
 
6
 
 
 
' ' ' 'H H H H / / / ^/ G G G ^G = = = ^= = =r   r1   )r   r   r   r   r    r   )r   r   r*   r   r    r   )r   
__future__r   r,   r@   r%   abcr   dataclassesr   typingr   tools.environments.baser   tools.interruptr   r   r   r)   r/   r1   r   r   r   <module>rn      s    # " " " " "          ! ! ! ! ! !       3 3 3 3 3 3 * * * * * * $" " " " " " " " $) ) ) ) ) ) ) )> > > >L L L L
x= x= x= x= x=O x= x= x= x= x=r   