Skip to content

Commit 0707b3a

Browse files
gedaiuWaylandYang
andauthored
A fact is not stale while a new version of its document is still on its way to extraction, so a re-push no longer flags facts it has not yet had a chance to restate (#1489)
* A fact is not stale while a new version of its document is still on its way to extraction, so a re-push no longer flags facts it has not yet had a chance to restate Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com> * A replay reads the wait for a new version at its own moment, so a fact whose new version was unread then is not stale then Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com> * The wait for a new version is read only in the present and found through the cited chunk, so a forced re-extract cannot change what a replay shows and evidence without a document id still waits Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com> --------- Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com> Co-authored-by: WaylandYang <145302500+WaylandYang@users.noreply.github.com>
1 parent 40a35bd commit 0707b3a

6 files changed

Lines changed: 324 additions & 10 deletions

File tree

‎crates/utopia-store/src/graph.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,7 @@ pub async fn entity_detail(
18431843
AND NOT EXISTS (SELECT 1 FROM fact_evidence fe
18441844
JOIN chunks c ON c.id = fe.chunk_id
18451845
WHERE fe.fact_id = f.id AND {chunk_live})
1846+
AND NOT {awaits}
18461847
) AS stale,
18471848
(f.supersedes IS NOT NULL) AS corrected,
18481849
f.end_derived, f.end_after_period, {corrected_ends} AS corrected_ends,
@@ -1899,6 +1900,10 @@ pub async fn entity_detail(
18991900
holds_to = crate::world_axis::facts_holds_to("f"),
19001901
subject = crate::record_axis::owner_at("f", "subject_id", as_of.map(|_| 3), false),
19011902
object = crate::record_axis::owner_at("f", "object_id", as_of.map(|_| 3), true),
1903+
awaits = match as_of {
1904+
Some(_) => "false",
1905+
None => crate::review::awaits_a_new_version(),
1906+
},
19021907
chunk_live = crate::record_axis::chunk_live_at("c", as_of.map(|_| 3)),
19031908
violation_open = crate::record_axis::violation_open_at("v", as_of.map(|_| 3)),
19041909
conflict_open = crate::record_axis::conflict_open_at("c", as_of.map(|_| 3)),
@@ -2100,14 +2105,14 @@ pub async fn low_confidence_facts(
21002105
///
21012106
/// **「现行」这一条不能少。** 闭合是作废+改写,修正行带着原证据(`temporal::close_superseded`),
21022107
/// 只看证据存活性的话,刚闭合的行立刻回到这一档——那个出路等于不存在。
2103-
/// WHERE 与 `review::UNCONFIRMED_FACT` 同一套(0022:「结束不知哪天」不是开放)。
2108+
/// WHERE 与 `review::unconfirmed_fact` 同一套(0022:「结束不知哪天」不是开放)。
21042109
pub async fn stale_facts(
21052110
pool: &PgPool,
21062111
kb_id: Uuid,
21072112
limit: i64,
21082113
offset: i64,
21092114
) -> AppResult<Vec<FactReviewItem>> {
2110-
let rows: Vec<FactReviewItem> = sqlx::query_as(
2115+
let rows: Vec<FactReviewItem> = sqlx::query_as(&format!(
21112116
"SELECT f.id, s.canonical_name AS subject_name, COALESCE(r.label, fact_surface_predicate(f.id)) AS predicate_label,
21122117
COALESCE(o.canonical_name, f.object_value->>'summary') AS object_name,
21132118
f.valid_from, f.valid_to, f.confidence,
@@ -2124,9 +2129,11 @@ pub async fn stale_facts(
21242129
AND NOT EXISTS (SELECT 1 FROM fact_evidence fe
21252130
JOIN chunks c ON c.id = fe.chunk_id
21262131
WHERE fe.fact_id = f.id AND c.superseded_at IS NULL)
2132+
AND NOT {awaits}
21272133
ORDER BY f.recorded_at DESC, f.id DESC
21282134
LIMIT $2 OFFSET $3",
2129-
)
2135+
awaits = crate::review::awaits_a_new_version(),
2136+
))
21302137
.bind(kb_id)
21312138
.bind(limit)
21322139
.bind(offset)

‎crates/utopia-store/src/review.rs‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,45 @@ use uuid::Uuid;
1919
/// 迟早分叉成「徽标说 12 条,点进去 9 条」。
2020
pub const LOW_CONFIDENCE_BELOW: f32 = 0.75;
2121

22+
/// 事实引用的某个文档有一块现行分块还没人读:新版本写下了、还没抽取。它开口之前,
23+
/// 它可能没再提的东西还谈不上「没了」。只等还会被读的版本:还在摄入,或排着队、正在抽取。
24+
/// 抽取关着、跳过或失败的版本没人会读,等它就是永远等。别名固定用 `f`。
25+
///
26+
/// 文档从证据的分块找(`fe.chunk_id`),不从 `fe.document_id`:后者可以为空。
27+
///
28+
/// **只说当下。** `extracted_at` 没有历史:强制重抽把它清空再写成现在,一个分块
29+
/// 「T 时刻读过没有」事后无从得知。所以回放(0019)不问这一句,只看证据分块那时
30+
/// 是否已被取代,那是重抽动不了的。
31+
pub fn awaits_a_new_version() -> &'static str {
32+
"EXISTS (SELECT 1 FROM fact_evidence fe
33+
JOIN chunks cited ON cited.id = fe.chunk_id
34+
JOIN documents d ON d.id = cited.document_id
35+
JOIN chunks c ON c.document_id = d.id
36+
WHERE fe.fact_id = f.id AND c.superseded_at IS NULL AND c.extracted_at IS NULL
37+
AND (d.graph_status IN ('queued', 'extracting')
38+
OR d.status IN ('pending', 'parsing', 'indexing', 'embedding')))"
39+
}
40+
2241
/// 「待确认」的判据,写成 SQL 片段,`counts` 与总览(`review_summary`)共用:
2342
/// 有证据、但证据所在的分块全被新版本取代了,**而且这条事实至今仍成立**。别名固定用 `f`。
2443
///
2544
/// 上界那一句是「闭合」这个出路的前提:闭合是作废+改写,修正行把证据一起复制下来
2645
/// (`temporal::close_superseded`),少了它,刚闭合的行照样满足判据——队列永远清不掉,
2746
/// 再点一次只会撞上「区间已闭合」。判据与 0022 说同一句话:「结束不知哪天」不是开放。
28-
pub const UNCONFIRMED_FACT: &str = "f.valid_to IS NULL AND f.valid_to_precision IS NULL
47+
///
48+
/// 最后一句:新版本写下了、还没被读,就不算待确认(见 [`awaits_a_new_version`])。
49+
pub fn unconfirmed_fact() -> String {
50+
format!(
51+
"f.valid_to IS NULL AND f.valid_to_precision IS NULL
2952
AND EXISTS (SELECT 1 FROM fact_evidence fe WHERE fe.fact_id = f.id)
3053
AND NOT EXISTS (SELECT 1 FROM fact_evidence fe
3154
JOIN chunks c ON c.id = fe.chunk_id
3255
WHERE fe.fact_id = f.id
33-
AND c.superseded_at IS NULL)";
56+
AND c.superseded_at IS NULL)
57+
AND NOT {awaits}",
58+
awaits = awaits_a_new_version(),
59+
)
60+
}
3461

3562
pub async fn counts(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewCounts> {
3663
let sql = format!(
@@ -71,7 +98,7 @@ pub async fn counts(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewCounts> {
7198
(SELECT count(*) FROM (SELECT 1 FROM phrase_bindings b WHERE b.kb_id = $1 AND {waiting_phrase}
7299
UNION ALL SELECT 1 FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment,
73100
(SELECT count(*) FROM errata_actions WHERE kb_id = $1 AND status = 'held') AS errata",
74-
unconfirmed = UNCONFIRMED_FACT,
101+
unconfirmed = unconfirmed_fact(),
75102
waiting_phrase = crate::alignment_queue::WAITING_PHRASE,
76103
same = crate::resolution::TypeFilter::Same.clause(),
77104
conflict = crate::resolution::TypeFilter::Conflict.clause(),

‎crates/utopia-store/src/review_summary.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use utopia_core::models::{
1919
use utopia_core::AppResult;
2020
use uuid::Uuid;
2121

22-
use crate::review::{LOW_CONFIDENCE_BELOW, UNCONFIRMED_FACT};
22+
use crate::review::{unconfirmed_fact, LOW_CONFIDENCE_BELOW};
2323

2424
/// 「办过的」按天看多少天
2525
pub const DAILY_DAYS: i64 = 14;
@@ -107,7 +107,7 @@ async fn waiting(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewWaiting> {
107107
UNION ALL SELECT decided_at FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment_oldest,
108108
(SELECT count(*) FROM errata_actions WHERE kb_id = $1 AND status = 'held') AS errata,
109109
(SELECT min(created_at) FROM errata_actions WHERE kb_id = $1 AND status = 'held') AS errata_oldest",
110-
unconfirmed = UNCONFIRMED_FACT,
110+
unconfirmed = unconfirmed_fact(),
111111
waiting_phrase = crate::alignment_queue::WAITING_PHRASE,
112112
);
113113
let r: WaitingRow = sqlx::query_as(&sql)
@@ -333,7 +333,7 @@ async fn health(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewHealth> {
333333
WHERE kb_id = $1 AND status = 'open'
334334
) c ON c.fact_id = f.id
335335
WHERE f.kb_id = $1 AND f.invalidated_at IS NULL",
336-
unconfirmed = UNCONFIRMED_FACT,
336+
unconfirmed = unconfirmed_fact(),
337337
);
338338
let r: HealthRow = sqlx::query_as(&sql)
339339
.bind(kb_id)

0 commit comments

Comments
 (0)