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
A pack carries its own proof and the receiver re-runs the checker (#266)
Fabio Rovai7490402e71da
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 87e86645..44020625 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -541,6 +541,14 @@ jobs:run: OO_REQUIRE_FIXTURES=1 cargo test --test lean_refutation_producer_test -- --nocapture- name: Check the mixed certificate layerrun: OO_REQUIRE_FIXTURES=1 cargo test --test lean_mixed_certificate_test -- --nocapture+ - name: Re-run the checker over a certificate carried inside a pack, and refuse a forged one+ # Two of this file's legs need a real oo-cert and the rest do not, so+ # without this step the forgery leg would skip for ever in the `build`+ # job and nothing would count the skip. The leg that matters is the one+ # that forges a conclusion INSIDE the packed certificate, repairs every+ # digest so only the proof can object, and requires the receiver to+ # refuse the load.+ run: OO_REQUIRE_FIXTURES=1 cargo test --test pack_certificate_test -- --nocapture- name: Check the generic Horn-rule layer, and that a user rule cannot earn the absolute verdict# The five legs here are not interchangeable. Four of them prove the# checker rejects a specific forgery; the fifth proves that adiff --git a/CLAUDE.md b/CLAUDE.mdindex bc557ac5..f69e997d 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -104,8 +104,8 @@ Claude dynamically decides the next tool call based on what the previous tool re| `onto_support_check` | To check whether claims are backed by the sources they cite. Returns which claims cite NO source (computed) and, for the rest, verification TASKS: the claim as a sentence, the source, what to decide. Complements `onto_vocab_check`: conformance asks whether a claim is expressible, support asks whether it is true to its source, and a claim can fail either independently || `onto_support_verdict` | To record supported / refuted / unrelated for a claim after reading its source. Verdicts persist and are skipped on the next check || `onto_support_report` | To summarise provenance quality: unsourced-claim rate over the graph, support rate across the claims judged so far |-| `onto_pack` | To write the loaded graph and its verification evidence to a portable versioned artifact: sorted N-Triples, a manifest (name, version, counts, timestamp, tool version, sha256) and the lint/enforce results recorded at pack time. What you promote between environments is then a graph that has already passed its checks, with the evidence attached |-| `onto_unpack` | To load a pack, refusing it if the checksum does not match. `verify_only` inspects the manifest and evidence without loading |+| `onto_pack` | To write the loaded graph, its verification evidence and, with `certificate_dir`, its derivation certificate to a portable versioned artifact: sorted N-Triples, a manifest (name, version, counts, timestamp, tool version, sha256), the lint/enforce results recorded at pack time, and a `content_sha256` covering the graph AND the certificate together so a swapped certificate cannot hide behind a matching graph checksum. The evidence is what THIS engine said; the certificate is what the receiver's own Lean checker can re-derive, which is why they are separate fields. A certificate resting on assertions the packed graph does not contain is refused here, so the sender learns rather than the auditor |+| `onto_unpack` | To load a pack, refusing it if either digest does not match, and to re-run THIS machine's Lean checker over the certificate the pack carries. Four answers are kept apart and none collapses into another: the pack carries no certificate; the certificate was accepted (`certificate_accepted`, or `certificate_accepted_under_supplied_rules` when the sender supplied the rule table); it was REFUSED, which blocks the load unless `load_even_if_refused` is passed, because a graph whose own proof does not check is a defect; or this build has no checker, so `checker_absent_nothing_was_checked`, which is NOT a pass. A separate word says the certificate checks and is about premises this pack does not carry. An acceptance proves the materialised triples follow from the asserted ones under the rules that RAN, and proves nothing about whether the asserted triples are true or about axioms no rule fires on. `verify_only` inspects and checks without loading || `onto_temporal_snapshot` | To see which named graphs are in scope at a point in time. Two independent clocks: `valid_at` asks what was TRUE then, `as_of` asks what was KNOWN then. Both intervals are half-open: `temporal:recordedUntil` closes the recorded one, so `as_of` answers what was believed then rather than everything recorded by then. Graphs without validity metadata are timeless and always in scope, so the vocabulary is additive to an existing store. Bounds on all four axes are read as instants on the UTC timeline from `xsd:date`, `xsd:dateTime`, `xsd:gYearMonth` or `xsd:gYear`: a less precise bound names the FIRST instant of its period, and a value with no timezone offset is UTC. A bound matching none of those, or two different instants on one axis, makes the graph `invalid`: reported with a reason, never in scope and never timeless || `onto_temporal_query` | To run a SPARQL graph pattern against only the graphs in temporal scope: what the graph said at a given valid time, as known at a given recorded time || `onto_temporal_conflicts` | To separate genuine contradictions from pairs that never met, from withdrawn assertions and from asserted corrections. A pair one side of which a `temporal:retracts` link has withdrawn lands in `retracted`, checked first as the snapshot does; the tool takes no `as_of`, so a recorded retraction counts whenever it was recorded, and the note says so. A pair where one graph `temporal:supersedes` the other, directly or through a chain, lands in `corrections` whatever its periods, lineage that is asserted and never inferred; otherwise a disjointness violation only counts when the two assertions claim OVERLAPPING validity, and everything else lands in `non_overlapping`, which proves only that the two share no instant, judged on their bounds read as instants on the UTC timeline so a timezone offset is honoured: not that one replaced the other, and it also holds pairs separated by a gap, which is missing coverage rather than history. A pair whose graph has temporal metadata that could not be read goes to `undecided`, in neither bucket, because an unreadable period is not an open one. `superseded` is the same set under its old name, deprecated and dropped at 2.0 |diff --git a/docs/ci-gates.md b/docs/ci-gates.mdindex ad6e7d39..644dccfc 100644--- a/docs/ci-gates.md+++ b/docs/ci-gates.md@@ -68,6 +68,7 @@ job invokes the file at all.| `lean_refutation_test.rs` | lake + `oo-refute` | `lean` job | **strict** || `lean_refutation_producer_test.rs` | lake + `oo-refute` | `lean` job | **strict** || `lean_mixed_certificate_test.rs` | lake | `lean` job | **strict** |+| `pack_certificate_test.rs` | lake, for the two legs that forge a conclusion inside a packed certificate; the other six hand the checker in as a path and need nothing | `lean` job | **strict** || `lean_horn_certificate_test.rs` | lake | `lean` job | **strict** || `trace_label_test.rs` | lake + `oo-cert` + `oo-horn` | `lean` job | **strict** || `readme_example_test.rs` | lake | `lean` job | **strict** |diff --git a/docs/decisions/0017-a-pack-carries-its-own-proof.md b/docs/decisions/0017-a-pack-carries-its-own-proof.mdnew file mode 100644index 00000000..0570dc04--- /dev/null+++ b/docs/decisions/0017-a-pack-carries-its-own-proof.md@@ -0,0 +1,128 @@+# 0017. A pack carries its own proof, and the receiver re-runs it++**Status:** accepted, 28 September 2026.++## The failure this prevents++A receiver trusting the sender's engine because the sender's engine said it was fine.++`onto_pack` wrote a graph, a `sha256` over it, and an `evidence` block holding the lint and enforce+results recorded at pack time. `onto_unpack` compared that one digest and loaded. Every word in+`evidence` was an ASSERTION by the sender's engine, and nothing on the receiving side re-derived any+of it. The receiver verified integrity, which is a statement about bytes, and read soundness off a+report, which is a statement about a machine they were not running.++That is the shape this project exists to refuse everywhere else. `onto_reason` already writes a+certificate the Lean checker in `lean/` verifies, and `verdict.rs` already makes a certified word+unspellable without an exit code behind it. The artefact meant for promotion between environments+was the one place where the evidence was still a claim.++## The decision++A pack may carry the derivation certificate for the run that produced its graph. `onto_unpack` writes+those files out and runs THIS machine's checker over them. The verdict is the receiver's, produced by+`projection_entailment::run_checker`, which is the only function in the crate that can mint a+`verdict::Certified`. The sender's engine is out of the trusted set for that one claim.++Seven words, and none of them collapses into another:++| verdict | what happened | loads? |+| --- | --- | --- |+| `certificate_accepted` | `oo-cert` accepted it under `OOCert.certificate_sound` | yes |+| `certificate_accepted_under_supplied_rules` | `oo-horn` accepted it over a table the SENDER supplied | yes |+| `certificate_refused` | the checker named a step it would not accept | **no** |+| `certificate_unreadable` | exit 2, an unknown file name, or files that form no certificate | **no** |+| `certificate_not_about_this_pack` | the checker accepted it and its premises are not all in the graph | **no** |+| `checker_absent_nothing_was_checked` | no checker on this machine, or the caller turned the check off | yes |+| `no_certificate_in_pack` | the pack carries no proof, which every pack written before this does | yes |++The four outcomes a receiver has to tell apart are the first, the third, the sixth and the seventh,+and the sixth is the one that matters. "We did not check" must never render as "it passed", so the+absent case has its own word, carries no theorem, and is asserted in+`tests/pack_certificate_test.rs` by looking for the accepted word in the WHOLE serialised report and+requiring it to be absent. The wording follows `web/try/api/index.py`, which says the release carries+no checker rather than implying a check happened.++## Five choices, and why each went the way it did++**A refused certificate blocks the load.** A graph whose own proof does not check is a defect, not a+graph to promote. `load_even_if_refused` is the escape, so a receiver can still open the artefact and+look at it. This changes `onto_unpack` behaviour only for packs that carry a certificate, so no pack+written before this exists is affected.++**A pack carries ONE certificate, and the field is singular in the format.** A real promotion may+involve several runs, an RDFS pass and then a supplied Horn table, and making this a list is cheap now+and expensive later. It stays singular because a second certificate raises a question nobody can+answer honestly yet: what the COMBINED verdict is when one is accepted and the other refused. Until+there is an answer to that, a pack that needs two proofs is two packs.++**`pack` refuses a certificate that is not about the graph.** The sender learns at pack time rather+than the auditor learning at unpack time. The receiver refuses it too, because the receiver must not+depend on the sender having checked. The cost is that `onto_pack` can now fail where it used to+succeed, on a mistake that is easy to make: reason over one store, load another, pack.++**The CLI asymmetry is intended.** `unpack` reaches the CLI and the batch executor; `pack` stays+MCP-only. The receiver is the party this feature exists for, and requiring them to run an MCP client+to audit a pack would defeat it. The sender is already in a session.++**The certificate files travel as text, not base64.** A pack is meant to be diffable, which is the+same reason the graph is sorted N-Triples. The cost is escaped tabs and a larger file.++## The binding, stated precisely++`sha256` still covers the graph text and nothing else, so a reader who checked packs before this+existed is checking the same thing. It cannot see a swapped certificate, and a swapped certificate is+the whole attack: a certificate that is internally sound over SOMEBODY ELSE'S assertions checks+green, so a receiver verifying only the graph checksum would accept a true proof about premises that+are not in the pack.++`content_sha256` is the digest of, in order: the tag `oo-pack-content/1\n`, the length and bytes of+the graph text, a presence byte for the certificate and, when one is present, the length-prefixed+`format`, the length-prefixed `profile_claimed_by_sender`, the `asserted` and `derivations` counts,+the number of files, then every (name, body) pair in `BTreeMap` order with both halves+length-prefixed. Every part is length-prefixed so no two different (graph, certificate) pairs can+frame to the same bytes by running together at a seam. It is written out explicitly rather than taken+from `serde_json::to_string`, so a serde_json release cannot change what a security digest covers.++A pack that carries a certificate and no `content_sha256` is REFUSED rather than checked. Without+that rule the downgrade is free: strip the field, paste in a certificate sound over another graph,+and the old, weaker verification passes it.++## What an acceptance does not settle++A certificate proves that the materialised triples follow from the asserted ones under the rules that+RAN. It proves nothing about whether the asserted triples are true, that they describe the world,+that they came from the system the sender says they came from, or that the sender left nothing out.+It is also silent about axioms outside the fragment: a profile run evaluates part of OWL 2 RL, a+supplied table is whatever `rules.tsv` holds, and an axiom no rule fires on contributed nothing and+is invisible. `onto_dlp_boundary` over the loaded graph is what answers that second question.++Those sentences are in the RECEIVER'S REPORT, under `does_not_mean`, and not only in this file. A+limit stated in documentation is a limit the person reading the verdict does not see.++## Two limits that are named rather than closed++**The receiver's checker is still a binary somebody chose.** Residual hole 1 in `src/verdict.rs`+applies unchanged: `$OO_CERT` pointing at a script that exits zero and prints the right theorem name+earns the accepted word. That is now the RECEIVER'S own foot rather than the sender's, which is a+real improvement and not a closure. The report prints the `checker` block the binary says about+itself, so a reader can match its SHA-256 against a release's `SHASUMS.txt`, and a hostile binary can+print the same block with any numbers in it.++**Pack size.** The certificate travels as JSON strings and the whole pack is built in memory, with no+streaming path. Measured on 28 September 2026: `benchmark/reference/ies4.ttl`, 249 KB of Turtle and+3,976 asserted triples, reasoned under `owl-rl-ext` with materialisation, packs to 4,514,931 bytes,+of which 3,015,113 are the certificate and 1,415,791 the graph.+`benchmark/reference/pizza-reference.owl`, 241 KB and 2,332 triples, packs to 1,422,749 bytes with a+843,870-byte certificate. That is roughly 490 bytes of pack JSON per packed triple on the first, so a+1.9M-triple store is of the order of a gigabyte held in memory twice. `onto_pack` reports+`graph_bytes`, the certificate's `bytes` and `pack_json_bytes` so the cost is visible before a+receiver discovers it.++## What a pack is still not++`serialize("ntriples")` drops the graph name, so a pack of a store that reasoned with+`inference_graph: true` loses the asserted and inferred separation that decision 0001 is about. The+certificate is then the only thing in the pack that says which triples were derived. That was already+true of packs and the certificate makes it legible rather than making it worse, but a pack is not a+faithful copy of a quad store. N-Quads is the follow-up.diff --git a/docs/decisions/README.md b/docs/decisions/README.mdindex 8831becd..5b8ca41c 100644--- a/docs/decisions/README.md+++ b/docs/decisions/README.md@@ -20,6 +20,7 @@ and whose benefit is not gets dropped the first time it is inconvenient.| [0014](0014-a-verifier-that-cannot-read-the-code-verifies-a-rewrite.md) | A verifier that cannot read the code verifies a rewrite | Counting a green verifier as evidence about code it never read || [0015](0015-a-blank-line-is-a-line-or-it-is-not.md) | A blank line is a line, or it is not, and the format never said which | The same failure again, found by a third kernel and still OPEN: two checkers reading one file differently because nobody wrote down what an empty line is || [0016](0016-a-conclusion-names-the-axioms-responsible-for-it.md) | A conclusion names the axioms responsible for it, and minimality is re-run | Presenting a justification as minimal when nothing re-ran it |+| [0017](0017-a-pack-carries-its-own-proof.md) | A pack carries its own proof, and the receiver re-runs it | Promoting a graph between environments on the strength of what the sender's engine said about it |This table is not the whole set. Decisions 0009, 0010 and 0011 are in this directory and have no rowhere, and the number 0009 was used twice by two records written in parallel. Both are stated ratherdiff --git a/docs/lean-certificates.md b/docs/lean-certificates.mdindex 10649f0b..cd521408 100644--- a/docs/lean-certificates.md+++ b/docs/lean-certificates.md@@ -87,6 +87,76 @@ inside one.then the derivation steps that reached the clash in the same spelling as `derivations.tsv`, thenone `refute TAB rule` line with the clash rule's premises. See the refutation section below.+## Carrying a certificate inside a pack++`onto_pack` writes a graph, a `sha256` over it, and the lint and enforce results recorded at pack+time. That evidence is what the SENDER'S engine said. Pass `certificate_dir` and the pack also+carries the certificate for the run that produced the graph, and `onto_unpack` writes those files out+and runs THIS machine's checker over them. The receiver then learns whether the derivations hold+without trusting the engine that wrote the pack. See+[decision 0017](decisions/0017-a-pack-carries-its-own-proof.md).++**Which files travel.** Only the names a run of this engine writes: `asserted.tsv`,+`asserted.sha256`, `derivations.tsv`, `horn.tsv`, `rules.tsv`, `scope.tsv` and `refutation.tsv`. The+list is a whitelist in both directions. At pack time it stops whatever an earlier run left in the+directory going under the digest; at unpack time the names come from the sender, and a pack naming+`../../.ssh/authorized_keys` would otherwise be written wherever the receiver's scratch directory+resolves that to. The files travel as TEXT, not base64, so a pack stays diffable for the same reason+the graph is sorted.++Which checker reads it is decided by the files present and never by the pack's `format` string:+`derivations.tsv` means `oo-cert`, `horn.tsv` plus `rules.tsv` means `oo-horn check`. Reading the+sender's string would let the sender choose the receiver's checker. A directory holding both is+refused, because the two earn different verdicts.++**What `content_sha256` covers.** `sha256` still covers the graph text and nothing else.+`content_sha256` covers the tag `oo-pack-content/1\n`, the length and bytes of the graph, a presence+byte for the certificate and, when one is present, the length-prefixed `format`, the length-prefixed+`profile_claimed_by_sender`, the `asserted` and `derivations` counts, the number of files, then every+(name, body) pair in `BTreeMap` order with both halves length-prefixed. Everything is+length-prefixed so two different (graph, certificate) pairs cannot frame to the same bytes at a seam.+A pack that carries a certificate and no `content_sha256` is REFUSED rather than checked: without+that rule, stripping the field and pasting in a certificate sound over another graph passes the old,+weaker verification.++**The seven verdicts, and none collapses into another.**++| verdict | what happened | loads? |+| --- | --- | --- |+| `certificate_accepted` | `oo-cert` accepted it under `OOCert.certificate_sound` | yes |+| `certificate_accepted_under_supplied_rules` | `oo-horn` accepted it over a table the SENDER supplied | yes |+| `certificate_refused` | the checker named a step it would not accept | **no** |+| `certificate_unreadable` | exit 2, an unknown file name, or files that form no certificate | **no** |+| `certificate_not_about_this_pack` | the checker accepted it and its premises are not all in the graph | **no** |+| `checker_absent_nothing_was_checked` | no checker here, or the caller turned the check off | yes |+| `no_certificate_in_pack` | the pack carries no proof, which every pack written before this does | yes |++`certificate_not_about_this_pack` is the gate the checker cannot be. `oo-cert` verifies the steps+against the triples inside `asserted.tsv` and has no way to ask where they came from, so a+certificate that is internally sound over somebody else's premises checks green. `onto_unpack`+compares the certificate's `asserted.tsv` against the packed graph in the one spelling both sides+use, `reason::asserted_bytes`, and refuses the load when the proof rests on assertions the pack does+not contain. `onto_pack` refuses the same thing at pack time, so the sender learns rather than the+auditor, and the receiver-side refusal stays either way.++**What an acceptance does not settle.** A certificate proves the materialised triples follow from the+asserted ones under the rules that RAN. It proves nothing about whether the asserted triples are+true, and it is silent about axioms outside the fragment: an axiom no rule fires on contributed+nothing and is invisible here. Run `onto_dlp_boundary` over the loaded graph to see which of its+axioms the rule table can read. Those sentences are in the receiver's report, under `does_not_mean`,+because a limit stated only in documentation is a limit the person reading the verdict does not see.++**Two limits that are named rather than closed.** The receiver's checker is still a binary somebody+chose: `$OO_CERT` pointing at a script that exits zero and prints the right theorem name earns the+accepted word. That is now the receiver's own foot rather than the sender's, which is an improvement+and not a closure; the report prints the `checker` block the binary says about itself so a reader can+match its SHA-256 against a release's `SHASUMS.txt`. And the pack is built in memory with no+streaming path. Measured on 28 September 2026: `benchmark/reference/ies4.ttl`, 3,976 asserted triples+reasoned under `owl-rl-ext` with materialisation, packs to 4,514,931 bytes of which 3,015,113 are the+certificate; `benchmark/reference/pizza-reference.owl`, 2,332 triples, packs to 1,422,749 bytes with+a 843,870-byte certificate. `onto_pack` reports `graph_bytes`, the certificate's `bytes` and+`pack_json_bytes` so the cost is visible.+## What the file is NOT enough for`derivations.tsv` is a derivation DAG and it is tempting to read explanations out of it. Do not.diff --git a/docs/tool-reference.md b/docs/tool-reference.mdindex 91204a42..6bed9542 100644--- a/docs/tool-reference.md+++ b/docs/tool-reference.md@@ -151,7 +151,7 @@ it withheld.| `onto_ossie_import` | Compile an Apache Ossie (incubating, formerly Open Semantic Interchange) ontology document into OWL 2 DL + SHACL, and optionally load it into the active store so every other tool here works on it. || `onto_owl_shacl_coevolve_check` | Combined OWL+SHACL validation (#33, K-CAP 2025). || `onto_owl_shacl_coevolve_incremental` | Incremental coevolve check (#33 follow-on, K-CAP 2025). |-| `onto_pack` | Write the loaded graph and its verification evidence to a portable, versioned pack: sorted N-Triples plus a manifest (name, version, counts, timestamp, sha256, and the lint/enforce results recorded at pack time). |+| `onto_pack` | Write the loaded graph, its verification evidence and, optionally, its derivation certificate to a portable, versioned pack: sorted N-Triples plus a manifest (name, version, counts, timestamp, sha256, the lint/enforce results recorded at pack time, and a content_sha256 covering the graph AND the certificate together). Pass `certificate_dir` to carry the proof. || `onto_plan` | Terraform-style plan: diff current store against proposed Turtle. || `onto_plan_classical` | Invoke Fast Downward as a subprocess on a precompiled PDDL domain + problem (#50). || `onto_plan_compile_pddl` | Compile a PDDL domain from registered Dynamics action schemas (#43) plus a problem instance from the loaded graph and a goal Turtle slice (#45 Planner stub). |@@ -194,7 +194,7 @@ it withheld.| `onto_temporal_snapshot` | Which named graphs are in scope at a point in time, and which are excluded and why. || `onto_trace_label` | Label each step of a reasoning trace against the rule table, with a machine-checked verdict per step: entailed, not_entailed, or outside_the_fragment, plus the local answer over the premises the step itself cited. || `onto_unload` | Unload an ontology from memory. |-| `onto_unpack` | Load a pack written by onto_pack, refusing it if the checksum does not match. |+| `onto_unpack` | Load a pack written by onto_pack, refusing it if either digest does not match, and re-run this machine's Lean checker over the certificate the pack carries. A refused certificate blocks the load; an absent checker reports that nothing was checked, which is not a pass. || `onto_validate` | Validate RDF/OWL syntax. || `onto_validate_clinical` | Validate all class labels in the loaded ontology against clinical crosswalk data. || `onto_verify_cq` | Persist an LLM-supplied (or human-supplied) verdict on a CQ result (#39, ISWC 2025 Lippolis). |diff --git a/src/batch.rs b/src/batch.rsindex 5054781c..17bb7f27 100644--- a/src/batch.rs+++ b/src/batch.rs@@ -143,6 +143,7 @@ impl BatchRunner {"diff" => self.exec_diff(&cmd.args),"convert" => self.exec_convert(&cmd.args),"enforce" => self.exec_enforce(&cmd.args),+ "unpack" => self.exec_unpack(&cmd.args),"plan" => self.exec_plan(&cmd.args),"apply" => self.exec_apply(&cmd.args),"version" => self.exec_version(&cmd.args),@@ -856,6 +857,38 @@ impl BatchRunner {serde_json::from_str(&result).unwrap_or(json!({"raw": result}))}+ /// `unpack PATH [--verify-only] [--no-check] [--checker P] [--certificate-out D]+ /// [--load-even-if-refused]`+ ///+ /// Present so a batch script reaches the same verify-and-refuse loop the+ /// MCP tool does. `web/try/api/index.py` drives the engine in batch mode,+ /// so a route that exists only under MCP is a route that demo cannot show.+ /// `pack` has no arm here: the sender is already in a session, and the+ /// receiver is the party this feature is for.+ fn exec_unpack(&self, args: &[String]) -> Value {+ let Some(path) = args.first() else {+ return json!({"error": "unpack requires a pack path"});+ };+ let flag = |name: &str| args.iter().any(|a| a == name);+ let value = |name: &str| {+ args.iter()+ .position(|a| a == name)+ .and_then(|i| args.get(i + 1))+ .cloned()+ };+ let opts = crate::pack::UnpackOptions {+ verify_only: flag("--verify-only"),+ check_certificate: !flag("--no-check"),+ checker: value("--checker").map(std::path::PathBuf::from),+ certificate_out_dir: value("--certificate-out").map(std::path::PathBuf::from),+ load_even_if_refused: flag("--load-even-if-refused"),+ };+ match crate::pack::Packer::new(self.graph.clone()).unpack(path, &opts) {+ Ok(result) => serde_json::from_str(&result).unwrap_or(json!({"raw": result})),+ Err(e) => json!({"error": e.to_string()}),+ }+ }+fn exec_plan(&self, args: &[String]) -> Value {let file = match args.first() {Some(p) => p,diff --git a/src/inputs.rs b/src/inputs.rsindex 4d2e7a70..966e77a5 100644--- a/src/inputs.rs+++ b/src/inputs.rs@@ -1555,14 +1555,37 @@ pub struct OntoPackInput {pub version: Option<String>,/// Record lint and enforce results in the manifest as evidence (default true)pub include_evidence: Option<bool>,+ /// A certificate directory written by a previous `onto_reason` with+ /// `certificate_dir`. Its files travel inside the pack and the manifest's+ /// content digest covers them, so the receiver re-runs the Lean checker+ /// over them instead of trusting this engine. Refused at pack time when the+ /// certificate rests on assertions the graph being packed does not contain.+ pub certificate_dir: Option<String>,+ /// The profile the certificate came from, recorded as a SENDER'S CLAIM. No+ /// checker reads it: `oo-cert` checks each step against the rule the step+ /// names.+ pub profile: Option<String>,}#[derive(Deserialize, JsonSchema)]pub struct OntoUnpackInput {/// Pack to readpub path: String,- /// Verify the checksum and report the manifest without loading (default false)+ /// Verify the digests and the certificate and report, without loading+ /// (default false)pub verify_only: Option<bool>,+ /// Run the Lean checker over the packed certificate (default true). Off+ /// reports `checker_absent_nothing_was_checked`; there is no setting that+ /// makes an unchecked run print a checked word.+ pub check_certificate: Option<bool>,+ /// Path to `oo-cert` or `oo-horn`. Default: $OO_CERT / $OO_HORN, then+ /// lean/.lake/build/bin, then $PATH.+ pub checker: Option<String>,+ /// Keep the unpacked certificate files here so the check can be repeated by+ /// hand. Default: a scratch directory that is removed.+ pub certificate_out_dir: Option<String>,+ /// Load the graph even when its certificate was refused (default false)+ pub load_even_if_refused: Option<bool>,}#[derive(Deserialize, JsonSchema)]diff --git a/src/main.rs b/src/main.rsindex d8adc3d7..e9422b76 100644--- a/src/main.rs+++ b/src/main.rs@@ -848,6 +848,36 @@ enum Commands {#[arg(default_value = "generic")]pack: String,},+ /// Load a pack, refusing it if a digest does not match, and re-run the Lean+ /// checker over the certificate the pack carries.+ ///+ /// The four answers this can give are kept apart: the pack carries no+ /// certificate; the certificate was accepted; it was REFUSED, which stops+ /// the load; or this build has no checker, so nothing was checked. The last+ /// one is not a pass.+ ///+ /// `pack` stays MCP-only and this does not, on purpose. The receiver is the+ /// party this exists for, and requiring them to run an MCP client to audit+ /// a pack would defeat it.+ Unpack {+ /// Pack to read+ path: String,+ /// Report without loading+ #[arg(long)]+ verify_only: bool,+ /// Skip the checker. Reports `checker_absent_nothing_was_checked`.+ #[arg(long)]+ no_check: bool,+ /// Path to oo-cert or oo-horn+ #[arg(long)]+ checker: Option<String>,+ /// Keep the unpacked certificate here so the check can be repeated+ #[arg(long)]+ certificate_out: Option<String>,+ /// Load the graph even when its certificate was refused+ #[arg(long)]+ load_even_if_refused: bool,+ },/// Run active SPARQL watchersMonitor,/// Clear monitor block state@@ -3423,6 +3453,27 @@ async fn async_main() -> anyhow::Result<()> {.unwrap_or_else(|e| format!(r#"{{"error":"{}"}}"#, e));output_result(&result, cli.pretty);}+ Commands::Unpack {+ path,+ verify_only,+ no_check,+ checker,+ certificate_out,+ load_even_if_refused,+ } => {+ let (_db, graph) = setup(&cli.data_dir)?;+ let opts = open_ontologies::pack::UnpackOptions {+ verify_only,+ check_certificate: !no_check,+ checker: checker.map(std::path::PathBuf::from),+ certificate_out_dir: certificate_out.map(std::path::PathBuf::from),+ load_even_if_refused,+ };+ let result = open_ontologies::pack::Packer::new(graph)+ .unpack(&path, &opts)+ .unwrap_or_else(|e| format!(r#"{{"error":"{e}"}}"#));+ output_result(&result, cli.pretty);+ }Commands::Monitor => {let (db, graph) = setup(&cli.data_dir)?;let monitor = open_ontologies::monitor::Monitor::new(db, graph);diff --git a/src/pack.rs b/src/pack.rsindex 5ad10618..00bd028d 100644--- a/src/pack.rs+++ b/src/pack.rs@@ -1,4 +1,5 @@-//! Knowledge packs: a verified graph and its evidence in one portable file.+//! Knowledge packs: a verified graph, its evidence and its proof in one+//! portable file.//!//! TrustGraph calls the idea a knowledge core, and it is the right one: what//! you promote between environments should be a versioned artifact, not a@@ -14,16 +15,97 @@//! - a checksum over the graph, so tampering or truncation is detectable;//! - the verification evidence recorded at pack time (lint, enforce), so//! the receiving environment can see what the graph passed rather than-//! take it on trust.+//! take it on trust;+//! - and, when the sender has one, the derivation certificate for the run+//! that produced the graph, plus a second checksum over the graph AND the+//! certificate together.//!-//! Unpacking verifies the checksum before loading a single triple.+//! # Evidence and proof are different objects+//!+//! `evidence` is what the SENDER'S engine said. A receiver who reads it is+//! trusting a report. The certificate is not a report: it is the premises and+//! the derivation steps, and the receiver runs their own checker over them.+//! The engine that wrote the pack is then out of the trusted set for that one+//! claim, which is why the two live in separate fields rather than being+//! folded together.+//!+//! What a certificate settles and what it does not is stated in the RECEIVER'S+//! report rather than only here, in [`MEANS`], [`DOES_NOT_MEAN`] and+//! [`ABSENT_MEANS`]. The short form: it proves the materialised triples follow+//! from the asserted ones under the rules that ran. It proves nothing about+//! whether the asserted triples are true, and it says nothing about axioms no+//! rule in the fragment can read.+//!+//! Unpacking verifies both digests before loading a single triple, and a+//! certificate this machine's checker REFUSED stops the load unless the caller+//! asks for it anyway.-use crate::graph::GraphStore;+use crate::graph::{GraphStore, ReadScope};+use crate::projection_entailment::{self as pe, CertKind, CheckerStatus, LAKE_INSTALL};+use crate::verdict::Certified;use sha2::{Digest, Sha256};+use std::collections::{BTreeMap, BTreeSet};+use std::path::{Path, PathBuf};use std::sync::Arc;-pub struct Packer {- graph: Arc<GraphStore>,+/// The only file names a pack may carry inside a certificate, in BOTH+/// directions.+///+/// At pack time a certificate directory can hold whatever an earlier run left+/// in it, and slurping the directory would put unrelated bytes under the+/// content digest. At unpack time the names come from the SENDER, and a pack+/// naming `../../.ssh/authorized_keys` would otherwise be written wherever the+/// receiver's scratch directory resolves that to. Every name here is one+/// `Reasoner::run_full` writes or one `Reasoner::run_horn_scoped` writes.+const CERTIFICATE_FILES: &[&str] = &[+ "asserted.tsv",+ "asserted.sha256",+ "derivations.tsv",+ "horn.tsv",+ "rules.tsv",+ "scope.tsv",+ "refutation.tsv",+];++/// A certificate directory, carried inside a pack.+///+/// The files travel as text rather than as base64 because they are TSV and a+/// pack is meant to be diffable, which is the same reason the graph is sorted+/// N-Triples. Bytes are preserved exactly: nothing here normalises a line+/// ending, because the checker reads these bytes and a rewritten `\r\n` would+/// be a different file from the one the reasoner wrote and hashed.+///+/// ONE certificate, and the field is singular in the format rather than a list.+/// A real promotion may involve several runs, an RDFS pass and then a supplied+/// Horn table, and making this a list would be cheap now and expensive later.+/// It is singular because a second certificate raises a question nobody can+/// answer honestly yet: what the COMBINED verdict is when one is accepted and+/// the other refused. Until there is an answer to that, a pack that needs two+/// proofs is two packs.+#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]+pub struct PackedCertificate {+ /// `oo-cert/1` or `oo-horn/1`, echoed from the reasoner's report. ECHOED+ /// AND NEVER ACTED ON: [`kind_of`] decides which checker reads this from+ /// the files that are present, so a sender cannot point the receiver at+ /// the wrong checker by editing a string.+ pub format: String,+ /// File name to contents. A `BTreeMap` because [`content_digest`] frames+ /// the entries in iteration order, and two orders would be two digests for+ /// one certificate.+ pub files: BTreeMap<String, String>,+ /// Lines in `asserted.tsv` and in the derivations file. Under the content+ /// digest like everything else, so a reader can size the certificate before+ /// writing a byte of it to disk and still be reading a number nobody+ /// edited.+ pub asserted: usize,+ pub derivations: usize,+ /// The profile or rule file the SENDER says produced this. Nothing in the+ /// certificate proves it and no checker reads it: `oo-cert` checks each+ /// step against the rule the step itself names, and `oo-horn` against+ /// `rules.tsv`. It is recorded because it is useful and it is labelled a+ /// sender's claim everywhere it is printed.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub profile_claimed_by_sender: Option<String>,}#[derive(serde::Serialize, serde::Deserialize)]@@ -33,11 +115,53 @@ pub struct Manifest {pub created_at: String,pub tool_version: String,pub triples: usize,+ /// SHA-256 of the graph text and NOTHING ELSE. Unchanged, including for a+ /// pack that carries a certificate, so a reader who checked packs before+ /// this field existed is still checking the same thing.pub sha256: String,/// What the graph passed at pack time. Absent means not checked, which/// is itself information the receiver should act on.#[serde(default, skip_serializing_if = "Option::is_none")]pub evidence: Option<serde_json::Value>,+ /// The derivation certificate for the run that produced this graph.+ ///+ /// `evidence` above is what the sender's engine SAID. This is what a+ /// receiver's own checker can re-derive, which is a different kind of+ /// object, so it gets its own field rather than being folded into the+ /// evidence blob.+ ///+ /// `#[serde(default)]` here is explicitness and NOT the backward+ /// compatibility guarantee, which is worth writing down because it is easy+ /// to believe otherwise. serde's derive already reads a missing field of+ /// type `Option<T>` as `None`, so deleting the attribute changes nothing:+ /// that was mutated and the old-pack test stayed green. What actually keeps+ /// an old pack loading is the `(None, None)` arm of the digest match in+ /// [`Packer::unpack`], and+ /// `pack_certificate_test::an_old_pack_loads_and_reports_that_it_carries_no_certificate`+ /// is what turns red when that arm starts refusing.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub certificate: Option<PackedCertificate>,+ /// SHA-256 over the graph AND the certificate, framed by+ /// [`content_digest`].+ ///+ /// THE BINDING, stated precisely. `content_sha256` is the digest of, in+ /// order: a domain-separating tag, the length and bytes of the graph text,+ /// a presence byte for the certificate and, when one is present, the+ /// length-prefixed `format`, the length-prefixed+ /// `profile_claimed_by_sender`, the `asserted` and `derivations` counts,+ /// the number of files, and then every (name, body) pair in `BTreeMap`+ /// order, each half length-prefixed. So it covers every field of+ /// [`PackedCertificate`] and every byte of every file in it.+ ///+ /// `sha256` cannot see a swapped certificate, and a swapped certificate is+ /// the whole attack: a certificate that is internally sound over SOMEBODY+ /// ELSE'S assertions checks green, and a receiver that verified only the+ /// graph checksum would accept a true proof about premises that are not in+ /// the pack. Absent on every pack written before this field existed, which+ /// is why a pack that carries a certificate and no `content_sha256` is+ /// refused rather than checked (see [`Packer::unpack`]).+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub content_sha256: Option<String>,}#[derive(serde::Serialize, serde::Deserialize)]@@ -53,19 +177,451 @@ fn checksum(text: &str) -> String {format!("{:x}", hasher.finalize())}+/// The digest that binds a certificate to the graph it travels with.+///+/// Every part is length-prefixed, so no two different (graph, certificate)+/// pairs can frame to the same bytes by running together at a seam. That is the+/// framing [`crate::verdict::subject_digest`] uses, for the same reason, and it+/// is written out explicitly rather than taken from `serde_json::to_string` so+/// that a serde_json release cannot change what a security digest covers.+///+/// Public because the comparison is the point, and because a test that plays a+/// competent attacker has to be able to recompute it.+pub fn content_digest(graph: &str, cert: Option<&PackedCertificate>) -> String {+ let mut h = Sha256::new();+ h.update(b"oo-pack-content/1\n");+ h.update((graph.len() as u64).to_le_bytes());+ h.update(graph.as_bytes());+ match cert {+ None => h.update(0u64.to_le_bytes()),+ Some(c) => {+ h.update(1u64.to_le_bytes());+ for s in [+ c.format.as_str(),+ c.profile_claimed_by_sender.as_deref().unwrap_or(""),+ ] {+ h.update((s.len() as u64).to_le_bytes());+ h.update(s.as_bytes());+ }+ h.update((c.asserted as u64).to_le_bytes());+ h.update((c.derivations as u64).to_le_bytes());+ h.update((c.files.len() as u64).to_le_bytes());+ for (name, body) in &c.files {+ h.update((name.len() as u64).to_le_bytes());+ h.update(name.as_bytes());+ h.update((body.len() as u64).to_le_bytes());+ h.update(body.as_bytes());+ }+ }+ }+ format!("{:x}", h.finalize())+}++/// What a receiver learned about the certificate in a pack. NEVER COLLAPSED.+///+/// The two accepted variants carry a [`Certified`], which has no public+/// constructor anywhere in the crate, so neither can be named on a path that+/// did not run a checker and read a zero exit code off it.+/// [`PackCertVerdict::CheckerAbsent`] and+/// [`PackCertVerdict::NoCertificateInPack`] are separate words on purpose:+/// "this build has no checker" and "this pack has no proof" are different+/// facts, and neither may render as anything that reads like a pass.+///+/// ```compile_fail+/// use open_ontologies::pack::PackCertVerdict;+/// use open_ontologies::verdict::Certified;+/// let v = PackCertVerdict::Accepted(Certified {+/// theorem: "OOCert.certificate_sound",+/// subject: [0u8; 32],+/// });+/// ```+#[derive(Clone, Debug)]+pub enum PackCertVerdict {+ /// CERTIFIED. `oo-cert` accepted the packed certificate under+ /// `OOCert.certificate_sound`.+ Accepted(Certified),+ /// CERTIFIED AND WEAKER. `oo-horn check` accepted a certificate over a rule+ /// table the SENDER supplied, so the conclusions hold in every model of the+ /// asserted triples that also satisfies that table. Never shortened to the+ /// word above.+ AcceptedUnderSuppliedRules(Certified),+ /// Exit 1. The checker named a step it would not accept. A defect in the+ /// pack, never a downgrade to an unchecked verdict.+ Refused,+ /// Exit 2, an unreadable file, an unknown file name, or files that do not+ /// form a certificate either checker reads. Not a verdict in either+ /// direction, exactly as `lean/Main.lean` reserves the two codes.+ Unreadable,+ /// The certificate checks and is about assertions this pack does not+ /// contain. A sound proof over premises nobody shipped.+ NotAboutThisPack,+ /// No checker on this machine. NOTHING WAS CHECKED.+ CheckerAbsent,+ /// The pack carries no certificate at all. Every pack written before this+ /// field existed lands here.+ NoCertificateInPack,+}++impl PackCertVerdict {+ pub fn word(&self) -> &'static str {+ match self {+ PackCertVerdict::Accepted(_) => "certificate_accepted",+ PackCertVerdict::AcceptedUnderSuppliedRules(_) => {+ "certificate_accepted_under_supplied_rules"+ }+ PackCertVerdict::Refused => "certificate_refused",+ PackCertVerdict::Unreadable => "certificate_unreadable",+ PackCertVerdict::NotAboutThisPack => "certificate_not_about_this_pack",+ PackCertVerdict::CheckerAbsent => "checker_absent_nothing_was_checked",+ PackCertVerdict::NoCertificateInPack => "no_certificate_in_pack",+ }+ }+ pub fn theorem(&self) -> Option<&'static str> {+ match self {+ PackCertVerdict::Accepted(c) | PackCertVerdict::AcceptedUnderSuppliedRules(c) => {+ Some(c.theorem())+ }+ _ => None,+ }+ }+ /// True only for the two variants a checker had to accept to produce.+ pub fn is_checked(&self) -> bool {+ self.theorem().is_some()+ }+ /// Whether this verdict stops the graph being loaded. A refused or+ /// unreadable certificate, and one that is about another graph, are defects+ /// in the pack: a graph whose own proof does not check is not a graph to+ /// promote. An absent checker is not a defect and does not block: refusing+ /// the load there would make a proof-carrying pack unusable on every+ /// machine without Lean, which is the opposite of the point.+ pub fn blocks_the_load(&self) -> bool {+ matches!(+ self,+ PackCertVerdict::Refused+ | PackCertVerdict::Unreadable+ | PackCertVerdict::NotAboutThisPack+ )+ }+ /// Every word, for a suite that enumerates the vocabulary without being+ /// able to build the certified variants.+ pub const WORDS: [&'static str; 7] = [+ "certificate_accepted",+ "certificate_accepted_under_supplied_rules",+ "certificate_refused",+ "certificate_unreadable",+ "certificate_not_about_this_pack",+ "checker_absent_nothing_was_checked",+ "no_certificate_in_pack",+ ];+}++// Written by hand: `serialize_as_word!` is private to src/verdict.rs.+impl serde::Serialize for PackCertVerdict {+ fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {+ s.serialize_str(self.word())+ }+}++impl std::fmt::Display for PackCertVerdict {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.write_str(self.word())+ }+}++/// What an acceptance settles, in the report rather than in the documentation.+///+/// Held as a constant because the same words belong in `onto_unpack`, in the+/// CLI output and in the decision record, and a claim that lives in four copies+/// drifts in three of them.+pub const MEANS: &str = "A checker on THIS machine re-derived every conclusion in this pack's \+ certificate from the triples the certificate lists as asserted, and accepted all of them. \+ You did not have to trust the sender's engine to learn that. To repeat it by hand, unpack \+ again with certificate_out_dir set and run the command in check_with over the files it \+ keeps.";++/// What an acceptance does NOT settle. Printed beside every accepted verdict.+pub const DOES_NOT_MEAN: &str = "The asserted triples themselves are NOT proved. A certificate \+ proves that the materialised triples follow from the asserted ones under the rules that ran, \+ and nothing more. It does not check that the asserted triples are true, that they describe \+ the world, that they came from the system the sender says they came from, or that the sender \+ left anything out. It is also silent about axioms outside the fragment: a profile run \+ evaluates part of OWL 2 RL, a supplied table is whatever rules.tsv holds, and an axiom no \+ rule fires on contributed nothing and is invisible here. Run onto_dlp_boundary over the \+ loaded graph to see which of its axioms the rule table can read. A sound proof over the \+ wrong premises is still the wrong answer.";++/// What an absent checker settles, which is nothing.+///+/// The wording follows `web/try/api/index.py`, which says the release carries+/// no checker rather than implying a check happened. "We did not check" must+/// never render as "it passed".+pub const ABSENT_MEANS: &str = "No checker ran here, so NOTHING WAS CHECKED. That is not a pass \+ and it is not a failure: no statement in this report rests on a checker having run.";++/// The limit of what running the checker buys, printed beside every checker+/// result.+///+/// The receiver chose the binary. `$OO_CERT` pointing at a script that exits+/// zero and prints the right theorem name earns the accepted word, exactly as+/// `src/verdict.rs` residual hole 1 says. That is now the RECEIVER'S own foot+/// rather than the sender's, which is an improvement and not a closure, and the+/// report says so rather than leaving it in a comment.+pub const CHECKER_LIMIT: &str = "The checker is a binary THIS machine chose. A path in $OO_CERT \+ that exits zero and prints the right theorem name earns the accepted word, so this verdict \+ is only as good as the binary behind it. checker_says_it_is below is the block the binary \+ printed about itself; match its sha256 against the SHASUMS.txt of the release you meant to \+ run. A hostile binary can print any block it likes and nothing here would notice. This is \+ the limit of what running a checker buys you, and it is the receiver's own risk rather than \+ the sender's, which is the improvement.";++/// Which checker a packed certificate is for, decided by the files it carries.+///+/// `run_full` writes `derivations.tsv` and `run_horn_scoped` writes `horn.tsv`+/// plus the `rules.tsv` the run evaluated, so the file names are the+/// discriminator. The `format` field is not consulted: reading it would let a+/// sender choose the receiver's checker.+fn kind_of(files: &BTreeMap<String, String>) -> anyhow::Result<CertKind> {+ anyhow::ensure!(+ files.contains_key("asserted.tsv"),+ "this certificate carries no asserted.tsv, so there is nothing for a checker to check \+ the derivations against"+ );+ match (+ files.contains_key("derivations.tsv"),+ files.contains_key("horn.tsv"),+ ) {+ (true, false) => Ok(CertKind::OoCert),+ (false, true) => {+ anyhow::ensure!(+ files.contains_key("rules.tsv"),+ "horn.tsv is checked against the rules.tsv the run evaluated and this certificate \+ carries none, so there is no table for oo-horn to check it against"+ );+ Ok(CertKind::OoHorn)+ }+ (true, true) => anyhow::bail!(+ "this certificate carries both derivations.tsv and horn.tsv, so two runs wrote into \+ one directory. Which checker reads it would be decided here rather than by the \+ sender, and the two earn different verdicts, so it is refused"+ ),+ (false, false) => anyhow::bail!(+ "this certificate carries neither derivations.tsv nor horn.tsv, so it records no \+ derivation"+ ),+ }+}++/// Read a certificate directory into the form a pack carries.+pub fn read_certificate_dir(+ dir: &Path,+ profile_claimed: Option<&str>,+) -> anyhow::Result<PackedCertificate> {+ anyhow::ensure!(+ dir.is_dir(),+ "{} is not a directory, so there is no certificate to pack. Run onto_reason with \+ certificate_dir first",+ dir.display()+ );+ let mut files: BTreeMap<String, String> = BTreeMap::new();+ for name in CERTIFICATE_FILES {+ let p = dir.join(name);+ if !p.exists() {+ continue;+ }+ let bytes = std::fs::read(&p)?;+ // Not `from_utf8_lossy`. A pack is JSON, and a lossy conversion would+ // hand the receiver's checker different bytes from the ones the+ // reasoner wrote and hashed into asserted.sha256.+ let body = String::from_utf8(bytes).map_err(|e| {+ anyhow::anyhow!(+ "{} is not valid UTF-8 ({e}), so carrying it in a pack would change the bytes \+ the checker reads",+ p.display()+ )+ })?;+ files.insert((*name).to_string(), body);+ }+ let kind = kind_of(&files)?;+ let asserted = files["asserted.tsv"]+ .lines()+ .filter(|l| !l.is_empty())+ .count();+ let derivations = files[kind.derivations_file()]+ .lines()+ .filter(|l| !l.is_empty())+ .count();+ Ok(PackedCertificate {+ format: match kind {+ CertKind::OoCert => "oo-cert/1".to_string(),+ CertKind::OoHorn => "oo-horn/1".to_string(),+ },+ files,+ asserted,+ derivations,+ profile_claimed_by_sender: profile_claimed.map(str::to_string),+ })+}++/// How much of a certificate the graph in front of us accounts for.+///+/// Two counts, and they answer two different questions. `in_certificate_only`+/// is the one that can refuse a pack: an assertion the certificate rests on+/// that is not in the graph means the proof is about premises nobody shipped,+/// and the checker cannot see that, because `oo-cert` verifies the steps+/// against the triples in `asserted.tsv` and has no way to ask where they came+/// from. `conclusions_not_in_pack` is reported and never refuses: a sender who+/// reasoned without materialising ships a graph that does not contain its own+/// conclusions, which is a legitimate thing to do and a thing the receiver+/// should be told.+struct Coverage {+ asserted_in_certificate: usize,+ in_certificate_only: Vec<String>,+ in_certificate_only_total: usize,+ conclusions: usize,+ conclusions_not_in_pack: Vec<String>,+ conclusions_not_in_pack_total: usize,+ unparsed_derivation_lines: usize,+}++/// The conclusions a certificate records, in the same spelling `asserted.tsv`+/// uses.+///+/// `derivations.tsv` is `rule` then 3(1+k) term fields, so the conclusion is+/// fields 1..4. `horn.tsv` is+/// `ruleIndex, bindCount, (var, term)*, cs, cp, co, premises*`, so the+/// conclusion starts at 2 + 2 * bindCount. A line whose shape does not parse is+/// COUNTED and never skipped in silence: a swallowed parse failure would let a+/// conclusion escape the coverage count, and the count would then be a number+/// that cannot go wrong.+fn conclusions_of(files: &BTreeMap<String, String>, kind: CertKind) -> (BTreeSet<String>, usize) {+ let mut out = BTreeSet::new();+ let mut unparsed = 0usize;+ for line in files[kind.derivations_file()].lines() {+ if line.is_empty() {+ continue;+ }+ let f: Vec<&str> = line.split('\t').collect();+ let at = match kind {+ CertKind::OoCert => 1usize,+ CertKind::OoHorn => match f.get(1).and_then(|k| k.parse::<usize>().ok()) {+ Some(k) => 2 + 2 * k,+ None => {+ unparsed += 1;+ continue;+ }+ },+ };+ match (f.get(at), f.get(at + 1), f.get(at + 2)) {+ (Some(s), Some(p), Some(o)) => {+ out.insert(format!("{s}\t{p}\t{o}"));+ }+ _ => unparsed += 1,+ }+ }+ (out, unparsed)+}++/// Compare a certificate against a graph, in the ONE spelling both sides use.+///+/// [`crate::reason::asserted_bytes`] is the function that turns a selection of+/// triples into `asserted.tsv` bytes, and the certificate's own `asserted.tsv`+/// was written by the same code, so the two sets are comparable as sets of+/// lines and no second spelling is introduced here.+fn coverage(+ graph: &Arc<GraphStore>,+ files: &BTreeMap<String, String>,+ kind: CertKind,+) -> anyhow::Result<Coverage> {+ let (triples, _) = graph.triples_in_scope(&ReadScope::AllGraphs)?;+ let bytes = crate::reason::asserted_bytes(&triples)?;+ let in_graph: BTreeSet<String> = String::from_utf8_lossy(&bytes)+ .lines()+ .map(str::to_string)+ .collect();+ let in_cert: BTreeSet<String> = files["asserted.tsv"]+ .lines()+ .filter(|l| !l.is_empty())+ .map(str::to_string)+ .collect();+ let (concl, unparsed) = conclusions_of(files, kind);+ let missing_premises: Vec<String> = in_cert.difference(&in_graph).cloned().collect();+ let missing_conclusions: Vec<String> = concl.difference(&in_graph).cloned().collect();+ Ok(Coverage {+ asserted_in_certificate: in_cert.len(),+ in_certificate_only_total: missing_premises.len(),+ in_certificate_only: missing_premises.into_iter().take(8).collect(),+ conclusions: concl.len(),+ conclusions_not_in_pack_total: missing_conclusions.len(),+ conclusions_not_in_pack: missing_conclusions.into_iter().take(8).collect(),+ unparsed_derivation_lines: unparsed,+ })+}++/// A store holding exactly the triples a pack's graph text carries.+///+/// Asked of a store of its own rather than of the caller's, because the+/// question is about the graph IN THE PACK and asking it of the caller's store+/// would answer a different question over whatever was already loaded.+fn stage(graph_text: &str) -> anyhow::Result<Arc<GraphStore>> {+ let staged = Arc::new(GraphStore::new());+ staged.load_ntriples(graph_text)?;+ Ok(staged)+}++/// What to pack. A struct rather than six positional arguments, because+/// `pack("a", "b", "c", None, None, None)` is a line nobody can read.+pub struct PackRequest<'a> {+ pub path: &'a str,+ pub name: &'a str,+ pub version: &'a str,+ pub evidence: Option<serde_json::Value>,+ /// A directory a previous `reason --certificate DIR` wrote. `None` packs no+ /// certificate, which is what every pack written before this field existed+ /// carries.+ ///+ /// The certificate is taken from a run that HAPPENED rather than produced+ /// here. Packing would otherwise have to choose the profile and the+ /// materialisation target itself, and the certificate would then describe a+ /// run nobody asked for.+ pub certificate_dir: Option<&'a Path>,+ pub profile_claimed: Option<&'a str>,+}++/// What the receiver is allowed to decide.+#[derive(Default)]+pub struct UnpackOptions {+ pub verify_only: bool,+ /// Off skips the checker and reports `checker_absent_nothing_was_checked`+ /// with `skipped_by_caller: true`. There is no option that makes an+ /// unchecked run print a checked word.+ pub check_certificate: bool,+ /// An explicit checker path, passed through to+ /// [`crate::projection_entailment::find_checker`]. Handed in rather than+ /// read from `$OO_CERT` here so that a test can point at a fake without+ /// mutating the environment of a parallel test.+ pub checker: Option<PathBuf>,+ /// Keep the certificate files here instead of in a scratch directory that+ /// is removed, so a receiver can re-run the checker by hand.+ pub certificate_out_dir: Option<PathBuf>,+ /// Load the graph even when the certificate did not check. Off by default:+ /// a graph whose own proof is refused is a defect, not something to+ /// promote. This changes behaviour only for packs that carry a+ /// certificate, so no pack written before this existed is affected.+ pub load_even_if_refused: bool,+}++pub struct Packer {+ graph: Arc<GraphStore>,+}+impl Packer {pub fn new(graph: Arc<GraphStore>) -> Self {Self { graph }}- /// Write the loaded graph and its evidence to `path` as a pack.- pub fn pack(- &self,- path: &str,- name: &str,- version: &str,- evidence: Option<serde_json::Value>,- ) -> anyhow::Result<String> {+ /// Write the loaded graph, its evidence and, when one is given, the+ /// certificate for the run that produced it.+ pub fn pack(&self, req: &PackRequest<'_>) -> anyhow::Result<String> {let nt = self.graph.serialize("ntriples")?;// Sorted, so the same graph always produces the same bytes: packs@@ -75,34 +631,96 @@ impl Packer {lines.sort_unstable();let graph = lines.join("\n") + "\n";+ let certificate = match req.certificate_dir {+ None => None,+ Some(dir) => {+ let cert = read_certificate_dir(dir, req.profile_claimed)?;+ // Refused at pack time, not warned about. A certificate whose+ // assertions are not all in the graph being packed produces an+ // artefact whose proof is about premises the pack does not+ // contain, and a receiver's checker would accept it. Catching+ // it here costs one set difference and no Lean toolchain, so+ // the sender learns it rather than the auditor. The receiver+ // refuses it too: the receiver must not depend on the sender+ // having checked.+ //+ // Asked of a store staged from THE PACKED TEXT rather than of+ // the live store, so the sender computes the identical set the+ // receiver will compute. Asking the live store a slightly+ // different question is how a pack-time pass and an unpack-time+ // refusal end up in the same artefact.+ let kind = kind_of(&cert.files)?;+ let staged = stage(&graph)?;+ let c = coverage(&staged, &cert.files, kind)?;+ anyhow::ensure!(+ c.in_certificate_only.is_empty(),+ "this certificate rests on {} assertion(s) that are not in the graph being \+ packed, for example {:?}. A checker would accept it, because oo-cert \+ verifies the steps against the triples inside the certificate and cannot \+ ask where they came from. Reason and pack the same graph",+ c.in_certificate_only_total,+ c.in_certificate_only.first()+ );+ Some(cert)+ }+ };+let manifest = Manifest {- name: name.to_string(),- version: version.to_string(),+ name: req.name.to_string(),+ version: req.version.to_string(),created_at: chrono::Utc::now().to_rfc3339(),tool_version: env!("CARGO_PKG_VERSION").to_string(),triples: lines.len(),sha256: checksum(&graph),- evidence,+ evidence: req.evidence.clone(),+ content_sha256: Some(content_digest(&graph, certificate.as_ref())),+ certificate,};let pack = Pack { manifest, graph };+ // The whole pack is built in memory, certificate included, and there is+ // no streaming path. A large store's certificate makes a very large+ // pack, so the byte counts below are reported rather than left for a+ // receiver to discover when the file will not fit.let json = serde_json::to_string_pretty(&pack)?;- std::fs::write(path, &json)?;+ std::fs::write(req.path, &json)?;Ok(serde_json::json!({"ok": true,- "path": path,+ "path": req.path,"name": pack.manifest.name,"version": pack.manifest.version,"triples": pack.manifest.triples,"sha256": pack.manifest.sha256,+ "content_sha256": pack.manifest.content_sha256,+ "content_sha256_covers": "the graph text and every field and file of the certificate, \+ each length-prefixed. sha256 above covers the graph alone \+ and cannot see a swapped certificate","has_evidence": pack.manifest.evidence.is_some(),+ "certificate": pack.manifest.certificate.as_ref().map(|c| serde_json::json!({+ "format": c.format,+ "files": c.files.keys().collect::<Vec<_>>(),+ "asserted": c.asserted,+ "derivations": c.derivations,+ "bytes": c.files.values().map(|v| v.len()).sum::<usize>(),+ "profile_claimed_by_sender": c.profile_claimed_by_sender,+ "checked_by_this_packer": false,+ "means": "the certificate travels with the graph and the content digest covers \+ both. NOTHING HERE HAS CHECKED IT: the receiver runs its own checker, \+ which is the point of shipping it",+ })),+ "graph_bytes": pack.graph.len(),+ "pack_json_bytes": json.len(),+ "size_means": "the pack is assembled in memory with no streaming path, so \+ pack_json_bytes is also roughly the peak cost of writing it and of \+ reading it back",}).to_string())}- /// Load a pack, refusing it if the checksum does not match.- pub fn unpack(&self, path: &str, verify_only: bool) -> anyhow::Result<String> {+ /// Load a pack, refusing it if a digest does not match, and re-check any+ /// certificate it carries with THIS machine's checker.+ pub fn unpack(&self, path: &str, opts: &UnpackOptions) -> anyhow::Result<String> {let raw = std::fs::read_to_string(path)?;let pack: Pack = serde_json::from_str(&raw)?;@@ -112,16 +730,90 @@ impl Packer {"error": "checksum mismatch: the pack was modified or truncated after it was written","expected": pack.manifest.sha256,"actual": actual,+ "verified": false,+ "loaded": false,}).to_string());}- if verify_only {+ // The digest above covers the graph and nothing else. A pack that+ // carries a certificate and no digest over both bodies is REFUSED+ // rather than checked: dropping `content_sha256` and pasting in a+ // certificate that is sound over another graph leaves `sha256`+ // matching, and there would be nothing left to notice with.+ match (&pack.manifest.certificate, &pack.manifest.content_sha256) {+ (Some(_), None) => {+ return Ok(serde_json::json!({+ "error": "this pack carries a certificate and no content_sha256 binding it to \+ the graph. The graph checksum cannot see a swapped certificate, so \+ an unbound one is refused rather than checked",+ "verified": false,+ "loaded": false,+ "certificate": {+ "present": true,+ "verdict": PackCertVerdict::Refused,+ "means": ABSENT_MEANS,+ },+ })+ .to_string());+ }+ (cert, Some(recorded)) => {+ let recomputed = content_digest(&pack.graph, cert.as_ref());+ if *recorded != recomputed {+ return Ok(serde_json::json!({+ "error": "content checksum mismatch: the graph and the certificate in this \+ pack are not the pair that was written. One of them was replaced \+ afterwards",+ "expected": recorded,+ "actual": recomputed,+ "verified": false,+ "loaded": false,+ })+ .to_string());+ }+ }+ // An old pack: no certificate and no content digest. It loads, and+ // the report says it carries no proof rather than failing.+ (None, None) => {}+ }++ let (verdict, report) = match &pack.manifest.certificate {+ None => (+ PackCertVerdict::NoCertificateInPack,+ serde_json::json!({+ "present": false,+ "verdict": PackCertVerdict::NoCertificateInPack,+ "theorem": serde_json::Value::Null,+ "means": "this pack carries no certificate, so there is nothing here to \+ re-derive. Its evidence is what the sender's engine said about the \+ graph and this receiver has re-run none of it",+ }),+ ),+ Some(cert) => self.check_packed_certificate(&pack, cert, opts)?,+ };++ if opts.verify_only {return Ok(serde_json::json!({"ok": true,"verified": true,"loaded": false,"manifest": pack.manifest,+ "certificate": report,+ })+ .to_string());+ }++ if verdict.blocks_the_load() && !opts.load_even_if_refused {+ return Ok(serde_json::json!({+ "ok": false,+ "verified": true,+ "loaded": false,+ "why_not_loaded": "the certificate in this pack did not check on this machine, so \+ nothing was loaded. A graph whose own proof is refused is a \+ defect, not something to promote. Pass load_even_if_refused to \+ load it anyway and inspect it",+ "manifest": pack.manifest,+ "certificate": report,}).to_string());}@@ -133,7 +825,303 @@ impl Packer {"loaded": true,"triples_loaded": loaded,"manifest": pack.manifest,+ "certificate": report,}).to_string())}++ fn check_packed_certificate(+ &self,+ pack: &Pack,+ cert: &PackedCertificate,+ opts: &UnpackOptions,+ ) -> anyhow::Result<(PackCertVerdict, serde_json::Value)> {+ let base = |v: &PackCertVerdict, extra: serde_json::Value| {+ let mut o = serde_json::json!({+ "present": true,+ "verdict": v.clone(),+ "theorem": v.theorem(),+ "checked_here": v.is_checked(),+ "format_claimed_by_sender": cert.format,+ "profile_claimed_by_sender": cert.profile_claimed_by_sender,+ "asserted": cert.asserted,+ "derivations": cert.derivations,+ "certificate_bytes": cert.files.values().map(|v| v.len()).sum::<usize>(),+ });+ if let (Some(a), Some(b)) = (o.as_object_mut(), extra.as_object()) {+ for (k, val) in b {+ a.insert(k.clone(), val.clone());+ }+ }+ o+ };++ let kind = match kind_of(&cert.files) {+ Ok(k) => k,+ Err(e) => {+ let v = PackCertVerdict::Unreadable;+ let r = base(+ &v,+ serde_json::json!({"why": e.to_string(), "means": ABSENT_MEANS}),+ );+ return Ok((v, r));+ }+ };++ if !opts.check_certificate {+ let v = PackCertVerdict::CheckerAbsent;+ let r = base(+ &v,+ serde_json::json!({+ "skipped_by_caller": true,+ "means": ABSENT_MEANS,+ "why": "the caller asked for no check, so nothing was checked. This is the \+ same word an absent checker earns, because the two produce the same \+ amount of evidence",+ }),+ );+ return Ok((v, r));+ }++ // The checker reads files, so the pack's strings go to disk. The name+ // joined is the WHITELIST's own literal and never the sender's string,+ // so a crafted name cannot escape this directory even if the equality+ // test below were wrong.+ let dir = match &opts.certificate_out_dir {+ Some(d) => d.clone(),+ None => scratch_dir(),+ };+ // A read-only filesystem with no writable temp directory is a reason to+ // report that nothing was checked, not a reason to fail an unpack that+ // used to succeed.+ if let Err(e) = std::fs::create_dir_all(&dir) {+ let v = PackCertVerdict::Unreadable;+ let r = base(+ &v,+ serde_json::json!({+ "why": format!(+ "the certificate could not be written to {} ({e}), so no checker could \+ read it", dir.display()+ ),+ "means": ABSENT_MEANS,+ }),+ );+ return Ok((v, r));+ }+ for name in cert.files.keys() {+ let Some(known) = CERTIFICATE_FILES.iter().find(|k| *k == name) else {+ let v = PackCertVerdict::Unreadable;+ let r = base(+ &v,+ serde_json::json!({+ "why": format!(+ "this pack names a certificate file this build does not write: \+ {name:?}. It was not written to disk"+ ),+ "allowed": CERTIFICATE_FILES,+ "means": ABSENT_MEANS,+ }),+ );+ return Ok((v, r));+ };+ if let Err(e) = std::fs::write(dir.join(*known), &cert.files[*known]) {+ let v = PackCertVerdict::Unreadable;+ let r = base(+ &v,+ serde_json::json!({+ "why": format!("{known} could not be written to {} ({e})", dir.display()),+ "means": ABSENT_MEANS,+ }),+ );+ return Ok((v, r));+ }+ }++ let asserted = dir.join("asserted.tsv");+ let derivations = dir.join(kind.derivations_file());+ let rules = (kind == CertKind::OoHorn).then(|| dir.join("rules.tsv"));+ let status = pe::run_checker(+ kind,+ opts.checker.as_deref(),+ &asserted,+ &derivations,+ rules.as_deref(),+ );++ // The question is about the graph IN THE PACK, and it is asked BEFORE+ // anything is loaded, so a pack that is about to be refused never+ // touches the receiver's graph.+ let staged = stage(&pack.graph)?;+ let cover = coverage(&staged, &cert.files, kind)?;++ let check_with = match &rules {+ Some(r) => format!(+ "{} check {} {} {}",+ kind.binary(),+ r.display(),+ asserted.display(),+ derivations.display()+ ),+ None => format!(+ "{} {} {}",+ kind.binary(),+ asserted.display(),+ derivations.display()+ ),+ };+ // A scratch directory is removed once the checker has read it, so the+ // command above names files that will not be there. Saying which is+ // cheap; printing a command a reader cannot run and letting them find+ // out is not.+ let kept = opts.certificate_out_dir.is_some();+ let checker_block = serde_json::json!({+ "status": status.word(),+ "check_with": check_with,+ "certificate_dir": dir.display().to_string(),+ "certificate_dir_kept": kept,+ "check_with_means": if kept {+ "the certificate files are still in certificate_dir, so this command repeats the \+ check exactly"+ } else {+ "the certificate went to a scratch directory that is removed once the checker has \+ read it, so this command names files that no longer exist. Unpack again with \+ certificate_out_dir set to keep them and run it"+ },+ "checker_says_it_is": checker_self_block(&status),+ "checker_limit": CHECKER_LIMIT,+ "coverage": {+ "asserted_in_certificate": cover.asserted_in_certificate,+ "in_certificate_only": cover.in_certificate_only_total,+ "sample_in_certificate_only": cover.in_certificate_only,+ "conclusions": cover.conclusions,+ "conclusions_not_in_pack": cover.conclusions_not_in_pack_total,+ "sample_conclusions_not_in_pack": cover.conclusions_not_in_pack,+ "unparsed_derivation_lines": cover.unparsed_derivation_lines,+ "means": "in_certificate_only counts assertions the proof rests on that this pack \+ does not contain, and a checker cannot see them. conclusions_not_in_pack \+ counts triples the certificate derives that this pack does not carry, \+ which is the normal shape when the sender reasoned without materialising",+ },+ "refutation_in_pack": cert.files.contains_key("refutation.tsv"),+ "refutation_means": "a refutation.tsv is carried when the run found a contradiction. \+ NOTHING in this crate runs oo-refute, so it was not checked here \+ and no verdict in this report is about it",+ });++ // The order of these arms is the policy. A checker that said no, or+ // could not read the files, outranks everything: those are defects. A+ // certificate that checks and is about another graph is next. Only then+ // can an acceptance be reported, and only while carrying the token the+ // run earned.+ let (verdict, extra) = match status {+ CheckerStatus::Rejected { ref stdout } => (+ PackCertVerdict::Refused,+ serde_json::json!({+ "checker_said": stdout,+ "means": "the checker named a step it would not accept. STOP. This pack's own \+ proof does not check, which is a defect in the pack",+ }),+ ),+ CheckerStatus::Unreadable { ref stdout } => (+ PackCertVerdict::Unreadable,+ serde_json::json!({"checker_said": stdout, "means": ABSENT_MEANS}),+ ),+ CheckerStatus::Absent { ref what, install } => (+ PackCertVerdict::CheckerAbsent,+ serde_json::json!({"why": what, "install": install, "means": ABSENT_MEANS}),+ ),+ CheckerStatus::NotNeeded { ref what } => (+ PackCertVerdict::CheckerAbsent,+ serde_json::json!({"why": what, "install": LAKE_INSTALL, "means": ABSENT_MEANS}),+ ),+ CheckerStatus::Accepted(ref a) if cover.in_certificate_only_total > 0 => (+ PackCertVerdict::NotAboutThisPack,+ serde_json::json!({+ "checker_said": a.stdout(),+ "means": "the checker accepted this certificate and it is not about this pack. \+ Its derivations follow from the assertions listed INSIDE it, and \+ those are not all in the graph you were sent, so the proof is over \+ premises nobody shipped",+ }),+ ),+ CheckerStatus::Accepted(ref a) => {+ let cert_token = a.certified();+ let weaker = matches!(kind, CertKind::OoHorn);+ let v = match kind {+ CertKind::OoCert => PackCertVerdict::Accepted(cert_token),+ CertKind::OoHorn => PackCertVerdict::AcceptedUnderSuppliedRules(cert_token),+ };+ (+ v,+ serde_json::json!({+ "checker_said": a.stdout(),+ "means": MEANS,+ "does_not_mean": DOES_NOT_MEAN,+ "and_only_under_these_rules": weaker.then_some(+ "this certificate is over a rule table the SENDER supplied, carried in \+ rules.tsv. Its conclusions hold in every model of the asserted \+ triples that also satisfies that table, and a table saying every \+ supplier is compliant produces steps that check green for ever. Read \+ rules.tsv before reading the verdict"),+ }),+ )+ }+ };++ let mut r = base(&verdict, checker_block);+ if let (Some(a), Some(b)) = (r.as_object_mut(), extra.as_object()) {+ for (k, val) in b {+ a.insert(k.clone(), val.clone());+ }+ }+ // Rewritten from the verdict AFTER the merge, so no `extra` arm can put+ // a theorem name beside a verdict that did not earn one.+ r["theorem"] = match verdict.theorem() {+ Some(t) => serde_json::json!(t),+ None => serde_json::Value::Null,+ };+ r["checked_here"] = serde_json::json!(verdict.is_checked());+ if opts.certificate_out_dir.is_none() {+ // Best effort, and it is fine if it fails: a leftover scratch+ // directory is untidy and a missing one would break the+ // check_with command this report just printed.+ let _ = std::fs::remove_dir_all(&dir);+ }+ Ok((verdict, r))+ }+}++/// The `checker` block the binary printed ABOUT ITSELF, read out of its stdout.+///+/// Never constructed here. A Rust literal naming which binary ran would be+/// exactly the string a reader cannot check. `None` when the process printed no+/// such block, which is the honest answer for an old checker and for anything+/// that is not a checker at all.+fn checker_self_block(status: &CheckerStatus) -> Option<serde_json::Value> {+ let text = match status {+ CheckerStatus::Accepted(a) => a.stdout(),+ CheckerStatus::Rejected { stdout } | CheckerStatus::Unreadable { stdout } => stdout,+ CheckerStatus::Absent { .. } | CheckerStatus::NotNeeded { .. } => return None,+ };+ let line = text.lines().find(|l| l.starts_with('{'))?;+ let v: serde_json::Value = serde_json::from_str(line).ok()?;+ v.get("checker").cloned()+}++/// A directory nothing else is writing into.+///+/// The pid alone is not enough: `cargo test` runs these as threads of ONE+/// process, so two unpacks in one binary would collide on it and each would see+/// the other's files.+fn scratch_dir() -> PathBuf {+ static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);+ let nanos = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| d.as_nanos())+ .unwrap_or(0);+ let serial = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);+ std::env::temp_dir().join(format!(+ "oo-unpack-{}-{nanos}-{serial}",+ std::process::id()+ ))}diff --git a/src/server.rs b/src/server.rsindex b260b12a..dca2c83d 100644--- a/src/server.rs+++ b/src/server.rs@@ -1209,7 +1209,7 @@ impl OpenOntologiesServer {}}- #[tool(name = "onto_pack", description = "Write the loaded graph and its verification evidence to a portable, versioned pack: sorted N-Triples plus a manifest (name, version, counts, timestamp, sha256, and the lint/enforce results recorded at pack time). Use to promote a verified graph between environments as one auditable artifact.")]+ #[tool(name = "onto_pack", description = "Write the loaded graph, its verification evidence and, optionally, its derivation certificate to a portable, versioned pack: sorted N-Triples plus a manifest (name, version, counts, timestamp, sha256, the lint/enforce results recorded at pack time, and a content_sha256 covering the graph AND the certificate together). Pass certificate_dir to carry the proof: the evidence is what this engine SAID, the certificate is what the receiver's own Lean checker can re-derive. A certificate resting on assertions the packed graph does not contain is refused here rather than at the auditor's end.")]async fn onto_pack(&self, Parameters(input): Parameters<OntoPackInput>) -> String {use crate::pack::Packer;let name = input.name.unwrap_or_else(|| {@@ -1230,19 +1230,32 @@ impl OpenOntologiesServer {} else {None};- match Packer::new(self.graph.clone()).pack(- &input.path, &name,- &input.version.unwrap_or_else(|| "1.0.0".into()), evidence,- ) {+ let version = input.version.unwrap_or_else(|| "1.0.0".into());+ let cert_dir = input.certificate_dir.as_deref().map(std::path::Path::new);+ match Packer::new(self.graph.clone()).pack(&crate::pack::PackRequest {+ path: &input.path,+ name: &name,+ version: &version,+ evidence,+ certificate_dir: cert_dir,+ profile_claimed: input.profile.as_deref(),+ }) {Ok(json) => json,Err(e) => Self::err_json(e),}}- #[tool(name = "onto_unpack", description = "Load a pack written by onto_pack, refusing it if the checksum does not match. Pass verify_only to inspect the manifest and evidence without loading.")]+ #[tool(name = "onto_unpack", description = "Load a pack written by onto_pack, refusing it if either digest does not match, and re-run THIS machine's Lean checker over the certificate the pack carries. Four answers are kept apart: the pack carries no certificate; the certificate was accepted; it was REFUSED, which blocks the load unless load_even_if_refused is passed; or this build has no checker, so nothing was checked, which is not a pass. An acceptance proves the materialised triples follow from the asserted ones under the rules that ran; it proves nothing about whether the asserted triples are true.")]async fn onto_unpack(&self, Parameters(input): Parameters<OntoUnpackInput>) -> String {use crate::pack::Packer;- match Packer::new(self.graph.clone()).unpack(&input.path, input.verify_only.unwrap_or(false)) {+ let opts = crate::pack::UnpackOptions {+ verify_only: input.verify_only.unwrap_or(false),+ check_certificate: input.check_certificate.unwrap_or(true),+ checker: input.checker.map(std::path::PathBuf::from),+ certificate_out_dir: input.certificate_out_dir.map(std::path::PathBuf::from),+ load_even_if_refused: input.load_even_if_refused.unwrap_or(false),+ };+ match Packer::new(self.graph.clone()).unpack(&input.path, &opts) {Ok(json) => json,Err(e) => Self::err_json(e),}diff --git a/tests/pack_certificate_test.rs b/tests/pack_certificate_test.rsnew file mode 100644index 00000000..84f5b08b--- /dev/null+++ b/tests/pack_certificate_test.rs@@ -0,0 +1,811 @@+//! Proof-carrying packs: the receiver re-runs the checker and refuses the load.+//!+//! `onto_pack` used to write a graph, a checksum over it, and the lint and+//! enforce results the SENDER'S engine produced. A receiver could verify+//! integrity and nothing else: the evidence was an assertion by a machine they+//! had to trust. A pack now carries the derivation certificate too, bound to+//! the graph by a second digest, and `onto_unpack` runs this machine's Lean+//! checker over it.+//!+//! Four outcomes have to stay apart, and the fourth is the one that matters:+//!+//! 1. no certificate in the pack;+//! 2. a certificate this machine's checker ACCEPTED;+//! 3. a certificate it REFUSED, which stops the load;+//! 4. no checker in this build, so NOTHING WAS CHECKED.+//!+//! The last is not a pass, and the assertion that pins it is the one that looks+//! for the accepted word in the whole serialised report and requires it to be+//! absent.+//!+//! The two legs that need a real `oo-cert` skip loudly through+//! `common::skip_unless`; the `lean` job runs this file with+//! `OO_REQUIRE_FIXTURES=1`, which turns that skip into a failure. Everything+//! else runs with no toolchain, by handing the checker in as a path.++mod common;++use open_ontologies::graph::GraphStore;+use open_ontologies::pack::{PackCertVerdict, PackRequest, PackedCertificate, Packer, UnpackOptions, content_digest};+use open_ontologies::reason::{InferenceTarget, Reasoner};+use open_ontologies::verdict::CHECKER_OWNED_WORDS;+use serde_json::Value;+use std::path::{Path, PathBuf};+use std::process::Command;+use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::{Arc, OnceLock};++// ───────────────────────────────────────────────────────────────────────────+// Fixtures+// ───────────────────────────────────────────────────────────────────────────++const PREFIXES: &str = r#"+ @prefix : <http://ex.org/> .+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+"#;++/// An ontology whose `owl:intersectionOf` puts BLANK NODES in both the graph+/// and `asserted.tsv`.+///+/// Deliberate. The coverage check compares the certificate's `asserted.tsv`+/// against `reason::asserted_bytes` over a store staged from the packed+/// N-Triples, so it depends on `serialize("ntriples")` then `load_ntriples`+/// preserving blank-node labels. `GraphStore::load_lines` does not rename them,+/// so it does today. If that ever changes, every pack whose ontology uses an+/// `rdf:List` starts reporting `certificate_not_about_this_pack` and refusing+/// to load, and this file is what says so rather than a support ticket.+const LIST_BEARING: &str = r#"+ :A rdfs:subClassOf :B . :B rdfs:subClassOf :C . :a a :A .+ :I owl:intersectionOf ( :M1 :M2 ) . :i a :M1 , :M2 .+"#;++/// A different ontology, for the certificate that is about somebody else's+/// graph.+const OTHER_GRAPH: &str = r#"+ :P rdfs:subClassOf :Q . :Q rdfs:subClassOf :R . :p a :P .+"#;++fn repo() -> PathBuf {+ PathBuf::from(env!("CARGO_MANIFEST_DIR"))+}++fn scratch(name: &str) -> PathBuf {+ static NEXT: AtomicU64 = AtomicU64::new(0);+ let serial = NEXT.fetch_add(1, Ordering::Relaxed);+ let dir = std::env::temp_dir().join(format!(+ "oo-packcert-{name}-{}-{serial}",+ std::process::id()+ ));+ let _ = std::fs::remove_dir_all(&dir);+ std::fs::create_dir_all(&dir).unwrap();+ dir+}++/// Reason over `ttl` with a certificate, and return (store, certificate dir).+fn certify(ttl: &str, dir: &Path) -> Arc<GraphStore> {+ let store = Arc::new(GraphStore::new());+ store.load_turtle(&format!("{PREFIXES}{ttl}"), None).unwrap();+ Reasoner::run_full(+ &store,+ "owl-rl-ext",+ true,+ InferenceTarget::DefaultGraph,+ Some(dir),+ )+ .unwrap();+ store+}++/// Write a pack carrying the certificate in `cert_dir`, and return its path.+fn pack_with_certificate(store: &Arc<GraphStore>, cert_dir: &Path, out: &Path) -> Value {+ let path = out.join("carried.oopack");+ let json = Packer::new(store.clone())+ .pack(&PackRequest {+ path: path.to_str().unwrap(),+ name: "carried",+ version: "1.0.0",+ evidence: None,+ certificate_dir: Some(cert_dir),+ profile_claimed: Some("owl-rl-ext"),+ })+ .expect("pack with a certificate");+ serde_json::from_str(&json).expect("the pack result is JSON")+}++fn read_pack(path: &Path) -> Value {+ serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()+}++fn write_pack(path: &Path, pack: &Value) {+ std::fs::write(path, serde_json::to_string_pretty(pack).unwrap()).unwrap();+}++/// Unpack into a fresh store, so nothing a previous leg loaded can be mistaken+/// for what this one loaded.+fn unpack(path: &Path, opts: &UnpackOptions) -> Value {+ let store = Arc::new(GraphStore::new());+ let gate = common::exec_gate();+ let out = Packer::new(store)+ .unpack(path.to_str().unwrap(), opts)+ .expect("unpack returns a report rather than an error");+ drop(gate);+ serde_json::from_str(&out).expect("the unpack report is JSON")+}++fn options(checker: Option<PathBuf>) -> UnpackOptions {+ UnpackOptions {+ verify_only: false,+ check_certificate: true,+ checker,+ certificate_out_dir: None,+ load_even_if_refused: false,+ }+}++// ───────────────────────────────────────────────────────────────────────────+// Checkers, fake and real+// ───────────────────────────────────────────────────────────────────────────++/// A script that prints what a checker prints and exits with `code`.+///+/// A fresh path per call. `cargo test` runs these as parallel threads of one+/// process, and a shared path means one thread rewriting a script while another+/// execs it, which on Linux is ETXTBSY. `common::exec_gate` covers the other+/// half of the same race: any write while any fork is in flight.+fn fake_checker(code: i32) -> PathBuf {+ static NEXT: AtomicU64 = AtomicU64::new(0);+ let dir = std::env::temp_dir().join("oo-packcert-fake-checkers");+ std::fs::create_dir_all(&dir).unwrap();+ let ext = if cfg!(windows) { "cmd" } else { "sh" };+ let serial = NEXT.fetch_add(1, Ordering::Relaxed);+ let p = dir.join(format!("exit{code}_{}_{serial}.{ext}", std::process::id()));+ // It PRINTS the theorem, because a real checker does and the mint reads it.+ // A script that exits zero in silence names nothing and mints nothing.+ let say = r#"{"ok":true,"theorem":"OOCert.certificate_sound","checker":{"name":"fake","sha256":"0"}}"#;+ let body = if cfg!(windows) {+ format!("@echo off\r\necho {say}\r\nexit /b {code}\r\n")+ } else {+ format!("#!/bin/sh\necho '{say}'\nexit {code}\n")+ };+ {+ let _gate = common::exec_gate();+ std::fs::write(&p, body).unwrap();+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt as _;+ std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();+ }+ }+ p+}++fn lean_dir() -> PathBuf {+ repo().join("lean")+}++fn lake_available() -> bool {+ // `.current_dir(lean_dir())` is not cosmetic: elan resolves the toolchain+ // from the working directory's `lean-toolchain`, and the crate root has+ // none in its ancestry.+ Command::new("lake")+ .arg("--version")+ .current_dir(lean_dir())+ .output()+ .map(|o| o.status.success())+ .unwrap_or(false)+}++fn skip() -> bool {+ common::skip_unless(+ lake_available(),+ "lake (the Lean 4 build tool)",+ "install elan from https://github.com/leanprover/elan; lean/lean-toolchain pins the version",+ )+}++/// The real `oo-cert`, built once per test binary. The proofs are part of the+/// build, so a failure here is a failure and never a skip.+fn real_checker() -> PathBuf {+ static BUILT: OnceLock<PathBuf> = OnceLock::new();+ BUILT+ .get_or_init(|| {+ let out = Command::new("lake")+ .arg("build")+ .current_dir(lean_dir())+ .output()+ .expect("run lake build");+ assert!(+ out.status.success(),+ "lake build failed:\n{}\n{}",+ String::from_utf8_lossy(&out.stdout),+ String::from_utf8_lossy(&out.stderr)+ );+ let exe = lean_dir()+ .join(".lake")+ .join("build")+ .join("bin")+ .join("oo-cert");+ assert!(exe.exists(), "checker binary missing at {}", exe.display());+ exe+ })+ .clone()+}++/// The forged line from `tests/lean_certificate_test.rs`: an `rdfs9` step whose+/// premises do not support its conclusion.+const FORGED_RDFS9: &str = "rdfs9\t<http://ex.org/a>\t<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>\t<http://ex.org/Z>\t<http://ex.org/a>\t<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>\t<http://ex.org/A>\t<http://ex.org/A>\t<http://www.w3.org/2000/01/rdf-schema#subClassOf>\t<http://ex.org/B>\n";++// ───────────────────────────────────────────────────────────────────────────+// 1. Backward compatibility+// ───────────────────────────────────────────────────────────────────────────++/// A pack written before any of this existed still loads, and says it carries+/// no proof rather than failing.+///+/// Written BY HAND, with exactly the six manifest fields the old `Manifest`+/// had, so it is a genuine pre-change artefact and not a new pack with a field+/// omitted.+///+/// WHAT THIS DOES AND DOES NOT REST ON, measured rather than argued. Deleting+/// `#[serde(default)]` from both new manifest fields leaves this test GREEN:+/// serde's derive already reads a MISSING field of type `Option<T>` as `None`,+/// so the attribute is explicitness and not the guarantee. The design note this+/// was built from claimed the opposite, and the mutation disproved it. What the+/// guarantee actually rests on is the `(None, None)` arm of the digest match in+/// `Packer::unpack`, which lets a pack with no certificate and no content+/// digest through. Making that arm refuse, which is the realistic regression+/// the moment somebody decides every pack must carry the new digest, turns this+/// test red.+#[test]+fn an_old_pack_loads_and_reports_that_it_carries_no_certificate() {+ let dir = scratch("old-pack");+ let graph = "<http://ex.org/a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://ex.org/A> .\n";+ let mut h = <sha2::Sha256 as sha2::Digest>::new();+ sha2::Digest::update(&mut h, graph.as_bytes());+ let sha = format!("{:x}", sha2::Digest::finalize(h));+ let old = serde_json::json!({+ "manifest": {+ "name": "legacy",+ "version": "1.0.0",+ "created_at": "2026-01-01T00:00:00+00:00",+ "tool_version": "1.6.0",+ "triples": 1,+ "sha256": sha,+ },+ "graph": graph,+ });+ let path = dir.join("legacy.oopack");+ write_pack(&path, &old);++ let r = unpack(&path, &options(None));+ assert_eq!(r["verified"], true, "{r:#}");+ assert_eq!(r["loaded"], true, "an old pack must still load: {r:#}");+ assert_eq!(r["triples_loaded"], 1, "{r:#}");+ assert_eq!(r["certificate"]["present"], false, "{r:#}");+ assert_eq!(+ r["certificate"]["verdict"], "no_certificate_in_pack",+ "an old pack carries no proof and the report has to say which: {r:#}"+ );+ assert!(+ r["certificate"]["theorem"].is_null(),+ "no theorem without a checker: {r:#}"+ );+ // The manifest comes back as it was written. A future change that invents+ // an empty certificate object for an old pack would be reporting a proof+ // nobody shipped.+ let m = r["manifest"].as_object().expect("a manifest object");+ assert!(+ !m.contains_key("certificate") && !m.contains_key("content_sha256"),+ "an old pack's manifest must not grow fields it never had: {r:#}"+ );++ // And a pack this build writes with no certificate lands in the same place,+ // while still carrying the new digest.+ let fresh = dir.join("fresh.oopack");+ let store = Arc::new(GraphStore::new());+ store.load_ntriples(graph).unwrap();+ let written: Value = serde_json::from_str(+ &Packer::new(store)+ .pack(&PackRequest {+ path: fresh.to_str().unwrap(),+ name: "fresh",+ version: "1.0.0",+ evidence: None,+ certificate_dir: None,+ profile_claimed: None,+ })+ .unwrap(),+ )+ .unwrap();+ assert!(+ written["content_sha256"].is_string(),+ "every pack this build writes is bound: {written:#}"+ );+ assert!(written["certificate"].is_null(), "{written:#}");+ let back = unpack(&fresh, &options(None));+ assert_eq!(back["certificate"]["verdict"], "no_certificate_in_pack", "{back:#}");+ assert_eq!(back["loaded"], true, "{back:#}");+}++// ───────────────────────────────────────────────────────────────────────────+// 2. The four outcomes+// ───────────────────────────────────────────────────────────────────────────++/// The receiver can tell the four outcomes apart, and "nothing was checked"+/// never renders as "it passed".+#[test]+fn the_receiver_outcomes_are_distinguishable() {+ let cert_dir = scratch("outcomes-cert");+ let out = scratch("outcomes");+ let store = certify(LIST_BEARING, &cert_dir);+ let packed = pack_with_certificate(&store, &cert_dir, &out);+ let path = out.join("carried.oopack");++ assert_eq!(packed["certificate"]["checked_by_this_packer"], false, "{packed:#}");+ assert!(+ packed["pack_json_bytes"].as_u64().unwrap() > 0,+ "the pack size is reported so the cost of carrying a proof is visible: {packed:#}"+ );++ // (a) a checker that exits zero and names the theorem.+ let accepted = unpack(&path, &options(Some(fake_checker(0))));+ assert_eq!(+ accepted["certificate"]["verdict"], "certificate_accepted",+ "{accepted:#}"+ );+ assert_eq!(+ accepted["certificate"]["theorem"], "OOCert.certificate_sound",+ "{accepted:#}"+ );+ assert_eq!(accepted["loaded"], true, "{accepted:#}");+ assert_eq!(accepted["certificate"]["checked_here"], true, "{accepted:#}");+ // The limits, in the receiver's report and not only in a comment.+ let does_not = accepted["certificate"]["does_not_mean"]+ .as_str()+ .expect("an acceptance states what it does not settle");+ assert!(+ does_not.contains("NOT proved") && does_not.contains("outside the fragment"),+ "the report must say the asserted triples are not proved and that axioms outside \+ the fragment are invisible: {does_not}"+ );+ assert!(+ accepted["certificate"]["checker_limit"]+ .as_str()+ .unwrap_or_default()+ .contains("binary THIS machine chose"),+ "the report must say the checker is a binary the receiver chose: {accepted:#}"+ );+ assert_eq!(+ accepted["certificate"]["checker_says_it_is"]["name"], "fake",+ "the checker's own self-identifying block is echoed so a reader can match its \+ sha256 against a release: {accepted:#}"+ );++ // The scratch directory is removed once the checker has read it, so the+ // command the report prints names files that are gone, and the report says+ // so rather than letting a reader find out.+ assert_eq!(+ accepted["certificate"]["certificate_dir_kept"], false,+ "{accepted:#}"+ );+ assert!(+ accepted["certificate"]["check_with_means"]+ .as_str()+ .unwrap_or_default()+ .contains("no longer exist"),+ "a command over a deleted directory must be labelled as one: {accepted:#}"+ );+ assert!(+ !PathBuf::from(+ accepted["certificate"]["certificate_dir"]+ .as_str()+ .unwrap()+ )+ .exists(),+ "the scratch directory must actually be gone: {accepted:#}"+ );++ // ... and with certificate_out_dir the files stay, so the command runs.+ let kept_dir = scratch("outcomes-kept").join("cert");+ let kept = unpack(+ &path,+ &UnpackOptions {+ certificate_out_dir: Some(kept_dir.clone()),+ ..options(Some(fake_checker(0)))+ },+ );+ assert_eq!(kept["certificate"]["certificate_dir_kept"], true, "{kept:#}");+ assert!(+ kept_dir.join("asserted.tsv").exists() && kept_dir.join("derivations.tsv").exists(),+ "the files a receiver was told to re-check must be there: {}",+ kept_dir.display()+ );+ assert!(+ kept["certificate"]["check_with_means"]+ .as_str()+ .unwrap_or_default()+ .contains("repeats the check exactly"),+ "{kept:#}"+ );++ // (b) exit 1: refused, and the load is blocked.+ let refused = unpack(&path, &options(Some(fake_checker(1))));+ assert_eq!(+ refused["certificate"]["verdict"], "certificate_refused",+ "{refused:#}"+ );+ assert_eq!(+ refused["loaded"], false,+ "a graph whose own proof is refused must not be promoted: {refused:#}"+ );+ assert!(refused["certificate"]["theorem"].is_null(), "{refused:#}");++ // ... unless the caller asks for it, which is the documented escape.+ let anyway = unpack(+ &path,+ &UnpackOptions {+ load_even_if_refused: true,+ ..options(Some(fake_checker(1)))+ },+ );+ assert_eq!(anyway["loaded"], true, "{anyway:#}");+ assert_eq!(+ anyway["certificate"]["verdict"], "certificate_refused",+ "loading it anyway must not change the verdict: {anyway:#}"+ );++ // (c) exit 2: unreadable, which is not a verdict in either direction.+ let unreadable = unpack(&path, &options(Some(fake_checker(2))));+ assert_eq!(+ unreadable["certificate"]["verdict"], "certificate_unreadable",+ "{unreadable:#}"+ );+ assert_eq!(unreadable["loaded"], false, "{unreadable:#}");++ // (d) no checker on this machine. The graph still loads, and the report+ // must not contain the accepted word anywhere in it.+ let absent = unpack(&path, &options(Some(PathBuf::from("/nonexistent/oo-cert"))));+ assert_eq!(+ absent["certificate"]["verdict"], "checker_absent_nothing_was_checked",+ "{absent:#}"+ );+ assert_eq!(+ absent["loaded"], true,+ "an absent checker must not make a proof-carrying pack unusable: {absent:#}"+ );+ assert!(absent["certificate"]["theorem"].is_null(), "{absent:#}");+ assert_eq!(absent["certificate"]["checked_here"], false, "{absent:#}");+ let whole = serde_json::to_string(&absent).unwrap();+ assert!(+ !whole.contains("certificate_accepted"),+ "we did not check, and the report says it passed somewhere: {absent:#}"+ );+ assert!(+ absent["certificate"]["means"]+ .as_str()+ .unwrap_or_default()+ .contains("NOTHING WAS CHECKED"),+ "{absent:#}"+ );++ // And the caller who turns the check off earns the same word, because the+ // two produce the same amount of evidence.+ let skipped = unpack(+ &path,+ &UnpackOptions {+ check_certificate: false,+ ..options(None)+ },+ );+ assert_eq!(+ skipped["certificate"]["verdict"], "checker_absent_nothing_was_checked",+ "{skipped:#}"+ );+ assert_eq!(skipped["certificate"]["skipped_by_caller"], true, "{skipped:#}");+}++// ───────────────────────────────────────────────────────────────────────────+// 3. The binding+// ───────────────────────────────────────────────────────────────────────────++/// A swapped certificate is caught by the manifest digest.+///+/// `sha256` covers the graph and nothing else, so it matches after the swap.+/// `content_sha256` covers the graph AND every field and file of the+/// certificate, and it is the only thing that can notice. The checker is never+/// spawned, which is the point: the refusal is the digest's and not the+/// checker's.+#[test]+fn a_swapped_certificate_is_caught_by_the_content_digest() {+ let out = scratch("swap");+ let mine = scratch("swap-mine-cert");+ let theirs = scratch("swap-theirs-cert");+ let store = certify(LIST_BEARING, &mine);+ let other = certify(OTHER_GRAPH, &theirs);+ pack_with_certificate(&store, &mine, &out);+ let other_out = scratch("swap-other");+ pack_with_certificate(&other, &theirs, &other_out);++ let path = out.join("carried.oopack");+ let mut pack = read_pack(&path);+ let other_pack = read_pack(&other_out.join("carried.oopack"));++ // Only the certificate changes. The graph, and therefore `sha256`, is+ // untouched.+ pack["manifest"]["certificate"] = other_pack["manifest"]["certificate"].clone();+ write_pack(&path, &pack);++ let r = unpack(&path, &options(Some(fake_checker(0))));+ assert_eq!(r["loaded"], false, "{r:#}");+ assert!(+ r["error"]+ .as_str()+ .unwrap_or_default()+ .contains("content checksum mismatch"),+ "a swapped certificate must be caught by the content digest: {r:#}"+ );+ assert!(+ r["certificate"].is_null(),+ "the checker must never be reached on a pack whose bodies do not match: {r:#}"+ );+}++/// A pack that carries a certificate and no `content_sha256` is REFUSED rather+/// than checked.+///+/// Without this the downgrade is free: strip the new field, paste in a+/// certificate that is sound over another graph, and the old, weaker+/// verification passes it.+#[test]+fn a_certificate_with_no_content_digest_is_refused_not_checked() {+ let cert_dir = scratch("unbound-cert");+ let out = scratch("unbound");+ let store = certify(LIST_BEARING, &cert_dir);+ pack_with_certificate(&store, &cert_dir, &out);++ let path = out.join("carried.oopack");+ let mut pack = read_pack(&path);+ pack["manifest"]+ .as_object_mut()+ .unwrap()+ .remove("content_sha256");+ assert!(+ pack["manifest"]["certificate"].is_object(),+ "the certificate has to stay, or this tests nothing"+ );+ write_pack(&path, &pack);++ let r = unpack(&path, &options(Some(fake_checker(0))));+ assert_eq!(r["loaded"], false, "{r:#}");+ assert!(+ r["error"]+ .as_str()+ .unwrap_or_default()+ .contains("content_sha256"),+ "the refusal must name the missing binding: {r:#}"+ );+ let whole = serde_json::to_string(&r).unwrap();+ assert!(+ !whole.contains("certificate_accepted"),+ "an unbound certificate must not earn an accepted word: {r:#}"+ );+}++// ───────────────────────────────────────────────────────────────────────────+// 4. The real checker+// ───────────────────────────────────────────────────────────────────────────++/// A conclusion forged INSIDE the packed certificate is refused by the real+/// `oo-cert`, whether or not the attacker repairs the digest.+#[test]+fn a_forged_conclusion_in_the_packed_certificate_is_refused() {+ if skip() {+ return;+ }+ let checker = real_checker();+ let cert_dir = scratch("forge-cert");+ let out = scratch("forge");+ let store = certify(LIST_BEARING, &cert_dir);+ let packed = pack_with_certificate(&store, &cert_dir, &out);+ let path = out.join("carried.oopack");++ // The honest case first. A forgery test whose honest case was never shown+ // to pass proves nothing.+ let honest = unpack(&path, &options(Some(checker.clone())));+ assert_eq!(+ honest["certificate"]["verdict"], "certificate_accepted",+ "the honest certificate must be accepted by the real checker first: {honest:#}"+ );+ assert_eq!(+ honest["certificate"]["theorem"], "OOCert.certificate_sound",+ "{honest:#}"+ );+ assert_eq!(honest["loaded"], true, "{honest:#}");+ assert_eq!(+ honest["certificate"]["coverage"]["in_certificate_only"], 0,+ "the certificate's premises are all in the pack: {honest:#}"+ );+ eprintln!(+ "PACK SIZE: graph {} bytes, certificate {} bytes, pack JSON {} bytes",+ packed["graph_bytes"], packed["certificate"]["bytes"], packed["pack_json_bytes"]+ );++ // (a) the lazy attacker: forge a line and leave the digest alone.+ let mut pack = read_pack(&path);+ let forged = format!(+ "{}{FORGED_RDFS9}",+ pack["manifest"]["certificate"]["files"]["derivations.tsv"]+ .as_str()+ .unwrap()+ );+ pack["manifest"]["certificate"]["files"]["derivations.tsv"] =+ serde_json::json!(forged.clone());+ write_pack(&path, &pack);+ let lazy = unpack(&path, &options(Some(checker.clone())));+ assert_eq!(lazy["loaded"], false, "{lazy:#}");+ assert!(+ lazy["error"]+ .as_str()+ .unwrap_or_default()+ .contains("content checksum mismatch"),+ "the binding must notice a forged line before any checker runs: {lazy:#}"+ );++ // (b) the competent attacker: repair every digest, so only the PROOF can+ // refuse it.+ let cert: PackedCertificate =+ serde_json::from_value(pack["manifest"]["certificate"].clone()).unwrap();+ pack["manifest"]["content_sha256"] = serde_json::json!(content_digest(+ pack["graph"].as_str().unwrap(),+ Some(&cert)+ ));+ write_pack(&path, &pack);+ let competent = unpack(&path, &options(Some(checker)));+ assert_eq!(+ competent["certificate"]["verdict"], "certificate_refused",+ "the real checker must refuse a forged conclusion even when every digest \+ matches: {competent:#}"+ );+ assert_eq!(competent["loaded"], false, "{competent:#}");+ let said = competent["certificate"]["checker_said"]+ .as_str()+ .unwrap_or_default();+ assert!(+ said.contains("\"ok\":false") && said.contains("first_rejected"),+ "the checker's own words must be quoted: {said}"+ );+}++/// A certificate the checker ACCEPTS, about a graph this pack does not carry,+/// is refused anyway.+///+/// The checker was right and the pack is still refused. `oo-cert` verifies the+/// steps against the triples inside the certificate and has no way to ask where+/// they came from, so this is the gate the checker cannot be.+#[test]+fn a_certificate_about_another_graph_is_not_about_this_pack() {+ if skip() {+ return;+ }+ let checker = real_checker();+ let mine = scratch("elsewhere-mine");+ let theirs = scratch("elsewhere-theirs");+ let out = scratch("elsewhere");+ let store = certify(LIST_BEARING, &mine);+ let other = certify(OTHER_GRAPH, &theirs);++ // `Packer::pack` refuses this at pack time, so the artefact is assembled by+ // hand: the receiver must not depend on the sender having refused it.+ pack_with_certificate(&store, &mine, &out);+ let other_out = scratch("elsewhere-other");+ pack_with_certificate(&other, &theirs, &other_out);+ let path = out.join("carried.oopack");+ let mut pack = read_pack(&path);+ pack["manifest"]["certificate"] =+ read_pack(&other_out.join("carried.oopack"))["manifest"]["certificate"].clone();+ let cert: PackedCertificate =+ serde_json::from_value(pack["manifest"]["certificate"].clone()).unwrap();+ pack["manifest"]["content_sha256"] = serde_json::json!(content_digest(+ pack["graph"].as_str().unwrap(),+ Some(&cert)+ ));+ write_pack(&path, &pack);++ let r = unpack(&path, &options(Some(checker)));+ assert_eq!(+ r["certificate"]["verdict"], "certificate_not_about_this_pack",+ "{r:#}"+ );+ assert_eq!(r["loaded"], false, "{r:#}");+ assert!(+ r["certificate"]["coverage"]["in_certificate_only"]+ .as_u64()+ .unwrap_or(0)+ > 0,+ "the refusal rests on premises the pack does not carry: {r:#}"+ );+ assert!(+ r["certificate"]["checker_said"]+ .as_str()+ .unwrap_or_default()+ .contains("\"ok\":true"),+ "the checker accepted it and the pack is refused anyway, which is the whole \+ point: {r:#}"+ );+ assert!(r["certificate"]["theorem"].is_null(), "{r:#}");+}++/// The sender learns at pack time, so the auditor does not learn at unpack+/// time. The receiver-side refusal above stays either way.+#[test]+fn pack_refuses_a_certificate_that_is_not_about_the_graph() {+ let mine = scratch("senderside-mine");+ let theirs = scratch("senderside-theirs");+ let out = scratch("senderside");+ let store = certify(LIST_BEARING, &mine);+ let _other = certify(OTHER_GRAPH, &theirs);++ let path = out.join("wrong.oopack");+ let err = Packer::new(store)+ .pack(&PackRequest {+ path: path.to_str().unwrap(),+ name: "wrong",+ version: "1.0.0",+ evidence: None,+ certificate_dir: Some(&theirs),+ profile_claimed: None,+ })+ .expect_err("packing somebody else's certificate must fail");+ let text = err.to_string();+ assert!(+ text.contains("not in the graph being packed"),+ "the sender must be told which way it is wrong: {text}"+ );+ assert!(+ !path.exists(),+ "a refused pack must not be left on disk: {}",+ path.display()+ );+}++// ───────────────────────────────────────────────────────────────────────────+// 5. Vocabulary+// ───────────────────────────────────────────────────────────────────────────++/// No word this module states is a word a Lean checker states.+///+/// `src/verdict.rs` asserts this over the vocabularies defined in that module+/// and cannot see this one.+#[test]+fn no_pack_verdict_word_is_a_checker_word() {+ for w in PackCertVerdict::WORDS {+ assert!(+ !CHECKER_OWNED_WORDS.contains(&w),+ "{w} is a word the Lean binaries print for themselves. An engine opinion and a \+ machine-checked result must never share a string"+ );+ }+ let mut seen: Vec<&str> = PackCertVerdict::WORDS.to_vec();+ seen.sort_unstable();+ let before = seen.len();+ seen.dedup();+ assert_eq!(before, seen.len(), "two verdicts share a word: {seen:?}");++ // The list cannot rot away from the enum: every variant that can be built+ // without a checker states a word that is on it.+ for v in [+ PackCertVerdict::Refused,+ PackCertVerdict::Unreadable,+ PackCertVerdict::NotAboutThisPack,+ PackCertVerdict::CheckerAbsent,+ PackCertVerdict::NoCertificateInPack,+ ] {+ assert!(+ PackCertVerdict::WORDS.contains(&v.word()),+ "{} is stated by a variant and missing from WORDS",+ v.word()+ );+ assert!(!v.is_checked(), "{v} carries no checker evidence");+ }+}