Apply entity scoping in ContextGraph policy fallback

This commit is contained in:
KaifAhmad1
2026-02-18 12:50:08 +05:30
parent f9f19f343e
commit 8bd4df74e1
2 changed files with 48 additions and 2 deletions
+4 -2
View File
@@ -301,7 +301,7 @@ class PolicyEngine:
policies: List[Policy] = []
for data in latest_by_policy_id.values():
policies.append(self._dict_to_policy({
policy = self._dict_to_policy({
"policy_id": data.get("policy_id"),
"name": data.get("name"),
"description": data.get("description"),
@@ -311,7 +311,9 @@ class PolicyEngine:
"created_at": data.get("created_at"),
"updated_at": data.get("updated_at"),
"metadata": data.get("metadata", {})
}))
})
if self._policy_matches_entities(policy, entities):
policies.append(policy)
self.logger.info(f"Found {len(policies)} applicable policies for category {category}")
return policies
+44
View File
@@ -223,6 +223,50 @@ class TestPolicyEngine:
policies = policy_engine.get_applicable_policies(category, None)
assert policies == []
def test_get_applicable_policies_context_graph_fallback_respects_entities(self):
"""Test entity scoping is applied in find_nodes() fallback path."""
category = "credit_approval"
entities = ["customer:target"]
class _ContextGraphLike:
def find_nodes(self, node_type=None):
if node_type != "Policy":
return []
return [
{
"metadata": {
"policy_id": "policy_match",
"name": "Scoped policy",
"description": "Applies to target customer",
"rules": {},
"category": "credit_approval",
"version": "1.0",
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat(),
"metadata": {"entities": ["customer:target"]},
}
},
{
"metadata": {
"policy_id": "policy_other",
"name": "Other scoped policy",
"description": "Applies elsewhere",
"rules": {},
"category": "credit_approval",
"version": "1.0",
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat(),
"metadata": {"entities": ["customer:other"]},
}
},
]
engine = PolicyEngine(graph_store=_ContextGraphLike())
policies = engine.get_applicable_policies(category, entities)
assert len(policies) == 1
assert policies[0].policy_id == "policy_match"
def test_check_compliance_success(self, policy_engine, mock_graph_store):
"""Test successful compliance checking."""