World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
A derivation reads no further than its premises (0022, second cut) (#397)
WaylandYangae219478346a
diff --git a/crates/utopia-store/src/reasoning.rs b/crates/utopia-store/src/reasoning.rsindex 739b723d..cb124a25 100644--- a/crates/utopia-store/src/reasoning.rs+++ b/crates/utopia-store/src/reasoning.rs@@ -517,13 +517,13 @@ pub async fn open_violations(limit: i64,offset: i64,) -> AppResult<Vec<AxiomViolation>> {- Ok(sqlx::query_as(+ Ok(sqlx::query_as(&format!("WITH triple AS (SELECT f.id,s.canonical_name || ' · '|| COALESCE(r.label, fact_surface_predicate(f.id), '?') || ' · '|| COALESCE(o.canonical_name, f.object_value ->> 'summary',- f.object_value #>> '{}', '?') AS text,+ f.object_value #>> '{{}}', '?') AS text,r.label AS predicateFROM facts fJOIN entities s ON s.id = f.subject_id@@ -540,7 +540,7 @@ pub async fn open_violations(ORDER BY x.ord)FROM unnest(v.path) WITH ORDINALITY AS x(id, ord)JOIN triple pt ON pt.id = x.id), '[]'::jsonb) AS path,- lf.valid_to IS NULL AS left_open,+ {left_holds_to} IS NULL AS left_open,lf.confidence AS left_confidence,EXISTS (SELECT 1 FROM entities e@@ -556,7 +556,9 @@ pub async fn open_violations(WHERE v.kb_id = $1 AND v.status = 'open'ORDER BY v.detected_at DESCLIMIT $2 OFFSET $3",- )+ // 「左边还开着」按读出来的终点判(0022):结束了不知哪天的不算开着+ left_holds_to = crate::world_axis::facts_holds_to("lf"),+ )).bind(kb_id).bind(limit).bind(offset)@@ -834,18 +836,45 @@ pub struct DeriveReport {/// 一次取数,三样东西:带区间的边、每条事实的区间、精度与置信度。/// `run` 与 `materialize` 共用——两边看到的边必须是同一批+/// 前提的精度、置信度,以及两端各自**是不是锚点**(0022):来自证据日期而不是原文+/// 日期的那一端,推出来的派生行在那一端没有精度可言+type PremiseMeta = (Option<String>, Option<String>, f32, bool, bool);+type TimedEdges = (Vec<TimedEdge>,HashMap<Uuid, (Option<i64>, Option<i64>)>,- HashMap<Uuid, (Option<String>, Option<String>, f32)>,+ HashMap<Uuid, PremiseMeta>,);+/// 一条前提**读出来的**区间(0022):原文没给起点就从锚点起,说结束了不知哪天就到+/// 锚点为止。两端都不知道的行读成空区间,求交时自然掉出去——它支撑不了任何派生。+/// 返回 `(from, to, from_anchored, to_anchored)`。+fn read_span(+ from: Option<chrono::DateTime<chrono::Utc>>,+ to: Option<chrono::DateTime<chrono::Utc>>,+ to_precision: Option<&str>,+ attested_at: chrono::DateTime<chrono::Utc>,+) -> (Option<i64>, Option<i64>, bool, bool) {+ let anchor = attested_at.timestamp();+ let (f, from_anchored) = match from {+ Some(x) => (Some(x.timestamp()), false),+ None => (Some(anchor), true),+ };+ let (t, to_anchored) = match (to, to_precision) {+ (Some(x), _) => (Some(x.timestamp()), false),+ (None, Some(p)) if p == crate::graph::ENDED_UNKNOWN => (Some(anchor), true),+ (None, _) => (None, false),+ };+ (f, t, from_anchored, to_anchored)+}+async fn timed_edges(pool: &PgPool, kb_id: Uuid) -> AppResult<TimedEdges> {// 输入**只有断言**。派生住在另一张表,所以这里连过滤都不必写——那正是// 分表买到的东西:忘了排除的后果是推不出东西,不是把自己的输出喂回自己let rows: Vec<EdgeRow> = sqlx::query_as("SELECT id, predicate_id, subject_id, object_id,- valid_from, valid_to, valid_from_precision, valid_to_precision, confidence+ valid_from, valid_to, valid_from_precision, valid_to_precision, confidence,+ attested_atFROM factsWHERE kb_id = $1AND invalidated_at IS NULL@@ -857,10 +886,13 @@ async fn timed_edges(pool: &PgPool, kb_id: Uuid) -> AppResult<TimedEdges> {.await?;let mut edges = Vec::with_capacity(rows.len());- let mut meta: HashMap<Uuid, (Option<String>, Option<String>, f32)> = HashMap::new();+ let mut meta: HashMap<Uuid, PremiseMeta> = HashMap::new();let mut spans: HashMap<Uuid, (Option<i64>, Option<i64>)> = HashMap::new();- for (id, pred, subj, obj, from, to, fp, tp, conf) in rows {- let (f, t) = (from.map(|x| x.timestamp()), to.map(|x| x.timestamp()));+ for (id, pred, subj, obj, from, to, fp, tp, conf, attested) in rows {+ // 按读出来的区间推(0022):没起点的前提从最早的证据起,结束了不知哪天的+ // 到说出它的那份文档为止。读成开放的话,一条经过 "former CEO" 的链会推出+ // 一条今天还成立的边+ let (f, t, fa, ta) = read_span(from, to, tp.as_deref(), attested);edges.push(TimedEdge {edge: Edge {fact: id,@@ -872,7 +904,7 @@ async fn timed_edges(pool: &PgPool, kb_id: Uuid) -> AppResult<TimedEdges> {to: t,});spans.insert(id, (f, t));- meta.insert(id, (fp, tp, conf));+ meta.insert(id, (fp, tp, conf, fa, ta));}Ok((edges, spans, meta))}@@ -1036,6 +1068,7 @@ type EdgeRow = (Option<String>,Option<String>,f32,+ chrono::DateTime<chrono::Utc>,);type LiveRow = (@@ -1236,6 +1269,7 @@ type AttrFactRow = (Option<String>,f32,Option<Uuid>,+ chrono::DateTime<chrono::Utc>,);/// 属性事实:字面值通道上的活事实,连同区间与精度。@@ -1248,13 +1282,13 @@ async fn attribute_facts() -> AppResult<(Vec<utopia_reason::rules::AttrFact>,HashMap<Uuid, (Option<i64>, Option<i64>)>,- HashMap<Uuid, (Option<String>, Option<String>, f32)>,+ HashMap<Uuid, PremiseMeta>,HashMap<Uuid, Option<Uuid>>,)> {let rows: Vec<AttrFactRow> = sqlx::query_as("SELECT f.id, f.subject_id, f.predicate_id, f.object_value,f.valid_from, f.valid_to, f.valid_from_precision, f.valid_to_precision,- f.confidence, e.type_id+ f.confidence, e.type_id, f.attested_atFROM facts fJOIN entities e ON e.id = f.subject_idWHERE f.kb_id = $1@@ -1271,8 +1305,9 @@ async fn attribute_facts(let mut spans = HashMap::new();let mut meta = HashMap::new();let mut type_of = HashMap::new();- for (id, subject, predicate, value, from, to, fp, tp, conf, type_id) in rows {- let (f, t) = (from.map(|x| x.timestamp()), to.map(|x| x.timestamp()));+ for (id, subject, predicate, value, from, to, fp, tp, conf, type_id, attested) in rows {+ // 与公理那一路同一种读法(0022):读数没日期就从它的文档起算+ let (f, t, fa, ta) = read_span(from, to, tp.as_deref(), attested);// 属性事实的字面值是 `{"value": …, "unit": …}`;比较的是里面那个 value。// 取不到就把整个对象交给求值器——它对认不出的形状一律判不满足let inner = value.get("value").cloned().unwrap_or_else(|| value.clone());@@ -1283,7 +1318,7 @@ async fn attribute_facts(value: inner,});spans.insert(id, (f, t));- meta.insert(id, (fp, tp, conf));+ meta.insert(id, (fp, tp, conf, fa, ta));type_of.insert(subject, type_id);}Ok((facts, spans, meta, type_of))@@ -1296,7 +1331,7 @@ async fn attribute_facts(pub async fn materialize(pool: &PgPool, kb_id: Uuid) -> AppResult<DeriveReport> {let ax = axioms(pool, kb_id).await?;let rules = compile_rules(pool, kb_id, &ax).await?;- let (edges, spans, mut meta) = timed_edges(pool, kb_id).await?;+ let (edges, mut spans, mut meta) = timed_edges(pool, kb_id).await?;let derivation = utopia_reason::derive::derive(&edges, &ax);// asserted > derived 是硬性的(0002):撞上断言的派生不落地。人认可过并存的@@ -1425,8 +1460,10 @@ pub async fn materialize(pool: &PgPool, kb_id: Uuid) -> AppResult<DeriveReport>});}}- // 前提的精度与置信度:两趟共用下面那段,所以两份 meta 也要合起来+ // 前提的精度与置信度:两趟共用下面那段,所以两份 meta 也要合起来;+ // 区间也要——落地时要对着前提的区间认出派生的哪一端是锚点顶上的meta.extend(attr_meta);+ spans.extend(attr_spans);}let mut tx = pool.begin().await?;@@ -1470,16 +1507,30 @@ pub async fn materialize(pool: &PgPool, kb_id: Uuid) -> AppResult<DeriveReport>let mut fp: Option<String> = None;let mut tp: Option<String> = None;let mut conf = 1.0f32;+ // 派生的那一端是不是某条前提的**锚点**顶上来的(0022):是,就没有精度可言+ let mut from_anchored = false;+ let mut to_anchored = false;for p in &d.premises {- if let Some((pf, pt, pc)) = meta.get(p) {+ if let Some((pf, pt, pc, fa, ta)) = meta.get(p) {fp = coarsest(fp.as_deref(), pf.as_deref());- tp = coarsest(tp.as_deref(), pt.as_deref());+ // 'unknown' 不是粒度,是「结束了不知哪天」的标记;它顶上来的那一端+ // 走下面 to_anchored 那条路,不进粒度的比较+ tp = coarsest(+ tp.as_deref(),+ pt.as_deref().filter(|p| *p != crate::graph::ENDED_UNKNOWN),+ );conf = conf.min(*pc);+ if let Some((sf, st)) = spans.get(p) {+ from_anchored |= *fa && *sf == d.from;+ to_anchored |= *ta && *st == d.to;+ }}}- // 约束是「有日期才有精度」——交集把某一端算成无界时,那一端的精度也得清掉- let fp = d.from.and(fp);- let tp = d.to.and(tp);+ // 约束是「有精度必有日期」(0022 放宽了反向):交集把某一端算成无界时,那一端+ // 的精度清掉;那一端若来自证据日期而不是原文的日期,也没有精度——在无知的地方+ // 填一个确定的值,正是 `facts.valid_from_precision` 那条注释说的病+ let fp = if from_anchored { None } else { d.from.and(fp) };+ let tp = if to_anchored { None } else { d.to.and(tp) };let id = Uuid::now_v7();sqlx::query("INSERT INTO derived_facts (id, kb_id, subject_id, predicate_id, object_id,diff --git a/crates/utopia-store/src/world_axis.rs b/crates/utopia-store/src/world_axis.rsindex 831fa7aa..ed97378a 100644--- a/crates/utopia-store/src/world_axis.rs+++ b/crates/utopia-store/src/world_axis.rs@@ -54,8 +54,8 @@ pub fn interval_holds_at(from: &str, to: &str, param: usize) -> String {}/// `derived_facts`:派生在 T 时刻成立。两端由求值器按前提**读出来的**区间求交写入-/// (0022 第 4 条,第二刀),所以这里是纯粹的包含。第一刀里派生行仍带着前提原样的-/// NULL 起点,那一半随第二刀补上。+/// (0022 第 4 条:没起点的前提从锚点起算,结束了不知哪天的到锚点为止),所以这里+/// 是纯粹的包含——派生行自己不需要锚点。pub fn derived_hold_at(alias: &str, param: usize) -> String {interval_holds_at(&format!("{alias}.valid_from"),diff --git a/crates/utopia-store/tests/an_unknown_date_is_not_an_open_one.rs b/crates/utopia-store/tests/an_unknown_date_is_not_an_open_one.rsindex 7c457beb..de5af9c4 100644--- a/crates/utopia-store/tests/an_unknown_date_is_not_an_open_one.rs+++ b/crates/utopia-store/tests/an_unknown_date_is_not_an_open_one.rs@@ -523,3 +523,180 @@ async fn an_ending_without_a_date_closes_the_dated_row_it_ends() -> anyhow::Resu.await?;run}++/// 派生读前提**读出来的**区间(0022 第 4 条):一条 part_of 链里,没起点的那一环从它+/// 的证据日期起算,结束了不知哪天的那一环到说出它的那份文档为止。派生行的两端因此+/// 是前提共同支撑的那一段;来自锚点的那一端没有精度。+#[tokio::test]+async fn a_derivation_reads_no_further_than_its_premises() -> 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 {+ // part_of 传递;四个组织 a ⊂ b ⊂ c ⊂ d+ let part_of = Uuid::now_v7();+ sqlx::query(+ "INSERT INTO relation_types (id, kb_id, key, label, is_transitive)+ VALUES ($1, $2, 'part_of', 'part of', TRUE)",+ )+ .bind(part_of)+ .bind(f.kb)+ .execute(&pool)+ .await?;+ let org_type: Uuid = sqlx::query_scalar("SELECT type_id FROM entities WHERE id = $1")+ .bind(f.meridian)+ .fetch_one(&pool)+ .await?;+ let mut ids = Vec::new();+ for name in ["Team A", "Group B", "Division C", "Company D"] {+ let id = Uuid::now_v7();+ sqlx::query(+ "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",+ )+ .bind(id)+ .bind(f.kb)+ .bind(org_type)+ .bind(name)+ .execute(&pool)+ .await?;+ ids.push(id);+ }+ let (a, b, c, d) = (ids[0], ids[1], ids[2], ids[3]);+ // a ⊂ b:原文给了起点 2022-01-01+ fact(+ &pool,+ &f,+ a,+ part_of,+ b,+ Validity::starting(Some(t("2022-01-01T00:00:00Z")), Some("day"))+ .attested(Some(t("2022-01-01T00:00:00Z"))),+ )+ .await?;+ // b ⊂ c:没起点,出自 2024-02-20 的文档+ fact(+ &pool,+ &f,+ b,+ part_of,+ c,+ Validity::default().attested(Some(t("2024-02-20T00:00:00Z"))),+ )+ .await?;+ // c ⊂ d:2020-01-01 起,2025-10-15 的文档说已不再(日期未记录)+ fact(+ &pool,+ &f,+ c,+ part_of,+ d,+ Validity::starting(Some(t("2020-01-01T00:00:00Z")), Some("day"))+ .ended_when_unknown()+ .attested(Some(t("2025-10-15T00:00:00Z"))),+ )+ .await?;++ utopia_store::reasoning::materialize(&pool, f.kb).await?;++ type DerivedRow = (+ Option<chrono::DateTime<chrono::Utc>>,+ Option<String>,+ Option<chrono::DateTime<chrono::Utc>>,+ Option<String>,+ );+ let derived = |s: Uuid, o: Uuid| {+ let pool = pool.clone();+ async move {+ let row: Option<DerivedRow> = sqlx::query_as(+ "SELECT valid_from, valid_from_precision, valid_to, valid_to_precision+ FROM derived_facts+ WHERE kb_id = $1 AND subject_id = $2 AND object_id = $3+ AND invalidated_at IS NULL",+ )+ .bind(f.kb)+ .bind(s)+ .bind(o)+ .fetch_optional(&pool)+ .await?;+ Ok::<_, anyhow::Error>(row)+ }+ };++ // a ⊂ c:起点是 b ⊂ c 的证据日期(锚点),没有精度;终点开放+ let ac = derived(a, c).await?.expect("a ⊂ c 推得出");+ assert_eq!(+ ac.0,+ Some(t("2024-02-20T00:00:00Z")),+ "从没起点那一环的证据起算"+ );+ assert_eq!(ac.1, None, "来自锚点的一端没有精度");+ assert_eq!(ac.2, None);++ // a ⊂ d:起点同上;终点是 c ⊂ d 的锚点(说出结束的那份文档),也没有精度+ let ad = derived(a, d).await?.expect("a ⊂ d 推得出");+ assert_eq!(ad.0, Some(t("2024-02-20T00:00:00Z")));+ assert_eq!(ad.1, None);+ assert_eq!(+ ad.2,+ Some(t("2025-10-15T00:00:00Z")),+ "到结束的那份文档为止"+ );+ assert_eq!(ad.3, None, "'unknown' 不是粒度,锚点顶上来的一端没有精度");++ // b ⊂ d:起点是 b ⊂ c 的锚点、终点是 c ⊂ d 的锚点——两端都没有精度+ let bd = derived(b, d).await?.expect("b ⊂ d 推得出");+ assert_eq!(+ (bd.0, bd.2),+ (+ Some(t("2024-02-20T00:00:00Z")),+ Some(t("2025-10-15T00:00:00Z"))+ )+ );+ assert_eq!((bd.1, bd.3), (None, None));++ // 图上按读出来的区间亮灭:2023 年 a ⊂ c 还不存在;2024 年中在;2026 年 a ⊂ d 已灭+ // 从 b 看两跳:a ⊂ b 与 c ⊂ d 把 a、d 都收进来,派生边才都在「这些节点之间」+ let at = |when: &str| {+ let pool = pool.clone();+ let when = when.to_string();+ async move {+ let (_, edges) =+ utopia_store::graph::neighborhood(&pool, f.kb, b, 2, Some(t(&when)), None)+ .await?;+ Ok::<_, anyhow::Error>(+ edges+ .into_iter()+ .filter(|e| e.derived)+ .map(|e| (e.source, e.target))+ .collect::<HashSet<_>>(),+ )+ }+ };+ assert!(!at("2023-06-01T00:00:00Z").await?.contains(&(a, c)));+ assert!(at("2024-06-01T00:00:00Z").await?.contains(&(a, c)));+ assert!(at("2024-06-01T00:00:00Z").await?.contains(&(a, d)));+ assert!(+ !at("2026-01-01T00:00:00Z").await?.contains(&(a, d)),+ "链经过已结束的一环,之后不成立"+ );+ assert!(+ at("2026-01-01T00:00:00Z").await?.contains(&(a, c)),+ "另一条链还开着"+ );+ Ok::<_, anyhow::Error>(())+ }+ .await;++ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")+ .bind(f.kb)+ .execute(&pool)+ .await?;+ sqlx::query("DELETE FROM organizations WHERE id = $1")+ .bind(f.org)+ .execute(&pool)+ .await?;+ run+}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.mdindex 88e2153f..bdd10b3a 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**: first cut implemented · `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 · an undated ending now also **closes** the dated open row it ends (`close_with_unknown_end`), which the first open question below had left to the write side · derived rows still read their stated bounds — the second cut · a bare open row plus an undated ending is #393+- **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 · a bare open row plus an undated ending is #393- **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).diff --git a/docs/decisions/README.md b/docs/decisions/README.mdindex c9ed1e47..0dbe3e3c 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 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) | First cut implemented · `world_axis` predicate beside `record_axis`, `facts.attested_at` anchors a missing start or an undated end at the document that attests it, every server read and both client filters on the read interval, an undated ending closes the dated row it ends · derived rows on read intervals are the second cut · a bare open row plus an undated ending is #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 · a bare open row plus an undated ending is #393 |## Not a decision recorddiff --git a/migrations/0031_a_derived_bound_from_evidence_has_no_precision.sql b/migrations/0031_a_derived_bound_from_evidence_has_no_precision.sqlnew file mode 100644index 00000000..e95852d1--- /dev/null+++ b/migrations/0031_a_derived_bound_from_evidence_has_no_precision.sql@@ -0,0 +1,14 @@+-- 0022 第 4 条:派生行的两端由求值器按前提**读出来的**区间求交写入——没起点的前提+-- 从它的证据日期起算,结束了不知哪天的到说出它的那份文档为止。这样一端若来自证据+-- 日期而不是原文的日期,它没有精度可言:给它标一个 day 就是在无知的地方填一个+-- 确定的值(`facts.valid_from_precision` 那条注释说的病)。+--+-- 0013 的约束是「有日期当且仅当有精度」。放宽为「有精度必有日期」——反向不再要求。+-- 只放宽派生表:`facts` 上原文给的日期总带着原文的粒度,那边的约束照旧。+ALTER TABLE derived_facts+ DROP CONSTRAINT derived_from_precision_matches_date,+ DROP CONSTRAINT derived_to_precision_matches_date,+ ADD CONSTRAINT derived_from_precision_needs_date+ CHECK (valid_from_precision IS NULL OR valid_from IS NOT NULL),+ ADD CONSTRAINT derived_to_precision_needs_date+ CHECK (valid_to_precision IS NULL OR valid_to IS NOT NULL);