Ë
    ÂÓ^j©  ã                   óp  — U d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddl	mZ  ej                  e«      Zd	Zd
Z G d„ ded¬«      Z G d„ de«      Zg i dœZeed<   daedz  ed<   dee   fd„Zdefd„Zdeeef   defd„Zdefd„Zdefd„Zdededz  dedz  fd„Zdededdfd„Z dedz  fd „Z!y)!u®  Detect whether the process is being invoked by an AI coding agent.

Detection is based on environment variables that AI agents set in their shell
sessions. `AI_AGENT` and `AGENT` are treated as a universal standard (any
tool can set its harness id there); the remaining checks are tool-specific and
ordered by priority (first match wins).

The list of known harnesses is maintained on the Hub and exposed at
`{ENDPOINT}/api/agent-harnesses`. We fetch it at most once a day and cache it
locally so the list can be updated without requiring a new client release.

Detection is entirely best-effort: there is no hardcoded list of harnesses. When
the registry cannot be fetched (and no cached copy is available), detection simply
reports "no agent". Any error while fetching/reading the registry is swallowed â€”
detection must never make a process fail.

More details: https://huggingface.co/docs/hub/agents-overview#register-your-agent-harness
é    N)ÚPath)ÚOptionalÚ	TypedDicté   )Ú	constantsé   )Úloggingi€Q é   c                   ó&   — e Zd ZU dZeeef   ed<   y)ÚHarnessInfozbA single harness entry. `envVars` maps an env var name to a match pattern (see `_env_vars_match`).ÚenvVarsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚdictÚstrÚ__annotations__© ó    ún/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/huggingface_hub/utils/_detect_agent.pyr   r   4   s   … Ùlà�#�s�(‰^Ôr   r   F)Útotalc                   ó6   — e Zd ZU dZee   ed<   eeef   ed<   y)ÚRegistryzJThe agent harness registry, as served by `{ENDPOINT}/api/agent-harnesses`.ÚstandardEnvVarsÚ	harnessesN)	r   r   r   r   Úlistr   r   r   r   r   r   r   r   r   :   s   … ÙTà˜#‘YÓØ�C˜Ð$Ñ%Ô%r   r   )r   r   Ú_EMPTY_REGISTRYÚ	_registryÚreturnc                  ób  — t        «       } | j                  d«      xs g }| j                  d«      xs i }|j                  «       D ]i  \  }}|xs i j                  d«      }|rt        |«      r|c S |D ]9  }t        j
                  j                  |d«      j                  «       |k(  sŒ5|c c S  Œk |j                  «       D �ch c]  }|j                  «       ’Œ }}|D ]J  }t        j
                  j                  |d«      j                  «       j                  «       x}	sŒB|	|v r|	c S  y yc c}w )a[  Return the id of the detected AI agent harness or `None`.

    Harnesses are checked in registry order; for each one we match its env var
    pattern(s) and, failing that, the standard `AI_AGENT` / `AGENT` vars
    against the harness id. The first match wins. When a standard var is set to
    an unrecognized value, `"unknown"` is returned.
    r   r   r   Ú ÚunknownN)	Ú_get_registryÚgetÚitemsÚ_env_vars_matchÚosÚenvironÚstripÚkeysÚlower)
ÚregistryÚstandard_varsr   Ú
harness_idÚinfoÚenv_varsÚvarÚkÚlowercased_harnessesÚvalues
             r   Údetect_agentr6   I   s   € ô ‹€HØ—L‘LÐ!2Ó3Ò9°r€MØ—‘˜[Ó)Ò/¨R€Ià%ŸO™OÓ-ò "Ñˆ
