
    )j                    8   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mZ ddl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e/ddfdZ0ddl1m2Z2m3Z3 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/Z4d0 Z5 e2j6        dd1e4e5d23           dS )4a  
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_resolve_safe_cwd_sanitize_subprocess_env)windows_hide_flags)	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!   N   s    As7||A2vvwww2JD$byy!!$!!!!r""KE4t    c                   F   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<   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/                  Z0e.j/        ed)<    e#dd!          Z1e	e.j2                 ed*<    e#dd!          Z3e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_idwatcher_message_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)4__name__
__module____qualname____doc__str__annotations__r(   r)   r*   r   r   r+   
subprocessPopenr,   r
   r-   r/   floatr0   boolr1   r2   MAX_OUTPUT_CHARSr3   r4   r6   r7   r8   r9   r:   r;   r<   r=   r>   r	   listr@   r   rC   rD   rE   rF   rG   rH   rI   	threadingLockrJ   rK   ThreadrL    r"   r    r$   r$   Y   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	 	 	 	 	 dAdedededededede	fdZ	 	 	 	 dBd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dCd)Zd&edee	         fd*ZdDd,Zd&edefd-Z dEd&ed0ed1edefd2Z!dFd&ededefd3Z"d&edefd4Z#d&ed5edefd6Z$dGd&ed5edefd7Z%d&edefd8Z&defd9Z'dFdede(fd:Z)dedefd;Z*dedefd<Z+dFdedefd=Z,d> Z-d? Z.defd@Z/d	S )H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	_finishedrY   rZ   rJ   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   Nr\   ).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)rp   rv   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   |	rv|rr| j                            |j        |j        |j        d|j	        |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        |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
message_idmessagerr      i  z
...(truncated)watch_match)r   r)   r&   r   patternoutputr   r   r   r   r   r   r   )r@   rE   r0   
splitlinesappendrstriptimerJ   rG   rD   lenrH   rI   WATCH_STRIKE_LIMITr>   rF   WATCH_MIN_INTERVAL_SECONDSrC   re   putr%   r)   r&   r7   r8   r9   r:   r;   r<   r|   _global_watch_admit)rm   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!4#
 #
 	 	 	 	 	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   )
