
    `gj                    f   U d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZmZmZmZ  ej                   e      ZdZdZ ej*                         Z ej.                         Zdaded	<   dd
ZddZ	 	 	 	 ddZe
dd	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZeee
dd	 	 	 	 	 	 	 	 	 ddZ dddZ!y)z9Background keepalive for long-lived Nous Portal sessions.    )annotationsN)Optional)!ACCESS_TOKEN_REFRESH_SKEW_SECONDSNOUS_INVOKE_JWT_MIN_TTL_SECONDS	AuthError_agent_key_is_usable_is_expiringget_provider_auth_state resolve_nous_runtime_credentialsi`T  <   Optional[threading.Thread]_keepalive_threadc                    | t        |       S 	 t        t        j                  dd            S # t        t        f$ r Y yw xY w)NHERMES_NOUS_TIMEOUT_SECONDS15g      .@)floatosgetenv	TypeError
ValueError)values    Q/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/nous_auth_keepalive.py_timeout_secondsr      sE    U|RYY<dCDDz" s   . A A c                P    t        | dd       t        | dd       t        | dd       dS )N	agent_keyagent_key_expires_atscope)r   r   r   )getattr)entrys    r   _entry_stater    '   s1    UK6 '/Et L.     c                   	 ddl m}  |d      }|r|j                         sy	 |j                         }|yt        t        |dd      t              }t        t        |      |       }|s|s)|j                         }|yt        j	                  d	       y
y
# t        $ r }t        j	                  d|       Y d}~yd}~ww xY w# t        $ r }t        j	                  d|       Y d}~yd}~ww xY w)zRefresh the current Nous credential pool entry when it is stale.

    Returns True when a pool entry exists and is usable/refreshed, False when a
    pool exists but no entry can be used, and None when no Nous pool exists.
    r   )	load_poolnousz4Nous auth keepalive: credential pool unavailable: %sNz9Nous auth keepalive: credential pool selection failed: %sF
expires_atz4Nous auth keepalive: refreshed credential pool entryT)agent.credential_poolr#   	Exceptionloggerdebughas_credentialsselectr	   r   r   r   r    try_refresh_current)min_key_ttl_secondsr#   poolexcr   access_expiring
key_usable	refresheds           r   _refresh_selected_pool_entryr3   /   s    3 
 t++-
 }"|T*)O &l5&9;NOJj,,.	KL9  KSQ  PRUVs.   B C 	B>B99B>	C*
C%%C*r-   timeout_secondsc                   t        dt        |             } t        |       }||S t        d      }|sy	 t	        t        |             t        j                  d       y# t        $ rC}|j                  rt        j                  d	|       nt        j                  d
|       Y d}~yd}~wt        $ r }t        j                  d
|       Y d}~yd}~ww xY w)z5Refresh Nous auth once if credentials are configured.r   )r-   Nr$   F)r5   z3Nous auth keepalive: refreshed singleton auth stateTz)Nous auth keepalive requires re-login: %szNous auth keepalive failed: %s)maxintr3   r
   r   r   r(   r)   r   relogin_requiredinfor'   )r-   r5   pool_resultstater/   s        r    refresh_nous_auth_keepalive_oncer=   [   s     b#&9":;./K #F+E(,_=	
 	JK KKCSILL93? 5s;s#   *A   	C)9B''C3CCc                   |dkD  r| j                  |      ry | j                         s0t        ||       | j                  |       | j                         s/y y )Nr   r4   )waitis_setr=   )
stop_eventinterval_secondsinitial_delay_secondsr-   r5   s        r   _keepalive_looprD   ~   sR     q Z__5J%K!( 3+	
 	() !r!   rB   rC   r-   r5   c                   | dk  ryt         5  t        #t        j                         rt        cddd       S t        j	                          t        j                  t        t        ft        |       t        dt        |            t        dt        |            |ddd      at        j                          t        j                  d       t        cddd       S # 1 sw Y   yxY w)	z2Start the process-wide Nous auth keepalive thread.r   Nr   rE   Tznous-auth-keepalive)targetargskwargsdaemonnamezNous auth keepalive started)_keepalive_lockr   is_alive_keepalive_stopclear	threadingThreadrD   r8   r7   startr(   r)   rE   s       r   start_nous_auth_keepaliverS      s     1 
 !(->-G-G-I$! ! 	%,,"!#$'(8$9),Q4I0J)K'*2s3F/G'H#2	 &
 	!23 '! ! !s    CBCC#c                    t         5  t        }t        j                          ddd       "|j	                         r|j                  |        t         5  t        |u rdaddd       y# 1 sw Y   GxY w# 1 sw Y   yxY w)z@Stop the keepalive thread. Intended for graceful shutdown/tests.N)timeout)rL   r   rN   setrM   join)rU   threads     r   stop_nous_auth_keepaliverY      sw     
 " foo/G$	 %& $% % 
% %s   A(A4(A14A=)r   Optional[float]returnr   )r   objectr[   dict)r-   r8   r[   zOptional[bool])r-   r8   r5   rZ   r[   bool)rA   zthreading.EventrB   r8   rC   r8   r-   r8   r5   rZ   r[   None)
rB   r8   rC   r8   r-   r8   r5   rZ   r[   r   )g      @)rU   r   r[   r_   )"__doc__
__future__r   loggingr   rP   typingr   hermes_cli.authr   r   r   r   r	   r
   r   	getLogger__name__r(   $NOUS_AUTH_KEEPALIVE_INTERVAL_SECONDS)NOUS_AUTH_KEEPALIVE_INITIAL_DELAY_SECONDSLockrL   EventrN   r   __annotations__r   r    r3   r=   rD   rS   rY    r!   r   <module>rm      sC   ? "  	     
		8	$'2 $,. ) ).."!)//#04 - 4)) )\  ?'+   %  
	 F** * 	*
 * %* 
** A!J>'+!! ! 	!
 %!  !D
%r!   