
    (GjH                     $   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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dud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z  de#dz  fd"Z-d!e#dz  de&fd#Z.d!e#dz  de&fd$Z/de%de&fd%Z0d&e1e#ef         de&fd'Z2d(edee#         fd)Z3d!e#d(ede&fd*Z4dd+d&e1e#ef         de%d,ee         de&fd-Z5de1fd.Z6de1e#ef         fd/Z7d0edee1e#ef                  fd1Z8d0ed2e1e#ef         ddfd3Z9dudee         dee1         fd4Z:dud5ee         dee1e#ef                  fd6Z;d&ee1e#ef                  dee%         fd7Z<ded8e&ddfd9Z=dvd:Z>de&fd;Z?de%de&fd<Z@dvd=ZAde&fd>ZBdvd?ZCdud5ee         de&fd@ZDdvdAZEeeeeeeeeedB	dCedDedEedFedGedHedIedJedKeddfdLZFdud0ee         dee1e#ef                  fdMZGdNede%fdOZH eIdPh          ZJdQe&dCedFede&fdRZKdQe&dCede&fdSZL	 dudd+dTee1e#ef                  d,ee         dee%         fdUZMdvdVZNdude#de#dWee1e#ef                  deOe&ee1e#ef                  f         fdXZPde#de#ddfdYZQdddZd[ee%         d\ee%         de%fd]ZRd^ZSd_ZTd`ZUd_ZVdefdaZWdefdbZXdce#dde%de&fdeZYd0edfe#dge#dde%de&f
dhZZdie%de&fdjZ[de&fdkZ\dvdlZ]die%de&fdmZ^de&fdnZ_de&fdoZ`dvdpZa	 dudqdrdee         d8e&dee%         fdsZb	 dudqdrdee         d8e&de&fdtZcdS )wu  
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    4/home/rurouni/.hermes/hermes-agent/gateway/status.py_get_pid_pathr   -   s    D-    pid_pathc                 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   3   s4    !!"8999D(((r   c                  N    t                                          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)   @   s`    y233H H~~bi 0$)++2H72RSSTTJ >11r   c                  b    t          j        t          j                                                  S N)r   nowr   utc	isoformatr   r   r   _utc_now_isor/   I   s     <%%//111r   F)forcepidr0   c                   |rt           rddlm} 	 t          j        ddt          |           ddgddd |            	          }n0# t          $ r# t          j        | t          j
                   Y d
S w xY w|j        dk    r6|j        p|j        pd                                }t          |pd|            d
S |st          j
        nt!          t          dt          j
                  }t          j        | |           d
S )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.
    r   )windows_hide_flagstaskkillz/PIDz/Tz/FT
   )capture_outputtexttimeoutcreationflagsN ztaskkill failed for PID SIGKILL)_IS_WINDOWShermes_cli._subprocess_compatr3   
subprocessrunstrFileNotFoundErrorr$   killsignalSIGTERM
returncodestderrstdoutstripOSErrorgetattr)r1   r0   r3   resultdetailssigs         r   terminate_pidrN   M   s,       	EDDDDD
	^VSXXtT:#0022  FF ! 	 	 	GC(((FF	 !!};;BBDDG'E%E%E%EFFF %
U&..769fn+U+UCGCs   3A )A21A2identityc                     t          j        |                     d                                                    d d         S )Nutf-8   )hashlibsha256encode	hexdigest)rO   s    r   _scope_hashrW   n   s3    >(//'2233==??DDr   scopec                 J    t                      |  dt          |           dz  S )N-z.lock)r)   rW   )rX   rO   s     r   _get_scope_lock_pathr[   r   s*    ??DDH(=(=DDDDDr   c                    t          d|  d          }	 t          |                    d                                          d                   S # t          t
          t          t          t          f$ r Y nw xY w	 ddl	}t          t          |                    |                                           dz                      S # t          $ r Y dS w xY w)	u  Return a stable per-process start-time fingerprint, or None.

    Used as a PID-reuse guard: a ``(pid, start_time)`` pair uniquely identifies
    a process, so a recycled PID (same number, different process) yields a
    different value and is never mistaken for the original.

    On Linux this is field 22 of ``/proc/<pid>/stat`` (start time in clock
    ticks since boot, an int).  On platforms without ``/proc`` (macOS, Windows)
    we fall back to ``psutil.Process(pid).create_time()`` — a float epoch
    timestamp — quantized to an int (centiseconds) for stable equality.

    The two sources are never mixed on a single platform: ``/proc`` always
    succeeds first on Linux, and always fails on macOS/Windows so psutil is
    always used there.  Because the guard only compares the value recorded at
    spawn against the live value *on the same host*, the differing units across
    platforms are irrelevant — only same-source equality matters.
    /proc//statrQ   encoding   r   Nd   )r   int	read_textsplitrA   
