Skip to content

World's first open-source enterprise world model.

Exact-name resolution counts a candidate's facts only when it has to choose between candidates (#1309)

Szabo Bogdan02af3452c480
diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rs
index e9f11d59..08d6badf 100644
--- a/crates/utopia-store/src/resolution.rs
+++ b/crates/utopia-store/src/resolution.rs
@@ -193,7 +193,28 @@ struct Candidate {
canonical_name: String,
profile_embedding: Option<Vector>,
profile_n: i32,
- degree: i64,
+}
+
+/// 这些实体各自的度数(不数名字事实),与 `ids` 同序;没有事实的是 0。
+async fn degrees(pool: &PgPool, kb_id: Uuid, ids: &[Uuid]) -> AppResult<Vec<i64>> {
+ let rows: HashMap<Uuid, i64> = sqlx::query_as(&format!(
+ "SELECT n.id, count(*)
+ FROM unnest($2::uuid[]) AS n(id)
+ JOIN facts f ON f.kb_id = $1 AND (f.subject_id = n.id OR f.object_id = n.id)
+ AND f.invalidated_at IS NULL AND {not_name}
+ GROUP BY n.id",
+ not_name = crate::names::not_a_name("f"),
+ ))
+ .bind(kb_id)
+ .bind(ids)
+ .fetch_all(pool)
+ .await?
+ .into_iter()
+ .collect();
+ Ok(ids
+ .iter()
+ .map(|id| rows.get(id).copied().unwrap_or(0))
+ .collect())
}
/// 消解结果:mention 落到了哪个实体;附带需要入队的疑似重复审核对
@@ -378,11 +399,9 @@ async fn resolve_by_name(
let keys = recall_keys(&name);
// 名字召回读名字事实(0041 决定 1):本名、简称、曾用名都是 `known_as` 上的一条。
// 度数不数名字事实——名字不是一条「关于它的事」,数进去每个实体都凭空多一
+ // 度数要等真有几个候选得比时才数:每个候选一条计数子查询,大库上很贵(#1296)
let candidates: Vec<Candidate> = sqlx::query_as(&format!(
- "SELECT e.id, e.canonical_name, e.profile_embedding, e.profile_n,
- (SELECT count(*) FROM facts f
- WHERE f.kb_id = e.kb_id AND (f.subject_id = e.id OR f.object_id = e.id)
- AND f.invalidated_at IS NULL AND {not_name}) AS degree
+ "SELECT e.id, e.canonical_name, e.profile_embedding, e.profile_n
FROM entities e
-- IS NOT DISTINCT FROM 而不是 =(0009 的那个陷阱):开放图谱里的实体都没有类
-- (类由对齐来定),`type_id = NULL` 永远不成立,同名的它就永远撞不上——
@@ -392,7 +411,6 @@ async fn resolve_by_name(
-- 不是召回的桥——两篇文档里描述得一样的两个东西不能因此接到一起
AND e.description IS NULL
AND (lower(e.canonical_name) = ANY($3) OR {named})",
- not_name = crate::names::not_a_name("f"),
named = crate::names::has_name_in("e", 1, 3),
))
.bind(kb_id)
@@ -412,10 +430,19 @@ async fn resolve_by_name(
let Some(ctx) = context else {
// 无向量可比:v1 兼容 —— 归并到事实最多的同名候选
- let best = candidates
- .iter()
- .max_by_key(|c| c.degree)
- .expect("non-empty");
+ let best = match candidates.as_slice() {
+ [only] => only,
+ _ => {
+ let ids: Vec<Uuid> = candidates.iter().map(|c| c.id).collect();
+ let degree = degrees(pool, kb_id, &ids).await?;
+ candidates
+ .iter()
+ .zip(degree)
+ .max_by_key(|(_, d)| *d)
+ .map(|(c, _)| c)
+ .expect("non-empty")
+ }
+ };
touch_entity(pool, best.id).await?;
return Ok(Resolution {
entity_id: best.id,
@@ -435,7 +462,7 @@ async fn resolve_by_name(
// 有画像的候选算相似度;无画像(历史数据/无 embedding 期创建)单独归类
let mut scored: Vec<(&Candidate, f32)> = Vec::new();
- let mut unprofiled: Option<&Candidate> = None;
+ let mut unprofiled: Vec<&Candidate> = Vec::new();
for c in &candidates {
match c
.profile_embedding
@@ -443,11 +470,7 @@ async fn resolve_by_name(
.and_then(|p| cosine(p.as_slice(), ctx))
{
Some(sim) => scored.push((c, sim)),
- None => {
- if unprofiled.map(|u| c.degree > u.degree).unwrap_or(true) {
- unprofiled = Some(c);
- }
- }
+ None => unprofiled.push(c),
}
}
// 最高分候选:并列时保留先遇到的那个(与旧的 `sim > s` 严格大于一致)
@@ -522,6 +545,19 @@ async fn resolve_by_name(
});
}
}
+ let unprofiled = match unprofiled.as_slice() {
+ [] => None,
+ [only] => Some(*only),
+ many => {
+ let ids: Vec<Uuid> = many.iter().map(|c| c.id).collect();
+ let degree = degrees(pool, kb_id, &ids).await?;
+ many.iter()
+ .copied()
+ .zip(degree)
+ .reduce(|acc, cur| if cur.1 > acc.1 { cur } else { acc })
+ .map(|(c, _)| c)
+ }
+ };
if let Some(c) = unprofiled {
// 无画像候选无从判别:v1 兼容归并,并用本次上下文初始化画像
update_profile(pool, c.id, c.profile_n, ctx).await?;
diff --git a/crates/utopia-store/tests/store/a_single_name_candidate_is_not_counted.rs b/crates/utopia-store/tests/store/a_single_name_candidate_is_not_counted.rs
new file mode 100644
index 00000000..63febee7
--- /dev/null
+++ b/crates/utopia-store/tests/store/a_single_name_candidate_is_not_counted.rs
@@ -0,0 +1,84 @@
+//! 同名候选只有一个时没有可比的,不必数它的事实(#1296)。
+//! 一条连接的池子:sqlx 把它跑过的语句都缓存在 `pg_prepared_statements` 里,没跑过的就不在。
+
+use sqlx::postgres::PgPoolOptions;
+use utopia_store::names;
+use uuid::Uuid;
+
+#[tokio::test]
+async fn a_single_name_candidate_is_attached_without_counting_its_facts() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPoolOptions::new()
+ .max_connections(1)
+ .connect(&url)
+ .await?;
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ let (person, entity) = (Uuid::now_v7(), Uuid::now_v7());
+ let tag = "single-name-candidate";
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, $2)")
+ .bind(org)
+ .bind(tag)
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, $3)")
+ .bind(ws)
+ .bind(org)
+ .bind(tag)
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, $3)")
+ .bind(kb)
+ .bind(ws)
+ .bind(tag)
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, $3, $4)")
+ .bind(person)
+ .bind(kb)
+ .bind("person")
+ .bind("Person")
+ .execute(&pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(entity)
+ .bind(kb)
+ .bind(person)
+ .bind("林小满")
+ .execute(&pool)
+ .await?;
+ names::record(&pool, kb, entity, "林小满", None, None).await?;
+
+ let run = async {
+ let r = utopia_store::resolution::resolve_mention(
+ &pool,
+ kb,
+ Some(person),
+ "林小满",
+ None,
+ None,
+ None,
+ &[],
+ )
+ .await?;
+ assert!(!r.created && r.entity_id == entity, "唯一的同名候选就是它");
+ let counted: Vec<String> = sqlx::query_scalar(
+ "SELECT statement FROM pg_prepared_statements WHERE statement LIKE '%count(*)%facts f%'",
+ )
+ .persistent(false)
+ .fetch_all(&pool)
+ .await?;
+ assert_eq!(counted, Vec::<String>::new(), "只有一个候选,不该数事实");
+ anyhow::Ok(())
+ }
+ .await;
+
+ sqlx::query("DELETE FROM organizations WHERE id = $1")
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ run
+}
diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rs
index 1f61b94b..2915505a 100644
--- a/crates/utopia-store/tests/store/main.rs
+++ b/crates/utopia-store/tests/store/main.rs
@@ -88,6 +88,7 @@ mod a_secret_is_sealed_at_rest;
mod a_signature_holds_on_every_path;
mod a_signature_vector_lasts_while_its_text_does;
mod a_similar_name_is_proposed_not_merged;
+mod a_single_name_candidate_is_not_counted;
mod a_source_kind_is_listed_once;
mod a_source_reaches_only_where_it_was_granted;
mod a_statement_is_attested_by_what_its_document_says;