
    
Bje                        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mZmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ ej$                  dk(  rddlZnddlZd	Zd
ZdZej$                  dk(  Z e       ZdZdadZde
fdZdadee
   de
fdZde
fdZ de
fdZ!de"fdZ#ddde$de%ddfdZ&de"de"fdZ'de"de"de
fdZ(de$dee$   fdZ)de$dee$   fdZ*de$dee"   fd Z+de$de%fd!Z,d"e-e"ef   de%fd#Z.de-fd$Z/de-e"ef   fd%Z0d&e
dee-e"ef      fd'Z1d&e
d(e-e"ef   ddfd)Z2dadee
   dee-   fd*Z3dad+ee
   dee-e"ef      fd,Z4d"ee-e"ef      dee$   fd-Z5de
d.e%ddfd/Z6dbd0Z7de%fd1Z8de$de%fd2Z9dbd3Z:de%fd4Z;dbd5Z<dad+ee
   de%fd6Z=dbd7Z>eeeeeeeed8d9ed:ed;ed<ed=ed>ed?ed@eddfdAZ?dee-e"ef      fdBZ@dbdCZAdade"de"dDee-e"ef      deBe%ee-e"ef      f   fdEZCde"de"ddfdFZDdddGdHee$   dIee$   de$fdJZEdKZFdLZGdMZHdLZIde
fdNZJde
fdOZKdPe"dQe$de%fdRZLd&e
dSe"dTe"dQe$de%f
dUZMdVe$de%fdWZNde%fdXZOdbdYZPdVe$de%fdZZQde%fd[ZRdbd\ZS	 dad]d^dee
   d.e%dee$   fd_ZT	 dad]d^dee
   d.e%de%fd`ZUy)cu  
Gateway runtime status helpers.

Provides PID-file based detection of whether the gateway daemon is running,
used by send_message's check_fn to gate availability in the CLI.

The PID file lives at ``{HERMES_HOME}/gateway.pid``.  HERMES_HOME defaults to
``~/.hermes`` but can be overridden via the environment variable.  This means
separate HERMES_HOME directories naturally get separate PID files — a property
that will be useful when we add named profiles (multiple agents running
concurrently under distinct configurations).
    N)datetimetimezone)Pathget_hermes_home)AnyOptionalatomic_json_writewin32hermes-gatewayzgateway_state.jsonzgateway-lockszgateway.lock   returnc                       t               } | dz  S )z@Return the path to the gateway PID file, respecting HERMES_HOME.zgateway.pidr   homes    3/home/ubuntu/.hermes/hermes-agent/gateway/status.py_get_pid_pathr   ,   s    D-    pid_pathc                 V    | | j                  t              S t               }|t        z  S )z1Return the path to the runtime gateway lock file.)	with_name_GATEWAY_LOCK_FILENAMEr   )r   r   s     r   _get_gateway_lock_pathr   2   s.    !!"899D(((r   c                  <    t               j                  t              S )z5Return the persisted runtime health/status file path.)r   r   _RUNTIME_STATUS_FILE r   r   _get_runtime_status_pathr   :   s    ?$$%9::r   c                      t        j                  d      } | rt        |       S t        t        j                  dt        j                         dz  dz              }|dz  t        z  S )zBReturn the machine-local directory for token-scoped gateway locks.HERMES_GATEWAY_LOCK_DIRXDG_STATE_HOMEz.localstatehermes)osgetenvr   r   _LOCKS_DIRNAME)override
state_homes     r   _get_lock_dirr)   ?   sT    yy23HH~bii 0$))+2H72RSTJ >11r   c                  d    t        j                  t        j                        j	                         S N)r   nowr   utc	isoformatr   r   r   _utc_now_isor/   H   s    <<%//11r   F)forcepidr0   c                   |r|t         rv	 t        j                  ddt        |       ddgddd      }|j                  d	k7  r>|j                  xs |j                  xs d
j                         }t        |xs d|        y|st        j                  nt        t        dt        j                        }t        j                  | |       y# t        $ r' t        j                  | t        j                         Y yw xY w)zTerminate a PID with platform-appropriate force semantics.

    POSIX uses SIGTERM/SIGKILL. Windows uses taskkill /T /F for true force-kill
    because os.kill(..., SIGTERM) is not equivalent to a tree-killing hard stop.
    taskkillz/PIDz/Tz/FT
   capture_outputtexttimeoutNr    ztaskkill failed for PID SIGKILL)_IS_WINDOWS
subprocessrunstrFileNotFoundErrorr$   killsignalSIGTERM
returncodestderrstdoutstripOSErrorgetattr)r1   r0   resultdetailssigs        r   terminate_pidrL   L   s     		^^VSXtT:#	F !}};;BBDG'E'?u%EFF %&..769fnn+UCGGC ! 	GGC(	s   'C -C76C7identityc                 l    t        j                  | j                  d            j                         d d S )Nutf-8   )hashlibsha256encode	hexdigest)rM   s    r   _scope_hashrU   g   s*    >>(//'23==?DDr   scopec                 :    t               |  dt        |       dz  S )N-z.lock)r)   rU   )rV   rM   s     r   _get_scope_lock_pathrY   k   s"    ?waH(='>eDDDr   c                     t        d|  d      }	 t        |j                  d      j                         d         S # t        t
        t        t        t        f$ r Y yw xY w)z:Return the kernel start time for a process when available./proc/z/statrO   encoding   N)	r   int	read_textsplitr?   
IndexErrorPermissionError
ValueErrorrG   )r1   	stat_paths     r   _get_process_start_timerf   o   s^    vcU%()I9&&&8>>@DEEz?JP s   += AAc                     t        |       S )zBPublic wrapper for retrieving a process start time when available.)rf   )r1   s    r   get_process_start_timerh   y   s    "3''r   c                    t        d|  d      }	 |j                         }|r1|j                  dd      j                  dd      j	                         S 	 t        j                  dd	t        |       d
dgddd      }|j                  dk(  r4|j                  j	                         r|j                  j	                         S y# t
        t        t        f$ r Y w xY w# t        t        j                  f$ r Y yw xY w)zReturn the process command line as a space-separated string.

    On Linux, reads /proc/<pid>/cmdline directly.  On macOS and other
    platforms without /proc, falls back to ``ps -p <pid> -o command=``.
    r[   z/cmdline        rO   ignore)errorspsz-pz-ozcommand=T   r5   r   N)r   
read_bytesreplacedecoderF   r?   rc   rG   r<   r=   r>   rC   rE   TimeoutExpired)r1   cmdline_pathrawrI   s       r   _read_process_cmdlinerv   ~   s     &X./LW%%' ;;w-44WX4NTTVV
4S44	
 !fmm&9&9&;==&&(( % 8  Z../ s$   C  A)C  CCC65C6c                 L    t        |       syd}t        fd|D              S )zBReturn True when the live PID still looks like the Hermes gateway.F)hermes_cli.main gatewayhermes_cli/main.py gatewayhermes gatewayr   gateway/run.pyc              3   &   K   | ]  }|v  
 y wr+   r   .0patterncmdlines     r   	<genexpr>z._looks_like_gateway_process.<locals>.<genexpr>        :gw'!:   )rv   any)r1   patternsr   s     @r   _looks_like_gateway_processr      s-    #C(GH ::::r   recordc                     | j                  d      t        k7  ry| j                  d      }t        |t              r|sydj	                  d |D              d}t        fd|D              S )zMValidate gateway identity from PID-file metadata when cmdline is unavailable.kindFargv c              3   2   K   | ]  }t        |        y wr+   )r>   )r~   parts     r   r   z-_record_looks_like_gateway.<locals>.<genexpr>   s     2Ts4y2s   )rx   ry   rz   r{   c              3   &   K   | ]  }|v  
 y wr+   r   r}   s     r   r   z-_record_looks_like_gateway.<locals>.<genexpr>   r   r   )get_GATEWAY_KIND
isinstancelistjoinr   )r   r   r   r   s      @r   _record_looks_like_gatewayr      sb    zz&]*::fDdD!hh2T22GH ::::r   c                      t        j                         t        t        t        j
                        t        t        j                               dS )N)r1   r   r   
start_time)r$   getpidr   r   sysr   rf   r   r   r   _build_pid_recordr      s1    yy{SXX-biik:	 r   c            	      Z    t               } | j                  dd ddi t               d       | S )NstartingFr   )gateway_stateexit_reasonrestart_requestedactive_agents	platforms
updated_at)r   updater/   )payloads    r   _build_runtime_status_recordr      s6    !GNN#""n  Nr   pathc                    | j                         sy 	 | j                  d      j                         }|sy 	 t	        j
                  |      }t        |t              r|S d S # t        $ r Y y w xY w# t        j                  $ r Y y w xY w)NrO   r\   )	existsr`   rF   rG   jsonloadsJSONDecodeErrorr   dict)r   ru   r   s      r   _read_json_filer      s    ;;=nngn.446 **S/ !$/79T9    s"    A  A/  	A,+A,/BBr   c                 "    t        | |d d       y )N),:)indent
