Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
Handle FalkorDB policy rows in applicability parsing
KaifAhmad1f9f19f343e32
diff --git a/semantica/context/policy_engine.py b/semantica/context/policy_engine.pyindex 59fabcaf..9085a6d4 100644--- a/semantica/context/policy_engine.py+++ b/semantica/context/policy_engine.py@@ -267,6 +267,12 @@ class PolicyEngine:policy_data = record.get("p") if isinstance(record, dict) else Noneif not isinstance(policy_data, dict):policy_data = record if isinstance(record, dict) else {}+ if not isinstance(policy_data, dict) or not policy_data.get("policy_id"):+ self.logger.debug(+ "Skipping malformed policy record in get_applicable_policies: "+ f"{record}"+ )+ continuepolicy = self._dict_to_policy(policy_data)if self._policy_matches_entities(policy, entities):policies.append(policy)@@ -318,7 +324,23 @@ class PolicyEngine:"""Normalize execute_query result shapes to record lists."""if isinstance(results, dict):records = results.get("records", [])- return records if isinstance(records, list) else []+ if not isinstance(records, list):+ return []++ # FalkorDB shape: {"records": [[...], ...], "header": ["col1", ...]}+ header = results.get("header")+ if (+ isinstance(header, list)+ and records+ and all(isinstance(row, list) for row in records)+ ):+ normalized: List[Dict[str, Any]] = []+ for row in records:+ row_map: Dict[str, Any] = dict(zip(header, row))+ normalized.append(row_map)+ return normalized++ return recordsif isinstance(results, list):return resultsreturn []diff --git a/tests/context/test_policy_engine.py b/tests/context/test_policy_engine.pyindex 2728a7bb..89d0ce72 100644--- a/tests/context/test_policy_engine.py+++ b/tests/context/test_policy_engine.py@@ -187,6 +187,42 @@ class TestPolicyEngine:assert len(policies) == 1assert policies[0].category == category++ def test_get_applicable_policies_falkordb_row_shape(self, policy_engine, mock_graph_store):+ """Test policy parsing when backend returns FalkorDB list rows + header."""+ category = "credit_approval"+ mock_graph_store.execute_query.return_value = {+ "records": [+ [+ {+ "policy_id": "policy_001",+ "name": "Credit Approval Policy",+ "description": "Standard credit approval rules",+ "rules": {"min_score": 650},+ "category": "credit_approval",+ "version": "1.0",+ "created_at": datetime.now().isoformat(),+ "updated_at": datetime.now().isoformat(),+ "metadata": {},+ }+ ]+ ],+ "header": ["p"],+ }++ policies = policy_engine.get_applicable_policies(category, None)++ assert len(policies) == 1+ assert policies[0].policy_id == "policy_001"++ def test_get_applicable_policies_skips_malformed_record(self, policy_engine, mock_graph_store):+ """Test malformed policy records are skipped instead of crashing."""+ category = "credit_approval"+ mock_graph_store.execute_query.return_value = [{"unexpected": "shape"}]++ policies = policy_engine.get_applicable_policies(category, None)++ assert policies == []def test_check_compliance_success(self, policy_engine, mock_graph_store):"""Test successful compliance checking."""