ó
    °"³j>  ã                  ój  • % S SK Jr  S SKrS SKrS SKrS SKrS SKJrJrJ	r	J
r
JrJr  S SKrS SKrSSKJrJrJrJr  SSKJrJr  SSKJr  SS	KJrJr  SS
KJr  S/r\R>                  " \ 5      r!S\"S'   Sr#\	" S\RH                  \RH                  5      r%SS jr&SS jr' " S S\\%   5      r( " S S5      r)g)é    )ÚannotationsN)ÚUnionÚGenericÚTypeVarÚCallableÚOptionalÚcasté   )ÚAccessTokenÚAccessTokenProviderÚAsyncAccessTokenProviderÚis_async_token_provideré   )ÚasyncifyÚ(signature_without_evaluating_annotations)ÚWorkloadIdentityError)ÚADVISORY_REFRESH_SECONDSÚMANDATORY_REFRESH_SECONDS)ÚAnthropicErrorÚ
TokenCachezlogging.LoggerÚlogé   Ú_EventTc                óà   • [         R                  " U 5      (       a  U R                  5         [        S5      e[	        U [
        5      (       d"  [        S[        U 5      R                   S35      eU $ )Nzicredentials provider returned a coroutine instead of an AccessToken; pass the `async def` function itselfzcredentials provider returned z instead of an AccessToken)ÚinspectÚiscoroutineÚcloser   Ú
isinstancer   ÚtypeÚ__name__)Úvalues    Ú]/home/mande/repo/quber/.venv/lib/python3.13/site-packages/anthropic/lib/credentials/_cache.pyÚvalid_access_tokenr#      sa   € Ü×Ò˜5×!Ñ!à�‰ŒÜØwó
ð 	
ô �eœ[×)Ñ)ÜÐ=¼dÀ5»k×>RÑ>RÐ=SÐSmÐnÓoÐoØ€Ló    c                ó–   •  [        S [        U 5      R                  R                  5        5       5      $ ! [        [
        4 a     gf = f)z]False for a legacy provider that takes no `force_refresh` argument; it is called without one.c              3  óš   #   • U  HA  nUR                   S :H  =(       d&    UR                  [        R                  R                  L v •  MC     g7f)Úforce_refreshN)ÚnameÚkindr   Ú	ParameterÚVAR_KEYWORD)Ú.0Ú	parameters     r"   Ú	<genexpr>Ú(accepts_force_refresh.<locals>.<genexpr>-   s=   é € ð 
âc�	ð �N‰N˜oÑ-×`°·±Ä7×CTÑCT×C`ÑC`Ð1`Ô`Úcùs   ‚A	AT)Úanyr   Ú
parametersÚvaluesÚ	TypeErrorÚ
ValueError)Úproviders    r"   Úaccepts_force_refreshr6   *   sN   € ðÜñ 
äEÀhÓO×ZÑZ×aÑaÔcó
ó 
ð 	
øô ”zÐ"ó áðús   ‚25 µAÁAc                  ó6   • \ rS rSrSr          SS jrSrg)ÚRefreshé6   zXWhat a caller that `TokenCache._next_step` told to call the provider needs to finish up.c               ó:   • Xl          X l         X0l         X@l        g )N©Úowned_eventÚforceÚadvisory_fallbackÚremaining_seconds)Úselfr<   r=   r>   r?   s        r"   Ú__init__ÚRefresh.__init__9   s'   € ð /:Ôð	TàŒ
Ø^Ø!2ÔØTØ!2Õr$   )r>   r=   r<   r?   N)
r<   zOptional[_EventT]r=   Úboolr>   zOptional[AccessToken]r?   ÚintÚreturnÚNone)r    Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rA   Ú__static_attributes__© r$   r"   r8   r8   6   s<   † Ùbð3ð 'ð3ð ð	3ð
 1ð3ð ð3ð 
÷3r$   r8   c                  ó¸   • \ rS rSrSr\\\R                  S.         SS jjrSS jr	SS jr
SS jrSS jrSS	 jrSS
 jrSS jrSS jrSS jrSS jrSrg)r   éK   uê  Thread-safe cache wrapping an `AccessTokenProvider` or an
`AsyncAccessTokenProvider` with two-tier proactive refresh and
single-flight semantics.

`async_get_token` applies the same policy as `get_token`: an async
provider is awaited on the event loop, a sync one runs `get_token` in a
worker thread.

Refresh policy on each `get_token` call:

* No cached token â†’ call provider (blocking), cache, return.
* Cached with `expires_at=None` â†’ return cached forever (never refresh).
* More than `advisory_refresh_seconds` remaining â†’ return cached.
* Between `mandatory_refresh_seconds` and `advisory_refresh_seconds`
  remaining (advisory window) â†’ try provider; on success swap cache; on
  failure log a warning and return the stale cached token. If another
  caller is already refreshing, the advisory caller just returns the
  cached token â€” no second refresh, no waiting.
* Less than `mandatory_refresh_seconds` remaining or already expired
  (mandatory window) â†’ call provider; on failure RAISE. Concurrent
  mandatory callers wait on a shared `Event` so exactly one provider
  call is in flight.
* Cached with `expires_at=0` â†’ never served. The provider does its own
  caching, so every call asks it again and concurrent callers each make
  their own call, so the provider must be safe to call concurrently. Only
  exactly `0` does this; any other past value is an expired token and is
  refreshed one call at a time. After a 401 empties the cache, one caller
  makes the `force_refresh=True` call and callers that arrive after the
  401 wait for it; calls already in flight finish on their own and do not
  repopulate the cache. Normal caching resumes once the provider returns a
  token with a real expiry.

The lock is released before the provider call so a 30-second HTTP POST
doesn't serialize unrelated callers through a single thread. This matters
under async: `asyncify(get_token)` runs on the thread pool, and holding
the lock across the network call would pin an async worker for the whole
exchange.
)Úadvisory_refresh_secondsÚmandatory_refresh_secondsÚtime_sourcec               ó
  • S U l         S U l        [        U5      (       a  Xl        OXl         [        U5      U l        X l        X0l        X@l        [        R                  " 5       U l
        S U l        S U l        SU l        SU l        g )NFg        )Ú	_providerÚ_async_providerr   r6   Ú_accepts_force_refreshÚ	_advisoryÚ
_mandatoryÚ_time_sourceÚ	threadingÚLockÚ_lockÚ_cachedÚ_refresh_eventÚ_next_forceÚ_last_advisory_failure_time)r@   r5   rO   rP   rQ   s        r"   rA   ÚTokenCache.__init__s   s{   € ð 9=ˆŒØCGˆÔÜ" 8×,Ñ,Ø#+Õ à%ŒNÜ&;¸HÓ&EˆÔ#Ø1ŒØ3ŒØ'ÔÜ—^’^Ó%ˆŒ
Ø.2ˆŒð
 NRˆÔð !ˆÔð 36ˆÕ(r$   c               ó>   • U R                   (       d  U" 5       $ U" US9$ )zZInvoke `provider`, leaving out `force_refresh` for a legacy provider that doesn't take it.©r'   ©rU   ©r@   r5   r=   s      r"   Ú_invoke_providerÚTokenCache._invoke_provider“   s   € à×*×*Ù“:ÐÙ eÑ,Ð,r$   c             ƒ  ón   #   • U R                   (       d  U" 5       I Sh  v•N $ U" US9I Sh  v•N $  N N7f)z$Async version of `_invoke_provider`.Nrb   rc   rd   s      r"   Ú_async_invoke_providerÚ!TokenCache._async_invoke_provider™   s/   é € à×*×*Ù!›×#Ð#Ù¨EÑ2×2Ð2ñ $Ù2ùs   ‚5ž1Ÿ5¬3­5³5c               óà   •  [        U R                  XS95      $ ! [         aI  nUR                  S:w  a  e [        R                  S5        [        U R                  USS95      s SnA$ SnAff = f)zBCall the provider, retrying once on a 401 from the token endpoint.©r=   é‘  ú*Token provider returned 401; retrying onceTN)r#   re   r   Ústatus_coder   Údebug©r@   r5   r=   Úerrs       r"   Ú_call_providerÚTokenCache._call_providerŸ   sj   € ð	SÜ% d×&;Ñ&;¸HÐ&;Ð&RÓSÐSøÜ$ó 	SØ�‰ #Ó%ØÜ�I‰IÐBÔCÜ% d×&;Ñ&;¸HÈDÐ&;Ð&QÓRÕRûð		Sús   ‚ š
A-¤>A(Á"A-Á(A-c             ƒ  ó  #   •  [        U R                  XS9I Sh  v•N 5      $  N! [         aR  nUR                  S:w  a  e [        R                  S5        [        U R                  USS9I Sh  v•N  5      s SnA$ SnAff = f7f)z"Async version of `_call_provider`.rk   Nrl   rm   T)r#   rh   r   rn   r   ro   rp   s       r"   Ú_async_call_providerÚTokenCache._async_call_provider©   sy   é € ð	_Ü%¨D×,GÑ,GÈÐ,GÐ,^×&^Ó_Ð_Ñ&^øÜ$ó 	_Ø�‰ #Ó%ØÜ�I‰IÐBÔCÜ%¨D×,GÑ,GÈÐX\Ð,GÐ,]×&]Ð&]Ó^Õ^ûð		_üsH   ‚B„& ›$œ& £B¤& ¦
B°>A=Á.A1
Á/A=Á7BÁ8BÁ=BÂBc                ó¸  • U R                   nUc  [        S5      e U R                  [        R                  5      n[        U[        5      (       a  U$ [        U[        R                  5      (       a  UR                  5         Mh   U R                  XR                  S9nU R                  X25      $ ! [         a!  nU R                  XB5      nUc  e Us SnA$ SnAff = f)z5Return a valid bearer token, refreshing if necessary.Nz]This `TokenCache` wraps an async provider; call `async_get_token()` instead of `get_token()`.rk   )rS   ÚRuntimeErrorÚ
_next_steprY   ÚEventr   ÚstrÚwaitrr   r=   ÚBaseExceptionÚ_end_failed_refreshÚ_end_refresh©r@   r5   ÚstepÚfreshrq   Ústale_tokens         r"   Ú	get_tokenÚTokenCache.get_token³   sÉ   € à—>‘>ˆØÑÜØoóð ð Ø—?‘?¤9§?¡?Ó3ˆDÜ˜$¤×$Ñ$Ø�Ü˜$¤	§¡×0Ñ0Ø—	‘	”ñ ð#Ø×+Ñ+¨H¿J¹JÐ+ÐG�ð ×$Ñ$ UÓ1Ð1øô !ó #Ø"×6Ñ6°sÓA�ØÑ&ØØ"Õ"ûð	#ús   ÂB. Â.
CÂ8CÃCÃCc              ƒ  ó  #   • U R                   nUc"  [        U R                  5      " 5       I Sh  v•N $  U R                  [        R
                  5      n[        U[        5      (       a  U$ [        U[        R
                  5      (       a  UR                  5       I Sh  v•N   Mp   U R                  XR                  S9I Sh  v•N nU R                  X25      $  N§ N; N! [         a!  nU R                  XB5      nUc  e Us SnA$ SnAff = f7f)z„Async version of `get_token`.

An async provider is awaited on the event loop. A sync provider runs
`get_token` in a worker thread.
Nrk   )rT   r   r„   ry   Úanyiorz   r   r{   r|   ru   r=   r}   r~   r   r€   s         r"   Úasync_get_tokenÚTokenCache.async_get_tokenÒ   sç   é € ð ×'Ñ'ˆØÑÜ! $§.¡.Ô1Ó3×3Ð3ØØ—?‘?¤5§;¡;Ó/ˆDÜ˜$¤×$Ñ$Ø�Ü˜$¤§¡×,Ñ,Ø—i‘i“k×!Ð!Ùð#Ø"×7Ñ7¸Ï
É
Ð7ÐS×S�ð ×$Ñ$ UÓ1Ð1ñ# 4ñ "ñ
 TøÜ ó #Ø"×6Ñ6°sÓA�ØÑ&ØØ"Õ"ûð	#üsd   ‚-D¯C°A-DÂCÂDÂ%C ÃCÃC ÃDÃDÃC Ã
DÃ'DÃ=DÃ>DÄDÄDc                ó  • SnSnU R                      U R                  nUGb   UR                  c  UR                  sSSS5        $ UR                  S:X  a  [	        SSSSS9sSSS5        $ UR                  U R                  5       -
  nXPR                  :”  a  UR                  sSSS5        $ XPR                  :”  ai  U R                  b  UR                  sSSS5        $ U R                  5       U R                  -
  [        :  a  UR                  sSSS5        $ Un[        U5      nU R                  b#  [        [        U R                  5      sSSS5        $ U" 5       =o`l        [	        UU R                  UUS9sSSS5        $ ! , (       d  f       g= f)zäDecide under the lock what the caller does next.

A `str` is the token to serve. An event is a refresh in flight to wait on
before asking again. A `Refresh` means call the provider, then
`_end_refresh` or `_end_failed_refresh`.
Nr   Fr;   )r[   r\   Ú
expires_atÚtokenr8   rX   rV   rW   r]   r_   Ú ADVISORY_REFRESH_BACKOFF_SECONDSrD   r	   r   r^   )r@   Úevent_classr>   r?   ÚcachedÚ	remainingr<   s          r"   ry   ÚTokenCache._next_stepí   s\  € ð 48ÐØÐØ�Z‹ZØ—\‘\ˆFØÒ!Ø×$Ñ$Ñ,Ø!Ÿ<™<÷	 ‰Zð
 ×$Ñ$¨Ó)ô #¨t¸5ÐTXÐlmÑn÷ ‰Zð #×-Ñ-°×0AÑ0AÓ0CÑC�	ØŸ~™~Ó-Ø!Ÿ<™<÷ ‰Zð Ÿ™Ó.ð ×*Ñ*Ñ6Ø%Ÿ|™|÷# ‰Zð( ×(Ñ(Ó*¨T×-MÑ-MÑMÔPpÓpØ%Ÿ|™|÷+ ‰Zð, )/Ð%Ü(+¨I«Ð%à×"Ñ"Ñ.ô œG T×%8Ñ%8Ó9÷9 ‰Zñ> 1<³Ð=ˆKÔ-ÜØ'Ø×&Ñ&Ø"3Ø"3ñ	÷A �Z�Zús/   ‘)E>ÁE>Á)7E>Â*'E>Ã0E>Ä3E>Å"E>Å>
