Coverage for app / services / rules / conditions.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1""" 

2Condition evaluation — pairing a registered field with a registered operator. 

3 

4The whole of the matching logic lives here so svc-core has exactly one answer 

5to "did this condition fire?", whether the question comes from a policy rule 

6or an audit rule. 

7 

8Every failure mode returns False and logs. A rule that cannot be evaluated 

9must not match, because the alternative — treating "I don't understand this 

10condition" as "the condition holds" — makes a misconfigured block rule refuse 

11every invocation on the platform. 

12""" 

13 

14from __future__ import annotations 

15 

16import logging 

17from typing import Any 

18 

19from app.services.rules import fields as field_registry 

20from app.services.rules import operators as operator_registry 

21from app.services.rules.operators import ValueType 

22 

23logger = logging.getLogger(__name__) 

24 

25 

26def condition_matches(condition: dict[str, Any], context: dict[str, Any]) -> bool: 

27 """True when one condition block is satisfied by ``context``.""" 

28 field_key: str = condition.get("field", "") 

29 operator_key: str = condition.get("operator", "eq") 

30 expected: Any = condition.get("value", "") 

31 

32 field = field_registry.get_field(field_key) 

33 if field is None: 

34 logger.warning( 

35 "Rule condition references unknown field %r — condition will never " 

36 "match. Valid fields: %s", 

37 field_key, 

38 ", ".join(sorted(field_registry.field_keys())), 

39 ) 

40 return False 

41 

42 # ``always`` carries no comparison: it is the way to write a rule that 

43 # governs every invocation, so it must not be held to operator validity. 

44 if field.value_type is ValueType.none: 

45 return True 

46 

47 operator = operator_registry.get_operator(operator_key) 

48 if operator is None: 

49 logger.warning( 

50 "Rule condition uses unsupported operator %r — condition will never " 

51 "match. Valid operators: %s", 

52 operator_key, 

53 ", ".join(sorted(operator_registry.operator_keys())), 

54 ) 

55 return False 

56 

57 if not field_registry.supports(field, operator): 

58 logger.warning( 

59 "Rule condition pairs field %r (%s) with operator %r, which only " 

60 "applies to %s — condition will never match.", 

61 field_key, 

62 field.value_type.value, 

63 operator_key, 

64 ", ".join(sorted(t.value for t in operator.applies_to)), 

65 ) 

66 return False 

67 

68 actual = field.resolve(context) 

69 if actual is None: 

70 # Not an error: the field simply has no value in this phase or this 

71 # caller did not supply it. Silence is right — logging here would fire 

72 # on every invocation a partially-populated context touches. 

73 return False 

74 

75 return operator.evaluate(actual, expected) 

76 

77 

78def all_conditions_match( 

79 conditions: list[dict[str, Any]] | None, 

80 context: dict[str, Any], 

81) -> bool: 

82 """All conditions must match (AND semantics). Empty list → always matches.""" 

83 return all(condition_matches(c, context) for c in (conditions or []))