Skip to content

World's first open-source enterprise world model.

fix(store): degenerate record-axis predicates to the single-sided for "now" (#847)

plpycoinae69830c514e
diff --git a/crates/utopia-store/src/documents.rs b/crates/utopia-store/src/documents.rs
index c315c6fa..16858519 100644
--- a/crates/utopia-store/src/documents.rs
+++ b/crates/utopia-store/src/documents.rs
@@ -1644,7 +1644,7 @@ pub async fn vector_search(
LIMIT $3
)
SELECT id FROM nearest ORDER BY {resort}",
- live = crate::record_axis::chunk_live_at("c", 4),
+ live = crate::record_axis::chunk_live_at("c", as_of.map(|_| 4)),
same_dims = crate::vector_index::same_dims("c.embedding", dims),
distance = crate::vector_index::distance("c.embedding", 2, dims),
resort = crate::vector_index::RESORT,
@@ -1674,8 +1674,8 @@ pub async fn chunks_by_ids(
FROM chunks c JOIN documents d ON d.id = c.document_id
WHERE c.kb_id = $1 AND c.id = ANY($2)
AND {live} AND {doc_live}",
- live = crate::record_axis::chunk_live_at("c", 3),
- doc_live = crate::record_axis::document_live_at("d", 3),
+ live = crate::record_axis::chunk_live_at("c", as_of.map(|_| 3)),
+ doc_live = crate::record_axis::document_live_at("d", as_of.map(|_| 3)),
))
.bind(kb_id)
.bind(ids)
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index 5e80bbe3..aefc6a18 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -1127,10 +1127,7 @@ pub async fn add_evidence_located(
/// `owner`:**只在真的传了时刻时**才绑(#336)。`fact_owner_at` 包住列之后
/// `facts` 上按主宾的索引就用不上了,而「现在」是每次画图都要走的那条路
fn node_sql(as_of: Option<usize>, owner: Option<usize>) -> String {
- let held = match as_of {
- Some(param) => crate::record_axis::facts_held_at("f", param),
- None => "f.invalidated_at IS NULL".to_string(),
- };
+ let held = crate::record_axis::facts_held_at("f", as_of);
// 主宾也跟着倒:三月被合并掉的实体,在二月身上还挂着它自己的那些事实(#336)
let subject = crate::record_axis::owner_at("f", "subject_id", owner, false);
let object = crate::record_axis::owner_at("f", "object_id", owner, true);
@@ -1169,8 +1166,8 @@ pub async fn overview(
) -> AppResult<(Vec<GraphNode>, Vec<GraphEdge>, i64, i64)> {
let nodes: Vec<GraphNode> = sqlx::query_as(&format!(
"{} WHERE e.kb_id = $1 AND {visible} ORDER BY degree DESC, e.created_at LIMIT $2",
- node_sql(Some(3), as_of.map(|_| 3)),
- visible = crate::record_axis::entity_visible_at("e", 3),
+ node_sql(as_of.map(|_| 3), as_of.map(|_| 3)),
+ visible = crate::record_axis::entity_visible_at("e", as_of.map(|_| 3)),
))
.bind(kb_id)
.bind(limit)
@@ -1190,7 +1187,7 @@ pub async fn overview(
// 三月并掉的那个,在二月既该出现在画布上,也该数进这个总数里
let total_nodes: i64 = sqlx::query_scalar(&format!(
"SELECT count(*) FROM entities e WHERE e.kb_id = $1 AND {visible}",
- visible = crate::record_axis::entity_visible_at("e", 2),
+ visible = crate::record_axis::entity_visible_at("e", as_of.map(|_| 2)),
))
.bind(kb_id)
.bind(as_of)
@@ -1204,8 +1201,8 @@ pub async fn overview(
WHERE f.kb_id = $1 AND {facts_held} AND f.object_id IS NOT NULL)
+ (SELECT count(*) FROM derived_facts d
WHERE d.kb_id = $1 AND {derived_held} AND d.object_id IS NOT NULL)",
- facts_held = crate::record_axis::facts_held_at("f", 2),
- derived_held = crate::record_axis::derived_held_at("d", 2),
+ facts_held = crate::record_axis::facts_held_at("f", as_of.map(|_| 2)),
+ derived_held = crate::record_axis::derived_held_at("d", as_of.map(|_| 2)),
))
.bind(kb_id)
.bind(as_of)
@@ -1328,10 +1325,10 @@ async fn edges_among(
),
holds_from = crate::world_axis::facts_holds_from("f"),
holds_to = crate::world_axis::facts_holds_to("f"),
- facts_held = crate::record_axis::facts_held_at("f", 4),
- derived_held = crate::record_axis::derived_held_at("d", 4),
- violation_open = crate::record_axis::violation_open_at("v", 4),
- conflict_open = crate::record_axis::conflict_open_at("c", 4),
+ facts_held = crate::record_axis::facts_held_at("f", as_of.map(|_| 4)),
+ derived_held = crate::record_axis::derived_held_at("d", as_of.map(|_| 4)),
+ violation_open = crate::record_axis::violation_open_at("v", as_of.map(|_| 4)),
+ conflict_open = crate::record_axis::conflict_open_at("c", as_of.map(|_| 4)),
// 派生边不跟着倒:它们由引擎按当时的断言推出,主宾从来没被合并改写过
subject = crate::record_axis::owner_at("f", "subject_id", as_of.map(|_| 4), false),
object = crate::record_axis::owner_at("f", "object_id", as_of.map(|_| 4), true),
@@ -1378,7 +1375,7 @@ pub async fn neighborhood(
"SELECT {subject}, {object} FROM facts f
WHERE f.kb_id = $1 AND {facts_held} AND f.object_id IS NOT NULL
AND ({subject} = ANY($2) OR {object} = ANY($2))",
- facts_held = crate::record_axis::facts_held_at("f", 3),
+ facts_held = crate::record_axis::facts_held_at("f", as_of.map(|_| 3)),
subject = crate::record_axis::owner_at("f", "subject_id", as_of.map(|_| 3), false),
object = crate::record_axis::owner_at("f", "object_id", as_of.map(|_| 3), true),
))
@@ -1405,8 +1402,8 @@ pub async fn neighborhood(
let ids: Vec<Uuid> = seen.into_iter().collect();
let nodes: Vec<GraphNode> = sqlx::query_as(&format!(
"{} WHERE e.kb_id = $1 AND e.id = ANY($2) AND {visible}",
- node_sql(Some(3), as_of.map(|_| 3)),
- visible = crate::record_axis::entity_visible_at("e", 3),
+ node_sql(as_of.map(|_| 3), as_of.map(|_| 3)),
+ visible = crate::record_axis::entity_visible_at("e", as_of.map(|_| 3)),
))
.bind(kb_id)
.bind(&ids)
@@ -1456,10 +1453,7 @@ pub async fn search_entities(
let pattern = format!("%{}%", text.trim());
let named = crate::names::has_name_like("e", 2);
// 不回放时 SQL 里没有时刻参数,与从前逐字相同;回放时才多绑一个
- let visible = |param: usize| match as_of {
- Some(_) => crate::record_axis::entity_visible_at("e", param),
- None => "e.merged_into IS NULL".to_string(),
- };
+ let visible = |param: usize| crate::record_axis::entity_visible_at("e", as_of.map(|_| param));
let rewind = as_of.map(|_| 5);
let sql = format!(
"{} WHERE e.kb_id = $1 AND {visible}
@@ -1503,7 +1497,7 @@ pub async fn entity_detail(
) -> AppResult<(GraphNode, Vec<EntityFact>)> {
let node: GraphNode = sqlx::query_as(&format!(
"{} WHERE e.kb_id = $1 AND e.id = $2",
- node_sql(Some(3), as_of.map(|_| 3))
+ node_sql(as_of.map(|_| 3), as_of.map(|_| 3))
))
.bind(kb_id)
.bind(entity_id)
@@ -1566,15 +1560,15 @@ pub async fn entity_detail(
not_name = crate::names::not_a_name("f"),
said_as = said_as("f"),
represented = represented_by_typed("f"),
- facts_held = crate::record_axis::facts_held_at("f", 3),
+ facts_held = crate::record_axis::facts_held_at("f", as_of.map(|_| 3)),
facts_hold = crate::world_axis::facts_hold_at("f", 4),
holds_from = crate::world_axis::facts_holds_from("f"),
holds_to = crate::world_axis::facts_holds_to("f"),
subject = crate::record_axis::owner_at("f", "subject_id", as_of.map(|_| 3), false),
object = crate::record_axis::owner_at("f", "object_id", as_of.map(|_| 3), true),
- chunk_live = crate::record_axis::chunk_live_at("c", 3),
- violation_open = crate::record_axis::violation_open_at("v", 3),
- conflict_open = crate::record_axis::conflict_open_at("c", 3),
+ chunk_live = crate::record_axis::chunk_live_at("c", as_of.map(|_| 3)),
+ violation_open = crate::record_axis::violation_open_at("v", as_of.map(|_| 3)),
+ conflict_open = crate::record_axis::conflict_open_at("c", as_of.map(|_| 3)),
))
.bind(kb_id)
.bind(entity_id)
@@ -1716,10 +1710,7 @@ pub async fn same_name_peers(
entity_id: Uuid,
as_of: Option<chrono::DateTime<chrono::Utc>>,
) -> AppResult<Vec<GraphNode>> {
- let visible = match as_of {
- Some(_) => crate::record_axis::entity_visible_at("e", 3),
- None => "e.merged_into IS NULL".to_string(),
- };
+ let visible = crate::record_axis::entity_visible_at("e", as_of.map(|_| 3));
sqlx::query_as(&format!(
"{} WHERE e.kb_id = $1 AND {visible} AND e.id <> $2
AND lower(e.canonical_name) = (SELECT lower(canonical_name) FROM entities WHERE id = $2)
diff --git a/crates/utopia-store/src/names.rs b/crates/utopia-store/src/names.rs
index eac70aba..1ba7afcf 100644
--- a/crates/utopia-store/src/names.rs
+++ b/crates/utopia-store/src/names.rs
@@ -168,7 +168,7 @@ pub async fn for_entity(
AND {owner} = $2 AND {held}
ORDER BY canonical DESC, f.recorded_at",
owner = crate::record_axis::owner_at("f", "subject_id", as_of.map(|_| 3), false),
- held = crate::record_axis::facts_held_at("f", 3),
+ held = crate::record_axis::facts_held_at("f", as_of.map(|_| 3)),
))
.bind(kb_id)
.bind(entity_id)
diff --git a/crates/utopia-store/src/paths.rs b/crates/utopia-store/src/paths.rs
index 23294c05..e69baacb 100644
--- a/crates/utopia-store/src/paths.rs
+++ b/crates/utopia-store/src/paths.rs
@@ -328,7 +328,7 @@ async fn touching(
AND ({subject} = ANY($2) OR {object} = ANY($2))
AND {subject} <> {object}
AND {held} AND {hold}",
- held = record_axis::facts_held_at("f", 3),
+ held = record_axis::facts_held_at("f", as_of.map(|_| 3)),
hold = world_axis::facts_hold_at("f", 4),
))
.bind(kb_id)
@@ -360,7 +360,7 @@ async fn degrees(
AND ({subject} = n.id OR {object} = n.id)
AND {held} AND {hold}
GROUP BY n.id",
- held = record_axis::facts_held_at("f", 3),
+ held = record_axis::facts_held_at("f", as_of.map(|_| 3)),
hold = world_axis::facts_hold_at("f", 4),
))
.bind(kb_id)
diff --git a/crates/utopia-store/src/reasoning.rs b/crates/utopia-store/src/reasoning.rs
index 8c2b2e82..2fa23afd 100644
--- a/crates/utopia-store/src/reasoning.rs
+++ b/crates/utopia-store/src/reasoning.rs
@@ -2445,10 +2445,7 @@ pub async fn blocked_for_entity(
entity_id: Uuid,
as_of: Option<chrono::DateTime<chrono::Utc>>,
) -> AppResult<Vec<utopia_core::models::BlockedDerivation>> {
- let violation_open = match as_of {
- Some(_) => crate::record_axis::violation_open_at("v", 3),
- None => "v.status = 'open'".to_string(),
- };
+ let violation_open = crate::record_axis::violation_open_at("v", as_of.map(|_| 3));
Ok(sqlx::query_as(&format!(
"SELECT v.id AS violation_id,
(v.detail->>'subject_id')::uuid AS subject_id,
@@ -2613,7 +2610,7 @@ pub async fn derived_for_entity(
AND {derived_hold}
ORDER BY d.derived_at DESC",
derived_hold = crate::world_axis::derived_hold_at("d", 3),
- derived_held = crate::record_axis::derived_held_at("d", 4),
+ derived_held = crate::record_axis::derived_held_at("d", as_of.map(|_| 4)),
))
.bind(kb_id)
.bind(entity_id)
diff --git a/crates/utopia-store/src/record_axis.rs b/crates/utopia-store/src/record_axis.rs
index 98b72e1c..01387f07 100644
--- a/crates/utopia-store/src/record_axis.rs
+++ b/crates/utopia-store/src/record_axis.rs
@@ -11,27 +11,45 @@
//! **写路径不用**(0019):`confirm_fact` / `reject_fact`、采纳的撤销、去重查重
//! 都是对"当前那一行"的守卫——修正永远发生在现在,没有"以三月的身份改一行"这回事。
//!
-//! 参数绑 `Option<DateTime<Utc>>`:`NULL` 即"现在",谓词随之退化成
-//! `invalidated_at IS NULL`(不会有哪一行的作废时刻晚于此刻)。读路径因此
-//! 只写一条语句,而不是为回放和当下各写一条——两条就是下一次漏改的地方。
+//! 参数是 `Option<usize>`(绑定参数的序号):`None` 即"现在",谓词在 **SQL 文本上**
+//! 退化成 `invalidated_at IS NULL`——不只是语义等价,还要让优化器看见它,否则
+//! `IS NULL OR > now()` 的双支会让 `WHERE invalidated_at IS NULL` 的部分索引
+//! 整棵不可用(`held()` 的注释记着那次 61.6s 的教训)。读路径因此只写一处调用,
+//! 而不是为回放和当下各写一条——两条就是下一次漏改的地方。
+//!
+//! **退化成 `IS NULL` 押在一条写入不变量上**:任何行都不会携带未来的
+//! `invalidated_at` / `deleted_at` / `decided_at` / `resolved_at`——撤销永远盖
+//! `now()` 的戳。有它,`IS NULL` 才与 `IS NULL OR > now()` 同义;没有 CHECK
+//! 守着它,只有这段文字。它若破了(比如预填一个未来的作废时刻),`None` 路径
+//! 会把"尚未生效的作废"当成"从未作废"。退化前有一个调用点碰巧对这种违例稳健,
+//! 退化后四个谓词全都指望它——所以它必须写在这里,让下一个读者撞上。
/// 起止两列构成的记录轴区间:`since <= T < invalidated_at`。
-fn held(alias: &str, since: &str, param: usize) -> String {
- format!(
- "{alias}.{since} <= coalesce(${param}, now()) \
- AND ({alias}.invalidated_at IS NULL OR {alias}.invalidated_at > coalesce(${param}, now()))"
- )
+///
+/// **`None` 必须退化成单支谓词**,不能照旧输出 `IS NULL OR > now()` 的双支:
+/// 部分索引(`facts_live_subject_idx` 等,`WHERE invalidated_at IS NULL`)只在
+/// 查询谓词能推出 `invalidated_at IS NULL` 时可用,`OR > now()` 让优化器
+/// 证不出来——读路径退成逐实体全表扫(9311 实体 × 94683 事实的库上
+/// `graph/overview` 节点查询 61.6s,退化后 4.3s)。
+fn held(alias: &str, since: &str, as_of: Option<usize>) -> String {
+ match as_of {
+ None => format!("{alias}.invalidated_at IS NULL"),
+ Some(param) => format!(
+ "{alias}.{since} <= coalesce(${param}, now()) \
+ AND ({alias}.invalidated_at IS NULL OR {alias}.invalidated_at > coalesce(${param}, now()))"
+ ),
+ }
}
/// `facts`:断言在 T 时刻仍被我们持有。
-pub fn facts_held_at(alias: &str, param: usize) -> String {
- held(alias, "recorded_at", param)
+pub fn facts_held_at(alias: &str, as_of: Option<usize>) -> String {
+ held(alias, "recorded_at", as_of)
}
/// `derived_facts`:派生在 T 时刻已推出且未被推翻——回放的图上留着**当时**推出的边,
/// 而不是今天这套规则的结论。
-pub fn derived_held_at(alias: &str, param: usize) -> String {
- held(alias, "derived_at", param)
+pub fn derived_held_at(alias: &str, as_of: Option<usize>) -> String {
+ held(alias, "derived_at", as_of)
}
/// `axiom_violations`:违规在 T 时刻还开着。列名与上面两张表不同(`detected_at` /
@@ -39,62 +57,83 @@ pub fn derived_held_at(alias: &str, param: usize) -> String {
///
/// 已裁掉却没留 `decided_at` 的历史行按"当时就不开着"算:宁可少画一条幽灵边,
/// 也不要凭空给三月的图加一条今天才发现的矛盾。
-pub fn violation_open_at(alias: &str, param: usize) -> String {
- format!(
- "{alias}.detected_at <= coalesce(${param}, now()) \
- AND ({alias}.status = 'open' OR {alias}.decided_at > coalesce(${param}, now()))"
- )
+pub fn violation_open_at(alias: &str, as_of: Option<usize>) -> String {
+ match as_of {
+ None => format!("{alias}.status = 'open'"),
+ Some(param) => format!(
+ "{alias}.detected_at <= coalesce(${param}, now()) \
+ AND ({alias}.status = 'open' OR {alias}.decided_at > coalesce(${param}, now()))"
+ ),
+ }
}
/// `fact_conflicts`:时态冲突在 T 时刻还开着。
-pub fn conflict_open_at(alias: &str, param: usize) -> String {
- format!(
- "{alias}.created_at <= coalesce(${param}, now()) \
- AND ({alias}.status = 'open' OR {alias}.resolved_at > coalesce(${param}, now()))"
- )
+pub fn conflict_open_at(alias: &str, as_of: Option<usize>) -> String {
+ match as_of {
+ None => format!("{alias}.status = 'open'"),
+ Some(param) => format!(
+ "{alias}.created_at <= coalesce(${param}, now()) \
+ AND ({alias}.status = 'open' OR {alias}.resolved_at > coalesce(${param}, now()))"
+ ),
+ }
}
/// `documents`:文档在 T 时刻还在库里。删除留墓碑(#268),所以"删掉的文档"
/// 在删除之前的任何时刻都该照常出现——它的分块当时确实是可检索的。
-pub fn document_live_at(alias: &str, param: usize) -> String {
- format!(
- "{alias}.created_at <= coalesce(${param}, now()) \
- AND ({alias}.deleted_at IS NULL OR {alias}.deleted_at > coalesce(${param}, now()))"
- )
+pub fn document_live_at(alias: &str, as_of: Option<usize>) -> String {
+ match as_of {
+ None => format!("{alias}.deleted_at IS NULL"),
+ Some(param) => format!(
+ "{alias}.created_at <= coalesce(${param}, now()) \
+ AND ({alias}.deleted_at IS NULL OR {alias}.deleted_at > coalesce(${param}, now()))"
+ ),
+ }
}
/// `chunks`:分块在 T 时刻还是现行版本。证据是否"已消失"要按当时的版本判——
/// 今天被重解析顶掉的段落,在三月的图上仍然是活证据。
-pub fn chunk_live_at(alias: &str, param: usize) -> String {
- format!(
- "{alias}.created_at <= coalesce(${param}, now()) \
- AND ({alias}.superseded_at IS NULL OR {alias}.superseded_at > coalesce(${param}, now()))"
- )
+pub fn chunk_live_at(alias: &str, as_of: Option<usize>) -> String {
+ match as_of {
+ None => format!("{alias}.superseded_at IS NULL"),
+ Some(param) => format!(
+ "{alias}.created_at <= coalesce(${param}, now()) \
+ AND ({alias}.superseded_at IS NULL OR {alias}.superseded_at > coalesce(${param}, now()))"
+ ),
+ }
}
/// `entity_merges`:这次合并在 T 时刻**生效着**吗(0019 第二刀 / #336)。
///
/// 实体身上没有记录轴——`merged_into` 只说合并发生过,不说何时。时刻在这张表上,
/// 而它和别的表问的是同一个问题,所以列名不同、形状一样。
-pub fn merge_in_effect_at(alias: &str, param: usize) -> String {
- format!(
- "{alias}.created_at <= coalesce(${param}, now()) \
- AND ({alias}.reverted_at IS NULL OR {alias}.reverted_at > coalesce(${param}, now()))"
- )
+pub fn merge_in_effect_at(alias: &str, as_of: Option<usize>) -> String {
+ match as_of {
+ None => format!("{alias}.reverted_at IS NULL"),
+ Some(param) => format!(
+ "{alias}.created_at <= coalesce(${param}, now()) \
+ AND ({alias}.reverted_at IS NULL OR {alias}.reverted_at > coalesce(${param}, now()))"
+ ),
+ }
}
/// 实体在 T 时刻是不是一个独立的节点:那时已经存在,且没有被一次生效中的合并吞掉。
///
-/// **取代读路径上的 `merged_into IS NULL`。** 参数为 NULL 时两者等价(已撤销的合并
+/// **取代读路径上的 `merged_into IS NULL`。** 参数为 `None` 时两者等价(已撤销的合并
/// 此刻不生效,那个实体本来就该出现),但传了时刻之后,三月被并掉的实体在二月
/// 会重新长回来——那正是这一刀要的。
-pub fn entity_visible_at(alias: &str, param: usize) -> String {
- let merged = merge_in_effect_at("m", param);
- format!(
- "{alias}.created_at <= coalesce(${param}, now()) \
- AND NOT EXISTS (SELECT 1 FROM entity_merges m \
+pub fn entity_visible_at(alias: &str, as_of: Option<usize>) -> String {
+ let merged = merge_in_effect_at("m", as_of);
+ match as_of {
+ None => format!(
+ "NOT EXISTS (SELECT 1 FROM entity_merges m \
WHERE m.source_id = {alias}.id AND {merged})"
- )
+ ),
+ Some(param) => format!(
+ "{alias}.created_at <= coalesce(${param}, now()) \
+ AND NOT EXISTS (SELECT 1 FROM entity_merges m \
+ WHERE m.source_id = {alias}.id AND {merged})"
+ ),
+ }
}
/// 一条事实在 T 时刻的主语(`on_object = false`)或宾语。
@@ -109,3 +148,65 @@ pub fn owner_at(fact_alias: &str, column: &str, as_of: Option<usize>, on_object:
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// 「现在」那条路必须退化成单支。这不是风格问题:`IS NULL OR > now()` 让
+ /// 优化器证不出 `invalidated_at IS NULL`,`facts_live_subject_idx` 这类
+ /// 部分索引整棵不可用(实测双支走 `facts_live_time_idx` 每次扫 9,877 行,
+ /// 单支走 BitmapOr 扫 4 行)。性能这条性质在别处没有守卫,只有这里。
+ #[test]
+ fn a_predicate_for_now_has_no_second_branch() {
+ for (what, sql) in [
+ ("facts_held_at", facts_held_at("f", None)),
+ ("derived_held_at", derived_held_at("d", None)),
+ ("violation_open_at", violation_open_at("v", None)),
+ ("conflict_open_at", conflict_open_at("c", None)),
+ ("document_live_at", document_live_at("d", None)),
+ ("chunk_live_at", chunk_live_at("c", None)),
+ ("merge_in_effect_at", merge_in_effect_at("m", None)),
+ ] {
+ assert!(
+ !sql.contains(" OR "),
+ "{what} 在「现在」下还留着双支:{sql}"
+ );
+ assert!(
+ !sql.contains('$'),
+ "{what} 在「现在」下不该有绑定参数:{sql}"
+ );
+ assert!(
+ !sql.contains("now()"),
+ "{what} 在「现在」下不该比时刻:{sql}"
+ );
+ }
+ }
+
+ /// 回放那条路原样保留:带上时刻参数、两支都在。少了哪一支,三月的图上就会
+ /// 多出今天才作废的行,或者少掉当时还活着的行。
+ #[test]
+ fn a_predicate_for_a_moment_keeps_both_branches() {
+ let sql = facts_held_at("f", Some(3));
+ assert!(
+ sql.contains("f.recorded_at <= coalesce($3, now())"),
+ "{sql}"
+ );
+ assert!(
+ sql.contains("f.invalidated_at IS NULL OR f.invalidated_at > coalesce($3, now())"),
+ "{sql}"
+ );
+ }
+
+ /// `entity_visible_at(None)` 顶替读路径上的 `merged_into IS NULL`:它换成了
+ /// 对 `entity_merges` 的反连接(多一次 hash anti join),但不再按实体行判。
+ /// 传了时刻才额外要求「那时已经建出来了」。
+ #[test]
+ fn an_entity_is_visible_now_unless_a_live_merge_swallowed_it() {
+ let now = entity_visible_at("e", None);
+ assert!(now.starts_with("NOT EXISTS"), "{now}");
+ assert!(now.contains("m.reverted_at IS NULL"), "{now}");
+ assert!(!now.contains("e.created_at"), "{now}");
+ assert!(entity_visible_at("e", Some(2)).contains("e.created_at <= coalesce($2, now())"));
+ }
+}
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 501af9de..dfcfd1e6 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
@@ -474,7 +474,7 @@ async fn a_revert_does_not_rewrite_what_the_merge_window_held() -> anyhow::Resul
let pool = pool.clone();
let (kb, pred, lease) = (f.kb, f.deadline, f.lease);
async move {
- let held = utopia_store::record_axis::facts_held_at("f", 2);
+ let held = utopia_store::record_axis::facts_held_at("f", Some(2));
let owner = utopia_store::record_axis::owner_at("f", "subject_id", Some(2), false);
let rows: Vec<(String, Option<String>, bool)> = sqlx::query_as(&format!(
"SELECT object_value #>> '{{value}}', to_char(valid_to, 'YYYY-MM-DD'), {owner} = $3