Fc                ó¸  • U R                      UR                  c.  U R                  b   U R                  R                  S:X  a  Xl        OGUR                  (       d  U R
                  (       d  Xl        UR                  (       a  SU l        S U l        S S S 5        UR                  b  UR                  R                  5         UR                  $ ! , (       d  f       NA= f)Nr   F)	r[   r<   r\   r‹   r=   r^   r]   ÚsetrŒ   )r@   r‚   Úrefreshs      r"   r   ÚTokenCache._end_refresh  sœ   € Ø�Z‹ZØ×"Ñ"Ñ*ð —<‘<Ñ+°·±×0GÑ0GÈ1Ó0LØ#(”Løð —=—=¨×(8×(8Ø#(”LØ—=—=à',�DÔ$Ø&*�Ô#÷ ð  ×ÑÑ*Ø×Ñ×#Ñ#Ô%Ø�{‰{Ð÷% �Zús   �BCÃ
Cc                ó  • UR                   b6  U R                     SU l        SSS5        UR                   R                  5         UR                  b%  [        U[        [        R                  45      (       d  g[        R                  SUR                  U5        U R                     U R                  5       U l        SSS5        UR                  R                  $ ! , (       d  f       N¼= f! , (       d  f       UR                  R                  $ = f)z…Release any waiters. Returns the stale token to serve if this was an advisory
refresh, or `None` if the caller should re-raise `err`.NzGAdvisory token refresh failed (%ds remaining); serving cached token: %s)r<   r[   r]   r“   r>   r   r   Úhttpx2Ú	HTTPErrorr   Úwarningr?   rX   r_   rŒ   )r@   rq   r”   s      r"   r~   ÚTokenCache._end_failed_refresh2  sË   € ð ×ÑÑ*Ø—“Ø&*�Ô#÷ à×Ñ×#Ñ#Ô%Ø×$Ñ$Ñ,´J¸sÄ^ÔU[×UeÑUeÐDf×4gÑ4gØÜ�‰ØUØ×%Ñ%Øô	
ð
 �Z‹ZØ/3×/@Ñ/@Ó/BˆDÔ,÷ à×(Ñ(×.Ñ.Ð.÷ •ú÷ ŒZà×(Ñ(×.Ñ.Ð.ús   šCÂ$C)Ã
C&Ã)
Dc                ól   • U R                      SU l        SU l        SSS5        g! , (       d  f       g= f)zÔClear the cached token so the next `get_token` re-invokes the provider.

Also sets a one-shot `force_refresh` flag so on-disk providers skip
their freshness short-circuit instead of re-serving the revoked token.
NT)r[   r\   r^   )r@   s    r"   Ú
invalidateÚTokenCache.invalidateD  s$   € ð �Z‹ZØˆDŒLØ#ˆDÔ÷ �ZŽZús   �%¥
3)rU   rV   rT   r\   r_   r[   rW   r^   rS   r]   rX   N)
r5   z.AccessTokenProvider | AsyncAccessTokenProviderrO   rD   rP   rD   rQ   zCallable[[], float]rE   rF   )r5   r   r=   rC   rE   r   )r5   r   r=   rC   rE   r   )rE   r{   )rŽ   zCallable[[], _EventT]rE   z%Union[str, _EventT, Refresh[_EventT]])r‚   r   r”   úRefresh[_EventT]rE   r{   )rq   r}   r”   rž   rE   zOptional[str])rE   rF   )r    rG   rH   rI   rJ   r   r   ÚtimerA   re   rh   rr   ru   r„   rˆ   ry   r   r~   rœ   rK   rL   r$   r"   r   r   K   sƒ   † ñ%ðV )AØ)BØ+/¯9©9ñ6à@ð6ð #&ð	6ð
 $'ð6ð )ð6ð 
õ6ô@-ô3ôSô_ô2ô>2ô6.ô`ô*/÷$$r$   )r!   ÚobjectrE   r   )r5   zCallable[..., object]rE   rC   )*Ú
__future__r   rŸ   r   ÚloggingrY   Útypingr   r   r   r   r   r	   r‡   r—   Ú_typesr   r   r   r   Ú_utilsr   r   Ú	_workloadr   Ú
_constantsr   r   Ú_exceptionsr   Ú__all__Ú	getLoggerr    r   Ú__annotations__r�   rz   r   r#   r6   r8   r   rL   r$   r"   Ú<module>r¬      s“   ðÞ "ã Û Û Û ß D× Dã Û ç gÓ gß HÝ ,ß KÝ )àˆ.€à×'Ò'¨Ó1€€^Ó 1ð
 $%Ð  á
�)˜YŸ_™_¨e¯k©kÓ
:€ô	ô	ô3ˆg�gÑô 3÷*A$ò A$r$   