IndexErrorPermissionError
ValueErrorrI   psutilroundProcesscreate_time	Exception)r1   	stat_pathri   s      r   _get_process_start_timero   v   s    $ (c((())I9&&&88>>@@DEEEz?JP   5,,88::S@AABBB   tts$   :A #A65A6:AC 
CCc                      t          |           S )zBPublic wrapper for retrieving a process start time when available.)ro   )r1   s    r   get_process_start_timerq      s    "3'''r   c                    t          d|  d          }	 |                                }|r=|                    dd                              dd                                          S n# t
          t          t          f$ r Y nw xY wt          s	 t          j
        dd	t          |           d
dgddd          }|j        dk    r2|j                                        r|j                                        S n# t          t          j        f$ r Y nw xY w	 ddl}|                    |           }|                                }|rd                    |          S n# t&          $ r Y nw xY wdS )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=``.
    On Windows (no /proc, no ps), uses psutil.
    r]   z/cmdline        rQ   ignore)errorsps-p-ozcommand=T   r6   r7   r8   r   N )r   
read_bytesreplacedecoderH   rA   rg   rI   r<   r>   r?   r@   rE   rG   TimeoutExpiredri   rk   cmdlinejoinrm   )r1   cmdline_pathrawrK   ri   proccmdline_partss          r   _read_process_cmdliner      s    ....//LW%%''  	W;;w--44WX4NNTTVVV	W 8     
	^tSXXtZ8#	  F  A%%&-*=*=*?*?%}**,,,23 	 	 	D	~~c"" 	+88M***	+    4s7   A) )BBA&C6 6DDAE 
E%$E%commandc                    | sdS 	 t          j        | d          }n$# t          $ r |                                 }Y nw xY wd |D             }|sdS |D ]C}|dk    s|                    d          r dS |                    dd	          d
         }|dv r dS Dd                    |          }d|v pd|v pt          d |D                       }|sdS g }d}|D ]N}|rd}|dv rd}|                    d          s|                    d          r9|                    |           Ot          |          D ]1\  }	}|dk    r|	d	z   t          |          k    r dS ||	d	z            c S dS )ao  Return the Hermes gateway lifecycle subcommand from a command line.

    Lifecycle decisions (is the gateway up? did restart relaunch it?) must not
    fire on loose substring matches.  The previous ``"... gateway" in cmdline``
    test also matched ``hermes_cli.main gateway status`` and even unrelated
    processes like ``python -m tui_gateway`` -- which made ``restart()`` race
    against a still-draining old process and ``status``/``start`` report false
    positives.  This requires the actual ``gateway`` subcommand followed by
    ``run`` (or one of the gateway-dedicated entrypoints), excluding the other
    ``gateway`` management subcommands and any process that merely contains the
    word "gateway".

    Tokenizes quote-aware (``shlex``) so quoted Windows paths with spaces
    (``"C:\Program Files\...\hermes-gateway.exe"``) survive, and strips
    ``--profile``/``-p`` selectors from anywhere in argv -- Hermes's
    ``_apply_profile_override`` removes them before argparse, so the profile
    flag (and a profile literally named ``gateway``) can legally appear on
    either side of the ``gateway`` subcommand.
    NF)posixc                     g | ]=}|                     d                               dd                                          >S )z"'\/)rH   r~   lower.0ts     r   
<listcomp>z/_gateway_command_subcommand.<locals>.<listcomp>   s>    LLLAaggenn$$T3//5577LLLr   zgateway/run.pyz/gateway/run.pyr?   r      )r   zhermes-gateway.exer|   zhermes_cli.mainzhermes_cli/main.pyc              3   R   K   | ]"}|                     d d          d         dv V  #dS )r   r   r   )r#   z
hermes.exeN)rsplitr   s     r   	<genexpr>z._gateway_command_subcommand.<locals>.<genexpr>   s;      PPAqxxQ#'??PPPPPPr   )z	--profilerx   Tz
--profile=z-p=gateway)shlexre   rh   endswithr   r   any
startswithappend	enumeratelen)
r   
raw_tokenstokenstokenbasenamejoinedhas_gateway_entryfiltered	skip_nextis
             r   _gateway_command_subcommandr      s   (  t%[666

 % % %]]__