separatorsr
   )r   r   s     r   _write_json_filer      s    dGDZHr   c                    | xs
 t               } | j                         sy 	 | j                         j                         }|sy 	 t        j                  |      }t        |t              rd|iS t        |t              r|S y # t        $ r Y y w xY w# t
        j                  $ r! 	 dt        |      icY S # t        $ r Y Y y w xY ww xY wNr1   )r   r   r`   rF   rG   r   r   r   r_   rd   r   r   )r   ru   r   s      r   _read_pid_recordr      s    *=?H??  "((* **S/ '3w'4 %    	3s8$$ 		sA   A? B ?	B
BC"B1.C1	B>:C=B>>C	lock_pathc                 0    t        | xs
 t                     S r+   )r   r   )r   s    r   _read_gateway_lock_recordr     s    IA)?)ABBr   c                 Z    | sy 	 t        | d         S # t        t        t        f$ r Y y w xY wr   )r_   KeyError	TypeErrorrd   )r   s    r   _pid_from_recordr   	  s5    6%=!!i, s    **cleanup_stalec                    |sy	 | j                  d       	 t        |       j                  d       y# t        $ r Y (w xY w# t        $ r Y yw xY w)a  Delete a stale gateway PID file (and its sibling lock metadata).

    Called from ``get_running_pid()`` after the runtime lock has already been
    confirmed inactive, so the on-disk metadata is known to belong to a dead
    process.  Unlike ``remove_pid_file()`` (which defensively refuses to delete
    a PID file whose ``pid`` field differs from ``os.getpid()`` to protect
    ``--replace`` handoffs), this path force-unlinks both files so the next
    startup sees a clean slate.
    NT
