+
    -pÐjá–  ã                  ó  a € 0 t $ ^ RIHt ^ RIt^ RIt^ RIHt ^ RIHtHtH	t	 ^ RI
HtHtHtHt ^ RIt^ RIHtHt ^ RIHtHtHt ^ RIHt ^ R	IHt ^ R
IHt ]'       d   ^ RI
Ht ]P<                  ! ]4      t Rt!R R lt"]! RR7       ! R R4      4       t#]! RR7       ! R R4      4       t$R R lt%R R lt&R R lt'R R lt(RR/R R  llt)R! R" lt*R# R$ lt+R% R& lt,R'R/R( R) llt-R* R+ lt.R, R- lt/R. R/ lt0RRR0R1/R2 R3 llt1R4 R5 lt2]! RR7       ! R6 R74      4       t3RRR0R1/R8 R9 llt4R: R; lt5R<R/R= R> llt6R? R@ lt7RA RB lt8RC RD lt9RERRF]:! 4       /RG RH llt;RIRJRKRL/t<Rwt=RM RN lt>RO RP lt?RxRQ RR llt@RxRS RT lltARxRUR/RV RW llltBRX RY ltC]! RR7       ! RZ R[4      4       tDR\tER] R^ ltFR_ R` ltGRa Rb ltHRcRRdRReR/Rf Rg lltIRxRh Ri lltJRj Rk ltKRl]@Rm]JRn]K/tLRo]MRp&   Rl]A/tNRo]MRq&   R1tORr Rs ltPRcRRdRRURRtR/Ru Rv lltQR# )yé    )ÚannotationsN)Ú	dataclass)ÚdatetimeÚtimezoneÚUTC)ÚTYPE_CHECKINGÚAnyÚCallableÚOptional)Ú_is_oauth_tokenÚresolve_anthropic_token)Ú	AuthErrorÚ_read_codex_tokensÚ!resolve_codex_runtime_credentials)Ú_codex_pool_route_base_url)Úresolve_runtime_provider)Úsafe_strftime)Ú	TypeGuardu-   Status: access depleted â€” top up to restorec               ó   € V ^8„  d   QhRR/# )é   Úreturnr   © )Úformats   "Ú"/opt/hermes/agent/account_usage.pyÚ__annotate__r      s   € ÷ ñ �(ñ ó    c                 ó6   € \         P                  ! \        4      # ©N)r   Únowr   r   r   r   Ú_utc_nowr       s   € Ü�<Š<œÓÐr   T)Úfrozenc                  óZ   € ] tR t^t$ R]R&   RtR]R&   RtR]R&   RtR]R	&   R
tR]R&   Rt	R# )ÚAccountUsageWindowÚstrÚlabelNzOptional[float]Úused_percentúOptional[datetime]Úreset_atúOptional[str]ÚdetailÚaccountÚscoper   )
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__annotations__r&   r(   r*   r,   Ú__static_attributes__r   r   r   r#   r#      s2   ‡ àƒJØ$(€L�/Ó(Ø#'€HÐ Ó'Ø €FˆMÓ ð €Eˆ3×r   r#   c                  ó®   € ] tR t^)t$ R]R&   R]R&   R]R&   RtR]R&   RtR	]R
&   RtR]R&   RtR]R&   Rt	R	]R&   Rt
R	]R&   RtR]R&   ]R R l4       tRtR# )ÚAccountUsageSnapshotr$   ÚproviderÚsourcer   Ú
fetched_atzAccount limitsÚtitleNr)   Úplanztuple[AccountUsageWindow, ...]Úwindowsútuple[str, ...]ÚdetailsÚunavailable_reasonÚidentityzOptional[dict]Úrawc               ó   € V ^8„  d   QhRR/# ©r   r   Úboolr   )r   s   "r   r   Ú!AccountUsageSnapshot.__annotate__<   s   € ÷ Rñ R˜4ñ Rr   c                	ó†   € \        V P                  ;'       g    V P                  4      ;'       d    V P                  '       * # r   )rB   r:   r<   r=   ©Úselfs   &r   Ú	availableÚAccountUsageSnapshot.available;   s.   € ä�D—L‘L×0Ð0 D§L¡LÓ1×QÐQ¸$×:QÑ:QÔ6QÐQr   r   )r-   r.   r/   r0   r1   r8   r9   r:   r<   r=   r>   r?   ÚpropertyrG   r2   r   r   r   r4   r4   )   sr   ‡ àƒMØƒKØÓØ!€Eˆ3Ó!Ø€Dˆ-ÓØ.0€GÐ+Ó0Ø!€Gˆ_Ó!Ø(,Ð˜Ó,ð #€HˆmÓ"ð €CˆÓàôRó ôRr   r4   c               ó0   € V ^8„  d   QhRRRRRRRRRRR	R
/# )r   r5   r$   r6   r:   Úlistr<   Úkwr	   r   r4   r   )r   s   "r   r   r   @   s?   € ÷ Oñ O˜ð O Sð O°4ð OÀ$ð OÈcð OÐVjñ Or   c                ó`   € \        RR V RVR\        4       R\        V4      R\        V4      /VB # )r5   r6   r7   r:   r<   r   )r4   r    Útuple)r5   r6   r:   r<   rL   s   &&&&,r   Ú	_snapshotrO   @   s^   € Üñ  O¨ð  O¸&ð  OÌXËZð  OÔafÐgnÓaoð  OÔy~ð  @Gó  zHð  Oð  LNñ  Oð  Or   c               ó    € V ^8„  d   QhRRRR/# )r   Úvaluer)   r   r   )r   s   "r   r   r   D   s   € ÷ Tñ T˜Mð T¨mñ Tr   c                ó¼   € \        T ;'       g    R 4      P                  4       pV'       d1   VP                  RR4      P                  RR4      P                  4       # R# )Ú Ú_Ú Ú-N)r$   ÚstripÚreplacer8   )rQ   Úcleaneds   & r   Ú_title_case_slugrZ   D   sI   € Ü�%—+�+˜2Ó×$Ñ$Ó&€GßBIˆ7�?‰?˜3 Ó$×,Ñ,¨S°#Ó6×<Ñ<Ó>ÐSÈtÐSr   c               ó    € V ^8„  d   QhRRRR/# )r   rQ   r	   r   r'   r   )r   s   "r   r   r   I   s   € ÷ ñ �Sð Ð/ñ r   c                óæ  € V R9   d   R # \        V \        \        34      '       d&   \        P                  ! \        V 4      \
        R7      # \        V \        4      '       d   V P                  4       ;p'       g   R # VP                  R4      '       d   VR R R,           MTp \        P                  ! V4      pVP                  '       d   V# VP                  \
        R7      #   \         d     R # i ; i)N)ÚtzÚZz+00:00)Útzinfo>   NrS   éÿÿÿÿ)Ú
isinstanceÚintÚfloatr   Úfromtimestampr   r$   rW   ÚendswithÚfromisoformatr_   rX   Ú
ValueError)rQ   ÚtextÚdts   &  r   Ú	_parse_dtrj   I   sº   € Ø�
ÔÙÜ�%œ#œu˜×&Ò&Ü×%Ò%¤e¨E£l´sÔ;Ð;Ü�eœS×!Ò!°%·+±+³-Ð*?¨$Ö*?ÙØ#'§=¡=°×#5Ò#5ˆ4��ˆ9�xÖ¸4€DðÜ×#Ò# DÓ)ˆØ—Y—Y�YˆrÐ: B§J¡J´c JÓ$:Ð:øÜô Úðús   Â!'C! Ã	C! ÃC! Ã!C0Ã/C0c               ó    € V ^8„  d   QhRRRR/# )r   ri   r'   r   r$   r   )r   s   "r   r   r   X   s   € ÷ Xñ XÐ(ð X¨Sñ Xr   c                ó^  € V '       g   R # \        V P                  4       R4      p\        V \        4       ,
          P	                  4       4      pV^ 8:  d   RV R2# \        VR4      w  r4V^<,          pV^8¼  d   \        V^4      w  rcRV RV RV R2# V'       d   RV RV R	V R2# RV R	V R2# )
