Changesets · Branches · Tags · Compare
| Changeset | 2d52f7a2fb652bbab85503c1f25cec642ffe0a7c446f0361893b3c4ea23feafb |
|---|---|
| Author | WaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-08-31 10:37 +08:00 |
| Committer | GitHub <noreply@github.com> · 2026-08-31 10:37 +08:00 |
| Parents | 5d1e79240894 |
7 files changed
modified crates/utopia-extract/src/lib.rs +49 −2
| @@ -132,10 +132,33 @@ | ||
| 132 | 132 | .join("\n"); |
| 133 | 133 | // 记号只在真有签名时解释一次;没有签名的库,提示词一字不变。 |
| 134 | 134 | // 说明用英文——提示词的**指令语言**是英文,只有 description 跟语料走 |
| 135 | + // | |
| 136 | + // **签名管两件事,而它们的可覆盖性不同。** 第一版把两件事混成了一句 | |
| 137 | + // "It is a hint, not a rule — when the text says otherwise, write what the | |
| 138 | + // text says",于是模型连参数顺序也一并按原文的说法写: | |
| 139 | + // | |
| 140 | + // Elon Musk (person) --employee--> Microsoft | |
| 141 | + // | |
| 142 | + // 而 schema.org 声明的是 employee (organization → person)。实测一次跑里 | |
| 143 | + // 130 条可校验的事实有 102 条这样反着落库——**恰恰是本体包最主要的卖点失效**, | |
| 144 | + // 选 schema.org 的理由就是「方向是声明的不是描述的」。 | |
| 145 | + // | |
| 146 | + // 两件事分开说: | |
| 147 | + // | |
| 148 | + // - **哪些类型能参与**:提示不是闸门。本体可能写错,原文说西雅图就写西雅图。 | |
| 149 | + // 0001 的判断在这里不变——硬闸门会系统性丢数据,part_of 烧我们的正是那样。 | |
| 150 | + // - **参数顺序**:由签名定。顺序不是关于世界的断言,是这个 key 的编码约定; | |
| 151 | + // 原文从来没有「说了别的方向」,它只说两个实体之间存在某种关系。 | |
| 152 | + // 反着说时该交换主宾,而不是反过来用这个关系。 | |
| 135 | 153 | let sig_note = if relations.iter().any(|r| !r.signature.is_empty()) { |
| 136 | 154 | ". A parenthesis after the key is the type signature, subject then object; \ |
| 137 | - \"|\" means or, \"*\" means unconstrained. It is a hint, not a rule — \ | |
| 138 | - when the text says otherwise, write what the text says" | |
| 155 | + \"|\" means or, \"*\" means unconstrained. Which kinds of things may take part \ | |
| 156 | + is a hint, not a rule — when the text says otherwise, write what the text says. \ | |
| 157 | + The order is not a hint: the signature fixes which side is the subject. If the \ | |
| 158 | + text puts them the other way round, swap subject and object so that the subject \ | |
| 159 | + matches the left side — do not reverse the relation. For example, given \ | |
| 160 | + \"employee (organization → person)\" and a text saying \"X is an employee of Y\", \ | |
| 161 | + write Y as the subject and X as the object" | |
| 139 | 162 | } else { |
| 140 | 163 | "" |
| 141 | 164 | }; |
| @@ -638,6 +661,30 @@ | ||
| 638 | 661 | assert!(msgs[0].content.contains("hint, not a rule")); |
| 639 | 662 | } |
| 640 | 663 | |
| 664 | + /// **但顺序不是提示。** | |
| 665 | + /// | |
| 666 | + /// 两句话必须同时在场,少哪一句都退回一种老毛病:少了「提示不是闸门」, | |
| 667 | + /// 本体写错时系统性丢数据(part_of 那种方式);少了「顺序由签名定」, | |
| 668 | + /// 模型按英语直觉写 `Musk --employee--> Microsoft`,而 schema.org 声明的是 | |
| 669 | + /// `employee (organization → person)`——实测一次跑里 130 条可校验的事实 | |
| 670 | + /// 有 102 条这样反着落库。 | |
| 671 | + #[test] | |
| 672 | + fn the_prompt_says_the_order_is_not_a_hint() { | |
| 673 | + let rels = vec![rel("employee", "d", "organization → person")]; | |
| 674 | + let msgs = build_messages(&[], &rels, &[], None, "a.txt", &[], "text"); | |
| 675 | + let c = &msgs[0].content; | |
| 676 | + assert!(c.contains("hint, not a rule"), "类型那句丢了"); | |
| 677 | + assert!(c.contains("The order is not a hint"), "顺序那句丢了"); | |
| 678 | + assert!( | |
| 679 | + c.contains("swap subject and object"), | |
| 680 | + "只说了顺序重要,没说反着写时该怎么办" | |
| 681 | + ); | |
| 682 | + assert!( | |
| 683 | + c.contains("do not reverse the relation"), | |
| 684 | + "少了这句,模型可能去找一个反向关系而不是交换主宾" | |
| 685 | + ); | |
| 686 | + } | |
| 687 | + | |
| 641 | 688 | /// 已知实体必须落在 **user** 消息里、紧挨着正文。 |
| 642 | 689 | /// |
| 643 | 690 | /// 理由是服从性不是缓存:抽象规则打不过挨着它的具体块。清单放进 system 的 |
modified crates/utopia-server/src/extraction.rs +113 −0
| @@ -781,6 +781,72 @@ | ||
| 781 | 781 | (subject_id, object_id) |
| 782 | 782 | }; |
| 783 | 783 | |
| 784 | + // **主语违反 domain、而宾语符合时,按本体声明的方向把它掰正。** | |
| 785 | + // | |
| 786 | + // 先试过提示词,三轮都没赢:违反率从 57% 压到 35%,但压下去的全是 | |
| 787 | + // 类型判错那一半;**真·反向纹丝不动**(22.7% → 17.1% → 17.6%, | |
| 788 | + // 后两个在噪声里)。模型看得见 `employee (organization → person)`, | |
| 789 | + // 就是不照做——英语的 "X is an employee of Y" 太强。 | |
| 790 | + // | |
| 791 | + // 这不是新原则:`produced_by` 命中 `produces` 时(见上面那个 `swap`) | |
| 792 | + // 早就在自动翻转主宾了,区别只在触发条件是**措辞**还是**签名**。 | |
| 793 | + // | |
| 794 | + // 当初反对自动对调的理由是实体类型不可靠——实测 Elon Musk 被判成 | |
| 795 | + // `researcher`。那个前提已经不成立:祖先地板与签名类恒在修好之后, | |
| 796 | + // 同一批人判成了 `person`。而判据本身很窄——**主语违反且宾语符合**, | |
| 797 | + // 两侧都要对上才动。 | |
| 798 | + // | |
| 799 | + // **但绝不静默。** 掰正要留信号:0001 反对的是「用可能错的声明驱动 | |
| 800 | + // 自动动作」,而看得见、可复查、可反悔的动作不属于那一类。 | |
| 801 | + let (subject_id, object_id) = if let Some(pid) = predicate_id { | |
| 802 | + // 类型**从库里的实体读**,不用抽取器手上那份 `entity_type_of`: | |
| 803 | + // 那份只覆盖模型在这一块里声明过的实体,而宾语常是别处已存在的实体, | |
| 804 | + // 这一块没重新声明它,于是查不到、判不了、掰不动。实测差别不小—— | |
| 805 | + // 用声明那份时反向只降到 7.0%,剩下的正是宾语类型查不到的那些 | |
| 806 | + let s_fits = | |
| 807 | + utopia_store::ontology::entity_fits_domain(&state.pool, pid, subject_id).await; | |
| 808 | + if let Ok(Some(false)) = s_fits { | |
| 809 | + let o_fits = | |
| 810 | + utopia_store::ontology::entity_fits_domain(&state.pool, pid, object_id) | |
| 811 | + .await; | |
| 812 | + if let Ok(Some(true)) = o_fits { | |
| 813 | + drop_signal( | |
| 814 | + state, | |
| 815 | + doc.kb_id, | |
| 816 | + document_id, | |
| 817 | + utopia_store::extraction_drops::reason::DIRECTION_CORRECTED, | |
| 818 | + &f.predicate, | |
| 819 | + Some(&format!( | |
| 820 | + "{} → {} 按签名掰正为 {} → {}", | |
| 821 | + f.subject, | |
| 822 | + f.object.as_deref().unwrap_or("?"), | |
| 823 | + f.object.as_deref().unwrap_or("?"), | |
| 824 | + f.subject | |
| 825 | + )), | |
| 826 | + ) | |
| 827 | + .await; | |
| 828 | + (object_id, subject_id) | |
| 829 | + } else { | |
| 830 | + // 对调也不合法:这是选错了关系或类型判错,不是方向问题。 | |
| 831 | + // 照原样落库 + 记信号,交给人看——**不猜** | |
| 832 | + drop_signal( | |
| 833 | + state, | |
| 834 | + doc.kb_id, | |
| 835 | + document_id, | |
| 836 | + utopia_store::extraction_drops::reason::DOMAIN_MISMATCH, | |
| 837 | + &f.predicate, | |
| 838 | + Some(&format!("{} — {} →", f.subject, f.predicate)), | |
| 839 | + ) | |
| 840 | + .await; | |
| 841 | + (subject_id, object_id) | |
| 842 | + } | |
| 843 | + } else { | |
| 844 | + (subject_id, object_id) | |
| 845 | + } | |
| 846 | + } else { | |
| 847 | + (subject_id, object_id) | |
| 848 | + }; | |
| 849 | + | |
| 784 | 850 | { |
| 785 | 851 | let (fact_id, created) = utopia_store::graph::insert_fact( |
| 786 | 852 | &state.pool, |
| @@ -1071,6 +1137,30 @@ | ||
| 1071 | 1137 | ) |
| 1072 | 1138 | .await?, |
| 1073 | 1139 | ); |
| 1140 | + // **命中什么,就把它的祖先一起铺出去。** | |
| 1141 | + // | |
| 1142 | + // 向量检索天然偏爱字面出现在正文里的叶子类。实测一个讲 Sutskever 的分块, | |
| 1143 | + // 976 个类按距离排:`researcher` 第 4、`corporation` 第 27, | |
| 1144 | + // 而 `organization` 第 177、`person` 第 359——**前 40 名里一个泛化基类都没有**。 | |
| 1145 | + // 正文写的是 "a researcher at"、"the corporation",从不写 "person"。 | |
| 1146 | + // | |
| 1147 | + // 两个症状,同一个根因: | |
| 1148 | + // | |
| 1149 | + // - 实体判成 `researcher`(schema.org 里它是 `Audience` 的子类,不是人), | |
| 1150 | + // 于是 `works_for (domain=person)` 全成了违规 | |
| 1151 | + // - `employee (organization → person)` 的签名**退化成 `(* → *)`**——`sig_of` | |
| 1152 | + // 只认铺出去的类,一侧没铺就写 `*`。模型根本没见过那个方向约束 | |
| 1153 | + // | |
| 1154 | + // 从前这道地板由 `seed_classes`(`builtin` 的类)兜着,`build_lists` 的注释写着 | |
| 1155 | + // 「内置类恒在:检索漏掉的分块仍然要有地方落脚」。种子退场后(#128)判据就悬空了—— | |
| 1156 | + // 它当初碰巧等价,只因为种子类正好是那几个通用类。 | |
| 1157 | + // | |
| 1158 | + // 用祖先补这道地板,比维护一张"通用类"清单好:**继承链本来就是本体自己声明的 | |
| 1159 | + // 泛化关系**,谁是谁的上位不需要我们再判断一次。代价是每块多铺几层祖先。 | |
| 1160 | + if !classes.is_empty() { | |
| 1161 | + let picked: Vec<Uuid> = classes.iter().copied().collect(); | |
| 1162 | + classes.extend(utopia_store::ontology::ancestors_of(&state.pool, &picked).await?); | |
| 1163 | + } | |
| 1074 | 1164 | let mut rels: HashSet<Uuid> = HashSet::new(); |
| 1075 | 1165 | // 关系与属性分开检索:两段在提示词里是分开的,混在一起取会让其中一段 |
| 1076 | 1166 | // 被另一段挤空 |
| @@ -1094,6 +1184,29 @@ | ||
| 1094 | 1184 | ) |
| 1095 | 1185 | .await?, |
| 1096 | 1186 | ); |
| 1187 | + // **一个关系被铺出去,它签名点名的类就得跟着铺。** | |
| 1188 | + // | |
| 1189 | + // 类与关系是各自独立检索的,而签名依赖两者的交集——`sig_of` 只认铺出去的类, | |
| 1190 | + // 一侧没铺就写 `*`。于是常出现这种局面:`employee` 跟正文语义相近被捞了进来, | |
| 1191 | + // 而它的 `organization`(第 795 名)与 `person`(第 630 名)离正文字面很远, | |
| 1192 | + // 一个都没捞到,签名退化成 `(* → *)`——**方向约束整个消失**,模型按英语直觉 | |
| 1193 | + // 写 `Musk --employee--> Microsoft`,而 schema.org 声明的是 organization → person。 | |
| 1194 | + // | |
| 1195 | + // 上面那道祖先地板治不了这种块:它从"命中的叶子"往上长,而这里一个相关的 | |
| 1196 | + // 叶子都没命中,没有叶子也就没有祖先。 | |
| 1197 | + // | |
| 1198 | + // `sig_of` 的注释说「签名指向看不见的类只会误导」——顾虑是对的,但抹掉签名 | |
| 1199 | + // 是拿丢失方向来换。**把类拉进来**两头都保住:模型看得见那个类,签名也排得出。 | |
| 1200 | + // 顺带还对:这些类正是模型马上要用来判类型的那些,`employee` 在场就说明 | |
| 1201 | + // 这一块讲的是雇佣,`organization`/`person` 本来就该在候选里—— | |
| 1202 | + // 按字面相似度捞不到它们,但**本体的结构知道**。 | |
| 1203 | + let sig_classes: HashSet<Uuid> = rtypes | |
| 1204 | + .iter() | |
| 1205 | + .filter(|r| rels.contains(&r.id)) | |
| 1206 | + .flat_map(|r| r.domains.iter().chain(r.ranges.iter()).copied()) | |
| 1207 | + .collect(); | |
| 1208 | + classes.extend(sig_classes); | |
| 1209 | + | |
| 1097 | 1210 | // 一个候选都没检索到 = 索引还没建好,退回全量而不是给一份空清单 |
| 1098 | 1211 | if classes.len() <= seed_classes.len() && rels.is_empty() { |
| 1099 | 1212 | return Ok(None); |
modified crates/utopia-store/src/extraction_drops.rs +6 −0
| @@ -28,6 +28,12 @@ | ||
| 28 | 28 | pub const OBJECT_MISSING: &str = "object_missing"; |
| 29 | 29 | /// 模型给的这一条不合结构(缺 predicate 之类)→ 只跳这一条,不牵连整块 |
| 30 | 30 | pub const MALFORMED_ITEM: &str = "malformed_item"; |
| 31 | + /// 主语的类型对不上关系声明的 domain,**且对调也不合法**——那是选错了关系 | |
| 32 | + /// 或类型判错,不是方向问题。照原样落库 + 记信号,交给人看,不猜 | |
| 33 | + pub const DOMAIN_MISMATCH: &str = "domain_mismatch"; | |
| 34 | + /// 主语违反 domain 而宾语符合,已按本体声明的方向把主宾掰正。 | |
| 35 | + /// **动作必须留痕**:自动的、看不见的改写才是 0001 反对的那种 | |
| 36 | + pub const DIRECTION_CORRECTED: &str = "direction_corrected"; | |
| 31 | 37 | /// 模型输出被截断(撞上 max_tokens)→ 已完整的那些留下,尾巴丢掉 |
| 32 | 38 | pub const TRUNCATED_REPLY: &str = "truncated_reply"; |
| 33 | 39 | } |
modified crates/utopia-store/src/ontology.rs +90 −0
| @@ -1562,3 +1562,93 @@ | ||
| 1562 | 1562 | .await?; |
| 1563 | 1563 | Ok(n) |
| 1564 | 1564 | } |
| 1565 | + | |
| 1566 | +/// 主语的类型合不合这个关系声明的 domain。沿继承链往上找。 | |
| 1567 | +/// | |
| 1568 | +/// **只回答,不动数据。** 0001 已经判过:签名是提示不是闸门, | |
| 1569 | +/// 「用可能错的声明驱动自动动作风险高」。而实体类型本身也是模型判出来的 | |
| 1570 | +/// (实测里 Elon Musk 被判成 `researcher`),拿它去翻转事实方向, | |
| 1571 | +/// 是两层不确定叠在一起还静默改写。所以这里的结果只用来落一条信号。 | |
| 1572 | +/// | |
| 1573 | +/// 三种回答,别混成两种: | |
| 1574 | +/// - `Some(true)` 合 | |
| 1575 | +/// - `Some(false)` 不合——**这才是信号** | |
| 1576 | +/// - `None` 没得判(关系没声明 domain,或实体还没有类型) | |
| 1577 | +pub async fn subject_fits_domain( | |
| 1578 | + pool: &PgPool, | |
| 1579 | + relation_type_id: Uuid, | |
| 1580 | + subject_type_id: Option<Uuid>, | |
| 1581 | +) -> AppResult<Option<bool>> { | |
| 1582 | + let Some(subject_type_id) = subject_type_id else { | |
| 1583 | + return Ok(None); | |
| 1584 | + }; | |
| 1585 | + let (declared, ok): (i64, i64) = sqlx::query_as( | |
| 1586 | + "WITH RECURSIVE up(id) AS ( | |
| 1587 | + SELECT $2::uuid | |
| 1588 | + UNION | |
| 1589 | + SELECT p.parent_id FROM entity_type_parents p JOIN up ON p.child_id = up.id | |
| 1590 | + ) | |
| 1591 | + SELECT (SELECT count(*) FROM relation_type_domains WHERE relation_type_id = $1), | |
| 1592 | + (SELECT count(*) FROM relation_type_domains d | |
| 1593 | + JOIN up ON up.id = d.entity_type_id | |
| 1594 | + WHERE d.relation_type_id = $1)", | |
| 1595 | + ) | |
| 1596 | + .bind(relation_type_id) | |
| 1597 | + .bind(subject_type_id) | |
| 1598 | + .fetch_one(pool) | |
| 1599 | + .await?; | |
| 1600 | + Ok((declared > 0).then_some(ok > 0)) | |
| 1601 | +} | |
| 1602 | + | |
| 1603 | +/// 这些类的全部祖先(不含自己)。沿 `subClassOf` 上溯,多继承与菱形都走得通。 | |
| 1604 | +/// | |
| 1605 | +/// 给按块检索的候选补地板用:向量检索偏爱字面出现在正文里的叶子类, | |
| 1606 | +/// 泛化基类排得很后(实测 `person` 在 976 个类里排第 359),于是提示词里 | |
| 1607 | +/// 没有它们——实体无处落脚,关系签名也退化成 `*`。祖先是本体自己声明的 | |
| 1608 | +/// 泛化关系,拿它补比维护一张「通用类」清单可靠。 | |
| 1609 | +pub async fn ancestors_of(pool: &PgPool, ids: &[Uuid]) -> AppResult<Vec<Uuid>> { | |
| 1610 | + if ids.is_empty() { | |
| 1611 | + return Ok(Vec::new()); | |
| 1612 | + } | |
| 1613 | + // 递归项里 UNION 去重,菱形继承不会把同一个祖先展开两次 | |
| 1614 | + let rows: Vec<(Uuid,)> = sqlx::query_as( | |
| 1615 | + "WITH RECURSIVE up(id) AS ( | |
| 1616 | + SELECT unnest($1::uuid[]) | |
| 1617 | + UNION | |
| 1618 | + SELECT p.parent_id FROM entity_type_parents p JOIN up ON p.child_id = up.id | |
| 1619 | + ) | |
| 1620 | + SELECT id FROM up WHERE id <> ALL($1::uuid[])", | |
| 1621 | + ) | |
| 1622 | + .bind(ids) | |
| 1623 | + .fetch_all(pool) | |
| 1624 | + .await?; | |
| 1625 | + Ok(rows.into_iter().map(|(id,)| id).collect()) | |
| 1626 | +} | |
| 1627 | + | |
| 1628 | +/// 同 [`subject_fits_domain`],但类型**从库里的实体读**,不靠调用方手上那份。 | |
| 1629 | +/// | |
| 1630 | +/// 抽取器手上的 `entity_type_of` 只覆盖模型在这一块里声明过的实体;宾语常常 | |
| 1631 | +/// 是别处已经存在的实体,这一块没重新声明它的类型,于是查不到、判不了。 | |
| 1632 | +/// 而消解已经把它连到了库里那一行——那里有类型,用它才判得全。 | |
| 1633 | +pub async fn entity_fits_domain( | |
| 1634 | + pool: &PgPool, | |
| 1635 | + relation_type_id: Uuid, | |
| 1636 | + entity_id: Uuid, | |
| 1637 | +) -> AppResult<Option<bool>> { | |
| 1638 | + let (declared, ok): (i64, i64) = sqlx::query_as( | |
| 1639 | + "WITH RECURSIVE up(id) AS ( | |
| 1640 | + SELECT type_id FROM entities WHERE id = $2 | |
| 1641 | + UNION | |
| 1642 | + SELECT p.parent_id FROM entity_type_parents p JOIN up ON p.child_id = up.id | |
| 1643 | + ) | |
| 1644 | + SELECT (SELECT count(*) FROM relation_type_domains WHERE relation_type_id = $1), | |
| 1645 | + (SELECT count(*) FROM relation_type_domains d | |
| 1646 | + JOIN up ON up.id = d.entity_type_id | |
| 1647 | + WHERE d.relation_type_id = $1)", | |
| 1648 | + ) | |
| 1649 | + .bind(relation_type_id) | |
| 1650 | + .bind(entity_id) | |
| 1651 | + .fetch_one(pool) | |
| 1652 | + .await?; | |
| 1653 | + Ok((declared > 0).then_some(ok > 0)) | |
| 1654 | +} |
added crates/utopia-store/tests/the_floor_under_retrieval.rs +129 −0
| @@ -0,0 +1,129 @@ | ||
| 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 | +} |
modified web/src/i18n/en.ts +2 −0
| @@ -337,6 +337,8 @@ | ||
| 337 | 337 | object_missing: "Relation had no object", |
| 338 | 338 | malformed_item: "The model's item did not fit the schema", |
| 339 | 339 | truncated_reply: "The model's reply was cut off", |
| 340 | + domain_mismatch: "The subject does not fit the relation, and swapping would not help", | |
| 341 | + direction_corrected: "Subject and object were swapped to match the signature", | |
| 340 | 342 | } as Record<string, string>, |
| 341 | 343 | // 来源级重抽:不危险,只是费时费钱——轻确认,文案直说成本与保留项 |
| 342 | 344 | reExtractSource: "Re-extract", |
modified web/src/i18n/zh.ts +2 −0
| @@ -311,6 +311,8 @@ | ||
| 311 | 311 | object_missing: "关系缺少宾语", |
| 312 | 312 | malformed_item: "模型给的这一条结构不合", |
| 313 | 313 | truncated_reply: "模型的输出被截断", |
| 314 | + domain_mismatch: "主语对不上这个关系,对调也不行", | |
| 315 | + direction_corrected: "已按签名把主宾掰正", | |
| 314 | 316 | }, |
| 315 | 317 | reExtractSource: "重新抽取", |
| 316 | 318 | reExtractTitle: "重新抽取这个来源?", |