+
    „WÝjƒ[  ã                  óØ  a € 0 t $ R t^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
t
^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RIHtHt ^ RIHt ]P4                  ! ]4      tRtRtRYtRZt ^t!^t"R	 R
 lt#R R lt$R R lt%Rt&Rs'R](R&   R R lt)R R lt*R R lt+RRRR/t,R R lt-RRR R!/R" R# llt./ t/R$](R%&   ^xt0R&t1R't2R(t3^t4R)t5R*t6R+t7/ t8R,](R-&   / t9R.](R/&   ]Pt                  ! 4       t;R0 R1 lt<R2t=R3t>]?! 0 R[m4      t@R4 R5 ltAR6 R7 ltBR8 R9 ltCR: R; ltDR< R= ltER> R? ltFR@ RA ltGRBtHRC RD ltIRE RF ltJRG RH ltKRIRJ/RK RL lltLRM RN ltMRO RP ltNRQ RR ltORSRTRURV/RW RX lltPR# )\z™GitHub Copilot authentication utilities (credential order matches the Copilot CLI:
COPILOT_GITHUB_TOKEN, GH_TOKEN, GITHUB_TOKEN, then ``gh auth token``).)ÚannotationsN)ÚPath)ÚOptional)Ú
IS_WINDOWSÚwindows_hide_flags©Úatomic_json_writezIv1.b507a08c87ecfe98Úghp_c               ó    € V ^8„  d   QhRRRR/# )é   ÚtokenÚstrÚreturnztuple[bool, str]© )Úformats   "ÚKC:\Users\johnn\AppData\Local\hermes\hermes-agent\hermes_cli\copilot_auth.pyÚ__annotate__r   &   s   € ÷ ñ  #ð Ð*:ñ ó    c                óà   € V P                  4       p V '       g   R# V P                  \        4      '       d   R# V P                  \        4      '       g   RRRP	                  \        4       R23# R# )z5Validate that a token is usable with the Copilot API.FzOUnsupported GitHub token format for the Copilot API. Supported token prefixes: z, Ú.)FzEmpty token)Fu3  Classic Personal Access Tokens (ghp_*) are not supported by the Copilot API. Use one of:
  â†’ `copilot login` or `hermes model` to authenticate via OAuth
  â†’ A fine-grained PAT (github_pat_*) with Copilot Requests permission
  â†’ `gh auth login` with the default device code flow (produces gho_* tokens))TÚOK)ÚstripÚ
startswithÚ_CLASSIC_PAT_PREFIXÚ_SUPPORTED_PREFIXESÚjoin)r   s   &r   Úvalidate_copilot_tokenr   &   s|   € à�K‰K‹M€EßØ#Ð#Ø×ÑÔ+×,Ò,ð_ð 	_ð ×ÑÔ/×0Ò0Øð)Ø)-¯©Ô3FÓ)GÐ(HÈðKðLð 	Lð Ðr   c               ó   € V ^8„  d   QhRR/# )r   r   ztuple[str, str]r   )r   s   "r   r   r   9   s   € ÷ ñ ˜ñ r   c                 ó–  € Rp \          Fd  p\        P                  ! VR4      P                  4       pV'       g   K2  Rp \	        V4      w  r4V'       d   W!3u # \
        P                  RW4       Kf  	  V '       d   \
        P                  R4       R# \        4       pV'       d(   \	        V4      w  r4V'       g   \        RV 24      hVR3# R# )	u’   Resolve a GitHub token suitable for Copilot API use â†’ (token, source); ("", "") if none.

Raises ValueError if only a classic PAT is available.
FÚ Tz"Token from %s is not supported: %sz±Copilot env var(s) set but none held a supported token; skipping `gh auth token` fallback to honor explicit env-var intent (and avoid the subprocess cost on cold start, #60800).z7Token from `gh auth token` is not usable with Copilot. zgh auth token)r   r   )
ÚCOPILOT_ENV_VARSÚosÚgetenvr   r   ÚloggerÚwarningÚdebugÚ_try_gh_cli_tokenÚ
ValueError)Úany_env_var_setÚenv_varÚvalÚvalidÚmsgr   s         r   Úresolve_copilot_tokenr-   9   s¹   € ð
 €Oß#ˆÜ�iŠi˜ Ó$×*Ñ*Ó,ˆßÙØˆÜ+¨CÓ0‰
ˆßØ�<ÒÜ�‰Ð;¸WÖJñ $÷ Ü�‰ð 4ô 	5ð ˆÜÓ€EßÜ+¨EÓ2‰
ˆßÜÐVÐWZÐV[Ð\Ó]Ð]Ø�oÐ%Ð%Ø€Mr   c               ó   € V ^8„  d   QhRR/# )r   r   z	list[str]r   )r   s   "r   r   r   Y   s   € ÷ 
ñ 
˜Iñ 
r   c                 óÚ   € ^ RI Hp  ^ RIHpHp V ! R. V! 4       OV! 4       O5R7      p. pVP
                   F1  pVP                  V9  g   K  VP                  VP                  4       K3  	  V# )zTEvery present ``gh`` in probe order: PATH first, then Homebrew and ``~/.local/bin``.)Úlocate_command)Úhomebrew_dirsÚuser_local_binÚgh)Ú
