
    oq'j{                      d Z ddlm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mZ ddlmZmZ ddlmZmZmZmZ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  dd
lm!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.  ej/        e0          Z1dWdZ2dZ3dZ4dZ5 e6h d          Z7dZ8dZ9dZ:dZ;e; dZ<dZ=dZ>dZ?dZ@e=e>e?e@hZAdZBdZCdZDdZE e6h d          ZFe G d d                       ZGdXd$ZHdYd(ZIdZd+ZJd[d.ZKd\d2ZLd]d4ZMd^d8ZNd_d:ZOd`d<ZPdadbd>ZQdadcdBZRdddDZSdedFZTdfdHZUdIZV G dJ dK          ZWdgdMZXdhdNZYdidPZZdidQZ[djdTZ\dkdUZ]dldVZ^dS )mz<Persistent multi-credential pool for same-provider failover.    )annotationsN)	dataclassfieldsreplace)datetimetimezone)AnyDictListOptionalSetTuple)OPENROUTER_BASE_URL)load_env)is_borrowed_credential_source$sanitize_borrowed_credential_payload)'CODEX_ACCESS_TOKEN_REFRESH_SKEW_SECONDSPROVIDER_REGISTRY_auth_store_lock_codex_access_token_is_expiring_decode_jwt_claims_load_auth_store_load_provider_state_resolve_kimi_base_url_resolve_zai_base_url_save_auth_store_save_provider_state_store_provider_stateread_credential_poolwrite_credential_poolreturnOptional[dict]c                 F    	 ddl m}   |             S # t          $ r Y dS w xY w)z.Load config.yaml, returning None on any error.r   load_configN)hermes_cli.configr%   	Exceptionr$   s    :/home/ubuntu/.hermes/hermes-agent/agent/credential_pool.py_load_config_safer)   +   sG    111111{}}   tts    
  ok	exhausteddead>   invalid_grantinvalid_tokentoken_revokedtoken_invalidatedunauthorized_clientrefresh_token_reusediQ oauthapi_keymanualz:device_code
fill_firstround_robinrandom
least_usedi,    zcustom:>   tlsscope	client_id
expires_in
token_typeobtained_atagent_key_idsecret_sourceportal_base_urlagent_key_reusedsecret_fingerprintagent_key_expires_inagent_key_obtained_atc                     e Zd ZU ded<   ded<   ded<   ded<   ded<   ded<   ded	<   d
Zded<   d
Zded<   d
Zded<   d
Zded<   d
Zded<   d
Z	ded<   d
Z
ded<   d
Zded<   d
Zded<   d
Zded<   d
Zded<   d
Zded<   d
Zded<   d
Zded<   dZded<   d
Zded<   d  Zd*d"Zed+d&            Zd,d'Zed-d(            Zed.d)            Zd
S )/PooledCredentialstrprovideridlabel	auth_typeintprioritysourceaccess_tokenNOptional[str]refresh_tokenlast_statusOptional[float]last_status_atOptional[int]last_error_codelast_error_reasonlast_error_messagelast_error_reset_atbase_url
expires_atexpires_at_mslast_refreshinference_base_url	agent_keyagent_key_expires_atr   request_countDict[str, Any]extrac                &    | j         	i | _         d S d S N)rf   selfs    r(   __post_init__zPooledCredential.__post_init__   s    :DJJJ     namec                    |t           v r| j                            |          S t          dt	          |           j         d|          )N'z' object has no attribute )_EXTRA_KEYSrf   getAttributeErrortype__name__)rj   rm   s     r(   __getattr__zPooledCredential.__getattr__   sJ    ;:>>$'''Xd!4XXPTXXYYYrl   payloadr!   'PooledCredential'c                   d t          |           D             }fd|D             }d|v r3t          |d         t                    rt          |d                   |d<   fdt          D             }||d<   |                    dt          j                    j        d d                    |                    d	                    d	|                     |                    d
t                     |                    dd           |                    d	t                     |                    dd            | dd|i|S )Nc                2    h | ]}|j         d k    |j         S )rK   rm   .0fs     r(   	<setcomp>z-PooledCredential.from_dict.<locals>.<setcomp>   s&    KKK!af
6J6Jqv6J6J6Jrl   c                D    i | ]}|v |                     |          S  )rq   r|   krv   s     r(   
<dictcomp>z.PooledCredential.from_dict.<locals>.<dictcomp>   s*    GGGa!w,,7;;q>>,,,rl   rW   c                :    i | ]}|v |         ||         S rh   r   r   s     r(   r   z.PooledCredential.from_dict.<locals>.<dictcomp>   s1    ___1qG||PQ
H^GAJH^H^H^rl   rf   rL      rM   rQ   rN   rP   r   rR    rK   r   )r   
isinstancerJ   _parse_absolute_timestamprp   
setdefaultuuiduuid4hexrq   AUTH_TYPE_API_KEYSOURCE_MANUAL)clsrK   rv   field_namesdatarf   s     `   r(   	from_dictzPooledCredential.from_dict   sD   KKvc{{KKKGGGG;GGGt##
48H3I3(O(O#%>tDT?U%V%VD!"_______Wdjll.rr2333Xx!@!@AAA%6777
A&&&-000+++s--H----rl   c                   h d}i }t          |           D ]6}|j        dv rt          | |j                  }|	|j        |v r
|||j        <   7| j                                        D ]\  }}||||<   t          || j                  S )N>   rU   rW   rY   rZ   r[   r\   >   rf   rK   )r   rm   getattrrf   itemsr   rK   )rj   _ALWAYS_EMITresult	field_defvaluer   vs          r(   to_dictzPooledCredential.to_dict   s    
 
 
 "$ 	/ 	/I~!666D).11E INl$B$B).y~&J$$&& 	 	DAq}q	3FDMJJJrl   c           	     V   | j         dk    r| j        | j        f| j        | j        ffD ]j\  }}t          |t                    rP|                                r<t          j	        |t          | dd           |          r|                                c S kdS t          | j        pd          S )Nnousr<   )r<   r^   r   )rK   rb   rc   rR   r^   r   rJ   stripauth_mod_nous_invoke_jwt_is_usabler   )rj   tokenr^   s      r(   runtime_api_keyz PooledCredential.runtime_api_key   s    =F"" !:;"DO4& ) )!z
 uc**	)	) !;%dGT::#-  	) !;;==(((24$*+++rl   c                B    | j         dk    r| j        p| j        S | j        S )Nr   )rK   ra   r]   ri   s    r(   runtime_base_urlz!PooledCredential.runtime_base_url   s'    =F""*;dm;}rl   )rm   rJ   )rK   rJ   rv   re   r!   rw   )r!   re   )r!   rJ   )r!   rS   )rt   
__module____qualname____annotations__rT   rU   rW   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   rf   rk   ru   classmethodr   r   propertyr   r   r   rl   r(   rI   rI      s        MMMGGGJJJNNNMMMKKK#'M''''!%K%%%%&*N****%)O))))'+++++(,,,,,+/////"H"""" $J$$$$#'M''''"&L&&&&(,,,,,#I####*.....M E      Z Z Z Z
 . . . [. K K K K* , , , X,*    X  rl   rI   r   rJ   fallbackc                    t          |           }dD ]V}|                    |          }t          |t                    r*|                                r|                                c S W|S )N)emailpreferred_usernameupn)r   rq   r   rJ   r   )r   r   claimskeyr   s        r(   label_from_tokenr      sh    &&F5 ! !

3eS!! 	!ekkmm 	!;;==   Orl   entriesList[PooledCredential]rO   c                >    t          d | D             d          dz   S )Nc              3  $   K   | ]}|j         V  d S rh   rP   r|   entrys     r(   	<genexpr>z!_next_priority.<locals>.<genexpr>   s$      445444444rl   )default   )max)r   s    r(   _next_priorityr      s)    44G444bAAAAEErl   rQ   boolc                    | pd                                                                 }|t          k    p|                    t           d          S )Nr   :)r   lowerr   
startswith)rQ   
normalizeds     r(   _is_manual_sourcer      sJ    ,B%%''--//J&T**?*?=@S@S@S*T*TTrl   
error_coderX   c                D    | dk    rt           S | dk    rt          S t          S )zHReturn cooldown seconds based on the HTTP status that caused exhaustion.  i  )EXHAUSTED_TTL_401_SECONDSEXHAUSTED_TTL_429_SECONDSEXHAUSTED_TTL_DEFAULT_SECONDS)r   s    r(   _exhausted_ttlr      s*    S((S((((rl   r   r	   rV   c                   | | dk    rdS t          | t          t          f          r$t          |           }|dk    rdS |dk    r|dz  n|S t          | t                    r|                                 }|sdS 	 t          |          }n# t
          $ r d}Y nw xY w||dk    r|dz  n|S 	 t          j        |                    dd                    	                                S # t
          $ r Y dS w xY wdS )zBest-effort parse for provider reset timestamps.

    Accepts epoch seconds, epoch milliseconds, and ISO-8601 strings.
    Returns seconds since epoch.
    Nr   r   l    J)     @@Zz+00:00)