Úunknownz%Y-%m-%d %H:%M %Zznow (Ú)i  zin zd zh (zh zm ()r   Ú
astimezonerb   r    Útotal_secondsÚdivmod)ri   Ústamprp   ÚhoursÚremÚminutesÚdayss   &      r   Ú_format_resetrw   X   sÄ   € ßÙÜ˜"Ÿ-™-›/Ð+>Ó?€EÜ˜œh›j�×7Ñ7Ó9Ó:€MØ˜ÔØ�u�g˜QÐÐÜ˜ tÓ,�J€EØ�R�i€GØ�„{Ü˜U BÓ'‰ˆØ�T�F˜"˜U˜G 3 u g¨QÐ/Ð/ß27ˆS���r˜'˜ # e W¨AÐ.ÐW¸sÀ7À)È3ÈuÈgÐUVÐ=WÐWr   ÚmarkdownFc               ó$   € V ^8„  d   QhRRRRRR/# )r   ÚsnapshotúOptional[AccountUsageSnapshot]rx   rB   r   ú	list[str]r   )r   s   "r   r   r   g   s$   € ÷ ñ Ð)Gð ÐVZð Ðgpñ r   c               ó>  € V '       g   . # V'       d   R MRpV P                   '       d   RV P                    R2MRpRV V P                   V 2RV P                   V 2.pV P                   Fã  pVP                  f   VP
                   R2pMW\        VP                  4      pVP
                   R\        ^ \        ^dV,
          4      4       R\        ^ \        V4      4       R	2pVP                  '       d!   VR
