+
    .pÐjñb  ã                  ó  € R t ^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
t
^ RIHtHt ^ RIHt ^ RIHtHt ^ RIHt ]P*                  ! ]4      tRtRtR	t]! 0 RKm4      t]R
0,
          t]P<                  ! 4       R8H  tRs ]	PB                  ! 4       t"RLR R llt#RLR R llt$]! RR7       ! R R4      4       t%RR/R R llt&RR/R R llt'R R lt(] ! R R4      4       t)R R lt*R R  lt+R! R" lt,R# R$ lt-R%R&/R' R( llt.RRR)R/R* R+ llt/RLR, R- llt0R. R/ lt1R0 R1 lt2R2 R3 lt3R4 R5 lt4RR/R6 R7 llt5RRR8R&R9R&/R: R; llt6R< R= lt7R> R? lt8R@ RA lt9RBR/RC RD llt:RRRBR/RE RF llt;RG RH lt<RI RJ lt=R# )Mu-  Process identity: spawn tags, the machine-wide spawn ledger, and the Windows job-object self-attach.

Three layers make every long-lived Hermes process positively identifiable, so reapers (``hermes
update``, Desktop startup sweeps) never guess lineage from PPID archaeology or cmdline matching:
1. spawn tags (``HERMES_SPAWN`` env stamped by the spawner); 2. a ``(pid, create_time)`` ledger;
3. Windows job-object self-attach with ``KILL_ON_JOB_CLOSE`` so the whole child tree dies with the
root â€” no launcherâ†’worker chains left holding ``.pyd`` locks.
)ÚannotationsN)ÚasdictÚ	dataclass)ÚPath)ÚOptionalÚSequence)Úatomic_json_writeÚHERMES_SPAWNÚv1zspawn-ledger.jsonú
mcp-helperÚWindowsc               ó    € V ^8„  d   QhRRRR/# )é   Úproject_rootúOptional[Path]ÚreturnÚstr© )Úformats   "Ú*/opt/hermes/hermes_cli/process_identity.pyÚ__annotate__r   0   s   € ÷ Qñ Q˜^ð Q°sñ Qó    c                óÞ  € V f    ^ RI Hp \        V4      p  \        \        V 4      P                  4       4      P                  4       p\        P                  ! VP                  RR4      4      P                  4       R,          #   \         d5    \        \        4      P                  4       P                  P                  p  Lªi ; i  \         d    \        T 4      P                  4       p L£i ; i)zBStable 12-hex identifier for THIS install (derived from its path).)ÚPROJECT_ROOTzutf-8Úreplace:Né   N)Úhermes_constantsr   r   Ú	ExceptionÚ__file__ÚresolveÚparentr   ÚlowerÚOSErrorÚhashlibÚsha256ÚencodeÚ	hexdigest)r   Ú_rootÚ	canonicals   &  r   Ú
install_idr)   0   sÀ   € àÒð	BÝ>ä ›;ˆLð.Üœ˜\Ó*×2Ñ2Ó4Ó5×;Ñ;Ó=ˆ	ô �>Š>˜)×*Ñ*¨7°IÓ>Ó?×IÑIÓKÈCÕPÐPøô ô 	BÜ¤›>×1Ñ1Ó3×:Ñ:×AÑAŠLð	Bûô ô .Ü˜Ó%×+Ñ+Ó-Š	ð.ús"   †B ˜0C Â<CÃCÃ$C,Ã+C,c               ó    € V ^8„  d   QhRRRR/# )r   ÚpidúOptional[int]r   úOptional[float]r   )r   s   "r   r   r   @   s   € ÷ ñ ˜mð °ñ r   c                ó²   €  ^ RI p\        TP                  V f   \        P                  ! 4       MT 4      P                  4       4      #   \         d     R# i ; i)z[``psutil`` create time for ``pid`` (default: this process); ``None`` when psutil can't say.N)ÚpsutilÚfloatÚProcessÚosÚgetpidÚcreate_timer   )r+   r/   s   & r   Ú_process_create_timer5   @   sE   € ðÛä�V—^‘^°3²;¤B§I¢I¤KÀCÓH×TÑTÓVÓWÐWøÜô Úðús   ‚AA ÁAÁAT)Úfrozenc                  ó@   € ] tR t^Mt$ R]R&   R]R&   R]R&   R]R&   RtR	# )
ÚSpawnTagr   ÚinstallÚpurposeÚintÚspawner_pidr-   Úspawner_creater   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__annotations__Ú__static_attributes__r   r   r   r8   r8   M   s   ‡ àƒLØƒLØÓØ#×#r   r8   r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r:   r   r   r   r   r   )r   s   "r   r   r   U   s&   € ÷ fñ f˜Sð f°>ð fÈSñ fr   c          	     ó¨   € \        4       pVe   VR MRpRP                  \        \        V4      V \	        \
        P                  ! 4       4      V34      # )zGValue for the child's ``HERMES_SPAWN`` env var, stamped by the spawner.z.3fÚ-Ú:)r5   ÚjoinÚ_TAG_VERSIONr)   r   r2   r3   )r:   r   ÚcreateÚcreate_parts   &$  r   Úbuild_spawn_tagrL   U   sE   € ä!Ó#€FØ%+Ò%7�V˜C‘L¸S€KØ�8‰8”\¤:¨lÓ#;¸WÄcÌ"Ï)Ê)Ë+ÓFVÐXcÐdÓeÐer   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r:   r   r   r   r   zdict[str, str]r   )r   s   "r   r   r   \   s&   € ÷ Pñ P�sð P¨^ð PÀ~ñ Pr   c               ó&   € \         \        WR7      /# )z9Env fragment a spawner merges into a child's environment.©r   )ÚSPAWN_ENV_VARrL   )r:   r   s   &$r   Ú	spawn_envrQ   \   s   € äœ?¨7ÔNÐOÐOr   c               ó    € V ^8„  d   QhRRRR/# )r   ÚrawÚobjectr   zOptional[SpawnTag]r   )r   s   "r   r   r   a   s   € ÷ Hñ H˜ð HÐ$6ñ Hr   c                óZ  € \        V \        4      '       d   V P                  R4      M. p\        V4      ^8w  g   V^ ,          \        8w  d   R# Vw  r#rEpV'       d	   V'       g   R#  \        V4      pVR8X  d   RM