r   rO   floatrJ   r   
ValueErrorr   fromisoformatr   	timestamp)r   numericraws      r(   r   r     s;    }t%#u&& L,,a<<4#*->#>#>wGK% kkmm 	4	CjjGG 	 	 	GGG	'.1B'B'B7V##O	)#++c8*D*DEEOOQQQ 	 	 	44	4s$   9B	 	BB+9C% %
C32C3messagec                x   | sd S t          j        d| t           j                  }|rTt          |                    d                    }|                    d                                          dk    r|dz  n|S t          j        d| t           j                  }|r"t          |                    d                    S t          j        d| t           j                  }|rKt          |                    d                    dz  t          |                    d                    d	z  z   S t          j        d
| t           j                  }|r%t          |                    d                    dz  S t          j        d| t           j                  }|r%t          |                    d                    d	z  S d S )Nz,quotaResetDelay[:\s\"]+(\d+(?:\.\d+)?)(ms|s)r      msr   z?retry\s+(?:after\s+)?(\d+(?:\.\d+)?)\s*(?:sec|secs|seconds|s\b)z'resets?\s+in\s+(\d+)\s*hr\s+(\d+)\s*minr:   <   zresets?\s+in\s+(\d+)\s*hr\bzresets?\s+in\s+(\d+)\s*min\b)research
IGNORECASEr   groupr   rO   )r   delay_matchr   	sec_matchhr_min_matchhr_only_matchmin_only_matchs          r(   _extract_retry_delay_secondsr      s    t)KWVXVcddK Qk''**++!,!2!21!5!5!;!;!=!=!E!Euv~~5P	\^egigtuuI )Y__Q''(((9GRTR_``L S<%%a(())D03|7I7I!7L7L3M3MPR3RRRI<gr}UUM 2=&&q))**T11Y>WWN 1>''**++b004rl   error_contextOptional[Dict[str, Any]]re   c                   t          | t                    si S i }|                     d          }t          |t                    r+|                                r|                                |d<   |                     d          }t          |t                    r+|                                r|                                |d<   |                     d          p)|                     d          p|                     d          }t          |          }|<t          |t                    r't          |          }|t          j                    |z   }|||d<   |S )Nreasonr   reset_at	resets_atretry_until)r   dictrq   rJ   r   r   r   time)r   r   r   r   r   parsed_reset_atretry_delay_secondss          r(   _normalize_error_contextr   7  sK   mT** 	!#Jx((F&# .6<<>> .%||~~
8	**G'3 0GMMOO 0 '
9*%% 	,[))	,]++ 
 099O:gs#;#;:7CC*"ikk,??O"!0
:rl   r   c                    | j         t          k    rd S t          t          | dd                     }||S | j        r| j        t          | j                  z   S d S )Nr\   )rU   STATUS_EXHAUSTEDr   r   rW   r   rY   )r   r   s     r(   _exhausted_untilr   P  se    ,,,t(8Mt)T)TUUH L#nU5J&K&KKK4rl   rm   c                v    |                                                                                      dd          S )z>Normalize a custom provider name for use as a pool key suffix. -)r   r   r   rz   s    r(   _normalize_custom_pool_namer   [  s,    ::<<''S111rl   configc              #    K   | t                      } | dS |                     d          }t          |t                    s$	 ddlm}  ||           }n# t          $ r Y dS w xY w|sdS |D ]V}t          |t                    s|                    d          }t          |t                    sCt          |          |fV  WdS )zJYield (normalized_name, entry_dict) for each valid custom_providers entry.Ncustom_providersr   )get_compatible_custom_providersrm   )
r)   rq   r   listr&   r   r'   r   rJ   r   )r   r   r   r   rm   s        r(   _iter_custom_providersr   `  s     ~"$$~zz"455&-- 	IIIIII>>vFF 	 	 	FF	 ! 7 7%&& 	yy  $$$ 	)$//666667 7s   A 
A"!A"r]   rS   provider_namec                   | sdS |                                                      d          }|r6t          |          }t                      D ]\  }}||k    rt           | c S t                      D ]c\  }}t          |                    d          pd                                                               d          }|r||k    rt           | c S ddS )a~  Look up the custom_providers list in config.yaml and return 'custom:<name>' for a matching base_url.

    When provider_name is given, prefer matching by name first (solving the case where
    multiple custom providers share the same base_url but have different API keys).
    Falls back to base_url matching when no name match is found.

    Returns None if no match is found.
    N/r]   r   )r   rstripr   r   CUSTOM_POOL_PREFIXrJ   rq   )r]   r   normalized_urlnormalized_name	norm_namer   	entry_urls          r(   get_custom_provider_pool_keyr  z  s	     t^^%%,,S11N
  :5mDD 6 8 8 	: 	:IuO++,9i99999 , 344 6 6	5		*--344::<<CCCHH	 	6n44(5)555554rl   	List[str]c                 X     t          d           t           fd D                       S )z?Return all 'custom:*' pool keys that have entries in auth.json.Nc              3     K   | ]P}|                     t                    t                              |          t                    D|         L|V  Qd S rh   )r   r  r   rq   r   )r|   r   	pool_datas     r(   r   z-list_custom_pool_providers.<locals>.<genexpr>  sx        >>,-- y}}S))400 cN	     rl   )r   sorted)r  s   @r(   list_custom_pool_providersr    sH    $T**I          rl   pool_keyc                    |                      t                    sdS | t          t                    d         }t                      D ]\  }}||k    r|c S dS )zWReturn the custom_providers config entry matching a pool key like 'custom:together.ai'.N)r   r  lenr   )r  suffixr  r   s       r(   _get_custom_provider_configr    so    122 tc,--../F244  	5LLL 4rl   rK   c                H   t                      }|t          S |                    d          }t          |t                    st          S t          |                    | d          pd                                                                          }|t          v r|S t          S )z8Return the configured selection strategy for a provider.Ncredential_pool_strategiesr   )	r)   STRATEGY_FILL_FIRSTrq   r   r   rJ   r   r   SUPPORTED_POOL_STRATEGIES)rK   r   