\        VP                  4       2,          pM(VP                  '       d   VRVP                   2,          pVP                  V4       Kå  	  VP                  V P                  4       V P                  '       d   VP                  RV P                   24       V# )z**rS   z (rn   u   ðŸ“ˆ z
Provider: z: unavailableú: z% remaining (z% used)u    â€¢ resets õ    â€¢ zUnavailable: )r9   r8   r5   r:   r&   r%   rc   ÚmaxÚroundr(   rw   r*   ÚappendÚextendr<   r=   )rz   rx   Úboldr9   ÚlinesÚwindowÚbaseÚuseds   &$      r   Úrender_account_usage_linesr‰   g   sh  € ßØˆ	ß‰4 €DØ$,§M§M MˆR�—‘ˆ˜aÑ °r€DØ�T�F˜8Ÿ>™>Ð*¨4¨&Ð1°ZÀ×@QÑ@QÐ?RÐSWÐRXÐ3YÐZ€EØ×"Ô"ˆØ×ÑÒ&Ø—l‘l�^ =Ð1‰Dä˜×,Ñ,Ó-ˆDØ—l‘l�^ 2¤c¨!¬U°3¸µ:Ó->Ó&?Ð%@ÀÌcÐRSÔUZÐ[_ÓU`ÓNaÐMbÐbiÐjˆDØ�?�?ˆ?Ø�l¤=°·±Ó#AÐ"BÐCÕC‰DØ�]�]ˆ]Ø�e˜FŸM™M˜?Ð+Õ+ˆDØ�‰�TÖñ #ð 
‡L�L�×!Ñ!Ô"Ø×"×"Ð"Ø�‰�} X×%@Ñ%@Ð$AÐBÔCØ€Lr   c               ó    € V ^8„  d   QhRRRR/# )r   Údrc   r   r$   r   )r   s   "r   r   r   ~   s   € ÷ ñ �ð ˜#ñ r   c                ó   € R V R 2# )Ú$z,.2fr   )r‹   s   &r   Ú_fmt_usdrŽ   ~   s   € Øˆq�ˆhˆ<Ðr   c               ó    € V ^8„  d   QhRRRR/# ©r   Úvr	   r   zTypeGuard[float]r   )r   s   "r   r   r   ‚   s   € ÷ 'ñ 'ˆsð 'Ð'ñ 'r   c                ó.   € \        V \        \        34      # r   )ra   rb   rc   ©r‘   s   &r   Ú_is_numr”   ‚   s   € Ü�aœ#œu˜Ó&Ð&r   c               ó    € V ^8„  d   QhRRRR/# r�   r   )r   s   "r   r   r   †   s   € ÷ Gñ G�cð GÐ.ñ Gr   c                óŽ   € \        V 4      ;'       d4    \        V \        4      '       * ;'       d    \        P                  ! V 4      # )zgTrue iff v is a real number (int/float, not bool, not NaN/Inf); TypeGuard so callers can do arithmetic.)r”   ra   rB   ÚmathÚisfiniter“   s   &r   Ú_is_finite_numr™   †   s/   € ä�1‹:×FÐFœj¨¬DÓ1Ô1×FÐF´d·m²mÀAÓ6FÐFr   r9   c               ó0   € V ^8„  d   QhRRRRRRRRRRR	R
/# )r   r:   rK   r<   Útailr6   r$   r9   r)   r   r{   r   )r   s   "r   r   r   ‹   sD   € ÷ _ñ _˜Dð _¨4ð _°tð _Èð _ÐS`ð _ð  mKñ _r   c          	     óR   € V '       g   V'       g   R# \        RW0W,           RVR7      # )zPNous snapshot with *tail* lines appended, or None when there is nothing to show.NÚnouszNous credits)r8   r9   )rO   )r:   r<   r›   r6   r9   s   &&&$$r   Ú_nous_snapshotrž   ‹   s    € çŸ7ÙÜ�V˜V¨g­nÀNÐY]Ô^Ð^r   c               ó   € V ^8„  d   QhRR/# ©r   r   r{   r   )r   s   "r   r   r   ’   s   € ÷ (ñ (Ð1Oñ (r   c                ó&  €  ^ RI Hp V e   \        V RR4      '       g   R# \        V RR4      p\        V RR4      p. p. pVe§   \        VRR4      p\        VRR4      p\        V4      '       d|   V^ 8”  du   \        V4      '       dd   Wv8:  d^   VP	                  \        R	\        R
\        RWg,
          V,          R,          4      4      \        V4       R\        V4       R2R7      4       VeJ   R FC  w  r‰\        W(R4      p
\        V
4      '       g   K$  VP	                  V	 R\        V
4       24       KE  	  Vel   \        VRR4      p\        V4      '       d%   V^ 8”  d   VP	                  R\        V4       24       \        VRR4      pV'       d   VP	                  RV 24       \        V RR4      RJ d   VP	                  \        4       \        YERV! V 4       2R.RVe   \        VRR4      R7      # RR7      #   \        \        3 d     R# i ; i)u³   NousPortalAccountInfo â†’ /usage snapshot: dollar magnitudes + renewal date + portal CTA, plus a ``% used``
gauge when the portal supplies ``monthly_credits``. Fail-open â†’ None.©Únous_portal_topup_urlNÚ	logged_inFÚpaid_service_access_infoÚsubscriptionÚmonthly_creditsÚcredits_remainingÚSubscriptionç        ç      Y@z of ú left©r%   r&   r*   r~   Úrollover_creditsz
Rollover: Úcurrent_period_endzRenews: Úpaid_service_accesszTop up: z(or run /topup)zportal-accountr9   )r6   r9   ))Úsubscription_credits_remainingúSubscription credits)Úpurchased_credits_remainingúTop-up credits)Útotal_usable_creditsúTotal usable)Úhermes_cli.nous_accountr£   Úgetattrr™   r‚   r#   r€   ÚminrŽ   Ú_DEPLETED_LINErž   ÚAttributeErrorÚ	TypeError)Úaccount_infor£   ÚaccessÚsubr:   r<   ÚcapÚsub_remainingÚattrr%   rQ   ÚrolloverÚ
period_ends   &            r   Úbuild_nous_credits_snapshotrÅ   ’   s  € ð%ÝAØÒ¤w¨|¸[È%×'PÒ'PÙÜ˜Ð'AÀ4ÓHˆÜ�l N°DÓ9ˆØ,.ˆØˆð Š?Ü˜#Ð0°$Ó7ˆCÜ# CÐ)<¸dÓCˆMÜ˜c×"Ò" s¨Q¤w´>À-×3PÒ3PÐUbÔUiØ—‘Ô1Ø(´s¸3ÄÀEÈCÕL_ÐcfÕKfÐinÕKnÓ@oÓ7pÜ& }Ó5Ð6°d¼8ÀC»=¸/ÈÐOô ô ð Òó }‘�ä ¨dÓ3�Ü! %×(Ô(Ø—N‘N e W¨B¬x¸«Ð.?Ð#@ÖAñ	 }ð
 Š?Ü˜sÐ$6¸Ó=ˆHÜ˜h×'Ò'¨H°q¬LØ—‘ ¬H°XÓ,>Ð+?Ð@ÔAÜ  Ð&:¸DÓAˆJßØ—‘ ¨*¨Ð6Ô7Ü�<Ð!6¸Ó=ÀÓFØ�N‰Nœ>Ô*Ü˜g°8Ñ<QÐR^Ó<_Ð;`Ð1aÐctÐ0uØ%5ÐZ]ÒZi¼GÀCÈÐQUÓ<Vôuð 	uØosôuð 	uøäœIÐ&ô Úðús$   ‚G; ¡C-G; ÄC G; Ç4G; Ç;HÈHc               ó   € V ^8„  d   QhRR/# rA   r   )r   s   "r   r   r   ½   s   € ÷ ñ ˜ñ r   c                 óØ   €  ^ RI Hp  V ! R4      ;'       g    / P                  R4      p\        V\        4      ;'       d    \        VP                  4       4      #   \         d     R# i ; i)zNCheap local auth-state check: a Nous access token is present. Fail-open False.)Úget_provider_auth_stater�   Úaccess_tokenF)Úhermes_cli.authrÈ   Úgetra   r$   rB   rW   Ú	Exception)rÈ   Útoks     r   Ú_nous_logged_inrÎ   ½   sY   € ðÝ;Ù& vÓ.×4Ð4°"×9Ñ9¸.ÓIˆÜ˜#œsÓ#×9Ð9¬¨S¯Y©Y«[Ó(9Ð9øÜô Úðús   ‚A —(A Á A ÁA)Á(A)c               ó   € V ^8„  d   QhRR/# )r   Útimeoutrc   r   )r   s   "r   r   r   Ç   s   € ÷ "ñ " 5ñ "r   c                óL  € ^ RI p^ RIHp ^ RIHp VP                  4       pV! ^R7      pVP                  VP                  VRR7      p VP                  V R7      VP                  RR	7       #   \         d    TP                  R
 4       h i ; i  TP                  RR	7       i ; i)aú  Wall-clock-bounded fresh portal account fetch (raises on any failure/timeout).

No ``with`` block on purpose: ``Executor.__exit__`` joins the worker via
``shutdown(wait=True)``, so a portal that accepts the connection but never
answers would hold the caller until the provider's own timeout instead of
``timeout``. The abandoned daemon worker runs on to its own network timeout
and never blocks the caller or process exit; its eventual exception is
drained so GC never logs "exception was never retrieved".N)Úget_nous_portal_account_info)ÚDaemonThreadPoolExecutor)Úmax_workersT)Úforce_fresh©rÐ   F)Úwaitc                ó"   € V P                  4       # r   )Ú	exception)Úfs   &r   Ú<lambda>Ú'_fetch_portal_account.<locals>.<lambda>Ú   s
   € ¨1¯;©;¬=r   )Úcontextvarsr·   rÒ   Útools.daemon_poolrÓ   Úcopy_contextÚsubmitÚrunÚresultÚshutdownÚBaseExceptionÚadd_done_callback)rÐ   rÝ   rÒ   rÓ   ÚcontextÚpoolÚfutures   &      r   Ú_fetch_portal_accountré   Ç   s–   € ó ÝDÝ:à×&Ñ&Ó(€GÙ#°Ô2€DØ�[‰[˜Ÿ™Ð&BÐPTˆ[ÓU€Fð"Ø�}‰} Wˆ}Ó-ð
 	�‰˜5ˆÕ!øô	 ô Ø× Ñ Ñ!8Ô9Øðûð 	�‰˜5ˆÕ!ús   Á	A- Á-BÂB ÂB#rÐ   ç      $@c               ó$   € V ^8„  d   QhRRRRRR/# )r   rx   rB   rÐ   rc   r   r|   r   )r   s   "r   r   r   à   s!   € ÷ ñ  Dð ¸5ð ÈIñ r   c                ó2  €  ^ RI Hp V! 4       pVe   \        \	        V4      V R7      # \        4       '       g   . #  \        \        V4      4      p\        W@R7      #   \         d    Rp L\i ; i  \         d    \        P                  RRR7       . u # i ; i)uI  Rendered Nous-credits /usage lines, or [] when there's nothing to show. Independent of any live agent
(logged-in gate, then a bounded portal fetch); shared by CLI ``_show_usage`` and the TUI ``session.usage`` RPC.
Fail-open: any hiccup or timeout â†’ []. HERMES_DEV_CREDITS_FIXTURE renders from the fixture instead of the portal.)Údev_fixture_credits_stateN©rx   u9   credits â–¸ /usage portal fetch/render failed (fail-open)T©Úexc_info)
Úagent.credits_trackerrí   rÌ   r‰   Ú_snapshot_from_credits_staterÎ   rÅ   ré   ÚloggerÚdebug)rx   rÐ   rí   Úfixturerz   s   $$   r   Únous_credits_linesrö   à   s™   € ðÝCÙ+Ó-ˆð ÒÜ)Ô*FÀwÓ*OÐZbÔcÐcÜ×ÒØˆ	ðÜ.Ô/DÀWÓ/MÓNˆÜ)¨(ÔFÐFøô ô ØŠðûô ô ä�‰ÐPÐ[_ˆÔ`ØŠ	ðús"   ‚A ¼A/ ÁA,Á+A,Á/$BÂBc               ó   € V ^8„  d   QhRR/# r    r   )r   s   "r   r   r   ö   s   € ÷ ñ Ð+Iñ r   c                ó�  €  V f   R# . p. p\        V RR4      p\        V RR4      p\        V RR4      p\        V4      '       dn   \        P                  ! V4      '       dR   TP	                  \        R\        R\        RVR,          4      4      V'       d   V'       d
   RV R	V R
2MRR7      4       VR3\        V RR4      R3\        V RR4      R33 F%  w  rgV'       g   K  VP	                  V RV 24       K'  	  \        V RR4      RJ d   VP	                  \        4       \        WR.RR7      #   \        \        3 d     R# i ; i)u½   Header-shaped CreditsState (dev fixture) â†’ /usage snapshot, same shape as the portal path. *_usd strings
are display-only; the % comes from CreditsState.used_fraction. Fail-open â†’ None.NÚused_fractionÚsubscription_usdÚsubscription_limit_usdr©   rª   r«   r�   ú of $r¬   r­   r²   Úpurchased_usdr´   Úremaining_usdr¶   z: $Úpaid_accessTFu,   (dev fixture â€” HERMES_DEV_CREDITS_FIXTURE)zdev-fixture)r6   )r¸   r”   r—   r˜   r‚   r#   r€   r¹   rº   rž   r»   r¼   )Ústater:   r<   ÚufÚsub_usdÚcap_usdrQ   r%   s   &       r   rò   rò   ö   sE  € ðØŠ=ÙØ,.ˆØˆÜ�U˜O¨TÓ2ˆÜ˜%Ð!3°TÓ:ˆÜ˜%Ð!9¸4Ó@ˆÜ�2�;Š;œ4Ÿ=š=¨×,Ò,Ø�N‰NÔ-Ø$´3°s¼CÀÀrÈEÅzÓ<RÓ3Sß;BÇw˜˜7˜) 5¨¨	°Ñ7ÐTXôô ð &Ð'=Ð>ÄÈÐP_ÐaeÓAfÐhxÐ@yÜ% e¨_¸dÓCÀ^ÐTóV‰LˆEç‰uØ—‘ % ¨¨E¨7Ð3Ö4ñVô �5˜-¨Ó.°%Ó7Ø�N‰Nœ>Ô*Ü˜gÐ1_Ð0`ÐivÔwÐwøÜœIÐ&ô Úðús*   ‚D0 ˆAD0 Á <D0 Â?D0 Ã!AD0 Ä0EÅEc                  ó^   € ] tR tRt$ RtR]R&   RtR]R&   RtR]R	&   RtR]R
&   Rt	R]R&   Rt
R# )ÚCreditsViewi  uÃ   Surface-agnostic ``/topup`` balance view: one portal fetch, consumed identically by every money surface.
Fail-open: not logged in / portal unreachable â†’ ``logged_in`` False, ``topup_url`` None.rB   r¤   r;   Úbalance_linesNr)   Úidentity_lineÚ	topup_urlFÚdepletedr   )r-   r.   r/   r0   Ú__doc__r1   r  r  r  r	  r2   r   r   r   r  r    s7   ‡ ñbð ƒOØ%'€M�?Ó'Ø#'€M�=Ó'Ø#€Iˆ}Ó#Ø€Hˆd×r   r  c               ó$   € V ^8„  d   QhRRRRRR/# )r   rx   rB   rÐ   rc   r   r  r   )r   s   "r   r   r     s!   € ÷ ñ  Dð ¸5ð ÈKñ r   c                óÜ  € \        RR7      p\        4       '       g   V#  \        V4      pTe   \        TRR4      '       g   T# ^ RIHp \        \        T4      T R	7       Uu. uF,  pTP                  4       P                  R4      '       d   K*  TNK.  	  pp\        TR
R4      3 Uu. uF  qw'       g   K  \        T4      NK  	  pp\        TRR4      p	T	'       d   TP                  RT	 24       \        R\        T4      T'       d   RRP!                  T4      ,           MRT! T4      \        TRR4      RJ R7      #   \         d    \        P                  RRR7       Tu # i ; iu upi u upi )uõ   /topup view: balance block + identity line + top-up URL. Reuses the /usage fetch + snapshot so numbers
match; the balance block drops the trailing top-up/hint lines (/topup has its own affordance).
Fail-open â†’ ``CreditsView(logged_in=False)``.F)r¤   u2   credits â–¸ /topup portal fetch failed (fail-open)Trï   Nr¤   r¢   rî   ÚemailÚorg_namezorg zTopping up as ú / r°   )r¤   r  r  r  r	  )zTop up:z(or run)r  rÎ   ré   rÌ   ró   rô   r¸   r·   r£   r‰   rÅ   ÚlstripÚ
startswithr$   r‚   rN   Újoin)
rx   rÐ   Únot_logged_inr+   r£   Úliner  r‘   Úwhor  s
   $$        r   Úbuild_credits_viewr    s[  € ô  ¨%Ô0€MÜ×ÒØÐðÜ'¨Ó0ˆð ‚œg g¨{¸E×BÒBØÐÝ=ô /Ô/JÈ7Ó/SÐ^fÕgóágˆDØ�{‰{‹}×'Ñ'Ð(>×?÷ 	ˆÙgð ð ô
 $ G¨W°dÓ;Ñ=Ó
CÑ=�aÃŒ6Œ3ˆqŽ6Ñ=€CÐ
CÜ�w 
¨DÓ1€HßØ�
‰
�T˜(˜Ð$Ô%ÜØ¤e¨MÓ&:ß>AÐ'¨%¯*©*°S«/Ö9ÀtÑWlÐmtÓWuÜ˜Ð"7¸Ó>À%ÐGôð øô ô Ü�‰ÐIÐTXˆÔYØÒðüòùò
 Ds)    D: Á"'E$ÂE$Â&	E)Â4E)Ä:$E!Å E!c               ó    € V ^8„  d   QhRRRR/# )r   Úbase_urlr$   r   ztuple[str, str, str]r   )r   s   "r   r   r   <  s   € ÷ sñ s #ð sÐ*>ñ sr   c                óâ   € T ;'       g    RP                  4       P                  R4      ;'       g    RpVP                  R4      pTRV9   d   RMR,           pVR,           VR	,           VR
,           3# )zÀCodex backend endpoints (usage, reset-credits list, consume). Mirrors the Codex CLI's PathStyle
split: ``/backend-api`` bases use the ChatGPT ``/wham/`` paths; everything else ``/api/codex/``.rS   Ú/z%https://chatgpt.com/backend-api/codexz/codexz/backend-apiz/whamz
/api/codexz/usagez/rate-limit-reset-creditsz!/rate-limit-reset-credits/consume)rW   ÚrstripÚremovesuffix)r  Ú
normalizedÚprefixs   &  r   Ú_codex_backend_urlsr  <  sm   € ð —.�.˜b×'Ñ'Ó)×0Ñ0°Ó5×`Ð`Ð9`€JØ×(Ñ(¨Ó2€JØ n¸
Ô&B™7ÈÕU€FØ�XÕ˜vÐ(CÕCÀVÐNqÕEqÐrÐrr   Úforce_refreshc               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r  r)   Úapi_keyr   rB   r   ztuple[str, str, Optional[str]]r   )r   s   "r   r   r   E  s2   € ÷ 2gñ 2gØð2gØ&3ð2gØGKð2gà#ñ2gr   c               ó  € \        T;'       g    R4      P                  4       pV'       d/   V'       g'   T\        T ;'       g    R4      P                  4       R3# V'       dµ    \        \        4       P                  R4      ;'       g    / P                  RR4      ;'       g    R4      P                  4       pWC8w  dZ   ^ RIHp V! R4      P                  VR7      pVf   \        R4      hVP                  \        VP                  ;'       g    T 4      R3#  R	R
/pV'       d   R
VR&   \        R/ VB pRp	 \        4       P                  R4      ;'       g    / p
\        V
P                  RR4      ;'       g    R4      P                  4       ;'       g    Rp	VR,          \        VP                  RR4      ;'       g    R4      P                  4       V	3#   \         d    Rp EL,i ; i  \         d    \        P                  RR
R7        Lui ; i  \         d    \        P                  RR
R7        Mi ; i^ RIHp T! R4      P                  4       pTf   \        R4      hTP                  \        TP                  ;'       g    T 4      R3# )uÚ   Codex quota credentials: explicit live-agent creds â†’ native runtime resolver (itself pool-aware) â†’ direct
pool select. Native OAuth stores device-code logins in the pool, so the singleton store alone is not enough.rS   NÚtokensrÉ   )Ú	load_poolúopenai-codex)Úapi_key_hintz;Could not refresh the Codex credential this session runs onÚrefresh_if_expiringTr   Ú
account_idu5   codex â–¸ /usage account_id read failed (best-effort)rï   r"  r  u@   codex â–¸ /usage runtime resolver returned no creds; trying poolz7No available openai-codex credential in credential poolr   )r$   rW   r   rË   r   Úagent.credential_poolr%  Útry_refresh_matchingÚRuntimeErrorÚruntime_api_keyr   Úruntime_base_urlr   ró   rô   Úselect)r  r"  r   Úexplicit_keyÚsingleton_keyr%  ÚentryÚresolve_kwargsÚcredsr)  r$  s   &&$        r   Ú _resolve_codex_usage_credentialsr5  E  sO  € ô
 �w—}�} "Ó%×+Ñ+Ó-€LßŸMØœS § ¨RÓ0×6Ñ6Ó8¸$Ð>Ð>ßð	ÜÔ!3Ó!5×!9Ñ!9¸(Ó!C×!IÐ!IÀr× NÑ NÈ~Ð_aÓ b× hÐ hÐfhÓi×oÑoÓqˆMð Ô(Ý7Ù˜nÓ-×BÑBÐP\ÐBÓ]ˆEØŠ}Ü"Ð#`ÓaÐaØ×(Ñ(Ô*DÀU×E[ÑE[×EgÐEgÐ_gÓ*hÐjnÐnÐnðhð 0°Ð6ˆßØ.2ˆN˜?Ñ+Ü1ÑC°NÑCˆØ$(ˆ
ð	aÜ'Ó)×-Ñ-¨hÓ7×=Ð=¸2ˆFÜ˜VŸZ™Z¨°bÓ9×?Ð?¸RÓ@×FÑFÓH×PÐPÈDˆJð �YÕ¤ U§Y¡Y¨z¸2Ó%>×%DÐ%DÀ"Ó!E×!KÑ!KÓ!MÈzÐYÐYøô9 ô 	Ø‹Mð	ûô2 ô 	aä�L‰LÐPÐ[_ˆLÖ`ð	aûô ô hÜ�‰ÐWÐbfˆÖgðhúõ 0Ù�nÓ%×,Ñ,Ó.€EØ‚}ÜÐTÓUÐUà× Ñ Ô"<¸U×=SÑ=S×=_Ð=_ÐW_Ó"`ÐbfÐfÐfsr   Á#$G ÂG Â"G ÄH Ä#H Ä6G' Å G' Å7G' ÆG' Æ%H Æ<H ÇG$Ç#G$Ç'"HÈ	H ÈHÈH È"H4È3H4c               ó    € V ^8„  d   QhRRRR/# )r   ÚpayloadÚdictr   rb   r   )r   s   "r   r   r   z  s   € ÷ +ñ + $ð +¨3ñ +r   c                ó’   € V P                  R 4      ;'       g    / P                  R4      p\        V4      '       d   \        V4      # ^ # )Úrate_limit_reset_creditsÚavailable_count)rË   r”   rb   )r7  r?   s   & r   Ú_codex_banked_resetsr<  z  s<   € Ø�;‰;Ð1Ó2×8Ð8°b×
=Ñ
=Ð>OÓ
P€CÜ˜s—|’|Œ3ˆs‹8Ð*¨Ð*r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Útokenr$   r)  r)   r   údict[str, str]r   )r   s   "r   r   r     s&   € ÷ jñ j˜#ð j¨=ð j¸^ñ jr   c                óR   € ^ RI Hp RRV  2RRRR/V! V 4      CV'       d   RV/C# / C# )	z`auth.json's ``account_id`` wins over the JWT claim; the JWT still supplies the residency header.)Úcodex_account_headersÚAuthorizationúBearer ÚAcceptúapplication/jsonú
User-Agentz	codex-clizChatGPT-Account-ID)Úagent.codex_headersrA  )r>  r)  rA  s   && r   Ú_codex_headersrH    sU   € å9Ø˜w u gÐ.°Ð:LÈlÐ\gð jÙ# EÓ*ðjßU_Ð0DÀjÐ/Qðjð jØegðjð jr   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   Úurlr$   Úheadersr?  rÐ   rc   r   r8  r   )r   s   "r   r   r   †  s(   € ÷ !ñ !�3ð ! ð !¸Uð !Àtñ !r   c               óð   € \         P                  ! VR 7      ;_uu_ 4       pVP                  WR7      pVP                  4        RRR4       XP	                  4       ;'       g    / #   + '       g   i     L*; i)rÖ   ©rK  N)ÚhttpxÚClientrË   Úraise_for_statusÚjson)rJ  rK  rÐ   ÚclientÚresponses   &&$  r   Ú	_get_jsonrT  †  sS   € Ü	�Š˜g×	&Õ	&¨&Ø—:‘:˜c�:Ó3ˆØ×!Ñ!Ô#÷ 
'ð �=‰=‹?× Ð ˜bÐ ÷ 
'×	&ús    #A%Á%A5	ÚfractionÚmodel_scopedc               ó4   € V ^8„  d   QhRRRRRRRRRR	R
RRR/# )r   r6   r8  Úmappingútuple[tuple[str, str], ...]Úused_keyr$   Ú	reset_keyrU  rB   rV  zfrozenset[str] | set[str]r   zlist[AccountUsageWindow]r   )r   s   "r   r   r   �  sF   € ÷ ñ ØðØ6ðØBEðØRUðØdhðà+ðð ñr   c               óD  € . pV F—  w  rxV P                  V4      ;'       g    / p	V	P                  V4      p
V
f   K7  \        V
4      p
V'       d   V
^8:  d
   V
