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
Make a load atomic to a concurrent reader (#195)
Fabio Rovai1b1516f6f0ca
diff --git a/src/graph.rs b/src/graph.rsindex 2a3754a9..1a7afbee 100644--- a/src/graph.rs+++ b/src/graph.rs@@ -232,9 +232,22 @@ impl GraphStore {// re-inserting a statement it already holds changes nothing and must not be// counted as a load.let before = store.len().unwrap_or(0);+ // One transaction for the whole document, so a concurrent reader+ // sees the store before this load or after it and never part way+ // through. Parsing already happened above, which made a load+ // all-or-nothing against a SYNTAX error (#93). It was not+ // all-or-nothing against a concurrent READ: the inserts ran one at a+ // time, so another session walking the store could capture a+ // half-loaded ontology, and a certified session would then write an+ // asserted.tsv describing it. Measured in+ // tests/store_atomicity_test.rs. serve-http and daemon share one+ // Arc<GraphStore> across every session, so the second party is not+ // hypothetical.+ let mut txn = store.start_transaction()?;for quad in &quads {- store.insert(quad)?;+ txn.insert(quad);}+ txn.commit()?;Ok(store.len().unwrap_or(before).saturating_sub(before))}@@ -259,9 +272,12 @@ impl GraphStore {// re-inserting a statement it already holds changes nothing and must not be// counted as a load.let before = store.len().unwrap_or(0);+ // Atomic for the reason given on the first loader above.+ let mut txn = store.start_transaction()?;for quad in &quads {- store.insert(quad)?;+ txn.insert(quad);}+ txn.commit()?;Ok(store.len().unwrap_or(before).saturating_sub(before))}@@ -290,9 +306,12 @@ impl GraphStore {// re-inserting a statement it already holds changes nothing and must not be// counted as a load.let before = store.len().unwrap_or(0);+ // Atomic for the reason given on the first loader above.+ let mut txn = store.start_transaction()?;for quad in &quads {- store.insert(quad)?;+ txn.insert(quad);}+ txn.commit()?;Ok(store.len().unwrap_or(before).saturating_sub(before))}@@ -878,6 +897,13 @@ impl GraphStore {// `tcb_8_across_runs_only_the_default_graph_leaks` caught it.return self.triples_outside(&[crate::reason::INFERRED_GRAPH]);};+ // One transaction for the WHOLE scope, not one per graph. A scoped run+ // reads several named graphs and its certificate asserts they were+ // read together; reading them under separate transactions would let a+ // write land between two of them and produce an asserted set that+ // existed at no instant, which is the defect this closes arriving one+ // level down.+ self.read_in_one_transaction(|txn| {let mut triples = Vec::new();// The names actually read, in the order read, so a certificate records// what it was built from rather than what was asked for. The two differ@@ -888,7 +914,7 @@ impl GraphStore {// `triples.len()` while the closure still holds it mutably.let mut take = |g: GraphNameRef<'_>| -> anyhow::Result<usize> {let mut n = 0usize;- for quad in self.store.quads_for_pattern(None, None, None, Some(g)) {+ for quad in txn.quads_for_pattern(None, None, None, Some(g)) {let q = quad?;triples.push((q.subject.to_string(),@@ -909,6 +935,7 @@ impl GraphStore {}}Ok((triples, read))+ })}/// Run a SELECT over exactly the graphs a scope names.@@ -977,6 +1004,38 @@ impl GraphStore {Ok(out)}+ /// Read the store inside ONE transaction, so a concurrent write cannot be+ /// observed half applied.+ ///+ /// Oxigraph 0.5 gives transactions the "repeatable read" isolation level:+ /// the state a reader sees does not change for the duration. Without one,+ /// `iter()` walks a store another thread may be writing to, and a+ /// certified run can then record an `asserted.tsv` describing a graph that+ /// existed at no instant. `tests/store_atomicity_test.rs` measures exactly+ /// that: a reader caught a one-quad-at-a-time write half done 620 times,+ /// and saw only the before and after states once the write was+ /// transactional.+ ///+ /// This matters for TCB-6 and TCB-7, which say `asserted.tsv` IS the graph+ /// reasoned over. The Lean checker cannot see a torn read: the file would+ /// be internally consistent and would prove things about a graph nobody+ /// ever had. `serve-http` and `daemon` share one `Arc<GraphStore>` across+ /// every session, so the two writers are not hypothetical.+ ///+ /// The transaction is committed rather than rolled back because it writes+ /// nothing, and committing an empty transaction is how Oxigraph ends one.+ /// A failure to end it is returned rather than swallowed: a reader that+ /// leaked a transaction would be a worse defect than the one this closes.+ fn read_in_one_transaction<T>(+ &self,+ f: impl FnOnce(&oxigraph::store::Transaction<'_>) -> anyhow::Result<T>,+ ) -> anyhow::Result<T> {+ let txn = self.store.start_transaction()?;+ let out = f(&txn);+ txn.commit()?;+ out+ }+/// Every triple in the store EXCEPT those in the named graphs listed, in/// the same spelling [`all_triples`](Self::all_triples) yields, together/// with the names of the graphs that were read.@@ -997,9 +1056,10 @@ impl GraphStore {/// graphs it read is checkable against the store, and one that does not is/// not. `<default>` is the unnamed graph.pub fn triples_outside(&self, excluded: &[&str]) -> anyhow::Result<AssertedTriples> {+ self.read_in_one_transaction(|txn| {let mut triples = Vec::new();let mut read: BTreeSet<String> = BTreeSet::new();- for quad in self.store.iter() {+ for quad in txn.iter() {let quad = quad?;let name = match &quad.graph_name {GraphName::DefaultGraph => "<default>".to_string(),@@ -1016,7 +1076,8 @@ impl GraphStore {quad.object.to_string(),));}- Ok((triples, read.into_iter().collect()))+ Ok((triples, read.into_iter().collect()))+ })}/// Copy one named graph of this store into a fresh store's DEFAULT graph,diff --git a/tests/load_is_atomic_test.rs b/tests/load_is_atomic_test.rsnew file mode 100644index 00000000..51f8b6e1--- /dev/null+++ b/tests/load_is_atomic_test.rs@@ -0,0 +1,81 @@+//! **A load is all or nothing to a concurrent reader.**+//!+//! `load_turtle` already parsed the whole document before touching the store,+//! which made a load all-or-nothing against a SYNTAX error (#93). It was not+//! all-or-nothing against a concurrent READ: the inserts ran one quad at a+//! time, so another session walking the store could capture a half-loaded+//! ontology.+//!+//! That matters because of who the other session is. `serve-http` and `daemon`+//! share one `Arc<GraphStore>` across every session, and if the reader is a+//! certified run it writes an `asserted.tsv` describing whatever it saw. TCB-6+//! and TCB-7 say that file IS the graph reasoned over. The Lean checker cannot+//! see the difference: a half-loaded graph is internally consistent, so the+//! certificate would prove things about an ontology nobody ever had.+//!+//! `tests/store_atomicity_test.rs` measured the raw store behaviour. This+//! measures `GraphStore::load_turtle`, which is what callers actually use.++use open_ontologies::graph::GraphStore;+use std::collections::BTreeSet;+use std::sync::Arc;++/// Enough statements that a reader gets many chances to catch the load half+/// done, and small enough to stay quick.+const N: usize = 400;++fn document() -> String {+ let mut s = String::from("@prefix ex: <http://example.org/> .\n");+ for i in 0..N {+ s.push_str(&format!("ex:s{i} ex:p ex:o .\n"));+ }+ s+}++#[test]+fn a_reader_never_sees_a_load_part_way_through() {+ let graph = Arc::new(GraphStore::new());+ let writer = Arc::clone(&graph);+ let doc = document();++ let t = std::thread::spawn(move || {+ writer.load_turtle(&doc, None).expect("load");+ });++ // Poll until the load lands, recording every size seen on the way.+ let mut seen = BTreeSet::new();+ loop {+ let n = graph.all_triples().map(|t| t.len()).unwrap_or(0);+ seen.insert(n);+ if n >= N {+ break;+ }+ }+ t.join().expect("writer");++ let partial: Vec<&usize> = seen.iter().filter(|n| **n > 0 && **n < N).collect();+ assert!(+ partial.is_empty(),+ "a reader saw the store holding {partial:?} statements while a {N} statement document \+ was loading. A certified run reading at that moment would record an asserted.tsv for an \+ ontology that was never loaded, and the checker would accept it because the file is \+ internally consistent. Sizes seen: {seen:?}"+ );+ assert!(+ seen.contains(&N),+ "the load never completed, so this test measured nothing: {seen:?}"+ );+}++/// The all-or-nothing guarantee against a syntax error still holds. A+/// transaction that committed before the parse checked out would trade one+/// defect for the other.+#[test]+fn a_document_with_a_syntax_error_still_loads_nothing() {+ let graph = Arc::new(GraphStore::new());+ let mut doc = document();+ doc.push_str("ex:broken ex:p ex:o\nthis is not turtle .\n");+ assert!(graph.load_turtle(&doc, None).is_err(), "a malformed document must be refused");+ let n = graph.all_triples().map(|t| t.len()).unwrap_or(0);+ assert_eq!(n, 0, "a refused load must leave the store empty, found {n} statements");+}