known_dirs)Úhermes_platform.resolverr0   Ú#hermes_platform.resolver.known_dirsr1   r2   ÚpresentÚvalueÚappend)r0   r1   r2   ÚresÚseenÚcands         r   Ú_gh_cli_candidatesr=   Y   sZ   € å7ßQá
˜Ð*O©M«OÐ*O¹nÓ>NÑ*OÔ
P€CØ€DØ—”ˆØ�:‰:˜TÖ!Ø�K‰K˜Ÿ
™
Ö#ñ ð €Kr   g     Àr@z"tuple[float, Optional[str]] | NoneÚ_gh_cli_token_cachec               ó   € V ^8„  d   QhRR/# ©r   r   ÚNoner   )r   s   "r   r   r   n   s   € ÷ ñ ¨ñ r   c                 ó
   € Rs R# )zJReset the ``gh auth token`` probe cache (used by tests and re-auth flows).N)r>   r   r   r   Ú_invalidate_gh_cli_token_cacherC   n   s
   € ð Òr   c               ó   € V ^8„  d   QhRR/# ©r   r   úOptional[str]r   )r   s   "r   r   r   t   s   € ÷ 	ñ 	˜=ñ 	r   c                 ó    € \         P                  ! 4       p \        pVe"   W^ ,          ,
          \        8  d
   V^,          # \	        4       pW3sV# )zYToken from ``gh auth token`` when available; the result (incl. a miss) is cached per TTL.)ÚtimeÚ	monotonicr>   Ú_GH_CLI_TOKEN_CACHE_TTL_SECONDSÚ_probe_gh_cli_token)ÚnowÚcacher   s      r   r&   r&   t   sG   € ô �.Š.Ó
€CÜ€EØÒ˜S¨¥8�^Ô.MÔMØ�Q�xˆÜÓ!€EØ˜,ÐØ€Lr   c               ó   € V ^8„  d   QhRR/# rE   r   )r   s   "r   r   r   €   s   € ÷ ñ ˜]ñ r   c                 ó$  € \         P                  ! RR4      P                  4       p \         P                  P	                  4        UUu/ uF  w  rVR9  g   K  WbK  	  pppVP                  RR4       VP                  RR4       \        '       d   R\        4       /M/ pV '       d   RV .M. p\        4        F‘  pVRR	.VOp \        P                  ! V3R
RRRRRRRR^RVR\        P                  /VBRR/B pTP                   ^ 8X  g   KU  TP"                  P                  4       '       g   Kw  TP"                  P                  4       u # 	  R# u uppi   \        \        P                  3 d"   p	\        P                  RYi4        Rp	?	KÔ  Rp	?	ii ; i)zHUncached ``gh auth token`` subprocess probe (see ``_try_gh_cli_token``).ÚCOPILOT_GH_HOSTr   ÚGH_PROMPT_DISABLEDÚ1ÚGH_NO_UPDATE_NOTIFIERÚcreationflagsz
--hostnameÚauthr   Úcapture_outputTÚtextÚencodingzutf-8ÚerrorsÚreplaceÚtimeoutÚenvÚstdinÚcheckFz#gh CLI token lookup failed (%s): %sN>   ÚGH_TOKENÚGITHUB_TOKEN)r!   r"   r   ÚenvironÚitemsÚ
setdefaultr   r   r=   Ú
subprocessÚrunÚDEVNULLÚFileNotFoundErrorÚTimeoutExpiredr#   r%   Ú
returncodeÚstdout)
ÚhostnameÚkÚvÚ	clean_envÚ_popen_kwargsÚ	host_argsÚgh_pathÚcmdÚresultÚexcs
             r   rK   rK   €   s‰  € ä�yŠyÐ*¨BÓ/×5Ñ5Ó7€Hä"$§*¡*×"2Ñ"2Ô"4Ô^Ñ"4™$˜!¸ÐA]Ñ8]”�’Ñ"4€IÑ^Ø×ÑÐ-¨sÔ3Ø×ÑÐ0°#Ô6ß?I»z�_Ô&8Ó&:Ñ;Èr€Mß,4�˜xÑ(¸"€IÜ%Ö'ˆØ˜ Ð4¨)Ð4ˆð	Ü—^’^ Cñ \¸ð \À4ð \ÐRYð \Ø+4ð\Ø>?ð\ØENð\ä*4×*<Ñ*<ð\à@Mñ\àUZò\ˆFð ×Ñ Ö! f§m¡m×&9Ñ&9×&;Ô&;Ø—=‘=×&Ñ&Ó(Ò(ñ (ñ ùó! _øô "¤:×#<Ñ#<Ð=ô 	Ü�L‰LÐ>ÀÔMÝûð	ús$   ÁEÁEÃ7EÅFÅ.F
Æ
FÚexpired_tokenu,     âœ— Device code expired. Please try again.Úaccess_deniedu     âœ— Authorization was denied.c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   Úurlr   ÚfieldsÚdictr[   Úfloatr   r   )r   s   "r   r   r   ›   s(   € ÷ 0ñ 0�Cð 0 ð 0°ð 0¸$ñ 0r   c           
     óš  € \         P                  P                  V \         P                  P	                  V4      P                  4       R RRRRR/R7      p\         P                  P                  W2R7      ;_uu_ 4       p\        P                  ! VP                  4       P                  4       4      uuRRR4       #   + '       g   i     R# ; i)	ÚAcceptúapplication/jsonú