^d,          p
TP                  \        YŠ\	        V	P                  V4      4      Wu9   d   RMRR7      4       K™  	  V# )z®Build windows from ``source[key][used_key]``; ``fraction`` scales values <= 1 to percent.
``model_scoped`` keys build windows that cap only a model family, never the account.Úmodelr+   )r%   r&   r(   r,   )rË   rc   r‚   r#   rj   )r6   rX  rZ  r[  rU  rV  r:   Úkeyr%   r†   rˆ   s   &&&&$$     r   Ú_usage_windowsr_  �  s“   € ð )+€GÛ‰
ˆØ—‘˜C“×&Ð& BˆØ�z‰z˜(Ó#ˆØŠ<ÙÜ�T‹{ˆß˜ œ	Ø�C�KˆDØ�‰Ô)Ø´Y¸v¿z¹zÈ)Ó?TÓ5UØ Ô0‘'°iô
ö 	ñ ð €Nr   iPF  ÚSessioni€:	 ÚWeeklyc               ó    € V ^8„  d   QhRRRR/# )r   Ú
rate_limitr8  r   rY  r   )r   s   "r   r   r   ¨  s   € ÷ ñ  Tð Ð.Iñ r   c                óJ  € . p\          F�  w  r#V P                  V4      ;'       g    / p\        V\        4      '       d   VP                  R4      MRp\	        V4      '       d    \
        P                  \        V4      V4      MTpVP                  W&34       K�  	  \        V4      # )aµ  Label Codex windows by their published duration, not response position (#65387).

