Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(context): extend causal vocabulary normalization to sibling methods (#1184)

Aldrin Joseph248ae57694e0
diff --git a/semantica/context/context_graph.py b/semantica/context/context_graph.py
index d18c2b54..7f781a4b 100644
--- a/semantica/context/context_graph.py
+++ b/semantica/context/context_graph.py
@@ -2889,10 +2889,13 @@ class ContextGraph:
Returns:
List of precedent decisions
"""
- # Find decisions connected via PRECEDENT_FOR relationships
+ # Find decisions connected via PRECEDENT_FOR relationships, accepting
+ # the analyzer vocabulary's "precedes" spelling as well (issue #1184).
precedent_ids = []
for edge in self.edges:
- if edge.target_id == decision_id and edge.edge_type == "PRECEDENT_FOR":
+ if edge.target_id == decision_id and edge.edge_type.upper() in {
+ "PRECEDENT_FOR", "PRECEDES",
+ }:
precedent_ids.append(edge.source_id)
# Convert to Decision objects
@@ -3453,8 +3456,13 @@ class ContextGraph:
# Explicit causal relationships recorded via add_causal_relationship() are
# ground truth and always count as direct influence, in either direction.
- for edge_type in _CAUSAL_EDGE_TYPES:
- for edge in self.edge_type_index.get(edge_type, []):
+ # The index is keyed by the raw edge_type string ("causes" and "CAUSED"
+ # are separate keys), so filter by normalized type instead of iterating
+ # a fixed spelling list.
+ for edge_type, edges in self.edge_type_index.items():
+ if edge_type.upper() not in _CAUSAL_TRAVERSAL_TYPES:
+ continue
+ for edge in edges:
if edge.source_id == decision_id and edge.target_id in self._decisions:
direct_influence.add(edge.target_id)
elif edge.target_id == decision_id and edge.source_id in self._decisions:
@@ -3600,8 +3608,12 @@ class ContextGraph:
# record_decision() (e.g. a graph restored via from_dict), so only
# causes with a known decision record are kept.
incoming_causal_edges = defaultdict(list)
- for edge_type in _CAUSAL_EDGE_TYPES:
- for edge in self.edge_type_index.get(edge_type, []):
+ # The index is keyed by the raw edge_type string ("causes" and
+ # "CAUSED" are separate keys), so filter by normalized type.
+ for edge_type, edges in self.edge_type_index.items():
+ if edge_type.upper() not in _CAUSAL_TRAVERSAL_TYPES:
+ continue
+ for edge in edges:
if edge.source_id in self._decisions:
incoming_causal_edges[edge.target_id].append(edge)
diff --git a/tests/context/test_decision_causal_edge_regression.py b/tests/context/test_decision_causal_edge_regression.py
index 211864d0..eae7eae3 100644
--- a/tests/context/test_decision_causal_edge_regression.py
+++ b/tests/context/test_decision_causal_edge_regression.py
@@ -391,3 +391,64 @@ def test_add_causal_relationship_rejects_non_string_with_value_error():
for bad_type in (None, 42, ["CAUSED"]):
with pytest.raises(ValueError):
graph.add_causal_relationship(cause, effect, relationship_type=bad_type)
+
+
+def test_analyze_decision_influence_sees_lowercase_causal_edge():
+ """Issue #1184 follow-up: influence analysis reads the same edge index as
+ the causal traversal, so lowercase edges must count as direct influence."""
+ graph = ContextGraph(advanced_analytics=True)
+ cause = graph.record_decision(
+ category="a", scenario="upstream", reasoning="r",
+ outcome="x", confidence=0.9,
+ )
+ effect = graph.record_decision(
+ category="b", scenario="downstream", reasoning="r",
+ outcome="y", confidence=0.9,
+ )
+ graph.add_edge(cause, effect, "causes")
+
+ impact = graph.analyze_decision_influence(cause)
+
+ direct_ids = {entry["decision_id"] for entry in impact["direct_influence"]}
+ assert effect in direct_ids
+
+
+def test_trace_decision_causality_sees_lowercase_causal_edge():
+ """Issue #1184 follow-up: the trace must not return an empty audit chain
+ for a decision with an explicit lowercase upstream causal edge."""
+ graph = ContextGraph(advanced_analytics=True)
+ cause = graph.record_decision(
+ category="a", scenario="upstream", reasoning="r",
+ outcome="x", confidence=0.9,
+ )
+ effect = graph.record_decision(
+ category="b", scenario="downstream", reasoning="r",
+ outcome="y", confidence=0.9,
+ )
+ graph.add_edge(cause, effect, "causes")
+
+ trace = graph.trace_decision_causality(effect)
+
+ assert any(
+ hop["from"] == cause and hop["to"] == effect
+ for chain in trace for hop in chain["hops"]
+ ), "lowercase causal edge must appear in the traced chain"
+
+
+def test_find_precedents_sees_lowercase_precedent_edge():
+ """Issue #1184 follow-up: precedent lookup must accept the analyzer's
+ spelling alongside the canonical PRECEDENT_FOR."""
+ graph = ContextGraph(advanced_analytics=True)
+ precedent = graph.record_decision(
+ category="a", scenario="earlier", reasoning="r",
+ outcome="x", confidence=0.9,
+ )
+ later = graph.record_decision(
+ category="b", scenario="later", reasoning="r",
+ outcome="y", confidence=0.9,
+ )
+ graph.add_edge(precedent, later, "precedes")
+
+ precedents = graph.find_precedents(later)
+
+ assert [d.decision_id for d in precedents] == [precedent]