
    KiG                        d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
 ej                    dk    ZddlmZmZ ddlmZmZ ddlmZmZmZmZ ddlmZ  ej        e          Z e            dz  Zd	Zd
ZdZ e G d d                      Z! G d d          Z" e"            Z#ddl$m%Z% ddddg dddddddddddddddddd ddd!d"gd#d$Z&d% Z' e%j(        dd&e&e'd'(           dS ))a  
Process Registry -- In-memory registry for managed background processes.

Tracks processes spawned via terminal(background=true), providing:
  - Output buffering (rolling 200KB window)
  - Status polling and log retrieval
  - Blocking wait with interrupt support
  - Process killing
  - Crash recovery via JSON checkpoint file
  - Session-scoped tracking for gateway reset protection

Background processes execute THROUGH the environment interface -- nothing
runs on the host machine unless TERMINAL_ENV=local. For Docker, Singularity,
Modal, Daytona, and SSH backends, the command runs inside the sandbox.

Usage:
    from tools.process_registry import process_registry

    # Spawn a background process (called from terminal_tool)
    session = process_registry.spawn(env, "pytest -v", task_id="task_123")

    # Poll for status
    result = process_registry.poll(session.id)

    # Block until done
    result = process_registry.wait(session.id, timeout=300)

    # Kill it
    process_registry.kill(session.id)
    NWindows)_find_shell_sanitize_subprocess_env)	dataclassfield)AnyDictListOptional)get_hermes_homezprocesses.jsoni@ i  @   c                      e Zd ZU dZeed<   eed<   dZeed<   dZeed<   dZe	e
         ed<   dZe	ej                 ed	<   dZeed
<   dZe	e         ed<   dZeed<   dZeed<   dZe	e
         ed<   dZeed<   eZe
ed<   dZeed<   dZeed<   dZeed<   dZeed<   dZe
ed<    eej                   Z!ej         ed<    edd          Z"e	ej#                 ed<    edd          Z$eed<   dS )ProcessSessionz3A tracked background process with output buffering.idcommand task_idsession_keyNpidprocessenv_refcwdg        
started_atFexited	exit_codeoutput_buffermax_output_charsdetachedwatcher_platformwatcher_chat_idwatcher_thread_idr   watcher_interval)default_factory_lock)defaultrepr_reader_thread_pty)%__name__
__module____qualname____doc__str__annotations__r   r   r   r   intr   
subprocessPopenr   r   r   r   floatr   boolr   r   MAX_OUTPUT_CHARSr   r   r   r    r!   r"   r   	threadingLockr$   r'   Threadr(        3/home/ubuntu/hermes-agent/tools/process_registry.pyr   r   >   s        ==GGGLLLGSKC#*.GXj&'...GSC#JFD#Ix}###M3,c,,,HdcOSsc!E).AAAE9>AAA16t%1P1P1PNHY-.PPPd///D#/////r9   r   c                      e Zd ZdZdZd Zededefd            Z	 	 	 	 	 d1d
edededede	de
defdZ	 	 	 	 d2ded
edededededefdZdefdZdedededefdZdefdZdefdZdedee         fdZdede	fdZd3ded!ed"ede	fd#Zd4dedede	fd$Zdede	fd%Zded&ede	fd'Zd5ded&ede	fd(Zd4dedefd)Zdede
fd*Zdede
fd+Zd4dedefd,Z d- Z!d. Z"d/ Z#defd0Z$dS )6ProcessRegistrya$  
    In-memory registry of running and finished background processes.

    Thread-safe. Accessed from:
      - Executor threads (terminal_tool, process tool handlers)
      - Gateway asyncio loop (watcher tasks, session reset checks)
      - Cleanup thread (sandbox reaping coordination)
    )z'bash: cannot set terminal process groupz"bash: no job control in this shellzno job control in this shellz!cannot set terminal process groupz)tcsetattr: Inappropriate ioctl for devicec                 `    i | _         i | _        t          j                    | _        g | _        d S N)_running	_finishedr5   r6   r$   pending_watchersselfs    r:   __init__zProcessRegistry.__init__k   s/    3546^%%
 79r9   textreturnc                    |                      d          rat          fdt          j        D                       r<                    d           r%t          fdt          j        D                       <d                              S )z:Strip shell startup warnings from the beginning of output.