The usage API keys windows ``primary_window``/``secondary_window`` by position; when only the
weekly limit is returned it occupies ``primary_window`` and the positional mapping mislabeled it
``Session``. Windows whose ``limit_window_seconds`` is missing or unrecognized keep the legacy
positional label so duration-less payloads render exactly as before.
Úlimit_window_secondsN)	Ú_CODEX_WINDOW_POSITIONAL_LABELSrË   ra   r8  r”   Ú_CODEX_WINDOW_LABELS_BY_SECONDSrb   r‚   rN   )rc  Úlabelsr^  Úfallbackr†   Úsecondsr%   s   &      r   Ú_codex_window_labelsrk  ¨  sˆ   € ð €Fß8‰ˆØ—‘ Ó$×*Ð*¨ˆÜ8BÀ6Ì4×8PÒ8P�&—*‘*Ð3Ô4ÐVZˆÜOVÐW^×O_ÒO_Ô/×3Ñ3´C¸³LÀ(ÔKÐemˆØ�‰�s�lÖ#ñ	 9ô
 �‹=Ðr   c               ó    € V ^8„  d   QhRRRR/# )r   Úcountrb   r   r$   r   )r   s   "r   r   r   ¹  s   € ÷ %ñ %�3ð %˜3ñ %r   c                ó   € V ^8w  d   R# R# )é   ÚsrS   r   )rm  s   &r   Ú_pluralrq  ¹  s   € Ø˜1”*ˆ3Ð$ "Ð$r   c               ó$   € V ^8„  d   QhRRRRRR/# ©r   r  r)   r"  r   r{   r   )r   s   "r   r   r   ½  s)   € ÷ Oñ OØðOØ-:ðOà#ñOr   c                ó   € \        WR R7      # )F©Ú	read_only©Ú_fetch_codex_account_usage_impl©r  r"  s   &&r   Ú_fetch_codex_account_usagerz  ½  s   € ô +¨8ÈÔNÐNr   c               ó$   € V ^8„  d   QhRRRRRR/# rs  r   )r   s   "r   r   r   Ã  s)   € ÷ Nñ NØðNØ-:ðNà#ñNr   c                ó   € \        WRR7      # )u?  Picker-cache variant: a 401 is reported, never repaired. The live-agent path
(``_fetch_codex_account_usage``) retries via a forced credential refresh because the
session it serves owns that credential; a per-credential read of the pool must NOT
rotate, refresh or mutate anything â€” it just marks that account unknown.Tru  rw  ry  s   &&r   Ú$_fetch_codex_account_usage_read_onlyr}  Ã  s   € ô +¨8ÈÔMÐMr   rv  c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r  r)   r"  rv  rB   r   r{   r   )r   s   "r   r   r   Í  s2   € ÷  Lñ  LØð LØ-:ð LØQUð Là#ñ Lr   c               ó2  € \        W4      w  r4p \        \        V4      ^ ,          \        W54      RR7      pVP                  R4      ;'       g    / p\        V\        V4      RR	4      p	. p
\        V4      pV^ 8”  d"   V
P                  R
V R\        V4       R24       VP                  R4      ;'       g    / VP                  R4      ;'       g    / P                  R4      rÜVP                  R4      '       d1   \        V4      '       d    V
P                  R\        V4      R 24       M?VP                  R4      '       d)   VP                  R4      '       d   V
P                  R4       \!        RRWš\#        VP                  R4      4      \%        V4      VR7      #   \        P
                   dh   pTP                  P                  R8w  d   h T'       d   h \        YRR7      w  r4p\        \        T4      ^ ,          \        Y54      RR7      p Rp?ELÚRp?ii ; i)r   ç      .@rÖ   é‘  T©r   Nrc  r&   r(   z	You have ú resetz& banked - use /usage reset to activateÚcreditsÚbalanceÚhas_creditsúCredits balance: $ú.2fÚ	unlimitedzCredits balance: unlimitedr&  Ú	usage_apiÚ	plan_type)r9   r>   r?   )r5  rT  r  rH  rN  ÚHTTPStatusErrorrS  Ústatus_coderË   r_  rk  r<  r‚   rq  r”   rc   rO   rZ   Ú_codex_snapshot_identity)r  r"  rv  r>  Úresolved_base_urlr)  r7  Úexcrc  r:   r<   rm  r„  r…  s   &&$           r   rx  rx  Í  sÔ  € ô ,LÈHÓ+^Ñ(€E˜jð