\        V4      pT^ 8”  d   \        Y4Yx4      # R#   \         d     R# i ; i)z@Parse a ``HERMES_SPAWN`` value; ``None`` for anything malformed.rG   NrF   )	Ú
isinstancer   ÚsplitÚlenrI   r;   r0   Ú
ValueErrorr8   )	rS   ÚpartsÚ_r9   r:   Úpid_sÚcreate_sr+   rJ   s	   &        r   Úparse_spawn_tagr^   a   s—   € ä(¨¬c×2Ò2ˆC�I‰I�cŒN¸€EÜ
ˆ5ƒz�Q„˜% �(¤lÔ2ÙØ+0Ñ(€A� ßŸ'ÙðÜ�%‹jˆØ! Sœ‘¬e°H«oˆð 7:¸A´gŒ8�G cÓ2ÐGÀ4ÐGøô ô Úðús   Á'B ÂB*Â)B*c                  ó®   € ] tR t^tt$ R]R&   R]R&   R]R&   R]R&   R]R	&   R]R
&   R]R&   R]R&   RtR]R&   RtR]R&   RtR]R&   RtR]R&   Rt	R]R&   Rt
R# )ÚLedgerEntryr;   r+   r-   r4   r   r:   r9   r,   r<   r=   r0   Úregistered_atÚargvÚ ÚhostNÚportÚprofileÚhermes_homeFÚboolÚisolatedr   )r>   r?   r@   rA   rB   rd   re   rf   rg   ri   rC   r   r   r   r`   r`   t   sc   ‡ à	ƒHØ Ó ØƒLØƒLØÓØ#Ó#ØÓØ
ƒIð €Dˆ#ƒNØ€Dˆ-ÓØ€GˆSÓØ€K�Óð €Hˆd×r   r`   c               ó   € V ^8„  d   QhRR/# )r   r   r   r   )r   s   "r   r   r   ‰   s   € ÷ 	9ñ 	9�dñ 	9r   c                 óª   €  ^ RI Hp  \        V ! 4       4      \        ,          #   \         d%    ^ RIHp \        T! 4       4      \        ,          u # i ; i)zCMachine-root ledger path (shared by every profile of this install).)Úget_default_hermes_root)Úget_hermes_home)r   rl   r   ÚLEDGER_FILENAMEr   Úhermes_cli.configrm   )rl   rm   s     r   Ú_ledger_pathrp   ‰   sC   € ð9Ý<äÑ+Ó-Ó.´Õ@Ð@øÜô 9Ý5ä‘OÓ%Ó&¬Õ8Ò8ð9ús   ‚ # £,AÁAc               ó    € V ^8„  d   QhRRRR/# ©r   Úpathr   r   zOptional[list[dict]]r   )r   s   "r   r   r   •   s   € ÷ \ñ \�tð \Ð 4ñ \r   c                ó�  €  V P                  RR7      pTP	                  4       '       g   . #  \
        P                  ! T4      p\        T\        4      '       d+   T Uu. uF  p\        T\        4      '       g   K  TNK  	  up# R#   \         d    . u # \        \        3 d     R# i ; i  \        \        3 d     R# i ; iu upi )zÑEntries list, ``[]`` for empty/missing, ``None`` for CORRUPT (never silently an empty roster).

