Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions crates/utopia-store/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,7 @@ pub async fn entity_detail(
AND NOT EXISTS (SELECT 1 FROM fact_evidence fe
JOIN chunks c ON c.id = fe.chunk_id
WHERE fe.fact_id = f.id AND {chunk_live})
AND NOT {awaits}
) AS stale,
(f.supersedes IS NOT NULL) AS corrected,
f.end_derived, f.end_after_period, {corrected_ends} AS corrected_ends,
Expand Down Expand Up @@ -1899,6 +1900,10 @@ pub async fn entity_detail(
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),
awaits = match as_of {
Some(_) => "false",
None => crate::review::awaits_a_new_version(),
},
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)),
Expand Down Expand Up @@ -2100,14 +2105,14 @@ pub async fn low_confidence_facts(
///
/// **「现行」这一条不能少。** 闭合是作废+改写,修正行带着原证据(`temporal::close_superseded`),
/// 只看证据存活性的话,刚闭合的行立刻回到这一档——那个出路等于不存在。
/// WHERE 与 `review::UNCONFIRMED_FACT` 同一套(0022:「结束不知哪天」不是开放)。
/// WHERE 与 `review::unconfirmed_fact` 同一套(0022:「结束不知哪天」不是开放)。
pub async fn stale_facts(
pool: &PgPool,
kb_id: Uuid,
limit: i64,
offset: i64,
) -> AppResult<Vec<FactReviewItem>> {
let rows: Vec<FactReviewItem> = sqlx::query_as(
let rows: Vec<FactReviewItem> = sqlx::query_as(&format!(
"SELECT f.id, s.canonical_name AS subject_name, COALESCE(r.label, fact_surface_predicate(f.id)) AS predicate_label,
COALESCE(o.canonical_name, f.object_value->>'summary') AS object_name,
f.valid_from, f.valid_to, f.confidence,
Expand All @@ -2124,9 +2129,11 @@ pub async fn stale_facts(
AND NOT EXISTS (SELECT 1 FROM fact_evidence fe
JOIN chunks c ON c.id = fe.chunk_id
WHERE fe.fact_id = f.id AND c.superseded_at IS NULL)
AND NOT {awaits}
ORDER BY f.recorded_at DESC, f.id DESC
LIMIT $2 OFFSET $3",
)
awaits = crate::review::awaits_a_new_version(),
))
.bind(kb_id)
.bind(limit)
.bind(offset)
Expand Down
33 changes: 30 additions & 3 deletions crates/utopia-store/src/review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,45 @@ use uuid::Uuid;
/// 迟早分叉成「徽标说 12 条,点进去 9 条」。
pub const LOW_CONFIDENCE_BELOW: f32 = 0.75;

/// 事实引用的某个文档有一块现行分块还没人读:新版本写下了、还没抽取。它开口之前,
/// 它可能没再提的东西还谈不上「没了」。只等还会被读的版本:还在摄入,或排着队、正在抽取。
/// 抽取关着、跳过或失败的版本没人会读,等它就是永远等。别名固定用 `f`。
///
/// 文档从证据的分块找(`fe.chunk_id`),不从 `fe.document_id`:后者可以为空。
///
/// **只说当下。** `extracted_at` 没有历史:强制重抽把它清空再写成现在,一个分块
/// 「T 时刻读过没有」事后无从得知。所以回放(0019)不问这一句,只看证据分块那时
/// 是否已被取代,那是重抽动不了的。
pub fn awaits_a_new_version() -> &'static str {
"EXISTS (SELECT 1 FROM fact_evidence fe
JOIN chunks cited ON cited.id = fe.chunk_id
JOIN documents d ON d.id = cited.document_id
JOIN chunks c ON c.document_id = d.id
WHERE fe.fact_id = f.id AND c.superseded_at IS NULL AND c.extracted_at IS NULL
AND (d.graph_status IN ('queued', 'extracting')
OR d.status IN ('pending', 'parsing', 'indexing', 'embedding')))"
}

/// 「待确认」的判据,写成 SQL 片段,`counts` 与总览(`review_summary`)共用:
/// 有证据、但证据所在的分块全被新版本取代了,**而且这条事实至今仍成立**。别名固定用 `f`。
///
/// 上界那一句是「闭合」这个出路的前提:闭合是作废+改写,修正行把证据一起复制下来
/// (`temporal::close_superseded`),少了它,刚闭合的行照样满足判据——队列永远清不掉,
/// 再点一次只会撞上「区间已闭合」。判据与 0022 说同一句话:「结束不知哪天」不是开放。
pub const UNCONFIRMED_FACT: &str = "f.valid_to IS NULL AND f.valid_to_precision IS NULL
///
/// 最后一句:新版本写下了、还没被读,就不算待确认(见 [`awaits_a_new_version`])。
pub fn unconfirmed_fact() -> String {
format!(
"f.valid_to IS NULL AND f.valid_to_precision IS NULL
AND EXISTS (SELECT 1 FROM fact_evidence fe WHERE fe.fact_id = f.id)
AND NOT EXISTS (SELECT 1 FROM fact_evidence fe
JOIN chunks c ON c.id = fe.chunk_id
WHERE fe.fact_id = f.id
AND c.superseded_at IS NULL)";
AND c.superseded_at IS NULL)
AND NOT {awaits}",
awaits = awaits_a_new_version(),
)
}

pub async fn counts(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewCounts> {
let sql = format!(
Expand Down Expand Up @@ -71,7 +98,7 @@ pub async fn counts(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewCounts> {
(SELECT count(*) FROM (SELECT 1 FROM phrase_bindings b WHERE b.kb_id = $1 AND {waiting_phrase}
UNION ALL SELECT 1 FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment,
(SELECT count(*) FROM errata_actions WHERE kb_id = $1 AND status = 'held') AS errata",
unconfirmed = UNCONFIRMED_FACT,
unconfirmed = unconfirmed_fact(),
waiting_phrase = crate::alignment_queue::WAITING_PHRASE,
same = crate::resolution::TypeFilter::Same.clause(),
conflict = crate::resolution::TypeFilter::Conflict.clause(),
Expand Down
6 changes: 3 additions & 3 deletions crates/utopia-store/src/review_summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use utopia_core::models::{
use utopia_core::AppResult;
use uuid::Uuid;

use crate::review::{LOW_CONFIDENCE_BELOW, UNCONFIRMED_FACT};
use crate::review::{unconfirmed_fact, LOW_CONFIDENCE_BELOW};

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