Skip to content

World's first open-source enterprise world model.

An ending has its own anchor (#433)

WaylandYang7a846e22c322
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index 7214e58f..fcd61b34 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -137,7 +137,8 @@ pub struct Validity<'a> {
pub to: Option<chrono::DateTime<chrono::Utc>>,
pub to_precision: Option<&'a str>,
/// 这次观察的证据是哪一天的——文档的日期(0022)。`None` 即此刻:人此刻写下
- /// 的事实,人就是证据。落库成 `attested_at`;同一断言再被观察到时只往早挪。
+ /// 的事实,人就是证据。落库成 `attested_from`,说结束了不知哪天的观察还落成
+ /// `attested_to`(#393,两端各有各的锚点);同一断言再被观察到时只往早挪。
/// 没有起点的事实从它起成立,结束了不知哪天的到它为止——**它不是起点**,所以
/// 不写进 `from`(0003 拒绝过把文档日期填进日期列)
pub attested_at: Option<chrono::DateTime<chrono::Utc>>,
@@ -265,24 +266,21 @@ async fn insert_fact_inner(
FactObject::Value(v) => q.bind(v),
};
let same: Vec<FactSpanRow> = q.fetch_all(pool).await?;
- // 「结束了,不知哪天」的观察撞上同断言**带起点的开放行**(0022):关上它。
+ // 「结束了,不知哪天」的观察撞上同断言的**开放行**(0022 / #393):关上它。
// 不并进去——并进去等于把「它结束了」这唯一带来的信息丢掉(同 valid_from 那条
// 精确重复的路会这么干);也不另立一行——另立一行让两条各说各话,开放的那条
// 照旧被读成「至今仍是」(#345 的那道题正是这样挂的)。
- // 修正走 supersede:旧行作废,新行终点仍空、精度 'unknown',锚在说出结束的那份
- // 文档上——读出来就是「到它为止」。只关有起点的:两端都不知道的行一个锚点装不下
- // 两头,那一半还是开问题
+ // 修正走 supersede:旧行作废,新行终点仍空、精度 'unknown',`attested_to` 锚在说出
+ // 结束的那份文档上;起点照旧——有日期的用日期,没日期的裸行留着它自己的
+ // `attested_from`(第一份证据)。两个锚点,裸行也关得上
if validity.to.is_none() && validity.to_precision == Some(ENDED_UNKNOWN) {
- let open_dated = same
+ let open = same
.iter()
.filter(|(_, vf, vt, vtp)| {
- vf.is_some()
- && vt.is_none()
- && vtp.is_none()
- && validity.from.is_none_or(|f| Some(f) == *vf)
+ vt.is_none() && vtp.is_none() && validity.from.is_none_or(|f| Some(f) == *vf)
})
.max_by_key(|(_, vf, _, _)| *vf);
- if let Some((open, _, _, _)) = open_dated {
+ if let Some((open, _, _, _)) = open {
if let Some(closed) =
crate::temporal::close_with_unknown_end(pool, *open, validity.attested_at).await?
{
@@ -328,16 +326,21 @@ async fn insert_fact_inner(
let id = Uuid::now_v7();
let insert_sql = match object {
FactObject::Entity(_) => {
+ // 说结束了不知哪天的观察,说出结束的就是它自己那份文档:attested_to 也落它
"INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id,
valid_from, valid_from_precision,
- valid_to, valid_to_precision, confidence, attested_at)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, COALESCE($11, now()))"
+ valid_to, valid_to_precision, confidence,
+ attested_from, attested_to)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, COALESCE($11, now()),
+ CASE WHEN $9::text = 'unknown' THEN COALESCE($11, now()) END)"
}
FactObject::Value(_) => {
"INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_value,
valid_from, valid_from_precision,
- valid_to, valid_to_precision, confidence, attested_at)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, COALESCE($11, now()))"
+ valid_to, valid_to_precision, confidence,
+ attested_from, attested_to)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, COALESCE($11, now()),
+ CASE WHEN $9::text = 'unknown' THEN COALESCE($11, now()) END)"
}
};
let mut ins = sqlx::query(insert_sql)
@@ -393,11 +396,20 @@ async fn attest_earlier(
at: Option<chrono::DateTime<chrono::Utc>>,
) -> AppResult<()> {
if let Some(at) = at {
- sqlx::query("UPDATE facts SET attested_at = least(attested_at, $2) WHERE id = $1")
- .bind(fact_id)
- .bind(at)
- .execute(pool)
- .await?;
+ // 两个锚点都只往早挪:更早的文档既是它成立的更早证据,若它说的是结束,也是
+ // 结束得更早的证据。attested_to 只在结束未知的行上有,NULL 的留 NULL
+ sqlx::query(
+ // LEAST 会跳过 NULL——开放行的 attested_to 是 NULL,直接 least 会给它凭空长出一个
+ // 终点锚,撞上 CHECK。NULL 的留 NULL
+ "UPDATE facts SET attested_from = least(attested_from, $2),
+ attested_to = CASE WHEN attested_to IS NULL THEN NULL
+ ELSE least(attested_to, $2) END
+ WHERE id = $1",
+ )
+ .bind(fact_id)
+ .bind(at)
+ .execute(pool)
+ .await?;
}
Ok(())
}
@@ -1659,11 +1671,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_at)
+ attested_from, attested_to)
SELECT $1, kb_id, $6, $3, $4, $5,
valid_from, valid_from_precision,
valid_to, valid_to_precision, confidence, id,
- attested_at
+ attested_from, attested_to
FROM facts WHERE id = $2 AND invalidated_at IS NULL
RETURNING id",
)
diff --git a/crates/utopia-store/src/reasoning.rs b/crates/utopia-store/src/reasoning.rs
index 3a585969..286b5e1d 100644
--- a/crates/utopia-store/src/reasoning.rs
+++ b/crates/utopia-store/src/reasoning.rs
@@ -853,17 +853,19 @@ 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>,
+ attested_from: chrono::DateTime<chrono::Utc>,
+ attested_to: Option<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),
+ None => (Some(attested_from.timestamp()), 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),
+ // 结束未知的行按 CHECK 必带 attested_to;万一没有,按开放读——宁可多推一点,
+ // 也不凭空造一个终点
+ let (t, to_anchored) = match (to, to_precision, attested_to) {
+ (Some(x), _, _) => (Some(x.timestamp()), false),
+ (None, Some(p), Some(a)) if p == crate::graph::ENDED_UNKNOWN => (Some(a.timestamp()), true),
+ (None, _, _) => (None, false),
};
(f, t, from_anchored, to_anchored)
}
@@ -874,7 +876,7 @@ 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,
- attested_at
+ attested_from, attested_to
FROM facts
WHERE kb_id = $1
AND invalidated_at IS NULL
@@ -888,11 +890,11 @@ async fn timed_edges(pool: &PgPool, kb_id: Uuid) -> AppResult<TimedEdges> {
let mut edges = Vec::with_capacity(rows.len());
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, attested) in rows {
+ for (id, pred, subj, obj, from, to, fp, tp, conf, attested_from, attested_to) in rows {
// 按读出来的区间推(0022):没起点的前提从最早的证据起,结束了不知哪天的
// 到说出它的那份文档为止。读成开放的话,一条经过 "former CEO" 的链会推出
// 一条今天还成立的边
- let (f, t, fa, ta) = read_span(from, to, tp.as_deref(), attested);
+ let (f, t, fa, ta) = read_span(from, to, tp.as_deref(), attested_from, attested_to);
edges.push(TimedEdge {
edge: Edge {
fact: id,
@@ -1071,6 +1073,7 @@ type EdgeRow = (
Option<String>,
f32,
chrono::DateTime<chrono::Utc>,
+ Option<chrono::DateTime<chrono::Utc>>,
);
type LiveRow = (
@@ -1272,6 +1275,7 @@ type AttrFactRow = (
f32,
Option<Uuid>,
chrono::DateTime<chrono::Utc>,
+ Option<chrono::DateTime<chrono::Utc>>,
);
/// 属性事实:字面值通道上的活事实,连同区间与精度。
@@ -1290,7 +1294,7 @@ async fn attribute_facts(
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.attested_at
+ f.confidence, e.type_id, f.attested_from, f.attested_to
FROM facts f
JOIN entities e ON e.id = f.subject_id
WHERE f.kb_id = $1
@@ -1307,9 +1311,23 @@ 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, attested) in rows {
+ for (
+ id,
+ subject,
+ predicate,
+ value,
+ from,
+ to,
+ fp,
+ tp,
+ conf,
+ type_id,
+ attested_from,
+ attested_to,
+ ) in rows
+ {
// 与公理那一路同一种读法(0022):读数没日期就从它的文档起算
- let (f, t, fa, ta) = read_span(from, to, tp.as_deref(), attested);
+ let (f, t, fa, ta) = read_span(from, to, tp.as_deref(), attested_from, attested_to);
// 属性事实的字面值是 `{"value": …, "unit": …}`;比较的是里面那个 value。
// 取不到就把整个对象交给求值器——它对认不出的形状一律判不满足
let inner = value.get("value").cloned().unwrap_or_else(|| value.clone());
diff --git a/crates/utopia-store/src/temporal.rs b/crates/utopia-store/src/temporal.rs
index c22faeac..13bb7fed 100644
--- a/crates/utopia-store/src/temporal.rs
+++ b/crates/utopia-store/src/temporal.rs
@@ -332,9 +332,11 @@ pub async fn close_superseded(
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_at)
+ 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_at
+ valid_from, valid_from_precision, $3, $4, confidence, id,
+ attested_from, NULL
FROM facts WHERE id = $2 AND invalidated_at IS NULL
RETURNING id",
)
@@ -358,10 +360,10 @@ pub async fn close_superseded(
Ok(Some(corrected))
}
-/// 作废 + 改写成「结束了,不知哪天」(0022):旧行记 invalidated_at,修正行终点仍是
-/// NULL、精度 'unknown',锚点换成**说出结束的那份文档**——读出来就是「到它为止」
-/// (起点是原文给的,锚点只管结束端)。证据引用随行复制。
-/// 返回修正行 id;`None` = 这条已不是带起点的开放行,没动。
+/// 作废 + 改写成「结束了,不知哪天」(0022 / #393):旧行记 invalidated_at,修正行终点仍是
+/// NULL、精度 'unknown',`attested_to` 锚在**说出结束的那份文档**——读出来就是「到它为止」;
+/// `attested_from` 从旧行继承——没起点的裸行靠它记着第一份证据,读出来是「从那时起」。
+/// 证据引用随行复制。返回修正行 id;`None` = 这条已不是开放行,没动。
pub async fn close_with_unknown_end(
pool: &PgPool,
fact_id: Uuid,
@@ -372,12 +374,14 @@ pub async fn close_with_unknown_end(
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_at)
+ 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, NULL, $3, confidence, id, COALESCE($4, now())
+ 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_from IS NOT NULL AND valid_to IS NULL AND valid_to_precision IS NULL
+ AND valid_to IS NULL AND valid_to_precision IS NULL
RETURNING id",
)
.bind(corrected)
@@ -440,9 +444,12 @@ pub async fn correct_interval(
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_at)
+ valid_to, valid_to_precision, confidence, supersedes,
+ attested_from, attested_to)
SELECT $1, kb_id, subject_id, predicate_id, object_id, object_value,
- $3, $4, $5, $6, confidence, id, attested_at
+ $3, $4, $5, $6, confidence, id,
+ attested_from,
+ CASE WHEN $6::text = 'unknown' THEN COALESCE(attested_to, now()) END
FROM facts WHERE id = $2 AND invalidated_at IS NULL
RETURNING id",
)
diff --git a/crates/utopia-store/src/world_axis.rs b/crates/utopia-store/src/world_axis.rs
index ed97378a..d2237416 100644
--- a/crates/utopia-store/src/world_axis.rs
+++ b/crates/utopia-store/src/world_axis.rs
@@ -10,11 +10,12 @@
//! 无声无息。前端也不再自己算一遍——边和事实带着 `holds_from` / `holds_to`
//! (按这里同一套表达式投影出来的「读出来的区间」),滑杆只按它们过滤。
//!
-//! 未知的一端**读到证据为止**:`attested_at` 是这一行的各次观察里最早那份文档的
-//! 日期。没有起点 → 从它起成立;结束了不知哪天 → 到它为止。两端的不对称是故意
-//! 的:开放的结束端仍读作「直到有人说它结束」——结束会以记录的形式到来(后面的
-//! 文档、人的修正),把行关上;而缺失的起点没有这样的修正者,不会有谁来说
-//! 「2023 年它还没开始」。所以事实从有证据的那一刻起成立,之前的诚实答案是没有。
+//! 未知的一端**读到证据为止**,两端各有各的锚点(#393):`attested_from` 是这一行的
+//! 各次观察里最早那份文档的日期——没有起点 → 从它起成立;`attested_to` 是**说出结束**
+//! 的那份文档的日期,只在「结束了不知哪天」的行上有——到它为止。一个锚点装不下两头:
+//! 没起点的裸行被一句「不再担任」关上时,起点要用第一份证据、终点要用说结束的那份。
+//! 两端的不对称仍是故意的:开放的结束端读作「直到有人说它结束」——结束会以记录的形式
+//! 到来,把行关上;缺失的起点没有这样的修正者,不会有谁来说「2023 年它还没开始」。
//!
//! `at` 为 NULL 在世界轴上是**每一刻**(画布画的是历史,滑杆负责收窄),与记录轴
//! 的「NULL 即现在」不同:没有人持有一个晚于此刻的信念,而没有时刻的图是全部
@@ -22,7 +23,7 @@
/// `facts`:读出来的下界——原文给了起点用起点,否则从最早的证据起。
pub fn facts_holds_from(alias: &str) -> String {
- format!("COALESCE({alias}.valid_from, {alias}.attested_at)")
+ format!("COALESCE({alias}.valid_from, {alias}.attested_from)")
}
/// `facts`:读出来的上界——原文给了终点用终点;说结束了但不知哪天,到最早说出它
@@ -30,7 +31,7 @@ pub fn facts_holds_from(alias: &str) -> String {
pub fn facts_holds_to(alias: &str) -> String {
format!(
"CASE WHEN {alias}.valid_to IS NOT NULL THEN {alias}.valid_to \
- WHEN {alias}.valid_to_precision = 'unknown' THEN {alias}.attested_at END"
+ WHEN {alias}.valid_to_precision = 'unknown' THEN {alias}.attested_to END"
)
}
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.rs
index de5af9c4..254d50ad 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
@@ -22,6 +22,14 @@ use sqlx::PgPool;
use utopia_store::graph::Validity;
use uuid::Uuid;
+/// 裸行关上之后那一行:起点、终点精度、两个锚点
+type BareClosedRow = (
+ Option<chrono::DateTime<chrono::Utc>>,
+ Option<String>,
+ chrono::DateTime<chrono::Utc>,
+ Option<chrono::DateTime<chrono::Utc>>,
+);
+
/// 关上之后那一行:起点、终点、终点精度、supersedes、锚点
type ClosedRow = (
Option<chrono::DateTime<chrono::Utc>>,
@@ -157,7 +165,7 @@ async fn edges_at(pool: &PgPool, f: &Fixture, at: Option<&str>) -> anyhow::Resul
async fn attested_at(pool: &PgPool, id: Uuid) -> anyhow::Result<chrono::DateTime<chrono::Utc>> {
Ok(
- sqlx::query_scalar("SELECT attested_at FROM facts WHERE id = $1")
+ sqlx::query_scalar("SELECT attested_from FROM facts WHERE id = $1")
.bind(id)
.fetch_one(pool)
.await?,
@@ -466,7 +474,7 @@ async fn an_ending_without_a_date_closes_the_dated_row_it_ends() -> anyhow::Resu
assert!(invalidated.is_some(), "旧行作废,不删");
assert!(supersedes.is_none());
let row: ClosedRow = sqlx::query_as(
- "SELECT valid_from, valid_to, valid_to_precision, supersedes, attested_at
+ "SELECT valid_from, valid_to, valid_to_precision, supersedes, attested_to
FROM facts WHERE id = $1",
)
.bind(closed)
@@ -476,7 +484,11 @@ async fn an_ending_without_a_date_closes_the_dated_row_it_ends() -> anyhow::Resu
assert_eq!(row.1, None);
assert_eq!(row.2.as_deref(), Some("unknown"));
assert_eq!(row.3, Some(open), "supersedes 链上");
- assert_eq!(row.4, t("2025-10-15T00:00:00Z"), "锚在说出结束的那份文档");
+ assert_eq!(
+ row.4,
+ t("2025-10-15T00:00:00Z"),
+ "结束端锚在说出结束的那份文档"
+ );
// #345 那道题:2026 年 1 月不该再有 Staff Engineer;2024 年 6 月还有
assert!(!edges_at(&pool, &f, Some("2026-01-01T00:00:00Z"))
@@ -700,3 +712,89 @@ async fn a_derivation_reads_no_further_than_its_premises() -> anyhow::Result<()>
.await?;
run
}
+
+/// #393:没起点的裸行也关得上——起点留着第一份证据的锚点,终点锚在说出结束的那份文档。
+/// 从前去重把这句结束并进裸行,「它结束了」丢掉,那行继续读成「至今仍是」。
+#[tokio::test]
+async fn a_bare_row_closes_with_an_anchor_at_each_end() -> 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 {
+ // 2024-02-20 的便签:担任 Staff Engineer,没写从哪天起
+ let (bare, _) = fact(
+ &pool,
+ &f,
+ f.lin,
+ f.holds_title,
+ f.staff_engineer,
+ Validity::default().attested(Some(t("2024-02-20T00:00:00Z"))),
+ )
+ .await?;
+ // 2025-10-15 的角色说明:不再担任,日期未记录——同一断言,也没起点
+ let (closed, created) = fact(
+ &pool,
+ &f,
+ f.lin,
+ f.holds_title,
+ f.staff_engineer,
+ Validity::default()
+ .ended_when_unknown()
+ .attested(Some(t("2025-10-15T00:00:00Z"))),
+ )
+ .await?;
+ assert!(created, "关上是一条修正行,不再是并进去");
+ assert_ne!(closed, bare);
+
+ let (from, to_prec, attested_from, attested_to): BareClosedRow = sqlx::query_as(
+ "SELECT valid_from, valid_to_precision, attested_from, attested_to
+ FROM facts WHERE id = $1",
+ )
+ .bind(closed)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(from, None, "起点原文没给,仍然没给");
+ assert_eq!(to_prec.as_deref(), Some("unknown"));
+ assert_eq!(
+ attested_from,
+ t("2024-02-20T00:00:00Z"),
+ "起点的锚点是第一份证据"
+ );
+ assert_eq!(
+ attested_to,
+ Some(t("2025-10-15T00:00:00Z")),
+ "终点的锚点是说出结束的那份文档"
+ );
+
+ // 读出来:[第一份证据, 说结束的文档)
+ assert!(!edges_at(&pool, &f, Some("2023-12-01T00:00:00Z"))
+ .await?
+ .contains(&closed));
+ assert!(edges_at(&pool, &f, Some("2024-06-01T00:00:00Z"))
+ .await?
+ .contains(&closed));
+ assert!(!edges_at(&pool, &f, Some("2026-01-01T00:00:00Z"))
+ .await?
+ .contains(&closed));
+ let (_, edges) =
+ utopia_store::graph::neighborhood(&pool, f.kb, f.lin, 1, None, None).await?;
+ let row = edges.iter().find(|e| e.id == closed).unwrap();
+ assert_eq!(row.holds_from, Some(t("2024-02-20T00:00:00Z")));
+ assert_eq!(row.holds_to, Some(t("2025-10-15T00:00:00Z")));
+ 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.md
index bdd10b3a..4e78312a 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 · 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 · third cut (#393): a second anchor, `attested_to`, so a bare open row closes too
- **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).
@@ -44,21 +44,21 @@ The client never re-derives it either. Edges and entity facts gain `holds_from`
### 2. An unknown bound reaches as far as the evidence, and no further
-`facts.attested_at TIMESTAMPTZ NOT NULL`: the earliest date among the documents whose observations were merged into the row. The rule:
+Two anchors, one per end (the first cut had one, `attested_at`, and #393 showed why that is one too few — see below). `facts.attested_from TIMESTAMPTZ NOT NULL`: the earliest date among the documents whose observations were merged into the row. `facts.attested_to TIMESTAMPTZ`: the date of the document that said the fact was over, present exactly when the end is `'unknown'` (a CHECK ties the two). The rule:
-- the lower bound is `valid_from` when stated, else `attested_at`;
-- the upper bound is `valid_to` when stated; `attested_at` when the precision says `'unknown'`; open otherwise.
+- the lower bound is `valid_from` when stated, else `attested_from`;
+- the upper bound is `valid_to` when stated; `attested_to` when the precision says `'unknown'`; open otherwise.
```sql
-COALESCE(f.valid_from, f.attested_at) <= $at
+COALESCE(f.valid_from, f.attested_from) <= $at
AND CASE WHEN f.valid_to IS NOT NULL THEN f.valid_to > $at
- WHEN f.valid_to_precision = 'unknown' THEN f.attested_at > $at
+ WHEN f.valid_to_precision = 'unknown' THEN f.attested_to > $at
ELSE TRUE END
```
The asymmetry between the two ends is kept on purpose. An open end still reads as *holds until told otherwise*: forward continuation is a convention the ledger can afford, because endings arrive as records — a later document, a person's correction — and close the row. A missing start does not read as *since always*, because backward continuation has no corrector; nothing will ever arrive to say "and in 2023 it had not started yet". So a fact holds from the moment there is evidence for it, and before that the honest answer is none: a raise approved in a note dated 2024-02-20 was approved no later than that, and nothing places it in January 2023 (#352). An ending the text states without a date bounds the fact from above at the document that states it (#345): the row says "does not hold by 2025-10-15", and now the reads say the same.
-A row with both ends unknown — "no longer holds X", date not recorded, start never given — holds at no moment. It is a closing statement, and reading it as one is right; the panel still lists it under history, marked ended.
+A row with no stated start that an undated ending closes holds from its first evidence to the ending's document — `[attested_from, attested_to)`; that is what the second anchor buys. A fresh row that only says "no longer holds X", date not recorded and start never given, has the same document at both ends and holds at no moment: it is a closing statement, and reading it as one is right; the panel still lists it under history, marked ended.
Why the document's date and not `recorded_at`: back-filled corpora. The benchmark ingests documents about 2023–2025 in one evening; anchored on `recorded_at`, every undated fact would appear in 2026 and at no historical moment. `recorded_at` is also the other clock — 0019 separated the two and this record does not leak one into the other. `documents.doc_time` is world-axis (published, modified, or given on ingest).
@@ -100,6 +100,6 @@ The slider and a timed question stop returning a fact before its evidence or aft
## Open questions
- **An ending does not close the dated row it ends.** ~~`reconcile_new_fact` returns early for any fact that has ended, so the "no longer holds" row from #352 sits beside the 2023-06-01 row that still says "holds".~~ Settled in the first cut, because the benchmark showed the read rule alone leaves #345's question failing: the ended-unknown row read correctly, and the dated open row beside it went on holding. An undated ending that meets an open row **with a stated start** of the same assertion now closes it — a superseding row with `'unknown'` at the end, anchored on the ending's document, the old row invalidated (`temporal::close_with_unknown_end`). A repeated ending reuses the closed row and only moves its anchor earlier.
-- **A bare open row cannot be closed the same way** (#393). With no stated start the one anchor would have to serve both ends — held from the first note, ended by the second. Today the exact-duplicate path in `insert_fact_inner` merges the ending into the bare row and drops it. Two anchors is the likely answer; it is a schema decision and waits for its own line.
+- **A bare open row could not be closed the same way** (#393). ~~With no stated start the one anchor would have to serve both ends — held from the first note, ended by the second. The exact-duplicate path in `insert_fact_inner` merged the ending into the bare row and dropped it.~~ Settled with a second anchor: `attested_at` became `attested_from`, and `attested_to` carries the date of the document that stated the ending, present exactly when the end is `'unknown'`. `close_with_unknown_end` now closes any open row of the assertion — stated start or not — keeping `attested_from` and setting `attested_to`; a repeated ending only moves either anchor earlier. Every superseding writer copies both; a person's own edit that marks a fact ended-unknown anchors the end at now.
- **Retrieval is untimed.** Vector and full-text recall take `as_of` (0019) but no `at`; a chunk about 2025 answers a question about 2023 and the model is left to notice. Out of scope; noted so the benchmark's chat probe is read with that in mind.
- **Showing the anchor.** Whether the panel should say "attested 2024-02-20" beside a blank start, so a person sees why the slider hides the edge before then. Deferred until the first cut has been looked at.
diff --git a/docs/decisions/README.md b/docs/decisions/README.md
index 8c1dfc99..0b235f12 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) | 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 |
+| 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) |
| 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 |
diff --git a/migrations/0034_an_ending_has_its_own_anchor.sql b/migrations/0034_an_ending_has_its_own_anchor.sql
new file mode 100644
index 00000000..3864abe1
--- /dev/null
+++ b/migrations/0034_an_ending_has_its_own_anchor.sql
@@ -0,0 +1,21 @@
+-- #393(0022 的第一个开问题):结束端要有自己的锚点。
+--
+-- 0022 给每行一个锚点 attested_at——各次观察里最早那份文档的日期:没起点的事实从它起
+-- 成立,结束了不知哪天的到它为止。一个锚点装不下两头:没起点的裸行被一句「不再担任,
+-- 日期未记录」关上时,起点要用第一份证据的日期、终点要用说出结束的那份文档的日期。
+-- 于是那种行从前关不上——去重把结束并进裸行,「它结束了」这唯一带来的信息就丢了。
+--
+-- attested_at 改名 attested_from,只管起点;新加 attested_to,只管「结束了不知哪天」的
+-- 终点——有这个状态才有它,没有就是 NULL,CHECK 钉住等价。三值逻辑的坑照旧:先写
+-- IS NOT NULL 再比字面量,否则精度为 NULL 的行会让 CHECK 静默放行。
+ALTER TABLE facts RENAME COLUMN attested_at TO attested_from;
+ALTER TABLE facts ADD COLUMN attested_to TIMESTAMPTZ;
+
+-- 回填:结束未知的行,说出结束的就是它自己的证据(0022 第一刀关行时把锚点换成了它,
+-- 新立的结束行锚点本来就是它那份文档)
+UPDATE facts SET attested_to = attested_from
+ WHERE valid_to IS NULL AND valid_to_precision IS NOT NULL AND valid_to_precision = 'unknown';
+
+ALTER TABLE facts ADD CONSTRAINT facts_ended_unknown_has_anchor CHECK (
+ (valid_to IS NULL AND valid_to_precision IS NOT NULL AND valid_to_precision = 'unknown')
+ = (attested_to IS NOT NULL));