From ba8404d82ad36189ead4ee86b3bceceb9ff24186 Mon Sep 17 00:00:00 2001 From: yucheng Date: Wed, 23 Sep 2026 01:59:31 +0000 Subject: [PATCH] fix(policy_engine): log ancestor admissions once per attachment scan Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/proxy/policy_engine/policy_matcher.py | 76 +++++++++++-------- .../policy_engine/test_policy_matcher.py | 27 ++++--- 2 files changed, 59 insertions(+), 44 deletions(-) diff --git a/litellm/proxy/policy_engine/policy_matcher.py b/litellm/proxy/policy_engine/policy_matcher.py index 22da1bcfb5a..2ea2def8331 100644 --- a/litellm/proxy/policy_engine/policy_matcher.py +++ b/litellm/proxy/policy_engine/policy_matcher.py @@ -137,14 +137,46 @@ class PolicyMatcher: context: PolicyMatchContext, policies: dict[str, Policy] | None = None, ) -> Callable[[str], bool]: - """Predicate telling whether a policy exists and any policy in its inheritance chain applies to the context.""" + """ + Predicate telling whether a policy exists and any policy in its + inheritance chain applies to the context. Admissions where the + policy's own condition missed but an ancestor applies are logged at + INFO, once per attachment scan. + """ resolved: Final = policies if policies is not None else PolicyMatcher._registry_policies() - return lambda policy_name: bool( - PolicyMatcher.get_policies_with_matching_conditions( - policy_names=(policy_name,), - context=context, - policies=resolved, + + def applies(policy_name: str) -> bool: + applying: Final = PolicyMatcher._applying_chain_members( + policy_name=policy_name, context=context, policies=resolved ) + if applying and policy_name not in applying: + verbose_proxy_logger.info( + "Policy '%s' applied through ancestor '%s' although its own condition did not match " + "(team_alias=%s, key_alias=%s, model=%s)", + policy_name, + applying[0], + context.team_alias, + context.key_alias, + context.model, + ) + return bool(applying) + + return applies + + @staticmethod + def _applying_chain_members( + policy_name: str, + context: PolicyMatchContext, + policies: dict[str, Policy], + ) -> tuple[str, ...]: + from litellm.proxy.policy_engine.condition_evaluator import ConditionEvaluator + + chain: Final = PolicyResolver.resolve_inheritance_chain(policy_name=policy_name, policies=policies) + return tuple( + name + for name in chain + if (policy := policies.get(name)) is not None + and (policy.condition is None or ConditionEvaluator.evaluate(policy.condition, context)) ) @staticmethod @@ -168,8 +200,7 @@ class PolicyMatcher: resolver then drops only the chain members whose own condition fails, so a child whose condition misses still contributes the guardrails of its unconditional ancestors. A missing policy resolves to an empty - chain and does not apply. Admissions where the policy's own condition - missed but an ancestor applies are logged at INFO. + chain and does not apply. Args: policy_names: List of policy names to filter @@ -179,28 +210,9 @@ class PolicyMatcher: Returns: List of policy names that apply to the context """ - from litellm.proxy.policy_engine.condition_evaluator import ConditionEvaluator - resolved: Final = policies if policies is not None else PolicyMatcher._registry_policies() - - def chain_applies(policy_name: str) -> bool: - chain: Final = PolicyResolver.resolve_inheritance_chain(policy_name=policy_name, policies=resolved) - applying: Final = tuple( - name - for name in chain - if (policy := resolved.get(name)) is not None - and (policy.condition is None or ConditionEvaluator.evaluate(policy.condition, context)) - ) - if applying and policy_name not in applying: - verbose_proxy_logger.info( - "Policy '%s' applied through ancestor '%s' although its own condition did not match " - "(team_alias=%s, key_alias=%s, model=%s)", - policy_name, - applying[0], - context.team_alias, - context.key_alias, - context.model, - ) - return bool(applying) - - return [policy_name for policy_name in policy_names if chain_applies(policy_name)] + return [ + policy_name + for policy_name in policy_names + if PolicyMatcher._applying_chain_members(policy_name, context, resolved) + ] diff --git a/tests/test_litellm/proxy/policy_engine/test_policy_matcher.py b/tests/test_litellm/proxy/policy_engine/test_policy_matcher.py index 3b27f789f31..27153e67ab5 100644 --- a/tests/test_litellm/proxy/policy_engine/test_policy_matcher.py +++ b/tests/test_litellm/proxy/policy_engine/test_policy_matcher.py @@ -392,11 +392,9 @@ class TestAncestorAdmissionLogging: def test_logs_when_admitted_through_ancestor_only(self, caplog): context: Final = PolicyMatchContext(team_alias="t", key_alias="k", model="gpt-4o") with caplog.at_level(logging.INFO, logger="LiteLLM Proxy"): - result: Final = PolicyMatcher.get_policies_with_matching_conditions( - policy_names=["child"], context=context, policies=self._chain() - ) + result: Final = PolicyMatcher.policy_applies(context, self._chain())("child") records: Final = [r for r in caplog.records if "applied through ancestor" in r.getMessage()] - assert result == ["child"] + assert result is True assert len(records) == 1 assert "applied through ancestor 'parent'" in records[0].getMessage() assert "'child'" in records[0].getMessage() @@ -404,10 +402,8 @@ class TestAncestorAdmissionLogging: def test_no_log_when_own_condition_matches(self, caplog): context: Final = PolicyMatchContext(team_alias="t", key_alias="k", model="gpt-5.5") with caplog.at_level(logging.INFO, logger="LiteLLM Proxy"): - result: Final = PolicyMatcher.get_policies_with_matching_conditions( - policy_names=["child"], context=context, policies=self._chain() - ) - assert result == ["child"] + result: Final = PolicyMatcher.policy_applies(context, self._chain())("child") + assert result is True assert not [r for r in caplog.records if "applied through ancestor" in r.getMessage()] def test_no_log_when_no_chain_member_applies(self, caplog): @@ -424,8 +420,15 @@ class TestAncestorAdmissionLogging: } context: Final = PolicyMatchContext(team_alias="t", key_alias="k", model="gpt-4o") with caplog.at_level(logging.INFO, logger="LiteLLM Proxy"): - result: Final = PolicyMatcher.get_policies_with_matching_conditions( - policy_names=["child"], context=context, policies=policies - ) - assert result == [] + result: Final = PolicyMatcher.policy_applies(context, policies)("child") + assert result is False + assert not [r for r in caplog.records if "applied through ancestor" in r.getMessage()] + + def test_condition_filter_logs_nothing(self, caplog): + context: Final = PolicyMatchContext(team_alias="t", key_alias="k", model="gpt-4o") + with caplog.at_level(logging.INFO, logger="LiteLLM Proxy"): + result: Final = PolicyMatcher.get_policies_with_matching_conditions( + policy_names=["child"], context=context, policies=self._chain() + ) + assert result == ["child"] assert not [r for r in caplog.records if "applied through ancestor" in r.getMessage()]