
    iE                         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 dZ!dZ"dZ#e G d d                      Z$ G d d          Z% e%            Z&ddl'm(Z(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<   dZeed<   dZeed<   dZ e
ed<   dZ!eed<    e"e#          Z$e%e         ed<    e"dd           Z&e
ed!<    e"dd           Z'e
ed"<    e"dd           Z(eed#<    e"dd           Z)eed$<    e"dd           Z*e
ed%<    e"dd           Z+eed&<    e"e,j-                  Z.e,j-        ed'<    e"dd           Z/e	e,j0                 ed(<    e"dd           Z1eed)<   dS )*ProcessSessionz3A tracked background process with output buffering.idcommand task_idsession_keyNpidprocessenv_refcwd        
started_atFexited	exit_codeoutput_buffermax_output_charsdetachedhost	pid_scopewatcher_platformwatcher_chat_idwatcher_user_idwatcher_user_namewatcher_thread_idr   watcher_intervalnotify_on_complete)default_factorywatch_patterns)defaultrepr_watch_hits_watch_suppressed_watch_overload_since_watch_disabled_watch_window_hits_watch_window_start_lock_reader_thread_pty)2__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(   r)   r*   r+   r   listr-   r
   r0   r1   r2   r3   r4   r5   	threadingLockr6   r7   Threadr8        >/home/agentuser/.hermes/hermes-agent/tools/process_registry.pyr   r   C   s        ==GGGLLLGSKC#*.GXj&'...GSC#JFD#Ix}###M3,c,,,HdIscOSOSssc$$$$ %d ; ; ;NDI;;;uQU333K333"U15999s999#(55#A#A#A5AAA!E%e<<<OT<<<#eAE::::::!&s!?!?!????!E).AAAE9>AAA16t%1P1P1PNHY-.PPPd///D#/////rJ   r   c                      e Zd ZdZdZd Zededefd            Zde	dedd	fd
Z
edee         defd            Zdee	         dee	         fdZededd	fd            Zededefd            Z	 	 	 	 	 d:dedededededede	fdZ	 	 	 	 d;d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d ef
d!Zde	fd"Zde	fd#Zd$edefd%Zd$edee	         fd&Zd$edefd'Zd<d$ed*ed+edefd,Zd=d$ededefd-Zd$edefd.Zd$ed/edefd0Z d>d$ed/edefd1Z!d$edefd2Z"d=dede#fd3Z$dedefd4Z%dedefd5Z&d=dedefd6Z'd7 Z(d8 Z)defd9Z*d	S )?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d l}|                                | _        t                      | _
        d S )Nr   )_running	_finishedrF   rG   r6   pending_watchersqueueQueuecompletion_queueset_completion_consumed)self
_queue_mods     rK   __init__zProcessRegistry.__init__|   s_    3546^%%
 79 	#"""2<2B2B2D2D *-!!!rJ   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   NrI   ).0noiseliness     rK   	<genexpr>z5ProcessRegistry._clean_shell_noise.<locals>.<genexpr>   s,      cc%EU1X-ccccccrJ   r   )splitanyrM   _SHELL_NOISE_SUBSTRINGSpopjoin)rZ   ra   s    @rK   _clean_shell_noisez"ProcessRegistry._clean_shell_noise   s     

4   	cccc?;bccccc 	IIaLLL  	cccc?;bccccc 	yyrJ   sessionnew_textNc                    |j         r|j        rdS g }d}|                                D ]=}|j         D ]3}||v r-|                    |                                           ||} n4>|sdS t          j                    }|j        5  ||j        z
  t          k    rd|_	        ||_        |j	        t          k    r|xj        t          |          z  c_        |j        dk    r||_        n~||j        z
  t          k    rkd|_        | j                            |j        |j        |j        d|j        |j        |j        |j        |j        |j        d|j         d|j         dd	           	 ddd           dS |xj	        d
