Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

fix(diff): resolve both sides against the old file's base

Vishal Habiba9163d0737fe
diff --git a/src/graph.rs b/src/graph.rs
index b4dce6ae..c64b7f1b 100644
--- a/src/graph.rs
+++ b/src/graph.rs
@@ -321,6 +321,14 @@ impl GraphStore {
/// syntax reader existed each of those called the RDF parser directly, and
/// adding a format meant finding all of them.
fn parse_document(path: &str, content: &str) -> anyhow::Result<Vec<Quad>> {
+ Self::parse_document_with_base(path, content, Self::file_base_iri(path))
+ }
+
+ fn parse_document_with_base(
+ path: &str,
+ content: &str,
+ base: Option<String>,
+ ) -> anyhow::Result<Vec<Quad>> {
match Self::detect_source_format(path, content) {
SourceFormat::OwlFunctional => Ok(crate::ofn::parse(content)?
.into_iter()
@@ -333,7 +341,7 @@ impl GraphStore {
// would not load a single triple before this. OFN needs none:
// its grammar admits only absolute IRIs.
let mut parser = RdfParser::from_format(format);
- if let Some(base) = Self::file_base_iri(path) {
+ if let Some(base) = base {
parser = parser.with_base_iri(base)?;
}
Ok(parser
@@ -386,11 +394,27 @@ impl GraphStore {
Self::content_as_turtle(path, content)
}
+ /// As `read_as_turtle`, but relative IRIs resolve against `base_path`
+ /// rather than `path`. A diff reads both sides against the old file, so
+ /// `<#Dog>` names the same IRI in both versions instead of two.
+ pub fn read_as_turtle_against(path: &str, base_path: &str) -> anyhow::Result<String> {
+ let content = std::fs::read_to_string(path)?;
+ Self::convert_to_turtle(path, content, Self::file_base_iri(base_path))
+ }
+
/// As `read_as_turtle`, for content already in hand (stdin, for instance).
pub fn content_as_turtle(path_hint: &str, content: String) -> anyhow::Result<String> {
+ Self::convert_to_turtle(path_hint, content, Self::file_base_iri(path_hint))
+ }
+
+ fn convert_to_turtle(
+ path_hint: &str,
+ content: String,
+ base: Option<String>,
+ ) -> anyhow::Result<String> {
let format = Self::detect_source_format(path_hint, &content);
if format == SourceFormat::Rdf(RdfFormat::Turtle) {
- if let Some(base) = Self::file_base_iri(path_hint) {
+ if let Some(base) = base {
// Keep the default base on the first source line so downstream
// parsers report the document's original line numbers. A later
// @base still overrides it; only first-line columns gain the prefix.
@@ -401,7 +425,7 @@ impl GraphStore {
let store = Self::new();
{
let inner = &store.store;
- let quads = Self::parse_document(path_hint, &content)?;
+ let quads = Self::parse_document_with_base(path_hint, &content, base)?;
for quad in &quads {
inner.insert(quad)?;
}
diff --git a/src/main.rs b/src/main.rs
index 189f4816..0c6d0948 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2755,11 +2755,16 @@ async fn async_main() -> anyhow::Result<()> {
Commands::Diff { old_path, new_path } => {
use open_ontologies::ontology::OntologyService;
// Read both sides as lint does: an RDF/XML file is converted rather
- // than handed to the Turtle parser, and errors stay JSON.
- let read = |path: &str| match GraphStore::read_as_turtle(path) {
+ // than handed to the Turtle parser, and errors stay JSON. Both sides
+ // resolve relative IRIs against the old file, so `<#Dog>` is the same
+ // IRI in both versions.
+ let read = |path: &str| match GraphStore::read_as_turtle_against(path, &old_path) {
Ok(c) => c,
Err(e) => {
- output_json(&serde_json::json!({"error": e.to_string()}), cli.pretty);
+ output_json(
+ &serde_json::json!({"error": format!("Cannot read {path}: {e}")}),
+ cli.pretty,
+ );
std::process::exit(1);
}
};
diff --git a/src/server.rs b/src/server.rs
index 740b3343..abe84064 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -558,7 +558,9 @@ impl OpenOntologiesServer {
Ok(c) => c,
Err(e) => return Self::err_json(format!("Cannot read {}: {}", input.old_path, e)),
};
- let new = match GraphStore::read_as_turtle(&input.new_path) {
+ // Against the old file's base, so a relative `<#Dog>` is one IRI on
+ // both sides and only real changes show up.
+ let new = match GraphStore::read_as_turtle_against(&input.new_path, &input.old_path) {
Ok(c) => c,
Err(e) => return Self::err_json(format!("Cannot read {}: {}", input.new_path, e)),
};
diff --git a/tests/mcp_lint_diff_formats_test.rs b/tests/mcp_lint_diff_formats_test.rs
index c961eadc..ce24b6b9 100644
--- a/tests/mcp_lint_diff_formats_test.rs
+++ b/tests/mcp_lint_diff_formats_test.rs
@@ -116,3 +116,65 @@ async fn onto_diff_reads_rdf_xml_like_turtle() {
assert_eq!(out["added"], 0, "{out}");
assert_eq!(out["removed"], 0, "{out}");
}
+
+/// Two versions of a relative-IRI ontology: v2 adds one class.
+const REL_V1: &str = "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n<#Dog> a owl:Class .\n<#Cat> a owl:Class .\n";
+const REL_V2: &str = "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n<#Dog> a owl:Class .\n<#Cat> a owl:Class .\n<#Cow> a owl:Class .\n";
+
+/// Each file used to get its own `@base`, so `<#Dog>` was two IRIs and every
+/// triple came back as both added and removed (3 and 2 here).
+#[tokio::test]
+async fn onto_diff_resolves_both_sides_against_one_base() {
+ let (tmp, server) = server();
+ let v1 = write(&tmp, "v1.ttl", REL_V1);
+ let v2 = write(&tmp, "v2.ttl", REL_V2);
+
+ let out: serde_json::Value = serde_json::from_str(
+ &server
+ .onto_diff(Parameters(OntoDiffInput {
+ old_path: v1,
+ new_path: v2,
+ }))
+ .await,
+ )
+ .unwrap();
+ assert!(out.get("error").is_none(), "{out}");
+ assert_eq!(out["added"], 1, "{out}");
+ assert_eq!(out["removed"], 0, "{out}");
+}
+
+fn cli_diff(old: &str, new: &str) -> (Option<i32>, serde_json::Value) {
+ let out = std::process::Command::new(env!("CARGO_BIN_EXE_open-ontologies"))
+ .args(["diff", old, new])
+ .output()
+ .unwrap();
+ let stdout = String::from_utf8_lossy(&out.stdout);
+ let json = serde_json::from_str(stdout.trim())
+ .unwrap_or_else(|e| panic!("not JSON ({e}): {stdout}"));
+ (out.status.code(), json)
+}
+
+#[test]
+fn cli_diff_resolves_both_sides_against_one_base() {
+ let tmp = tempfile::tempdir().unwrap();
+ let v1 = write(&tmp, "v1.ttl", REL_V1);
+ let v2 = write(&tmp, "v2.ttl", REL_V2);
+
+ let (_, out) = cli_diff(&v1, &v2);
+ assert!(out.get("error").is_none(), "{out}");
+ assert_eq!(out["added"], 1, "{out}");
+ assert_eq!(out["removed"], 0, "{out}");
+}
+
+#[test]
+fn cli_diff_names_the_file_it_cannot_read() {
+ let tmp = tempfile::tempdir().unwrap();
+ let v1 = write(&tmp, "v1.ttl", REL_V1);
+ let missing = tmp.path().join("missing.ttl");
+ let missing = missing.to_str().unwrap();
+
+ let (code, out) = cli_diff(&v1, missing);
+ assert_eq!(code, Some(1), "{out}");
+ let err = out["error"].as_str().unwrap_or_default();
+ assert!(err.starts_with(&format!("Cannot read {missing}: ")), "{out}");
+}