Ë
    âß²j]  ã                  óœ   — d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
mZ ddlmZ dd„Ze
 G d	„ d
«      «       Z G d„ d«      Z e«       Zy)zëOwnership leases for replaceable runtime registrations.

The coordinator models registration *generations*, not just value identity: the same provider
singleton may be registered again after an older ownership generation was unloaded.
é    )ÚannotationsN)ÚCallableÚHashable)Úcontextmanager)Ú	dataclassÚfield)ÚAnyc                ó´   — t        | t        «      rEt        |t        «      r5t        | «      t        |«      k(  xr t        d„ t	        | |«      D «       «      S | |u S )zPCompare opaque registry snapshots using identity only (element-wise for tuples).c              3  ó:   K  — | ]  \  }}t        ||«      –— Œ y ­w©N)Úsame_registration)Ú.0ÚaÚbs      ú;/home/ubuntu/.hermes/hermes-agent/registration_lifecycle.pyú	<genexpr>z$same_registration.<locals>.<genexpr>   s   è ø€ Ò.dÉ4È1ÈaÔ/@ÀÀA×/FÑ.dùs   ‚)Ú
isinstanceÚtupleÚlenÚallÚzip)ÚleftÚrights     r   r   r      sL   € ä�$œÔ¤:¨e´UÔ#;Ü�4‹yœC ›JÑ&Òd¬3Ñ.dÔSVÐW[Ð]bÓScÔ.dÓ+dÐdØ�5ˆ=Ðó    c                  ó†   — e Zd ZU dZded<   ded<   ded<   ded<   d	ed
<   dZded<   dZded<    edd¬«      Zded<   dd„Z	y)ÚReplacementLeasez8One ownership generation in a replaceable registry slot.z'ReplacementCoordinator'Úcoordinatorr   Úslotr	   ÚcurrentÚpreviousúCallable[[Any], bool]ÚrestoreNúCallable[[], None] | NoneÚfinalizez'ReplacementLease | None'ÚpredecessorTF)ÚdefaultÚinitÚboolÚactivec                ó:   — | j                   j                  | «       y r   )r   Údispose©Úselfs    r   r+   zReplacementLease.dispose$   s   € Ø×Ñ× Ñ  Õ&r   ©ÚreturnÚNone)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r$   r%   r   r)   r+   © r   r   r   r      sL   … áBà)Ó)Ø
ƒNØƒLØƒMØ"Ó"Ø*.€HÐ'Ó.Ø-1€KÐ*Ó1Ù ¨EÔ2€FˆDÓ2ô'r   r   c                  óD   — e Zd ZdZdd„Zed„ «       Zddœ	 	 	 d	d„Zd
d„Zy)ÚReplacementCoordinatorzCLink and remove registration generations in arbitrary unload order.c                óD   — i | _         t        j                  «       | _        y r   )Ú_activeÚ	threadingÚRLockÚ_lockr,   s    r   Ú__init__zReplacementCoordinator.__init__+   s   € Ø?AˆŒÜ—_‘_Ó&ˆ�
r   c              #  óV   K  — | j                   5  d–— ddd«       y# 1 sw Y   yxY w­w)z@Serialize a registry snapshot/write/acquire with lease disposal.N)r=   r,   s    r   Útransactionz"ReplacementCoordinator.transaction/   s&   è ø€ ð �Z‰Zñ 	Û÷	÷ 	ñ 	üs   ‚)�”	)�&¢)N)r$   c          
     ó  ‡— | j                   5  | j                  j                  |g «      }t        ˆfd„t	        |«      D «       d«      }t        | ||‰|||«      }|j                  |«       |cddd«       S # 1 sw Y   yxY w)z@Attach a new live generation to the matching active predecessor.c              3  ój   •K  — | ]*  }|j                   sŒt        |j                  ‰«      sŒ'|–— Œ, y ­wr   )r)   r   r   )r   Úcr    s     €r   r   z1ReplacementCoordinator.acquire.<locals>.<genexpr>:   s)   øè ø€ Òs a¸q¿x»xÔL]Ð^_×^gÑ^gÐiqÕLr¤Ñsùs   ƒ3•3¬3N)r=   r:   Ú