missing_ok)unlink	Exceptionr   r   r   s     r   _cleanup_invalid_pid_pathr     s_     4(x(//4/@    s   4 A 	A A 	AAc                    | j                  d       | j                          t        j                  t	               |        | j                          	 t        j                  | j                                y # t        $ r Y y w xY w)Nr   )
seektruncater   dumpr   flushr$   fsyncfilenorG   handles    r   _write_gateway_lock_recordr   (  s[    
KKN
OOII!6*
LLN
! s   #A5 5	B Bc                    	 t         r| j                  dt        j                         | j	                         dk(  r!| j                  d       | j                          | j                  t               t        j                  | j                         t        j                  d       yt        j                  | j                         t        j                  t        j                  z         y# t         t"        f$ r Y yw xY w)Nr   
   TF)r;   r   r$   SEEK_ENDtellwriter   _WINDOWS_LOCK_OFFSETmsvcrtlockingr   LK_NBLCKfcntlflockLOCK_EXLOCK_NBBlockingIOErrorrG   r   s    r   _try_acquire_file_lockr   3  s    KK2;;'{{}!T"KK,-NN6==?FOOQ?  KK)FGW% s   B"C) %AC) )C;:C;c                    	 ddl }t        |j                  t        |                   S # t        $ r Y nw xY wt
        r	 ddl}|j                  j                  }|j                  |j                  _        |j                  |j                  _        |j                  |j                  _        d}d}d}d}d}|j                  ||z  dt        |             }	|	s|j                         }