rh   rk   rl   ri   rj   WATCH_GLOBAL_WINDOW_SECONDSWATCH_GLOBAL_MAX_PER_WINDOWWATCH_GLOBAL_COOLDOWN_SECONDSre   r   )rm   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                 ,    | sdS ddl m}  ||           S )z1Best-effort liveness check for host-visible PIDs.Fr   )_pid_exists)gateway.statusr   )r*   r   s     r    _is_host_pid_alivez"ProcessRegistry._is_host_pid_alive  s5      	5 	/.....{3r"   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.Nr5   T)r0   r4   r6   r   r*   rJ   r1   _move_to_finished)rm   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                    t           r	 t          j        ddt          |           ddgdddt	                      t          j                   n`# t          t          j        t          f$ rA 	 t          j
        | t          j                   n# t          t          t          f$ r Y nw xY wY nw xY wdS d	dl}	 |                    |           }|                    d
          D ]'}	 |                                 # |j        $ r Y $w xY w|                                 dS # |j        $ r Y dS t          t          f$ rC 	 t          j
        | t          j                   Y dS # t          t          t          f$ r Y Y dS w xY ww xY w)u  Terminate a host-visible PID and its descendants.

        POSIX: walks the process tree with ``psutil`` and SIGTERMs
        children before the parent so subprocess trees (e.g. Chromium
        renderers/GPU helpers spawned by an ``agent-browser`` daemon)
        don't get reparented to init and survive cleanup.

        Windows: shells out to ``taskkill /PID <pid> /T /F``. This is
        the documented Microsoft primitive for tree-kill and matches the
        existing convention in ``gateway.status.terminate_pid``. We can't
        reuse the POSIX psutil path on Windows because:

          1. Windows doesn't maintain a Unix-style process tree —
             ``psutil.Process.children(recursive=True)`` walks PPID
             links that go stale when intermediate processes exit, so
             enumeration is best-effort and misses orphaned descendants.
          2. ``psutil.Process.terminate()`` on Windows is
             ``TerminateProcess()`` which kills only the target handle
             and is a hard kill — there is no Windows equivalent of a
             SIGTERM that cascades through a process group. (See the
             warning in ``gateway/status.py::terminate_pid``: "os.kill
             with SIGTERM is not equivalent to a tree-killing hard stop"
             on Windows.) Headless Chromium has no GUI window, so the
             softer ``taskkill /T`` without ``/F`` won't reach it either.

        ``psutil`` is a hard dependency (see ``pyproject.toml``); the
        bare-``os.kill`` fallback covers OSError / PermissionError on
        POSIX and a missing ``taskkill.exe`` on Windows (effectively
        unreachable on real Windows installs, but cheap insurance).
        taskkillz/PIDz/Tz/FTr   )capture_outputrp   timeoutcreationflagsstdinNr   	recursive)_IS_WINDOWSrS   runrQ   r   DEVNULLFileNotFoundErrorTimeoutExpiredOSErroroskillsignalSIGTERMProcessLookupErrorPermissionErrorpsutilProcesschildren	terminateNoSuchProcess)r*   r   parentchilds       r    _terminate_host_pidz#ProcessRegistry._terminate_host_pid  s   @  	S4>#'"4"6"6$,     &z'@'J   GC0000!3_E   D
 F	^^C((F488  OO%%%%+   D# 	 	 	FF) 	 	 	V^,,,,,,/A   	s   AA B))B	B)	B# B)"B##B)(B)3-D !C65D 6
D D DD 
E<)E<:EE83E<7E88E<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
isinstancerQ   
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          |||t	          |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 # t:          $ r t<                              d           Y n1t@          $ r%}t<                              d|           Y d}~nd}~ww xY wt                      }	t          t
          j        |          }d|d<   t          rdtC                      ini }tE          j#        |	d	d
| gfd|j        |ddtD          j$        tD          j%        tD          j&        t          rdnt
          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   |                                  n# t@          $ r 	 t          s	 tU          tV          dtV          j,                  }t          j-        t          j.        |j                  |           nE# t^          t`          tb          f$ r |2                                 Y nw xY w|2                                 n# t@          $ r Y nw xY w	 |3                    d           n# t@          $ r Y nw xY w w xY w|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 moder   utf-8replace)	rp   r-   r   encodingerrorsstdoutstderrr   
