World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Restore shared facts when either deleted source returns (#791)
Maya Kid9ec188a4f2b7
diff --git a/crates/utopia-store/src/documents.rs b/crates/utopia-store/src/documents.rsindex 9044f9fe..6aa35efb 100644--- a/crates/utopia-store/src/documents.rs+++ b/crates/utopia-store/src/documents.rs@@ -1078,7 +1078,8 @@ async fn lock_cited_timelines(/// 撤销一次删除:文档、这次打标的分块、这次作废的事实原路复活,形状照 `revert_merge`。///-/// 只救 `document_deletions` 名单上的——更早版本的旧分块、删除之前就作废的事实+/// 只救 `document_deletions` 名单上的(包括最后一个共同出处删除时的名单)——+/// 更早版本的旧分块、删除之前就作废的事实/// 都不在名单里。三条路都从这里走:人点撤销、同步撞见墓碑、同内容重传pub async fn restore(pool: &PgPool, kb_id: Uuid, id: Uuid) -> AppResult<Document> {let mut tx = pool.begin().await?;@@ -1135,17 +1136,40 @@ async fn restore_tx(.bind(&chunk_ids).execute(&mut **tx).await?;+ // 甲乙共同作证时,只有最后删除的乙会把事实记进作废名单。恢复甲也应救回它,+ // 但只认删除事务留下的作废时间;后来另行撤回的事实不能借旧名单复活。+ let shared: Vec<(Uuid, chrono::DateTime<chrono::Utc>)> = sqlx::query_as(+ "SELECT DISTINCT f.id, f.invalidated_at FROM facts f+ JOIN fact_evidence fe ON fe.fact_id = f.id+ JOIN chunks c ON c.id = fe.chunk_id+ WHERE f.kb_id = $1 AND c.document_id = $2 AND f.invalidated_at IS NOT NULL+ AND EXISTS (SELECT 1 FROM document_deletions dd+ JOIN documents d ON d.id = dd.document_id+ WHERE dd.kb_id = $1 AND dd.reverted_at IS NULL+ AND f.id = ANY(dd.invalidated_facts)+ AND f.invalidated_at = d.deleted_at)",+ )+ .bind(kb_id)+ .bind(id)+ .fetch_all(&mut **tx)+ .await?;+ let (shared_ids, shared_stamps): (Vec<_>, Vec<_>) = shared.into_iter().unzip();+ let restored: Vec<Uuid> = fact_ids.iter().chain(&shared_ids).copied().collect();// 复活的事实回到各自的时间线上;一直引用着这篇文档的事实,排序用的日期也回来了。- // 先锁、再复活、再重算(同删除)- let (cited, timelines) = lock_cited_timelines(tx, kb_id, id, &fact_ids).await?;+ // 先锁、再复活、再重算(同删除);等锁期间若又作废,不覆盖新的决定。+ let (cited, timelines) = lock_cited_timelines(tx, kb_id, id, &restored).await?;sqlx::query("UPDATE facts SET invalidated_at = NULL- WHERE id = ANY($1) AND invalidated_at IS NOT NULL",+ WHERE invalidated_at IS NOT NULL+ AND (id = ANY($1) OR (id, invalidated_at) IN+ (SELECT * FROM unnest($2::uuid[], $3::timestamptz[])))",).bind(&fact_ids)+ .bind(&shared_ids)+ .bind(&shared_stamps).execute(&mut **tx).await?;- let touched: Vec<Uuid> = cited.iter().chain(&fact_ids).copied().collect();+ let touched: Vec<Uuid> = cited.iter().chain(&restored).copied().collect();reattest_tx(tx, &touched).await?;crate::temporal::tidy_timelines_tx(tx, kb_id, &timelines).await?;sqlx::query("UPDATE document_deletions SET reverted_at = now() WHERE id = $1")diff --git a/crates/utopia-store/tests/a_deletion_is_an_event.rs b/crates/utopia-store/tests/a_deletion_is_an_event.rsindex 63d908bd..d5642576 100644--- a/crates/utopia-store/tests/a_deletion_is_an_event.rs+++ b/crates/utopia-store/tests/a_deletion_is_an_event.rs@@ -315,3 +315,61 @@ async fn a_deletion_is_an_event() -> anyhow::Result<()> {.await;run}++async fn shared_source_restore(purge_last: bool) -> anyhow::Result<()> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ let f = seed(&pool).await?;+ let result = async {+ let a = document(&pool, &f, "a.md", "restore-a").await?;+ let b = document(&pool, &f, "b.md", "restore-b").await?;+ let a1 = chunk(&pool, &f, a, 1).await?;+ let b1 = chunk(&pool, &f, b, 1).await?;+ let x = entity(&pool, &f, "X").await?;+ let y = entity(&pool, &f, "Y").await?;+ let shared = fact(&pool, &f, x, y, &[a1, b1]).await?;+ let z = entity(&pool, &f, "Z").await?;+ let only_b = fact(&pool, &f, x, z, &[b1]).await?;+ let already_gone = fact(&pool, &f, y, x, &[a1, b1]).await?;+ sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1")+ .bind(already_gone).execute(&pool).await?;+ assert_eq!(documents::delete(&pool, f.kb, a, None).await?.invalidated_facts, 0);+ assert_eq!(documents::delete(&pool, f.kb, b, None).await?.invalidated_facts, 2);+ assert!(!live(&pool, shared).await?);+ if purge_last {+ documents::purge(&pool, f.kb, b).await?;+ }+ documents::restore(&pool, f.kb, a).await?;+ let restored_shared = live(&pool, shared).await?;+ let restored_old = live(&pool, already_gone).await?;+ assert!(!live(&pool, only_b).await?, "b's exclusive fact stays retired");+ assert!(listed(&pool, &f, a).await?);+ assert!(!listed(&pool, &f, b).await?);+ assert!(chunk_live(&pool, a1).await?.0);+ assert!(!restored_old, "a fact retired before either deletion stays retired");+ anyhow::ensure!(restored_shared, "restoring the first source must recover the shared fact retired by deleting the second source");+ Ok::<_, anyhow::Error>(())+ }.await;+ sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")+ .bind(f.kb)+ .execute(&pool)+ .await?;+ sqlx::query("DELETE FROM organizations WHERE id = $1")+ .bind(f.org)+ .execute(&pool)+ .await?;+ result+}++#[tokio::test]+async fn restoring_either_source_recovers_a_shared_fact() -> anyhow::Result<()> {+ shared_source_restore(false).await+}++#[tokio::test]+async fn restoring_a_source_recovers_a_fact_after_the_last_source_was_purged() -> anyhow::Result<()>+{+ shared_source_restore(true).await+}