Elffuss BraidHub

kikocis/utopia public

Clicking Run is not the same as deciding what a thing is (#117)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Changesetdf0616f1e05bac28f415edd1d33e00f7df22388cced328308141706901dc4546
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-08-31 01:09 +08:00
CommitterGitHub <noreply@github.com> · 2026-08-31 01:09 +08:00
Parentsae59bab5e01a

Browse the files at this changeset

3 files changed

modified crates/utopia-server/src/api/ontology_routes.rs +1 −1
@@ -1398,7 +1398,7 @@
13981398 Path(kb_id): Path<Uuid>,
13991399 ) -> ApiResult<Json<serde_json::Value>> {
14001400 require_kb(&state, &user, kb_id, Role::Editor).await?;
1401- let outcome = crate::type_resolution::resolve(&state, kb_id, Some(user.id)).await?;
1401+ let outcome = crate::type_resolution::resolve(&state, kb_id).await?;
14021402 let _ = utopia_store::audit::record(
14031403 &state.pool,
14041404 Some(kb_id),
modified crates/utopia-server/src/type_resolution.rs +22 −8
@@ -382,13 +382,27 @@
382382 pub crosses_axis: bool,
383383 }
384384
385-/// 跑一遍类型消解:检索候选 → 裁决 → 三档处置。
386-pub async fn resolve(
387- state: &AppState,
388- kb_id: Uuid,
389- // 落库那一步记在谁头上。这条路是人在界面上按的,所以有人
390- actor: Option<Uuid>,
391-) -> AppResult<ResolutionOutcome> {
385+/// 跑一轮类型消解并落库。
386+///
387+/// **落库时不带 actor,尽管是人点的运行。** `retype_entities` 的 actor 参数
388+/// 现在有两重身份:账本里记"谁改的",而 0051 之后它还决定 `type_source`——
389+/// 有 actor 就是 `human`,而 `human` 意味着**引擎从此不再碰这个实体**。
390+///
391+/// 这两个问题不是一回事:
392+///
393+/// | | 谁发起 | 谁判定这个实体是什么 |
394+/// |---|---|---|
395+/// | 手工改实体类型 | 人 | 人 |
396+/// | 认可类对 + 点名实体 | 人 | 人 |
397+/// | **跑一轮消解** | 人点了运行 | **引擎** |
398+///
399+/// 第三行传了 user 就等于宣称"这个类是人判的",于是**跑过一次消解的实体
400+/// 从此永远不再被消解**。实测:一个没有任何人工 PATCH 记录(`entity.retyped`
401+/// 审计 0 条)的库,跑完消解后每个实体都成了 `type_source = human`,
402+/// 下一次预览返回空列表。
403+///
404+/// 谁点的运行记在 `ontology.types_resolved` 审计里,带批次 id,查得到。
405+pub async fn resolve(state: &AppState, kb_id: Uuid) -> AppResult<ResolutionOutcome> {
392406 let items = preview(state, kb_id).await?;
393407 let items: Vec<_> = items
394408 .into_iter()
@@ -524,7 +538,7 @@
524538 (None, 0)
525539 } else {
526540 let (b, n) =
527- utopia_store::resolution::retype_entities(&state.pool, kb_id, &picks, actor).await?;
541+ utopia_store::resolution::retype_entities(&state.pool, kb_id, &picks, None).await?;
528542 (Some(b), n)
529543 };
530544 Ok(ResolutionOutcome {
modified crates/utopia-store/tests/human_type_decisions.rs +55 −0
@@ -310,3 +310,58 @@
310310 .await?;
311311 run
312312 }
313+
314+/// **引擎改过的实体,下一轮还要捞得回来。**
315+///
316+/// 这一条守的是一个真出过的事故:类型消解落库时把「点运行的那个人」当成了
317+/// `retype_entities` 的 actor,而 0051 之后有 actor 就写 `type_source = 'human'`。
318+/// 于是**跑过一次消解的实体从此永远不再被消解**——一个没有任何人工 PATCH 记录
319+/// 的库,跑完一轮之后预览返回空列表,而且没有任何报错。
320+///
321+/// 「谁点的运行」与「谁判定这个实体是什么」是两件事。前者记在
322+/// `ontology.types_resolved` 审计里,后者才该决定 `type_source`。
323+#[tokio::test]
324+async fn an_engine_retype_does_not_lock_the_entity_out_of_the_next_round() -> anyhow::Result<()> {
325+ let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else {
326+ eprintln!("跳过:未设 UTOPIA_DATABASE_URL");
327+ return Ok(());
328+ };
329+ let pool = PgPool::connect(&url).await?;
330+ let f = seed(&pool).await?;
331+
332+ let run = async {
333+ let e = entity(&pool, &f, "Initech", Some(f.org_type), "extracted").await?;
334+ // 留一个 specific_type:让它在「取材条件」上始终够格,这样落选与否
335+ // 只取决于 type_source——否则改成叶子类之后它本来就该落选,测不出东西
336+ sqlx::query("UPDATE entities SET specific_type = 'startup company' WHERE id = $1")
337+ .bind(e)
338+ .execute(&pool)
339+ .await?;
340+ // 引擎自动裁决:没有人为这一条背书
341+ utopia_store::resolution::retype_entities(&pool, f.kb, &[(e, f.sub_type)], None).await?;
342+
343+ assert_eq!(
344+ source_of(&pool, e).await?,
345+ "inferred",
346+ "引擎裁决不是人的背书"
347+ );
348+
349+ let picked = utopia_store::resolution::entities_for_type_resolution(&pool, f.kb, 100)
350+ .await?
351+ .into_iter()
352+ .map(|c| c.id)
353+ .collect::<std::collections::HashSet<_>>();
354+ assert!(
355+ picked.contains(&e),
356+ "引擎改过一次不该把实体锁死——本体还会长大,它还得能被重判"
357+ );
358+ Ok::<_, anyhow::Error>(())
359+ }
360+ .await;
361+
362+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
363+ .bind(f.kb)
364+ .execute(&pool)
365+ .await?;
366+ run
367+}