Ë
    uPêj¦'  ã                   óL   — d Z ddlmZ ddlmZ ddlmZmZmZm	Z	  G d„ d«      Z
y)zù
Policy Resolver - Resolves final guardrail list from policies.

Handles:
- Inheritance chain resolution (inherit with add/remove)
- Applying add/remove guardrails
- Evaluating model conditions
- Combining guardrails from multiple matching policies
é    )ÚFinal)Úverbose_proxy_logger)ÚGuardrailPipelineÚPolicyÚPolicyMatchContextÚResolvedPolicyc                   óš  — e Zd ZdZe	 ddedeeef   dee   dz  de	e   fd„«       Z
e	 ddedeeef   dedz  defd	„«       Ze	 	 ddedeeef   dz  d
e	e   dz  de	e   fd„«       Ze	 	 ddedeeef   dz  d
e	e   dz  de	eeef      fd„«       Zede	eeef      dee   fd„«       Ze	 	 ddeeef   dz  dedz  deeef   fd„«       Zy)ÚPolicyResolverz�
    Resolves the final list of guardrails from policies.

    Handles:
    - Inheritance chains with add/remove operations
    - Model-based conditions
    NÚpolicy_nameÚpoliciesÚvisitedÚreturnc                 ó  — |€
t        «       }| |v rt        j                  d| «       g S |j                  | «      }|€g S |j	                  | «       |j
                  r(t        j                  |j
                  ||¬«      }|| gz   S | gS )aX  
        Get the inheritance chain for a policy (from root to policy).

        Args:
            policy_name: Name of the policy
            policies: Dictionary of all policies
            visited: Set of visited policies (for cycle detection)

        Returns:
            List of policy names from root ancestor to the given policy
        z-Circular inheritance detected for policy '%s')r   r   r   )Úsetr   ÚwarningÚgetÚaddÚinheritr
   Úresolve_inheritance_chain)r   r   r   ÚpolicyÚparent_chains        úp/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/policy_engine/policy_resolver.pyr   z(PolicyResolver.resolve_inheritance_chain   s’   € ð" ˆ?Ü“eˆGà˜'Ñ!Ü ×(Ñ(Ð)XÐZeÔfØˆIà Ÿ™ [Ó1ˆØˆ>ØˆIà�‰�KÔ à�>Š>Ü"0×"JÑ"JØ"ŸN™N°XÀwð #Kó #ˆLð   ; -Ñ/Ð/àˆ}Ðó    Úcontextc                 óø  — ddl m} t        j                  | |¬«      }t	        «       }|D ]¸  }|j                  |«      }|€Œ|�@|j                  �4|j                  |j                  |¬«      st        j                  d|«       ŒY|j                  j                  «       D ]  }|j                  |«       Œ |j                  j                  «       D ]  }|j                  |«       Œ Œº t        | t!        |«      |¬«      S )a  
        Resolve the final guardrails for a single policy, including inheritance.

        This method:
        1. Resolves the inheritance chain
        2. Applies add/remove from each policy in the chain
        3. Evaluates model conditions (if context provided)

        Args:
            policy_name: Name of the policy to resolve
            policies: Dictionary of all policies
            context: Optional request context for evaluating conditions

        Returns:
            ResolvedPolicy with final guardrails list
        r   )ÚConditionEvaluator)r   r   )Ú	conditionr   z8Policy '%s' condition did not match, skipping guardrails)r   Ú
guardrailsÚinheritance_chain)Ú/litellm.proxy.policy_engine.condition_evaluatorr   r
   r   r   r   r   Úevaluater   Údebugr   Úget_addr   Ú
