Skip to content

World's first open-source enterprise world model.

Type resolution runs itself after extraction, and applies only what stays in the subtree (#297)

WaylandYanged3e33652d74
diff --git a/crates/utopia-core/src/models.rs b/crates/utopia-core/src/models.rs
index 10440ef3..87e20c07 100644
--- a/crates/utopia-core/src/models.rs
+++ b/crates/utopia-core/src/models.rs
@@ -859,6 +859,9 @@ pub struct KnowledgeBase {
/// 而 0001 判据 2 说「本体是引导不是执法」:声明可能是错的,不该在用户
/// 没表态时就按它改图
pub materialize_inferences: bool,
+ /// 抽取结束自动排一轮类型消解(0016 C2)。**只自动落地在原类子树里精化的那一档**,
+ /// 跨轴的改判仍留给人。缺省开:基准上自动那一档的命中 39/41(#297),且每批可撤
+ pub auto_type_resolution: bool,
/// 多久重推一次(分钟)。见 `knowledge_bases.inference_interval_minutes`
pub inference_interval_minutes: i32,
/// 上次推完的时间。**答的是「上次看过没有」,不是「上次改过没有」**
diff --git a/crates/utopia-server/src/api/kbs.rs b/crates/utopia-server/src/api/kbs.rs
index bffba332..500cfb20 100644
--- a/crates/utopia-server/src/api/kbs.rs
+++ b/crates/utopia-server/src/api/kbs.rs
@@ -44,6 +44,9 @@ pub struct UpdateKbReq {
/// 多久重推一次(分钟)。事实持续在变,只靠手点会让派生一直是缺的
#[serde(default)]
pub inference_interval_minutes: Option<i32>,
+ /// 抽取结束自动排一轮类型消解(缺省开)。见 docs/decisions/0016 C2
+ #[serde(default)]
+ pub auto_type_resolution: Option<bool>,
}
/// 用户可见的 KB 列表(restricted 库仅矩阵成员与系统管理员可见)。
@@ -101,6 +104,7 @@ pub async fn create(
None,
None,
None,
+ None,
)
.await?;
}
@@ -180,6 +184,7 @@ pub async fn update(
req.ontology_lang.as_deref(),
req.materialize_inferences,
req.inference_interval_minutes,
+ req.auto_type_resolution,
)
.await?;
// 审计只记不阻断
diff --git a/crates/utopia-server/src/extraction.rs b/crates/utopia-server/src/extraction.rs
index eb6246fc..93dd141e 100644
--- a/crates/utopia-server/src/extraction.rs
+++ b/crates/utopia-server/src/extraction.rs
@@ -1361,6 +1361,16 @@ async fn run(state: &AppState, document_id: Uuid, proposed_by: Option<Uuid>) ->
if needs_adjudication || conflicts_found {
state.emit_review(doc.kb_id);
}
+ // 类型消解排队自动跑(0016 C2):开关开着就排一个库级任务,同库已排着的不重复。
+ // 任务自己只看引擎没看过的实体、只自动落地子树内精化的那一档
+ if kb.auto_type_resolution {
+ utopia_store::jobs::enqueue_unless_queued(
+ &state.pool,
+ "resolve_types",
+ serde_json::json!({ "kb_id": doc.kb_id }),
+ )
+ .await?;
+ }
// 自动扩本体:开关开着、且这一批都抽完了,由最后一篇触发。
// 判据是显式开关而不是"本体有没有被碰过"——后者是从行为推断意图,
// 推错的后果很荒唐(在提案上点一次 Add 就永久关掉建议),而且一旦为假
diff --git a/crates/utopia-server/src/main.rs b/crates/utopia-server/src/main.rs
index fb1ac961..5383077d 100644
--- a/crates/utopia-server/src/main.rs
+++ b/crates/utopia-server/src/main.rs
@@ -399,6 +399,15 @@ async fn dispatch(st: &state::AppState, job: &utopia_store::jobs::Job) -> anyhow
.ok_or_else(|| anyhow::anyhow!("payload 缺少 kb_id"))?;
ontology_index::refresh(st, kb_id).await.map(|_| ())
}
+ "resolve_types" => {
+ let kb_id: Uuid = job
+ .payload
+ .get("kb_id")
+ .and_then(|v| v.as_str())
+ .and_then(|s| s.parse().ok())
+ .ok_or_else(|| anyhow::anyhow!("payload 缺少 kb_id"))?;
+ type_resolution::resolve_types_job(st, kb_id).await
+ }
"adjudicate_entities" => {
let kb_id: Uuid = job
.payload
diff --git a/crates/utopia-server/src/type_resolution.rs b/crates/utopia-server/src/type_resolution.rs
index a53ab3bb..bb2c1770 100644
--- a/crates/utopia-server/src/type_resolution.rs
+++ b/crates/utopia-server/src/type_resolution.rs
@@ -104,6 +104,15 @@ pub struct NeighbourVote {
/// 独立成一步是有意的——在花力气建裁决之前,先回答"检索到底找不找得到"。
/// 找不到的话,裁决做得再好也没有用。
pub async fn preview(state: &AppState, kb_id: Uuid) -> AppResult<Vec<TypeSuggestion>> {
+ preview_with(state, kb_id, false).await
+}
+
+/// `unattended` = 自动跑:只看引擎没看过的实体(见 `entities_for_type_resolution`)
+async fn preview_with(
+ state: &AppState,
+ kb_id: Uuid,
+ unattended: bool,
+) -> AppResult<Vec<TypeSuggestion>> {
// 只补类那一半:这一步用不到关系,而一份大本体的关系有一千多条
let _ = ontology_index::refresh_scoped(
state,
@@ -111,8 +120,13 @@ pub async fn preview(state: &AppState, kb_id: Uuid) -> AppResult<Vec<TypeSuggest
Some(utopia_store::ontology::TypeKind::Entity),
)
.await;
- let subjects =
- utopia_store::resolution::entities_for_type_resolution(&state.pool, kb_id, BATCH).await?;
+ let subjects = utopia_store::resolution::entities_for_type_resolution(
+ &state.pool,
+ kb_id,
+ BATCH,
+ unattended,
+ )
+ .await?;
if subjects.is_empty() {
return Ok(Vec::new());
}
@@ -416,7 +430,67 @@ pub struct ReviewItem {
///
/// 谁点的运行记在 `ontology.types_resolved` 审计里,带批次 id,查得到。
pub async fn resolve(state: &AppState, kb_id: Uuid) -> AppResult<ResolutionOutcome> {
- let items = preview(state, kb_id).await?;
+ resolve_with(state, kb_id, false).await
+}
+
+/// 抽取结束后的自动一轮(0016 C2)。
+///
+/// 与人点的那一轮是同一套判定——在原类子树里精化的自动改,跨轴的留给人——差别只有
+/// 两处:只看引擎没看过的实体,且看过的打上 `type_resolved_at`,下一轮不回头。
+/// 待人工的那一档这里**不落库**:它们留在候选里,人在 Ontology 页点一次运行就会再
+/// 见到(那条路不看标记)。开关关着的库什么都不做。
+///
+/// 一次任务最多跑 `MAX_ROUNDS` 轮,每轮一次模型调用、六十个实体;更多的等下一篇
+/// 文档抽完再排——任务是按库去重入队的,不会堆积
+pub async fn resolve_types_job(state: &AppState, kb_id: Uuid) -> anyhow::Result<()> {
+ const MAX_ROUNDS: usize = 10;
+ let kb = utopia_store::kbs::get(&state.pool, kb_id).await?;
+ if !kb.auto_type_resolution {
+ return Ok(());
+ }
+ let (mut retyped, mut for_review, mut left_alone, mut rounds) = (0u32, 0usize, 0usize, 0usize);
+ for _ in 0..MAX_ROUNDS {
+ let outcome = resolve_with(state, kb_id, true).await?;
+ let seen = outcome.retyped as usize + outcome.for_review.len() + outcome.left_alone.len();
+ if seen == 0 {
+ break;
+ }
+ rounds += 1;
+ retyped += outcome.retyped;
+ for_review += outcome.for_review.len();
+ left_alone += outcome.left_alone.len();
+ }
+ if rounds > 0 {
+ let _ = utopia_store::audit::record_opt(
+ &state.pool,
+ Some(kb_id),
+ None,
+ "ontology.types_resolved",
+ "knowledge_base",
+ Some(kb_id),
+ serde_json::json!({
+ "via": "job", "rounds": rounds, "retyped": retyped,
+ "for_review": for_review, "left_alone": left_alone,
+ }),
+ )
+ .await;
+ tracing::info!(%kb_id, rounds, retyped, for_review, left_alone, "类型消解自动跑完成");
+ }
+ Ok(())
+}
+
+async fn resolve_with(
+ state: &AppState,
+ kb_id: Uuid,
+ unattended: bool,
+) -> AppResult<ResolutionOutcome> {
+ let items = preview_with(state, kb_id, unattended).await?;
+ // 自动跑:这一批不管裁成哪一档都算看过(连检索都没给候选的也算——
+ // 它们下一轮照样没有),否则同一批实体每轮都回来
+ if unattended && !items.is_empty() {
+ let ids: Vec<Uuid> = items.iter().map(|i| i.entity_id).collect();
+ utopia_store::resolution::mark_type_judged(&state.pool, kb_id, &ids).await?;
+ }
let items: Vec<_> = items
.into_iter()
.filter(|i| !i.candidates.is_empty() || !i.neighbours.is_empty())
diff --git a/crates/utopia-store/src/jobs.rs b/crates/utopia-store/src/jobs.rs
index c86a7cbd..400bfa1a 100644
--- a/crates/utopia-store/src/jobs.rs
+++ b/crates/utopia-store/src/jobs.rs
@@ -21,6 +21,27 @@ pub struct Job {
pub max_attempts: i32,
}
+/// 同种任务、同样载荷已经排着就不再排:一批文档各自抽完都想触发同一个库级任务
+/// (类型消解、对账),排十次跑十次是浪费,而且后九次看到的是第一次跑完的库。
+/// 返回 None = 已有一条在排队
+pub async fn enqueue_unless_queued(
+ pool: &PgPool,
+ kind: &str,
+ payload: serde_json::Value,
+) -> AppResult<Option<i64>> {
+ let row: Option<(i64,)> = sqlx::query_as(
+ "INSERT INTO jobs (kind, payload)
+ SELECT $1, $2
+ WHERE NOT EXISTS (SELECT 1 FROM jobs WHERE kind = $1 AND payload = $2 AND status = 'queued')
+ RETURNING id",
+ )
+ .bind(kind)
+ .bind(payload)
+ .fetch_optional(pool)
+ .await?;
+ Ok(row.map(|(id,)| id))
+}
+
pub async fn enqueue(pool: &PgPool, kind: &str, payload: serde_json::Value) -> AppResult<i64> {
let (id,): (i64,) =
sqlx::query_as("INSERT INTO jobs (kind, payload) VALUES ($1, $2) RETURNING id")
diff --git a/crates/utopia-store/src/kbs.rs b/crates/utopia-store/src/kbs.rs
index ee5dc7eb..fe8939af 100644
--- a/crates/utopia-store/src/kbs.rs
+++ b/crates/utopia-store/src/kbs.rs
@@ -84,6 +84,7 @@ pub async fn update(
ontology_lang: Option<&str>,
materialize_inferences: Option<bool>,
inference_interval_minutes: Option<i32>,
+ auto_type_resolution: Option<bool>,
) -> AppResult<KnowledgeBase> {
// 改语言不回头重写已有的类——它们已经是这个库的数据,可能有人手工调过。
// 这一列往后管的是**新**描述(自动扩本体、AI 建议)写成什么语言
@@ -118,6 +119,7 @@ pub async fn update(
ontology_lang = COALESCE($6, ontology_lang),
materialize_inferences = COALESCE($7, materialize_inferences),
inference_interval_minutes = COALESCE($8, inference_interval_minutes),
+ auto_type_resolution = COALESCE($9, auto_type_resolution),
updated_at = now()
WHERE id = $1 RETURNING *",
)
@@ -129,6 +131,7 @@ pub async fn update(
.bind(ontology_lang)
.bind(materialize_inferences)
.bind(inference_interval_minutes)
+ .bind(auto_type_resolution)
.fetch_optional(pool)
.await?
.ok_or(AppError::NotFound)
diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rs
index 9d886a66..6d38a782 100644
--- a/crates/utopia-store/src/resolution.rs
+++ b/crates/utopia-store/src/resolution.rs
@@ -1972,10 +1972,15 @@ pub struct TypeCandidateSubject {
/// 值得送去精化类型的实体:**还没有类的**,或者模型报过一个词表外类型的。
///
/// 类型化得已经很具体的实体不动——重判一次只有下降风险,没有上升空间。
+///
+/// `unattended` = 自动跑(0016 C2):**跳过引擎已经看过的**(`type_resolved_at`)。候选按
+/// 事实数排序、一轮六十个,不跳过的话每一轮都是同一批,后面的永远轮不到。人点的那条
+/// 路传 false——人要的是把现状重新审一遍
pub async fn entities_for_type_resolution(
pool: &PgPool,
kb_id: Uuid,
limit: i64,
+ unattended: bool,
) -> AppResult<Vec<TypeCandidateSubject>> {
Ok(sqlx::query_as(
"SELECT e.id, e.canonical_name, e.aliases, t.key AS coarse_key, t.id AS coarse_id,
@@ -2029,15 +2034,30 @@ pub async fn entities_for_type_resolution(
-- 唯一的用武之地。只看前两种就把它整个漏掉了
AND (e.type_id IS NULL OR e.proposed_type IS NOT NULL OR e.specific_type IS NOT NULL
OR EXISTS (SELECT 1 FROM entity_type_parents p WHERE p.parent_id = t.id))
+ AND (NOT $3::bool OR e.type_resolved_at IS NULL)
ORDER BY fact_count DESC, e.created_at
LIMIT $2",
)
.bind(kb_id)
.bind(limit)
+ .bind(unattended)
.fetch_all(pool)
.await?)
}
+/// 引擎看过了:三档(改了 / 留给人 / 不动)都算看过,自动跑不再回头。
+/// 人手工改类、合并之类的写路径不清这个标记——它们本来也不在自动跑的候选里
+pub async fn mark_type_judged(pool: &PgPool, kb_id: Uuid, ids: &[Uuid]) -> AppResult<u64> {
+ Ok(sqlx::query(
+ "UPDATE entities SET type_resolved_at = now() WHERE kb_id = $1 AND id = ANY($2)",
+ )
+ .bind(kb_id)
+ .bind(ids)
+ .execute(pool)
+ .await?
+ .rows_affected())
+}
+
/// 某个类的全部后代(含自身)。精化只能往粗类的后代走。
///
/// **递归而不是一层**:本体是 DAG,`software_application ⊂ creative_work ⊂ thing`,
diff --git a/crates/utopia-store/tests/a_judged_entity_waits_its_turn.rs b/crates/utopia-store/tests/a_judged_entity_waits_its_turn.rs
new file mode 100644
index 00000000..e51c19a2
--- /dev/null
+++ b/crates/utopia-store/tests/a_judged_entity_waits_its_turn.rs
@@ -0,0 +1,156 @@
+//! 0016 C2:类型消解自动跑,看过的不再回头,同库的任务不重复排。
+//!
+//! 候选查询按事实数排序、一轮六十个;自动跑若不跳过看过的,每一轮都是同一批,
+//! 后面的永远轮不到。这里守三件事:
+//!
+//! 1. **自动跑跳过看过的,人点的不跳。** 打了 `type_resolved_at` 的实体在
+//! `unattended = true` 时不出现,`false` 时照常出现。
+//! 2. **人拍过板的两条路都不碰。** `type_source = 'human'` 的实体谁也不重判。
+//! 3. **同种任务同载荷排着就不再排。** `enqueue_unless_queued` 第二次返回 None;
+//! 那条跑完(不再 queued)之后才能再排。
+//!
+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
+
+use sqlx::PgPool;
+use utopia_store::{jobs, resolution};
+use uuid::Uuid;
+
+struct Fx {
+ org: Uuid,
+ kb: Uuid,
+ thing: Uuid,
+}
+
+async fn seed(pool: &PgPool) -> anyhow::Result<Fx> {
+ let (org, ws, kb, thing, sub) = (
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ );
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'judged-test')")
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'judged-test')")
+ .bind(ws)
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'judged-test')",
+ )
+ .bind(kb)
+ .bind(ws)
+ .execute(pool)
+ .await?;
+ // 一个有子类的类:现类还有子类,是候选查询的第三种条件
+ sqlx::query(
+ "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'thing', 'Thing')",
+ )
+ .bind(thing)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'gadget', 'Gadget')",
+ )
+ .bind(sub)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ sqlx::query("INSERT INTO entity_type_parents (child_id, parent_id) VALUES ($1, $2)")
+ .bind(sub)
+ .bind(thing)
+ .execute(pool)
+ .await?;
+ Ok(Fx { org, kb, thing })
+}
+
+async fn entity(pool: &PgPool, f: &Fx, name: &str, source: &str) -> anyhow::Result<Uuid> {
+ let id = Uuid::now_v7();
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name, type_source)
+ VALUES ($1, $2, $3, $4, $5)",
+ )
+ .bind(id)
+ .bind(f.kb)
+ .bind(f.thing)
+ .bind(name)
+ .bind(source)
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+async fn candidates(pool: &PgPool, f: &Fx, unattended: bool) -> anyhow::Result<Vec<Uuid>> {
+ Ok(
+ resolution::entities_for_type_resolution(pool, f.kb, 50, unattended)
+ .await?
+ .into_iter()
+ .map(|s| s.id)
+ .collect(),
+ )
+}
+
+#[tokio::test]
+async fn a_judged_entity_waits_its_turn() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let f = seed(&pool).await?;
+ let mut job_ids: Vec<i64> = Vec::new();
+
+ let run = async {
+ let fresh = entity(&pool, &f, "Fresh", "inferred").await?;
+ let seen = entity(&pool, &f, "Seen", "inferred").await?;
+ let human = entity(&pool, &f, "Decided", "human").await?;
+ resolution::mark_type_judged(&pool, f.kb, &[seen]).await?;
+
+ // 1. 自动跑跳过看过的;人点的不跳
+ let auto = candidates(&pool, &f, true).await?;
+ assert!(auto.contains(&fresh));
+ assert!(
+ !auto.contains(&seen),
+ "the engine does not look twice on its own"
+ );
+ let manual = candidates(&pool, &f, false).await?;
+ assert!(manual.contains(&fresh) && manual.contains(&seen));
+
+ // 2. 人拍过板的两条路都不碰
+ assert!(!auto.contains(&human) && !manual.contains(&human));
+
+ // 3. 同库的任务不重复排
+ let payload = serde_json::json!({ "kb_id": f.kb });
+ let first = jobs::enqueue_unless_queued(&pool, "resolve_types", payload.clone()).await?;
+ assert!(first.is_some());
+ job_ids.extend(first);
+ let second = jobs::enqueue_unless_queued(&pool, "resolve_types", payload.clone()).await?;
+ assert!(second.is_none(), "one queued job per base is enough");
+ sqlx::query("UPDATE jobs SET status = 'done' WHERE id = $1")
+ .bind(first.unwrap())
+ .execute(&pool)
+ .await?;
+ let third = jobs::enqueue_unless_queued(&pool, "resolve_types", payload).await?;
+ assert!(third.is_some(), "once it ran, the next one may queue");
+ job_ids.extend(third);
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+
+ let _ = sqlx::query("DELETE FROM jobs WHERE id = ANY($1)")
+ .bind(&job_ids)
+ .execute(&pool)
+ .await;
+ let _ = sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(&pool)
+ .await;
+ let _ = sqlx::query("DELETE FROM organizations WHERE id = $1")
+ .bind(f.org)
+ .execute(&pool)
+ .await;
+ run
+}
diff --git a/crates/utopia-store/tests/human_type_decisions.rs b/crates/utopia-store/tests/human_type_decisions.rs
index 081fd85a..be1392de 100644
--- a/crates/utopia-store/tests/human_type_decisions.rs
+++ b/crates/utopia-store/tests/human_type_decisions.rs
@@ -109,11 +109,12 @@ async fn type_resolution_leaves_human_decisions_alone() -> anyhow::Result<()> {
let by_human = entity(&pool, &f, "Acme", Some(f.org_type), "human").await?;
let by_engine = entity(&pool, &f, "Globex", Some(f.org_type), "extracted").await?;
- let picked = utopia_store::resolution::entities_for_type_resolution(&pool, f.kb, 100)
- .await?
- .into_iter()
- .map(|c| c.id)
- .collect::<std::collections::HashSet<_>>();
+ let picked =
+ utopia_store::resolution::entities_for_type_resolution(&pool, f.kb, 100, false)
+ .await?
+ .into_iter()
+ .map(|c| c.id)
+ .collect::<std::collections::HashSet<_>>();
assert!(
picked.contains(&by_engine),
@@ -341,11 +342,12 @@ async fn an_engine_retype_does_not_lock_the_entity_out_of_the_next_round() -> an
"引擎裁决不是人的背书"
);
- let picked = utopia_store::resolution::entities_for_type_resolution(&pool, f.kb, 100)
- .await?
- .into_iter()
- .map(|c| c.id)
- .collect::<std::collections::HashSet<_>>();
+ let picked =
+ utopia_store::resolution::entities_for_type_resolution(&pool, f.kb, 100, false)
+ .await?
+ .into_iter()
+ .map(|c| c.id)
+ .collect::<std::collections::HashSet<_>>();
assert!(
picked.contains(&e),
"引擎改过一次不该把实体锁死——本体还会长大,它还得能被重判"
diff --git a/docs/decisions/0016-close-the-open-seams-before-cutting-new-ones.md b/docs/decisions/0016-close-the-open-seams-before-cutting-new-ones.md
index 6be478df..089bb58a 100644
--- a/docs/decisions/0016-close-the-open-seams-before-cutting-new-ones.md
+++ b/docs/decisions/0016-close-the-open-seams-before-cutting-new-ones.md
@@ -1,6 +1,6 @@
# 0016 · Close the open seams before cutting new ones
-- **Status**: in progress · the first schedule after v0.1.0 · A1, A2, A3 done · B1 done (#227) · B2 done (#238 / #243, [0017](0017-a-contradiction-points-upstream.md)) · B3 done: the signature half (#190 / #196), cross-pack signatures and range-aware direction (#233), `disjointWith` into resolution · D2's blank-base problem worked around with builtin `metric` / `dimension` classes (#231), the pack itself still planned · the lakehouse engines landed ahead of D4 (#239, [0018](0018-the-lakehouse-is-one-protocol-away.md)) · C1 done: a Wikidata answer key for the Wikipedia corpora (#289)
+- **Status**: in progress · the first schedule after v0.1.0 · A1, A2, A3 done · B1 done (#227) · B2 done (#238 / #243, [0017](0017-a-contradiction-points-upstream.md)) · B3 done: the signature half (#190 / #196), cross-pack signatures and range-aware direction (#233), `disjointWith` into resolution · D2's blank-base problem worked around with builtin `metric` / `dimension` classes (#231), the pack itself still planned · the lakehouse engines landed ahead of D4 (#239, [0018](0018-the-lakehouse-is-one-protocol-away.md)) · C1 done: a Wikidata answer key for the Wikipedia corpora (#289) · C2 done: type resolution queued after extraction, only the in-subtree tier applied on its own, on by default after that tier scored 39/41 against the Wikidata key (#297)
- **Written**: 2026-09-02 · condensed into English 2026-09-03
- **Related**: written after checking [0001](0001-ontology-import-and-governance.md) through [0015](0015-recording-a-sentence-is-not-asserting-a-fact.md) against the code; every item below has its source in those fifteen records, whose 2026-09-02 revision notes are the check's product. This record only orders them and says why this order.
diff --git a/docs/decisions/README.md b/docs/decisions/README.md
index b5988691..c7805d8b 100644
--- a/docs/decisions/README.md
+++ b/docs/decisions/README.md
@@ -39,7 +39,7 @@ The test for writing one: if someone (including us) looks at a piece of code in
| 0013 | [A source hands over its history](0013-a-source-should-hand-over-its-history.md) | Implemented for GitHub, Jira and Notion (#134 / #135 / #213) · Feishu and Confluence not started · `instant` precision not triggered |
| 0014 | [Identity from the person, scope from the token](0014-identity-from-the-person-scope-from-the-token.md) | Implemented (#180) · five read-only MCP tools over Streamable HTTP · tokens page at `/account/tokens` · `can_write` still hard-coded false |
| 0015 | [A recorded sentence waits for a nod](0015-recording-a-sentence-is-not-asserting-a-fact.md) | Implemented · memory facts wait in `pending_facts`, nod queue and chat card, `remember` reopened · MCP write is the next cut |
-| 0016 | [Close the open seams before cutting new ones](0016-close-the-open-seams-before-cutting-new-ones.md) | In progress · A done · B done (B4 deferred) · C1 done (#289), C2–C5 open · D2 worked around (#231); the lakehouse landed ahead of D4 (#239) |
+| 0016 | [Close the open seams before cutting new ones](0016-close-the-open-seams-before-cutting-new-ones.md) | In progress · A done · B done (B4 deferred) · C1 done (#289) · C2 done (#297) · C3–C5 open · D2 worked around (#231); the lakehouse landed ahead of D4 (#239) |
| 0017 | [A contradiction points at an error upstream](0017-a-contradiction-points-upstream.md) | Implemented · B2a: engine and queue, per-item cap, aggregation by rule pair, cards with clues and repairs (#238) · B2b: contested edges in the alert colour, ghost edges for blocked derivations, the disputed chip and the "did not land" section in the panel (#243) |
| 0018 | [The lakehouse is one protocol away](0018-the-lakehouse-is-one-protocol-away.md) | Implemented: Trino (Iceberg / Delta / Hive), Databricks and Snowflake behind the same trait, scheme picks the engine (#239) · replay tests only, real clusters wanted (#240–#242) · MaxCompute waits |
| 0019 | [The second clock can be rewound](0019-the-second-clock-can-be-rewound.md) | Planned · the recording axis is stored but nothing reads it as of a moment; one `held_at(T)` predicate in place of the hard-coded `invalidated_at IS NULL`, `at` and `as_of` separate to the API · read-only, no migration |
diff --git a/migrations/0024_types_resolve_themselves.sql b/migrations/0024_types_resolve_themselves.sql
new file mode 100644
index 00000000..91cfd97a
--- /dev/null
+++ b/migrations/0024_types_resolve_themselves.sql
@@ -0,0 +1,13 @@
+-- 类型消解排队自动跑(0016 的 C2,0001 P3a 的收尾)。
+--
+-- 此前类型消解只在有人点的时候跑:新文档抽完,实体带着粗类型躺在图里,直到谁想起来
+-- 去 Ontology 页点一下。现在抽取结束自动排一个 resolve_types 任务,只自动落地最安全的
+-- 一档(在原类的子树里精化);跨轴的改判仍留给人。
+--
+-- 引擎看过的实体打上 type_resolved_at:自动跑只挑没看过的,否则候选查询按事实数排序,
+-- 每一轮都是同一批六十个,后面的永远轮不到。人点的那条路不看这个标记——人要的是重新
+-- 审一遍。
+ALTER TABLE entities ADD COLUMN type_resolved_at TIMESTAMPTZ;
+-- 缺省开:在 ai-timeline × schema.org 上量过,自动落地那一档对 Wikidata 答案卷的命中
+-- 39/41(两个"错"里一个是答案卷写窄了);它只往子树里走一格,每一批都可撤
+ALTER TABLE knowledge_bases ADD COLUMN auto_type_resolution BOOLEAN NOT NULL DEFAULT TRUE;
diff --git a/scripts/bench/README.md b/scripts/bench/README.md
index 43c687c7..23ceaeec 100644
--- a/scripts/bench/README.md
+++ b/scripts/bench/README.md
@@ -50,6 +50,11 @@ node scripts/bench/run.mjs --corpus pharma --ontology /tmp/schemaorg.ttl --label
一路签名;这里要量的是"本体规模",比例稳定就够用。
- `for_review` 按**没改**算进 miss。它确实还没改——算成命中就是把人的活记在机器账上。
- `absent` = 标准答案里有、但抽取压根没抽出这个实体。它不是消解的错,单独一栏。
+- **命中 = 可接受类或它的子类**(按库里的类层级算)。答案卷给的是锚(organization、
+ place),引擎答的常常更具体(research_organization、city)——精化正是要它做的事。
+- 手填的答案卷按子串匹配名字,生成的(`match: "exact"`)按全名精确匹配。
+- `score.tiers`:分档打分。`auto` 是自动落地那一档对答案卷的命中,`review` 是待人工那一档
+ 若盲目照收会怎样——放不放开自动跑看的是前者。
## 标准答案会写错
diff --git a/scripts/bench/make-truth.mjs b/scripts/bench/make-truth.mjs
index 86ef5760..838acdc6 100644
--- a/scripts/bench/make-truth.mjs
+++ b/scripts/bench/make-truth.mjs
@@ -83,6 +83,9 @@ const out = {
source: raw.source,
generated_at: new Date().toISOString(),
vocabulary: mapping.vocabulary,
+ // 生成的答案卷按**全名精确**匹配。手填的卷用子串(名字每次略有出入),而这里的
+ // key 是维基百科条目名:子串会把 "OpenAI" 的答案套到 "OpenAI Foundation" 头上
+ match: "exact",
expect: Object.fromEntries(Object.keys(expect).sort((a, b) => a.localeCompare(b)).map((k) => [k, expect[k]])),
};
fs.writeFileSync(outPath, JSON.stringify(out, null, 2) + "\n");
diff --git a/scripts/bench/run.mjs b/scripts/bench/run.mjs
index f973e4f4..927b37a0 100644
--- a/scripts/bench/run.mjs
+++ b/scripts/bench/run.mjs
@@ -114,9 +114,13 @@ async function main() {
const truthPath = args.truth
? path.resolve(args.truth)
: path.join(HERE, "truth", corpusName + ".json");
- const truth = fs.existsSync(truthPath)
- ? JSON.parse(fs.readFileSync(truthPath, "utf8")).expect
- : null;
+ const truthFile = fs.existsSync(truthPath) ? JSON.parse(fs.readFileSync(truthPath, "utf8")) : null;
+ const truth = truthFile?.expect ?? null;
+ // 匹配方式跟着答案卷走:手填的按子串(名字每次略有出入),生成的按全名精确
+ //("OpenAI" 的答案不该套到 "OpenAI Foundation" 头上)
+ const exactNames = truthFile?.match === "exact";
+ const nameMatches = (name, frag) =>
+ exactNames ? name.trim().toLowerCase() === frag.toLowerCase() : name.includes(frag);
try {
await api("POST", "/api/v1/auth/register", {
@@ -281,6 +285,72 @@ async function main() {
const outcome = await api("POST", "/api/v1/kbs/" + kb + "/ontology/type-resolution");
const resolveMs = Date.now() - t2;
+ // **分档打分**(0016 的 C2 之前先量):自动改的那一档准不准,待人工那一档若照单
+ // 全收会怎样。总分把待人工按「没改」算,看不出自动那一档单独的水平;而放不放开
+ // 自动跑,看的正是这一档
+ // **命中 = 可接受类或它的子类。** 答案卷给的是锚(organization、place),引擎答的
+ // 常常更具体(research_organization、city)——精化正是要它做的事,不是错
+ const ancestors = new Map();
+ for (const l of psql(
+ "WITH RECURSIVE up(child, parent) AS (" +
+ " SELECT p.child_id, p.parent_id FROM entity_type_parents p JOIN entity_types t ON t.id = p.child_id" +
+ " WHERE t.kb_id = '" +
+ kb +
+ "' UNION SELECT up.child, p.parent_id FROM up JOIN entity_type_parents p ON p.child_id = up.parent)" +
+ " SELECT c.key || '|' || pk.key FROM up JOIN entity_types c ON c.id = up.child" +
+ " JOIN entity_types pk ON pk.id = up.parent",
+ )
+ .split(String.fromCharCode(10))
+ .filter((l) => l.includes("|"))) {
+ const i = l.lastIndexOf("|");
+ const c = l.slice(0, i);
+ if (!ancestors.has(c)) ancestors.set(c, new Set());
+ ancestors.get(c).add(l.slice(i + 1));
+ }
+ const accepts = (accept, key) =>
+ accept.includes(key) || [...(ancestors.get(key) ?? [])].some((a) => accept.includes(a));
+ const inTruth = (name) => Object.entries(truth ?? {}).find(([frag]) => nameMatches(name, frag));
+ const judge = (name, key) => {
+ const t = inTruth(name);
+ if (!t) return "unknown";
+ if (t[1].length === 0) return "should_leave";
+ return accepts(t[1], key) ? "hit" : "miss";
+ };
+ const tally = (pairs) => {
+ const c = { hit: 0, miss: 0, should_leave: 0, unknown: 0, examples: [] };
+ for (const [name, key] of pairs) {
+ const j = judge(name, key);
+ c[j] += 1;
+ if (j === "miss" && c.examples.length < 12) {
+ c.examples.push(name + " → " + key + ",期望 " + inTruth(name)[1].join("|"));
+ }
+ }
+ return c;
+ };
+ const split = (out) =>
+ out
+ .split("\n")
+ .filter(Boolean)
+ .map((l) => {
+ const i = l.lastIndexOf("|");
+ return [l.slice(0, i), l.slice(i + 1)];
+ });
+ const autoPairs = outcome.batch
+ ? split(
+ psql(
+ "SELECT e.canonical_name || '|' || t.key FROM entity_retypes r" +
+ " JOIN entities e ON e.id = r.entity_id JOIN entity_types t ON t.id = r.to_type_id" +
+ " WHERE r.batch_id = '" +
+ outcome.batch +
+ "'",
+ ),
+ )
+ : [];
+ const reviewPairs = (outcome.for_review ?? []).map((r) => [r.name, r.choice]);
+ const tiers = truth
+ ? { auto: tally(autoPairs), review: tally(reviewPairs), left_alone: outcome.left_alone.length }
+ : null;
+
// 打分。**待人工的按"没改"算**——它确实还没改,算成命中就是把人的活记在机器账上。
//
// **LEFT JOIN,且没有类时写 `-`**(0009)。内连接会让未分类实体整个不出现,
@@ -309,7 +379,7 @@ async function main() {
for (const [frag, accept] of Object.entries(truth ?? {})) {
// 按片段匹配而不是全等:抽取给的名字每次略有出入
//("星云科技" / "星云科技(上海)有限公司"),全等会把这种变化算成失败
- const found = rows.filter(([name]) => name.includes(frag));
+ const found = rows.filter(([name]) => nameMatches(name, frag));
if (found.length === 0) {
absent += 1;
continue;
@@ -321,7 +391,7 @@ async function main() {
wronglyChanged += 1;
notes.push(frag + ":本不该改,却成了 " + keys.join("/"));
} else correctlyLeft += 1;
- } else if (keys.some((k) => accept.includes(k))) {
+ } else if (keys.some((k) => accepts(accept, k))) {
hit += 1;
} else {
miss += 1;
@@ -358,12 +428,13 @@ async function main() {
),
},
resolution: {
+ batch: outcome.batch,
retyped: outcome.retyped,
for_review: outcome.for_review.length,
left_alone: outcome.left_alone.length,
},
score: truth
- ? { hit, miss, correctlyLeft, wronglyChanged, absent, notes }
+ ? { hit, miss, correctlyLeft, wronglyChanged, absent, notes, tiers }
: "无答案键,不打分",
ms: { extract: extractMs, import: importMs, resolve: resolveMs },
},
diff --git a/scripts/bench/truth/ai-timeline-ends.wikidata.json b/scripts/bench/truth/ai-timeline-ends.wikidata.json
index 8c0251fa..610f6257 100644
--- a/scripts/bench/truth/ai-timeline-ends.wikidata.json
+++ b/scripts/bench/truth/ai-timeline-ends.wikidata.json
@@ -1,8 +1,9 @@
{
"note": "Generated by make-truth.mjs from ai-timeline-ends.wikidata.raw.json and p31-to-classes.json; do not edit by hand — fix the mapping or refetch.",
"source": "Wikipedia API (pageprops.wikibase_item, links) + Wikidata SPARQL (wdt:P31, wdt:P279, rdfs:label)",
- "generated_at": "2026-09-04T01:57:44.144Z",
+ "generated_at": "2026-09-04T05:21:44.115Z",
"vocabulary": "schema-org",
+ "match": "exact",
"expect": {
"Accenture": [
"corporation",
diff --git a/scripts/bench/truth/ai-timeline.wikidata.json b/scripts/bench/truth/ai-timeline.wikidata.json
index 97f7cce7..8d1cae6d 100644
--- a/scripts/bench/truth/ai-timeline.wikidata.json
+++ b/scripts/bench/truth/ai-timeline.wikidata.json
@@ -1,8 +1,9 @@
{
"note": "Generated by make-truth.mjs from ai-timeline.wikidata.raw.json and p31-to-classes.json; do not edit by hand — fix the mapping or refetch.",
"source": "Wikipedia API (pageprops.wikibase_item, links) + Wikidata SPARQL (wdt:P31, wdt:P279, rdfs:label)",
- "generated_at": "2026-09-04T01:57:44.030Z",
+ "generated_at": "2026-09-04T05:21:43.990Z",
"vocabulary": "schema-org",
+ "match": "exact",
"expect": {
"Aaron Chatterji": [
"person"
diff --git a/web/src/api.ts b/web/src/api.ts
index 7a6837dc..48e1b81b 100644
--- a/web/src/api.ts
+++ b/web/src/api.ts
@@ -105,6 +105,8 @@ export interface Kb {
/** 把推出来的事实写进账本(R1)。**缺省关**——推理往图里加东西,
* 而声明可能是错的,不该在用户没表态时就按它改图 */
materialize_inferences: boolean;
+ /** 抽取结束自动排一轮类型消解(只自动落地子树内精化的那一档) */
+ auto_type_resolution: boolean;
/** 多久重推一次(分钟)。事实持续在变,只靠手点会让派生一直是缺的 */
inference_interval_minutes: number;
/** 上次推完的时间 */
diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts
index d71f99a8..38dfe76d 100644
--- a/web/src/i18n/en.ts
+++ b/web/src/i18n/en.ts
@@ -1502,6 +1502,9 @@ export const en = {
materialize: "Materialize inferences",
materializeNote:
"Write facts the ontology entails into the ledger — transitive chains and symmetric pairs. Off by default: a declaration can be wrong, and this one changes the graph. Derived facts are marked and can be taken back.",
+ autoResolveTypes: "Resolve entity types after extraction",
+ autoResolveTypesNote:
+ "After each document is extracted, run a round of type resolution on entities the engine has not looked at yet. Only refinements within the current class are applied on their own — a re-classification across the tree still waits for you on the Ontology page. Every batch is listed there and can be undone.",
inferEvery: "Re-derive every",
minutes: "minutes",
lastInference: (when: string) => `last run ${when}`,
diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.ts
index e28e8567..b75fed2c 100644
--- a/web/src/i18n/zh.ts
+++ b/web/src/i18n/zh.ts
@@ -1342,6 +1342,9 @@ export const zh: Strings = {
materialize: "物化推理",
materializeNote:
"把本体蕴含的事实写进账本——传递链与对称对。默认关:声明可能是错的,而这一步会改图。派生事实带标记,也随时可以撤回。",
+ autoResolveTypes: "抽取后自动消解实体类型",
+ autoResolveTypesNote:
+ "每篇文档抽完,对引擎还没看过的实体跑一轮类型消解。只有在现类子树里精化的才自动落地,跨轴的改判仍留在本体页等你。每一批都列在本体页,随时可以撤回。",
inferEvery: "每隔",
minutes: "分钟重推",
lastInference: (when: string) => `上次 ${when}`,
diff --git a/web/src/pages/KbSettings.tsx b/web/src/pages/KbSettings.tsx
index cf663168..bf41dc56 100644
--- a/web/src/pages/KbSettings.tsx
+++ b/web/src/pages/KbSettings.tsx
@@ -78,6 +78,8 @@ export function KbSettings() {
const [autoExtend, setAutoExtend] = useState(true);
// **默认关**,与上面那个相反:推理往账本里写事实,而声明可能是错的
const [materialize, setMaterialize] = useState(false);
+ // 类型消解自动跑(0016 C2):只自动落地子树内精化的那一档
+ const [autoResolve, setAutoResolve] = useState(false);
const [inferMins, setInferMins] = useState(60);
const [ontoLang, setOntoLang] = useState<"en" | "zh">("en");
const [confirmingDelete, setConfirmingDelete] = useState(false);
@@ -90,6 +92,7 @@ export function KbSettings() {
setVisibility(kb.data.visibility);
setAutoExtend(kb.data.auto_extend_ontology);
setMaterialize(kb.data.materialize_inferences);
+ setAutoResolve(kb.data.auto_type_resolution);
setInferMins(kb.data.inference_interval_minutes);
setOntoLang(kb.data.ontology_lang);
}
@@ -108,6 +111,7 @@ export function KbSettings() {
visibility,
auto_extend_ontology: autoExtend,
materialize_inferences: materialize,
+ auto_type_resolution: autoResolve,
inference_interval_minutes: inferMins,
ontology_lang: ontoLang,
}),
@@ -282,6 +286,23 @@ export function KbSettings() {
</span>
</span>
</label>
+ {/* 类型消解自动跑:抽完排一轮,只自动改子树内精化的那一档,跨轴的仍留给人 */}
+ <label className="flex items-start gap-2.5 pt-1 cursor-pointer">
+ <input
+ type="checkbox"
+ className="mt-0.5 accent-[var(--u-accent)]"
+ checked={autoResolve}
+ onChange={(e) => setAutoResolve(e.target.checked)}
+ />
+ <span className="min-w-0">
+ <span className="block text-sm text-neutral-200">
+ {S.kbset.autoResolveTypes}
+ </span>
+ <span className="block text-xs leading-relaxed text-neutral-500">
+ {S.kbset.autoResolveTypesNote}
+ </span>
+ </span>
+ </label>
{/* 重推间隔。**只在开着的时候露出来**——关着时它不影响任何事,
摆在那里只会让人以为设了就会推 */}
{materialize && (