Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Apply entity scoping in ContextGraph policy fallback

KaifAhmad18bd4df74e19d
diff --git a/semantica/context/policy_engine.py b/semantica/context/policy_engine.py
index 9085a6d4..b4d064ee 100644
--- a/semantica/context/policy_engine.py
+++ b/semantica/context/policy_engine.py
@@ -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
diff --git a/tests/context/test_policy_engine.py b/tests/context/test_policy_engine.py
index 89d0ce72..c7152aee 100644
--- a/tests/context/test_policy_engine.py
+++ b/tests/context/test_policy_engine.py
@@ -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."""