Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Address review: warn on duplicate rule, guard non-string conditions

KaifAhmad17455ed254c61
diff --git a/semantica/reasoning/reasoner.py b/semantica/reasoning/reasoner.py
index cfe2320b..2ba3e5c6 100644
--- a/semantica/reasoning/reasoner.py
+++ b/semantica/reasoning/reasoner.py
@@ -100,9 +100,9 @@ class Reasoner:
and existing.conditions == rule.conditions
and existing.conclusion == rule.conclusion
):
- self.logger.debug(
+ self.logger.warning(
f"Skipping duplicate rule (same conditions/conclusion as '{existing.rule_id}'): "
- f"IF {' AND '.join(rule.conditions)} THEN {rule.conclusion}"
+ f"IF {' AND '.join(map(str, rule.conditions))} THEN {rule.conclusion}"
)
return existing
diff --git a/tests/reasoning/test_reasoner.py b/tests/reasoning/test_reasoner.py
index 9552e5a2..a7e19ce4 100644
--- a/tests/reasoning/test_reasoner.py
+++ b/tests/reasoning/test_reasoner.py
@@ -110,6 +110,39 @@ class TestReasoner(unittest.TestCase):
# unbounded rule duplication.
self.assertEqual(result2, [])
+ def test_add_rule_duplicate_logs_warning(self):
+ """Bug #732 follow-up — a skipped duplicate rule must be surfaced via a
+ warning log, not silently swallowed at debug level."""
+ rule_str = "IF Person(?x) AND Parent(?x, ?y) THEN Child(?y, ?x)"
+ self.reasoner.add_rule(rule_str)
+
+ with self.assertLogs(self.reasoner.logger.name, level="WARNING") as cm:
+ self.reasoner.add_rule(rule_str)
+
+ self.assertTrue(any("duplicate rule" 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
+ (Rule.conditions is typed List[Any])."""
+ rule = Rule(
+ rule_id="r1",
+ name="Test Rule",
+ conditions=[("Person", "?x")],
+ conclusion="B(?x)",
+ )
+ duplicate = Rule(
+ rule_id="r2",
+ name="Test Rule Duplicate",
+ conditions=[("Person", "?x")],
+ conclusion="B(?x)",
+ )
+ self.reasoner.add_rule(rule)
+ result = self.reasoner.add_rule(duplicate)
+
+ self.assertIs(result, rule)
+ self.assertEqual(len(self.reasoner.rules), 1)
+
def test_add_rule_does_not_dedupe_distinct_rules(self):
"""Rules with different conditions/conclusions must still both be added."""
self.reasoner.add_rule("IF A(?x) THEN B(?x)")