§
    øžyj¿  ã                  óJ   — d Z ddlmZ ddlmZ ddlmZmZ dd	„Zdd„Z	dd„Z
dS )zAFail-closed URL identity normalization for model/provider routes.é    )Úannotations)ÚAny)ÚurlsplitÚ
urlunsplitÚbase_urlr   ÚreturnÚstrc                óŠ  — t          | pd¦  «        }|sdS t          d„ |D ¦   «         ¦  «        r|S d|                     dd¦  «        d         v }	 t          |¦  «        }|j        }|j        r|s|S |j                             ¦   «         }d|v r3|                     dd¦  «        \  }}|                     ¦   «         › d|› �}n|                     ¦   «         }|j        }	n# t          t          f$ r |cY S w xY w|j
                             dd¦  «        d	         }
|
                     d
¦  «        sd|v rd
|› d�}|	�||	fdvr|› d|	› �}d|j
        v r&|j
                             dd¦  «        d         › d|› �}|j        }|                     d¦  «        r|s
|dd	…         }t          ||||j        df¦  «        }|r|j        s|dz  }|S )z<Canonicalize only proven-equivalent endpoint URL components.Ú c              3  ó<   K  — | ]}t          |¦  «        d k    V — ŒdS )é    N)Úord)Ú.0Úchars     ú?/home/ragecks/.hermes/hermes-agent/hermes_cli/route_identity.pyú	<genexpr>z+normalize_route_base_url.<locals>.<genexpr>   s-   è è € Ð
-Ð
- �3ˆt‰9Œ9˜ÒÐ
-Ð
-Ð
-Ð
-Ð
-Ð
-ó    ú?ú#é   r   ú%ú@éÿÿÿÿú[ú:ú]N>   ©ÚhttpéP   ©Úhttpsi»  ú/)r	   ÚanyÚsplitr   ÚhostnameÚschemeÚlowerÚportÚ	TypeErrorÚ
ValueErrorÚnetlocÚrsplitÚ
startswithÚpathÚendswithr   Úquery)r   ÚrawÚhad_query_delimiterÚparsedr%   r&   ÚaddressÚzoneÚhostr(   Ú
route_hostr.   Ú
normalizeds                r   Únormalize_route_base_urlr9   	   s3  € å
ˆhˆn˜"Ñ
Ô
€CØð ØˆrÝ
Ð
-Ð
-¨Ð
-Ñ
-Ô
-Ñ-Ô-ð Øˆ
Ø §¢¨3°Ñ!2Ô!2°1Ô!5Ð5ÐðÝ˜#‘”ˆØ”?ˆØŒ}ð 	 Hð 	ØˆJØ”×$Ò$Ñ&Ô&ˆØ�(ˆ?ˆ?Ø$ŸNšN¨3°Ñ2Ô2‰MˆG�TØ—m’m‘o”oÐ.Ð.¨Ð.Ð.ˆDˆDà—>’>Ñ#Ô#ˆDØŒ{ˆˆøÝ•zÐ"ð ð ð Øˆ
ˆ
ˆ
ðøøøð ”×%Ò% c¨1Ñ-Ô-¨bÔ1€JØ×Ò˜SÑ!Ô!ð  S¨D [ [Ø�4ˆ{ˆ{ˆ{ˆØÐ˜V T˜NÐ2PÐPÐPØÐÐ˜ÐÐˆØ
ˆfŒmÐÐØ”-×&Ò& s¨AÑ.Ô.¨qÔ1Ð:Ð:°DÐ:Ð:ˆàŒ;€DØ‡}‚}�SÑÔð Ð"5ð Ø�C�R�CŒyˆå˜V T¨4°´¸rÐBÑCÔC€JØð  6¤<ð Ø�cÑˆ
ØÐs   Á C Á1A+C ÃC3Ã2C3Úconfigured_modelÚactive_modelÚconfigured_base_urlÚactive_base_urlÚconfigured_providerÚactive_providerÚboolc                óê   — t          | pd¦  «                             ¦   «         } | r)| t          |pd¦  «                             ¦   «         k    rdS 	 ddlm}  |||||¦  «        S # t          $ r Y dS w xY w)zúTrue when a configured ``model.context_length`` pin no longer matches its runtime route.

    Fail-closed: any error during route comparison returns ``True`` (drop the pin)
    so a stale window never silently inflates the compression threshold.
    r   Tr   )Ú_context_route_mismatch)r	   ÚstripÚagent.agent_initrB   Ú	Exception)r:   r;   r<   r=   r>   r?   rB   s          r   Úshould_clear_context_pinrF   2   s±   € õ Ð+Ð1¨rÑ2Ô2×8Ò8Ñ:Ô:ÐØð Ð,µ°LÐ4FÀBÑ0GÔ0G×0MÒ0MÑ0OÔ0OÒOÐOØˆtð
Ø<Ð<Ð<Ð<Ð<Ð<à&Ð&ØØØØñ	
ô 
ð 	
øõ ð ð ð Øˆtˆtðøøøs   ÁA$ Á$
A2Á1A2c           	   ƒ  óZ   K  — ddl }|                     t          | |||||¦  «        ƒ d{V —†S )uV  Async wrapper for ``should_clear_context_pin``.

    Offloads the route comparison to a worker thread so async gateway
    handlers never run it on the event loop â€” the resolution chain is
    cache-only (``allow_network=False``) but can still do cold-start disk
    I/O. Shares all logic with the sync version â€” no code duplication.
    r   N)ÚasyncioÚ	to_threadrF   )r:   r;   r<   r=   r>   r?   rH   s          r   Úshould_clear_context_pin_asyncrJ   O   s\   è è € ð €N€N€Nà×"Ò"Ý ØØØØØØñô ð ð ð ð ð ð ð r   N)r   r   r   r	   )r:   r   r;   r   r<   r   r=   r   r>   r   r?   r   r   r@   )Ú__doc__Ú
__future__r   Útypingr   Úurllib.parser   r   r9   rF   rJ   © r   r   ú<module>rP      sŽ   ðØ GÐ Gà "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -ð&ð &ð &ð &ðRð ð ð ð:ð ð ð ð ð r   