preexec_fnzproc-reader-SIGKILL   r   )4r$   uuiduuid4hexr   r   getcwdr   r   winptyr   
ptyprocessr   r   environspawnr-   r*   rL   rY   r[   _pty_reader_loopr%   rK   startrJ   _prune_if_neededr`   _write_checkpointImportErrorr   warningr   r   rS   rT   PIPESTDOUTr   setsidr+   _reader_loopr   r   r   killpggetpgidr   r   r   r   wait)rm   r&   r-   r(   r)   r   r   r~   _PtyProcessCls
user_shellpty_envpty_procreaderebg_env_popen_kwargsprockill_signals                     r    spawn_localzProcessRegistry.spawn_local  s   & !.tz||',..#!#"455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??%(!"CNV*<*>*>??TV!5G!5!56
?$$*9tt	
 
 
 
 h#	%(Z0GJ00	  F &,G"LLNNN 4 4%%''',3gj)4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ""$$$$ 	 	 	
"  $&-fi&P&P	"*TX"6"6DDDD.I $ $ $		$ IIKKK   		!	$$$$   '	* s   ,C E= ,$EE= E  E= #E $E= =$G#	G,GGAL $K>2L >LL LL 
O>)O1AM>=O>+N,)O+N,,OO>
OO>OO>O,+O>,
O96O>8O99O>r   r   c                    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                                          }|                                D ]@}|                                }|                                rt          |          |_         nA|j        nd|_        t          |
                    dd                    |_        |j        dk    rd|_        |
                    dd                                          |_        n/# 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        s|| j        |j        <   ddd           n# 1 swxY w Y   |j        s|                                  |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,   r6   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 F)r   rewrite_compound_backgroundr   r'   T
returncoder   zFailed to start: zproc-poller-r   )r$   r   r   r   r   r   r%   shlexquoteexecutegetstripr   isdigitr   r*   r0   r1   r2   r   rY   r[   _env_poller_looprK   r   rJ   r   r`   r   )rm   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 		<[[,1 !  F
 ZZ"--3355F))++  zz||<<>> "%d))GKE {"!%$'

<(D(D$E$E!$))(*G%(.

8R(@(@(F(F(H(H% 	< 	< 	<!GN "G$;$;$;G!!!!!!	<
 ~ 		%,sHh	B0GJ00	  F &,G"LLNNNZ 	4 	4!!###> 4,3gj)	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4
 ~ 	%""$$$s+   ?D
H
 

H6H11H6+KK
K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: %sr   r   z$Process wait timed out or failed: %s)r+   r   readr}   rJ   r2   r   r3   r   r   r   r   r	  r0   r  r1   r   )rm   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  r0   r   sleepr  r  r   rJ   r2   r3   r   r  r   r   r1   
ValueError
IndexErrorr   r   )rm   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  )rL   isaliver.  r   rQ   decoderJ   r2   r   r3   r   EOFErrorr   r   r   r	  r0   hasattrrA  r1   r   )rm   r~   ptyr0  rp   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   |                                  |re|j        r`ddlm} |j	        r ||j	        dd                   nd}| j
                            d|j        |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&   r1   r   )rJ   r`   r{   r%   ra   r   r>   tools.ansi_striprI  r2   re   r   r)   r&   r1   )rm   r~   was_runningrI  output_tails        r    r   z!ProcessRegistry._move_to_finished_  sO    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&2"?$.%' '     
	 
	 
	 
	s   2AA
A
r   c                     || j         v S )zJCheck if a completion notification was already consumed via wait/poll/log.)rg   )rm   r   s     r    is_completion_consumedz&ProcessRegistry.is_completion_consumed|  s    T666r"   list[tuple[dict, str]]c                    g }| j                                         s	 | j                                         }n# t          $ r Y nw xY w|                    dd          }|                    d          dk    r|                     |          rt          |          }|r|                    ||f           | j                                         |S )zPop all pending notification events and return formatted pairs.

        Returns a list of (raw_event, formatted_text) tuples.
        Skips completion events that were already consumed via wait/poll/log.
        r   r'   r   rK  )re   empty