|
|k(  ry|
|k(  ry	y	 |j                  |	d      }||k(  |j                  |	       S # |j                  |	       w xY w# t         t"        f$ r Y yw xY w	 t%        j&                  t        |       d       y	# t(        $ r Y yt*        $ r Y y	t         $ r Y yw xY w)
u  Cross-platform "is this PID alive" check that does NOT kill the target.

    CRITICAL on Windows: Python's ``os.kill(pid, 0)`` is NOT a no-op like it
    is on POSIX. CPython's Windows implementation
    (``Modules/posixmodule.c::os_kill_impl``) treats ``sig=0`` as
    ``CTRL_C_EVENT`` because the two values collide at the C level, and
    routes it through ``GenerateConsoleCtrlEvent(0, pid)`` — which sends
    a Ctrl+C to the entire console process group containing the target
    PID, not just the PID itself. Any caller that wanted to "check if
    this PID is alive" via ``os.kill(pid, 0)`` on Windows was silently
    killing that process (and often unrelated processes in the same
    console group). Long-standing Python quirk; see bpo-14484.

    Implementation: prefer :mod:`psutil` (hard dependency — the canonical
    cross-platform answer, maintained by Giampaolo Rodolà, uses
    ``OpenProcess + GetExitCodeProcess`` on Windows internally). Fall back
    to a hand-rolled ctypes ``OpenProcess`` / ``WaitForSingleObject`` pair
    on Windows + ``os.kill(pid, 0)`` on POSIX if psutil is somehow
    unavailable — e.g. stripped-down install or import error during the
    scaffold phase before ``psutil`` is pip-installed.
    r   Ni   r   i  W   ro   FT)psutilbool
pid_existsr_   ImportErrorr;   ctypeswindllkernel32c_void_pOpenProcessrestypec_uintWaitForSingleObjectGetLastErrorCloseHandlerG   AttributeErrorr$   r@   ProcessLookupErrorrc   )r1   r   r   r   !PROCESS_QUERY_LIMITED_INFORMATIONSYNCHRONIZEWAIT_TIMEOUTERROR_INVALID_PARAMETERERROR_ACCESS_DENIEDr   errwait_results               r   _pid_existsr   C  s   ,F%%c#h/00  	}}--H ,2??H  (39==H((0,2MMH!!)06-"K%L&(#"#))1K?CF ++-11 ---&::61E #l2$$V,$$V,( 				GGCHa ! 	 	 		s[   &) 	55 B+D2 ,D2 4D 
D2 D//D2 2EEE( (	F3F=FFc                 &   	 t         rI| j                  t               t        j                  | j                         t        j                  d       y t        j                  | j                         t        j                         y # t        $ r Y y w xY w)Nr   )r;   r   r   r   r   r   LK_UNLCKr   r   LOCK_UNrG   r   s    r   _release_file_lockr     s[    KK,-NN6==?FOOQ?KK7 s   AB 2B 	BBc                      t         yt               } | j                  j                  dd       t	        | dd      }t        |      s|j                          yt        |       |a y)zClaim the cross-process runtime lock for the gateway.

    Unlike the PID file, the lock is owned by the live process itself. If the
    process dies abruptly, the OS releases the lock automatically.
    Tparentsexist_oka+rO   r\   F)_gateway_lock_handler   parentmkdiropenr   closer   )r   r   s     r   acquire_gateway_runtime_lockr    s_     '!#DKKdT2$w/F!&)v&!r   c                  p    t         } | yda t        |        	 | j                          y# t        $ r Y yw xY w)z<Release the gateway runtime lock when owned by this process.N)r  r   r  rG   r   s    r   release_gateway_runtime_lockr	    s?     "F~v s   ) 	55c                    | xs
 t               }t        |t               k(  ry|j                         syt        |dd      }	 t	        |      rt        |       	 	 |j                          y	 	 |j                          y# t        $ r Y yw xY w# t        $ r Y yw xY w# 	 |j                          w # t        $ r Y w w xY wxY w)zFReturn True when some process currently owns the gateway runtime lock.TFr  rO   r\   )r   r  r   r  r   r   r  rG   )r   resolved_lock_pathr   s      r   is_gateway_runtime_lock_activer    s     #>&<&>',>BXBZ,Z$$&$dW=F	!&)v&	LLN 	LLN 		w 			LLN 		sT   B A? .B ?	B