get_removeÚdiscardr   Úlist)	r   r   r   r   r   r   Úchain_policy_namer   Ú	guardrails	            r   Úresolve_policy_guardrailsz(PolicyResolver.resolve_policy_guardrailsE   s  € õ, 	Wä#1×#KÑ#KÐXcÐnvÐ#KÓ#wÐô '*£eˆ
ð "3ò 	.ÐØ—\‘\Ð"3Ó4ˆFØˆ~Øð Ð" v×'7Ñ'7Ð'CØ)×2Ñ2Ø$×.Ñ.Ø#ð 3ô ô )×.Ñ.ØRÐTeôð ð $×.Ñ.×6Ñ6Ó8ò *�	Ø—‘˜yÕ)ð*ð $×.Ñ.×9Ñ9Ó;ò .�	Ø×"Ñ" 9Õ-ñ.ð+	.ô0 Ø#Ü˜JÓ'Ø/ô
ð 	
r   Úpolicy_namesc                 ó   — ddl m} ddlm} |€) |«       }|j	                  «       sg S |j                  «       }|�|n|j                  | ¬«      }|s8t        j                  d| j                  | j                  | j                  «       g S t        «       }|D ]V  }t        j                  ||| ¬«      }	|j                  |	j                   «       t        j                  d||	j                   «       ŒX t#        |«      }
t        j                  d|
«       |
S )	ax  
        Resolve the final list of guardrails for a request context.

        This:
        1. Finds all policies that match the context via policy_attachments (or policy_names if provided)
        2. Resolves each policy's guardrails (including inheritance)
        3. Evaluates model conditions
        4. Combines all guardrails (union)

        Args:
            context: The request context
            policies: Dictionary of all policies (if None, uses global registry)
            policy_names: If provided, use this list instead of attachment matching

        Returns:
            List of guardrail names to apply
        r   ©ÚPolicyMatcher©Úget_policy_registry©r   z@No policies match context: team_alias=%s, key_alias=%s, model=%s©r   r   r   z&Policy '%s' contributes guardrails: %sz Final guardrails for context: %s)Ú*litellm.proxy.policy_engine.policy_matcherr-   Ú+litellm.proxy.policy_engine.policy_registryr/   Úis_initializedÚget_all_policiesÚget_matching_policiesr   r"   Ú
team_aliasÚ	key_aliasÚmodelr   r
   r)   Úupdater   r&   )r   r   r*   r-   r/   ÚregistryÚmatching_policy_namesÚall_guardrailsr   ÚresolvedÚresults              r   Úresolve_guardrails_for_contextz-PolicyResolver.resolve_guardrails_for_context�   s  € õ. 	MÝSàÐÙ1Ó3ˆHØ×*Ñ*Ô,Ø�	Ø×0Ñ0Ó2ˆHð )Ð4‰L¸-×:]Ñ:]ÐfmÐ:]Ó:nð 	ñ %Ü ×&Ñ&ØRØ×"Ñ"Ø×!Ñ!Ø—‘ô	ð ˆIô +.«%ˆà0ò 	sˆKÜ%×?Ñ?Ø'Ø!Øð @ó ˆHð
 ×!Ñ! (×"5Ñ"5Ô6Ü ×&Ñ&Ð'OÐQ\Ð^f×^qÑ^qÕrð	sô ˜^Ó,ˆÜ×"Ñ"Ð#EÀvÔNàˆr   c                 óž  — ddl m} ddlm} |€) |«       }|j	                  «       sg S |j                  «       }|�|n|j                  | ¬«      }|sg S g }|D ]t  }|j                  |«      }	|	€Œ|	j                  €Œ$|j                  ||	j                  f«       t        j                  d|t        |	j                  j                  «      «       Œv |S )aJ  
        Resolve pipelines from matching policies for a request context.

        Returns (policy_name, pipeline) tuples for policies that have pipelines.
        Guardrails managed by pipelines should be excluded from the flat
        guardrails list to avoid double execution.

        Args:
            context: The request context
            policies: Dictionary of all policies (if None, uses global registry)
            policy_names: If provided, use this list instead of attachment matching

        Returns:
            List of (policy_name, GuardrailPipeline) tuples
        r   r,   r.   r0   z&Policy '%s' has pipeline with %s steps)r2   r-   r3   r/   r4   r5   r6   r   ÚpipelineÚappendr   r"   ÚlenÚsteps)