z  c_	        |xj        d
z  c_        d|_        |j        }d|_        ddd           n# 1 swxY w Y   d                    |dd                   }	t          |	          dk    r|	dd         dz   }	| j                            |j        |j        |j        d||	||j        |j        |j        |j        |j        d           dS )ai  Scan new output for watch patterns and queue notifications.

        Called from reader threads with new_text being the freshly-read chunk.
        Rate-limited: max WATCH_MAX_PER_WINDOW notifications per WATCH_WINDOW_SECONDS.
        If sustained overload exceeds WATCH_OVERLOAD_KILL_SECONDS, watching is
        disabled permanently for this process.
        Nr   r   Twatch_disabledz$Watch patterns disabled for process u    — too many matches (zB suppressed). Use process(action='poll') to check output manually.)
session_idr   r   type
suppressedplatformchat_iduser_id	user_name	thread_idmessage   r]      i  z
...(truncated)watch_match)rm   r   r   rn   patternoutputro   rp   rq   rr   rs   rt   )r-   r3   
splitlinesappendrstriptimer6   r5   WATCH_WINDOW_SECONDSr4   WATCH_MAX_PER_WINDOWr1   lenr2   WATCH_OVERLOAD_KILL_SECONDSrT   putr   r   r   r%   r&   r'   r(   r)   r0   rg   )
rW   ri   rj   matched_linesmatched_patternlinepatnowro   rz   s
             rK   _check_watch_patternsz%ProcessRegistry._check_watch_patterns   s9    % 	)@ 	F '')) 	 	D-  $;;!((777&.*-E	   	Fikk] *	* *	*W004HHH-.*.1+ )-AAA))S-?-??)) 0C7747G11788;VVV.2G+)--&-j'.':#*? 0&-&?$+$<#*#:#*#:%,%>%,%>T7: T T181JT T T/ /   " A*	* *	* *	* *	* *	* *	* *	* *	*F &&!+&&1$,/G) !2J()G%U*	* *	* *	* *	* *	* *	* *	* *	* *	* *	* *	* *	* *	* *	* *	*Z ="-..v;;ETE]%77F!!!*".!&$0.. 2 2#
 #
 	 	 	 	 	s   C!F435F44F8;F8r   c                 j    | sdS 	 t          j        | d           dS # t          t          f$ r Y dS w xY w)z1Best-effort liveness check for host-visible PIDs.Fr   T)oskillProcessLookupErrorPermissionErrorr   s    rK   _is_host_pid_alivez"ProcessRegistry._is_host_pid_alive   sQ      	5	GCOOO4"O4 	 	 	55	s    22c                 &   ||j         s|j        r|j        dk    r|S |                     |j                  r|S |j        5  |j         r|cddd           S d|_         d|_        ddd           n# 1 swxY w Y   |                     |           |S )zJUpdate recovered host-PID sessions when the underlying process has exited.Nr#   T)r   r"   r$   r   r   r6   r   _move_to_finished)rW   ri   s     rK   _refresh_detached_sessionz)ProcessRegistry._refresh_detached_session   s   ?gn?G4D?HY]cHcHcN""7;// 	N] 	% 	%~ 	% 	% 	% 	% 	% 	% 	% 	% "GN !%G	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	w'''s   	A1A11A58A5c                 4   t           r!t          j        | t          j                   dS 	 t          j        t          j        |           t          j                   dS # t          t          t          f$ r# t          j        | t          j                   Y dS w xY w)zKTerminate a host-visible PID without requiring the original process handle.N)