get_nowaitr   r  rP  format_process_notificationr   )rm   resultsevt_evt_sidrp   s        r    drain_notificationsz#ProcessRegistry.drain_notifications  s     '--// 
	,+6688   ww|R00Hwwv,..43N3Nx3X3X..s33D ,T{+++ '--// 
	, s   7 
AA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)rJ   r`   r  ra   r   )rm   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.)r0   r   pollr   r   fcntlfilenoF_GETFLF_SETFLr   
O_NONBLOCKr.  r   rQ   rC  BlockingIOErrorr   r6  r   r   r%   rJ   r2   r   r3   r1   infor   )rm   r~   r  rcdrainedr   r]  fdflagsr0  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   rH  N	not_foundNo process with ID statuserrorr'   r0   running)r   r&   rm  r*   uptime_secondsoutput_previewr1   Tr4   z=Process recovered after restart -- output history unavailablenote)rL  rI  r  rh  rJ   r2   r%   r&   r0   r*   r   r   r/   r1   rg   addr4   )rm   r   rI  r~   rr  r*  s         r    r\  zProcessRegistry.poll  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   rH  Nrj  rk  rl  r0   rp  rr   z lines)r   rm  r   total_linesshowing)rL  rI  r  rJ   r2   r   r   r%   r0   r|   rg   rt  )rm   r   rv  rw  rI  r~   full_outputrv   ry  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   rH  )is_interruptedTERMINAL_TIMEOUT180   NzRequested wait of z%s was clamped to configured limit of r   rj  rk  rl  r0   rJ  )rm  r1   r   timeout_noteinterruptedro  z+User sent a new message -- wait interrupted)rm  r   rs  r   r   )rm  r   zWaited zs, process still running)rL  rI  tools.interruptr  r   r   getenvr6  	TypeErrorr  r   	monotonicr   rh  r0   rg   rt  r1   r2   r5  )rm   r   r   rI  _is_interrupteddefault_timeoutmax_timeoutrequested_timeoutr  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| dS |j        r
d|j        dS 	 |j        rZ	 |j                            d           n# t
          $ r/ |j        r$t          j        |j        t          j
                   Y nw xY w|j        r	 t          r|j                                         nd	dl}	 |                    |j        j                  }|                    d
          D ]'}	 |                                 # |j        $ r Y $w xY w|                                 n# |j        $ r Y nw xY w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t;          |          dcY d}~S d}~ww xY w)zKill a background process.Nrj  rk  rl  already_exited)rm  r1   T)forcer   r   zkill r3  r   r   r5   rn  zkRecovered process cannot be killed after restart because its original runtime handle is no longer availableikilled)rm  r   )r  r0   r1   rL   r   r   r*   r   r   r   r   r+   r   r   r   r   r   r   r   r,   r  r4   r6   r   rJ   r   r   r   r%   rQ   )rm   r   r~   r   r   r   r  s          r    kill_processzProcessRegistry.kill_processm  s   ((:&&?)4V*4V4VWWW> 	*$.  7	8| /=L***6666  = = ={ =V^<<<=  (+" !113333%	!%+^^GO4G%H%HF)/4)H)H ) )!)$)OO$5$5$5$5'-'; !) !) !)$(D!)",,....%3 ! ! ! D!*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   J	 A J	 5BJ	 B
J	 %E 7D3 9DD3 
DD3 DD3 2E 3
E =E ?E  E J	 *E2/J	 1E22A1J	 #G>2J	 >HJ	 H!J	 (J	 A J	 	
J/J*$J/*J/datac                 .   |                      |          }|dd| dS |j        rdddS t          |d          r|j        r	 t          r:t          |t                    r|                    d          nt          |          }n,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).Nrj  rk  rl  r  Process has already finishedrL   r   ok)rm  bytes_writtenrn  ?Process stdin not available (non-local backend or stdin closed))r  r0   rE  rL   r   r   bytesrC  rQ   encodewriter   r   r+   r   flush)rm   r   r  r~   pty_datar  s         r    write_stdinzProcessRegistry.write_stdin  s   ((:&&?)4V*4V4VWWW> 	Y.9WXXX 7F## 
	< 
	<	< W7A$7N7N]t{{7333TWX\T]T]HH7A$7L7LVt{{7333RVH""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>   BC 
D'C>8D>DAE. .
F8F	FFc                 4    |                      ||dz             S )zGSend data + newline to a running process's stdin (like pressing Enter).rr   )r  )rm   r   r  s      r    submit_stdinzProcessRegistry.submit_stdin  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.Nrj  rk  rl  r  r  rL   r  zEOF sent)rm  r   rn  r  zstdin closed)
r  r0   rE  rL   sendeofr   rQ   r+   r   close)rm   r   r~   r  s       r    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                 N    	 t          | j                  S # t          $ r Y dS w xY w)ak  Return the count of currently-running background processes.

        Cheap O(1) read of the running dict, suitable for status-bar polling
        on every render tick. CPython dict ``len()`` is atomic; callers do not
        need to hold ``self._lock``. Reflects ``_running`` only: sessions are
        moved to ``_finished`` when their subprocess exits.
        r   )r   r`   r   )rm   s    r    count_runningzProcessRegistry.count_running  s9    	t}%%% 	 	 	11	s    