B	BBB?B0/B?0	B<9B?;B<<B?c                     t               } | j                  j                  dd       t        j                  t                     }	 t        j                  | t        j                  t        j                  z  t        j                  z        }	 t        j                  |dd      5 }|j                  |       ddd       y# t        $ r  w xY w# 1 sw Y   yxY w# t        $ r$ 	 | j                  d        # t         $ r Y  w xY ww xY w)zWrite the current process PID and metadata to the gateway PID file.

    Uses atomic O_CREAT | O_EXCL creation so that concurrent --replace
    invocations race: exactly one process wins and the rest get
    FileExistsError.
    Tr   wrO   r\   Nr   )r   r  r  r   dumpsr   r$   r  O_CREATO_EXCLO_WRONLYFileExistsErrorfdopenr   r   r   rG   )r   r   fdfs       r   write_pid_filer    s     ?DKKdT2ZZ)+,FWWT2::		1BKK?@YYr31 	QGGFO	 	  	 	 	KK4K( 	  		s[   AC  C %C7C  CCC C 	D$C76D7	D DDD)r   r   r   r   platformplatform_state
error_codeerror_messager   r   r   r   r  r  r  r  c                 B   t               }t        |      xs
 t               }	t               }
|	j	                  di        |
d   |	d<   |
d   |	d<   |
d   |	d<   |
d   |	d<   t               |	d<   | t        ur| |	d<   |t        ur||	d<   |t        urt        |      |	d	<   |t        urt        d
t        |            |	d<   |t        urQ|	d   j                  |i       }|t        ur||d<   |t        ur||d<   |t        ur||d<   t               |d<   ||	d   |<   t        ||	       y)zBPersist gateway runtime health information for diagnostics/status.r   r   r1   r   r   r   r   r   r   r   r   r"   r  r  N)r   r   r   r   
setdefaultr/   _UNSETr   maxr_   r   r   )r   r   r   r   r  r  r  r  r   r   current_recordplatform_payloads               r   write_runtime_statusr"    sV    $%Dd#E'C'EG&(N{B'$V,GFO#E*GEN$V,GFO*<8GL(NGLF"#0 & !,&'+,='>#$F"#&q#m*<#= v";/33HbA'(6W%V#-7\*&0=_-)5&)9X&T7#r   c                  (    t        t                     S )z=Read the persisted gateway runtime health/status information.)r   r   r   r   r   read_runtime_statusr$    s    3566r   c                      	 t               } t        |       }|)	 t        |d         }||t        j                         k7  ry| j                  d       y# t        t        t
        f$ r d}Y Ew xY w# t        $ r Y yw xY w)ah  Remove the gateway PID file, but only if it belongs to this process.

    During --replace handoffs, the old process's atexit handler can fire AFTER
    the new process has written its own PID file.  Blindly removing the file
    would delete the new process's record, leaving the gateway running with no
    PID file (invisible to ``get_running_pid()``).
    Nr1   Tr   )
