+
    Ki                        R t ^ RIHt ^ RIt^ RIt^ RIt^ RIHt ^ RIH	t	 ^ RI
Ht ^ RIHt ^ RIHt ]	! RR	7       ! R
 R4      4       t]	! RR	7       ! R R4      4       tR R ltR R lt ! R R]4      tR# )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                  H    ] tR t^t$ RtR]R&   R]R&   R]R&   RtR]R	&   R
tR# )PreparedModalExeczCNormalized command data passed to a transport-specific exec runner.strcommandcwdinttimeoutN
str | None
stdin_data )__name__
__module____qualname____firstlineno____doc____annotations__r   __static_attributes__r       </home/ubuntu/hermes-agent/tools/environments/modal_common.pyr
   r
      s    ML	HL!J
!r   r
   c                  8    ] tR t^%t$ RtRtR]R&   RtR]R&   RtR# )ModalExecStartz*Transport response after starting an exec.Nz
Any | Nonehandledict | Noneimmediate_resultr   )	r   r   r   r   r   r   r   r    r   r   r   r   r   r   %   s    4FJ$(k(r   r   c               $    V ^8  d   QhRRRRRR/# )   r   r   r   returnr   )formats   "r   __annotate__r%   -   s!     > >c >s >s >r   c                    R\         P                  ! 4       P                  R,           2pW!9   d,   R\         P                  ! 4       P                  R,           2pK1  V  RV RV RV 2# )zDAppend stdin as a shell heredoc for transports without stdin piping.HERMES_EOF_:N   Nz << 'z'

)uuiduuid4hex)r   r   markers   && r   wrap_modal_stdin_heredocr.   -   sa    4::<++B/01F

tzz|//345YeF83zl"VH==r   c               $    V ^8  d   QhRRRRRR/# )r"   r   r   
sudo_stdinr#   r   )r$   s   "r   r%   r%   5   s&     L L# L3 L3 Lr   c                V    R\         P                  ! VP                  4       4       RV  2# )zFFeed sudo via a shell pipe for transports without direct stdin piping.zprintf '%s\n' z | )shlexquoterstrip)r   r0   s   &&r   wrap_modal_sudo_piper5   5   s(    U[[):):)<=>c'KKr   c                      ] tR t^:t$ RtRtRtRtR]R&   Rt	Rt
R R
RRR/R R llltR R ltRR	R
RRR/R R lltR R ltR R ltR R lt]R R l4       t]R R l4       t]R R l4       tRtR# )!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   c          
     ,    V ^8  d   QhRRRRRRRRRR	/# )
r"   r   r   r   r   
int | Noner   r   r#   dictr   )r$   s   "r   r%   *BaseModalExecutionEnvironment.__annotate__C   s<     84 8484 84
 84 84 
84r   c               	r   V P                  4        V P                  VVVVR 7      p V P                  V4      pTP                  e   TP                  # TP                  f   T P	                  T P
                   R24      # RpT P                  e8   \        P                  ! 4       TP                  ,           T P                  ,           p \        4       '       d9    T P                  TP                  4       T P                  T P                  ^4      #  T P!                  TP                  4      p	T	e   T	# TeR   \        P                  ! 4       T8  d8    T P                  TP                  4       T P#                  TP                  4      # \        P$                  ! T P&                  4       K    \         d+   pT P	                  T P
                   RT 24      u Rp?# Rp?ii ; i  \         d     Li ; i  \         d+   pT P	                  T P
                   RT 24      u Rp?# Rp?ii ; i  \         d     Li ; i))r   r   r   z: Nz): transport did not return an exec handle)_before_execute_prepare_modal_exec_start_modal_exec	Exception_error_result_unexpected_error_prefixr    r   r9   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   execute%BaseModalExecutionEnvironment.executeC   s    	++!	 , 
	Q**84E !!-)))<<%%0011Z[  --9~~'(*:*::T=_=__H++ELL9 ||D$:$:C@@U..u||< !#(8H(D++ELL9 55h6F6FGGJJt223M  	Q%%)F)F(Gr#&OPP	Q& !   U))T-J-J,K2cU*STTU ! s_   F' G G0 H( 'G2GGGG-,G-0H%;H H% H%(H65H6c                   V ^8  d   QhRR/# )r"   r#   Noner   )r$   s   "r   r%   r>   }   s       r   c                    R# )z8Hook for backends that need pre-exec sync or validation.Nr   )rO   s   &r   r@   -BaseModalExecutionEnvironment._before_execute}   s    r   r   c          
     ,    V ^8  d   QhRRRRRRRRRR	/# )
r"   r   r   r   r   r<   r   r   r#   r
   r   )r$   s   "r   r%   r>      s<     
 

 	

 
 
 

r   c               	*   T;'       g    V P                   pT;'       g    V P                  pTpV P                  R 8X  d   TMRpVe   V P                  R8X  d   \        Wt4      pV P	                  V4      w  ryV	e   \        Wy4      p\        VVVVR7      # )r8   Nheredoc)r   r   r   r   )r   r   _stdin_moder.   _prepare_commandr5   r
   )
