
    `gj                        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y)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	<   y)
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        Q/root/.hermes/venv/lib/python3.12/site-packages/tools/environments/modal_utils.pyr   r      s    ML	HL!J
!r   r   c                  .    e Zd ZU dZdZded<   dZded<   y)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FJ$(k(r   r   c                    dt        j                         j                  dd  }||v r)dt        j                         j                  dd  }||v r)|  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(   -   sq    4::<++BQ/01F
J
tzz|//345 J
YeF83zl"VH==r   c                T    dt        j                  |j                                d|  S )zFFeed sudo via a shell pipe for transports without direct stdin piping.zprintf '%s\n' z | )shlexquoterstrip)r   
sudo_stdins     r   wrap_modal_sudo_piper.   5   s(    U[[):):)<=>c'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	 	 	 	 	 	 	 	 	 	 	 	 	 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dd       Zed d       Zy)!BaseModalExecutionEnvironmentu  Execution flow for the *managed* Modal transport (gateway-owned sandbox).

    This deliberately overrides :meth:`BaseEnvironment.execute` because the
    tool-gateway handles command preparation, CWD tracking, and env-snapshot
    management on the server side.  The base class's ``_wrap_command`` /
    ``_wait_for_process`` / snapshot machinery does not apply here — the
    gateway owns that responsibility.  See ``ManagedModalEnvironment`` for the
    concrete subclass.
    payloadg      ?Nzfloat | None_client_timeout_grace_secondsz[Command interrupted]zModal execution error TF)r   r   rewrite_compound_backgroundbounded_capturec                  |}|}| j                          | j                  ||||      }	 | j                  |      }	|	j                  |	j                  S |	j                  | j	                  | j
                   d      S d }| j                  .t        j                         |j                  z   | j                  z   }t        j                         }||d}	 t               r8	 | j                  |	j                         | j                  | j                  d      S 	 | j!                  |	j                        }||S |Nt        j                         |k\  r7	 | j                  |	j                         | j#                  |j                        S 	 ddlm}  ||d       t        j(                  | j*                         # t        $ r*}
| j	                  | j
                   d|
       cY d }
~
S d }
~
ww xY w# t        $ r Y w xY w# t        $ r*}
| j	                  | j
                   d|
       cY d }
~
S d }
~
ww xY w# t        $ r Y w xY w# t        $ r Y w xY w)	Nr   r   r   z: z): transport did not return an exec handle)
last_touchstart   r   )touch_activity_if_duezmodal command running)_before_execute_prepare_modal_exec_start_modal_exec	Exception_error_result_unexpected_error_prefixr   r   r2   time	monotonicr   r   _cancel_modal_exec_result_interrupt_output_poll_modal_exec_timeout_result_for_modaltools.environments.baser;   sleep_poll_interval_seconds)selfr   r   r   r   r4   r5   _preparedr9   excdeadline_now_activity_stateresultr;   s                   r   executez%BaseModalExecutionEnvironment.executeK   sb    (
 ++!	 , 
	Q**84E !!-)))<<%%0011Z[  --9~~'(*:*::T=_=__H~~

 ++ELL9 ||D$:$:C@@U..u||< !#(8H(D++ELL9 55h6F6FGGI%o7NO JJt223= +  	Q%%)F)F(Gr#&OPP	Q2 !   U))T-J-J,K2cU*STTU !   sq   F6 G, G; H1 I  6	G)?G$G)$G),	G87G8;	H.H)#H.)H.1	H=<H= 	IIc                     y)z8Hook for backends that need pre-exec sync or validation.Nr   )rL   s    r   r<   z-BaseModalExecutionEnvironment._before_execute   s    r   r7   c                  |xs | j                   }|xs | j                  }|}| j                  dk(  r|nd }|| j                  dk(  rt        ||      }| j	                  |      \  }}	|	t        ||	      }t        ||||      S )Nr1   heredoc)r   r   r   r   )r   r   _stdin_moder(   _prepare_commandr.   r   )
rL   r   r   r   r   effective_cwdeffective_timeoutexec_command
exec_stdinr-   s
             r   r=   z1BaseModalExecutionEnvironment._prepare_modal_exec   s     txx#3t||#'#3#3y#@Zd
!d&6&6)&C3L*ML#'#8#8#F j!/jIL  %!	
 	
r   c                    ||dS )N)output
returncoder   )rL   r_   r`   s      r   rE   z%BaseModalExecutionEnvironment._result   s    $
 	
r   c                &    | j                  |d      S )N   rE   )rL   r_   s     r   r@   z+BaseModalExecutionEnvironment._error_result   s    ||FA&&r   c                .    | j                  d| dd      S )NzCommand timed out after s|   rc   )rL   r   s     r   rH   z7BaseModalExecutionEnvironment._timeout_result_for_modal   s    ||6wiqA3GGr   c                     y)z Begin a transport-specific exec.Nr   )rL   rN   s     r   r>   z/BaseModalExecutionEnvironment._start_modal_exec       r   c                     y)z8Return a final result dict when complete, else ``None``.Nr   rL   r   s     r   rG   z.BaseModalExecutionEnvironment._poll_modal_exec   rh   r   c                     y)z.Cancel or terminate the active transport exec.Nr   rj   s     r   rD   z0BaseModalExecutionEnvironment._cancel_modal_exec   rh   r   )r3   )r   r   r   r   r   
int | Noner   r   r4   boolr5   rm   returndict)rn   None)
r   r   r   r   r   rl   r   r   rn   r   )r_   r   r`   r   rn   ro   )r_   r   rn   ro   )r   r   rn   ro   )rN   r   rn   r   )r   r   rn   r   )r   r   rn   rp   )r   r   r   r   rX   rK   r2   r   rF   rA   rT   r<   r=   rE   r@   rH   r   r>   rG   rD   r   r   r   r0   r0   :   s)    K!26!<6/6
 P4
 #!%,0 %P4P4 P4
 P4 P4 &*P4 P4 
P4d "!%

 	

 
 
 

6
'H / / G G = =r   r0   )r   r   r   r   rn   r   )r   r   r-   r   rn   r   )r   
__future__r   r*   rB   r$   abcr   dataclassesr   typingr   rI   r   tools.interruptr   r   r   r(   r.   r0   r   r   r   <module>rv      s|    #     !  3 * $" " " $) ) )>L
X=O X=r   