r   r   r_   r   r   rd   r$   r   r   r   )r   r   file_pids      r   remove_pid_filer'    s     & ve}- #BIIK(?t$ i4     s9   A1 A A1 A1 A.+A1 -A..A1 1	A=<A=metadatac                 ,   t        | |      }|j                  j                  dd       i t               | t	        |      |xs i t               d}t        |      }|#|j                         r	 |j                  d       |rh	 t        |d         }|t        j                         k(  r3|j!                  d      |j!                  d      k(  rt#        ||       d|fS |du }|st%        |      sd}nt'        |      }|j!                  d      |||j!                  d      k7  rd}|s8|j!                  d      '|%t)        |      st+        |      }	|	t-        |      sd}|sq	 t/        d| d	      }
|
j                         rQ|
j1                  d
      j3                         D ].  }|j5                  d      s|j7                         d   }|dv rd} n |r	 |j                  d       nd|fS 	 t        j:                  |t        j<                  t        j>                  z  t        j@                  z        }	 t        jD                  |dd
      5 }tG        jH                  ||       ddd       y# t        $ r Y w xY w# t        t        t        f$ r d}Y w xY w# t        t8        f$ r Y w xY w# t        $ r Y w xY w# tB        $ r dt        |      fcY S w xY w# 1 sw Y   yxY w# tJ        $ r$ 	 |j                  d        # t        $ r Y  w xY ww xY w)zAcquire a machine-local lock keyed by scope + identity.

    Used to prevent multiple local gateways from using the same external identity
    at once (e.g. the same Telegram bot token across different HERMES_HOME dirs).
    Tr   )rV   identity_hashr(  r   Nr   r1   r   r[   z/statusrO   r\   zState:r   >   TtFr  )TN)&rY   r  r  r   rU   r/   r   r   r   rG   r_   r   r   rd   r$   r   r   r   r   rf   r   rv   r   r   r`   
splitlines
startswithra   rc   r  r  r  r  r  r  r   r   r   )rV   rM   r(  r   r   existingexisting_pidstalecurrent_startlive_cmdline_proc_status_line_stater  r   s                  r   acquire_scoped_lockr7  6  s<    %UH5I4$7

$X.N"nF y)HI,,.
	- 	 x/L 299;&8<<+ET`Ia+aY/>!$|, 7 ELL.:%1%l)CC E  \2:%-7E#8#FL#/7QRZ7[ $ 
'+f\N',J'K'..0)5)?)?)?)Q)\)\)^ *#(#3#3H#=-2[[]1-=F'-';04$)*   D 1 (?"1WWY

