+
    Ӄi!(                         R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
 ]P                  ! ]4      t ! R R4      tR# )zKPersistent shell mixin: file-based IPC protocol for long-lived bash shells.N)abstractmethod)is_interruptedc                     a  ] tR t^t o Rt]V 3R lR l4       t]V 3R lR l4       t]R 4       t]RRR	R/V 3R
 lR ll4       t	]R 4       t
RtRtRt]V 3R lR l4       tR tR tR&RRR	R/V 3R lR llltR&RRR	R/V 3R lR llltR tR tV 3R lR ltV 3R lR ltRRR	R/V 3R lR lltV 3R  lR! lt]V 3R" lR# l4       tV 3R$ ltR%tV tR# )'PersistentShellMixinzMixin that adds persistent shell capability to any BaseEnvironment.

Subclasses must implement ``_spawn_shell_process()``, ``_read_temp_files()``,
``_kill_shell_children()``, ``_execute_oneshot()``, and ``_cleanup_temp_files()``.
c                4   < V ^8  d   QhRS[ P                  /#    return)
subprocessPopen)format__classdict__s   "@/home/ubuntu/hermes-agent/tools/environments/persistent_shell.py__annotate__!PersistentShellMixin.__annotate__   s    ;;j&6&6;    c                    R # N selfs   &r   _spawn_shell_process)PersistentShellMixin._spawn_shell_process   s    8;r   c                6   < V ^8  d   QhRS[ RS[S[ ,          /# )r   pathsr	   )strlist)r   r   s   "r   r   r      s    ==s=tCy=r   c                    R # r   r   )r   r   s   &*r   _read_temp_files%PersistentShellMixin._read_temp_files   s    :=r   c                    R # r   r   r   s   &r   _kill_shell_children)PersistentShellMixin._kill_shell_children   s    $'r   timeoutN
stdin_datac          
      T   < V ^8  d   QhRS[ RS[ RS[R,          RS[ R,          RS[/# r   commandcwdr#   Nr$   r	   r   intdict)r   r   s   "r   r   r   #   sB     E E E# E"%*E%(4ZE;?Er   c                   R # r   r   r   r'   r(   r#   r$   s   &&&$$r   _execute_oneshot%PersistentShellMixin._execute_oneshot"   s	     BEr   c                    R # r   r   r   s   &r   _cleanup_temp_files(PersistentShellMixin._cleanup_temp_files'   s    #&r    g{Gz?g      ?c                    < V ^8  d   QhRS[ /# r   r   )r   r   s   "r   r   r   /   s     < <c <r   c                     R V P                    2# )z/tmp/hermes-persistent-)_session_idr   s   &r   _temp_prefix!PersistentShellMixin._temp_prefix.   s    ()9)9(:;;r   c                J   \         P                  ! 4       V n        R V n        RV n        R V n        \        P                  ! 4       P                  R,          V n	        V P                  pV R2V n        V R2V n        V R2V n        V R2V n        V R2V n        V P!                  4       V n        RV n        \         P"                  ! V P$                  RR	7      V n        V P&                  P)                  4        R
V P                   RV P                   RV P                   RV P                   RV P                   RV P                   RV P                   R2pV P+                  V4       \,        P.                  ! 4       R,           p\,        P.                  ! 4       V8  dp   V P1                  V P                  4      ^ ,          P3                  4       pVP5                  4       '       d   \7        V4      V n        M4\,        P8                  ! R4       K  \:        P=                  R4       R V n        V P
                  '       d,   \:        P?                  RV P                  V P
                  4       V P1                  V P                  4      ^ ,          P3                  4       pV'       d	   WPn         R # R # )NF:N   Nz-stdoutz-stderrz-statusz-cwdz-pidT)targetdaemonz export TERM=${TERM:-dumb}
touch  z
echo $$ > z
pwd > 
g      @g?z#Could not read persistent shell PIDz-Persistent shell started (session=%s, pid=%d))!	threadingLock_shell_lock_shell_proc_shell_alive
_shell_piduuiduuid4hexr7   r8   _pshell_stdout_pshell_stderr_pshell_status_pshell_cwd_pshell_pid_filer   Thread_drain_shell_output_drain_threadstart_send_to_shelltime	monotonicr   stripisdigitr*   sleeploggerwarninginfor(   )r   pinit_scriptdeadlinepid_strreported_cwds   &     r   _init_persistent_shell+PersistentShellMixin._init_persistent_shell6   s;   $>>+48"'&*::<++C0!"7m!"7m!"7mS:#$#T
446 &--++D
 	  "(()4+>+>*?q""#1T%5%5$6a8M8M7N O../ 0%%&b	* 	 	K(>>#c)nn)++D,A,AB1EKKMG  "%g,JJtNN@A"DO???KK?  $//
 ,,T-=-=>qAGGI#H r   c                n   V P                   f   R # V P                  '       d   V P                  4         V P                   P                  P	                  4         V P                   P                  4        V P                   P                  ^R7       RV n        R V n         \        V R4      '       dA   V P                  P                  4       '       d   V P                  P                  RR7       R # R # R #   \
         d     Li ; i  \        P                   d    T P                   P                  4         Li ; i)N)r#   FrP   g      ?)rC   r7   r1   stdinclose	Exception	terminatewaitr
   TimeoutExpiredkillrD   hasattrrP   is_alivejoinr   s   &r   _cleanup_persistent_shell.PersistentShellMixin._cleanup_persistent_shellj   s   #$$&	""((*	$&&(!!!!, "4))d.@.@.I.I.K.K##C#0 /L)  		
 (( 	$!!#	$s#   $C1 6D 1C?>C?/D43D4c          
      T   < V ^8  d   QhRS[ RS[ RS[R,          RS[ R,          RS[/# r&   r)   )r   r   s   "r   r   r      s;     	
 	
s 	
 	
t	
$J	
26	
r   c               r    V P                   '       d   V P                  WW4R 7      # V P                  WW4R 7      # )r#   r$   )
persistent_execute_persistentr.   r-   s   &&&$$r   executePersistentShellMixin.execute   sI     ???++g ,   $$' % 
 	
r   c          
      T   < V ^8  d   QhRS[ RS[ RS[R,          RS[ R,          RS[/# r&   r)   )r   r   s   "r   r   r      s;     
 
s 
 
!$t
$'$J
:>
r   c               (    V P                  WW4R7      # )u   Always use the oneshot (non-persistent) execution path.

This bypasses _shell_lock so it can run concurrently with a
long-running command in the persistent shell — used by
execute_code's file-based RPC polling thread.
rq   )r.   r-   s   &&&$$r   execute_oneshot$PersistentShellMixin.execute_oneshot   s!     $$' % 
 	
r   c                N    V P                   '       d   V P                  4        R # R # r   )rr   rm   r   s   &r   cleanupPersistentShellMixin.cleanup   s    ???**, r   c                r     V P                   P                   F  pK  	  R V n        R#   \         d     Li ; i)FN)rC   stdoutre   rD   )r   _s   & r   rO   (PersistentShellMixin._drain_shell_output   s>    	%%,, - "  		s   ( 66c                    < V ^8  d   QhRS[ /# )r   textr5   )r   r   s   "r   r   r      s     & &3 &r   c                   V P                   '       d   V P                  f   R #  V P                  P                  P                  V4       V P                  P                  P	                  4        R #   \
        \        3 d    RT n          R # i ; i)NF)rD   rC   rc   writeflushBrokenPipeErrorOSError)r   r   s   &&r   rR   #PersistentShellMixin._send_to_shell   so       D$4$4$<	&""((.""((*) 	& %D	&s   A	A/ /B
Bc                :   < V ^8  d   QhRS[ S[S[S[3,          /# r   )tupler   r*   )r   r   s   "r   r   r      s     . .sC})= .r   c                   V P                  V P                  V P                  V P                  V P                  4      w  rr4V P                  W4      pVP                  4       pR V9   d   VP                  R ^4      ^,          p \        VP                  4       4      pWWVP                  4       3#   \         d    ^p L!i ; i):)
r   rI   rJ   rK   rL   _merge_outputrU   splitr*   
ValueError)r   r~   stderr
status_rawr(   outputstatus	exit_codes   &       r   _read_persistent_output,PersistentShellMixin._read_persistent_output   s    *.*?*?!4!4!1!1+
'
 ##F3!!#&=\\#q)!,F	FLLN+I #))+--  	I	s   B- -B=<B=c          
      T   < V ^8  d   QhRS[ RS[ RS[R,          RS[ R,          RS[/# r&   r)   )r   r   s   "r   r   r      s;      3 S %(4Z(+d
>Br   c                  V P                   '       g&   \        P                  R 4       V P                  4        V P	                  V4      w  rVT;'       g    V P
                  pV'       g	   V'       d   V P                  WW4R7      # V P                  ;_uu_ 4        V P                  WRV4      uuRRR4       #   + '       g   i     R# ; i)z$Persistent shell died, restarting...rq   N)	rD   rX   rZ   r`   _prepare_commandr#   r.   rB   _execute_persistent_locked)r   r'   r(   r#   r$   exec_command
sudo_stdineffective_timeouts   &&&$$   r   rs   (PersistentShellMixin._execute_persistent   s        KK>?'')#'#8#8#A #33t||((g )   22#4 s   B55C	c                2   < V ^8  d   QhRS[ RS[ RS[RS[/# )r   r'   r(   r#   r	   r)   )r   r   s   "r   r   r      s+     =; =;# =;C =;,/=;48=;r   c                
   T;'       g    V P                   p\        P                  ! 4       P                  R ,          pRV P                   RV P
                   RV P                   R2pV P                  V4       VP                  RR4      pR\        P                  ! V4       RV RV P                   R	V P
                   R
V P                   RV RV P                   R2pV P                  V4       \        P                  ! 4       V,           p	V P                  p
 \        4       '       d2   V P!                  4        V P#                  4       w  p pRVR,           R^/# \        P                  ! 4       V	8  dO   V P!                  4        V P#                  4       w  p pV'       d   RVRV R2,           R^|/# V P%                  V4      # V P&                  '       g   RRR^/# V P)                  V P                  4      ^ ,          P+                  4       pVP-                  VR,           4      '       d   M6\        P.                  ! V
4       \1        V
R,          V P2                  4      p
EKG  V P#                  4       w  rpV'       d   Wn         RVRV/# ):N   Nz: > z
: > r?   'z'\''zcd z
eval 'z' < /dev/null > z 2> z
__EC=$?
pwd > z
echo z	:$__EC > r   z
[Command interrupted]
returncodez
[Command timed out after zs]z&Persistent shell died during executionr   g      ?)r(   rF   rG   rH   rI   rJ   rK   rR   replaceshlexquoterL   rS   rT   _poll_interval_startr   r!   r   _timeout_resultrD   r   rU   
startswithrW   min_poll_interval_max)r   r'   r(   r#   work_dircmd_idtruncateescaped
ipc_scriptr]   poll_intervalr   r   status_contentr   new_cwds   &&&&            r   r   /PersistentShellMixin._execute_persistent_locked   s{   ??$((!!"%4&&' (&&' (&&'r+ 	
 	H%//#w/ %++h'( )I-d.A.A-B$tGZGZF[ \%%& '89T%8%8$9	= 	 	J'>>#g-11))+#;;=1f'@@ # 
 ~~(*))+#;;=1 &-H	QS+T"T$c  ++G44$$$F ! 
 "2243F3FGJPPRN((#66JJ}%   3T5L5LMM%)%A%A%C"7H&,	::r   c                ,   < V ^8  d   QhRS[ RS[ RS[ /# )r   r~   r   r	   r5   )r   r   s   "r   r   r     s"        c  3  3  r   c                    . pV P                  4       '       d!   VP                  V P                  R 4      4       VP                  4       '       d!   VP                  VP                  R 4      4       R P                  V4      # )r?   )rU   appendrstriprl   )r~   r   partss   && r   r   "PersistentShellMixin._merge_output  sV    <<>>LLt,-<<>>LLt,-yyr   c                J   < V ^8  d   Qh/ S[ ;R&   S[;R&   S[;R&   S[;R&   # )r   rr   r7   r   r   )boolr   float)r   r   s   "r   r   r      s9       4 5 6  &7 8 $9 r   )rP   rL   rM   rK   rJ   rI   r7   rD   rB   rE   rC   r(   )r3   )__name__
__module____qualname____firstlineno____doc__r   r   r   r!   r.   r1   r7   r   r   propertyr8   r`   rm   rt   rx   r{   rO   rR   r   rs   r   staticmethodr   __annotate_func____static_attributes____classdictcell__)r   s   @r   r   r      s3     ; ;= =' 'E/3E26E E E & &K"& $< <2$h16	
&*	
)-	
 	

.2
15
 
-"& &. .&2659 &=; =;~    Y  r   r   )r   loggingr   r
   r@   rS   rF   abcr   tools.interruptr   	getLoggerr   rX   r   r   r   r   <module>r      s<    Q        *			8	$R  R r   