_IS_WINDOWSr   r   signalSIGTERMkillpggetpgidOSErrorr   r   r   s    rK   _terminate_host_pidz#ProcessRegistry._terminate_host_pid  s      	GC(((F	)Ibjoov~66666+_= 	) 	) 	)GC((((((	)s   1A 6BBenvc                 D   t          | dd          }t          |          r	  |            }t          |t                    r,|                    d          r|                    d          pdS n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wdS )zEReturn the writable sandbox temp dir for env-backed background tasks.get_temp_dirN/z*Could not resolve environment temp dir: %sz/tmp)	getattrcallable
isinstancer=   
startswithr}   	Exceptionloggerdebug)r   r   temp_direxcs       rK   _env_temp_dirzProcessRegistry._env_temp_dir  s     sND99L!! 	PP'<>>h,, 71D1DS1I1I 7#??3//636 P P PI3OOOOOOOOPvs   A
A. .
B8BBr   Fr   r   r   r   env_varsuse_ptyc                    t          dt          j                    j        dd          ||||pt	          j                    t          j                              }|ri	 t          rddlm	} nddl
m	} t                      }	t          t          j        |          }
d|
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	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-liczset +m; )   x   )r   r   
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)	rZ   r   r   encodingerrorsstdoutstderrstdin
preexec_fnzproc-reader-)'r   uuiduuid4hexr   getcwdr~   r   winptyr   
ptyprocessr   r   environspawnr   r   r8   rF   rH   _pty_reader_loopr   r7   startr6   _prune_if_neededrO   _write_checkpointImportErrorr   warningr   r@   rA   PIPESTDOUTsetsidr   _reader_loop)rW   r   r   r   r   r   r   ri   _PtyProcessCls
user_shellpty_envpty_procreaderebg_envprocs                   rK   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*+)//)=G)=)=>(	 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??%(!"!5G!5!56?$/*9tt	
 
 
 h !$,
,,	
 
 
 "(Z 	0 	0!!###(/DM'*%	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	   sU   C E0 $EE0 EE0 EE0 0$G	GF??G)$KK Kr   timeoutc                    t          dt          j                    j        dd          ||||t	          j                    |d          }|                     |          }| d|j         d}	| d|j         d}
| d|j         d	}t          j        |          }t          j        |          }t          j        |	          }t          j        |
          }t          j        |          }d
| d| 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        sEt)          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.
        r   Nr   sandbox)r   r   r   r   r   r   r   r$   z/hermes_bg_z.logz.pidz.exitz	mkdir -p z && ( nohup bash -lc z > z$ 2>&1; rc=$?; printf '%s\n' "$rc" > z ) & echo $! > z && cat r   rz   r   TzFailed to start: zproc-poller-r   )r   r   r   r   r~   r   r   shlexquoteexecutegetstripr{   isdigitr?   r   r   r   r   r    rF   rH   _env_poller_loopr7   r   r6   r   rO   r   )rW   r   r   r   r   r   r   ri   r   log_pathpid_path	exit_pathquoted_commandquoted_temp_dirquoted_log_pathquoted_pid_pathquoted_exit_path
bg_commandresultrz   r   r   r   s                          rK   spawn_via_envzProcessRegistry.spawn_via_env  s.   ( !.tz||',..#y{{	
 	
 	
 %%c**;;7:;;;;;7:;;;==GJ===	W--+h//+h//+h// ;y11D D D .D D3BD D/?D D )D D 3BD D 		<[[W[==FZZ"--3355F))++  zz||<<>> "%d))GKE  	< 	< 	<!GN "G$;$;$;G!!!!!!	<
 ~ 		%,sHh	B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F 
G F;;G $II
I
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 # 	 |j                             d           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wd|_        |j         j        |_        |                     |           w xY w)	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   r   readrh   r6   r    r   r!   r   r   r   r   waitr   
returncoder   r   )rW   ri   first_chunkchunkr   s        rK   r   zProcessRegistry._reader_loop  s   	,;.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 **7E:::;   	? 	? 	?LL:A>>>>>>>>	?H$$Q$//// H H HCQGGGGGGGGH!GN ' :G""7+++++H$$Q$//// H H HCQGGGGGGGGH!GN ' :G""7++++s   AB> ABB> BB> "B#B> =E. >
C-C(#E. (C--E. 1D 
D<D77D<.G,0FG,
F;F61G,6F;;1G,r   r   r   c                 N   t          j        |          }t          j        |          }t          j        |          }d}	|j        s^t          j        d           	 |                    d| dd          }
|
                    dd          }|rt          |          |	k    r
