Ë
    ÂÓ^jn  ã                   ó€  — d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZ  ej"                  e«      ZdZd	Ze G d
„ d«      «       Ze G d„ d«      «       Zde	fd„Zdede	fd„Zdededz  fd„Zddœdedededededz  dedz  dedz  dee   dee   dz  ddfd„Zdeddfd„Zde	deddfd „Z y)!z?Best-effort local cache for SandboxPool hosts (host/pool mode).é    N)ÚasdictÚ	dataclassÚfield)ÚPath)ÚListé   )Ú	constants)ÚWeakFileLockÚloggingg      @c                   ó\   — e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   dZeed	<   y
)Ú
CachedHostaC  A single host Job of a pool, as last seen by some process.

    `base_url` + `nonce` are everything needed to rebuild the in-job server transport
    (`_SandboxServer`) without an `inspect_job` round-trip: the per-sandbox auth token is
    re-derived from the user's HF token and `nonce` (see `_derive_sandbox_token`).
    Újob_idÚownerÚbase_urlÚnonceÚcapacityÚliveç        Ú
updated_atN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__Úintr   Úfloat© ó    úi/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/huggingface_hub/_sandbox_cache.pyr   r   &   s1   … ñð ƒKØƒJØƒMØƒJØƒMØ
ƒIØ€J�Ôr   r   c                   ó¨   — e Zd ZU dZeed<   eed<   eed<   eed<   edz  ed<   edz  ed<   edz  ed	<    ee¬
«      Z	e
e   ed<   eZeed<   dZeed<   y)Ú	PoolCachezJCached view of one pool: its config (to boot new hosts) + its known hosts.Úpool_idÚimageÚflavorÚsandboxes_per_hostNÚ	max_hostsÚidle_timeoutÚ	namespace)Údefault_factoryÚhostsÚversionr   r   )r   r   r   r   r   r   r   r   Úlistr+   r   r   Ú_CACHE_VERSIONr,   r   r   r   r   r    r"   r"   8   s`   … áTàƒLØƒJØƒKØÓØ�T‰zÓØ˜‘*ÓØ�T‰zÓÙ#°DÔ9€Eˆ4�
ÑÓ9Ø!€GˆSÓ!Ø€J�Ôr   r"   Úreturnc                  ó@   — t        t        j                  «      dz  dz  S )NÚsandboxÚpools)r   r	   ÚHF_HOMEr   r   r    Ú
_pools_dirr4   H   s   € Ü”	×!Ñ!Ó" YÑ.°Ñ8Ð8r   r#   c                 óp   ‡ — t        ˆ fd„dD «       «      s‰ dv rt        d‰ ›�«      ‚t        «       ‰ › d�z  S )z.Path of the cache file for `pool_id` (no I/O).c              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­w)Nr   )Ú.0Úcr#   s     €r    ú	<genexpr>z"pool_cache_path.<locals>.<genexpr>N   s   øè ø€ Ò
5˜Aˆ1�Œ<Ñ
5ùs   ƒ)ú/ú\ú )ú.z..zInvalid pool id: z.json)ÚanyÚ
ValueErrorr4   )r#   s   `r    Úpool_cache_pathr@   L   s@   ø€ ä
Ó
5Ð!4Ô
5Ô5¸ÀKÑ9OÜÐ,¨W¨KÐ8Ó9Ð9Ü‹<˜W˜I UÐ+Ñ+Ð+r   c                 ó´  — 	 t        | «      }|j                  dd¬«      5 }t        j                  |«      }ddd«       j	                  d«      t
        k7  ry|j                  dg «      D �cg c]  }t        d	i |¤Ž‘Œ }}t        d	i |¤d|i¤ŽS # 1 sw Y   ŒYxY wc c}w # t        $ r Y yt        $ r%}t        j                  d| ›d|› �«       Y d}~yd}~ww xY w)
zOReturn the cached view of `pool_id`, or `None` if missing/corrupt/incompatible.Úrúutf-8©ÚencodingNr,   r+   z+Ignoring unreadable sandbox pool cache for ú: r   )r@   ÚopenÚjsonÚloadÚgetr.   Úpopr   r"   ÚFileNotFoundErrorÚ	ExceptionÚloggerÚdebug)r#   ÚpathÚfÚdataÚhr+   Úes          r    Úread_pool_cacherU   S   sÎ   € ðÜ˜wÓ'ˆØ�Y‰Y�s WˆYÓ-ð 	 °Ü—9‘9˜Q“<ˆD÷	 à�8‰8�IÓ¤.Ò0ØØ*.¯(©(°7¸BÓ*?Ö@ Q”‘˜a“Ð@ˆÐ@ÜÑ-˜4Ñ- uÒ-Ð-÷	 ð 	 üò Aøäò ÙÜò Ü�‰ÐBÀ7À+ÈRÐPQÈsÐSÔTÜûðúsE   ‚B  B¶ B ÁB Á+BÁ=B ÂBÂB Â	CÂ*CÂ2CÃC)Údead_host_idsr$   r%   r&   r'   r(   r)   r+   rV   c                ó˜  — |xs
 t        «       }		 t        | «      }
