
    `gjw                        d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	Z	ddl
mZ dZdddZdd	Zdd
ZddZddZdddd	 	 	 	 	 	 	 	 	 	 	 ddZdgZy)zLBounded, non-destructive readiness probes for authenticated health surfaces.    )annotationsN)Path)Any)get_hermes_homeg     V@c                >    d| i}|r||d<   |j                  |       |S )Nstatusdetail)update)r   r	   extraresults       D/root/.hermes/venv/lib/python3.12/site-packages/gateway/readiness.py_checkr      s*    &/F!x
MM%M    c                   | dz  }|j                         st        dd      S 	 d|j                          d}t        j                  |dd      5 }|j                  d	       |j                  d
      j                          d d d        t        d      S # 1 sw Y   xY w# t        $ r)}t        dt        |      j                        cY d }~S d }~ww xY w)Nzstate.dbokznot initializedzfile:z?mode=roTg      ?)uritimeoutzPRAGMA query_only = ONz&SELECT name FROM sqlite_master LIMIT 1degraded)
existsr   as_posixsqlite3connectexecutefetchone	Exceptiontype__name__)homepathr   connexcs        r   _probe_state_dbr"      s    *D;;=d-..
6 dmmo&h/__SdC8 	NDLL12LLABKKM	N d|	N 	N  6j$s)"4"4556s5   ,B 1B B BB 	C(CCCc                T   | dz  }|j                         st        dd      S 	 t        j                  |j	                  d            }|t        |t              st        dd      S t        d      S # t        $ r-}t        ddt        |      j                   d	      cY d }~S d }~ww xY w)
Nzconfig.yamlr   zusing defaultszutf-8)encodingr   ztop level is not a mappingzinvalid config ())
r   r   yaml	safe_load	read_text
isinstancedictr   r   r   )r   r   rawr!   s       r   _probe_configr,   +   s    -D;;=d,--LnnT^^W^=>?:c4#8*&BCCd| Lj$4T#Y5G5G4H"JKKLs$   AA1 &
A1 1	B':"B"B'"B'c                J   	 t        j                  |       }|j                  r&t        |j                  |j                  z  dz  d      nd}|t
        k\  rdnd}t        |||j                        S # t        $ r)}t        dt        |      j                        cY d }~S d }~ww xY w)Nd      g        r   r   )used_percent
free_bytes)shutil
disk_usagetotalroundused_DISK_DEGRADED_PERCENTr   freer   r   r   )r   usageused_pctr   r!   s        r   _probe_diskr;   8   s    6!!$'AF5%**u{{2c91=RU'+AAtf8

KK 6j$s)"4"4556s   A-A0 0	B"9BB"B"c                   t        | j                  d      xs d      }| j                  d      }d}d}t        |t              r+t	        |      }t        d |j                         D              }|dv rdnd}t        ||||	      S )
Ngateway_stateunknown	platformsr   c              3     K   | ]W  }t        |t              rEt        |j                  d       xs |j                  d      xs d      j	                         dv rd Y yw)stater    >   r   running	connectedr/   N)r)   r*   strgetlower).0values     r   	<genexpr>z!_probe_gateway.<locals>.<genexpr>I   sW      
%&EIIg&C%))H*=CDJJL-. 
s   AA>   rC   drainingr   r   )rA   connected_platformsr?   )rE   rF   r)   r*   lensumvaluesr   )runtime_statusrA   r?   rD   
configuredr   s         r   _probe_gatewayrR   B   s    ""?3@yAE"";/IIJ)T"^
 
"))+
 
	 55T:F&9PZ[[r   )active_api_runsprocess_completion_queue_depthactive_delegationsc                   t               }t        |t              r|ni }t        |      t	        |      t        t        | xs d      j                         rdnd      t        |      t        |      t        dt        dt        |            t        dt        |            t        dt        |                  d}t        d |j                         D              rdnd}||dS )	a  Return bounded readiness diagnostics without mutating runtime state.

    The detailed health endpoint is authenticated. Even there, probes expose
    status and counts only: never config values, credentials, paths, commands,
    queue payloads, or exception messages.
    rB   r   r   r   )rS   process_completionsrU   )state_dbconfigmodeldiskgatewaybackground_queuesc              3  D   K   | ]  }|j                  d       dk(    yw)r   r   N)rF   )rH   items     r   rJ   z,collect_runtime_readiness.<locals>.<genexpr>q   s     Q$((8,4Qs    )r   checks)r   r)   r*   r"   r,   r   rE   stripr;   rR   maxintallrO   )	configured_modelrP   rS   rT   rU   r   runtimer`   overalls	            r   collect_runtime_readinessrh   T   s     D *>4 @nbG#D)%$4$: ; A A CTD!!'*#3#78 #As+I'J K"1c*<&=>	
F QQQdWaG00r   rh   )N)r   rE   r	   z
str | Noner   r   returndict[str, Any])r   r   ri   rj   )rP   rj   ri   rj   )re   rE   rP   zdict[str, Any] | NonerS   rc   rT   rc   rU   rc   ri   rj   )__doc__
__future__r   r2   r   pathlibr   typingr   r&   hermes_constantsr   r7   r   r"   r,   r;   rR   rh   __all__ r   r   <module>rr      s    R "      ,  6"
L6\, *+11 *1 	1
 %(1 1 1B '
'r   