Skip to content

World's first open-source enterprise world model.

An end moved to the end of its period says so on the row (#1295)

Maya Kidfea5171ffc1d
diff --git a/crates/utopia-cli/src/main.rs b/crates/utopia-cli/src/main.rs
index 7a9ba501..c88fc1b7 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 = 101;
+const CURRENT_SCHEMA_VERSION: u32 = 102;
fn main() -> anyhow::Result<()> {
dotenvy::dotenv().ok();
diff --git a/crates/utopia-core/src/models.rs b/crates/utopia-core/src/models.rs
index 360bef0c..81ba60c4 100644
--- a/crates/utopia-core/src/models.rs
+++ b/crates/utopia-core/src/models.rs
@@ -830,6 +830,9 @@ pub struct EntityFact {
/// 终点是时间线推出来的(`facts.end_derived`):后一条事实开始时引擎把它关上,原文没说
/// 这一天(#970)
pub end_derived: bool,
+ /// `valid_to` 仍是存的边界;true 说原文讲的是在它之前结束的那个时段(`facts.end_after_period`,
+ /// #994):「2024 年离开」存 2025-01-01,原文说的是 2024
+ pub end_after_period: bool,
/// 区间是人改过的(#970):`corrected_ends` 有值。与 `corrected` 不同,那一位是任何一次
/// 改写:自动接续、审阅裁决、人手改
pub time_corrected: bool,
diff --git a/crates/utopia-server/src/api/chat_graph_citation_tests.rs b/crates/utopia-server/src/api/chat_graph_citation_tests.rs
index f5c23b28..8b09de74 100644
--- a/crates/utopia-server/src/api/chat_graph_citation_tests.rs
+++ b/crates/utopia-server/src/api/chat_graph_citation_tests.rs
@@ -696,6 +696,7 @@ async fn a_fact_line_says_when_a_date_is_not_the_passages() -> anyhow::Result<()
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ end_after_period: false,
},
)
.await?
@@ -820,6 +821,7 @@ async fn a_fact_line_says_when_a_date_is_not_the_passages() -> anyhow::Result<()
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ end_after_period: false,
},
)
.await?
diff --git a/crates/utopia-server/src/api/graph_routes.rs b/crates/utopia-server/src/api/graph_routes.rs
index eed176b9..728c51f0 100644
--- a/crates/utopia-server/src/api/graph_routes.rs
+++ b/crates/utopia-server/src/api/graph_routes.rs
@@ -10,6 +10,10 @@ use crate::auth::AuthUser;
use crate::error::ApiResult;
use crate::state::AppState;
+#[cfg(test)]
+#[path = "graph_routes_time_tests.rs"]
+mod time_tests;
+
pub(super) async fn require_kb(
state: &AppState,
user: &utopia_core::models::User,
@@ -273,7 +277,8 @@ pub async fn update_entity(
/// 一条事实的新有效区间。**整体替换,不是部分更新**:区间的两端互相定义,
/// 「把结束端清空」与「这次不动结束端」必须能分辨,而缺字段与 null 在 JSON 里
-/// 分不开。界面提交的是表单里的四个值,不是差异。
+/// 分不开。界面提交的是表单里的四个值,不是差异。`end_after_period` 是例外:缺省即
+/// 保留,见那一格。
#[derive(Deserialize)]
pub struct FactTimePatch {
#[serde(default)]
@@ -289,6 +294,13 @@ pub struct FactTimePatch {
/// 这里升格成字段
#[serde(default)]
pub note: Option<String>,
+ /// 终点是不是原文所说时段之后的那一刻(`facts.end_after_period`,#994)。**唯一一个
+ /// 缺省即保留的字段**:读出来什么就发回什么的客户端(不认识这个字段的也一样)不该
+ /// 改掉它。不给时,终点(截到精度之后的值与精度)没变、起点还在,就留着存的标记;清掉
+ /// 起点的保留时刻、丢掉标记(没有起点的行第一刀不标);别的终点照给的收下。路由从不
+ /// 换算:存的、收的都是边界本身,按时段显示与换算是界面的事
+ #[serde(default)]
+ pub end_after_period: Option<bool>,
}
// 梯子只有一张(0024):与数据库的 CHECK、抽取端、显示端同一列
@@ -301,9 +313,21 @@ struct FactInterval {
valid_from_precision: Option<String>,
valid_to: Option<chrono::DateTime<chrono::Utc>>,
valid_to_precision: Option<String>,
+ end_after_period: bool,
+ /// 谓词声明成状态:标记只能落在这样的行上
+ declared_state: bool,
+}
+
+/// 显式要标记,却不是一个能带它的终点
+fn bad_end_after_period() -> AppError {
+ AppError::invalid(
+ "bad_end_after_period",
+ "An end that names a period needs a state property, a start and an end date.",
+ )
}
-/// 复刻 `facts` 表的两条 CHECK,外加一条数据库没有的:起点不能晚于终点。
+/// 复刻 `facts` 表的 CHECK:两端的精度,标记要两端带得了它(`period_mark_fits`,属性是不是
+/// 状态读到行之后再查);外加一条数据库没有的:截到各自精度之后,起点不能晚于终点。
///
/// 在这里挡住是为了让错误说人话。交给数据库挡会得到一句约束名,而这三态
/// (仍在持续 / 结束了不知哪天 / 某日结束)本来就是账本里最容易记混的地方。
@@ -330,8 +354,19 @@ fn check_interval(p: &FactTimePatch) -> Result<(), AppError> {
))
}
}
+ // 显式要标记:两端得带得了它。属性是不是状态要读到行才知道,这里先当它是,只看两端
+ if p.end_after_period == Some(true)
+ && !utopia_store::graph::period_mark_fits(true, p.valid_from, p.valid_to, false)
+ {
+ return Err(bad_end_after_period());
+ }
+ // 比截到各自精度之后的两端:存下的就是截过的。比原值时,小时精度的 08:30 终点与分钟精度
+ // 的 08:10 起点放得过去,截完成了 08:10 → 08:00,存下一段倒着的区间
+ let truncate = utopia_store::graph::truncate_to;
if let (Some(from), Some(to)) = (p.valid_from, p.valid_to) {
- if from > to {
+ if truncate(from, p.valid_from_precision.as_deref())
+ > truncate(to, p.valid_to_precision.as_deref())
+ {
return Err(AppError::invalid(
"interval_inverted",
"The start of the interval is after its end.",
@@ -360,9 +395,12 @@ pub async fn update_fact_time(
// 归属与状态校验:本 KB 的、未作废的事实才可改。派生事实不在此列——
// 它的区间来自前提,改它等于改一个算出来的值,下一轮推理就会覆盖
let before: Option<FactInterval> = sqlx::query_as(
- "SELECT valid_from, valid_from_precision, valid_to, valid_to_precision
- FROM facts
- WHERE id = $1 AND kb_id = $2 AND invalidated_at IS NULL AND derived_by_rule IS NULL",
+ "SELECT f.valid_from, f.valid_from_precision, f.valid_to, f.valid_to_precision,
+ f.end_after_period, COALESCE(r.temporal = 'state', false) AS declared_state
+ FROM facts f
+ LEFT JOIN relation_types r ON r.id = f.predicate_id
+ WHERE f.id = $1 AND f.kb_id = $2 AND f.invalidated_at IS NULL
+ AND f.derived_by_rule IS NULL",
)
.bind(fact_id)
.bind(kb_id)
@@ -372,12 +410,39 @@ pub async fn update_fact_time(
let Some(before) = before else {
return Err(AppError::NotFound.into());
};
+ // 存下的是截到精度之后的值:比较、判标记都按它
+ let truncate = utopia_store::graph::truncate_to;
+ let from = req
+ .valid_from
+ .map(|t| truncate(t, req.valid_from_precision.as_deref()));
+ let to = req
+ .valid_to
+ .map(|t| truncate(t, req.valid_to_precision.as_deref()));
+ // 读到了行,属性是不是状态也知道了:显式要的标记按整条规矩再判一遍
+ if req.end_after_period == Some(true)
+ && !utopia_store::graph::period_mark_fits(before.declared_state, from, to, false)
+ {
+ return Err(bad_end_after_period().into());
+ }
+ // 不给标记:发回存的终点(时刻与精度)、这次的两端还带得了它,就是没动它,留着;清掉起点
+ // 的带不了,丢掉(时刻照旧)。属性这里不再判:存的标记是按当时的属性写下的,原样发回的
+ // 请求不改写这一行
+ let end_after_period = match req.end_after_period {
+ Some(mark) => mark,
+ None => {
+ before.end_after_period
+ && to == before.valid_to
+ && req.valid_to_precision == before.valid_to_precision
+ && utopia_store::graph::period_mark_fits(true, from, to, false)
+ }
+ };
// 一字未改就不写一条修正进账本:History 上多一行「改过」而区间没动,
// 读者会去找那个不存在的差异
- if before.valid_from == req.valid_from
+ if before.valid_from == from
&& before.valid_from_precision == req.valid_from_precision
- && before.valid_to == req.valid_to
+ && before.valid_to == to
&& before.valid_to_precision == req.valid_to_precision
+ && before.end_after_period == end_after_period
{
return Ok(Json(json!({ "ok": true, "unchanged": true })));
}
@@ -393,6 +458,7 @@ pub async fn update_fact_time(
// 修正行从被替代的行继承锚点(correct_interval 的 SQL 里):改区间是重述
// 同一份证据,不是更新的证据
attested_at: None,
+ end_after_period,
};
let Some(corrected) =
utopia_store::temporal::correct_interval(&state.pool, fact_id, validity).await?
@@ -417,12 +483,14 @@ pub async fn update_fact_time(
"valid_from_precision": before.valid_from_precision,
"valid_to": before.valid_to.map(|t| t.to_rfc3339()),
"valid_to_precision": before.valid_to_precision,
+ "end_after_period": before.end_after_period,
});
d["to"] = json!({
"valid_from": req.valid_from.map(|t| t.to_rfc3339()),
"valid_from_precision": req.valid_from_precision,
"valid_to": req.valid_to.map(|t| t.to_rfc3339()),
"valid_to_precision": req.valid_to_precision,
+ "end_after_period": end_after_period,
});
if let Some(note) = req.note.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
d["note"] = json!(note);
diff --git a/crates/utopia-server/src/api/graph_routes_time_tests.rs b/crates/utopia-server/src/api/graph_routes_time_tests.rs
new file mode 100644
index 00000000..3ddaa6a3
--- /dev/null
+++ b/crates/utopia-server/src/api/graph_routes_time_tests.rs
@@ -0,0 +1,168 @@
+//! 改区间的路由收发的是存的边界(#994):`end_after_period` 是唯一一个缺省即保留的字段。
+//!
+//! 实体详情读得到标记;不带它的请求,终点(时刻与精度)没动、起点还在就留着,改了终点或清掉
+//! 起点就丢掉(时刻照旧);显式的值照收,只改标记也算改了终点;显式 true 得是状态属性、有起点、
+//! 终点有日期;倒置按截到精度之后的两端判。每一步的结果收进一张表一起比,dev 上哪几步不同一次
+//! 看全。没有 `UTOPIA_DATABASE_URL` 时跳过。
+use axum::body::{to_bytes, Body};
+use axum::http::{Request, StatusCode};
+use axum::Router;
+use chrono::{DateTime, Utc};
+use serde_json::{json, Value};
+use std::sync::Arc;
+use tower::ServiceExt;
+use uuid::Uuid;
+
+const MARCH: &str = "2024-03-01T00:00:00Z";
+const FEB: &str = "2024-02-01T00:00:00Z";
+const END: &str = "2025-01-01T00:00:00Z";
+const LATER: &str = "2026-01-01T00:00:00Z";
+
+async fn send(
+ app: &Router,
+ token: &str,
+ method: &str,
+ uri: String,
+ body: &Value,
+) -> anyhow::Result<(StatusCode, Value)> {
+ let response = app
+ .clone()
+ .oneshot(
+ Request::builder()
+ .method(method)
+ .uri(uri)
+ .header("content-type", "application/json")
+ .header("authorization", format!("Bearer {token}"))
+ .body(Body::from(body.to_string()))?,
+ )
+ .await?;
+ let status = response.status();
+ let bytes = to_bytes(response.into_body(), usize::MAX).await?;
+ Ok((status, serde_json::from_slice(&bytes)?))
+}
+
+/// 一个请求体:起点日精度、终点年精度,空串是没有;标记给了才带
+fn span(from: &str, to: &str, mark: Option<bool>) -> Value {
+ let from = Some(from).filter(|t| !t.is_empty());
+ let to = Some(to).filter(|t| !t.is_empty());
+ let mut body = json!({
+ "valid_from": from, "valid_from_precision": from.map(|_| "day"),
+ "valid_to": to, "valid_to_precision": to.map(|_| "year"),
+ });
+ if let Some(mark) = mark {
+ body["end_after_period"] = json!(mark);
+ }
+ body
+}
+
+#[tokio::test]
+async fn a_correction_keeps_the_mark_unless_the_request_changes_it() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = sqlx::PgPool::connect(&url).await?;
+ utopia_store::db::migrate(&pool).await?;
+ let [org, ws, kb, editor, works_for, lin, meridian, marked, loose] =
+ std::array::from_fn(|_| Uuid::now_v7());
+ // Only locally generated UUIDs and the constants above are interpolated into fixture SQL.
+ sqlx::raw_sql(&format!(
+ "INSERT INTO organizations(id,name) VALUES ('{org}','fact-time-route');
+ INSERT INTO workspaces(id,org_id,name) VALUES ('{ws}','{org}','fact-time-route');
+ INSERT INTO users(id,org_id,email,display_name,password_hash)
+ VALUES ('{editor}','{org}','{editor}@time.test','editor','unused');
+ INSERT INTO knowledge_bases(id,workspace_id,name) VALUES ('{kb}','{ws}','times');
+ INSERT INTO kb_members(kb_id,user_id,role) VALUES ('{kb}','{editor}','editor');
+ INSERT INTO relation_types(id,kb_id,key,label,temporal)
+ VALUES ('{works_for}','{kb}','works_for','works for','state');
+ INSERT INTO entities(id,kb_id,canonical_name)
+ VALUES ('{lin}','{kb}','Lin Zhao'), ('{meridian}','{kb}','Meridian Systems');
+ -- 「left in 2024」关上的一行,记着说的是 2024;同样的区间、没有谓词的一行
+ INSERT INTO facts(id,kb_id,subject_id,predicate_id,object_id,layer,confidence,
+ valid_from,valid_from_precision,valid_to,valid_to_precision,
+ end_after_period) VALUES
+ ('{marked}','{kb}','{lin}','{works_for}','{meridian}','typed',0.9,
+ '{MARCH}','day','{END}','year',TRUE),
+ ('{loose}','{kb}','{lin}',NULL,'{meridian}','typed',0.9,
+ '{MARCH}','day','{END}','year',FALSE);"
+ ))
+ .execute(&pool)
+ .await?;
+ let result = async {
+ let dir = tempfile::tempdir()?;
+ let config = utopia_core::config::AppConfig {
+ data_dir: dir.path().to_string_lossy().into_owned(),
+ ..Default::default()
+ };
+ let search = Arc::new(utopia_search::SearchIndex::open(&dir.path().join("search"))?);
+ let state = crate::state::AppState::new(pool.clone(), &config, search, "test-only".into());
+ let token = crate::auth::issue_token(&state, editor)?;
+ let app = crate::api::router(state, &config);
+
+ let detail = format!("/api/v1/kbs/{kb}/entities/{lin}");
+ let (_, detail) = send(&app, &token, "GET", detail, &Value::Null).await?;
+ let mut read = detail["facts"].as_array().into_iter().flatten();
+ let read = read.find(|x| x["id"] == json!(marked));
+ let mut seen = vec![("entity detail", json!(read.map(|x| &x["end_after_period"])))];
+ let mut wanted = vec![("entity detail", json!(true))];
+ // 每一步:改哪一行(None 是上一步写下的那行)、请求体、要的结果——写下的那一行(标记、
+ // 人改过哪一端、终点),或拒绝的代码。不带标记的:只改起点留着,改了终点的时刻或精度、
+ // 清掉起点都丢掉;显式的照收,只改标记把终点那一端记成人改的;显式 true 缺起点、缺终点、
+ // 不是状态的都不收
+ let bad = json!([422, "bad_end_after_period"]);
+ let steps = [
+ ("start only", None, span(FEB, END, None), json!([true, "start", END])),
+ ("mark false", None, span(FEB, END, Some(false)), json!([false, "both", END])),
+ ("mark true", None, span(FEB, END, Some(true)), json!([true, "both", END])),
+ ("new end", None, span(FEB, LATER, None), json!([false, "both", LATER])),
+ ("mark true again", None, span(FEB, END, Some(true)), json!([true, "both", END])),
+ (
+ "precision only",
+ None,
+ json!({"valid_from": FEB, "valid_from_precision": "day",
+ "valid_to": END, "valid_to_precision": "month"}),
+ json!([false, "both", END]),
+ ),
+ ("marked once more", None, span(FEB, END, Some(true)), json!([true, "both", END])),
+ ("start cleared", None, span("", END, None), json!([false, "both", END])),
+ ("true, no start", None, span("", END, Some(true)), bad.clone()),
+ ("true, no end", None, span(FEB, "", Some(true)), bad.clone()),
+ ("true, no state", Some(loose), span(MARCH, END, Some(true)), bad),
+ // 小时精度的 08:30 是 08:00,早于分钟精度的起点 08:10
+ (
+ "inverted once truncated",
+ Some(loose),
+ json!({"valid_from": "2026-06-01T08:10:00Z", "valid_from_precision": "minute",
+ "valid_to": "2026-06-01T08:30:00Z", "valid_to_precision": "hour"}),
+ json!([422, "interval_inverted"]),
+ ),
+ ];
+ let mut fact = marked;
+ for (label, target, body, want) in steps {
+ let uri = format!("/api/v1/kbs/{kb}/facts/{}", target.unwrap_or(fact));
+ let (status, reply) = send(&app, &token, "PATCH", uri, &body).await?;
+ let got = match reply["fact_id"].as_str() {
+ Some(id) => {
+ fact = id.parse()?;
+ let row: (bool, Option<String>, Option<DateTime<Utc>>) = sqlx::query_as(
+ "SELECT end_after_period, corrected_ends, valid_to FROM facts WHERE id = $1",
+ )
+ .bind(fact)
+ .fetch_one(&pool)
+ .await?;
+ json!(row)
+ }
+ None => json!([status.as_u16(), reply["code"]]),
+ };
+ seen.push((label, got));
+ wanted.push((label, want));
+ }
+ assert_eq!(seen, wanted);
+ anyhow::Ok(())
+ }
+ .await;
+ sqlx::query("DELETE FROM organizations WHERE id = $1")
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ result
+}
diff --git a/crates/utopia-server/src/api/tools.rs b/crates/utopia-server/src/api/tools.rs
index 149f68d0..ec5c6b4e 100644
--- a/crates/utopia-server/src/api/tools.rs
+++ b/crates/utopia-server/src/api/tools.rs
@@ -1532,6 +1532,7 @@ mod tests {
stale: false,
corrected: false,
end_derived: false,
+ end_after_period: false,
time_corrected: false,
corrected_ends: None,
closed_by_id: None,
diff --git a/crates/utopia-server/src/api/tools_graph.rs b/crates/utopia-server/src/api/tools_graph.rs
index 0a1beb83..70fa110f 100644
--- a/crates/utopia-server/src/api/tools_graph.rs
+++ b/crates/utopia-server/src/api/tools_graph.rs
@@ -1500,6 +1500,7 @@ mod tests {
stale: false,
corrected: false,
end_derived: false,
+ end_after_period: false,
time_corrected: false,
corrected_ends: None,
closed_by_id: None,
diff --git a/crates/utopia-store/src/errata.rs b/crates/utopia-store/src/errata.rs
index c8ab95af..e98c92d5 100644
--- a/crates/utopia-store/src/errata.rs
+++ b/crates/utopia-store/src/errata.rs
@@ -617,7 +617,7 @@ async fn apply(
let fact = fact_id.ok_or_else(|| AppError::Validation("no fact to retract".into()))?;
validity_row = sqlx::query_as(
"SELECT valid_from, valid_from_precision, valid_from_grade, valid_to, valid_to_precision,
- attested_from FROM facts WHERE id = $1",
+ end_after_period, attested_from FROM facts WHERE id = $1",
)
.bind(fact)
.fetch_optional(pool)
@@ -647,6 +647,8 @@ async fn apply(
to: v.valid_to,
to_precision: v.valid_to_precision.as_deref(),
attested_at: v.attested_from.or(doc_time),
+ // 新行接旧行的终点,也接它说的是哪个时段(#994);改到事件、恒常属性上的,写入口丢掉
+ end_after_period: v.end_after_period,
};
let object = match (target.object_id, &target.object_value) {
(Some(o), _) => FactObject::Entity(o),
@@ -719,6 +721,7 @@ struct ValidityRow {
valid_from_grade: Option<String>,
valid_to: Option<DateTime<Utc>>,
valid_to_precision: Option<String>,
+ end_after_period: bool,
attested_from: Option<DateTime<Utc>>,
}
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index 1ef3c1a8..ad2c802c 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -340,6 +340,10 @@ pub struct Validity<'a> {
/// `C` 有时间词但锚不到。`None` = 没经过时间解析(人写的、规则算的),按 A 算。
/// 时态引擎读它决定这一行能不能关上前任
pub from_grade: Option<&'a str>,
+ /// 终点是原文所说时段之后的那一刻(`facts.end_after_period`,#994):「2024 年离开」关上
+ /// 2024-03-01 起的那段,存 2025-01-01,原文说的是 2024。只看值分不出它和「到 2025 年」。
+ /// 写入口归一(`drop_period_mark_unless`),只留在带得了它的行上([`period_mark_fits`])
+ pub end_after_period: bool,
}
/// `valid_to_precision` 表示「结束了,但不知道是哪天」。
@@ -458,6 +462,7 @@ impl<'a> Validity<'a> {
to_precision: None,
attested_at: None,
from_grade: None,
+ end_after_period: false,
}
}
@@ -527,6 +532,15 @@ impl<'a> Validity<'a> {
self
}
+ /// 「终点说的是它之前那个时段」带不了的行([`period_mark_fits`]),标记在这里丢掉:两个
+ /// 写入口(`insert_fact_on`、`correct_interval`)在 [`Validity::under`] 之后各过一遍。
+ /// 事件归一成一刻、恒常抹掉日期、没有谓词的行不过 `under`,都落在这里。进写入口的终点是
+ /// 写明的,不是时间线画的
+ pub(crate) fn drop_period_mark_unless(mut self, declared_state: bool) -> Self {
+ self.end_after_period &= period_mark_fits(declared_state, self.from, self.to, false);
+ self
+ }
+
/// 这条断言是否已经不再成立——**两种结束都算**。
///
/// 判据写在这里而不是散在各处的 `valid_to.is_some()`:那种写法会把
@@ -536,6 +550,23 @@ impl<'a> Validity<'a> {
}
}
+/// 一个终点带不带得了「说的是它之前那个时段」的标记(`end_after_period`,#994):属性声明成
+/// 状态、有起点、终点有日期、不是时间线画的。
+///
+/// 与约束 `facts_end_after_period_needs_a_stated_end` 同一句话(属性是不是状态约束看不到,
+/// 由这里查)。Rust 这边判这件事的都调它:写入口的归一(`Validity::drop_period_mark_unless`)、
+/// `temporal::rewrite_end_tx` 落标记前、`temporal::mark_end_after_period` 补标记前、改区间的
+/// 路由。没有起点的行第一刀不标:终点只在起点落在那个时段里时才被挪到时段的尽头。第二刀让
+/// 时间解析自己写这种终点时,起点那一条在这里和约束里一起去掉
+pub fn period_mark_fits(
+ declared_state: bool,
+ from: Option<chrono::DateTime<chrono::Utc>>,
+ to: Option<chrono::DateTime<chrono::Utc>>,
+ end_derived: bool,
+) -> bool {
+ declared_state && from.is_some() && to.is_some() && !end_derived
+}
+
/// 一段起止同值的状态(#966):写入、改区间、关上都拒它,说的是同一句话
pub fn empty_state_span() -> AppError {
AppError::invalid(
@@ -592,7 +623,8 @@ pub(crate) async fn insert_fact_on(
validity.truncated().under(temporal)?
} else {
validity.truncated()
- };
+ }
+ .drop_period_mark_unless(declared_state);
let same_sql = match object {
FactObject::Entity(_) => {
"SELECT id, valid_from, valid_to, valid_to_precision FROM facts
@@ -650,9 +682,14 @@ pub(crate) async fn insert_fact_on(
&& vtp.as_deref() == Some(ENDED_UNKNOWN)
&& validity.from.is_none_or(|f| Some(f) == *vf)
}) {
- if let Some(stated) =
- crate::temporal::state_derived_end(&mut *conn, *ended, None, validity.attested_at)
- .await?
+ if let Some(stated) = crate::temporal::state_derived_end(
+ &mut *conn,
+ *ended,
+ None,
+ false,
+ validity.attested_at,
+ )
+ .await?
{
return Ok((stated, true));
}
@@ -674,27 +711,43 @@ pub(crate) async fn insert_fact_on(
// 照旧关在时段的开头(0053 修订 2026-09-27)。同一天开始又结束的也是这样:关在
// 那一天的尽头,不关在它自己的起点——那是一段不成立的状态(#966)
let period_end = declared_state.then(|| bucket_end(to, Some(precision)));
+ // 关在哪一刻,和终点是不是因此挪到了时段的尽头:挪了的,行上记下终点说的是它之前
+ // 那个时段(`end_after_period`,#994),读者才印得出原文说的 2024
let closes_at = |from: Option<chrono::DateTime<chrono::Utc>>| match (from, period_end) {
- (Some(f), Some(end)) if to <= f && f < end => end,
- _ => to,
+ (Some(f), Some(end)) if to <= f && f < end => (end, true),
+ _ => (to, false),
};
// 已经关在那一刻的:同一件事,复用那一行(引擎推的终点改成写明的,同上)
if let Some((ended, from, _, _)) = same
.iter()
- .find(|(_, vf, vt, _)| *vt == Some(closes_at(*vf)))
+ .find(|(_, vf, vt, _)| *vt == Some(closes_at(*vf).0))
{
+ let (at, moved) = closes_at(*from);
if let Some(stated) = crate::temporal::state_derived_end(
&mut *conn,
*ended,
- Some((closes_at(*from), precision)),
+ Some((at, precision)),
+ moved,
validity.attested_at,
)
.await?
{
return Ok((stated, true));
}
- attest_earlier(&mut *conn, *ended, validity.attested_at, true).await?;
- return Ok((*ended, false));
+ // 写明的终点被另一种说法再说一遍:那一行是「到 2025 年」关上的,这次是「2024 年
+ // 离开」,同一个终点。标上它,两句谁先到都一样。上面只比了时刻:人改过的终点、
+ // 别的精度写明的终点不改,由 `mark_end_after_period` 锁住那一行再查。
+ // 标上了也是并进那一行,不报新行:报成新行,物化会给它记上这句的
+ // `from_statement_id`、规则会记上 `implied`,人关上的行就成了算出来的,这句的
+ // 陈述一没,它跟着被作废
+ let marked = if moved {
+ crate::temporal::mark_end_after_period(&mut *conn, *ended, precision).await?
+ } else {
+ None
+ };
+ let row = marked.unwrap_or(*ended);
+ attest_earlier(&mut *conn, row, validity.attested_at, true).await?;
+ return Ok((row, false));
}
let open = same
.iter()
@@ -708,13 +761,9 @@ pub(crate) async fn insert_fact_on(
})
.max_by_key(|(_, vf, _, _)| *vf);
if let Some((open, from, _, _)) = open {
- if let Some(closed) = crate::temporal::close_superseded(
- &mut *conn,
- *open,
- closes_at(*from),
- precision,
- )
- .await?
+ let (at, moved) = closes_at(*from);
+ if let Some(closed) =
+ crate::temporal::close_stated(&mut *conn, *open, at, precision, moved).await?
{
return Ok((closed, true));
}
@@ -724,13 +773,16 @@ pub(crate) async fn insert_fact_on(
// 精确重复:同 valid_from → 复用。同起点、**这次带了终点、那行还开着** → 关上它
// (「自 2020-01-10 起任董事」之后读到「2020-01-10 至 2024-04-30 任董事」)
if let Some((existing, _, vt, vtp)) = same.iter().find(|(_, vf, _, _)| *vf == validity.from) {
+ // 这次带来的终点若说的是它之前那个时段(勘误改出来的行接旧行的标记),关上时一起带上;
+ // 复用的那行原样留着它自己的终点和标记
if temporal == Temporal::State && vt.is_none() && vtp.is_none() {
if let Some(to) = validity.to {
- if let Some(closed) = crate::temporal::close_superseded(
+ if let Some(closed) = crate::temporal::close_stated(
&mut *conn,
*existing,
to,
validity.to_precision.unwrap_or("day"),
+ validity.end_after_period,
)
.await?
{
@@ -747,6 +799,7 @@ pub(crate) async fn insert_fact_on(
&mut *conn,
*existing,
stated_to,
+ validity.end_after_period,
validity.attested_at,
)
.await?
@@ -800,22 +853,22 @@ pub(crate) async fn insert_fact_on(
if vf.is_some() {
return None;
}
- let end = match (validity.to, *vt) {
+ let (end, moved) = match (validity.to, *vt) {
(None, Some(t)) if declared_state && t <= from => {
let end = bucket_end(t, Some(vtp.as_deref().unwrap_or("day")));
if from >= end {
return None;
}
- Some(end)
+ (Some(end), true)
}
(Some(to), Some(t)) if to != t => return None,
- (_, vt) => vt,
+ (_, vt) => (vt, false),
};
- Some((*id, end, vtp.clone()))
+ Some((*id, end, vtp.clone(), moved))
}),
None => None,
};
- if let Some((_, Some(vt), vtp)) = &refine_target {
+ if let Some((_, Some(vt), vtp, moved)) = &refine_target {
if validity.to.is_none() {
validity.to = Some(*vt);
validity.to_precision = vtp.as_deref().map(|p| match p {
@@ -827,9 +880,11 @@ pub(crate) async fn insert_fact_on(
"second" => "second",
_ => ENDED_UNKNOWN,
});
+ // 沿用来的终点挪到了时段的尽头:与收尾那条路同一个标记,两种陈述顺序写下同一行
+ validity.end_after_period = *moved;
}
}
- let refine_target = refine_target.map(|(id, _, _)| id);
+ let refine_target = refine_target.map(|(id, _, _, _)| id);
let id = Uuid::now_v7();
let insert_sql = match object {
@@ -838,17 +893,17 @@ pub(crate) async fn insert_fact_on(
"INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id,
valid_from, valid_from_precision,
valid_to, valid_to_precision, confidence,
- attested_from, attested_to, valid_from_grade)
+ attested_from, attested_to, valid_from_grade, end_after_period)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, COALESCE($11, now()),
- CASE WHEN $9::text = 'unknown' THEN COALESCE($11, now()) END, $12)"
+ CASE WHEN $9::text = 'unknown' THEN COALESCE($11, now()) END, $12, $13)"
}
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_from, attested_to, valid_from_grade)
+ attested_from, attested_to, valid_from_grade, end_after_period)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, COALESCE($11, now()),
- CASE WHEN $9::text = 'unknown' THEN COALESCE($11, now()) END, $12)"
+ CASE WHEN $9::text = 'unknown' THEN COALESCE($11, now()) END, $12, $13)"
}
};
let mut ins = sqlx::query(insert_sql)
@@ -867,6 +922,7 @@ pub(crate) async fn insert_fact_on(
.bind(confidence)
.bind(validity.attested_at)
.bind(validity.from_grade)
+ .bind(validity.end_after_period)
.execute(&mut *conn)
.await?;
@@ -1750,7 +1806,7 @@ pub async fn entity_detail(
WHERE fe.fact_id = f.id AND {chunk_live})
) AS stale,
(f.supersedes IS NOT NULL) AS corrected,
- f.end_derived, {corrected_ends} AS corrected_ends,
+ f.end_derived, f.end_after_period, {corrected_ends} AS corrected_ends,
{corrected_ends} IS NOT NULL AS time_corrected,
closer.closed_by_id, closer.closed_by, closer.closed_by_value,
CASE WHEN {corrected_ends} IS NOT NULL THEN {correction_note} END
@@ -2543,6 +2599,7 @@ mod temporal_shape_tests {
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ end_after_period: false,
}
.under(Temporal::Event)
.unwrap();
@@ -2560,6 +2617,7 @@ mod temporal_shape_tests {
to_precision: Some("month"),
attested_at: None,
from_grade: None,
+ end_after_period: false,
}
.under(Temporal::Event)
.unwrap();
@@ -2604,6 +2662,7 @@ mod temporal_shape_tests {
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ end_after_period: false,
};
let refused = moment
.under(Temporal::State)
diff --git a/crates/utopia-store/src/materialize.rs b/crates/utopia-store/src/materialize.rs
index cd81523b..2fc72d21 100644
--- a/crates/utopia-store/src/materialize.rs
+++ b/crates/utopia-store/src/materialize.rs
@@ -289,7 +289,8 @@ async fn materialize_in_tx(
let reverse = d.direction == "reverse";
// 一刻的陈述在状态属性下按绑定说的读(#966):开始写成从那一刻起,结束写成到那一刻
// 为止(走 `insert_fact_on`「某天结束了」那条路,关上开放的那段)。什么都不算的
- // 在查询里已经排掉了([`MOMENT_UNREAD`])
+ // 在查询里已经排掉了([`MOMENT_UNREAD`])。陈述不带 `end_after_period`:终点挪没挪到
+ // 时段的尽头由写入路径定(#994 第一刀)
let moment = d.temporal.as_deref() == Some("state")
&& d.valid_from.is_some()
&& d.valid_from == d.valid_to;
@@ -301,6 +302,7 @@ async fn materialize_in_tx(
to: d.valid_to,
to_precision: d.valid_to_precision.as_deref(),
attested_at: d.attested_from,
+ end_after_period: false,
},
(true, Some("start")) => Validity {
from: d.valid_from,
@@ -309,6 +311,7 @@ async fn materialize_in_tx(
to: None,
to_precision: None,
attested_at: d.attested_from,
+ end_after_period: false,
},
(true, Some("end")) => Validity {
from: None,
@@ -317,6 +320,7 @@ async fn materialize_in_tx(
to: d.valid_from,
to_precision: d.valid_from_precision.as_deref(),
attested_at: d.attested_from,
+ end_after_period: false,
},
(true, _) => continue,
};
@@ -592,6 +596,7 @@ async fn imply_in_tx(
to: d.valid_to,
to_precision: d.valid_to_precision.as_deref(),
attested_at: d.attested_from,
+ end_after_period: false,
};
let value;
let object = match (d.object_id, &d.object_value) {
diff --git a/crates/utopia-store/src/pending.rs b/crates/utopia-store/src/pending.rs
index 82280351..8d581902 100644
--- a/crates/utopia-store/src/pending.rs
+++ b/crates/utopia-store/src/pending.rs
@@ -315,6 +315,7 @@ pub async fn confirm(pool: &PgPool, kb_id: Uuid, id: Uuid) -> AppResult<Confirme
attested_at,
// 待定的事实是人或代理提的,日期是直接给的,没经过时间解析
from_grade: None,
+ end_after_period: false,
};
let (fact_id, created) = match (v.object_id, v.object_value.as_ref()) {
(Some(object_id), _) => {
diff --git a/crates/utopia-store/src/temporal.rs b/crates/utopia-store/src/temporal.rs
index 7b21cef7..9b68dda3 100644
--- a/crates/utopia-store/src/temporal.rs
+++ b/crates/utopia-store/src/temporal.rs
@@ -590,7 +590,7 @@ async fn tidy(
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? {
+ if let Some(corrected) = rewrite_end_tx(tx, id, &end, derived, false, false).await? {
rewritten.insert(id, corrected);
report.corrected.push(corrected);
}
@@ -880,6 +880,19 @@ pub async fn close_superseded<'a>(
fact_id: Uuid,
valid_to: DateTime<Utc>,
valid_to_precision: &str,
+) -> AppResult<Option<Uuid>> {
+ // 按日期关上的(审核里关、冲突的 close_at、违规的 fact_closed)给的就是终点那一刻,不标
+ close_stated(pool, fact_id, valid_to, valid_to_precision, false).await
+}
+
+/// [`close_superseded`],外加这个终点是不是原文所说时段之后的那一刻(`end_after_period`,
+/// #994):写入路径把「2024 年离开」关在 2025-01-01 时标上,读者才印得出 2024
+pub(crate) async fn close_stated<'a>(
+ pool: impl sqlx::Acquire<'a, Database = Postgres>,
+ fact_id: Uuid,
+ valid_to: DateTime<Utc>,
+ valid_to_precision: &str,
+ after_period: bool,
) -> AppResult<Option<Uuid>> {
// 闭合点截到它的精度(0024):月精度的闭合就是那个月的 1 日 0 点
let end = End::At(
@@ -887,7 +900,7 @@ pub async fn close_superseded<'a>(
valid_to_precision.to_string(),
);
let mut tx = pool.begin().await?;
- let corrected = rewrite_end_tx(&mut tx, fact_id, &end, false, false).await?;
+ let corrected = rewrite_end_tx(&mut tx, fact_id, &end, false, false, after_period).await?;
tx.commit().await?;
Ok(corrected)
}
@@ -903,19 +916,28 @@ pub async fn close_with_unknown_end<'a>(
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?;
+ let corrected = rewrite_end_tx(
+ &mut tx,
+ fact_id,
+ &End::Unknown(attested_at),
+ false,
+ true,
+ false,
+ )
+ .await?;
tx.commit().await?;
Ok(corrected)
}
/// 原文说出了一条**引擎关上**的行的终点:改写成原文说的,此后不再重算(#679 第三轮评审)。
-/// `valid_to` 为 `None` 是「结束了,不知哪天」,锚在 `attested_at`。返回修正行 id;
-/// 行已作废,或它的终点本来就是写明的,返回 `None`
+/// `valid_to` 为 `None` 是「结束了,不知哪天」,锚在 `attested_at`;`after_period`:原文说的
+/// 是那一刻之前的那个时段(#994)。返回修正行 id;行已作废,或它的终点本来就是写明的,
+/// 返回 `None`
pub async fn state_derived_end<'a>(
pool: impl sqlx::Acquire<'a, Database = Postgres>,
fact_id: Uuid,
valid_to: Option<(DateTime<Utc>, &str)>,
+ after_period: bool,
attested_at: Option<DateTime<Utc>>,
) -> AppResult<Option<Uuid>> {
let end = match valid_to {
@@ -933,15 +955,60 @@ pub async fn state_derived_end<'a>(
.fetch_optional(&mut *tx)
.await?;
let corrected = match derived {
- Some(true) => rewrite_end_tx(&mut tx, fact_id, &end, false, false).await?,
+ Some(true) => rewrite_end_tx(&mut tx, fact_id, &end, false, false, after_period).await?,
_ => None,
};
tx.commit().await?;
Ok(corrected)
}
+/// 一条写明的终点又被另一种说法说了一遍:「until 2025」关上的行又读到「left in 2024」。
+/// 时刻不变,改写成带标记的一行(#994);反过来的顺序在收尾那条路上已经标了,两种顺序
+/// 写下同一行。
+///
+/// 调用方(`insert_fact_on`)只比了时刻,这里锁住那一行再查两件事:精度与这次说的相同——
+/// 日精度写明的 2025-01-01 是那一天,不是 2024 年的尽头;终点不是人改过的
+/// (`corrected_ends` 为空或只有起点)——人把显示的 2024 改成 2025,就是要到 2025 年。
+/// 已经标过的不再标。那一行带不带得了标记(引擎画的终点、没有起点的行带不了),读出来交给
+/// `graph::period_mark_fits` 判。返回修正行 id;不必标的返回 `None`
+pub(crate) async fn mark_end_after_period<'a>(
+ pool: impl sqlx::Acquire<'a, Database = Postgres>,
+ fact_id: Uuid,
+ precision: &str,
+) -> AppResult<Option<Uuid>> {
+ let mut tx = pool.begin().await?;
+ #[allow(clippy::type_complexity)]
+ let locked_row: Option<(Option<DateTime<Utc>>, Option<DateTime<Utc>>, bool, bool)> =
+ sqlx::query_as(
+ "SELECT f.valid_from, f.valid_to, f.end_derived,
+ COALESCE(r.temporal = 'state', false) AS declared_state
+ FROM facts f LEFT JOIN relation_types r ON r.id = f.predicate_id
+ WHERE f.id = $1 AND f.invalidated_at IS NULL
+ AND f.valid_to_precision = $2 AND NOT f.end_after_period
+ AND (f.corrected_ends IS NULL OR f.corrected_ends = 'start')
+ FOR UPDATE OF f",
+ )
+ .bind(fact_id)
+ .bind(precision)
+ .fetch_optional(&mut *tx)
+ .await?;
+ let marked = match locked_row {
+ // 起点那一半人改过的照旧是人的:改写的 CASE 把 'start' 原样带过去
+ Some((from, Some(at), end_derived, declared_state))
+ if crate::graph::period_mark_fits(declared_state, from, Some(at), end_derived) =>
+ {
+ let end = End::At(at, precision.to_string());
+ rewrite_end_tx(&mut tx, fact_id, &end, false, false, true).await?
+ }
+ _ => None,
+ };
+ tx.commit().await?;
+ Ok(marked)
+}
+
/// 作废 + 改写一行的终点。`derived`:这个终点是引擎按时间线推出来的(0057),之后随
-/// 时间线重算;`require_open`:只改还开着的行。
+/// 时间线重算;`require_open`:只改还开着的行;`after_period`:终点是原文所说时段之后的
+/// 那一刻(#994)。
///
/// 先 `FOR UPDATE` 锁住旧行:并行的两次改写只有一次看得见它还活着,另一次拿到 `None`,
/// 不会各插一条修正行;往这一行上添证据的也要等它(`graph::add_evidence`),证据不会落在
@@ -952,6 +1019,7 @@ async fn rewrite_end_tx(
end: &End,
derived: bool,
require_open: bool,
+ after_period: bool,
) -> AppResult<Option<Uuid>> {
let alive: Option<(Option<DateTime<Utc>>, Option<Uuid>)> = sqlx::query_as(
"SELECT valid_from, predicate_id FROM facts
@@ -983,13 +1051,22 @@ async fn rewrite_end_tx(
End::At(at, precision) => (Some(*at), Some(precision.as_str()), None),
End::Unknown(anchor) => (None, Some(crate::graph::ENDED_UNKNOWN), *anchor),
};
+ // 标记只落在带得了它的终点上(`graph::period_mark_fits`):时间线画的、不知哪天的、重新
+ // 打开的终点,没有起点或属性不是状态的行,一律不带——起点和谓词是上面锁住时读的。要标时
+ // 才去查属性:不标的改写(时间线重画、关成不知哪天)不多一次查询
+ let after_period = after_period && {
+ let declared_state = predicate.is_some()
+ && crate::graph::predicate_temporal(&mut **tx, predicate).await?
+ == crate::graph::Temporal::State;
+ crate::graph::period_mark_fits(declared_state, from, valid_to, derived)
+ };
let corrected = 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_from_grade,
valid_to, valid_to_precision, confidence, supersedes,
attested_from, attested_by, attested_to, end_derived,
- from_statement_id, implied, corrected_ends)
+ from_statement_id, implied, corrected_ends, end_after_period)
SELECT $1, kb_id, subject_id, predicate_id, object_id, object_value,
valid_from, valid_from_precision, valid_from_grade, $3, $4, confidence, id,
attested_from, attested_by,
@@ -998,7 +1075,8 @@ async fn rewrite_end_tx(
-- 终点在这里被重新写下(文档说了终点,或时间线推了一个),人改过的终点就不在了;
-- 起点那一半照旧
CASE corrected_ends WHEN 'both' THEN 'start' WHEN 'end' THEN NULL
- ELSE corrected_ends END
+ ELSE corrected_ends END,
+ $7
FROM facts WHERE id = $2",
)
.bind(corrected)
@@ -1007,6 +1085,7 @@ async fn rewrite_end_tx(
.bind(precision)
.bind(anchor)
.bind(derived)
+ .bind(after_period)
.execute(&mut **tx)
.await?;
carry_open_conflicts(tx, fact_id, corrected).await?;
@@ -1073,11 +1152,11 @@ pub async fn rehome_tx(
valid_from, valid_from_precision, valid_to, valid_to_precision,
confidence, derived_by_rule, supersedes,
attested_from, attested_to, end_derived,
- from_statement_id, implied, corrected_ends)
+ from_statement_id, implied, corrected_ends, end_after_period)
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_statement_id, implied, corrected_ends
+ from_statement_id, implied, corrected_ends, end_after_period
FROM facts WHERE id = $2",
)
.bind(moved)
@@ -1184,12 +1263,15 @@ async fn copy_evidence(
/// 改写同一条规矩([`copy_materialization_links`])。不带过去的话,陈述不再被活着的
/// 类型化行代表,画面上它按原话、原来的区间回来,下一轮物化还照它再算一行。
///
-/// 改了哪一端记在修正行上(`corrected_ends`,#970):值或精度变了的那一端,与旧行已有的
-/// 取并集,与修正同一个事务——事实行据此说「这个日期是人改的」,不再靠事后那条审计。
+/// 改了哪一端记在修正行上(`corrected_ends`,#970):值或精度(终点还有标记)变了的那一端,
+/// 与旧行已有的取并集,与修正同一个事务——事实行据此说「这个日期是人改的」,不再靠事后那条审计。
/// 人交来的是整段区间,改完两端都是人的(`end_derived` 清掉,时间线不再重画它):原来
/// 是时间线推出来的终点,人没改它的值也是把它钉住了,一样算人的;原来就是原文说的那一端
/// 没改,仍是原文的
///
+/// 终点是不是原文所说时段之后的那一刻(`end_after_period`,#994)由调用方给,这里只归一:
+/// 修正路由按请求定它,从不替人换算
+///
/// 返回修正行 id;`None` 表示这条已被并发改写或作废,本次没有动手。
pub async fn correct_interval(
pool: &PgPool,
@@ -1209,15 +1291,17 @@ pub async fn correct_interval(
let validity = match predicate {
Some(_) => validity.truncated().under(temporal)?,
None => validity.truncated(),
- };
+ }
+ .drop_period_mark_unless(predicate.is_some() && temporal == crate::graph::Temporal::State);
let mut tx = pool.begin().await?;
let corrected = Uuid::now_v7();
+ // 标记变了也算终点变了(#994):人把显示的 2024 改成 2025,存的时刻没动,说的却是另一年
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,
- from_statement_id, implied, corrected_ends)
+ from_statement_id, implied, corrected_ends, end_after_period)
SELECT $1, kb_id, subject_id, predicate_id, object_id, object_value,
$3, $4, $5, $6, confidence, id,
attested_from,
@@ -1225,7 +1309,8 @@ pub async fn correct_interval(
from_statement_id, implied,
CASE WHEN start_marked AND end_marked THEN 'both'
WHEN start_marked THEN 'start'
- WHEN end_marked THEN 'end' END
+ WHEN end_marked THEN 'end' END,
+ $7
FROM (SELECT f.*,
c.start_changed OR COALESCE(f.corrected_ends IN ('start', 'both'), false)
AS start_marked,
@@ -1238,6 +1323,7 @@ pub async fn correct_interval(
AS start_changed,
f.valid_to IS DISTINCT FROM $5::timestamptz
OR f.valid_to_precision IS DISTINCT FROM $6::text
+ OR f.end_after_period IS DISTINCT FROM $7::boolean
AS end_changed) c
WHERE f.id = $2 AND f.invalidated_at IS NULL) old
RETURNING id",
@@ -1248,6 +1334,7 @@ pub async fn correct_interval(
.bind(validity.from_precision)
.bind(validity.to)
.bind(validity.to_precision)
+ .bind(validity.end_after_period)
.fetch_optional(&mut *tx)
.await?;
// 已被并发修正过:不重复动手(与 close_superseded 同一防线)
diff --git a/crates/utopia-store/tests/a_corrected_row_says_which_end_changed.rs b/crates/utopia-store/tests/a_corrected_row_says_which_end_changed.rs
index 581d46b2..a421e6b7 100644
--- a/crates/utopia-store/tests/a_corrected_row_says_which_end_changed.rs
+++ b/crates/utopia-store/tests/a_corrected_row_says_which_end_changed.rs
@@ -36,6 +36,7 @@ fn between(from: &str, to: Option<&str>) -> Validity<'static> {
to_precision: to.map(|_| "day"),
attested_at: None,
from_grade: None,
+ ..Default::default()
}
}
diff --git a/crates/utopia-store/tests/store/a_fact_awaits_a_nod.rs b/crates/utopia-store/tests/store/a_fact_awaits_a_nod.rs
index 0249cc06..c2a0a082 100644
--- a/crates/utopia-store/tests/store/a_fact_awaits_a_nod.rs
+++ b/crates/utopia-store/tests/store/a_fact_awaits_a_nod.rs
@@ -120,6 +120,7 @@ async fn a_remembered_fact_waits_for_a_nod() -> anyhow::Result<()> {
to_precision: None,
attested_at: None,
from_grade: None,
+ ..Default::default()
},
confidence: 0.9,
chunk_id: f.chunk,
diff --git a/crates/utopia-store/tests/store/a_moment_marks_its_state.rs b/crates/utopia-store/tests/store/a_moment_marks_its_state.rs
index 7f2634eb..d454a997 100644
--- a/crates/utopia-store/tests/store/a_moment_marks_its_state.rs
+++ b/crates/utopia-store/tests/store/a_moment_marks_its_state.rs
@@ -14,6 +14,10 @@
//! 照旧;已有的空段不并进后来的观察,也不被它关上;勘误改不了一段不成立的时间,也不能把
//! 一刻的事件改成状态;规则没有 marks,只说了一刻的陈述不蕴含状态,只有起点的照旧。
//!
+//! 终点因此挪到时段尽头的行记着原文说的是那个时段(`end_after_period`,#994):收尾、精化
+//! 两条路都记;关在「until 2025」的行再读到「left in 2024」也记(人只改过起点的也记),时间线画
+//! 的终点被它说出来也记;搬主宾、勘误改出来的行带着它,改成事件的不带。
+//!
//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
use chrono::{DateTime, Utc};
@@ -45,6 +49,7 @@ fn moment() -> Validity<'static> {
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ ..Default::default()
}
}
@@ -57,6 +62,7 @@ fn ending(at: DateTime<Utc>) -> Validity<'static> {
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ ..Default::default()
}
}
@@ -274,6 +280,21 @@ impl Base {
)
}
+ /// Lin Zhao —works_for→ object,走写入口(抽取、点头、人写的事实同一个门)
+ async fn write_to(&self, object: Uuid, v: Validity<'_>) -> anyhow::Result<Uuid> {
+ let (fact, _) = graph::insert_fact(
+ &self.pool,
+ self.kb,
+ self.lin,
+ Some(self.works_for),
+ object,
+ v,
+ 0.9,
+ )
+ .await?;
+ Ok(fact)
+ }
+
async fn remove(self) -> anyhow::Result<()> {
sqlx::query("DELETE FROM organizations WHERE id = $1")
.bind(self.org)
@@ -997,7 +1018,8 @@ async fn a_row_a_person_set_to_a_single_date_is_not_retired() -> anyhow::Result<
/// 终点说的是一个时段,比的却是时刻(#975 评审):「left … in 2024」存成 2024-01-01,年精度。
/// 「works for … from 2024-03-01」从这个时段里开始,关在时段的尽头 2025-01-01,而不是另立一行
-/// `- → 2024`、让它一直开着到 2025 年以后。先读到哪一句都一样
+/// `- → 2024`、让它一直开着到 2025 年以后。先读到哪一句都一样;行上记着终点说的是 2024
+/// (#994:收尾那条路和精化那条路都记)
#[tokio::test]
async fn a_coarse_end_closes_a_row_that_starts_inside_its_period() -> anyhow::Result<()> {
let Some(pool) = pool().await? else {
@@ -1008,6 +1030,7 @@ async fn a_coarse_end_closes_a_row_that_starts_inside_its_period() -> anyhow::Re
t("2024-01-01T00:00:00Z"),
t("2025-01-01T00:00:00Z"),
);
+ let mut marks: Vec<Vec<bool>> = Vec::new();
for order in [["works for", "left"], ["left", "works for"]] {
let base = Base::new(&pool).await?;
let run = async {
@@ -1037,12 +1060,22 @@ async fn a_coarse_end_closes_a_row_that_starts_inside_its_period() -> anyhow::Re
"{order:?}: not still working there in 2025"
);
assert_eq!(materialize(&pool, base.kb).await?, Outcome::default());
+ marks.push(
+ sqlx::query_scalar(
+ "SELECT end_after_period FROM facts
+ WHERE kb_id = $1 AND layer = 'typed' AND invalidated_at IS NULL",
+ )
+ .bind(base.kb)
+ .fetch_all(&pool)
+ .await?,
+ );
anyhow::Ok(())
}
.await;
base.remove().await?;
run?;
}
+ assert_eq!(marks, [[true], [true]], "the end names 2024, not 2025");
Ok(())
}
@@ -1108,6 +1141,245 @@ async fn a_coarse_end_leaves_rows_outside_its_period_as_before() -> anyhow::Resu
Ok(())
}
+/// 一个终点,两种说法(#994):人写的 2024-03-01 起的一行关在「until 2025」,文档又说「left in
+/// 2024」,同一个终点:标上,并进那一行,它仍是人的(反过来的顺序是收尾那条路);另一份文档再说
+/// 一次,标过的原样留着。人只把起点挪到 4 月的一行(Dune)也标。日精度写明的 2025-01-01(Aster)
+/// 是那一天,人改过的终点(Cobalt)是人的,都不改写
+#[tokio::test]
+async fn one_end_named_two_ways_is_marked() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let base = Base::new(&pool).await?;
+ let run = async {
+ let (year, next_year) = (t("2024-01-01T00:00:00Z"), t("2025-01-01T00:00:00Z"));
+ let since = Validity::starting(Some(t("2024-03-01T00:00:00Z")), Some("day"));
+ let until_2025 = Validity {
+ to_precision: Some("year"),
+ ..ending(next_year)
+ };
+ let (cobalt, dune) = (Uuid::now_v7(), Uuid::now_v7());
+ sqlx::query(
+ "INSERT INTO entities (id, kb_id, type_id, canonical_name)
+ VALUES ($1, $2, $3, 'Cobalt'), ($4, $2, $3, 'Dune')",
+ )
+ .bind(cobalt)
+ .bind(base.kb)
+ .bind(base.company)
+ .bind(dune)
+ .execute(&pool)
+ .await?;
+ for (object, end) in [(base.meridian, until_2025), (base.aster, ending(next_year))] {
+ base.write_to(object, since).await?;
+ base.write_to(object, end).await?;
+ }
+ let by_hand = Validity {
+ to: Some(next_year),
+ to_precision: Some("year"),
+ ..since
+ };
+ let open = base.write_to(cobalt, since).await?;
+ utopia_store::temporal::correct_interval(&pool, open, by_hand).await?;
+ let stated = base.write_to(dune, by_hand).await?;
+ let april = Validity {
+ from: Some(t("2024-04-01T00:00:00Z")),
+ ..by_hand
+ };
+ utopia_store::temporal::correct_interval(&pool, stated, april).await?;
+ for object in [base.meridian, base.meridian, base.aster, cobalt, dune] {
+ base.statement_to("left", object, Some(year), Some(year), "year")
+ .await?;
+ }
+ base.bind("left", Some("end")).await?;
+ materialize(&pool, base.kb).await?;
+ // 每个宾语活着的那一行:标记,仍是人的(不是这句算出来的),那边被取代过几行
+ let rows: Vec<(String, bool, bool, i64)> = sqlx::query_as(
+ "SELECT o.canonical_name, f.end_after_period, f.from_statement_id IS NULL,
+ (SELECT count(*) FROM facts g WHERE g.layer = 'typed'
+ AND g.object_id = f.object_id AND g.invalidated_at IS NOT NULL)
+ FROM facts f JOIN entities o ON o.id = f.object_id
+ WHERE f.kb_id = $1 AND f.layer = 'typed' AND f.invalidated_at IS NULL
+ ORDER BY 1",
+ )
+ .bind(base.kb)
+ .fetch_all(&pool)
+ .await?;
+ // Meridian 开着的那行先被「until 2025」关上、再被标上,各取代一次;Dune 改过一次起点
+ let expected = [
+ ("Aster Labs", false, 1),
+ ("Cobalt", false, 1),
+ ("Dune", true, 2),
+ ("Meridian Systems", true, 2),
+ ]
+ .map(|(o, mark, gone)| (o.to_string(), mark, true, gone));
+ assert_eq!(rows, expected);
+ anyhow::Ok(())
+ }
+ .await;
+ base.remove().await?;
+ run
+}
+
+/// 时间线画的终点被原文说出来(#994):works_for 声明成函数性、继任者 2025-01-01 起,时间线在那天
+/// 画上 Meridian 的终点(日精度);「left in 2024」说的是同一刻,改成写明的年精度终点,记着说的是
+/// 2024
+#[tokio::test]
+async fn a_drawn_end_the_text_names_as_a_period_is_marked() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let base = Base::new(&pool).await?;
+ let run = async {
+ let next_year = t("2025-01-01T00:00:00Z");
+ sqlx::query("UPDATE relation_types SET functional = true WHERE id = $1")
+ .bind(base.works_for)
+ .execute(&pool)
+ .await?;
+ let since = Validity::starting(Some(t("2024-03-01T00:00:00Z")), Some("day"));
+ base.write_to(base.meridian, since).await?;
+ let next = Validity::starting(Some(next_year), Some("day"));
+ let next = base.write_to(base.aster, next).await?;
+ utopia_store::temporal::reconcile_moved_facts(&pool, base.kb, &[next]).await?;
+ // Meridian 那一行的终点:精度、是不是时间线画的、标记
+ let end = || {
+ sqlx::query_as::<_, (String, bool, bool)>(
+ "SELECT valid_to_precision, end_derived, end_after_period FROM facts
+ WHERE object_id = $1 AND invalidated_at IS NULL",
+ )
+ .bind(base.meridian)
+ .fetch_one(&pool)
+ };
+ let drawn = end().await?;
+ let left_in_2024 = Validity {
+ to_precision: Some("year"),
+ ..ending(t("2024-01-01T00:00:00Z"))
+ };
+ base.write_to(base.meridian, left_in_2024).await?;
+ assert_eq!(
+ (drawn, end().await?),
+ (("day".into(), true, false), ("year".into(), false, true)),
+ "drawn, then stated as the end of 2024"
+ );
+ anyhow::Ok(())
+ }
+ .await;
+ base.remove().await?;
+ run
+}
+
+/// 标记随行走(#994):撤回合并搬主宾用的 `rehome_tx` 带着它;勘误改出来的行接旧行的标记——改到
+/// 同起点、还开着的一行的宾语上,那一行经同起点那条路关上;改到同起点、终点是时间线画的一行的
+/// 宾语上,那个终点改成写明的——都带着它;属性改成事件的,写入口丢掉它
+#[tokio::test]
+async fn a_marked_end_rides_through_a_rehome_and_a_revision() -> anyhow::Result<()> {
+ let Some(pool) = pool().await? else {
+ return Ok(());
+ };
+ let base = Base::new(&pool).await?;
+ let run = async {
+ let kb = base.kb;
+ let since = Validity::starting(Some(t("2024-03-01T00:00:00Z")), Some("day"));
+ let closed = Validity {
+ to: Some(t("2025-01-01T00:00:00Z")),
+ to_precision: Some("year"),
+ ..since
+ };
+ let row = base.write_to(base.meridian, closed).await?;
+ let (cobalt, drawn, doc) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
+ let (company, lin, works_for) = (base.company, base.lin, base.works_for);
+ // Only locally generated UUIDs are interpolated into fixture SQL.
+ sqlx::raw_sql(&format!(
+ "UPDATE facts SET end_after_period = TRUE WHERE id = '{row}';
+ INSERT INTO entities (id, kb_id, type_id, canonical_name)
+ VALUES ('{cobalt}', '{kb}', '{company}', 'Cobalt');
+ INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, layer, confidence,
+ valid_from, valid_from_precision, valid_to, valid_to_precision,
+ end_derived)
+ VALUES ('{drawn}', '{kb}', '{lin}', '{works_for}', '{cobalt}', 'typed', 0.9,
+ '2024-03-01T00:00:00Z', 'day', '2025-01-01T00:00:00Z', 'day', TRUE);
+ INSERT INTO documents (id, kb_id, filename, sha256)
+ VALUES ('{doc}', '{kb}', 'cv.txt', 'x');"
+ ))
+ .execute(&pool)
+ .await?;
+ let mut tx = pool.begin().await?;
+ let moved = utopia_store::temporal::rehome_tx(&mut tx, row, None, Some(base.aster))
+ .await?
+ .expect("the row was live");
+ tx.commit().await?;
+
+ // 勘误改三次:宾语改回 Meridian,那边已有一行 2024-03-01 起开着;改到 Cobalt,那边同起点的
+ // 一行终点是时间线画的(日精度,上面写下的);属性改成 acquired(事件)
+ let open = base.write_to(base.meridian, since).await?;
+ let mut candidate = Candidate {
+ fact_id: moved,
+ statement_id: None,
+ predicate_id: base.works_for,
+ subject_id: base.lin,
+ subject: "Lin Zhao".into(),
+ subject_class: Some("person".into()),
+ property: "works_for".into(),
+ property_label: "works for".into(),
+ object_id: Some(base.aster),
+ object: "Aster Labs".into(),
+ object_class: Some("organization".into()),
+ flag: Some("name_absent".into()),
+ quote: None,
+ };
+ // 一行的标记,和它取代的是哪一行
+ let read = |id: Uuid| {
+ sqlx::query_as::<_, (bool, Option<Uuid>)>(
+ "SELECT end_after_period, supersedes FROM facts WHERE id = $1",
+ )
+ .bind(id)
+ .fetch_one(&pool)
+ };
+ let mut rows = vec![read(moved).await?];
+ let run_id = errata::start_run(&pool, kb, doc, 3, 0).await?;
+ for (property, object) in [
+ (None, Some("Meridian Systems")),
+ (None, Some("Cobalt")),
+ (Some("acquired"), None),
+ ] {
+ let recorded = errata::record(
+ &pool,
+ kb,
+ ActionInput {
+ run_id,
+ document_id: doc,
+ candidate: Some(&candidate),
+ proposed: Proposed::Revise {
+ property: property.map(Into::into),
+ object: object.map(Into::into),
+ },
+ reason: "the document says otherwise",
+ quote: Some("Lin Zhao left Aster Labs for Meridian Systems"),
+ document_text: "Lin Zhao left Aster Labs for Meridian Systems in 2024.",
+ },
+ )
+ .await?;
+ candidate.fact_id = recorded
+ .new_fact_id
+ .unwrap_or_else(|| panic!("{recorded:?}"));
+ rows.push(read(candidate.fact_id).await?);
+ }
+ assert_eq!(
+ rows,
+ [
+ (true, Some(row)),
+ (true, Some(open)),
+ (true, Some(drawn)),
+ (false, None)
+ ],
+ "rehomed; revised onto an open row and a drawn end; onto an event"
+ );
+ anyhow::Ok(())
+ }
+ .await;
+ base.remove().await?;
+ run
+}
+
/// 两个值同一刻开始的冲突(works_for 声明成函数性):人选「关上旧的」却不给日期,旧的就关在
/// 新的起点——也是它自己的起点,一段不成立的状态,拒绝(`empty_state_span`)。冲突留着等人
/// 给一个日期
diff --git a/crates/utopia-store/tests/store/a_path_joins_two_entities.rs b/crates/utopia-store/tests/store/a_path_joins_two_entities.rs
index 36d516ae..4ba81a86 100644
--- a/crates/utopia-store/tests/store/a_path_joins_two_entities.rs
+++ b/crates/utopia-store/tests/store/a_path_joins_two_entities.rs
@@ -109,6 +109,7 @@ async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> {
to_precision: to.map(|_| "month"),
attested_at: None,
from_grade: None,
+ ..Default::default()
};
let mut direct = Uuid::nil();
for (s, p, o, v) in [
diff --git a/crates/utopia-store/tests/store/a_timeline_is_recomputed_from_the_rows_it_has.rs b/crates/utopia-store/tests/store/a_timeline_is_recomputed_from_the_rows_it_has.rs
index f6216db5..079f7c75 100644
--- a/crates/utopia-store/tests/store/a_timeline_is_recomputed_from_the_rows_it_has.rs
+++ b/crates/utopia-store/tests/store/a_timeline_is_recomputed_from_the_rows_it_has.rs
@@ -120,6 +120,7 @@ async fn observe(pool: &PgPool, f: &Fixture, x: Seen<'_>) -> anyhow::Result<Uuid
to_precision: x.to.map(|_| "day"),
attested_at: None,
from_grade: x.grade,
+ ..Default::default()
};
let mut chunk = None;
if let Some(doc_time) = x.doc {
@@ -621,6 +622,7 @@ async fn a_revert_keeps_the_interval_a_person_corrected() -> anyhow::Result<()>
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ ..Default::default()
},
)
.await?
diff --git a/crates/utopia-store/tests/store/a_wrong_time_can_be_corrected.rs b/crates/utopia-store/tests/store/a_wrong_time_can_be_corrected.rs
index d06286d1..c0e85d1f 100644
--- a/crates/utopia-store/tests/store/a_wrong_time_can_be_corrected.rs
+++ b/crates/utopia-store/tests/store/a_wrong_time_can_be_corrected.rs
@@ -257,6 +257,7 @@ async fn an_end_that_was_never_there_can_be_taken_back() -> anyhow::Result<()> {
to_precision: Some("year"),
attested_at: None,
from_grade: None,
+ ..Default::default()
},
)
.await?
diff --git a/crates/utopia-store/tests/store/an_end_date_closes_the_open_span.rs b/crates/utopia-store/tests/store/an_end_date_closes_the_open_span.rs
index 0d2a2cd2..da6c351e 100644
--- a/crates/utopia-store/tests/store/an_end_date_closes_the_open_span.rs
+++ b/crates/utopia-store/tests/store/an_end_date_closes_the_open_span.rs
@@ -26,6 +26,7 @@ fn span(
to_precision: to.map(|_| "day"),
attested_at: Some(day(attested)),
from_grade: None,
+ ..Default::default()
}
}
@@ -54,6 +55,7 @@ fn ends(to: &str, attested: &str) -> utopia_store::graph::Validity<'static> {
to_precision: Some("day"),
attested_at: Some(day(attested)),
from_grade: None,
+ ..Default::default()
}
}
@@ -147,6 +149,7 @@ async fn an_end_date_closes_the_open_span() -> anyhow::Result<()> {
to_precision: None,
attested_at: Some(day("2025-08-01")),
from_grade: None,
+ ..Default::default()
},
0.9,
)
diff --git a/crates/utopia-store/tests/store/an_event_holds_at_the_moment_it_names.rs b/crates/utopia-store/tests/store/an_event_holds_at_the_moment_it_names.rs
index 53ac19c7..784d50a3 100644
--- a/crates/utopia-store/tests/store/an_event_holds_at_the_moment_it_names.rs
+++ b/crates/utopia-store/tests/store/an_event_holds_at_the_moment_it_names.rs
@@ -312,6 +312,7 @@ async fn a_span_an_ending_or_an_unknown_end_given_to_an_event_collapses() -> any
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ ..Default::default()
},
)
.await?;
@@ -338,6 +339,7 @@ async fn a_span_an_ending_or_an_unknown_end_given_to_an_event_collapses() -> any
to_precision: Some("month"),
attested_at: None,
from_grade: None,
+ ..Default::default()
},
)
.await?;
@@ -581,6 +583,7 @@ async fn a_person_correcting_an_event_leaves_it_one_moment() -> anyhow::Result<(
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ ..Default::default()
},
)
.await?
diff --git a/crates/utopia-store/tests/store/an_unknown_date_is_not_an_open_one.rs b/crates/utopia-store/tests/store/an_unknown_date_is_not_an_open_one.rs
index 302a93db..010a99b3 100644
--- a/crates/utopia-store/tests/store/an_unknown_date_is_not_an_open_one.rs
+++ b/crates/utopia-store/tests/store/an_unknown_date_is_not_an_open_one.rs
@@ -392,6 +392,7 @@ async fn the_anchor_moves_earlier_and_is_inherited() -> anyhow::Result<()> {
to_precision: Some("day"),
attested_at: None,
from_grade: None,
+ ..Default::default()
},
)
.await?
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
index 8108ce5f..ad013843 100644
--- 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
@@ -152,6 +152,7 @@ async fn observe(pool: &PgPool, f: &Fixture, x: Seen<'_>) -> anyhow::Result<Uuid
x.to.map(|_| "day")
},
attested_at: None,
+ ..Default::default()
};
let mut chunk = None;
if let Some(day) = x.doc {
diff --git a/docs/decisions/0031-an-event-holds-at-the-moment-it-names.md b/docs/decisions/0031-an-event-holds-at-the-moment-it-names.md
index a2398706..790a2f53 100644
--- a/docs/decisions/0031-an-event-holds-at-the-moment-it-names.md
+++ b/docs/decisions/0031-an-event-holds-at-the-moment-it-names.md
@@ -27,6 +27,8 @@ The rule sits in `insert_fact_inner` and `correct_interval`, not in the writers.
2026-09-27: under a property declared as a state, the same rule now refuses a row whose two ends are equal, and a single date bound to a state is read by what its binding says it marks. See [0053](0053-a-phrase-decision-records-the-inputs-it-considered.md)'s revision of that date (#966).
+2026-10-17: an end the closing and refinement paths move to the end of the period it names is now marked on the row, `end_after_period`: the stored instant is the one after that period. See [0053](0053-a-phrase-decision-records-the-inputs-it-considered.md)'s revision of that date (#994).
+
2026-10-18: the export writes `utopia:temporal` on every property, `state` included, and the import reads it on an object property: a new relation takes the value the file declares, a re-import changes an existing one to it and moves its `updated_at` as an edit on the page does, and a file that says nothing leaves a relation alone, so a value a person set survives an unannotated re-import while re-importing an export is exact. A value other than the three is reported under the predicate in the import preview, and the relation stays as it was (#1291).
Both ends carry the value because that is the one shape the row can explain on its own. A reader that does not consult the predicate — an old client, an export consumer, a hand-written query — sees a fact that held on one day. It cannot see *holds from that day on*, which is what an open end would have told it. The representation fails safe.
diff --git a/docs/decisions/0053-a-phrase-decision-records-the-inputs-it-considered.md b/docs/decisions/0053-a-phrase-decision-records-the-inputs-it-considered.md
index 4ed4a654..272890aa 100644
--- a/docs/decisions/0053-a-phrase-decision-records-the-inputs-it-considered.md
+++ b/docs/decisions/0053-a-phrase-decision-records-the-inputs-it-considered.md
@@ -1,8 +1,8 @@
# 0053 · A phrase decision records the inputs it considered
-- **Status**: Implemented 2026-09-23 (#878, migration 0072) · revised 2026-09-26 (migration 0092) · revised 2026-09-27 (#975, migration 0098)
+- **Status**: Implemented 2026-09-23 (#878, migration 0072) · revised 2026-09-26 (migration 0092) · revised 2026-09-27 (#975, migration 0098) · revised 2026-10-17 (#1295, migration 0114) · open: the backfill and printing the named period (#994 cut 1), time resolution's ends (cut 2)
- **Written**: 2026-09-23
-- **Related**: [0044](0044-the-ontology-is-a-view-over-what-documents-say.md) decision 3; [0051](0051-a-human-phrase-decision-carries-its-materialization-work.md); #807, #795, #801 (withdrawn), #773, #754, #966
+- **Related**: [0044](0044-the-ontology-is-a-view-over-what-documents-say.md) decision 3; [0051](0051-a-human-phrase-decision-carries-its-materialization-work.md); #807, #795, #801 (withdrawn), #773, #754, #966, #994
## Problem
@@ -265,6 +265,123 @@ With a scripted model:
typed row live;
- a person's binding is not asked, and the queue lists it until a person's decision writes the value.
+## Revision 2026-10-17 (an end that names a period)
+
+Since the revision above, "works for Meridian from 2024-03-01" with "left in 2024" is stored as
+2024-03-01 → 2025-01-01 at year precision. On the world axis that is right: the state holds through
+2024 and not in 2025. But "until 2025" closing the same row stores the same value, so the values
+cannot tell the two apart, and every reader prints the stored instant: chat and MCP print `→ 2025`,
+the graph page `~ 2025`, the history, document and pending rows `2025-01`, the export
+`validThrough "2025"^^xsd:gYear`, and the correction dialog pre-fills 2025 (#994).
+
+**The row says so.** The stored instant stays. `facts.end_after_period` (migration 0114) records
+that the end is the instant after the period the text named, and every reader is to print that
+period. A CHECK, `facts_end_after_period_needs_a_stated_end`, keeps it on a dated end the timeline
+did not draw, on a row with a start. On the Rust side that rule, with the state property the CHECK
+cannot see, is one function, `graph::period_mark_fits`, which both write entries, `rewrite_end_tx`,
+`mark_end_after_period` and the correction route consult. A start-less row never gets it in this
+cut: an end is moved only when the row's start lies in the period. Cut 2, when time resolution
+writes such ends itself, replaces the constraint by name without the start clause and drops that
+clause from the function.
+
+**Who sets it.** The two places the revision above moves an end:
+
+- `insert_fact_on`'s end-only closing path, when it closes an open row at the period's end. An end
+ the timeline drew at that instant is restated there as a written end, marked.
+- `refine_target`, when it takes an inherited end to the period's end.
+
+An open row closed by both "until 2025" and "left in 2024" is marked in either order. When the until
+comes first, or a date given in Review closed the row, "left in 2024" finds the row already closed at
+that instant and rewrites it with the mark, under a row lock (`temporal::mark_end_after_period`), so
+that order leaves one more superseded row. The rewrite reports a merge, not a new row: a row a person
+wrote or closed stays the person's and outlives the statement that marked it, and a marked row read
+again is left alone. Two exceptions: an end a person corrected (`corrected_ends` is `end` or `both`)
+stays as the person set it, and an end stated at another precision is not rewritten (a day-precision
+2025-01-01 is that day, not the end of 2024).
+
+**Who carries or drops it.** `rewrite_end_tx` writes it in the insert #1054 extended and forces it
+off wherever `period_mark_fits` says the row cannot carry it (a drawn or unknown end, a start-less
+row, a property not declared as a state), so the engine's redraws and `close_with_unknown_end` never
+carry it. `rehome_tx` copies it, an errata revision takes it with the old row's time, and the
+same-start branch passes it on when it closes the open row it finds or restates the end the timeline
+drew there. Both write entries drop it where the row cannot carry it: an event, an eternal property,
+no property, no start or no dated end, so a revision onto an event drops it. Merges move rows in
+place; an unmerge moves them in place, or through `rehome_tx` for rows rewritten after the merge;
+the mark stays either way. `close_superseded` writes none, so a close by date in Review, a
+conflict's `close_at` and a violation's `fact_closed` stay stated ends without it; those three
+routes stay outside both cuts.
+
+**Corrections.** `PATCH /api/v1/kbs/{id}/facts/{fact_id}` keeps reading and accepting the stored
+boundary, and the route never translates. The request takes an optional `end_after_period`, the one
+field whose absence means "keep":
+
+- Absent, an end equal to the stored one (the value after truncation, and the precision) keeps the
+ mark while the request keeps a start, so a client that sends back what it read changes nothing. A
+ request that clears the start keeps the instant and drops the mark, since a start-less row is not
+ marked. Any other end is taken as given.
+- An explicit `true` needs a property declared as a state, a start and a dated end; otherwise the
+ route answers 422 `bad_end_after_period`.
+- A change of the mark is a change of the end in `corrected_ends`: changing the shown 2024 to 2025
+ is a correction although the stored instant stays.
+- `interval_inverted` compares both ends after truncation to their precisions. An hour end of 08:30
+ is 08:00 and is refused against a start of 08:10 at minute precision; it used to be stored
+ inverted.
+
+Entity detail returns the mark beside `valid_to`, which stays the stored boundary. The dialog, still
+to be built, shows the named period and always sends the mark. An untouched end goes back as
+stored, and without the mark when the start is cleared. A start moved to or past an untouched end
+is refused as today, now with English and Chinese text. An edited end is translated with the write
+path's rule.
+
+**One printing rule, still to be built.** A marked end prints one unit before the stored instant,
+at its own precision, whatever the precision: 2025-01-01 at year precision prints 2024, 2024-04-01
+at month precision 2024-03. An unmarked end prints as today. The readers: `time_text::span` for
+chat and MCP, an `end_after_period` field beside `valid_to` in MCP's structured results,
+`fmtInterval` and the names list, the history, document, pending and review rows, and the export.
+
+**Export.** This is a deliberate change at the compatibility boundary `web/src/docs/mcp.md`
+describes: for a marked row, the old `validThrough` named the next period. `validThrough` itself
+changes, rather than a separate term carrying the named period, as published on #994 on 2026-10-15:
+`validThrough "2024"^^xsd:gYear` with `utopia:endAfterPeriod true`. The stored instant can be
+recovered: when `endAfterPeriod` is true, it is the end of `validThrough`'s period (`bucket_end`),
+with `validThroughPrecision` for an end below a day.
+
+**Rows written before 0114** stay unmarked until the backfill that follows.
+
+**Limits.**
+
+- Endings that arrive before the start ("until 2025", then "left in 2024", then "works for … from
+ 2024-03-01") leave the row unmarked on write: the start-less row in between cannot carry the mark,
+ and the refinement copies its end without it. Cut 2's start-less marked rows revisit it.
+- "Left in 2024" and "left in 2025" on a row from 2024-03-01 both close it at 2025-01-01, and in
+ either order the row ends up marked: the mark settles the disagreement as 2024.
+
+Not doing:
+
+- **Cut 2**: time resolution storing a named period's end as the instant after its last unit, a
+ marked end read backwards by `insert_fact_on`, start-less marked rows, `combine`.
+- **The three closes by date** above: they write the date given, and an end before the start among
+ them is #1294.
+- **The merge's duplicate removal**, which compares neither ends nor marks; it predates this.
+- **History rows**: an invalidated row keeps the stored instant.
+- **MCP `names[]`**, which carries no precisions, so a mark alone would mean nothing there.
+
+Regression coverage, with a real PostgreSQL:
+
+- "Left in 2024" marks the row it closes from 2024-03-01 in both statement orders, the closing path
+ and the refinement.
+- A person's row closed by "until 2025" is marked when a document says "left in 2024", stays the
+ person's, and is left alone when a second document says it again; one whose start a person moved
+ within 2024 is marked too. A day-precision end and an end a person corrected are not rewritten. An
+ end the timeline drew becomes a written, marked end.
+- A rehome carries the mark, and so does an errata revision onto a row with the same start, open
+ (which it closes) or with a drawn end (which it restates); a revision onto an event drops it.
+- Entity detail returns the mark. The route keeps it for a start-only change; drops it for a new
+ end, or a new precision, sent without it and for a cleared start, keeping the instant; writes an
+ explicit `false` or `true`, a change of the mark alone counting as a change of the end; refuses an
+ explicit `true` without a start, a dated end or a state property; and refuses an interval
+ inverted only after truncation.
+
## Status history
The status line as it stood on 2026-09-27, before status lines were cut to one line:
diff --git a/docs/design/ledger.md b/docs/design/ledger.md
index 1da7df67..5ba4e7f5 100644
--- a/docs/design/ledger.md
+++ b/docs/design/ledger.md
@@ -8,7 +8,7 @@ in [time](time.md), derived facts in [rules](rules.md).
**A fact is a row that is never updated in place.** `facts (kb, subject_id, predicate_id NULL,
object_id | object_value, confidence, valid_*, *_precision, attested_from, attested_to, recorded_at,
-invalidated_at, supersedes, end_derived, layer, phrase)`. A correction inserts a new row that
+invalidated_at, supersedes, end_derived, end_after_period, layer, phrase)`. A correction inserts a new row that
`supersedes` the old one and invalidates it; a retraction invalidates; `entity_history` reads the
chain back as "recorded as X, refined to Y, by whom" [0001, 0003]. Confirming a mapping by
`UPDATE ... confidence = 1.0` was the one violation and is why mappings left the ledger [0011].
diff --git a/docs/design/time.md b/docs/design/time.md
index 0369d15a..f4f3724a 100644
--- a/docs/design/time.md
+++ b/docs/design/time.md
@@ -67,7 +67,10 @@ graph with its date. An end names its bucket: an open row that starts inside it
bucket's end ("left in 2024" closes a row from March 2024 at 2025-01-01), a row that starts before it
closes at its start. Materialized state rows with equal ends written before this are retired and
computed again when one of their source statements has those ends; a row a person set to a single
-date is kept [0053 revision, #966]. The `[event]` / `[eternal]` prompt marks of 0031 d4 and the zoned-time sentence of 0024
+date is kept [0053 revision, #966]. An end moved to its bucket's end is marked `end_after_period`,
+by the closing path and the refinement alike, and an open row closed by both "until 2025" and "left
+in 2024" is marked in either order; a correction keeps the mark while the end stays as stored and a
+start remains [0053 revision 2026-10-17, #994]. The `[event]` / `[eternal]` prompt marks of 0031 d4 and the zoned-time sentence of 0024
belonged to the typed contract and are gone with it; the write rule in the store is what remains.
**Derived rows** store the interval as read: the intersection of premise read intervals, an anchored
@@ -116,6 +119,8 @@ only when that date came from the content or a dating source, else null [#731].
bench no lower than 17 of 18 on both runs [0045].
- The `as_of` control on the graph page; the anchor shown beside a blank start; an event drawn as a
point; retrieval that takes `at` [0019, 0022, 0031].
+- Every reader prints a marked end as the period it names: chat and MCP, the review lines, the
+ export, the web views and the time dialog (#994 cut 1) [0053 revision 2026-10-17].
## Open questions
diff --git a/migrations/0114_an_end_that_names_a_period_says_so.sql b/migrations/0114_an_end_that_names_a_period_says_so.sql
new file mode 100644
index 00000000..ec5435b4
--- /dev/null
+++ b/migrations/0114_an_end_that_names_a_period_says_so.sql
@@ -0,0 +1,19 @@
+-- 终点说的是它之前那个时段时,行上记一笔(#994,0053 的修订)。
+--
+-- 「works for Meridian from 2024-03-01」再加「left in 2024」,#975 之后存成 2024-03-01 →
+-- 2025-01-01(年精度):世界轴上这是对的,2024 年全年在职,2025 年不在。可「until 2025」
+-- 关上同一行,存的也是 2025-01-01(年精度)。只看值分不出原文说的是 2024 还是 2025,读者
+-- 只能按存的时刻印,印出来是下一个时段。
+--
+-- `end_after_period`:终点是原文所说时段之后的那一刻。存的时刻不变,世界轴照旧读它,读者
+-- 据此印原文说的时段。写入路径在 #975 挪终点的两处设它(收尾、精化);一条开着的行被
+-- 「until 2025」与「left in 2024」两句关上,哪一句先到都设;改写、搬主宾、勘误改出来的行
+-- 带着它;时间线画的终点、不知哪天的终点从不带。约束要求有起点:终点只在起点落在那个时段
+-- 里时才被挪到时段的尽头,没有起点的行第一刀不标。第二刀让时间解析自己写这种终点时,按
+-- 名字换掉这条约束,去掉起点那一条(Rust 一侧同一句话在 `graph::period_mark_fits`)。
+--
+-- 约束先删再加,整份文件可以重放。
+ALTER TABLE facts ADD COLUMN IF NOT EXISTS end_after_period BOOLEAN NOT NULL DEFAULT FALSE;
+ALTER TABLE facts DROP CONSTRAINT IF EXISTS facts_end_after_period_needs_a_stated_end;
+ALTER TABLE facts ADD CONSTRAINT facts_end_after_period_needs_a_stated_end
+ CHECK (NOT end_after_period OR (valid_from IS NOT NULL AND valid_to IS NOT NULL AND NOT end_derived));