
    .cj	                    B    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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/ice/.hermes/hermes-agent/hermes_cli/route_identity.py	<genexpr>z+normalize_route_base_url.<locals>.<genexpr>   s-      
-
-3t99
-
-
-
-
-
-    ?#   r   %@[:]N>   httpP   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   
hn"

C r

-
-
-
-
--- 
3!2!21!55#?} 	H 	J$$&&(??$NN322MGTmmoo....DD>>##D{z"   


 %%c1--b1JS!! SD[[4{{{VTN2PPP
fm-&&sA..q1::D::;D}}S "5 CRCyVT4rBCCJ 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)zTrue 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     +1r2288:: ,L4FB0G0G0M0M0O0OOOt
<<<<<<&&	
 
 	
    tts   A$ $
A21A2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    r   r   <module>rL      sz    G G " " " " " "       - - - - - - - -& & & &R     r   