Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
reason: refuse a conclusion that cannot be written down, and say what the Lean never sees
FABIOTESS95d6bb830d18
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d3f01c7d..11f34746 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,30 @@ All notable changes to Open Ontologies are documented here.## [Unreleased]### Added+- **The trusted computing base of the certificate layer is written down and+ property-tested.** `docs/trusted-computing-base.md` enumerates, as twenty-nine+ checkable properties, everything `lean/` assumes about the Rust: that+ `asserted.tsv` is the graph the engine reasoned over with nothing dropped and+ nothing added, that every emitted step is a step the engine took, that the+ interner round-trips, that the rule table the certificate is checked against+ is the table that was evaluated, and that no term can carry the field or+ record separator of a format that has no escaping layer of its own.+ `tests/certificate_boundary_proptest.rs` property-tests them with generators+ built to forge a derivation step: literals holding tabs, newlines, carriage+ returns, quotes and backslashes, a literal spelled exactly like an IRI, a+ literal spelled exactly like a whole extra TSV line, combining characters+ against their precomposed form, percent-encoded separators inside IRIs, the+ empty graph. `proptest` is a DEV-dependency; the shipped binary's dependency+ surface is unchanged. Three Kani harnesses in `src/reason.rs` (`make verify`)+ prove the serialisation properties over every byte pattern at a fixed term+ length rather than over a sample; the bound is stated on each harness, along+ with the two formulations that made CBMC measure the wrong function. A fourth,+ over `parse_pat`, does NOT terminate (no verdict at 14 minutes and 9.5GB,+ because `anyhow`'s error formatting is flattened whether or not the refusal+ paths are reachable); it is excluded from `make verify`, carries its+ measurements, and the property is sampled instead. The first property-test run+ found the `rdfs7` defect below, and measuring its reach found the `cls-avf`+ one.- **First-order export, over the translation a machine-checked adequacy theoremis about.** `fol --out DIR --format tptp|clif` (also `onto_fol_export` andbatch `fol`) writes the loaded ontology as TPTP FOF or as ISO/IEC 24707@@ -70,6 +94,40 @@ All notable changes to Open Ontologies are documented here.trace. See docs/lean-certificates.md and decision 0002.### Fixed+- **A rule concluding a triple with a non-IRI PREDICATE left the store half+ materialised, with no certificate, and reported failure.** `rdfs7` reads+ `s sub o` and `sub rdfs:subPropertyOf super` and concludes `s super o`, and+ nothing required `super` to be an IRI. `:p rdfs:subPropertyOf [ owl:inverseOf+ :q ]` is enough, and that is exactly what the OWL 2 mapping to RDF produces+ for `SubObjectPropertyOf(:p ObjectInverseOf(:q))`, so an ordinary OWL+ ontology made `reason` fail with `Parser error: The predicate of a triple must+ be an IRI`. The failure was not clean: `GraphStore::load_ntriples` streams,+ inserting each quad as it parses it, so the batch failed PARTWAY. Measured on+ one fixture over three consecutive runs of identical input, 40, 9 and 24 of+ 40 good inferences stayed in the store, the number being hash iteration order,+ and `run_full` then returned the error and never wrote the certificate. The+ store was left holding uncertified inferences while the caller was told the+ run had failed. In `--dry-run` the materialiser never ran, so the certificate+ WAS written, containing a conclusion no RDF serialiser can express; the Lean+ checker holds terms as opaque strings and accepts it. Reachable from `rdfs7`,+ `prp-inv1`, `prp-inv2` and `cls-hv1`, each of which takes its conclusion's+ predicate from an object position where RDF permits a blank node or a literal.+ Found by the new property tests at case 115, shrunk to two triples.+- **`cls-avf` could still conclude a triple with a LITERAL subject.** The same+ defect one position over, found while measuring the reach of the one above.+ `cls-avf` derives `y rdf:type c` from `x rdf:type ∀P.c` and `x P y`, so an+ `owl:allValuesFrom` restriction on a property with a literal value derived+ `"x" rdf:type :D` and broke the materialiser the same way. The subject case+ was found on 30 August 2026 and guarded at `prp-symp`, `prp-inv1`, `prp-inv2`+ and `eq-sym`; `cls-avf` was not among them, and this one needs no unusual+ modelling at all. Both positions are now decided in ONE place,+ `writable_triple`, shared with `run_horn`, which was the only path that+ already refused either: a refused conclusion is not materialised, not+ certified and not available as a premise, and the run reports+ `skipped_unserialisable` with examples. `tests/reason_unwritable_predicate_test.rs`+ pins every reachable rule, and pins that `prp-symp` and `prp-trp` are NOT+ reachable, because their property must already be the predicate of a stored+ triple and is therefore an IRI.- **The CLIF export produced files that parse cleanly and yield NOTHING.**Every sentence was emitted as `(cl:comment '...' SENTENCE)`, the shapeISO/IEC 21838-2's BFO files use. Measured against both CLIF parsers thatdiff --git a/Cargo.lock b/Cargo.lockindex db4eb3ef..161cc321 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -660,7 +660,16 @@ version = "0.5.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"dependencies = [- "bit-vec",+ "bit-vec 0.6.3",+]++[[package]]+name = "bit-set"+version = "0.8.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3"+dependencies = [+ "bit-vec 0.8.0",][[package]]@@ -669,6 +678,12 @@ version = "0.6.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"+[[package]]+name = "bit-vec"+version = "0.8.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"+[[package]]name = "bitflags"version = "1.3.2"@@ -2812,6 +2827,7 @@ dependencies = ["libc","oxigraph","parquet",+ "proptest","quick-xml","rayon","reqwest",@@ -3282,6 +3298,25 @@ dependencies = ["unicode-ident",]+[[package]]+name = "proptest"+version = "1.11.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4b45fcc2344c680f5025fe57779faef368840d0bd1f42f216291f0dc4ace4744"+dependencies = [+ "bit-set 0.8.0",+ "bit-vec 0.8.0",+ "bitflags 2.13.0",+ "num-traits",+ "rand 0.9.4",+ "rand_chacha 0.9.0",+ "rand_xorshift",+ "regex-syntax",+ "rusty-fork",+ "tempfile",+ "unarray",+]+[[package]]name = "prost"version = "0.11.9"@@ -3325,6 +3360,12 @@ dependencies = ["syn 1.0.109",]+[[package]]+name = "quick-error"+version = "1.2.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"+[[package]]name = "quick-xml"version = "0.37.5"@@ -3504,6 +3545,15 @@ dependencies = ["rand 0.8.6",]+[[package]]+name = "rand_xorshift"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a"+dependencies = [+ "rand_core 0.9.5",+]+[[package]]name = "rawpointer"version = "0.2.1"@@ -3888,6 +3938,18 @@ version = "1.0.22"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"+[[package]]+name = "rusty-fork"+version = "0.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2"+dependencies = [+ "fnv",+ "quick-error",+ "tempfile",+ "wait-timeout",+]+[[package]]name = "ryu"version = "1.0.23"@@ -5043,7 +5105,7 @@ checksum = "3f0674154b96abb73f8bdade9e6b6e22fa65f4e13da8d84c0659bd7d3c4739b9"dependencies = ["anyhow","anymap3",- "bit-set",+ "bit-set 0.5.3","derive-new","downcast-rs","dyn-clone",@@ -5229,6 +5291,12 @@ version = "0.1.7"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971"+[[package]]+name = "unarray"+version = "0.1.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94"+[[package]]name = "unicode-bidi"version = "0.3.18"@@ -5348,6 +5416,15 @@ version = "0.9.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"+[[package]]+name = "wait-timeout"+version = "0.2.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11"+dependencies = [+ "libc",+]+[[package]]name = "walkdir"version = "2.5.0"diff --git a/Cargo.toml b/Cargo.tomlindex 1e841fca..231f60dd 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -96,6 +96,20 @@ plugins = ["dep:wasmi"]# is not an inner product on the ambient coordinates).turbovec = ["dep:turbovec", "embeddings"]+[lints.rust]+# `cfg(kani)` is set by the Kani model checker's own compiler driver, never by+# cargo, so an ordinary build has no way to know the name is real and warns that+# the `#[cfg(kani)]` harnesses in `src/reason.rs` are a typo. Declaring it here+# says the name is expected; it does not enable anything.+unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kani)'] }+[dev-dependencies]tempfile = "3"wat = "1.256.0"+# Property testing for the certificate boundary (docs/trusted-computing-base.md).+# The Lean proofs are conditional on the Rust writing down the truth, and that+# boundary is a tab-separated format carrying arbitrary RDF terms: exactly the+# shape where a hand-written example passes and an adversarial one forges a+# derivation. DEV-ONLY on purpose. The shipped binary's dependency surface does+# not grow by a single crate.+proptest = "1"diff --git a/Makefile b/Makefileindex a1b031fb..6a000517 100644--- a/Makefile+++ b/Makefile@@ -1,4 +1,4 @@-.PHONY: build test lint audit bench bench-pizza bench-ontoaxiom bench-mushroom bench-vision bench-reasoner bench-oaei docker clean demo demo-verify demo-verify-pipeline+.PHONY: build test lint audit verify bench bench-pizza bench-ontoaxiom bench-mushroom bench-vision bench-reasoner bench-oaei docker clean demo demo-verify demo-verify-pipeline# ─── Development ─────────────────────────────────────────────────────────────@@ -14,6 +14,24 @@ lint:audit:cargo audit+# Bounded model checking of the pure functions on the certificate layer's+# trusted boundary (docs/trusted-computing-base.md). Needs+# `cargo install --locked kani-verifier && cargo kani setup`, which pulls its+# own toolchain, so this is NOT part of `check`: it is minutes per harness and+# a developer without Kani installed must still be able to run the gates.+# `cargo test` covers the same statements by sampling.+# `parse_pat_and_render_are_inverse` is deliberately NOT here. It is a fourth+# harness in the same module and it does not terminate: no verdict at 14 minutes+# and 9.5GB, worse when the input is constrained, because `parse_pat` returns+# `anyhow::Result` and CBMC flattens the error-formatting machinery whether or+# not the refusal paths are reachable. The harness carries the measurements and+# what would close it. A target that hangs is worse than one that is honest+# about its coverage.+verify:+ cargo kani --harness asserted_line_round_trips+ cargo kani --harness triple_fields_append_exactly_three+ cargo kani --harness writable_triple_decides_both_positions+check: lint test audit# ─── Benchmarks ──────────────────────────────────────────────────────────────diff --git a/README.md b/README.mdindex eb4d21ca..3c446c01 100644--- a/README.md+++ b/README.md@@ -1009,6 +1009,13 @@ was in the engine: `cls-svf1` derived the converse of a subclass axiom. Detailsin [docs/lean-certificates.md](docs/lean-certificates.md) and[decision 0002](docs/decisions/0002-an-inference-carries-a-certificate.md).+The theorem is conditional, and what it is conditional on is the Rust writing+down the truth: the asserted graph the certificate names, and the steps it+records. That boundary is small, it is the whole trusted base of this layer, and+it is enumerated as twenty-nine checkable properties in+[docs/trusted-computing-base.md](docs/trusted-computing-base.md), with what+checks each and what is still trusted. Read it before relying on a certificate.+### Studio```mermaid@@ -1094,6 +1101,7 @@ flowchart TD| Schema Alignment | [docs/alignment.md](docs/alignment.md) || SHIQ Reasoning | [docs/reasoning.md](docs/reasoning.md) || Derivation certificates, Lean checker | [docs/lean-certificates.md](docs/lean-certificates.md) |+| What the Lean proofs assume about the Rust | [docs/trusted-computing-base.md](docs/trusted-computing-base.md) || Semantic Embeddings | [docs/embeddings.md](docs/embeddings.md) || Clinical Crosswalks | [docs/clinical.md](docs/clinical.md) || IES Ecosystem | [docs/ies-ecosystem.md](docs/ies-ecosystem.md) |diff --git a/docs/lean-certificates.md b/docs/lean-certificates.mdindex 63a03004..80360df1 100644--- a/docs/lean-certificates.md+++ b/docs/lean-certificates.md@@ -172,14 +172,34 @@ Nineteen of the engine's rules are Horn rules and appear in the built-in table.`cls-uni` are not: their premise count is the length of an RDF list, which is data rather than fixedby the rule, so they remain hardcoded arms. A user rule language stops at the same boundary.-The Rust reasoner does not yet evaluate a supplied rule table, so today this layer checks-certificates rather than producing them. See+The Rust reasoner evaluates a supplied rule table through `reason --rules RULES.tsv --certificate+DIR`, which writes `rules.tsv`, `asserted.tsv` and `horn.tsv` and states no verdict of its own.+Nothing is materialised into the store: a conclusion drawn under a table you wrote holds only in+models that satisfy that table, and merging it in beside the assertions would lose exactly the+distinction this is about. See[decision 0003](decisions/0003-a-rule-is-data-and-an-assumption-is-not-a-fact.md).+## What the checker assumes about the engine++The theorem is conditional. It says that IF the asserted graph is `G` and IF these steps check,+THEN the conclusions hold in every model of `G`. Everything to the left of that is the Rust writing+down the truth, and the checker cannot see any of it: it reads files, not the store and not the+run. `docs/trusted-computing-base.md` enumerates that boundary as twenty-nine checkable properties+and says for each what checks it. Read it before relying on a certificate, because a certificate is+a claim about a file and the file's relationship to the store is the part nobody proved.+## Known limitationsStated rather than discovered later.+- **A conclusion no RDF serialiser can write is refused, not derived.** A literal in subject+ position or a non-IRI in predicate position produces a triple the store cannot hold. Such a+ conclusion is not materialised, not certified, and not used as a premise, so a run that hits one+ derives LESS than its rule set licenses. The count and up to three examples come back as+ `skipped_unserialisable` and `skipped_examples`. This is reachable from ordinary OWL: `:p+ rdfs:subPropertyOf [ owl:inverseOf :q ]` is what the OWL 2 mapping to RDF produces for+ `SubObjectPropertyOf(:p ObjectInverseOf(:q))`, and it makes `rdfs7` conclude a triple with a+ blank node in predicate position.- **The rule table is this engine's, not W3C's.** `by_rule` describes what this engine did. It isnot an OWL 2 RL conformance claim, and the engine does not implement every rule in the profile.- **`asserted.tsv` is the store, not your file.** Quads are flattened, so a triple present in twodiff --git a/docs/trusted-computing-base.md b/docs/trusted-computing-base.mdnew file mode 100644index 00000000..4e116def--- /dev/null+++ b/docs/trusted-computing-base.md@@ -0,0 +1,395 @@+# The trusted computing base of the Lean certificate layer++`lean/` proves a conditional. `OOCert.certificate_sound` says: if the asserted graph is `G`, and+if these derivation steps check, then every conclusion holds in every model of `G`. Everything to+the left of that "if" is the Rust engine writing down the truth. If the engine emits a certificate+whose `asserted.tsv` is not the graph it reasoned over, or whose steps are not the steps it took,+then a valid Lean proof certifies a false claim and the layer is decoration.++That gap is the trusted computing base. This page names it exactly, as a list of properties that can+be checked, and says for each one what checks it today.++The point of writing it down is that an unwritten trusted base cannot be reviewed. A reader who+finishes this page should be able to say how much of the engine is verified (almost none of it) and+which specific, small pieces carry the weight (the ones below).++## What the Lean side never sees++The checkers are pure functions of files on disk. They read `asserted.tsv`, `derivations.tsv`,+`rules.tsv`, `horn.tsv`, `axioms.tsv` and `model.tsv`, and nothing else. In particular they cannot+see:++- the RDF store, so they cannot tell whether `asserted.tsv` is the store's contents;+- the reasoner's execution, so they cannot tell whether a step in `derivations.tsv` was ever taken;+- the store after the run, so they cannot tell whether a triple was materialised without a+ certificate line covering it;+- the rule file the user passed, so they cannot tell whether `rules.tsv` is the table the engine+ evaluated.++Each of those four blind spots is a property below.++## The emission paths++| path | entry point | files written | checker |+|---|---|---|---|+| built-in forward chaining | `Reasoner::run_full` (`src/reason.rs`) | `asserted.tsv`, `derivations.tsv` | `oo-cert` |+| supplied Horn rules | `Reasoner::run_horn` (`src/reason.rs`) | `rules.tsv`, `asserted.tsv`, `horn.tsv` | `oo-horn` |+| DL model certificate | `DlReasoner::write_model_certificate` (`src/tableaux.rs`) | `axioms.tsv`, `model.tsv` | `oo-dlmodel` |+| SHACL | no Rust emission | N-Triples handed to the Lean evaluator | `oo-shacl` |++The SHACL row is different in kind and is treated separately at the end.++## The properties++Each property has an identifier, a statement, the code it is about, and how it is checked. `TCB-*`+identifiers are used by `tests/certificate_boundary_proptest.rs`, which names them in its test+functions.++### Serialisation: the format cannot be forged from inside a term++All six files are tab-separated, carry arbitrary RDF terms, and have no escaping layer of their own.+`src/reason.rs` writes terms with `push('\t')` between them and `push('\n')` at the end of a line,+and nothing in that code inspects a term. `lean/OOCert/Parse.lean` states the assumption that makes+this safe, in a comment rather than a theorem:++> Tabs and newlines cannot occur inside an N-Triples term (they are escaped), so splitting on them is+> exact.++That sentence is the whole of the escaping argument, and it is a claim about a third-party crate.+The terms come from `oxrdf`'s `Display`, through `GraphStore::all_triples`, which calls+`quad.subject.to_string()` and friends. `oxrdf 0.3.3` escapes `\t`, `\n`, `\r`, `"` and `\` inside a+literal's lexical form (`print_quoted_str` in `literal.rs`), and does **not** escape anything inside+an IRI: `NamedNodeRef`'s `Display` is `write!(f, "<{}>", self.as_str())`. Nothing in this repository+re-checks either half.++- **TCB-1 (field integrity, asserted).** Every line of `asserted.tsv` splits on `\t` into exactly+ three fields, and the number of lines equals the number of triples the run started from. A term+ carrying a tab would split a line into four fields, and a term carrying a newline would split one+ line into two; either forges a triple the store never held.+- **TCB-2 (field integrity, derivations).** Every line of `derivations.tsv` splits into+ `1 + 3 * (1 + premises)` fields with `premises >= 1`.+- **TCB-3 (field integrity, Horn).** Every line of `horn.tsv` splits into+ `2 + 2 * binds + 3 * (1 + body)` fields, with `binds` and `body` read from the line's own header+ and from `rules.tsv`.+- **TCB-4 (no separator in any term).** No term written to any certificate file contains `\t`,+ `\n` or `\r`. This is the property TCB-1 to TCB-3 exist to detect the failure of; stated+ separately because it is the one that has to survive an `oxrdf` upgrade.+- **TCB-5 (term spellings do not collide).** A literal whose lexical form is `<http://example/x>`+ and the IRI `<http://example/x>` must not have the same spelling in a certificate file. They do+ not, because a literal is quoted, but the two checkers compare terms as opaque strings and would+ be unable to tell them apart if they ever agreed.++### The asserted graph++- **TCB-6 (asserted is the graph, no drops).** Every triple in the store at the start of a certified+ run appears as a line in `asserted.tsv`. A dropped assertion makes the checker reject a legitimate+ step, so this direction is fail-safe, but it is still part of the claim the file makes.+- **TCB-7 (asserted is the graph, no additions).** Every line of `asserted.tsv` corresponds to a+ triple that was in the store at the start of the run. This is the unsafe direction. An extra line+ is an axiom nobody asserted, and every conclusion resting on it is certified against a graph that+ does not exist.+- **TCB-8 (no inference leaks into the assertions, within a run).** No conclusion of+ `derivations.tsv` appears in `asserted.tsv`. The engine reaches a fixpoint in one pass and+ captures `facts` before materialising, so this holds within a run.+ **Across runs it does not hold and cannot be made to hold by this layer.** Materialising into the+ default graph turns run N's conclusions into run N+1's assertions, and `asserted.tsv` has no+ column that says "derived". `docs/lean-certificates.md` states this under "Known limitations" and+ decision 0001 is the mitigation (`inference_graph: true` keeps them in a named graph). Note that+ `GraphStore::all_triples` iterates `store.iter()` over every graph, so inferences parked in+ `https://open-ontologies.org/graph/inferred` by an earlier run are read back as assertions by a+ later certified run. The separation protects `save`, not the certificate.+- **TCB-9 (the store is a set, the file is a list).** A triple present in two named graphs is+ written twice, because `all_triples` flattens quads. Duplicate lines are harmless to soundness+ (the Lean side builds a `HashSet`) but `asserted` in the JSON report counts lines, not distinct+ triples. The Horn path reports both numbers; the built-in path reports only the line count.++### The derivation steps++- **TCB-10 (every inference is certified).** The set of conclusions in `derivations.tsv` equals the+ set of triples the run added to the store. A triple materialised without a line covering it is an+ uncertified triple sitting in the store under the certificate's cover. Structurally this holds+ because the single `emit` closure is the only path that pushes a candidate, and it records the+ first derivation of anything not already in the closure, but nothing outside that reading enforces+ it.+- **TCB-11 (one line per inference).** `derivations.len() == inferred_count`. A conclusion is+ recorded the first time it is derived and never again.+- **TCB-12 (premises precede conclusions).** Every premise of the step on line `i` is either in+ `asserted.tsv` or is the conclusion of a line `j < i`. No step cites itself.+ `OOCert.checkHornAll` and `OOCert.checkAll` both demand this, so a violation is a rejection rather+ than a false pass. It is in this list because an emitter that violates it produces certificates+ that fail for no reason a user can act on, and because the argument that it holds (indices are+ rebuilt from `triple_set` at the top of each round, and `new` is merged only at the bottom) is a+ reading of the loop, not a check.+- **TCB-13 (the rule name is one the checker knows).** Every rule name in `derivations.tsv` is+ accepted by `OOCert.Rule.ofName?`. An unknown name is a parse error, so this is fail-safe, but a+ rule renamed on one side and not the other silently stops the corpus test from covering it.+- **TCB-14 (the premise order matches the rule's arm).** `OOCert.checkStep` pattern-matches the+ premises positionally per rule. The emitter passes them in a hand-written order at each of the+ thirty-one `emit` call sites. A wrong order is a rejection, not a false pass, and+ `tests/lean_certificate_test.rs` covers every rule, but nothing derives the order from a single+ source.++### The interner++Every term in every certificate file goes through `Interner` (`src/reason.rs`), a `HashMap<String,+u32>` plus a `Vec<String>`. Both the reasoning and the writing use interned identifiers, so if the+interner were not a bijection two different terms would print the same and a step would be checked+against a triple that is not the one the engine used.++- **TCB-15 (round trip).** `resolve(intern(s)) == s` for every string.+- **TCB-16 (injectivity).** `intern(a) == intern(b)` if and only if `a == b`.+- **TCB-17 (stability).** An identifier's resolution does not change as more strings are interned.++### The rule table++- **TCB-18 (the table checked is the table evaluated).** `run_horn` writes `rules_tsv(&rules)`+ rather than the user's file, and re-parses what it wrote and compares before writing it out. That+ guard is in the code already and is the single best thing in this layer. The property it does not+ cover is that `OOCert.HornParse.parseRules` reads that file as the same table, because the+ comparison is between two Rust values.+- **TCB-19 (Rust and Lean parse a rule table the same way).** For every table the Rust accepts,+ `parseRules` yields the same rules. The two parsers are independent implementations. Rust is+ strictly stricter on three points, all in the refusing direction: it requires a constant to be in+ N-Triples spelling, it refuses a head variable not bound by the body, and it refuses a carriage+ return. Lean's `patOf` treats any field not starting with `?` as a constant.+- **TCB-20 (render and parse are inverse).** `parse_rules(rules_tsv(r)) == r`. In particular a+ constant never renders to something that reads back as a variable, and a variable name never+ renders to something that reads back as a constant.+- **TCB-21 (`rules_tsv` and `ruleStr` agree byte for byte).** The Rust doc comment on `rules_tsv`+ asserts this of `OOCert.HornParse.ruleStr`. It matters because the JSON report publishes a SHA-256+ of the Rust rendering, and `oo-horn` decides `entailed` against `entailed_under_supplied_rules` by+ comparing tables.+- **TCB-22 (the rule index means the same thing on both sides).** `horn.tsv` cites a rule by index.+ The index is a position in `crules`, which is built by mapping over `rules` in order, and+ `rules.tsv` is rendered from `rules` in the same order. Empty lines are skipped by both parsers and+ `rules_tsv` never emits one.+- **TCB-23 (the binding instantiates the body and the head).** For each `horn.tsv` line, the+ premises equal the rule's body under the written substitution, and the conclusion equals the head+ under it. `OOCert.checkHornStep` checks exactly this, so it is fail-safe, and it is listed because+ a disagreement here is the difference between a certificate that checks and one that does not.+- **TCB-24 (nothing is materialised under a supplied table).** `run_horn` writes no triple into the+ store. A conclusion under a user's rules holds only in models that satisfy those rules, and+ merging it into the store loses that distinction. This is decision 0003.++### The DL model certificate++`axioms.tsv` and `model.tsv` are tab separated at the top level and **space separated inside a+concept** (`concept_string` in `src/tableaux.rs`), so the DL path is exposed to spaces as well as+tabs.++- **TCB-25 (every name survives the format).** `name_is_safe` refuses any name that is empty or+ contains a space, tab, carriage return or newline, and the emitter refuses to write a certificate+ if any name fails. This is the only explicit injection guard in the codebase, and it is in the+ right place.+- **TCB-26 (the guard covers every name that is written).** The `names` vector is built by walking+ every axiom variant, every role in `model.rext` and every class in `model.cext`. Individuals in+ `model.ind` are covered only because `DlAxiom::Indiv(i)` is emitted for every individual that+ reaches the model. That is an argument about two separate loops agreeing, not a check.+- **TCB-27 (the emitter refuses rather than lies).** Before writing, the emitter runs the same+ semantics the checker runs (`model.well_formed`, then `model.holds` on every axiom) and refuses+ on failure. A certificate that will not check is worse than no certificate.++### SHACL++There is no Rust emission path. `tests/shacl_core_verified_test.rs` hands the Lean evaluator+N-Triples produced by the engine's own parser with an explicit base IRI, and the Lean side parses+them itself. So the boundary here is not a certificate format but the N-Triples serialiser, and the+Lean parser is a second, independent RDF reader.++- **TCB-28 (the two RDF readers agree).** `Shacl.Parse.readTerm` accepts a subset of N-Triples and+ keeps escapes undecoded, so `"a\tb"` is the term `"a\tb"` with a backslash and a `t` in it, not a+ tab. The engine's terms are in the same undecoded spelling, so the two agree, but they agree by+ construction on both sides rather than by a check. A term the Lean parser reads differently is a+ shape applied to a different graph, silently.+- **TCB-29 (the SHACL layer is not a certificate layer).** `oo-shacl` validates; it does not check+ a record of what the Rust validator did. The Rust SHACL validator's agreement with the verified+ one is measured by `tests/shacl_core_verified_test.rs` and by the pyshacl differential, and+ measurement is not proof.++## Status++| property | checked by | how |+|---|---|---|+| TCB-1 | `tests/certificate_boundary_proptest.rs`, `src/reason.rs` | property, end to end and on the writer |+| TCB-2, TCB-3 | same | property |+| TCB-4 | `tests/certificate_boundary_proptest.rs` | property, plus two deterministic tests: the parser refuses a separator inside an IRI, and Turtle's long-string form (which carries RAW control characters) comes back escaped |+| TCB-5 | proptest | property |+| TCB-6, TCB-7 | proptest | `asserted.tsv` is re-read through the N-Triples parser into a fresh store and compared |+| TCB-8 | proptest | property within a run; the across-run failure is pinned by a test that asserts the DEFECT, so the day it changes the documentation is forced to change |+| TCB-9 | deterministic test | a quad in two named graphs is two lines |+| TCB-10, TCB-11 | proptest | conclusions compared against the store's before and after |+| TCB-12 | proptest | both the built-in and the Horn path |+| TCB-13 | proptest | the name list is read out of `lean/OOCert/Rules.lean` at test time |+| **TCB-14** | `tests/lean_certificate_test.rs` | the real checker, over every rule. **Not** re-derived here |+| TCB-15, TCB-16, TCB-17 | `src/reason.rs` unit property tests | the interner is private |+| TCB-18 | in the code, and proptest | the engine re-parses what it wrote and refuses on mismatch |+| TCB-19, TCB-21 | proptest, plus the REAL checker | property against a transcription of `OOCert.HornParse` in the test file, because a Lean process per case is not a property test. The transcription's fidelity is then an assumption, so one deterministic test puts the adversarial shapes (a numeric rule name, a non-ASCII one, a `??x` variable, a literal spelled exactly like an IRI as a constant, a blank node, an empty-bodied rule) through `oo-horn check` itself and requires the conditional verdict |+| TCB-20, TCB-22 | proptest | render and parse are inverse, and no rendered line is empty |+| TCB-23, TCB-24 | proptest | the substitution is re-applied from `rules.tsv` independently of the engine |+| TCB-25 | `src/tableaux.rs` unit property tests | `name_is_safe`, `concept_string` token counts, `axiom_line` field counts |+| **TCB-26** | nothing | the argument that `names` covers `model.ind` is two loops agreeing |+| TCB-27 | in the code | `tests/dl_model_certificate_test.rs` exercises it; not property-tested |+| **TCB-28, TCB-29** | nothing here | the SHACL boundary is different in kind, see below |++The generators build adversarial graphs (literals holding tabs, newlines, carriage returns, quotes+and backslashes; a lexical form spelled exactly like an IRI; a lexical form spelled exactly like a+whole extra TSV line; combining characters against their precomposed form; NUL; the empty graph;+language tags; typed literals; percent-encoded separators inside IRIs) and adversarial rule tables,+and then drive the real public entry points.++### Bounded model checking++Kani 0.67.0 installed and ran here, on `nightly-2025-11-21-aarch64-apple-darwin`, over the whole+crate. Three harnesses in `src/reason.rs` prove the serialisation properties over every byte pattern+at a fixed shape, rather than over the patterns a generator drew. A fourth was written and does not+terminate; it is reported below rather than quietly dropped. `make verify` runs the three. None are+part of `make check`, because Kani pulls its own toolchain and the same statements are sampled by+`cargo test` for anyone without it.++| harness | property | result |+|---|---|---|+| `asserted_line_round_trips` | TCB-1 | SUCCESSFUL, 1476 checks, 0 failed, 12.8s |+| `triple_fields_append_exactly_three` | TCB-2, TCB-3 | SUCCESSFUL, 1485 checks, 0 failed, 32.5s |+| `writable_triple_decides_both_positions` | the guard that fixed the defect below | SUCCESSFUL, 198 checks, 0 failed, 1.6s |+| `parse_pat_and_render_are_inverse` | TCB-20 | **NO VERDICT.** Abandoned at 14 minutes and 9.5 GB |++**The one that failed to run.** `parse_pat` returns `anyhow::Result` and every refusal formats a+message naming the line and the position, so the function body carries the whole formatting+machinery. CBMC flattens a function before it solves, so an `assume` that makes the refusal paths+infeasible is a constraint for the solver and not a cut in the program: constraining the input made+it worse, not better. Measured, with nothing else competing: four unconstrained ASCII bytes, no+verdict at 7 minutes and 3.0 GB; two bytes, no verdict at 8 minutes and 2.8 GB; two bytes with the+first constrained to the three spellings the function accepts, no verdict at 14 minutes and 9.5 GB.+It is excluded from `make verify`, because a target that hangs is worse than one that is honest+about its coverage, and the property is sampled instead (1024 cases on the function, plus whole+rule tables in the integration suite). What would close it is lifting the classification out of+`parse_pat` into a pure `Option<Pat>` function, leaving the messages where they are. That is a+refactor of shipped code and it was not made here.++**What the bounds are, and they are real.** In the three that verify, terms are a FIXED three bytes,+each byte unconstrained ASCII. The length is fixed rather than symbolic because a symbolic length+makes every offset in the line symbolic and every slice bound a case split: at two bytes per term+with a symbolic length, CBMC reached fifteen gigabytes without a verdict. So the harnesses prove+every byte pattern at one shape, and the property tests sample the shapes, including the empty+string. Neither alone is the whole claim, and anyone quoting "verified" off this table without the+bound is misquoting it.++Two other costs were paid down rather than hidden, because both are the kind of thing that makes a+verification effort quietly measure the wrong function:++- `core::str::from_utf8` put CBMC inside `run_utf8_validation` once per buffer: seventeen minutes and+ three gigabytes with no verdict, the log a wall of `Unwinding loop ... run_utf8_validation`. The+ harness assumes ASCII, which is exactly what that function checks, so it uses+ `from_utf8_unchecked`.+- Asserting on `body.split('\t')` put CBMC inside `CharSearcher`: ten minutes and two gigabytes at+ two bytes per term. It is also the wrong claim. The reader is `OOCert.Parse.parseTriples`, which is+ Lean's `String.splitOn`, so a proof about Rust's `str::split` proves a property of the wrong+ splitter. The harnesses assert the BYTE LAYOUT (exactly two tabs, exactly one newline, that+ newline last, the three terms at the offsets those separators imply), which is what any correct+ splitter needs and all it needs. The `split` formulation is kept as a property test, where it is+ free.++The pattern in all three is the same, and it is the thing to take away from this section: twice, the+first formulation of a harness measured a function nobody asked about. A verification effort that+does not check what it is actually measuring produces a green result about the wrong thing, which is+the failure mode this whole layer exists to attack.++### What this found++The first run of TCB-1, at case 115, found a real defect, shrunk to two triples:++```+<http://e/A> <http://e/q> <http://e/A> .+<http://e/q> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> _:b0 .+```++`rdfs7` concluded `<http://e/A> _:b0 <http://e/A>`, a blank node in predicate position, which no+RDF serialisation can express. `GraphStore::load_ntriples` streams, so materialising failed+PARTWAY, leaving an arbitrary, hash-order-dependent prefix of the inferences in the store with no+certificate covering any of them, and `run_full` returned the parser's error. In a dry run the+certificate WAS written and contained the unserialisable conclusion, which the Lean checker accepts+because it holds terms as opaque strings. It is reachable from ordinary OWL:+`:p rdfs:subPropertyOf [ owl:inverseOf :q ]` is what the OWL 2 mapping to RDF produces for+`SubObjectPropertyOf(:p ObjectInverseOf(:q))`.++Measuring how far it reached found a second one. The SUBJECT case was found on 30 August 2026 and+guarded at `prp-symp`, `prp-inv1`, `prp-inv2` and `eq-sym`. It was still open at `cls-avf`, which+derives `y rdf:type c` from `x rdf:type ∀P.c` and `x P y`: an `owl:allValuesFrom` restriction on a+property with a literal value derived `"x" rdf:type :D` and broke the materialiser the same way.+That one needs no anonymous property expression and no unusual modelling at all.++Six fixtures, all legal RDF, made the unfixed engine return a parser error: `rdfs7`, `prp-inv1`,+`prp-inv2` and `cls-hv1` for the predicate position, `cls-avf` for the subject position, and+`rdfs7` again with a literal rather than a blank node. There is now one guard, `writable_triple`,+deciding both positions and shared with `run_horn`, which was the only path that already refused+either. Pinned by `tests/reason_unwritable_predicate_test.rs`, which also pins that `prp-symp` and+`prp-trp` cannot reach it.++The lesson is the one this document is for. The 30 August fix was four targeted guards at the sites+its author had found, and the sites its author had not found stayed broken for two weeks. The+central guard is the fix; the property test is what found the sites.++## What is still trusted after all of this++This is the part that matters, and it is longer than the part that is checked. Nothing below is+verified. Each entry says what would close it.++1. **`oxrdf`'s escaping is the entire escaping layer.** `asserted.tsv` is safe because+ `print_quoted_str` turns a tab inside a literal into `\t`. This repository has no escaper of its+ own and never inspects a term. TCB-4 pins the behaviour a test can observe, which means an+ upgrade that changed it would fail loudly; it does not mean it cannot change, and it does not+ cover a term that reaches the store by a route the tests do not exercise.+ *To close it:* escape at the certificate writer rather than relying on the term's `Display`, or+ change the format to one that cannot be forged by its payload (length-prefixed, or JSON Lines).+ Both are changes to `lean/`'s parsers as well, which is why neither was done here.+2. **`oxiri`'s IRI validation is why a separator cannot reach an IRI.** `NamedNodeRef`'s `Display`+ is `write!(f, "<{}>", self.as_str())` with no escaping at all, so the only thing standing between+ a tab and `asserted.tsv` is that the parser refused to build the IRI. Tens of thousands of lines+ of parsing sit behind that sentence and none of it is verified here.+ *To close it:* check the term at the writer, as in (1). The check is four lines; the reason it is+ not there is that it would be a second place the question is decided.+3. **TCB-14, the premise ORDER per rule.** `OOCert.checkStep` matches premises positionally, arm by+ arm, and the emitter passes them in a hand-written order at thirty-one call sites. The two agree+ because `tests/lean_certificate_test.rs` walks every rule and the checker accepts. Nothing+ derives one from the other, so a rule added with the wrong order fails at the checker rather than+ at compile time. *To close it:* generate both sides from one table, which is what the Horn path+ already does and the built-in path does not.+4. **TCB-26, the DL name guard's coverage.** `name_is_safe` is applied to a `names` vector built by+ one loop; the files are written by another. Individuals in `model.ind` are covered only because+ `DlAxiom::Indiv(i)` happens to be emitted for every individual that reaches the model.+ *To close it:* check at the point of writing, not in a separate pass.+5. **TCB-8 across runs.** Materialising into the default graph turns an inference into the next+ run's assertion, and `asserted.tsv` has no column that says "derived". `inference_graph: true`+ does not fix it either: `GraphStore::all_triples` reads every named graph, so a later certified+ run sees them as assertions. The separation protects `save`; it does not protect the certificate.+ *To close it:* have the certified path read the asserted graph only, and say in the certificate+ which graphs it read.+6. **Materialisation is not atomic, and that is now latent rather than fixed.** `GraphStore::+ load_lines` runs `for quad in parser { store.insert(&quad?)?; }`, so a parse error partway+ through a batch leaves everything before it inserted and propagates the error. That is what+ turned the `rdfs7` defect from a crash into uncertified triples in the store. `writable_triple`+ removes the only way the reasoner could hand it an unparseable line, so the path is unreachable+ TODAY; the hazard is still there for any future caller.+ *To close it:* insert into a transaction, or build the batch through a serialiser that cannot+ produce an unparseable line rather than through string concatenation.+7. **The reasoner's completeness.** The checker proves each recorded step is a sound instance of a+ rule it knows. It says nothing about whether the engine found every inference, so a certificate+ is evidence about what was derived and not about what follows.+8. **The SHIQ tableaux reasoner.** Model certificates cover positive satisfiability answers only.+ Unsatisfiability and inconsistency carry no certificate at all, and those are the answers a+ consistency check is usually asked for.+9. **The Rust SHACL validator (TCB-28, TCB-29).** Not certified. `oo-shacl` is a second, verified+ evaluator that the Rust one is measured against; a measurement is not a proof, and the two read+ RDF with two different parsers whose agreement is by construction on both sides rather than by a+ check.+10. **Everything between the store and the certificate that is not a term:** the SPARQL layer, the+ MCP server, the CLI, the daemon, the file I/O, the roughly fifty thousand lines of `src/`. The+ Kani harnesses cover four pure functions totalling about twenty lines. That is the correct+ proportion to report: this work verified the joint, not the machine.++The honest summary is that the trusted base is four things: the serialisation of a term, the+identity of the asserted graph, the completeness of the derivation record, and the identity of the+rule table. Those are now property-tested and, for the pure parts, bounded-model-checked. Every one+of them still rests on a dependency's behaviour that this repository observes rather than enforces.+The engine is not verified. It was never going to be, and a report that read as though it were+would be the same defect this project exists to attack.diff --git a/src/reason.rs b/src/reason.rsindex 42009d16..bc34b65e 100644--- a/src/reason.rs+++ b/src/reason.rs@@ -37,6 +37,68 @@ struct Derivation {premises: Vec<Fact>,}+/// Can this triple be written by an RDF serialiser?+///+/// A literal cannot be a subject and only an IRI can be a predicate, in every+/// RDF 1.1 serialisation. A rule that concludes otherwise has produced+/// something the store cannot hold, and the materialiser does not find out+/// until it is halfway through inserting the batch.+///+/// Both positions, and ONE definition, because the previous attempt was four+/// targeted guards and it missed two things.+///+/// The subject case was found on 30 August 2026 and guarded at `prp-symp`,+/// `prp-inv1`, `prp-inv2` and `eq-sym`. It was still open at `cls-avf`, which+/// concludes `y rdf:type c` from `x type ∀P.c` and `x P y`, so an+/// `owl:allValuesFrom` restriction on a property with a literal value derived+/// `"x" rdf:type :D`. That needs no unusual modelling at all.+///+/// The predicate case was never guarded anywhere outside `run_horn`. It is+/// reachable from `rdfs7`, `prp-inv1`, `prp-inv2` and `cls-hv1`, each of which+/// takes its conclusion's predicate from an object position where RDF permits a+/// blank node or a literal. `:p rdfs:subPropertyOf [ owl:inverseOf :q ]` is+/// enough, and that is what the OWL 2 mapping to RDF produces for+/// `SubObjectPropertyOf(:p ObjectInverseOf(:q))`. Found by+/// `tests/certificate_boundary_proptest.rs` on 14 September 2026 and pinned by+/// `tests/reason_unwritable_predicate_test.rs`, which also pins that `prp-symp`+/// and `prp-trp` CANNOT reach it, because their property must already be the+/// predicate of a stored triple and is therefore an IRI.+///+/// Terms arrive in their N-Triples spelling, so a literal begins with `"` and+/// an IRI with `<`.+fn writable_triple(subject: &str, predicate: &str) -> bool {+ !subject.starts_with('"') && predicate.starts_with('<')+}++/// Append one line of `asserted.tsv`: `s TAB p TAB o NEWLINE`.+///+/// This is the whole of that file's grammar, and it is the narrowest point of+/// the trusted computing base: `OOCert.certificate_sound` is conditional on the+/// asserted graph, `OOCert.Parse.parseTriples` recovers it by splitting on+/// those two characters, and nothing between the two escapes anything. It lives+/// in one function so that the claim "a line is exactly three fields" is a+/// claim about one place, verified in `kani_harnesses` below rather than only+/// sampled.+fn push_asserted_line(out: &mut String, s: &str, p: &str, o: &str) {+ out.push_str(s);+ out.push('\t');+ out.push_str(p);+ out.push('\t');+ out.push_str(o);+ out.push('\n');+}++/// Append a triple as three further fields of a line already begun, the shape+/// `derivations.tsv` and `horn.tsv` use after their header fields.+fn push_triple_fields(out: &mut String, s: &str, p: &str, o: &str) {+ out.push('\t');+ out.push_str(s);+ out.push('\t');+ out.push_str(p);+ out.push('\t');+ out.push_str(o);+}+/// Intern strings to u32 IDs for efficient reasoning.struct Interner {to_id: HashMap<String, u32>,@@ -252,6 +314,13 @@ impl Reasoner {let mut derivations: Vec<Derivation> = Vec::new();let mut recorded: HashSet<Fact> = HashSet::new();+ // Conclusions refused for being unwritable, and up to three of them to+ // show. A set rather than a counter, for the reason `run_horn` gives:+ // a refused conclusion never enters `triple_set`, so every later round+ // derives it again and a counter would report attempts.+ let mut refused: HashSet<Fact> = HashSet::new();+ let mut skipped_samples: Vec<String> = Vec::new();+loop {iterations += 1;let before = triple_set.len();@@ -400,7 +469,30 @@ impl Reasoner {// certificate was asked for, records the first derivation of each// triple not already in the closure, with the premises in the// order the checker expects for that rule.+ //+ // It is also the one place that refuses a conclusion no RDF+ // serialiser can write. Four rules below guard the subject position+ // themselves and are left alone; this guard is central because the+ // per-rule approach is what left the PREDICATE position open, and a+ // rule added later gets the guard without its author remembering.+ // Deriving less is the sound direction, and the count is reported+ // rather than swallowed.+ let interner_ref = &interner;+ let refused_ref = &mut refused;+ let samples_ref = &mut skipped_samples;let mut emit = |t: Fact, rule: &'static str, premises: &[Fact]| {+ let (s, p, _) = t;+ if !writable_triple(interner_ref.resolve(s), interner_ref.resolve(p)) {+ if refused_ref.insert(t) && samples_ref.len() < 3 {+ samples_ref.push(format!(+ "{} {} {} (by {rule})",+ interner_ref.resolve(t.0),+ interner_ref.resolve(t.1),+ interner_ref.resolve(t.2)+ ));+ }+ return;+ }if certify && !triple_set.contains(&t) && recorded.insert(t) {derivations.push(Derivation { rule, conclusion: t, premises: premises.to_vec() });}@@ -1013,17 +1105,32 @@ impl Reasoner {// they were put.result["inference_graph"] = serde_json::json!(INFERRED_GRAPH);}+ if !refused.is_empty() {+ // Said out loud, because this run derived LESS than its rule set+ // licenses and a reader comparing two runs' counts is entitled to+ // know why.+ result["skipped_unserialisable"] = serde_json::json!(refused.len());+ result["skipped_examples"] = serde_json::json!(skipped_samples);+ result["skipped_reason"] = serde_json::json!(+ "the rule concluded a triple no RDF serialiser can write (a literal in subject \+ position, or a non-IRI in predicate position). Such conclusions are neither \+ materialised nor certified nor used as premises. Before this was guarded the \+ materialiser failed partway through inserting the batch, which left the store \+ holding an arbitrary, run-dependent prefix of the inferences with no certificate \+ covering any of them"+ );+ }if let Some(dir) = certificate_dir {std::fs::create_dir_all(dir)?;let mut asserted = String::with_capacity(facts.len() * 96);for &(s, p, o) in &facts {- asserted.push_str(interner.resolve(s));- asserted.push('\t');- asserted.push_str(interner.resolve(p));- asserted.push('\t');- asserted.push_str(interner.resolve(o));- asserted.push('\n');+ push_asserted_line(+ &mut asserted,+ interner.resolve(s),+ interner.resolve(p),+ interner.resolve(o),+ );}std::fs::write(dir.join("asserted.tsv"), asserted)?;@@ -1033,12 +1140,12 @@ impl Reasoner {*by_rule.entry(d.rule).or_default() += 1;lines.push_str(d.rule);for &(s, p, o) in std::iter::once(&d.conclusion).chain(d.premises.iter()) {- lines.push('\t');- lines.push_str(interner.resolve(s));- lines.push('\t');- lines.push_str(interner.resolve(p));- lines.push('\t');- lines.push_str(interner.resolve(o));+ push_triple_fields(+ &mut lines,+ interner.resolve(s),+ interner.resolve(p),+ interner.resolve(o),+ );}lines.push('\n');}@@ -1483,14 +1590,17 @@ impl Reasoner {// A rule whose head puts a literal in subject position, or anything but// an IRI in predicate position, produces something no RDF serialiser- // can write. The built-in loop guards the first case for four of its- // rules; here both are refused, the count is reported, and the+ // can write. Both are refused, the count is reported, and the// conclusion is not used as a premise for anything else. Deriving less// is the sound direction, but it means the emitted set is the fixpoint// of the table over WRITABLE triples, which is what// `skipped_unserialisable` in the response is there to say.+ //+ // `run_full` now shares the predicate (`writable_triple`). It used to+ // have its own guard on the subject position only, at four rule sites,+ // and this path was the only one that refused a non-IRI predicate.let serialisable = |interner: &Interner, (s, p, _o): Fact| -> bool {- !interner.resolve(s).starts_with('"') && interner.resolve(p).starts_with('<')+ writable_triple(interner.resolve(s), interner.resolve(p))};let mut known: HashSet<Fact> = facts.iter().copied().collect();@@ -1596,12 +1706,12 @@ impl Reasoner {let mut asserted = String::with_capacity(facts.len() * 96);for &(s, p, o) in &facts {- asserted.push_str(interner.resolve(s));- asserted.push('\t');- asserted.push_str(interner.resolve(p));- asserted.push('\t');- asserted.push_str(interner.resolve(o));- asserted.push('\n');+ push_asserted_line(+ &mut asserted,+ interner.resolve(s),+ interner.resolve(p),+ interner.resolve(o),+ );}std::fs::write(certificate_dir.join("asserted.tsv"), asserted)?;@@ -1619,12 +1729,12 @@ impl Reasoner {horn.push_str(interner.resolve(*term));}for &(s, p, o) in std::iter::once(&st.conclusion).chain(st.premises.iter()) {- horn.push('\t');- horn.push_str(interner.resolve(s));- horn.push('\t');- horn.push_str(interner.resolve(p));- horn.push('\t');- horn.push_str(interner.resolve(o));+ push_triple_fields(+ &mut horn,+ interner.resolve(s),+ interner.resolve(p),+ interner.resolve(o),+ );}horn.push('\n');}@@ -1717,3 +1827,367 @@ impl Reasoner {Ok(result.to_string())}}++// ─────────────────────────────────────────────────────────────────────────────+// The trusted boundary, verified+//+// `docs/trusted-computing-base.md` enumerates what `lean/` assumes about this+// file. `tests/certificate_boundary_proptest.rs` property-tests the parts that+// need a store. What is left is a handful of pure functions, and those are+// small enough to do better than sample: the Kani harnesses below prove the+// same statements over EVERY input up to a length bound, rather than over the+// inputs a generator happened to draw.+// ─────────────────────────────────────────────────────────────────────────────++#[cfg(test)]+mod boundary_tests {+ use super::*;+ use proptest::prelude::*;++ /// Terms that carry no separator, so the writer's precondition holds and+ /// the round trip is the thing under test.+ fn term() -> impl Strategy<Value = String> {+ prop_oneof![+ Just(String::new()),+ Just("<http://e/a>".to_string()),+ Just("\"lit\"".to_string()),+ Just("\"a\\tb\"".to_string()),+ Just("_:b0".to_string()),+ Just("e\u{0301}".to_string()),+ Just("\u{0000}".to_string()),+ "[^\t\n\r]{0,6}",+ ]+ }++ proptest! {+ #![proptest_config(ProptestConfig { cases: 1024, ..ProptestConfig::default() })]++ /// TCB-15 and TCB-16. The interner is a bijection between the strings it+ /// has seen and the ids it has issued. Every term in every certificate+ /// file passes through it, so if it were not, two different terms would+ /// print the same and a step would be checked against a triple the+ /// engine never used.+ #[test]+ fn tcb_15_16_the_interner_is_a_bijection(ss in prop::collection::vec(term(), 0..16)) {+ let mut i = Interner::new();+ let ids: Vec<u32> = ss.iter().map(|s| i.intern(s)).collect();+ for (s, &id) in ss.iter().zip(&ids) {+ prop_assert_eq!(i.resolve(id), s.as_str());+ }+ for a in 0..ss.len() {+ for b in 0..ss.len() {+ prop_assert_eq!(ids[a] == ids[b], ss[a] == ss[b]);+ }+ }+ }++ /// TCB-17. An id keeps its meaning as more strings arrive. The+ /// certificate writers resolve ids long after the reasoning that+ /// created them.+ #[test]+ fn tcb_17_interning_more_does_not_move_an_id(+ early in prop::collection::vec(term(), 1..8),+ late in prop::collection::vec(term(), 0..8),+ ) {+ let mut i = Interner::new();+ let ids: Vec<u32> = early.iter().map(|s| i.intern(s)).collect();+ for s in &late {+ i.intern(s);+ }+ for (s, &id) in early.iter().zip(&ids) {+ prop_assert_eq!(i.resolve(id), s.as_str());+ }+ }++ /// TCB-1, as a statement about the writer alone. Given three terms that+ /// carry no separator, the line splits back into exactly those three.+ /// The Kani harness below proves the same thing over every input up to+ /// a bound; this one runs in CI without a model checker installed.+ #[test]+ fn tcb_1_an_asserted_line_splits_back_into_its_three_terms(+ s in term(), p in term(), o in term(),+ ) {+ let mut out = String::new();+ push_asserted_line(&mut out, &s, &p, &o);+ prop_assert!(out.ends_with('\n'));+ let body = &out[..out.len() - 1];+ prop_assert!(!body.contains('\n'));+ let f: Vec<&str> = body.split('\t').collect();+ prop_assert_eq!(f, vec![s.as_str(), p.as_str(), o.as_str()]);+ }++ /// TCB-2 and TCB-3, as a statement about the writer alone.+ #[test]+ fn tcb_2_3_triple_fields_append_exactly_three_fields(+ head in "[a-z0-9-]{1,8}",+ ts in prop::collection::vec((term(), term(), term()), 1..4),+ ) {+ let mut out = String::new();+ out.push_str(&head);+ for (s, p, o) in &ts {+ push_triple_fields(&mut out, s, p, o);+ }+ let f: Vec<&str> = out.split('\t').collect();+ prop_assert_eq!(f.len(), 1 + 3 * ts.len());+ prop_assert_eq!(f[0], head.as_str());+ for (i, (s, p, o)) in ts.iter().enumerate() {+ prop_assert_eq!(f[1 + 3 * i], s.as_str());+ prop_assert_eq!(f[2 + 3 * i], p.as_str());+ prop_assert_eq!(f[3 + 3 * i], o.as_str());+ }+ }++ /// TCB-20 at one rule position. `parse_pat` and `Pat::render` are+ /// inverse, so a constant never renders to something that reads back as+ /// a variable. `run_horn`'s re-parse guard rests on this.+ #[test]+ fn tcb_20_parse_pat_and_render_are_inverse(field in "[^\t\n\r]{0,8}") {+ if let Ok(p) = parse_pat(&field, 1, "position") {+ prop_assert_eq!(p.render(), field);+ }+ }+ }+}++/// Bounded model checking of the pure functions on the trusted boundary.+///+/// Run with `cargo kani --harness <name>`. Not part of `cargo test`: Kani+/// compiles the crate with its own toolchain, and a `#[cfg(kani)]` block is+/// invisible to a normal build. What each harness proves and what it does NOT+/// prove is written on it, because a bounded proof read as an unbounded one is+/// the same defect this whole layer exists to attack.+#[cfg(kani)]+mod kani_harnesses {+ use super::*;++ /// An ASCII string of EXACTLY `N` bytes, every byte unconstrained below+ /// 0x80. `kani::any` cannot produce a `String`; ASCII because every byte+ /// below 0x80 is valid UTF-8 on its own.+ ///+ /// Two decisions here are what make these harnesses terminate, and both are+ /// worth recording because the first attempts did not.+ ///+ /// `from_utf8_unchecked` rather than `from_utf8`. With the checked version+ /// CBMC symbolically executes `core::str::validations::run_utf8_validation`+ /// once per buffer, and that loop, not the function under test, becomes+ /// what the harness measures: seventeen minutes and three gigabytes with no+ /// verdict, the log a wall of `Unwinding loop ... run_utf8_validation`. The+ /// `assume` below already establishes exactly what `from_utf8` would check.+ ///+ /// A FIXED length rather than a symbolic one. A symbolic `n` makes every+ /// offset in the produced line symbolic, every slice bound a case split,+ /// and the cost compounds across three terms: two bytes per term with a+ /// symbolic length reached fifteen gigabytes without a verdict. Fixing the+ /// length leaves every BYTE unconstrained, which is the dimension that+ /// matters here (a tab or a newline getting into a term), and moves the+ /// length dimension to `boundary_tests`, which samples it including the+ /// empty string. That is a real limitation of these proofs and it is stated+ /// on each of them rather than left for a reader to infer.+ fn any_ascii<const N: usize>(buf: &mut [u8; N]) -> &str {+ *buf = kani::any();+ for b in buf.iter() {+ kani::assume(*b < 0x80);+ }+ // SAFETY: every byte is assumed below 0x80, so the buffer is ASCII and+ // therefore valid UTF-8. The assumption is the model checker's, so it+ // holds on every path it explores.+ unsafe { core::str::from_utf8_unchecked(&buf[..]) }+ }++ /// TCB-1, over every byte pattern rather than over a sample.+ ///+ /// For ANY three separator-free ASCII terms, `push_asserted_line` writes a+ /// line carrying exactly two tabs and one newline, the newline last, with+ /// the three terms at the offsets those separators imply. This is the+ /// property a forged literal breaks, and the reason+ /// `lean/OOCert/Parse.lean` is entitled to say that splitting is exact.+ ///+ /// What it does NOT prove: that no term ever carries a separator. That is a+ /// fact about `oxrdf` and `oxiri`, not about this function, and it is+ /// pinned by test rather than proved.+ ///+ /// The claim is about the BYTES, not about Rust's `split`. The reader is+ /// `OOCert.Parse.parseTriples`, which is Lean's `String.splitOn`, so a proof+ /// about `str::split` would be a proof about the wrong splitter. What both+ /// need is the layout: the line holds exactly two TAB bytes and exactly one+ /// NEWLINE, that newline is last, and the three terms sit at the offsets+ /// their lengths dictate. Any correct splitter recovers `s`, `p` and `o`+ /// from that and from nothing less.+ ///+ /// Stating it that way is also what makes it verifiable. Asserting on+ /// `body.split('\t')` instead put CBMC inside `CharSearcher` over a symbolic+ /// buffer and ran ten minutes to two gigabytes without a verdict at two+ /// bytes per term. The `split` formulation is kept as a property test in+ /// `boundary_tests`, where it is free.+ ///+ /// Three bytes per term, each byte unconstrained ASCII, the LENGTH fixed.+ /// See `any_ascii` for why the length is fixed and what that costs the+ /// claim: `boundary_tests` samples the lengths, including the empty string,+ /// and this proves every byte pattern at one shape. A bounded proof read as+ /// an unbounded one is the defect this layer exists to attack, so the bound+ /// is on the harness rather than in a footnote.+ ///+ /// `with_capacity` so the buffer never reallocates. CBMC models the+ /// allocator, and a realloc is state that has nothing to do with the claim.+ #[kani::proof]+ #[kani::unwind(16)]+ fn asserted_line_round_trips() {+ const N: usize = 3;+ let (mut a, mut b, mut c) = ([0u8; N], [0u8; N], [0u8; N]);+ let s = any_ascii(&mut a);+ let p = any_ascii(&mut b);+ let o = any_ascii(&mut c);+ kani::assume(!s.contains('\t') && !s.contains('\n'));+ kani::assume(!p.contains('\t') && !p.contains('\n'));+ kani::assume(!o.contains('\t') && !o.contains('\n'));++ let mut out = String::with_capacity(4 * N);+ push_asserted_line(&mut out, s, p, o);+ let w = out.as_bytes();++ // Nothing added and nothing lost: three terms, two tabs, one newline.+ assert!(w.len() == 3 * N + 3);++ // The separators are where the grammar says.+ assert!(w[N] == b'\t');+ assert!(w[2 * N + 1] == b'\t');+ assert!(w[3 * N + 2] == b'\n');++ // And nowhere else, so any correct splitter finds exactly three fields.+ // The reader is `OOCert.Parse.parseTriples`, which is Lean's+ // `String.splitOn`, so the claim has to be about the bytes rather than+ // about Rust's `str::split`.+ let mut tabs = 0usize;+ let mut newlines = 0usize;+ let mut i = 0usize;+ while i < w.len() {+ if w[i] == b'\t' {+ tabs += 1;+ }+ if w[i] == b'\n' {+ newlines += 1;+ }+ i += 1;+ }+ assert!(tabs == 2);+ assert!(newlines == 1);++ // The three terms are at the offsets those separators imply, byte for+ // byte, so no term was altered on the way out.+ let mut k = 0usize;+ while k < N {+ assert!(w[k] == s.as_bytes()[k]);+ assert!(w[N + 1 + k] == p.as_bytes()[k]);+ assert!(w[2 * N + 2 + k] == o.as_bytes()[k]);+ k += 1;+ }+ }++ /// TCB-2 and TCB-3. Three fields appended to a line already begun come back+ /// as three fields, so a step's conclusion and its premises cannot run into+ /// one another.+ #[kani::proof]+ #[kani::unwind(16)]+ fn triple_fields_append_exactly_three() {+ const N: usize = 3;+ let (mut a, mut b, mut c) = ([0u8; N], [0u8; N], [0u8; N]);+ let s = any_ascii(&mut a);+ let p = any_ascii(&mut b);+ let o = any_ascii(&mut c);+ kani::assume(!s.contains('\t') && !p.contains('\t') && !o.contains('\t'));+ kani::assume(!s.contains('\n') && !p.contains('\n') && !o.contains('\n'));++ let mut out = String::with_capacity(4 * N + 4);+ out.push('r');+ push_triple_fields(&mut out, s, p, o);+ let w = out.as_bytes();++ assert!(w.len() == 1 + 3 * N + 3);+ assert!(w[0] == b'r');+ assert!(w[1] == b'\t');+ assert!(w[2 + N] == b'\t');+ assert!(w[3 + 2 * N] == b'\t');++ // Exactly three tabs are added and no newline, so the header field+ // keeps its position and the triple occupies exactly the next three.+ let mut tabs = 0usize;+ let mut i = 0usize;+ while i < w.len() {+ if w[i] == b'\t' {+ tabs += 1;+ }+ assert!(w[i] != b'\n');+ i += 1;+ }+ assert!(tabs == 3);++ let mut k = 0usize;+ while k < N {+ assert!(w[2 + k] == s.as_bytes()[k]);+ assert!(w[3 + N + k] == p.as_bytes()[k]);+ assert!(w[4 + 2 * N + k] == o.as_bytes()[k]);+ k += 1;+ }+ }++ /// The guard that fixed the unwritable-predicate defect. For ANY pair of+ /// strings it decides both positions and never panics. The statement is+ /// small because the function is; its value is that the engine now has+ /// exactly one of these, so this is the only place the question is decided.+ #[kani::proof]+ #[kani::unwind(12)]+ fn writable_triple_decides_both_positions() {+ let (mut a, mut b) = ([0u8; 4], [0u8; 4]);+ let s = any_ascii(&mut a);+ let p = any_ascii(&mut b);+ let ok = writable_triple(s, p);+ assert!(ok == (s.as_bytes()[0] != b'"' && p.as_bytes()[0] == b'<'));+ }++ /// TCB-20, one rule-table position at a time. `parse_pat` and+ /// `Pat::render` are inverse, so a constant never renders to something that+ /// reads back as a variable. Because `parse_pat` returns+ /// `anyhow::Result`, this also proves it cannot panic on any input of the+ /// bounded length.+ /// TCB-20. **THIS HARNESS DOES NOT TERMINATE AND IS NOT IN `make verify`.**+ /// It is kept because the attempt is worth more written down than deleted,+ /// and because a reader who wants to finish it should not have to rediscover+ /// why it is hard.+ ///+ /// Measured on this machine, with nothing else competing:+ ///+ /// | input | outcome |+ /// |---|---|+ /// | 4 ASCII bytes, unconstrained | no verdict at 7 minutes, 3.0 GB |+ /// | 2 ASCII bytes, unconstrained | no verdict at 8 minutes, 2.8 GB |+ /// | 2 ASCII bytes, first byte constrained to `?`, `<` or `"` | no verdict at 14 minutes, 9.5 GB |+ ///+ /// The cost is not the input. `parse_pat` returns `anyhow::Result` and every+ /// refusal formats a message naming the line and the position, so the+ /// function body carries the whole formatting machinery, and CBMC flattens a+ /// function before it solves: an `assume` that makes the refusals infeasible+ /// is a constraint for the solver, not a cut in the program, so it does not+ /// remove the work. Constraining the input made it WORSE, which is the+ /// evidence for that reading.+ ///+ /// What would close it: lift the classification out of `parse_pat` into a+ /// pure function returning an `Option<Pat>`, with `parse_pat` reduced to+ /// that call plus the error messages. Then the harness targets a function+ /// with no allocation and no formatting, and the messages stay where they+ /// are. That is a refactor of shipped code and it was not made here.+ ///+ /// Until then the property is sampled, not proved:+ /// `boundary_tests::tcb_20_parse_pat_and_render_are_inverse` over 1024 cases,+ /// and `tests/certificate_boundary_proptest.rs` over whole rule tables.+ #[kani::proof]+ #[kani::unwind(8)]+ fn parse_pat_and_render_are_inverse() {+ let mut buf = [0u8; 2];+ let field = any_ascii(&mut buf);+ kani::assume(!field.contains('\t') && !field.contains('\n'));+ let head = field.as_bytes()[0];+ kani::assume(head == b'?' || head == b'<' || head == b'"');+ let p = parse_pat(field, 1, "position").expect("the accepted spellings parse");+ assert!(p.render() == field);+ }+}diff --git a/src/tableaux.rs b/src/tableaux.rsindex b77c4bc8..1b4a905f 100644--- a/src/tableaux.rs+++ b/src/tableaux.rs@@ -3856,3 +3856,198 @@ impl DlReasoner {self.emit(&tableau, &ind_to_node, self.abox_axioms(), dir)}}++// ─────────────────────────────────────────────────────────────────────────────+// The model certificate's serialisation boundary+//+// `axioms.tsv` is tab separated at the top level and SPACE separated inside a+// concept, and it carries IRIs and literals. `docs/trusted-computing-base.md`+// calls that TCB-25 and TCB-26: `name_is_safe` is the only explicit injection+// guard in this codebase, and the argument that it covers every name that gets+// written is an argument about two loops agreeing rather than a check.+// ─────────────────────────────────────────────────────────────────────────────++#[cfg(test)]+mod certificate_boundary_tests {+ use super::*;+ use proptest::prelude::*;++ /// Names drawn from exactly the two sides of `name_is_safe`: some that must+ /// pass, some that must not, and the grammar's own keywords, which must be+ /// harmless because the encoding is prefix and positional rather than+ /// delimited.+ fn name() -> impl Strategy<Value = String> {+ prop_oneof![+ Just("<http://e/A>".to_string()),+ Just("\"lit\"".to_string()),+ Just("_:b0".to_string()),+ // Keywords of the concept grammar.+ Just("atom".to_string()),+ Just("top".to_string()),+ Just("not".to_string()),+ Just("some".to_string()),+ Just("min".to_string()),+ Just("2".to_string()),+ // Must be refused.+ Just("a b".to_string()),+ Just("a\tb".to_string()),+ Just("a\nb".to_string()),+ Just("a\rb".to_string()),+ Just(String::new()),+ "[^ \t\n\r]{1,5}",+ ]+ }++ /// A concept over name SLOTS rather than interned ids, so the shape can be+ /// generated once and instantiated against whatever names the case drew.+ #[derive(Clone, Debug)]+ enum Shape {+ Top,+ Bot,+ Atom(usize),+ NegAtom(usize),+ And(Vec<Shape>),+ Or(Vec<Shape>),+ Exists(usize, Box<Shape>),+ ForAll(usize, Box<Shape>),+ Min(usize, u32, Box<Shape>),+ Max(usize, u32, Box<Shape>),+ }++ fn shape() -> impl Strategy<Value = Shape> {+ let leaf = prop_oneof![+ Just(Shape::Top),+ Just(Shape::Bot),+ (0usize..6).prop_map(Shape::Atom),+ (0usize..6).prop_map(Shape::NegAtom),+ ];+ leaf.prop_recursive(3, 12, 3, |inner| {+ prop_oneof![+ prop::collection::vec(inner.clone(), 0..3).prop_map(Shape::And),+ prop::collection::vec(inner.clone(), 0..3).prop_map(Shape::Or),+ (0usize..6, inner.clone()).prop_map(|(r, f)| Shape::Exists(r, Box::new(f))),+ (0usize..6, inner.clone()).prop_map(|(r, f)| Shape::ForAll(r, Box::new(f))),+ (0usize..6, 0u32..3, inner.clone())+ .prop_map(|(r, n, f)| Shape::Min(r, n, Box::new(f))),+ (0usize..6, 0u32..3, inner).prop_map(|(r, n, f)| Shape::Max(r, n, Box::new(f))),+ ]+ })+ }++ fn build(s: &Shape, ids: &[u32]) -> Concept {+ let pick = |i: usize| ids[i % ids.len()];+ match s {+ Shape::Top => Concept::Top,+ Shape::Bot => Concept::Bottom,+ Shape::Atom(i) => Concept::Atom(pick(*i)),+ Shape::NegAtom(i) => Concept::NegAtom(pick(*i)),+ Shape::And(cs) => Concept::And(cs.iter().map(|c| build(c, ids)).collect()),+ Shape::Or(cs) => Concept::Or(cs.iter().map(|c| build(c, ids)).collect()),+ Shape::Exists(r, f) => Concept::Exists(pick(*r), Box::new(build(f, ids))),+ Shape::ForAll(r, f) => Concept::ForAll(pick(*r), Box::new(build(f, ids))),+ Shape::Min(r, n, f) => Concept::MinCard(pick(*r), *n, Box::new(build(f, ids))),+ Shape::Max(r, n, f) => Concept::MaxCard(pick(*r), *n, Box::new(build(f, ids))),+ }+ }++ /// How many space-separated tokens the encoding in `write_concept` must+ /// produce for a concept, derived from the shape alone. Written out here so+ /// it is an independent statement of the grammar rather than a second call+ /// to the same code.+ fn tokens(c: &Concept) -> usize {+ match c {+ Concept::Top | Concept::Bottom => 1,+ Concept::Atom(_) => 2,+ Concept::NegAtom(_) => 3,+ Concept::And(cs) => nary_tokens(cs, &Concept::Top),+ Concept::Or(cs) => nary_tokens(cs, &Concept::Bottom),+ Concept::Exists(_, f) | Concept::ForAll(_, f) => 2 + tokens(f),+ Concept::MinCard(_, _, f) | Concept::MaxCard(_, _, f) => 3 + tokens(f),+ }+ }++ fn nary_tokens(cs: &[Concept], unit: &Concept) -> usize {+ match cs.split_first() {+ None => tokens(unit),+ Some((head, [])) => tokens(head),+ Some((head, rest)) => 1 + tokens(head) + nary_tokens(rest, unit),+ }+ }++ proptest! {+ #![proptest_config(ProptestConfig { cases: 512, ..ProptestConfig::default() })]++ /// TCB-25. `name_is_safe` refuses exactly the names that would break the+ /// format: empty, or carrying a space, tab, carriage return or newline.+ #[test]+ fn tcb_25_name_is_safe_refuses_every_separator(n in name()) {+ let safe = name_is_safe(&n);+ prop_assert_eq!(+ safe,+ !n.is_empty() && !n.contains([' ', '\t', '\n', '\r'])+ );+ }++ /// TCB-25. Given names the guard accepts, a concept serialises to+ /// exactly the number of space-separated tokens its shape determines.+ ///+ /// This is the property that makes the encoding readable back: the+ /// grammar is prefix and positional, so a name that happens to be a+ /// keyword (`atom`, `top`, `min`) is harmless, and a name carrying a+ /// space is not. If the guard ever stopped refusing one, the token count+ /// would drift from the shape and the checker would read a different+ /// concept from the one the reasoner decided about.+ #[test]+ fn tcb_25_a_safe_concept_serialises_to_the_tokens_its_shape_demands(+ ns in prop::collection::vec(name(), 1..6),+ sh in shape(),+ ) {+ let mut interner = Interner::new();+ let ids: Vec<u32> = ns.iter().map(|n| interner.intern(n)).collect();+ let c = build(&sh, &ids);+ let s = concept_string(&interner, &c);+ if ns.iter().all(|n| name_is_safe(n)) {+ prop_assert_eq!(+ s.split(' ').count(), tokens(&c),+ "token count drifted from the shape: {:?}", s+ );+ prop_assert!(!s.contains('\t') && !s.contains('\n') && !s.contains('\r'));+ }+ }++ /// TCB-25 for `axiom_line`. Every axiom is a tab-separated record whose+ /// field count its variant fixes, so a name carrying a tab would add a+ /// field and the checker would read a different axiom.+ #[test]+ fn tcb_25_an_axiom_line_has_the_fields_its_variant_fixes(n in name()) {+ let mut interner = Interner::new();+ let id = interner.intern(&n);+ let c = Concept::Atom(id);+ let cases: Vec<(DlAxiom, usize)> = vec![+ (DlAxiom::Sub(c.clone(), c.clone()), 3),+ (DlAxiom::Disjoint(c.clone(), c.clone()), 3),+ (DlAxiom::Domain(id, c.clone()), 3),+ (DlAxiom::Range(id, c.clone()), 3),+ (DlAxiom::SubRole(id, id), 3),+ (DlAxiom::Trans(id), 2),+ (DlAxiom::Sym(id), 2),+ (DlAxiom::Inv(id, id), 3),+ (DlAxiom::InvFunc(id), 2),+ (DlAxiom::Inst(id, c.clone()), 3),+ (DlAxiom::Rel(id, id, id), 4),+ (DlAxiom::Indiv(id), 2),+ (DlAxiom::NonEmpty(c), 2),+ ];+ for (a, want) in cases {+ let line = axiom_line(&interner, &a);+ if name_is_safe(&n) {+ prop_assert_eq!(+ line.split('\t').count(), want,+ "{:?} is not {} tab-separated fields", line, want+ );+ prop_assert!(!line.contains('\n') && !line.contains('\r'));+ }+ }+ }+ }+}diff --git a/tests/certificate_boundary_proptest.proptest-regressions b/tests/certificate_boundary_proptest.proptest-regressionsnew file mode 100644index 00000000..db64f378--- /dev/null+++ b/tests/certificate_boundary_proptest.proptest-regressions@@ -0,0 +1,26 @@+# Seeds for failure cases proptest has generated in the past. It is+# automatically read and these particular cases re-run before any+# novel cases are generated.+#+# It is recommended to check this file in to source control so that+# everyone who runs the test benefits from these saved cases.+#+# What is kept here is curated. Seeds produced by deliberately breaking the+# engine, to show that a property can fail at all, are NOT kept: they pin+# nothing, because the code they failed against never existed. What is kept is+# every seed that found a real defect.+#+# 14 September 2026. `rdfs7` concluded a triple with a blank node in predicate+# position, from `:p rdfs:subPropertyOf [ owl:inverseOf :q ]`, which is what the+# OWL 2 mapping to RDF produces for `SubObjectPropertyOf(:p ObjectInverseOf(:q))`.+# The materialiser then failed partway through inserting the batch, leaving the+# store holding an arbitrary, hash-order-dependent prefix of the inferences with+# no certificate covering any of them, and returned the parser's error.+# Fixed by `writable_triple` in `src/reason.rs`; pinned by+# `tests/reason_unwritable_predicate_test.rs`.+cc be2b5f1c6ff62a60dd37b1c2b235744ffac11721100af4fe3785ccac4ff657b3 # shrinks to nt = "<http://e/A> <http://e/~a> <http://e/A> .\n<http://e/~a> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> _:b0 .\n"+cc 9cc33b8d0b2f16258f38d36e139ea8ff95bedcb902c4ec1eefef030c3e179e60 # shrinks to nt = "<http://e/q> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> _:b0 .\n<http://e/A> <http://e/q> <http://e/A> .\n"+cc c9bc9ffd7637a23832cc3aa06ba54e83e86e1fb28484cc5f4e45e395997f4605 # shrinks to nt = "<http://e/A> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> _:b0 .\n<http://e/A> <http://e/A> <http://e/A> .\n"+cc bb07b46dc697a2aae5a49c5aaf4a3d920e4178c9b6f1a84e95bdade63e6110f9 # shrinks to nt = "<http://e/a%09b> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> _:b0 .\n<http://e/A> <http://e/a%09b> <http://e/A> .\n"+cc 22a2833c5a8a535bae6ca4b2a9272ecc987551739657463b7b0a39cf2389bbdb # shrinks to nt = "<http://e/A> <http://e/A> <http://e/A> .\n<http://e/A> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> _:b0 .\n"+cc 9afb7684ba21383452bd0dc23e31f66c0163cfa7abbb209b0157e878364efd5d # shrinks to nt = "<http://e/A> <http://e/q> <http://e/A> .\n<http://e/q> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> _:b0 .\n"diff --git a/tests/certificate_boundary_proptest.rs b/tests/certificate_boundary_proptest.rsnew file mode 100644index 00000000..ebba0992--- /dev/null+++ b/tests/certificate_boundary_proptest.rs@@ -0,0 +1,1277 @@+//! Property tests for the trusted computing base of the Lean certificate layer.+//!+//! `lean/` proves a conditional: IF the asserted graph is `G` and IF these steps+//! check, THEN the conclusions hold in every model of `G`. Everything to the+//! left of that "if" is the Rust writing down the truth. If `asserted.tsv` is+//! not the graph the engine reasoned over, or a step is not a step it took, a+//! valid Lean proof certifies a false claim.+//!+//! That boundary is small and it is enumerated in+//! `docs/trusted-computing-base.md` as `TCB-1` to `TCB-29`. This file+//! property-tests it. The identifiers in the test names are the ones in that+//! document, and a reader should be able to go from either to the other.+//!+//! # Why properties and not examples+//!+//! The certificate format is tab-separated, has no escaping layer of its own,+//! and carries arbitrary RDF literals. `lean/OOCert/Parse.lean` says so in a+//! comment:+//!+//! > Tabs and newlines cannot occur inside an N-Triples term (they are+//! > escaped), so splitting on them is exact.+//!+//! That is the entire escaping argument, it is a claim about `oxrdf`, and+//! nothing re-checks it. A hand-written test uses the terms its author thought+//! of. The generators here are built to forge a derivation step: lexical forms+//! holding tabs, newlines, carriage returns, quotes and backslashes; a literal+//! spelled exactly like an IRI; a literal spelled exactly like a whole extra+//! TSV line; combining characters against their precomposed form; the empty+//! string; NUL.+//!+//! # What needs a Lean toolchain and what does not+//!+//! The properties do not. They are about the side of the boundary the proofs+//! assume, so they must hold whether or not `lake` is installed, and they+//! re-derive what the checker would demand instead of asking it. A Lean process+//! per generated case would not be a property test in any event.+//!+//! One test at the end is the exception, and it is there because re-deriving+//! what the checker demands means transcribing it, and a transcription is an+//! assumption at exactly the boundary this file exists to check.+//! `tcb_19_21_the_real_lean_checker_accepts_an_adversarial_table` puts the+//! adversarial shapes through `oo-horn check` itself. It skips loudly without a+//! toolchain, and `OO_REQUIRE_FIXTURES=1` turns that skip into a failure.+//!+//! `tests/lean_certificate_test.rs` and `tests/lean_horn_certificate_test.rs`+//! remain the corpus-wide end-to-end gates.++mod common;++use std::collections::BTreeSet;+use std::path::{Path, PathBuf};+use std::sync::Arc;++use open_ontologies::graph::GraphStore;+use open_ontologies::reason::{parse_rules, rules_tsv, AtomPat, InferenceTarget, Pat, Reasoner, RulePattern};+use proptest::prelude::*;++// ─────────────────────────────────────────────────────────────────────────────+// Generators+//+// Everything here is chosen to break a tab-separated format, not to look like+// realistic data. Realistic data is what the rest of the suite already covers.+// ─────────────────────────────────────────────────────────────────────────────++/// Lexical forms that attack the serialisation.+fn adversarial_lexical() -> impl Strategy<Value = String> {+ prop_oneof![+ // The separators themselves. If any of these reaches a certificate+ // file unescaped, a line gains a field or splits in two.+ Just("\t".to_string()),+ Just("\n".to_string()),+ Just("\r".to_string()),+ Just("\r\n".to_string()),+ // A whole forged line. If the escaping fails, this one literal becomes+ // a second `asserted.tsv` triple that the store never held.+ Just("x\n<http://e/forged>\t<http://e/p>\t<http://e/o>".to_string()),+ // A forged field. Splits one triple into four fields.+ Just("a\tb".to_string()),+ // Quote and backslash: the escape machinery's own characters.+ Just("\"".to_string()),+ Just("\\".to_string()),+ Just("\\\\".to_string()),+ // Already-escaped spellings. `"a\tb"` as six characters must not be+ // confused with `"a<TAB>b"`, in either direction.+ Just("\\t".to_string()),+ Just("\\n".to_string()),+ Just("\\u0009".to_string()),+ // A literal whose lexical form is exactly an IRI. TCB-5.+ Just("<http://e/s>".to_string()),+ // A literal that looks like a blank node, and one that looks like a+ // rule-table variable.+ Just("_:b0".to_string()),+ Just("?x".to_string()),+ // Unicode: a combining sequence against its precomposed form. Both+ // sides compare bytes, so these must stay distinct all the way through.+ Just("e\u{0301}".to_string()),+ Just("\u{00e9}".to_string()),+ // Control characters and the empty string.+ Just("\u{0000}".to_string()),+ Just("\u{007f}".to_string()),+ Just("".to_string()),+ Just(" ".to_string()),+ Just(" .".to_string()),+ Just(" . \n".to_string()),+ // Ordinary text, so the graph is not pathological in every position.+ "[a-z]{1,5}",+ ]+}++/// The path segment of a generated IRI. Restricted to characters `oxiri`+/// accepts, because `TCB_4_separators_cannot_enter_an_iri` covers the rejection+/// of the rest and a graph that never loads tests nothing.+fn iri_local() -> impl Strategy<Value = String> {+ prop_oneof![+ Just("A".to_string()),+ Just("B".to_string()),+ Just("C".to_string()),+ Just("p".to_string()),+ Just("q".to_string()),+ Just("x".to_string()),+ // Percent-encoded tab. Legal in an IRI, and a reader that decoded+ // percent-escapes on the way into a certificate would be forging.+ Just("a%09b".to_string()),+ Just("a%0Ab".to_string()),+ // A quote and a bracket inside an IRI path.+ Just("a'b".to_string()),+ Just("~a".to_string()),+ ]+}++fn bnode_label() -> impl Strategy<Value = String> {+ prop_oneof![Just("b0".to_string()), Just("b1".to_string()), Just("x".to_string())]+}++#[derive(Clone, Debug)]+enum Term {+ Iri(String),+ Blank(String),+ Lit(String),+ LitLang(String, String),+ LitTyped(String, String),+}++/// Escape a lexical form into the N-Triples `STRING_LITERAL_QUOTE` production.+/// This is the test's own escaper, deliberately not the engine's, so a bug in+/// the engine's cannot hide behind it.+fn nt_escape(s: &str) -> String {+ let mut out = String::new();+ for c in s.chars() {+ match c {+ '\\' => out.push_str("\\\\"),+ '"' => out.push_str("\\\""),+ '\n' => out.push_str("\\n"),+ '\r' => out.push_str("\\r"),+ '\t' => out.push_str("\\t"),+ c if (c as u32) < 0x20 || c as u32 == 0x7f => {+ out.push_str(&format!("\\u{:04X}", c as u32))+ }+ c => out.push(c),+ }+ }+ out+}++impl Term {+ fn to_nt(&self) -> String {+ match self {+ Term::Iri(l) => format!("<http://e/{l}>"),+ Term::Blank(b) => format!("_:{b}"),+ Term::Lit(s) => format!("\"{}\"", nt_escape(s)),+ Term::LitLang(s, l) => format!("\"{}\"@{}", nt_escape(s), l),+ Term::LitTyped(s, d) => format!("\"{}\"^^<http://e/{}>", nt_escape(s), d),+ }+ }+}++fn arb_object() -> impl Strategy<Value = Term> {+ prop_oneof![+ iri_local().prop_map(Term::Iri),+ bnode_label().prop_map(Term::Blank),+ adversarial_lexical().prop_map(Term::Lit),+ adversarial_lexical().prop_map(|s| Term::LitLang(s, "en-gb".to_string())),+ (adversarial_lexical(), iri_local()).prop_map(|(s, d)| Term::LitTyped(s, d)),+ ]+}++fn arb_subject() -> impl Strategy<Value = Term> {+ prop_oneof![iri_local().prop_map(Term::Iri), bnode_label().prop_map(Term::Blank)]+}++const RDF_TYPE: &str = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>";+const RDFS_SUBCLASS: &str = "<http://www.w3.org/2000/01/rdf-schema#subClassOf>";+const RDFS_SUBPROP: &str = "<http://www.w3.org/2000/01/rdf-schema#subPropertyOf>";+const RDFS_DOMAIN: &str = "<http://www.w3.org/2000/01/rdf-schema#domain>";+const RDFS_RANGE: &str = "<http://www.w3.org/2000/01/rdf-schema#range>";++/// Predicates are biased hard towards the vocabulary the reasoner fires on.+/// A generated graph with no schema triples produces no derivations, and a+/// certificate with no steps tests nothing about steps.+fn arb_predicate() -> impl Strategy<Value = String> {+ prop_oneof![+ 6 => prop_oneof![+ Just(RDF_TYPE.to_string()),+ Just(RDFS_SUBCLASS.to_string()),+ Just(RDFS_SUBPROP.to_string()),+ Just(RDFS_DOMAIN.to_string()),+ Just(RDFS_RANGE.to_string()),+ ],+ 1 => iri_local().prop_map(|l| format!("<http://e/{l}>")),+ ]+}++/// An N-Triples document. Returned as text because loading it is the only way+/// to get terms into the store through the path a user's data takes.+fn arb_graph() -> impl Strategy<Value = String> {+ prop::collection::vec((arb_subject(), arb_predicate(), arb_object()), 0..8).prop_map(|ts| {+ let mut out = String::new();+ for (s, p, o) in ts {+ out.push_str(&s.to_nt());+ out.push(' ');+ out.push_str(&p);+ out.push(' ');+ out.push_str(&o.to_nt());+ out.push_str(" .\n");+ }+ out+ })+}++// ─────────────────────────────────────────────────────────────────────────────+// Helpers+// ─────────────────────────────────────────────────────────────────────────────++fn repo() -> PathBuf {+ PathBuf::from(env!("CARGO_MANIFEST_DIR"))+}++/// A scratch directory unique to the calling process and to a counter, because+/// proptest runs hundreds of cases and cargo runs test binaries in parallel.+fn scratch(tag: &str) -> PathBuf {+ use std::sync::atomic::{AtomicUsize, Ordering};+ static N: AtomicUsize = AtomicUsize::new(0);+ let dir = std::env::temp_dir().join(format!(+ "oo-tcb-{tag}-{}-{}",+ std::process::id(),+ N.fetch_add(1, Ordering::Relaxed)+ ));+ let _ = std::fs::remove_dir_all(&dir);+ std::fs::create_dir_all(&dir).unwrap();+ dir+}++/// The triples of a store, as the N-Triples spellings the certificate uses.+fn store_set(g: &GraphStore) -> BTreeSet<(String, String, String)> {+ g.all_triples().unwrap().into_iter().collect()+}++/// Split a TSV line, refusing to guess. Returns the fields.+fn fields(line: &str) -> Vec<&str> {+ line.split('\t').collect()+}++/// Every rule name `OOCert.Rule.name` gives, read out of the Lean source.+///+/// Read rather than duplicated: a rule renamed on one side and not the other+/// is exactly the drift this is here to catch, and a hardcoded list in this+/// file would drift with it.+fn lean_rule_names() -> BTreeSet<String> {+ let src = std::fs::read_to_string(repo().join("lean/OOCert/Rules.lean"))+ .expect("lean/OOCert/Rules.lean is tracked by this repository");+ let start = src.find("def Rule.name").expect("Rule.name is defined");+ let end = src[start..].find("def Rule.all").expect("Rule.all follows Rule.name") + start;+ let body = &src[start..end];+ let mut out = BTreeSet::new();+ let mut rest = body;+ while let Some(i) = rest.find('"') {+ rest = &rest[i + 1..];+ let Some(j) = rest.find('"') else { break };+ out.insert(rest[..j].to_string());+ rest = &rest[j + 1..];+ }+ assert!(out.len() >= 20, "parsed {} rule names out of Rules.lean", out.len());+ out+}++/// Run a certified reason over `nt`, returning the store's before and after+/// triple sets and the two certificate files.+struct Run {+ before: BTreeSet<(String, String, String)>,+ after: BTreeSet<(String, String, String)>,+ asserted: String,+ derivations: String,+ reported_inferred: usize,+ reported_asserted: usize,+ reported_derivations: usize,+}++fn certified_run(nt: &str) -> Option<Run> {+ let g = Arc::new(GraphStore::new());+ // A generated document that will not parse is not a counterexample to+ // anything here: the injection properties are about what happens AFTER a+ // term is in the store.+ g.load_ntriples(nt).ok()?;+ let before = store_set(&g);+ let dir = scratch("run");+ let out = Reasoner::run_full(+ &g,+ "owl-rl-ext",+ true,+ InferenceTarget::DefaultGraph,+ Some(&dir),+ )+ .expect("a certified run over a loaded graph does not fail");+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ let after = store_set(&g);+ let r = Run {+ before,+ after,+ asserted: std::fs::read_to_string(dir.join("asserted.tsv")).unwrap(),+ derivations: std::fs::read_to_string(dir.join("derivations.tsv")).unwrap(),+ reported_inferred: json["inferred_count"].as_u64().unwrap() as usize,+ reported_asserted: json["certificate"]["asserted"].as_u64().unwrap() as usize,+ reported_derivations: json["certificate"]["derivations"].as_u64().unwrap() as usize,+ };+ let _ = std::fs::remove_dir_all(&dir);+ Some(r)+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-1, TCB-4, TCB-5: the serialisation cannot be forged from inside a term+// ─────────────────────────────────────────────────────────────────────────────++proptest! {+ #![proptest_config(ProptestConfig { cases: 256, ..ProptestConfig::default() })]++ /// TCB-1. Every line of `asserted.tsv` is exactly three tab-separated+ /// fields, and there are as many lines as the run started with triples.+ ///+ /// This is the property a forged literal breaks. A term carrying a tab+ /// gives a line four fields; a term carrying a newline turns one line into+ /// two, and the second one is a triple nobody asserted.+ #[test]+ fn tcb_1_asserted_lines_are_exactly_three_fields(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ let lines: Vec<&str> = run.asserted.lines().collect();+ for (i, line) in lines.iter().enumerate() {+ prop_assert_eq!(+ fields(line).len(), 3,+ "asserted.tsv line {} is not three fields: {:?}", i, line+ );+ }+ prop_assert_eq!(lines.len(), run.reported_asserted);+ prop_assert_eq!(lines.len(), run.before.len());+ // A file that does not end in a newline would merge with anything+ // appended, and the Lean parser splits on "\n".+ prop_assert!(run.asserted.is_empty() || run.asserted.ends_with('\n'));+ }++ /// TCB-4. No term written to a certificate file contains a field or record+ /// separator. TCB-1 to TCB-3 detect the consequence; this states the cause,+ /// because it is the one that has to survive an `oxrdf` upgrade.+ #[test]+ fn tcb_4_no_term_carries_a_separator(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ for line in run.asserted.lines().chain(run.derivations.lines()) {+ for f in fields(line) {+ prop_assert!(+ !f.contains('\t') && !f.contains('\n') && !f.contains('\r'),+ "field carries a separator: {:?}", f+ );+ }+ }+ }++ /// TCB-5. A literal and an IRI never have the same spelling, so the+ /// checkers, which compare terms as opaque strings, cannot confuse them.+ #[test]+ fn tcb_5_literal_and_iri_spellings_are_disjoint(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ for line in run.asserted.lines() {+ for f in fields(line) {+ let looks_iri = f.starts_with('<') && f.ends_with('>');+ let looks_lit = f.starts_with('"');+ prop_assert!(+ !(looks_iri && looks_lit),+ "term is both an IRI and a literal: {:?}", f+ );+ prop_assert!(+ looks_iri || looks_lit || f.starts_with("_:"),+ "term is in no N-Triples shape: {:?}", f+ );+ }+ }+ }+}++/// TCB-4, the other half. A separator cannot get INTO an IRI, because the+/// parser refuses one. Not a property test: the point is the exact rejection,+/// and there are only a handful of ways to write the attack.+///+/// This pins a guarantee `oxiri` provides and this repository relies on without+/// re-checking. If an upgrade ever accepts one of these, `NamedNodeRef`'s+/// `Display` is `write!(f, "<{}>", self.as_str())` with no escaping, and the+/// character goes straight into `asserted.tsv`.+#[test]+fn tcb_4_separators_cannot_enter_an_iri() {+ let attacks = [+ ("raw tab", "<http://e/s> <http://e/p> <http://e/a\tb> .\n"),+ ("escaped tab", "<http://e/s> <http://e/p> <http://e/a\\u0009b> .\n"),+ ("escaped newline", "<http://e/s> <http://e/p> <http://e/a\\u000Ab> .\n"),+ ("escaped cr", "<http://e/s> <http://e/p> <http://e/a\\u000Db> .\n"),+ ("escaped space", "<http://e/s> <http://e/p> <http://e/a\\u0020b> .\n"),+ ("raw space", "<http://e/s> <http://e/p> <http://e/a b> .\n"),+ (+ "tab in the datatype IRI",+ "<http://e/s> <http://e/p> \"v\"^^<http://e/a\\u0009b> .\n",+ ),+ ];+ for (what, nt) in attacks {+ let g = GraphStore::new();+ let r = g.load_ntriples(nt);+ assert!(+ r.is_err(),+ "the parser accepted {what}, so a separator can reach an IRI and \+ `asserted.tsv` can be forged from inside a term"+ );+ }+}++/// TCB-4, the third way in. Turtle's long-string form carries RAW control+/// characters, so it is the realistic route to a tab inside a literal: no+/// escape sequence is involved on the way in, and the engine's own serialiser+/// is the only thing that puts one back.+#[test]+fn tcb_4_raw_control_characters_from_turtle_are_escaped_on_the_way_out() {+ let ttl = "<http://e/s> <http://e/p> \"\"\"a\tb\nc\rd\"\"\" .\n";+ let g = Arc::new(GraphStore::new());+ g.load_turtle(ttl, None).expect("Turtle long strings carry raw control characters");+ let dir = scratch("rawctl");+ Reasoner::run_full(&g, "owl-rl-ext", false, InferenceTarget::DefaultGraph, Some(&dir)).unwrap();+ let asserted = std::fs::read_to_string(dir.join("asserted.tsv")).unwrap();+ assert_eq!(+ asserted.lines().count(),+ 1,+ "a raw tab and newline in a literal split the line: {asserted:?}"+ );+ assert_eq!(fields(asserted.lines().next().unwrap()).len(), 3, "{asserted:?}");+ let _ = std::fs::remove_dir_all(&dir);+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-6, TCB-7: `asserted.tsv` is the graph+// ─────────────────────────────────────────────────────────────────────────────++proptest! {+ #![proptest_config(ProptestConfig { cases: 128, ..ProptestConfig::default() })]++ /// TCB-6 and TCB-7. `asserted.tsv` round-trips: reading it back into a+ /// fresh store gives exactly the graph the run started from. Nothing+ /// dropped, nothing added.+ ///+ /// The re-read goes through the N-Triples parser, a different code path+ /// from the one that wrote the file, so a term that serialises to something+ /// unreadable fails here rather than at the checker.+ #[test]+ fn tcb_6_7_asserted_round_trips_to_the_same_graph(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ let mut back = String::new();+ for line in run.asserted.lines() {+ let f = fields(line);+ prop_assert_eq!(f.len(), 3, "{:?}", line);+ back.push_str(f[0]);+ back.push(' ');+ back.push_str(f[1]);+ back.push(' ');+ back.push_str(f[2]);+ back.push_str(" .\n");+ }+ let fresh = GraphStore::new();+ fresh.load_ntriples(&back).map_err(|e| TestCaseError::fail(format!(+ "asserted.tsv does not read back as N-Triples: {e}"+ )))?;+ prop_assert_eq!(store_set(&fresh), run.before.clone());+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-2, TCB-8, TCB-10, TCB-11, TCB-12, TCB-13: the derivation record+// ─────────────────────────────────────────────────────────────────────────────++/// One parsed `derivations.tsv` line.+struct Step {+ rule: String,+ conclusion: (String, String, String),+ premises: Vec<(String, String, String)>,+}++fn parse_derivations(text: &str) -> Result<Vec<Step>, String> {+ let mut out = Vec::new();+ for (n, line) in text.lines().enumerate() {+ if line.is_empty() {+ continue;+ }+ let f = fields(line);+ if f.len() < 7 || !(f.len() - 1).is_multiple_of(3) {+ return Err(format!("line {n}: {} fields: {line:?}", f.len()));+ }+ let mut triples = Vec::new();+ for c in f[1..].chunks(3) {+ triples.push((c[0].to_string(), c[1].to_string(), c[2].to_string()));+ }+ let conclusion = triples.remove(0);+ out.push(Step { rule: f[0].to_string(), conclusion, premises: triples });+ }+ Ok(out)+}++proptest! {+ #![proptest_config(ProptestConfig { cases: 128, ..ProptestConfig::default() })]++ /// TCB-2. `derivations.tsv` lines are `1 + 3 * (1 + premises)` fields with+ /// at least one premise. This is the shape `OOCert.Parse.parseSteps`+ /// demands, re-derived here rather than asked of the checker.+ #[test]+ fn tcb_2_derivation_lines_have_the_shape_the_parser_demands(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ let steps = parse_derivations(&run.derivations)+ .map_err(TestCaseError::fail)?;+ for s in &steps {+ prop_assert!(!s.premises.is_empty(), "rule {} emitted no premise", s.rule);+ }+ prop_assert!(run.derivations.is_empty() || run.derivations.ends_with('\n'));+ }++ /// TCB-10 and TCB-11. The conclusions are exactly the triples the run added+ /// to the store, one line each.+ ///+ /// The unsafe direction is a triple in the store with no line covering it:+ /// an uncertified inference sitting under the certificate's cover, which a+ /// consumer reading `{"ok": true}` has no way to see.+ #[test]+ fn tcb_10_11_every_inference_is_certified_exactly_once(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ let steps = parse_derivations(&run.derivations).map_err(TestCaseError::fail)?;+ let inferred: BTreeSet<_> = run.after.difference(&run.before).cloned().collect();+ let concluded: BTreeSet<_> = steps.iter().map(|s| s.conclusion.clone()).collect();+ prop_assert_eq!(&concluded, &inferred, "certified set is not the inferred set");+ // One LINE per inference, not merely one distinct conclusion.+ prop_assert_eq!(steps.len(), inferred.len(), "a conclusion was certified twice");+ prop_assert_eq!(steps.len(), run.reported_derivations);+ prop_assert_eq!(inferred.len(), run.reported_inferred);+ }++ /// TCB-8. No conclusion appears in `asserted.tsv`. Within a run the engine+ /// captures the assertions before it materialises, so an inference cannot+ /// be read back as an axiom.+ ///+ /// ACROSS runs this does not hold, and no test here can make it: see+ /// `docs/trusted-computing-base.md` TCB-8 and decision 0001.+ #[test]+ fn tcb_8_no_inference_appears_among_the_assertions(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ let steps = parse_derivations(&run.derivations).map_err(TestCaseError::fail)?;+ let mut asserted = BTreeSet::new();+ for line in run.asserted.lines() {+ let f = fields(line);+ asserted.insert((f[0].to_string(), f[1].to_string(), f[2].to_string()));+ }+ for s in &steps {+ prop_assert!(+ !asserted.contains(&s.conclusion),+ "rule {} concluded a triple that asserted.tsv also claims: {:?}",+ s.rule, s.conclusion+ );+ }+ }++ /// TCB-12. Every premise is asserted or was concluded by an EARLIER line,+ /// so no step cites itself and no step cites a triple from the future.+ /// `OOCert.checkAll` demands this; the emitter has to supply it.+ #[test]+ fn tcb_12_premises_precede_their_conclusions(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ let steps = parse_derivations(&run.derivations).map_err(TestCaseError::fail)?;+ let mut known: BTreeSet<(String, String, String)> = BTreeSet::new();+ for line in run.asserted.lines() {+ let f = fields(line);+ known.insert((f[0].to_string(), f[1].to_string(), f[2].to_string()));+ }+ for (i, s) in steps.iter().enumerate() {+ for p in &s.premises {+ prop_assert!(+ known.contains(p),+ "step {i} ({}) cites a premise that is neither asserted nor concluded \+ earlier: {:?}", s.rule, p+ );+ }+ known.insert(s.conclusion.clone());+ }+ }++ /// TCB-13. Every rule name is one `OOCert.Rule.ofName?` accepts. The list is+ /// read out of `lean/OOCert/Rules.lean` at test time, so a rename on one+ /// side fails here.+ #[test]+ fn tcb_13_rule_names_are_names_the_checker_knows(nt in arb_graph()) {+ let Some(run) = certified_run(&nt) else { return Ok(()) };+ let known = lean_rule_names();+ let steps = parse_derivations(&run.derivations).map_err(TestCaseError::fail)?;+ for s in &steps {+ prop_assert!(known.contains(&s.rule), "unknown rule name {:?}", s.rule);+ }+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-19, TCB-20, TCB-21, TCB-22: the rule table+// ─────────────────────────────────────────────────────────────────────────────++/// A faithful transcription of `lean/OOCert/HornParse.lean`.+///+/// This is a model, not the checker. It exists so a generated table can be run+/// through BOTH parsers in a property test without a Lean toolchain in the+/// loop. Its fidelity is the assumption; `tests/lean_horn_certificate_test.rs`+/// pins the real thing on the built-in table.+mod lean_model {+ #[derive(Clone, Debug, PartialEq, Eq)]+ pub enum Pat {+ Const(String),+ Var(String),+ }++ /// `patOf`: any field starting with `?` is a variable, everything else a+ /// constant. Note it is strictly more permissive than the Rust parser.+ pub fn pat_of(s: &str) -> Pat {+ if s.starts_with('?') {+ Pat::Var(s.chars().skip(1).collect())+ } else {+ Pat::Const(s.to_string())+ }+ }++ pub fn pat_str(p: &Pat) -> String {+ match p {+ Pat::Const(c) => c.clone(),+ Pat::Var(v) => format!("?{v}"),+ }+ }++ #[derive(Clone, Debug, PartialEq, Eq)]+ pub struct Rule {+ pub name: String,+ pub body: Vec<[Pat; 3]>,+ pub head: [Pat; 3],+ }++ /// `ruleStr`: `String.intercalate "\t" ([name, len] ++ body ++ head)`.+ pub fn rule_str(r: &Rule) -> String {+ let mut parts: Vec<String> = vec![r.name.clone(), r.body.len().to_string()];+ for a in r.body.iter().chain(std::iter::once(&r.head)) {+ for p in a {+ parts.push(pat_str(p));+ }+ }+ parts.join("\t")+ }++ fn group_atoms(fs: &[&str]) -> Option<Vec<[Pat; 3]>> {+ if !fs.len().is_multiple_of(3) {+ return None;+ }+ Some(fs.chunks(3).map(|c| [pat_of(c[0]), pat_of(c[1]), pat_of(c[2])]).collect())+ }++ /// `parseRules`. `k.toNat?` is Lean's arbitrary-precision Nat parse, so a+ /// number too large for `usize` still parses there and then fails the field+ /// count; that is modelled with `u128` and a saturating comparison.+ pub fn parse_rules(content: &str) -> Result<Vec<Rule>, String> {+ let mut out = Vec::new();+ for (i, line) in content.split('\n').enumerate() {+ let n = i + 1;+ if line.is_empty() {+ continue;+ }+ let fs: Vec<&str> = line.split('\t').collect();+ if fs.len() < 2 {+ return Err(format!("rules line {n}: expected name, body length, patterns"));+ }+ let (name, k, rest) = (fs[0], fs[1], &fs[2..]);+ if k.is_empty() || !k.bytes().all(|b| b.is_ascii_digit()) {+ return Err(format!("rules line {n}: body length is not a number"));+ }+ let m: u128 = k.parse().map_err(|_| format!("rules line {n}: unrepresentable"))?;+ let want = m.saturating_mul(3).saturating_add(3);+ if rest.len() as u128 != want {+ return Err(format!("rules line {n}: expected {want} pattern fields"));+ }+ let m = m as usize;+ let (Some(body), Some(head)) = (group_atoms(&rest[..3 * m]), group_atoms(&rest[3 * m..]))+ else {+ return Err(format!("rules line {n}: malformed patterns"));+ };+ let [head] = <[_; 1]>::try_from(head).map_err(|_| "head".to_string())?;+ out.push(Rule { name: name.to_string(), body, head });+ }+ Ok(out)+ }+}++fn to_model(r: &RulePattern) -> lean_model::Rule {+ fn p(p: &Pat) -> lean_model::Pat {+ match p {+ Pat::Const(c) => lean_model::Pat::Const(c.clone()),+ Pat::Var(v) => lean_model::Pat::Var(v.clone()),+ }+ }+ fn a(a: &AtomPat) -> [lean_model::Pat; 3] {+ [p(&a.s), p(&a.p), p(&a.o)]+ }+ lean_model::Rule {+ name: r.name.clone(),+ body: r.body.iter().map(a).collect(),+ head: a(&r.head),+ }+}++/// Fields for a rule-table position. Adversarial in the directions that matter:+/// a constant that could be read as a variable, a variable whose name starts+/// with `?`, a term that is not in N-Triples spelling at all.+fn arb_rule_field() -> impl Strategy<Value = String> {+ prop_oneof![+ Just("?x".to_string()),+ Just("?y".to_string()),+ Just("??x".to_string()),+ Just("?".to_string()),+ Just("<http://e/A>".to_string()),+ Just("<http://e/B>".to_string()),+ Just(RDF_TYPE.to_string()),+ Just(RDFS_SUBCLASS.to_string()),+ Just("_:b0".to_string()),+ Just("\"lit\"".to_string()),+ Just("\"?x\"".to_string()),+ Just("\"<http://e/A>\"".to_string()),+ Just("".to_string()),+ Just("bare".to_string()),+ Just("5".to_string()),+ ]+}++fn arb_rule_line() -> impl Strategy<Value = String> {+ (+ prop_oneof![Just("r".to_string()), Just("5".to_string()), Just("".to_string()), Just("r\u{00e9}".to_string())],+ 0usize..3,+ prop::collection::vec(arb_rule_field(), 3..10),+ )+ .prop_map(|(name, m, mut fs)| {+ // Usually make the field count agree with the declared body length,+ // so the parser gets past its arity check often enough to test the+ // interesting part.+ fs.resize(3 * m + 3, "?x".to_string());+ let mut parts = vec![name, m.to_string()];+ parts.extend(fs);+ parts.join("\t")+ })+}++fn arb_rule_table() -> impl Strategy<Value = String> {+ prop::collection::vec(arb_rule_line(), 0..4).prop_map(|ls| {+ let mut s = ls.join("\n");+ if !s.is_empty() {+ s.push('\n');+ }+ s+ })+}++proptest! {+ #![proptest_config(ProptestConfig { cases: 512, ..ProptestConfig::default() })]++ /// TCB-20. `parse_rules` and `rules_tsv` are inverse on everything+ /// `parse_rules` accepts. In particular a constant never renders to+ /// something that reads back as a variable.+ ///+ /// This is what makes the re-parse guard inside `run_horn` meaningful: the+ /// engine writes `rules_tsv(&rules)` and checks it reads back as `rules`,+ /// and that check is only as good as the round trip being total rather than+ /// accidental on the tables anyone has tried.+ #[test]+ fn tcb_20_render_and_parse_are_inverse(text in arb_rule_table()) {+ let Ok(rules) = parse_rules(&text) else { return Ok(()) };+ let rendered = rules_tsv(&rules);+ let reparsed = parse_rules(&rendered)+ .map_err(|e| TestCaseError::fail(format!("rendered table does not parse: {e}")))?;+ prop_assert_eq!(reparsed, rules, "render/parse is not the identity on {:?}", rendered);+ // Idempotent, so a table that has been through the engine once is a+ // fixed point and its SHA-256 is stable.+ prop_assert_eq!(rules_tsv(&parse_rules(&rendered).unwrap()), rendered);+ }++ /// TCB-21. `rules_tsv` is byte-identical, per line, to+ /// `OOCert.HornParse.ruleStr` as transcribed in `lean_model`. The engine+ /// publishes a SHA-256 of its own rendering and `oo-horn` decides `entailed`+ /// against `entailed_under_supplied_rules` by comparing tables, so a+ /// difference of one byte changes a verdict.+ #[test]+ fn tcb_21_rust_and_lean_render_a_table_identically(text in arb_rule_table()) {+ let Ok(rules) = parse_rules(&text) else { return Ok(()) };+ let rust = rules_tsv(&rules);+ let lean: String = rules+ .iter()+ .map(|r| lean_model::rule_str(&to_model(r)) + "\n")+ .collect();+ prop_assert_eq!(rust, lean);+ }++ /// TCB-19. Whatever the Rust accepts, the Lean parser reads as the same+ /// table. The Rust is strictly stricter, and the strictness is all in the+ /// refusing direction, so the two can only disagree by the Rust accepting+ /// something the Lean reads differently. That is the failure this looks for.+ #[test]+ fn tcb_19_rust_and_lean_parse_a_table_the_same_way(text in arb_rule_table()) {+ let Ok(rules) = parse_rules(&text) else { return Ok(()) };+ let lean = lean_model::parse_rules(&text)+ .map_err(|e| TestCaseError::fail(format!(+ "the Rust accepted a table the Lean parser refuses ({e}): {text:?}"+ )))?;+ let mine: Vec<lean_model::Rule> = rules.iter().map(to_model).collect();+ prop_assert_eq!(lean, mine);+ }++ /// TCB-22, the parsing half. Both parsers skip empty lines, so a rule's+ /// index is its position in the parsed list on both sides, and+ /// `rules_tsv` never emits an empty line for that index to slip past.+ #[test]+ fn tcb_22_rendered_tables_have_no_empty_lines(text in arb_rule_table()) {+ let Ok(rules) = parse_rules(&text) else { return Ok(()) };+ let rendered = rules_tsv(&rules);+ prop_assert_eq!(rendered.lines().count(), rules.len());+ for line in rendered.lines() {+ prop_assert!(!line.is_empty());+ prop_assert!(!line.contains('\r'));+ }+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-3, TCB-22, TCB-23, TCB-24: the Horn certificate+// ─────────────────────────────────────────────────────────────────────────────++/// A rule table that `parse_rules` will accept, built rather than filtered:+/// every head position is a constant or a variable the body binds.+fn arb_valid_rule_table() -> impl Strategy<Value = String> {+ let atom = prop_oneof![+ Just(["?x".to_string(), RDF_TYPE.to_string(), "?c".to_string()]),+ Just(["?c".to_string(), RDFS_SUBCLASS.to_string(), "?d".to_string()]),+ Just(["?x".to_string(), "?p".to_string(), "?y".to_string()]),+ Just(["?p".to_string(), RDFS_DOMAIN.to_string(), "?c".to_string()]),+ Just(["?x".to_string(), RDF_TYPE.to_string(), "<http://e/A>".to_string()]),+ ];+ let head = prop_oneof![+ Just(["?x".to_string(), RDF_TYPE.to_string(), "<http://e/B>".to_string()]),+ Just(["<http://e/k>".to_string(), RDF_TYPE.to_string(), "<http://e/B>".to_string()]),+ // A head that puts a literal in subject position when `?c` binds one.+ // `run_horn` must refuse to write it rather than emit an unserialisable+ // triple, and must not use it as a premise for anything.+ Just(["?c".to_string(), RDF_TYPE.to_string(), "<http://e/C>".to_string()]),+ ];+ prop::collection::vec((prop::collection::vec(atom, 1..3), head), 1..3).prop_map(|rs| {+ let mut out = String::new();+ for (i, (body, head)) in rs.into_iter().enumerate() {+ // Keep only the head variables the body binds: the engine refuses a+ // table that does not, and a generator that produced them would+ // spend every case in the error path.+ let bound: BTreeSet<String> = body+ .iter()+ .flatten()+ .filter(|f| f.starts_with('?'))+ .cloned()+ .collect();+ if head.iter().any(|f| f.starts_with('?') && !bound.contains(f)) {+ continue;+ }+ let mut parts = vec![format!("r{i}"), body.len().to_string()];+ for a in body.iter().chain(std::iter::once(&head)) {+ parts.extend(a.iter().cloned());+ }+ out.push_str(&parts.join("\t"));+ out.push('\n');+ }+ if out.is_empty() {+ out.push_str(&format!("r0\t1\t?x\t{RDF_TYPE}\t?c\t?x\t{RDF_TYPE}\t<http://e/B>\n"));+ }+ out+ })+}++/// A `horn.tsv` line, parsed against the table it cites.+struct HornStep {+ rule: usize,+ binds: Vec<(String, String)>,+ conclusion: (String, String, String),+ premises: Vec<(String, String, String)>,+}++fn parse_horn(text: &str) -> Result<Vec<HornStep>, String> {+ let mut out = Vec::new();+ for (n, line) in text.lines().enumerate() {+ if line.is_empty() {+ continue;+ }+ let f = fields(line);+ if f.len() < 2 {+ return Err(format!("horn line {n}: {} fields", f.len()));+ }+ let rule: usize = f[0].parse().map_err(|_| format!("horn line {n}: rule index"))?;+ let k: usize = f[1].parse().map_err(|_| format!("horn line {n}: bind count"))?;+ if f.len() < 2 + 2 * k + 3 {+ return Err(format!("horn line {n}: too short for {k} bindings and a conclusion"));+ }+ let binds: Vec<(String, String)> = f[2..2 + 2 * k]+ .chunks(2)+ .map(|c| (c[0].to_string(), c[1].to_string()))+ .collect();+ let rest = &f[2 + 2 * k..];+ if !rest.len().is_multiple_of(3) {+ return Err(format!("horn line {n}: {} trailing fields", rest.len()));+ }+ let mut triples: Vec<(String, String, String)> = rest+ .chunks(3)+ .map(|c| (c[0].to_string(), c[1].to_string(), c[2].to_string()))+ .collect();+ let conclusion = triples.remove(0);+ out.push(HornStep { rule, binds, conclusion, premises: triples });+ }+ Ok(out)+}++/// Apply a written binding to a pattern, the way `OOCert.AtomPat.inst` does.+fn inst(p: &Pat, binds: &[(String, String)]) -> Option<String> {+ match p {+ Pat::Const(c) => Some(c.clone()),+ Pat::Var(v) => binds.iter().find(|(n, _)| n == v).map(|(_, t)| t.clone()),+ }+}++fn inst_atom(a: &AtomPat, binds: &[(String, String)]) -> Option<(String, String, String)> {+ Some((inst(&a.s, binds)?, inst(&a.p, binds)?, inst(&a.o, binds)?))+}++proptest! {+ #![proptest_config(ProptestConfig { cases: 64, ..ProptestConfig::default() })]++ /// TCB-3, TCB-22, TCB-23 and TCB-24 in one pass over a real `run_horn`.+ ///+ /// The substitution is re-applied here from `rules.tsv` and the written+ /// binding, independently of the engine. That is the whole point: if the+ /// emitter's premises are not the body under its own binding, the checker+ /// rejects and nobody learns why, and if they agree by construction in both+ /// places the agreement is worth nothing.+ #[test]+ fn tcb_3_22_23_24_horn_certificate_is_self_consistent(+ nt in arb_graph(),+ table in arb_valid_rule_table(),+ ) {+ let g = Arc::new(GraphStore::new());+ let Ok(_) = g.load_ntriples(&nt) else { return Ok(()) };+ let before = store_set(&g);++ let dir = scratch("horn");+ let rules_path = dir.join("input-rules.tsv");+ std::fs::write(&rules_path, &table).unwrap();+ let cert = dir.join("cert");+ let Ok(_) = Reasoner::run_horn(&g, &rules_path, &cert) else {+ let _ = std::fs::remove_dir_all(&dir);+ return Ok(());+ };++ let written = std::fs::read_to_string(cert.join("rules.tsv")).unwrap();+ let asserted = std::fs::read_to_string(cert.join("asserted.tsv")).unwrap();+ let horn = std::fs::read_to_string(cert.join("horn.tsv")).unwrap();+ let _ = std::fs::remove_dir_all(&dir);++ // TCB-24: nothing is materialised under a supplied table.+ prop_assert_eq!(store_set(&g), before.clone(), "run_horn wrote into the store");++ // TCB-18/20: what is checked is what was evaluated.+ let rules = parse_rules(&written).map_err(|e| TestCaseError::fail(e.to_string()))?;+ prop_assert_eq!(rules_tsv(&rules), written.clone());++ let steps = parse_horn(&horn).map_err(TestCaseError::fail)?;++ // TCB-3: field shape, checked against the cited rule's arity.+ for (i, line) in horn.lines().enumerate() {+ let s = &steps[i];+ // TCB-22: the index names a rule in the table that was written.+ prop_assert!(s.rule < rules.len(), "step {i} cites rule {} of {}", s.rule, rules.len());+ let r = &rules[s.rule];+ let want = 2 + 2 * s.binds.len() + 3 * (1 + r.body.len());+ prop_assert_eq!(fields(line).len(), want, "step {} shape: {:?}", i, line);+ }++ // TCB-4 again, on the Horn files.+ for line in asserted.lines().chain(horn.lines()).chain(written.lines()) {+ for f in fields(line) {+ prop_assert!(!f.contains('\n') && !f.contains('\r'), "separator in {:?}", f);+ }+ }++ let mut known: BTreeSet<(String, String, String)> = BTreeSet::new();+ for line in asserted.lines() {+ let f = fields(line);+ prop_assert_eq!(f.len(), 3, "{:?}", line);+ known.insert((f[0].to_string(), f[1].to_string(), f[2].to_string()));+ }+ prop_assert_eq!(known.len(), before.len());+ prop_assert_eq!(&known, &before);++ for (i, s) in steps.iter().enumerate() {+ let r = &rules[s.rule];+ // TCB-23: the binding names every variable of the rule, once.+ let names: BTreeSet<&String> = s.binds.iter().map(|(n, _)| n).collect();+ prop_assert_eq!(names.len(), s.binds.len(), "step {} repeats a variable", i);++ // TCB-23: premises are the body under the written binding, in order.+ let want: Option<Vec<_>> = r.body.iter().map(|a| inst_atom(a, &s.binds)).collect();+ let want = want.ok_or_else(|| TestCaseError::fail(format!(+ "step {i} leaves a body variable unbound"+ )))?;+ prop_assert_eq!(&s.premises, &want, "step {} premises are not the body instance", i);++ // TCB-23: the conclusion is the head under the same binding.+ let head = inst_atom(&r.head, &s.binds).ok_or_else(|| TestCaseError::fail(+ format!("step {i} leaves a head variable unbound")+ ))?;+ prop_assert_eq!(&s.conclusion, &head, "step {} conclusion is not the head instance", i);++ // TCB-12 for the Horn path.+ for p in &s.premises {+ prop_assert!(known.contains(p), "step {i} cites an unknown premise {p:?}");+ }+ known.insert(s.conclusion.clone());+ }+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-9: the store is a set and the file is a list+// ─────────────────────────────────────────────────────────────────────────────++/// A triple asserted in two named graphs is written to `asserted.tsv` twice,+/// because `all_triples` flattens quads. Harmless to soundness, because the+/// Lean side builds a `HashSet`, and stated so that `asserted` in the JSON is+/// read as a line count and not a triple count.+#[test]+fn tcb_9_a_quad_in_two_graphs_is_two_lines() {+ let g = Arc::new(GraphStore::new());+ g.load_nquads(+ "<http://e/s> <http://e/p> <http://e/o> <http://e/g1> .\n\+ <http://e/s> <http://e/p> <http://e/o> <http://e/g2> .\n",+ )+ .unwrap();+ let dir = scratch("quads");+ let out =+ Reasoner::run_full(&g, "rdfs", false, InferenceTarget::DefaultGraph, Some(&dir)).unwrap();+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ let asserted = std::fs::read_to_string(dir.join("asserted.tsv")).unwrap();+ assert_eq!(asserted.lines().count(), 2, "{asserted:?}");+ assert_eq!(json["certificate"]["asserted"], 2);+ let distinct: BTreeSet<&str> = asserted.lines().collect();+ assert_eq!(distinct.len(), 1, "the two lines are the same triple");+ let _ = std::fs::remove_dir_all(&dir);+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-8, the part that does NOT hold+// ─────────────────────────────────────────────────────────────────────────────++/// Materialising into the default graph turns run N's conclusions into run+/// N+1's assertions, and nothing in the certificate says which is which.+///+/// This test asserts the DEFECT, because it is real, documented in+/// `docs/lean-certificates.md` and in decision 0001, and mitigated rather than+/// fixed. A test that asserted the property would fail; a test that was absent+/// would let someone believe the property holds. Pinning the failure means the+/// day the behaviour changes, this fails and the documentation gets corrected.+#[test]+fn tcb_8_across_runs_an_inference_becomes_an_assertion() {+ let ttl = "<http://e/x> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://e/A> .\n\+ <http://e/A> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://e/B> .\n";++ // Default graph: the inference comes back as an assertion.+ let g = Arc::new(GraphStore::new());+ g.load_ntriples(ttl).unwrap();+ let d1 = scratch("leak1");+ Reasoner::run_full(&g, "rdfs", true, InferenceTarget::DefaultGraph, Some(&d1)).unwrap();+ let concluded: BTreeSet<String> = std::fs::read_to_string(d1.join("derivations.tsv"))+ .unwrap()+ .lines()+ .map(|l| fields(l)[1..4].join("\t"))+ .collect();+ assert!(!concluded.is_empty(), "the fixture must infer something");+ let d2 = scratch("leak2");+ Reasoner::run_full(&g, "rdfs", true, InferenceTarget::DefaultGraph, Some(&d2)).unwrap();+ let asserted2: BTreeSet<String> =+ std::fs::read_to_string(d2.join("asserted.tsv")).unwrap().lines().map(str::to_string).collect();+ assert!(+ concluded.iter().any(|c| asserted2.contains(c)),+ "the known limitation is gone; docs/lean-certificates.md and \+ docs/trusted-computing-base.md TCB-8 must be corrected"+ );++ // The named inference graph does not fix it either: `all_triples` reads+ // every graph, so a later certified run still sees them as assertions. It+ // protects `save`, not the certificate.+ let h = Arc::new(GraphStore::new());+ h.load_ntriples(ttl).unwrap();+ let d3 = scratch("leak3");+ Reasoner::run_full(&h, "rdfs", true, InferenceTarget::Inferred, Some(&d3)).unwrap();+ let d4 = scratch("leak4");+ Reasoner::run_full(&h, "rdfs", true, InferenceTarget::Inferred, Some(&d4)).unwrap();+ let asserted4: BTreeSet<String> =+ std::fs::read_to_string(d4.join("asserted.tsv")).unwrap().lines().map(str::to_string).collect();+ assert!(+ concluded.iter().any(|c| asserted4.contains(c)),+ "inference_graph now keeps derived triples out of a later asserted.tsv; \+ TCB-8 must be corrected"+ );++ for d in [&d1, &d2, &d3, &d4] {+ let _ = std::fs::remove_dir_all(d);+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-13: the rule-name list is readable at all+// ─────────────────────────────────────────────────────────────────────────────++/// The extractor above is itself a parser, and a parser that silently returns+/// the empty set would make `tcb_13` vacuous.+#[test]+fn tcb_13_the_lean_rule_name_list_is_recovered() {+ let names = lean_rule_names();+ for expect in ["rdfs2", "rdfs9", "rdfs11", "prp-trp", "eq-sym", "cls-svf1", "scm-rng2"] {+ assert!(names.contains(expect), "{expect} missing from {names:?}");+ }+ assert_eq!(names.len(), 29, "the engine documents twenty-nine rule ids: {names:?}");+}++// ─────────────────────────────────────────────────────────────────────────────+// TCB-19 and TCB-21 against the REAL Lean parsers+// ─────────────────────────────────────────────────────────────────────────────++/// The property tests above run a generated table through a TRANSCRIPTION of+/// `OOCert.HornParse` written in this file, because a Lean process per case is+/// not a property test. The transcription's fidelity is then an assumption, and+/// an assumption at the trusted boundary is what this whole exercise is about.+///+/// This closes it for the adversarial shapes specifically: a literal whose+/// lexical form is an IRI, a variable name beginning with `?`, a term that is a+/// bare number, a rule name that is a number, a non-ASCII rule name, an+/// empty-bodied rule. Each is put through the engine and then through+/// `oo-horn check`, which is the real parser and the real proof.+///+/// Skips loudly without a Lean toolchain; `OO_REQUIRE_FIXTURES=1` makes the+/// skip a failure, as everywhere else in this suite.+#[test]+fn tcb_19_21_the_real_lean_checker_accepts_an_adversarial_table() {+ let lean = repo().join("lean");+ let lake_ok = std::process::Command::new("lake")+ .arg("--version")+ .current_dir(&lean)+ .output()+ .map(|o| o.status.success())+ .unwrap_or(false);+ if common::skip_unless(+ lake_ok,+ "lake (the Lean 4 build tool), to run the real oo-horn parser",+ "install elan from https://github.com/leanprover/elan",+ ) {+ return;+ }++ let build = std::process::Command::new("lake")+ .arg("build")+ .current_dir(&lean)+ .output()+ .expect("run lake build");+ assert!(+ build.status.success(),+ "lake build failed:\n{}",+ String::from_utf8_lossy(&build.stderr)+ );+ let exe = lean.join(".lake").join("build").join("bin").join("oo-horn");+ assert!(exe.exists(), "oo-horn missing at {}", exe.display());++ // Every field here is one the Rust parser accepts and that could be read+ // differently by a parser that guessed.+ let table = format!(+ "{}{}{}{}",+ // A rule name that is a number, and a literal constant spelled exactly+ // like an IRI in the body.+ format_args!("5\t1\t?x\t{RDF_TYPE}\t\"<http://e/A>\"\t?x\t{RDF_TYPE}\t<http://e/N>\n"),+ // A variable whose name begins with `?`, so the field is `??x`.+ format_args!("r\u{e9}\t1\t??x\t{RDF_TYPE}\t<http://e/A>\t??x\t{RDF_TYPE}\t<http://e/B>\n"),+ // A blank node and a plain literal as constants.+ format_args!("b\t1\t?x\t{RDFS_SUBCLASS}\t\"5\"\t?x\t{RDF_TYPE}\t<http://e/C>\n"),+ // An empty body: the rule asserts its head unconditionally.+ format_args!("e\t0\t<http://e/k>\t{RDF_TYPE}\t<http://e/D>\n"),+ );++ let nt = format!(+ "<http://e/s> {RDF_TYPE} \"<http://e/A>\" .\n\+ <http://e/s> {RDF_TYPE} <http://e/A> .\n\+ <http://e/s> {RDFS_SUBCLASS} \"5\" .\n"+ );++ let g = Arc::new(GraphStore::new());+ g.load_ntriples(&nt).unwrap();+ let dir = scratch("leanhorn");+ let rules_path = dir.join("in.tsv");+ std::fs::write(&rules_path, &table).unwrap();+ let cert = dir.join("cert");+ let out = Reasoner::run_horn(&g, &rules_path, &cert)+ .expect("the engine accepts this table, so it must be able to certify over it");+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ assert!(json["derived_triples"].as_u64().unwrap() > 0, "{out}");++ let run = std::process::Command::new(&exe)+ .arg("check")+ .arg(cert.join("rules.tsv"))+ .arg(cert.join("asserted.tsv"))+ .arg(cert.join("horn.tsv"))+ .output()+ .expect("run oo-horn");+ let stdout = String::from_utf8_lossy(&run.stdout).to_string();+ assert_eq!(+ run.status.code(),+ Some(0),+ "oo-horn rejected a certificate over an adversarial but legal table. \+ The Rust and the Lean parsers disagree, which is TCB-19.\nstdout: {stdout}\nstderr: {}",+ String::from_utf8_lossy(&run.stderr)+ );+ // A user table is not the built-in one, so the verdict must be the+ // conditional one. Decision 0003.+ assert!(+ stdout.contains("entailed_under_supplied_rules"),+ "expected the conditional verdict, got {stdout}"+ );+ let _ = std::fs::remove_dir_all(&dir);+}++/// `docs/trusted-computing-base.md` is the deliverable the properties above are+/// named after. A property renamed without the document being updated leaves a+/// reader with no way to look one up from the other.+#[test]+fn the_trusted_base_document_names_every_property_tested_here() {+ let doc = std::fs::read_to_string(repo().join("docs/trusted-computing-base.md")).unwrap();+ let src = std::fs::read_to_string(Path::new(file!()))+ .or_else(|_| std::fs::read_to_string(repo().join("tests/certificate_boundary_proptest.rs")))+ .unwrap();+ let mut seen = BTreeSet::new();+ for fname in src.split("fn tcb_").skip(1) {+ let ids: String = fname.chars().take_while(|c| c.is_ascii_digit() || *c == '_').collect();+ for part in ids.split('_').filter(|p| !p.is_empty()) {+ if part.chars().all(|c| c.is_ascii_digit()) {+ seen.insert(part.to_string());+ }+ }+ }+ assert!(!seen.is_empty(), "no TCB identifiers found in this file");+ for id in &seen {+ assert!(+ doc.contains(&format!("TCB-{id} ")) || doc.contains(&format!("TCB-{id}.")),+ "TCB-{id} is tested but not described in docs/trusted-computing-base.md"+ );+ }+}diff --git a/tests/reason_unwritable_predicate_test.rs b/tests/reason_unwritable_predicate_test.rsnew file mode 100644index 00000000..ad6c480e--- /dev/null+++ b/tests/reason_unwritable_predicate_test.rs@@ -0,0 +1,262 @@+//! A conclusion whose PREDICATE is not an IRI.+//!+//! # The defect+//!+//! `rdfs7` reads `s sub o` and `sub rdfs:subPropertyOf super` and concludes+//! `s super o`. Nothing required `super` to be an IRI, and `rdfs:subPropertyOf`+//! takes any term as its object, so a blank node there produced a conclusion+//! with a blank node in predicate position. No RDF serialisation can write one.+//!+//! The materialiser turns the inferred set into one N-Triples document and+//! hands it to `GraphStore::load_ntriples`, which streams: it inserts each quad+//! as it parses it. So the bad line did not make the batch fail cleanly. It+//! made it fail PARTWAY, after an arbitrary number of good triples were already+//! in the store, and `run_full` then returned the parser's error and never+//! wrote the certificate. The store was left holding uncertified inferences and+//! the caller was told the run had failed. The number kept was hash-iteration+//! order, so it differed between runs on identical input: measured at 40, 9 and+//! 24 of 40 on three consecutive runs of the same graph.+//!+//! The same defect for the SUBJECT position was found on 30 August 2026 and+//! guarded at four rule sites (`tests/reason_literal_subject_test.rs`). The+//! predicate position was left open, and `run_horn` was the only path that+//! refused it. Found by `tests/certificate_boundary_proptest.rs` on 14+//! September 2026, at case 115, shrunk to two triples.+//!+//! # Why it matters beyond the crash+//!+//! It is a certificate defect, not only a robustness one. In `--dry-run` the+//! materialiser never runs, so nothing failed and the certificate was written+//! WITH the unserialisable conclusion in it. The Lean checker holds terms as+//! opaque strings and its semantics puts properties in the domain, so it+//! accepts a blank node in predicate position and the certificate checks. It is+//! sound and it describes a triple the store cannot hold.+//!+//! # The fix+//!+//! One guard, in `emit`, covering both positions, shared with `run_horn`+//! (`writable_triple` in `src/reason.rs`). A refused conclusion is not+//! materialised, not certified, and not available as a premise, and the run+//! reports how many it refused.++use std::sync::Arc;++use open_ontologies::graph::GraphStore;+use open_ontologies::reason::{InferenceTarget, Reasoner};++fn scratch(name: &str) -> std::path::PathBuf {+ let dir = std::env::temp_dir().join(format!("oo-unwritable-{name}-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ dir+}++const TYPE: &str = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>";+const SUBCLASS: &str = "<http://www.w3.org/2000/01/rdf-schema#subClassOf>";+const SUBPROP: &str = "<http://www.w3.org/2000/01/rdf-schema#subPropertyOf>";++/// The shrunken counterexample, exactly as proptest reported it.+#[test]+fn a_blank_node_super_property_does_not_break_the_run() {+ let nt = format!("<http://e/A> <http://e/q> <http://e/A> .\n<http://e/q> {SUBPROP} _:b0 .\n");+ let g = Arc::new(GraphStore::new());+ g.load_ntriples(&nt).unwrap();+ let before = g.triple_count();+ let dir = scratch("blank");++ let out = Reasoner::run_full(&g, "rdfs", true, InferenceTarget::DefaultGraph, Some(&dir))+ .expect("a blank node as the object of rdfs:subPropertyOf is legal RDF");+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();++ assert_eq!(json["skipped_unserialisable"], 1, "{out}");+ assert!(+ json["skipped_examples"][0].as_str().unwrap().contains("(by rdfs7)"),+ "{out}"+ );+ assert_eq!(g.triple_count(), before, "an unwritable triple was materialised");+ assert!(dir.join("asserted.tsv").is_file(), "the certificate was not written");+ let _ = std::fs::remove_dir_all(&dir);+}++/// The realistic route in. `SubObjectPropertyOf(:p ObjectInverseOf(:q))` maps to+/// RDF as `:p rdfs:subPropertyOf [ owl:inverseOf :q ]`, which is a blank node in+/// exactly the position that breaks `rdfs7`. An ordinary OWL 2 ontology was+/// enough to make `reason` fail.+#[test]+fn the_owl_anonymous_inverse_property_idiom_reasons() {+ let ttl = r#"+@prefix : <http://e/> .+@prefix owl: <http://www.w3.org/2002/07/owl#> .+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+:p a owl:ObjectProperty ; rdfs:subPropertyOf [ owl:inverseOf :q ] .+:a :p :b .+"#;+ let g = Arc::new(GraphStore::new());+ g.load_turtle(ttl, None).unwrap();+ let dir = scratch("owlidiom");+ let out = Reasoner::run_full(&g, "owl-rl", true, InferenceTarget::DefaultGraph, Some(&dir))+ .expect("a standard OWL 2 ontology must not make the reasoner fail");+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ assert_eq!(json["skipped_unserialisable"], 1, "{out}");+ assert!(dir.join("derivations.tsv").is_file());+ let _ = std::fs::remove_dir_all(&dir);+}++/// Every rule that can reach the defect, each from a fixture that is legal RDF+/// and, in four of the five cases, an ordinary OWL 2 idiom. Measured against the+/// unfixed engine: each of these returned+/// `Parser error: The predicate of a triple must be an IRI` or+/// `... The subject of a triple must be an IRI or a blank node`.+///+/// `cls-avf` is the one to read. It needs no anonymous property expression at+/// all: an `owl:allValuesFrom` restriction plus a literal value is enough, and+/// it concludes `"x" rdf:type :D`. That is the SUBJECT defect, at a fifth rule+/// the 30 August 2026 fix did not reach, and it went unnoticed because that fix+/// was four targeted guards rather than one central one.+///+/// `prp-symp` and `prp-trp` are NOT reachable and are here to say so: both+/// require their property to already be the predicate of a stored triple, so it+/// is an IRI by construction.+#[test]+fn every_rule_that_can_reach_the_defect_is_covered() {+ let prefixes = "@prefix : <http://e/> .\n\+ @prefix owl: <http://www.w3.org/2002/07/owl#> .\n\+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n\+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";+ let cases: [(&str, &str); 5] = [+ // SubObjectPropertyOf(:p ObjectInverseOf(:q)).+ ("rdfs7", ":p rdfs:subPropertyOf [ owl:inverseOf :q ] .\n:a :p :b .\n"),+ ("prp-inv1", ":p owl:inverseOf [ owl:inverseOf :q ] .\n:a :p :b .\n"),+ ("prp-inv2", "[ owl:inverseOf :q ] owl:inverseOf :r .\n:a :q :b .\n"),+ // owl:onProperty on an anonymous inverse property expression.+ (+ "cls-hv1",+ ":C rdfs:subClassOf [ a owl:Restriction ; owl:onProperty [ owl:inverseOf :q ] ; \+ owl:hasValue :v ] .\n:a a :C .\n",+ ),+ // allValuesFrom plus a literal value. No anonymous anything.+ (+ "cls-avf",+ ":C rdfs:subClassOf [ a owl:Restriction ; owl:onProperty :p ; \+ owl:allValuesFrom :D ] .\n:a a :C ; :p \"x\" .\n",+ ),+ ];+ for (rule, body) in cases {+ let g = Arc::new(GraphStore::new());+ g.load_turtle(&format!("{prefixes}{body}"), None).unwrap();+ let dir = scratch(rule);+ let out =+ Reasoner::run_full(&g, "owl-rl-ext", true, InferenceTarget::DefaultGraph, Some(&dir))+ .unwrap_or_else(|e| panic!("{rule}: legal RDF made the reasoner fail: {e}"));+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ assert_eq!(json["skipped_unserialisable"], 1, "{rule}: {out}");+ assert!(+ json["skipped_examples"][0].as_str().unwrap().contains(&format!("(by {rule})")),+ "{rule}: {out}"+ );+ assert!(dir.join("asserted.tsv").is_file(), "{rule}: no certificate");+ let _ = std::fs::remove_dir_all(&dir);+ }++ // Not reachable, and pinned so that a future change that makes them+ // reachable shows up as a surprise rather than as silence.+ for (what, body) in [+ ("prp-symp", "[] a owl:SymmetricProperty .\n:a :p :b .\n"),+ ("prp-trp", "[] a owl:TransitiveProperty .\n:a :p :b .\n:b :p :c .\n"),+ ] {+ let g = Arc::new(GraphStore::new());+ g.load_turtle(&format!("{prefixes}{body}"), None).unwrap();+ let dir = scratch(what);+ let out =+ Reasoner::run_full(&g, "owl-rl-ext", true, InferenceTarget::DefaultGraph, Some(&dir))+ .unwrap();+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ assert!(+ json["skipped_unserialisable"].is_null(),+ "{what} became reachable: {out}"+ );+ let _ = std::fs::remove_dir_all(&dir);+ }+}++/// `owl:inverseOf` whose object is a literal. Garbage as OWL, legal as RDF, and+/// `prp-inv1` used to turn it into a literal in predicate position.+#[test]+fn a_literal_inverse_property_does_not_break_the_run() {+ let nt = "<http://e/p> <http://www.w3.org/2002/07/owl#inverseOf> \"junk\" .\n\+ <http://e/a> <http://e/p> <http://e/b> .\n";+ let g = Arc::new(GraphStore::new());+ g.load_ntriples(nt).unwrap();+ let dir = scratch("litpred");+ let out = Reasoner::run_full(&g, "owl-rl", true, InferenceTarget::DefaultGraph, Some(&dir))+ .expect("a literal object of owl:inverseOf is legal RDF");+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ assert_eq!(json["skipped_unserialisable"], 1, "{out}");+ assert!(+ json["skipped_examples"][0].as_str().unwrap().contains("(by prp-inv1)"),+ "{out}"+ );+ let _ = std::fs::remove_dir_all(&dir);+}++/// The consequence that made this more than a crash: the good inferences used to+/// land in the store and the run still reported failure, with no certificate.+/// How many landed depended on hash iteration order, so the store's contents+/// were not a function of its input.+#[test]+fn the_good_inferences_survive_and_the_run_is_deterministic() {+ let mut nt = String::new();+ for i in 0..40 {+ nt.push_str(&format!("<http://e/x{i}> {TYPE} <http://e/A> .\n"));+ }+ nt.push_str(&format!("<http://e/A> {SUBCLASS} <http://e/B> .\n"));+ // The poison: two triples that make rdfs7 conclude a blank-node predicate.+ nt.push_str("<http://e/A> <http://e/p> <http://e/A> .\n");+ nt.push_str(&format!("<http://e/p> {SUBPROP} _:b0 .\n"));++ let mut sizes = Vec::new();+ for trial in 0..5 {+ let g = Arc::new(GraphStore::new());+ g.load_ntriples(&nt).unwrap();+ let dir = scratch(&format!("partial{trial}"));+ let out = Reasoner::run_full(&g, "rdfs", true, InferenceTarget::DefaultGraph, Some(&dir))+ .expect("one unwritable conclusion must not fail the whole batch");+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();+ // The 40 rdfs9 conclusions are unaffected by the one refusal.+ assert_eq!(json["inferred_count"], 40, "{out}");+ assert!(dir.join("asserted.tsv").is_file());+ sizes.push(g.triple_count());+ let _ = std::fs::remove_dir_all(&dir);+ }+ assert!(+ sizes.windows(2).all(|w| w[0] == w[1]),+ "the store's size after the run depends on hash iteration order: {sizes:?}"+ );+}++/// A dry run must not certify what a materialising run refuses. Before the fix+/// the two disagreed: `--dry-run` wrote a certificate containing the+/// unserialisable conclusion, and materialising failed on it.+#[test]+fn a_dry_run_certifies_exactly_what_a_materialising_run_does() {+ let nt = format!("<http://e/A> <http://e/q> <http://e/A> .\n<http://e/q> {SUBPROP} _:b0 .\n");++ let wet = Arc::new(GraphStore::new());+ wet.load_ntriples(&nt).unwrap();+ let dw = scratch("wet");+ Reasoner::run_full(&wet, "rdfs", true, InferenceTarget::DefaultGraph, Some(&dw)).unwrap();++ let dry = Arc::new(GraphStore::new());+ dry.load_ntriples(&nt).unwrap();+ let dd = scratch("dry");+ Reasoner::run_full(&dry, "rdfs", false, InferenceTarget::DefaultGraph, Some(&dd)).unwrap();++ let a = std::fs::read_to_string(dw.join("derivations.tsv")).unwrap();+ let b = std::fs::read_to_string(dd.join("derivations.tsv")).unwrap();+ assert_eq!(a, b, "a dry run and a materialising run certified different things");+ assert!(+ !a.contains("_:b0\t"),+ "an unserialisable conclusion was certified: {a:?}"+ );+ let _ = std::fs::remove_dir_all(&dw);+ let _ = std::fs::remove_dir_all(&dd);+}