
    i                        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#dZ$dZ%de&de'f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, Z0 e-j1        dd-e/e0d./           dS )0a  
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  @         
      secondsreturnc                     t          dt          |                     }|dk     r| dS t          |d          \  }}|dk     r| d| dS t          |d          \  }}| d| dS )Nr   <   szm zh m)maxintdivmod)r   r   minssecshourss        ;/home/ubuntu/.hermes/hermes-agent/tools/process_registry.pyformat_uptime_shortr   M   s    As7||A2vvwww2JD$byy!!$!!!!r""KE4t    c                   8   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"dd           Z,e
ed'<    e"e-j.                  Z/e-j.        ed(<    e"dd           Z0e	e-j1                 ed)<    e"dd           Z2e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_disabled_watch_last_emit_at_watch_cooldown_until_watch_strike_candidate_watch_consecutive_strikes_lock_reader_thread_pty)3__name__
__module____qualname____doc__str__annotations__r&   r'   r(   r   r   r)   
subprocessPopenr*   r   r+   r-   floatr.   boolr/   r0   MAX_OUTPUT_CHARSr1   r2   r4   r5   r6   r7   r8   r9   r:   r;   r   listr=   r
   r@   rA   rB   rC   rD   rE   rF   	threadingLockrG   rH   ThreadrI    r    r   r"   r"   X   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!E%e<<<OT<<< "'s!?!?!????#(55#A#A#A5AAA$)E%e$D$D$DTDDD&+eAE&B&B&BBBB!E).AAAE9>AAA16t%1P1P1PNHY-.PPPd///D#/////r    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
dede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@d*Zd&edefd+ZdAd&ed.ed/edefd0Z dBd&ededefd1Z!d&edefd2Z"d&ed3edefd4Z#dCd&ed3edefd5Z$d&edefd6Z%dBdede&fd7Z'dedefd8Z(dedefd9Z)dBdedefd:Z*d; Z+d< Z,defd=Z-d	S )D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                      | _
        t          j                    | _        d| _        d| _        d| _        d| _        d S )Nr   r,   )_running	_finishedrV   rW   rG   pending_watchersqueueQueuecompletion_queueset_completion_consumed_global_watch_lock_global_watch_window_start_global_watch_window_hits_global_watch_tripped_until$_global_watch_suppressed_during_trip)self
_queue_mods     r   __init__zProcessRegistry.__init__   s    3546^%%
 79 	#"""2<2B2B2D2D *-!
 #,."2"214'./&25(9:111r    textr   c                    |                      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   NrY   ).0noiseliness     r   	<genexpr>z5ProcessRegistry._clean_shell_noise.<locals>.<genexpr>   s,      cc%EU1X-ccccccr    r   )splitanyr[   _SHELL_NOISE_SUBSTRINGSpopjoin)rm   rs   s    @r   _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r    sessionnew_textNc                    |j         r|j        rdS |j        rdS g }d}|                                D ]=}|j         D ]3}||v r-|                    |                                           ||} n4>|sdS t          j                    }d}|j        5  |j        ri||j        k     r^|xj	        t          |          z  c_	        |j        s7d|_        |xj        dz  c_        |j        t          k    rd|_        d|_        d}d}	nR|j        r|j        sd|_        d|_        ||_        |t           z   |_        |xj        dz  c_        |j	        }
d|_	        d}	ddd           n# 1 swxY w Y   |	rp|rl| j                            |j        |j        |j        d|j	        |j        |j        |j        |j        |j        d|j         dt           d	t            d
d           dS d                    |dd                   }t          |          dk    r|dd         dz   }|                     |          sdS | j                            |j        |j        |j        d|||
|j        |j        |j        |j        |j        d           dS )uz  Scan new output for watch patterns and queue notifications.

        Called from reader threads with new_text being the freshly-read chunk.

        Per-session rate limit: at most ONE watch-match notification per
        WATCH_MIN_INTERVAL_SECONDS. Any match arriving inside the cooldown
        window is dropped and counts as ONE strike for that window. After
        WATCH_STRIKE_LIMIT consecutive strike windows, watch_patterns is
        disabled for this session and the session is promoted to
        notify_on_complete semantics — one notification when the process
        actually exits, no more mid-process spam.
        NFT   r   watch_disabledz$Watch patterns disabled for process u    — z7 consecutive rate-limit windows triggered (min spacing zms). Falling back to notify_on_complete semantics; you'll get exactly one notification when the process exits.)
