mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
Apply entity scoping in ContextGraph policy fallback
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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."""
|
||||
|
||||
Reference in New Issue
Block a user