User-AgentúHermesAgent/1.0zContent-Typez!application/x-www-form-urlencoded)ÚdataÚheaders©r[   N)ÚurllibÚrequestÚRequestÚparseÚ	urlencodeÚencodeÚurlopenÚjsonÚloadsÚreadÚdecode)rx   ry   r[   ÚreqÚresps   &&&  r   Ú
_post_formr‘   ›   s–   € Ü
�.‰.×
 Ñ
 Ø”&—,‘,×(Ñ(¨Ó0×7Ñ7Ó9ØÐ-¨|Ð=NØÐ!DðFð !ó G€Cô 
�‰×	Ñ	 Ð	×	5Ô	5¸Ü�zŠz˜$Ÿ)™)›+×,Ñ,Ó.Ó/÷ 
6×	5×	5Ó	5ús   Á<2B9Â9C
	Úhostz
github.comÚtimeout_secondsi,  c               ó$   € V ^8„  d   QhRRRRRR/# )r   r’   r   r“   r{   r   rF   r   )r   s   "r   r   r   ¤   s$   € ÷ 1ñ 1Øð1Ø27ð1ØBOñ1r   c                ó  € V P                  R4      p \        RV R2R\        RR/^4      pTP                  R
R4      pTP                  RR4      pTP                  RR4      p\        TP                  R\        4      ^4      pT'       d	   T'       g   \        R4       R	# \        RT RT R24       \        RRRR7       R\        RTRR/p	\        P                  ! 4       T,           p
\        P                  ! 4       T
8  Ed   \        P                  ! T\        ,           4        \        RT R2T	^
4      pTP                  R4      '       d   \        R4       TR,          # TP                  RR4      pTR8X  dQ   TP                  R4      p\        T\        \         34      ;'       d    T^ 8„  pT'       d   \        T4      MT^,           pTR!9   d   \        RRRR7       Kù  T'       g   EK  \        R\"        P                  TRT 24      ,           4       R	# \        R 4       R	#   \         d0   p\        P                  RT4       \        RT 24        R	p?R	# R	p?ii ; i  \         d    \        RRRR7        EK–  i ; i)"z2Run the GitHub OAuth device code flow for Copilot.Ú/úhttps://z/login/device/codeÚ	client_idÚscopez	read:userz+Failed to initiate device authorization: %su,     âœ— Failed to start device authorization: NÚverification_urizhttps://github.com/login/deviceÚ	user_coder   Údevice_codeÚintervalu*     âœ— GitHub did not return a device code.z"
  Open this URL in your browser: z
  Enter this code: Ú
z  Waiting for authorization...T)ÚendÚflushÚ
grant_typez,urn:ietf:params:oauth:grant-type:device_codez/login/oauth/access_tokenr   Úaccess_tokenu    âœ“ÚerrorÚ	slow_downu     âœ— Authorization failed: u+   
  âœ— Timed out waiting for authorization.)Úauthorization_pendingr¤   )Úrstripr‘   ÚCOPILOT_OAUTH_CLIENT_IDÚ	Exceptionr#   r£   ÚprintÚgetÚmaxÚ_DEVICE_CODE_POLL_INTERVALrH   rI   ÚsleepÚ_DEVICE_CODE_POLL_SAFETY_MARGINÚ