session_idr'   r$   type
suppressedplatformchat_iduser_id	user_name	thread_idmessagero      i  z
...(truncated)watch_match)r   r'   r$   r   patternoutputr   r   r   r   r   r   )r=   rB   r.   
splitlinesappendrstriptimerG   rD   rA   lenrE   rF   WATCH_STRIKE_LIMITr;   rC   WATCH_MIN_INTERVAL_SECONDSr@   rb   putr#   r'   r$   r5   r6   r7   r8   r9   ry   _global_watch_admit)rj   r{   r|   matched_linesmatched_patternlinepatnowshould_disablereturn_earlyr   r   s               r   _check_watch_patternsz%ProcessRegistry._check_watch_patterns   s    % 	)@ 	F
 > 	F '')) 	 	D-  $;;!((777&.*-E	   	Fikk] %	% %	%
 ,  %w7T1T1T))S-?-??))6 	.6:G366!;669=OOO26/ 6:2)-# 1;#;; :;G627/ /2+036P0P-##q(##$6
,-)$K%	% %	% %	% %	% %	% %	% %	% %	% %	% %	% %	% %	% %	% %	% %	%N  	  %))")*#*#6&,")"; ' 8&6&6!(!:!(!:Lwz L L-L L(BL L L+ +   & F ="-..v;;ETE]%77F '',, 	F!!!*".!&$0.. 2 2#
 #
 	 	 	 	 	s   CEE#&E#r   c                    | j         5  | j        rJ|| j        k    r?| j        }d| _        d| _        || _        d| _        |dk    rdddd|d| ddddddd}nd}nd}| j        r || j        k     r| xj        d	z  c_        d
}d}ni|| j        z
  t
          k    r|| _        d| _        | j        t          k    r$|t          z   | _        | xj        d	z  c_        |}d
}n| xj        d	z  c_        d}d}ddd           n# 1 swxY w Y   || j        	                    |           |>| j        	                    dddddt           dt
           dt           ddddddd
           |S )a1  Return True if this watch_match event is allowed through the global breaker.

        Semantics:
        - If we're currently in a cooldown period, drop the event and count it.
        - Otherwise, slide the rolling window and check the global cap.
        - If the cap is exceeded, trip the breaker for WATCH_GLOBAL_COOLDOWN_SECONDS
          and emit ONE summary event so the agent/user sees "N notifications were
          suppressed" instead of getting them individually.
        - When the cooldown ends, emit a release summary and reset counters.
        r,   r   r%   watch_overflow_releasedz%Watch-pattern notifications resumed. z1 match event(s) were suppressed during the flood.)r   r'   r$   r   r   r   r   r   r   r   r   Nr~   FTwatch_overflow_trippedzWatch-pattern overflow: >z notifications in zCs across all processes. Suppressing further watch_match events for zs.)
r   r'   r$   r   r   r   r   r   r   r   )
re   rh   ri   rf   rg   WATCH_GLOBAL_WINDOW_SECONDSWATCH_GLOBAL_MAX_PER_WINDOWWATCH_GLOBAL_COOLDOWN_SECONDSrb   r   )rj   r   r   release_msgadmittrip_nows         r   r   z#ProcessRegistry._global_watch_admit;  sb    $ 3	! 3	!/ #C4;[4[4[!F
360<=925/12.>> ')')#% 9&0])] ] ] %'#%#%%'%'# #KK" #'KK" / !C$:Z4Z4Z99Q>99 88<WWW69D356D215PPP7:=Z7ZD4==B=="H!EE22a722#H Eg3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	! 3	!l "!%%k222!%% !090K 9 9(C9 9 59 9 9
 ' '   " s   C$C88C<?C<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    r   _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.Nr3   T)r.   r2   r4   r   r(   rG   r/   _move_to_finished)rj   r{   s     r   _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    r   _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
