| 1 | //! 检索候选要**带上祖先**,否则提示词里没有泛化基类。 |
| 2 | //! |
| 3 | //! 为什么非要连库:`ancestors_of` 整个活在一段递归 SQL 里,多继承与菱形 |
| 4 | //! 走不走得通、会不会把同一个祖先展开两次,`cargo check` 一个字都不说。 |
| 5 | //! |
| 6 | //! 守的是这条实测出来的因果链:向量检索天然偏爱字面出现在正文里的叶子类 |
| 7 | //!(一个讲 Sutskever 的分块,976 个类里 `researcher` 排第 4、`person` 排第 359), |
| 8 | //! 前 40 名里一个泛化基类都没有。于是两个症状一起出现——实体被判成 |
| 9 | //! `researcher`(schema.org 里它是 `Audience` 的子类),而 |
| 10 | //! `employee (organization → person)` 的签名退化成 `(* → *)`,模型根本没见过方向约束。 |
| 11 | //! |
| 12 | //! 从前这道地板由「内置类恒在」兜着,种子退场后判据悬空了。 |
| 13 | //! |
| 14 | //! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆。 |
| 15 | |
| 16 | use sqlx::PgPool; |
| 17 | use uuid::Uuid; |
| 18 | |
| 19 | /// 造一段菱形:`researcher → audience → thing`,`corporation → organization → thing`, |
| 20 | /// 外加一个多继承的 `agent`(同时挂在 thing 与 organization 下)。 |
| 21 | /// |
| 22 | /// 菱形是关键:没有去重的递归会把 `thing` 展开两次。 |
| 23 | async fn seed(pool: &PgPool) -> anyhow::Result<(Uuid, Vec<(&'static str, Uuid)>)> { |
| 24 | let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()); |
| 25 | sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'floor-test')") |
| 26 | .bind(org) |
| 27 | .execute(pool) |
| 28 | .await?; |
| 29 | sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'floor-test')") |
| 30 | .bind(ws) |
| 31 | .bind(org) |
| 32 | .execute(pool) |
| 33 | .await?; |
| 34 | sqlx::query( |
| 35 | "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'floor-test')", |
| 36 | ) |
| 37 | .bind(kb) |
| 38 | .bind(ws) |
| 39 | .execute(pool) |
| 40 | .await?; |
| 41 | |
| 42 | let keys = [ |
| 43 | "thing", |
| 44 | "audience", |
| 45 | "researcher", |
| 46 | "organization", |
| 47 | "corporation", |
| 48 | "agent", |
| 49 | ]; |
| 50 | let mut ids: Vec<(&'static str, Uuid)> = Vec::new(); |
| 51 | for k in keys { |
| 52 | let id = Uuid::now_v7(); |
| 53 | sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, $3, $3)") |
| 54 | .bind(id) |
| 55 | .bind(kb) |
| 56 | .bind(k) |
| 57 | .execute(pool) |
| 58 | .await?; |
| 59 | ids.push((k, id)); |
| 60 | } |
| 61 | let get = |k: &str| ids.iter().find(|(n, _)| *n == k).unwrap().1; |
| 62 | for (child, parent) in [ |
| 63 | ("audience", "thing"), |
| 64 | ("researcher", "audience"), |
| 65 | ("organization", "thing"), |
| 66 | ("corporation", "organization"), |
| 67 | // 多继承 + 菱形:agent 两条路都通到 thing |
| 68 | ("agent", "thing"), |
| 69 | ("agent", "organization"), |
| 70 | ] { |
| 71 | sqlx::query( |
| 72 | "INSERT INTO entity_type_parents (child_id, parent_id) VALUES ($1, $2) |
| 73 | ON CONFLICT DO NOTHING", |
| 74 | ) |
| 75 | .bind(get(child)) |
| 76 | .bind(get(parent)) |
| 77 | .execute(pool) |
| 78 | .await?; |
| 79 | } |
| 80 | Ok((kb, ids)) |
| 81 | } |
| 82 | |
| 83 | #[tokio::test] |
| 84 | async fn a_retrieved_leaf_brings_its_ancestors() -> anyhow::Result<()> { |
| 85 | let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else { |
| 86 | eprintln!("跳过:未设 UTOPIA_DATABASE_URL"); |
| 87 | return Ok(()); |
| 88 | }; |
| 89 | let pool = PgPool::connect(&url).await?; |
| 90 | // 开跑前先扫地:断言 panic 会跳过 teardown |
| 91 | sqlx::query("DELETE FROM organizations WHERE name = 'floor-test'") |
| 92 | .execute(&pool) |
| 93 | .await?; |
| 94 | let (kb, ids) = seed(&pool).await?; |
| 95 | let id = |k: &str| ids.iter().find(|(n, _)| *n == k).unwrap().1; |
| 96 | |
| 97 | let run = async { |
| 98 | // 检索只捞到了叶子——正文写的是 "a researcher at the corporation" |
| 99 | let leaves = vec![id("researcher"), id("corporation")]; |
| 100 | let anc = utopia_store::ontology::ancestors_of(&pool, &leaves).await?; |
| 101 | |
| 102 | for k in ["audience", "thing", "organization"] { |
| 103 | assert!(anc.contains(&id(k)), "祖先里少了 {k}——地板没补上"); |
| 104 | } |
| 105 | // 自己不算祖先:调用方会把两者并起来,重复只是噪声 |
| 106 | for k in ["researcher", "corporation"] { |
| 107 | assert!(!anc.contains(&id(k)), "{k} 是它自己,不该出现在祖先里"); |
| 108 | } |
| 109 | |
| 110 | // **菱形去重**:agent 有两条路通到 thing,thing 只该出现一次 |
| 111 | let anc2 = utopia_store::ontology::ancestors_of(&pool, &[id("agent")]).await?; |
| 112 | let things = anc2.iter().filter(|x| **x == id("thing")).count(); |
| 113 | assert_eq!(things, 1, "菱形继承把 thing 展开了 {things} 次"); |
| 114 | assert!(anc2.contains(&id("organization")), "多继承的另一条腿丢了"); |
| 115 | |
| 116 | // 空输入不该炸,也不该扫全表 |
| 117 | assert!(utopia_store::ontology::ancestors_of(&pool, &[]) |
| 118 | .await? |
| 119 | .is_empty()); |
| 120 | Ok::<(), anyhow::Error>(()) |
| 121 | } |
| 122 | .await; |
| 123 | |
| 124 | sqlx::query("DELETE FROM organizations WHERE name = 'floor-test'") |
| 125 | .execute(&pool) |
| 126 | .await?; |
| 127 | let _ = kb; |
| 128 | run |
| 129 | } |