Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Address Copilot review: fix forward-chain semantics regression, sorted() hot spot, add premises test coverage

Sameer6305e8c9e221ef1f
diff --git a/semantica/reasoning/reasoner.py b/semantica/reasoning/reasoner.py
index 1896600a..15aa781e 100644
--- a/semantica/reasoning/reasoner.py
+++ b/semantica/reasoning/reasoner.py
@@ -180,34 +180,48 @@ class Reasoner:
new_facts_added = False
iteration += 1
- # Aggregate every derivation of the same conclusion across all rules
- # in this pass before deciding whether it's a new fact. Multiple
- # variable bindings (or multiple rules) can independently derive the
- # identical instantiated conclusion (e.g. "Person(A)" and "Person(B)"
- # both satisfying "IF Person(?x) THEN ExistsPerson()"); without this,
- # only whichever derivation happened to be enumerated first would
- # "win" and the rest would be silently discarded, making the
- # recorded premises depend on unordered set iteration.
- pass_matches: Dict[str, Tuple[List[str], Rule]] = {}
+ # Snapshot facts that existed before this pass, so we can tell a
+ # fact that was already known apart from one newly derived during
+ # this same pass. Newly derived conclusions are added to
+ # self.facts immediately (not deferred to the end of the pass) so
+ # that later rules in this same pass can chain off facts inferred
+ # earlier in the pass -- e.g. "IF A THEN B" firing lets
+ # "IF B THEN C" fire in the same pass rather than requiring an
+ # extra outer iteration.
+ pre_pass_facts = frozenset(self.facts)
+ # Tracks conclusions newly derived in this pass, keyed to the
+ # InferenceResult already appended to `results`, so multiple
+ # derivations of the identical conclusion (different bindings
+ # and/or different rules within the same pass) merge their
+ # premises into one result instead of creating duplicates or
+ # silently dropping premises (the #733 fix).
+ pass_results: Dict[str, InferenceResult] = {}
+
for rule in self.rules:
for conclusion, matched_facts in self._match_rule(rule):
- if conclusion in self.facts:
+ if conclusion in pass_results:
+ # Another derivation of a conclusion already produced
+ # earlier in this same pass: merge premises, dedup.
+ existing = pass_results[conclusion]
+ for fact in matched_facts:
+ if fact not in existing.premises:
+ existing.premises.append(fact)
continue
- premises, attributed_rule = pass_matches.setdefault(conclusion, ([], rule))
- for fact in matched_facts:
- if fact not in premises:
- premises.append(fact)
-
- for conclusion in sorted(pass_matches):
- matched_facts, rule = pass_matches[conclusion]
- self.facts.add(conclusion)
- results.append(InferenceResult(
- conclusion=conclusion,
- rule_used=rule,
- premises=matched_facts,
- confidence=rule.confidence
- ))
- new_facts_added = True
+ if conclusion in pre_pass_facts:
+ # Already known before this pass started -- not a
+ # new derivation.
+ continue
+
+ self.facts.add(conclusion)
+ inference_result = InferenceResult(
+ conclusion=conclusion,
+ rule_used=rule,
+ premises=list(matched_facts),
+ confidence=rule.confidence
+ )
+ pass_results[conclusion] = inference_result
+ results.append(inference_result)
+ new_facts_added = True
self.progress_tracker.stop_tracking(
tracking_id,
@@ -332,6 +346,13 @@ class Reasoner:
if not rule.conditions:
return []
+ # self.facts is not mutated anywhere within this method, so sort it
+ # once here rather than re-sorting on every (bindings, condition)
+ # pair below -- sorted() was previously called once per inner-loop
+ # entry, which re-allocates and re-sorts the full fact set repeatedly
+ # and is a hot spot for larger fact sets.
+ sorted_facts = sorted(self.facts)
+
# Each entry pairs a set of variable bindings with the facts that were
# matched to produce those bindings, so the facts survive alongside
# the bindings as conditions accumulate.
@@ -340,7 +361,7 @@ class Reasoner:
for condition in rule.conditions:
new_bindings_list = []
for bindings, matched_facts in bindings_list:
- for fact in sorted(self.facts):
+ for fact in sorted_facts:
match_bindings = self._match_pattern(condition, fact, bindings)
if match_bindings is not None:
new_bindings_list.append((match_bindings, matched_facts + [fact]))
diff --git a/tests/reasoning/test_reasoner.py b/tests/reasoning/test_reasoner.py
index 55c556c6..cfd00feb 100644
--- a/tests/reasoning/test_reasoner.py
+++ b/tests/reasoning/test_reasoner.py
@@ -64,6 +64,22 @@ class TestReasoner(unittest.TestCase):
self.assertIn("Person(John)", result.premises)
self.assertIn("Parent(John, Jane)", result.premises)
+ def test_forward_chaining_premises(self):
+ """Mirrors test_backward_chaining_simple: forward_chain() must attach the
+ specific facts that matched the rule's conditions as premises, not leave
+ them empty (regression guard for issue #733)."""
+ self.reasoner.add_rule("IF Person(?x) AND Parent(?x, ?y) THEN Child(?y, ?x)")
+ self.reasoner.add_fact("Person(John)")
+ self.reasoner.add_fact("Parent(John, Jane)")
+
+ results = self.reasoner.forward_chain()
+ self.assertEqual(len(results), 1)
+ result = results[0]
+ self.assertEqual(result.conclusion, "Child(Jane, John)")
+ self.assertEqual(len(result.premises), 2)
+ self.assertIn("Person(John)", result.premises)
+ self.assertIn("Parent(John, Jane)", result.premises)
+
def test_infer_facts(self):
facts = ["Person(John)", "Parent(John, Jane)"]
rules = ["IF Person(?x) AND Parent(?x, ?y) THEN Child(?y, ?x)"]