ÜÜÐ 1Ó2°1Õ5´~ÀeÓ7XÐbfô
ˆð —‘˜\Ó*×0Ð0¨b€JÜ˜ZÔ)=¸jÓ)IÈ>Ð[eÓf€GØ€GÜ  Ó)€EØˆq„yØ�‰˜ 5 '¨´¸³Ð/?Ð?eÐfÔgØ—{‘{ 9Ó-×3Ð3°°g·k±kÀ)Ó6L×6RÐ6RÐPR×5WÑ5WÐXaÓ5bˆWØ‡{�{�=×!Ò!¤g¨g×&6Ò&6Ø�‰Ð+¬E°'«N¸3Ð+?Ð@ÕAØ	�‰�]×	#Ò	#¨¯©°K×(@Ò(@Ø�‰Ð3Ô4Ü�^ [°'ÔIYÐZa×ZeÑZeÐfqÓZrÓIsÜ6°uÓ=À7ôLð Løô/ × Ñ ô 
Ø�<‰<×#Ñ# sÔ*ØßàÜ/OØ¨Tô0
Ñ,ˆ *ô ÜÐ 1Ó2°1Õ5´~ÀeÓ7XÐbfô
Žûð
ús   �'F ÆHÆ/#HÇ8HÈHc               ó    € V ^8„  d   QhRRRR/# )r   r>  r$   r   r)   r   )r   s   "r   r   r   ð  s   € ÷ Iñ I Cð I¨Mñ Ir   c                ó^   € ^ RI Hp V! V 4      pV'       d   RV^ ,           RV^,           2# R# )zéTrusted per-account identity of a Codex credential: the decoded JWT principal
(``chatgpt_account_id`` + ``sub``), never the token itself. Two credentials for one
workspace member share it, so pool accounts are counted once, honestly.)Ú_codex_principal_identityzcodex:Ú:N)r*  r“  )r>  r“  Ú	principals   &  r   rŽ  rŽ  ð  s4   € õ @á)¨%Ó0€Iß5>ˆV�I˜a•L�>  9¨Q¥< .Ð1ÐHÀDÐHr   c                  ób   € ] tR tRt$ RtR]R&   R]R&   ^ tR]R&   ^ tR]R&   ]R	 R
 l4       t	Rt
R# )ÚCodexResetRedeemResultiú  z>Outcome of a `/usage reset` attempt against the Codex backend.r$   ÚstatusÚmessagerb   r;  Úwindows_resetc               ó   € V ^8„  d   QhRR/# rA   r   )r   s   "r   r   Ú#CodexResetRedeemResult.__annotate__  s   € ÷ &ñ &˜$ñ &r   c                	ó    € V P                   R 8H  # )Úreset)r˜  rE   s   &r   ÚredeemedÚCodexResetRedeemResult.redeemed  s   € à�{‰{˜gÑ%Ð%r   r   N)r-   r.   r/   r0   r
  r1   r;  rš  rI   rŸ  r2   r   r   r   r—  r—  ú  s5   ‡ áHàƒKØƒLØ€O�SÓØ€M�3Óàô&ó ô&r   r—  r«   c               ó    € V ^8„  d   QhRRRR/# )r   r™  r$   r   r—  r   )r   s   "r   r   r     s   € ÷ Iñ I˜#ð IÐ"8ñ Ir   c                ó   € \        R V R7      # )Úunavailable©r˜  r™  )r—  )r™  s   &r   Ú_unavailabler¥    s   € Ü!¨ÀÔHÐHr   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r7  r8  rG   rb   ÚforcerB   r   z Optional[CodexResetRedeemResult]r   )r   s   "r   r   r     s)   € ÷ ñ  ð °ð ¸Tð ÐFfñ r   c                ó–  a€ V^ 8:  d   \        RRR7      # V P                  R4      ;'       g    / oV3R lR 4        Uu. uF!  p\        V4      '       g   K  \        V4      NK#  	  ppV'       d   \	        R.VO5!  MRpV'       g   Ve   V\
        8¼  d   R# Ve   RVR	 R
2MRp\        RVRV RV R\        V4       R2R7      # u upi )zhRefuse a redemption that would be wasted (no banked credits, or no window fully used and not ``force``).Úno_credits_bankedu>   No banked reset credits on this account â€” nothing to redeem.r¤  rc  c              3  óx   <"  € T F/  pSP                  V4      ;'       g    / P                  R 4      x € K1  	  R# 5i)r&   N)rË   )Ú.0Úkrc  s   & €r   Ú	<genexpr>Ú%_codex_reset_guard.<locals>.<genexpr>  s>   øé € ð  $Cñ  \BÐVW Z§^¡^°AÓ%6×%<Ð%<¸"×$AÑ$AÀ.×$QÐ$Qó  \Bùs   ƒ: :rª   Nzyour busiest window is only z.0fz% usedz6your current usage could not be confirmed as exhaustedÚnot_exhaustedu   âš ï¸� Not redeeming: zl. A banked reset restores your FULL 5h + weekly limits, so spending it now would waste most of it. You have rƒ  z5 banked. Use `/usage reset --force` to redeem anyway.)r˜  r;  r™  )Úprimary_windowÚsecondary_window)r—  rË   r”   rc   r€   Ú_CODEX_WINDOW_EXHAUSTED_PERCENTrq  )r7  rG   r§  ÚuÚ	used_pctsÚ
worst_usedÚ
usage_noterc  s   &&&    @r   Ú_codex_reset_guardr·    sþ   ø€ à�A„~Ü%Ð-@ð  KKô  Lð  	LØ—‘˜\Ó*×0Ð0¨b€Jô $Cñ  \Bô  $Có  ñ  $C˜aÜ˜A—Jô ”�q–ñ  $C€Ið  ç:C¤# cÐ"6¨IÔ"6È€Jß�Ò'¨JÔ:YÔ,YÙØKUÒKaÐ0°¸CÐ0@ÀÑGØOð ä!Ø°	Ø)¨*¨ð 69Ø9B¸À6Ì'ÐR[ÓJ\ÐI]ð ^@ðAôð ùò s   ¿CÁCc               ó$   € V ^8„  d   QhRRRRRR/# )r   Úbodyr8  rG   rb   r   r—  r   )r   s   "r   r   r   %  s'   € ÷ tñ t˜tð t°ð tÐ8Nñ tr   c           	     ó  € \        V P                  RR4      ;'       g    R4      P                  4       P                  4       p\	        ^ V^,
          4      pRRV R\        V4       R2V3RRV3R	RR
RV3/pW$9  d   \        RV : 24      # ^ pVR8X  d>    ^ RIHp V! 4        V P                  R4      p\        V4      '       d   \        V4      M^ pWB,          w  r‰\        W(W•R7      #   \         d    \        P                  RRR7        Lji ; i)z^Map the consume response ``code`` to a result (``reset`` also lifts persisted pool cooldowns).ÚcoderS   rž  u:   âœ… Reset redeemed â€” your usage limits have been reset. z banked resetz remaining.Únothing_to_resetu\   Backend reports nothing to reset â€” your limits aren't exhausted. The credit was NOT spent.Ú	no_creditÚalready_redeemeduI   This redemption was already processed â€” no additional credit was spent.z,Unexpected response from the Codex backend: )Ú clear_codex_pool_quota_cooldownsz;Failed to clear Codex pool cooldowns after reset redemptionTrï   rš  )r˜  r™  r;  rš  )z:Backend reports no available reset credit on this account.r   )r$   rË   rW   Úlowerr€   rq  r¥  rÊ   r¿  rÌ   ró   rô   r”   rb   r—  )
r¹  rG   r»  Ú	remainingÚoutcomesrš  r¿  r?   r™  rm  s
   &&        r   Ú_codex_reset_outcomerÃ  %  s0  € äˆt�x‰x˜ Ó#×)Ð) rÓ*×0Ñ0Ó2×8Ñ8Ó:€DÜ�A�y 1•}Ó%€IàÐNÈyÈkÐYfÔgnÐoxÓgyÐfzð  {Fð  GØðàÐ{ð  ~Gð  HØÐVØÐhÐjsÐtð,€Hð ÔÜÐJÈ4É(ÐSÓTÐTØ€MØˆw„ð	gÝHÙ,Ô.ð �h‰h�Ó'ˆÜ$+¨C§L¢Lœ˜Cœ°aˆØ•^�N€GÜ!¨ÐPUÔsÐsøô ô 	gÜ�L‰LÐVÐaeˆLÖfð	gús   ÂC' Ã'"DÄDr  r"  r§  c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r  r)   r"  r§  rB   r   r—  r   )r   s   "r   r   r   A  s,   € ÷ 31ñ 31Øð31Ø0=ð31ØMQð31àñ31r   c           	     ó®  € ^ RI p \        W4      w  rEp\	        TP                  4       4      p \        ^4       Fñ  p\        T4      w  ršp\        YF4      p \        P                  ! RR7      ;_uu_ 4       pTP                  YœR7      pTP                  4        TP                  4       ;'       g    / p\        T4      p\        TTT4      pTe   TuuRRR4       u # TP!                  T/ TCRR/CRT/R	7      pTP                  4        TP                  4       ;'       g    / pRRR4        M	   \)        XX4      #   \         d    \        R4      u # i ; i  + '       g   i     L=; i  \        P"                   dS   pTP$                  P&                  R
