Skip to content

World's first open-source enterprise world model.

Importing an ontology queues alignment behind the index refresh, as the editor does (#1286)

Maya Kid5b76a41dbb90
diff --git a/crates/utopia-server/src/api/ontology_routes.rs b/crates/utopia-server/src/api/ontology_routes.rs
index 9a2a21ce..ee944bb0 100644
--- a/crates/utopia-server/src/api/ontology_routes.rs
+++ b/crates/utopia-server/src/api/ontology_routes.rs
@@ -146,13 +146,13 @@ pub struct EntityTypeReq {
pub description: Option<String>,
}
-/// 编辑器改了本体之后:先补向量索引,再排对齐。
+/// 编辑器或导入改了本体之后:先补向量索引,再排对齐。
///
/// **新元素要有向量,对齐的短名单才看得见它。** 候选多于短名单长度时按向量挑
/// (`phrase_alignment::shortlist`),类别词的候选类也按向量检索
/// (`type_alignment::candidates_for`);没向量的元素永远进不了名单,形状的指纹就不变,
/// 建了也不重判——第一次真跑 `bordered_by` 就是这样(0061 cut 1.1 的采纳路径已经补了,
-/// 这里是编辑器的四个入口)。描述改了也算:向量按「当时嵌的原文」判陈,改了描述的行
+/// 这里是编辑器的四个入口与 OWL 导入)。描述改了也算:向量按「当时嵌的原文」判陈,改了描述的行
/// 会被同一个任务重嵌。
///
/// **排任务,不就地跑。** 就地嵌一次要付一趟嵌入请求的固定开销,本体页一口气建 28 条
@@ -706,6 +706,10 @@ pub async fn apply_import(
let violations =
utopia_store::reasoning::reconcile_ontology(&mut tx, kb_id, &was_detected).await?;
tx.commit().await?;
+ // 本体多了类和属性:没有类时判成「没有」的类别词、对不上属性的签名要重判(0044 对齐)。
+ // 与编辑器同一步:先补向量再排类别词对齐,它收尾时排短语对齐。补向量那份 apply 已经
+ // 排了,撞上排着的不再排;导入已经落库,排不上也不把它报成失败
+ let _ = reindex_then_align(&state, kb_id, "align_types").await;
state.emit_review(kb_id);
Ok(Json(
json!({ "import_id": import_id, "plan": plan, "violations": violations }),
diff --git a/crates/utopia-server/src/api/token_setup_tests.rs b/crates/utopia-server/src/api/token_setup_tests.rs
index e08b9574..c1b8da78 100644
--- a/crates/utopia-server/src/api/token_setup_tests.rs
+++ b/crates/utopia-server/src/api/token_setup_tests.rs
@@ -102,6 +102,24 @@ impl Fixture {
self.respond(request).await
}
+ /// 真导入一份 Turtle:字段要带文件名,读上传只收带文件名的,同网页的导入面板
+ async fn import_turtle(
+ &self,
+ kb: Uuid,
+ token: &str,
+ ttl: &str,
+ ) -> anyhow::Result<(StatusCode, Value)> {
+ let body = format!(
+ "--x\r\nContent-Disposition: form-data; name=\"file\"; filename=\"o.ttl\"\r\n\r\n\
+ {ttl}\r\n--x--\r\n"
+ );
+ let request = Request::post(format!("/api/v1/kbs/{kb}/ontology/imports"))
+ .header("authorization", format!("Bearer {token}"))
+ .header("content-type", "multipart/form-data; boundary=x")
+ .body(Body::from(body))?;
+ self.respond(request).await
+ }
+
async fn respond(&self, request: Request<Body>) -> anyhow::Result<(StatusCode, Value)> {
let response = self.app.clone().oneshot(request).await?;
let status = response.status();
@@ -179,6 +197,14 @@ impl Fixture {
}
async fn cleanup(self) -> anyhow::Result<()> {
+ // 任务按载荷里的 kb_id 挂在库上,没有外键,删组织带不走它们
+ sqlx::query(
+ "DELETE FROM jobs WHERE payload->>'kb_id' IN
+ (SELECT id::text FROM knowledge_bases WHERE workspace_id = $1)",
+ )
+ .bind(self.ws)
+ .execute(&self.pool)
+ .await?;
sqlx::query("DELETE FROM organizations WHERE id = $1")
.bind(self.org)
.execute(&self.pool)
@@ -381,3 +407,39 @@ async fn a_write_token_reaches_no_further_than_its_owners_role_and_its_base_list
f.cleanup().await?;
result
}
+
+/// 文档先到、本体后导(建库对话框说本体以后还能再导):没有类时类别词判成「没有」,
+/// 短语签名对不上属性。导进来的类与属性要接住它们就得重排对齐,同编辑器的入口一样先排
+/// `embed_ontology` 再排对齐;不排的话要等下一篇文档,最后一篇之后导进来的类没有实例
+#[tokio::test]
+async fn importing_an_ontology_queues_alignment_behind_the_index_refresh() -> anyhow::Result<()> {
+ let Some(f) = Fixture::new().await? else {
+ return Ok(());
+ };
+ let result = async {
+ let kb = f.seeded_kb("robots").await?;
+ let token = f.token(f.admin, "write", None).await;
+ let ttl = "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n\
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\
+ @prefix ex: <http://example.org/> .\n\
+ ex:Robot a owl:Class ; rdfs:label \"Robot\" .\n";
+ let (status, body) = f.import_turtle(kb, &token, ttl).await?;
+ anyhow::ensure!(status == StatusCode::OK, "{status} {body}");
+
+ let queued: Vec<String> = sqlx::query_scalar(
+ "SELECT kind FROM jobs WHERE status = 'queued' AND payload->>'kb_id' = $1
+ ORDER BY run_at, id",
+ )
+ .bind(kb.to_string())
+ .fetch_all(&f.pool)
+ .await?;
+ anyhow::ensure!(
+ queued == ["embed_ontology", "align_types"],
+ "the alignment is queued behind the index refresh: {queued:?}"
+ );
+ anyhow::Ok(())
+ }
+ .await;
+ f.cleanup().await?;
+ result
+}
diff --git a/docs/design/ontology.md b/docs/design/ontology.md
index 60dfcd6e..6f17dac7 100644
--- a/docs/design/ontology.md
+++ b/docs/design/ontology.md
@@ -79,10 +79,10 @@ label shares a word with the phrase; without an embedding model the model sees e
candidate. A property without a vector never enters the shortlist, so the fingerprint of the
shapes it could bind does not change and they are never re-decided: every path that adds or
relabels an element therefore refreshes the index before alignment. The ontology agent's adoption,
-like the editor's create and update handlers, queues `embed_ontology` ahead of the alignment job;
-adoption queues both in the element's commit [0061]. Both aligners wait while that job is queued or
-running, so the order is vectors, then kind words, then phrases. The shortlist is part of the
-decision's basis, so a changed list re-asks. The candidate
+like the editor's create and update handlers and an OWL import, queues `embed_ontology` ahead of
+the alignment job; adoption queues both in the element's commit [0061]. Both aligners wait while
+that job is queued or running, so the order is vectors, then kind words, then phrases. The
+shortlist is part of the decision's basis, so a changed list re-asks. The candidate
properties are described once per batch and each item names only its keys. A signature the votes disagree on is `undecided` for the alignment queue of #725; one with no
fitting property is `none`, its statements stay in the open graph and it counts toward the
workbench's suggestions. Bindings live in `phrase_bindings`. A binding to a state property also