Mirrors the #89298 contract: corrupt is a distinct state that must never be silently treated as an empty
roster.
z	utf-8-sig)ÚencodingN)Ú	read_textÚFileNotFoundErrorr"   ÚUnicodeErrorÚstripÚjsonÚloadsrY   Ú	TypeErrorrV   ÚlistÚdict)rs   ÚtextÚparsedÚes   &   r   Ú_read_ledgerr‚   •   s±   € ðØ�~‰~ {ˆ~Ó3ˆð
 �:‰:�<Š<Øˆ	ðÜ—’˜DÓ!ˆô :DÀFÌD×9QÒ9Q‘vÓ5‘v�!¤¨A¬t×!4�AˆA‘vÑ5Ð[ÐW[Ð[øô ô ØŠ	Ü”\Ð"ô Úðûô œ	Ð"ô Úðüâ5s:   ‚B ­B+ ÁCÁ:CÂB(ÂB(Â#B(Â'B(Â+C Â?C c               ó    € V ^8„  d   QhRRRR/# rr   r   )r   s   "r   r   r   ª   s   € ÷ 
ñ 
 Tð 
Ð.Bñ 
r   c                óê   € \        V 4      pVfR   V P                  V P                  R,           4      p \        P                  ! W4       \
        P                  RV4       V# V#   \         d     T# i ; i)zULedger entries; ``None`` after parking a corrupt file. Caller holds ``_LEDGER_LOCK``.z.corruptz(spawn ledger was unreadable; moved to %s)r‚   Úwith_suffixÚsuffixr2   r   ÚloggerÚwarningr"   )rs   ÚentriesÚparkeds   &  r   Ú_read_ledger_or_quarantiner‹   ª   sn   € ä˜4Ó €GØ‚Ø×!Ñ! $§+¡+°
Õ":Ó;ˆð	Ü�JŠJ�tÔ$Ü�N‰NÐEÀvÔNð €Nˆ7€Nøô ô 	ØØ€Nð	ús   ³,A# Á#A2Á1A2c               ó    € V ^8„  d   QhRRRR/# )r   r4   r-   r   rh   r   )r   s   "r   r   r   ·   s   € ÷ \ñ \¨ð \¸Tñ \r   c                ó„   € VRJ ;'       g6    \        \        V P                  4       4      \        V4      ,
          4      R8  # )zZDoes the live ``proc`` match a recorded ``create_time`` (2 s tolerance; ``None`` matches)?Nç       @)Úabsr0   r4   )Úprocr4   s   &&r   Ú_same_incarnationr‘   ·   s8   € à˜$Ð×[Ð[¤#¤e¨D×,<Ñ,<Ó,>Ó&?Ä%ÈÓBTÕ&TÓ"UÐX[Ñ"[Ð[r   ÚstrictFc               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r+   r;   r4   r-   r’   rh   r   úOptional[bool]r   )r   s   "r   r   r   ¼   s)   € ÷ ñ ˜Cð ¨oð È$ð Ð[iñ r   c               ó  €  ^ RI p TP                  \        T 4      4      pT'       d]   TRJ;'       dQ    TP	                  4       T8H  ;'       d6    TP                  4       ;'       d    TP                  4       TP                  8g  # \        YA4      ;'       d:    \        P                  R8H  ;'       g    TP                  4       \        TRR4      8g  #   \         d     R# i ; i  TP                   d     R# \         d     R# i ; i)z9True/False when provable; ``None`` when psutil can't say.NÚntÚSTATUS_ZOMBIEÚzombieF)r/   r   r1   r;   r4   Ú
is_runningÚstatusr—   r‘   r2   ÚnameÚgetattrÚNoSuchProcess)r+   r4   r’   r/   r�   s   &&$  r   Ú_pid_alive_matchesrž   ¼   s   € ðÛðØ�~‰~œc #›hÓ'ˆßØ tÐ+÷ Tð T°×0@Ñ0@Ó0BÀkÑ0Q÷ Tð TØŸ™Ó)÷Tð TØ.2¯k©k«m¸v×?SÑ?SÑ.SðUô
 ! Ó3÷ \ð \Ü�G‰G�t‰O×ZÐZ˜tŸ{™{›}´¸ÀÐQYÓ0ZÑZð	\øô ô Úðûð ×Ñô ÚÜô ÚðúsQ   ‚C ‡,C# ´C# ÁC# Á'C# ÂC# ÂC# Â2C# ÃC ÃC Ã#DÃ7DÄ DÄDÚdetailc               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r:   r   r   r   rŸ   zOptional[dict]r   rh   r   )r   s   "r   r   r   Ò   s*   € ÷  ñ  ˜3ð  °ð  ÐP^ð  Ðjnñ  r   c               ó2  € ^ RI Hp \        \        P                  P                  \        4      4      pV'       d   VP                  VP                  3M	\        4       w  rV\        \        P                  ! 4       \        4       WWV4      pV! 4       Vn        V'       dŸ    \        VP                  R4      ;'       g    R4      Vn        VP                  R4      e   \!        VR,          4      MRVn        \        VP                  R4      ;'       g    R4      Vn        \'        VP                  R4      4      Vn         ^ RIpRP1                  VP2                  R	,          4      Vn        \7        V4      #   \*        \,        3 d     LJi ; i  \4         d     L/i ; i)
až  Record this process in the machine spawn ledger. Best-effort.

Called at the top of every long-lived entry point; dead ``(pid, create_time)`` entries are
pruned on every write. ``detail`` may carry ``host``/``port``/``profile`` so the update
pipeline can relaunch a manually-started serve with its real bind address, and ``isolated``
so attach-first discovery skips a backend that opted out of the host singleton.
)Úhermes_home_keyrd   rc   re   Nrf   ri   Ú ºNé
   N)r   r¢   r^   r2   ÚenvironÚgetrP   r<   r=   Ú_desktop_spawner_identityÚ
_new_entryr3   r5   rg   r   rd   r;   re   rf   rh   ri   r|   rY   ÚsysrH   rb   r   Ú_append_entry)	r:   r   rŸ   r¢   Útagr<   r=   ÚentryÚ_syss	   &$$      r   Úregister_selfr¯   Ò   s6  € õ 1ä
œ"Ÿ*™*Ÿ.™.¬Ó7Ó
8€CßKN 3§?¡?°C×4FÑ4FÑ"GÔTmÓToÑ€KÜ”r—y’y“{Ô$8Ó$:¸GÐS^Óo€EÙ'Ó)€EÔßð	Ü˜VŸZ™Z¨Ó/×5Ð5°2Ó6ˆEŒJØ06·
±
¸6Ó0BÒ0Nœ˜V F�^Ô,ÐTXˆEŒJÜ §
¡
¨9Ó 5× ;Ð ;¸Ó<ˆEŒMÜ! &§*¡*¨ZÓ"8Ó9ˆEŒNðÛð —X‘X˜dŸi™i¨�nÓ-ˆŒ
ô ˜ÓÐøô œ:Ð&ô 	Ùð	ûô ô Ùðús1   ÂE1 Â:AE1 Ä+E1 Ä;+F Å1FÆFÆFÆFc               ó    € V ^8„  d   QhRRRR/# )r   rb   zOptional[Sequence[str]]r   rh   r   )r   s   "r   r   r   ó   s   € ÷ Uñ UÐ#:ð UÀdñ Ur   c                ó  € \         P                  P                  R4      R8w  d   R# \         P                  P                  R4      '       d   R# ^ RIHp T! \        V f    \        P                  R,          4      4      # T 4      4      # )a&  Whether this process is the backend Desktop spawned and owns.

``HERMES_DESKTOP=1`` is inherited by every shell and agent child the app launches, so the
flag alone is not ownership proof (same class as #116107). Desktop hands its backend a
per-spawn credential the terminal pane never receives (and the terminal tool's env policy
strips from agent children): the local pool spawn mints ``HERMES_DASHBOARD_SESSION_TOKEN``,
the SSH spawn passes a 0600 token FILE on argv and deliberately sets no token env var.
``argv`` defaults to this process's own.
ÚHERMES_DESKTOPÚ1FÚHERMES_DASHBOARD_SESSION_TOKENT)Úis_desktop_ssh_backend_argv:é   NN)r2   r¦   r§   Úhermes_cli._startup_fastrµ   r}   rª   rb   )rb   rµ   s   & r   Úis_desktop_owned_backendr¸   ó   s^   € ô 
‡z�z‡~�~Ð&Ó'¨3Ô.ÙÜ	‡z�z‡~�~Ð6×7Ò7ÙÝDá&¤t¸DºL¬C¯H©H°R­LÓ'SÓTÐTÈdÓ'SÓTÐTr   c               ó   € V ^8„  d   QhRR/# ©r   r   rh   r   )r   s   "r   r   r     s   € ÷ rñ r $ñ rr   c                 óœ   € \         P                  P                  R4      R8H  ;'       d$    \         P                  P                  RR4      R8g  # )z­The backend Desktop spawned on this machine: the local pool spawn's credential, never the
SSH spawn (its token arrives as a file on argv, and that host is another computer).r²   r³   r´   rc   )r2   r¦   r§   r   r   r   Úis_local_desktop_backendr¼     s<   € ô �:‰:�>‰>Ð*Ó+¨sÑ2×qÐq´r·z±z·~±~ÐFfÐhjÓ7kÐoqÑ7qÐqr   c               ó   € V ^8„  d   QhRR/# )r   r   z%tuple[Optional[int], Optional[float]]r   )r   s   "r   r   r     s   € ÷ !ñ !Ð#Hñ !r   c                 óž  €  \        \        P                  P                  RR4      4      p V ^ 8:  d   R# \        P                  P                  RR4      pVP                  R4      '       g   V R3#  V \        VP                  R^4      ^,          4      R,          3#   \        \
        3 d    ^ p  L…i ; i  \
        \        3 d    T R3u # i ; i)	zýSpawner ``(pid, create_time)`` from the Electron app's HERMES_PARENT_PID (+ optional