isinstancerN   
startswithr   	Exceptionloggerdebug)r   r   temp_direxcs       r   _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; )r   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)	rm   r+   r   encodingerrorsstdoutstderrstdin
preexec_fnzproc-reader-)'r"   uuiduuid4hexr   getcwdr   r   winptyr   
ptyprocessr   r   environspawnr+   r(   rI   rV   rX   _pty_reader_loopr#   rH   startrG   _prune_if_neededr]   _write_checkpointImportErrorr   warningr   rP   rQ   PIPESTDOUTsetsidr)   _reader_loop)rj   r$   r+   r&   r'   r   r   r{   _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*+)//)=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*   r4   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   r   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/   r0   rV   rX   _env_poller_looprH   r   rG   r   r]   r   )rj   r   r$   r+   r&   r'   r   r{   r   log_pathpid_path	exit_pathquoted_commandquoted_temp_dirquoted_log_pathquoted_pid_pathquoted_exit_path
bg_commandresultr   r   r   r   s                          r   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   readrz   rG   r0   r   r1   r   r   r   r   waitr.   
returncoder/   r   )rj   r{   first_chunkchunkr   s        r   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   r   r%   Nzkill -0 "$(cat z# 2>/dev/null)" 2>/dev/null; echo $?r  r   0T)r   r   r.   r   sleepr   r  r   rG   r0   r1   r   r  r   r   r/   
ValueError
IndexErrorr   r   )rj   r{   r   r  r  r  r
  r  r  prev_output_lenr  
new_outputdeltacheckcheck_outputexit_resultexit_strs                    r   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   )rI   isaliver  r   rN   decoderG   r0   r   r1   r   EOFErrorr   r   r   r  r.   hasattrr(  r/   r   )rj   r{   ptyr  rm   r   s         r   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)r   r   r$   r/   r   )rG   r]   rx   r#   r^   r   r;   tools.ansi_stripr0  r0   rb   r   r$   r/   )rj   r{   was_runningr0  output_tails        r   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
r   c                     || j         v S )zJCheck if a completion notification was already consumed via wait/poll/log.)rd   )rj   r   s     r   is_completion_consumedz&ProcessRegistry.is_completion_consumed  s    T666r    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)rG   r]   r  r^   r   )rj   r   r{   s      r   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r"   c                    ||j         rdS t          |dd          }|dS 	 |                                }n# t          $ r Y dS w xY w|dS d}t          |dd          }|at          sY	 ddl}|                                }|                    ||j                  }|                    ||j        |t          j
        z             	 |                                }	|	r.t          |	t                    r|	n|	                    dd          }n# t          t           t"          f$ r Y nw xY w	 |                    ||j        |           nB# t          $ r Y n6w xY w# 	 |                    ||j        |           w # t          $ r Y w w xY wxY wn8# t          $ r+}
t$                              d	|j        |
           Y d}
~
nd}
~
ww xY w|j        5  |rG|xj        |z  c_        t/          |j                  |j        k    r|j        |j         d         |_        d
|_         ||_        ddd           n# 1 swxY w Y   t$                              d|j        |           |                     |           dS )u$  Reconcile session.exited against the real child process state.

        The reader thread (`_reader_loop`) sets `session.exited = True` only
        in its `finally` block, which runs when `stdout.read()` returns EOF.
        If the direct `Popen` child has exited but a descendant process (e.g.
        a daemon spawned by `hermes update` restarting the gateway) is still
        holding the stdout pipe open, the reader blocks forever and poll()
        keeps returning "running" indefinitely (issue #17327 — 74 polls over
        7 minutes on Feishu).

        This helper closes that window: when `session.exited` is still False
        but the direct child's `Popen.poll()` reports an exit code, drain any
        readable bytes non-blocking and flip `session.exited`. The orphaned
        reader thread remains stuck on its blocking `read()` but is a daemon
        thread and will be reaped with the process.

        Safe no-op on sessions without a local `Popen` (env/PTY), already-
        exited sessions, and detached-recovered sessions.
        Nr)   r%   r   r   r   r   r'  z$Non-blocking drain failed for %s: %sTzxReconciled session %s: direct child exited with code %s but reader was still blocked (orphaned pipe). Flipped to exited.)r.   r   pollr   r   fcntlfilenoF_GETFLF_SETFLr   