r   r   r*   r-   r/   r;   r<   Ú	pipelinesr   r   s
             r   Úresolve_pipelines_for_contextz,PolicyResolver.resolve_pipelines_for_contextÀ   sÙ   € õ* 	MÝSàÐÙ1Ó3ˆHØ×*Ñ*Ô,Ø�	Ø×0Ñ0Ó2ˆHð )Ð4‰L¸-×:]Ñ:]ÐfmÐ:]Ó:nð 	ñ %ØˆIà@Bˆ	Ø0ò 	ˆKØ—\‘\ +Ó.ˆFØˆ~ØØ�‰Ñ*Ø× Ñ  +¨v¯©Ð!?Ô@Ü$×*Ñ*Ø<¸kÌ3ÈvÏÉ×OdÑOdÓKeõð	ð Ðr   rF   c                 ó†   — t        «       }| D ]1  \  }}|j                  D ]  }|j                  |j                  «       Œ Œ3 |S )z–
        Get the set of guardrail names managed by pipelines.

        These guardrails should be excluded from normal independent execution.
        )r   rE   r   r(   )rF   ÚmanagedÚ_policy_namerB   Ústeps        r   Úget_pipeline_managed_guardrailsz.PolicyResolver.get_pipeline_managed_guardrailsñ   sI   € ô $'£5ˆØ&/ò 	,Ñ"ˆL˜(Ø Ÿ™ò ,�Ø—‘˜DŸN™NÕ+ñ,ð	,ð ˆr   c                 ó°   — ddl m} | €) |«       }|j                  «       si S |j                  «       } i }| D ]  }t        j                  || |¬«      ||<   Œ |S )a…  
        Resolve all policies and return their final guardrails.

        Useful for debugging and displaying policy configurations.

        Args:
            policies: Dictionary of all policies (if None, uses global registry)
            context: Optional context for evaluating conditions

        Returns:
            Dictionary mapping policy names to ResolvedPolicy objects
        r   r.   r1   )r3   r/   r4   r5   r
   r)   )r   r   r/   r;   r>   r   s         r   Úget_all_resolved_policiesz(PolicyResolver.get_all_resolved_policies   st   € õ" 	TàÐÙ1Ó3ˆHØ×*Ñ*Ô,Ø�	Ø×0Ñ0Ó2ˆHà57ˆà#ò 	ˆKÜ$2×$LÑ$LØ'Ø!Øð %Mó %ˆH�[Ò!ð	ð ˆr   )N)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstaticmethodÚstrÚdictr   r   r&   r   r   r   r)   r@   Útupler   rG   rL   rN   © r   r   r
   r
      só  „ ñð ð $(ñ#Øð#à�s˜F�{Ñ#ð#ð �S‘˜D‘ð#ð 
ˆc‰ò	#ó ð#ðJ ð .2ñ9
Øð9
à�s˜F�{Ñ#ð9
ð $ dÑ*ð9
ð 
ò	9
ó ð9
ðv ð .2Ø)-ñ<Ø#ð<à�s˜F�{Ñ# dÑ*ð<ð ˜3‘i $Ñ&ð<ð 
ˆc‰ò	<ó ð<ð| ð .2Ø)-ñ.Ø#ð.à�s˜F�{Ñ# dÑ*ð.ð ˜3‘i $Ñ&ð.ð 
ˆe�CÐ*Ð*Ñ+Ñ	,ò	.ó ð.ð` ðØ˜˜cÐ#4Ð4Ñ5Ñ6ðà	ˆS‰òó ðð à-1Ø-1ñ!Ø�s˜F�{Ñ# dÑ*ð!à# dÑ*ð!ð 
ˆc�>Ð!Ñ	"ò!ó ñ!r   r
   N)rR   Útypingr   Úlitellm._loggingr   Ú!litellm.types.proxy.policy_enginer   r   r   r   r
   rW   r   r   ú<module>r[      s(   ðñõ å 1÷ó ÷Lò Lr   