c              3   ,   K   | ]}|d          v V  dS )r   Nr8   ).0noiseliness     r:   	<genexpr>z5ProcessRegistry._clean_shell_noise.<locals>.<genexpr>w   s,      cc%EU1X-ccccccr9   r   )splitanyr<   _SHELL_NOISE_SUBSTRINGSpopjoin)rE   rL   s    @r:   _clean_shell_noisez"ProcessRegistry._clean_shell_noises   s     

4   	cccc?;bccccc 	IIaLLL  	cccc?;bccccc 	yyr9   Nr   Fr   r   r   r   env_varsuse_ptyc                    t          dt          j                    j        dd          ||||pt	          j                    t          j                              }|rf	 t          rddlm	} nddl
m	} t                      }	t          t          j        |          }
d|
d<   |                    |	d	|g|j        |
d
          }|j        |_        ||_        t%          j        | j        |fdd|j                   }||_        |                                 | j        5  |                                  || j        |j        <   ddd           n# 1 swxY w Y   |                                  |S # t8          $ r t:                              d           Y n1t>          $ r%}t:                              d|           Y d}~nd}~ww xY wt                      }	t          t          j        |          }d|d<   tA          j!        |	d	|gd|j        |ddt@          j"        t@          j#        t@          j"        t          rdnt          j$        
  
        }||_%        |j        |_        t%          j        | j&        |fdd|j                   }||_        |                                 | j        5  |                                  || j        |j        <   ddd           n# 1 swxY w Y   |                                  |S )aw  
        Spawn a background process locally.

        Only for TERMINAL_ENV=local. Other backends use spawn_via_env().

        Args:
            use_pty: If True, use a pseudo-terminal via ptyprocess for interactive
                     CLI tools (Codex, Claude Code, Python REPL). Falls back to
                     subprocess.Popen if ptyprocess is not installed.
        proc_N   )r   r   r   r   r   r   r   )
PtyProcess1PYTHONUNBUFFEREDz-lic)   x   )r   env
dimensionsTzproc-pty-reader-targetargsdaemonnamez3ptyprocess not installed, falling back to pipe modez0PTY spawn failed (%s), falling back to pipe modeutf-8replace)	rE   r   r^   encodingerrorsstdoutstderrstdin
preexec_fnzproc-reader-)'r   uuiduuid4hexosgetcwdtime_IS_WINDOWSwinptyrY   
ptyprocessr   r   environspawnr   r   r(   r5   r7   _pty_reader_loopr   r'   startr$   _prune_if_neededr?   _write_checkpointImportErrorloggerwarning	Exceptionr0   r1   PIPESTDOUTsetsidr   _reader_loop)rC   r   r   r   r   rT   rU   session_PtyProcessCls
user_shellpty_envpty_procreaderebg_envprocs                   r:   spawn_localzProcessRegistry.spawn_local}   s   & !.tz||',..#"ry{{y{{
 
 
  (	V&V HCCCCCCCGGGGGG(]]