``winms:<ms>`` start marker) parent-death watchdog vars, so ledger lineage works with every
Desktop version without a TS change. ``(None, None)`` when absent/malformed.ÚHERMES_PARENT_PIDrc   NÚHERMES_PARENT_START_MARKERzwinms:rG   g     @�@)NN)
r;   r2   r¦   r§   r|   rY   Ú
startswithr0   rW   Ú
IndexError)r<   Úmarkers     r   r¨   r¨     sÇ   € ðÜœ"Ÿ*™*Ÿ.™.Ð)<¸bÓAÓBˆð �aÔØÐÜ�Z‰Z�^‰^Ð8¸"Ó=€FØ×Ñ˜X×&Ò&Ø˜DÐ Ð ð!ØœE &§,¡,¨s°AÓ"6°qÕ"9Ó:¸VÕCÐCÐCøô ”zÐ"ô ØŠðûô œ
Ð#ô !Ø˜DÐ Ò ð!ús#   ‚)B Á0*B4 ÂB1Â0B1Â4CÃCc               ó4   € V ^8„  d   QhRRRRRRRRR	R
RRRR/# )r   r+   r;   r4   r-   r:   r   r   r   r<   r,   r=   r   r`   r   )r   s   "r   r   r     sF   € ÷ ñ Ø	ðØ*ðØ58ðØHVðàðà0?ðð ñr   c                ó\   € \        WV\        V4      WE\        P                  ! 4       R R7      # )rc   )rb   )r`   r)   Útime)r+   r4   r:   r   r<   r=   s   &&&&&&r   r©   r©     s.   € ô Ø˜'¤:¨lÓ#;¸[ÔZ^×ZcÒZcÓZeÐlnôð r   c               ó    € V ^8„  d   QhRRRR/# )r   r­   r`   r   rh   r   )r   s   "r   r   r   (  s   € ÷ ñ ˜ð ¨ñ r   c                ó~  € \        4       p\        ;_uu_ 4        \        V4      ;'       g    . pV Uu. uFn  p\        VP	                  R4      \
        4      '       g   K*  VR,          V P                  8w  g   KD  \        VR,          VP	                  R4      4      RJg   Kl  VNKp  	  ppVP                  \        V 4      4        ^ RI
Hp V! VP                  4       \        WRRR7        RRR4       R# u upi   \         d$    \        P!                  R	RR
7        RRR4       R# i ; i  + '       g   i     R# ; i)uÆ   Prune dead entries and append ``entry`` â€” the ONLY ledger write path.

Serialized under ``_LEDGER_LOCK`` with an atomic tmp+replace; no writer touches the file
outside this function.

See #91660.
r+   r4   F)Úmkdir_under_hermes_homei€  T)ÚmodeÚensure_asciiNzspawn ledger write failed©Úexc_info)rp   Ú_LEDGER_LOCKr‹   rV   r§   r;   r+   rž   Úappendr   r   rÉ   r    r   r"   r‡   Údebug)r­   rs   r‰   r�   ÚprunedrÉ   s   &     r   r«   r«   (  s  € ô ‹>€Dß	ŽÜ,¨TÓ2×8Ð8°bˆñ ó
Ù�!Ü˜!Ÿ%™% ›,¬×,ô à�%•˜EŸI™IÑ%ô ô # 1 U¥8¨Q¯U©U°=Ó-AÓBÈ%ÐO÷ ˆA‘wð 	ð 
ð 	�‰”f˜U“mÔ$ð		Ý@Ù# D§K¡KÔ0ô ˜d°ÀTÕJØ÷! 
‰ùò
øô ô 	Ü�L‰LÐ4¸tˆLÔDØ÷' 
‰ð"	ú÷# 
�ˆúsL   ™D+³'C5ÁC5Á9#C5Â!C5Â'D+Ã&C:Ã5D+Ã:"D(ÄD+Ä'D(Ä(D+Ä+D<	c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r+   r;   r:   r   r   r   r   rh   r   )r   s   "r   r   r   G  s)   € ÷  ñ  ˜ð   cð  ¸Nð  ÐVZñ  r   c          	     ó†  €  \        V 4      p T ^ 8”  d   \        T 4      MRpTf   R# \	        YY\
        P                  ! 4       \        4       4      p ^ RIpRP                  TP                  T 4      P                  4       R,          4      Tn        \        T4      #   \        \        3 d     R# i ; i  \         d     L0i ; i)a–  Record a CHILD process this process just spawned. Best-effort.

