| 1 | # 0002 · Reasoning engine |
| 2 | |
| 3 | - **Status**: R0 built: six fact-level violation kinds in `axiom_violations` (five checks plus |
| 4 | `derived_contradiction`, 0017), eight ontology defect kinds in `ontology_defects`, two Review tabs. R1 built behind the KB switch `materialize_inferences` |
| 5 | (default off), rules from four axiom kinds (#132, #177, #179). R2 built as a proof chain |
| 6 | (`GET /kbs/{id}/derived/{id}/proof`, 0016 B1). Contradiction signals for derivations built per |
| 7 | [0017](0017-a-contradiction-points-upstream.md). R3 not built: every run recomputes the whole KB, |
| 8 | on `inference_interval_minutes` (default 60). |
| 9 | - **Written**: 2026-08-28 · condensed into English 2026-09-03 |
| 10 | - **Related**: [0001](0001-ontology-import-and-governance.md) P5 (this record replaces its schedule); |
| 11 | [0010](0010-no-relation-is-no-relation.md); |
| 12 | [0015](0015-recording-a-sentence-is-not-asserting-a-fact.md) reuses decision 3's criterion; |
| 13 | [0016](0016-close-the-open-seams-before-cutting-new-ones.md); [0017](0017-a-contradiction-points-upstream.md) |
| 14 | |
| 15 | ## The problem |
| 16 | |
| 17 | A reasoner amplifies defects. On `Industry Corpus` (28 press releases) 39% of edges were `related_to`, |
| 18 | and 185 `part_of` facts expanded to 828 under transitive closure at depth cap 10, with per-depth counts |
| 19 | oscillating from depth 5 instead of decaying: cycles, real ones, from extraction errors such as |
| 20 | `Microsoft → FarmBeats for Students → Microsoft`. Closure on day one would derive "Microsoft part_of |
| 21 | Microsoft" with an evidence chain attached. The same rule evaluator run backwards is a consistency |
| 22 | check with no truth maintenance and no explosion, so the engine is built as a checker first, the |
| 23 | corpus is cleaned, then materialization is switched on. |
| 24 | |
| 25 | ## Decisions |
| 26 | |
| 27 | 1. **R0 checks and writes no facts.** Pure logic in `utopia-reason`; kinds `self_loop`, `asymmetry`, |
| 28 | `cycle` (depth-first, with the full `path`), `functional` (including inverse-functional) and |
| 29 | `signature` (#190 / #196: subject outside the domain or object outside the range, computed in SQL |
| 30 | by `store::reasoning::signature_breaks`; untyped entities excluded). Rows go to `axiom_violations`, |
| 31 | which asks "wrong data or wrong definition": `fact_retracted` / `axiom_relaxed` / `accepted`. |
| 32 | 2. **The ontology checks itself first.** `ontology_defects`, eight kinds (symmetric and asymmetric, |
| 33 | transitive and functional, subclass cycle, disjoint with an ancestor, inherited disjoint, |
| 34 | self-inverse, inverse not mutual, sub-property cycle), shown before violations because a |
| 35 | self-contradicting ontology makes every fact-level conclusion suspect. A KB without an ontology |
| 36 | pack reports zero, since there is no criterion; importing a pack runs a check. |
| 37 | 3. **Derived facts live in their own table.** `derived_facts`, with |
| 38 | `fact_derivations (derived_fact_id, premise_fact_id, seq)` and the rule on `derived_facts.rule_id` |
| 39 | (migration `0013_reasoning.sql`). `fact_evidence.chunk_id` is NOT NULL and a derivation's evidence |
| 40 | is other facts; and of forty-odd queries over `facts` one knew a flag, so a new query would read |
| 41 | derivations as assertions by default. Separate tables fail the safe way: a forgotten UNION hides |
| 42 | derivations. `facts.derived_by_rule` is never written; its only reference is a Review filter. |
| 43 | 4. **Asserted beats derived, hard.** `reconcile_new_fact` auto-closes older facts of a functional |
| 44 | predicate, so a derivation on that path would let one wrong rule close human assertions. An |
| 45 | asserted triple is never derived (`derive.rs`); derived vs asserted does not land and becomes an |
| 46 | `axiom_violations` row of kind `derived_contradiction` (cap 50 per predicate); derived vs derived |
| 47 | aggregates per rule pair into `ontology_defects` kind `rules_disagree` and neither lands (0017). |
| 48 | 5. **Rules come only from ontology axioms.** Transitive, Symmetric (#132), inverseOf, subPropertyOf |
| 49 | (#177, #179; projection in migration `0016`); no user DSL. Rule identity is `(kb, predicate, kind)` |
| 50 | and rows are kept when an axiom is withdrawn, so `rule_id` and history survive. Inverse |
| 51 | normalization at axiom load fills gaps only; a mismatched pair goes to `inverse_not_mutual`. |
| 52 | Cold-start bootstrap declares no axiom (`Axioms::default()`): the reasoner's criteria are |
| 53 | written by people. |
| 54 | 6. **Materialization is a KB switch, default off.** The job records its run time before deriving so |
| 55 | a failure cannot loop; endpoints return an explicit error while off. `MAX_DERIVED_PER_PREDICATE` = |
| 56 | 20,000, truncation reported as `capped`; an `unruled` counter that should stay zero records a |
| 57 | real bug (rule lookup by predicate instead of `via` dropped cross-predicate rules silently). |
| 58 | Derived edges are gold on the graph behind a toggle; the entity panel has a "derived" section. |
| 59 | 7. **Explanation is a chain** (`reasoning::proof`). `fact_derivations` records asserted premises |
| 60 | only, so the proof runs derivation → assertions → evidence → chunk → document by `seq`. |
| 61 | Retracted premises stay listed and flagged, so a proof stays readable after invalidation. |
| 62 | 8. **Validity intervals intersect**: half-open, empty means nothing derived, touching endpoints do |
| 63 | not overlap, precision the coarsest premise, confidence the minimum, literal objects excluded |
| 64 | (`object_id IS NOT NULL`). Cycles go to Review with their path, since both edges came from the |
| 65 | model and neither deserves prior trust; R1 never derives a self-loop. |
| 66 | |
| 67 | ## Dead ends |
| 68 | |
| 69 | - **R0 output into `fact_conflicts`, "zero new UI".** That table asks "which one is right"; |
| 70 | violations ask "data or definition". New table, two new Review tabs. |
| 71 | - **Fact-level `disjointWith` violations** as an R0 kind: not built; disjointness feeds the ontology |
| 72 | self-check only. |
| 73 | - **Derivations shown by `entity_history` for free.** Its UNION has facts, merges and retypes; |
| 74 | visibility went to the graph and the panel instead. |
| 75 | |
| 76 | ## Revisions |
| 77 | |
| 78 | - 2026-09-02: assumed derivations enter `facts` under the `derived_by_rule` column reserved in |
| 79 | `0004_graph.sql`; the code has `derived_facts` and the column is never written. |
| 80 | - 2026-09-02: the "11 existing contradictions" rested on the seed ontology's `functional` |
| 81 | declarations; with the seeds gone, a KB without a pack reports zero. |
| 82 | - 2026-09-03: the contradiction signals promised in decision 4 exist (0017); the `related_to` |
| 83 | obstacle (39% empty edges) vanished with 0010, since null-predicate edges never enter reasoning. |
| 84 | |
| 85 | ## Open questions |
| 86 | |
| 87 | - Materialize or evaluate at query time: 4.5× expansion on a small corpus, unknown at scale. For now |
| 88 | a per-predicate cap and a periodic full recompute. |
| 89 | - R3 incremental maintenance: correctness is the hardest to verify, and full recompute holds. |