rO   r   r   r   r   effective_cwdeffective_timeoutexec_command
exec_stdinr0   s
   &&$$$     r   rA   1BaseModalExecutionEnvironment._prepare_modal_exec   s     txx#33t||#'#3#3y#@Zd
!d&6&6)&C3LML#'#8#8#F !/IL  %!	
 	
r   c               $    V ^8  d   QhRRRRRR/# )r"   outputr   
returncoder   r#   r=   r   )r$   s   "r   r%   r>      s!     
 
c 
s 
t 
r   c                	    R VRV/# )rf   rg   r   )rO   rf   rg   s   &&&r   rI   %BaseModalExecutionEnvironment._result   s    f*
 	
r   c                    V ^8  d   QhRRRR/# )r"   rf   r   r#   r=   r   )r$   s   "r   r%   r>      s     ' 'C 'D 'r   c                	&    V P                  V^4      # )   rI   )rO   rf   s   &&r   rD   +BaseModalExecutionEnvironment._error_result   s    ||FA&&r   c                    V ^8  d   QhRRRR/# )r"   r   r   r#   r=   r   )r$   s   "r   r%   r>      s     H H H Hr   c                	.    V P                  R V R2^|4      # )zCommand timed out after srm   )rO   r   s   &&r   rL   7BaseModalExecutionEnvironment._timeout_result_for_modal   s    ||6wiqA3GGr   c                    V ^8  d   QhRRRR/# )r"   rP   r
   r#   r   r   )r$   s   "r   r%   r>      s     / /*; / /r   c                    R# )z Begin a transport-specific exec.Nr   )rO   rP   s   &&r   rB   /BaseModalExecutionEnvironment._start_modal_exec       r   c                    V ^8  d   QhRRRR/# )r"   r   r   r#   r   r   )r$   s   "r   r%   r>      s     G Gs G{ Gr   c                    R# )z8Return a final result dict when complete, else ``None``.Nr   rO   r   s   &&r   rK   .BaseModalExecutionEnvironment._poll_modal_exec   rv   r   c                    V ^8  d   QhRRRR/# )r"   r   r   r#   rX   r   )r$   s   "r   r%   r>      s     = = = =r   c                    R# )z.Cancel or terminate the active transport exec.Nr   ry   s   &&r   rH   0BaseModalExecutionEnvironment._cancel_modal_exec   rv   r   r   )r:   )r   r   r   r   r   r^   rN   r9   r   rJ   rE   rU   r@   rA   rI   rD   rL   r   rB   rK   rH   r   r   r   r   r7   r7   :   s    HK!26!<6/684
 #84 "&84 84t
 	

 #
 "&
6
'H / / G G = =r   r7   )r   
__future__r   r2   rF   r*   abcr   dataclassesr   typingr   tools.environments.baser   tools.interruptr   r
   r   r.   r5   r7   r   r   r   <module>r      s|    #     !  3 * $" " " $) ) )>L
x=O x=r   