Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Warn and document confidence-discard behavior on rule dedup (review follow-up for #732)

Sameer6305506b7060a187
diff --git a/semantica/reasoning/reasoner.py b/semantica/reasoning/reasoner.py
index 7b6fd99e..b1fe9be5 100644
--- a/semantica/reasoning/reasoner.py
+++ b/semantica/reasoning/reasoner.py
@@ -88,6 +88,14 @@ class Reasoner:
code (e.g. a Jupyter cell that calls add_rule() + add_fact() on an
existing Reasoner) idempotent instead of silently duplicating rules
on every rerun (#732).
+
+ On dedup, the ORIGINAL rule's confidence, priority, and metadata are
+ retained; the incoming rule's differing fields are discarded, not
+ merged or upserted -- except for priority, where self.rules is
+ re-sorted to reflect any change made directly on the retained Rule
+ object after it was first added (see the re-sort below). If the
+ incoming rule's confidence differs from the retained rule's, a
+ warning is logged so the discrepancy isn't silently swallowed.
"""
if isinstance(rule_def, Rule):
rule = rule_def
@@ -104,6 +112,12 @@ class Reasoner:
f"Skipping duplicate rule (same conditions/conclusion as '{existing.rule_id}'): "
f"IF {' AND '.join(map(str, rule.conditions))} THEN {rule.conclusion}"
)
+ if existing.confidence != rule.confidence:
+ self.logger.warning(
+ f"Duplicate rule '{existing.rule_id}' was re-added with a different "
+ f"confidence ({rule.confidence}); the existing confidence "
+ f"({existing.confidence}) is retained and the new value is discarded."
+ )
# Rule is a mutable dataclass, so `existing.priority` may have
# changed since it was added -- re-sort so the dedup path
# keeps the same self-healing ordering the append path has,
diff --git a/tests/reasoning/test_reasoner.py b/tests/reasoning/test_reasoner.py
index f85153a2..f63cf2e8 100644
--- a/tests/reasoning/test_reasoner.py
+++ b/tests/reasoning/test_reasoner.py
@@ -121,6 +121,27 @@ class TestReasoner(unittest.TestCase):
self.assertTrue(any("duplicate rule" in msg for msg in cm.output))
+ def test_add_rule_duplicate_with_different_confidence_logs_warning(self):
+ """Re-adding an identical rule (same conditions/conclusion) with a
+ different confidence must warn that the new confidence is discarded
+ and the original is retained, not silently drop it."""
+ rule_v1 = Rule(
+ rule_id="r1", name="Rule", conditions=["A(?x)"], conclusion="B(?x)",
+ confidence=0.6,
+ )
+ rule_v2 = Rule(
+ rule_id="r2", name="Rule v2", conditions=["A(?x)"], conclusion="B(?x)",
+ confidence=0.95,
+ )
+ self.reasoner.add_rule(rule_v1)
+
+ with self.assertLogs(self.reasoner.logger.name, level="WARNING") as cm:
+ result = self.reasoner.add_rule(rule_v2)
+
+ self.assertIs(result, rule_v1)
+ self.assertEqual(result.confidence, 0.6)
+ self.assertTrue(any("different confidence" in msg for msg in cm.output))
+
def test_add_rule_duplicate_with_non_string_conditions_does_not_raise(self):
"""Bug #732 follow-up — the duplicate-rule warning message building must
not raise TypeError when Rule.conditions contains non-string entries