8w  g   T^ 8”  d   h  \        YRR7      w  rEp Rp?EK�    \         d    TRhi ; iRp?ii ; i  \        P"                   dK   pTP$                  P&                  pTR9   d   \        RT R24      u Rp?# \        RT R24      u Rp?# Rp?i\         d   p\        RT 24      u Rp?# Rp?ii ; i)u‰  Redeem one banked Codex rate-limit reset credit (`/usage reset`), mirroring the Codex CLI picker: GET usage â†’
guard (a reset restores the WHOLE 5h + weekly allowance, and the backend's own ``nothing_to_reset`` guard is
less clear) â†’ POST consume with a fresh UUID ``redeem_request_id`` and no ``credit_id`` (the backend picks the
next credit). Never raises: every failure returns a result.NzWNo Codex credentials available. Run `hermes auth` to sign in with your ChatGPT account.r€  rÖ   rM  úContent-TyperE  Úredeem_request_id)rK  rQ  r�  Tr‚  z)Codex backend rejected the request (HTTP ur   ). Reset credits require ChatGPT-account (OAuth) auth â€” run `hermes auth` and sign in with your ChatGPT account.zCodex backend error (HTTP u   ) â€” try again shortly.z#Could not reach the Codex backend: )r�  i“  )Úuuidr5  rÌ   r¥  r$   Úuuid4Úranger  rH  rN  rO  rË   rP  rQ  r<  r·  ÚpostrŒ  rS  r�  rÃ  )r  r"  r§  rÈ  r>  r�  r)  rÇ  ÚattemptÚ	usage_urlÚ_credits_urlÚconsume_urlrK  rR  Ú
usage_respr7  rG   ÚrefusedÚconsume_respr¹  r�  r»  s   $$$                   r   Úredeem_codex_reset_creditrÓ  A  sN  € ó ðwÜ/OÐPXÓ/bÑ,ˆ *ô ˜DŸJ™J›LÓ)Ðð%IÜ˜Q–xˆGÜ3FÐGXÓ3YÑ0ˆI [Ü$ UÓ7ˆGð(Ü—\’\¨$×/Õ/°6Ø!'§¡¨I Ó!G�JØ×/Ñ/Ô1Ø(Ÿo™oÓ/×5Ð5°2�GÜ 4°WÓ =�IÜ0°¸)ÀUÓK�GØÒ*Ø&÷ 0Ô/ð $*§;¡;Ø#Ð-\°Ð-\¸ÐI[Ñ-\Ø1Ð3DÐEð $/ó $�Lð !×1Ñ1Ô3Ø'×,Ñ,Ó.×4Ð4°"�D÷ 0ñ ò%  ôJ    iÓ0Ð0øôS ô wÜÐuÓvÒvðwú÷ 0×/ûô ×(Ñ(ô 	(Ø—<‘<×+Ñ+¨sÔ2°gÀ´kØð(Ü;[Ø ¸ô<Ñ8�E¯jøô !ô (à 4Ð'ð(þð	(ûô × Ñ ô YØ�|‰|×'Ñ'ˆØ�:ÔÜÐ"KÈDÈ6ð Rlð !ló mõ mäÐ8¸¸Ð>VÐWÓXÕXûÜô IÜÐAÀ#ÀÐGÓHÕHûðIúsÆ   †D; ®'G ÁE*Á5AEÃ
E*ÃG ÃAEÄEÄ"E*Ä*G Ä,G Ä;EÅEÅE'Å"E*Å*GÅ?#GÆ#F:Æ3G Æ:G	Ç	GÇGÇG ÇIÇ)+H.ÈIÈH.È(IÈ.IÈ;IÈ<IÉ	IÉIc               ó$   € V ^8„  d   QhRRRRRR/# rs  r   )r   s   "r   r   r   w  s)   € ÷ Gñ GØðGØ-:ðGà#ñGr   c           
     óÈ  € \        T;'       g    R 4      P                  4       pT;'       g#    \        4       ;'       g    R P                  4       pV'       g   R# \        V4      '       g   \	        RR. . RR7      # RRV 2RR	R
R	RRRR/p\        RVRR7      p\        VR"RRRRR0R7      p. pVP                  R4      ;'       g    / pVP                  R4      VP                  R4      r©VP                  R4      '       dX   \        V	4      '       dG   \        V
4      '       d6   TP                  RV	R RV
R RVP                  R 4      ;'       g    R! 24       \	        RRWg4      # )#rS   NÚ	anthropicÚoauth_usage_apizMAnthropic account limits are only available for OAuth-backed Claude accounts.)r=   rB  rC  rD  rE  rÆ  zanthropic-betazoauth-2025-04-20rF  zclaude-code/2.1.0z)https://api.anthropic.com/api/oauth/usager€  rÖ   Úseven_day_opusÚseven_day_sonnetÚutilizationÚ	resets_atT)rU  rV  Úextra_usageÚused_creditsÚmonthly_limitÚ
is_enabledzExtra usage: rˆ  r  rU   ÚcurrencyÚUSD))Ú	five_hourzCurrent session)Ú	seven_dayzCurrent week)rØ  z	Opus week)rÙ  zSonnet week)
r$   rW   r   r   rO   rT  r_  rË   r”   r‚   )r  r"  Úexplicitr>  rK  r7  r:   r<   ÚextrarÝ  rÞ  s   &&         r   Ú_fetch_anthropic_account_usageræ  w  sm  € ô �7—=�=˜bÓ!×'Ñ'Ó)€HØ×AÐAÔ0Ó2×8Ð8°b×?Ñ?ÓA€EßÙÜ˜5×!Ò!Ü˜Ð&7¸¸RØ,{ô}ð 	}à '¨%¨Ð 1°8Ð=OÐQ_ÐasØÐ!3°\ÐCVðX€GäÐCÀWÐVZÔ[€GÜØð 7Ø8EÀ{Ð]aØ&Ð(:Ð;ô€Gð
 €GØ�K‰K˜Ó&×,Ð,¨"€EØ"'§)¡)¨NÓ";¸U¿Y¹YÀÓ=W�-Ø‡y�y�×Ò¤7¨<×#8Ò#8¼WÀ]×=SÒ=SØ�‰˜ |°CÐ&8¸¸MÈ#Ð;NÈaÐPU×PYÑPYÐZdÓPe×PnÐPnÐinÐOoÐpÔqÜ�[Ð"3°WÓFÐFr   c               ó$   € V ^8„  d   QhRRRRRR/# rs  r   )r   s   "r   r   r   •  s'   € ÷ "Dñ "D¨mð "DÀmð "DÐXvñ "Dr   c           	     óh  aaa€ \        R WR7      p\        VP                  RR4      ;'       g    R4      P                  4       pV'       g   R# \        VP                  RR4      ;'       g    R4      P	                  R4      oRRV 2R	R
/o\
        P                  ! RR7      ;_uu_ 4       oR VVV3R llpV! R4      p V! R4      pRRR4       \        XP                  R4      ;'       g    R4      \        VP                  R4      ;'       g    R4      ,
          pR\        RV4      R 2.p. p	XP                  R4      VP                  R4      VP                  R4      rËp
\        VP                  R4      ;'       g    R4      P                  4       p\        V
4      '       d·   \        V
4      ^ 8”  d§   \        V4      '       d–   ^ \        V4      u;8:  d   \        V
4      8:  du   M Mq\        V
4      \        V4      rþRVR RVR R2.V'       d   RV 2.M. OpV	P                  \        RWï,
          V,          ^d,          RP                  V4      R 7      4       \        V4      '       d•   R!\        V4      R R"2.pR% F]  w  ppVP                  V4      p\        V4      '       g   K*  \        V4      ^ 8”  g   K<  VP                  R\        V4      R R#V 24       K_  	  VP                  RP                  V4      4       \        R R$W˜4      #   \         d    / p ELUi ; i  + '       g   i     ELb; i)&Ú
openrouter)Ú	requestedÚexplicit_base_urlÚexplicit_api_keyr"  rS   Nr  r  rB  rC  rD  rE  rê   rÖ   c               ó    € V ^8„  d   QhRRRR/# )r   Úpathr$   r   r8  r   )r   s   "r   r   Ú5_fetch_openrouter_account_usage.<locals>.__annotate__�  s   € ÷ 	9ñ 	9˜ð 	9 ñ 	9r   c                óº   <€ SP                  S R V  2SR7      pVP                  4        VP                  4       ;'       g    / P                  R4      ;'       g    / # )r  rM  Údata)rË   rP  rQ  )rî  ÚresprR  rK  r  s   & €€€r   Ú_dataÚ._fetch_openrouter_account_usage.<locals>._data�  sS   ø€ Ø—:‘:  ¨A¨d¨VÐ4¸g�:ÓFˆDØ×!Ñ!Ô#Ø—I‘I“K×%Ð% 2×*Ñ*¨6Ó2×8Ð8°bÐ8r   r„  r^  Útotal_creditsrª   Útotal_usager‡  rˆ  ÚlimitÚlimit_remainingÚusageÚlimit_resetr�   rü   z
 remainingzresets zAPI key quotar   r­   zAPI key usage: $z totalrU   Úcredits_api))Úusage_dailyÚtoday)Úusage_weeklyz	this week)Úusage_monthlyz