$$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 r\   )r   )rt   r   rm   s     r    
<listcomp>z1ProcessRegistry.list_sessions.<locals>.<listcomp>  s'    PPPa66q99PPPr"   c                 *    g | ]}|j         k    |S r\   r(   rt   r   r(   s     r    r  z1ProcessRegistry.list_sessions.<locals>.<listcomp>  s%    LLL!qyG7K7KA7K7K7Kr"   ru  z%Y-%m-%dT%H:%M:%Sr0   rp  i8r'   )r   r&   r-   r*   r/   rq  rm  rr  r1   Tr4   )rJ   rX   r`   valuesra   r%   r&   r-   r*   r   strftime	localtimer/   r   r0   r2   r1   r4   r   )rm   r(   all_sessionsr*  r   entrys   ``    r    list_sessionszProcessRegistry.list_sessions  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(   r0   r  s     r    rw   z7ProcessRegistry.has_active_processes.<locals>.<genexpr>"  sG         	W$5QX     r"   rJ   rX   r`   r  r   ry   )rm   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)   r0   )rt   r   r)   s     r    rw   z9ProcessRegistry.has_active_for_session.<locals>.<genexpr>0  sG         ,=QX     r"   r  )rm   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>8  s;       OqyG';';QX'; ';';';r"   Nr   rm  >   r  r  r   )rJ   r`   r  r  r%   r  )rm   r(   targetsr  r~   r*  s    `    r    kill_allzProcessRegistry.kill_all5  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 r\   )r/   FINISHED_TTL_SECONDS)rt   sidr   r   s      r    r  z4ProcessRegistry._prune_if_needed.<locals>.<listcomp>J  s:     
 
 
Cal"&::: :::r"   c                 (    j         |          j        S r  )ra   r/   )r  rm   s    r    <lambda>z2ProcessRegistry._prune_if_needed.<locals>.<lambda>U  s    DN3<O<Z r"   )keyN)
r   ra   itemsrg   discardr   r`   MAX_PROCESSESminkeys)rm   expiredr  total	oldest_idtrackedstaler   s   `      @r    r   z ProcessRegistry._prune_if_neededF  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                 d   	 | 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|j                   	 ddd           n# 1 swxY w Y   ddlm}  |t0          |           dS # t2          $ r(}t4                              d|d           Y d}~dS d}~ww xY w)z=Write running process metadata to checkpoint file atomically.r   r&   r*   r6   r-   r/   r(   r)   r7   r8   r9   r:   r;   r<   r=   r>   r@   Nr   )atomic_json_writez#Failed to write checkpoint file: %sT)exc_info)rJ   r`   r  r0   r   r%   r&   r*   r6   r-   r/   r(   r)   r7   r8   r9   r:   r;   r<   r=   r>   r@   utilsr  CHECKPOINT_PATHr   r   r   )rm   entriesr   r  r  s        r    r   z!ProcessRegistry._write_checkpointc  s    	R  --//  A8  ((!$(%qy( "15( (	(
 "15( )!,( &qy( *1=( /0B( .q/@( .q/@( 01D( 01D( 1!2F( /0B(  1!2F!(" -a.>#(                 2 0/////ow77777 	R 	R 	RLL>DLQQQQQQQQQ	Rs;   C= CCC= CC=  C!C= =
