Skip to content

World's first open-source enterprise world model.

A timeline is placed whole, whatever order its values arrive in (#679)

WaylandYang573fda3f3cbb
diff --git a/crates/utopia-extract/src/lib.rs b/crates/utopia-extract/src/lib.rs
index 5d1ff432..6e1ca4c8 100644
--- a/crates/utopia-extract/src/lib.rs
+++ b/crates/utopia-extract/src/lib.rs
@@ -96,6 +96,10 @@ pub struct ExtractedFact {
/// 同上,宾语那一侧
#[serde(default)]
pub object_span: Option<String>,
+ /// 日期属性的值只相对一件事给出(「触发日后 45 天」),没有日历上的日期(#681 §4)。
+ /// 模型判断、模型标;服务端不认这类说法的词,只看这个标记决定收不收
+ #[serde(default)]
+ pub relative: bool,
}
/// 提示词里的一条关系。
@@ -291,7 +295,8 @@ pub fn build_messages_with_opening(
let attr_rules = if attributes.is_empty() {
String::new()
} else {
- "\n10. Attribute facts carry \"value\" (no \"object\"): number = the figure **as the text writes it, magnitude and currency included** \n (\"86亿元\", \"$5 billion\", \"4,300 人\") — never reduce it to a bare number, the server converts; date = \"YYYY[-MM[-DD]]\" (a zoned clock time only when the text gives one); bool = true/false; \
+ "\n10. Attribute facts carry \"value\" (no \"object\"): number = the figure **as the text writes it, magnitude and currency included** \n (\"86亿元\", \"$5 billion\", \"4,300 人\") — never reduce it to a bare number, the server converts; date = \"YYYY[-MM[-DD]]\" (a zoned clock time only when the text gives one) — a date the text gives only relative to an event \
+ (\"45 days after the Trigger Date\", \"within 30 days of closing\") has no calendar date to convert: write it as the text writes it and add \"relative\": true; bool = true/false; \
text = a short string. Only attach an attribute to a subject of its listed class. \
valid_from = when this value took effect, if the text or the opening of the document says so. \
A document that changes a value set earlier — amends, extends or replaces it — makes the new value \
@@ -377,7 +382,8 @@ pub fn build_messages_with_opening(
units and all — except a date, which is always written in the format of rule 3 \
(\"June 23, 2020\" is \"2020-06-23\"): the server keeps a date only in that \
format, and a date written any other way is lost. A deadline or a period stated \
- relative to an event, with no calendar date, is not a date. \
+ relative to an event, with no calendar date, is not a date: keep it as written \
+ and mark it \"relative\" as rule 10 says. \
**A stated figure left out is the loss that costs most**: the reader \
came for those numbers, and no later step can recover one that was never written \
down.\n\
@@ -1045,6 +1051,25 @@ fn next_token(s: &str) -> (&str, &str) {
(&s[..end], &s[end..])
}
+/// 一个属性值落库时的样子:按 datatype 归一成 `{"value": …}`;失败返回 None,调用方记
+/// `attr_datatype`。
+///
+/// 日期属性上一个**相对**的值(#681 §4):解不成日期、模型又标了 `relative`、写的是一段非空
+/// 文字时,照原文收下,值里带 `"relative": true`。它不是日期,从不当日期比较或排序。解得成
+/// 日期的照日期存,标错了也不当相对;没标的非日期值仍然不收
+pub fn attr_object_value(
+ datatype: &str,
+ raw: &serde_json::Value,
+ relative: bool,
+) -> Option<serde_json::Value> {
+ if let Some(value) = normalize_attr_value(datatype, raw) {
+ return Some(serde_json::json!({ "value": value }));
+ }
+ let written = raw.as_str().map(str::trim).filter(|s| !s.is_empty())?;
+ (datatype == "date" && relative)
+ .then(|| serde_json::json!({ "value": written, "relative": true }))
+}
+
/// 属性值按 datatype 归一。失败返回 None——宁缺勿脏,调用方跳过并记日志。
/// number 容忍千分位/空格;date 要求 YYYY[-MM[-DD]] 且保留原精度;bool 宽容 yes/no。
pub fn normalize_attr_value(datatype: &str, raw: &serde_json::Value) -> Option<serde_json::Value> {
@@ -1169,6 +1194,59 @@ fn split_zone(clock: &str) -> Option<(&str, chrono::Duration)> {
mod prompt_shape_tests {
use super::*;
+ /// 第十份补充协议把截止日改成「触发日后 45 天」:模型标 relative,服务端照原文收;
+ /// 没标的、不是日期属性的、空的都不走这条
+ #[test]
+ fn a_relative_date_is_kept_as_written_only_when_marked() {
+ let raw = serde_json::json!("45 days after the Trigger Date");
+ assert_eq!(
+ attr_object_value("date", &raw, true),
+ Some(
+ serde_json::json!({ "value": "45 days after the Trigger Date", "relative": true })
+ )
+ );
+ assert_eq!(attr_object_value("date", &raw, false), None, "没标就不收");
+ assert_eq!(
+ attr_object_value("date", &serde_json::json!(" "), true),
+ None
+ );
+ // 解得成日期的照日期存,标了 relative 也不当相对
+ assert_eq!(
+ attr_object_value("date", &serde_json::json!("2020-06-23"), true),
+ Some(serde_json::json!({ "value": "2020-06-23" }))
+ );
+ // 不是日期属性:只按它自己的 datatype 归一,relative 不起作用
+ assert_eq!(
+ attr_object_value("bool", &serde_json::json!("45 days after"), true),
+ None
+ );
+ assert_eq!(
+ attr_object_value("number", &serde_json::json!("1,250"), true),
+ Some(serde_json::json!({ "value": 1250.0 }))
+ );
+ let fact: ExtractedFact = serde_json::from_value(serde_json::json!({
+ "subject": "Lease", "predicate": "expansion_option_deadline",
+ "value": "45 days after the Trigger Date", "relative": true
+ }))
+ .unwrap();
+ assert!(fact.relative);
+ let plain: ExtractedFact = serde_json::from_value(serde_json::json!({
+ "subject": "Lease", "predicate": "expansion_option_deadline", "value": "2020-06-23"
+ }))
+ .unwrap();
+ assert!(!plain.relative, "没写就不是");
+ let msgs = build_messages(
+ &[],
+ &[],
+ &["lease.deadline (date)".into()],
+ None,
+ "a.txt",
+ &[],
+ "text",
+ );
+ assert!(msgs[0].content.contains("add \"relative\": true"));
+ }
+
fn rel(key: &str, description: &str, signature: &str) -> PromptRelation {
PromptRelation {
key: key.into(),
diff --git a/crates/utopia-extract/src/normalize.rs b/crates/utopia-extract/src/normalize.rs
index 399f374e..e26f9df7 100644
--- a/crates/utopia-extract/src/normalize.rs
+++ b/crates/utopia-extract/src/normalize.rs
@@ -225,6 +225,7 @@ fn value_fact(
quote: f.quote.clone(),
subject_span: f.subject_span.clone(),
object_span: None,
+ relative: false,
}
}
@@ -536,6 +537,7 @@ mod tests {
quote: Some(quote.into()),
subject_span: Some(subject.into()),
object_span: object.map(str::to_string),
+ relative: false,
}
}
fn valued(subject: &str, predicate: &str, value: &str, quote: &str) -> ExtractedFact {
diff --git a/crates/utopia-server/src/extraction.rs b/crates/utopia-server/src/extraction.rs
index 336d04b3..3089bf35 100644
--- a/crates/utopia-server/src/extraction.rs
+++ b/crates/utopia-server/src/extraction.rs
@@ -1726,7 +1726,11 @@ async fn run(state: &AppState, document_id: Uuid, proposer: Proposer) -> anyhow:
}
};
let datatype = attr.datatype.as_deref().unwrap_or("text");
- let Some(normalized) = utopia_extract::normalize_attr_value(datatype, &raw) else {
+ // 只相对一件事给出的日期(「触发日后 45 天」,#681 §4)照原文收下、带着标记:
+ // 它是新的状态值,时态引擎照常用它接替前一个截止日
+ let Some(mut object_value) =
+ utopia_extract::attr_object_value(datatype, &raw, f.relative)
+ else {
tracing::debug!(%document_id, attr = attr.key, ?raw, "属性值不合 datatype,跳过");
drop_signal(
state,
@@ -1739,7 +1743,6 @@ async fn run(state: &AppState, document_id: Uuid, proposer: Proposer) -> anyhow:
.await;
continue;
};
- let mut object_value = serde_json::json!({ "value": normalized });
// 单位随事实落笔:类型上的单位以后改了,旧值仍按记录时的单位读。
// 记哪个单位照 `unit_for`——从前这里无条件盖上声明的单位,实测
//「提供 500 兆瓦的风电」被模型记成金额,再盖上 ¥ 就成了 500 块钱
@@ -1791,11 +1794,11 @@ async fn run(state: &AppState, document_id: Uuid, proposer: Proposer) -> anyhow:
Some(f.predicate.as_str()),
)
.await?;
- if !created {
- continue;
+ if created {
+ fact_count += 1;
}
- fact_count += 1;
- // 单值属性 = functional:新值闭合旧值(属性历史由此而来)
+ // 单值属性 = functional:新值闭合旧值(属性历史由此而来)。并进已有断言的也对:
+ // 这份证据的日期可能更早,时间线的形状跟着变(#679)
if attr.functional && attr.temporal == "state" {
let report = utopia_store::temporal::reconcile_new_fact(
&state.pool,
@@ -2757,12 +2760,12 @@ async fn run(state: &AppState, document_id: Uuid, proposer: Proposer) -> anyhow:
Some(f.predicate.as_str()),
)
.await?;
- if !created {
- continue;
+ if created {
+ fact_count += 1;
}
- fact_count += 1;
// 时态对账:带唯一性约束的状态关系落新事实即检测矛盾(纯规则点查,
- // 自动闭合走"作废+改写",拿不准进 fact_conflicts 人裁)
+ // 自动闭合走"作废+改写",拿不准进 fact_conflicts 人裁)。并进已有断言的
+ // 也对:多了一份证据,时间线的形状可能跟着变(#679)
// 没有谓词就没有关系元数据,也就不参与时态对账——
// 一条说不出是什么关系的边,本来就不可能带唯一性约束
if let Some((pid, (func, inv_func, temporal))) =
diff --git a/crates/utopia-server/src/rdf.rs b/crates/utopia-server/src/rdf.rs
index 824d420e..512c5558 100644
--- a/crates/utopia-server/src/rdf.rs
+++ b/crates/utopia-server/src/rdf.rs
@@ -472,6 +472,10 @@ pub fn emit_fact(
if let Some(o) = &object {
sink.triple(TripleRef::new(stmt.as_ref(), rdf::OBJECT, o.as_ref()))?;
}
+ // 只相对一件事给出的值(「触发日后 45 天」,#681 §4):字面量是原文,这一行说它不是日期
+ if f.object_value.as_ref().is_some_and(is_relative) {
+ sink.l(&stmt, &utopia("relativeValue"), &flag(true))?;
+ }
emit_validity(
sink,
&stmt,
@@ -626,7 +630,13 @@ fn emit_validity(
Ok(())
}
-/// 属性事实的字面值。`{"value": …, "unit": …}` 或 `{"summary": …}`
+/// 值上带着 `"relative": true`:原文只相对一件事给出它,没有日历上的日期
+fn is_relative(v: &serde_json::Value) -> bool {
+ v.get("relative").and_then(|r| r.as_bool()) == Some(true)
+}
+
+/// 属性事实的字面值。`{"value": …, "unit": …}` 或 `{"summary": …}`。
+/// 相对的值写成普通字符串:`"45 days after the Trigger Date"^^xsd:date` 是个不合法的字面量
fn literal_value(v: &serde_json::Value, datatype: Option<&str>) -> Literal {
let raw = v.get("value").unwrap_or(v);
let as_text = match raw {
@@ -639,6 +649,7 @@ fn literal_value(v: &serde_json::Value, datatype: Option<&str>) -> Literal {
other => other.to_string(),
};
let ty: NamedNodeRef<'_> = match datatype {
+ _ if is_relative(v) => xsd::STRING,
Some("number") => xsd::DECIMAL,
Some("date") => xsd::DATE,
Some("bool") => xsd::BOOLEAN,
@@ -951,6 +962,36 @@ mod tests {
);
}
+ /// 日期属性上相对的值(#681 §4)导出成普通字符串,陈述上另有一行说它是相对的——
+ /// 写成 xsd:date 的字面量不合法,严格的解析器会整份拒收
+ #[test]
+ fn a_relative_deadline_is_a_string_that_says_it_is_relative() {
+ let dated = literal_value(&serde_json::json!({ "value": "2020-06-23" }), Some("date"));
+ assert_eq!(dated.datatype(), xsd::DATE);
+ let relative = literal_value(
+ &serde_json::json!({ "value": "45 days after the Trigger Date", "relative": true }),
+ Some("date"),
+ );
+ assert_eq!(relative.datatype(), xsd::STRING);
+ assert_eq!(relative.value(), "45 days after the Trigger Date");
+
+ let mut attr = fact(5);
+ attr.predicate_id = Some(id(4));
+ attr.object_id = None;
+ attr.object_value = Some(
+ serde_json::json!({ "value": "45 days after the Trigger Date", "relative": true }),
+ );
+ let quads = export(Format::Turtle, |sink, names, vocab| {
+ emit_fact(sink, names, vocab, &attr, at("2026-06-01T00:00:00Z")).unwrap();
+ });
+ assert!(has(
+ &quads,
+ STMT,
+ "urn:utopia:ns:relativeValue",
+ "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>"
+ ));
+ }
+
/// 业务规则的结论也要出现在导出里,而且宾语是**字面值**。
///
/// 这一条挡的是一次静默丢失:取数那边原本 `JOIN rules`,而业务规则的
diff --git a/crates/utopia-store/src/documents.rs b/crates/utopia-store/src/documents.rs
index 97b1d62f..6fb5e4fe 100644
--- a/crates/utopia-store/src/documents.rs
+++ b/crates/utopia-store/src/documents.rs
@@ -822,11 +822,15 @@ pub async fn delete(
.bind(id)
.fetch_all(&mut *tx)
.await?;
+ // 作废的事实可能是别的值的后任:它走了,关在它开始时的前任要重新接上;没作废、只是
+ // 少了这份证据的,排序用的日期也可能变。牵连的时间线先按固定顺序锁上,再作废任何一行
+ // (与撤回合并同一个顺序,见 temporal 模块头)
+ let (cited, timelines) = lock_cited_timelines(&mut tx, kb_id, id, &[]).await?;
// 先打了墓碑再算:这篇文档此刻已经算「已删除」,所以只剩它作出处的事实才落网;
// 另一篇活着的文档里也有证据的一条不动——删一份重复上传不该掀掉半张图
let facts: Vec<(Uuid,)> = sqlx::query_as(
"UPDATE facts f SET invalidated_at = now()
- WHERE f.kb_id = $1 AND f.invalidated_at IS NULL
+ WHERE f.kb_id = $1 AND f.invalidated_at IS NULL AND f.id = ANY($3)
AND EXISTS (SELECT 1 FROM fact_evidence fe
JOIN chunks c ON c.id = fe.chunk_id
WHERE fe.fact_id = f.id AND c.document_id = $2)
@@ -844,10 +848,13 @@ pub async fn delete(
)
.bind(kb_id)
.bind(id)
+ .bind(&cited)
.fetch_all(&mut *tx)
.await?;
let chunk_ids: Vec<Uuid> = chunks.into_iter().map(|(c,)| c).collect();
let fact_ids: Vec<Uuid> = facts.into_iter().map(|(f,)| f).collect();
+ reattest_tx(&mut tx, &cited).await?;
+ crate::temporal::tidy_timelines_tx(&mut tx, kb_id, &timelines).await?;
let deletion_id = Uuid::now_v7();
sqlx::query(
"INSERT INTO document_deletions
@@ -872,6 +879,77 @@ pub async fn delete(
})
}
+/// 证据文档变了(删了一篇、撤销了删除),把这些事实的「最早证据日期」按还在的文档重算。
+///
+/// 读路径拿它当没起点的事实从哪天起成立(`facts_holds_from`),引擎排时间线用的是同一个
+/// 日期(`temporal::DATED_AT`)。删掉最早那份文档之后引擎的锚点挪到了第二份,这里不跟着
+/// 挪的话,前任读到新锚点为止、它却还从旧日期读起,两段叠了一年(#679 第四轮评审)。
+/// 一份带日期的文档都不剩的,保留原值——那是别的来源(人写的)给的日期
+async fn reattest_tx(tx: &mut Transaction<'_, Postgres>, fact_ids: &[Uuid]) -> AppResult<()> {
+ if fact_ids.is_empty() {
+ return Ok(());
+ }
+ sqlx::query(
+ "UPDATE facts f SET attested_from = e.first
+ FROM (SELECT fe.fact_id, min(d.doc_time) AS first
+ FROM fact_evidence fe
+ JOIN chunks c ON c.id = fe.chunk_id
+ JOIN documents d ON d.id = c.document_id
+ WHERE fe.fact_id = ANY($1) AND d.deleted_at IS NULL AND d.doc_time IS NOT NULL
+ GROUP BY fe.fact_id) e
+ WHERE f.id = e.fact_id AND f.invalidated_at IS NULL
+ AND f.attested_from IS DISTINCT FROM e.first",
+ )
+ .bind(fact_ids)
+ .execute(&mut **tx)
+ .await?;
+ Ok(())
+}
+
+/// 引用这篇文档的现存事实,连同它们所在的唯一性时间线,锁上之后返回。
+///
+/// **锁上之后再读一遍**(#679 第三轮评审):等锁的时候,时间线重算可能把其中一行改写成了
+/// 新的一行,头一遍读到的是旧 id——拿旧名单去作废,新的那一行就活下来,出处却已经删了。
+/// 改写只在时间线的锁里发生,锁上之后名单不会再变;再读出来的行若落在没锁上的时间线上
+/// (撤回合并把它送回了源实体),把那几条也锁上
+///
+/// `also`:一并要锁的别的事实(撤销删除时要复活的那些)。**一次锁齐**:先锁引用的、
+/// 再锁复活的,分两轮拿锁的话,另一个按顺序拿同样两把锁的事务会和它互相等死
+async fn lock_cited_timelines(
+ tx: &mut Transaction<'_, Postgres>,
+ kb_id: Uuid,
+ document_id: Uuid,
+ also: &[Uuid],
+) -> AppResult<(Vec<Uuid>, Vec<crate::temporal::Timeline>)> {
+ let cited_sql = "SELECT DISTINCT f.id FROM facts f
+ JOIN fact_evidence fe ON fe.fact_id = f.id
+ JOIN chunks c ON c.id = fe.chunk_id
+ WHERE f.kb_id = $1 AND f.invalidated_at IS NULL AND c.document_id = $2";
+ let cited: Vec<Uuid> = sqlx::query_scalar(cited_sql)
+ .bind(kb_id)
+ .bind(document_id)
+ .fetch_all(&mut **tx)
+ .await?;
+ let first: Vec<Uuid> = cited.iter().chain(also).copied().collect();
+ let mut timelines = crate::temporal::timelines_of(&mut **tx, kb_id, &first, None).await?;
+ crate::temporal::lock_timelines(tx, kb_id, &timelines).await?;
+ let cited: Vec<Uuid> = sqlx::query_scalar(cited_sql)
+ .bind(kb_id)
+ .bind(document_id)
+ .fetch_all(&mut **tx)
+ .await?;
+ let late: Vec<_> = crate::temporal::timelines_of(&mut **tx, kb_id, &cited, None)
+ .await?
+ .into_iter()
+ .filter(|t| !timelines.contains(t))
+ .collect();
+ if !late.is_empty() {
+ crate::temporal::lock_timelines(tx, kb_id, &late).await?;
+ timelines.extend(late);
+ }
+ Ok((cited, timelines))
+}
+
/// 撤销一次删除:文档、这次打标的分块、这次作废的事实原路复活,形状照 `revert_merge`。
///
/// 只救 `document_deletions` 名单上的——更早版本的旧分块、删除之前就作废的事实
@@ -931,6 +1009,9 @@ async fn restore_tx(
.bind(&chunk_ids)
.execute(&mut **tx)
.await?;
+ // 复活的事实回到各自的时间线上;一直引用着这篇文档的事实,排序用的日期也回来了。
+ // 先锁、再复活、再重算(同删除)
+ let (cited, timelines) = lock_cited_timelines(tx, kb_id, id, &fact_ids).await?;
sqlx::query(
"UPDATE facts SET invalidated_at = NULL
WHERE id = ANY($1) AND invalidated_at IS NOT NULL",
@@ -938,6 +1019,9 @@ async fn restore_tx(
.bind(&fact_ids)
.execute(&mut **tx)
.await?;
+ let touched: Vec<Uuid> = cited.iter().chain(&fact_ids).copied().collect();
+ reattest_tx(tx, &touched).await?;
+ crate::temporal::tidy_timelines_tx(tx, kb_id, &timelines).await?;
sqlx::query("UPDATE document_deletions SET reverted_at = now() WHERE id = $1")
.bind(deletion_id)
.execute(&mut **tx)
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index b3a3c56a..d3230cdf 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -494,12 +494,18 @@ async fn insert_fact_inner(
}
}
// 已经关上的(结束了不知哪天)再听到一次「结束了」:同一件事,复用那一行。
- // 锚点只往早挪——更早的文档说它结束了,它就结束得更早
+ // 锚点只往早挪——更早的文档说它结束了,它就结束得更早。那一行的终点若是引擎推的,
+ // 现在原文说出来了:改成写明的,此后不随时间线重算(#679 第三轮评审)
if let Some((ended, _, _, _)) = same.iter().find(|(_, vf, vt, vtp)| {
vt.is_none()
&& vtp.as_deref() == Some(ENDED_UNKNOWN)
&& validity.from.is_none_or(|f| Some(f) == *vf)
}) {
+ if let Some(stated) =
+ crate::temporal::state_derived_end(pool, *ended, None, validity.attested_at).await?
+ {
+ return Ok((stated, true));
+ }
attest_earlier(pool, *ended, validity.attested_at).await?;
return Ok((*ended, false));
}
@@ -511,8 +517,19 @@ async fn insert_fact_inner(
属性随行),与 #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)) {
+ let precision = validity.to_precision.unwrap_or("day");
+ if let Some(stated) = crate::temporal::state_derived_end(
+ pool,
+ *ended,
+ Some((to, precision)),
+ validity.attested_at,
+ )
+ .await?
+ {
+ return Ok((stated, true));
+ }
attest_earlier(pool, *ended, validity.attested_at).await?;
return Ok((*ended, false));
}
@@ -553,6 +570,18 @@ async fn insert_fact_inner(
}
}
}
+ // 那行已经关上,这次观察也说了终点:终点若是引擎推的,改成原文说的
+ if temporal == Temporal::State && (vt.is_some() || vtp.is_some()) && validity.has_ended() {
+ let stated_to = validity
+ .to
+ .map(|to| (to, validity.to_precision.unwrap_or("day")));
+ if let Some(stated) =
+ crate::temporal::state_derived_end(pool, *existing, stated_to, validity.attested_at)
+ .await?
+ {
+ return Ok((stated, true));
+ }
+ }
attest_earlier(pool, *existing, validity.attested_at).await?;
return Ok((*existing, false));
}
@@ -753,18 +782,47 @@ pub async fn add_evidence(
// 冲突时补写表层谓词而非整行跳过:重抽命中的多是已有的 (事实, 分块) 对,
// DO NOTHING 会让存量证据永远填不上这一列。只在原值为空时补,不覆盖——
// 同一分块的同一条事实,第一次记下的说法就是它的说法
+ //
+ // **证据落在活着的那一行上**(#679 第三轮评审)。落库到写证据之间,时间线重算可能已经
+ // 把这一行改写掉(换了终点、换了 id):改写时复制的证据里没有这一条,写在旧行上就丢了。
+ // 先 `FOR SHARE` 锁住这一行——正在改写它的事务持着 `FOR UPDATE`,这里等它提交;
+ // 等到的若已作废,顺着 supersedes 走到它改写出来的那一行。被驳回、没有后继的,证据仍记在它身上
+ let mut tx = pool.begin().await?;
+ let mut target = fact_id;
+ loop {
+ let live: Option<bool> =
+ sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id = $1 FOR SHARE")
+ .bind(target)
+ .fetch_optional(&mut *tx)
+ .await?;
+ if live != Some(false) {
+ break;
+ }
+ let next: Option<Uuid> = sqlx::query_scalar(
+ "SELECT id FROM facts WHERE supersedes = $1
+ ORDER BY invalidated_at IS NULL DESC, recorded_at DESC LIMIT 1",
+ )
+ .bind(target)
+ .fetch_optional(&mut *tx)
+ .await?;
+ match next {
+ Some(next) => target = next,
+ None => break,
+ }
+ }
sqlx::query(
"INSERT INTO fact_evidence (fact_id, chunk_id, quote, proposed_predicate, document_id, doc_version)
SELECT $1, $2, $3, left($4, 120), c.document_id, c.doc_version FROM chunks c WHERE c.id = $2
ON CONFLICT (fact_id, chunk_id) DO UPDATE
SET proposed_predicate = COALESCE(fact_evidence.proposed_predicate, EXCLUDED.proposed_predicate)",
)
- .bind(fact_id)
+ .bind(target)
.bind(chunk_id)
.bind(quote)
.bind(proposed)
- .execute(pool)
+ .execute(&mut *tx)
.await?;
+ tx.commit().await?;
Ok(())
}
@@ -1449,15 +1507,7 @@ pub async fn confirm_fact(pool: &PgPool, kb_id: Uuid, fact_id: Uuid) -> AppResul
/// 人工否决事实:作废(账本 append-only,不 DELETE)。
pub async fn reject_fact(pool: &PgPool, kb_id: Uuid, fact_id: Uuid) -> AppResult<()> {
- let res = sqlx::query(
- "UPDATE facts SET invalidated_at = now()
- WHERE id = $1 AND kb_id = $2 AND invalidated_at IS NULL",
- )
- .bind(fact_id)
- .bind(kb_id)
- .execute(pool)
- .await?;
- if res.rows_affected() == 0 {
+ if !crate::temporal::retract(pool, kb_id, fact_id).await? {
return Err(AppError::NotFound);
}
Ok(())
@@ -2111,11 +2161,11 @@ async fn adopt(
"INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, object_value,
valid_from, valid_from_precision,
valid_to, valid_to_precision, confidence, supersedes,
- attested_from, attested_to)
+ attested_from, attested_to, end_derived)
SELECT $1, kb_id, $6, $3, $4, $5,
valid_from, valid_from_precision,
valid_to, valid_to_precision, confidence, id,
- attested_from, attested_to
+ attested_from, attested_to, end_derived
FROM facts WHERE id = $2 AND invalidated_at IS NULL
RETURNING id",
)
diff --git a/crates/utopia-store/src/pending.rs b/crates/utopia-store/src/pending.rs
index 36510292..0c746fe5 100644
--- a/crates/utopia-store/src/pending.rs
+++ b/crates/utopia-store/src/pending.rs
@@ -286,42 +286,41 @@ pub async fn confirm(pool: &PgPool, kb_id: Uuid, id: Uuid) -> AppResult<Confirme
.await?;
// 与抽取相同的时态对账:带唯一性约束的状态关系,新事实闭合旧事实。
- // 这正是记忆该有的行为——「Mira 交给 Devin 了」说完,Mira 那条就该闭合
+ // 这正是记忆该有的行为——「Mira 交给 Devin 了」说完,Mira 那条就该闭合。
+ // 并进已有断言的也对:多了一次观察,时间线的形状可能跟着变(#679)
let mut conflicts = 0u32;
- if created {
- if let Some(pid) = v.predicate_id {
- let meta: Option<(bool, bool, String)> = sqlx::query_as(
- "SELECT functional, inverse_functional, temporal FROM relation_types WHERE id = $1",
- )
- .bind(pid)
- .fetch_optional(pool)
- .await?;
- if let Some((functional, inverse_functional, temporal)) = meta {
- if temporal == "state" {
- let mut directions = Vec::new();
- if functional {
- directions.push(crate::temporal::Uniqueness::SubjectSide);
- }
- // 宾语侧唯一只对实体宾语有意义;字面值没有「谁被指着」
- if inverse_functional && v.object_id.is_some() {
- directions.push(crate::temporal::Uniqueness::ObjectSide);
- }
- for dir in directions {
- let report = crate::temporal::reconcile_new_fact(
- pool,
- kb_id,
- fact_id,
- v.subject_id,
- pid,
- v.object_id,
- v.object_value.as_ref(),
- dir,
- validity,
- v.confidence,
- )
- .await?;
- conflicts += report.conflicts;
- }
+ if let Some(pid) = v.predicate_id {
+ let meta: Option<(bool, bool, String)> = sqlx::query_as(
+ "SELECT functional, inverse_functional, temporal FROM relation_types WHERE id = $1",
+ )
+ .bind(pid)
+ .fetch_optional(pool)
+ .await?;
+ if let Some((functional, inverse_functional, temporal)) = meta {
+ if temporal == "state" {
+ let mut directions = Vec::new();
+ if functional {
+ directions.push(crate::temporal::Uniqueness::SubjectSide);
+ }
+ // 宾语侧唯一只对实体宾语有意义;字面值没有「谁被指着」
+ if inverse_functional && v.object_id.is_some() {
+ directions.push(crate::temporal::Uniqueness::ObjectSide);
+ }
+ for dir in directions {
+ let report = crate::temporal::reconcile_new_fact(
+ pool,
+ kb_id,
+ fact_id,
+ v.subject_id,
+ pid,
+ v.object_id,
+ v.object_value.as_ref(),
+ dir,
+ validity,
+ v.confidence,
+ )
+ .await?;
+ conflicts += report.conflicts;
}
}
}
diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rs
index 52393878..38f0e5de 100644
--- a/crates/utopia-store/src/resolution.rs
+++ b/crates/utopia-store/src/resolution.rs
@@ -1716,6 +1716,20 @@ pub async fn merge_entities(
let mut tx = pool.begin().await?;
+ // 搬动会牵连的时间线先按固定顺序锁上,再改任何一行(撤回合并同一个顺序,见 temporal
+ // 模块头):不锁的话,合并与撤回、合并与落库对账会各拿一半行锁互相等
+ let moving: Vec<Uuid> = sqlx::query_scalar(
+ "SELECT id FROM facts WHERE kb_id = $1 AND (subject_id = $2 OR object_id = $2)",
+ )
+ .bind(kb_id)
+ .bind(source_id)
+ .fetch_all(&mut *tx)
+ .await?;
+ let timelines =
+ crate::temporal::timelines_of(&mut *tx, kb_id, &moving, Some((&[source_id], target_id)))
+ .await?;
+ crate::temporal::lock_timelines(&mut tx, kb_id, &timelines).await?;
+
// 互指事实(合并后变自环)→ 作废
let cross: Vec<(Uuid,)> = sqlx::query_as(
"UPDATE facts SET invalidated_at = now()
@@ -1905,9 +1919,9 @@ pub async fn merge_entities(
// 搬移后的时态对账:换了主/宾的事实等价于新观察落库——两个对象折成一个后,
// 唯一性不变量才第一次看得到旧开放区间与继任者相撞(如"星尘"并入"星尘项目",
// 旧负责人的 leads 应在新任起点闭合)。
- // 修正行 id 记入合并账本:这些修正的唯一成因是本次合并,回滚时必须随之撤销。
- // 注:本步在事务外,失败有自愈性——残留的旧开放行会在下一条相关新事实落库时
- // 被常规插入对账撞到并闭合。
+ // 修正行 id 记入合并账本,供审计。回滚不靠它:回滚按搬走之后剩下的行重算时间线,
+ // 这些引擎画的终点自然跟着变(0057)。
+ // 注:本步在事务外,失败有自愈性——这条时间线下一次有事实落库时整条重算。
let moved_all: Vec<Uuid> = moved_subject
.iter()
.chain(moved_object.iter())
@@ -1954,18 +1968,47 @@ struct MergeRow {
moved_subject_facts: Vec<Uuid>,
moved_object_facts: Vec<Uuid>,
invalidated_facts: Vec<Uuid>,
- temporal_corrections: Vec<Uuid>,
target_profile_before: Option<Vector>,
target_profile_n_before: i32,
target_type_before: Option<Uuid>,
reverted_at: Option<DateTime<Utc>>,
}
+/// 搬过的事实,连同从它们改写出来的每一行(顺着 supersedes 往下走到底,作废的也算)
+async fn with_rewrites(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ roots: &[Uuid],
+) -> AppResult<Vec<Uuid>> {
+ Ok(sqlx::query_scalar(
+ "WITH RECURSIVE chain(id) AS (
+ SELECT unnest($1::uuid[])
+ UNION
+ SELECT f.id FROM facts f JOIN chain c ON f.supersedes = c.id
+ )
+ SELECT id FROM chain",
+ )
+ .bind(roots)
+ .fetch_all(&mut **tx)
+ .await?)
+}
+
/// 精确回滚一次合并:事实原路搬回、作废撤销、target 画像与类型恢复快照、source 复活。
+///
+/// **搬回去的是搬过来的事实连同从它们改写出来的每一行。** 合并之后引擎把它关上过、人改过
+/// 它的区间、人驳回过它——那些行都是它的,跟着回源实体,各自保持原样:人改的区间还在,
+/// 驳回的仍是驳回的。合并当时引擎做的闭合不单独撤:两边的时间线在搬完之后按剩下的行重算,
+/// 边界随搬走的行走了,关在那里的行自然重新打开,写明的终点不动(0057)。
+///
+/// 怎么搬分两种,为的是记录轴回放合并窗口时仍答得对(0027 的 `fact_owner_at` 只认账本):
+/// 账本上的行原地改回主语/宾语;合并之后才改写出来的行不在账本上,活着的作废、在源实体上
+/// 另起一行接着它,作废了的留在目标实体上——它们在那段窗口里确实挂在那里
+///
+/// 搬动之前先按固定顺序拿下两边所有牵连时间线的锁(见 temporal 模块头):落库对账先拿锁
+/// 再锁行,这里要是先改行再拿锁,两边会互相等死
pub async fn revert_merge(pool: &PgPool, kb_id: Uuid, merge_id: Uuid) -> AppResult<()> {
let m: MergeRow = sqlx::query_as(
"SELECT source_id, target_id, moved_subject_facts, moved_object_facts,
- invalidated_facts, temporal_corrections, target_profile_before,
+ invalidated_facts, target_profile_before,
target_profile_n_before, target_type_before, reverted_at
FROM entity_merges WHERE id = $1 AND kb_id = $2",
)
@@ -1979,39 +2022,73 @@ pub async fn revert_merge(pool: &PgPool, kb_id: Uuid, merge_id: Uuid) -> AppResu
}
let mut tx = pool.begin().await?;
- sqlx::query("UPDATE facts SET subject_id = $1 WHERE id = ANY($2)")
+ // 事实此刻挂在谁身上:目标实体,或者目标后来又并进去的实体(S 并进 T、T 再并进 C,
+ // 撤回 S→T 时 S 的事实在 C 身上)。只认目标的话,连环合并里撤回头一环,源实体
+ // 复活了、它的事实却留在链尾(#679 第四轮评审)
+ let holders: Vec<Uuid> = sqlx::query_scalar(
+ "WITH RECURSIVE chain(id) AS (
+ SELECT $1::uuid
+ UNION SELECT e.merged_into FROM entities e JOIN chain ON e.id = chain.id
+ WHERE e.merged_into IS NOT NULL)
+ SELECT id FROM chain",
+ )
+ .bind(m.target_id)
+ .fetch_all(&mut *tx)
+ .await?;
+ let touched: Vec<Uuid> = with_rewrites(&mut tx, &m.moved_subject_facts)
+ .await?
+ .into_iter()
+ .chain(with_rewrites(&mut tx, &m.moved_object_facts).await?)
+ .chain(m.invalidated_facts.iter().copied())
+ .collect();
+ let timelines =
+ crate::temporal::timelines_of(&mut *tx, kb_id, &touched, Some((&holders, m.source_id)))
+ .await?;
+ crate::temporal::lock_timelines(&mut tx, kb_id, &timelines).await?;
+ // 锁上之后再走一遍:等锁的时候,引擎可能刚从它们改写出新的一行
+ let subject_rows = with_rewrites(&mut tx, &m.moved_subject_facts).await?;
+ let object_rows = with_rewrites(&mut tx, &m.moved_object_facts).await?;
+
+ sqlx::query("UPDATE facts SET subject_id = $1 WHERE id = ANY($2) AND subject_id = ANY($3)")
.bind(m.source_id)
.bind(&m.moved_subject_facts)
+ .bind(&holders)
.execute(&mut *tx)
.await?;
- sqlx::query("UPDATE facts SET object_id = $1 WHERE id = ANY($2)")
+ sqlx::query("UPDATE facts SET object_id = $1 WHERE id = ANY($2) AND object_id = ANY($3)")
.bind(m.source_id)
.bind(&m.moved_object_facts)
+ .bind(&holders)
.execute(&mut *tx)
.await?;
+ for (rows, ledger, on_object) in [
+ (&subject_rows, &m.moved_subject_facts, false),
+ (&object_rows, &m.moved_object_facts, true),
+ ] {
+ let holder = if on_object { "object_id" } else { "subject_id" };
+ let live: Vec<Uuid> = sqlx::query_scalar(&format!(
+ "SELECT id FROM facts
+ WHERE id = ANY($1) AND id <> ALL($2) AND invalidated_at IS NULL AND {holder} = ANY($3)"
+ ))
+ .bind(rows)
+ .bind(ledger)
+ .bind(&holders)
+ .fetch_all(&mut *tx)
+ .await?;
+ for id in live {
+ let (subject, object) = if on_object {
+ (None, Some(m.source_id))
+ } else {
+ (Some(m.source_id), None)
+ };
+ crate::temporal::rehome_tx(&mut tx, id, subject, object).await?;
+ }
+ }
sqlx::query("UPDATE facts SET invalidated_at = NULL WHERE id = ANY($1)")
.bind(&m.invalidated_facts)
.execute(&mut *tx)
.await?;
-
- // 合并引发的时态修正随之撤销:这些修正的唯一成因是本次合并(两实体折一后
- // 不变量才看到的相撞),成因既撤、修正随撤——先恢复被取代的原行,再作废修正行。
- // 只撤仍存活的修正:之后被真实新观察再度改写过的链保持不动(那部分有独立依据)。
- sqlx::query(
- "UPDATE facts SET invalidated_at = NULL WHERE id IN (
- SELECT supersedes FROM facts
- WHERE id = ANY($1) AND invalidated_at IS NULL AND supersedes IS NOT NULL)",
- )
- .bind(&m.temporal_corrections)
- .execute(&mut *tx)
- .await?;
- sqlx::query(
- "UPDATE facts SET invalidated_at = now()
- WHERE id = ANY($1) AND invalidated_at IS NULL",
- )
- .bind(&m.temporal_corrections)
- .execute(&mut *tx)
- .await?;
+ crate::temporal::tidy_timelines_tx(&mut tx, kb_id, &timelines).await?;
let source = entity_full(pool, kb_id, m.source_id).await?;
// source 的名字是它的名字事实,已经跟着 moved_subject_facts 搬回去了
diff --git a/crates/utopia-store/src/temporal.rs b/crates/utopia-store/src/temporal.rs
index a3e6f94f..0cfedad3 100644
--- a/crates/utopia-store/src/temporal.rs
+++ b/crates/utopia-store/src/temporal.rs
@@ -4,49 +4,374 @@
//! - 纯规则判定,零 LLM——模糊性已在上游(消解归并实体、本体标 functional)消化
//! - 闭合走"作废 + 改写"而非原地改:旧断言 invalidated_at 记下"何时被修正",
//! 修正行闭合区间并以 supersedes 链回旧行——"以当时的认知回放当时"得以成立
-//! - 闭合点只用世界时间(新事实的 valid_from),绝不用摄取时刻顶替
+//! - 闭合点只用世界时间:后任的 valid_from;后任没有起点时,前任写成「结束了,不知哪天」,
+//! 锚在后任最早那份自带日期的证据上(0022 的形状,#681 §1)。文档日期从不写进日期列
//! - 拿不准(缺时间/同时开始/低置信)绝不硬闭合,进 fact_conflicts 由人裁决
+//!
+//! **一条时间线是一个整体。** 同一 (库, 持有者, 谓词, 唯一性方向) 的所有现存行按时间排成
+//! 一列。一行的终点要么是原文或人写明的,从不重算;要么由引擎推出(`facts.end_derived`,
+//! 0057):开着的行、引擎关上的行,都止于它之后最近的另一个值开始时。事实落库或又被观察到、
+//! 合并搬来或撤回、文档删除或恢复,都把这一列按当下有哪些行**一次**重算到这个形状——结果
+//! 只取决于有哪些行,与它们到达的先后无关(#679)。
+//!
+//! 一条关系两侧都唯一(functional 且 inverse functional,「一个人同时只领导一个项目,一个
+//! 项目同时只有一个领导」)时,一行同时在两条时间线上:它的终点是两侧各推一个、取早的那个。
+//!
+//! 重算在一个事务里、持着这条时间线的咨询锁做完。一次要动几条时间线的事务(撤回合并、删除
+//! 文档)先按固定顺序把这些锁全拿到,再改任何一行:落库对账也是先拿锁再锁行,两边顺序一致,
+//! 谁也不会拿着行锁去等咨询锁。两侧都唯一的关系一条谓词一把锁——重算一侧要读另一侧。
+
+use std::collections::{BTreeSet, HashMap, HashSet};
use chrono::{DateTime, Utc};
-use sqlx::PgPool;
+use sqlx::{PgPool, Postgres, Transaction};
use utopia_core::models::ConflictView;
use utopia_core::AppResult;
use uuid::Uuid;
-/// 低于此置信度的新事实不允许自动改写历史(进审)。
+/// 低于此置信度的后任不允许自动改写前任的历史(进审)。
const AUTO_CLOSE_MIN_CONFIDENCE: f32 = 0.75;
-/// 命中的开放期事实(不变量下至多一条,引擎上线前的历史脏数据可能多条)。
-#[derive(Debug, sqlx::FromRow)]
-struct OpenFact {
- id: Uuid,
- valid_from: Option<DateTime<Utc>>,
- /// 闭合别人的区间时要用它当那个时刻的粒度
- valid_from_precision: Option<String>,
-}
+/// 证据文件**自带**的最早日期,按 `facts` 的别名 `f` 投影。只认正文(`content`)与来源
+/// (`source`)给的日期:上传时刻、文件修改时间不是文档自己的日期——拿它们排序,
+/// 每条没起点的旧行都会被读成「此刻还在」。删掉的文档不再作证
+const DATED_AT: &str = "(SELECT min(d.doc_time) FROM fact_evidence fe
+ JOIN documents d ON d.id = fe.document_id
+ WHERE fe.fact_id = f.id AND d.doc_time IS NOT NULL
+ AND d.deleted_at IS NULL
+ AND d.doc_time_source IN ('content', 'source'))";
/// 唯一性方向:functional = 主语侧(张三同时只 reports_to 一人);
/// inverse functional = 宾语侧(一个项目同时只有一个 leads 它的人)。
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Uniqueness {
SubjectSide,
ObjectSide,
}
-/// 对账结果:自动闭合产生的修正行 id(调用方按需记账,如合并回滚要撤销它们)
-/// 与进入人审的冲突数。
+impl Uniqueness {
+ fn other(self) -> Self {
+ match self {
+ Self::SubjectSide => Self::ObjectSide,
+ Self::ObjectSide => Self::SubjectSide,
+ }
+ }
+}
+
+/// 一条唯一性时间线:谁(持有者)在哪个谓词、哪个方向上同时只能有一个值
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub struct Timeline {
+ pub holder: Uuid,
+ pub predicate_id: Uuid,
+ pub side: Uniqueness,
+ /// 谓词两侧都唯一:这条线上的每一行也在另一侧的一条线上
+ pub both_sides: bool,
+}
+
+impl Timeline {
+ fn lock_key(&self, kb_id: Uuid) -> String {
+ if self.both_sides {
+ format!("timeline:{kb_id}:{}:both", self.predicate_id)
+ } else {
+ format!(
+ "timeline:{kb_id}:{}:{}:{:?}",
+ self.holder, self.predicate_id, self.side
+ )
+ }
+ }
+}
+
+/// 对账结果:自动闭合产生的修正行 id(调用方按需记账)与这次新进人审的冲突数。
#[derive(Debug, Default)]
pub struct ReconcileReport {
pub corrected: Vec<Uuid>,
pub conflicts: u32,
}
-/// 一条新 state 事实落库后、沿指定唯一性方向的对账。调用方负责判断关系确实
-/// 带该方向的唯一性且 temporal = state(本体元数据在抽取任务里已加载)。
+/// 时间线上的一行
+#[derive(Debug, Clone, sqlx::FromRow)]
+struct Row {
+ id: Uuid,
+ subject_id: Uuid,
+ object_id: Option<Uuid>,
+ object_value: Option<serde_json::Value>,
+ valid_from: Option<DateTime<Utc>>,
+ valid_from_precision: Option<String>,
+ valid_to: Option<DateTime<Utc>>,
+ valid_to_precision: Option<String>,
+ attested_to: Option<DateTime<Utc>>,
+ confidence: f32,
+ end_derived: bool,
+ dated_at: Option<DateTime<Utc>>,
+}
+
+/// 一行的终点
+#[derive(Debug, Clone, PartialEq)]
+enum End {
+ /// 仍在持续
+ Open,
+ /// 在这一刻结束,带精度
+ At(DateTime<Utc>, String),
+ /// 结束了,不知哪天;读出侧读到锚点为止
+ Unknown(Option<DateTime<Utc>>),
+}
+
+impl End {
+ /// 读出来止于哪一刻;开着的没有
+ fn instant(&self) -> Option<DateTime<Utc>> {
+ match self {
+ End::Open => None,
+ End::At(at, _) => Some(*at),
+ End::Unknown(anchor) => *anchor,
+ }
+ }
+
+ /// 两个终点里早的那个;同一刻时写着日期的胜过锚点
+ fn earlier(self, other: End) -> End {
+ match (self.instant(), other.instant()) {
+ (None, _) => other,
+ (_, None) => self,
+ (Some(a), Some(b)) if b < a => other,
+ (Some(a), Some(b)) if a == b && matches!(other, End::At(..)) => other,
+ _ => self,
+ }
+ }
+}
+
+impl Row {
+ /// 排序用的时刻:起点。没有起点时,最早那份自带日期的证据——但只对还开着、或者由引擎
+ /// 关上的行成立:它们的证据说那天还成立。原文说已经结束的行,证据的日期只说明「那天
+ /// 之前结束了」,拿它排序会让一个早就结束的值去关上当下的值(#679 第三轮评审)
+ fn key(&self) -> Option<DateTime<Utc>> {
+ match self.valid_from {
+ Some(start) => Some(start),
+ None if self.is_open() || self.end_derived => self.dated_at,
+ None => None,
+ }
+ }
+ fn is_open(&self) -> bool {
+ self.valid_to.is_none() && self.valid_to_precision.is_none()
+ }
+ /// 终点由引擎按时间线定:开着的,或引擎关上的
+ fn recomputable(&self) -> bool {
+ self.is_open() || self.end_derived
+ }
+ /// 终点的时刻:日期;「结束了,不知哪天」的锚点(读出侧同样读到它为止)
+ fn end(&self) -> Option<DateTime<Utc>> {
+ match (self.valid_to, self.valid_to_precision.as_deref()) {
+ (Some(to), _) => Some(to),
+ (None, Some(crate::graph::ENDED_UNKNOWN)) => self.attested_to,
+ _ => None,
+ }
+ }
+ /// 眼下写着的终点
+ fn current_end(&self) -> End {
+ match (self.valid_to, self.valid_to_precision.as_deref()) {
+ (Some(to), p) => End::At(to, p.unwrap_or("day").to_string()),
+ (None, Some(crate::graph::ENDED_UNKNOWN)) => End::Unknown(self.attested_to),
+ _ => End::Open,
+ }
+ }
+ /// 前一段止于这一行开始时,终点写成什么:有起点就是那一刻;没有起点,是「结束了,
+ /// 不知哪天」,锚在这一行自带日期的证据上
+ fn end_before(&self) -> End {
+ match self.valid_from {
+ Some(start) => {
+ let precision = self.valid_from_precision.as_deref().unwrap_or("day");
+ End::At(
+ crate::graph::truncate_to(start, Some(precision)),
+ precision.to_string(),
+ )
+ }
+ None => End::Unknown(self.dated_at),
+ }
+ }
+ /// 在 `at` 这一刻还成立
+ fn holds_at(&self, at: DateTime<Utc>) -> bool {
+ self.is_open() || self.end().is_some_and(|end| end > at)
+ }
+ /// 这一行在 `side` 那一侧的持有者
+ fn holder(&self, side: Uniqueness) -> Option<Uuid> {
+ match side {
+ Uniqueness::SubjectSide => Some(self.subject_id),
+ Uniqueness::ObjectSide => self.object_id,
+ }
+ }
+}
+
+/// 两行说的是不是同一个值(同一侧:主语侧比宾语,宾语侧比主语)
+fn same_value(side: Uniqueness, a: &Row, b: &Row) -> bool {
+ match side {
+ Uniqueness::SubjectSide => a.object_id == b.object_id && a.object_value == b.object_value,
+ Uniqueness::ObjectSide => a.subject_id == b.subject_id,
+ }
+}
+
+async fn lock_timeline(
+ tx: &mut Transaction<'_, Postgres>,
+ kb_id: Uuid,
+ timeline: Timeline,
+) -> AppResult<()> {
+ lock_timelines(tx, kb_id, &[timeline]).await
+}
+
+/// 一次锁的时间线超过这么多条,改拿谓词一级的排他锁(见 [`lock_timelines`])
+const BULK_TIMELINES: usize = 256;
+
+/// 拿下几条时间线的咨询锁,**按固定顺序**。一次要动多条时间线的事务都走这里:
+/// 大家按同一个顺序排队,谁也不会拿着一条去等另一条。同一事务里重复拿同一把锁无妨。
+///
+/// **锁分两级。** 平常先拿谓词一级的共享锁,再拿时间线一级的排他锁——同一个谓词上
+/// 不同持有者的时间线互不相干,照旧并行。一次要锁的时间线太多时(删一篇给几千个实体
+/// 各记了一个属性的表格),改拿这些谓词的排他锁、不再逐条锁:逐条锁时 8000 条要拿
+/// 8000 把锁,两万条时 Postgres 的锁表(`max_locks_per_transaction`)直接装不下,
+/// 文档就删不掉了(#679 第四轮评审)。谓词锁在前、时间线锁在后,各自排序,
+/// 两级之间不会反着等
+pub async fn lock_timelines(
+ tx: &mut Transaction<'_, Postgres>,
+ kb_id: Uuid,
+ timelines: &[Timeline],
+) -> AppResult<()> {
+ if timelines.is_empty() {
+ return Ok(());
+ }
+ let mut predicates: Vec<String> = timelines
+ .iter()
+ .map(|t| format!("predicate:{kb_id}:{}", t.predicate_id))
+ .collect();
+ predicates.sort();
+ predicates.dedup();
+ let bulk = timelines.len() > BULK_TIMELINES;
+ let predicate_lock = if bulk {
+ "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))"
+ } else {
+ "SELECT pg_advisory_xact_lock_shared(hashtextextended($1, 0))"
+ };
+ for key in predicates {
+ sqlx::query(predicate_lock)
+ .bind(key)
+ .execute(&mut **tx)
+ .await?;
+ }
+ if bulk {
+ return Ok(());
+ }
+ let mut keys: Vec<String> = timelines.iter().map(|t| t.lock_key(kb_id)).collect();
+ keys.sort();
+ keys.dedup();
+ for key in keys {
+ sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))")
+ .bind(key)
+ .execute(&mut **tx)
+ .await?;
+ }
+ Ok(())
+}
+
+async fn load_timeline(
+ tx: &mut Transaction<'_, Postgres>,
+ kb_id: Uuid,
+ timeline: Timeline,
+) -> AppResult<Vec<Row>> {
+ let holder_column = match timeline.side {
+ Uniqueness::SubjectSide => "f.subject_id",
+ Uniqueness::ObjectSide => "f.object_id",
+ };
+ let rows = sqlx::query_as(&format!(
+ "SELECT f.id, f.subject_id, f.object_id, f.object_value,
+ f.valid_from, f.valid_from_precision, f.valid_to, f.valid_to_precision,
+ f.attested_to, f.confidence, f.end_derived, {DATED_AT} AS dated_at
+ FROM facts f
+ WHERE f.kb_id = $1 AND {holder_column} = $2 AND f.predicate_id = $3
+ AND f.invalidated_at IS NULL
+ ORDER BY f.recorded_at
+ FOR UPDATE OF f"
+ ))
+ .bind(kb_id)
+ .bind(timeline.holder)
+ .bind(timeline.predicate_id)
+ .fetch_all(&mut **tx)
+ .await?;
+ Ok(rows)
+}
+
+/// 这些事实所在的全部唯一性时间线(只算 temporal = state、声明了唯一性的谓词)。
+///
+/// `swap = (from, to)`:主语或宾语是 `from` 的事实,换成 `to` 之后落在的那条也算上——
+/// 撤回合并要在搬动任何一行之前,把搬走前、搬回后两边的锁都拿到
+pub async fn timelines_of<'e, E>(
+ executor: E,
+ kb_id: Uuid,
+ fact_ids: &[Uuid],
+ // 这些持有者上的时间线,另记一份换到后面那个实体上(合并、撤回合并时两边都要锁)
+ swap: Option<(&[Uuid], Uuid)>,
+) -> AppResult<Vec<Timeline>>
+where
+ E: sqlx::PgExecutor<'e>,
+{
+ #[derive(sqlx::FromRow)]
+ struct Placed {
+ subject_id: Uuid,
+ object_id: Option<Uuid>,
+ predicate_id: Uuid,
+ functional: bool,
+ inverse_functional: bool,
+ }
+ if fact_ids.is_empty() {
+ return Ok(Vec::new());
+ }
+ let placed: Vec<Placed> = sqlx::query_as(
+ "SELECT DISTINCT f.subject_id, f.object_id, f.predicate_id, r.functional, r.inverse_functional
+ FROM facts f JOIN relation_types r ON r.id = f.predicate_id
+ WHERE f.kb_id = $1 AND f.id = ANY($2)
+ AND r.temporal = 'state' AND (r.functional OR r.inverse_functional)",
+ )
+ .bind(kb_id)
+ .bind(fact_ids)
+ .fetch_all(executor)
+ .await?;
+ let swapped = |id: Uuid| {
+ swap.filter(|(from, _)| from.contains(&id))
+ .map(|(_, to)| to)
+ };
+ let mut seen = HashSet::new();
+ let mut out = Vec::new();
+ for p in placed {
+ let both_sides = p.functional && p.inverse_functional;
+ let mut holders = Vec::new();
+ if p.functional {
+ holders.push((p.subject_id, Uniqueness::SubjectSide));
+ holders.extend(swapped(p.subject_id).map(|to| (to, Uniqueness::SubjectSide)));
+ }
+ // 宾语侧唯一性只对实体宾语有意义(字面值不"被占用")
+ if let (true, Some(object)) = (p.inverse_functional, p.object_id) {
+ holders.push((object, Uniqueness::ObjectSide));
+ holders.extend(swapped(object).map(|to| (to, Uniqueness::ObjectSide)));
+ }
+ for (holder, side) in holders {
+ let timeline = Timeline {
+ holder,
+ predicate_id: p.predicate_id,
+ side,
+ both_sides,
+ };
+ if seen.insert(timeline) {
+ out.push(timeline);
+ }
+ }
+ }
+ Ok(out)
+}
+
+/// 一条 state 事实**被观察到**之后、沿指定唯一性方向的对账。新落库的要调,**又被观察到**
+/// 的也要调(同一断言多了一份证据,那份证据的日期可能更早,时间线的形状跟着变),写完
+/// 证据再调。调用方负责判断关系确实带该方向的唯一性且 temporal = state。
///
/// 宾语可以是实体(object_id)或字面值(object_value,属性事实)——
/// "宾语不同"的判定是 (object_id, object_value) 组合比较:工资从 3 万变 3.5 万
/// 与"从张三换成李四"走同一条闭合路径。
+///
+/// 这条事实的时间、置信度从库里读,参数只用来决定对哪条时间线
#[allow(clippy::too_many_arguments)]
pub async fn reconcile_new_fact(
pool: &PgPool,
@@ -55,133 +380,256 @@ pub async fn reconcile_new_fact(
subject_id: Uuid,
predicate_id: Uuid,
object_id: Option<Uuid>,
- object_value: Option<&serde_json::Value>,
+ _object_value: Option<&serde_json::Value>,
direction: Uniqueness,
- new_validity: crate::graph::Validity<'_>,
- new_confidence: f32,
+ _new_validity: crate::graph::Validity<'_>,
+ _new_confidence: f32,
) -> AppResult<ReconcileReport> {
- // 已闭区间的新事实是历史陈述,不威胁"开放期唯一"不变量——不触发任何改写
- // (闭区间之间的重叠矛盾是更细的区间代数,暂不自动裁,留给 Review 的人眼)
- if new_validity.has_ended() {
- return Ok(ReconcileReport::default());
- }
- // 宾语侧唯一性只对实体宾语有意义(字面值不"被占用")
- if matches!(direction, Uniqueness::ObjectSide) && object_id.is_none() {
- return Ok(ReconcileReport::default());
- }
- // 不变量点查:主语侧 = 同 (kb, S, P) 宾语不同;宾语侧 = 同 (kb, P, O) 主语不同
- let sql = match direction {
- Uniqueness::SubjectSide => {
- "SELECT id, valid_from, valid_from_precision FROM facts
- WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3
- AND valid_to IS NULL AND valid_to_precision IS NULL
- AND invalidated_at IS NULL
- AND id <> $4
- AND (object_id IS DISTINCT FROM $5 OR object_value IS DISTINCT FROM $6)
- ORDER BY valid_from ASC NULLS LAST, recorded_at ASC"
- }
- Uniqueness::ObjectSide => {
- "SELECT id, valid_from, valid_from_precision FROM facts
- WHERE kb_id = $1 AND object_id = $5 AND predicate_id = $3
- AND valid_to IS NULL AND valid_to_precision IS NULL
- AND invalidated_at IS NULL
- AND id <> $4 AND subject_id IS DISTINCT FROM $2
- ORDER BY valid_from ASC NULLS LAST, recorded_at ASC"
- }
+ let holder = match direction {
+ Uniqueness::SubjectSide => subject_id,
+ Uniqueness::ObjectSide => match object_id {
+ Some(o) => o,
+ None => return Ok(ReconcileReport::default()),
+ },
};
- let q = sqlx::query_as(sql)
- .bind(kb_id)
- .bind(subject_id)
- .bind(predicate_id)
- .bind(new_fact_id)
- .bind(object_id);
- let open: Vec<OpenFact> = match direction {
- Uniqueness::SubjectSide => q.bind(object_value).fetch_all(pool).await?,
- Uniqueness::ObjectSide => q.fetch_all(pool).await?,
+ let both_sides: bool = sqlx::query_scalar(
+ "SELECT functional AND inverse_functional FROM relation_types WHERE id = $1",
+ )
+ .bind(predicate_id)
+ .fetch_optional(pool)
+ .await?
+ .unwrap_or(false);
+ let timeline = Timeline {
+ holder,
+ predicate_id,
+ side: direction,
+ both_sides,
};
- if open.is_empty() {
- return Ok(ReconcileReport::default());
- }
-
let mut report = ReconcileReport::default();
- for old in open {
- match (old.valid_from, new_validity.from) {
- // 新事实没有世界时间:闭合点无从谈起 → 人裁
- (_, None) => {
- record_conflict(pool, kb_id, old.id, new_fact_id, "no_time").await?;
- report.conflicts += 1;
- }
- // 同一时刻开始:谁接替谁说不清 → 人裁
- (Some(of), Some(nf)) if of == nf => {
- record_conflict(pool, kb_id, old.id, new_fact_id, "simultaneous").await?;
- report.conflicts += 1;
+ let mut tx = pool.begin().await?;
+ lock_timeline(&mut tx, kb_id, timeline).await?;
+ arrive(&mut tx, kb_id, timeline, new_fact_id, &[], &mut report).await?;
+ tidy(&mut tx, kb_id, timeline, &mut report).await?;
+ tx.commit().await?;
+ Ok(report)
+}
+
+/// 一条事实来到它的时间线上,记下重算裁不了的,交给人(调用方已持锁):
+///
+/// - 与别的值同一时刻开始、两边那一刻都还成立:谁接替谁说不清
+/// - 两边都还开着,有一边说不出时间(没起点、也没有自带日期的证据):谁先谁后无从谈起。
+/// 不论哪一边先到都是这一对冲突,不替人关上任何一边
+///
+/// `settled`:同一批里已经来过的事实,不再与它们重复成对
+async fn arrive(
+ tx: &mut Transaction<'_, Postgres>,
+ kb_id: Uuid,
+ timeline: Timeline,
+ fact_id: Uuid,
+ settled: &[Uuid],
+ report: &mut ReconcileReport,
+) -> AppResult<()> {
+ let rows = load_timeline(tx, kb_id, timeline).await?;
+ let Some(new) = rows.iter().find(|r| r.id == fact_id) else {
+ return Ok(());
+ };
+ let others = rows
+ .iter()
+ .filter(|r| r.id != new.id && !same_value(timeline.side, r, new))
+ .filter(|r| !settled.contains(&r.id));
+ for other in others {
+ let reason = match (new.key(), other.key()) {
+ (Some(at), Some(theirs)) if at == theirs && new.holds_at(at) && other.holds_at(at) => {
+ "simultaneous"
}
- // 新事实开始得更早:它是历史前任,闭合在旧事实的开始
- (Some(of), Some(nf)) if nf < of => {
- if new_confidence < AUTO_CLOSE_MIN_CONFIDENCE {
- record_conflict(pool, kb_id, old.id, new_fact_id, "low_confidence").await?;
- report.conflicts += 1;
- } else if let Some(id) = close_superseded(
- pool,
- new_fact_id,
- of,
- old.valid_from_precision.as_deref().unwrap_or("day"),
- )
- .await?
- {
- report.corrected.push(id);
- }
+ (None, _) | (_, None) if new.is_open() && other.is_open() => "no_time",
+ _ => continue,
+ };
+ if record_conflict_tx(tx, kb_id, other.id, new.id, reason).await? {
+ report.conflicts += 1;
+ }
+ }
+ Ok(())
+}
+
+/// 重算一条时间线要改的地方(单元测试看的形状)
+#[cfg(test)]
+#[derive(Debug, Default, PartialEq)]
+struct Plan {
+ /// (行, 它该有的终点)
+ ends: Vec<(Uuid, End)>,
+ /// (行, 置信度不够、没让它关上的最近那个后任):交给人
+ held: Vec<(Uuid, Uuid)>,
+}
+
+/// 一条时间线上每一行该有的终点(纯函数,不碰库):只含终点由引擎定的行。
+///
+/// 能排进时间线的行(有起点,或有自带日期的证据)按时刻排好;终点是写明的行不动。其余
+/// 每一行——开着的、引擎关上的——止于它之后最近的、值不同的那一行开始时;后面没有这样
+/// 的行就开着。那一行置信度不够时不许它改写历史:跳过它、交给人,再看下一行。
+///
+/// 每一行的终点只取决于各行的时刻、值和置信度,改写终点不改这三样,所以一次算完就是
+/// 最终的样子,不必一轮一轮地来;行怎么排进来的也不影响结果
+fn desired_ends(side: Uniqueness, rows: &[Row]) -> (HashMap<Uuid, End>, Vec<(Uuid, Uuid)>) {
+ let mut keyed: Vec<&Row> = rows.iter().filter(|r| r.key().is_some()).collect();
+ // 同一刻开始的几行,写着起点的排前面:后任取它,前任就止于一个日期而不是一个锚点
+ keyed.sort_by_key(|r| (r.key(), r.valid_from.is_none(), r.id));
+ let mut ends = HashMap::new();
+ let mut held = Vec::new();
+ for (i, row) in keyed.iter().enumerate() {
+ if !row.recomputable() {
+ continue;
+ }
+ let mut end = End::Open;
+ let mut doubtful = None;
+ for later in keyed[i + 1..]
+ .iter()
+ .filter(|later| later.key() > row.key() && !same_value(side, row, later))
+ {
+ if later.confidence < AUTO_CLOSE_MIN_CONFIDENCE {
+ doubtful.get_or_insert(later.id);
+ continue;
}
- // 常规接替:旧事实闭合在新事实的开始(旧事实无起点也适用——起点未知但已结束)
- (_, Some(nf)) => {
- if new_confidence < AUTO_CLOSE_MIN_CONFIDENCE {
- record_conflict(pool, kb_id, old.id, new_fact_id, "low_confidence").await?;
- report.conflicts += 1;
- } else if let Some(id) = close_superseded(
- pool,
- old.id,
- nf,
- new_validity.from_precision.unwrap_or("day"),
- )
- .await?
- {
- report.corrected.push(id);
+ end = later.end_before();
+ break;
+ }
+ if let Some(later) = doubtful {
+ held.push((row.id, later));
+ }
+ ends.insert(row.id, end);
+ }
+ (ends, held)
+}
+
+/// 单侧时间线的重算计划:该有的终点与眼下写着的不同的那些行
+#[cfg(test)]
+fn plan_timeline(side: Uniqueness, rows: &[Row]) -> Plan {
+ let (ends, held) = desired_ends(side, rows);
+ Plan {
+ ends: diff(rows, ends),
+ held,
+ }
+}
+
+/// 该有的终点里与眼下不同的,按行在时间线上的顺序
+fn diff(rows: &[Row], mut ends: HashMap<Uuid, End>) -> Vec<(Uuid, End)> {
+ rows.iter()
+ .filter_map(|r| {
+ let end = ends.remove(&r.id)?;
+ (r.current_end() != end).then_some((r.id, end))
+ })
+ .collect()
+}
+
+/// 把一条时间线重算到 [`desired_ends`] 说的样子(调用方已持锁)。两侧都唯一的关系,
+/// 每一行再按它另一侧的时间线推一次,取早的那个终点
+async fn tidy(
+ tx: &mut Transaction<'_, Postgres>,
+ kb_id: Uuid,
+ timeline: Timeline,
+ report: &mut ReconcileReport,
+) -> AppResult<()> {
+ let rows = load_timeline(tx, kb_id, timeline).await?;
+ let (mut ends, mut held) = desired_ends(timeline.side, &rows);
+ if timeline.both_sides {
+ let other = timeline.side.other();
+ let holders: BTreeSet<Uuid> = rows.iter().filter_map(|r| r.holder(other)).collect();
+ for holder in holders {
+ let across = Timeline {
+ holder,
+ side: other,
+ ..timeline
+ };
+ let across_rows = load_timeline(tx, kb_id, across).await?;
+ let (across_ends, across_held) = desired_ends(other, &across_rows);
+ for (id, end) in across_ends {
+ if let Some(mine) = ends.remove(&id) {
+ ends.insert(id, mine.earlier(end));
}
}
+ held.extend(
+ across_held
+ .into_iter()
+ .filter(|(row, _)| ends.contains_key(row)),
+ );
+ }
+ }
+ let mut rewritten: HashMap<Uuid, Uuid> = HashMap::new();
+ for (id, end) in diff(&rows, ends) {
+ // 重新打开的行只是开着,终点不是谁推出来的
+ let derived = end != End::Open;
+ if let Some(corrected) = rewrite_end_tx(tx, id, &end, derived, false).await? {
+ rewritten.insert(id, corrected);
+ report.corrected.push(corrected);
}
}
+ for (row, later) in held {
+ let row = rewritten.get(&row).copied().unwrap_or(row);
+ let later = rewritten.get(&later).copied().unwrap_or(later);
+ if record_conflict_tx(tx, kb_id, row, later, "low_confidence").await? {
+ report.conflicts += 1;
+ }
+ }
+ Ok(())
+}
+
+/// 把几条时间线各自重算一遍(调用方已按 [`lock_timelines`] 拿到锁)。撤回合并、删除或
+/// 恢复文档在自己的事务里改完行之后调
+pub async fn tidy_timelines_tx(
+ tx: &mut Transaction<'_, Postgres>,
+ kb_id: Uuid,
+ timelines: &[Timeline],
+) -> AppResult<ReconcileReport> {
+ let mut report = ReconcileReport::default();
+ for timeline in timelines {
+ tidy(tx, kb_id, *timeline, &mut report).await?;
+ }
Ok(report)
}
/// 实体合并搬移事实后的对账:换了主/宾的事实等价于"新落库的观察"——
-/// 两个对象折成一个之后,唯一性不变量才第一次看得到它们相撞。
-/// 按 recorded_at 顺序逐条重跑插入时对账;非唯一性关系、已闭合区间、
-/// 已被前一条改写作废的事实自动跳过。
-/// 返回的修正行 id 由调用方记入合并账本——这些修正的唯一成因是合并本身,
-/// 回滚合并时必须随之撤销(修正行作废、被取代的原行恢复),否则修正行会
-/// 错挂在 target 上而其依据已随回滚离开。
+/// 两个对象折成一个之后,唯一性不变量才第一次看得到它们相撞。人改过区间之后也走这里。
+/// 返回的修正行 id 由调用方记入合并账本,供审计
pub async fn reconcile_moved_facts(
pool: &PgPool,
kb_id: Uuid,
fact_ids: &[Uuid],
) -> AppResult<ReconcileReport> {
- if fact_ids.is_empty() {
- return Ok(ReconcileReport::default());
+ reconcile_facts(pool, kb_id, fact_ids).await
+}
+
+/// 一批事实所在的每条时间线:批里的事实逐条「来到」时间线上(记下该交给人的),整条
+/// 重算一遍。一条时间线一个事务
+async fn reconcile_facts(
+ pool: &PgPool,
+ kb_id: Uuid,
+ fact_ids: &[Uuid],
+) -> AppResult<ReconcileReport> {
+ let mut report = ReconcileReport::default();
+ let batch: HashSet<Uuid> = fact_ids.iter().copied().collect();
+ for timeline in timelines_of(pool, kb_id, fact_ids, None).await? {
+ let mut tx = pool.begin().await?;
+ lock_timeline(&mut tx, kb_id, timeline).await?;
+ let rows = load_timeline(&mut tx, kb_id, timeline).await?;
+ let mut arriving: Vec<&Row> = rows.iter().filter(|r| batch.contains(&r.id)).collect();
+ arriving.sort_by_key(|r| (r.key().is_none(), r.key(), r.id));
+ let arriving: Vec<Uuid> = arriving.iter().map(|r| r.id).collect();
+ for (i, id) in arriving.iter().enumerate() {
+ arrive(&mut tx, kb_id, timeline, *id, &arriving[..i], &mut report).await?;
+ }
+ tidy(&mut tx, kb_id, timeline, &mut report).await?;
+ tx.commit().await?;
}
- reconcile_open(pool, kb_id, fact_ids, "f.recorded_at").await
+ Ok(report)
}
-/// 声明来晚了:一条谓词上所有开放事实按**年表**重跑一遍落库时的对账(#341)。
+/// 声明来晚了:一条谓词上所有现存事实所在的时间线重算一遍(#341)。
///
/// 本体自己长出来的库里没人声明过唯一性,接任不会闭合前任——三个人同时在管一个
/// 项目。人补上声明之后,这里把已经躺在账上的行对一遍。走的是与落库时同一条
/// 路(作废 + 改写,supersedes 链回旧行),所以记录轴倒回声明之前仍看得见三条
/// 开放的行;拿不准的(缺时间 / 同时开始 / 低置信)照旧进人审,不硬闭合。
///
-/// 按 `valid_from` 而不是 `recorded_at` 走:合并搬移按摄入顺序回放是对的,
-/// 因为那是在重演落库;这里三条早就都在账上,谁先谁后只有年表说了算——
-/// 前任必须闭合在**最早的**后任起点上,而周七的文档完全可能比李四的先到。
-///
/// 没有声明的谓词拒绝:引擎不替人推断(bootstrap_ontology.rs 写了为什么)。
pub async fn reconcile_predicate(
pool: &PgPool,
@@ -213,108 +661,42 @@ pub async fn reconcile_predicate(
}
let ids: Vec<Uuid> = sqlx::query_scalar(
"SELECT id FROM facts
- WHERE kb_id = $1 AND predicate_id = $2
- AND invalidated_at IS NULL AND valid_to IS NULL AND valid_to_precision IS NULL
- ORDER BY valid_from ASC NULLS LAST, recorded_at ASC",
+ WHERE kb_id = $1 AND predicate_id = $2 AND invalidated_at IS NULL
+ AND (object_id IS NOT NULL OR object_value IS NOT NULL)",
)
.bind(kb_id)
.bind(predicate_id)
.fetch_all(pool)
.await?;
- if ids.is_empty() {
- return Ok(ReconcileReport::default());
- }
- reconcile_open(
- pool,
- kb_id,
- &ids,
- "f.valid_from ASC NULLS LAST, f.recorded_at ASC",
- )
- .await
+ reconcile_facts(pool, kb_id, &ids).await
}
-/// 一批开放期事实按 `order`(SQL 的 ORDER BY 表达式)逐条当作"新落库的观察"对账。
-async fn reconcile_open(
- pool: &PgPool,
- kb_id: Uuid,
- fact_ids: &[Uuid],
- order: &str,
-) -> AppResult<ReconcileReport> {
- #[derive(sqlx::FromRow)]
- struct OpenRow {
- id: Uuid,
- subject_id: Uuid,
- predicate_id: Uuid,
- object_id: Option<Uuid>,
- object_value: Option<serde_json::Value>,
- valid_from: Option<DateTime<Utc>>,
- valid_from_precision: Option<String>,
- confidence: f32,
- functional: bool,
- inverse_functional: bool,
- }
- let rows: Vec<OpenRow> = sqlx::query_as(&format!(
- "SELECT f.id, f.subject_id, f.predicate_id, f.object_id, f.object_value,
- f.valid_from, f.valid_from_precision,
- f.confidence, r.functional, r.inverse_functional
- FROM facts f JOIN relation_types r ON r.id = f.predicate_id
- WHERE f.kb_id = $1 AND f.id = ANY($2)
- AND f.invalidated_at IS NULL AND f.valid_to IS NULL
- AND r.temporal = 'state' AND (r.functional OR r.inverse_functional)
- ORDER BY {order}"
- ))
+/// 撤掉一条事实(人判它是抽取错误):它从来不在,时间线按剩下的行重算——关在它开始时的
+/// 前任重新接上。先锁时间线再作废(见模块头)。返回有没有撤掉一行;已经作废的不算
+pub async fn retract(pool: &PgPool, kb_id: Uuid, fact_id: Uuid) -> AppResult<bool> {
+ let mut tx = pool.begin().await?;
+ let timelines = timelines_of(&mut *tx, kb_id, &[fact_id], None).await?;
+ lock_timelines(&mut tx, kb_id, &timelines).await?;
+ let retracted = sqlx::query(
+ "UPDATE facts SET invalidated_at = now()
+ WHERE id = $1 AND kb_id = $2 AND invalidated_at IS NULL",
+ )
+ .bind(fact_id)
.bind(kb_id)
- .bind(fact_ids)
- .fetch_all(pool)
- .await?;
-
- let mut report = ReconcileReport::default();
- for f in rows {
- // 字面值事实(属性)合并后同样对账:两个"张三"折成一个,工资撞车也要闭合
- if f.object_id.is_none() && f.object_value.is_none() {
- continue;
- }
- // 前面的闭合可能已把本条改写作废——逐条复核存活性再当"新事实"用
- let (alive,): (bool,) = sqlx::query_as(
- "SELECT EXISTS (SELECT 1 FROM facts
- WHERE id = $1 AND invalidated_at IS NULL AND valid_to IS NULL)",
- )
- .bind(f.id)
- .fetch_one(pool)
- .await?;
- if !alive {
- continue;
- }
- let mut directions = Vec::new();
- if f.functional {
- directions.push(Uniqueness::SubjectSide);
- }
- if f.inverse_functional {
- directions.push(Uniqueness::ObjectSide);
- }
- for dir in directions {
- let r = reconcile_new_fact(
- pool,
- kb_id,
- f.id,
- f.subject_id,
- f.predicate_id,
- f.object_id,
- f.object_value.as_ref(),
- dir,
- crate::graph::Validity::starting(f.valid_from, f.valid_from_precision.as_deref()),
- f.confidence,
- )
- .await?;
- report.corrected.extend(r.corrected);
- report.conflicts += r.conflicts;
- }
+ .execute(&mut *tx)
+ .await?
+ .rows_affected()
+ > 0;
+ if retracted {
+ tidy_timelines_tx(&mut tx, kb_id, &timelines).await?;
}
- Ok(report)
+ tx.commit().await?;
+ Ok(retracted)
}
-/// 作废 + 改写:旧行记 invalidated_at(认知轴),插入闭合区间的修正行
-/// (世界轴),证据引用随行复制。返回修正行 id;`None` = 这条已不是开放行,没动。
+/// 作废 + 改写成**写明的**终点:旧行记 invalidated_at(认知轴),插入闭合区间的修正行
+/// (世界轴),证据引用随行复制。原文说它在哪天结束、人裁决把它关上,都走这里——
+/// 这个终点此后不再由引擎重算。返回修正行 id;`None` = 这条已被作废,没动。
pub async fn close_superseded(
pool: &PgPool,
fact_id: Uuid,
@@ -322,83 +704,198 @@ pub async fn close_superseded(
valid_to_precision: &str,
) -> AppResult<Option<Uuid>> {
// 闭合点截到它的精度(0024):月精度的闭合就是那个月的 1 日 0 点
- let valid_to = crate::graph::truncate_to(valid_to, Some(valid_to_precision));
+ let end = End::At(
+ crate::graph::truncate_to(valid_to, Some(valid_to_precision)),
+ valid_to_precision.to_string(),
+ );
let mut tx = pool.begin().await?;
- let corrected = Uuid::now_v7();
- let inserted: Option<(Uuid,)> = sqlx::query_as(
- "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, object_value,
- valid_from, valid_from_precision,
- valid_to, valid_to_precision, confidence, supersedes,
- attested_from, attested_to)
- SELECT $1, kb_id, subject_id, predicate_id, object_id, object_value,
- valid_from, valid_from_precision, $3, $4, confidence, id,
- attested_from, NULL
- FROM facts WHERE id = $2 AND invalidated_at IS NULL
- RETURNING id",
- )
- .bind(corrected)
- .bind(fact_id)
- .bind(valid_to)
- .bind(valid_to_precision)
- .fetch_optional(&mut *tx)
- .await?;
- // 已被改写过(并发,或同一批对账里前一步已经闭合了它):不重复动手,也不算一次修正
- if inserted.is_none() {
- tx.rollback().await?;
- return Ok(None);
- }
- sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1")
- .bind(fact_id)
- .execute(&mut *tx)
- .await?;
- copy_evidence(&mut tx, fact_id, corrected).await?;
- copy_qualifiers(&mut tx, fact_id, corrected).await?;
+ let corrected = rewrite_end_tx(&mut tx, fact_id, &end, false, false).await?;
tx.commit().await?;
- Ok(Some(corrected))
+ Ok(corrected)
}
/// 作废 + 改写成「结束了,不知哪天」(0022 / #393):旧行记 invalidated_at,修正行终点仍是
/// NULL、精度 'unknown',`attested_to` 锚在**说出结束的那份文档**——读出来就是「到它为止」;
/// `attested_from` 从旧行继承——没起点的裸行靠它记着第一份证据,读出来是「从那时起」。
-/// 证据引用随行复制。返回修正行 id;`None` = 这条已不是开放行,没动。
+/// 这是原文写明的结束,引擎不重算。证据引用随行复制。返回修正行 id;`None` = 这条已不是
+/// 开放行,没动。
pub async fn close_with_unknown_end(
pool: &PgPool,
fact_id: Uuid,
attested_at: Option<DateTime<Utc>>,
) -> AppResult<Option<Uuid>> {
let mut tx = pool.begin().await?;
+ let corrected =
+ rewrite_end_tx(&mut tx, fact_id, &End::Unknown(attested_at), false, true).await?;
+ tx.commit().await?;
+ Ok(corrected)
+}
+
+/// 原文说出了一条**引擎关上**的行的终点:改写成原文说的,此后不再重算(#679 第三轮评审)。
+/// `valid_to` 为 `None` 是「结束了,不知哪天」,锚在 `attested_at`。返回修正行 id;
+/// 行已作废,或它的终点本来就是写明的,返回 `None`
+pub async fn state_derived_end(
+ pool: &PgPool,
+ fact_id: Uuid,
+ valid_to: Option<(DateTime<Utc>, &str)>,
+ attested_at: Option<DateTime<Utc>>,
+) -> AppResult<Option<Uuid>> {
+ let end = match valid_to {
+ Some((at, precision)) => End::At(
+ crate::graph::truncate_to(at, Some(precision)),
+ precision.to_string(),
+ ),
+ None => End::Unknown(attested_at),
+ };
+ let mut tx = pool.begin().await?;
+ let derived: Option<bool> = sqlx::query_scalar(
+ "SELECT end_derived FROM facts WHERE id = $1 AND invalidated_at IS NULL FOR UPDATE",
+ )
+ .bind(fact_id)
+ .fetch_optional(&mut *tx)
+ .await?;
+ let corrected = match derived {
+ Some(true) => rewrite_end_tx(&mut tx, fact_id, &end, false, false).await?,
+ _ => None,
+ };
+ tx.commit().await?;
+ Ok(corrected)
+}
+
+/// 作废 + 改写一行的终点。`derived`:这个终点是引擎按时间线推出来的(0057),之后随
+/// 时间线重算;`require_open`:只改还开着的行。
+///
+/// 先 `FOR UPDATE` 锁住旧行:并行的两次改写只有一次看得见它还活着,另一次拿到 `None`,
+/// 不会各插一条修正行;往这一行上添证据的也要等它(`graph::add_evidence`),证据不会落在
+/// 刚被作废的旧行上。还没人裁的冲突跟着换到修正行上:问题还在,只是这一行换了终点
+async fn rewrite_end_tx(
+ tx: &mut Transaction<'_, Postgres>,
+ fact_id: Uuid,
+ end: &End,
+ derived: bool,
+ require_open: bool,
+) -> AppResult<Option<Uuid>> {
+ let alive: Option<(Uuid,)> = sqlx::query_as(
+ "SELECT id FROM facts
+ WHERE id = $1 AND invalidated_at IS NULL
+ AND (NOT $2 OR (valid_to IS NULL AND valid_to_precision IS NULL))
+ FOR UPDATE",
+ )
+ .bind(fact_id)
+ .bind(require_open)
+ .fetch_optional(&mut **tx)
+ .await?;
+ if alive.is_none() {
+ return Ok(None);
+ }
+ let (valid_to, precision, anchor) = match end {
+ End::Open => (None, None, None),
+ End::At(at, precision) => (Some(*at), Some(precision.as_str()), None),
+ End::Unknown(anchor) => (None, Some(crate::graph::ENDED_UNKNOWN), *anchor),
+ };
let corrected = Uuid::now_v7();
- let inserted: Option<(Uuid,)> = sqlx::query_as(
+ sqlx::query(
"INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, object_value,
valid_from, valid_from_precision,
valid_to, valid_to_precision, confidence, supersedes,
- attested_from, attested_to)
+ attested_from, attested_to, end_derived)
SELECT $1, kb_id, subject_id, predicate_id, object_id, object_value,
- valid_from, valid_from_precision, NULL, $3, confidence, id,
- attested_from, COALESCE($4, now())
- FROM facts
- WHERE id = $2 AND invalidated_at IS NULL
- AND valid_to IS NULL AND valid_to_precision IS NULL
- RETURNING id",
+ valid_from, valid_from_precision, $3, $4, confidence, id,
+ attested_from, CASE WHEN $4::text = 'unknown' THEN COALESCE($5, now()) END, $6
+ FROM facts WHERE id = $2",
)
.bind(corrected)
.bind(fact_id)
- .bind(crate::graph::ENDED_UNKNOWN)
- .bind(attested_at)
- .fetch_optional(&mut *tx)
+ .bind(valid_to)
+ .bind(precision)
+ .bind(anchor)
+ .bind(derived)
+ .execute(&mut **tx)
.await?;
- if inserted.is_none() {
- tx.rollback().await?;
+ carry_open_conflicts(tx, fact_id, corrected).await?;
+ sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1")
+ .bind(fact_id)
+ .execute(&mut **tx)
+ .await?;
+ copy_evidence(tx, fact_id, corrected).await?;
+ copy_qualifiers(tx, fact_id, corrected).await?;
+ Ok(Some(corrected))
+}
+
+/// 作废 + 改写一行的持有者:主语或宾语换成另一个实体,其余照旧(证据、边上的属性随行)。
+/// 撤回合并把合并之后才改写出来的行送回源实体时用——原地改主语,记录轴回放合并窗口时
+/// 就找不到它当时挂在哪(0027 只认合并账本上的行)。返回新行 id;`None` = 已作废
+pub async fn rehome_tx(
+ tx: &mut Transaction<'_, Postgres>,
+ fact_id: Uuid,
+ subject_id: Option<Uuid>,
+ object_id: Option<Uuid>,
+) -> AppResult<Option<Uuid>> {
+ let alive: Option<(Uuid,)> =
+ sqlx::query_as("SELECT id FROM facts WHERE id = $1 AND invalidated_at IS NULL FOR UPDATE")
+ .bind(fact_id)
+ .fetch_optional(&mut **tx)
+ .await?;
+ if alive.is_none() {
return Ok(None);
}
+ let moved = Uuid::now_v7();
+ sqlx::query(
+ "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, object_value,
+ valid_from, valid_from_precision, valid_to, valid_to_precision,
+ confidence, derived_by_rule, supersedes,
+ attested_from, attested_to, end_derived)
+ SELECT $1, kb_id, COALESCE($3, subject_id), predicate_id, COALESCE($4, object_id),
+ object_value, valid_from, valid_from_precision, valid_to, valid_to_precision,
+ confidence, derived_by_rule, id, attested_from, attested_to, end_derived
+ FROM facts WHERE id = $2",
+ )
+ .bind(moved)
+ .bind(fact_id)
+ .bind(subject_id)
+ .bind(object_id)
+ .execute(&mut **tx)
+ .await?;
+ // 开着的冲突先换到新行上:旧行一作废,0051 的触发器就把它们撤下,之后重算也不会
+ // 再记同一天开始的那一对——撤回完两行叠在一起,审核队列却是空的(#679 第四轮评审)
+ carry_open_conflicts(tx, fact_id, moved).await?;
sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1")
.bind(fact_id)
- .execute(&mut *tx)
+ .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))
+ copy_evidence(tx, fact_id, moved).await?;
+ copy_qualifiers(tx, fact_id, moved).await?;
+ Ok(Some(moved))
+}
+
+/// 还开着的冲突从旧行换到修正行上(旧行一作废,0051 的触发器就会把它们撤下)。
+/// 修正行上已经有同一对的,留着旧的那条随作废撤下
+async fn carry_open_conflicts(
+ tx: &mut Transaction<'_, Postgres>,
+ from: Uuid,
+ to: Uuid,
+) -> AppResult<()> {
+ sqlx::query(
+ "UPDATE fact_conflicts c SET old_fact_id = $2
+ WHERE c.status = 'open' AND c.old_fact_id = $1
+ AND NOT EXISTS (SELECT 1 FROM fact_conflicts d
+ WHERE d.old_fact_id = $2 AND d.new_fact_id = c.new_fact_id)",
+ )
+ .bind(from)
+ .bind(to)
+ .execute(&mut **tx)
+ .await?;
+ sqlx::query(
+ "UPDATE fact_conflicts c SET new_fact_id = $2
+ WHERE c.status = 'open' AND c.new_fact_id = $1
+ AND NOT EXISTS (SELECT 1 FROM fact_conflicts d
+ WHERE d.new_fact_id = $2 AND d.old_fact_id = c.old_fact_id)",
+ )
+ .bind(from)
+ .bind(to)
+ .execute(&mut **tx)
+ .await?;
+ Ok(())
}
/// 边上的属性随修正行复制(0037):纠正的是时间区间,边上的金额、职务照旧——
@@ -501,16 +998,37 @@ pub async fn correct_interval(
Ok(Some(corrected))
}
-async fn record_conflict(
- pool: &PgPool,
+/// 记一对冲突。返回这一对是不是第一次记:时间线每次重算都会再看见同一对,
+/// 已经在队列里(或人已经裁过)的不再算作新冲突
+async fn record_conflict_tx(
+ tx: &mut Transaction<'_, Postgres>,
kb_id: Uuid,
old_fact_id: Uuid,
new_fact_id: Uuid,
reason: &str,
-) -> AppResult<()> {
- sqlx::query(
+) -> AppResult<bool> {
+ // 人已经裁过「两个都留着」的,不因为两行后来被改写(换了 id)就再问一遍:
+ // 顺着 supersedes 往上找两行的前身,那一对裁过就不记(#679 第三轮评审)
+ let inserted = sqlx::query(
"INSERT INTO fact_conflicts (id, kb_id, old_fact_id, new_fact_id, reason)
- VALUES ($1, $2, $3, $4, $5)
+ SELECT $1, $2, $3, $4, $5
+ WHERE NOT EXISTS (
+ WITH RECURSIVE a(id) AS (
+ SELECT $3::uuid
+ UNION SELECT f.supersedes FROM facts f JOIN a ON f.id = a.id
+ WHERE f.supersedes IS NOT NULL),
+ b(id) AS (
+ SELECT $4::uuid
+ UNION SELECT f.supersedes FROM facts f JOIN b ON f.id = b.id
+ WHERE f.supersedes IS NOT NULL)
+ SELECT 1 FROM fact_conflicts c
+ WHERE c.status = 'resolved' AND c.resolution = 'kept_both'
+ AND ((c.old_fact_id IN (SELECT id FROM a) AND c.new_fact_id IN (SELECT id FROM b))
+ OR (c.old_fact_id IN (SELECT id FROM b) AND c.new_fact_id IN (SELECT id FROM a))))
+ -- 同一对反过来记过的也算记过:重算按时间线顺序看,谁是「旧」谁是「新」
+ -- 随到达顺序变,不去重的话一次全量重算会翻出一堆方向相反的重复
+ AND NOT EXISTS (SELECT 1 FROM fact_conflicts r
+ WHERE r.old_fact_id = $4 AND r.new_fact_id = $3 AND r.status = 'open')
ON CONFLICT (old_fact_id, new_fact_id) DO NOTHING",
)
.bind(Uuid::now_v7())
@@ -518,9 +1036,9 @@ async fn record_conflict(
.bind(old_fact_id)
.bind(new_fact_id)
.bind(reason)
- .execute(pool)
+ .execute(&mut **tx)
.await?;
- Ok(())
+ Ok(inserted.rows_affected() > 0)
}
/// Review 页的冲突列表(双方事实带名字与区间)。
@@ -611,10 +1129,7 @@ pub async fn resolve_conflict(
}
"keep" => "kept_both",
"reject_new" => {
- sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1")
- .bind(new_fact_id)
- .execute(pool)
- .await?;
+ retract(pool, kb_id, new_fact_id).await?;
// 波及:同一新事实撞出的其他 open 冲突一并出队(新事实已死,无从裁起)
sqlx::query(
"UPDATE fact_conflicts
@@ -905,6 +1420,151 @@ mod tests {
}
}
+ fn day(y: i32, m: u32, d: u32) -> DateTime<Utc> {
+ Utc.with_ymd_and_hms(y, m, d, 0, 0, 0).unwrap()
+ }
+
+ /// 时间线上的一行:值、起点、没起点时自带日期的证据
+ fn row(value: &str, from: Option<DateTime<Utc>>, dated: Option<DateTime<Utc>>) -> Row {
+ Row {
+ id: Uuid::now_v7(),
+ subject_id: Uuid::nil(),
+ object_id: None,
+ object_value: Some(serde_json::json!({ "value": value })),
+ valid_from: from,
+ valid_from_precision: from.map(|_| "day".to_string()),
+ valid_to: None,
+ valid_to_precision: None,
+ attested_to: None,
+ confidence: 0.9,
+ end_derived: false,
+ dated_at: dated,
+ }
+ }
+
+ /// 把计划落到行上(改写成新 id 的事与此无关,原地换终点),直到计划为空
+ fn settle(rows: &mut [Row]) {
+ let plan = plan_timeline(Uniqueness::SubjectSide, rows);
+ for (id, end) in plan.ends {
+ let r = rows.iter_mut().find(|r| r.id == id).unwrap();
+ r.end_derived = end != End::Open;
+ (r.valid_to, r.valid_to_precision, r.attested_to) = match end {
+ End::Open => (None, None, None),
+ End::At(t, p) => (Some(t), Some(p), None),
+ End::Unknown(a) => (None, Some("unknown".into()), a),
+ };
+ }
+ assert_eq!(
+ plan_timeline(Uniqueness::SubjectSide, rows).ends,
+ vec![],
+ "一次算完就是最终的样子"
+ );
+ }
+
+ fn shape(rows: &[Row]) -> Vec<(String, End)> {
+ let mut out: Vec<_> = rows
+ .iter()
+ .map(|r| {
+ (
+ r.object_value.as_ref().unwrap()["value"]
+ .as_str()
+ .unwrap()
+ .to_string(),
+ r.current_end(),
+ )
+ })
+ .collect();
+ out.sort_by(|a, b| a.0.cmp(&b.0));
+ out
+ }
+
+ /// 同一批行,不论以什么顺序排进来,算出来的时间线一样
+ #[test]
+ fn a_timeline_does_not_depend_on_the_order_its_rows_arrive() {
+ let base = vec![
+ row("v1", Some(day(2020, 2, 18)), None),
+ row("v2", Some(day(2020, 3, 17)), None),
+ row("v3", None, Some(day(2020, 4, 14))),
+ row("v4", Some(day(2020, 5, 26)), None),
+ row("v5", None, Some(day(2020, 6, 8))),
+ ];
+ let mut first = base.clone();
+ settle(&mut first);
+ assert_eq!(
+ shape(&first),
+ vec![
+ ("v1".into(), End::At(day(2020, 3, 17), "day".into())),
+ ("v2".into(), End::Unknown(Some(day(2020, 4, 14)))),
+ ("v3".into(), End::At(day(2020, 5, 26), "day".into())),
+ ("v4".into(), End::Unknown(Some(day(2020, 6, 8)))),
+ ("v5".into(), End::Open),
+ ]
+ );
+ for rotation in 1..base.len() {
+ let mut rows = base.clone();
+ rows.rotate_left(rotation);
+ rows.reverse();
+ settle(&mut rows);
+ assert_eq!(shape(&rows), shape(&first));
+ }
+ }
+
+ /// 后面那一段挪了、走了,引擎画的终点跟着变;写明的终点不动
+ #[test]
+ fn a_derived_end_follows_what_comes_next_and_a_stated_one_stays() {
+ let mut rows = vec![
+ row("A", Some(day(2016, 5, 16)), None),
+ row("B", None, Some(day(2020, 8, 13))),
+ ];
+ settle(&mut rows);
+ assert_eq!(rows[0].current_end(), End::Unknown(Some(day(2020, 8, 13))));
+ // 后来的文件说 B 从 7 月 31 日起:A 止于那一天
+ rows[1].valid_from = Some(day(2020, 7, 31));
+ rows[1].valid_from_precision = Some("day".into());
+ settle(&mut rows);
+ assert_eq!(
+ rows[0].current_end(),
+ End::At(day(2020, 7, 31), "day".into())
+ );
+ // B 被撤回:A 重新开着
+ rows.pop();
+ settle(&mut rows);
+ assert_eq!(rows[0].current_end(), End::Open);
+ assert!(!rows[0].end_derived);
+
+ // 原文写明 C 到 2019 年底,后面的 D 从 2019 年 6 月起:两份原文说法相左,不替人挑
+ let mut stated = vec![
+ row("C", Some(day(2018, 1, 1)), None),
+ row("D", Some(day(2019, 6, 1)), None),
+ ];
+ stated[0].valid_to = Some(day(2019, 12, 31));
+ stated[0].valid_to_precision = Some("day".into());
+ assert_eq!(
+ plan_timeline(Uniqueness::SubjectSide, &stated),
+ Plan::default()
+ );
+ }
+
+ /// 置信度不够的后任不许关上前任,交给人;前任止于它之后第一个够格的后任
+ #[test]
+ fn a_doubtful_successor_is_held_and_the_next_sure_one_closes() {
+ let mut rows = vec![
+ row("A", Some(day(2020, 1, 1)), None),
+ row("B", Some(day(2020, 2, 1)), None),
+ row("C", Some(day(2020, 3, 1)), None),
+ ];
+ rows[1].confidence = 0.5;
+ let plan = plan_timeline(Uniqueness::SubjectSide, &rows);
+ assert_eq!(
+ plan.ends,
+ vec![
+ (rows[0].id, End::At(day(2020, 3, 1), "day".into())),
+ (rows[1].id, End::At(day(2020, 3, 1), "day".into())),
+ ]
+ );
+ assert_eq!(plan.held, vec![(rows[0].id, rows[1].id)]);
+ }
+
#[test]
fn a_chain_of_three_closes_twice() {
let values = [
diff --git a/crates/utopia-store/tests/a_late_value_takes_its_place_in_history.rs b/crates/utopia-store/tests/a_late_value_takes_its_place_in_history.rs
new file mode 100644
index 00000000..2ca59a8a
--- /dev/null
+++ b/crates/utopia-store/tests/a_late_value_takes_its_place_in_history.rs
@@ -0,0 +1,451 @@
+//! 单值属性的新值乱序到达,年表仍然一段接一段。
+//!
+//! Blackbaud 总部租约的二期行权截止日在 2020 年上半年被补充协议改了四次。十几份补充协议
+//! 并行抽取,谁先落库全看分块多少:第五份(2 月 18 日起)先到,第九份(6 月 8 日起)
+//! 第二个到,第七份、第六份最后才到。时态引擎从前只拿新值去比「仍在成立」的那一条,
+//! 于是晚到的两份都关在 6 月 8 日,而第五份那段 [2 月 18 日, 6 月 8 日) 没人去切——
+//! 问 5 月 1 日的截止日,查出三个。
+//!
+//! 这里钉两件事:乱序到达最后排成正确的年表;原文自己写明的终点不被切。
+//!
+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
+
+use serde_json::json;
+use sqlx::PgPool;
+use utopia_store::graph::Validity;
+use utopia_store::temporal::Uniqueness;
+use uuid::Uuid;
+
+struct Fixture {
+ kb: Uuid,
+ lease: Uuid,
+ deadline: Uuid,
+}
+
+async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> {
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ let (etype, deadline, lease) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'late-value-test')")
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'late-value-test')")
+ .bind(ws)
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'late-value-test')",
+ )
+ .bind(kb)
+ .bind(ws)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'lease', 'Lease')",
+ )
+ .bind(etype)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO relation_types (id, kb_id, key, label, kind, datatype, temporal, functional)
+ VALUES ($1, $2, 'option_deadline', 'option deadline', 'attribute', 'date', 'state', TRUE)",
+ )
+ .bind(deadline)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, 'HQ Lease')",
+ )
+ .bind(lease)
+ .bind(kb)
+ .bind(etype)
+ .execute(pool)
+ .await?;
+ Ok(Fixture {
+ kb,
+ lease,
+ deadline,
+ })
+}
+
+fn t(day: &str) -> chrono::DateTime<chrono::Utc> {
+ format!("{day}T00:00:00Z").parse().unwrap()
+}
+
+/// 落一条值,然后照抽取的做法对账
+async fn arrive(
+ pool: &PgPool,
+ f: &Fixture,
+ value: &str,
+ from: &str,
+ to: Option<&str>,
+) -> anyhow::Result<Uuid> {
+ let mut validity = Validity::starting(Some(t(from)), Some("day"));
+ if let Some(to) = to {
+ validity.to = Some(t(to));
+ validity.to_precision = Some("day");
+ }
+ let object = json!({ "value": value });
+ let (id, _) = utopia_store::graph::insert_value_fact(
+ pool,
+ f.kb,
+ f.lease,
+ Some(f.deadline),
+ &object,
+ validity,
+ 0.9,
+ )
+ .await?;
+ utopia_store::temporal::reconcile_new_fact(
+ pool,
+ f.kb,
+ id,
+ f.lease,
+ f.deadline,
+ None,
+ Some(&object),
+ Uniqueness::SubjectSide,
+ validity,
+ 0.9,
+ )
+ .await?;
+ Ok(id)
+}
+
+/// 现存的各段:(值, 起点, 终点),按起点排
+async fn timeline(
+ pool: &PgPool,
+ f: &Fixture,
+) -> anyhow::Result<Vec<(String, String, Option<String>)>> {
+ let rows: Vec<(String, String, Option<String>)> = sqlx::query_as(
+ "SELECT object_value #>> '{value}', to_char(valid_from, 'YYYY-MM-DD'),
+ to_char(valid_to, 'YYYY-MM-DD')
+ FROM facts
+ WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3 AND invalidated_at IS NULL
+ ORDER BY valid_from",
+ )
+ .bind(f.kb)
+ .bind(f.lease)
+ .bind(f.deadline)
+ .fetch_all(pool)
+ .await?;
+ Ok(rows)
+}
+
+fn seg(value: &str, from: &str, to: Option<&str>) -> (String, String, Option<String>) {
+ (value.into(), from.into(), to.map(Into::into))
+}
+
+#[tokio::test]
+async fn amendments_that_arrive_out_of_order_still_hand_over_one_to_the_next() -> 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 run = async {
+ // 第五、第九、第七、第六份,按它们真实的落库顺序
+ arrive(&pool, &f, "2020-03-17", "2020-02-18", None).await?;
+ arrive(&pool, &f, "2020-06-23", "2020-06-08", None).await?;
+ arrive(&pool, &f, "2020-05-26", "2020-04-14", None).await?;
+ arrive(&pool, &f, "2020-04-14", "2020-03-17", None).await?;
+
+ assert_eq!(
+ timeline(&pool, &f).await?,
+ vec![
+ seg("2020-03-17", "2020-02-18", Some("2020-03-17")),
+ seg("2020-04-14", "2020-03-17", Some("2020-04-14")),
+ seg("2020-05-26", "2020-04-14", Some("2020-06-08")),
+ seg("2020-06-23", "2020-06-08", None),
+ ],
+ "每一段都止于下一段的起点,任何一天只有一个截止日"
+ );
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(&pool)
+ .await?;
+ run
+}
+
+/// 原文写明「到 12 月 31 日为止」的一段,不是接替留下的边界:年中来的新值不切它。
+/// 那种重叠是两份原文说法相左,不该由引擎替人挑一个
+#[tokio::test]
+async fn an_end_the_text_states_is_not_cut_by_a_later_value() -> 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 run = async {
+ arrive(&pool, &f, "2020-09-30", "2020-01-01", Some("2020-12-31")).await?;
+ arrive(&pool, &f, "2020-10-31", "2020-06-01", None).await?;
+
+ let rows = timeline(&pool, &f).await?;
+ assert!(
+ rows.contains(&seg("2020-09-30", "2020-01-01", Some("2020-12-31"))),
+ "原文写的终点原样留着:{rows:?}"
+ );
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(&pool)
+ .await?;
+ run
+}
+
+/// 从一份带日期的文件里读到的值:起点可以没有,证据文件的日期总是有的
+async fn arrive_from_document(
+ pool: &PgPool,
+ f: &Fixture,
+ value: &str,
+ from: Option<&str>,
+ document_date: &str,
+) -> anyhow::Result<Uuid> {
+ arrive_object_from_document(pool, f, json!({ "value": value }), from, document_date).await
+}
+
+async fn arrive_object_from_document(
+ pool: &PgPool,
+ f: &Fixture,
+ object: serde_json::Value,
+ from: Option<&str>,
+ document_date: &str,
+) -> anyhow::Result<Uuid> {
+ let value = object["value"].as_str().unwrap_or_default().to_string();
+ let value = value.as_str();
+ let (doc, chunk) = (Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query(
+ "INSERT INTO documents (id, kb_id, filename, sha256, doc_time, doc_time_source) VALUES ($1, $2, $3, $3, $4, 'content')",
+ )
+ .bind(doc)
+ .bind(f.kb)
+ .bind(format!("amendment-{document_date}-{value}.html"))
+ .bind(t(document_date))
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1, $2, $3, 0, $4)",
+ )
+ .bind(chunk)
+ .bind(f.kb)
+ .bind(doc)
+ .bind(value)
+ .execute(pool)
+ .await?;
+ let validity = match from {
+ Some(from) => Validity::starting(Some(t(from)), Some("day")),
+ None => Validity::default(),
+ }
+ .attested(Some(t(document_date)));
+ let (id, _) = utopia_store::graph::insert_value_fact(
+ pool,
+ f.kb,
+ f.lease,
+ Some(f.deadline),
+ &object,
+ validity,
+ 0.9,
+ )
+ .await?;
+ utopia_store::graph::add_evidence(pool, id, chunk, Some(value), None).await?;
+ utopia_store::temporal::reconcile_new_fact(
+ pool,
+ f.kb,
+ id,
+ f.lease,
+ f.deadline,
+ None,
+ Some(&object),
+ Uniqueness::SubjectSide,
+ validity,
+ 0.9,
+ )
+ .await?;
+ Ok(id)
+}
+
+/// 第五份补充协议没抽出起点,它先进了人审;第八份来时,从前把它和 3 月 17 日起的
+/// 第六份一起关在 5 月 26 日,两段叠了两个多月。它的后任是它之后最近的已知起点
+#[tokio::test]
+async fn a_value_with_no_start_ends_where_the_next_known_value_begins() -> 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 run = async {
+ arrive_from_document(&pool, &f, "2020-04-14", Some("2020-03-17"), "2020-03-17").await?;
+ arrive_from_document(&pool, &f, "2020-03-17", None, "2020-02-18").await?;
+ arrive_from_document(&pool, &f, "2020-06-09", Some("2020-05-26"), "2020-05-26").await?;
+
+ let rows: Vec<(String, Option<String>, Option<String>)> = sqlx::query_as(
+ "SELECT object_value #>> '{value}', to_char(valid_from, 'YYYY-MM-DD'),
+ to_char(valid_to, 'YYYY-MM-DD')
+ FROM facts
+ WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3 AND invalidated_at IS NULL
+ ORDER BY valid_to NULLS LAST",
+ )
+ .bind(f.kb)
+ .bind(f.lease)
+ .bind(f.deadline)
+ .fetch_all(&pool)
+ .await?;
+ assert_eq!(
+ rows,
+ vec![
+ ("2020-03-17".into(), None, Some("2020-03-17".into())),
+ (
+ "2020-04-14".into(),
+ Some("2020-03-17".into()),
+ Some("2020-05-26".into())
+ ),
+ ("2020-06-09".into(), Some("2020-05-26".into()), None),
+ ]
+ );
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(&pool)
+ .await?;
+ run
+}
+
+/// 第十一份补充协议(8 月 13 日)说房东是 BBHQ1,没说从哪天起;另一份说 HPBB1 从 2016 年起
+/// 是房东。BBHQ1 在 8 月 13 日成立,所以它排在 HPBB1 之后:HPBB1 写成「结束了,不知哪天」,
+/// 锚在 8 月 13 日(#681 §1,0022 的形状)。日期不写进日期列,读回来 HPBB1 到 8 月 13 日为止、
+/// BBHQ1 从 8 月 13 日起,首尾相接不重叠
+#[tokio::test]
+async fn a_dated_value_with_no_start_takes_over_from_its_date() -> 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 run = async {
+ let later = arrive_from_document(&pool, &f, "BBHQ1", None, "2020-08-13").await?;
+ arrive_from_document(&pool, &f, "HPBB1", Some("2016-05-16"), "2016-05-16").await?;
+
+ let (still_open,): (bool,) = sqlx::query_as(
+ "SELECT valid_to IS NULL AND valid_to_precision IS NULL AND invalidated_at IS NULL
+ FROM facts WHERE id = $1",
+ )
+ .bind(later)
+ .fetch_one(&pool)
+ .await?;
+ assert!(still_open, "BBHQ1 没被关在 2016 年");
+ let earlier: (
+ Option<String>,
+ Option<String>,
+ Option<String>,
+ Option<String>,
+ ) = sqlx::query_as(
+ "SELECT to_char(valid_from, 'YYYY-MM-DD'), to_char(valid_to, 'YYYY-MM-DD'),
+ valid_to_precision, to_char(attested_to, 'YYYY-MM-DD')
+ FROM facts
+ WHERE kb_id = $1 AND object_value #>> '{value}' = 'HPBB1'
+ AND invalidated_at IS NULL",
+ )
+ .bind(f.kb)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(
+ earlier,
+ (
+ Some("2016-05-16".into()),
+ None,
+ Some("unknown".into()),
+ Some("2020-08-13".into())
+ ),
+ "HPBB1 结束了、不知哪天,锚在 BBHQ1 那份文件的日期上"
+ );
+ let (conflicts,): (i64,) =
+ sqlx::query_as("SELECT count(*) FROM fact_conflicts WHERE kb_id = $1")
+ .bind(f.kb)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(conflicts, 0, "排得出先后,就不用人看");
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(&pool)
+ .await?;
+ run
+}
+
+/// 第十份补充协议(6 月 26 日)把截止日改成「触发日后 45 天」:没有日历日期,抽取标成 relative
+/// 照原文存下(#681 §4)。它仍是这份租约新的截止日,所以 6 月 23 日那个到它为止——
+/// 它没有起点,前一个就写成「结束了,不知哪天」,锚在 6 月 26 日这份文件上(§1)
+#[tokio::test]
+async fn a_deadline_stated_relative_to_an_event_still_ends_the_dated_one_before_it(
+) -> 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 run = async {
+ arrive_from_document(&pool, &f, "2020-06-23", Some("2020-06-08"), "2020-06-08").await?;
+ let relative = arrive_object_from_document(
+ &pool,
+ &f,
+ json!({ "value": "45 days after the Trigger Date", "relative": true }),
+ None,
+ "2020-06-26",
+ )
+ .await?;
+
+ let (open,): (bool,) = sqlx::query_as(
+ "SELECT valid_to IS NULL AND valid_to_precision IS NULL AND invalidated_at IS NULL
+ FROM facts WHERE id = $1",
+ )
+ .bind(relative)
+ .fetch_one(&pool)
+ .await?;
+ assert!(open, "相对的截止日是现在的截止日");
+ let before: (Option<String>, Option<String>, Option<String>) = sqlx::query_as(
+ "SELECT to_char(valid_to, 'YYYY-MM-DD'), valid_to_precision,
+ to_char(attested_to, 'YYYY-MM-DD')
+ FROM facts
+ WHERE kb_id = $1 AND object_value #>> '{value}' = '2020-06-23'
+ AND invalidated_at IS NULL",
+ )
+ .bind(f.kb)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(
+ before,
+ (None, Some("unknown".into()), Some("2020-06-26".into())),
+ "6 月 23 日那个结束了、不知哪天,锚在第十份补充协议的日期"
+ );
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(&pool)
+ .await?;
+ run
+}
diff --git a/crates/utopia-store/tests/a_timeline_holds_whatever_the_order.rs b/crates/utopia-store/tests/a_timeline_holds_whatever_the_order.rs
new file mode 100644
index 00000000..50b2632a
--- /dev/null
+++ b/crates/utopia-store/tests/a_timeline_holds_whatever_the_order.rs
@@ -0,0 +1,509 @@
+//! A timeline of a single-valued state stays whole however its values arrive.
+//!
+//! These are the #679 review reproductions, kept as regression tests: newest-first arrival,
+//! re-extraction of the same values, an undated upload, a dated value with no start, a merge
+//! followed by an arrival and a revert, a merge that moves two values and is reverted, and two
+//! late values reconciled at the same time.
+//!
+//! Skipped (not failed) without `UTOPIA_DATABASE_URL`. Each test builds and drops its own base.
+
+use serde_json::json;
+use sqlx::PgPool;
+use utopia_store::graph::Validity;
+use utopia_store::temporal::Uniqueness;
+use uuid::Uuid;
+
+struct Fixture {
+ kb: Uuid,
+ etype: Uuid,
+ lease: Uuid,
+ deadline: Uuid,
+}
+
+async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> {
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ let (etype, deadline, lease) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'rv')")
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'rv')")
+ .bind(ws)
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'rv')")
+ .bind(kb)
+ .bind(ws)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'lease', 'Lease')",
+ )
+ .bind(etype)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO relation_types (id, kb_id, key, label, kind, datatype, temporal, functional)
+ VALUES ($1, $2, 'option_deadline', 'option deadline', 'attribute', 'date', 'state', TRUE)",
+ )
+ .bind(deadline)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ let lease = entity(pool, kb, etype, "HQ Lease", lease).await?;
+ Ok(Fixture {
+ kb,
+ etype,
+ lease,
+ deadline,
+ })
+}
+
+async fn entity(
+ pool: &PgPool,
+ kb: Uuid,
+ etype: Uuid,
+ name: &str,
+ id: Uuid,
+) -> anyhow::Result<Uuid> {
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(id)
+ .bind(kb)
+ .bind(etype)
+ .bind(name)
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+fn t(day: &str) -> chrono::DateTime<chrono::Utc> {
+ format!("{day}T00:00:00Z").parse().unwrap()
+}
+
+/// Mimics extraction: insert, then reconcile only when created.
+#[allow(clippy::too_many_arguments)]
+async fn arrive_on(
+ pool: &PgPool,
+ f: &Fixture,
+ subject: Uuid,
+ value: &str,
+ from: Option<&str>,
+ doc: Option<(chrono::DateTime<chrono::Utc>, &str)>,
+ confidence: f32,
+ reconcile: bool,
+) -> anyhow::Result<Uuid> {
+ let mut validity = match from {
+ Some(from) => Validity::starting(Some(t(from)), Some("day")),
+ None => Validity::default(),
+ };
+ let mut chunk = None;
+ if let Some((doc_time, source)) = doc {
+ let (d, c) = (Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query(
+ "INSERT INTO documents (id, kb_id, filename, sha256, doc_time, doc_time_source)
+ VALUES ($1, $2, $3, $3, $4, $5)",
+ )
+ .bind(d)
+ .bind(f.kb)
+ .bind(format!("doc-{d}.html"))
+ .bind(doc_time)
+ .bind(source)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1, $2, $3, 0, $4)",
+ )
+ .bind(c)
+ .bind(f.kb)
+ .bind(d)
+ .bind(value)
+ .execute(pool)
+ .await?;
+ validity = validity.attested(Some(doc_time));
+ chunk = Some(c);
+ }
+ let object = json!({ "value": value });
+ let (id, created) = utopia_store::graph::insert_value_fact(
+ pool,
+ f.kb,
+ subject,
+ Some(f.deadline),
+ &object,
+ validity,
+ confidence,
+ )
+ .await?;
+ if let Some(c) = chunk {
+ utopia_store::graph::add_evidence(pool, id, c, Some(value), None).await?;
+ }
+ if created && reconcile {
+ utopia_store::temporal::reconcile_new_fact(
+ pool,
+ f.kb,
+ id,
+ subject,
+ f.deadline,
+ None,
+ Some(&object),
+ Uniqueness::SubjectSide,
+ validity,
+ confidence,
+ )
+ .await?;
+ }
+ Ok(id)
+}
+
+async fn arrive(pool: &PgPool, f: &Fixture, value: &str, from: &str) -> anyhow::Result<Uuid> {
+ arrive_on(pool, f, f.lease, value, Some(from), None, 0.9, true).await
+}
+
+type Seg = (String, Option<String>, Option<String>);
+
+async fn timeline_of(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<Vec<Seg>> {
+ Ok(sqlx::query_as(
+ "SELECT object_value #>> '{value}', to_char(valid_from, 'YYYY-MM-DD'),
+ to_char(valid_to, 'YYYY-MM-DD')
+ FROM facts
+ WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3 AND invalidated_at IS NULL
+ ORDER BY valid_from NULLS FIRST, valid_to NULLS LAST",
+ )
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_all(pool)
+ .await?)
+}
+
+/// Pairs of live dated rows whose intervals overlap.
+async fn overlaps(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<i64> {
+ Ok(sqlx::query_scalar(
+ "SELECT count(*) FROM facts a JOIN facts b
+ ON a.kb_id = b.kb_id AND a.subject_id = b.subject_id AND a.predicate_id = b.predicate_id
+ AND a.id < b.id AND a.object_value IS DISTINCT FROM b.object_value
+ WHERE a.kb_id = $1 AND a.subject_id = $2 AND a.predicate_id = $3
+ AND a.invalidated_at IS NULL AND b.invalidated_at IS NULL
+ AND a.valid_from IS NOT NULL AND b.valid_from IS NOT NULL
+ AND a.valid_from < COALESCE(b.valid_to, 'infinity')
+ AND b.valid_from < COALESCE(a.valid_to, 'infinity')",
+ )
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_one(pool)
+ .await?)
+}
+
+async fn cleanup(pool: &PgPool, f: &Fixture) -> anyhow::Result<()> {
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(pool)
+ .await?;
+ Ok(())
+}
+
+fn s(v: &str, from: Option<&str>, to: Option<&str>) -> Seg {
+ (v.into(), from.map(Into::into), to.map(Into::into))
+}
+
+#[tokio::test]
+async fn newest_first_arrival_leaves_no_overlap() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let f = seed(&pool).await?;
+ // 9th, 7th, 5th, 6th: newest first, then the rest
+ arrive(&pool, &f, "2020-06-23", "2020-06-08").await?;
+ arrive(&pool, &f, "2020-05-26", "2020-04-14").await?;
+ arrive(&pool, &f, "2020-03-17", "2020-02-18").await?;
+ arrive(&pool, &f, "2020-04-14", "2020-03-17").await?;
+ let rows = timeline_of(&pool, &f, f.lease).await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(n, 0, "{rows:?}");
+ Ok(())
+}
+
+#[tokio::test]
+async fn extracting_the_same_values_again_changes_nothing() -> 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 chain = [
+ ("2020-03-17", "2020-02-18"),
+ ("2020-06-23", "2020-06-08"),
+ ("2020-05-26", "2020-04-14"),
+ ("2020-04-14", "2020-03-17"),
+ ];
+ for (v, from) in chain {
+ arrive(&pool, &f, v, from).await?;
+ }
+ let before = timeline_of(&pool, &f, f.lease).await?;
+ for (v, from) in chain.iter().rev() {
+ arrive(&pool, &f, v, from).await?;
+ }
+ let after = timeline_of(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(before, after);
+ Ok(())
+}
+
+/// Undated upload: documents.create stores doc_time = now(), doc_time_source = 'upload_time'.
+#[tokio::test]
+async fn an_undated_upload_is_not_a_document_date() -> 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 now = chrono::Utc::now();
+ let bare = arrive_on(
+ &pool,
+ &f,
+ f.lease,
+ "BBHQ1",
+ None,
+ Some((now, "upload_time")),
+ 0.9,
+ true,
+ )
+ .await?;
+ arrive_on(
+ &pool,
+ &f,
+ f.lease,
+ "HPBB1",
+ Some("2021-01-01"),
+ Some((t("2021-01-01"), "content")),
+ 0.9,
+ true,
+ )
+ .await?;
+ let (open,): (bool,) = sqlx::query_as(
+ "SELECT EXISTS (SELECT 1 FROM facts WHERE id = $1 AND invalidated_at IS NULL AND valid_to IS NULL)",
+ )
+ .bind(bare)
+ .fetch_one(&pool)
+ .await?;
+ let (conflicts,): (i64,) =
+ sqlx::query_as("SELECT count(*) FROM fact_conflicts WHERE old_fact_id = $1")
+ .bind(bare)
+ .fetch_one(&pool)
+ .await?;
+ let rows = timeline_of(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ // The upload time does not place BBHQ1 in 2026, so HPBB1 (2021) does not supersede it
+ // and it does not supersede HPBB1. With no time of its own it goes to a person as a
+ // no_time pair, whichever arrives first (third review, item 11), and both stay open.
+ assert!(
+ open && conflicts == 1,
+ "an undated row is neither ordered by its upload time nor closed; conflicts={conflicts}, rows={rows:?}"
+ );
+ Ok(())
+}
+
+/// Dated start-less X (2020-08-13) left open beside Y (2016) after a no_time conflict;
+/// Z (2021) arrives. X held on 2020-08-13 < 2021, so Z is its successor.
+#[tokio::test]
+async fn a_dated_value_with_no_start_ends_at_the_next_start_after_its_date() -> 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 x = arrive_on(
+ &pool,
+ &f,
+ f.lease,
+ "BBHQ1",
+ None,
+ Some((t("2020-08-13"), "content")),
+ 0.9,
+ true,
+ )
+ .await?;
+ arrive_on(
+ &pool,
+ &f,
+ f.lease,
+ "HPBB1",
+ Some("2016-05-16"),
+ Some((t("2016-05-16"), "content")),
+ 0.9,
+ true,
+ )
+ .await?;
+ arrive_on(
+ &pool,
+ &f,
+ f.lease,
+ "NEWCO",
+ Some("2021-01-01"),
+ Some((t("2021-01-01"), "content")),
+ 0.9,
+ true,
+ )
+ .await?;
+ let rows = timeline_of(&pool, &f, f.lease).await?;
+ let (open,): (bool,) = sqlx::query_as(
+ "SELECT EXISTS (SELECT 1 FROM facts WHERE id = $1 AND invalidated_at IS NULL AND valid_to IS NULL)",
+ )
+ .bind(x)
+ .fetch_one(&pool)
+ .await?;
+ cleanup(&pool, &f).await?;
+ assert!(!open, "{rows:?}");
+ Ok(())
+}
+
+/// Merge moves a value that slots into the target's history; a later arrival slots into the
+/// merge-made boundary; revert then leaves that arrival ending at a boundary that no longer exists.
+#[tokio::test]
+async fn a_revert_releases_a_value_that_arrived_after_the_merge() -> 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 source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ arrive(&pool, &f, "A", "2020-01-01").await?;
+ arrive(&pool, &f, "B", "2020-06-01").await?;
+ arrive_on(&pool, &f, source, "D", Some("2020-04-01"), None, 0.9, true).await?;
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "rv").await?;
+ let after_merge = timeline_of(&pool, &f, f.lease).await?;
+ arrive(&pool, &f, "N", "2020-02-01").await?;
+ let after_arrival = timeline_of(&pool, &f, f.lease).await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let after_revert = timeline_of(&pool, &f, f.lease).await?;
+ let source_rows = timeline_of(&pool, &f, source).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(
+ after_revert,
+ vec![
+ s("A", Some("2020-01-01"), Some("2020-02-01")),
+ s("N", Some("2020-02-01"), Some("2020-06-01")),
+ s("B", Some("2020-06-01"), None),
+ ],
+ "N no longer ends at D's start once D goes back; merge={after_merge:?} arrival={after_arrival:?}"
+ );
+ assert_eq!(source_rows, vec![s("D", Some("2020-04-01"), None)]);
+ Ok(())
+}
+
+/// Two open values on the source (e.g. written before the predicate was declared functional):
+/// a merge re-cuts a correction it made itself, and revert cannot restore the chain.
+#[tokio::test]
+async fn a_revert_restores_both_timelines_a_merge_rewrote() -> 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 source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ arrive(&pool, &f, "A", "2020-01-01").await?;
+ arrive(&pool, &f, "B", "2020-06-01").await?;
+ arrive_on(&pool, &f, source, "D", Some("2020-03-01"), None, 0.9, false).await?;
+ arrive_on(&pool, &f, source, "E", Some("2020-04-01"), None, 0.9, false).await?;
+ let before_target = timeline_of(&pool, &f, f.lease).await?;
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "rv").await?;
+ let after_merge = timeline_of(&pool, &f, f.lease).await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let after_revert = timeline_of(&pool, &f, f.lease).await?;
+ let source_rows = timeline_of(&pool, &f, source).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(
+ after_revert, before_target,
+ "target history not restored; after merge it was {after_merge:?}"
+ );
+ // 两个值都回到源实体上。回来的是改写出的新行,不是把旧行复活:撤回也是一次认知变更,
+ // 记录轴上看得见(0057);源实体上的时间线顺带按两行重算
+ assert_eq!(
+ source_rows,
+ vec![
+ s("D", Some("2020-03-01"), Some("2020-04-01")),
+ s("E", Some("2020-04-01"), None),
+ ],
+ "source lost a value"
+ );
+ Ok(())
+}
+
+/// Two late values that slot into the same closed interval, reconciled concurrently.
+#[tokio::test]
+async fn two_late_values_reconciled_at_once_leave_one_timeline() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = sqlx::postgres::PgPoolOptions::new()
+ .max_connections(8)
+ .connect(&url)
+ .await?;
+ let mut bad = 0;
+ let trials = 40;
+ let mut example = None;
+ for _ in 0..trials {
+ let f = seed(&pool).await?;
+ arrive(&pool, &f, "2020-03-17", "2020-02-18").await?;
+ arrive(&pool, &f, "2020-06-23", "2020-06-08").await?;
+ let (a, b) = tokio::join!(
+ arrive(&pool, &f, "2020-05-26", "2020-04-14"),
+ arrive(&pool, &f, "2020-04-14", "2020-03-17"),
+ );
+ a?;
+ b?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ let rows = timeline_of(&pool, &f, f.lease).await?;
+ if n > 0 || rows.len() != 4 {
+ bad += 1;
+ example.get_or_insert(rows);
+ }
+ cleanup(&pool, &f).await?;
+ }
+ println!("RV concurrent: {bad}/{trials} trials ended with overlaps or extra rows; example={example:?}");
+ assert_eq!(bad, 0);
+ Ok(())
+}
+
+/// A merge that moves only closed rows still rearranges the target's timeline: the source's
+/// D [03-01, 05-01) and E [05-01, 07-01) arrive beside the target's open A from 01-01, and A
+/// must end where D begins.
+#[tokio::test]
+async fn a_merge_of_closed_rows_still_ends_the_open_one_they_follow() -> 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 source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ arrive(&pool, &f, "A", "2020-01-01").await?;
+ arrive_on(&pool, &f, source, "D", Some("2020-03-01"), None, 0.9, true).await?;
+ arrive_on(&pool, &f, source, "E", Some("2020-05-01"), None, 0.9, true).await?;
+ // E's end is one the text states
+ sqlx::query(
+ "UPDATE facts SET valid_to = '2020-07-01', valid_to_precision = 'day'
+ WHERE subject_id = $1 AND object_value #>> '{value}' = 'E' AND invalidated_at IS NULL",
+ )
+ .bind(source)
+ .execute(&pool)
+ .await?;
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "closed rows")
+ .await?;
+ let rows = timeline_of(&pool, &f, f.lease).await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(n, 0, "{rows:?}");
+ assert!(
+ rows.contains(&s("A", Some("2020-01-01"), Some("2020-03-01"))),
+ "{rows:?}"
+ );
+ Ok(())
+}
diff --git a/crates/utopia-store/tests/a_timeline_is_recomputed_from_the_rows_it_has.rs b/crates/utopia-store/tests/a_timeline_is_recomputed_from_the_rows_it_has.rs
new file mode 100644
index 00000000..90eb369e
--- /dev/null
+++ b/crates/utopia-store/tests/a_timeline_is_recomputed_from_the_rows_it_has.rs
@@ -0,0 +1,826 @@
+//! 一条时间线只取决于它有哪些行(#679 第二轮评审的复现,留作回归)。
+//!
+//! 第一版靠「终点正好等于另一行的起点」认出引擎画的界:后面那一段挪了(晚到的证据把
+//! 日期往前挪、补上了起点)就认不出来,界留在原地、两段叠在一起;整理一轮一轮地来,
+//! 轮数用完就静默停下;撤回合并先改行再拿锁,与落库对账互相等死,还会复活人驳回的行、
+//! 丢掉人改的区间。现在引擎画的终点记在行上(0057),每次按当下的行一次重算。
+//!
+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
+
+use serde_json::json;
+use sqlx::PgPool;
+use utopia_store::graph::Validity;
+use utopia_store::temporal::Uniqueness;
+use uuid::Uuid;
+
+struct Fixture {
+ kb: Uuid,
+ etype: Uuid,
+ lease: Uuid,
+ deadline: Uuid,
+}
+
+async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> {
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ let (etype, deadline, lease) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'recompute-test')")
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'recompute-test')")
+ .bind(ws)
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'recompute-test')",
+ )
+ .bind(kb)
+ .bind(ws)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'lease', 'Lease')",
+ )
+ .bind(etype)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO relation_types (id, kb_id, key, label, kind, datatype, temporal, functional)
+ VALUES ($1, $2, 'option_deadline', 'option deadline', 'attribute', 'date', 'state', TRUE)",
+ )
+ .bind(deadline)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ let lease = entity(pool, kb, etype, "HQ Lease", lease).await?;
+ Ok(Fixture {
+ kb,
+ etype,
+ lease,
+ deadline,
+ })
+}
+
+async fn entity(
+ pool: &PgPool,
+ kb: Uuid,
+ etype: Uuid,
+ name: &str,
+ id: Uuid,
+) -> anyhow::Result<Uuid> {
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(id)
+ .bind(kb)
+ .bind(etype)
+ .bind(name)
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+fn t(day: &str) -> chrono::DateTime<chrono::Utc> {
+ format!("{day}T00:00:00Z").parse().unwrap()
+}
+
+/// 一次观察:值、起止、出自哪天的哪种文档、置信度
+#[derive(Clone, Copy)]
+struct Seen<'a> {
+ subject: Uuid,
+ value: &'a str,
+ from: Option<&'a str>,
+ to: Option<&'a str>,
+ doc: Option<&'a str>,
+ confidence: f32,
+}
+
+fn seen<'a>(f: &Fixture, value: &'a str, from: &'a str) -> Seen<'a> {
+ Seen {
+ subject: f.lease,
+ value,
+ from: Some(from),
+ to: None,
+ doc: None,
+ confidence: 0.9,
+ }
+}
+
+/// 抽取的写法:落库(或并进已有断言)、写证据、对账——**并进已有断言的也对账**
+async fn observe(pool: &PgPool, f: &Fixture, x: Seen<'_>) -> anyhow::Result<Uuid> {
+ let mut validity = Validity {
+ from: x.from.map(t),
+ from_precision: x.from.map(|_| "day"),
+ to: x.to.map(t),
+ to_precision: x.to.map(|_| "day"),
+ attested_at: None,
+ };
+ let mut chunk = None;
+ if let Some(doc_time) = x.doc {
+ let (d, c) = (Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query(
+ "INSERT INTO documents (id, kb_id, filename, sha256, doc_time, doc_time_source)
+ VALUES ($1, $2, $3, $3, $4, 'content')",
+ )
+ .bind(d)
+ .bind(f.kb)
+ .bind(format!("doc-{d}.html"))
+ .bind(t(doc_time))
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1, $2, $3, 0, $4)",
+ )
+ .bind(c)
+ .bind(f.kb)
+ .bind(d)
+ .bind(x.value)
+ .execute(pool)
+ .await?;
+ validity = validity.attested(Some(t(doc_time)));
+ chunk = Some(c);
+ }
+ let object = json!({ "value": x.value });
+ let (id, _created) = utopia_store::graph::insert_value_fact(
+ pool,
+ f.kb,
+ x.subject,
+ Some(f.deadline),
+ &object,
+ validity,
+ x.confidence,
+ )
+ .await?;
+ if let Some(c) = chunk {
+ utopia_store::graph::add_evidence(pool, id, c, Some(x.value), None).await?;
+ }
+ utopia_store::temporal::reconcile_new_fact(
+ pool,
+ f.kb,
+ id,
+ x.subject,
+ f.deadline,
+ None,
+ Some(&object),
+ Uniqueness::SubjectSide,
+ validity,
+ x.confidence,
+ )
+ .await?;
+ Ok(id)
+}
+
+/// (值, 起点, 终点, 终点精度, 终点锚点, 读出的起点, 读出的终点)
+type Seg = (
+ String,
+ Option<String>,
+ Option<String>,
+ Option<String>,
+ Option<String>,
+ Option<String>,
+ Option<String>,
+);
+
+async fn timeline(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<Vec<Seg>> {
+ let hf = utopia_store::world_axis::facts_holds_from("f");
+ let ht = utopia_store::world_axis::facts_holds_to("f");
+ Ok(sqlx::query_as(&format!(
+ "SELECT object_value #>> '{{value}}', to_char(valid_from, 'YYYY-MM-DD'),
+ to_char(valid_to, 'YYYY-MM-DD'), valid_to_precision,
+ to_char(attested_to, 'YYYY-MM-DD'),
+ to_char({hf}, 'YYYY-MM-DD'), to_char({ht}, 'YYYY-MM-DD')
+ FROM facts f
+ WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3 AND invalidated_at IS NULL
+ ORDER BY {hf} NULLS FIRST, {ht} NULLS LAST, object_value #>> '{{value}}'"
+ ))
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_all(pool)
+ .await?)
+}
+
+/// 读出来的区间叠在一起、值又不同的行有几对
+async fn overlaps(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<i64> {
+ let hf = utopia_store::world_axis::facts_holds_from("f");
+ let ht = utopia_store::world_axis::facts_holds_to("f");
+ Ok(sqlx::query_scalar(&format!(
+ "WITH r AS (
+ SELECT f.id, f.object_value, {hf} AS hf, {ht} AS ht FROM facts f
+ WHERE f.kb_id = $1 AND f.subject_id = $2 AND f.predicate_id = $3
+ AND f.invalidated_at IS NULL)
+ SELECT count(*) FROM r a JOIN r b ON a.id < b.id
+ AND a.object_value IS DISTINCT FROM b.object_value
+ WHERE COALESCE(a.hf, '-infinity') < COALESCE(b.ht, 'infinity')
+ AND COALESCE(b.hf, '-infinity') < COALESCE(a.ht, 'infinity')"
+ ))
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_one(pool)
+ .await?)
+}
+
+async fn open_rows(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<i64> {
+ Ok(sqlx::query_scalar(
+ "SELECT count(*) FROM facts WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3
+ AND invalidated_at IS NULL AND valid_to IS NULL AND valid_to_precision IS NULL",
+ )
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_one(pool)
+ .await?)
+}
+
+async fn live_id(pool: &PgPool, f: &Fixture, subject: Uuid, value: &str) -> anyhow::Result<Uuid> {
+ Ok(sqlx::query_scalar(
+ "SELECT id FROM facts WHERE kb_id = $1 AND subject_id = $2
+ AND object_value #>> '{value}' = $3 AND invalidated_at IS NULL",
+ )
+ .bind(f.kb)
+ .bind(subject)
+ .bind(value)
+ .fetch_one(pool)
+ .await?)
+}
+
+async fn cleanup(pool: &PgPool, f: &Fixture) -> anyhow::Result<()> {
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(pool)
+ .await?;
+ Ok(())
+}
+
+async fn pool() -> anyhow::Result<Option<PgPool>> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(None);
+ };
+ Ok(Some(
+ sqlx::postgres::PgPoolOptions::new()
+ .max_connections(12)
+ .connect(&url)
+ .await?,
+ ))
+}
+
+/// 后面那一段晚到的文件补上了起点(第十一份补充协议之后,交割文件说 BBHQ1 从 7 月 31 日
+/// 起就是房东):HPBB1 止于那一天,不再叠到 8 月 13 日
+#[tokio::test]
+async fn a_start_that_arrives_later_moves_the_end_before_it() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ doc: Some("2016-05-16"),
+ ..seen(&f, "HPBB1", "2016-05-16")
+ },
+ )
+ .await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ from: None,
+ doc: Some("2020-08-13"),
+ ..seen(&f, "BBHQ1", "")
+ },
+ )
+ .await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ doc: Some("2020-09-01"),
+ ..seen(&f, "BBHQ1", "2020-07-31")
+ },
+ )
+ .await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(n, 0, "{rows:?}");
+ assert_eq!(rows[0].0, "HPBB1");
+ assert_eq!(rows[0].2.as_deref(), Some("2020-07-31"), "{rows:?}");
+ Ok(())
+}
+
+/// 没起点的后任又被一份更早的文件提到:前任的锚点跟着往前挪
+#[tokio::test]
+async fn earlier_evidence_for_a_value_with_no_start_moves_the_end_before_it() -> anyhow::Result<()>
+{
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ doc: Some("2019-01-01"),
+ ..seen(&f, "P", "2019-01-01")
+ },
+ )
+ .await?;
+ let startless = |doc| Seen {
+ from: None,
+ doc: Some(doc),
+ ..seen(&f, "S", "")
+ };
+ observe(&pool, &f, startless("2021-06-01")).await?;
+ observe(&pool, &f, startless("2020-06-01")).await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(n, 0, "{rows:?}");
+ assert_eq!(rows[0].0, "P");
+ assert_eq!(rows[0].4.as_deref(), Some("2020-06-01"), "{rows:?}");
+ Ok(())
+}
+
+/// 一条没起点的值先到,有起点的后到:文档日期从不写进日期列,也不叠
+#[tokio::test]
+async fn a_value_with_no_start_that_arrives_first_still_takes_its_place() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ from: None,
+ doc: Some("2021-01-01"),
+ ..seen(&f, "S", "")
+ },
+ )
+ .await?;
+ observe(&pool, &f, seen(&f, "P", "2019-01-01")).await?;
+ observe(&pool, &f, seen(&f, "Q", "2020-01-01")).await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ let in_date_columns: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM facts
+ WHERE kb_id = $1 AND (valid_from = '2021-01-01' OR valid_to = '2021-01-01')",
+ )
+ .bind(f.kb)
+ .fetch_one(&pool)
+ .await?;
+ let open = open_rows(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!((n, in_date_columns, open), (0, 0, 1));
+ Ok(())
+}
+
+/// 已经结束的新值与开着的旧值,谁先到都一样
+#[tokio::test]
+async fn a_closed_value_and_an_open_one_meet_the_same_way_in_either_order() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let other = entity(&pool, f.kb, f.etype, "Other Lease", Uuid::now_v7()).await?;
+ let closed = |subject| Seen {
+ subject,
+ to: Some("2021-01-01"),
+ ..seen(&f, "B", "2019-01-01")
+ };
+ observe(&pool, &f, seen(&f, "A", "2018-01-01")).await?;
+ observe(&pool, &f, closed(f.lease)).await?;
+ observe(&pool, &f, closed(other)).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: other,
+ ..seen(&f, "A", "2018-01-01")
+ },
+ )
+ .await?;
+ let a_then_b = timeline(&pool, &f, f.lease).await?;
+ let b_then_a = timeline(&pool, &f, other).await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(a_then_b, b_then_a);
+ assert_eq!(n, 0);
+ Ok(())
+}
+
+/// 七十个置信度不够的值排在前面,一个够格的值到了:每一个都关上,没有轮数用完这回事
+#[tokio::test]
+async fn doubtful_values_do_not_keep_a_sure_one_from_closing_them() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ for i in 0..70 {
+ let (value, day) = (
+ format!("w{i:02}"),
+ format!("{}-{:02}-01", 2000 + i / 12, i % 12 + 1),
+ );
+ observe(
+ &pool,
+ &f,
+ Seen {
+ confidence: 0.5,
+ ..seen(&f, &value, &day)
+ },
+ )
+ .await?;
+ }
+ observe(&pool, &f, seen(&f, "Z", "2010-01-01")).await?;
+ let open = open_rows(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(open, 1, "只有 Z 还开着");
+ Ok(())
+}
+
+/// 两条各一百个值的时间线交错着合并:一次重算排好,没有轮数上限
+#[tokio::test]
+async fn a_merge_of_two_long_timelines_leaves_one_timeline() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ for i in 0..200 {
+ let (value, day) = (
+ format!("v{i:03}"),
+ format!("{}-{:02}-01", 2010 + i / 12, i % 12 + 1),
+ );
+ let subject = if i % 2 == 0 { f.lease } else { source };
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject,
+ ..seen(&f, &value, &day)
+ },
+ )
+ .await?;
+ }
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test").await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ let open = open_rows(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!((n, open), (0, 1));
+ Ok(())
+}
+
+/// 合并两条交错的时间线再撤回:两边都回到合并之前的样子
+#[tokio::test]
+async fn a_revert_gives_both_timelines_back_as_they_were() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ for (v, d) in [
+ ("A", "2020-01-01"),
+ ("C", "2020-05-01"),
+ ("E", "2020-09-01"),
+ ] {
+ observe(&pool, &f, seen(&f, v, d)).await?;
+ }
+ for (v, d) in [
+ ("B", "2020-03-01"),
+ ("D", "2020-07-01"),
+ ("F", "2020-11-01"),
+ ] {
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, v, d)
+ },
+ )
+ .await?;
+ }
+ let (target_before, source_before) = (
+ timeline(&pool, &f, f.lease).await?,
+ timeline(&pool, &f, source).await?,
+ );
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ let merged = timeline(&pool, &f, f.lease).await?;
+ let merged_overlaps = overlaps(&pool, &f, f.lease).await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let (target_after, source_after) = (
+ timeline(&pool, &f, f.lease).await?,
+ timeline(&pool, &f, source).await?,
+ );
+ cleanup(&pool, &f).await?;
+ assert_eq!((merged.len(), merged_overlaps), (6, 0));
+ assert_eq!(target_after, target_before);
+ assert_eq!(source_after, source_before);
+ Ok(())
+}
+
+/// 合并之后又到了一个值、关上了搬来的那一行:撤回时那一行(连同关上它的改写)回源实体
+#[tokio::test]
+async fn a_revert_takes_back_a_moved_value_closed_after_the_merge() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ observe(&pool, &f, seen(&f, "A", "2019-01-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, "D", "2020-04-01")
+ },
+ )
+ .await?;
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ observe(&pool, &f, seen(&f, "N", "2020-06-01")).await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let target = timeline(&pool, &f, f.lease).await?;
+ let back = timeline(&pool, &f, source).await?;
+ cleanup(&pool, &f).await?;
+ assert!(target.iter().all(|r| r.0 != "D"), "{target:?}");
+ assert_eq!(back.len(), 1);
+ assert_eq!((back[0].0.as_str(), back[0].2.as_deref()), ("D", None));
+ assert_eq!(target[0].2.as_deref(), Some("2020-06-01"), "A 止于 N");
+ Ok(())
+}
+
+/// 合并把 A 关在 D 开始时,人把 A 的起点改成了 2019-12-01:撤回之后人改的那一行还在
+#[tokio::test]
+async fn a_revert_keeps_the_interval_a_person_corrected() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ observe(&pool, &f, seen(&f, "A", "2020-01-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, "D", "2020-04-01")
+ },
+ )
+ .await?;
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ let a = live_id(&pool, &f, f.lease, "A").await?;
+ let fixed = utopia_store::temporal::correct_interval(
+ &pool,
+ a,
+ Validity {
+ from: Some(t("2019-12-01")),
+ from_precision: Some("day"),
+ to: Some(t("2020-04-01")),
+ to_precision: Some("day"),
+ attested_at: None,
+ },
+ )
+ .await?
+ .unwrap();
+ utopia_store::temporal::reconcile_moved_facts(&pool, f.kb, &[fixed]).await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(
+ rows,
+ vec![(
+ "A".to_string(),
+ Some("2019-12-01".to_string()),
+ Some("2020-04-01".to_string()),
+ Some("day".to_string()),
+ None,
+ Some("2019-12-01".to_string()),
+ Some("2020-04-01".to_string()),
+ )]
+ );
+ Ok(())
+}
+
+/// 合并把 A 关上之后,人把 A 驳回了:撤回合并不让它回来
+#[tokio::test]
+async fn a_revert_does_not_bring_back_a_value_a_person_rejected() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ observe(&pool, &f, seen(&f, "A", "2020-01-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, "D", "2020-04-01")
+ },
+ )
+ .await?;
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ let a = live_id(&pool, &f, f.lease, "A").await?;
+ utopia_store::graph::reject_fact(&pool, f.kb, a).await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert!(rows.iter().all(|r| r.0 != "A"), "{rows:?}");
+ Ok(())
+}
+
+/// 人驳回了后任:关在它开始时的前任重新开着
+#[tokio::test]
+async fn rejecting_a_successor_reopens_the_value_it_closed() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "A", "2020-01-01")).await?;
+ let b = observe(&pool, &f, seen(&f, "B", "2020-04-01")).await?;
+ utopia_store::graph::reject_fact(&pool, f.kb, b).await?;
+ let open = open_rows(&pool, &f, f.lease).await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(open, 1);
+ assert_eq!((rows[0].0.as_str(), rows[0].2.as_deref()), ("A", None));
+ Ok(())
+}
+
+/// 人写明的终点不随时间线重算:后任走了,它仍然关着
+#[tokio::test]
+async fn an_end_a_person_wrote_is_never_recomputed() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let a = observe(&pool, &f, seen(&f, "A", "2020-01-01")).await?;
+ utopia_store::temporal::close_superseded(&pool, a, t("2020-03-01"), "day").await?;
+ let b = observe(&pool, &f, seen(&f, "B", "2020-04-01")).await?;
+ utopia_store::graph::reject_fact(&pool, f.kb, b).await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].2.as_deref(), Some("2020-03-01"));
+ Ok(())
+}
+
+/// 删掉后任出自的文档,前任重新开着;恢复文档,前任又关上
+#[tokio::test]
+async fn deleting_and_restoring_a_document_moves_the_end_it_drew() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "A", "2020-01-01")).await?;
+ let b = observe(
+ &pool,
+ &f,
+ Seen {
+ doc: Some("2020-04-01"),
+ ..seen(&f, "B", "2020-04-01")
+ },
+ )
+ .await?;
+ let doc: Uuid = sqlx::query_scalar("SELECT document_id FROM fact_evidence WHERE fact_id = $1")
+ .bind(b)
+ .fetch_one(&pool)
+ .await?;
+ let a_end = || async {
+ let (end,): (Option<String>,) = sqlx::query_as(
+ "SELECT to_char(valid_to, 'YYYY-MM-DD') FROM facts
+ WHERE kb_id = $1 AND object_value #>> '{value}' = 'A' AND invalidated_at IS NULL",
+ )
+ .bind(f.kb)
+ .fetch_one(&pool)
+ .await?;
+ anyhow::Ok(end)
+ };
+ assert_eq!(a_end().await?.as_deref(), Some("2020-04-01"));
+ utopia_store::documents::delete(&pool, f.kb, doc, None).await?;
+ let deleted = a_end().await?;
+ utopia_store::documents::restore(&pool, f.kb, doc).await?;
+ let restored = a_end().await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(deleted, None);
+ assert_eq!(restored.as_deref(), Some("2020-04-01"));
+ Ok(())
+}
+
+/// 并行落库:三个、五个值同时到,最后都是一条时间线
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+async fn values_that_arrive_together_leave_one_timeline() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ for _ in 0..10 {
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "v1", "2020-02-18")).await?;
+ observe(&pool, &f, seen(&f, "v5", "2020-06-08")).await?;
+ let (x, y, z) = tokio::join!(
+ observe(&pool, &f, seen(&f, "v4", "2020-05-10")),
+ observe(&pool, &f, seen(&f, "v2", "2020-03-17")),
+ observe(&pool, &f, seen(&f, "v3", "2020-04-14")),
+ );
+ x?;
+ y?;
+ z?;
+ let three = (
+ overlaps(&pool, &f, f.lease).await?,
+ open_rows(&pool, &f, f.lease).await?,
+ );
+ cleanup(&pool, &f).await?;
+ assert_eq!(three, (0, 1));
+
+ let f = seed(&pool).await?;
+ let (p, q, r, s, u) = tokio::join!(
+ observe(&pool, &f, seen(&f, "v4", "2020-05-10")),
+ observe(&pool, &f, seen(&f, "v2", "2020-03-17")),
+ observe(&pool, &f, seen(&f, "v5", "2020-06-08")),
+ observe(&pool, &f, seen(&f, "v1", "2020-02-18")),
+ observe(&pool, &f, seen(&f, "v3", "2020-04-14")),
+ );
+ for result in [p, q, r, s, u] {
+ result?;
+ }
+ let five = (
+ timeline(&pool, &f, f.lease).await?.len(),
+ overlaps(&pool, &f, f.lease).await?,
+ open_rows(&pool, &f, f.lease).await?,
+ );
+ cleanup(&pool, &f).await?;
+ assert_eq!(five, (5, 0, 1));
+ }
+ Ok(())
+}
+
+/// 撤回合并与落库同时进行:谁也不等死谁,最后没有叠在一起的区间
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+async fn a_revert_and_arriving_values_do_not_deadlock() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let mut errors = Vec::new();
+ for i in 0..40 {
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ for (v, d) in [
+ ("A", "2020-01-01"),
+ ("C", "2020-05-01"),
+ ("E", "2020-09-01"),
+ ] {
+ observe(&pool, &f, seen(&f, v, d)).await?;
+ }
+ for (v, d) in [("B", "2020-03-01"), ("D", "2020-07-01")] {
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, v, d)
+ },
+ )
+ .await?;
+ }
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ let delay = std::time::Duration::from_millis(i % 6);
+ let (reverted, n, m) = tokio::join!(
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge),
+ async {
+ tokio::time::sleep(delay).await;
+ observe(&pool, &f, seen(&f, "N", "2020-02-01")).await
+ },
+ observe(&pool, &f, seen(&f, "M", "2020-08-01")),
+ );
+ if let Err(e) = reverted {
+ errors.push(format!("revert: {e}"));
+ }
+ for arrived in [n, m] {
+ if let Err(e) = arrived {
+ errors.push(format!("arrive: {e}"));
+ }
+ }
+ let (target, back) = (
+ overlaps(&pool, &f, f.lease).await?,
+ overlaps(&pool, &f, source).await?,
+ );
+ cleanup(&pool, &f).await?;
+ assert_eq!((target, back), (0, 0), "trial {i}");
+ }
+ assert!(errors.is_empty(), "{errors:?}");
+ Ok(())
+}
diff --git a/crates/utopia-store/tests/the_engine_redraws_only_what_it_drew.rs b/crates/utopia-store/tests/the_engine_redraws_only_what_it_drew.rs
new file mode 100644
index 00000000..20ca6af1
--- /dev/null
+++ b/crates/utopia-store/tests/the_engine_redraws_only_what_it_drew.rs
@@ -0,0 +1,977 @@
+//! 引擎只重画自己画的终点(#679 第三轮评审的复现,留作回归)。
+//!
+//! 第二版把「引擎画的终点」记在行上、每次一次重算。评审又找出它碰坏的地方:两侧都唯一的
+//! 关系一侧关上、另一侧又打开;原文说已经结束的旧值被当成后任,关掉了当下的值;撤回合并
+//! 让记录轴回放合并窗口时答案变了;删除文档读名单早于上锁;改写把没裁的冲突撤下、把人裁过
+//! 的冲突又问一遍;原文说出的终点并进引擎关上的行之后仍被重算;删掉的文档还在给行排序;
+//! 证据写到刚被改写掉的旧行上;说不出时间的值谁先到结果不同;迁移之前关上的行永远不重算。
+//!
+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
+
+use serde_json::json;
+use sqlx::PgPool;
+use utopia_store::graph::Validity;
+use utopia_store::temporal::Uniqueness;
+use uuid::Uuid;
+
+struct Fixture {
+ kb: Uuid,
+ etype: Uuid,
+ lease: Uuid,
+ deadline: Uuid,
+}
+
+async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> {
+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ let (etype, deadline, lease) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'redraw-test')")
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'redraw-test')")
+ .bind(ws)
+ .bind(org)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'redraw-test')",
+ )
+ .bind(kb)
+ .bind(ws)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'lease', 'Lease')",
+ )
+ .bind(etype)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO relation_types (id, kb_id, key, label, kind, datatype, temporal, functional)
+ VALUES ($1, $2, 'option_deadline', 'option deadline', 'attribute', 'date', 'state', TRUE)",
+ )
+ .bind(deadline)
+ .bind(kb)
+ .execute(pool)
+ .await?;
+ let lease = entity(pool, kb, etype, "HQ Lease", lease).await?;
+ Ok(Fixture {
+ kb,
+ etype,
+ lease,
+ deadline,
+ })
+}
+
+async fn entity(
+ pool: &PgPool,
+ kb: Uuid,
+ etype: Uuid,
+ name: &str,
+ id: Uuid,
+) -> anyhow::Result<Uuid> {
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
+ )
+ .bind(id)
+ .bind(kb)
+ .bind(etype)
+ .bind(name)
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+fn t(day: &str) -> chrono::DateTime<chrono::Utc> {
+ format!("{day}T00:00:00Z").parse().unwrap()
+}
+
+/// 一次观察:值、起止、出自哪天的文档、置信度
+#[derive(Clone, Copy)]
+struct Seen<'a> {
+ subject: Uuid,
+ value: &'a str,
+ from: Option<&'a str>,
+ to: Option<&'a str>,
+ ended_unknown: bool,
+ doc: Option<&'a str>,
+ confidence: f32,
+}
+
+fn seen<'a>(f: &Fixture, value: &'a str, from: &'a str) -> Seen<'a> {
+ Seen {
+ subject: f.lease,
+ value,
+ from: Some(from),
+ to: None,
+ ended_unknown: false,
+ doc: None,
+ confidence: 0.9,
+ }
+}
+
+/// 一份自带日期的文档,一个分块
+async fn document(pool: &PgPool, kb: Uuid, day: &str, text: &str) -> anyhow::Result<(Uuid, Uuid)> {
+ let (d, c) = (Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query(
+ "INSERT INTO documents (id, kb_id, filename, sha256, doc_time, doc_time_source)
+ VALUES ($1, $2, $3, $3, $4, 'content')",
+ )
+ .bind(d)
+ .bind(kb)
+ .bind(format!("doc-{d}.html"))
+ .bind(t(day))
+ .execute(pool)
+ .await?;
+ sqlx::query(
+ "INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1, $2, $3, 0, $4)",
+ )
+ .bind(c)
+ .bind(kb)
+ .bind(d)
+ .bind(text)
+ .execute(pool)
+ .await?;
+ Ok((d, c))
+}
+
+/// 抽取的写法:落库(或并进已有断言)、写证据、对账
+async fn observe(pool: &PgPool, f: &Fixture, x: Seen<'_>) -> anyhow::Result<Uuid> {
+ let mut validity = Validity {
+ from: x.from.map(t),
+ from_precision: x.from.map(|_| "day"),
+ to: x.to.map(t),
+ to_precision: if x.ended_unknown {
+ Some("unknown")
+ } else {
+ x.to.map(|_| "day")
+ },
+ attested_at: None,
+ };
+ let mut chunk = None;
+ if let Some(day) = x.doc {
+ let (_, c) = document(pool, f.kb, day, x.value).await?;
+ validity = validity.attested(Some(t(day)));
+ chunk = Some(c);
+ }
+ let object = json!({ "value": x.value });
+ let (id, _) = utopia_store::graph::insert_value_fact(
+ pool,
+ f.kb,
+ x.subject,
+ Some(f.deadline),
+ &object,
+ validity,
+ x.confidence,
+ )
+ .await?;
+ if let Some(c) = chunk {
+ utopia_store::graph::add_evidence(pool, id, c, Some(x.value), None).await?;
+ }
+ utopia_store::temporal::reconcile_new_fact(
+ pool,
+ f.kb,
+ id,
+ x.subject,
+ f.deadline,
+ None,
+ Some(&object),
+ Uniqueness::SubjectSide,
+ validity,
+ x.confidence,
+ )
+ .await?;
+ Ok(id)
+}
+
+/// (值, 起点, 终点, 终点精度, 终点锚点)
+type Seg = (
+ String,
+ Option<String>,
+ Option<String>,
+ Option<String>,
+ Option<String>,
+);
+
+async fn timeline(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<Vec<Seg>> {
+ let hf = utopia_store::world_axis::facts_holds_from("f");
+ let ht = utopia_store::world_axis::facts_holds_to("f");
+ Ok(sqlx::query_as(&format!(
+ "SELECT object_value #>> '{{value}}', to_char(valid_from, 'YYYY-MM-DD'),
+ to_char(valid_to, 'YYYY-MM-DD'), valid_to_precision,
+ to_char(attested_to, 'YYYY-MM-DD')
+ FROM facts f
+ WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3 AND invalidated_at IS NULL
+ ORDER BY {hf} NULLS FIRST, {ht} NULLS LAST, object_value #>> '{{value}}'"
+ ))
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_all(pool)
+ .await?)
+}
+
+async fn overlaps(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<i64> {
+ let hf = utopia_store::world_axis::facts_holds_from("f");
+ let ht = utopia_store::world_axis::facts_holds_to("f");
+ Ok(sqlx::query_scalar(&format!(
+ "WITH r AS (
+ SELECT f.id, f.object_value, {hf} AS hf, {ht} AS ht FROM facts f
+ WHERE f.kb_id = $1 AND f.subject_id = $2 AND f.predicate_id = $3
+ AND f.invalidated_at IS NULL)
+ SELECT count(*) FROM r a JOIN r b ON a.id < b.id
+ AND a.object_value IS DISTINCT FROM b.object_value
+ WHERE COALESCE(a.hf, '-infinity') < COALESCE(b.ht, 'infinity')
+ AND COALESCE(b.hf, '-infinity') < COALESCE(a.ht, 'infinity')"
+ ))
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_one(pool)
+ .await?)
+}
+
+async fn open_rows(pool: &PgPool, f: &Fixture, subject: Uuid) -> anyhow::Result<i64> {
+ Ok(sqlx::query_scalar(
+ "SELECT count(*) FROM facts WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3
+ AND invalidated_at IS NULL AND valid_to IS NULL AND valid_to_precision IS NULL",
+ )
+ .bind(f.kb)
+ .bind(subject)
+ .bind(f.deadline)
+ .fetch_one(pool)
+ .await?)
+}
+
+async fn open_conflicts(pool: &PgPool, kb: Uuid) -> anyhow::Result<Vec<String>> {
+ Ok(sqlx::query_scalar(
+ "SELECT reason FROM fact_conflicts WHERE kb_id = $1 AND status = 'open' ORDER BY reason",
+ )
+ .bind(kb)
+ .fetch_all(pool)
+ .await?)
+}
+
+async fn cleanup(pool: &PgPool, f: &Fixture) -> anyhow::Result<()> {
+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
+ .bind(f.kb)
+ .execute(pool)
+ .await?;
+ Ok(())
+}
+
+async fn pool() -> anyhow::Result<Option<PgPool>> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(None);
+ };
+ Ok(Some(
+ sqlx::postgres::PgPoolOptions::new()
+ .max_connections(16)
+ .connect(&url)
+ .await?,
+ ))
+}
+
+/// 「一个人同时只领导一个项目,一个项目同时只有一个领导」:一行在两条时间线上。
+/// Pat 先领导 A、后领导 B,A 关在 B 开始时;再读到一遍「Pat 领导 A」,什么都不变
+#[tokio::test]
+async fn a_relation_unique_on_both_sides_does_not_flip_between_them() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let leads = Uuid::now_v7();
+ sqlx::query(
+ "INSERT INTO relation_types (id, kb_id, key, label, kind, temporal, functional, inverse_functional)
+ VALUES ($1, $2, 'leads', 'leads', 'relation', 'state', TRUE, TRUE)",
+ )
+ .bind(leads)
+ .bind(f.kb)
+ .execute(&pool)
+ .await?;
+ let pat = entity(&pool, f.kb, f.etype, "Pat", Uuid::now_v7()).await?;
+ let a = entity(&pool, f.kb, f.etype, "Project A", Uuid::now_v7()).await?;
+ let b = entity(&pool, f.kb, f.etype, "Project B", Uuid::now_v7()).await?;
+ let edge = |object: Uuid, from: &'static str| {
+ let pool = pool.clone();
+ let kb = f.kb;
+ async move {
+ let validity = Validity::starting(Some(t(from)), Some("day"));
+ let (id, _) = utopia_store::graph::insert_fact(
+ &pool,
+ kb,
+ pat,
+ Some(leads),
+ object,
+ validity,
+ 0.9,
+ )
+ .await?;
+ for side in [Uniqueness::SubjectSide, Uniqueness::ObjectSide] {
+ utopia_store::temporal::reconcile_new_fact(
+ &pool,
+ kb,
+ id,
+ pat,
+ leads,
+ Some(object),
+ None,
+ side,
+ validity,
+ 0.9,
+ )
+ .await?;
+ }
+ anyhow::Ok(())
+ }
+ };
+ let state = || {
+ let pool = pool.clone();
+ let kb = f.kb;
+ async move {
+ let rows: Vec<(String, Option<String>)> = sqlx::query_as(
+ "SELECT o.canonical_name, to_char(x.valid_to, 'YYYY-MM-DD')
+ FROM facts x JOIN entities o ON o.id = x.object_id
+ WHERE x.kb_id = $1 AND x.predicate_id = $2 AND x.invalidated_at IS NULL
+ ORDER BY x.valid_from",
+ )
+ .bind(kb)
+ .bind(leads)
+ .fetch_all(&pool)
+ .await?;
+ let total: i64 =
+ sqlx::query_scalar("SELECT count(*) FROM facts WHERE predicate_id = $1")
+ .bind(leads)
+ .fetch_one(&pool)
+ .await?;
+ anyhow::Ok((rows, total))
+ }
+ };
+ edge(a, "2020-01-01").await?;
+ edge(b, "2021-01-01").await?;
+ let after_b = state().await?;
+ edge(a, "2020-01-01").await?;
+ edge(a, "2020-01-01").await?;
+ let again = state().await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(
+ after_b.0,
+ vec![
+ ("Project A".to_string(), Some("2021-01-01".to_string())),
+ ("Project B".to_string(), None),
+ ]
+ );
+ assert_eq!(
+ again, after_b,
+ "再读一遍没变的事实,时间线不该变,也不该多出行"
+ );
+ Ok(())
+}
+
+/// 原文说一个旧值已经结束(「HPBB1 不再是房东」出自 2021 年的文件):那份文件的日期只说明
+/// 那天之前它结束了,不能把它排在 2021 年、去关上当下的值
+#[tokio::test]
+async fn a_value_the_text_says_has_ended_does_not_close_the_current_one() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ doc: Some("2016-05-16"),
+ ..seen(&f, "HPBB1", "2016-05-16")
+ },
+ )
+ .await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ doc: Some("2020-09-01"),
+ ..seen(&f, "BBHQ1", "2020-07-31")
+ },
+ )
+ .await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ from: None,
+ ended_unknown: true,
+ doc: Some("2021-03-01"),
+ ..seen(&f, "HPBB1", "")
+ },
+ )
+ .await?;
+ let recital = open_rows(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+
+ // 结束在一个写明的日子,没有起点,出自一份更晚的文件
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "NEW", "2020-01-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ from: None,
+ to: Some("2019-12-31"),
+ doc: Some("2021-03-01"),
+ ..seen(&f, "OLD", "")
+ },
+ )
+ .await?;
+ let dated = open_rows(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!((recital, dated), (1, 1));
+ Ok(())
+}
+
+/// 合并;合并之后到了一个值,关上了搬来的那一行;撤回。回放合并窗口里的某一刻,
+/// 撤回之前问与之后问答案一样
+#[tokio::test]
+async fn a_revert_does_not_rewrite_what_the_merge_window_held() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ observe(&pool, &f, seen(&f, "A", "2019-01-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, "D", "2020-04-01")
+ },
+ )
+ .await?;
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ observe(&pool, &f, seen(&f, "N", "2020-06-01")).await?;
+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;
+ let mid: chrono::DateTime<chrono::Utc> = sqlx::query_scalar("SELECT clock_timestamp()")
+ .fetch_one(&pool)
+ .await?;
+ let replay = || {
+ let pool = pool.clone();
+ let (kb, pred, lease) = (f.kb, f.deadline, f.lease);
+ async move {
+ let held = utopia_store::record_axis::facts_held_at("f", 2);
+ let owner = utopia_store::record_axis::owner_at("f", "subject_id", Some(2), false);
+ let rows: Vec<(String, Option<String>, bool)> = sqlx::query_as(&format!(
+ "SELECT object_value #>> '{{value}}', to_char(valid_to, 'YYYY-MM-DD'), {owner} = $3
+ FROM facts f WHERE f.kb_id = $1 AND f.predicate_id = $4 AND {held}
+ ORDER BY valid_from"
+ ))
+ .bind(kb)
+ .bind(mid)
+ .bind(lease)
+ .bind(pred)
+ .fetch_all(&pool)
+ .await?;
+ anyhow::Ok(rows)
+ }
+ };
+ let before = replay().await?;
+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let after = replay().await?;
+ let back = timeline(&pool, &f, source).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(after, before);
+ assert_eq!(back.len(), 1, "D 回到源实体上");
+ Ok(())
+}
+
+/// 删除文档等锁的时候,那一行被时间线重算改写成了新的一行:锁上之后重读名单,新行一并作废
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+async fn a_deletion_that_waited_for_the_lock_still_takes_the_rewritten_row() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let a = observe(
+ &pool,
+ &f,
+ Seen {
+ doc: Some("2020-01-01"),
+ ..seen(&f, "A", "2020-01-01")
+ },
+ )
+ .await?;
+ let doc: Uuid = sqlx::query_scalar("SELECT document_id FROM fact_evidence WHERE fact_id = $1")
+ .bind(a)
+ .fetch_one(&pool)
+ .await?;
+ // 像一次时间线重算那样持着锁,把 A 改写成新的一行
+ let key = format!("timeline:{}:{}:{}:SubjectSide", f.kb, f.lease, f.deadline);
+ let mut tx = pool.begin().await?;
+ sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))")
+ .bind(&key)
+ .execute(&mut *tx)
+ .await?;
+ let deleting = {
+ let pool = pool.clone();
+ let kb = f.kb;
+ tokio::spawn(async move { utopia_store::documents::delete(&pool, kb, doc, None).await })
+ };
+ tokio::time::sleep(std::time::Duration::from_millis(400)).await;
+ let rewritten = Uuid::now_v7();
+ sqlx::query(
+ "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_value, valid_from,
+ valid_from_precision, valid_to, valid_to_precision, confidence,
+ supersedes, attested_from, end_derived)
+ SELECT $1, kb_id, subject_id, predicate_id, object_value, valid_from, valid_from_precision,
+ '2020-06-01', 'day', confidence, id, attested_from, TRUE FROM facts WHERE id = $2",
+ )
+ .bind(rewritten)
+ .bind(a)
+ .execute(&mut *tx)
+ .await?;
+ sqlx::query(
+ "INSERT INTO fact_evidence (fact_id, chunk_id, quote, document_id, doc_version)
+ SELECT $1, chunk_id, quote, document_id, doc_version FROM fact_evidence WHERE fact_id = $2",
+ )
+ .bind(rewritten)
+ .bind(a)
+ .execute(&mut *tx)
+ .await?;
+ sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1")
+ .bind(a)
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ deleting.await??;
+ let alive: bool = sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id = $1")
+ .bind(rewritten)
+ .fetch_one(&pool)
+ .await?;
+ cleanup(&pool, &f).await?;
+ assert!(!alive, "唯一的出处删掉了,改写出来的那一行也该作废");
+ Ok(())
+}
+
+/// 并行落库:证据不会写到刚被改写掉的旧行上,每一条现存的行都有证据
+#[tokio::test(flavor = "multi_thread", worker_threads = 6)]
+async fn evidence_follows_a_row_rewritten_while_it_was_written() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ for _ in 0..30 {
+ let f = seed(&pool).await?;
+ let dated = |value: &'static str, day: &'static str, with_start: bool| Seen {
+ from: with_start.then_some(day),
+ doc: Some(day),
+ ..seen(&f, value, day)
+ };
+ let (p, q, r, s, u) = tokio::join!(
+ observe(&pool, &f, dated("v4", "2020-05-10", true)),
+ observe(&pool, &f, dated("v2", "2020-03-17", true)),
+ observe(&pool, &f, dated("v5", "2020-06-08", false)),
+ observe(&pool, &f, dated("v1", "2020-02-18", true)),
+ observe(&pool, &f, dated("v3", "2020-04-14", false)),
+ );
+ for result in [p, q, r, s, u] {
+ result?;
+ }
+ let bare: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM facts f WHERE f.kb_id = $1 AND f.invalidated_at IS NULL
+ AND NOT EXISTS (SELECT 1 FROM fact_evidence fe WHERE fe.fact_id = f.id)",
+ )
+ .bind(f.kb)
+ .fetch_one(&pool)
+ .await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!((bare, n), (0, 0));
+ }
+ Ok(())
+}
+
+/// 人裁过「两个都留着」的一对,两行后来被改写,不再问第二遍
+#[tokio::test]
+async fn a_pair_a_person_kept_is_not_asked_again_after_a_rewrite() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "A", "2020-01-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ confidence: 0.5,
+ ..seen(&f, "B", "2020-03-01")
+ },
+ )
+ .await?;
+ let asked: Vec<Uuid> =
+ sqlx::query_scalar("SELECT id FROM fact_conflicts WHERE kb_id = $1 AND status = 'open'")
+ .bind(f.kb)
+ .fetch_all(&pool)
+ .await?;
+ for conflict in &asked {
+ utopia_store::temporal::resolve_conflict(&pool, f.kb, *conflict, "keep", None, "day")
+ .await?;
+ }
+ observe(&pool, &f, seen(&f, "C", "2020-06-01")).await?;
+ let again = open_conflicts(&pool, f.kb).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(asked.len(), 1);
+ assert_eq!(again, Vec::<String>::new());
+ Ok(())
+}
+
+/// 同一天开始的两个值记了一对冲突;后来的值把两行都改写了,那对冲突还开着
+#[tokio::test]
+async fn an_unresolved_conflict_stays_open_when_its_rows_are_rewritten() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "P", "2020-01-01")).await?;
+ observe(&pool, &f, seen(&f, "Q", "2020-01-01")).await?;
+ let before = open_conflicts(&pool, f.kb).await?;
+ observe(&pool, &f, seen(&f, "R", "2020-06-01")).await?;
+ let after = open_conflicts(&pool, f.kb).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(before, vec!["simultaneous".to_string()]);
+ assert_eq!(after, before);
+ Ok(())
+}
+
+/// 引擎把 A 关在 B 开始时;一份文件写明 A 到那天为止。后来 B 被驳回,A 仍然到那天为止
+#[tokio::test]
+async fn an_end_the_text_states_on_a_row_the_engine_closed_is_kept() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "A", "2020-01-01")).await?;
+ let b = observe(&pool, &f, seen(&f, "B", "2020-03-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ to: Some("2020-03-01"),
+ doc: Some("2020-04-01"),
+ ..seen(&f, "A", "2020-01-01")
+ },
+ )
+ .await?;
+ utopia_store::graph::reject_fact(&pool, f.kb, b).await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].2.as_deref(), Some("2020-03-01"));
+ Ok(())
+}
+
+/// 删掉一个没起点的值最早那份文档:它不再在那天排进时间线,前任的锚点跟着往后挪
+#[tokio::test]
+async fn a_deleted_document_no_longer_dates_a_row() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "P", "2019-01-01")).await?;
+ let s = observe(
+ &pool,
+ &f,
+ Seen {
+ from: None,
+ doc: Some("2020-06-01"),
+ ..seen(&f, "S", "")
+ },
+ )
+ .await?;
+ let early: Uuid =
+ sqlx::query_scalar("SELECT document_id FROM fact_evidence WHERE fact_id = $1")
+ .bind(s)
+ .fetch_one(&pool)
+ .await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ from: None,
+ doc: Some("2021-06-01"),
+ ..seen(&f, "S", "")
+ },
+ )
+ .await?;
+ utopia_store::documents::delete(&pool, f.kb, early, None).await?;
+ let rows = timeline(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(rows[0].0, "P");
+ assert_eq!(rows[0].4.as_deref(), Some("2021-06-01"), "{rows:?}");
+ Ok(())
+}
+
+/// 一个说不出时间的值(没起点,也没有自带日期的证据)与一个有日期的值:谁先到都是同一对
+/// 冲突交给人,两边都开着
+#[tokio::test]
+async fn a_value_with_no_time_meets_a_dated_one_the_same_way_in_either_order() -> anyhow::Result<()>
+{
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let other = entity(&pool, f.kb, f.etype, "Other Lease", Uuid::now_v7()).await?;
+ let keyless = |subject| Seen {
+ subject,
+ from: None,
+ ..seen(&f, "K", "")
+ };
+ observe(&pool, &f, keyless(f.lease)).await?;
+ observe(&pool, &f, seen(&f, "N", "2020-01-01")).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: other,
+ ..seen(&f, "N", "2020-01-01")
+ },
+ )
+ .await?;
+ observe(&pool, &f, keyless(other)).await?;
+ let shape = |rows: Vec<Seg>| {
+ rows.into_iter()
+ .map(|r| (r.0, r.2, r.3))
+ .collect::<std::collections::BTreeSet<_>>()
+ };
+ let k_first = shape(timeline(&pool, &f, f.lease).await?);
+ let n_first = shape(timeline(&pool, &f, other).await?);
+ let asked = open_conflicts(&pool, f.kb).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(k_first, n_first);
+ assert_eq!(asked, vec!["no_time".to_string(), "no_time".to_string()]);
+ Ok(())
+}
+
+/// 迁移之前引擎关上的行由 0057 回填成引擎画的:已有的库再来两份晚到的补充协议,
+/// 时间线照样排得开。人关上的、原文说出终点的不回填
+#[tokio::test]
+async fn the_migration_marks_the_ends_the_old_engine_drew() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let backfill =
+ include_str!("../../../migrations/0057_an_end_the_engine_drew_moves_with_what_follows.sql");
+ let backfill = &backfill[backfill.find("UPDATE facts c").expect("0057 回填语句")..];
+
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "v1", "2020-02-18")).await?;
+ observe(&pool, &f, seen(&f, "v5", "2020-06-08")).await?;
+ // 人手动关上的一段,与引擎关的形状一样
+ let other = entity(&pool, f.kb, f.etype, "Other Lease", Uuid::now_v7()).await?;
+ let by_hand = observe(
+ &pool,
+ &f,
+ Seen {
+ subject: other,
+ ..seen(&f, "h1", "2020-01-01")
+ },
+ )
+ .await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: other,
+ confidence: 0.5,
+ ..seen(&f, "h2", "2020-03-01")
+ },
+ )
+ .await?;
+ utopia_store::temporal::close_superseded(&pool, by_hand, t("2020-03-01"), "day").await?;
+ sqlx::query(
+ "INSERT INTO audit_events (id, kb_id, action, target_kind, target_id)
+ VALUES ($1, $2, 'fact.close', 'fact', $3)",
+ )
+ .bind(Uuid::now_v7())
+ .bind(f.kb)
+ .bind(by_hand)
+ .execute(&pool)
+ .await?;
+ // 迁移之前的库:这一列全是假
+ sqlx::query("UPDATE facts SET end_derived = FALSE WHERE kb_id = $1")
+ .bind(f.kb)
+ .execute(&pool)
+ .await?;
+ sqlx::raw_sql(backfill).execute(&pool).await?;
+ let marked: Vec<String> = sqlx::query_scalar(
+ "SELECT object_value #>> '{value}' FROM facts
+ WHERE kb_id = $1 AND invalidated_at IS NULL AND end_derived ORDER BY 1",
+ )
+ .bind(f.kb)
+ .fetch_all(&pool)
+ .await?;
+ observe(&pool, &f, seen(&f, "v2", "2020-03-17")).await?;
+ observe(&pool, &f, seen(&f, "v3", "2020-04-14")).await?;
+ let n = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(marked, vec!["v1".to_string()], "只有引擎关的那一段");
+ assert_eq!(n, 0);
+ Ok(())
+}
+
+/// 连环合并里撤回头一环(#679 第四轮评审):S 并进 T、T 再并进 C,撤回 S→T。
+/// S 的事实此刻挂在 C 身上,也要跟着 S 回去,C 的时间线上不留它
+#[tokio::test]
+async fn undoing_the_first_merge_of_a_chain_brings_its_facts_home() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease S", Uuid::now_v7()).await?;
+ let chain_end = entity(&pool, f.kb, f.etype, "Lease C", Uuid::now_v7()).await?;
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, "S1", "2020-01-01")
+ },
+ )
+ .await?;
+ let first =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ utopia_store::resolution::merge_entities(&pool, f.kb, f.lease, chain_end, None, "test").await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, first).await?;
+ let home = timeline(&pool, &f, source).await?;
+ let tail = timeline(&pool, &f, chain_end).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(home.len(), 1, "S1 回到 S 上:{home:?}");
+ assert_eq!(home[0].0, "S1");
+ assert!(tail.iter().all(|s| s.0 != "S1"), "C 上不留 S1:{tail:?}");
+ Ok(())
+}
+
+/// 撤回合并把合并之后改写出来的行送回源实体时,它们身上开着的冲突跟着走(#679 第四轮评审)
+#[tokio::test]
+async fn a_revert_keeps_the_conflict_its_rows_carried() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let source = entity(&pool, f.kb, f.etype, "Lease Agreement", Uuid::now_v7()).await?;
+ for value in ["D", "E"] {
+ observe(
+ &pool,
+ &f,
+ Seen {
+ subject: source,
+ ..seen(&f, value, "2020-01-01")
+ },
+ )
+ .await?;
+ }
+ let merge =
+ utopia_store::resolution::merge_entities(&pool, f.kb, source, f.lease, None, "test")
+ .await?;
+ observe(&pool, &f, seen(&f, "N", "2020-06-01")).await?;
+ utopia_store::resolution::revert_merge(&pool, f.kb, merge).await?;
+ let conflicts = open_conflicts(&pool, f.kb).await?;
+ let back = timeline(&pool, &f, source).await?;
+ let overlap = overlaps(&pool, &f, source).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(back.len(), 2, "D 与 E 回到源实体:{back:?}");
+ assert!(
+ overlap == 0 || conflicts.contains(&"simultaneous".to_string()),
+ "两行叠在一起就得有一条开着的冲突:{conflicts:?}"
+ );
+ Ok(())
+}
+
+/// 删一篇给几百个实体各记了一个属性的文档:改拿谓词一级的锁,删得掉,也都作废了。
+/// 逐条锁的时候几千条就要几千把锁,两万条时锁表装不下(#679 第四轮评审)
+#[tokio::test]
+async fn deleting_a_document_that_dates_many_timelines_still_works() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ let (doc, chunk) = document(&pool, f.kb, "2020-01-01", "a table").await?;
+ // 默认 300 条走谓词锁那条路; 可以复现评审时锁表装不下的规模
+ let n: usize = std::env::var("REDRAW_BULK_N")
+ .ok()
+ .and_then(|v| v.parse().ok())
+ .unwrap_or(300);
+ for i in 0..n {
+ let holder = entity(&pool, f.kb, f.etype, &format!("Lease {i}"), Uuid::now_v7()).await?;
+ let (id, _) = utopia_store::graph::insert_value_fact(
+ &pool,
+ f.kb,
+ holder,
+ Some(f.deadline),
+ &json!({ "value": format!("2021-01-{:02}", i % 28 + 1) }),
+ Validity {
+ from: Some(t("2020-01-01")),
+ from_precision: Some("day"),
+ ..Default::default()
+ },
+ 0.9,
+ )
+ .await?;
+ utopia_store::graph::add_evidence(&pool, id, chunk, Some("row"), None).await?;
+ }
+ let report = utopia_store::documents::delete(&pool, f.kb, doc, None).await?;
+ let restored = utopia_store::documents::restore(&pool, f.kb, doc).await;
+ let live: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM facts WHERE kb_id = $1 AND predicate_id = $2 AND invalidated_at IS NULL",
+ )
+ .bind(f.kb)
+ .bind(f.deadline)
+ .fetch_one(&pool)
+ .await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!(report.invalidated_facts, n);
+ assert!(restored.is_ok(), "{:?}", restored.err());
+ assert_eq!(live, n as i64, "撤销删除之后全都回来");
+ Ok(())
+}
+
+/// 删掉没起点那一行最早的证据文档:引擎的锚点挪到第二份文档,读出来的起点也跟着挪,
+/// 两段不叠;撤销删除又都回去(#679 第四轮评审)
+#[tokio::test]
+async fn deleting_the_first_document_moves_the_read_start_with_the_anchor() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let f = seed(&pool).await?;
+ observe(&pool, &f, seen(&f, "P", "2019-01-01")).await?;
+ for day in ["2020-06-01", "2021-06-01"] {
+ observe(
+ &pool,
+ &f,
+ Seen {
+ from: None,
+ doc: Some(day),
+ ..seen(&f, "S", day)
+ },
+ )
+ .await?;
+ }
+ let first: Uuid = sqlx::query_scalar(
+ "SELECT d.id FROM fact_evidence fe JOIN chunks c ON c.id = fe.chunk_id
+ JOIN documents d ON d.id = c.document_id
+ WHERE d.kb_id = $1 ORDER BY d.doc_time LIMIT 1",
+ )
+ .bind(f.kb)
+ .fetch_one(&pool)
+ .await?;
+ let before = overlaps(&pool, &f, f.lease).await?;
+ utopia_store::documents::delete(&pool, f.kb, first, None).await?;
+ let after_delete = overlaps(&pool, &f, f.lease).await?;
+ utopia_store::documents::restore(&pool, f.kb, first).await?;
+ let after_restore = overlaps(&pool, &f, f.lease).await?;
+ cleanup(&pool, &f).await?;
+ assert_eq!((before, after_delete, after_restore), (0, 0, 0));
+ 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 bcf1042d..cd849d7e 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 · fourth cut (2026-09-11): a **dated** ending closes the open row the same way, and every superseding row carries the edge's qualifiers
+- **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 · fifth cut (2026-09-14, #679): the temporal engine places a value in its timeline by the same anchors the reads use — a row with no stated start is ordered by its earliest dated evidence, and a predecessor it supersedes closes as ended-unknown anchored there; an end the engine drew is marked (`facts.end_derived`, migration 0057) and every change to a timeline recomputes those ends from the rows it has; a deadline stated relative to an event is stored as written, flagged `relative`, and closes the dated one before it the same way
- **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).
@@ -66,6 +66,32 @@ Why a column and not a join over `fact_evidence` at read time: the slider filter
Why not the document's date in `valid_from` / `valid_to` with a marker precision: 0003 refused this for the end — the indeterminate instant — and the reason holds for the start. Every reader of a stated column would have to check the precision before believing the date, and the panel would print "since 2024-02-20" for a fact the text only places *by* then. The anchor lives in a column that says what it is.
+> **Revised 2026-09-14 (#679, #681 §1): the engine orders by the same anchor the reads use.** This decision gave the *reads* a rule for a row with no stated start — it holds from its first evidence — and left the temporal engine on stated starts alone. The engine closed a start-less open row at whatever new start arrived. That gave two wrong timelines on the Blackbaud HQ lease chain. The 11th amendment (2020-08-13) says the landlord is BBHQ1 without saying since when; when the 2016 lease's HPBB1 arrived later, BBHQ1 was closed at 2016, a landlord before the lease existed. And a start-less deadline kept overlapping the dated ones around it.
+>
+> The engine now orders a start-less row by its earliest evidence date, and closes the row before it the way this record already writes an undated ending. The predecessor gets `valid_to` NULL, precision `'unknown'`, and `attested_to` set to that date, so the reads return it until that date and the start-less row from it: they abut and do not overlap. A document date is still never written into `valid_from` or `valid_to`.
+>
+> An end the engine draws is only the shadow of the value that follows. If that value later gains a stated start, is found in an earlier document, is reverted out of a merge, or loses its document, the end has to move with it. The first version of this cut recognised such ends by one test: the end equals some other row's start. As soon as the successor moved, the test failed and the old end stayed, overlapping. The review showed four more consequences: a cap on tidy rounds that stopped silently, order dependence, a revert that deadlocked with extraction, and a revert that brought back a value a person had rejected.
+>
+> So the row now says who drew its end. `end_derived` is true only on rows the engine closed. The text's own ends, a person's corrections and a person's conflict decisions keep it false, and are never recomputed. When a document later states the end the engine had drawn, the row takes the stated end and the flag goes false. Any change to a timeline recomputes it in one pass: a value arrives, is observed again with new evidence, is merged in or reverted out, is rejected, or its document is deleted or restored. Each open or engine-closed row ends where the next different value with enough confidence begins, or stays open. The result depends only on which rows exist; the order they came in does not change it.
+>
+> A relation unique on both sides (functional and inverse-functional) puts each row on two timelines. Its end is the earlier of the two ends its sides draw. One lock per such predicate lets a recompute read the other side.
+>
+> A transaction that touches several timelines takes all their advisory locks in a fixed order before it changes any row, and re-reads what it will change once it holds them. That covers merge, merge revert, and document deletion or restore. A revert moves the merge's own ledger rows back in place. A live row rewritten after the merge is superseded by a new row on the source, and an invalidated one stays on the target, so replaying the merge window on the recording axis still answers as it did.
+>
+> A rewrite carries the row's open conflicts to the new row. A pair a person resolved as "keep both" is not asked again for rewritten rows descended from the same pair.
+>
+> Migration 0057 backfills the flag conservatively on rows the old engine closed:
+> - the rewritten parent was open, with the same start and value;
+> - the end equals the start of another value on the same timeline;
+> - there is no evidence the parent lacked;
+> - no person closed or re-timed the parent.
+>
+> Anything else counts as stated.
+>
+> The ordering date is `min(doc_time)` over the row's evidence, counting only documents whose own date is known (`doc_time_source` of `content` or `source`). It is not `attested_from`, for the reason section 3 gives in reverse. An undated document anchors `attested_from` at the moment of recording, and ordering by that would read every start-less row as holding now, turning an ordinary "X, then Y from 2021" into a conflict. A row with no dated evidence has no place in the timeline; when it and another value are both open, the pair goes to a person, whichever arrived first. A start-less row whose text says it has ended is not placed by its evidence either: that date shows it ended by then, not that it held then. Evidence from a deleted document no longer places a row. The concern above about copied evidence does not bite here: a superseding row's evidence is the same documents, and the earliest of them is still the earliest date the row is known to hold.
+>
+> A value the text gives only relative to an event, such as a deadline of "45 days after the Trigger Date", has no calendar date. The extractor marks it `relative` (the model decides; the server matches no wording), and the server stores it as written. It is still the current value, so it closes the dated value before it in the same way, anchored at its own document. It is never compared or sorted as a date. An unflagged value that is not a date is still dropped as `attr_datatype`.
+
### 3. Where the anchor comes from
- **Extraction.** `insert_fact` / `insert_value_fact` take the document's `doc_time`. When an observation merges into an existing row (same stated start), `attested_at = least(attested_at, doc_time)`: an earlier document is earlier evidence. A document with no date anchors at the moment of recording — the best the ledger has.
diff --git a/docs/decisions/README.md b/docs/decisions/README.md
index 2f8e5c24..40ea635b 100644
--- a/docs/decisions/README.md
+++ b/docs/decisions/README.md
@@ -45,7 +45,7 @@ The test for writing one: if someone (including us) looks at a piece of code in
| 0019 | [The second clock can be rewound](0019-the-second-clock-can-be-rewound.md) | Implemented in three cuts · `held_at` and `as_of` on every graph read (#317), entities' own clock by unwinding `entity_merges` (#337), retrieval as of a moment · the entity panel and `entity_facts` rewind derivations too (#549) · the control on the graph page is still open (#307), full-text recall is still "now" only |
| 0020 | [An auditor reads it without us](0020-an-auditor-reads-it-without-us.md) | Implemented · `GET /kbs/{id}/export?format=turtle\|jsonld` streams the base as RDF, `rdf.rs` holds the mapping · SPARQL waits, and the record says why (#308) |
| 0021 | [A rule reads attributes and concludes a type](0021-a-rule-reads-attributes-and-concludes-a-type.md) | Implemented (#359) · `derived_facts` widened to match `facts`, rules authored in `attribute_rules` from the ontology page, evaluated in the materialisation job, explained in the entity panel with their premises · read-only over MCP, writing a rule stays out · no canvas marker, and a conclusion is rewritten rather than edited |
-| 0022 | [An unknown date is not an open one](0022-an-unknown-date-is-not-an-open-one.md) | Implemented in two cuts (#394 and the derived cut) · `world_axis` predicate beside `record_axis`, `facts.attested_at` anchors a missing start or an undated end at the document that attests it, every read and both client filters on the read interval, an undated ending closes the dated row it ends, derived rows intersect premise intervals as read and carry no precision on an anchored bound · two anchors (`attested_from` / `attested_to`), so a bare open row closes too (#393) |
+| 0022 | [An unknown date is not an open one](0022-an-unknown-date-is-not-an-open-one.md) | Implemented in two cuts (#394 and the derived cut) · `world_axis` predicate beside `record_axis`, `facts.attested_at` anchors a missing start or an undated end at the document that attests it, every read and both client filters on the read interval, an undated ending closes the dated row it ends, derived rows intersect premise intervals as read and carry no precision on an anchored bound · two anchors (`attested_from` / `attested_to`), so a bare open row closes too (#393) · the temporal engine orders a start-less row by its earliest dated evidence and closes its predecessor as ended-unknown there; ends the engine drew are marked and recomputed from the rows a timeline has (0057); a relative deadline is stored as written (#679) |
| 0023 | [RSS observations are not documents](0023-rss-observations-are-not-documents.md) | Implemented in #326 |
| 0024 | [The world axis reaches the second](0024-the-world-axis-reaches-the-second.md) | Implemented · the precision ladder runs year → second (not below: no source states less), a stored value is truncated to its precision by CHECK on all three tables, a clock time without a zone is a date, one list spells the ladder in the extractor, the renderers, the export and the evaluator; a derived bound takes the precision of the premise that set it |
| 0025 | [Governance reads the ledger before it decides](0025-governance-reads-the-ledger-before-it-decides.md) | Cut 1 implemented · a per-base `governance` switch, a `govern` job that works the duplicates queue first in, first out with each head's cluster, precedents pulled from the ledger into the prompt, a gate where the agent's own confidence decides and history lowers the bar or blocks (revised after the first big-file run), every look a row in `agent_decisions`, answers through the person's own decide path · the switch, the Agent queue, the proposal chip and the Overview section in the UI · a pair the batch cannot settle is looked at again with tools and then decided or asked about, within a daily budget · two reverts in a week turn the switch off and raise an alert · identity rules the model reads and two it cannot argue with (name shapes, type families), measured against a hand-labeled set of 411 name pairs by `scripts/bench/govern.mjs` |
diff --git a/migrations/0057_an_end_the_engine_drew_moves_with_what_follows.sql b/migrations/0057_an_end_the_engine_drew_moves_with_what_follows.sql
new file mode 100644
index 00000000..df10846a
--- /dev/null
+++ b/migrations/0057_an_end_the_engine_drew_moves_with_what_follows.sql
@@ -0,0 +1,55 @@
+-- 一行的终点是谁定的:时态引擎按时间线推出来的,还是原文、人写下的(#679 评审)。
+--
+-- 引擎把一段开放的值关在下一段开始时。那个终点没有哪份文件说过,它只是「后面那一段
+-- 从这天起」的影子:后面那一段挪了(晚到的证据把它的日期往前挪、补上了起点)、走了
+-- (撤回合并、删了文档),这个终点就该跟着变。原文写明的终点、人改过的区间一律不重算——
+-- 两份原文说法相左时,该由人来挑。
+--
+-- 从前靠「终点正好等于另一行的起点」认出引擎画的界,后面那一段一挪就认不出来,界留在
+-- 原地,两段叠在一起。现在由写这一行的人记下:引擎写的为真,其余为假。
+ALTER TABLE facts ADD COLUMN end_derived BOOLEAN NOT NULL DEFAULT FALSE;
+
+-- 回填:迁移之前引擎关上的行。不回填的话它们一律算写明的,永远不重算——已有的库再来两份
+-- 晚到的补充协议,照样叠在一起。
+--
+-- 只认得出来的那种,宁可漏:一条现存的闭合行,
+-- · 谓词是声明了唯一性的状态关系;
+-- · 它改写的前身还开着,起点、值都与它相同——引擎关一行只换终点;
+-- · 它的终点正好是同一条时间线上另一个值的起点——引擎只关在那里;
+-- · 它的证据没有前身之外的——原文说出终点的那次观察会带来新证据;
+-- · 前身上没有人的决定:没被人裁决关上(fact_conflicts 的 closed),没被人手动关上或改过
+-- 时间(审计里的 fact.close / fact.time_corrected)。
+UPDATE facts c
+ SET end_derived = TRUE
+ FROM facts p, relation_types r
+ WHERE p.id = c.supersedes
+ AND r.id = c.predicate_id
+ AND r.temporal = 'state' AND (r.functional OR r.inverse_functional)
+ AND c.invalidated_at IS NULL
+ AND c.valid_to IS NOT NULL
+ AND p.valid_to IS NULL AND p.valid_to_precision IS NULL
+ AND p.valid_from IS NOT DISTINCT FROM c.valid_from
+ AND p.subject_id = c.subject_id
+ AND p.predicate_id = c.predicate_id
+ AND p.object_id IS NOT DISTINCT FROM c.object_id
+ AND p.object_value IS NOT DISTINCT FROM c.object_value
+ AND EXISTS (
+ SELECT 1 FROM facts o
+ WHERE o.kb_id = c.kb_id AND o.predicate_id = c.predicate_id
+ AND o.valid_from = c.valid_to
+ AND ((r.functional AND o.subject_id = c.subject_id
+ AND (o.object_id IS DISTINCT FROM c.object_id
+ OR o.object_value IS DISTINCT FROM c.object_value))
+ OR (r.inverse_functional AND c.object_id IS NOT NULL
+ AND o.object_id = c.object_id AND o.subject_id <> c.subject_id)))
+ AND NOT EXISTS (
+ SELECT 1 FROM fact_evidence ce
+ WHERE ce.fact_id = c.id
+ AND NOT EXISTS (SELECT 1 FROM fact_evidence pe
+ WHERE pe.fact_id = p.id AND pe.chunk_id = ce.chunk_id))
+ AND NOT EXISTS (
+ SELECT 1 FROM fact_conflicts x
+ WHERE x.old_fact_id = p.id AND x.resolution = 'closed')
+ AND NOT EXISTS (
+ SELECT 1 FROM audit_events a
+ WHERE a.target_id = p.id AND a.action IN ('fact.close', 'fact.time_corrected'));