| 1 | //! R2:一条派生的证明要能一路读到原句(`docs/decisions/0002`)。 |
| 2 | //! |
| 3 | //! 前提一律是断言(`fact_derivations` 不记派生),所以证明是一条链: |
| 4 | //! 派生 → 按 `seq` 排好的断言 → 每条断言的证据 → chunk。这里守三件事: |
| 5 | //! |
| 6 | //! 1. **顺序对**。`A part_of B`、`B part_of C` 推出 `A part_of C`,证明第一步是 A→B。 |
| 7 | //! 2. **叶子是原句**。每一步带着它的证据,引句就是当初抽出它的那句话。 |
| 8 | //! 3. **撤了的前提照样列出并打标记**。派生随之失效,`proof` 仍能回看当时靠的是什么。 |
| 9 | //! |
| 10 | //! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。 |
| 11 | |
| 12 | use sqlx::PgPool; |
| 13 | use utopia_store::reasoning; |
| 14 | use uuid::Uuid; |
| 15 | |
| 16 | struct Fixture { |
| 17 | org: Uuid, |
| 18 | kb: Uuid, |
| 19 | part_of: Uuid, |
| 20 | a: Uuid, |
| 21 | b: Uuid, |
| 22 | c: Uuid, |
| 23 | doc: Uuid, |
| 24 | chunk_ab: Uuid, |
| 25 | chunk_bc: Uuid, |
| 26 | } |
| 27 | |
| 28 | async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> { |
| 29 | let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()); |
| 30 | let etype = Uuid::now_v7(); |
| 31 | let part_of = Uuid::now_v7(); |
| 32 | let (a, b, c) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()); |
| 33 | let (src, doc, chunk_ab, chunk_bc) = ( |
| 34 | Uuid::now_v7(), |
| 35 | Uuid::now_v7(), |
| 36 | Uuid::now_v7(), |
| 37 | Uuid::now_v7(), |
| 38 | ); |
| 39 | |
| 40 | sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'proof-test')") |
| 41 | .bind(org) |
| 42 | .execute(pool) |
| 43 | .await?; |
| 44 | sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'proof-test')") |
| 45 | .bind(ws) |
| 46 | .bind(org) |
| 47 | .execute(pool) |
| 48 | .await?; |
| 49 | sqlx::query( |
| 50 | "INSERT INTO knowledge_bases (id, workspace_id, name, materialize_inferences) |
| 51 | VALUES ($1, $2, 'proof-test', TRUE)", |
| 52 | ) |
| 53 | .bind(kb) |
| 54 | .bind(ws) |
| 55 | .execute(pool) |
| 56 | .await?; |
| 57 | sqlx::query( |
| 58 | "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'thing', 'Thing')", |
| 59 | ) |
| 60 | .bind(etype) |
| 61 | .bind(kb) |
| 62 | .execute(pool) |
| 63 | .await?; |
| 64 | sqlx::query( |
| 65 | "INSERT INTO relation_types (id, kb_id, key, label, is_transitive) |
| 66 | VALUES ($1, $2, 'part_of', 'part of', TRUE)", |
| 67 | ) |
| 68 | .bind(part_of) |
| 69 | .bind(kb) |
| 70 | .execute(pool) |
| 71 | .await?; |
| 72 | for (id, name) in [(a, "FarmBeats"), (b, "Azure"), (c, "Microsoft")] { |
| 73 | sqlx::query( |
| 74 | "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)", |
| 75 | ) |
| 76 | .bind(id) |
| 77 | .bind(kb) |
| 78 | .bind(etype) |
| 79 | .bind(name) |
| 80 | .execute(pool) |
| 81 | .await?; |
| 82 | } |
| 83 | sqlx::query("INSERT INTO sources (id, kb_id, name) VALUES ($1, $2, 'proof-test')") |
| 84 | .bind(src) |
| 85 | .bind(kb) |
| 86 | .execute(pool) |
| 87 | .await?; |
| 88 | sqlx::query( |
| 89 | "INSERT INTO documents (id, kb_id, source_id, filename, sha256, status) |
| 90 | VALUES ($1, $2, $3, 'press.md', 'proof', 'ready')", |
| 91 | ) |
| 92 | .bind(doc) |
| 93 | .bind(kb) |
| 94 | .bind(src) |
| 95 | .execute(pool) |
| 96 | .await?; |
| 97 | for (id, seq, text) in [ |
| 98 | (chunk_ab, 0i32, "FarmBeats is part of Azure."), |
| 99 | (chunk_bc, 1i32, "Azure is part of Microsoft."), |
| 100 | ] { |
| 101 | sqlx::query( |
| 102 | "INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1, $2, $3, $4, $5)", |
| 103 | ) |
| 104 | .bind(id) |
| 105 | .bind(kb) |
| 106 | .bind(doc) |
| 107 | .bind(seq) |
| 108 | .bind(text) |
| 109 | .execute(pool) |
| 110 | .await?; |
| 111 | } |
| 112 | Ok(Fixture { |
| 113 | org, |
| 114 | kb, |
| 115 | part_of, |
| 116 | a, |
| 117 | b, |
| 118 | c, |
| 119 | doc, |
| 120 | chunk_ab, |
| 121 | chunk_bc, |
| 122 | }) |
| 123 | } |
| 124 | |
| 125 | /// 一条断言,带一句原文当证据 |
| 126 | async fn asserted( |
| 127 | pool: &PgPool, |
| 128 | f: &Fixture, |
| 129 | subject: Uuid, |
| 130 | object: Uuid, |
| 131 | chunk: Uuid, |
| 132 | quote: &str, |
| 133 | ) -> anyhow::Result<Uuid> { |
| 134 | let id = Uuid::now_v7(); |
| 135 | sqlx::query( |
| 136 | "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, confidence) |
| 137 | VALUES ($1, $2, $3, $4, $5, 0.9)", |
| 138 | ) |
| 139 | .bind(id) |
| 140 | .bind(f.kb) |
| 141 | .bind(subject) |
| 142 | .bind(f.part_of) |
| 143 | .bind(object) |
| 144 | .execute(pool) |
| 145 | .await?; |
| 146 | sqlx::query( |
| 147 | "INSERT INTO fact_evidence (fact_id, chunk_id, quote, proposed_predicate, document_id, doc_version) |
| 148 | VALUES ($1, $2, $3, 'part of', $4, 1)", |
| 149 | ) |
| 150 | .bind(id) |
| 151 | .bind(chunk) |
| 152 | .bind(quote) |
| 153 | .bind(f.doc) |
| 154 | .execute(pool) |
| 155 | .await?; |
| 156 | Ok(id) |
| 157 | } |
| 158 | |
| 159 | #[tokio::test] |
| 160 | async fn a_proof_reaches_the_sentence() -> anyhow::Result<()> { |
| 161 | let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else { |
| 162 | eprintln!("跳过:未设 UTOPIA_DATABASE_URL"); |
| 163 | return Ok(()); |
| 164 | }; |
| 165 | let pool = PgPool::connect(&url).await?; |
| 166 | let f = seed(&pool).await?; |
| 167 | |
| 168 | let run = async { |
| 169 | let ab = asserted( |
| 170 | &pool, |
| 171 | &f, |
| 172 | f.a, |
| 173 | f.b, |
| 174 | f.chunk_ab, |
| 175 | "FarmBeats is part of Azure", |
| 176 | ) |
| 177 | .await?; |
| 178 | let bc = asserted( |
| 179 | &pool, |
| 180 | &f, |
| 181 | f.b, |
| 182 | f.c, |
| 183 | f.chunk_bc, |
| 184 | "Azure is part of Microsoft", |
| 185 | ) |
| 186 | .await?; |
| 187 | reasoning::materialize(&pool, f.kb).await?; |
| 188 | |
| 189 | let derived: Vec<utopia_core::models::DerivedFactView> = |
| 190 | reasoning::derived_for_entity(&pool, f.kb, f.a).await?; |
| 191 | let ac = derived |
| 192 | .iter() |
| 193 | .find(|d| d.subject_id == f.a && d.object_id == f.c) |
| 194 | .expect("A part_of C should be derived"); |
| 195 | |
| 196 | // 1. 顺序对,2. 叶子是原句 |
| 197 | let proof = reasoning::proof(&pool, f.kb, ac.id) |
| 198 | .await? |
| 199 | .expect("live derivation has a proof"); |
| 200 | assert_eq!(proof.derived.id, ac.id); |
| 201 | assert_eq!(proof.steps.len(), 2); |
| 202 | assert_eq!( |
| 203 | proof.steps[0].fact_id, ab, |
| 204 | "the chain starts where the derivation starts" |
| 205 | ); |
| 206 | assert_eq!(proof.steps[1].fact_id, bc); |
| 207 | assert_eq!(proof.steps[0].subject, "FarmBeats"); |
| 208 | assert_eq!(proof.steps[0].object.as_deref(), Some("Azure")); |
| 209 | assert_eq!(proof.steps[0].predicate.as_deref(), Some("part of")); |
| 210 | assert_eq!(proof.steps[0].evidence.len(), 1); |
| 211 | assert_eq!( |
| 212 | proof.steps[0].evidence[0].quote.as_deref(), |
| 213 | Some("FarmBeats is part of Azure"), |
| 214 | "the leaf of a proof is the sentence it was extracted from" |
| 215 | ); |
| 216 | assert_eq!(proof.steps[0].evidence[0].chunk_id, f.chunk_ab); |
| 217 | assert_eq!(proof.steps[1].evidence[0].chunk_id, f.chunk_bc); |
| 218 | assert!(proof.steps.iter().all(|s| !s.retracted)); |
| 219 | |
| 220 | // 一个不存在的 id 不是错误,是「没有证明」 |
| 221 | assert!(reasoning::proof(&pool, f.kb, Uuid::now_v7()) |
| 222 | .await? |
| 223 | .is_none()); |
| 224 | |
| 225 | // 3. 撤掉一条前提:派生失效,证明还在,且那一步打上标记 |
| 226 | sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1") |
| 227 | .bind(bc) |
| 228 | .execute(&pool) |
| 229 | .await?; |
| 230 | reasoning::materialize(&pool, f.kb).await?; |
| 231 | let (gone,): (bool,) = |
| 232 | sqlx::query_as("SELECT invalidated_at IS NOT NULL FROM derived_facts WHERE id = $1") |
| 233 | .bind(ac.id) |
| 234 | .fetch_one(&pool) |
| 235 | .await?; |
| 236 | assert!(gone, "a derivation falls with its premise"); |
| 237 | let proof = reasoning::proof(&pool, f.kb, ac.id) |
| 238 | .await? |
| 239 | .expect("an invalidated derivation still explains itself"); |
| 240 | assert!(!proof.steps[0].retracted); |
| 241 | assert!( |
| 242 | proof.steps[1].retracted, |
| 243 | "the retracted premise is marked, not hidden" |
| 244 | ); |
| 245 | anyhow::Ok(()) |
| 246 | } |
| 247 | .await; |
| 248 | |
| 249 | let _ = sqlx::query("DELETE FROM organizations WHERE id = $1") |
| 250 | .bind(f.org) |
| 251 | .execute(&pool) |
| 252 | .await; |
| 253 | run |
| 254 | } |