Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(context): dedupe heuristic causes in trace_decision_causality (#1360)

cxzg007ec55385b781e
diff --git a/semantica/context/context_graph.py b/semantica/context/context_graph.py
index da2e3dfc..1bb2ec84 100644
--- a/semantica/context/context_graph.py
+++ b/semantica/context/context_graph.py
@@ -4791,14 +4791,18 @@ class ContextGraph:
# Find potential causes (decisions that influenced this one) via
# shared entities/timestamps - additive heuristic, skipping anything
- # already covered by an explicit relationship above.
- potential_causes = []
+ # already covered by an explicit relationship above. Deduplicate by
+ # decision id (dict preserves insertion order): a decision sharing
+ # several entities with the current one is one potential cause,
+ # not one per shared entity, otherwise the trace reports the same
+ # "influences" chain once per overlapping entity.
+ potential_causes = {}
for entity in current_decision["entities"]:
for other_decision_id in self._entity_index.get(entity, set()):
if other_decision_id != current_id and other_decision_id not in explicit_cause_ids:
other_decision = self._decisions[other_decision_id]
if other_decision["timestamp"] < current_decision["timestamp"]:
- potential_causes.append(other_decision_id)
+ potential_causes[other_decision_id] = None
for cause_id in potential_causes:
cause_dec = self._decisions.get(cause_id, {})
diff --git a/tests/context/test_decision_causal_edge_regression.py b/tests/context/test_decision_causal_edge_regression.py
index eae7eae3..3bfa1591 100644
--- a/tests/context/test_decision_causal_edge_regression.py
+++ b/tests/context/test_decision_causal_edge_regression.py
@@ -452,3 +452,42 @@ def test_find_precedents_sees_lowercase_precedent_edge():
precedents = graph.find_precedents(later)
assert [d.decision_id for d in precedents] == [precedent]
+
+
+def test_heuristic_cause_reported_once_per_shared_entity_pair():
+ """A potential cause found through the shared-entity heuristic must be
+ reported once, not once per shared entity.
+
+ ``trace_decision_causality()`` collects ``potential_causes`` by looping
+ over every entity of the current decision, so a decision sharing two
+ entities with an earlier one (e.g. the same customer and the same
+ property) used to be appended twice and produced two identical
+ "influences" chains.
+ """
+ graph = ContextGraph(advanced_analytics=True)
+ cause = graph.record_decision(
+ category="lending", scenario="earlier review", reasoning="r",
+ outcome="approved", confidence=0.9,
+ entities=["customer_123", "property_456"],
+ )
+ effect = graph.record_decision(
+ category="risk", scenario="later review", reasoning="r",
+ outcome="flagged", confidence=0.9,
+ entities=["customer_123", "property_456"],
+ )
+ # Pin timestamps so the earlier/later ordering is deterministic.
+ graph._decisions[cause]["timestamp"] = 100.0
+ graph._decisions[effect]["timestamp"] = 200.0
+
+ chains = graph.trace_decision_chain(effect)
+
+ influence_hops = [
+ (hop["from"], hop["to"])
+ for chain in chains
+ for hop in chain["hops"]
+ if hop["type"] == "influences"
+ ]
+ assert influence_hops, "shared-entity heuristic must find the earlier decision"
+ assert influence_hops.count((cause, effect)) == 1, (
+ "the same (cause, effect) pair must be reported once, not once per shared entity"
+ )