strategiesstrategys       r(   get_pool_strategyr    s      F~""899Jj$'' #"":>>(B//5266<<>>DDFFH,,,rl   r   c                     e Zd Zd?dZd@dZd@d	ZdAd
ZdBdZdCdZdDdZ	dEdZ
	 dFdGdZdHdZdHdZdHdZdHd ZdId!ZdJd#ZdKd$ZdBd%Zd&d&d'dLd*ZdBd+ZdBd,Zddd-dMd0ZdFdNd2ZdOd3ZdBd4ZdBd5ZdPd7ZdQd9ZdRd=ZdHd>ZdS )SCredentialPoolrK   rJ   r   r   c                    || _         t          |d           | _        d | _        t	          |          | _        t          j                    | _        i | _	        t          | _        d S )Nc                    | j         S rh   r   r   s    r(   <lambda>z)CredentialPool.__init__.<locals>.<lambda>  s    %. rl   r   )rK   r  _entries_current_idr  	_strategy	threadingLock_lock_active_leases%DEFAULT_MAX_CONCURRENT_PER_CREDENTIAL_max_concurrent)rj   rK   r   s      r(   __init__zCredentialPool.__init__  s]     w,H,HIII*.*844^%%
.0Drl   r!   r   c                *    t          | j                  S rh   )r   r!  ri   s    r(   has_credentialszCredentialPool.has_credentials      DM"""rl   c                D    t          |                                           S )zCTrue if at least one entry is not currently in exhaustion cooldown.)r   _available_entriesri   s    r(   has_availablezCredentialPool.has_available  s    D++--...rl   c                *    t          | j                  S rh   )r   r!  ri   s    r(   r   zCredentialPool.entries  r-  rl   Optional[PooledCredential]c                X      j         sd S t           fd j        D             d           S )Nc              3  <   K   | ]}|j         j        k    |V  d S rh   )rL   r"  r|   r   rj   s     r(   r   z)CredentialPool.current.<locals>.<genexpr>  s2      VVuTEU9U9UU9U9U9U9UVVrl   )r"  nextr!  ri   s   `r(   currentzCredentialPool.current  s;     	4VVVVVVVX\]]]rl   oldrI   newNonec                t    t          | j                  D ]"\  }}|j        |j        k    r|| j        |<    dS #dS )z4Swap an entry in-place by id, preserving sort order.N)	enumerater!  rL   )rj   r8  r9  idxr   s        r(   _replace_entryzCredentialPool._replace_entry  sP    #DM22 	 	JCx36!!%(c" "	 	rl   c                N    t          | j        d | j        D                        d S )Nc                6    g | ]}|                                 S r   r   r   s     r(   
<listcomp>z+CredentialPool._persist.<locals>.<listcomp>  s     888U]]__888rl   )r    rK   r!  ri   s    r(   _persistzCredentialPool._persist  s6    M88$-888	
 	
 	
 	
 	
rl   status_coderX   normalized_errorre   c                    |dk    rdS |                     d          }t          |t                    sdS |                                                                t
          v S )uT  Detect upstream-permanent OAuth failures that won't recover on TTL.

        Only fires for 401 responses whose error code/reason matches a known
        terminal OAuth state (token_invalidated, token_revoked, invalid_grant,
        etc.).  Distinguishes permanent failures from transient ones like
        token_expired (refreshable) or generic 401 without a specific reason
        (could be a server-side glitch worth retrying).

        Returns False for non-401 status codes — 429 rate limits and 402
        billing failures are transient by nature and should keep TTL semantics.
        r   Fr   )rq   r   rJ   r   r   _TERMINAL_AUTH_REASONS)rj   rD  rE  r   s       r(   _is_terminal_auth_failurez(CredentialPool._is_terminal_auth_failure  s^      #5!%%h//&#&& 	5||~~##%%)???rl   Nr   r   r   c                   t          |          }|                     ||          rt          }nt          }t	          ||t          j                    ||                    d          |                    d          |                    d                    }|                     ||           |                                  |S )Nr   r   r   rU   rW   rY   rZ   r[   r\   )	r   rH  STATUS_DEADr   r   r   rq   r>  rC  )rj   r   rD  r   rE  terminal_statusupdateds          r(   _mark_exhaustedzCredentialPool._mark_exhausted  s     4MBB ))+7GHH 	/)OO.O'9;;'.228<</33I>> 0 4 4Z @ @
 
 
 	E7+++rl   c           	     (   | j         dk    s|j        dk    r|S 	 ddlm}  |            }|s|S |                    dd          }|                    dd          }|                    dd          }|rm||j        k    rbt                              d	|j                   t          ||||d
d
d
          }| 
                    ||           |                                  |S n2# t          $ r%}t                              d|           Y d
}~nd
}~ww xY w|S )a  Sync a claude_code pool entry from ~/.claude/.credentials.json if tokens differ.

        OAuth refresh tokens are single-use. When something external (e.g.
        Claude Code CLI, or another profile's pool) refreshes the token, it
        writes the new pair to ~/.claude/.credentials.json. The pool entry's
        refresh token becomes stale. This method detects that and syncs.
        	anthropicclaude_coder   )read_claude_code_credentialsrefreshTokenr   accessToken	expiresAtzKPool entry %s: syncing tokens from credentials file (refresh token changed)NrR   rT   r_   rU   rW   rY   z(Failed to sync from credentials file: %s)rK   rQ   agent.anthropic_adapterrR  rq   rT   loggerdebugrL   r   r>  rC  r'   )	rj   r   rR  credsfile_refreshfile_accessfile_expiresrM  excs	            r(   +_sync_anthropic_entry_from_credentials_filez:CredentialPool._sync_anthropic_entry_from_credentials_file  s\    =K''5<=+H+HL	JLLLLLL0022E  99^R88L))M266K 99[!44L 0C C Cjlqltuuu!!,".". $#'$(   ##E7333 	J 	J 	JLLCSIIIIIIII	Js   C  B0C   
D*D

Dc           	     ^   | j         dk    s|j        dk    r|S 	 t                      5  t                      }t	          |d          }ddd           n# 1 swxY w Y   t          |t                    s|S |                    d          }t          |t                    s|S |                    dd          }|                    dd          }|j        pd}|j	        pd}|r||k    s|r||k    rt                              d|j                   ||p|j	        ddddddd	}	|                    d
          r|d
         |	d
<   t          |fi |	}
|                     ||
           |                                  |
S n2# t           $ r%}t                              d|           Y d}~nd}~ww xY w|S )u  Sync a Codex device_code pool entry from auth.json if tokens differ.

        When a Codex OAuth access token expires (or the ChatGPT account hits
        its 5h/weekly quota), the pool entry gets marked ``STATUS_EXHAUSTED``
        with a ``last_error_reset_at`` that can be many hours in the future.
        Meanwhile the user may run ``hermes model`` / ``hermes auth`` which
        performs a fresh device-code login and writes new tokens to
        ``auth.json`` under ``_auth_store_lock``.  Without this sync the pool
        entry stays frozen until ``last_error_reset_at`` elapses — even
        though fresh credentials are sitting on disk — and every request
        fails with "no available entries (all exhausted or empty)".

        Mirrors the Nous/Anthropic resync paths above.  Only applies to
        device_code-sourced entries; env/API-key-sourced entries have no
        auth.json shadow to sync from.
        openai-codexdevice_codeNtokensrR   r   rT   zQPool entry %s: syncing Codex tokens from auth.json (refreshed by another process)rR   rT   rU   rW   rY   rZ   r[   r\   r`   z-Failed to sync Codex entry from auth.json: %srK   rQ   r   r   r   r   r   rq   rR   rT   rX  rY  rL   r   r>  rC  r'   rj   r   
auth_storestaterc  store_accessstore_refreshentry_accessentry_refreshfield_updatesrM  r^  s               r(   !_sync_codex_entry_from_auth_storez0CredentialPool._sync_codex_entry_from_auth_storeB  s[   " =N**elm.K.KL*	O!## I I-//
,ZHHI I I I I I I I I I I I I I I eT** YYx((Ffd++ !::nb99L"JJ;;M !-3L!/52M ,,! -&3}&D&D5H   %1%2%Ie6I#'&*'+)-*.+/	1 	1 99^,, J49.4IM.1!%99=99##E7333 	O 	O 	OLLH#NNNNNNNN	OG   E; AE; AE; AE; 5+E; !CE; ;
F*F%%F*c           	     ^   | j         dk    s|j        dk    r|S 	 t                      5  t                      }t	          |d          }ddd           n# 1 swxY w Y   t          |t                    s|S |                    d          }t          |t                    s|S |                    dd          }|                    dd          }|j        pd}|j	        pd}|r||k    s|r||k    rt                              d|j                   ||p|j	        ddddddd	}	|                    d
          r|d
         |	d
<   t          |fi |	}
|                     ||
           |                                  |
S n2# t           $ r%}t                              d|           Y d}~nd}~ww xY w|S )a  Sync an xAI OAuth pool entry from auth.json if tokens differ.

        xAI OAuth refresh tokens are single-use.  When another Hermes process
        (or another profile sharing the same auth.json) refreshes the token,
        it writes the new pair to ``providers["xai-oauth"]["tokens"]`` under
        ``_auth_store_lock``.  Without this resync, our in-memory pool entry
        keeps the consumed refresh_token and the next ``_refresh_entry`` call
        would replay it and get a ``refresh_token_reused``-style 4xx.

        Only applies to entries seeded from the singleton (``loopback_pkce``);
        manually added entries (``manual:xai_pkce``) are independent
        credentials with their own refresh-token lifecycle.
        	xai-oauthloopback_pkceNrc  rR   r   rT   zUPool entry %s: syncing xAI OAuth tokens from auth.json (refreshed by another process)rd  r`   z1Failed to sync xAI OAuth entry from auth.json: %sre  rf  s               r(   %_sync_xai_oauth_entry_from_auth_storez4CredentialPool._sync_xai_oauth_entry_from_auth_store  sY    =K''5<?+J+JL'	S!## F F-//
,ZEEF F F F F F F F F F F F F F F eT** YYx((Ffd++ !::nb99L"JJ;;M -3L!/52M ,,! -&3}&D&D5H   %1%2%Ie6I#'&*'+)-*.+/	1 	1 99^,, J49.4IM.1!%99=99##E7333 	S 	S 	SLLLcRRRRRRRR	Sro  c                   | j         dk    sj        dk    rS 	 t                      5  t                      }t	          |d          }ddd           n# 1 swxY w Y   |sS |                    dd          }|                    dd          }|||                    d          |                    d          |                    d	          |                    d
          d}t          fd|                                D                       }|r't          	                    dj
                   ddddddd}|r||d<   |r||d<   |                    d          r|d         |d<   |                    d          r|d         |d<   |                    d	          r|d	         |d	<   |                    d
          r|d
         |d
<   t          j                  }	dD ]}
|                    |
          }|||	|
<   t          fd|	i|}|                     |           |                                  |S n2# t           $ r%}t          	                    d|           Y d}~nd}~ww xY wS )a  Sync a Nous pool entry from auth.json if tokens differ.

        Nous OAuth refresh tokens are single-use.  When another process
        (e.g. a concurrent cron) refreshes the token via
        ``resolve_nous_runtime_credentials``, it writes fresh tokens to
        auth.json under ``_auth_store_lock``.  The pool entry's tokens
        become stale.  This method detects that and adopts the newer pair,
        avoiding a "refresh token reuse" revocation on the Nous Portal.
        r   rb  NrT   r   rR   r^   rb   rc   ra   )rR   rT   r^   rb   rc   ra   c              3  P   K   | ] \  }}|d vot          |d          |k    V  !dS )Nr   N)r   )r|   r   r   r   s      r(   r   zBCredentialPool._sync_nous_entry_from_auth_store.<locals>.<genexpr>  sV        C Z'NGE3,E,E,N     rl   z0Pool entry %s: syncing Nous state from auth.jsonrJ  r@   r>   rA   rF   rD   rG   rf   z,Failed to sync Nous entry from auth.json: %s)rK   rQ   r   r   r   rq   anyr   rX  rY  rL   r   rf   r   r>  rC  r'   )rj   r   rg  rh  rj  ri  comparable_updatesshould_syncrm  extra_updates	extra_keyvalrM  r^  s    `            r(    _sync_nous_entry_from_auth_storez/CredentialPool._sync_nous_entry_from_auth_store  s^    =F""elm&C&CL9	N!## A A-//
,Z@@A A A A A A A A A A A A A A A  !IIor::M 99^R88L ,!.#ii55"YY{33(-		2H(I(I&+ii0D&E&E" "     "4":":"<"<    K  #FH  
 $(&*'+)-*.+/1 1   A4@M.1  C5BM/299\** F272EM,/99[)) D16{1CM+.99344 Z<ABX<YM"8999122 V:?@T:UM"67 $U[ 1 1"; 7 7I  ))I..C36i0!%NN}NNN##E7333G#H  	N 	N 	NLLGMMMMMMMM	NsA   H< AH< AH< AH< #GH< <
I+I&&I+c                   |j         dvrdS 	 t                      5  t                      }| j        dk    rt	          |d          }|	 ddd           dS |j        |d<   |j        r
|j        |d<   |j        r
|j        |d<   |j        r
|j        |d<   |j	        r
|j	        |d<   d	D ]#}|j
                            |          }||||<   $|j        r
|j        |d
<   t          |d|d           nz| j        dk    rt	          |d          }t          |t                    s	 ddd           dS |                    d          }t          |t                    s	 ddd           dS |j        |d<   |j        r
|j        |d<   |j        r
|j        |d<   t          |d|d           n| j        dk    rt	          |d          }t          |t                    s	 ddd           dS |                    d          }t          |t                    s	 ddd           dS |j        |d<   |j        r
|j        |d<   |j        r
|j        |d<   t          |d|d           n	 ddd           dS t#          |           ddd           dS # 1 swxY w Y   dS # t$          $ r,}t&                              d| j        |           Y d}~dS d}~ww xY w)ux  Write refreshed pool entry tokens back to auth.json providers.

        After a pool-level refresh, the pool entry has fresh tokens but
        auth.json's ``providers.<id>`` still holds the pre-refresh state.
        On the next ``load_pool()``, ``_seed_from_singletons()`` reads that
        stale state and can overwrite the fresh pool entry — potentially
        re-seeding a consumed single-use refresh token.

        Applies to any OAuth provider whose singleton lives in auth.json
        (currently Nous, OpenAI Codex, and xAI Grok OAuth).

        ``set_active=False`` on every write: a pool sync-back is a
        token-rotation side effect, not the user choosing a provider.
        Using ``_save_provider_state`` (which sets ``active_provider``)
        here would mean every Nous/Codex/xAI refresh in a multi-provider
        setup silently flips the ``active_provider`` flag — the next
        ``hermes`` invocation that defaults to the active provider
        (e.g. setup wizard, ``hermes auth status``) would land on
        whatever provider happened to refresh last, not whatever the
        user actually chose.
        >   rb  rr  Nr   rR   rT   r^   rb   rc   rw  ra   F)
set_activera  rc  r`   rq  z3Failed to sync %s pool entry back to auth store: %s)rQ   r   r   rK   r   rR   rT   r^   rb   rc   rf   rq   ra   r   r   r   r`   r   r'   rX  rY  )rj   r   rg  rh  r|  r}  rc  r^  s           r(   %_sync_device_code_entry_to_auth_storez4CredentialPool._sync_device_code_entry_to_auth_store  sb   2 <???F;	d!## 8- 8--//
=F**0VDDE}8- 8- 8- 8- 8- 8- 8- 8- -2,>E.)* E161Do.' ?.3.>l+ =-2_k*1 S8=8R45&? 3 3	 $kooi88?/2E),/ O6;6N23)*fePUVVVVV]n440^LLE%eT22 98- 8- 8- 8- 8- 8- 8- 8-: #YYx00F%fd33 ?8- 8- 8- 8- 8- 8- 8- 8-@ .3-?F>** F272E/) C050Bn-)*neX]^^^^^]k110[IIE%eT22 U8- 8- 8- 8- 8- 8- 8- 8-V #YYx00F%fd33 [8- 8- 8- 8- 8- 8- 8- 8-\ .3-?F>** F272E/) C050Bn-)*k5UZ[[[[[ m8- 8- 8- 8- 8- 8- 8- 8-p !,,,q8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8- 8-r  	d 	d 	dLLNPTP]_bccccccccc	ds   J -JJ CJ J -+JJ %A1JJ #+JJ AJJ )J8J J		J J	J 
K!KKforcec          
        |j         t          k    s|j        s|r|                     |d            d S 	 | j        dk    rddlm}  ||j        |j                            d                    }t          ||d         |d         |d         	          }|j        d
k    r[	 ddlm
}  ||d         |d         |d                    nx# t          $ r&}t                              d|           Y d }~nMd }~ww xY wnC| j        dk    rm|                     |          }||ur|}t          j        |j        |j                  }t          ||d         |d         |                    d                    }n| j        dk    rm|                     |          }||ur|}t          j        |j        |j                  }t          ||d         |d         |                    d                    }nS| j        dk    rF|                     |          }||ur|}t          j        |           |                     |          }n|S 
n># t          $ 
r0}	t                              d| j        |j        |	           | j        dk    r|j        d
k    r|                     |          }|j        |j        k    r4t                              d           	 ddlm}  ||j        |j                            d                    }t          ||d         |d         |d         t2          d d           }|                     ||           |                                  	 ddlm
}  ||d         |d         |d                    n2# t          $ r%}t                              d|           Y d }~nd }~ww xY w|cY d }	~	S # t          $ r%}
t                              d|
           Y d }
~
n>d }
~
ww xY w|                     |          s!t                              d           |cY d }	~	S | j        dk    r|                     |          }|j        |j        k    rft                              d           t          |t2          d d d d d           }|                     ||           |                                  |cY d }	~	S t          j        |	          r+t                              d           	 t=                      5  t?                      }tA          |d          pi }tC          |tD                    r:|                    d          pi }tC          |tD                    rtG          |                    d          pd          $                                }tG          |j        pd          $                                }|r||k    r|%                    dd            |%                    dd            ||d<   dtM          |	dd          tG          |	          d d!tO          j(        tR          j*                  +                                d"|d#<   tY          |d|           t[          |           d d d            n# 1 swxY w Y   n2# t          $ r%}t                              d$|           Y d }~nd }~ww xY wd% | j.        D             | _.        | j/        |j        k    rd | _/        |                                  Y d }	~	d S | j        dk    r|                     |          }|j        |j        k    rft                              d&           t          |t2          d d d d d           }|                     ||           |                                  |cY d }	~	S t          j0        |	          r+t                              d'           	 t=                      5  t?                      }tA          |d          pi }tC          |tD                    r:|                    d          pi }tC          |tD                    rtG          |                    d          pd          $                                }tG          |j        pd          $                                }|r||k    r|%                    dd            |%                    dd            ||d<   dtM          |	dd          tG          |	          d d!tO          j(        tR          j*                  +                                d"|d#<   tY          |d|           t[          |           d d d            n# 1 swxY w Y   n2# t          $ r%}t                              d(|           Y d }~nd }~ww xY wd) | j.        D             | _.        | j/        |j        k    rd | _/        |                                  Y d }	~	d S | j        dk    r|                     |          }|j        |j        k    r{t                              d*           t          |t2          d d d d d           }|                     ||           |                                  | 1                    |           |cY d }	~	S t          j2        |	          rt                              d+           	 t=                      5  t?                      }tA          |d          p&|j3        |j4        |j5        |j6        |j7        |j8        d,}tG          |                    d          pd          $                                }tG          |j        pd          $                                }|r||k    rNt          j9        ||	d -           t          j:        ||	d -           tY          |d|           t[          |           d d d            n# 1 swxY w Y   n2# t          $ r%}t                              d.|           Y d }~nd }~ww xY wt          j;        d/t          j;         hfd0| j.        D             | _.        | j/        |j        k    rd | _/        |                                  Y d }	~	d S |                     |d            Y d }	~	d S d }	~	ww xY wt          |t2          d d d d d           }|                     ||           |                                  | 1                    |           |S )1NrP  r   )refresh_anthropic_oauth_purehermes_pkce)use_jsonrR   rT   r_   )rR   rT   r_   rQ  )_write_claude_code_credentialsz7Failed to write refreshed token to credentials file: %sra  r`   )rR   rT   r`   rq  r   )force_refreshz'Credential refresh failed for %s/%s: %sz8Retrying refresh with synced token from credentials filerV  zDFailed to write refreshed token to credentials file (retry path): %szRetry refresh also failed: %sz7Credentials file has valid token, using without refreshuD   xAI OAuth refresh failed but auth.json has newer tokens — adoptingrJ  zIxAI OAuth refresh token is terminally invalid; clearing local token staterc  r   codeunknowncredential_pool_refresh_failureT)rK   r  r   r   relogin_requiredatlast_auth_errorz,Failed to clear terminal xAI OAuth state: %sc                (    g | ]}|j         d k    |S )rr  rQ   r|   items     r(   rB  z1CredentialPool._refresh_entry.<locals>.<listcomp>  s/     % % %!%;/99 999rl   uF   Codex OAuth refresh failed but auth.json has newer tokens — adoptingzKCodex OAuth refresh token is terminally invalid; clearing local token statez.Failed to clear terminal Codex OAuth state: %sc                (    g | ]}|j         d k    |S )rb  r  r  s     r(   rB  z1CredentialPool._refresh_entry.<locals>.<listcomp>T  s/     % % %!%;-77 777rl   u?   Nous refresh failed but auth.json has newer tokens — adoptingzDNous refresh token is terminally invalid; clearing local token state)r=   rC   ra   r?   r<   r;   )r   z-Failed to clear terminal Nous OAuth state: %szmanual:c                &    g | ]}|j         v|S r   r  )r|   r  singleton_sourcess     r(   rB  z1CredentialPool._refresh_entry.<locals>.<listcomp>  s1     % % %!%;.??? ???rl   )<rN   AUTH_TYPE_OAUTHrT   rN  rK   rW  r  rQ   endswithr   r  r'   rX  rY  rn  r   refresh_codex_oauth_purerR   rq   rs  refresh_xai_oauth_purer~   resolve_nous_runtime_credentialsrL   r_  	STATUS_OKr>  rC  _entry_needs_refresh$_is_terminal_xai_oauth_refresh_errorr   r   r   r   r   rJ   r   popr   r   nowr   utc	isoformatr   r   r!  r"  &_is_terminal_codex_oauth_refresh_errorr  _is_terminal_nous_refresh_errorr=   rC   ra   r?   r<   r;   _quarantine_nous_oauth_state_quarantine_nous_pool_entriesNOUS_DEVICE_CODE_SOURCE)rj   r   r  r  	refreshedrM  r  wexcsyncedr^  	retry_excrg  rh  rc  rj  rl  	clear_excr  s                    @r(   _refresh_entryzCredentialPool._refresh_entry\  s   ?o--U5H- 2$$UD1114z	}++PPPPPP88'"\22=AA  	 "!*>!:"+O"<"+O"<	   <=00fZZZZZZ66%n5%o6%o6   
 % f f f%^`deeeeeeeef 1 .00
 ??FF&&"E$=&' 	 "!*>!:"+O"<!*~!>!>	   +-- CCEJJ&&"E$;&' 	 "!*>!:"+O"<!*~!>!>	   &((>>uEE&&"E9"'    ??FF p	 p	 p	LLBDMSXS[]`aaa }++0M0MII%PP'5+>>>LL![\\\QXXXXXX$@$@"0%+]%;%;M%J%J% % %	 #*")2>)B*3O*D*3O*D(1+/,0# # # ++FG<<<w^^^^^^:: ). 9 )/ : )/ :   
  ) w w w"LL)oquvvvvvvvvw&$ Q Q Q%DiPPPPPPPPQ226:: "LL!Z[[[!MMMMMM }++CCEJJ'5+>>>LL^   &$-'+(,*.+/,0  G ''888MMOOO"NNNNNN @EE & LLc  -// E E)9););J$8[$Q$Q$WUWE)%66 E).8)<)<)B#-fd#;#; !E47

?8S8S8YWY4Z4Z4`4`4b4bM478K8Qr4R4R4X4X4Z4ZM+8 %EM]<Z<Z(.

>4(H(H(H(.

?D(I(I(I:@h8C4;C4S4S7:3xx6W@D2:,x|2L2L2V2V2X2XD* D*.?(@ )=ZV[(\(\(\(8(D(D(D+E E E E E E E E E E E E E E E, %   JI       % %)-% % %DM '5833+/(MMOOO44444
 }..??FF'5+>>>LL`   &$-'+(,*.+/,0  G ''888MMOOO"NNNNNN B3GG & LLe  -// E E)9););J$8^$T$T$ZXZE)%66 E).8)<)<)B#-fd#;#; !E47

?8S8S8YWY4Z4Z4`4`4b4bM478K8Qr4R4R4X4X4Z4ZM+8 %EM]<Z<Z(.

>4(H(H(H(.

?D(I(I(I:@h8F4;C4S4S7:3xx6W@D2:,x|2L2L2V2V2X2XD* D*.?(@ )=ZY^(_(_(_(8(D(D(D+E E E E E E E E E E E E E E E, %   Li       % %)-% % %DM '5833+/(MMOOO44444 }&&>>uEE'5+>>>LL!bccc%$-'+(,*.+/,0  G ''888MMOOO>>wGGG"NNNNNN;C@@ * LL!ghhha-// = =)9););J$8V$L$L %-2_383H6;6N.3.>).',yQ QE -0		/0J0J0Pb,Q,Q,W,W,Y,YM,/0C0Ir,J,J,P,P,R,RM#0 =M]4R4R ( E$)$'+L!" !" !" !"
 !) F$.$'+L!" !" !" !"
 !5Z O O O 0 < < <1= = = = = = = = = = = = = = =2 % a a a%TV_````````a !8D("BDD)%% % % %)-% % %DM '5833+/(MMOOO44444  ---44444ap	d ! "# $
 
 
 	E7+++ 	227;;;s  A+I  %C I 
C7C2,I 2C77EI q<A?q7BN3%M:9N3:
N)N$N3$N))N3-q<3
O"=Oq7O""3q7q<Bq7,q<2/q7"Y90E1Y-!Y9-Y1	1Y94Y1	5Y98q79
Z(Z#q7#Z((Aq72Bq7q<	/q79eE1e8ee	ee	eq7
e?e:5q7:e??Aq7	B&q7/q<5/q7%o3C:n9-o9n=	=o n=	oq7
o4o/*q7/o44A!q7q77q<c                   |j         t          k    rdS | j        dk    rE|j        dS t	          |j                  t	          t          j                    dz            dz   k    S | j        dk    rt          |j        t                    S | j        dk    r$t          j
        |j        t          j                  S | j        dk    rdS dS )NFrP    i ra  rq  r   )rN   r  rK   r_   rO   r   r   rR   r   r   _xai_access_token_is_expiring%XAI_ACCESS_TOKEN_REFRESH_SKEW_SECONDSrj   r   s     r(   r  z#CredentialPool._entry_needs_refresh  s    ?o--5=K''"*uu*++s49;;3E/F/F/PPP=N**2"7   =K''9">   =F"" 5url   c                l    | j         5  |                                 cd d d            S # 1 swxY w Y   d S rh   )r&  _select_unlockedri   s    r(   selectzCredentialPool.select  s}    Z 	+ 	+((**	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+   )--Fclear_expiredrefreshr  r  c          
        t          j                     }d}g }g }| j        D ]m}| j        dk    r=|j        dk    r2|j        t
          t          hv r|                     |          }||ur|}d}| j        dk    r=|j        dk    r2|j        t
          t          hv r|                     |          }||ur|}d}| j        dk    r=|j        dk    r2|j        t
          t          hv r| 	                    |          }||ur|}d}| j        dk    r=|j        d	k    r2|j        t
          t          hv r| 
                    |          }||ur|}d}|j        t          k    rt          |j                  r{|j        pd
}	|	rp||	z
  t          k    rb|j        p|j        dd         }
t                               d|
|j        pd||	z
  dz  | j                   |                    |j                   d}|j        t
          k    rPt)          |          }|||k     r|r5t+          |t,          ddddd          }|                     ||           |}d}|r2|                     |          r|                     |d          }|V|}|                    |           o|r't5          |          fd| j        D             | _        |r|                                  |S )a  Return entries not currently in exhaustion cooldown.

        When *clear_expired* is True, entries whose cooldown has elapsed are
        reset to STATUS_OK and persisted.  When *refresh* is True, entries
        that need a token refresh are refreshed (skipped on failure).
        FrP  rQ  Tr   rb  ra  rq  rr  r   N   uh   credential pool: pruning DEAD manual entry %s (reason=%s, age=%.1fh) — re-add via `hermes auth add %s`r  g      @rJ  r  c                &    g | ]}|j         v|S r   rL   )r|   e
pruned_idss     r(   rB  z5CredentialPool._available_entries.<locals>.<listcomp>9  s%    PPP1Z9O9OQ9O9O9Orl   )r   r!  rK   rQ   rU   r   rK  r_  r~  rn  rs  r   rW   DEAD_MANUAL_PRUNE_TTL_SECONDSrM   rL   rX  warningrZ   appendr   r   r  r>  r  r  setrC  )rj   r  r  r  cleared_anyentries_to_prune	availabler   r  dead_at_labelexhausted_untilclearedr  r  s                 @r(   r/  z!CredentialPool._available_entries  sv    ikk&(,.	] `	$ `	$E ,,1N1N).>-LLLII%PP&&"E"&K
 ''55).>-LLL>>uEE&&"E"&K //55).>-LLL??FF&&"E"&K
 ,,77).>-LLLCCEJJ&&"E"&K K// %U\22 +#27aG +3=3P#P#P!&!<!Y"!3@y 7]f4 M   )//999&*  $444"25"9"9".33H3H  '%$-'+(,*.+/,0  G ''w777#E"&K "444U;; " //U/CC	$!U#### 	Q-..JPPPPPPPDM 	MMOOOrl   c                   |                      dd          }|s#d | _        t                              d           d S | j        t
          k    r"t          j        |          j        | _        S | j        t          k    rbt          |          dk    rOt          |d           t          j        dz             }|                     |           j        | _        |S | j        t          k    rt          |          dk    r|d         fd	| j        D             }|                    t          t          | j                  dz
  
                     d t%          |          D             | _        |                                  j        | _        |                                 pS |d         j        | _        S )NTr  z>credential pool: no available entries (all exhausted or empty)r   c                    | j         S rh   rd   )r  s    r(   r  z1CredentialPool._select_unlocked.<locals>.<lambda>K  s     rl   r   r  r   c                4    g | ]}|j         j         k    |S r   r  )r|   	candidater   s     r(   rB  z3CredentialPool._select_unlocked.<locals>.<listcomp>T  s)    \\\Y9<SXS[C[C[yC[C[C[rl   r   c                6    g | ]\  }}t          ||           S r   r   )r|   r=  r  s      r(   rB  z3CredentialPool._select_unlocked.<locals>.<listcomp>V  s)    ggg.#yWY===gggrl   )r/  r"  rX  infor#  STRATEGY_RANDOMr8   choicerL   STRATEGY_LEAST_USEDr  minr   rd   r>  STRATEGY_ROUND_ROBINr!  r  r<  rC  r7  )rj   r  rM  rotatedr   s       @r(   r  zCredentialPool._select_unlocked>  s   ++$+MM	 	#DKKXYYY4>_,,M),,E$xDL>000S^^a5G5G	'@'@AAAEe53F3JKKKGw///$xDN>111c)nnq6H6HaLE\\\\$-\\\GNN753t}3E3E3IJJJKKKggT]^eTfTfgggDMMMOOO$xD<<>>*U*! 8rl   c                r    |                                  }||S |                                 }|r|d         nd S Nr   )r7  r/  )rj   r7  r  s      r(   peekzCredentialPool.peek_  s>    ,,..N++--	(2y||d2rl   )r   api_key_hintr  rS   c                  | j         5  d r!t          fd| j        D             d           (|                                 p|                                 	 d d d            d S j        pj        d d         }|                     ||           t          fd| j        D                       }|j        t          k    r%t                              d|||j        pd           nt                              d||           d | _        |                                 }|r1|j        p|j        d d         }t                              d|           |cd d d            S # 1 swxY w Y   d S )Nc              3  2   K   | ]}|j         k    |V  d S rh   )r   )r|   r  r  s     r(   r   z;CredentialPool.mark_exhausted_and_rotate.<locals>.<genexpr>u  s0      SS11Bl1R1RQ1R1R1R1RSSrl   r  c              3  <   K   | ]}|j         j         k    |V  d S rh   r  )r|   r  r   s     r(   r   z;CredentialPool.mark_exhausted_and_rotate.<locals>.<genexpr>  s1      >>qQTUX-=-=-=-=-=-=>>rl   ux   credential pool: marking %s DEAD (status=%s, reason=%s) — permanently failed, will NOT re-enter rotation until re-authr  z;credential pool: marking %s exhausted (status=%s), rotatingzcredential pool: rotated to %s)r&  r6  r!  r7  r  rM   rL   rN  rU   rK  rX  r  rZ   r  r"  )	rj   rD  r   r  r  updated_entry
next_entry_next_labelr   s	      `    @r(   mark_exhausted_and_rotatez(CredentialPool.mark_exhausted_and_rotatef  s    Z %	 %	E 
 SSSSSSS  }A$*?*?*A*A}%	 %	 %	 %	 %	 %	 %	 %	 [0EHRaRLF  ]CCC >>>>DM>>> M (K77SK)H)UI    QK    $D..00J K(.C*-2C<kJJJK%	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	s   AE%*C.E%%E),E)credential_idc                     j         5  |r; j                            |d          dz    j        |<   | _        |cddd           S                      dd          }|s	 ddd           dS  fd|D             }|r|n|}t          | fd          } j                            |j        d          dz    j        |j        <   |j         _        |j        cddd           S # 1 swxY w Y   dS )	a\  Acquire a soft lease on a credential.

        If a specific credential_id is provided, lease that entry directly.
        Otherwise prefer the least-leased available credential, using priority as
        a stable tie-breaker. When every credential is already at the soft cap,
        still return the least-leased one instead of blocking.
        r   r   NTr  c                f    g | ]-}j                             |j        d           j        k     +|.S )r   )r'  rq   rL   r)  r5  s     r(   rB  z0CredentialPool.acquire_lease.<locals>.<listcomp>  sF       &**58Q77$:NNN NNNrl   c                R    j                             | j        d          | j        fS r  )r'  rq   rL   rP   )r   rj   s    r(   r  z.CredentialPool.acquire_lease.<locals>.<lambda>  s#    4#6#:#:58Q#G#G"X rl   r   )r&  r'  rq   r"  r/  r  rL   )rj   r  r  	below_cap
candidateschosens   `     r(   acquire_leasezCredentialPool.acquire_lease  s    Z 	 	 %595H5L5L]\]5^5^ab5b#M2#0 $		 	 	 	 	 	 	 	 //dD/QQI 	 	 	 	 	 	 	 	   #,  I '0>YJXXXX  F .2-@-D-DVYPQ-R-RUV-VD	*%yD9+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   1C%C%.A*C%%C),C)c                    | j         5  | j                            |d          }|dk    r| j                            |d           n|dz
  | j        |<   ddd           dS # 1 swxY w Y   dS )z/Release a previously acquired credential lease.r   r   N)r&  r'  rq   r  )rj   r  counts      r(   release_leasezCredentialPool.release_lease  s    Z 	? 	?'++M1==Ezz#''t<<<<5:QY#M2	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?s   AA  A$'A$c                l    | j         5  |                                 cd d d            S # 1 swxY w Y   d S rh   )r&  _try_refresh_current_unlockedri   s    r(   try_refresh_currentz"CredentialPool.try_refresh_current  s}    Z 	8 	85577	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8r  c                    |                                  }|d S |                     |d          }||j        | _        |S )NTr  )r7  r  rL   r"  )rj   r   r  s      r(   r  z,CredentialPool._try_refresh_current_unlocked  sG    =4''T'::	 (|Drl   rO   c                   d}g }| j         D ][}|j        s|j        s|j        r/|                    t          |d d d d d d                      |dz  }F|                    |           \|r|| _         |                                  |S )Nr   rJ  r   )r!  rU   rW   rY   r  r   rC  )rj   r  new_entriesr   s       r(   reset_statuseszCredentialPool.reset_statuses  s    ] 	* 	*E  *E$8 *E<Q *""$('+(,*.+/,0  
 
 
 
""5)))) 	'DMMMOOOrl   indexc                   |dk     s|t          | j                  k    rd S | j                            |dz
            }d t          | j                  D             | _        |                                  | j        |j        k    rd | _        |S )Nr   c                6    g | ]\  }}t          ||           S r  r  )r|   new_priorityr   s      r(   rB  z/CredentialPool.remove_index.<locals>.<listcomp>  s9     
 
 
#e EL111
 
 
rl   )r  r!  r  r<  rC  r"  rL   )rj   r  removeds      r(   remove_indexzCredentialPool.remove_index  s    199DM 2 2224-##EAI..
 
'0'?'?
 
 
 	wz))#Drl   targetr	   ?Tuple[Optional[int], Optional[PooledCredential], Optional[str]]c                ^   t          |pd                                          sdS t          | j        d          D ]\  }}|j        k    r||d fc S fdt          | j        d          D             }t          |          dk    r|d         d         |d         d         d fS t          |          dk    r	d d d dfS                                 rMt                    }d|cxk    rt          | j                  k    rn n|| j        |dz
           d fS d d d	| d
fS d d d dfS )Nr   )NNzNo credential target provided.r   )startc                    g | ]J\  }}|j                                                                                                         k    F||fKS r   )rM   r   r   )r|   r=  r   r   s      r(   rB  z1CredentialPool.resolve_target.<locals>.<listcomp>  sX     
 
 
U{  ""((**ciikk99 %L999rl   r   zAmbiguous credential label "z-". Use the numeric index or entry id instead.zNo credential #.zNo credential matching "z".)rJ   r   r<  r!  rL   r  isdigitrO   )rj   r  r=  r   label_matchesr  r   s         @r(   resolve_targetzCredentialPool.resolve_target  s   &,B%%'' 	@??#DM;;; 	( 	(JCx3E4'''' 
 
 
 
'Q???
 
 

 }"" #A&a(8(;TAA}!!pcppppp;;== 	:HHEE////S///////dmEAI6<<999999T=c=====rl   c                    t          |t          | j                            }| j                            |           |                                  |S )Nr   )r   r   r!  r  rC  r  s     r(   	add_entryzCredentialPool.add_entry  sF    t}(E(EFFFU###rl   )rK   rJ   r   r   )r!   r   )r!   r   )r!   r2  )r8  rI   r9  rI   r!   r:  )r!   r:  )rD  rX   rE  re   r!   r   rh   )r   rI   rD  rX   r   r   r!   rI   )r   rI   r!   rI   )r   rI   r!   r:  )r   rI   r  r   r!   r2  )r   rI   r!   r   )r  r   r  r   r!   r   )rD  rX   r   r   r  rS   r!   r2  )r  rS   r!   rS   )r  rJ   r!   r:  )r!   rO   )r  rO   r!   r2  )r  r	   r!   r  ) rt   r   r   r*  r,  r0  r   r7  r>  rC  rH  rN  r_  rn  rs  r~  r  r  r  r  r/  r  r  r  r  r  r  r  r  r  r  r  r   rl   r(   r  r    s       E E E E# # # #/ / / /# # # #^ ^ ^ ^
   
 
 
 
@ @ @ @6 37	    ># # # #J> > > >@8 8 8 8tF F F FPVd Vd Vd VdpQ Q Q Qf
   0+ + + + ;@QV q q q q q qf   B3 3 3 3 37&*, , , , , ,\    >? ? ? ?8 8 8 8      0   > > > >2     rl   r  rv   c                   d }t          |           D ]\  }}|j        |k    r|} n||                    dt          j                    j        d d                    |                    dt          |                      |                    d|                    d          p|           |                     t          
                    ||                     dS | |         }i }i }	d t          |          D             }
|                                D ]d\  }}|dv s||dk    r|j        r||
v rt          ||          |k    r|||<   8|t          v r#|j                            |          |k    r||	|<   e|s|	rL|	ri |j        |	|d<   t#          |fi |}|| |<   |                                |                                k    S d	S )
NrL   r   rP   rM   Tc                    h | ]	}|j         
S r   rz   r{   s     r(   r~   z _upsert_entry.<locals>.<setcomp>  s    555qAF555rl   >   rL   rP   rf   F)r<  rQ   r   r   r   r   r   rq   r  rI   r   r   r   rM   r   rp   rf   r   r   )r   rK   rQ   rv   existing_idxr=  r   existingrm  r{  _field_namesr   r   rM  s                 r(   _upsert_entryr    s   L((  
U<6!!LE " 4!1"1"!5666:~g'>'>???7GKK$8$8$BFCCC'11(GDDEEEt|$HMM55F8$4$4555Lmmoo 
+ 
+
U$$$'>>hn>,x%%..%*c"K~!!#&&%//%*c" 7 7 	I%H%H-%HM'"(44m44 ' !!W__%6%6665rl   c                `  	 | dk    rdS dddddd	t          d	 |D             d
           }t          d |D             	fd          }g ||}d t          |          D             }d}t          |          D ]1\  }}|j        |k    r!t          ||          |||j                 <   d}2|S )NrP  Fr   r   r         )zenv:ANTHROPIC_TOKENzenv:CLAUDE_CODE_OAUTH_TOKENr  rQ  zenv:ANTHROPIC_API_KEYc              3  B   K   | ]}t          |j                  |V  d S rh   r   rQ   r   s     r(   r   z-_normalize_pool_priorities.<locals>.<genexpr>C  s2      GG5'8'F'FGGGGGGGrl   c                    | j         S rh   r   r  s    r(   r  z,_normalize_pool_priorities.<locals>.<lambda>D  s    %. rl   r   c              3  B   K   | ]}t          |j                  |V  d S rh   r  r   s     r(   r   z-_normalize_pool_priorities.<locals>.<genexpr>G  s2      KK5+<U\+J+JKKKKKKKrl   c                n                         | j        t                              | j        | j        fS rh   )rq   rQ   r  rP   rM   )r   source_ranks    r(   r  z,_normalize_pool_priorities.<locals>.<lambda>H  s/    OOEL#k*:*:;;NK
 rl   c                $    i | ]\  }}|j         |S r   r  )r|   r=  r   s      r(   r   z._normalize_pool_priorities.<locals>.<dictcomp>P  s     DDD:33DDDrl   r   T)r  r<  rP   r   rL   )
rK   r   manual_entriesseeded_entriesordered	id_to_idxchangedr  r   r  s
            @r(   _normalize_pool_prioritiesr  7  s   ;u  !'(!" K GGGGGG((  N KKGKKK
 
 
 
  N 100GDD71C1CDDDIG(11  e>\))+25<+P+P+PGIeh'(GNrl   Tuple[bool, Set[str]]c                  # d}t                      }t                      }	 ddlm} n# t          $ r d }Y nw xY w| dk    r	 ddlm}  |d          s||fS n# t          $ r Y nw xY wt                      #dQ#fd
} |d          } |d          p
 |d          }	t          |o|	           }
|
r9d |D             }t          |          t          |          k    r	||d d <   d}||fS ddl	m
}m} d |            fd |            ffD ]\  }}|r|                    d          r || |          r)|                    |           |t          || ||t          |                    dd          |                    d          |                    d          t!          |                    dd          |          d          z  }n| dk    rt#          |d          }t          t%          |t&                    okt)          |                    d          pd                                          p5t)          |                    d          pd                                                    }|r7|s5d |D             }t          |          t          |          k    r	||d d <   d}|rB|r? || d          s2|                    d           t)          |                    d          pd                                          }|p#t!          |                    dd          d          }|t          || di dddt          d|                    dd          d |                    d           d!|                    d!          d"|                    d"          d#|                    d#          d$|                    d$          d%|                    d%          d&|                    d&          d|                    d          d'|                    d'          d(|                    d(          d)|                    d)          d*|                    d*          d+|                    d+          d,|                    d,          |                    d-          t%          |                    d.          t&                    r|                    d.          nd |d/          z  }n| d0k    r	 dd1lm}m}  |            \  }}|r ||          }d2|                                v rd3nd4| } || |          sR|                    |           t5          j        |           }|t          || ||t6          ||r|j        nd|d5          z  }n/# t:          $ r&}t<                              d6|           Y d }~nd }~ww xY w| d7k    r	 dd8lm }  |d9          }|                    d:d          }|r|                    dd;          } || |          sq|                    |           |t          || ||t          ||                    d<          |                    d=d          |                    d>|          d?          z  }n6# t:          $ r&}t<                              d@|           Y d }~nd }~ww xY w| dAk    r	 ddBlm!}  |dA          }|rP|                    d          r:dC} || |          s+|                    |           d }	 ddDl"m"} |                    d!d          }|r7tG          |$                    |          %                                dEz            }n# t:          $ r d }Y nw xY wt)          |                    d&d          pd          &                    dF          } |t          || ||t          |d         |                    d           || |                    dd          p#t!          |                    dd          |          dG          z  }n# t:          $ r&}t<                              dH|           Y d }~njd }~ww xY w| dIk    rC || d          r||fS t#          |dI          }t%          |t&                    r|                    dJ          nd }!t%          |!t&                    r|!                    d          r|                    d           t)          |                    d          pd                                          }|t          || ddt          |!                    dd          |!                    d           dK|                    dL          |p#t!          |!                    dd          d          dM          z  }n| dNk    r || dO          r||fS t#          |dN          }t%          |t&                    r|                    dJ          nd }!t%          |!t&                    r|!                    d          r|                    dO           ddPlm'}" |"} |t          || dOdOt          |!                    dd          |!                    d           | |                    dL          t!          |!                    dd          dO          dM          z  }||fS )RNFr   is_source_suppressedc                    dS NFr   _p_ss     r(   _is_suppressedz-_seed_from_singletons.<locals>._is_suppressedc      5rl   rP  )!is_provider_explicitly_configuredr   rJ   r!   c                                         |           p t          j                             |           pd                                S rv  )rq   osenvironr   )r   	_env_files    r(   _env_valz'_seed_from_singletons.<locals>._env_val  s9    MM#&&C"*..*=*=CJJLLLrl   ANTHROPIC_API_KEYANTHROPIC_TOKENCLAUDE_CODE_OAUTH_TOKENc                $    g | ]}|j         d v|S )>   rQ  r  r  r   s     r(   rB  z)_seed_from_singletons.<locals>.<listcomp>  s0       <'EEE EEErl   T)rR  read_hermes_oauth_credentialsr  rQ  rT  r   rS  rU  )rQ   rN   rR   rT   r_   rM   r   rR   rb   c                $    g | ]}|j         d v|S )>   manual:device_coderb  r  r   s     r(   rB  z)_seed_from_singletons.<locals>.<listcomp>  s0       <'LLL LLLrl   rb  rM   rQ   rN   rT   r^   r?   r<   r=   rC   ra   rc   r@   r>   rA   rF   rD   rG   r;   )rG   r;   rM   copilot)resolve_copilot_tokenget_copilot_api_tokenghgh_clienv:rQ   rN   rR   r]   rM   zCopilot token seed failed: %sz
qwen-oauth) resolve_qwen_runtime_credentials)refresh_if_expiringr4   zqwen-clir_   r]   	auth_file)rQ   rN   rR   r_   r]   rM   z Qwen OAuth token seed failed: %szminimax-oauth)get_provider_auth_stater3   )r   r  r   )rQ   rN   rR   rT   r_   r]   rM   z#MiniMax OAuth token seed failed: %sra  rc  z%https://chatgpt.com/backend-api/codexr`   )rQ   rN   rR   rT   r]   r`   rM   rq  rr  )DEFAULT_XAI_OAUTH_BASE_URLr   rJ   r!   rJ   )(r  r   hermes_cli.authr!  ImportErrorr)  r   r   r  rW  rR  r3  rq   addr  r  r   r   r   r   rJ   r   hermes_cli.copilot_authr7  r8  r   r   r   ra   r'   rX  rY  r=  r@  r   rO   r   r   r  rA  )$rK   r   r  active_sourcesrg  r'  r)  r.  anthropic_api_keyanthropic_oauth_envapi_key_path_explicitretainedrR  r3  source_namerZ  rh  has_runtime_materialcustom_labelseeded_labelr7  r8  r   rQ   	api_tokenpconfigr^  r=  r@  r_   _dtr   r]   rc  rA  r-  s$                                      @r(   _seed_from_singletonsrS  Y  sB   G"uuN!##JJJJJJJJ   	 	 	 	 	 ;
	IIIIII44[AA /../ 	 	 	D	& JJ		M 	M 	M 	M 	M 	M %H%899H&''N884M+N+N 	 !%%6%R?R;R S S  	+ #*  H 8}}G,,%
N**gggggggg 99;;<88::;#
 	 	K  =11 !>(K88 "";///="-%4(-		-(D(D).>)B)B).;)?)?!1%))M22N2NP[!\!\ 	  	, 
V		$Z88#ud## EIIn--344::<< =uyy--344::<<	 
  
  	- 	 #*  H 8}}G,,%
 *	) *	..=2Y2Y *	}---
 uyy117R88>>@@L' +;		."--}, ,L }m #EIInb$A$A $UYY%?%?	
 !%))L"9"9 !%))L"9"9 UYYw//  ;!7!7 &uyy1B'C'C )%))4H*I*I  ;!7!7 +EII6L,M,M& "599]#;#;'( !%))L"9"9)* #EIIn$=$=+, +EII6L,M,M-. '		2D(E(E/0 .3YY7N-O-O/9%))E:J:JD/Q/Q[599U+++W[)5  	     GD 
Y			?\\\\\\\\1133ME6 11%88	*.&,,..*@*@hhoVoo%~h<< "&&{333/3H==G} #&1):,5FM(U(B(BSU%+ 	    G  	? 	? 	?LL8#>>>>>>>>	? 
\	!	!	BHHHHHH44OOOEIIi,,E #ii*==%~h<< "&&{333} #&1)8,1-2YY-G-G(-		*b(A(A%*YY{K%H%H 	    G  	B 	B 	BLL;SAAAAAAAA	B 
_	$	$!	E??????++O<<E >22 %%~h<< "&&{333$(M-<<<<<<#iib99 [,/0A0A#0F0F0P0P0R0RUY0Y,Z,ZM$ - - -(,-"599-A2#F#F#L"MMTTUXYYH} #&1)8,1.,A-2YY-G-G-:(0%*YYw%;%; &?O %		." = ={@ @
 
	    G   	E 	E 	ELL>DDDDDDDD	E 
^	#	#
 >(M22 	+N**$Z@@(25$(?(?I8$$$T fd## 	

>(B(B 	}---uyy117R88>>@@L}+!0$*JJ~r$B$B%+ZZ%@%@ G$)IIn$=$=)l-=fjjY[>\>\^k-l-l 	  G 
[	 	  >(O44 	+N**$Z==(25$(?(?I8$$$Tfd## 	

>(B(B 	///BBBBBB1H}-!0$*JJ~r$B$B%+ZZ%@%@ ($)IIn$=$=-fjj.L.Lo^^ 	  G N""s   ( 88A 
A&%A&5BV 
WV??WB=Z 
Z=Z88Z=A`. A]/ .`. /]>;`. =]>>B.`. .
a8aac                   d}t                      }d d}	 ddlm} n# t          $ r d }Y nw xY wd!dt          dd"fd}| dk    r_ |d          }|rNd} || |          r||fS |                    |           |t          || | ||d|t                              z  }||fS t          j	        |           }	|	r|	j
        t          k    r||fS d}
|	j        r# ||	j                                      d          }
t          |	j                  }| dk    rg d}|D ]} ||          }|sd| } || |          r"|                    |           | dk    r|                    d          st           nt          }|
p|	j        }| dk    rt%          ||	j        |
          }n| dk    rt'          ||	j        |
          }|t          || | ||||||                    z  }||fS )#NFr   rJ   r!   c                    t                      }|                    |           p t          j                            |           pd}|                                S rv  )r   rq   r+  r,  r   )r   env_filer}  s      r(   _get_env_prefer_dotenvz._seed_from_env.<locals>._get_env_prefer_dotenv  sB    ::ll3<2:>>##6#6<"yy{{rl   r   r   c                    dS r#  r   r$  s     r(   _is_source_suppressedz-_seed_from_env.<locals>._is_source_suppressed  r(  rl   env_varrS   c                    	 ddl m}  ||           }n# t          $ r d }Y nw xY w|r!t          |                                          nd S )Nr   )get_secret_source)hermes_cli.env_loaderr\  r'   rJ   r   )rZ  r\  source_labels      r(   _secret_source_for_envz._seed_from_env.<locals>._secret_source_for_env  ss    	 ??????,,W55LL 	  	  	 LLL	 ,8Bs<  &&(((dBs    ##)rN   rQ   r   r]   rN   re   c                <    | ||||d} |          }|r||d<   |S )Nr<  rB   r   )rQ   rZ  r   r]   rN   rv   rB   r_  s          r(   _env_payloadz$_seed_from_env.<locals>._env_payload  sI     "! #
 #
 /.w77 	5'4GO$rl   
openrouterOPENROUTER_API_KEYzenv:OPENROUTER_API_KEY)rQ   rZ  r   r]   r   r   rP  )r0  r1  r/  r;  z
sk-ant-apizkimi-codingzai)rQ   rZ  r   r]   rN   rB  )rZ  rJ   r!   rS   )rQ   rJ   rZ  rJ   r   rJ   r]   rJ   rN   rJ   r!   re   )r  rC  r!  rD  r   rE  r  r   r   rq   rN   base_url_env_varr  r   api_key_env_varsr   r  ra   r   r   )rK   r   r  rG  rW  rY  ra  r   rQ   rQ  env_urlenv_varsrZ  rN   r]   r_  s                  @r(   _seed_from_envri    s   G"uuN   QQQQQQQ   	 	 	 	 	C C C C +       ( <&&';<< 	-F$$Xv66 /..v&&&}!00	  	
 
 
G &&#H--G 'g'+<<<&&G O(()ABBII#NNG,--H;
 
 
  
 
&&w// 	!!!  622 	6"""'/;'>'>uGWGWXdGeGe'>OOk|	8g8}$$-eW5OQXYYHH,UG4NPWXXH=L!#  	
 
 	
 N""s    ..rG  Set[str]c                v    fd| D             }t          |          t          |           k    rdS || d d <   dS )Nc                    g | ]F}t          |j                  s.|j        v s%t          |j        |j                  s|j        d k    D|GS )r  )r   rQ   r   rK   )r|   r   rG  s     r(   rB  z/_prune_stale_seeded_entries.<locals>.<listcomp>  sk       U\** <>)))%,GG * |},, 	 -,,rl   FT)r  )r   rG  rK  s    ` r(   _prune_stale_seeded_entriesrm    s[         H 8}}G$$uGAAAJ4rl   c                   d}t                      }	 ddlm} n# t          $ r d }Y nw xY wt	          |           }|rt          |                    d          pd                                          }t          |                    d          pd                                                              d          }t          |                    d	          pd                                          }|rHd
| }	 || |	          s7|	                    |	           |t          || |	|	t          |||p|	d          z  }	 t                      }
|
r|
                    d          nd}t          |t                    rNt          |                    d          pd                                                                          }t          |                    d          pd                                                              d          }d}dD ]V}|                    |          }t          |t
                    r*|                                r|                                } nW|dk    r\|rZ|rXt!          |          }|| k    rCd}	 || |	          s5|	                    |	           |t          || |	|	t          ||dd          z  }n# t"          $ r Y nw xY w||fS )zJSeed a custom endpoint pool from custom_providers config and model config.Fr   r   c                    dS r#  r   r$  s     r(   r'  z)_seed_custom_pool.<locals>._is_suppressed-  r(  rl   r4   r   r]   r   rm   zconfig:r<  modelNrK   )r4   apicustommodel_config)r  rC  r!  rD  r  rJ   rq   r   r  rE  r  r   r)   r   r   r   r  r'   )r  r   r  rG  r'  	cp_configr4   r]   rm   rQ   r   	model_cfgmodel_providermodel_base_urlmodel_api_keyr   r   matched_keys                     r(   _seed_custom_poolrz  $  sA   G"uuNJJJJJJJ   	 	 	 	 	
 ,H55I immI..4"55;;==y}}Z006B77==??FFsKK9==((.B//5577 	%t%%F!>(F33 ""6***="(%6(/$,!% 	   "$$+1;FJJw'''t	i&& 	 z!:!:!@bAAGGIIOOQQN z!:!:!@bAAGGIIPPQTUUNM'  MM!$$a%% !'')) $%GGIIME))n)):>JJ(**+F)>(F;; &**6222=#$"*0->0=,:)7 	$ $      N""s    ))=FK	 	
KKc                p     pd                                                                  t                     }t           fd|D                       } fd|D             }                     t
                    r+t           |          \  }}|p|}|t          ||          z  }nUt           |          \  }}t           |          \  }	}
|p|p|	}|t          |||
z            z  }|t           |          z  }|r*t           d t          |d           D                        t           |          S )Nr   c              3  j   K   | ]-}t          |t                    ot          |          |k    V  .d S rh   )r   r   r   r|   rv   rK   s     r(   r   zload_pool.<locals>.<genexpr>p  s[       ! !  	7D!! 	O0(CCwN! ! ! ! ! !rl   c                F    g | ]}t                               |          S r   )rI   r   r}  s     r(   rB  zload_pool.<locals>.<listcomp>u  s*    XXX))(G<<XXXrl   c                6    g | ]}|                                 S r   rA  r   s     r(   rB  zload_pool.<locals>.<listcomp>  s     ZZZU]]__ZZZrl   c                    | j         S rh   r   )r  s    r(   r  zload_pool.<locals>.<lambda>  s    4= rl   r   )r   r   r   rx  r   r  rz  rm  rS  ri  r  r    r  r  )rK   raw_entriesraw_needs_sanitizationr   custom_changedcustom_sourcesr  singleton_changedr  env_changedenv_sourcess   `          r(   	load_poolr  m  s   B%%''--//H&x00K  ! ! ! ! #! ! !  
 YXXXKXXXG-.. 
A):8W)M)M&(:N.wGGG/DXw/W/W,,#1(G#D#D [(L,=L.w8IK8WXXX-h@@@ 
ZZ&>X>X*Y*Y*YZZZ	
 	
 	
 (G,,,rl   )r!   r"   )r   rJ   r   rJ   r!   rJ   )r   r   r!   rO   )rQ   rJ   r!   r   )r   rX   r!   rO   )r   r	   r!   rV   )r   rJ   r!   rV   )r   r   r!   re   )r   rI   r!   rV   )rm   rJ   r!   rJ   rh   )r   r"   )r]   rS   r   rS   r!   rS   )r!   r  )r  rJ   r!   r   )rK   rJ   r!   rJ   )