Mirror of :func:`register_self` for children that cannot register themselves (stdio MCP
helpers: arbitrary ``npx``/binary servers never import Hermes code). Records the child's
``(pid, create_time)`` with THIS process as spawner, so a helper whose spawner is provably gone
is a reapable orphan and one whose spawner is alive is never reaped.
FNr£   r¤   )r;   r|   rY   r5   r©   r2   r3   r/   rH   r1   Úcmdlinerb   r   r«   )r+   r:   r   Úchild_creater­   r/   s   &&$   r   Úregister_childrÖ   G  s°   € ðÜ�#‹hˆð 14°a´Ô'¨Ô,¸T€LØÒÙÜ�s¨'ÄÇÂÃÔNbÓNdÓe€EðÛà—X‘X˜fŸn™n¨SÓ1×9Ñ9Ó;¸CÕ@ÓAˆŒ
ô ˜ÓÐøô ”zÐ"ô Úðûô ô Ùðús#   ‚B Á>B2 ÂB/Â.B/Â2C Â?C Úall_installsÚverified_onlyc               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r   r   r×   rh   rØ   r   z
list[dict]r   )r   s   "r   r   r   `  s,   € ÷ ñ Ø#ðØ:>ðØW[ðàñr   c           	     ó  € \        V 4      p\        ;_uu_ 4        \        \        4       4      pRRR4       Xf   . # V Uu. uF§  pV'       g   VP	                  R4      V8X  g   K#  \        VP	                  R4      \        4      '       g   KJ  V'       d,   \        VR,          VP	                  R4      RR7      RJ g   K|  M(\        VR,          VP	                  R4      4      RJg   K¥  VNK©  	  up#   + '       g   i     LÊ; iu upi )uN  Ledger entries for this install, or all installs when explicitly requested.

``verified_only`` requires an exact live PID/create-time pair. The default preserves
unknown processes for reapers, which must not mistake missing proof for a dead process.

