World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
A statement is attested by the date its own section speaks from, and one with no time holds at every moment (#995)
WaylandYangea1fde8f52ac
diff --git a/crates/utopia-cli/src/main.rs b/crates/utopia-cli/src/main.rsindex 97fa065f..2d518cad 100644--- a/crates/utopia-cli/src/main.rs+++ b/crates/utopia-cli/src/main.rs@@ -82,7 +82,7 @@ struct ManifestDataDir {/// not a side effect of a code change.// 是迁移文件的**个数**,不是最大的编号(守卫 `schema_version_policy_compares_against_current`// 按个数比):编号有空缺时两者不同——0071 由一个开放 PR 占着,0072 先落,个数是 71-const CURRENT_SCHEMA_VERSION: u32 = 90;+const CURRENT_SCHEMA_VERSION: u32 = 91;fn main() -> anyhow::Result<()> {dotenvy::dotenv().ok();diff --git a/crates/utopia-core/src/models.rs b/crates/utopia-core/src/models.rsindex 62f52962..9a6f064c 100644--- a/crates/utopia-core/src/models.rs+++ b/crates/utopia-core/src/models.rs@@ -833,6 +833,10 @@ pub struct EntityFact {/// 不再自己把 NULL 解释成开放pub holds_from: Option<DateTime<Utc>>,pub holds_to: Option<DateTime<Utc>>,+ /// 见证从文档的哪条日期来,照文档的字(0064):「提报日期 2026年9月4日」。没有起点的+ /// 事实,`holds_from` 就是这个见证的日期——它是「截至」,不是起点+ #[sqlx(default)]+ pub attested_by: Option<String>,pub confidence: f32,pub evidence_count: i64,/// 证据全部停留在来源文档的旧版(未被现行内容确认;不代表事实失效)diff --git a/crates/utopia-server/src/api/tools.rs b/crates/utopia-server/src/api/tools.rsindex 685ad453..0c28606f 100644--- a/crates/utopia-server/src/api/tools.rs+++ b/crates/utopia-server/src/api/tools.rs@@ -1498,6 +1498,7 @@ mod tests {valid_to_precision: Some("day".into()),holds_from: Some(t("2023-06-01T00:00:00Z")),holds_to: Some(t("2024-02-20T00:00:00Z")),+ attested_by: None,confidence: 0.9,evidence_count: 1,stale: false,diff --git a/crates/utopia-server/src/api/tools_graph.rs b/crates/utopia-server/src/api/tools_graph.rsindex fd2c8206..0a1beb83 100644--- a/crates/utopia-server/src/api/tools_graph.rs+++ b/crates/utopia-server/src/api/tools_graph.rs@@ -1494,6 +1494,7 @@ mod tests {valid_to_precision: None,holds_from: Some("2021-01-01T00:00:00Z".parse().unwrap()),holds_to: None,+ attested_by: None,confidence: 0.9,evidence_count: 1,stale: false,diff --git a/crates/utopia-server/src/extraction_open.rs b/crates/utopia-server/src/extraction_open.rsindex ea25b66b..237c194a 100644--- a/crates/utopia-server/src/extraction_open.rs+++ b/crates/utopia-server/src/extraction_open.rs@@ -16,8 +16,9 @@//! 塌成一个,0041 的名字事实照记),被描述的东西建成没有名字事实的实体//! (`create_described`),免得「一家医院」成了召回的桥。//!-//! 两根时间轴(0022 / #714):`attested_from` 永远是此刻;`attested_at` 只在文档日期-//! 来自内容或来源系统时才用它——上传时刻与文件修改时间都不是文档说的日期。+//! 两根时间轴(0022 / #714 / 0064):陈述入库时的见证只认来源系统给文档的日期,没有就+//! 留空——处理文档的时刻不是文档说的日期。文档自己说的日期(各块报上来的 `t`)在抽完+//! 之后由时间解析那一步按节写到陈述上(`time_resolution::attest_statements`)。use crate::extraction::{chat_retrying_rate_limits_at, drop_signal, incomplete_reason, origin_ceiling, resolve_handle,diff --git a/crates/utopia-server/src/time_context_tests.rs b/crates/utopia-server/src/time_context_tests.rsindex 0cf4a730..d5a5765d 100644--- a/crates/utopia-server/src/time_context_tests.rs+++ b/crates/utopia-server/src/time_context_tests.rs@@ -180,6 +180,30 @@ async fn last_week_is_counted_from_the_date_of_its_own_section() -> anyhow::Resu.fetch_all(&pool).await?;let day = |y, m, d| Utc.with_ymd_and_hms(y, m, d, 0, 0, 0).unwrap();+ // 每条陈述由它自己那一节的提报日期作证,连那条日期的名字;不是处理文档的那一刻+ let attested: Vec<(String, Option<DateTime<Utc>>, Option<String>)> = sqlx::query_as(+ "SELECT o.canonical_name, f.attested_from, f.attested_by+ FROM facts f JOIN entities o ON o.id = f.object_id+ WHERE f.kb_id = $1 AND f.layer = 'open' ORDER BY o.canonical_name",+ )+ .bind(kb)+ .fetch_all(&pool)+ .await?;+ assert_eq!(+ attested,+ vec![+ (+ "泰国市场".to_string(),+ Some(day(2026, 9, 4)),+ Some("提报日期 2026年9月4日".to_string())+ ),+ (+ "韩国市场".to_string(),+ Some(day(2026, 8, 28)),+ Some("提报日期 2026年8月28日".to_string())+ ),+ ]+ );assert_eq!(dated,vec![diff --git a/crates/utopia-server/src/time_resolution.rs b/crates/utopia-server/src/time_resolution.rsindex 83637081..283f485e 100644--- a/crates/utopia-server/src/time_resolution.rs+++ b/crates/utopia-server/src/time_resolution.rs@@ -426,8 +426,26 @@ pub async fn resolve_document(state: &AppState, document_id: Uuid) -> anyhow::Re}};- // 抽取报上来的期间(0064)并进文档命名的期间里:解释时按名字指它+ // 老路读开头定出来的日期(抽取一条都没报的时候):它就是整篇的起算点,和抽取报上来的+ // 一样进条目里,下面给陈述作证、给相对的时间词当基准都从条目里找let mut context = context;+ if context.entries.is_empty() {+ if let (Some(date), Some(words)) = (context.date.clone(), context.date_words.clone()) {+ context.entries.push(utopia_extract::time::TimeEntry {+ kind: "now".into(),+ name: String::new(),+ words,+ from: date,+ to: None,+ scope: Vec::new(),+ chunk: None,+ char_start: None,+ });+ }+ }+ attest_statements(state, &doc, &context).await?;++ // 抽取报上来的期间(0064)并进文档命名的期间里:解释时按名字指它for e in context.entries.clone()@@ -653,6 +671,57 @@ pub async fn resolve_document(state: &AppState, document_id: Uuid) -> anyhow::ReOk(())}+/// 这篇文档的陈述各由它所在那一节里文本说话的那一刻作证(0064 决定 3)。+///+/// 一条陈述在哪一节,看它的引文在它那一块里的位置、那一块正文里的标题;管着它的是范围+/// 最深的那个 `now`。没有的不写——见证留空,这条陈述在任何时点都成立(决定 5)。+/// 这是「截至」,不是起点:报告里的「所属市场为全球」说的是那天是这样,不是从那天起+async fn attest_statements(+ state: &AppState,+ doc: &utopia_core::models::Document,+ context: &DocumentDating,+) -> anyhow::Result<()> {+ if !context.entries.iter().any(|e| e.kind == "now") {+ return Ok(());+ }+ let pool = &state.pool;+ let rows: Vec<(Uuid, String, Option<i32>)> = sqlx::query_as(+ "SELECT fe.fact_id, c.text, fe.quote_start+ FROM fact_evidence fe+ JOIN facts f ON f.id = fe.fact_id+ JOIN chunks c ON c.id = fe.chunk_id+ WHERE fe.document_id = $1 AND f.layer = 'open' AND f.invalidated_at IS NULL",+ )+ .bind(doc.id)+ .fetch_all(pool)+ .await?;+ let mut moved = 0usize;+ for (fact_id, text, start) in rows {+ let path = headings_at(&text, usize::try_from(start.unwrap_or(0)).unwrap_or(0));+ let Some(now) = now_in_force(&context.entries, &path) else {+ continue;+ };+ let Some((at, _)) = parts_to_time(&now.from) else {+ continue;+ };+ let by = if now.name.is_empty() {+ now.words.clone()+ } else {+ format!("{} {}", now.name, now.words)+ };+ if utopia_store::graph::attest_statement(pool, fact_id, at, &by).await? {+ moved += 1;+ }+ }+ // 已经物化出来的类型化行跟着它们的陈述走+ let typed = utopia_store::materialize::sync_typed_attestation(pool, doc.kb_id).await?;+ if moved > 0 || typed > 0 {+ tracing::info!(document_id = %doc.id, statements = moved, typed, "陈述按它所在那一节的日期作了证");+ state.emit_graph(doc.kb_id);+ }+ Ok(())+}+#[cfg(test)]#[path = "time_context_tests.rs"]mod context_tests;diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rsindex 963533b0..fced2b7c 100644--- a/crates/utopia-store/src/graph.rs+++ b/crates/utopia-store/src/graph.rs@@ -947,6 +947,32 @@ async fn attest_earlier<'e>(Ok(())}+/// 一条陈述的见证:它所在那一节里文本说话的那一刻,连同那条日期的名字(0064 决定 3)。+///+/// 只往早挪(同 [`attest_earlier`]):同一句话在更早的文档里说过,见证就是更早的那份。+/// 名字跟着日期走——日期没动,名字也不动。物化出来的类型化行自己没有出处,跟着陈述走+/// (`materialize::sync_typed_attestation`,调用方在写完一篇文档的陈述之后叫一次)+pub async fn attest_statement(+ pool: &PgPool,+ fact_id: Uuid,+ at: chrono::DateTime<chrono::Utc>,+ by: &str,+) -> AppResult<bool> {+ let moved = sqlx::query(+ "UPDATE facts+ SET attested_from = $2, attested_by = $3+ WHERE id = $1 AND layer = 'open' AND invalidated_at IS NULL+ AND (attested_from IS NULL OR attested_from > $2+ OR (attested_from = $2 AND attested_by IS DISTINCT FROM $3))",+ )+ .bind(fact_id)+ .bind(at)+ .bind(by)+ .execute(pool)+ .await?;+ Ok(moved.rows_affected() > 0)+}+/// 字面值宾语的事实(object_value 通道,问数映射首个消费者)。/// 去重:同 (S,P) 且 object_value 完全相等的 live 事实只存一条。#[allow(clippy::too_many_arguments)]@@ -985,8 +1011,10 @@ pub async fn insert_value_fact(/// **不走 [`insert_fact_inner`] 那道门**:不问 `predicate_temporal`,不过/// `Validity::under`,不写任何 `valid_*`,也不碰时间线。一条开放陈述在世界轴上还/// 没有位置——它提到的时间是照抄的字(`time_mentions`),把字读成日期是 0045-/// 后面那几刀的事。这里只有记录轴:`attested_from` 是文档日期(调用方只在-/// `doc_time_source IN ('content', 'source')` 时传,#714)或此刻。+/// 后面那几刀的事。这里只有记录轴:`attested_from` 是来源系统给文档的日期(调用方只在+/// `doc_time_source IN ('content', 'source')` 时传,#714),没有就留空——**不填此刻**+/// (0064 决定 5):处理文档的时刻不是文档说的日期。文档自己说的日期在抽完之后由+/// [`attest_statement`] 写上来。////// 调用方随后要把 `proposed_predicate = phrase` 写到证据上([`add_evidence_located`]),/// 于是所有已经容得下空谓词的读路径(`fact_surface_predicate`)不改一字就按短语显示它@@ -1041,12 +1069,12 @@ pub async fn insert_open_statement(FactObject::Entity(_) => {"INSERT INTO facts (id, kb_id, subject_id, layer, phrase, object_id,confidence, attested_from)- VALUES ($1, $2, $3, 'open', $4, $5, $6, COALESCE($7, now()))"+ VALUES ($1, $2, $3, 'open', $4, $5, $6, $7)"}FactObject::Value(_) => {"INSERT INTO facts (id, kb_id, subject_id, layer, phrase, object_value,confidence, attested_from)- VALUES ($1, $2, $3, 'open', $4, $5, $6, COALESCE($7, now()))"+ VALUES ($1, $2, $3, 'open', $4, $5, $6, $7)"}};let mut ins = sqlx::query(insert_sql)@@ -1070,7 +1098,8 @@ pub async fn insert_open_statement(/// 结束端的三种状态与 [`Validity`] 同一张表:`(None, None)` 仍在持续、/// `(None, Some("unknown"))` 结束了不知哪天、`(Some(t), Some(精度))` 某时结束。/// 结束了不知哪天的要有自己的锚点(`facts_ended_unknown_has_anchor`,#393):说出结束的-/// 就是这条陈述自己的文档,锚点取 `attested_from`;其余两种状态把 `attested_to` 清空+/// 就是这条陈述自己的文档,锚点取 `attested_from`,文档没说自己是哪天的(0064 决定 5)+/// 就取账本记下它的那一刻;其余两种状态把 `attested_to` 清空////// 没有这一行、它不是开放行、或它已作废:一行不改,返回 `not_an_open_statement`pub async fn set_open_validity(@@ -1092,7 +1121,7 @@ pub async fn set_open_validity(SET valid_from = $2, valid_from_precision = $3,valid_to = $4, valid_to_precision = $5, valid_from_grade = $6,attested_to = CASE WHEN $4 IS NULL AND $5 = 'unknown'- THEN COALESCE(attested_to, attested_from) END+ THEN COALESCE(attested_to, attested_from, recorded_at) ENDWHERE id = $1 AND layer = 'open' AND invalidated_at IS NULL",).bind(fact_id)@@ -1693,7 +1722,7 @@ pub async fn entity_detail(CASE WHEN {subject} = $2 THEN {object} ELSE {subject} END AS other_id,o.canonical_name AS other_name, ot.label AS other_type, f.object_value,f.valid_from, f.valid_from_precision, f.valid_to, f.valid_to_precision,- {holds_from} AS holds_from, {holds_to} AS holds_to, f.confidence,+ {holds_from} AS holds_from, {holds_to} AS holds_to, f.attested_by, f.confidence,(SELECT count(*) FROM fact_evidence fe WHERE fe.fact_id = f.id) AS evidence_count,(EXISTS (SELECT 1 FROM fact_evidence fe WHERE fe.fact_id = f.id)AND NOT EXISTS (SELECT 1 FROM fact_evidence fediff --git a/crates/utopia-store/src/materialize.rs b/crates/utopia-store/src/materialize.rsindex a3e4a89c..15f85aed 100644--- a/crates/utopia-store/src/materialize.rs+++ b/crates/utopia-store/src/materialize.rs@@ -413,6 +413,9 @@ async fn materialize_in_tx(.execute(&mut **tx).await?;}+ // 3a. 见证跟着来源陈述走(0064 决定 3、5):类型化的行自己没有出处。上面的写入口在+ // 陈述没有见证时填的是此刻(那是给人写的事实用的缺省),这里改回陈述的+ sync_typed_attestation(&mut **tx, kb_id).await?;// 3b. 已批准的规则算隐含行(0044 决定 3 第五片)。读数只查缓存:缓存里没有的这一轮// 不算,`read_phrases` 填上之后再来。短语规则按陈述触发,类别词规则按实体触发let implied = imply_in_tx(tx, kb_id, &mut written).await?;@@ -429,6 +432,34 @@ async fn materialize_in_tx())}+/// 类型化行的见证 = 它的来源陈述里最早的那个见证,连同那条日期的名字;来源陈述一条见证+/// 都没有的,它也没有(任何时点都成立,0064 决定 5)。只动从陈述物化出来的行:人写的、+/// 规则算的没有 `typed_fact_sources`,不在这里+pub async fn sync_typed_attestation<'e>(+ ex: impl sqlx::Executor<'e, Database = sqlx::Postgres>,+ kb_id: Uuid,+) -> AppResult<u64> {+ let moved = sqlx::query(+ "UPDATE facts t+ SET attested_from = src.at, attested_by = src.by+ FROM (SELECT ts.fact_id,+ min(s.attested_from) AS at,+ (array_agg(s.attested_by ORDER BY s.attested_from NULLS LAST, s.id))[1] AS by+ FROM typed_fact_sources ts+ JOIN facts s ON s.id = ts.statement_id+ WHERE s.kb_id = $1+ GROUP BY ts.fact_id) src+ WHERE t.id = src.fact_id AND t.kb_id = $1 AND t.layer = 'typed'+ AND t.invalidated_at IS NULL+ AND (t.attested_from IS DISTINCT FROM src.at+ OR t.attested_by IS DISTINCT FROM src.by)",+ )+ .bind(kb_id)+ .execute(ex)+ .await?;+ Ok(moved.rows_affected())+}+#[derive(sqlx::FromRow)]struct Implied {rule: Uuid,diff --git a/crates/utopia-store/src/pending.rs b/crates/utopia-store/src/pending.rsindex 750bbbed..82280351 100644--- a/crates/utopia-store/src/pending.rs+++ b/crates/utopia-store/src/pending.rs@@ -423,7 +423,8 @@ async fn confirm_open() -> AppResult<Confirmed> {// 记录轴的锚点只认文档自己说的日期(#714):`doc_time_source` 是 content / source// 才取 `doc_time`,上传时间与文件 mtime 不算。记忆文档是 'none',于是这里为空,- // `insert_open_statement` 落成此刻——人此刻说的话,人就是证据+ // 落成此刻——人此刻点了头的话,人就是证据。抽取那条路没有这个缺省(0064 决定 5):+ // 那里没有人在场,文档没说的就是没有let attested_at: Option<chrono::DateTime<chrono::Utc>> = sqlx::query_scalar("SELECT d.doc_time FROM chunks c JOIN documents d ON d.id = c.document_idWHERE c.id = $1 AND d.doc_time_source IN ('content', 'source')",@@ -443,7 +444,7 @@ async fn confirm_open(v.subject_id,phrase,object,- attested_at,+ attested_at.or_else(|| Some(chrono::Utc::now())),v.confidence,).await?;diff --git a/crates/utopia-store/src/reasoning.rs b/crates/utopia-store/src/reasoning.rsindex 0db87acd..d127e2c0 100644--- a/crates/utopia-store/src/reasoning.rs+++ b/crates/utopia-store/src/reasoning.rs@@ -1239,7 +1239,7 @@ fn read_span(from_precision: Option<&str>,to: Option<chrono::DateTime<chrono::Utc>>,to_precision: Option<&str>,- attested_from: chrono::DateTime<chrono::Utc>,+ attested_from: Option<chrono::DateTime<chrono::Utc>>,attested_to: Option<chrono::DateTime<chrono::Utc>>,) -> (Option<i64>, Option<i64>, bool, bool) {use crate::graph::Temporal;@@ -1251,8 +1251,10 @@ fn read_span(// 0031 之前写下的事件行终点是空的,按起点那个桶读Temporal::Event => {return match from {+ // 没日期的事件区间为空。没有见证的(0064 决定 5)也一样:两端都落在+ // 读不到的远处,和任何区间都不相交None => {- let a = attested_from.timestamp();+ let a = attested_from.map_or(i64::MAX / 4, |a| a.timestamp());(Some(a), Some(a), true, true)}Some(f) => {@@ -1268,7 +1270,11 @@ fn read_span(}let (f, from_anchored) = match from {Some(x) => (Some(x.timestamp()), false),- None => (Some(attested_from.timestamp()), true),+ // 没有起点:从见证起;连见证都没有的,下界开放——任何时点都成立(0064 决定 5)+ None => match attested_from {+ Some(a) => (Some(a.timestamp()), true),+ None => (None, false),+ },};// 结束未知的行按 CHECK 必带 attested_to;万一没有,按开放读——宁可多推一点,// 也不凭空造一个终点@@ -1510,7 +1516,7 @@ type EdgeRow = (Option<String>,Option<String>,f32,- chrono::DateTime<chrono::Utc>,+ Option<chrono::DateTime<chrono::Utc>>,Option<chrono::DateTime<chrono::Utc>>,);@@ -1840,7 +1846,7 @@ type AttrFactRow = (Option<String>,f32,Option<Uuid>,- chrono::DateTime<chrono::Utc>,+ Option<chrono::DateTime<chrono::Utc>>,Option<chrono::DateTime<chrono::Utc>>,);diff --git a/crates/utopia-store/src/world_axis.rs b/crates/utopia-store/src/world_axis.rsindex be640247..3fe3b4c8 100644--- a/crates/utopia-store/src/world_axis.rs+++ b/crates/utopia-store/src/world_axis.rs@@ -28,7 +28,10 @@ fn temporal_of(alias: &str) -> String {}/// `facts`:读出来的下界——原文给了起点用起点,否则从最早的证据起。-/// 恒常没有下界(NULL 即开放):证据日期闸的是「从何时起知道」,恒常的东西不从何时起+/// 恒常没有下界(NULL 即开放):证据日期闸的是「从何时起知道」,恒常的东西不从何时起。+///+/// 既没有起点也没有见证的行(0064 决定 5):文档没说它什么时候成立,也没说自己是哪天的,+/// 下界开放——任何时点都成立。没日期的事件除外,见 [`facts_hold_at`]pub fn facts_holds_from(alias: &str) -> String {format!("CASE WHEN {temporal} = 'eternal' THEN NULL \@@ -60,8 +63,21 @@ pub fn facts_holds_to(alias: &str) -> String {/// `facts`:断言在 T 时刻成立。`$param` 为 NULL 即不过滤。/// 两端都可能开放(恒常),所以是纯粹的区间包含+///+/// 没日期的事件发生过但不知何时,任何时点都不算成立(0022)。有见证时它的两端同为见证,+/// 区间是空的;没有见证时(0064 决定 5)两端都是 NULL,读成区间就成了两端开放,所以单独+/// 挡掉——不拿 `infinity` 当端点,那两个值还要投影给读的人。没有谓词的行(开放陈述)+/// 的时间语义是 NULL,读作状态:比较用 `IS NOT DISTINCT FROM`,否则整个谓词是 NULL,+/// 放在 WHERE 里就把每一条开放陈述都滤掉了pub fn facts_hold_at(alias: &str, param: usize) -> String {- interval_holds_at(&facts_holds_from(alias), &facts_holds_to(alias), param)+ format!(+ "(${param}::timestamptz IS NULL \+ OR (NOT ({temporal} IS NOT DISTINCT FROM 'event' AND {alias}.valid_from IS NULL \+ AND {alias}.attested_from IS NULL) \+ AND {holds}))",+ temporal = temporal_of(alias),+ holds = interval_holds_at(&facts_holds_from(alias), &facts_holds_to(alias), param),+ )}/// 纯粹的区间包含,NULL 一端即开放。派生行与幽灵边(0017 §3,区间在 `detail` 里)diff --git a/crates/utopia-store/tests/store/a_statement_is_attested_by_what_its_document_says.rs b/crates/utopia-store/tests/store/a_statement_is_attested_by_what_its_document_says.rsnew file mode 100644index 00000000..063865c2--- /dev/null+++ b/crates/utopia-store/tests/store/a_statement_is_attested_by_what_its_document_says.rs@@ -0,0 +1,158 @@+//! 陈述的见证来自文档自己说的日期,没有就是没有(0064 决定 3、5)。+//!+//! - 抽取写下的开放陈述不再拿处理文档的那一刻当见证:文档没说就留空;+//! - 留空的行在世界轴上任何时点都成立(下界开放);没日期的事件除外,它照旧任何时点都不成立;+//! - `attest_statement` 写上见证和那条日期的名字,只往早挪;+//! - 物化出来的类型化行跟着它的来源陈述走。+//!+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。++use chrono::{DateTime, TimeZone, Utc};+use sqlx::PgPool;+use utopia_store::graph::{self, FactObject};+use uuid::Uuid;++const ORG: &str = "statement-attested-by-document-test";++async fn anchor(+ pool: &PgPool,+ fact: Uuid,+) -> anyhow::Result<(Option<DateTime<Utc>>, Option<String>)> {+ Ok(+ sqlx::query_as("SELECT attested_from, attested_by FROM facts WHERE id = $1")+ .bind(fact)+ .fetch_one(pool)+ .await?,+ )+}++/// 这一行在 T 时刻成立吗,按读路径自己的谓词+async fn holds_at(pool: &PgPool, fact: Uuid, at: DateTime<Utc>) -> anyhow::Result<bool> {+ let sql = format!(+ "SELECT {} FROM facts f WHERE f.id = $1",+ utopia_store::world_axis::facts_hold_at("f", 2)+ );+ Ok(sqlx::query_scalar(&sql)+ .bind(fact)+ .bind(at)+ .fetch_one(pool)+ .await?)+}++#[tokio::test]+async fn an_unattested_statement_holds_at_every_moment_and_an_attested_one_from_its_date(+) -> anyhow::Result<()> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ sqlx::query("DELETE FROM organizations WHERE name = $1")+ .bind(ORG)+ .execute(&pool)+ .await?;+ let (org, ws, kb, watch, market, launch) = (+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ );+ sqlx::raw_sql(&format!(+ "INSERT INTO organizations(id,name) VALUES ('{org}','{ORG}');+ INSERT INTO workspaces(id,org_id,name) VALUES ('{ws}','{org}','{ORG}');+ INSERT INTO knowledge_bases(id,workspace_id,name) VALUES ('{kb}','{ws}','{ORG}');+ INSERT INTO entities(id,kb_id,canonical_name) VALUES+ ('{watch}','{kb}','码表'), ('{market}','{kb}','全球');+ INSERT INTO relation_types(id,kb_id,key,label,kind,temporal,description) VALUES+ ('{launch}','{kb}','launched_in','launched in','relation','event','');"+ ))+ .execute(&pool)+ .await?;++ let run = async {+ let long_ago = Utc.with_ymd_and_hms(2015, 1, 1, 0, 0, 0).unwrap();+ let report_day = Utc.with_ymd_and_hms(2026, 9, 4, 0, 0, 0).unwrap();+ let earlier_report = Utc.with_ymd_and_hms(2026, 8, 28, 0, 0, 0).unwrap();++ // 文档没说自己是哪天的:没有见证,任何时点都成立+ let (statement, _) = graph::insert_open_statement(+ &pool,+ kb,+ watch,+ "所属市场为",+ FactObject::Entity(market),+ None,+ 0.9,+ )+ .await?;+ assert_eq!(anchor(&pool, statement).await?, (None, None));+ assert!(holds_at(&pool, statement, long_ago).await?, "no time at all: holds at every moment");+ assert!(holds_at(&pool, statement, Utc::now()).await?);++ // 它所在那一节的日期作证:从那天起读得到,之前读不到(0022 的下界)+ assert!(graph::attest_statement(&pool, statement, report_day, "提报日期 2026年9月4日").await?);+ assert_eq!(+ anchor(&pool, statement).await?,+ (Some(report_day), Some("提报日期 2026年9月4日".to_string()))+ );+ assert!(!holds_at(&pool, statement, long_ago).await?);+ assert!(holds_at(&pool, statement, report_day).await?);+ // 更早的文档也说过:见证往早挪,名字跟着;更晚的不动它+ assert!(graph::attest_statement(&pool, statement, earlier_report, "提报日期 2026年8月28日").await?);+ assert!(!graph::attest_statement(&pool, statement, report_day, "提报日期 2026年9月4日").await?);+ assert_eq!(+ anchor(&pool, statement).await?,+ (Some(earlier_report), Some("提报日期 2026年8月28日".to_string()))+ );++ // 物化出来的类型化行跟着来源陈述走;来源没有见证的,它也没有+ let (typed, bare_typed) = (Uuid::now_v7(), Uuid::now_v7());+ let (bare, _) = graph::insert_open_statement(+ &pool,+ kb,+ watch,+ "上市于",+ FactObject::Entity(market),+ None,+ 0.9,+ )+ .await?;+ for (id, source) in [(typed, statement), (bare_typed, bare)] {+ sqlx::query(+ "INSERT INTO facts (id, kb_id, subject_id, object_id, predicate_id, layer, from_statement_id)+ VALUES ($1, $2, $3, $4, $5, 'typed', $6)",+ )+ .bind(id)+ .bind(kb)+ .bind(watch)+ .bind(market)+ .bind(launch)+ .bind(source)+ .execute(&pool)+ .await?;+ sqlx::query("INSERT INTO typed_fact_sources (fact_id, statement_id) VALUES ($1, $2)")+ .bind(id)+ .bind(source)+ .execute(&pool)+ .await?;+ }+ utopia_store::materialize::sync_typed_attestation(&pool, kb).await?;+ assert_eq!(+ anchor(&pool, typed).await?,+ (Some(earlier_report), Some("提报日期 2026年8月28日".to_string()))+ );+ assert_eq!(anchor(&pool, bare_typed).await?, (None, None));+ // 没日期的事件任何时点都不算成立(0022),没有见证也一样+ assert!(!holds_at(&pool, bare_typed, long_ago).await?);+ assert!(!holds_at(&pool, bare_typed, Utc::now()).await?);+ Ok::<(), anyhow::Error>(())+ }+ .await;++ sqlx::query("DELETE FROM organizations WHERE name = $1")+ .bind(ORG)+ .execute(&pool)+ .await?;+ run+}diff --git a/crates/utopia-store/tests/store/a_time_mention_is_resolved_against_its_document.rs b/crates/utopia-store/tests/store/a_time_mention_is_resolved_against_its_document.rsindex 697db59d..3861d3fc 100644--- a/crates/utopia-store/tests/store/a_time_mention_is_resolved_against_its_document.rs+++ b/crates/utopia-store/tests/store/a_time_mention_is_resolved_against_its_document.rs@@ -404,9 +404,10 @@ async fn a_mention_is_read_then_computed_then_written_to_the_open_row() -> anyhovalid_to_precision: Some("unknown".into()),});- // 结束了不知哪天的有自己的锚点:说出结束的就是这条陈述自己的文档(#393)+ // 结束了不知哪天的有自己的锚点:说出结束的就是这条陈述自己的文档(#393)。+ // 这篇文档没说自己是哪天的,陈述没有见证(0064 决定 5),锚点取账本记下它的那一刻let anchored: (bool,) = sqlx::query_as(- "SELECT attested_to IS NOT NULL AND attested_to = attested_from+ "SELECT attested_to IS NOT NULL AND attested_to = COALESCE(attested_from, recorded_at)FROM facts WHERE id = $1",).bind(fact)diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rsindex f9500757..775641a7 100644--- a/crates/utopia-store/tests/store/main.rs+++ b/crates/utopia-store/tests/store/main.rs@@ -84,6 +84,7 @@ mod a_secret_is_sealed_at_rest;mod a_signature_holds_on_every_path;mod a_source_kind_is_listed_once;mod a_source_reaches_only_where_it_was_granted;+mod a_statement_is_attested_by_what_its_document_says;mod a_stopped_chat_is_replayed_as_incomplete;mod a_time_mention_is_resolved_against_its_document;mod a_time_mention_is_words_not_a_date;diff --git a/docs/decisions/0064-extraction-reads-time-as-it-goes.md b/docs/decisions/0064-extraction-reads-time-as-it-goes.mdindex 20bd30ee..12b6d947 100644--- a/docs/decisions/0064-extraction-reads-time-as-it-goes.md+++ b/docs/decisions/0064-extraction-reads-time-as-it-goes.md@@ -1,6 +1,6 @@# 0064 · Extraction reads time as it goes-- **Status**: In progress · accepted 2026-09-28 · cut 1 built (#990, the bench) · cut 2 built (#991): each chunk reports the dates its passage states with the headings they sit under, a relative expression is measured from the reference point of its own section, a heading that names one time dates the statements under it, a quarter and a week are read · open: cut 3 (as-of, null attestation, reads, the workbench line), cut 4 · decision 1 is revised below: the context does not depend on the order chunks are read in+- **Status**: In progress · accepted 2026-09-28 · cut 1 built (#990, the bench) · cut 2 built (#991): each chunk reports the dates its passage states with the headings they sit under, a relative expression is measured from the reference point of its own section, a heading that names one time dates the statements under it, a quarter and a week are read · cut 3 built (#995, migration 0100): a statement is attested by the moment the text of its own section speaks from, with that date's name; no attestation when the document states none, and such a row holds at every moment; the workbench says "as of" · open: cut 4 · decision 1 is revised below: the context does not depend on the order chunks are read in- **Written**: 2026-09-28 (conventions in the [README](README.md))- **Related**: [#987](https://github.com/deeplethe/utopia/issues/987) (every relation of an entity reads "undated"); [0045](0045-a-time-mention-is-resolved-against-its-document.md) decisions 1–5, which this record keeps and whose cut 2 it replaces; [0022](0022-an-unknown-date-is-not-an-open-one.md) decision 3 and the lower bound of a start-less row; [0044](0044-the-ontology-is-a-view-over-what-documents-say.md) cut 1 (open statements); [#714](https://github.com/deeplethe/utopia/issues/714); draft [#988](https://github.com/deeplethe/utopia/pull/988), which this supersedes@@ -48,9 +48,11 @@ The separate dating call over the opening goes away when extraction reported anyAs-of is not a start. "码表所属市场为全球" in a report of 4 September says the market was global on that day, not since that day. It is stored as the statement's attestation with the name of the entry that gave it (`attested_from`, `attested_by`), which is what 0045 decision 5 meant the column to hold; `valid_from` is written only when the words state a start.+*As built with cut 3.* The entry is chosen by structure, not named by the model per statement: a statement is attested by the `now` entry in force at the position of its quote, the same rule that gives a relative expression its reference point. The other named dates of a section (a data cut-off, an effective date) are kept in the context with their names and do not attest anything yet; choosing between them for one statement is a judgement about the statement and would cost a slot in the extraction reply. A statement a person nodded through is attested at the nod, as before: the person is the evidence.+**4. Relative expressions are measured from the reference point in force.** The model still returns an interpretation and code still computes (0045 decisions 2 and 8). What changes is that the reference point of the chunk is in front of the model when it reads "三个月后", so the expression is recorded as a mention with its anchor instead of being folded into the object. With no reference point in force the mention is grade C and waits (0045 decision 4).-**5. A fact with no time holds at every moment.** This answers 0045's first open question. `attested_from` is null unless decision 3 gave one; the insert no longer falls back to `now()`; a read at a moment T keeps a row that has neither a valid time nor an attestation. The workbench says "undated" for it, "as of 2026-08-31 (数据统计截止)" for an attested one, and an interval for one the words dated. Rows written before this carry the processing moment; a migration sets those to null where no entry of the document's context accounts for them.+**5. A fact with no time holds at every moment.** This answers 0045's first open question. `attested_from` is null unless decision 3 gave one; the insert no longer falls back to `now()`; a read at a moment T keeps a row that has neither a valid time nor an attestation. The workbench says "undated" for it, "as of 2026-08-31 (数据统计截止)" for an attested one, and an interval for one the words dated. Rows written before this carry the processing moment; migration 0100 moves open statements to their earliest dated evidence and sets the rest to null, and typed rows follow the statements they were materialised from. An event with no date holds at no moment (0022), attested or not. An ending said without a date needs an anchor by CHECK; with no attestation it takes the moment the ledger recorded it.**6. Interpretation rides with extraction.** With the context present, the mention and its interpretation are returned by the same call. The batch interpretation step stays only for re-resolution when an anchor arrives later (0045 cut 4).@@ -71,7 +73,7 @@ A bench before any change, `scripts/bench/timewords.mjs`: documents in Chinese a|---|---|---|| time expressions recorded as mentions | 18–20 of 29 (the bench; the first hand check said 2 of 7 on one report and was too low) | 90% · cut 2: 27–29 of 29 || starts right | absolute 4 of 4, relative to now 9 of 15, relative to a dated event 3 of 3, on a heading 0 of 6 | 95% absolute, 85% anchored (0045's) · cut 2: 11 of 12, 42 of 45, 9 of 9, 18 of 18 over three runs |-| statements with no time words given the right as-of | none | 80%, and none given a start |+| statements with no time words given the right as-of | none | 80%, and none given a start · cut 3: 28 of 30 over three runs, none given a start || the same document, three runs: sentences with the same outcome | 34 of 39 | 90% agree · cut 2: 34 of 39, the differences are sentences not extracted at all || tokens per chunk | 5.7k–7.3k | no higher than today · cut 2: 6.1k–8.5k, about a tenth higher |diff --git a/migrations/0100_a_statement_is_attested_by_what_its_document_says.sql b/migrations/0100_a_statement_is_attested_by_what_its_document_says.sqlnew file mode 100644index 00000000..e7efed6e--- /dev/null+++ b/migrations/0100_a_statement_is_attested_by_what_its_document_says.sql@@ -0,0 +1,38 @@+-- 0064 · cut 3:陈述的见证来自文档自己说的日期,没有就是没有。+--+-- 从前开放陈述入库时 `attested_from` 填 `COALESCE(文档日期, now())`,而抽取跑在定日期之前,+-- 文档日期那时总是空的——于是每条陈述的见证都是处理文档的那一刻。读的人拿没起点的事实的+-- 下界时用的是 `COALESCE(valid_from, attested_from)`:一份 2020 年的报告今天传上来,它的+-- 事实读作「今天才有」,时间轴拉回去全不见了(#987;#714 说上传时刻不许进世界时间)。+--+-- 现在:见证是陈述所在那一节里文本说话的那一刻(0064 决定 3,抽取时各块报上来的日期里管着+-- 它的那个 `now`),连同那条日期的名字;文档没说就留空,留空的行在任何时点都成立(决定 5)。+-- 人此刻写下的事实照旧由此刻作证:这一列的缺省值不动,只是允许为空。+ALTER TABLE facts ALTER COLUMN attested_from DROP NOT NULL;+-- 见证从哪条日期来,照文档的字:「提报日期 2026年9月4日」+ALTER TABLE facts ADD COLUMN attested_by TEXT;++-- 已有的开放陈述:锚点挪到最早的带日期证据上(日期来自正文或来源系统才算,证据所在+-- 那一版的日期优先,与 `temporal::DATED_AT` 同一口径);一份带日期的证据都没有的留空。+-- 它们原来的值是处理文档的时刻,不是任何文档说的日期+UPDATE facts f+ SET attested_from = (+ SELECT min(COALESCE(v.doc_time, d.doc_time))+ FROM fact_evidence fe+ JOIN documents d ON d.id = fe.document_id+ LEFT JOIN document_versions v ON v.document_id = fe.document_id+ AND v.version = fe.doc_version+ WHERE fe.fact_id = f.id AND d.deleted_at IS NULL+ AND d.doc_time_source IN ('content', 'source')+ AND COALESCE(v.doc_time, d.doc_time) IS NOT NULL)+ WHERE f.layer = 'open';++-- 类型化的行自己没有出处,跟着物化它的那些陈述走:来源里最早的那个见证,都没有就留空+UPDATE facts t+ SET attested_from = src.at+ FROM (SELECT ts.fact_id, min(s.attested_from) AS at+ FROM typed_fact_sources ts+ JOIN facts s ON s.id = ts.statement_id+ GROUP BY ts.fact_id) src+ WHERE t.id = src.fact_id AND t.layer = 'typed'+ AND t.attested_from IS DISTINCT FROM src.at;diff --git a/scripts/bench/README.md b/scripts/bench/README.mdindex c7c71137..ae6e0e8a 100644--- a/scripts/bench/README.md+++ b/scripts/bench/README.md@@ -307,24 +307,30 @@ BENCH_SERVER_LOG=/path/to/server.log node scripts/bench/timewords.mjs --db utopi**2026-09-28,gemini-3.5-flash,各三遍(三遍的数用 / 隔开):**-| | dev(第 2 刀之前) | 0064 第 2 刀 |-|---|---|---|-| 句子抽到了(39) | 36 / 36 / 39 | 37 / 38 / 37 |-| 时间说法记成了时间词(29) | 18 / 19 / 20 | 28 / 29 / 27 |-| 起点对:写明的(4) | 4 / 4 / 4 | 3 / 4 / 4 |-| 起点对:相对此刻(15) | 9 / 9 / 9 | 14 / 15 / 13 |-| 起点对:相对有日期的事(3) | 3 / 3 / 3 | 3 / 3 / 3 |-| 起点对:节标题上的时间(6) | 0 / 0 / 0 | 6 / 6 / 6 |-| 没写时间的没被给起点 | 10/10 ×3 | 9/9、9/9、10/10 |-| 「截至」对(10) | 0 / 0 / 0 | 0 / 0 / 0 |-| 每块 token | 6,490 / 5,744 / 7,306 | 6,142 / 7,204 / 8,470 |-| 三遍结果一致的句子 | 34/39 | 34/39 |+| | dev(第 2 刀之前) | 0064 第 2 刀 | 0064 第 3 刀 |+|---|---|---|---|+| 句子抽到了(39) | 36 / 36 / 39 | 37 / 38 / 37 | 39 / 36 / 34 |+| 时间说法记成了时间词(29) | 18 / 19 / 20 | 28 / 29 / 27 | 29 / 27 / 25 |+| 起点对:写明的(4) | 4 / 4 / 4 | 3 / 4 / 4 | 4 / 3 / 3 |+| 起点对:相对此刻(15) | 9 / 9 / 9 | 14 / 15 / 13 | 14 / 12 / 11 |+| 起点对:相对有日期的事(3) | 3 / 3 / 3 | 3 / 3 / 3 | 3 / 3 / 3 |+| 起点对:节标题上的时间(6) | 0 / 0 / 0 | 6 / 6 / 6 | 6 / 6 / 6 |+| 没写时间的没被给起点 | 10/10 ×3 | 9/9、9/9、10/10 | 10/10、9/9、9/9 |+| 「截至」对(10) | 0 / 0 / 0 | 0 / 0 / 0 | 10 / 9 / 9 |+| 每块 token | 6,490 / 5,744 / 7,306 | 6,142 / 7,204 / 8,470 | 6,963 / 6,621 / 7,682 |+| 三遍结果一致的句子 | 34/39 | 34/39 | 33/39 |- 之前丢的四种:计划类的句子时间词进了宾语(「码表二代 — 发布 → 将在三个月后」);「上周」记下了但解释被当成坏条目丢掉(模型照字报粒度 week,阶梯上没有这一档);汇编里第二份周报的「上周」从第一份的提报日期起算;节标题上的季度到不了它下面的句子。第 2 刀之后剩下的错几乎都是那句话整句没抽到(`?`),是抽取本身的波动,不是时间。-- 「截至」一栏第 3 刀才动:没写时间的陈述今天拿到的是处理文档的那一刻。+- 「截至」一栏第 3 刀才动:之前没写时间的陈述拿到的是处理文档的那一刻;第 3 刀之后是它所在那一节里+ 文本说话的那一刻(会议纪要是开会那天,不是发出那天),没对上的那一条是那句话整句没抽到。+- gemini 的渠道当天下午下线,换 claude-haiku-4-5 把 dev 与第 2、3 刀各重跑三遍(提示词已压短):+ 时间说法记成时间词 12–13/29 对 23–25;起点对,相对此刻 6/15 对 11–13,节标题上 0/6 对 4–6;+ 「截至」0/10 对 9–10;每块 token 7.2k–7.4k 对 7.6k–8.8k。模型换了,前后的差距没变。+- 第 3 刀没动抽取,三遍之间「句子抽到了」从 39 掉到 34 是抽取本身的波动:同一份代码、同一份文档。+ 这张表上凡是跟着「抽到了」一起动的数(时间词、起点)都要这样读。- 每块 token 多了一成上下:提示词多了两段规则、回复多了 `t`,少了事后读开头的那一次调用。## 读数怎么算diff --git a/web/src/api.ts b/web/src/api.tsindex 06a34cc3..0d1e74cf 100644--- a/web/src/api.ts+++ b/web/src/api.ts@@ -1106,6 +1106,9 @@ export interface EntityFact {/** 读出来的区间(0022),与 GraphEdge 同义:「此刻成立」按它判 */holds_from: string | null;holds_to: string | null;+ /** 见证从文档的哪条日期来,照文档的字(0064):「提报日期 2026年9月4日」。没有起止的+ 事实,`holds_from` 就是见证的日期——它是「截至」,不是起点 */+ attested_by?: string | null;valid_from_precision: string | null;/** year | month | day,外加 unknown = 原文说它结束了但没说哪天 */valid_to_precision: string | null;diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.tsindex 030ab523..d0e9b286 100644--- a/web/src/i18n/en.ts+++ b/web/src/i18n/en.ts@@ -1046,6 +1046,9 @@ export const en = {historicalNote: (n: number) =>`${n} past fact${n === 1 ? "" : "s"} not shown — see Timeline →`,undated: "Undated",+ /* 0064:句子没写时间,文档说了自己是哪天的。是「截至」,不是「自…起」 */+ asOf: (date: string, by: string | null) =>+ by ? `as of ${date} · ${by}` : `as of ${date}`,/* 实体面板的 Relations:两节的标题、组尾的折、行上的证据开关 */fromEntity: (name: string) => `From ${name}`,toEntity: (name: string) => `To ${name}`,diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.tsindex 4a4dd31d..4d46b25c 100644--- a/web/src/i18n/zh.ts+++ b/web/src/i18n/zh.ts@@ -928,6 +928,8 @@ export const zh: Strings = {historyOngoing: "未闭合",historicalNote: (n: number) => `另有 ${n} 条历史事实未显示——见时间线 →`,undated: "无日期",+ asOf: (date: string, by: string | null) =>+ by ? `截至 ${date}(${by})` : `截至 ${date}`,fromEntity: (name) => `从 ${name} 出发`,toEntity: (name) => `指向 ${name}`,openEntity: (name) => `打开 ${name}`,diff --git a/web/src/pages/Graph.tsx b/web/src/pages/Graph.tsxindex b0c42d82..24f0c11e 100644--- a/web/src/pages/Graph.tsx+++ b/web/src/pages/Graph.tsx@@ -2266,6 +2266,16 @@ function TimeScrubber({/* ============ 实体侧栏 ============ *//* 抽出来供 vitest 测;UI 段(`EntityPanel`)内部闭包用同名 */+/** 句子自己没写时间,但它所在那一节的文本说了自己是哪天的(0064):「截至那天」。+ *+ * 这不是起点——报告里的「所属市场为全球」说的是那天是这样,不是从那天起——所以不画成+ * 区间,另写一句,连那条日期在文档里叫什么一起说。恒常的事实没有时间维度,不写 */+export function asOf(f: EntityFact): string {+ if (f.temporal === "eternal" || f.valid_from || f.valid_to || !f.holds_from) return "";+ const date = fmtTime(f.holds_from, "day");+ return date ? S.graph.asOf(date, f.attested_by ?? null) : "";+}+export function fmtInterval(f: EntityFact): string {// 时态 = 永恒:不画区间。// 写端 `Validity::under(Eternal)` 把 from/to 都抹成 null,render 时就空,@@ -3499,7 +3509,7 @@ function FactRow({interval ? "text-ink-2" : "italic text-ink-2",)}>- {interval || S.graph.undated}+ {interval || asOf(fact) || S.graph.undated}</span>{fact.stale && (<Chip tone="neutral" className="shrink-0 text-fine">