|
j                  j                  dd¬«       t	        t        |
«      dz   t        ¬«      5  t        | «      }|r|j                  ng D �ci c]  }|j                  |“Œ }}|D ]  }|||j                  <   Œ |	D ]  }|j                  |d«       Œ t        | ||||||t        |j                  «       «      t        j                  «       ¬«	      }t        |
|«       ddd«       yc c}w # 1 sw Y   yxY w# t         $ r%}t"        j%                  d| ›d|› �«       Y d}~yd}~ww xY w)	a•  Merge `hosts` into the cache for `pool_id` (best-effort, never raises).

    Concurrency-safe: under a file lock, the on-disk hosts are read, then `hosts` are
    upserted by `job_id` and `dead_host_ids` removed, so a process only adds/updates what
    it learned and never drops hosts another process discovered. The result is written
    atomically. The pool config is refreshed from the arguments.
    T)ÚparentsÚexist_okz.lock)ÚtimeoutN)	r#   r$   r%   r&   r'   r(   r)   r+   r   z'Could not write sandbox pool cache for rF   )Úsetr@   ÚparentÚmkdirr
   r   Ú_LOCK_TIMEOUTrU   r+   r   rK   r"   r-   ÚvaluesÚtimeÚ_atomic_writerM   rN   rO   )r#   r$   r%   r&   r'   r(   r)   r+   rV   ÚdeadrP   ÚexistingrS   ÚmergedÚhostr   ÚcacherT   s                     r    Úsave_pool_cacherg   d   s@  € ð& Ò!œC›E€DðQÜ˜wÓ'ˆØ�‰×Ñ $°ÐÔ6Üœ#˜d›) gÑ-´}ÔEñ 	'Ü& wÓ/ˆHÙ>F¨H¯NªNÈBÖP a�a—h‘h ‘kÐPˆFÐPØò +�Ø&*��t—{‘{Ò#ð+àò )�Ø—
‘
˜6 4Õ(ð)äØØØØ#5Ø#Ø)Ø#Ü˜6Ÿ=™=›?Ó+ÜŸ9™9›;ô
ˆEô ˜$ Ô&÷%	'ð 	'ùâP÷	'ð 	'ûô& ò QÜ�‰Ð>¸w¸kÈÈAÈ3ÐO×PÑPûðQúsI   �AD ÁDÁ3D
ÂA:DÄD Ä
DÄDÄD ÄD Ä	E	Ä$EÅE	c                 óž   — 	 t        | «      j                  d¬«       y# t        $ r%}t        j	                  d| › d|› �«       Y d}~yd}~ww xY w)z@Remove the cache file for `pool_id` (best-effort, never raises).T)Ú
missing_okz(Could not delete sandbox pool cache for rF   N)r@   ÚunlinkrM   rN   rO   )r#   rT   s     r    Údelete_pool_cacherk   ’   sM   € ðPÜ˜Ó ×'Ñ'°4Ð'Õ8øÜò PÜ�‰Ð?À¸yÈÈ1È#ÐN×OÑOûðPús   ‚ ž	A§AÁArP   rf   c                 ó$  — | j                   | j                  › dt        j                  «       › d�z  }|j	                  dd¬«      5 }t        j                  t        |«      |d¬«       ddd«       t        j                  || «       y# 1 sw Y   Œ xY w)	zSWrite the cache via a temp file + `os.replace` so readers never see a partial file.r=   z.tmpÚwrC   rD   é   )ÚindentN)	r\   ÚnameÚosÚgetpidrG   rH   Údumpr   Úreplace)rP   rf   ÚtmprQ   s       r    ra   ra   š   sp   € à
�+‰+˜4Ÿ9™9˜+ Q¤r§y¡y£{ m°4Ð8Ñ
8€CØ	�‰�# ˆÓ	(ð .¨AÜ�	‰	”&˜“- ¨1Õ-÷.ä‡J�Jˆs�DÕ÷.ð .ús   Á"BÂB)!r   rH   rq   r`   Údataclassesr   r   r   Úpathlibr   Útypingr   Ú r	   Úutilsr
   r   Ú
get_loggerr   rN   r.   r^   r   r"   r4   r   r@   rU   r   r[   rg   rk   ra   r   r   r    ú<module>r|      sr  ðñ Fã Û 	Û ß 0Ñ 0Ý Ý å ß (ð 
ˆ×	Ñ	˜HÓ	%€ð €ð €ð ÷ð ó ðð" ÷ð ó ðð9�Dó 9ð,˜Sð , Tó ,ð˜Sð  Y°Ñ%5ó ð6 &*ò+QØð+Qð ð+Qð ð	+Qð
 ð+Qð �T‰zð+Qð ˜‘*ð+Qð �T‰zð+Qð �
Ñð+Qð �s‘8˜d‘?ð+Qð 
ó+Qð\P˜sð P tó Pð˜ð  Yð °4ô r   