Skip to content

World's first open-source enterprise world model.

An end date closes the open span (#609)

WaylandYang56d13326f5c3
diff --git a/crates/utopia-extract/src/lib.rs b/crates/utopia-extract/src/lib.rs
index 337666ad..2bfc2d87 100644
--- a/crates/utopia-extract/src/lib.rs
+++ b/crates/utopia-extract/src/lib.rs
@@ -940,7 +940,13 @@ fn next_token(s: &str) -> (&str, &str) {
pub fn normalize_attr_value(datatype: &str, raw: &serde_json::Value) -> Option<serde_json::Value> {
match datatype {
"number" => match raw {
- serde_json::Value::Number(n) => Some(serde_json::Value::Number(n.clone())),
+ // 模型给的 JSON 数也过一遍 f64:`65` 与 "65%" 解出来的 `65.0` 是同一个数,
+ // 而 serde_json 把整数和浮点当两种值——实测同一条边上 65 撞 65.0 记成了冲突
+ serde_json::Value::Number(n) => n
+ .as_f64()
+ .filter(|f| f.is_finite())
+ .and_then(serde_json::Number::from_f64)
+ .map(serde_json::Value::Number),
serde_json::Value::String(s) => {
let cleaned: String = s
.chars()
@@ -1461,7 +1467,11 @@ mod tests {
normalize_attr_value("number", &json!("35,000")),
Some(json!(35000.0))
);
- assert_eq!(normalize_attr_value("number", &json!(42)), Some(json!(42)));
+ // JSON 里的整数也落成同一种数:`42` 与 "42" 解出来是同一个值
+ assert_eq!(
+ normalize_attr_value("number", &json!(42)),
+ Some(json!(42.0))
+ );
assert_eq!(normalize_attr_value("number", &json!("about ten")), None);
assert_eq!(
normalize_attr_value("date", &json!("2024-07")),
@@ -1651,3 +1661,20 @@ mod tests {
assert!(user.contains("subject_ref/object_ref"));
}
}
+
+#[cfg(test)]
+mod a_number_is_one_number {
+ use super::normalize_attr_value;
+ use serde_json::json;
+
+ /// 模型写 `65` 还是 "65%",落下来都是同一个数——不然同一条边上会记成冲突
+ #[test]
+ fn a_number_is_one_number_however_it_is_written() {
+ let a = normalize_attr_value("number", &json!(65)).unwrap();
+ let b = normalize_attr_value("number", &json!("65%")).unwrap();
+ let c = normalize_attr_value("number", &json!("65")).unwrap();
+ assert_eq!(a, b);
+ assert_eq!(a, c);
+ assert_eq!(a.as_f64(), Some(65.0));
+ }
+}
diff --git a/crates/utopia-server/src/extraction.rs b/crates/utopia-server/src/extraction.rs
index ee630e18..99083c7e 100644
--- a/crates/utopia-server/src/extraction.rs
+++ b/crates/utopia-server/src/extraction.rs
@@ -2988,6 +2988,11 @@ fn unit_for(
sibling_currency: Option<&str>,
declared: Option<&str>,
) -> Option<String> {
+ // 文本、日期、布尔值没有单位可言:"3年"、"30日" 是文本,尾巴上的字不是单位
+ // ——实测「期限=3年」被记成了 `3年 年`
+ if datatype != "number" {
+ return None;
+ }
let declared = declared.map(str::trim).filter(|u| !u.is_empty());
let text = raw.as_str();
if let Some(u) = text
@@ -3052,6 +3057,9 @@ mod unit_for_tests {
);
// 文本型属性没有币种可言
assert_eq!(unit_for(&s("B 轮"), "text", Some("CNY"), None), None);
+ // 文本值尾巴上的字也不是单位:"3年" 是期限的写法,不是 3 个「年」
+ assert_eq!(unit_for(&s("3年"), "text", None, Some("")), None);
+ assert_eq!(unit_for(&s("30日"), "text", None, None), None);
}
#[test]
@@ -3060,10 +3068,6 @@ mod unit_for_tests {
unit_for(&s("4300 人"), "number", None, Some("人")).as_deref(),
Some("人")
);
- assert_eq!(
- unit_for(&s("三年"), "text", None, Some("年")).as_deref(),
- Some("年")
- );
}
#[test]
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index ad6e74c7..83bf9369 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -107,6 +107,23 @@ pub enum QualifierWrite {
/// 往一条边上写一个字面值属性。**属性不进事实的去重键**:同一条边再听到一次带了
/// 金额的,是同一条边补上金额,不是第二条边。
+/// 两个属性值是不是同一个:数按数比(`65` 与 `65.0` 是同一个数——老库里存着整数,
+/// 新写的是浮点),其余按结构比
+fn qualifier_values_agree(a: &serde_json::Value, b: &serde_json::Value) -> bool {
+ match (a, b) {
+ (serde_json::Value::Number(x), serde_json::Value::Number(y)) => x.as_f64() == y.as_f64(),
+ (serde_json::Value::Object(x), serde_json::Value::Object(y)) => {
+ x.len() == y.len()
+ && x.iter()
+ .all(|(k, v)| y.get(k).is_some_and(|w| qualifier_values_agree(v, w)))
+ }
+ (serde_json::Value::Array(x), serde_json::Value::Array(y)) => {
+ x.len() == y.len() && x.iter().zip(y).all(|(v, w)| qualifier_values_agree(v, w))
+ }
+ _ => a == b,
+ }
+}
+
pub async fn upsert_fact_qualifier(
pool: &PgPool,
fact_id: Uuid,
@@ -121,7 +138,7 @@ pub async fn upsert_fact_qualifier(
.fetch_optional(pool)
.await?;
match existing {
- Some((v,)) if &v == value => Ok(QualifierWrite::Same),
+ Some((v,)) if qualifier_values_agree(&v, value) => Ok(QualifierWrite::Same),
Some(_) => Ok(QualifierWrite::Conflict),
None => {
sqlx::query(
@@ -487,8 +504,55 @@ async fn insert_fact_inner(
return Ok((*ended, false));
}
}
- // 精确重复:同 valid_from → 复用
- if let Some((existing, _, _, _)) = same.iter().find(|(_, vf, _, _)| *vf == validity.from) {
+ /* **「某天结束了」的观察(没起点、有终点)撞上同断言的开放行:关上它,不另立一行。**
+ 另立一行让两条各说各话,开放的那条照旧被读成「至今仍是」——实测「移出失信名单」
+ 「辞去董事职务」各多出一条 `- → 日期`,而原来那条还开着。事件没有开放行
+ (两端同一刻),所以只有状态走这里。修正走 supersede(作废 + 改写,证据和边上的
+ 属性随行),与 #393 关「不知哪天」同一条路;起点比终点晚的开放行不是这一段 */
+ if temporal == Temporal::State && validity.from.is_none() {
+ if let Some(to) = validity.to {
+ // 已经关在这一天的:同一件事,复用那一行
+ if let Some((ended, _, _, _)) = same.iter().find(|(_, _, vt, _)| *vt == Some(to)) {
+ attest_earlier(pool, *ended, validity.attested_at).await?;
+ return Ok((*ended, false));
+ }
+ let open = same
+ .iter()
+ .filter(|(_, vf, vt, vtp)| {
+ vt.is_none() && vtp.is_none() && vf.is_none_or(|f| f <= to)
+ })
+ .max_by_key(|(_, vf, _, _)| *vf);
+ if let Some((open, _, _, _)) = open {
+ if let Some(closed) = crate::temporal::close_superseded(
+ pool,
+ *open,
+ to,
+ validity.to_precision.unwrap_or("day"),
+ )
+ .await?
+ {
+ return Ok((closed, true));
+ }
+ }
+ }
+ }
+ // 精确重复:同 valid_from → 复用。同起点、**这次带了终点、那行还开着** → 关上它
+ // (「自 2020-01-10 起任董事」之后读到「2020-01-10 至 2024-04-30 任董事」)
+ if let Some((existing, _, vt, vtp)) = same.iter().find(|(_, vf, _, _)| *vf == validity.from) {
+ if temporal == Temporal::State && vt.is_none() && vtp.is_none() {
+ if let Some(to) = validity.to {
+ if let Some(closed) = crate::temporal::close_superseded(
+ pool,
+ *existing,
+ to,
+ validity.to_precision.unwrap_or("day"),
+ )
+ .await?
+ {
+ return Ok((closed, true));
+ }
+ }
+ }
attest_earlier(pool, *existing, validity.attested_at).await?;
return Ok((*existing, false));
}
@@ -504,15 +568,48 @@ async fn insert_fact_inner(
attest_earlier(pool, *existing, validity.attested_at).await?;
return Ok((*existing, false));
}
+ // 没有开放行,但这次观察的文档日期落在某条**已关上**的行里:说的是那一段,不是
+ // 新的一段——处罚决定书里的「董事李文博」,日期在他的任期之内。另立一条裸行会被
+ // 读成「至今仍是」,而任期明明已经关上了。文档日期在段之后的照旧另立:那可能真是
+ // 新的一段(再次任职),拿不准时宁分勿合
+ if let Some(at) = validity.attested_at {
+ if let Some((existing, _, _, _)) = same
+ .iter()
+ .find(|(_, vf, vt, _)| vt.is_some_and(|t| at <= t) && vf.is_none_or(|f| f <= at))
+ {
+ attest_earlier(pool, *existing, validity.attested_at).await?;
+ return Ok((*existing, false));
+ }
+ }
}
- // 时间精化候选:已有无时无终的裸行,本次观察带了起点 → 落库后作废裸行并链上
+ // 时间精化候选:已有无起点的行(裸行,或只知道终点的行——并行抽取时说结束的那份
+ // 文档可能先到),本次观察带了起点 → 落库后作废那行并链上。只知道终点的行,
+ // 终点跟着走:这次没说终点就沿用它的,说了就得是同一个
+ let mut validity = validity;
let refine_target = if validity.from.is_some() {
same.iter()
- .find(|(_, vf, vt, _)| vf.is_none() && vt.is_none())
- .map(|(id, _, _, _)| *id)
+ .find(|(_, vf, vt, _)| {
+ vf.is_none() && (vt.is_none() || validity.to.is_none() || *vt == validity.to)
+ })
+ .map(|(id, _, vt, vtp)| (*id, *vt, vtp.clone()))
} else {
None
};
+ if let Some((_, Some(vt), vtp)) = &refine_target {
+ if validity.to.is_none() {
+ validity.to = Some(*vt);
+ validity.to_precision = vtp.as_deref().map(|p| match p {
+ "year" => "year",
+ "month" => "month",
+ "day" => "day",
+ "hour" => "hour",
+ "minute" => "minute",
+ "second" => "second",
+ _ => ENDED_UNKNOWN,
+ });
+ }
+ }
+ let refine_target = refine_target.map(|(id, _, _)| id);
let id = Uuid::now_v7();
let insert_sql = match object {
@@ -574,6 +671,17 @@ async fn insert_fact_inner(
.bind(old_id)
.execute(pool)
.await?;
+ // 边上的属性也随行(0037):裸行上已有的金额、职务不因为精化了时间而丢
+ sqlx::query(
+ "INSERT INTO fact_qualifiers (fact_id, qualifier_type_id, value, entity_id)
+ SELECT $1, qualifier_type_id, value, entity_id
+ FROM fact_qualifiers WHERE fact_id = $2
+ ON CONFLICT DO NOTHING",
+ )
+ .bind(id)
+ .bind(old_id)
+ .execute(pool)
+ .await?;
}
Ok((id, true))
}
diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rs
index 4d86bd41..22650da2 100644
--- a/crates/utopia-store/src/resolution.rs
+++ b/crates/utopia-store/src/resolution.rs
@@ -292,6 +292,15 @@ pub async fn resolve_mention(
});
};
+ // 这一次调用**绝不能归上去**的全部:调用方点名排除的(`exclude`),加上这一轮
+ // 看过、并且会被判「不是同一个」的同名候选。下面无论走哪条分支决定新建,都要把
+ // 这份名单递给 `create_entity`——锁里那条回捞按名字捞,不给名单就会把它们捞回来
+ let weighed: Vec<Uuid> = exclude
+ .iter()
+ .copied()
+ .chain(candidates.iter().map(|c| c.id))
+ .collect();
+
// 有画像的候选算相似度;无画像(历史数据/无 embedding 期创建)单独归类
let mut scored: Vec<(&Candidate, f32)> = Vec::new();
let mut unprofiled: Option<&Candidate> = None;
@@ -347,7 +356,16 @@ pub async fn resolve_mention(
});
}
}
- let id = create_entity(pool, kb_id, type_id, &name, context).await?;
+ let (id, created) =
+ create_entity(pool, kb_id, type_id, &name, context, &weighed).await?;
+ if !created {
+ // 并行的另一份文档刚建好它:用它的,审核对也是它排的
+ return Ok(Resolution {
+ entity_id: id,
+ created: false,
+ reviews: Vec::new(),
+ });
+ }
refresh_disambiguators(pool, kb_id, &name).await?;
let reviews = [(best, sim), (runner, r_sim)]
.into_iter()
@@ -403,7 +421,15 @@ pub async fn resolve_mention(
});
}
}
- let id = create_entity(pool, kb_id, type_id, &name, context).await?;
+ let (id, created) = create_entity(pool, kb_id, type_id, &name, context, &weighed).await?;
+ if !created {
+ // 并行的另一份文档刚建好它:用它的,审核对也是它排的
+ return Ok(Resolution {
+ entity_id: id,
+ created: false,
+ reviews: Vec::new(),
+ });
+ }
refresh_disambiguators(pool, kb_id, &name).await?;
let mut reviews = best_scored
.filter(|(_, sim)| *sim >= SIM_NEW)
@@ -877,7 +903,21 @@ async fn resolve_type_drift(
}
}
- let id = create_entity(pool, kb_id, type_id, name, context).await?;
+ // 同上:点名排除的,加上跨类型同名里掂量过的
+ let weighed: Vec<Uuid> = exclude
+ .iter()
+ .copied()
+ .chain(cross.iter().map(|c| c.id))
+ .collect();
+ let (id, created) = create_entity(pool, kb_id, type_id, name, context, &weighed).await?;
+ if !created {
+ // 并行的另一份文档刚建好它:用它的,审核对也是它排的
+ return Ok(Resolution {
+ entity_id: id,
+ created: false,
+ reviews: Vec::new(),
+ });
+ }
if !cross.is_empty() {
// 跨类型同名并存:消歧后缀按名字分组(不分类型),需要刷新
refresh_disambiguators(pool, kb_id, name).await?;
@@ -911,6 +951,13 @@ async fn resolve_type_drift(
})
}
+/// 新建一个实体。返回 `(id, 是否真的新建)`。
+///
+/// **同名同类的新建串行化。** 上面的查找不在事务里:两份文档并行抽取,同一个名字
+/// 各自查一遍都没有、各自建一个——实测「澜图数据」在同一秒里建了两个,之后每一次
+/// 提到它都撞上两个候选,再各建一个、各排一对审核,一篇语料跑完裂成四个。
+/// 这里按(库,名字)拿事务级咨询锁,锁里再查一次:别人刚建好的,就用它的。
+/// 不同类型的同名不在此列——那是消歧的事,不是竞态
async fn create_entity(
pool: &PgPool,
kb_id: Uuid,
@@ -918,7 +965,37 @@ async fn create_entity(
type_id: Option<Uuid>,
name: &str,
context: Option<&[f32]>,
-) -> AppResult<Uuid> {
+ // 调用方**刚刚掂量过、并且决定不并**的那些同名实体。
+ //
+ // 锁里那条回捞不加这个就分不清两件事:一件是「并行的另一份文档一毫秒前
+ // 建好了同名的它」——该用它的;另一件是「这个名字本来就有人,而调用方看过
+ // 之后决定另建一个」——这时回捞只会捞回它刚拒绝的那个候选,等于让一把锁
+ // 替人把 mention 归到其中一个身上。同名并列那条路上这正是 #270 禁的事:
+ // 分不开就别硬分,谁也不归,两个都送审。
+ weighed: &[Uuid],
+) -> AppResult<(Uuid, bool)> {
+ let mut tx = pool.begin().await?;
+ sqlx::query("SELECT pg_advisory_xact_lock(hashtext($1), hashtext($2))")
+ .bind(kb_id.to_string())
+ .bind(name)
+ .execute(&mut *tx)
+ .await?;
+ let existing: Option<(Uuid,)> = sqlx::query_as(
+ "SELECT id FROM entities
+ WHERE kb_id = $1 AND canonical_name = $2 AND type_id IS NOT DISTINCT FROM $3
+ AND merged_into IS NULL AND id <> ALL($4)
+ ORDER BY id LIMIT 1",
+ )
+ .bind(kb_id)
+ .bind(name)
+ .bind(type_id)
+ .bind(weighed)
+ .fetch_optional(&mut *tx)
+ .await?;
+ if let Some((id,)) = existing {
+ tx.commit().await?;
+ return Ok((id, false));
+ }
let id = Uuid::now_v7();
sqlx::query(
"INSERT INTO entities (id, kb_id, type_id, canonical_name, profile_embedding, profile_n)
@@ -930,9 +1007,10 @@ async fn create_entity(
.bind(name)
.bind(context.map(|c| Vector::from(c.to_vec())))
.bind(i32::from(context.is_some()))
- .execute(pool)
+ .execute(&mut *tx)
.await?;
- Ok(id)
+ tx.commit().await?;
+ Ok((id, true))
}
async fn touch_entity(pool: &PgPool, id: Uuid) -> AppResult<()> {
diff --git a/crates/utopia-store/src/temporal.rs b/crates/utopia-store/src/temporal.rs
index 325bce51..92a25299 100644
--- a/crates/utopia-store/src/temporal.rs
+++ b/crates/utopia-store/src/temporal.rs
@@ -356,6 +356,7 @@ pub async fn close_superseded(
.execute(&mut *tx)
.await?;
copy_evidence(&mut tx, fact_id, corrected).await?;
+ copy_qualifiers(&mut tx, fact_id, corrected).await?;
tx.commit().await?;
Ok(Some(corrected))
}
@@ -399,10 +400,31 @@ pub async fn close_with_unknown_end(
.execute(&mut *tx)
.await?;
copy_evidence(&mut tx, fact_id, corrected).await?;
+ copy_qualifiers(&mut tx, fact_id, corrected).await?;
tx.commit().await?;
Ok(Some(corrected))
}
+/// 边上的属性随修正行复制(0037):纠正的是时间区间,边上的金额、职务照旧——
+/// 不搬的话,闭合一段任职就丢了它的职务
+async fn copy_qualifiers(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ from: Uuid,
+ to: Uuid,
+) -> AppResult<()> {
+ sqlx::query(
+ "INSERT INTO fact_qualifiers (fact_id, qualifier_type_id, value, entity_id)
+ SELECT $1, qualifier_type_id, value, entity_id
+ FROM fact_qualifiers WHERE fact_id = $2
+ ON CONFLICT DO NOTHING",
+ )
+ .bind(to)
+ .bind(from)
+ .execute(&mut **tx)
+ .await?;
+ Ok(())
+}
+
/// 证据引用随修正行复制。表层谓词一起搬:纠正的是时间区间,不是原文说了什么。
async fn copy_evidence(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
@@ -478,6 +500,7 @@ pub async fn correct_interval(
.execute(&mut *tx)
.await?;
copy_evidence(&mut tx, fact_id, corrected).await?;
+ copy_qualifiers(&mut tx, fact_id, corrected).await?;
tx.commit().await?;
Ok(Some(corrected))
}
diff --git a/crates/utopia-store/tests/a_name_created_twice_at_once_is_one_entity.rs b/crates/utopia-store/tests/a_name_created_twice_at_once_is_one_entity.rs
new file mode 100644
index 00000000..fa408d00
--- /dev/null
+++ b/crates/utopia-store/tests/a_name_created_twice_at_once_is_one_entity.rs
@@ -0,0 +1,71 @@
+//! 两份文档并行抽取,同一个名字同时到达消解:只建一个实体。
+//!
+//! 实测「澜图数据」在同一秒里建了两个,之后每次提到它都撞上两个候选、再各建一个,
+//! 一篇语料跑完裂成四个。新建按(库,名字)拿咨询锁串行化,锁里再查一次。
+
+use sqlx::PgPool;
+use uuid::Uuid;
+
+#[tokio::test]
+async fn a_name_created_twice_at_once_is_one_entity() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let tag = Uuid::now_v7();
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, $2)")
+ .bind(org)
+ .bind(format!("race-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, $3)")
+ .bind(ws)
+ .bind(org)
+ .bind(format!("race-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, $3)")
+ .bind(kb)
+ .bind(ws)
+ .bind(format!("race-{tag}"))
+ .execute(&pool)
+ .await?;
+ let class = Uuid::now_v7();
+ sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, $3, $4)")
+ .bind(class)
+ .bind(kb)
+ .bind("company")
+ .bind("公司")
+ .execute(&pool)
+ .await?;
+
+ let name = format!("澜图数据-{tag}");
+ let go = || {
+ utopia_store::resolution::resolve_mention(&pool, kb, Some(class), &name, None, None, &[])
+ };
+ let (a, b, c, d) = tokio::join!(go(), go(), go(), go());
+ let ids = [a?.entity_id, b?.entity_id, c?.entity_id, d?.entity_id];
+ assert!(
+ ids.iter().all(|id| *id == ids[0]),
+ "四次并行消解落到同一个实体:{ids:?}"
+ );
+ let rows: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM entities WHERE kb_id = $1 AND canonical_name = $2 AND merged_into IS NULL",
+ )
+ .bind(kb)
+ .bind(&name)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(rows, 1, "库里只有一个这个名字");
+
+ 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?;
+ Ok(())
+}
diff --git a/crates/utopia-store/tests/a_number_is_one_number_however_written.rs b/crates/utopia-store/tests/a_number_is_one_number_however_written.rs
new file mode 100644
index 00000000..43b2ea91
--- /dev/null
+++ b/crates/utopia-store/tests/a_number_is_one_number_however_written.rs
@@ -0,0 +1,119 @@
+//! 同一条边上,先写 `65`(老库里的整数)再写 `65.0`:是同一个数,不是冲突。
+
+use sqlx::PgPool;
+use uuid::Uuid;
+
+#[tokio::test]
+async fn a_number_is_one_number_however_written() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let tag = Uuid::now_v7();
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, $2)")
+ .bind(org)
+ .bind(format!("num-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, $3)")
+ .bind(ws)
+ .bind(org)
+ .bind(format!("num-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, $3)")
+ .bind(kb)
+ .bind(ws)
+ .bind(format!("num-{tag}"))
+ .execute(&pool)
+ .await?;
+ let class = Uuid::now_v7();
+ sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, $3, $4)")
+ .bind(class)
+ .bind(kb)
+ .bind("org")
+ .bind("Org")
+ .execute(&pool)
+ .await?;
+ let stake = utopia_store::ontology::create_relation_type(
+ &pool,
+ kb,
+ "stake",
+ "stake",
+ "state",
+ Default::default(),
+ "",
+ "attribute",
+ &[class],
+ &[],
+ Some("number"),
+ Some("%"),
+ )
+ .await?;
+ let holds = utopia_store::ontology::create_relation_type(
+ &pool,
+ kb,
+ "holds_stake_in",
+ "holds stake in",
+ "state",
+ Default::default(),
+ "",
+ "relation",
+ &[],
+ &[],
+ None,
+ None,
+ )
+ .await?;
+ let (a, b) = (Uuid::now_v7(), Uuid::now_v7());
+ for (id, name) in [(a, "澜图科技"), (b, "澜图新材料")] {
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(id)
+ .bind(kb)
+ .bind(class)
+ .bind(format!("{name}-{tag}"))
+ .execute(&pool)
+ .await?;
+ }
+ let (fact, _) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ a,
+ Some(holds),
+ b,
+ utopia_store::graph::Validity::default(),
+ 0.9,
+ )
+ .await?;
+
+ let as_int = serde_json::json!({ "value": 65, "unit": "%" });
+ let as_float = serde_json::json!({ "value": 65.0, "unit": "%" });
+ let other = serde_json::json!({ "value": 60.0, "unit": "%" });
+ assert_eq!(
+ utopia_store::graph::upsert_fact_qualifier(&pool, fact, stake, &as_int).await?,
+ utopia_store::graph::QualifierWrite::Set
+ );
+ assert_eq!(
+ utopia_store::graph::upsert_fact_qualifier(&pool, fact, stake, &as_float).await?,
+ utopia_store::graph::QualifierWrite::Same,
+ "65 与 65.0 是同一个数"
+ );
+ assert_eq!(
+ utopia_store::graph::upsert_fact_qualifier(&pool, fact, stake, &other).await?,
+ utopia_store::graph::QualifierWrite::Conflict,
+ "60 才是不一样的数"
+ );
+
+ 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?;
+ Ok(())
+}
diff --git a/crates/utopia-store/tests/an_end_date_closes_the_open_span.rs b/crates/utopia-store/tests/an_end_date_closes_the_open_span.rs
new file mode 100644
index 00000000..00ca2be2
--- /dev/null
+++ b/crates/utopia-store/tests/an_end_date_closes_the_open_span.rs
@@ -0,0 +1,377 @@
+//! 一段状态先被观察到起点,后来的文档说它某天结束了:账本关上那一段,不另立一行。
+//!
+//! 「2024-11-05 列入失信名单」之后读到「2025-03-12 移出名单」——移出那条没有起点、
+//! 只有终点,它说的是同一段的结束。旧行作废、修正行链回、边上的属性随行(0037)。
+
+use sqlx::PgPool;
+use uuid::Uuid;
+
+type Stamp = chrono::DateTime<chrono::Utc>;
+/// 活着的一行:id、起、止、链回哪一行
+type LiveRow = (Uuid, Option<Stamp>, Option<Stamp>, Option<Uuid>);
+
+fn day(s: &str) -> Stamp {
+ format!("{s}T00:00:00Z").parse().unwrap()
+}
+
+fn span(
+ from: Option<&str>,
+ to: Option<&str>,
+ attested: &str,
+) -> utopia_store::graph::Validity<'static> {
+ utopia_store::graph::Validity {
+ from: from.map(day),
+ from_precision: from.map(|_| "day"),
+ to: to.map(day),
+ to_precision: to.map(|_| "day"),
+ attested_at: Some(day(attested)),
+ }
+}
+
+async fn live(
+ pool: &PgPool,
+ kb: Uuid,
+ pred: Uuid,
+ object: Uuid,
+) -> Result<Vec<LiveRow>, sqlx::Error> {
+ sqlx::query_as(
+ "SELECT id, valid_from, valid_to, supersedes FROM facts
+ WHERE kb_id = $1 AND predicate_id = $2 AND object_id = $3 AND invalidated_at IS NULL",
+ )
+ .bind(kb)
+ .bind(pred)
+ .bind(object)
+ .fetch_all(pool)
+ .await
+}
+
+fn ends(to: &str, attested: &str) -> utopia_store::graph::Validity<'static> {
+ utopia_store::graph::Validity {
+ from: None,
+ from_precision: None,
+ to: Some(day(to)),
+ to_precision: Some("day"),
+ attested_at: Some(day(attested)),
+ }
+}
+
+#[tokio::test]
+async fn an_end_date_closes_the_open_span() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let tag = Uuid::now_v7();
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, $2)")
+ .bind(org)
+ .bind(format!("span-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, $3)")
+ .bind(ws)
+ .bind(org)
+ .bind(format!("span-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, $3)")
+ .bind(kb)
+ .bind(ws)
+ .bind(format!("span-{tag}"))
+ .execute(&pool)
+ .await?;
+ let class = Uuid::now_v7();
+ sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, $3, $4)")
+ .bind(class)
+ .bind(kb)
+ .bind("org")
+ .bind("Org")
+ .execute(&pool)
+ .await?;
+ let title = utopia_store::ontology::create_relation_type(
+ &pool,
+ kb,
+ "title",
+ "title",
+ "state",
+ Default::default(),
+ "",
+ "attribute",
+ &[class],
+ &[],
+ Some("text"),
+ None,
+ )
+ .await?;
+ let works_at = utopia_store::ontology::create_relation_type(
+ &pool,
+ kb,
+ "works_at",
+ "works at",
+ "state",
+ Default::default(),
+ "",
+ "relation",
+ &[],
+ &[],
+ None,
+ None,
+ )
+ .await?;
+ let (a, b) = (Uuid::now_v7(), Uuid::now_v7());
+ for (id, name) in [(a, "李文博"), (b, "澜图科技")] {
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(id)
+ .bind(kb)
+ .bind(class)
+ .bind(format!("{name}-{tag}"))
+ .execute(&pool)
+ .await?;
+ }
+
+ // 第一份文档:从 2020-01-10 起任董事
+ let (first, created) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ a,
+ Some(works_at),
+ b,
+ utopia_store::graph::Validity {
+ from: Some(day("2020-01-10")),
+ from_precision: Some("day"),
+ to: None,
+ to_precision: None,
+ attested_at: Some(day("2025-08-01")),
+ },
+ 0.9,
+ )
+ .await?;
+ assert!(created);
+ let value = serde_json::json!({ "value": "董事" });
+ utopia_store::graph::upsert_fact_qualifier(&pool, first, title, &value).await?;
+
+ // 第二份文档:2024-04-30 辞去董事职务——没起点、只有终点
+ let (closed, created) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ a,
+ Some(works_at),
+ b,
+ ends("2024-04-30", "2024-04-18"),
+ 0.9,
+ )
+ .await?;
+ assert!(created, "关上一段是一次改写,不是复用");
+ assert_ne!(closed, first);
+
+ let live: Vec<LiveRow> = sqlx::query_as(
+ "SELECT id, valid_from, valid_to, supersedes FROM facts
+ WHERE kb_id = $1 AND predicate_id = $2 AND invalidated_at IS NULL",
+ )
+ .bind(kb)
+ .bind(works_at)
+ .fetch_all(&pool)
+ .await?;
+ assert_eq!(live.len(), 1, "只剩一条活着的:那一段,关上了");
+ let (id, from, to, supersedes) = live[0];
+ assert_eq!(id, closed);
+ assert_eq!(from, Some(day("2020-01-10")), "起点照旧");
+ assert_eq!(to, Some(day("2024-04-30")), "终点是离任那天");
+ assert_eq!(supersedes, Some(first), "修正行链回旧行");
+ let old_dead: bool =
+ sqlx::query_scalar("SELECT invalidated_at IS NOT NULL FROM facts WHERE id = $1")
+ .bind(first)
+ .fetch_one(&pool)
+ .await?;
+ assert!(old_dead, "旧行在认知轴上作废,不删");
+ let quals = utopia_store::graph::fact_qualifiers_for(&pool, &[closed]).await?;
+ assert_eq!(quals[&closed].len(), 1, "职务随修正行");
+ assert_eq!(quals[&closed][0].value, Some(value));
+
+ // 第三份文档又说了一遍同一天离任:同一件事,复用那一行
+ let (again, created) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ a,
+ Some(works_at),
+ b,
+ ends("2024-04-30", "2024-05-02"),
+ 0.9,
+ )
+ .await?;
+ assert_eq!(again, closed);
+ assert!(!created);
+
+ // 处罚决定书里提到「董事李文博」,没写日期,文档日期在任期之内:说的是这一段
+ let (mentioned, created) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ a,
+ Some(works_at),
+ b,
+ span(None, None, "2024-04-17"),
+ 0.9,
+ )
+ .await?;
+ assert_eq!(mentioned, closed, "任期内的无日期提及并进关上的那一段");
+ assert!(!created);
+ // 任期之后的无日期提及:可能是再次任职,另立一条,等人裁
+ let (later, created) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ a,
+ Some(works_at),
+ b,
+ span(None, None, "2025-06-01"),
+ 0.9,
+ )
+ .await?;
+ assert!(created);
+ assert_ne!(later, closed);
+
+ 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?;
+ Ok(())
+}
+
+/// 同一段的三种来法,都只剩一条活着的行:
+/// 起点先到、后来的文档把整段说全;终点先到(并行抽取时说结束的那份先落库)、起点后到。
+#[tokio::test]
+async fn a_span_told_in_two_halves_is_one_row() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let tag = Uuid::now_v7();
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, $2)")
+ .bind(org)
+ .bind(format!("halves-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, $3)")
+ .bind(ws)
+ .bind(org)
+ .bind(format!("halves-{tag}"))
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, $3)")
+ .bind(kb)
+ .bind(ws)
+ .bind(format!("halves-{tag}"))
+ .execute(&pool)
+ .await?;
+ let class = Uuid::now_v7();
+ sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, $3, $4)")
+ .bind(class)
+ .bind(kb)
+ .bind("org")
+ .bind("Org")
+ .execute(&pool)
+ .await?;
+ let listed = utopia_store::ontology::create_relation_type(
+ &pool,
+ kb,
+ "listed",
+ "listed",
+ "state",
+ Default::default(),
+ "",
+ "relation",
+ &[],
+ &[],
+ None,
+ None,
+ )
+ .await?;
+ let mut ids = Vec::new();
+ for name in ["法院", "澜图新材料", "第二家"] {
+ 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(class)
+ .bind(format!("{name}-{tag}"))
+ .execute(&pool)
+ .await?;
+ ids.push(id);
+ }
+ let (court, a, b) = (ids[0], ids[1], ids[2]);
+
+ // 起点先到,后来的文档把整段说全(同起点 + 终点)→ 关上,不另立
+ let (first, _) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ court,
+ Some(listed),
+ a,
+ span(Some("2024-11-05"), None, "2024-11-07"),
+ 0.9,
+ )
+ .await?;
+ let (closed, created) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ court,
+ Some(listed),
+ a,
+ span(Some("2024-11-05"), Some("2025-03-12"), "2025-03-14"),
+ 0.9,
+ )
+ .await?;
+ assert!(created);
+ let rows = live(&pool, kb, listed, a).await?;
+ assert_eq!(rows.len(), 1, "同起点带终点:关上原来那行");
+ assert_eq!(rows[0].0, closed);
+ assert_eq!(rows[0].2, Some(day("2025-03-12")));
+ assert_eq!(rows[0].3, Some(first));
+
+ // 终点先到(说结束的那份文档先落库),起点后到 → 一行,两头都有
+ let (end_only, _) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ court,
+ Some(listed),
+ b,
+ ends("2025-03-12", "2025-03-14"),
+ 0.9,
+ )
+ .await?;
+ let (merged, created) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ court,
+ Some(listed),
+ b,
+ span(Some("2024-11-05"), None, "2024-11-07"),
+ 0.9,
+ )
+ .await?;
+ assert!(created);
+ let rows = live(&pool, kb, listed, b).await?;
+ assert_eq!(rows.len(), 1, "起点后到:并进只知道终点的那行");
+ assert_eq!(rows[0].0, merged);
+ assert_eq!(rows[0].1, Some(day("2024-11-05")), "起点是后到的");
+ assert_eq!(rows[0].2, Some(day("2025-03-12")), "终点沿用先到的");
+ assert_eq!(rows[0].3, Some(end_only));
+
+ 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?;
+ Ok(())
+}
diff --git a/docs/decisions/0022-an-unknown-date-is-not-an-open-one.md b/docs/decisions/0022-an-unknown-date-is-not-an-open-one.md
index 4e78312a..bcf1042d 100644
--- a/docs/decisions/0022-an-unknown-date-is-not-an-open-one.md
+++ b/docs/decisions/0022-an-unknown-date-is-not-an-open-one.md
@@ -1,6 +1,6 @@
# 0022 · An unknown date is not an open one
-- **Status**: implemented in two cuts · #394: `world_axis` beside `record_axis`, `facts.attested_at` set by every writer and backfilled, every server read and both client filters on the read interval, `holds_from` / `holds_to` on edges and entity facts, and an undated ending **closes** the dated open row it ends (`close_with_unknown_end`) · second cut: the evaluator intersects premise intervals as read (`read_span`), a derived bound that came from an anchor carries no precision (migration 0031 loosens the derived CHECK), and the violations list judges "still open" by the read end · third cut (#393): a second anchor, `attested_to`, so a bare open row closes too
+- **Status**: implemented in two cuts · #394: `world_axis` beside `record_axis`, `facts.attested_at` set by every writer and backfilled, every server read and both client filters on the read interval, `holds_from` / `holds_to` on edges and entity facts, and an undated ending **closes** the dated open row it ends (`close_with_unknown_end`) · second cut: the evaluator intersects premise intervals as read (`read_span`), a derived bound that came from an anchor carries no precision (migration 0031 loosens the derived CHECK), and the violations list judges "still open" by the read end · third cut (#393): a second anchor, `attested_to`, so a bare open row closes too · fourth cut (2026-09-11): a **dated** ending closes the open row the same way, and every superseding row carries the edge's qualifiers
- **Written**: 2026-09-06 (conventions in the [README](README.md))
- **Related**: [0003](0003-ontology-growth-loop.md)'s graph migration gave the end of a fact three states and refused to store a document's date as an indeterminate instant; this record keeps that refusal and puts the date in a column that says what it is. [0019](0019-the-second-clock-can-be-rewound.md) put the record-axis predicate in one place (`record_axis`) and kept `at` and `as_of` apart; this record does the same for the world axis. [0017](0017-a-contradiction-points-upstream.md) gave derived rows their own precisions, and [0021](0021-a-rule-reads-attributes-and-concludes-a-type.md)'s evaluator intersects premise intervals — both inherit the rule below. From #345 and #352, both found by the temporal benchmark (#306).
@@ -101,5 +101,6 @@ The slider and a timed question stop returning a fact before its evidence or aft
- **An ending does not close the dated row it ends.** ~~`reconcile_new_fact` returns early for any fact that has ended, so the "no longer holds" row from #352 sits beside the 2023-06-01 row that still says "holds".~~ Settled in the first cut, because the benchmark showed the read rule alone leaves #345's question failing: the ended-unknown row read correctly, and the dated open row beside it went on holding. An undated ending that meets an open row **with a stated start** of the same assertion now closes it — a superseding row with `'unknown'` at the end, anchored on the ending's document, the old row invalidated (`temporal::close_with_unknown_end`). A repeated ending reuses the closed row and only moves its anchor earlier.
- **A bare open row could not be closed the same way** (#393). ~~With no stated start the one anchor would have to serve both ends — held from the first note, ended by the second. The exact-duplicate path in `insert_fact_inner` merged the ending into the bare row and dropped it.~~ Settled with a second anchor: `attested_at` became `attested_from`, and `attested_to` carries the date of the document that stated the ending, present exactly when the end is `'unknown'`. `close_with_unknown_end` now closes any open row of the assertion — stated start or not — keeping `attested_from` and setting `attested_to`; a repeated ending only moves either anchor earlier. Every superseding writer copies both; a person's own edit that marks a fact ended-unknown anchors the end at now.
+- **A dated ending did not close the open row either** (2026-09-11). ~~"李文博于 2024-04-30 辞去董事职务" arrived as a state observation with no start and a stated end; `insert_fact_inner` had a path for an *undated* ending (above) and none for a dated one, so the ledger held `2020-01-10 → open` beside `open → 2024-04-30` and read the first as still holding. The same for a company taken off a court's list of defaulters.~~ Settled: a state observation with no start and a stated end meets the open row of the same assertion whose start is not later than that end and closes it through `close_superseded` — the same superseding path, the end dated and truncated to its precision; a repeated dated ending reuses the closed row and only moves its anchor earlier. The same cut made every superseding writer (`close_superseded`, `close_with_unknown_end`, `correct_interval`, the refinement in `insert_fact_inner`) copy the edge's qualifiers along with its evidence — before it, closing a directorship dropped its title ([0037](0037-a-relation-carries-its-own-attributes.md)).
- **Retrieval is untimed.** Vector and full-text recall take `as_of` (0019) but no `at`; a chunk about 2025 answers a question about 2023 and the model is left to notice. Out of scope; noted so the benchmark's chat probe is read with that in mind.
- **Showing the anchor.** Whether the panel should say "attested 2024-02-20" beside a blank start, so a person sees why the slider hides the edge before then. Deferred until the first cut has been looked at.
diff --git a/docs/decisions/0037-a-relation-carries-its-own-attributes.md b/docs/decisions/0037-a-relation-carries-its-own-attributes.md
index a27bf89b..b69fdf2d 100644
--- a/docs/decisions/0037-a-relation-carries-its-own-attributes.md
+++ b/docs/decisions/0037-a-relation-carries-its-own-attributes.md
@@ -123,6 +123,12 @@ rules corpus declared and undeclared, a Chinese corpus declared and undeclared):
different amounts and no conflict. The next cut (two rows plus `fact_conflicts` for a
disagreement) has to close this first — a per-base advisory lock around the insert, or a
partial unique index on live rows.
+- **A superseding row carried its evidence but not its qualifiers.** `close_superseded`,
+ `close_with_unknown_end`, `correct_interval` and the refinement path in `insert_fact_inner`
+ all predate this record; closing a 董事 span on the day of the resignation produced a live
+ row without its 职务. All four now copy `fact_qualifiers` the way `adopt` does (the
+ 2026-09-11 litigation rehearsal, with [0022](0022-an-unknown-date-is-not-an-open-one.md)'s
+ fourth cut).
- The model dates "earlier this year" to a concrete day and so mints a moment the text never
gave; identity follows the model's date. Visible on the timeline, not a qualifier defect.