Skip to content

World's first open-source enterprise world model.

List an entity named exactly what was searched before busier partial matches (#940)

Maya Kid9f2ac7eac4a4
diff --git a/crates/utopia-server/src/api/chat_empty_reply_tests.rs b/crates/utopia-server/src/api/chat_empty_reply_tests.rs
index 31af51ca..b66f51ae 100644
--- a/crates/utopia-server/src/api/chat_empty_reply_tests.rs
+++ b/crates/utopia-server/src/api/chat_empty_reply_tests.rs
@@ -366,6 +366,8 @@ async fn a_reply_that_stays_empty_is_an_error_after_one_retry() -> anyhow::Resul
mod fallback_tests;
#[path = "chat_history_tests.rs"]
mod history_tests;
+#[path = "chat_lookup_tests.rs"]
+mod lookup_tests;
#[path = "chat_persistence_tests.rs"]
mod persistence_tests;
#[path = "chat_registry_tests.rs"]
diff --git a/crates/utopia-server/src/api/chat_lookup_tests.rs b/crates/utopia-server/src/api/chat_lookup_tests.rs
new file mode 100644
index 00000000..589ba150
--- /dev/null
+++ b/crates/utopia-server/src/api/chat_lookup_tests.rs
@@ -0,0 +1,97 @@
+//! A name reaches the entity called exactly that, even behind busier partial matches.
+//!
+//! The graph tools read the first eight entities whose name contains the one asked for.
+//! Those used to be the eight with the most facts, so "Apple" beside nine busier
+//! "Apple Store …" entities never reached the one named Apple: `find_entities` listed the
+//! stores, and `entity_facts("Apple")` read Apple Store 1 and handed its facts over as
+//! Apple's.
+use super::*;
+
+/// The newest tool result the scripted endpoint was shown in one request
+fn tool_result(request: &serde_json::Value) -> String {
+ request["messages"]
+ .as_array()
+ .into_iter()
+ .flatten()
+ .rev()
+ .find(|m| m["role"] == "tool")
+ .and_then(|m| m["content"].as_str())
+ .unwrap_or_default()
+ .to_string()
+}
+
+async fn entity(f: &Fx, ty: Uuid, name: &str) -> anyhow::Result<Uuid> {
+ let id = Uuid::now_v7();
+ sqlx::query("INSERT INTO entities(id,kb_id,type_id,canonical_name) VALUES ($1,$2,$3,$4)")
+ .bind(id)
+ .bind(f.kb)
+ .bind(ty)
+ .bind(name)
+ .execute(&f.pool)
+ .await?;
+ Ok(id)
+}
+
+#[tokio::test]
+async fn a_name_reaches_the_entity_called_exactly_that() -> anyhow::Result<()> {
+ let Some(f) = fixture(Scripted::new(vec![
+ Reply::Tool("find_entities", r#"{"name":"Apple"}"#),
+ Reply::Tool("entity_facts", r#"{"entity_id":"Apple"}"#),
+ Reply::Text("Apple has no recorded facts."),
+ ]))
+ .await?
+ else {
+ return Ok(());
+ };
+ let ty = Uuid::now_v7();
+ sqlx::query(
+ "INSERT INTO entity_types(id,kb_id,key,label) VALUES ($1,$2,'organization','Organization')",
+ )
+ .bind(ty)
+ .bind(f.kb)
+ .execute(&f.pool)
+ .await?;
+ let located = Uuid::now_v7();
+ sqlx::query(
+ "INSERT INTO relation_types(id,kb_id,key,label,kind)
+ VALUES ($1,$2,'located_in','located in','relation')",
+ )
+ .bind(located)
+ .bind(f.kb)
+ .execute(&f.pool)
+ .await?;
+ let city = entity(&f, ty, "Cupertino").await?;
+ // The entity named Apple has no facts; each of the nine stores has one
+ let apple = entity(&f, ty, "Apple").await?;
+ for i in 1..=9 {
+ let store = entity(&f, ty, &format!("Apple Store {i}")).await?;
+ sqlx::query(
+ "INSERT INTO facts(id,kb_id,subject_id,predicate_id,object_id,recorded_at)
+ VALUES ($1,$2,$3,$4,$5,now())",
+ )
+ .bind(Uuid::now_v7())
+ .bind(f.kb)
+ .bind(store)
+ .bind(located)
+ .bind(city)
+ .execute(&f.pool)
+ .await?;
+ }
+
+ let sse = f.ask("Tell me about Apple.").await?;
+ assert!(sse.contains("event: done"), "{sse}");
+ let requests = f.requests();
+ let found = tool_result(&requests[1]);
+ assert!(
+ found.starts_with(&format!("Best match: {apple} | Apple | ")),
+ "find_entities must list the entity named Apple first:\n{found}"
+ );
+ let facts = tool_result(&requests[2]);
+ assert!(facts.contains("\"Apple\" = Apple ("), "{facts}");
+ assert!(facts.contains("Apple: no recorded facts."), "{facts}");
+ assert!(
+ !facts.contains("Cupertino"),
+ "a store's facts must not be read as Apple's:\n{facts}"
+ );
+ f.cleanup().await
+}
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index 22218e90..fb3293e1 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -1457,6 +1457,10 @@ pub async fn profile_distances(
/// 按名字找实体。**一并回总数**——「宁分勿合」本来就会造出一堆同名,
/// 固定十条的时候,想找的那个可能根本不在这十条里而界面上看不出来。
+///
+/// **名字完全相同的排在最前**(规范名或现行的别名),再按度数。只按度数的话,
+/// 问「Apple」而库里有九个事实更多的「Apple Store …」,叫 Apple 的那个排到第十,
+/// 对话与 MCP 的工具只读前八条——它找不到,按名字读事实时读成了另一个
pub async fn search_entities(
pool: &PgPool,
kb_id: Uuid,
@@ -1469,21 +1473,26 @@ pub async fn search_entities(
) -> AppResult<(Vec<GraphNode>, i64)> {
let pattern = format!("%{}%", text.trim());
let named = crate::names::has_name_like("e", 2);
+ // 同名的键与召回同一种写法(`resolution` 里 `has_name_in` 的用法):规整之后小写
+ let exact = vec![crate::resolution::normalize_name(text).to_lowercase()];
// 不回放时 SQL 里没有时刻参数,与从前逐字相同;回放时才多绑一个
let visible = |param: usize| crate::record_axis::entity_visible_at("e", as_of.map(|_| param));
- let rewind = as_of.map(|_| 5);
+ let rewind = as_of.map(|_| 6);
let sql = format!(
"{} WHERE e.kb_id = $1 AND {visible}
AND (e.canonical_name ILIKE $2 OR {named})
- ORDER BY degree DESC, e.canonical_name, e.id LIMIT $3 OFFSET $4",
+ ORDER BY (lower(e.canonical_name) = ANY($5) OR {same_name}) DESC,
+ degree DESC, e.canonical_name, e.id LIMIT $3 OFFSET $4",
node_sql(rewind, rewind),
- visible = visible(5),
+ visible = visible(6),
+ same_name = crate::names::has_name_in("e", 1, 5),
);
let mut nodes_query = sqlx::query_as::<_, GraphNode>(&sql)
.bind(kb_id)
.bind(&pattern)
.bind(limit)
- .bind(offset);
+ .bind(offset)
+ .bind(&exact);
if let Some(t) = as_of {
nodes_query = nodes_query.bind(t);
}
diff --git a/crates/utopia-store/tests/store/a_search_puts_the_exact_name_first.rs b/crates/utopia-store/tests/store/a_search_puts_the_exact_name_first.rs
new file mode 100644
index 00000000..6df5dabd
--- /dev/null
+++ b/crates/utopia-store/tests/store/a_search_puts_the_exact_name_first.rs
@@ -0,0 +1,148 @@
+//! 按名字找实体:名字完全相同的排在最前,再按度数。
+//!
+//! 从前只按度数排、截在 `limit` 上。问「Apple」而库里有九个事实更多的「Apple Store …」,
+//! 叫 Apple 的那个排到第十——对话与 MCP 的工具只读前八条,于是它找不到,按名字读事实时
+//! 读成了另一个。别名同理:「IBM」是 International Business Machines 的一个名字(0041),
+//! 问 IBM 的人要的就是它。
+//!
+//! 回放那一支多绑一个时刻参数,顺序跟着挪了一位,所以也要走一遍。
+//!
+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
+
+use sqlx::PgPool;
+use uuid::Uuid;
+
+async fn entity(pool: &PgPool, kb: Uuid, ty: Uuid, name: &str) -> anyhow::Result<Uuid> {
+ let id = Uuid::now_v7();
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(id)
+ .bind(kb)
+ .bind(ty)
+ .bind(name)
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+/// 一条事实,让 `subject` 的度数加一
+async fn busy(
+ pool: &PgPool,
+ kb: Uuid,
+ subject: Uuid,
+ rel: Uuid,
+ object: Uuid,
+) -> anyhow::Result<()> {
+ sqlx::query(
+ "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, recorded_at)
+ VALUES ($1, $2, $3, $4, $5, now())",
+ )
+ .bind(Uuid::now_v7())
+ .bind(kb)
+ .bind(subject)
+ .bind(rel)
+ .bind(object)
+ .execute(pool)
+ .await?;
+ Ok(())
+}
+
+#[tokio::test]
+async fn a_search_puts_the_exact_name_first() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let (org, ws, kb, ty, rel) = (
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ );
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'exact-name-search-test')")
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'exact-name-search-test')",
+ )
+ .bind(ws)
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'exact-name-search-test')",
+ )
+ .bind(kb)
+ .bind(ws)
+ .execute(&pool)
+ .await?;
+
+ let run = async {
+ sqlx::query(
+ "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'organization', 'Organization')",
+ )
+ .bind(ty)
+ .bind(kb)
+ .execute(&pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO relation_types (id, kb_id, key, label, kind)
+ VALUES ($1, $2, 'located_in', 'located in', 'relation')",
+ )
+ .bind(rel)
+ .bind(kb)
+ .execute(&pool)
+ .await?;
+ let city = entity(&pool, kb, ty, "Cupertino").await?;
+
+ // 叫 Apple 的没有事实;九个名字里带 Apple 的各有一条,度数都比它高
+ let apple = entity(&pool, kb, ty, "Apple").await?;
+ for i in 1..=9 {
+ let store = entity(&pool, kb, ty, &format!("Apple Store {i}")).await?;
+ busy(&pool, kb, store, rel, city).await?;
+ }
+ let (rows, total) =
+ utopia_store::graph::search_entities(&pool, kb, "Apple", 8, 0, None).await?;
+ assert_eq!(total, 10);
+ assert_eq!(rows.len(), 8);
+ assert_eq!(rows[0].id, apple, "the entity named exactly Apple comes first");
+
+ // 回放那一支:时刻参数挪了一位,同名仍在最前
+ let (rows, _) = utopia_store::graph::search_entities(
+ &pool,
+ kb,
+ "Apple",
+ 8,
+ 0,
+ Some(chrono::Utc::now()),
+ )
+ .await?;
+ assert_eq!(rows[0].id, apple, "the same holds when replaying a moment");
+
+ // 别名:IBM 是 International Business Machines 的一个名字,大小写不论
+ let ibm = entity(&pool, kb, ty, "International Business Machines").await?;
+ utopia_store::names::record(&pool, kb, ibm, "IBM", None, None).await?;
+ for i in 1..=9 {
+ let lab = entity(&pool, kb, ty, &format!("IBM Lab {i}")).await?;
+ busy(&pool, kb, lab, rel, city).await?;
+ }
+ let (rows, total) =
+ utopia_store::graph::search_entities(&pool, kb, "ibm", 8, 0, None).await?;
+ assert_eq!(total, 10);
+ assert_eq!(rows[0].id, ibm, "an entity whose name is exactly IBM comes first");
+ anyhow::Ok(())
+ }
+ .await;
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(kb)
+ .execute(&pool)
+ .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 78de844b..0717f424 100644
--- a/crates/utopia-store/tests/store/main.rs
+++ b/crates/utopia-store/tests/store/main.rs
@@ -73,6 +73,7 @@ mod a_rule_computes_what_it_concludes;
mod a_rule_concludes_a_type;
mod a_rule_reads_what_a_rule_concluded;
mod a_schema_document_is_searched_not_extracted;
+mod a_search_puts_the_exact_name_first;
mod a_search_reads_the_base_as_it_was;
mod a_secret_is_sealed_at_rest;
mod a_signature_holds_on_every_path;