isinstanceÚintr{   Ú_DEVICE_CODE_TERMINAL_ERRORS)r’   r“   ÚdomainÚdevice_datart   rš   r›   rœ   r�   Úpoll_fieldsÚdeadliners   r£   Úserver_intervalÚis_nums   $$             r   Úcopilot_device_code_loginr¸   ¤   s\  € ð �[‰[˜Ó€FðÜ  8¨F¨8Ð3EÐ!FØ"-Ô/FÈÐQ\Ð!]Ð_aócˆð #—‘Ð'9Ð;\Ó]ÐØ—‘ ¨RÓ0€IØ—/‘/ -°Ó4€KÜ�;—?‘? :Ô/IÓJÈAÓN€HßŸiÜÐ:Ô;ÙÜ	Ð/Ð0@Ð/Að B Ø )˜{¨"ð.ô /ä	Ð
*°¸$Õ?ØÔ 7¸ÈØÐ!OðQ€Kä�~Š~Ó /Õ1€HÜ
�.Š.Ó
˜XÕ
%Ü�
Š
�8Ô=Õ=Ô>ð	Ü (¨6¨(Ð2KÐ LÈkÐ[]Ó^ˆFð �:‰:�n×%Ò%Ü�&ŒMØ˜.Õ)Ð)Ø—
‘
˜7 BÓ'ˆØ�KÔà$Ÿj™j¨Ó4ˆOÜ ´#´u°Ó>×VÐVÀ?ÐUVÑCVˆFß/5”s˜?Ô+¸8Àa½<ˆHØÐ:Ô:Ü�#˜2 TÕ*ÙßŠ5Ü�$Ô5×9Ñ9¸%Ø9UÐV[ÐU\Ð7]ó_õ _ô `áÜ	Ð
8Ô9ÙøôU ô Ü�‰ÐBÀCÔHÜÐ<¸S¸EÐBÔCÝûðûô, ô 	Ü�#˜2 TÕ*Ûð	ús)   “H# Ä$I  È#IÈ.$IÉIÉ I>É=I>z+dict[str, tuple[str, float, Optional[str]]]Ú
_jwt_cachez0https://api.github.com/copilot_internal/v2/tokenzvscode/1.104.1zGitHubCopilotChat/0.26.7g      ø?z.copilot_jwt.jsoni   zdict[str, float]Ú_exchange_failure_cachezdict[str, threading.Lock]Ú_exchange_locksc               ó    € V ^8„  d   QhRRRR/# )r   Úfpr   r   zthreading.Lockr   )r   s   "r   r   r   ò   s   € ÷ ñ ˜3ð  >ñ r   c                óÎ   € \         ;_uu_ 4        \        P                  V 4      pVf   \        P                  ! 4       ;p\        V &   VuuR R R 4       #   + '       g   i     R # ; i)N)Ú_exchange_locks_guardr»   rª   Ú	threadingÚLock)r½   Úlocks   & r   Ú_exchange_lock_forrÃ   ò   sE   € ß	Ö	Ü×"Ñ" 2Ó&ˆØŠ<Ü)2¯ªÓ)9Ð9ˆD”? 2Ñ&Ø÷	 
×	×	Ó	ús   �9AÁA$	g      N@g      œ@c               ó    € V ^8„  d   QhRRRR/# )r   Ú	raw_tokenr   r   r   )r   s   "r   r   r      s   € ÷ ?ñ ? #ð ?¨#ñ ?r   c                ót   € \         P                  ! V P                  4       4      P                  4       R,          # )zNShort fingerprint of a raw token for cache keying (avoids storing full token).:Né   N)ÚhashlibÚsha256r‰   Ú	hexdigest)rÅ   s   &r   Ú_token_fingerprintrË      s(   € ä�>Š>˜)×*Ñ*Ó,Ó-×7Ñ7Ó9¸#Õ>Ð>r   c               ó    € V ^8„  d   QhRRRR/# )r   Úpathr   r   zOptional[dict]r   )r   s   "r   r   r     s   € ÷ ñ ˜$ð  >ñ r   c                ó�  € V P                  4       '       g   R#  V P                  4       P                  \        8”  d   \        P                  R\        4       R# \        P                  ! V P                  RR7      4      p\        V\        4      '       d   V# R#   \         d"   p\        P                  RT4        Rp?R# Rp?ii ; i)u¤   Bounded read of the on-disk JWT store â†’ dict, or None if missing/unusable (a store over
the 1 MiB cap or non-dict can't balloon memory or get rewritten back out).Nz6Persisted Copilot JWT store exceeds %d bytes; ignoringz	utf-8-sig)rX   z.Failed to read persisted Copilot JWT store: %s)ÚexistsÚstatÚst_sizeÚ_JWT_DISK_MAX_BYTESr#   r%   r‹   rŒ   Ú	read_textr¯   rz   r¨   )rÍ   Úloadedrt   s   &  r   Ú_read_jwt_storerÕ     s“   € ð �;‰;�=Š=Ùð	Ø�9‰9‹;×ÑÔ!4Ô4Ü�L‰LÐQÜ,ô.áÜ—’˜DŸN™N°K˜NÓ@ÓAˆÜ# F¬D×1Ò1ˆvÐ;°tÐ;øÜô Ü�‰ÐEÀsÔKÝûðús#   š=B Á=B ÂB ÂCÂ$C Ã Cc               ó   € V ^8„  d   QhRR/# )r   r   zOptional[Path]r   )r   s   "r   r   r     s   € ÷ ñ ˜ñ r   c                 ój   €  ^ RI Hp  \        V ! 4       4      \        ,          #   \         d     R# i ; i)zAPath to the on-disk exchanged-JWT cache (profile-aware), or None.©Úget_hermes_homeN)Úhermes_constantsrÙ   r   Ú_JWT_DISK_FILENAMEr¨   rØ   s    r   Ú_jwt_disk_pathrÜ     s0   € ðÝ4Ü‘OÓ%Ó&Ô);Õ;Ð;øÜô Úðús   ‚ # £2±2c               ó   € V ^8„  d   QhRR/# )r   Úverbr   r   )r   s   "r   r   r     s   € ÷ 	ñ 	˜#ñ 	r   c                ó®   € \        4       pV'       g   R#  V! V\        V4      4      #   \         d"   p\        P	                  RY4        Rp?R# Rp?ii ; i)zZRun ``op(path, store_or_None)`` against the disk store; failures are logged, never raised.NzFailed to %s Copilot JWT: %s)rÜ   rÕ   r¨   r#   r%   )rÞ   ÚoprÍ   rt   s   &&  r   Ú_with_jwt_storerá     sI   € äÓ€DßÙðÙ�$œ¨Ó-Ó.Ð.øÜô Ü�‰Ð3°TÔ?Ýûðús   –( ¨A³AÁAc               ó    € V ^8„  d   QhRRRR/# )r   rÅ   r   r   rA   r   )r   s   "r   r   r   +  s   € ÷ ,ñ ,¨Cð ,°Dñ ,r   c                ó®   a€ V '       g   R# \        V 4      o\        P                  SR4       \        P                  SR4       V3R lp\	        RV4       R# )u¦   Drop any cached exchanged JWT for ``raw_token`` (in-process + on-disk) â€” the runtime
stale-credential recovery path for ``model_not_available_for_integrator`` 400s.Nc                óH   <€ Ve   SV9   d   VS \        WR RR7       R # R # R # )Né€  ©ÚindentÚmoder   )rÍ   Ústorer½   s   &&€r   Ú_evictÚ,evict_cached_exchanged_token.<locals>._evict5  s*   ø€ ØÒ  u¤Ø�b�	Ü˜d°$¸U×Cñ "-Ñr   zevict cached)rË   r¹   Úpoprº   rá   )rÅ   rê   r½   s   & @r   Úevict_cached_exchanged_tokenrí   +  sE   ø€ ÷ ÙÜ	˜IÓ	&€BÜ‡N�N�2�tÔä×Ñ  DÔ)õDô
 �N FÖ+r   c               ó    € V ^8„  d   QhRRRR/# )r   r½   r   r   z*Optional[tuple[str, float, Optional[str]]]r   )r   s   "r   r   r   =  s   € ÷ 
4ñ 
4˜Cð 
4Ð$Nñ 
4r   c                ó(   a € V 3R lp\        RV4      # )uR   Persisted exchanged JWT for ``fp`` â†’ (api_token, expires_at, base_url), or None.c                ó"  <€ T;'       g    / P                  S4      p\        V\        4      '       g   R # VP                  RR4      p\        VP                  R^ 4      ;'       g    ^ 4      pV'       d   V'       d   W4VP                  R4      3# R # )NÚ	api_tokenr   Ú
expires_atÚbase_url)rª   r¯   rz   r{   )rÍ   ré   Úentryrñ   rò   r½   s   &&   €r   Ú_loadÚ"_load_jwt_from_disk.<locals>._load?  st   ø€ Ø—�˜"×!Ñ! "Ó%ˆÜ˜%¤×&Ò&ÙØ—I‘I˜k¨2Ó.ˆ	Ü˜5Ÿ9™9 \°1Ó5×:Ð:¸Ó;ˆ
ßAJÏz�	 u§y¡y°Ó'<Ð=ÐcÐ_cÐcr   zload persisted©rá   )r½   rõ   s   f r   Ú_load_jwt_from_diskrø   =  s   ø€ õdô Ð+¨UÓ3Ð3r   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR	/# )
r   r½   r   rñ   rò   r{   ró   rF   r   rA   r   )r   s   "r   r   r   J  s0   € ÷ 
&ñ 
&˜#ð 
&¨#ð 
&¸5ð 
&ÈMð 
&Ð^bñ 
&r   c                ó8   a aaa€ VVVV 3R lp\        RV4       R# )z:Persist an exchanged JWT (0o600), pruning expired entries.c           
     óR  <€ \         P                   ! 4       pT;'       g    / P                  4        UUu/ uFK  w  r4\        V\        4      '       g   K  \	        VP                  R ^ 4      ;'       g    ^ 4      V8”  g   KI  W4bKM  	  pppRSR SRS/VS	&   \        WRRR7       R# u uppi )rò   rñ   ró   Nrå   ræ   )rH   rb   r¯   rz   r{   rª   r   )
rÍ   ré   rL   rl   rm   Úkeptrñ   ró   rò   r½   s
   &&    €€€€r   Ú_saveÚ _save_jwt_to_disk.<locals>._saveL  sŸ   ø€ Ü�iŠi‹kˆà#Ÿk˜k r×0Ñ0Ô2ôQÙ2‘T�QÜ˜!œT×"ô Ü',¨Q¯U©U°<ÀÓ-C×-HÐ-HÀqÓ'IÈCÑ'Oô ˆAŠDÑ2ð 	ñ Qð   ¨L¸*ÀjÐRZÐ[ˆˆR‰Ü˜$¨T¸×>ùó	Qs   ´B#ÁB#Á1	B#Á?B#ÚpersistNr÷   )r½   rñ   rò   ró   rý   s   ffff r   Ú_save_jwt_to_diskr   J  s   û€ ÷?ð ?ô �I˜uÖ%r   g      @c               ó   € V ^8„  d   QhRR/# )r   r[   r{   r   )r   s   "r   r   r   \  s   € ÷ ñ  5ñ r   c                ó²  a aaa€ / o\         P                  ! 4       oR VVV V3R llp\         P                  ! VRRR7      pVP                  4        VP	                  S\
        ,           4       VP                  4       '       d,   SP                  4        \        RS\
        ,           R R24      hR	S9   d
   SR	,          hR
S9  d   \        R4      hSR
,          # )z¬urlopen() on a daemon thread, abandoned after timeout + _DNS_GRACE_SECONDS so a
DNS/getaddrinfo hang cannot block the caller. Raises the worker's exception or TimeoutError.c               ó   € V ^8„  d   QhRR/# r@   r   )r   s   "r   r   Ú&_urlopen_bounded.<locals>.__annotate__b  s   € ÷ 
ñ 
�Tñ 
r   c                 ó`  <€  \         P                  P                  SSR 7      p SP	                  4       '       d>   \
        P                  ! \        4      ;_uu_ 4        T P                  4        RRR4       R# T SR&   R#   \         d   pTSR&    Rp?R# Rp?ii ; i  + '       g   i     R# ; i)rƒ   rt   Nr�   )	r„   r…   rŠ   ÚBaseExceptionÚis_setÚ
contextlibÚsuppressr¨   Úclose)r�   rt   Ú	abandonedÚboxr�   r[   s     €€€€r   Ú_workerÚ!_urlopen_bounded.<locals>._workerb  s‡   ø€ ð	Ü—>‘>×)Ñ)¨#°wÐ)Ó?ˆDð ×Ñ×ÒÜ×$Ò$¤Y×/Õ/Ø—
‘
”÷ 0áØˆˆF‹øô ô 	ØˆC�‰JÝûð	ú÷ 0Ö/áús#   ƒ!A> ÁBÁ>BÂ	BÂBÂB-	zcopilot-token-exchangeT)ÚtargetÚnameÚdaemonz,copilot token exchange exceeded hard cap of z.0fzs (DNS/getaddrinfo hang?)rt   r�   z1copilot token exchange worker died without result)	rÀ   ÚEventÚThreadÚstartr   Ú_DNS_GRACE_SECONDSÚis_aliveÚsetÚTimeoutError)r�   r[   r  Útr  r  s   ff  @@r   Ú_urlopen_boundedr  \  s¾   û€ ð €CÜ—’Ó!€I÷
ó 
ô 	×Ò Ð.FÈtÔT€AØ‡G�G„IØ‡F�Fˆ7Ô'Õ'Ô(Ø‡z�z‡|‚|Ø�‰ŒÜÐIØ%Ô(:Õ:¸3Ð?Ð?XðZó [ð 	[à�„|Ø�%�jÐØ�SÔÜÐNÓOÐOØˆv�;Ðr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r[   r{   r½   r   r   rz   r   )r   s   "r   r   r   |  s!   € ÷ 2ñ 2¨Uð 2¸ð 2Àñ 2r   c           	     óJ  € RpRp\        ^\        ^,           4       Fj  p \        W4      ;_uu_ 4       p\        P                  ! VP                  4       P                  4       4      pRRR4       \        P                  VR4       Xu # 	  \        P                  ! 4       V'       d   \"        M\$        ,           \        V&   \'        R\         RV 24      Vh  + '       g   i     Lw; i  \         d°   pTp\        TRR4      ;'       g    \        TRR4      p	T	\        9   pT'       d   \        P                  RT	4        Rp? K»  T\        8  dG   \        T,          p
\        P                  RT\        YŠ4       \        P                   ! T
4        Rp?EKz   Rp?EK�  Rp?ii ; i)	z÷GET the exchange with backoff for startup network races; raises ValueError on failure.

Permanent rejections (401/403/404) skip the retry loop. Failures populate the negative
cache (long TTL for permanent, short for transient); success clears it.
NFÚcodeÚstatusz7Copilot token exchange rejected (HTTP %s); not retryingzCCopilot token exchange attempt %d/%d failed (%s); retrying in %.1fsz$Copilot token exchange failed after z attempts: )ÚrangeÚ_EXCHANGE_MAX_ATTEMPTSr  r‹   rŒ   r�   rŽ   rº   rì   r¨   ÚgetattrÚ!_EXCHANGE_PERMANENT_HTTP_STATUSESr#   r%   Ú_EXCHANGE_BACKOFF_BASE_SECONDSrH   r­   Ú'_EXCHANGE_FAILURE_TTL_PERMANENT_SECONDSÚ'_EXCHANGE_FAILURE_TTL_TRANSIENT_SECONDSr'   )r�   r[   r½   Úlast_excÚpermanent_failureÚattemptr�   r�   rt   r  Úsleep_ss   &&&        r   Ú_fetch_exchange_with_retryr*  |  sW  € ð %)€HØÐÜ˜Ô2°QÕ6Ö7ˆð	$Ü! #×/Ô/°4Ü—z’z $§)¡)£+×"4Ñ"4Ó"6Ó7�÷ 0ä#×'Ñ'¨¨DÔ1ØŠKñ 8ô$ #'§)¢)£+ß3DÕ/Ü4õ#6Ô˜BÑô Ð;Ô<RÐ;SÐS^Ø �zð#ó $Ø)1ð2÷' 0×/ûô ô 	$ØˆHÜ˜S &¨$Ó/×OÐO´7¸3ÀÈ$Ó3OˆFØ &Ô*KÑ KÐß Ü—‘ÐVÐX^Ô_ÞØÔ/Ô/Ü8¸7ÕB�Ü—‘ÐbØ$Ô&<¸côLä—
’
˜7×#Ó#ö	 0ûð	$ús;    C(³3CÁ&C(ÃC%Ã C(Ã(F"Ã34FÄ(FÅA
FÆF"c               ó   € V ^8„  d   QhRR/# )r   r   Úboolr   )r   s   "r   r   r   �  s   € ÷ Rñ R $ñ Rr   c                ó|   € \        V 4      ;'       d+    \        P                  ! 4       V ^,          \        ,
          8  # )é   )r,  rH   Ú_JWT_REFRESH_MARGIN_SECONDS)Úcacheds   &r   Ú_cache_entry_freshr1  �  s)   € Ü�‹<×QÐQœDŸIšI›K¨&°­)Ô6QÕ*QÑQÐQr   r[   g      $@c               ó$   € V ^8„  d   QhRRRRRR/# )r   rÅ   r   r[   r{   r   ú tuple[str, float, Optional[str]]r   )r   s   "r   r   r   ¡  s)   € ÷ Nñ NØðNØ %ðNØ1QñNr   c               óª  € \        V 4      p\        P                  V4      p\        V4      '       d   V# \        P                  VR4      p\
        P
                  ! 4       V8  d3   \        R\        V\
        P
                  ! 4       ,
          4       R24      h\        V4      ;_uu_ 4        \        WVR7      uuRRR4       #   + '       g   i     R# ; i)u¹  Exchange a raw GitHub token for a Copilot API token â†’ (token, expires_at, base_url).

The token is a semicolon-separated string (not a JWT) used as a Bearer token. ``base_url``
is the account-specific host: the exchange's ``endpoints.api`` (enterprise/proxied
accounts), else derived from the token's ``proxy-ep``; individual accounts have neither,
so it is None. Cached in-process until close to expiry. Raises ``ValueError`` on failure.
ç        úHCopilot token exchange recently failed; skipping re-attempt for another Úsrƒ   N)
rË   r¹   rª   r1  rº   rH   r'   r°   rÃ   Ú_exchange_copilot_token_locked)rÅ   r[   r½   r0  Ú_fail_untils   &$   r   Úexchange_copilot_tokenr:  ¡  s¦   € ô 
˜IÓ	&€Bô �^‰^˜BÓ€FÜ˜&×!Ò!ØˆÜ)×-Ñ-¨b°#Ó6€KÜ‡y‚yƒ{�[Ô Üð (Ü(+¨K¼$¿)º)»+Õ,EÓ(FÐ'GÀqðJó Kð 	Kô 
˜B×	Õ	Ü-¨iÀWÔM÷ 
 ×	×	Ó	ús   Â)CÃC	c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   rÅ   r   r½   r[   r{   r   r3  r   )r   s   "r   r   r   »  s,   € ÷ !+ñ !+Øð!+Øð!+Ø).ð!+Ø3Sñ!+r   c               ó  € \         P                  \        3 F)  pV! V4      p\        V4      '       g   K  V\         V&   Vu # 	  \        P                  VR 4      p\
        P
                  ! 4       V8  d3   \        R\        V\
        P
                  ! 4       ,
          4       R24      h\        P                  P                  \        RRRV  2R\        RRR	\        /R
7      p\        WbV4      pVP                  RR4      pV'       g   \        R4      h\        VP                  R4      ;'       g    ^ 4      ;'       g    \
        P
                  ! 4       R,           p	VP                  R4      p
\!        V
\"        4      '       dB   \%        V
P                  R4      ;'       g    R4      P'                  4       P)                  R4      MR;'       g    \+        V4      pW‰V3\         V&   \-        WW›4       \.        P1                  RW›4       W‰V3# )r5  r6  r7  ÚGETÚAuthorizationztoken r   r}   r~   úEditor-Version)Úmethodr‚   r   r   z+Copilot token exchange returned empty tokenrò   i  Ú	endpointsÚapir–   z3Copilot token exchanged, expires_at=%s, base_url=%s)r¹   rª   rø   r1  rº   rH   r'   r°   r„   r…   r†   Ú_TOKEN_EXCHANGE_URLÚ_EXCHANGE_USER_AGENTÚ_EDITOR_VERSIONr*  r{   r¯   rz   r   r   r¦   Ú_derive_base_url_from_proxy_epr   r#   r%   )rÅ   r½   r[   Úlookupr0  r9  r�   r�   rñ   rò   rA  ró   s   &&$         r   r8  r8  »  sÂ  € ô
 —>‘>Ô#6Ó7ˆÙ˜“ˆÜ˜f×%Ô%Ø#ŒJ�r‰NØŠMñ	 8ô *×-Ñ-¨b°#Ó6€KÜ‡y‚yƒ{�[Ô Üð (Ü(+¨K¼$¿)º)»+Õ,EÓ(FÐ'GÀqðJó Kð 	Kä
�.‰.×
 Ñ
 Ü EØ  F¨9¨+Ð"6¸ÔFZØÐ-Ð/?ÄðRð !ó S€Cô & c°BÓ7€DØ—‘˜ "Ó%€IßÜÐFÓGÐGÜ�t—x‘x Ó-×2Ð2°Ó3×IÐI´t·y²y³{ÀTÕ7I€Jð —‘˜Ó%€Iä?IÈ)ÔUY×?ZÒ?ZŒˆI�M‰M˜%Ó ×&Ð& BÓ'×-Ñ-Ó/×6Ñ6°sÔ;Ð`b÷3ð 3ä	'¨	Ó	2ð ð  ¨XÐ6„Jˆr�NÜ�b ZÔ:Ü
‡L�LÐFÈ
Ô]Ø (Ð*Ð*r   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   rF   r   )r   s   "r   r   r   ß  s   € ÷ !ñ !¨#ð !°-ñ !r   c                óò   € \         P                  ! RV 4      pV'       g   R# \         P                  ! RRVP                  ^4      ^R7      P	                  R4      p\         P                  ! RRV^R7      pR	V 2# )
uU   Copilot API base URL from the token's ``proxy-ep=proxy.<host>`` field (â†’ ``api.``).z(?:^|;)\s*proxy-ep=([^;\s]+)Nz
^https?://r   )Úcountr–   z^proxy\.zapi.r—   )ÚreÚsearchÚsubÚgroupr¦   )r   ÚmÚproxy_eps   &  r   rF  rF  ß  sa   € ä
�	Š	Ð1°5Ó9€AßÙÜ�vŠv�m R¨¯©°«¸1Ô=×DÑDÀSÓI€HÜ�vŠv�k 6¨8¸1Ô=€HØ�h�ZÐ Ð r   c               ó    € V ^8„  d   QhRRRR/# )r   rÅ   r   r   ztuple[str, Optional[str]]r   )r   s   "r   r   r   é  s   € ÷ 
ñ 
 Sð 
Ð-Fñ 
r   c                ó¢   € V '       g   V R3#  \        V 4      w  rpW3#   \         d%   p\        P                  RT4       T R3u Rp?# Rp?ii ; i)z…``(api_token, base_url)`` from the exchange, or ``(raw_token, None)`` when it fails
(accounts that don't need exchange keep working).Nz2Copilot token exchange failed, using raw token: %s)r:  r¨   r#   r%   )rÅ   rñ   Ú_ró   rt   s   &    r   Úget_copilot_api_tokenrT  é  sW   € ÷ Ø˜$ˆÐðÜ!7¸	Ó!BÑˆ	�hØÐ"Ð"øÜô Ü�‰ÐIÈ3ÔOØ˜$ˆÕûðús   Ž ŸAªA	ÁAÁ	AÚis_agent_turnTÚ	is_visionFc               ó$   € V ^8„  d   QhRRRRRR/# )r   rU  r,  rV  r   zdict[str, str]r   )r   s   "r   r   r   ö  s$   € ÷ 	ñ 	Øð	Ø.2ð	Ø?Mñ	r   c           
     óT   € R\         RRRRRRRV '       d   R	MR
/pV'       d   RVR&   V# )z4Build the standard headers for Copilot API requests.r?  r   r€   zCopilot-Integration-Idzvscode-chatzOpenai-Intentzconversation-editszx-initiatorÚagentÚuserÚtruezCopilot-Vision-Request)rE  )rU  rV  r‚   s   $$ r   Úcopilot_request_headersr\  ö  s?   € ð  0´À,ÐPaØ7¸Ø.Ð0DØ,¿©gÈFðT€G÷ Ø,2ˆÐ(Ñ)Ø€Nr   )Úgho_Úgithub_pat_Úghu_)ÚCOPILOT_GITHUB_TOKENr_   r`   >   é‘  é“  é”  )QÚ__conditional_annotations__Ú__doc__Ú
__future__r   r  rÈ   r‹   Úloggingr!   rK  Úshutilrd   rÀ   rH   Úurllib.parser„   Úurllib.requestÚpathlibr   Útypingr   Úhermes_cli._subprocess_compatr   r   Úutilsr   Ú	getLoggerÚ__name__r#   r§   r   r   r    r¬   r®   r   r-   r=   rJ   r>   Ú__annotations__rC   r&   rK   r±   r‘   r¸   r¹   r/  rC  rE  rD  r   r#  rÛ   rÒ   rº   r»   rÁ   r¿   rÃ   r%  r$  Ú	frozensetr"  rË   rÕ   rÜ   rá   rí   rø   r   r  r  r*  r1  r:  r8  rF  rT  r\  )rd  s   @r   Ú<module>rs     sØ  øðôJõ #ã Û Û Û Û 	Û 	Û Û Û Û Û Û Ý Ý ç HÝ #à	×	Ò	˜8Ó	$€ð 1Ð ØÐ ð 6Ð ØGÐ ØÐ Ø"#Ð õõ&õ@
ð" #(Ð Ø:>Ð Ð7Ó >õõ	õð. !0Ð1_Ø /Ð1Rð TÐ õ0ð1Øð1Ø:=÷1ðj ;=€
Ð7Ó <Ø!Ð àHÐ Ø"€Ø1Ð ð Ð Ø!$Ð Ø(Ð ØÐ ð -/Ð Ð)Ó .ð .0€Ð*Ó /Ø!ŸšÓ(Ð õð +/Ð 'Ø*0Ð 'á$-ªoÓ$>Ð !õ?õ
õ"õ	õ,õ$
4õ
&ð Ð õõ@2õBRðNØ(,÷Nõ4!+õH!õ
ð	Ø!ð	Ø5:÷	ñ 	r   