this month)r   r$   rË   rW   r  rN  rO  rÌ   rc   r€   r”   r‚   r#   r  rO   )r  r"  Úruntimer>  ró  r„  Úkey_datar…  r<   r:   r÷  rø  rù  rú  Úlimit_valueÚremaining_valueÚdetail_partsÚusage_partsr^  r%   rQ   rR  rK  r  s   &&                   @@@r   Ú_fetch_openrouter_account_usager  •  sÛ  ú€ Ü&°ÐQYÔt€GÜ�—‘˜I rÓ*×0Ð0¨bÓ1×7Ñ7Ó9€EßÙÜ�W—[‘[ ¨RÓ0×6Ð6°BÓ7×>Ñ>¸sÓC€JØ '¨%¨Ð 1°8Ð=OÐP€GÜ	�Š˜d×	#Õ	# v÷	9ò 	9ñ ˜	Ó"ˆð	Ù˜U“|ˆH÷ 
$ô �G—K‘K Ó0×7Ð7°CÓ8¼5ÀÇÁÈ]ÓA[×AbÐAbÐ_bÓ;cÕc€GØ#¤C¨¨WÓ$5°cÐ#:Ð;Ð<€GØ(*€GØ$,§L¡L°Ó$9¸8¿<¹<ÐHYÓ;ZÐ\d×\hÑ\hÐipÓ\q˜E€EÜ�h—l‘l =Ó1×7Ð7°RÓ8×>Ñ>Ó@€KÜˆu‡~‚~œ% ›,¨Ô*¬w°×/GÒ/GÈAÔQVÐWfÓQgÖLwÔkpÐqvÓkw×LwÜ',¨U£|´U¸?Ó5K�_Ø˜O¨CÐ0°°kÀ#Ð5FÀjÐQð  H×r}ÐX_Ð`kÐ_lÐVmÑUnð  DFð  HˆØ�‰Ô)°ÐP[ÕPmÐq|ÕO|ð  ADõ  ODØ18·±¸lÓ1KôMô 	Näˆu‡~‚~Ø)¬%°«,°sÐ);¸6ÐBÐCˆÛt‰JˆC�Ø—L‘L Ó%ˆEÜ�u�~Œ~¤%¨£,°Ö"2Ø×"Ñ" Q¤u¨U£|°CÐ&8¸¸%¸Ð#AÖBñ uð 	�‰�w—|‘| KÓ0Ô1Ü�\ =°'ÓCÐCøô' ô 	Ø‹Hð	ú÷ 
$×	#Ð	#ús*   Â*L Â?LÌLÌL ÌLÌL Ì L1	r&  rÖ  ré  zSdict[str, Callable[[Optional[str], Optional[str]], Optional[AccountUsageSnapshot]]]Ú_USAGE_FETCHERSÚ_READ_ONLY_USAGE_FETCHERSc               ó$   € V ^8„  d   QhRRRRRR/# rs  r   )r   s   "r   r   r   Í  s"   € ÷ 8ñ 8¨}ð 8À}ð 8ÐYwñ 8r   c                óÔ   a aa€ ^ RI Hp ^ RIHp \	        S 4      P
                  VP
                  J d   R# V! VVV 3R l\        RR7      pVP                  '       d   R# VP                  # )uº   Run the profile hook under the shared deadline; past it â†’ None. Exceptions re-raise in the
caller so ``fetch_account_usage`` fails open without a worker-thread traceback on ``/usage``.)Úrun_bounded_sync)ÚProviderProfileNc                 ó*   <€ SP                  SS R 7      # )ry  )Úfetch_account_usage)r"  r  Úprofiles   €€€r   rÛ   Ú)_call_plugin_usage_hook.<locals>.<lambda>Ö  s   ø€ �×+Ñ+°XÀwÐ+ÔOr   zplugin-account-usage)r%   )	Úagent.deadliner  Úproviders.baser  Útyper  ÚPLUGIN_USAGE_HOOK_DEADLINE_SÚ	timed_outrQ   )r  r  r"  r  r  Úboundeds   fff   r   Ú_call_plugin_usage_hookr  Í  sV   ú€ õ 0Ý.äˆGƒ}×(Ñ(¨O×,OÑ,OÓOÙÙÞOÜ$Ð,BôD€Gð ×$×$Ð$ˆ4Ð7¨'¯-©-Ð7r   Úidentity_idc               ó0   € V ^8„  d   QhRRRRRRRRRRRR	/# )
r   r5   r)   r  r"  rv  rB   r  r   r{   r   )r   s   "r   r   r   Û  s>   € ÷ ñ ØðØ*7ðØIVðàðà*7ðð $ñr   c               ó´  € ^ RI Hp \        T ;'       g    R4      P                  4       P	                  4       pV'       d   \
        P                  V4      MRpVf   \        P                  V4      p^ RIpVP                  4       p	 V'       d
   V! W4      p
M%^ RI
Hp V! V4      pV'       d   \        WÁV4      MRp
 V! W
WAV	R7       V
#   \         d     R# i ; i)r   )Úremember_account_usagerS   N)Úget_provider_profile)r  r  Ústarted_monotonic)Úagent.account_usage_cacher  r$   rW   rÀ  r  rË   r  ÚtimeÚ	monotonicÚ	providersr  r  rÌ   )r5   r  r"  rv  r  r  ÚslugÚfetcherÚ_timeÚstartedrz   r  r  s   &$$$$        r   r  r  Û  sÀ   € õ Aäˆx�~ˆ~˜2Ó×$Ñ$Ó&×,Ñ,Ó.€D÷ 6?Ô'×+Ñ+¨DÔ1ÀD€GØ‚Ü!×%Ñ% dÓ+ˆÛà�o‰oÓ€Gð	ßÙ˜xÓ1‰Hå6á*¨4Ó0ˆGßNUÔ.¨wÀ'ÔJÐ[_‰Hñ ˜8¸;Ø-4õ6à€Oøô ô Úðús$   ÂC ÂC ÂC Â,C ÃCÃC))r°  r`  )r±  ra  )NN)RÚ__conditional_annotations__Ú
__future__r   Úloggingr—   Údataclassesr   r   r   r   Útypingr   r	   r
   r   rN  Úagent.anthropic_credentialsr   r   rÊ   r   r   r   Úhermes_cli.auth_codexr   Úhermes_cli.runtime_providerr   Úhermes_timer   r   Ú	getLoggerr-   ró   rº   r    r#   r4   rO   rZ   rj   rw   r‰   rŽ   r”   r™   rž   rÅ   rÎ   ré   rö   rò   r  r  r  r5  r<  rH  rT  Ú	frozensetr_  rg  rf  rk  rq  rz  r}  rx  rŽ  r—  r²  r¥  r·  rÃ  rÓ  ræ  r  r  r1   r  r  r  r  )r&  s   @r   Ú<module>r1     sŒ  øðß "Ð "ã Û Ý !ß ,Ñ ,ß 9Ó 9ã ç Pß \Ñ \Ý <Ý @Ý %çÝ à	×	Ò	˜8Ó	$€à@€õñ �$Ô÷ð ó ðñ �$Ô÷Rð Ró ðRõ,OõTõ
õXðÐ]b÷ õ.õ'õGð
_Ðcg÷ _õ(õVõ"ð2¨5ð À4÷ õ,ñ6 �$Ô÷ð ó ðð¨5ð À4÷ õ>sð2gØNS÷2gõj+õ
jõ!ðØkpðá.7«k÷ð. $)¨)°V¸XÐ"FÐ Ø"aÐ õõ"%÷O÷Nñ LØX]÷ Lð  LõFIñ �$Ô÷
&ð 
&ó ð
&ð #(Ð õIõõ(tð831Ø!%ð31Ø@Dð31ØTY÷31÷lGõ<"DðL Ð.°Ð=[ØÐ1ðh€Ðdó ð Ð8ðrÐ Ðnó ð  $Ð õ8ðØ:>ðØY]ðàðà:>÷ñ r   