r   r   rK   rJ   rQ   rJ   rv   re   r!   r   )rK   rJ   r   r   r!   r   )rK   rJ   r   r   r!   r  )r   r   rG  rj  r!   r   )r  rJ   r   r   r!   r  )rK   rJ   r!   r  )___doc__
__future__r   loggingr+  r8   r$  r   r   r   dataclassesr   r   r   r   r   typingr	   r
   r   r   r   r   hermes_constantsr   r&   r   agent.credential_persistencer   r   rC  authr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    	getLoggerrt   rX  r)   r  r   rK  	frozensetrG  r  r  r   r   SOURCE_MANUAL_DEVICE_CODEr  r  r  r  r  r   r   r   r  rp   rI   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r(  r  r  r  rS  ri  rm  rz  r  r   rl   r(   <module>r     s   B B " " " " " "  				        				 2 2 2 2 2 2 2 2 2 2 ' ' ' ' ' ' ' ' 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 0 0 0 0 0 0 & & & & & &        # " " " " "                               " 
	8	$	$    	  
 # $ $ $   ( !-  ,::: " $ " 	  # #  ' 
   i      b b b b b b b bJ   F F F FU U U U
) ) ) )   <   .   2   2 2 2 2
7 7 7 7 74    <           )* %J J J J J J J JZ"& & & &R   D}# }# }# }#@
t# t# t# t#n   (F# F# F# F#R- - - - - -rl   