@@ -157,15 +157,22 @@ async fn fact(pool: &PgPool, f: &Fx, s: Uuid, o: Uuid, evidence: &[Uuid]) -> any
157157 Ok ( id)
158158}
159159
160- async fn live ( pool : & PgPool , id : Uuid ) -> anyhow:: Result < bool > {
160+ async fn invalidated_at (
161+ pool : & PgPool ,
162+ id : Uuid ,
163+ ) -> anyhow:: Result < Option < chrono:: DateTime < chrono:: Utc > > > {
161164 Ok (
162- sqlx:: query_scalar ( "SELECT invalidated_at IS NULL FROM facts WHERE id = $1" )
165+ sqlx:: query_scalar ( "SELECT invalidated_at FROM facts WHERE id = $1" )
163166 . bind ( id)
164167 . fetch_one ( pool)
165168 . await ?,
166169 )
167170}
168171
172+ async fn live ( pool : & PgPool , id : Uuid ) -> anyhow:: Result < bool > {
173+ Ok ( invalidated_at ( pool, id) . await ?. is_none ( ) )
174+ }
175+
169176async fn chunk_live ( pool : & PgPool , id : Uuid ) -> anyhow:: Result < ( bool , String ) > {
170177 Ok (
171178 sqlx:: query_as ( "SELECT superseded_at IS NULL, text FROM chunks WHERE id = $1" )
@@ -373,3 +380,50 @@ async fn restoring_a_source_recovers_a_fact_after_the_last_source_was_purged() -
373380{
374381 shared_source_restore ( true ) . await
375382}
383+
384+ #[ tokio:: test]
385+ async fn restoring_the_last_deleted_source_preserves_a_later_rejection ( ) -> anyhow:: Result < ( ) > {
386+ let Some ( url) = utopia_store:: test_db:: url ( ) else {
387+ return Ok ( ( ) ) ;
388+ } ;
389+ let pool = PgPool :: connect ( & url) . await ?;
390+ let f = seed ( & pool) . await ?;
391+ let result = async {
392+ let a = document ( & pool, & f, "a.md" , "restore-a" ) . await ?;
393+ let b = document ( & pool, & f, "b.md" , "restore-b" ) . await ?;
394+ let a1 = chunk ( & pool, & f, a, 1 ) . await ?;
395+ let b1 = chunk ( & pool, & f, b, 1 ) . await ?;
396+ let x = entity ( & pool, & f, "X" ) . await ?;
397+ let y = entity ( & pool, & f, "Y" ) . await ?;
398+ let z = entity ( & pool, & f, "Z" ) . await ?;
399+ let shared = fact ( & pool, & f, x, y, & [ a1, b1] ) . await ?;
400+ let only_b = fact ( & pool, & f, x, z, & [ b1] ) . await ?;
401+
402+ documents:: delete ( & pool, f. kb , a, None ) . await ?;
403+ let deletion = documents:: delete ( & pool, f. kb , b, None ) . await ?;
404+ assert_eq ! ( deletion. invalidated_facts, 2 ) ;
405+ documents:: restore ( & pool, f. kb , a) . await ?;
406+ utopia_store:: graph:: reject_fact ( & pool, f. kb , shared) . await ?;
407+ let rejected_at = invalidated_at ( & pool, shared) . await ?. expect ( "fact rejected" ) ;
408+
409+ let restored = documents:: restore ( & pool, f. kb , b) . await ?;
410+ assert ! ( restored. deleted_at. is_none( ) ) ;
411+ assert ! ( chunk_live( & pool, b1) . await ?. 0 ) ;
412+ assert ! ( live( & pool, only_b) . await ?, "B's fact should be restored" ) ;
413+ anyhow:: ensure!(
414+ invalidated_at( & pool, shared) . await ? == Some ( rejected_at) ,
415+ "restoring B must preserve the later human rejection"
416+ ) ;
417+ Ok :: < _ , anyhow:: Error > ( ( ) )
418+ }
419+ . await ;
420+ sqlx:: query ( "DELETE FROM knowledge_bases WHERE id = $1" )
421+ . bind ( f. kb )
422+ . execute ( & pool)
423+ . await ?;
424+ sqlx:: query ( "DELETE FROM organizations WHERE id = $1" )
425+ . bind ( f. org )
426+ . execute ( & pool)
427+ . await ?;
428+ result
429+ }
0 commit comments