Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fixed qodo review

Sameer63058e0419c86438
diff --git a/semantica/vector_store/vector_store.py b/semantica/vector_store/vector_store.py
index 60740df9..3166aaf2 100644
--- a/semantica/vector_store/vector_store.py
+++ b/semantica/vector_store/vector_store.py
@@ -1009,16 +1009,23 @@ class VectorStore:
) -> Dict[str, Any]:
"""
Build decision context graph.
-
+
Args:
decision_id: Decision vector ID
depth: Context depth
include_entities: Whether to include entities
include_policies: Whether to include policies
max_hops: Maximum hops for context expansion
-
+
Returns:
- Decision context graph
+ Decision context graph dict with keys:
+ - decision_id, decision_metadata, entities, policies,
+ related_decisions, context_graph.
+ If the backend cannot retrieve the raw vector for *decision_id*
+ (e.g. a FAISS index built without ``make_direct_map``), similarity
+ enrichment is skipped, a WARNING is emitted, and the key
+ ``similarity_unavailable=True`` is added. ``related_decisions``
+ remains an empty list for schema stability.
"""
# Get decision metadata
decision_metadata = self.get_metadata(decision_id)
@@ -1053,6 +1060,13 @@ class VectorStore:
"similarity": result["score"],
"metadata": result.get("metadata", {})
})
+ else:
+ self.logger.warning(
+ "Backend cannot retrieve vector for decision '%s' — "
+ "similarity enrichment skipped.",
+ decision_id,
+ )
+ context["similarity_unavailable"] = True
return context
@@ -1065,15 +1079,20 @@ class VectorStore:
) -> Dict[str, Any]:
"""
Generate explanation for a decision.
-
+
Args:
decision_id: Decision vector ID
include_paths: Whether to include reasoning paths
include_confidence: Whether to include confidence scores
include_weights: Whether to include similarity weights
-
+
Returns:
- Decision explanation
+ Decision explanation dict. When *include_paths* is True and the
+ backend can retrieve the raw vector, ``similar_decisions`` is
+ populated. If the backend cannot retrieve the vector (e.g. a FAISS
+ index without ``make_direct_map``), a WARNING is emitted, the key
+ ``similarity_unavailable=True`` is added, and ``similar_decisions``
+ is set to ``[]`` for schema stability.
"""
decision_metadata = self.get_metadata(decision_id)
if not decision_metadata:
@@ -1100,6 +1119,14 @@ class VectorStore:
if query_vector is not None:
similar_decisions = self.search_vectors(query_vector, k=3)
explanation["similar_decisions"] = similar_decisions
+ else:
+ self.logger.warning(
+ "Backend cannot retrieve vector for decision '%s' — "
+ "similarity enrichment skipped.",
+ decision_id,
+ )
+ explanation["similarity_unavailable"] = True
+ explanation["similar_decisions"] = []
return explanation
diff --git a/tests/vector_store/test_decision_embedding_pipeline.py b/tests/vector_store/test_decision_embedding_pipeline.py
index d65fe7da..ed65236d 100644
--- a/tests/vector_store/test_decision_embedding_pipeline.py
+++ b/tests/vector_store/test_decision_embedding_pipeline.py
@@ -910,3 +910,83 @@ class TestFilterByMetadataBackendBehavior:
)
for r in results:
assert r["metadata"]["category"] == "loan"
+
+
+class TestVectorRetrievalFailureBehavior:
+ """
+ Requirement: verify that when get_vector() returns None for an existing decision
+ (e.g., FAISS without reconstruct capability), we don't silently drop similarity
+ enrichment. We should get a warning log and an explicit marker in the output.
+ """
+
+ class _StubBackend:
+ def __init__(self):
+ self.metadata = {
+ "decision_1": {
+ "scenario": "Stub Scenario",
+ "reasoning": "Stub Reasoning",
+ "outcome": "approved"
+ }
+ }
+
+ def get_metadata(self, vector_id: str):
+ return self.metadata.get(vector_id)
+
+ def get_vector(self, vector_id: str):
+ # Explicitly return None to simulate a backend that cannot reconstruct vectors
+ return None
+
+ def _make_stub_store(self):
+ vs = VectorStore(backend="inmemory", config={"dimension": 4})
+ vs.embedder = None
+ # Replace the backend store with our stub
+ vs._backend_store = self._StubBackend()
+ vs.backend = "stub"
+ return vs
+
+ def test_build_decision_context_warns_and_marks_on_missing_vector(self, caplog):
+ """
+ When get_vector() returns None for an existing decision, build_decision_context
+ should warn and add 'similarity_unavailable': True to the context.
+ """
+ import logging
+ vs = self._make_stub_store()
+
+ with caplog.at_level(logging.WARNING, logger="semantica.vector_store"):
+ ctx = vs.build_decision_context(decision_id="decision_1", depth=1)
+
+ assert ctx["decision_id"] == "decision_1"
+ assert ctx.get("similarity_unavailable") is True, "Expected similarity_unavailable marker"
+ assert isinstance(ctx["related_decisions"], list), "related_decisions must be a list for schema stability"
+ assert len(ctx["related_decisions"]) == 0
+
+ warning_logged = any(
+ "decision_1" in record.message and record.levelname == "WARNING"
+ for record in caplog.records
+ )
+ assert warning_logged, f"Expected WARNING log mentioning decision_1; got: {caplog.records}"
+
+ def test_explain_decision_warns_and_marks_on_missing_vector(self, caplog):
+ """
+ When get_vector() returns None for an existing decision, explain_decision
+ with include_paths=True should warn and add 'similarity_unavailable': True.
+ """
+ import logging
+ vs = self._make_stub_store()
+
+ with caplog.at_level(logging.WARNING, logger="semantica.vector_store"):
+ explanation = vs.explain_decision(
+ decision_id="decision_1",
+ include_paths=True,
+ )
+
+ assert explanation["decision_id"] == "decision_1"
+ assert explanation.get("similarity_unavailable") is True, "Expected similarity_unavailable marker"
+ assert isinstance(explanation["similar_decisions"], list), "similar_decisions must be a list for schema stability"
+ assert len(explanation["similar_decisions"]) == 0
+
+ warning_logged = any(
+ "decision_1" in record.message and record.levelname == "WARNING"
+ for record in caplog.records
+ )
+ assert warning_logged, f"Expected WARNING log mentioning decision_1; got: {caplog.records}"