Entries whose ``(pid, create_time)`` no longer matches a live process are excluded (PID reuse reads as
dead, thanks to the create-time pair). A corrupt ledger is quarantined and read as empty â€” identical
philosophy to the backend-ownership fix (#89298): never let corruption erase or fake a roster; never let
it block the caller either.
Nr9   r+   r4   T)r’   F)r)   rÎ   r‹   rp   r§   rV   r;   rž   )r   r×   rØ   Úwant_installr‰   r�   s   $$$   r   Úledger_entriesrÜ   `  sØ   € ô ˜lÓ+€Lß	ŽÜ,¬\«^Ó<ˆ÷ 
à‚Øˆ	áóÙˆaß˜AŸE™E )Ó,°Ñ<ô 	
ä�q—u‘u˜U“|¤S×)ô 	
÷ ô    %¥¨!¯%©%°Ó*>ÀtÔLÐPTÐTô 	
ð UÜ#5°a¸µhÀÇÁÀmÓ@TÓ#UÐ]bÐ#b÷	 	
ˆ‘7ñð ÷	 
�üòs5   šC0Á DÁDÁ%"DÂDÂ%DÂ>$DÃ'DÃ0D 	c               ó    € V ^8„  d   QhRRRR/# )r   r­   r~   r   r”   r   )r   s   "r   r   r   {  s   € ÷ 0ñ 0˜4ð 0 Nñ 0r   c                ó°   € V P                  R4      p\        V\        4      '       d   V^ 8:  d   R# \        WP                  R4      4      pVf   R# V'       * # )zYIs the recorded spawner of this entry provably gone? ``None`` when unrecorded/unprovable.r<   Nr=   )r§   rV   r;   rž   )r­   r<   Úalives   &  r   Úspawner_is_deadrà   {  sL   € à—)‘)˜MÓ*€KÜ�k¤3×'Ò'¨;¸!Ô+;ÙÜ˜{¯I©IÐ6FÓ,GÓH€EØ’=ˆ4Ð/¨%¤iÐ/r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r+   r;   r4   r-   r   rh   r   )r   s   "r   r   r   „  s!   € ÷ ñ ˜cð °ð ÀDñ r   c                óB  €  ^ RI pVP                  V 4      p\        W14      '       g   R# VP                  4         VP	                  RR7       R#   TP
                   d    TP                  4         R# i ; i  \         d    \        P                  RT RR7        R# i ; i)zYSIGTERM, 2 s grace, then SIGKILL. False when the incarnation moved on or is already gone.NFrŽ   ©ÚtimeoutTz"orphan terminate failed for pid %srÌ   )
r/   r1   r‘   Ú	terminateÚwaitÚTimeoutExpiredÚkillr   r‡   rÐ   )r+   r4   r/   r�   s   &&  r   Ú_terminate_then_killré   „  s‘   € ðÛà�~‰~˜cÓ"ˆÜ  ×3Ò3ÙØ�‰Ôð	Ø�I‰I˜cˆIÔ"ñ øð ×$Ñ$ô 	Ø�I‰I�KÙð	ûô ô Ü�‰Ð9¸3ÈˆÔNÚðús3   ‚&A7 ªA7 »A Á!A4Á0A7 Á3A4Á4A7 Á7#BÂBc               ó    € V ^8„  d   QhRRRR/# )r   r+   r;   r   rh   r   )r   s   "r   r   r   —  s   € ÷ ñ ˜Cð  Dñ r   c                ó  €  ^ RI HpHpHp V! V 4      R9  d   R# Y! 4       ;'       g    R9   d   R# ^ RIp^ RIp\        RVP	                  4       VP                  V 4      P                  4       ,
          4      V8¬  #   \         d     R# i ; i)u½  Null-spawner owner with no live supervisor: reparented to init, old, lock-unclaimed.

Mirrors ``_reap_orphaned_desktop_local_serves``: operator-managed remote backends legitimately
sit at ppid 1 (systemd, an exited sshd), so ppid alone is never proof â€” the SSH-lock claim
and the 180 s lock-write grace exclude live-supervised backends. Windows: the ppid probe is
unavailable there, so this rung never fires (desktop tree-kill reaps instead).
)Ú_REAP_MIN_AGE_SECONDSÚ_lock_owned_serve_pidsÚ_process_ppidFNg        )é    r¶   r   )
Úhermes_cli.dashboard_procsrì   rí   rî   rÆ   r/   Úmaxr1   r4   r   )r+   rì   rí   rî   Ú_timer/   s   &     r   Ú_reparented_orphanró   —  s‚   € ð÷	Jñ 	Já˜Ó VÔ+ÙØÐ)Ó+×1Ð1¨rÔ2ÙÛãÜ�3˜Ÿ
™
› v§~¡~°cÓ':×'FÑ'FÓ'HÕHÓIÐMbÑbÐbøÜô Úðús!   ‚A; ›A; ©A; ²AA; Á;B
Â	B
Úkill_fnc               ó$   € V ^8„  d   QhRRRRRR/# )r   r+   r;   r4   r-   r   r,   r   )r   s   "r   r   r   ®  s$   € ÷ -ñ -Ø	ð-Ø*ð-Ø@Mñ-r   c               óv  a a€  \        S 4      o S ^ 8:  g   S \        P                  ! 4       8X  d   R#  \        TT 3R l\        4        4       R4      pTf   R# \        T4      RJd'   TP                  R4      f   \        S 4      '       g   R#  Te
   T! S 4       M$\        S TP                  R4      4      '       g   R#  \        P                  RTP                  R	4      S 4       S #   \        \        3 d     R# i ; i  \         d     R# i ; i  \         d    \        P                  RTRR7        R# i ; i)
uL  Kill the conflicting host owner when the ledger PROVES it is a dead session's orphan.

HELD_BY_OTHER rung for #121964: the owner PID is alive (re-parented to init), so
``record_is_stale()`` never fires and a fresh backend would loop observe-only against the
same orphan forever. Reaped only with a live ``serve``/``dashboard``/``gateway`` entry for
THIS install whose spawner is provably dead â€” or whose spawner was never recorded and which
is reparented to init, old, and claimed by no Desktop SSH lock. Live-spawned, lock-claimed,
young, interactive, self, or unprovable owners are never touched. Returns the reaped pid,
else ``None``; never raises.

# ponytail: a dead session's surviving backend.lock.json still shields its orphan (the lock
# has no session liveness); clearing stale locks is a Desktop-side lifecycle job, not this rung.
Nc              3  ó"  <"  € T F„  pVP                  R 4      S8X  g   K  VP                  R4      \        9   g   K7  VP                  R4      e7   Se3   \        \        VR,          4      \        S4      ,
          4      R8  g   K€  Vx € K†  	  R# 5i)r+   r:   r4   NrŽ   )r§   Ú_BACKEND_OWNER_PURPOSESr�   r0   )Ú.0r�   r4   r+   s   & €€r   Ú	<genexpr>Ú.reap_orphaned_backend_owner.<locals>.<genexpr>Å  s~   øé € ð OÑ(�1Ø—‘�e“ Ñ#ô à—‘�yÓ!Ô%<Ñ<ô ð —%‘%˜Ó&Ò.°+Ò2EÜœ˜q Õ/Ó0´5¸Ó3EÕEÓFÈÑL÷	 ŠQÓ(ùs   ƒB B¼ABÂ
BTr<   r4   z backend owner reap failed for %srÌ   z'reaped orphaned %s backend owner pid %sr:   )r;   r|   rY   r2   r3   ÚnextrÜ   r   rà   r§   ró   ré   r‡   rÐ   Úinfo)r+   r4   rô   r­   s   ff$ r   Úreap_orphaned_backend_ownerrþ   ®  s(  ù€ ðÜ�#‹hˆð ˆa„x�3œ"Ÿ)š)›+Ô%Ùð	ÜõOœÔ(ó Oð
 óˆð ‚}ÙÜ�uÓ TÓ)Ø�I‰I�mÓ$Ò,Ô1CÀC×1HÒ1HÙðØÒÙ�C�LÜ% c¨5¯9©9°]Ó+C×DÒDÙð Eô
 ‡K�KÐ9¸5¿9¹9ÀYÓ;OÐQTÔUØ€Jøô9 ”zÐ"ô Úðûô ô Úðûô ô Ü�‰Ð7¸ÈˆÔNÚðús:   „C' ³C? ÂD Â!D Ã'C<Ã;C<Ã?DÄDÄ#D8Ä7D8c               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   z	list[int]r   )r   s   "r   r   r   Þ  s   € ÷ ,ñ ,¨~ð ,ÐW`ñ ,r   c                óV  € . p \        V R7      p\        P                  ! 4       pT Fñ  p TP	                  R4      pTP	                  R4      R8w  g$   \        T\        4      '       d   T^ 8:  g   Yd8X  d   KP  \        T4      RJd   Ka  Te
   T! T4       Mr^ RIpTP                  T4      p\        Y…P	                  R4      4      '       g   K¥  \        '       d   \        T4       M#TP                  4         TP                  RR	7       TP#                  T4       Kó  	  T'       d!   \$        P)                  R\+        T4      T4       T#   \         d    Tu # i ; i  TP                   d    TP!                  4         Lui ; i  \         d    \$        P'                  R
