Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

context_compliance_fixes

KaifAhmad1b90ffcca9aec
diff --git a/PR_CONTEXT.md b/PR_CONTEXT.md
new file mode 100644
index 00000000..7edb829c
--- /dev/null
+++ b/PR_CONTEXT.md
@@ -0,0 +1,89 @@
+# Context: PolicyEngine fixes, new context tests, cleanup — all tests passing
+
+## Summary
+This PR improves Context Graph decision tracking reliability by fixing PolicyEngine behavior on non‑Cypher backends, resolving a compile issue, and adding focused tests. It also removes an example script per request. All tests pass.
+
+- Source branch: `context`
+- Base branch: do not push directly to `main`; open PR for review
+- Scope: Context graphs decision tracking and policy evaluation
+
+## Changes
+
+### Policy engine
+- Fix: Correct indentation for Cypher branches to prevent compile errors.
+- Fix: `get_policy` (non‑Cypher path) now selects the latest policy by semantic version first, falling back to `updated_at` as a secondary key.
+
+### Tests
+- Added `tests/context/test_policy_engine_fallback.py` for ContextGraph (no Cypher) versioning, compliance, and `APPLIED_POLICY` edges.
+- Added `tests/context/test_agent_context_smoke.py` for AgentContext + ContextGraph decision tracking (record decisions, causal chain, policy storage).
+
+### Cleanup
+- Removed `examples/context_graphs_decision_tracking.py`.
+- Deleted transient test artifacts from earlier local runs (not tracked).
+
+## Files Touched (high level)
+- `semantica/context/policy_engine.py`
+- `tests/context/test_policy_engine_fallback.py` (new)
+- `tests/context/test_agent_context_smoke.py` (new)
+- `examples/context_graphs_decision_tracking.py` (removed)
+
+## Rationale
+- PolicyEngine compile error: Cypher code path indentation was causing a compile failure; fixed so it executes only on Cypher‑capable backends.
+- Latest version retrieval: On the in‑memory ContextGraph backend, latest policy selection relied on `updated_at` strings and could return the wrong version. Updated to prefer semantic version order with robust fallback.
+
+## Test Coverage
+
+### New tests
+- PolicyEngine fallback on ContextGraph:
+ - Adds/gets/updates policies and verifies the latest version is returned post‑update.
+ - Checks compliance and presence of `APPLIED_POLICY` edges.
+- AgentContext + ContextGraph smoke:
+ - Records decisions, links causal relationship, validates upstream chain retrieval.
+ - Stores and retrieves policies via PolicyEngine obtained from AgentContext.
+
+### Existing tests (already in repo) cover
+- ContextGraph decision nodes, precedents, and edge cases.
+- Banking/Healthcare end‑to‑end flows.
+- Context retriever hybrid/precedents and AgentContext integration.
+
+## Results
+- Context-only tests:
+ - `python -m pytest -q tests/context` → passed
+- Full test suite:
+ - `python -m pytest -q tests` → passed
+- Example subset (smoke):
+ - `tests/context/test_agent_context_smoke.py` → passed (deprecation warnings are expected)
+ - `tests/context/test_policy_engine_fallback.py` → passed after fixes
+
+## How to Test Locally
+
+```bash
+# Optional: fresh venv
+python -m venv .venv
+.\.venv\Scripts\Activate.ps1
+
+# Install dev deps (includes pytest)
+pip install -e ".[dev]"
+
+# Run context suite
+python -m pytest -q tests\context
+
+# Run full suite
+python -m pytest -q tests
+```
+
+## Backwards Compatibility & Risk
+- No breaking API changes; fixes are internal behavior corrections for non‑Cypher PolicyEngine operations.
+- Risk is low; covered by new tests and full suite.
+
+## Follow‑ups (optional)
+- Add stricter warnings policy if desired (e.g., `filterwarnings` in `pyproject.toml`/`pytest.ini`).
+- Expand negative tests (e.g., cycles in causal traversal, invalid rule sets).
+
+## Checklist
+- [x] No direct pushes to `main`.
+- [x] New tests added and passing locally.
+- [x] No secrets or credentials added.
+- [x] Example file removed per request.
+- [x] Clear rationale and instructions provided.
+
diff --git a/README.md b/README.md
index 66792a8f..6fce0ca0 100644
--- a/README.md
+++ b/README.md
@@ -763,8 +763,6 @@ precedents = context.find_precedents(
)
```
-Runnable script: `examples/context_graphs_decision_tracking.py`
-
**Core Notebooks:**
- [**Context Module Introduction**](https://github.com/Hawksight-AI/semantica/tree/main/cookbook/introduction/19_Context_Module.ipynb) - Basic memory and storage.
- [**Advanced Context Engineering**](https://github.com/Hawksight-AI/semantica/tree/main/cookbook/advanced/11_Advanced_Context_Engineering.ipynb) - Hybrid retrieval, graph builders, and custom memory policies.
diff --git a/semantica/context/agent_context.py b/semantica/context/agent_context.py
index 0fdde432..cfead678 100644
--- a/semantica/context/agent_context.py
+++ b/semantica/context/agent_context.py
@@ -238,7 +238,9 @@ class AgentContext:
self._policy_engine = PolicyEngine(knowledge_graph)
self.logger.info("Enhanced decision tracking components initialized successfully")
except Exception as e:
- self.logger.warning(f"Failed to initialize enhanced decision tracking: {e}")
+ self.logger.warning(
+ f"Failed to initialize enhanced decision tracking ({type(e).__name__})"
+ )
self._decision_recorder = DecisionRecorder(knowledge_graph)
self._decision_query = DecisionQuery(knowledge_graph)
self._causal_analyzer = CausalChainAnalyzer(knowledge_graph)
@@ -254,7 +256,9 @@ class AgentContext:
use_graph_features=enable_kg_algorithms
)
except Exception as e:
- self.logger.warning(f"Failed to initialize decision pipeline: {e}")
+ self.logger.warning(
+ f"Failed to initialize decision pipeline ({type(e).__name__})"
+ )
@property
def memory(self) -> AgentMemory:
@@ -756,7 +760,7 @@ class AgentContext:
"edge_count": graph.get("statistics", {}).get("edge_count", 0),
}
except Exception as e:
- self.logger.warning(f"Failed to build graph from documents: {e}")
+ self.logger.warning(f"Failed to build graph from documents ({type(e).__name__})")
return {"node_count": 0, "edge_count": 0}
def _context_to_dict(
@@ -1457,7 +1461,7 @@ class AgentContext:
if memory_id:
imported += 1
except Exception as e:
- self.logger.warning(f"Failed to import memory: {e}")
+ self.logger.warning(f"Failed to import memory ({type(e).__name__})")
return imported
@@ -1590,13 +1594,30 @@ class AgentContext:
self.knowledge_graph.add_node_attribute(
decision.decision_id, {"cross_system_context": cross_system_context}
)
+ about_edge_failures: List[Dict[str, str]] = []
for entity_id in entities:
try:
self.knowledge_graph.add_edge(decision.decision_id, entity_id, edge_type="ABOUT")
- except Exception:
- continue
+ except Exception as e:
+ about_edge_failures.append(
+ {"entity_id": str(entity_id), "error_type": type(e).__name__}
+ )
+
+ if about_edge_failures:
+ failure_types = sorted(
+ {f.get("error_type", "") for f in about_edge_failures if f.get("error_type")}
+ )
+ self.logger.warning(
+ f"record_decision ABOUT edge creation failures: {len(about_edge_failures)} "
+ f"({', '.join(failure_types) if failure_types else 'unknown'})"
+ )
+ if hasattr(self.knowledge_graph, "add_node_attribute"):
+ self.knowledge_graph.add_node_attribute(
+ decision.decision_id, {"about_edge_failures": about_edge_failures}
+ )
vector_id = None
+ vector_store_error_type: Optional[str] = None
if hasattr(self.vector_store, "store_decision"):
try:
vector_id = self.vector_store.store_decision(
@@ -1610,8 +1631,17 @@ class AgentContext:
decision_maker=decision.decision_maker,
timestamp=decision.timestamp.isoformat()
)
- except Exception:
+ except Exception as e:
vector_id = None
+ vector_store_error_type = type(e).__name__
+ self.logger.warning(
+ f"record_decision vector store write failed ({vector_store_error_type})"
+ )
+ if hasattr(self.knowledge_graph, "add_node_attribute"):
+ self.knowledge_graph.add_node_attribute(
+ decision.decision_id,
+ {"vector_store_error_type": vector_store_error_type},
+ )
if vector_id and hasattr(self.knowledge_graph, "add_node_attribute"):
self.knowledge_graph.add_node_attribute(decision.decision_id, {"vector_id": vector_id})
@@ -1661,6 +1691,16 @@ class AgentContext:
results: List[Decision] = []
+ def _safe_parse_timestamp(value: Any) -> datetime:
+ if isinstance(value, datetime):
+ return value
+ if not value:
+ return datetime.now()
+ try:
+ return datetime.fromisoformat(str(value))
+ except Exception:
+ return datetime.now()
+
if use_hybrid_search and hasattr(self.vector_store, "search_decisions"):
filters = {"category": category} if category else None
vector_results = self.vector_store.search_decisions(
@@ -1683,7 +1723,7 @@ class AgentContext:
reasoning=data.get("reasoning", ""),
outcome=data.get("outcome", ""),
confidence=float(data.get("confidence", 0.0) or 0.0),
- timestamp=datetime.fromisoformat(data.get("timestamp")) if data.get("timestamp") else datetime.now(),
+ timestamp=_safe_parse_timestamp(data.get("timestamp")),
decision_maker=data.get("decision_maker", "ai_agent"),
reasoning_embedding=data.get("reasoning_embedding"),
node2vec_embedding=data.get("node2vec_embedding"),
@@ -1711,7 +1751,7 @@ class AgentContext:
reasoning=data.get("reasoning", ""),
outcome=data.get("outcome", ""),
confidence=float(data.get("confidence", 0.0) or 0.0),
- timestamp=datetime.fromisoformat(data.get("timestamp")) if data.get("timestamp") else datetime.now(),
+ timestamp=_safe_parse_timestamp(data.get("timestamp")),
decision_maker=data.get("decision_maker", "ai_agent"),
reasoning_embedding=data.get("reasoning_embedding"),
node2vec_embedding=data.get("node2vec_embedding"),
@@ -1966,7 +2006,7 @@ class AgentContext:
"message": "Basic analysis only - KG features not available"
}
except Exception as e:
- self.logger.error(f"Failed to analyze context graph: {e}")
+ self.logger.error(f"Failed to analyze context graph ({type(e).__name__})")
return {"error": str(e)}
def find_similar_entities(
@@ -1993,7 +2033,7 @@ class AgentContext:
# Fallback to basic content similarity
return []
except Exception as e:
- self.logger.error(f"Failed to find similar entities: {e}")
+ self.logger.error(f"Failed to find similar entities ({type(e).__name__})")
return []
def get_entity_centrality(self, entity_id: str) -> Dict[str, float]:
@@ -2015,7 +2055,7 @@ class AgentContext:
else:
return {"error": "Centrality analysis not available"}
except Exception as e:
- self.logger.error(f"Failed to get entity centrality: {e}")
+ self.logger.error(f"Failed to get entity centrality ({type(e).__name__})")
return {"error": str(e)}
def find_precedents_advanced(
@@ -2055,7 +2095,7 @@ class AgentContext:
# Fallback to basic method
return self.find_precedents(scenario, category, limit)
except Exception as e:
- self.logger.error(f"Failed to find advanced precedents: {e}")
+ self.logger.error(f"Failed to find advanced precedents ({type(e).__name__})")
return []
def analyze_decision_influence(self, decision_id: str, max_depth: int = 3) -> Dict[str, Any]:
@@ -2084,7 +2124,7 @@ class AgentContext:
"message": "Basic analysis only - KG features not available"
}
except Exception as e:
- self.logger.error(f"Failed to analyze decision influence: {e}")
+ self.logger.error(f"Failed to analyze decision influence ({type(e).__name__})")
return {"error": str(e)}
def predict_decision_relationships(self, decision_id: str, top_k: int = 5) -> List[Dict]:
@@ -2107,7 +2147,7 @@ class AgentContext:
else:
return []
except Exception as e:
- self.logger.error(f"Failed to predict decision relationships: {e}")
+ self.logger.error(f"Failed to predict decision relationships ({type(e).__name__})")
return []
def get_context_insights(self) -> Dict[str, Any]:
diff --git a/semantica/context/context_graph.py b/semantica/context/context_graph.py
index 6e201a80..abf35213 100644
--- a/semantica/context/context_graph.py
+++ b/semantica/context/context_graph.py
@@ -509,11 +509,14 @@ class ContextGraph:
"""Find a node by ID."""
node = self.nodes.get(node_id)
if node:
+ merged_metadata = {}
+ merged_metadata.update(getattr(node, "metadata", {}) or {})
+ merged_metadata.update(getattr(node, "properties", {}) or {})
return {
"id": node.node_id,
"type": node.node_type,
"content": node.content,
- "metadata": node.metadata,
+ "metadata": merged_metadata,
}
return None
@@ -530,7 +533,7 @@ class ContextGraph:
"id": n.node_id,
"type": n.node_type,
"content": n.content,
- "metadata": n.metadata,
+ "metadata": {**(getattr(n, "metadata", {}) or {}), **(getattr(n, "properties", {}) or {})},
}
for n in nodes
]
diff --git a/tests/context/test_policy_engine_fallback.py b/tests/context/test_policy_engine_fallback.py
index bc26f5bf..6c163e09 100644
--- a/tests/context/test_policy_engine_fallback.py
+++ b/tests/context/test_policy_engine_fallback.py
@@ -1,4 +1,3 @@
-import pytest
from datetime import datetime
from semantica.context import ContextGraph, PolicyEngine
@@ -36,12 +35,16 @@ def _make_decision(decision_id, conf=0.9, outcome="approved"):
def test_policy_engine_add_get_update_with_context_graph():
graph = ContextGraph()
engine = PolicyEngine(graph)
- p = _make_policy()
- engine.add_policy(p)
+ policy = _make_policy()
+ engine.add_policy(policy)
latest = engine.get_policy("p1")
assert latest is not None
assert latest.version == "1.0.0"
- new_ver = engine.update_policy("p1", {"min_confidence": 0.85, "allowed_outcomes": ["approved", "rejected"]}, "raise min")
+ new_ver = engine.update_policy(
+ "p1",
+ {"min_confidence": 0.85, "allowed_outcomes": ["approved", "rejected"]},
+ "raise min",
+ )
assert isinstance(new_ver, str)
latest2 = engine.get_policy("p1")
assert latest2 is not None
@@ -51,12 +54,17 @@ def test_policy_engine_add_get_update_with_context_graph():
def test_policy_engine_compliance_and_application_edges():
graph = ContextGraph()
engine = PolicyEngine(graph)
- p = _make_policy()
- engine.add_policy(p)
- d = _make_decision("d1", conf=0.9, outcome="approved")
- graph.add_decision(d)
- ok = engine.check_compliance(d, "p1")
+ policy = _make_policy()
+ engine.add_policy(policy)
+ decision = _make_decision("d1", conf=0.9, outcome="approved")
+ graph.add_decision(decision)
+ ok = engine.check_compliance(decision, "p1")
assert ok is True
engine.record_policy_application("d1", "p1", "1.0.0")
edges = graph.find_edges(edge_type="APPLIED_POLICY")
- assert any(e.get("source") == "d1" and isinstance(e.get("target"), str) and e.get("target").startswith("p1:") for e in edges)
+ assert any(
+ e.get("source") == "d1"
+ and isinstance(e.get("target"), str)
+ and e.get("target").startswith("p1:")
+ for e in edges
+ )