Elffuss BraidHub

kikocis/utopia public

Adopting an IRI adopts the shape that goes with it (#145)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Changeset047367b23d54fc70de84baea7557ef8f02f861cda9e6952f197bb0ac075e567e
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-08-31 19:19 +08:00
CommitterGitHub <noreply@github.com> · 2026-08-31 19:19 +08:00
Parentsb7cca7fa81d9

Browse the files at this changeset

2 files changed

modified crates/utopia-store/src/ontology.rs +13 −5
@@ -1144,10 +1144,18 @@
11441144
11451145 /// 把一个 IRI 认到已有的**本地**类上(原本没有 IRI 的那种)。
11461146 ///
1147-/// **只写 IRI,不动 label 与 description。** 认领要解决的是"这棵树是断的",
1148-/// 不是"用词汇表的说法覆盖用户的说法":种子类的描述是照着抽取调过的、
1149-/// 且跟库的语言走,而 schema.org 的描述是英文样板。覆盖它等于悄悄换掉
1150-/// 抽取提示词里最承重的那一句。
1147+/// **只写 IRI 与形状,不动 label、description、颜色。** 认领要解决的是
1148+/// "这棵树是断的",不是"用词汇表的说法覆盖用户的说法":种子类的描述是照着
1149+/// 抽取调过的、且跟库的语言走,而 schema.org 的描述是英文样板。覆盖它等于
1150+/// 悄悄换掉抽取提示词里最承重的那一句。
1151+///
1152+/// **形状要跟着改,因为形状说的就是来历**(方=词表声明的,圆=语料里长的)。
1153+/// 一个类被认领成"词表声明的"却还画成圆,画面就在说谎。实测过这个缝:
1154+/// 往一个已有 `person` / `organization` 的库里导 schema.org,
1155+/// 那几个类拿到了 IRI 却仍是圆的——有 IRI 却是圆的,自相矛盾。
1156+///
1157+/// 颜色不动:颜色是**身份**(同一个 key 永远同一个色),认领不改变它是谁。
1158+/// 形状是**来历**,认领恰恰改变了这一点。
11511159 ///
11521160 /// 只在 `iri IS NULL` 时写,所以重复导入是幂等的,也绝不会抢走另一个
11531161 /// 词汇表已经认领的类。
@@ -1158,7 +1166,7 @@
11581166 iri: &str,
11591167 ) -> AppResult<Option<Uuid>> {
11601168 let row: Option<(Uuid,)> = sqlx::query_as(
1161- "UPDATE entity_types SET iri = $3
1169+ "UPDATE entity_types SET iri = $3, shape = 'square'
11621170 WHERE kb_id = $1 AND key = $2 AND iri IS NULL
11631171 RETURNING id",
11641172 )
added crates/utopia-store/tests/adopting_an_iri_adopts_the_shape.rs +111 −0
@@ -0,0 +1,111 @@
1+//! 一个类被词汇表认领之后,**形状要跟着说实话**。
2+//!
3+//! 形状承载的是来历:方 = 词表声明的,圆 = 语料里长出来的。而 `adopt_iri_onto_key`
4+//! 是在形状还不表示来历的年代写的,它只写 IRI——于是往一个已经有 `person` /
5+//! `organization` 的库里导 schema.org,那几个类**拿到了 IRI 却仍然是圆的**。
6+//! 有 IRI 却是圆的,画面就在说谎。
7+//!
8+//! 这个缝隙只有真导入一次才撞得到:单看 `adopt_iri_onto_key` 的签名和调用点,
9+//! 没有任何东西提示"还有个字段该一起改"。实测撞上过(demo 库里 5 个类中招)。
10+//!
11+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
12+
13+use sqlx::PgPool;
14+use uuid::Uuid;
15+
16+#[tokio::test]
17+async fn adopting_an_iri_turns_the_class_square() -> anyhow::Result<()> {
18+ let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else {
19+ eprintln!("跳过:未设 UTOPIA_DATABASE_URL");
20+ return Ok(());
21+ };
22+ let pool = PgPool::connect(&url).await?;
23+ // 开跑前先扫地:断言 panic 会跳过 teardown,一次失败会给下一次留垃圾
24+ sqlx::query("DELETE FROM organizations WHERE name = 'adopt-shape-test'")
25+ .execute(&pool)
26+ .await?;
27+
28+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
29+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'adopt-shape-test')")
30+ .bind(org)
31+ .execute(&pool)
32+ .await?;
33+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'adopt-shape-test')")
34+ .bind(ws)
35+ .bind(org)
36+ .execute(&pool)
37+ .await?;
38+ sqlx::query(
39+ "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'adopt-shape-test')",
40+ )
41+ .bind(kb)
42+ .bind(ws)
43+ .execute(&pool)
44+ .await?;
45+
46+ let run = async {
47+ // 一个「语料里长出来的」类:没有 IRI,所以是圆的
48+ let grown = Uuid::now_v7();
49+ sqlx::query(
50+ "INSERT INTO entity_types (id, kb_id, key, label, color, shape)
51+ VALUES ($1, $2, 'person', 'Person', '#7fd0ff', 'circle')",
52+ )
53+ .bind(grown)
54+ .bind(kb)
55+ .execute(&pool)
56+ .await?;
57+
58+ let shape: String = sqlx::query_scalar("SELECT shape FROM entity_types WHERE id = $1")
59+ .bind(grown)
60+ .fetch_one(&pool)
61+ .await?;
62+ assert_eq!(shape, "circle", "还没被认领时该是圆的");
63+
64+ // 导入一份词汇表,把 schema.org 的 Person 认到这个 key 上
65+ let adopted = utopia_store::ontology::adopt_iri_onto_key(
66+ &pool,
67+ kb,
68+ "person",
69+ "https://schema.org/Person",
70+ )
71+ .await?;
72+ assert_eq!(adopted, Some(grown), "该认到那个已有的类上");
73+
74+ let (iri, shape, color): (Option<String>, String, String) =
75+ sqlx::query_as("SELECT iri, shape, color FROM entity_types WHERE id = $1")
76+ .bind(grown)
77+ .fetch_one(&pool)
78+ .await?;
79+ assert_eq!(iri.as_deref(), Some("https://schema.org/Person"));
80+ assert_eq!(
81+ shape, "square",
82+ "认领之后是词表声明的类了,形状要跟着说实话"
83+ );
84+ // **颜色不该动**:颜色是身份(同一个 key 永远同一个色),
85+ // 认领改变的是来历不是身份
86+ assert_eq!(color, "#7fd0ff", "认领不该动用户看惯的颜色");
87+
88+ // 幂等:再认一次不该改动任何东西(iri 已非空,UPDATE 匹配不上)
89+ let again = utopia_store::ontology::adopt_iri_onto_key(
90+ &pool,
91+ kb,
92+ "person",
93+ "https://example.org/OtherPerson",
94+ )
95+ .await?;
96+ assert_eq!(again, None, "已被认领的类不该被另一个词汇表抢走");
97+ let iri2: Option<String> = sqlx::query_scalar("SELECT iri FROM entity_types WHERE id = $1")
98+ .bind(grown)
99+ .fetch_one(&pool)
100+ .await?;
101+ assert_eq!(iri2.as_deref(), Some("https://schema.org/Person"));
102+
103+ Ok::<(), anyhow::Error>(())
104+ }
105+ .await;
106+
107+ sqlx::query("DELETE FROM organizations WHERE name = 'adopt-shape-test'")
108+ .execute(&pool)
109+ .await?;
110+ run
111+}