RYY 6 DEYYr31 	&VIIff%	& Y  		
 )Z0 	 L	 ` $_5 
    1oi0001	&   	- 	  		s   0I- I= #AJ 7J J/ .AJ> 5K& K$K& -	I:9I:=JJJ,+J,/	J;:J;>KKK#K& #K& &	L0LL	LLLLc                 *   t        | |      }t        |      }|sy|j                  d      t        j                         k7  ry|j                  d      t        t        j                               k7  ry	 |j                  d       y# t        $ r Y yw xY w)zDRelease a previously-acquired scope lock when owned by this process.Nr1   r   Tr   )rY   r   r   r$   r   rf   r   rG   )rV   rM   r   r/  s       r   release_scoped_lockr9    s    $UH5Iy)H||Ebiik)||L!%<RYY[%IID) s   3B 	BB)	owner_pidowner_start_timer:  r;  c                    t               }d}|j                         r|j                  d      D ]p  }| Tt        |      }t	        |t
              s!	 t        |j                  d            }|| k7  rB||j                  d      |k7  rY	 |j                  d       |dz  }r |S # t        t        f$ r Y w xY w# t        $ r Y w xY w)a&  Remove scoped lock files in the lock directory.

    Called during --replace to clean up stale locks left by stopped/killed
    gateway processes that did not release their locks gracefully. When an
    ``owner_pid`` is provided, only lock records belonging to that gateway
    process are removed. ``owner_start_time`` further narrows the match to
    protect against PID reuse.

    When no owner is provided, preserves the legacy behavior and removes every
    scoped lock file in the directory.

    Returns the number of lock files removed.
    r   z*.lockr1   r   Tr   r   )r)   r   globr   r   r   r_   r   r   rd   r   rG   )r:  r;  lock_dirremoved	lock_filer   
record_pids          r   release_all_scoped_locksrB    s    $ HG!x0 	I$(3!&$/!$VZZ%6!7J *$0

<04DD  D 11%	* N ":.   s$   B#B8#B54B58	CCz.gateway-takeover.json<   z.gateway-planned-stop.jsonc                  (    t               } | t        z  S )z6Return the path to the --replace takeover marker file.)r   _TAKEOVER_MARKER_FILENAMEr   s    r   _get_takeover_marker_pathrF    s    D+++r   c                  (    t               } | t        z  S )z<Return the path to the intentional gateway stop marker file.)r   _PLANNED_STOP_MARKER_FILENAMEr   s    r   _get_planned_stop_marker_pathrI    s    D///r   
written_atttl_sc                     	 t        j                  |       }t        j                  t        j                        |z
  j                         }||kD  S # t        t        f$ r Y yw xY w)NT)r   fromisoformatr,   r   r-   total_secondsr   rd   )rJ  rK  
written_dtages       r   _marker_is_stalerQ    sX    ++J7
||HLL)J6EEGU{z" s   AA A"!A"	pid_fieldstart_time_fieldc                   t        |       }|sy	 t        ||         }|j                  |      }|j                  d      xs d}t        ||      r	 | j                  d       yt        j                         }t        |      }	||k(  xr |d uxr |	d uxr ||	k(  }
	 | j                  d       |
S # t        t        t
        f$ r& 	 | j                  d       Y y# t        $ r Y Y yw xY ww xY w# t        $ r Y yw xY w# t        $ r Y |
S w xY w)NFrJ  r9   Tr   )r   r_   r   r   r   rd   r   rG   rQ  r$   r   rf   )r   rR  rS  rK  r   
target_pidtarget_start_timerJ  our_pidour_start_timematchess              r   _consume_pid_marker_for_selfrZ    sI    T"F		*+
"JJ'78ZZ-3
 
E*	KK4K( iikG,W5Ng 	0T)	0$&	0 /	 t$ N9 i, 	KK4K(   		  		  NsM   4B0 C- C< 0C*C	C&"C*%C&&C*-	C98C9<	D	D	rU  c                     	 t        |       }| |t        j                         t               d}t	        t               |       y# t        t        f$ r Y yw xY w)a  Record that ``target_pid`` is being replaced by the current process.

    Captures the target's ``start_time`` so that PID reuse after the
    target exits cannot later match the marker. Also records the
    replacer's PID and a UTC timestamp for TTL-based staleness checks.

    Returns True on successful write, False on any failure. The caller
    should proceed with the SIGTERM even if the write fails (the marker
    is a best-effort signal, not a correctness requirement).
    )rU  rV  replacer_pidrJ  TF)rf   r$   r   r/   r   rF  rG   rc   rU  rV  r   s      r   write_takeover_markerr^  6  sW    3J?$!2IIK&.	
 	24f=_%    A A AAc                  8    t        t               ddt              S )a  Check & unlink the takeover marker if it names the current process.

    Returns True only when a valid (non-stale) marker names this PID +
    start_time. A returning True indicates the current SIGTERM is a
    planned --replace takeover; the caller should exit 0 instead of
    signalling ``_signal_initiated_shutdown``.

    Always unlinks the marker on match (and on detected staleness) so
    subsequent unrelated signals don't re-trigger.
    rU  rV  rR  rS  rK  )rZ  rF  _TAKEOVER_MARKER_TTL_Sr   r   r    consume_takeover_marker_for_selfrc  O  s      (!#,$	 r   c                  X    	 t               j                  d       y# t        $ r Y yw xY w)zDRemove the takeover marker unconditionally. Safe to call repeatedly.Tr   N)rF  r   rG   r   r   r   clear_takeover_markerre  b  s-    !#**d*;     	))c                     	 t        |       }| |t        j                         t               d}t	        t               |       y# t        t        f$ r Y yw xY w)a&  Record that ``target_pid`` is being stopped intentionally.

    The gateway exits non-zero for unexpected SIGTERM so service managers can
    revive it. Service stop commands send the same SIGTERM, so the CLI writes
    this short-lived marker first to let the target process exit cleanly.
    )rU  rV  stopper_pidrJ  TF)rf   r$   r   r/   r   rI  rG   rc   r]  s      r   write_planned_stop_markerri  j  sW    3J?$!299;&.	
 	68&A_% r_  c                  8    t        t               ddt              S )zDReturn True when the current process is being intentionally stopped.rU  rV  ra  )rZ  rI  _PLANNED_STOP_MARKER_TTL_Sr   r   r   $consume_planned_stop_marker_for_selfrl    s    '%',(	 r   c                  X    	 t               j                  d       y# t        $ r Y yw xY w)z/Remove the planned-stop marker unconditionally.Tr   N)rI  r   rG   r   r   r   clear_planned_stop_markerrn    s-    %'..$.? rf  Tr   c                x   | xs
 t               }t        |      }t        |      }|st        ||       yt	        |      }t        |      }||fD ]\  }t        |      }|t        |      s|j                  d      }	t        |      }
|	|
|
|	k7  rCt        |      st        |      sZ|c S  t        ||       y)zReturn the PID of a running gateway instance, or ``None``.

    Checks the PID file and verifies the process is actually alive.
    Cleans up stale PID files automatically.
    ro  Nr   )r   r   r  r   r   r   r   r   r   rf   r   r   )r   r   resolved_pid_pathr  lock_activeprimary_recordfallback_recordr   r1   recorded_startr2  s              r   get_running_pidrv    s     !3MO/0AB01CDK!"3=Q%&78N/0BCO!?3 v&;3L1/4%-*CYgHg&s+/I&/QJ  /}Mr   c                     t        | |      duS )z1Check if the gateway daemon is currently running.ro  N)rv  r   s     r   is_gateway_runningrx    s     8=AMMr   r+   )r   N)V__doc__rQ   r   r$   rA   r<   r   r   r   pathlibr   hermes_constantsr   typingr   r	   utilsr   r  r   r   r   r   r&   r;   objectr  r   r  r   r   r   r   r)   r>   r/   r_   r   rL   rU   rY   rf   rh   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  r"  r$  r'  tupler7  r9  rB  rE  rb  rH  rk  rF  rI  rQ  rZ  r^  rc  re  ri  rl  rn  rv  rx  r   r   r   <module>r     sR     	   
 '  ,   #<<7 +  llg%	'   #  t  )Xd^ )t );$ ;
2t 22c 2 .3 s d t 6E# E# EE Es Et E # ( ( (
s x} <;S ;T ; ;tCH~ ;$ ;&4 
d38n 
:$ :8DcN#; : I4 I$sCx. IT Ix~ $ 8C$ C8DQTVYQYNC[ CXd38n5 (3-    ,d  GS GT GVd *htn  .6  # *$*$ *$ 	*$
 *$ *$ *$ *$ *$ 
*$Z7Xd38n5 7
0ds dc dXd3PS8n=U dafgkmuvz{~  AD  |D  wE  nF  hF  bG dNs c d $  $&**}* sm* 		*@ 5   <  ,4 ,0t 0 S T +
+ + 	+
 + 
+\c d 2$ &# $ *d   $% %tn% % c]	%R  $N NtnN N 
	Nr   