O_NONBLOCKr  r   rN   r*  BlockingIOErrorr   r  r   r   r#   rG   r0   r   r1   r/   infor   )rj   r{   r   rcdrainedr   r;  fdflagsr  r   s              r   _reconcile_local_exitz%ProcessRegistry._reconcile_local_exit#  s!   ( ?gn?Fw	400<F	BB 	 	 	FF	:F x..kT]]__B66Bur}/DEEE
"KKMME o+5eS+A+A"n%%u||T[dm|GnGn'*=   DBu====$   Bu====$    T T TCWZQRSSSSSSSST ] 	# 	# ^%%0%%w,--0HHH,3,A7C[B[B\B\,]G)!GN "G	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	DJ	
 	
 	

 	w'''''s   7 
AA+AF AD E D'$E &D''E +E F 
EF EF FE76F7
FFFFF 
G !F;;G 
AH..H25H2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)r   r$   rK  r(   uptime_secondsoutput_previewr/   Tr2   z=Process recovered after restart -- output history unavailablenote)r3  r0  r  rF  rG   r0   r#   r$   r.   r(   r   r   r-   r/   rd   addr2   )rj   r   r0  r{   rP  r  s         r   r:  zProcessRegistry.pollk  s   //////((:&&?)4V*4V4VWWW 	""7+++] 	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   "A00A47A4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/  NrH  rI  rJ  r.   rN  ro   z lines)r   rK  r   total_linesshowing)r3  r0  r  rG   r0   r   r   r#   r.   ry   rd   rR  )rj   r   rT  rU  r0  r{   full_outputrs   rW  selectedr  s              r   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                 b   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 r   rH  rI  rJ  r.   r1  )rK  r/   r   timeout_noteinterruptedrM  z+User sent a new message -- wait interrupted)rK  r   rQ  r~   r   )rK  r   zWaited zs, process still running)r3  r0  tools.interruptr]  r   r   getenvr  	TypeErrorr  r   	monotonicr   rF  r.   rd   rR  r/   r0   r  )rj   r   r   r0  _is_interrupteddefault_timeoutmax_timeoutrequested_timeoutra  effective_timeoutr{   deadliner  s                r   r  zProcessRegistry.wait  s    	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 &&w///~ 	)--j999&!(!2(j)>uvv)FGG 
   :-9F>*   +(j)>uvv)FGGI 
   :-9F>*JqMMM7 n))<   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.NrH  rI  rJ  already_exited)rK  r/   T)forcezkill r  r  r   r3   rL  zkRecovered process cannot be killed after restart because its original runtime handle is no longer availableikilled)rK  r   )r  r.   r/   rI   	terminater   r(   r   r   r   r   r)   r   r   r   r   r   r*   r   r2   r4   r   rG   r   r   r   r#   rN   rj   r   r{   r   s       r   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).NrH  rI  rJ  rn  Process has already finishedrI   r   ok)rK  bytes_writtenrL  ?Process stdin not available (non-local backend or stdin closed))r  r.   r,  rI   r   rN   encodewriter   r   r)   r   flush)rj   r   rt  r{   pty_datar   s         r   write_stdinzProcessRegistry.write_stdin2  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).ro   )r~  )rj   r   rt  s      r   submit_stdinzProcessRegistry.submit_stdinM  s    
D4K888r    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.NrH  rI  rJ  rn  rv  rI   rw  zEOF sent)rK  r   rL  ry  zstdin closed)
r  r.   r,  rI   sendeofr   rN   r)   r   closerr  s       r   close_stdinzProcessRegistry.close_stdinQ  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 rY   )r   )rq   r   rj   s     r   
<listcomp>z1ProcessRegistry.list_sessions.<locals>.<listcomp>m  s'    PPPa66q99PPPr    c                 *    g | ]}|j         k    |S rY   r&   rq   r   r&   s     r   r  z1ProcessRegistry.list_sessions.<locals>.<listcomp>p  s%    LLL!qyG7K7KA7K7K7Kr    rS  z%Y-%m-%dT%H:%M:%Sr.   rN  i8r%   )r   r$   r+   r(   r-   rO  rK  rP  r/   Tr2   )rG   rU   r]   valuesr^   r#   r$   r+   r(   r   strftime	localtimer-   r   r.   r0   r/   r2   r   )rj   r&   all_sessionsr  r   entrys   ``    r   list_sessionszProcessRegistry.list_sessionsh  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.   r  s     r   rt   z7ProcessRegistry.has_active_processes.<locals>.<genexpr>  sG         	W$5QX     r    rG   rU   r]   r  r   rv   )rj   r&   sessionsr{   s    `  r   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 r  )r'   r.   )rq   r   r'   s     r   rt   z9ProcessRegistry.has_active_for_session.<locals>.<genexpr>  sG         ,=QX     r    r  )rj   r'   r  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'; ';';';r    Nr   rK  )rp  rn  r~   )rG   r]   r  rs  r#   r  )rj   r&   targetsrp  r{   r  s    `    r   kill_allzProcessRegistry.kill_all  s    Z 	 	   =//11  G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  	 	G&&wz22Fzz(##'CCC!s   &;??c                 V    t          j                     fd j                                        D             }|D ]$} j        |=  j                            |           %t           j                  t           j                  z   }|t          k    rB j        r;t           j         fd          } j        |=  j                            |            j        	                                 j        	                                z  } j        |z
  }|r xj        |z  c_        dS dS )zGRemove oldest finished sessions if over MAX_PROCESSES. Must hold _lock.c                 @    g | ]\  }}|j         z
  t          k    |S rY   )r-   FINISHED_TTL_SECONDS)rq   sidr   r   s      r   r  z4ProcessRegistry._prune_if_needed.<locals>.<listcomp>  s:     
 
 
Cal"&::: :::r    c                 (    j         |          j        S r  )r^   r-   )r  rj   s    r   <lambda>z2ProcessRegistry._prune_if_needed.<locals>.<lambda>  s    DN3<O<Z r    )keyN)
r   r^   itemsrd   discardr   r]   MAX_PROCESSESminkeys)rj   expiredr  total	oldest_idtrackedstaler   s   `      @r   r   z ProcessRegistry._prune_if_needed  sN    ikk
 
 
 
"n2244
 
 
  	3 	3Cs#%--c2222 DM""S%8%88M!!dn!DN0Z0Z0Z0Z[[[Iy)%--i888
 -$$&&)<)<)>)>>)G3 	/%%.%%%%	/ 	/r    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.r   r$   r(   r4   r+   r-   r&   r'   r5   r6   r7   r8   r9   r:   r;   r=   Nr   )atomic_json_writez#Failed to write checkpoint file: %sT)exc_info)rG   r]   r  r.   r   r#   r$   r(   r4   r+   r-   r&   r'   r5   r6   r7   r8   r9   r:   r;   r=   utilsr  CHECKPOINT_PATHr   r   r   )rj   entriesr   r  r   s        r   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(   r4   r3   z=Skipping recovery for non-host process: %s (pid=%s, scope=%s)r$   unknownNr   r#   r   r&   r%   r'   r+   r-   r2   Tr5   r6   r7   r8   r9   r:   r;   Fr=   r~   z'Recovered detached process: %s (pid=%d))	r   check_intervalr'   r   r   r   r   r   r;   rY   )r  existsjsonloads	read_textr   r  r   rA  r   r"   r   rG   r]   r#   r$   r:   r_   r   r'   r5   r6   r7   r8   r9   r;   r   )rj   r  	recoveredr  r(   r4   aliver{   s           r   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"   r   N)r   rS  r  )r%   ).rJ   rK   rL   rM   rw   rl   staticmethodrN   rz   r"   r   rR   rS   r   r   r   r   r   r   r   r   dictr   r  r   r  r   r   r7  r  rF  r:  r[  r  rs  r~  r  r  rU   r  r  r  r  r   r   r  rY   r    r   r[   r[      s[        ; ; ;8           \ |^ |s |t | | | ||Uu U U U U Un  $    \.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F( F( F( F(Ps t    > 3   d    >K Ks KS KD K K K KZ: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r    r[   )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)rU   r:  logr  r   r{  submitr  z)Action to perform on background processes)r   enumdescriptionz]Process session ID (from terminal background output). Required for all actions except 'list'.)r   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.r~   )r   r  minimumz6Line offset for 'log' action (default: last 200 lines)z$Max lines to return for 'log' action)actionr   rt  r   rT  rU  r  )r   
propertiesrequired)r   r  
parametersc                 r   |                     d          }|                      dd          }|                      d          #t          |                      dd                    nd}|dk    r1t          j        dt                              |          id	          S |d
v r|st          d|           S |dk    r.t          j        t                              |          d	          S |dk    rYt          j        t                              ||                      dd          |                      dd                    d	          S |dk    rCt          j        t          	                    ||                      d                    d	          S |dk    r.t          j        t          
                    |          d	          S |dk    rPt          j        t                              |t          |                      dd                              d	          S |dk    rPt          j        t                              |t          |                      dd                              d	          S |dk    r.t          j        t                              |          d	          S t          d| d          S )Nr&   r  r%   r   rU   	processesr  F)ensure_ascii)r:  r  r  r   r{  r  r  zsession_id is required for r:  r  rT  r   rU  rS  )rT  rU  r  r   r   r   r{  rt  r  r  zUnknown process action: z8. Use: list, poll, log, wait, kill, write, submit, close)r  rN   r  dumpsprocess_registryr  r  r:  r[  r  rs  r~  r  r  )r   kwr&   r  r   s        r   _handle_processr  w  s   ffYGXXh##F48HH\4J4J4VTXXlB//000\^Jz;(8(F(Fw(F(W(WXglmmmm	N	N	N 	FDFDDEEEV:.33J??eTTTTu__:.77488Ha#8#8RU@V@V 8 X Xfkm m m mv:.33JQZH[H[3\\kpqqqqv:.;;JGGV[\\\\w:.:::s488TZ\^K_K_G`G`aapuvvvvx:.;;JDHHU[]_L`L`HaHabbqvwwwww:.:::FFUZ[[[[qqqqrrrr    terminalu   ⚙️)r   toolsetschemahandleremoji)2rM   r  loggingr   r   r   r   rP   rV   r   r   systemr   tools.environments.localr   r   dataclassesr   r   typingr   r	   r
   r   hermes_cli.configr   	getLoggerrJ   r   r  rT   r  r  r   r   r   r   r   r   rN   r   r"   r[   r  tools.registryr  r  PROCESS_SCHEMAr  registerrY   r    r   <module>r     s   >   				             ho9, J J J J J J J J ( ( ( ( ( ( ( ( , , , , , , , , , , , , - - - - - -		8	$	$ "/##&66        !    "       +0 +0 +0 +0 +0 +0 +0 +0\w w w w w w w wv% #?$$  0 / / / / / / / 	_  ![[[J  !~ 
 !a 
 "k  "W 
 "E /
 
: J?   * *Zs s s:  	
     r    