D/D**D/c                 Z   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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    rW| j                            |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*   r6   r5   z=Skipping recovery for non-host process: %s (pid=%s, scope=%s)r&   unknownNr   r%   r   r(   r'   r)   r-   r/   r4   Tr7   r8   r9   r:   r;   r<   r=   r>   Fr@   r   z'Recovered detached process: %s (pid=%d))
r   check_intervalr)   r   r   r   r   r   r   r>   r\   )r  existsjsonloads	read_textr   r  r   rc  r   r$   r   rJ   r`   r%   r&   r=   rb   r   r)   r7   r8   r9   r:   r;   r<   r>   r   )rm   r  	recoveredr  r*   r6   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 (-yy1Er'J'J'J  &+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   rQ  )r~   r$   r   N)r   ru  r  )r'   )0rM   rN   rO   rP   rz   ro   staticmethodrQ   r}   r$   r   rU   rV   r   r   r   r   r   r   r
   r   dictr  r+  r  r   r   r   rP  rY  r  rh  r\  r}  r	  r  r  r  r  r  rX   r  r  r  r  r   r   r  r\   r"   r    r^   r^      s        ; ; ;8           \ ~^ ~s ~t ~ ~ ~ ~@Uu U U U U Un    $       \ .1I hWeNf    & ? ? ? ? ? \?F 
3 
3 
 
 
 \
 G GG G 	G
 G G G 
G G G GZ ^ ^^ ^ 	^
 ^ ^ ^ 
^ ^ ^ ^D,N , , , ,83%3,/3;>3JM3Z]3 3 3 3j( ( ( ( (>    :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D8s D8t D8 D8 D8 D8L8c 8 8 8 8 8 8>9 9s 9# 9t 9 9 9 98c 8d 8 8 8 8.s     S D    >C D    # $      s    "/ / /:R R RBO O O O O O Or"   r^   rW  z
str | Nonec           	         |                      dd          }|                      dd          }|                      dd          }|dk    rd|                      dd	           d
S |dk    rb|                      dd          }|                      dd	          }|                      dd          }d| d| d| d| }|r	|d| dz  }|d
z  }|S |                      dd          }|                      dd	          }d| d| d| d| d
	S )zFormat a process notification event into a [IMPORTANT: ...] message.

    Handles completion events (notify_on_complete), watch pattern matches,
    and watch disabled events from the unified completion_queue.
    r   rK  r   r  r&   r   z[IMPORTANT: r   r'   ]r   r   ?r   r   r   z[IMPORTANT: Background process z matched watch pattern "z".
Command: z
Matched output:
z
(z/ earlier matches were suppressed by rate limit)r1   z completed (exit code z).
Command: z	
Output:
)r  )	rW  evt_type_sid_cmd_pat_out_suprp   _exits	            r    rU  rU    s    wwv|,,H77<++D779i((D###7cggi447777=  wwy#&&wwx$$ww|Q'''d ' '#' '' ' !%' ' 	  	PO$OOOODGGK%%E778R  D	$ 	 		 		 	 	 	 	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)rX   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   r  r   rv  rw  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   rX   	processesr  F)ensure_ascii>   r  r   r\  r	  r  r  r  zsession_id is required for r\  r  rv  r   rw  ru  )rv  rw  r	  r   r   r   r  r  r  r  zUnknown process action: z8. Use: list, poll, log, wait, kill, write, submit, close)r  rQ   r  dumpsprocess_registryr  r  r\  r}  r	  r  r  r  r  )r   kwr(   r  r   s        r    _handle_processr  2  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)7rP   r  loggingr   r   r  r   rS   rY   r   r   systemr   tools.environments.localr   r   r   hermes_cli._subprocess_compatr   dataclassesr   r	   typingr
   r   r   r   hermes_cli.configr   	getLoggerrM   r   r  rW   r  r  r   r   r   r   r   r   rQ   r!   r$   r^   r  r  rU  tools.registryr  r  PROCESS_SCHEMAr  registerr\   r"   r    <module>r     s0   >   				             ho9, ] ] ] ] ] ] ] ] ] ] < < < < < < ( ( ( ( ( ( ( ( , , , , , , , , , , , , - - - - - -		8	$	$ "/##&66        !    "       ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0^J J J J J J J J\* #?$$ #T #l # # # #R 0 / / / / / / / 	_  ![[[J  !~ 
 !a 
 "k  "W 
 "E /
 
: J?   * *Zs s s:  	
     r"   