�DØ’J˜B×#Ñ# IÓ.ˆÙœ¨Ô1ØÒØ ò 	"ˆCÜ�z‰z�~‰~˜c 2Ó&×,Ñ,Ó.°*Ó<Ø!Ô!ñ	"ð	"ð 09¯~©~Ó/?Ö@¨!˜AŸG™G�IÐ@ÐÐ@Øò ˆÜ—J‘J—N‘N 3¨Ó+×1Ñ1Ó3×9Ñ9Ó;Ð;ˆ5Ñ;ØÐ,Ñ,Ø’Ùð	ð ùò As   ÃD,c                  ó   — t        «       duS )zDReturn `True` if the process is being invoked by an AI coding agent.N)r6   r   r   r   Úis_agentr8   h   s   € ä‹> Ð%Ð%r   r1   c                 ó”   — | j                  «       D ]5  \  }}t        j                  j                  |«      }|sŒ(|dk(  r y||k(  sŒ5 y y)záReturn `True` if any `(var, pattern)` from the harness matches the environment.

    Supported patterns:
      - `"*"`: the variable is set to any non-empty value
      - `"<value>"`: the variable equals this exact value
    Ú*TF)r&   r(   r)   r%   )r1   r2   Úpatternr5   s       r   r'   r'   m   sP   € ð !Ÿ™Ó(ò ‰ˆˆWÜ—
‘
—‘˜sÓ#ˆÙØØ�cŠ>ÙØ�GÓÙðð r   c                  óž   — t         €	 t        «       a t         S t         S # t        $ r% t        j	                  dd¬«       t
        a Y t         S w xY w)zµReturn the harness registry, loading (and caching in-process) on first call.

    Best-effort: any unexpected error degrades to an empty registry so detection
    never raises.
    z+Could not resolve agent harnesses registry.T©Úexc_info)r   Ú_load_registryÚ	ExceptionÚloggerÚdebugr   r   r   r   r$   r$      sR   € ô Ðð	(Ü&Ó(ˆIô ÐŒ9Ðøô ò 	(Ü�L‰LÐFÐQUˆLÔVÜ'‰IÜÐð	(ús   ˆ
 ž&AÁAc                  ó®   — t         j                  } t        | t        ¬«      x}r|S t	        «       x}�t        | |«       |S t        | d¬«      x}r|S t        S )zÆResolve the registry from the local cache or the Hub.

    No hardcoded list: if the Hub is unreachable and no cached copy exists, an
    empty registry is returned (i.e. no agent is detected).
    )Úmax_ageN)r   ÚAGENT_HARNESSES_PATHÚ_read_cached_registryÚ_REGISTRY_TTL_SECONDSÚ_fetch_registryÚ_write_cached_registryr   )ÚpathÚcachedÚfetchedÚstales       r   r?   r?   �   sg   € ô ×)Ñ)€Dô ' tÔ5JÔKÐK€vÐKØˆô #Ó$Ð$ˆÐ1Ü˜t WÔ-Øˆô & d°DÔ9Ð9€uÐ9ØˆÜÐr   rJ   rD   c                 ót  — 	 t         j                  j                  | «      sy|�8t        j                  «       t         j                  j	                  | «      z
  |k\  ryt        | d¬«      5 }t        j                  |«      cddd«       S # 1 sw Y   yxY w# t        $ r t        j                  dd¬«       Y yw xY w)zBReturn the cached registry, or `None` if missing/stale/unreadable.Núutf-8©Úencodingz/Could not read cached agent harnesses registry.Tr=   )r(   rJ   ÚexistsÚtimeÚgetmtimeÚopenÚjsonÚloadr@   rA   rB   )rJ   rD   Úfs      r   rF   rF   ¦   s“   € ð	Ü�w‰w�~‰~˜dÔ#ØØÐ¤D§I¡I£K´"·'±'×2BÑ2BÀ4Ó2HÑ$HÈWÒ#TØÜ�$ Ô)ð 	 ¨QÜ—9‘9˜Q“<÷	 ÷ 	 ò 	 ûäò Ü�‰ÐFÐQUˆÔVÙðús:   ‚B ¢9B ÁB Á)BÁ>	B ÂBÂB ÂB Â B7Â6B7r-   c                 ó  — 	 t        | «      j                  j                  dd¬«       t        | dd¬«      5 }t	        j
                  ||«       d d d «       y # 1 sw Y   y xY w# t        $ r t        j                  dd¬«       Y y w xY w)NT)ÚparentsÚexist_okÚwrO   rP   z)Could not cache agent harnesses registry.r=   )	r   ÚparentÚmkdirrU   rV   Údumpr@   rA   rB   )rJ   r-   rX   s      r   rI   rI   ´   sw   € ðQÜˆT‹
×Ñ×Ñ¨°tÐÔ<Ü�$˜ gÔ.ð 	#°!Ü�I‰I�h Ô"÷	#÷ 	#ñ 	#ûäò QÜ�‰Ð@È4ˆÖPðQús.   ‚4A" ¶AÁA" ÁAÁA" ÁA" Á" BÂBc                  ó  — t         j                  ry	 ddlm}   | «       j	                  t         j
                  › d�t        ¬«      }|j                  «        |j                  «       S # t        $ r t        j                  dd¬«       Y yw xY w)	zMFetch the registry from the Hub. Returns `None` when offline or on any error.Nr   )Úget_sessionz/api/agent-harnesses)Útimeoutz6Could not fetch agent harnesses registry from the Hub.Tr=   )r   ÚHF_HUB_OFFLINEÚ_httpra   r%   ÚENDPOINTÚ_REGISTRY_FETCH_TIMEOUTÚraise_for_statusrV   r@   rA   rB   )ra   Úresponses     r   rH   rH   ½   s€   € ä×ÒØðÝ&á“=×$Ñ$Ü×!Ñ!Ð"Ð"6Ð7Ü+ð %ó 
ˆð 	×!Ñ!Ô#Ø�}‰}‹ÐøÜò Ü�‰ÐMÐX\ˆÔ]Ùðús   “AA& Á& B	ÂB	)"r   rV   r(   rS   Úpathlibr   Útypingr   r   r"   r   r	   Ú
get_loggerr   rA   rG   rf   r   r   r   r   r   r   r6   Úboolr8   r   r'   r$   r?   ÚintrF   rI   rH   r   r   r   ú<module>rn      s  ðòó& Û 	Û Ý ß &å Ý ð 
ˆ×	Ñ	˜HÓ	%€ð "Ð ð Ð ô�) 5õ ô&ˆyô &ð 13ÀÑD€�Ó Dð "€	ˆ8�d‰?Ó !ð�h˜s‘mó ð>&�$ó &ð
˜d 3¨ 8™nð °ó ð$�xó ð ˜ó ð. ð ¨c°D©jð ¸XÈ¹_ó ðQ ð Q°ð Q¸Tó Qð˜ D™ô r   