setdefaultÚnextÚreversedr   Úappend)	r-   r   r   r    r"   r$   Úleasesr%   Úleases	      `     r   ÚacquirezReplacementCoordinator.acquire5   su   ø€ ð �Z‰Zñ 	Ø—\‘\×,Ñ,¨T°2Ó6ˆFÜÓs¬8°FÓ+;ÔsÐuyÓzˆKÜ$ T¨4°¸(ÀGÈXÐWbÓcˆEØ�M‰M˜%Ô Ø÷	÷ 	ò 	ús   ŽAA6Á6A?c                ó
  — | j                   5  |j                  s
	 ddd«       y| j                  j                  |j                  g «      }t        d„ t        |«      D «       d«      }d|_        	 	 ||u rl|j                  |j                  }}|�3|j                  s'|j                  |j                  }}|�|j                  sŒ'|j                  |�|j                  n|«       |j                  �|j                  «        	 |D �cg c]  }|j                  sŒ|‘Œ }}|r|| j                  |j                  <   n)|r'| j                  j                  |j                  d«       	 ddd«       y# |j                  �|j                  «        w w xY wc c}w # |D �cg c]  }|j                  sŒ|‘Œ nc c}w }}|r|| j                  |j                  <   w |r'| j                  j                  |j                  d«       w w xY w# 1 sw Y   yxY w)z¦Remove *lease*, restoring the nearest still-live predecessor.

        ``restore`` -> ``finalize`` -> slot pruning each run even when an earlier step raises.
        Nc              3  ó:   K  — | ]  }|j                   sŒ|–— Œ y ­wr   )r)   )r   rC   s     r   r   z1ReplacementCoordinator.dispose.<locals>.<genexpr>H   s   è ø€ ÒC ¸!¿(»(œ1ÑCùs   ‚”F)r=   r)   r:   Úgetr   rE   rF   r    r%   r"   r   r$   Úpop)r-   rI   rH   ÚlatestÚreplacementr%   ÚitemÚlives           r   r+   zReplacementCoordinator.dispose?   sµ  € ð
 �Z‰Zñ 	7Ø—<’<Ø÷	7ð 	7ð —\‘\×%Ñ% e§j¡j°"Ó5ˆFÜÑC¤h¨vÓ&6ÔCÀTÓJˆFØ ˆEŒLð7ð	)Ø ‘à38·>±>À5×CTÑCT [˜Ø)Ð5¸k×>PÒ>PØ7B×7KÑ7KÈ[×MdÑMd¨˜Kð *Ð5¸k×>PÓ>PàŸ™¸[Ð=T k×&9Ò&9ÐZeÔfà—~‘~Ð1ØŸ™Õ(à)/Ö? °4·;³;šÐ?�Ð?ÙØ/3�D—L‘L §¡Ò,ÙØ—L‘L×$Ñ$ U§Z¡Z°Õ6÷1	7ð 	7øð" —~‘~Ð1ØŸ™Õ(ð 2üò @ø¨Ö? °4·;³;šÑ?ùÒ?�Ð?ÙØ/3�D—L‘L §¡Ò,ÙØ—L‘L×$Ñ$ U§Z¡Z°Õ6ð ú÷/	7ð 	7úsn   �G9¤A	G9Á/AE&Ã E&ÃFÃ<G9Ä FÄFÄAG9Å&FÆFÆG9ÆG6ÆF)
Æ$F)
Æ(AG6Ç6G9Ç9Hr.   )r   r   r   r	   r    r	   r"   r!   r$   r#   r/   r   )rI   r   r/   r0   )	r1   r2   r3   r4   r>   r   r@   rJ   r+   r6   r   r   r8   r8   (   s=   „ ÙMó'ð ñó ðð 7;ñØ3ðØ?Oóô7r   r8   )r   r	   r   r	   r/   r(   )r4   Ú
__future__r   r;   Úcollections.abcr   r   Ú
contextlibr   Údataclassesr   r   Útypingr	   r   r   r8   Úreplacement_coordinatorr6   r   r   ú<module>rY      sR   ðñõ #ã ß .Ý %ß (Ý óð ÷'ð 'ó ð'÷ 47ñ 47ñn 1Ó2Ñ r   