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(cq): truncate result excerpts at UTF-8 boundaries

Rudy Celekli6010a37c64d1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d864085..fcbbe176 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -46,6 +46,10 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- Keep competency-question result excerpts within the existing 800-byte budget
+ at a UTF-8 character boundary, so long multilingual descriptions return the
+ normal report instead of failing during formatting.
+
- **The embedding model is loaded once per process rather than once per HTTP
session** (#262). The HTTP arm hands the same server constructor to
`StreamableHttpService::new` as a per-session factory, so everything that
diff --git a/src/cq.rs b/src/cq.rs
index e50f7dcb..3d3e538f 100644
--- a/src/cq.rs
+++ b/src/cq.rs
@@ -80,7 +80,11 @@ pub fn run_cq_suite(graph: &Arc<GraphStore>, cqs: &[CompetencyQuestion]) -> CqRu
pitfalls.push(VSPO_HINT_NO_LABELS.to_string());
}
let excerpt = if js.len() > 800 {
- format!("{}...", &js[..800])
+ let mut end = 800;
+ while !js.is_char_boundary(end) {
+ end -= 1;
+ }
+ format!("{}...", &js[..end])
} else {
js
};
diff --git a/tests/cq_unicode_excerpt_test.rs b/tests/cq_unicode_excerpt_test.rs
new file mode 100644
index 00000000..2a56cb92
--- /dev/null
+++ b/tests/cq_unicode_excerpt_test.rs
@@ -0,0 +1,64 @@
+use std::sync::Arc;
+
+use open_ontologies::cq::{CompetencyQuestion, run_cq_suite};
+use open_ontologies::graph::GraphStore;
+
+const QUERY: &str = "SELECT ?description WHERE { ?product <http://www.w3.org/2000/01/rdf-schema#comment> ?description }";
+
+fn query_and_report(description: &str) -> (String, String) {
+ let graph = Arc::new(GraphStore::new());
+ graph
+ .load_turtle(
+ &format!(
+ "<https://example.org/product> <http://www.w3.org/2000/01/rdf-schema#comment> {} .",
+ serde_json::to_string(description).unwrap()
+ ),
+ None,
+ )
+ .unwrap();
+ let query = graph.sparql_select(QUERY).unwrap();
+ let query_result: serde_json::Value = serde_json::from_str(&query).unwrap();
+ assert_eq!(query_result["results"].as_array().unwrap().len(), 1);
+ let questions = [CompetencyQuestion {
+ id: "product-description".into(),
+ question: "What description is available for each product?".into(),
+ sparql: QUERY.into(),
+ expected_min_rows: Some(1),
+ }];
+ let report = run_cq_suite(&graph, &questions);
+ assert_eq!(report.total, 1);
+ assert_eq!(report.passed, 1);
+ assert_eq!(report.results[0].row_count, 1);
+ assert!(report.results[0].passed);
+ (query, report.results[0].raw_excerpt.clone())
+}
+
+#[test]
+fn long_multilingual_results_return_a_readable_excerpt_and_normal_counts() {
+ let paragraph = "この製品は日常の配送業務で利用する標準的な資材です。担当者は商品番号と数量を確認してから出荷予定を記録します。保管場所と配送先の名称は注文書に記載されています。説明文は利用者が商品を選択するときの参考情報として表示されます。";
+ for description in [paragraph.repeat(4), "Delivery 配送 📦 ".repeat(100)] {
+ let (query, excerpt) = query_and_report(&description);
+ assert!(query.len() > 800);
+ let prefix = excerpt.strip_suffix("...").unwrap();
+ assert!(prefix.len() <= 800);
+ assert!(query.starts_with(prefix));
+ let remainder = &query[prefix.len()..];
+ assert!(prefix.len() + remainder.chars().next().unwrap().len_utf8() > 800);
+ assert!(prefix.contains("配送"));
+ }
+}
+
+#[test]
+fn ascii_results_keep_the_existing_exact_800_byte_excerpt() {
+ let description =
+ "This product is a standard material for daily shipping operations. ".repeat(20);
+ let (query, excerpt) = query_and_report(&description);
+ assert_eq!(excerpt, format!("{}...", &query[..800]));
+}
+
+#[test]
+fn short_multilingual_results_are_returned_without_truncation() {
+ let (query, excerpt) = query_and_report("通常の配送資材です。");
+ assert!(query.len() <= 800);
+ assert_eq!(excerpt, query);
+}