§
    øžyj‰  ã            	       ó  — d Z ddlZddlmZ ddlmZ dZdZdZ e	ed         d	         ¦  «        Z
 e	ed         d
         ¦  «        Z	 ddeeef         dz  defd„Zdefd„Zdede	fd„Zdede	fd„Zddœde	de	de	de	fd„ZdS )zBShared gateway restart constants and supervisor detection helpers.é    N)ÚMapping)ÚDEFAULT_CONFIGéK   éN   Ú"HERMES_GATEWAY_EXTERNAL_SUPERVISORÚagentÚrestart_drain_timeoutÚrestart_after_turn_timeoutÚenvironÚreturnc                 óZ  — | €t           j        n| }|                     d¦  «        rdS |                     d¦  «        rdS |                     dd¦  «        }|r|dk    rdS t          |                     t          d¦  «        ¦  «                             ¦   «                              ¦   «         dv S )	z=Return whether this gateway process is owned by a supervisor.NÚINVOCATION_IDTÚHERMES_S6_SUPERVISED_CHILDÚXPC_SERVICE_NAMEÚ Ú0>   Ú1ÚonÚyesÚtrue)Úosr   ÚgetÚstrÚEXTERNAL_GATEWAY_SUPERVISOR_ENVÚstripÚlower)r   ÚenvÚxpc_services      ú5/home/ragecks/.hermes/hermes-agent/gateway/restart.pyÚis_gateway_supervisor_processr    %   s³   € ð  ˜�"Œ*ˆ*¨W€CØ
‡w‚wˆÑÔð ØˆtØ
‡w‚wÐ+Ñ,Ô,ð ØˆtØ—'’'Ð,¨bÑ1Ô1€KØð �{ cÒ)Ð)ØˆtÝˆs�wŠwÕ6¸Ñ;Ô;Ñ<Ô<×BÒBÑDÔD×JÒJÑLÔLð Qð ð ó    c                  ó~   — t           j                             d¦  «        pt           j                             d¦  «        S )u²  Return whether the gateway is running inside a container for restart
    routing purposes (Docker/Podman â‡’ the detached setsid path dies with the
    cgroup; exit-75 service restart is the only viable path).

    Extracted from the inline probe in the /restart handler so tests can mock
    container detection hermetically â€” a real ``/.dockerenv`` on a
    containerized CI runner otherwise flips the routing under the test.
    z/.dockerenvz/run/.containerenv)r   ÚpathÚexists© r!   r   Úis_container_restart_contextr&   9   s,   € õ Œ7�>Š>˜-Ñ(Ô(ÐP­B¬G¯NªNÐ;OÑ,PÔ,PÐPr!   Úrawc                 óÔ   — 	 t          | pd¦  «                             ¦   «         rt          | ¦  «        nt          }n# t          t
          f$ r
 t          cY S w xY wt          d|¦  «        S )zEParse a configured drain timeout, falling back to the shared default.r   ç        )r   r   ÚfloatÚ%DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUTÚ	TypeErrorÚ
ValueErrorÚmax©r'   Úvalues     r   Úparse_restart_drain_timeoutr1   E   sn   € ð5Ý! # )¨™nœn×2Ò2Ñ4Ô4Ð_•�c‘
”
�
Õ:_ˆˆøÝ•zÐ"ð 5ð 5ð 5Ý4Ð4Ð4Ð4ð5øøøåˆs�E‰?Œ?Ðs   ‚9< ¼AÁAc                 óò   — | €t           S t          | t          ¦  «        r|                      ¦   «         st           S 	 t	          | ¦  «        }n# t
          t          f$ r
 t           cY S w xY wt          d|¦  «        S )uÙ   Parse the after-turn wait cap for in-band restart, falling back to default.

    ``0`` is a deliberate disable (legacy immediate drain) and must not fall
    through to the default â€” unlike empty/missing input.
    Nr)   )Ú*DEFAULT_GATEWAY_RESTART_AFTER_TURN_TIMEOUTÚ
isinstancer   r   r*   r,   r-   r.   r/   s     r   Ú parse_restart_after_turn_timeoutr5   N   s‚   € ð €{Ý9Ð9Ý�#•sÑÔð : C§I¢I¡K¤Kð :Ý9Ð9ð:Ý�c‘
”
ˆˆøÝ•zÐ"ð :ð :ð :Ý9Ð9Ð9Ð9ð:øøøåˆs�E‰?Œ?Ðs   »A ÁA&Á%A&g      .@)ÚheadroomÚdrain_timeoutÚafter_turn_timeoutr6   c                ób  — 	 t          t          | ¦  «        d¦  «        }n# t          t          f$ r d}Y nw xY w	 t          t          |¦  «        d¦  «        }n# t          t          f$ r d}Y nw xY w	 t          t          |¦  «        d¦  «        }n# t          t          f$ r d}Y nw xY w||z   |z   S )aT  Seconds a CLI should wait for the gateway PID to exit after SIGUSR1.

    In-band restart may defer ``stop()`` until active turns finish
    (``after_turn_timeout``) and then spend up to ``drain_timeout`` inside
    ``stop()``. Callers that fall back to a hard kill on wait expiry must
    cover both phases or they reintroduce #77184.
    r)   )r.   r*   r,   r-   )r7   r8   r6   ÚdrainÚ
after_turnÚmargins         r   Ú resolve_restart_exit_wait_budgetr=   _   sæ   € ðÝ•E˜-Ñ(Ô(¨#Ñ.Ô.ˆˆøÝ•zÐ"ð ð ð ØˆˆˆðøøøðÝ�Ð1Ñ2Ô2°CÑ8Ô8ˆ
ˆ
øÝ•zÐ"ð ð ð Øˆ
ˆ
ˆ
ðøøøðÝ•U˜8‘_”_ cÑ*Ô*ˆˆøÝ•zÐ"ð ð ð Øˆˆˆðøøøà�:Ñ Ñ&Ð&s/   ‚   6µ6ºA ÁA.Á-A.Á2B ÂB&Â%B&)N)Ú__doc__r   Úcollections.abcr   Úhermes_cli.configr   Ú!GATEWAY_SERVICE_RESTART_EXIT_CODEÚGATEWAY_FATAL_CONFIG_EXIT_CODEr   r*   r+   r3   r   Úboolr    r&   Úobjectr1   r5   r=   r%   r!   r   ú<module>rE      s„  ðØ HÐ Hà 	€	€	€	Ø #Ð #Ð #Ð #Ð #Ð #à ,Ð ,Ð ,Ð ,Ð ,Ð ,ð %'Ð !ð "$Ð ð
 #GÐ à(-¨Ø�7ÔÐ3Ô4ñ)ô )Ð %ð .3¨UØ�7ÔÐ8Ô9ñ.ô .Ð *ð )-ðð Ø�S˜#�XÔ Ñ%ðà	ðð ð ð ð(	Q dð 	Qð 	Qð 	Qð 	Qð Vð °ð ð ð ð ð¨&ð °Uð ð ð ð ð* ð	'ð 'ð 'Øð'àð'ð ð	'ð
 ð'ð 'ð 'ð 'ð 'ð 'r!   