% MLLLLF t   $$$7H(I(I$55<<Q''+???55 @ XXfFV# 	Q6)	QPPPPPPP 
  t
 HI 	 	 	I'''IL)) 	U-=-=e-D-D 	h''  5Iq5CMM!!55A4s    >>c                 (    t          |           dk    S )zAReturn True only for a real ``gateway run`` process command line.r?   r   r   s    r   looks_like_gateway_command_liner     s    &w//588r   c                 $    t          |           dv S )a9  Return True for command lines that can host the gateway runtime.

    ``gateway restart`` is normally a management command, not the gateway
    runtime. On hosts without a service manager, though, the manual restart
    fallback executes ``run_gateway()`` in that same process, so its argv stays
    as ``gateway restart`` while it owns the webhook port and writes runtime
    state. Keep the public ``looks_like_gateway_command_line()`` strict, and
    use this broader matcher only when validating Hermes-owned runtime records
    or no-supervisor cleanup scans.
    >   r?   restartr   r   s    r   'looks_like_gateway_runtime_command_liner     s     'w//3EEEr   c                 F    t          |           }|sdS t          |          S )zBReturn True when the live PID still looks like the Hermes gateway.F)r   r   )r1   r   s     r   _looks_like_gateway_processr   %  s*    #C((G u*7333r   recordc                     |                      d          t          k    rdS |                      d          }t          |t                    r|sdS d                    d |D                       }t          |          S )zMValidate gateway identity from PID-file metadata when cmdline is unavailable.kindFargvr|   c              3   4   K   | ]}t          |          V  d S r+   )r@   )r   parts     r   r   z-_record_looks_like_gateway.<locals>.<genexpr>6  s(      22Ts4yy222222r   )get_GATEWAY_KIND
isinstancelistr   r   )r   r   r   s      r   _record_looks_like_gatewayr   -  s{    zz&]**u::fDdD!!  uhh22T22222G27;;;r   profile_homec                 4    | j         j        dk    r| j        S dS )ai  Return the profile id a HERMES_HOME directory represents, or None.

    A named profile's home is ``<root>/profiles/<name>`` (immediate parent is
    ``profiles``).  The root/default home (``~/.hermes`` or ``$HERMES_HOME``)
    has no such parent, so it maps to the default profile (``None`` here, which
    callers treat as "the bare, flag-less gateway").
    profilesN)parentname)r   s    r   _profile_name_for_homer   :  s#     :--  4r   c                    |                                  }t          |          }t          |                                           }|/|dk    r)|                                 }d| |v pd| |v pd| |v S d|v sd|v rdS d|v r	d| |vrdS dS )	u  Return True when a gateway command line belongs to ``profile_home``.

    Mirrors ``hermes_cli.gateway._matches_current_profile`` so the dashboard's
    cross-profile liveness fallback scopes a live PID to the *right* profile.
    In a per-profile container, one profile's stale ``gateway_state.json`` can
    record a PID that the OS has since recycled onto a DIFFERENT profile's live
    gateway.  That recycled PID's command line still ``looks_like_gateway`` —
    so without a profile check the dead profile is reported running.  A named
    profile gateway carries ``-p <name>``/``--profile <name>`` (or, rarely, an
    explicit ``HERMES_HOME=<path>``) on its argv; the default/root gateway runs
    bare with no profile flag.
    Ndefaultz
--profile z-p zhermes_home=z -p FT)r   r   r@   )r   r   
command_lcprofile_namehome_lc
profile_lcs         r    _command_line_belongs_to_profiler   G  s     J),77L,%%''GLI$=$=!''))
%%%3 6!Z!!Z/6'g'':5	
 z!!Vz%9%9u##(@w(@(@
(R(Ru4r   expected_homer   c                    t          |          }|r't          |          sdS |t          ||          sdS dS t          |           S )u6  Return True when a live PID still identifies as this gateway record.

    Prefer the live command line whenever it is readable. Runtime status files
    can outlive the gateway process they describe; if PID reuse leaves the same
    PID occupied by an s6 supervisor/log process, the stale record's argv should
    not make that unrelated process count as a running gateway.

    When ``expected_home`` is provided (the dashboard enumerating a specific
    profile's state file), the readable live command line must additionally
    belong to *that* profile — otherwise a PID recycled onto a different
    profile's live gateway would make the dead profile look alive.  When the
    live command line cannot be read (Windows/permission), fall back to the
    persisted record so cross-platform behavior is preserved.
    FNT)r   r   r   r   )r   r1   r   live_cmdlines       r    _record_matches_live_gateway_pidr   l  sc    ( )--L 6|DD 	5$-M-.
 .
$ 5t%f---r   c                      t          j                    t          t          t          j                  t          t          j                              dS )N)r1   r   r   
start_time)r$   getpidr   r   sysr   ro   r   r   r   _build_pid_recordr     s9    y{{SX-bikk::	  r   c            	      r    t                      } |                     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     sJ    !!GNN#""nn     Nr   pathc                 D   |                                  sd S 	 |                     d                                          }n# t          t          f$ r Y d S w xY w|sd S 	 t          j        |          }n# t
          j        $ r Y d S w xY wt          |t                    r|nd S )NrQ   r_   )
existsrd   rH   rI   UnicodeDecodeErrorjsonloadsJSONDecodeErrorr   dict)r   r   r   s      r   _read_json_filer     s    ;;== tnngn..4466'(    tt	
  t*S//   tt $//977T9s#   (A AAA3 3BBr   c                 ,    t          | |d d           d S )N),:)indent
separatorsr
   )r   r   s     r   _write_json_filer     s    dGDZHHHHHHr   c                    | pt                      } |                                 sd S 	 |                                                                 }n# t          t
          f$ r Y d S w xY w|sd S 	 t          j        |          }n9# t          j        $ r' 	 dt          |          icY S # t          $ r Y Y d S w xY ww xY wt          |t                    rd|iS t          |t                    r|S d S Nr1   )r   r   rd   rH   rI   r   r   r   r   rc   rh   r   r   )r   r   r   s      r   _read_pid_recordr     s1   *=??H?? t  ""((**'(    tt  t*S//   	3s88$$$$ 	 	 	444	 '3  w'4   4sA   &A A$#A$,B B7B$!B7$
B3.B72B33B7	lock_pathc                 <    t          | pt                                S r+   )r   r   )r   s    r   _read_gateway_lock_recordr     s    IA)?)A)ABBBr   c                 r    | sd S 	 t          | d                   S # t          t          t          f$ r Y d S w xY wr   )rc   KeyError	TypeErrorrh   )r   s    r   _pid_from_recordr     sO     t6%=!!!i,   tts    66cleanup_stalec                    |sdS 	 |                      d           n# t          $ r Y nw xY w	 t          |                                d           dS # t          $ r Y dS 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)unlinkrm   r   r   r   s     r   _cleanup_invalid_pid_pathr     s      4((((   x((//4/@@@@@   s    
**#A 
A! A!c                 2   |                      d           |                                  t          j        t	                      |            |                                  	 t          j        |                                            d S # t          $ r Y d S w xY w)Nr   )
seektruncater   dumpr   flushr$   fsyncfilenorI   handles    r   _write_gateway_lock_recordr	    s    
KKNNN
OOI!!6***
LLNNN
!!!!!   s    &B 
BBc                    	 t           r|                     dt          j                   |                                 dk    r)|                     d           |                                  |                     t                     t          j	        | 
                                t          j        d           n>t          j        | 
                                t          j        t          j        z             dS # t           t"          f$ r Y dS w xY w)Nr   
r   TF)r<   r  r$   SEEK_ENDtellwriter  _WINDOWS_LOCK_OFFSETmsvcrtlockingr  LK_NBLCKfcntlflockLOCK_EXLOCK_NBBlockingIOErrorrI   r  s    r   _try_acquire_file_lockr    s     	HKK2;'''{{}}!!T"""KK,---N6==??FOQ????K)FGGGtW%   uus   C3C7 7DDc                 .   	 ddl }	 |                    t          |                                                     |j        k    rdS n&# t          |dd          $ r Y dS t          $ r Y nw xY wt          |                    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
}|                    ||z  dt          |                     }	|	s&|                                }
|
|k    rdS |
|k    rdS dS 	 |                    |	d          }||k    |                    |	           S # |                    |	           w xY w# t*          t,          f$ r Y dS w xY w	 t/          dt          |            d                              d                                          }t5          |          dk    r|d         dk    rdS n# t6          $ r 	 t9          j        ddddt=          t          |                     gddd
          }|j        dk    r/|j         !                                "                    d          rY dS n# t          $ r Y nw xY wY ntF          tH          t*          f$ r Y nw xY w	 tK          j&        t          |           d           dS # tN          $ r Y dS tH          $ r Y dS t*          $ r Y dS 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   NFNoSuchProcessr   i   r   i  W   rz   Tr]   r^   rQ   r_      Zrw   ry   zstate=rx   r{   )(ri   rk   rc   statusSTATUS_ZOMBIErJ   rm   bool
pid_existsImportErrorr<   ctypeswindllkernel32c_void_pOpenProcessrestypec_uintWaitForSingleObjectGetLastErrorCloseHandlerI   AttributeErrorr   rd   re   r   rA   r>   r?   r@   rE   rG   rH   r   rf   rg   r$   rB   ProcessLookupError)r1   ri   r#  r%  !PROCESS_QUERY_LIMITED_INFORMATIONSYNCHRONIZEWAIT_TIMEOUTERROR_INVALID_PARAMETERERROR_ACCESS_DENIEDr  errwait_resultstat_fieldsrs                 r   _pid_existsr8    s   ,	~~c#hh''..00F4HHHu Iv33 	 	 	55 	 	 	D	F%%c#hh//000    D	MMM}-H ,2?H (39=H(0,2MH!)06-"K%L&(#"#))1K?C F  ++--111 5---4u-&::61EE #l2$$V,,,,$$V,,,,( 	 	 	55		-c#hh---..88'8JJPPRR  ;!##A#(=(=u  	 	 	
N44SXX?#'	   <1$$)9)9)D)DS)I)I$ 55   OW5 	 	 	D			GCHHa   4! 	 	 	55 	 	 	44 	 	 	55	s   B =A B A*B 	A*'B )A**1B 
B)(B)5BF F F -F FF F21F26A%H 
K)A.JK
J(%K'J((K-KK
"K. .
L;	L	LLc                 6   	 t           rN|                     t                     t          j        |                                 t          j        d           d S t          j        |                                 t          j	                   d S # t          $ r Y d S w xY w)Nr   )r<   r  r  r  r  r  LK_UNLCKr  r  LOCK_UNrI   r  s    r   _release_file_lockr<    s     	8KK,---N6==??FOQ?????K77777   s   AB
 1B
 

BBc                      t           dS t                      } | j                            dd           t	          | dd          }t          |          s|                                 dS t          |           |a dS )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.
    NTparentsexist_oka+rQ   r_   F)_gateway_lock_handler   r   mkdiropenr  closer	  )r   r  s     r   acquire_gateway_runtime_lockrF    s     't!##DKdT222$w///F!&)) uv&&&!4r   c                      t           } | dS da t          |            	 |                                  dS # t          $ r Y dS w xY w)z<Release the gateway runtime lock when owned by this process.N)rB  r<  rE  rI   r  s    r   release_gateway_runtime_lockrH    s_     "F~v   s   4 
AAc                    | pt                      }t          |t                      k    rdS |                                sdS t          |dd          }	 t	          |          r8t          |           	 	 |                                 dS # t          $ r Y dS w xY w	 	 |                                 dS # t          $ r Y dS w xY w# 	 |                                 w # t          $ r Y w w xY wxY w)zFReturn True when some process currently owns the gateway runtime lock.NTFrA  rQ   r_   )r   rB  r   rD  r  r<  rE  rI   )r   resolved_lock_pathr  s      r   is_gateway_runtime_lock_activerK    s4    #>&<&>&>',>BXBZBZ,Z,Zt$$&& u$dW===F	!&)) 	v&&&	LLNNNNN 	 	 	DD		 	LLNNNNN 	 	 	DD		LLNNNN 	 	 	D	sT   C 5B 
BBB4 4
CCC,CC,
C)&C,(C))C,c                  (   t                      } | j                            dd           t          j        t                                }	 t          j        | t          j        t          j	        z  t          j
        z            }n# t          $ r  w xY w	 t          j        |dd          5 }|                    |           ddd           dS # 1 swxY w Y   dS # t          $ r* 	 |                     d           n# t           $ r Y n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>  wrQ   r_   Nr   )r   r   rC  r   dumpsr   r$   rD  O_CREATO_EXCLO_WRONLYFileExistsErrorfdopenr  rm   r   rI   )r   r   fdfs       r   write_pid_filerV    sj    ??DKdT222Z)++,,FWT2:	1BK?@@   Yr3111 	QGGFOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   	KK4K(((( 	 	 	D	s`   9B BC -CC CC CC 
D(C?>D?
D	DDD)	r   r   r   r   platformplatform_state
error_codeerror_messageserved_profilesr   r   r   r   rW  rX  rY  rZ  r[  c        	            t                      }	t          |	          pt                      }
t                      }|
                    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          urt          |pg           |
d<   |t          urb|
d         
                    |i           }|t          ur||d<   |t          ur||d<   |t          ur||d<   t                      |d<   ||
d         |<   t          |	|
           dS )zBPersist gateway runtime health information for diagnostics/status.r   r   r1   r   r   r   r   r   r   r   r[  r"   rY  rZ  N)r   r   r   r   
setdefaultr/   _UNSETr   parse_active_agentsr   r   r   )r   r   r   r   rW  rX  rY  rZ  r[  r   r   current_recordplatform_payloads                r   write_runtime_statusrb    s    $%%Dd##E'C'E'EG&((N{B'''$V,GFO#E*GEN$V,GFO*<8GL(NNGLF""#0 &  !,&&'+,='>'>#$F""#6}#E#E f$$ &*/*?R%@%@!"v";/33HbAA''(6W%V##-7\*&&0=_-)5&)9X&T7#####r   c                 <    t          | pt                                S )aK  Read the persisted gateway runtime health/status information.

    ``path`` is optional so callers that need to inspect a *different*
    profile's state file (e.g. the dashboard enumerating every profile)
    can do so without mutating ``HERMES_HOME`` in-process.  Defaults to
    the active profile's ``gateway_state.json``.
    )r   r   )r   s    r   read_runtime_statusrd    s     4=#;#=#=>>>r   r   c                 n    	 t          dt          |                     S # t          t          f$ r Y dS w xY w)u  Coerce a persisted ``active_agents`` value to a clamped non-negative int.

    The shared coercion for the in-flight gateway-turn count. Used on the WRITE
    side (``write_runtime_status``) and by both HTTP read surfaces
    (``/api/status`` and ``/health/detailed``) so the count is clamped to a
    single contract — never negative, never raising on a manually-edited or
    otherwise non-numeric value (degrades to ``0``).
    r   )maxrc   r   rh   )r   s    r   r_  r_  '  sD    1c#hhz"   qqs    44runninggateway_runningc                 x    | sdS |t           vrdS 	 t          |          dk    S # t          t          f$ r Y dS w xY w)u  Whether the gateway is actively processing in-flight turns.

    The contract NAS gates lifecycle actions on.  Busy iff the gateway is live
    (``gateway_running``), in the ``running`` state, AND at least one agent is
    mid-turn (``active_agents > 0``).  Degrades to ``False`` whenever liveness
    is unknown, the state is anything but ``running``, or the count is
    absent/unparseable — i.e. a down or file-absent gateway reads "not busy",
    never a spurious "busy".

    NOTE: liveness keys off ``gateway_running`` (a live PID / health probe),
    NEVER ``updated_at`` — a healthy idle gateway never advances that timestamp.
    Fr   )_DRAINABLE_GATEWAY_STATESrc   r   rh   )rh  r   r   s      r   derive_gateway_busyrk  <  sa      u555u=!!A%%z"   uus   $ 99c                 2    t          |           o|t          v S )u  Whether the gateway can accept a begin-drain request right now.

    True iff the gateway is live and in the ``running`` state — i.e. not already
    draining/stopping/stopped and not in a failed-start state.  This is
    independent of ``active_agents``: an idle running gateway is drainable (the
    drain just completes immediately).  Degrades to ``False`` for a down or
    non-running gateway.
    )r   rj  )rh  r   s     r   derive_gateway_drainablerm  U  s       O]6O%OOr   runtimec                V   | | nt                      }t          |t                    sdS |                    d          dv rdS t	          |          }|t          |          sdS |                    d          }t          |          }|
|||k    rdS t          |||          r|S dS )a`  Return a live gateway PID from the runtime status record, if valid.

    ``get_running_pid()`` is the primary liveness source because it verifies the
    runtime lock and PID file.  Launch-service managers can still leave us with
    a live process and a fresh ``gateway_state.json`` but no ``gateway.pid``; use
    this as a conservative fallback by checking both the persisted state and the
    OS process identity.

    ``expected_home`` scopes the OS-identity check to a specific profile's
    HERMES_HOME.  Pass it when validating *another* profile's state file (the
    dashboard enumerating every profile): a stale record whose PID the OS has
    recycled onto a different profile's live gateway must not be reported
    running for the dead profile.  Omit it (the default) for the active
    profile, where any live gateway command line is acceptable.
    Nr   >   Nstoppedstartup_failedr   r   )rd  r   r   r   r   r8  ro   r   )rn  r   r   r1   recorded_startcurrent_starts         r   get_runtime_status_running_pidrt  a  s    ( !,gg2E2G2GGgt$$ t{{?##'JJJt
7
#
#C
{+c**{t[[..N+C00M"%^++t'MRRR 
4r   c                  6   	 t                      } t          |           }|Q	 t          |d                   }n# t          t          t
          f$ r d}Y nw xY w||t          j                    k    rdS |                     d           dS # t          $ r Y dS 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   rc   r   r   rh   r$   r   r   rm   )r   r   file_pids      r   remove_pid_filerw    s     && ve}--i4       #BIKK(?(?t$$$$$   s7   B
 8 B
 AB
 AB
 2B
 

BBmetadatac                    t          | |          }|j                            dd           i t                      | t	          |          |pi t                      d}t          |          }|<|                                r(	 |                    d           n# t          $ r Y nw xY w|r?	 t          |d                   }n# t          t          t          f$ r d}Y nw xY w|t          j                    k    r@|                    d          |                    d          k    rt#          ||           d|fS |du }|s}t%          |          sd}njt'          |          }|                    d          |||                    d          k    rd}|sH|                    d          3|1t)          |          s"t+          |          }	|	t-          |          sd}|s9|                    d          $|"t)          |          st+          |          }	|	d}|s	 t/          d| d	          }
|
                                rb|
                    d
                                          D ]9}|                    d          r"|                                d         }|dv rd} n:n# t          t8          f$ r Y nw xY w|r(	 |                    d           n# t          $ r Y nw xY wd|fS 	 t          j        |t          j        t          j        z  t          j         z            }n!# tB          $ r dt          |          fcY S w xY w	 t          j"        |dd
          5 }tG          j$        ||           ddd           n# 1 swxY w Y   n7# tJ          $ r* 	 |                    d           n# t          $ r Y nw xY w w xY wdS )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>  )rX   identity_hashrx  r   Nr   r1   r   r]   z/statusrQ   r_   zState:r   >   Tr   FrM  )TN)&r[   r   rC  r   rW   r/   r   r   r   rI   rc   r   r   rh   r$   r   r   r   r8  ro   r   r   r   r   rd   
splitlinesr   re   rg   rD  rO  rP  rQ  rR  rS  r   r  rm   )rX   rO   rx  r   r   existingexisting_pidstalers  r   _proc_status_line_staterT  r  s                  r   acquire_scoped_lockr    s    %UH55I4$777


$X..N"nn  F y))HI,,..
	---- 	 	 	D	 J#	 x//LL)Z0 	  	  	 LLL	  29;;&&8<<+E+ET`IaIa+a+aY///>!$ 8	|,, 7 7 E ELL..:%1%l)C)CCC E % \22:%-7EE . $9#F#FL#/7QRZ7[7[/ $ % \22>%17EE 2 $9#F#FL#/ $  
'+,J\,J,J,J'K'K'..00 *)5)?)?)?)Q)Q)\)\)^)^ * *#(#3#3H#=#= !*-2[[]]1-=F'-';';04$)E	!*
 $_5    	#  D 1111    (?"1WY
RY 6 DEE 1 1 1oi0000001Yr3111 	&VIff%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&   	---- 	 	 	D	 :s   B 
B)(B)0C C"!C"B	J) )J=<J=K 
K'&K'/9L) )MMN "N8N NN NN 
ON21O2
N?<O>N??Oc                 f   t          | |          }t          |          }|sdS |                    d          t          j                    k    rdS |                    d          t          t          j                              k    rdS 	 |                    d           dS # t          $ r Y dS w xY w)zDRelease a previously-acquired scope lock when owned by this process.Nr1   r   Tr   )r[   r   r   r$   r   ro   r   rI   )rX   rO   r   r}  s       r   release_scoped_lockr    s    $UH55Iy))H ||Ebikk))||L!!%<RY[[%I%IIID)))))   s   
B" "
B0/B0)	owner_pidowner_start_timer  r  c                    t                      }d}|                                r|                    d          D ]}| t          |          }t	          |t
                    s)	 t          |                    d                    }n# t          t          f$ r Y `w xY w|| k    rk||                    d          |k    r	 |
                    d           |dz  }# t          $ r Y w xY w|S )	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*.lockNr1   r   Tr   r   )r)   r   globr   r   r   rc   r   r   rh   r   rI   )r  r  lock_dirremoved	lock_filer   
record_pids          r   release_all_scoped_locksr  +  s'   $ HG !x00 	 	I$(33!&$// !$VZZ%6%6!7!7JJ!:.   H**$0

<004DDD  D 1111   Ns$   $"BBBC
C+*C+z.gateway-takeover.json<   z.gateway-planned-stop.jsonc                  2    t                      } | t          z  S )z6Return the path to the --replace takeover marker file.)r   _TAKEOVER_MARKER_FILENAMEr   s    r   _get_takeover_marker_pathr  q  s    D+++r   c                  2    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_pathr  w  s    D///r   
written_atttl_sc                     	 t          j        |           }t          j        t          j                  |z
                                  }||k    S # t          t          f$ r Y dS w xY w)NT)r   fromisoformatr,   r   r-   total_secondsr   rh   )r  r  
written_dtages       r   _marker_is_staler  }  sj    +J77
|HL))J6EEGGU{z"   tts   AA A$#A$	pid_fieldstart_time_fieldc                   t          |           }|sdS 	 t          ||                   }|                    |          }|                    d          pd}nF# t          t          t
          f$ r, 	 |                     d           n# t          $ r Y nw xY wY dS w xY wt          ||          r*	 |                     d           n# t          $ r Y nw xY wdS |                    d          }|!|t          t                                k    rdS t          j                    }	t          |	          }
||	k    rd}n|	|
||
k    }nd}	 |                     d           n# t          $ r Y nw xY w|S )NFr  r:   Tr   replacer_hermes_home)r   rc   r   r   r   rh   r   rI   r  r@   r   r$   r   ro   )r   r  r  r  r   
target_pidtarget_start_timer  replacer_homeour_pidour_start_timematchess               r   _consume_pid_marker_for_selfr    s    T""F u		*++
"JJ'788ZZ--3

i,   	KK4K(((( 	 	 	D	uu 
E** 	KK4K(((( 	 	 	D	u JJ566M ]c/:K:K6L6L%L%LuikkG,W55N W		&>+E#~5t$$$$    NsZ   AA B/BB
BBBBB.C 
CCE 
E,+E,r  c                 
   	 t          |           }| |t          j                    t          t	                                t                      d}t          t                      |           dS # t          t          f$ r Y dS 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).
    )r  r  replacer_pidr  r  TF)
ro   r$   r   r@   r   r/   r   r  rI   rg   r  r  r   s      r   write_takeover_markerr    s    3J??$!2IKK$'(9(9$:$:&..
 
 	244f===t_%   uus   A)A- -BBc                  J    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.
    r  r  r  r  r  )r  r  _TAKEOVER_MARKER_TTL_Sr   r   r    consume_takeover_marker_for_selfr    s-     (!##,$	   r   c                  n    	 t                                          d           dS # t          $ r Y dS w xY w)zDRemove the takeover marker unconditionally. Safe to call repeatedly.Tr   N)r  r   rI   r   r   r   clear_takeover_markerr    sL    !##**d*;;;;;      "& 
44c                     	 t          |           }| |t          j                    t                      d}t	          t                      |           dS # t          t          f$ r Y dS 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.
    )r  r  stopper_pidr  TF)ro   r$   r   r/   r   r  rI   rg   r  s      r   write_planned_stop_markerr    s|    3J??$!29;;&..	
 
 	688&AAAt_%   uus   AA A('A(c                  J    t          t                      ddt                    S )zDReturn True when the current process is being intentionally stopped.r  r  r  )r  r  _PLANNED_STOP_MARKER_TTL_Sr   r   r   $consume_planned_stop_marker_for_selfr    s+    '%'',(	   r   c                  @   t                      } t          |           }|sdS 	 t          |d                   }|                    d          }|                    d          pd}nF# t          t
          t          f$ r, 	 |                     d           n# t          $ r Y nw xY wY dS w xY wt          |t                    r*	 |                     d           n# t          $ r Y nw xY wdS t          j                    }||k    rdS t          |          }||||k    S dS )ue  Return True only when a live planned-stop marker names the current process.

    This is a **non-destructive** probe used by the watcher thread
    (``gateway/run.py:_run_planned_stop_watcher``) to decide whether to
    trigger shutdown. Unlike :func:`consume_planned_stop_marker_for_self`,
    it never unlinks a marker that matches us — the shutdown handler does
    the authoritative consume on its own thread.

    It *does* clean up markers that can never apply to this process:
    malformed markers and markers older than the TTL are unlinked so a
    stale file left behind by a previous gateway instance cannot wedge
    the new one. Markers naming a different PID/start_time are left in
    place (they may still be consumed legitimately by the process they
    name) but report False here.

    Returns False (without raising) on any read/parse error.
    Fr  r  r  r:   Tr   )r  r   rc   r   r   r   rh   r   rI   r  r  r$   r   ro   )r   r   r  r  r  r  r  s          r    planned_stop_marker_targets_selfr  $  sk   $ )**DT""F u
-..
"JJ':;;ZZ--3

i,   	KK4K(((( 	 	 	D	uu 
$>?? 	KK4K(((( 	 	 	D	uikkGWu -W55N$)C N224sH   AA% %B(=BB(
B!B( B!!B('B(C 
C%$C%c                  n    	 t                                          d           dS # t          $ r Y dS w xY w)z/Remove the planned-stop marker unconditionally.Tr   N)r  r   rI   r   r   r   clear_planned_stop_markerr  b  sL    %''..$.?????   r  Tr   c                   | pt                      }t          |          }t          |          }|s'| t                      }||S t	          ||           dS t          |          }t          |          }||fD ]g}t          |          }	|	t          |	          s$|	                    d          }
t          |	          }|
	|||
k    rSt          ||	          r|	c S ht	          ||           | t                      }||S dS )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.
    Nr  r   )r   r   rK  rt  r   r   r   r   r8  r   ro   r   )r   r   resolved_pid_pathrJ  lock_activeruntime_pidprimary_recordfallback_recordr   r1   rr  rs  s               r   get_running_pidr  j  sU    !3MOO/0ABB01CDDK 8::K&""!"3=QQQQt%&788N/0BCCO!?3  v&&;3 	L11/44%-*CYgHgHg+FC88 	JJJ	 /}MMMM466"4r   c                (    t          | |          duS )z1Check if the gateway daemon is currently running.r  N)r  r   s     r   is_gateway_runningr    s     8=AAAMMr   r+   )r   N)d__doc__rS   r   r$   r   rC   r>   r   r   r   pathlibr   hermes_constantsr   typingr   r	   utilsr   rW  r  r  r   r   r&   r<   objectr^  r   rB  r  r   r   r   r)   r@   r/   rc   r   rN   rW   r[   ro   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  r8  r<  rF  rH  rK  rV  rb  rd  r_  	frozensetrj  rk  rm  rt  rw  tupler  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   <module>r     s      				       



 ' ' ' ' ' ' ' '       , , , , , ,                 # # # # # #<7MMMMLLL +  lg%	'   #  t        ) )Xd^ )t ) ) ) );$ ; ; ; ;
2t 2 2 2 22c 2 2 2 2 .3   s d t    BE# E# E E E EE Es Et E E E E   #        F( ( ( ( ( (
's 'x} ' ' ' 'TGt Gd
 G G G GT9S4Z 9D 9 9 9 9
FS4Z FD F F F F4S 4T 4 4 4 4
<tCH~ 
<$ 
< 
< 
< 
<
 
(3- 
 
 
 
"c " "$ " " " "R %)	. . .cN.	. D>	.
 
. . . .@4    
d38n 
 
 
 
:$ :8DcN#; : : : :&I4 I$sCx. IT I I I I x~ $    :C C$ C8DQTVYQYNC[ C C C CXd38n5 (3-          ,   d     sS sT s s s sn   d    *    htn     .   6  # !0$ 0$ 0$0$ 0$ 	0$
 0$ 0$ 0$ 0$ 0$ 0$ 
0$ 0$ 0$ 0$f? ?htn ?c3h8P ? ? ? ?S S    $ &Iyk22 -0AD	   2	P 	Pc 	Pd 	P 	P 	P 	P )-) %)) ) )d38n%) D>) c]	) ) ) )X   0s ss sc sXd3PS8n=U safgkmuvz{~  AD  |D  wE  nF  hF  bG s s s sls c d    $  $&** * *}* sm* 		* * * *@ 5   <  ,4 , , , ,0t 0 0 0 0 S T    G
G G 	G
 G 
G G G GTc d    4$    &   # $    *d    ;$ ; ; ; ;|     $- - - -tn- - c]	- - - -b  $N N N NtnN N 
	N N N N N Nr   