||	d	         nd}t          |          }	|j        5  ||_	        t          |j	                  |j
        k    r|j	        |j
         d	         |_	        d	d	d	           n# 1 swxY w Y   |r|                     ||           |                    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.r      zcat  2>/dev/nullr   r   rz   r   Nzkill -0 "$(cat z# 2>/dev/null)" 2>/dev/null; echo $?r   r   0T)r   r   r   r~   sleepr   r   r   r6   r    r!   r   r   r{   r?   r   
ValueError
IndexErrorr   r   )rW   ri   r   r   r   r   r   r   r   prev_output_lenr   
new_outputdeltacheckcheck_outputexit_resultexit_strs                    rK   r   z ProcessRegistry._env_poller_loop  s>     +h//+h// ;y11. +	JqMMM)%IO%I%I%ISUVV#ZZ"55
 	C<?
OOo<]<]J'7'788ceE&)*ooO  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  C227EBBB \\\\ $    %yy266<<>> L$;$;$=$=b$A$G$G$I$IS$P$P"%++=/=== ! #. # #K  +x<<BBDDH/,/0C0C0E0Eb0I0O0O0Q0Q,R,R))&
3 / / /,.)))/%)GN**7333F   !%$&!&&w///M . +	 +	 +	 +	 +	s\   A(I' ?DI' DI' DCI' +>H* )I' *II' II' '-JJ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   r   r   )r   NzPTY stdout reader ended: %sz PTY wait timed out or failed: %sT
exitstatusr   )r8   isaliver   r   r=   decoder6   r    r   r!   r   EOFErrorr   r   r   r   r   hasattrr  r   r   )rW   ri   ptyr   rZ   r   s         rK   r   z ProcessRegistry._pty_reader_loop=  s^   l	;++-- HHTNNE B(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 227DAAA   E    E ++--   	; 	; 	;LL6::::::::	;	@HHJJJJ 	@ 	@ 	@LL;Q????????	@.5c<.H.HPCNNbw'''''s   D AC  *AB>2C  >CC  CC  D  
C8*D ,	C85D 7C88D 
D?D::D?E 
F"FFc                    | j         5  | j                            |j        d          du}|| j        |j        <   ddd           n# 1 swxY w Y   |                                  |r_|j        rZddlm} |j	        r ||j	        dd                   nd}| j
                            d|j        |j        |j        |d           dS dS dS )u   Move a session from running to finished.

        Idempotent: if the session was already moved (e.g. kill_process raced
        with the reader thread), the second call is a no-op — no duplicate
        completion notification is enqueued.
        Nr   
strip_ansi0r   
completion)rn   rm   r   r   rz   )r6   rO   rf   r   rP   r   r+   tools.ansi_stripr  r    rT   r   r   r   )rW   ri   was_runningr  output_tails        rK   r   z!ProcessRegistry._move_to_finished\  sI    Z 	1 	1-++GJ==TIK)0DN7:&	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	   
  		75 		333333GNG\d**W%:566%BCCCbdK!%%$%j"?$.%' '     		 		 		 		s   2AA
A
rm   c                     || j         v S )zJCheck if a completion notification was already consumed via wait/poll/log.)rV   )rW   rm   s     rK   is_completion_consumedz&ProcessRegistry.is_completion_consumedx  s    T666rJ   c                     | j         5  | j                            |          p| j                            |          }ddd           n# 1 swxY w Y   |                     |          S )z*Get a session by ID (running or finished).N)r6   rO   r   rP   r   )rW   rm   ri   s      rK   r   zProcessRegistry.get|  s    Z 	V 	Vm''
33Ut~7I7I*7U7UG	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V--g666s   5A		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                            |           |j        r
d|d<   d|d<   |S )z9Check status and get new output for a background process.r   r  N	not_foundNo process with ID statuserrorr   r   running)rm   r   r   r   uptime_secondsoutput_previewr   Tr"   z=Process recovered after restart -- output history unavailablenote)r  r  r   r6   r    r   r   r   r   r?   r~   r   r   rV   addr"   )rW   rm   r  ri   r%  r   s         rK   pollzProcessRegistry.poll  ss   //////((:&&?)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,
 
 > 	6")"3F;%))*555 	]!%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}
|j        r| j
                            |           |
S )z;Read the full output log with optional pagination by lines.r   r  Nr  r  r  r   r#  r]   z lines)rm   r   rz   total_linesshowing)r  r  r   r6   r    r{   r   r   r   rg   rV   r'  )rW   rm   r*  r+  r  ri   full_outputra   r-  selectedr   s              rK   read_logzProcessRegistry.read_log  sr   //////((:&&?)4V*4V4VWWW] 	< 	<$*W%:;;K	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< &&((%jj Q;;5199eVWW~HHVFUN23H "*").?hhiii))&h--///
 
 > 	6%))*555s   A

AAc                 8   ddl m} ddlm} 	 t	          t          j        dd                    }n# t          t          f$ r d}Y nw xY w|}|}d}|r||k    r|}	d| d	| d
}n|p|}	| 	                    |          }
|
dd| dS t          j                    |	z   }t          j                    |k     r|                     |
          }
|
j        rD| j                            |           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  )is_interruptedTERMINAL_TIMEOUT180   NzRequested wait of z%s was clamped to configured limit of sr  r  r  r   r  )r   r   rz   timeout_noteinterruptedr"  z+User sent a new message -- wait interrupted)r   rz   r&  rv   r   )r   rz   zWaited zs, process still running)r  r  tools.interruptr3  r?   r   getenvr  	TypeErrorr   r~   	monotonicr   r   rV   r'  r   r    r   )rW   rm   r   r  _is_interrupteddefault_timeoutmax_timeoutrequested_timeoutr8  effective_timeoutri   deadliner   s                rK   r   zProcessRegistry.wait  su    	0/////EEEEEE	"!"),>"F"FGGOOI& 	" 	" 	"!OOO	"%# 	A!2[!@!@ +9%6 9 9*59 9 9 L
 !2 @[((:&&?)4V*4V4VWWW>##&77n))44W==G~ 	)--j999&!(!2(j)>uvv)FGG 
   :-9F>*   +(j)>uvv)FGGI 
   :-9F>*JqMMM/ n))4   j!6uvv!>??
 
  	[%1F>""%Z/@%Z%Z%ZF>"s   "1 AAc                    |                      |          }|dd| dS |j        r
d|j        dS 	 |j        rZ	 |j                            d           n# t
          $ r/ |j        r$t          j        |j        t          j
                   Y nmw xY w|j        r	 t          r|j                                         n;t          j        t          j        |j        j                  t          j
                   n# t          t           f$ r |j                                         Y nw xY w|j        r-|j        r&|j                            d	|j         d
d           n|j        r|j        dk    r|j        r|                     |j                  sL|j        5  d|_        d|_        ddd           n# 1 swxY w Y   |                     |           d|j        dS |                     |j                   ndddS d|_        d|_        |                     |           |                                  d|j        dS # t
          $ r}dt7          |          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   r#   r!  zkRecovered process cannot be killed after restart because its original runtime handle is no longer availableikilled)r   rm   )r   r   r   r8   	terminater   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r$   r   r6   r   r   r   r   r=   rW   rm   ri   r   s       rK   kill_processzProcessRegistry.kill_process  s   ((:&&?)4V*4V4VWWW> 	*$.  -	8| %=L***6666  = = ={ =V^<<<=  +" S113333	"*W_-@"A"A6>RRR*O< + + +O((*****+ W[ ''(I(I(I(IST'UUUU! g&76&A&Agk&A..w{;;   1 1)-,0)1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 **7333"2%,%6   ((5555 &M   "GN #G""7+++""$$$&gjAAA 	8 	8 	8%A77777777	8s   H= A H= 5BH= B
H= AC9 7H= 9*D&#H= %D&&A1H= F2&H= 2F66H= 9F6:!H= H= <A H= =
I#II#I#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  rE  Process has already finishedr8   r   ok)r   bytes_writtenr!  ?Process stdin not available (non-local backend or stdin closed))r   r   r  r8   r   r=   encodewriter   r   r   r   flush)rW   rm   rK  ri   pty_datar   s         rK   write_stdinzProcessRegistry.write_stdinA  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).r]   )rU  )rW   rm   rK  s      rK   submit_stdinzProcessRegistry.submit_stdin\  s    
D4K888rJ   c                    |                      |          }|dd| dS |j        rdddS t          |d          rO|j        rH	 |j                                         dd	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        	                                 ddd
S # t
          $ r}dt          |          dcY d}~S d}~ww xY w)zGClose a running process's stdin / send EOF without killing the process.Nr  r  r  rE  rM  r8   rN  zEOF sent)r   ru   r!  rP  zstdin closed)
r   r   r  r8   sendeofr   r=   r   r   closerI  s       rK   close_stdinzProcessRegistry.close_stdin`  sb   ((:&&?)4V*4V4VWWW> 	Y.9WXXX7F## 	< 	<<$$&&&"&:>>> < < <")CFF;;;;;;;;<  	sgo&; 	s%0qrrr	8O!'')))"~>>> 	8 	8 	8%A77777777	8s<   A" "
B,B=BB$"C 
C-C("C-(C-c                      j         5  t           j                                                  t           j                                                  z   }ddd           n# 1 swxY w Y    fd|D             }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 | ]}                     |          S rI   )r   )r_   r7  rW   s     rK   
<listcomp>z1ProcessRegistry.list_sessions.<locals>.<listcomp>|  s'    PPPa66q99PPPrJ   c                 *    g | ]}|j         k    |S rI   r   r_   r7  r   s     rK   r^  z1ProcessRegistry.list_sessions.<locals>.<listcomp>  s%    LLL!qyG7K7KA7K7K7KrJ   r)  z%Y-%m-%dT%H:%M:%Sr   r#  i8r   )rm   r   r   r   r   r$  r   r%  r   Tr"   )r6   rE   rO   valuesrP   r   r   r   r   r~   strftime	localtimer   r?   r   r    r   r"   r|   )rW   r   all_sessionsr   r7  entrys   ``    rK   list_sessionszProcessRegistry.list_sessionsw  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 QPPP<PPP 	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                 h   | j         5  t          | j                                                  }ddd           n# 1 swxY w Y   |D ]}|                     |           | 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.Nc              3   >   K   | ]}|j         k    o|j         V  d S Nr   r   ra  s     rK   rb   z7ProcessRegistry.has_active_processes.<locals>.<genexpr>  sG         	W$5QX     rJ   r6   rE   rO   rb  r   rd   )rW   r   sessionsri   s    `  rK   has_active_processesz$ProcessRegistry.has_active_processes  P   Z 	4 	4DM002233H	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4   	4 	4G**73333Z 	 	    --//    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!   '<A A (2B''B+.B+c                 h   | j         5  t          | j                                                  }ddd           n# 1 swxY w Y   |D ]}|                     |           | 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.Nc              3   >   K   | ]}|j         k    o|j         V  d S rj  )r   r   )r_   r7  r   s     rK   rb   z9ProcessRegistry.has_active_for_session.<locals>.<genexpr>  sG         ,=QX     rJ   rl  )rW   r   rm  ri   s    `  rK   has_active_for_sessionz&ProcessRegistry.has_active_for_session  ro  rp  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 rj  rk  ra  s     rK   r^  z,ProcessRegistry.kill_all.<locals>.<listcomp>  s;       OqyG';';QX'; ';';';rJ   Nr   r   )rG  rE  rv   )r6   rO   rb  rJ  r   r   )rW   r   targetsrG  ri   r   s    `    rK   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 rI   )r   FINISHED_TTL_SECONDS)r_   sidr7  r   s      rK   r^  z4ProcessRegistry._prune_if_needed.<locals>.<listcomp>  s:     
 
 
Cal"&::: :::rJ   c                 (    j         |          j        S rj  )rP   r   )r{  rW   s    rK   <lambda>z2ProcessRegistry._prune_if_needed.<locals>.<lambda>  s    DN3<O<Z rJ   )keyN)r~   rP   itemsr   rO   MAX_PROCESSESmin)rW   expiredr{  total	oldest_idr   s   `    @rK   r   z ProcessRegistry._prune_if_needed  s     ikk
 
 
 
"n2244
 
 
  	$ 	$Cs## DM""S%8%88M!!dn!DN0Z0Z0Z0Z[[[Iy))) "!!!rJ   c                 T   	 | j         5  g }| j                                        D ]}|j        s|                    i d|j        d|j        d|j        d|j        d|j	        d|j
        d|j        d|j        d	|j        d
|j        d|j        d|j        d|j        d|j        d|j        d|j                   	 ddd           n# 1 swxY w Y   ddlm}  |t.          |           dS # t0          $ r(}t2                              d|d           Y d}~dS d}~ww xY w)z=Write running process metadata to checkpoint file atomically.rm   r   r   r$   r   r   r   r   r%   r&   r'   r(   r)   r*   r+   r-   Nr   )atomic_json_writez#Failed to write checkpoint file: %sT)exc_info)r6   rO   rb  r   r|   r   r   r   r$   r   r   r   r   r%   r&   r'   r(   r)   r*   r+   r-   utilsr  CHECKPOINT_PATHr   r   r   )rW   entriesr7  r  r   s        rK   r   z!ProcessRegistry._write_checkpoint  s   	R  --//  A8  ((!$(%qy( "15( (	(
 "15( )!,( &qy( *1=( /0B( .q/@( .q/@( 01D( 01D( /0B( 1!2F(  -a.>!(                 0 0/////ow77777 	R 	R 	RLL>DLQQQQQQQQQ	Rs;   C5 B;CC5 CC5 CC5 5
D'?D""D'c                     t                                           sdS 	 t          j        t                               d                    }n# t
          $ r Y dS w xY wd}|D ]}|                    d          }|s|                    dd          }|dk    r:t                              d|                    dd	          d
d         ||           q| 	                    |          }|rt          d!i d|d         d|                    dd	          d|                    dd          d|                    dd          d|d|d|                    d          d|                    dt          j                              ddd|                    dd          d|                    dd          d|                    dd          d|                    dd          d|                    dd          d|                    dd          d|                    dd          d|                    dg           }| 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    rQ| j                            |j        |j        |j        |j        |j        |j        |j        |j        |j        d 	           |                                  |S )"z
        On gateway startup, probe PIDs from checkpoint file.

        Returns the number of processes recovered as detached.
        r   r   )r   r   r$   r#   z=Skipping recovery for non-host process: %s (pid=%s, scope=%s)r   unknownN<   r   rm   r   r   r   r   r   r"   Tr%   r&   r'   r(   r)   r*   r+   Fr-   rv   z'Recovered detached process: %s (pid=%d))	rm   check_intervalr   rp   rq   rr   rs   rt   r+   rI   )r  existsjsonloads	read_textr   r   r   infor   r   r~   r6   rO   r   r   r*   rQ   r|   r   r%   r&   r'   r(   r)   r+   r   )rW   r  	recoveredrf  r   r$   aliveri   s           rK   recover_from_checkpointz'ProcessRegistry.recover_from_checkpoint  s    %%'' 	1	j!:!:G!:!L!LMMGG 	 	 	11	 	 :	 :	E))E""C 		+v66IF"" SIIi33CRC8	    ++C00E %(   \**!IIi;;; "IIi444 !&		- < < <	
  (i 		%(((  %yyty{{CCC "T &+YY/A2%F%F%F %*II.?$D$D$D %*II.?$D$D$D ',ii0CR&H&H&H ',ii0CR&H&H&H &+YY/A1%E%E%E  (-yy1Eu'M'M'M!" $)99-=r#B#B#B#& 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'.':$+$<#*#:#*#:%,%>%,%>.5.H
2 
2 
 
 
 	   s#   -A 
AAI##I'	*I'	)Nr   r   NF)Nr   r   r   )r   r)  rj  )r   )+r9   r:   r;   r<   re   rY   staticmethodr=   rh   r   r   r   r?   rC   r   r   r   r   r   dictr   r   r   r   r   r   r  r   r(  r1  r   rJ  rU  rW  r[  rE   rg  rn  rs  rw  r   r   r  rI   rJ   rK   rM   rM   j   s        / / /&           \ X^ Xs Xt X X X Xt  $    \.1I hWeNf    & 	) 	) 	) 	) 	) \	) 
3 
3 
 
 
 \
 n nn n 	n
 n n n 
n n n nh N NN N 	N
 N N N 
N N N Nd,N , , , ,83%3,/3;>3JM3Z]3 3 3 3j( ( ( ( (>    87 7 7 7 7 77c 7h~&> 7 7 7 7s t    6 3   d    >G Gs GS GD G G G GR:8s :8t :8 :8 :8 :8x8c 8 8 8 8 8 869 9s 9# 9t 9 9 9 98c 8d 8 8 8 8. S D    >C D    # $      s    "* * *&R R R@M M M M M M MrJ   rM   )registry
tool_errorr   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), 'close' (close stdin/send EOF).objectstring)rE   r(  logr   r   rR  submitrZ  z)Action to perform on background processes)rn   enumdescriptionz]Process session ID (from terminal background output). Required for all actions except 'list'.)rn   r  z@Text to send to process stdin (for 'write' and 'submit' actions)integerzJMax seconds to block for 'wait' action. Returns partial output on timeout.rv   )rn   r  minimumz6Line offset for 'log' action (default: last 200 lines)z$Max lines to return for 'log' action)actionrm   rK  r   r*  r+  r  )rn   
propertiesrequired)r   r  
parametersc                    dd l }|                    d          }|                     dd          }|                     d          #t          |                     dd                    nd}|dk    r2|                    dt                              |          id	
          S |dv r|st          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k    r/|                    t                              |          d	
          S t          d| d          S )Nr   r   r  r   rm   rE   	processesr`  F)ensure_ascii)r(  r  r   r   rR  r  rZ  zsession_id is required for r(  r  r*  r+  r)  )r*  r+  r   r   r   r   rR  rK  r  rZ  zUnknown process action: z8. Use: list, poll, log, wait, kill, write, submit, close)r  r   r=   dumpsprocess_registryrg  r  r(  r1  r   rJ  rU  rW  r[  )r   kw_jsonr   r  rm   s         rK   _handle_processr  |  s   ffYGXXh##F48HH\4J4J4VTXXlB//000\^J{{K)9)G)GPW)G)X)XYhm{nnn	N	N	N 	FDFD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w;;/;;JGGV[;\\\qqqqrrrrJ   terminalu   ⚙️)r   toolsetschemahandleremoji)-r<   r  loggingr   rp   r   r   r@   rF   r~   r   systemr   tools.environments.localr   r   dataclassesr   r   typingr   r	   r
   r   hermes_cli.configr   	getLoggerr9   r   r  rD   rz  r  r   r   r   r   rM   r  tools.registryr  r  PROCESS_SCHEMAr  registerrI   rJ   rK   <module>r     s   >   				             ho9, J J J J J J J J ( ( ( ( ( ( ( ( , , , , , , , , , , , , - - - - - -		8	$	$ "/##&66         #0 #0 #0 #0 #0 #0 #0 #0LY Y Y Y Y Y Y Yz #?$$  0 / / / / / / / 	_  ![[[J  !~ 
 !a 
 "k  "W 
 "E /
 
: J?   * *Zs s s<  	
     rJ   