TRR7        EK€  i ; i)u.  Kill ledger-registered stdio MCP helpers whose spawner is provably dead.

Ledger-driven startup-sweep rung (not cmdline-heuristic): a helper is reaped ONLY when it has a
live ``mcp-helper`` entry for THIS install AND ``spawner_is_dead`` is ``True`` â€” never
``None``/unprovable, never a live spawner.
rO   r+   r:   r   TNr4   rŽ   rã   z$mcp-helper orphan reap failed for %srÌ   z*reaped %d orphaned stdio MCP helper(s): %s)rÜ   r   r2   r3   r§   rV   r;   rà   r/   r1   r‘   Ú_IS_WINDOWSÚ_kill_process_tree_windowsrå   ræ   rç   rè   rÏ   r‡   rÐ   rý   rX   )	r   rô   Úreapedr‰   Úown_pidr­   r+   r/   r�   s	   $$       r   Úreap_orphaned_mcp_helpersr  Þ  so  € ð €FðÜ ¨lÔ;ˆô �iŠi‹k€GÛˆð	WØ—)‘)˜EÓ"ˆCØ�y‰y˜Ó# |Ô3¼:ÀcÌ3×;OÒ;OÐSVÐZ[ÔS[Ð_bÔ_mÙÜ˜uÓ%¨TÓ1ÙØÒ"Ù˜•ãà—~‘~ cÓ*�Ü(¨¯y©y¸Ó/G×HÒHÙ÷ “;Ü.¨tÕ4à—N‘NÔ$ð$ØŸ	™	¨#˜	Ô.ð �M‰M˜#Öñ5 ÷: Ü�‰Ð@Ä#ÀfÃ+ÈvÔVØ€MøôE ô ØŠðûð6 "×0Ñ0ô $ØŸ	™	žð$ûô ô 	WÜ�L‰LÐ?ÀÐQUˆL×VÐVð	Wús`   „E «A
F Á7F ÂAF ÃF ÃF Ã5EÄF ÅEÅEÅ!E=Å:F Å<E=Å=F Æ #F(Æ'F(c               ó   € V ^8„  d   QhRR/# )r   r   ÚNoner   )r   s   "r   r   r     s   € ÷ ñ ¨ñ r   c                óÚ  € ^ RI p V P                  RR7      pV F  p VP                  4        K  	   V P                  4         VP                  W .,           RR7      w  rET F  p TP                  4        K  	  R#   TP                  TP                  \        3 d    . p LŠi ; i  \         d     Kš  i ; i  \         d     L‚i ; i  \         d     R# i ; i  \         d     K‡  i ; i)u  Terminate *proc* and every still-alive descendant (npx.cmd â†’ node.exe), Windows-only
(#61059): without a pgid there is no group-kill, and grandchildren reparent to nothing
(ParentId=null) once the direct child exits, so they must be reached through the tree.NT)Ú	recursiverŽ   rã   )	r/   Úchildrenr�   ÚAccessDeniedr"   rå   r   Ú
wait_procsrè   )r�   r/   ÚdescendantsÚchildr[   rß   Úsurvivors   &      r   r  r    sö   € ó ðØ—m‘m¨d�mÓ3ˆó ˆð	Ø�O‰OÖñ ð
Ø�‰ÔðØ×$Ñ$ [°6Õ%9À3Ð$ÓG‰ˆó ˆð	Ø�M‰MŽOó øð × Ñ  &×"5Ñ"5´wÐ?ô ØŠðûô
 ô 	Úð	ûô ô Ùðûô ô Úðûô
 ô 	Úð	úsW   †A< žB&³B8 ÁC	 Á&CÁ<$B#Â"B#Â&B5Â4B5Â8CÃCÃ	CÃCÃC*Ã)C*c               ó   € V ^8„  d   QhRR/# rº   r   )r   s   "r   r   r   .  s   € ÷ :ñ :¨$ñ :r   c                 ó  a	a
aa€ \         '       d	   \        e	   \        RJ#  ^ RIo^ RIHo SP	                  RRR7      p RpRpRp^	p ! V3R	 lR
SP
                  4      o	 ! VV3R lRSP
                  4      o
 ! V	V
V3R lRSP
                  4      pV P                  RR4      pV'       g   R# V! 4       pW,          V,          VP                  n        V P                  WdSP                  V4      SP                  V4      4      pV'       d&   V P                  W`P                  4       4      '       g   V P                  V4       R# Vs\        P!                  R4       R#   \"         d    \        P!                  RRR7        R# i ; i)zöPlace this process in a job that dies (whole tree) when we die. Windows-only, idempotent.

``BREAKAWAY_OK`` keeps children spawned with ``CREATE_BREAKAWAY_FROM_JOB`` (gateway relaunch
during update, detached watchers) escaping exactly as before.
N)ÚwintypesÚkernel32T)Úuse_last_errori    i   i   c                  ó\   <€ ] tR tRtR U u. uF  q SP                  3NK  	  up tRtR# u up i )Ú5attach_self_to_kill_on_close_job.<locals>.IO_COUNTERSiB  r   N)ÚReadOperationCountÚWriteOperationCountÚOtherOperationCountÚReadTransferCountÚWriteTransferCountÚOtherTransferCount)r>   r?   r@   rA   Úc_ulonglongÚ_fields_rC   )ÚnÚctypess   0€r   ÚIO_COUNTERSr  B  s7   ø† ñ:Qó Rñ :Q°A˜F×.Ñ.Ó/ñ :Qñ RŒHùò Rs   Œ)r!  c            
      ó8  <€ ] tR tRtR ZP                  3R ZP                  3R ZP
                  3R Z P                  3R Z P                  3R ZP
                  3R Z P                   ZP                  4      3R	 ZP
                  3R
 ZP
                  3.	t	Rt
R# )ÚKattach_self_to_kill_on_close_job.<locals>.JOBOBJECT_BASIC_LIMIT_INFORMATIONiG  ÚPerProcessUserTimeLimitÚPerJobUserTimeLimitÚ
LimitFlagsÚMinimumWorkingSetSizeÚMaximumWorkingSetSizeÚActiveProcessLimitÚAffinityÚPriorityClassÚSchedulingClassr   N)r>   r?   r@   rA   ÚLARGE_INTEGERÚDWORDÚc_size_tÚPOINTERÚULONGr  rC   )r   r  s   €€r   Ú!JOBOBJECT_BASIC_LIMIT_INFORMATIONr#  G  s‰   ø† à*©H×,BÑ,BÐCÐF[Ñ]e×]sÑ]sÐEtØ™xŸ~™~Ð.Ð1HÉ&Ï/É/Ð0ZØ(©&¯/©/Ð:Ð=QÑS[×SaÑSaÐ<bØ™VŸ^™^©H¯N©NÓ;Ð<¸ÑPX×P^ÑP^Ð>_Ø"¡H§N¡NÐ3ðŒHr   r2  c                  óD   <€ ] tR tRtR Z3R Z 3.V3R lR 4       OtRtR# )ÚNattach_self_to_kill_on_close_job.<locals>.JOBOBJECT_EXTENDED_LIMIT_INFORMATIONiP  ÚBasicLimitInformationÚIoInfoc              3  	ó>   <"  € T F  qSP                   3x € K  	  R # 5i©N)r/  )rù   r  r   s   & €r   rú   ÚXattach_self_to_kill_on_close_job.<locals>.JOBOBJECT_EXTENDED_LIMIT_INFORMATION.<genexpr>T  s#   øé € ð kñ 1j¨1�f—o‘oÕ&ó 1jùs   ƒr   N)ÚProcessMemoryLimitÚJobMemoryLimitÚPeakProcessMemoryUsedÚPeakJobMemoryUsed)r>   r?   r@   rA   r  rC   )r!  r2  r   s   €€€r   Ú$JOBOBJECT_EXTENDED_LIMIT_INFORMATIONr4  P  s2   ø† à(Ñ*KÐLØ™;Ð'ðôkñ 1jó kðŒHr   r>  Fz$attached to kill-on-close job objectzjob object self-attach failedrÌ   )r  Ú_JOB_HANDLEr   r  ÚWinDLLÚ	StructureÚCreateJobObjectWr5  r&  ÚSetInformationJobObjectÚbyrefÚsizeofÚAssignProcessToJobObjectÚGetCurrentProcessÚCloseHandler‡   rÐ   r   )r  Ú"JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSEÚJOB_OBJECT_LIMIT_BREAKAWAY_OKÚ$JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OKÚ!JobObjectExtendedLimitInformationr>  Újobrý   Úokr!  r2  r   r  s            @@@@r   Ú attach_self_to_kill_on_close_jobrO  .  sb  û€ ÷ ‹;œ+Ò1Ü $Ð&Ð&ð1ÛÝ#à—=‘= ¸D�=ÓAˆà-3Ð*Ø(.Ð%Ø/5Ð,Ø,-Ð)÷	R˜&×*Ñ*ô 	R÷
	ð 	°×0@Ñ0@ô 	÷	ñ 	°6×3CÑ3Cô 	ð ×'Ñ'¨¨dÓ3ˆßÙÙ3Ó5ˆà.ÕNÐQuÕuð 	×"Ñ"Ô-ð ×-Ñ-¨cÐV\×VbÑVbÐcgÓVhÐjp×jwÑjwÐx|Ój}Ó~ˆß˜×:Ñ:¸3×@ZÑ@ZÓ@\×]Ò]Ø× Ñ  Ô%ÙØˆÜ�‰Ð;Ô<ÙøÜô Ü�‰Ð4¸tˆÔDÚðús   ¢B	E Â.BE ÅE Å"FÆF>   ÚserveÚgatewayÚ	dashboardr   r8  )>Ú__doc__Ú
__future__r   r#   rz   Úloggingr2   Úplatformrª   Ú	threadingrÆ   Údataclassesr   r   Úpathlibr   Útypingr   r   Úutilsr   Ú	getLoggerr>   r‡   rP   rI   rn   Ú	frozensetÚREAPABLE_PURPOSESrø   Úsystemr  r?  ÚLockrÎ   r)   r5   r8   rL   rQ   r^   r`   rp   r‚   r‹   r‘   rž   r¯   r¸   r¼   r¨   r©   r«   rÖ   rÜ   rà   ré   ró   rþ   r  r  rO  r   r   r   Ú<module>ra     s®  ðñõ #ã Û Û Û 	Û Û 
Û Û ß )Ý ß %å #à	×	Ò	˜8Ó	$€à€Ø€Ø%€ñ ÒMÓNÐ ð ,¨|¨nÕ<Ð à�oŠoÓ 9Ñ,€ð €Ø�~Š~Ó€÷Q÷ ñ �$Ô÷$ð $ó ð$ðfÀD÷ fðP¸d÷ Põ
Hð& ÷ð ó ðõ(	9õ\õ*
õ\ð
ÐQV÷ ð, À$ð  Ðae÷  ÷BUõ&rõ!õ&õð> ÈT÷  ð2Ø&*ðØAFðØ^c÷õ60õõ&ð.-Ø7;÷-ð`,Àð ,Èd÷ ,õd÷<:r   