22:xHH.1*+)//1(	 0   'l' #)0!8GJ88	   *0&Z 8 8))+++07DM'*-8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 &&((( V V VTUUUUU V V VQSTUUUUUUUUV !]]
 *"*h??%(!")?$/*9tt	
 
 
 h !$,
,,	
 
 
 "(Z 	0 	0!!###(/DM'*%	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	   sU   B=E- $E E- EE- EE- -$G	GF<<G#$KKK
   r^   timeoutc           	         t          dt          j                    j        dd          ||||t	          j                    |          }d|j         d}d|j         d}	t          j        |          }
d|
 d	| d
|	 d|	 }	 |                    ||          }|	                    dd          
                                }|                                D ]@}|
                                }|                                rt          |          |_         nAn/# t          $ r"}d|_        d|_        d| |_        Y d}~nd}~ww xY w|j        sDt'          j        | j        ||||	fdd|j                   }||_        |                                 | j        5  |                                  || j        |j        <   ddd           n# 1 swxY w Y   |                                  |S )a#  
        Spawn a background process through a non-local environment backend.

        For Docker/Singularity/Modal/Daytona/SSH: runs the command inside the sandbox
        using the environment's execute() interface. We wrap the command to
        capture the in-sandbox PID and redirect output to a log file inside
        the sandbox, then poll the log via subsequent execute() calls.

        This is less capable than local spawn (no live stdout pipe, no stdin),
        but it ensures the command runs in the correct sandbox context.
        rW   NrX   )r   r   r   r   r   r   r   z/tmp/hermes_bg_z.logz.pidznohup bash -c z > z 2>&1 & echo $! > z && cat r   outputr   TzFailed to start: zproc-poller-r`   )r   rm   rn   ro   rr   r   shlexquoteexecutegetstrip
splitlinesisdigitr/   r   r   r   r   r   r5   r7   _env_poller_loopr'   ry   r$   rz   r?   r{   )rC   r^   r   r   r   r   r   r   log_pathpid_pathquoted_command
bg_commandresultr   liner   r   s                    r:   spawn_via_envzProcessRegistry.spawn_via_env   s   ( !.tz||',..#y{{
 
 
 6WZ5555WZ555W--6^ 6 6 6 6!6 6+36 6 	
	<[[W[==FZZ"--3355F))++  zz||<<>> "%d))GKE  	< 	< 	<!GN "G$;$;$;G!!!!!!	<
 ~ 		%,sHh70GJ00	  F &,G"LLNNNZ 	0 	0!!###(/DM'*%	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	   s+   BD 
E!D>>E$GGGr   c                    d}	 	 |j         j                            d          }|sn|r|                     |          }d}|j        5  |xj        |z  c_        t          |j                  |j        k    r|j        |j         d         |_        ddd           n# 1 swxY w Y   n2# t          $ r%}t          
                    d|           Y d}~nd}~ww xY w	 |j                             d           n2# t          $ r%}t          
                    d|           Y d}~nd}~ww xY wd|_        |j         j        |_        |                     |           dS )	z:Background thread: read stdout from a local Popen process.T   FNzProcess stdout reader ended: %s   r   z$Process wait timed out or failed: %s)r   ri   readrS   r$   r   lenr   r   r}   debugwaitr   
returncoder   _move_to_finished)rC   r   first_chunkchunkr   s        r:   r   zProcessRegistry._reader_loop6  s   	?
b.33D99  ( 33E::E"'K] b b))U2))7011G4LLL070EwG_F_F`F`0a-b b b b b b b b b b b b b b b
b   	? 	? 	?LL:A>>>>>>>>	?	DO   ++++ 	D 	D 	DLL?CCCCCCCC	D#O6w'''''sU   AB( ABB( BB( "B#B( (
C2CCC7 7
D&D!!D&r   r   c                 F   |j         st          j        d           	 |                    d| dd          }|                    dd          }|r]|j        5  ||_        t          |j                  |j        k    r|j        |j         d         |_        ddd           n# 1 swxY w Y   |                    d	| d
d          }|                    dd          	                                }|r|
                                d         	                                dk    r|                    d| d
d          }	|	                    dd          	                                }
	 t          |

                                d         	                                          |_        n# t          t          f$ r
 d|_        Y nw xY wd|_         |                     |           dS n4# t           $ r' d|_         d|_        |                     |           Y dS w xY w|j         dS dS )zBBackground thread: poll a sandbox log file for non-local backends.   zcat  2>/dev/nullr   r   r   r   Nzkill -0 $(cat z" 2>/dev/null) 2>/dev/null; echo $?r   r   0zwait $(cat T)r   rr   sleepr   r   r$   r   r   r   r   r   r/   r   
ValueError
IndexErrorr   r   )rC   r   r^   r   r   r   
new_outputcheckcheck_outputexit_resultexit_strs              r:   r   z ProcessRegistry._env_poller_loopQ  s    . &	JqMMM$%BH%B%B%BBOO#ZZ"55
 f  f f0:-w4558PPP4;4I7KcJcJdJd4eG1f f f f f f f f f f f f f f f QXQQQ $    %yy266<<>> L$;$;$=$=b$A$G$G$I$IS$P$P"%++RhRRR ! #. # #K  +x<<BBDDH/,/0C0C0E0Eb0I0O0O0Q0Q,R,R))&
3 / / /,.)))/%)GN**7333F   !%$&!&&w///C . &	 &	 &	 &	 &	sZ   :G# ?B#G# #B''G# *B'+B;G# '>F& %G# &G>G#  GG# #-HHc                 `   |j         }	 |                                r	 |                    d          }|rt          |t                    r|n|                    dd          }|j        5  |xj        |z  c_        t          |j                  |j	        k    r|j        |j	         d         |_        ddd           n# 1 swxY w Y   n# t          $ r Y n#t          $ r Y nw xY w|                                n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w	 |                                 n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wd|_        t!          |d	          r|j        nd
|_        |                     |           dS )z2Background thread: read output from a PTY process.r   re   rf   )rh   NzPTY stdout reader ended: %sz PTY wait timed out or failed: %sT
exitstatusr   )r(   isaliver   
isinstancer-   decoder$   r   r   r   EOFErrorr   r}   r   r   r   hasattrr   r   r   )rC   r   ptyr   rE   r   s         r:   rx   z ProcessRegistry._pty_reader_loop}  sH   l	;++-- HHTNNE j(25#(>(>kuuELLQXajLDkDk$] j j#11T911"7#899G<TTT8?8MwOgNgNhNh8i 5j j j j j j j j j j j j j j j     E    E ++--   	; 	; 	;LL6::::::::	;	@HHJJJJ 	@ 	@ 	@LL;Q????????	@.5c<.H.HPCNNbw'''''s   C: AC
 *AB>2C
 >CC
 CC
 	C: 

C"C: 	C"C: !C""C: :
D)D$$D)-E 
E1E,,E1c                     | j         5  | j                            |j        d           || j        |j        <   ddd           n# 1 swxY w Y   |                                  dS )z(Move a session from running to finished.N)r$   r?   rQ   r   r@   r{   )rC   r   s     r:   r   z!ProcessRegistry._move_to_finished  s    Z 	1 	1Mgj$///)0DN7:&	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	     s   0AAA
session_idc                     | j         5  | j                            |          p| j                            |          cddd           S # 1 swxY w Y   dS )z*Get a session by ID (running or finished).N)r$   r?   r   r@   )rC   r   s     r:   r   zProcessRegistry.get  s    Z 	S 	S=$$Z00RDN4F4Fz4R4R	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	Ss   4A		AAc                    ddl m} |                     |          }|dd| dS |j        5  |j        r ||j        dd                   nd}ddd           n# 1 swxY w Y   |j        |j        |j        rd	nd
|j        t          t          j
                    |j        z
            |d}|j        r
|j        |d<   |j        r
d|d<   d|d<   |S )z9Check status and get new output for a background process.r   
strip_ansiN	not_foundNo process with ID statuserrorr   r   running)r   r   r   r   uptime_secondsoutput_previewr   Tr   z=Process recovered after restart -- output history unavailablenote)tools.ansi_stripr   r   r$   r   r   r   r   r   r/   rr   r   r   r   )rC   r   r   r   r   r   s         r:   pollzProcessRegistry.poll  s\   //////((:&&?)4V*4V4VWWW] 	h 	hJQJ_gZZ(=eff(EFFFegN	h 	h 	h 	h 	h 	h 	h 	h 	h 	h 	h 	h 	h 	h 	h "*").?hhi;!$)++0B"BCC,
 
 > 	4")"3F; 	]!%F:\F6Ns   "AA"Ar      offsetlimitc                    ddl m} |                     |          }|dd| dS |j        5   ||j                  }ddd           n# 1 swxY w Y   |                                }t          |          }|dk    r|dk    r|| d         }	n||||z            }	|j        |j        rdndd		                    |	          |t          |	           d
dS )z;Read the full output log with optional pagination by lines.r   r   Nr   r   r   r   r   rH   z lines)r   r   r   total_linesshowing)
r   r   r   r$   r   r   r   r   r   rR   )
rC   r   r   r   r   r   full_outputrL   r   selecteds
             r:   read_logzProcessRegistry.read_log  sM   //////((:&&?)4V*4V4VWWW] 	< 	<$*W%:;;K	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< &&((%jj Q;;5199eVWW~HHVFUN23H "*").?hhiii))&h--///
 
 	
s   A

AAc                    ddl m} ddlm} t	          t          j        dd                    }|}|}d}|r||k    r|}	d| d| d	}n|p|}	|                     |          }
|
d
d| dS t          j	                    |	z   }t          j	                    |k     r|
j
        r*d|
j         ||
j        dd                   d}|r||d<   |S |                                r%d ||
j        dd                   dd}|r||d<   |S t          j        d           t          j	                    |k     d ||
j        dd                   d}|r||d<   n	d|	 d|d<   |S )aY  
        Block until a process exits, timeout, or interrupt.

        Args:
            session_id: The process to wait for.
            timeout: Max seconds to block. Falls back to TERMINAL_TIMEOUT config.

        Returns:
            dict with status ("exited", "timeout", "interrupted", "not_found")
            and output snapshot.
        r   r   )_interrupt_eventTERMINAL_TIMEOUT180NzRequested wait of z%s was clamped to configured limit of sr   r   r   r   i0)r   r   r   timeout_noteinterruptedr   z+User sent a new message -- wait interrupted)r   r   r      r   )r   r   zWaited zs, process still running)r   r   tools.terminal_toolr   r/   rp   getenvr   rr   	monotonicr   r   r   is_setr   )rC   r   r   r   r   default_timeoutmax_timeoutrequested_timeoutr   effective_timeoutr   deadliner   s                r:   r   zProcessRegistry.wait  s    	0/////888888bi(:EBBCC%# 	A!2[!@!@ +9%6 9 9*59 9 9 L
 !2 @[((:&&?)4V*4V4VWWW>##&77n))~ &!(!2(j)>uvv)FGG 
   :-9F>*&&(( +(j)>uvv)FGGI 
   :-9F>*JqMMM+ n))0   j!6uvv!>??
 
  	[%1F>""%Z/@%Z%Z%ZF>"r9   c                    |                      |          }|dd| dS |j        r
d|j        dS 	 |j        rY	 |j                            d           n# t
          $ r. |j        r$t          j        |j        t          j
                   Y nw xY w|j        r	 t          r|j                                         n;t          j        t          j        |j        j                  t          j
                   nc# t          t           f$ r |j                                         Y n7w xY w|j        r,|j        r%|j                            d	|j         d
d           d|_        d|_        |                     |           |                                  d|j        dS # t
          $ r}dt-          |          dcY d}~S d}~ww xY w)zKill a background process.Nr   r   r   already_exited)r   r   T)forcezkill r   r   r   ikilled)r   r   r   )r   r   r   r(   	terminater   r   rp   killsignalSIGTERMr   rs   killpggetpgidProcessLookupErrorPermissionErrorr   r   r   r{   r   r-   )rC   r   r   r   s       r:   kill_processzProcessRegistry.kill_process#  s   ((:&&?)4V*4V4VWWW> 	*$.  	8| V=L***6666  = = ={ =V^<<<=  V+" S113333	"*W_-@"A"A6>RRR*O< + + +O((*****+ VW[ V''(I(I(I(IST'UUU!GN #G""7+++""$$$&gjAAA 	8 	8 	8%A77777777	8sf   F A F 5BF B
F AC7 6F 7*D$!F #D$$A6F 
G%F<6G<Gdatac                    |                      |          }|dd| dS |j        rdddS t          |d          r|j        r	 t	          |t
                    r|                    d          n|}|j                            |           d	t          |          d
S # t          $ r}dt          |          dcY d}~S d}~ww xY w|j
        r|j
        j        sdddS 	 |j
        j                            |           |j
        j                                         d	t          |          d
S # t          $ r}dt          |          dcY d}~S d}~ww xY w)zASend raw data to a running process's stdin (no newline appended).Nr   r   r   r   zProcess has already finishedr(   re   ok)r   bytes_writtenr   z?Process stdin not available (non-local backend or stdin closed))r   r   r   r(   r   r-   encodewriter   r   r   rk   flush)rC   r   r   r   pty_datar   s         r:   write_stdinzProcessRegistry.write_stdinL  s   ((:&&?)4V*4V4VWWW> 	Y.9WXXX 7F## 	< 	<<3=dC3H3HR4;;w///d""8,,,"&TCCC < < <")CFF;;;;;;;;<  	sgo&; 	s%0qrrr	8O!''---O!'')))"SYY??? 	8 	8 	8%A77777777	8s>   AB 
C&B=7C=CAD- -
E7EEEc                 4    |                      ||dz             S )zGSend data + newline to a running process's stdin (like pressing Enter).rH   )r  )rC   r   r   s      r:   submit_stdinzProcessRegistry.submit_stding  s    
D4K888r9   c                    | j         5  t          | j                                                  t          | j                                                  z   }ddd           n# 1 swxY w Y   rfd|D             }g }|D ]}|j        |j        dd         |j        |j        t          j
        dt          j        |j                            t          t          j	                    |j        z
            |j        rdnd|j        r|j        dd         ndd	}|j        r
|j        |d
<   |j        rd|d<   |                    |           |S )z1List all running and recently-finished processes.Nc                 *    g | ]}|j         k    |S r8   r   rJ   r   r   s     r:   
<listcomp>z1ProcessRegistry.list_sessions.<locals>.<listcomp>q  s%    LLL!qyG7K7KA7K7K7Kr9   r   z%Y-%m-%dT%H:%M:%Sr   r   i8r   )r   r   r   r   r   r   r   r   r   Tr   )r$   listr?   valuesr@   r   r   r   r   rr   strftime	localtimer   r/   r   r   r   r   append)rC   r   all_sessionsr   r   entrys    `    r:   list_sessionszProcessRegistry.list_sessionsk  s   Z 	X 	X 4 4 6 677$t~?T?T?V?V:W:WWL	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X  	MLLLL|LLLL 	! 	!Ad9TcT?uu"m,?PQP\A]A]^^"%dikkAL&@"A"A&'h=((I<=O"S!/$%%"8"8QS	 	E x 1%&[k"z )$(j!MM%    s   AA##A'*A'c                     | j         5  t          fd| j                                        D                       cddd           S # 1 swxY w Y   dS )z<Check if there are active (running) processes for a task_id.c              3   >   K   | ]}|j         k    o|j         V  d S r>   r   r   r	  s     r:   rM   z7ProcessRegistry.has_active_processes.<locals>.<genexpr>  sG         	W$5QX     r9   Nr$   rO   r?   r  )rC   r   s    `r:   has_active_processesz$ProcessRegistry.has_active_processes      Z 	 	    --//    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   2AAAc                     | j         5  t          fd| j                                        D                       cddd           S # 1 swxY w Y   dS )z>Check if there are active processes for a gateway session key.c              3   >   K   | ]}|j         k    o|j         V  d S r>   )r   r   )rJ   r   r   s     r:   rM   z9ProcessRegistry.has_active_for_session.<locals>.<genexpr>  sG         ,=QX     r9   Nr  )rC   r   s    `r:   has_active_for_sessionz&ProcessRegistry.has_active_for_session  r  r  c                 
   | j         5  fd| j                                        D             }ddd           n# 1 swxY w Y   d}|D ]8}|                     |j                  }|                    d          dv r|dz  }9|S )zQKill all running processes, optionally filtered by task_id. Returns count killed.c                 <    g | ]}|j         k    |j        |S r>   r  r	  s     r:   r
  z,ProcessRegistry.kill_all.<locals>.<listcomp>  s;       OqyG';';QX'; ';';';r9   Nr   r   )r   r   r   )r$   r?   r  r   r   r   )rC   r   targetsr   r   r   s    `    r:   kill_allzProcessRegistry.kill_all  s    Z 	 	   =//11  G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  	 	G&&wz22Fzz(##'CCC!s   &;??c                 T    t          j                     fd j                                        D             }|D ]
} j        |= t           j                  t           j                  z   }|t
          k    r* j        r%t           j         fd          } j        |= dS dS dS )zGRemove oldest finished sessions if over MAX_PROCESSES. Must hold _lock.c                 @    g | ]\  }}|j         z
  t          k    |S r8   )r   FINISHED_TTL_SECONDS)rJ   sidr   nows      r:   r
  z4ProcessRegistry._prune_if_needed.<locals>.<listcomp>  s:     
 
 
Cal"&::: :::r9   c                 (    j         |          j        S r>   )r@   r   )r$  rC   s    r:   <lambda>z2ProcessRegistry._prune_if_needed.<locals>.<lambda>  s    DN3<O<Z r9   )keyN)rr   r@   itemsr   r?   MAX_PROCESSESmin)rC   expiredr$  total	oldest_idr%  s   `    @r:   rz   z ProcessRegistry._prune_if_needed  s     ikk
 
 
 
"n2244
 
 
  	$ 	$Cs## DM""S%8%88M!!dn!DN0Z0Z0Z0Z[[[Iy))) "!!!r9   c                 n    | j         5  |                                  ddd           dS # 1 swxY w Y   dS )z1Public method to prune expired finished sessions.N)r$   rz   rB   s    r:   cleanup_expiredzProcessRegistry.cleanup_expired  s    Z 	$ 	$!!###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   *..c                    	 | j         5  g }| j                                        D ]a}|j        sX|                    |j        |j        |j        |j        |j	        |j
        |j        |j        |j        |j        |j        d           b	 ddd           n# 1 swxY w Y   ddlm}  |t$          |           dS # t&          $ r(}t(                              d|d           Y d}~dS d}~ww xY w)z=Write running process metadata to checkpoint file atomically.)r   r   r   r   r   r   r   r   r    r!   r"   Nr   atomic_json_writez#Failed to write checkpoint file: %sTexc_info)r$   r?   r  r   r  r   r   r   r   r   r   r   r   r    r!   r"   utilsr3  CHECKPOINT_PATHr   r}   r   )rC   entriesr   r3  r   s        r:   r{   z!ProcessRegistry._write_checkpoint  sm   	R  --//  A8 *+$'(y#$5#$5*+,'(y+,=010B/0/@121D010B( (                 & 0/////ow77777 	R 	R 	RLL>DLQQQQQQQQQ	Rs;   B8 A>BB8 BB8 BB8 8
C*C%%C*c                    t                                           sdS 	 t          j        t                               d                    }n# t
          $ r Y dS w xY wd}|D ]}|                    d          }|sd}	 t          j        |d           d}n# t          t          f$ r Y nw xY w|rt          |d         |                    dd	          |                    d
d          |                    dd          ||                    d          |                    dt          j                              d|                    dd          |                    dd          |                    dd          |                    dd                    }| j        5  || j        |j        <   ddd           n# 1 swxY w Y   |dz  }t                               d|j        dd         |           |j        dk    r?| j                            |j        |j        |j        |j        |j        |j        d           	 ddlm}  |t           g            n4# t
          $ r'}t                               d|d           Y d}~nd}~ww xY w|S )z
        On gateway startup, probe PIDs from checkpoint file.

        Returns the number of processes recovered as detached.
        r   re   )rg   r   FTr   r   unknownr   r   r   r   r   r   r    r!   r"   )r   r   r   r   r   r   r   r   r   r    r!   r"   Nr   z'Recovered detached process: %s (pid=%d)<   )r   check_intervalr   platformchat_id	thread_idr2  z#Could not clear checkpoint file: %sr4  )r7  existsjsonloads	read_textr   r   rp   r   r   r   r   rr   r$   r?   r   r}   infor   r"   rA   r  r   r   r    r!   r6  r3  r   )	rC   r8  	recoveredr  r   aliver   r3  r   s	            r:   recover_from_checkpointz'ProcessRegistry.recover_from_checkpoint  s"    %%'' 	1	j!:!:G!:!L!LMMGG 	 	 	11	 	 *	 *	E))E""C  EQ&8     (\*!IIi;;!IIi44 %		- < <		%(($yyty{{CC!%*YY/A2%F%F$)II.?$D$D&+ii0CR&H&H%*YY/A1%E%E   Z 8 807DM'*-8 8 8 8 8 8 8 8 8 8 8 8 8 8 8Q	EwWZXZWZG[]`aaa +a//)00&-j*1*B'.':$+$<#*#:%,%>2 2   	R//////or2222 	R 	R 	RLL>DLQQQQQQQQ	R sM   -A 
AA>BB*)B*F88F<	?F<	>I 
J JJ)Nr   r   NF)Nr   r   r   )r   r   r>   )r   )%r)   r*   r+   r,   rP   rD   staticmethodr-   rS   dictr3   r   r   r   r/   r   r   r   rx   r   r   r   r   r   r   r   r  r  r  r  r  r  r   rz   r0  r{   rG  r8   r9   r:   r<   r<   Y   s        9 9 9           \  n nn n 	n
 n n n 
n n n nh E EE E 	E
 E E E 
E E E ER(N ( ( ( (6*%*,/*;>*JM* * * *X( ( ( ( (<! ! ! ! !Sc Sh~&> S S S S
s t    4
 
3 
 
 
d 
 
 
 
8B Bs BS BD B B B BH'8s '8t '8 '8 '8 '8R8c 8 8 8 8 8 869 9s 9# 9t 9 9 9 9 S D    :C D    # $      s    "* * *"$ $ $R R R6B B B B B B Br9   r<   )registryr   aF  Manage background processes started with terminal(background=true). Actions: 'list' (show all), 'poll' (check status + new output), 'log' (full output with pagination), 'wait' (block until done or timeout), 'kill' (terminate), 'write' (send raw stdin data without newline), 'submit' (send data + Enter, for answering prompts).objectstring)r  r   logr   r   r   submitz)Action to perform on background processes)typeenumdescriptionz]Process session ID (from terminal background output). Required for all actions except 'list'.)rO  rQ  z@Text to send to process stdin (for 'write' and 'submit' actions)integerzJMax seconds to block for 'wait' action. Returns partial output on timeout.r   )rO  rQ  minimumz6Line offset for 'log' action (default: last 200 lines)z$Max lines to return for 'log' action)actionr   r   r   r   r   rT  )rO  
propertiesrequired)rd   rQ  
parametersc                 H   dd l }|                    d          }|                     dd          }|                     d          #t          |                     dd                    nd}|dk    r2|                    dt                              |          id	
          S |dv r|s|                    dd| id	
          S |dk    r/|                    t                              |          d	
          S |dk    rZ|                    t                              ||                     dd          |                     dd                    d	
          S |dk    rD|                    t                              ||                     d                    d	
          S |dk    r/|                    t          	                    |          d	
          S |dk    rQ|                    t          
                    |t          |                     dd                              d	
          S |dk    rQ|                    t                              |t          |                     dd                              d	
          S |                    dd| did	
          S )Nr   r   rT  r   r   r  	processesr  F)ensure_ascii)r   rM  r   r   r   rN  r   zsession_id is required for r   rM  r   r   r   )r   r   r   r   r   r   r   r   rN  zUnknown process action: z1. Use: list, poll, log, wait, kill, write, submit)rA  r   r-   dumpsprocess_registryr  r   r   r   r   r  r  )rb   kw_jsonr   rT  r   s         r:   _handle_processr_  W  s   ffYGXXh##F48HH\4J4J4VTXXlB//000\^J{{K)9)G)GPW)G)X)XYhm{nnn	E	E	E 	f;;)Ov)O)OP_d;eeeV;;/44Z@@u;UUUu__;;/88488Ha#8#8RU@V@V  9  X  Xfk  m m mv;;/44ZR[I\I\4]]lq;rrrv;;/<<ZHHW\;]]]w;;/;;JDHHU[]_L`L`HaHabbqv;wwwx;;/<<ZTXXV\^`MaMaIbIbccrw;xxx;;!uF!u!u!uv  FK;  L  L  Lr9   terminalu   ⚙️)rd   toolsetschemahandleremoji))r,   rA  loggingrp   r=  r   r   r0   r5   rr   rm   systemrs   tools.environments.localr   r   dataclassesr   r   typingr   r	   r
   r   hermes_cli.configr   	getLoggerr)   r}   r7  r4   r#  r*  r   r<   r\  tools.registryrJ  PROCESS_SCHEMAr_  registerr8   r9   r:   <module>ro     s   >   				             ho9, J J J J J J J J ( ( ( ( ( ( ( ( , , , , , , , , , , , , - - - - - -		8	$	$ "/##&66    0 0 0 0 0 0 0 04E E E E E E E ER #?$$  $ # # # # # 	?  !RRRJ  !~ 
 !a 
 "k  "W 
 "E /
 
: J?   * *ZL L L8  	
     r9   