Skip to content

World's first open-source enterprise world model.

The old ontology growth loop is retired: Suggest, auto-extension and adoption by wording go, and the agent's proposals are the one way the ontology grows (#1129)

WaylandYang31c698e14975
diff --git a/crates/utopia-core/src/models.rs b/crates/utopia-core/src/models.rs
index 058e4536..447de450 100644
--- a/crates/utopia-core/src/models.rs
+++ b/crates/utopia-core/src/models.rs
@@ -605,30 +605,6 @@ pub struct FactQualifier {
pub entity_name: Option<String>,
}
-/// 抽取未匹配统计(本体扩展建议的信号源)。
-#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
-pub struct OntologyMiss {
- pub kind: String,
- pub key: String,
- pub example: Option<String>,
- pub count: i32,
-}
-
-/// 一个待认领的表层谓词:原文这么说过,但本体里没有对应关系,事实降级成了
-/// related_to。与 `OntologyMiss` 的纯计数不同,它连着具体事实——所以采纳时
-/// 能说清"将重新归类 57 条",并真的去改。
-#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
-pub struct ProposedPredicate {
- pub form: String,
- /// 有多少条 live 的 related_to 事实由这个说法而来
- pub fact_count: i64,
- /// 出现在多少篇文档里。只在一篇里出现过的是那篇文档的用词,不是这个
- /// 组织的词汇——自动扩展据此设门槛,人工提案只作参考不拦
- pub doc_count: i64,
- /// 一条样例("Dino Crisis (Steam) → GeForce NOW"),让人一眼判断这是什么关系
- pub example: Option<String>,
-}
-
/// 一次 OWL 导入的记录。原文按内容寻址存在 blob 里,这行只是账。
/// `summary` 记下那次投影做了什么,包括**暂未投影**的公理——将来补上消费者
/// 时据此知道哪些导入值得重跑。
@@ -652,17 +628,6 @@ pub struct ModelLimit {
pub max_concurrent: i32,
}
-/// 一个待认领的实体类型:模型提议过、本体没有、实体因此降级成了 concept。
-/// 与 `ProposedPredicate` 对称——它连着具体实体,所以采纳时能说清"将重新归类
-/// 43 个"并真的去改,而不是只建一个空类。
-#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
-pub struct ProposedType {
- pub form: String,
- pub entity_count: i64,
- /// 一个样例名字,让人一眼判断这是什么类
- pub example: Option<String>,
-}
-
/// 抽取丢弃信号:事实抽出来了却没能落地,以及为什么。
/// 与 `OntologyMiss` 分开——那个说"你的本体缺这些"(读者是本体维护者),
/// 这个说"这些事实没落地"(读者是上传文档的人)。
@@ -1202,27 +1167,6 @@ pub struct TypeCandidate {
pub distance: f32,
}
-/// 一个被记下来、但本体里没有对应属性的**字面值**说法。
-///
-/// 跟 [`ProposedPredicate`] 是一对:那个是宾语指向实体的("收购"),
-/// 这个是宾语是字面值的("成立日期 = 2015")。两者不能混——提案要产出的东西
-/// 不一样(关系 vs 属性),而混起来的后果具体:一条 `founding_date` 会变成
-/// 一条指向「2015」这个假实体的边。
-#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
-pub struct ProposedAttribute {
- pub form: String,
- pub fact_count: i64,
- pub doc_count: i64,
- /// 一条样例值(`"2015"`、`1200`),让人一眼看出这是什么类型的数
- pub example: Option<String>,
- /// 这个说法**实际挂在哪些类上**(主语的类型)。
- ///
- /// 属性必须声明 domain,而 domain 猜错的代价是硬的:主语类型对不上
- /// 就整条丢弃(`attr_domain_mismatch`)。所以不问模型,直接从数据里取——
- /// 事实已经在那儿了,它们的主语是什么类是事实,不是判断
- pub domain_keys: Vec<String>,
-}
-
/// 一条口径改动之前的样子。
///
/// **存整版快照而不是差异**(0006):读的时候要回答的是「当时是什么」,
diff --git a/crates/utopia-server/src/api/mcp_tests.rs b/crates/utopia-server/src/api/mcp_tests.rs
index b7f285ac..110ec325 100644
--- a/crates/utopia-server/src/api/mcp_tests.rs
+++ b/crates/utopia-server/src/api/mcp_tests.rs
@@ -2284,100 +2284,6 @@ async fn computed_rule_descriptions_keep_the_expression_tree_and_identity() -> a
f.clean().await
}
-#[tokio::test]
-async fn written_magnitudes_keep_fractions_through_authenticated_adoption() -> anyhow::Result<()> {
- use axum::body::{to_bytes, Body};
- use axum::http::{Request, StatusCode};
- use tower::ServiceExt;
-
- let Some(f) = Fixture::new().await? else {
- return Ok(());
- };
- async fn check(f: &Fixture) -> anyhow::Result<()> {
- let auth = utopia_store::tokens::authenticate(&f.state.pool, &f.token).await?;
- sqlx::query("UPDATE kb_members SET role='editor' WHERE kb_id=$1 AND user_id=$2")
- .bind(f.kb)
- .bind(auth.user_id)
- .execute(&f.state.pool)
- .await?;
- let jwt = crate::auth::issue_token(&f.state, auth.user_id)?;
- let app = crate::api::router(f.state.clone(), &Default::default());
- for (index, input) in ["1.00000025 million", "100.000025万"]
- .into_iter()
- .enumerate()
- {
- let form = format!("fractional_amount_{index}");
- let raw = json!({"value":input,"unit":"$"});
- // This endpoint adopts unbound typed value facts. Open statements are
- // intentionally not used: their alignment is a different write path.
- let (old, _) = utopia_store::graph::insert_value_fact(
- &f.state.pool,
- f.kb,
- f.subject,
- None,
- &raw,
- utopia_store::graph::Validity::default(),
- 0.9,
- )
- .await?;
- sqlx::query("INSERT INTO fact_evidence(fact_id,chunk_id,document_id,doc_version,quote,proposed_predicate)
- VALUES ($1,$2,$3,1,$4,$5)")
- .bind(old).bind(f.chunk).bind(f.document).bind(input).bind(&form).execute(&f.state.pool).await?;
- let waiting = utopia_store::graph::value_facts_for_forms(
- &f.state.pool,
- f.kb,
- std::slice::from_ref(&form),
- )
- .await?;
- anyhow::ensure!(
- waiting.len() == 1 && waiting[0].0 == old,
- "fixture is not supported by adoption"
- );
- let response = app.clone().oneshot(Request::builder().method("POST")
- .uri(format!("/api/v1/kbs/{}/ontology/adopt-predicate",f.kb))
- .header("authorization",format!("Bearer {jwt}"))
- .header("content-type","application/json")
- .body(Body::from(json!({"key":form,"label":form,"forms":[form],"kind":"attribute","datatype":"number"}).to_string()))?).await?;
- let status = response.status();
- let bytes = to_bytes(response.into_body(), 1024 * 1024).await?;
- anyhow::ensure!(
- status == StatusCode::OK,
- "adoption rejected: {}",
- String::from_utf8_lossy(&bytes)
- );
- let result: Value = serde_json::from_slice(&bytes)?;
- anyhow::ensure!(result["remapped"] == 1, "no fact adopted: {result}");
- let attribute = uuid(&result["id"]);
- let (new, stored, supersedes): (Uuid,Value,Option<Uuid>) = sqlx::query_as(
- "SELECT id,object_value,supersedes FROM facts WHERE kb_id=$1 AND predicate_id=$2 AND invalidated_at IS NULL")
- .bind(f.kb).bind(attribute).fetch_one(&f.state.pool).await?;
- anyhow::ensure!(
- stored["value"].as_f64() == Some(1000000.25),
- "adoption rounded away .25: {stored}"
- );
- anyhow::ensure!(
- stored["unit"] == "$" && supersedes == Some(old),
- "unit or history lost"
- );
- let original: Value = sqlx::query_scalar("SELECT object_value FROM facts WHERE id=$1")
- .bind(old)
- .fetch_one(&f.state.pool)
- .await?;
- anyhow::ensure!(original == raw, "historical value rewritten");
- let evidence: bool = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM fact_evidence WHERE fact_id=$1 AND quote=$2 AND document_id=$3)")
- .bind(new).bind(input).bind(f.document).fetch_one(&f.state.pool).await?;
- anyhow::ensure!(evidence, "adopted fact lost source evidence");
- let audit: i64 = sqlx::query_scalar("SELECT count(*) FROM audit_events WHERE kb_id=$1 AND action='ontology.attribute_adopted' AND target_id=$2")
- .bind(f.kb).bind(attribute).fetch_one(&f.state.pool).await?;
- anyhow::ensure!(audit == 1, "missing adoption audit");
- }
- Ok(())
- }
- let result = check(&f).await;
- let cleanup = f.clean().await;
- result.and(cleanup)
-}
-
#[tokio::test]
async fn rule_reads_preserve_matches_and_empty_results() -> anyhow::Result<()> {
let Some(f) = Fixture::new().await? else {
diff --git a/crates/utopia-server/src/api/mod.rs b/crates/utopia-server/src/api/mod.rs
index 3e22eb0d..119831f9 100644
--- a/crates/utopia-server/src/api/mod.rs
+++ b/crates/utopia-server/src/api/mod.rs
@@ -350,15 +350,6 @@ pub fn router(state: AppState, cfg: &AppConfig) -> Router {
"/kbs/{id}/ontology/relation-types/{type_id}/reconcile",
post(ontology_routes::reconcile_relation_type),
)
- .route(
- "/kbs/{id}/ontology/misses/dismiss",
- post(ontology_routes::dismiss_miss),
- )
- .route(
- "/kbs/{id}/ontology/misses/restore",
- post(ontology_routes::restore_miss),
- )
- .route("/kbs/{id}/ontology/suggest", post(ontology_routes::suggest))
// 上次算出来、还没人表态的那些(见 `ontology_proposals`)。刷新页面靠它,不必重跑模型
.route(
"/kbs/{id}/ontology/proposals",
@@ -400,22 +391,6 @@ pub fn router(state: AppState, cfg: &AppConfig) -> Router {
"/kbs/{id}/ontology/imports/preview",
post(ontology_routes::preview_import).layer(DefaultBodyLimit::max(16 * 1024 * 1024)),
)
- .route(
- "/kbs/{id}/ontology/proposed-predicates",
- get(ontology_routes::proposed_predicates),
- )
- .route(
- "/kbs/{id}/ontology/auto-extension",
- get(ontology_routes::last_auto_extension),
- )
- .route(
- "/kbs/{id}/ontology/adopt-predicate",
- post(ontology_routes::adopt_predicate),
- )
- .route(
- "/kbs/{id}/ontology/adopt-predicate/{batch_id}",
- axum::routing::delete(ontology_routes::unadopt_predicate),
- )
.route("/kbs/{id}/search", post(search_routes::search))
.route("/kbs/{id}/chat", post(chat::chat))
.route("/kbs/{id}/chat/{conversation_id}/stop", post(chat::stop))
diff --git a/crates/utopia-server/src/api/ontology_routes.rs b/crates/utopia-server/src/api/ontology_routes.rs
index bb85fa8b..a075ce68 100644
--- a/crates/utopia-server/src/api/ontology_routes.rs
+++ b/crates/utopia-server/src/api/ontology_routes.rs
@@ -7,12 +7,10 @@ use serde::Deserialize;
use serde_json::json;
use utopia_core::models::Role;
use utopia_core::AppError;
-use utopia_llm::ChatMessage;
use uuid::Uuid;
use crate::auth::AuthUser;
use crate::error::ApiResult;
-use crate::llm_util;
use crate::state::AppState;
async fn require_kb(
@@ -30,17 +28,13 @@ pub async fn get(
Path(kb_id): Path<Uuid>,
) -> ApiResult<Json<serde_json::Value>> {
require_kb(&state, &user, kb_id, Role::Viewer).await?;
- let (entity_types, relation_types, misses, dismissed_misses) = tokio::try_join!(
+ let (entity_types, relation_types) = tokio::try_join!(
utopia_store::ontology::entity_type_views(&state.pool, kb_id),
utopia_store::ontology::relation_type_views(&state.pool, kb_id),
- utopia_store::ontology::list_misses(&state.pool, kb_id),
- utopia_store::ontology::list_dismissed_misses(&state.pool, kb_id),
)?;
Ok(Json(json!({
"entity_types": entity_types,
"relation_types": relation_types,
- "misses": misses,
- "dismissed_misses": dismissed_misses,
})))
}
@@ -510,91 +504,6 @@ pub async fn reconcile_relation_type(
})))
}
-#[derive(Deserialize)]
-pub struct DismissMissReq {
- pub kind: String,
- pub key: String,
-}
-
-pub async fn dismiss_miss(
- State(state): State<AppState>,
- AuthUser(user): AuthUser,
- Path(kb_id): Path<Uuid>,
- Json(req): Json<DismissMissReq>,
-) -> ApiResult<Json<serde_json::Value>> {
- require_kb(&state, &user, kb_id, Role::Editor).await?;
- utopia_store::ontology::dismiss_miss(&state.pool, kb_id, &req.kind, &req.key).await?;
- Ok(Json(json!({ "ok": true })))
-}
-
-pub async fn restore_miss(
- State(state): State<AppState>,
- AuthUser(user): AuthUser,
- Path(kb_id): Path<Uuid>,
- Json(req): Json<DismissMissReq>,
-) -> ApiResult<Json<serde_json::Value>> {
- require_kb(&state, &user, kb_id, Role::Editor).await?;
- utopia_store::ontology::restore_miss(&state.pool, kb_id, &req.kind, &req.key).await?;
- Ok(Json(json!({ "ok": true })))
-}
-
-/// LLM 本体扩展建议:现有本体 + 未匹配统计 → 提案(人审后经 create 端点合入)。
-/// `locale` 是**调用方**说的,不是后端的设置。reason 只给人看,而人就在这次请求的
-/// 另一端;界面语言在客户端(docs/decisions/0004),所以它只能这样传进来。
-#[derive(Deserialize, Default)]
-pub struct SuggestReq {
- #[serde(default)]
- pub locale: Option<String>,
-}
-
-pub async fn suggest(
- State(state): State<AppState>,
- AuthUser(user): AuthUser,
- Path(kb_id): Path<Uuid>,
- body: Option<Json<SuggestReq>>,
-) -> ApiResult<Json<serde_json::Value>> {
- require_kb(&state, &user, kb_id, Role::Editor).await?;
- let locale = body
- .and_then(|Json(b)| b.locale)
- .filter(|l| matches!(l.as_str(), "en" | "zh"))
- .unwrap_or_else(|| "en".into());
- // 人工那条路 min_docs = 0:面板上「出现在 1 篇」这个数字是显示给人看的,
- // 他自己判断得了。替他滤掉,只是让他少一条信息
- let proposals = build_proposals(&state, kb_id, &locale, 0).await?;
- // 算完就写下来(见 `ontology_proposals`)。从前这批结果只回给前端、存进一个 useState,
- // 刷新一次就没了——而重算要再调一次模型,且未必给出同一批归并
- persist_proposals(&state, kb_id, &proposals).await;
- Ok(Json(proposals))
-}
-
-/// 四个小节里的每一条都记一行。**失败不拦住返回**——提案已经算出来了,
-/// 存不下只是下次要重算,把整个请求判失败反而把算出来的也丢了。
-async fn persist_proposals(state: &AppState, kb_id: Uuid, proposals: &serde_json::Value) {
- const SECTIONS: [&str; 4] = [
- "entity_types",
- "relation_types",
- "attribute_types",
- "map_to",
- ];
- let mut rows: Vec<(String, String, serde_json::Value)> = Vec::new();
- for section in SECTIONS {
- let Some(items) = proposals.get(section).and_then(|v| v.as_array()) else {
- continue;
- };
- for it in items {
- // key 是这一条的身份(迁移里的唯一约束用的就是它)。没有 key 的
- // 存不了,也没法在采纳时对回来——跳过而不是编一个
- let Some(key) = it.get("key").and_then(|k| k.as_str()) else {
- continue;
- };
- rows.push((section.to_string(), key.to_string(), it.clone()));
- }
- }
- if let Err(e) = utopia_store::ontology::save_proposals(&state.pool, kb_id, &rows).await {
- tracing::warn!(%kb_id, error = %e, "本体提案落库失败,这一批只在本次响应里");
- }
-}
-
/// 还等着人看的提案,按接口原来的形状拼回去。
///
/// 前端因此不必区分「刚算出来的」与「上次存下的」——两者同一个类型、同一套渲染。
@@ -609,16 +518,18 @@ pub async fn stored_proposals(
"entity_types": [], "relation_types": [], "attribute_types": [], "map_to": []
});
for p in stored {
+ // 只给代理提的(0061)。旧的 Suggest 那条路已经退役,它留在老库里的提案没有
+ // 采纳的入口了,摆出来只是一排点不动的按钮
+ if p.proposed_by != "agent" {
+ continue;
+ }
if let Some(arr) = out.get_mut(&p.section).and_then(|v| v.as_array_mut()) {
let mut item = p.payload;
- // 代理提的多带三格:谁提的、服务哪些问题、会绑上哪些形状(0061)。Suggest 的
- // 一份没有,前端照旧
- if p.proposed_by == "agent" {
- if let Some(o) = item.as_object_mut() {
- o.insert("proposed_by".into(), json!("agent"));
- o.insert("serves".into(), json!(p.serves));
- o.insert("signatures".into(), p.signatures);
- }
+ // 多带三格:谁提的、服务哪些问题、会绑上哪些形状
+ if let Some(o) = item.as_object_mut() {
+ o.insert("proposed_by".into(), json!("agent"));
+ o.insert("serves".into(), json!(p.serves));
+ o.insert("signatures".into(), p.signatures);
}
arr.push(item);
}
@@ -678,652 +589,6 @@ pub async fn decide_proposal(
Ok(Json(json!({ "ok": true })))
}
-/// 生成本体扩展提案。人工点 Suggest 与冷启动自动扩本体走同一条路——
-/// 自动那条不该是另一套判断,只是少了点头那一步。
-/// 每个说法检索几个候选。
-///
-/// 小是刻意的:候选是给模型判断"是不是已经有了"用的,不是让它挑一个最像的凑合。
-/// 开大了只会让它在一堆勉强相关的条目里硬选一个映射过去。
-const CANDIDATES_PER_PROBE: i64 = 5;
-
-/// `min_docs`:**只把出现在这么多篇文档里的说法交给模型**。0 = 全给。
-///
-/// 这一位存在的理由是它曾经不存在。`ProposedPredicate.doc_count` 的注释写着
-/// 「自动扩展据此设门槛,人工提案只作参考不拦」,但接线没接完:
-/// `bootstrap_ontology` 把门槛算出来只用于 `forms.len()` 判断值不值得跑一次
-/// LLM,随后调这个函数时只传 kb_id,于是这里重查一遍**没过滤的**全量。
-///
-/// 实测后果(ai-timeline,348 块):交给模型 526 个说法,其中 456 个只在一篇
-/// 里出现过——**86.7% 是噪声**。模型从这堆里只挑出 9 个,`runs_on`(8 篇都有)
-/// 和 `founded_by`(4 篇)没被挑中,275 条事实继续没有谓词。反方向也漏:
-/// 5 个单篇说法被采纳了,其中两个还建成了新属性,门槛形同虚设。
-pub async fn build_proposals(
- state: &AppState,
- kb_id: Uuid,
- reason_lang: &str,
- min_docs: i64,
-) -> Result<serde_json::Value, AppError> {
- let kb = utopia_store::kbs::get(&state.pool, kb_id).await?;
- let settings = utopia_store::settings::get(&state.pool, kb.workspace_id)
- .await?
- .ok_or_else(|| AppError::invalid("no_chat_model", "Chat model not configured"))?;
- let client = llm_util::chat_client(&settings)
- .ok_or_else(|| AppError::invalid("no_chat_model", "Chat model not configured"))?;
-
- let mut misses = utopia_store::ontology::list_misses(&state.pool, kb_id).await?;
- // 表层谓词比 misses 多一样东西:它连着具体事实,所以提案能承诺"改写 N 条"。
- //
- // **两条线严格分开**,按宾语是实体还是字面值:`收购` 要的是一条关系,
- // `founding_date = "2015"` 要的是一个属性。混起来的后果具体——后者会被
- // 提成关系,于是长出一条指向「2015」这个假实体的边
- let mut forms = utopia_store::graph::proposed_predicates(&state.pool, kb_id).await?;
- let mut value_forms = utopia_store::graph::proposed_attributes(&state.pool, kb_id).await?;
- if min_docs > 0 {
- forms.retain(|f| f.doc_count >= min_docs);
- value_forms.retain(|f| f.doc_count >= min_docs);
- // **三份清单都要滤,滤一份等于没滤。** 同一个说法在提示词里出现两次:
- // miss 行("seen N times")和表层谓词行("on N fact(s)")。
- // `OntologyMiss` 没有文档维度,所以按活下来的说法集合筛——留下的是
- // 那些既跨了篇、又还没有谓词的。类那一路不动:`ProposedType`
- // 同样没有 doc_count,硬滤等于按另一个判据拦,而不是按这个
- let kept: std::collections::HashSet<&str> = forms.iter().map(|f| f.form.as_str()).collect();
- misses.retain(|m| m.kind != "relation_type" || kept.contains(m.key.as_str()));
- }
- if misses.is_empty() && forms.is_empty() && value_forms.is_empty() {
- return Ok(json!({
- "entity_types": [], "relation_types": [], "attribute_types": [], "map_to": []
- }));
- }
-
- // **本体的相关切片,不是它的全文。**
- //
- // 从前这里内联两串全量 key。两个毛病:一份 965 类的本体就是 1949 个 key
- // 的提示词;而且只有 key 没有描述,模型据此判断不了"这个说法是不是某个
- // 已有类型的同义",于是它只会新建,本体里就稳定长出重复。
- //
- // 现在按每个说法各检索几个最近的候选,带着描述给它看。提示词大小从此
- // 与本体规模无关,而"已经有一个了"这件事第一次变得可判断。
- let _ = crate::ontology_index::refresh(state, kb_id).await;
- // 谓词那一路:表层说法 + 关系类的 miss。样例给向量更多着落——
- // 光一个 acquires 太短,带上"星云科技 → 深蓝存储"就有了语境
- let pred_probes: Vec<String> = forms
- .iter()
- .map(|f| match f.example.as_deref() {
- Some(ex) if !ex.is_empty() => format!("{} — {ex}", f.form),
- _ => f.form.clone(),
- })
- .chain(
- misses
- .iter()
- .filter(|m| m.kind == "relation_type")
- .map(|m| m.key.clone()),
- )
- .collect();
- // 类那一路。**必须分开检索**:两张表的描述写的是完全不同的东西,
- // 拿一个词表外的类名去关系里找,回来的一定是勉强相关的关系
- let class_probes: Vec<String> = misses
- .iter()
- .filter(|m| m.kind == "entity_type")
- .map(|m| m.key.clone())
- .collect();
- let per_pred = crate::ontology_index::nearest_for_each(
- state,
- kb_id,
- &pred_probes,
- CANDIDATES_PER_PROBE,
- crate::ontology_index::Target::Predicate(None),
- )
- .await
- .unwrap_or_default();
- let per_class = crate::ontology_index::nearest_for_each(
- state,
- kb_id,
- &class_probes,
- CANDIDATES_PER_PROBE,
- crate::ontology_index::Target::Class,
- )
- .await
- .unwrap_or_default();
- // 属性那一路:只在属性里找。这里若不限 kind,"成立日期"最近的往往是
- // 某条关系,模型就会把一个字面值映射到一条边上去
- let attr_probes: Vec<String> = value_forms
- .iter()
- .map(|f| match f.example.as_deref() {
- Some(ex) if !ex.is_empty() => format!("{} = {ex}", f.form),
- _ => f.form.clone(),
- })
- .chain(
- misses
- .iter()
- .filter(|m| m.kind == "attribute_type")
- .map(|m| m.key.clone()),
- )
- .collect();
- let per_attr = crate::ontology_index::nearest_for_each(
- state,
- kb_id,
- &attr_probes,
- CANDIDATES_PER_PROBE,
- crate::ontology_index::Target::Predicate(Some("attribute")),
- )
- .await
- .unwrap_or_default();
- // 并集去重:几个说法常常指向同一个候选,逐个说法各列一遍是白费令牌
- let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
- let mut candidate_lines: Vec<String> = Vec::new();
- // 模型抄回来的 key 要能对回本体:候选表同时按 key、归一化 key、标签建索引
- let mut by_name: std::collections::HashMap<String, String> = std::collections::HashMap::new();
- // 每个候选是关系还是属性。映射到已有类型时,采纳走哪条改写路径由它定
- let mut kind_of_key: std::collections::HashMap<String, String> =
- std::collections::HashMap::new();
- for c in per_pred
- .iter()
- .chain(per_class.iter())
- .chain(per_attr.iter())
- .flatten()
- {
- if !seen.insert(c.key.clone()) {
- continue;
- }
- by_name.insert(normalize_name(&c.key), c.key.clone());
- by_name.insert(normalize_name(&c.label), c.key.clone());
- // 关系行自带 relation / attribute,类行没有 kind
- let kind = c.kind.as_deref().unwrap_or("entity type");
- kind_of_key.insert(c.key.clone(), kind.to_string());
- let d = c.description.trim();
- // **标签只在它确实多说了点什么的时候才写。**
- // 导进来的本体里 label 常常只是 key 的驼峰写法(acquired_from /
- // acquiredFrom),两个几乎一样的名字并排摆着,模型抄走的就是错的那个。
- // 中文标签配英文 key 那种才是标签有信息量的情形,那时才留
- let name = if normalize_name(&c.label) == normalize_name(&c.key) {
- String::new()
- } else {
- format!(" ({})", c.label)
- };
- candidate_lines.push(if d.is_empty() {
- format!("- {} [{kind}]{name}", c.key)
- } else {
- format!("- {} [{kind}]{name}: {d}", c.key)
- });
- }
- // 一个候选都没有(没配嵌入模型,或本体是空的)时如实说,别让模型
- // 以为"本体里什么都没有"从而放开手建
- let candidates_block = if candidate_lines.is_empty() {
- "(no candidates retrieved — the ontology may be empty, or embeddings are unavailable)"
- .to_string()
- } else {
- candidate_lines.join(
- "
-",
- )
- };
- let miss_lines: Vec<String> = misses
- .iter()
- .map(|m| {
- format!(
- "- [{}] \"{}\" seen {} times, e.g. {}",
- m.kind,
- m.key,
- m.count,
- m.example.as_deref().unwrap_or("-")
- )
- })
- .collect();
-
- // 表层谓词行带上事实数与样例:模型据此判断这是不是一个真关系,
- // 而 forms 字段让采纳时知道该改写哪些事实
- let form_lines: Vec<String> = forms
- .iter()
- .map(|f| {
- format!(
- "- \"{}\" on {} fact(s), e.g. {}",
- f.form,
- f.fact_count,
- f.example.as_deref().unwrap_or("-")
- )
- })
- .collect();
-
- // 字面值那一路多带两样:一条样例值(模型据此判断该是 number 还是 date),
- // 和这个说法实际挂在哪些类上。后者不是给模型看的,是采纳时直接拿来当
- // domain 的——属性的 domain 猜错,主语类型对不上就整条丢弃
- let value_lines: Vec<String> = value_forms
- .iter()
- .map(|f| {
- format!(
- "- \"{}\" on {} fact(s), value e.g. {}, seen on: {}",
- f.form,
- f.fact_count,
- f.example.as_deref().unwrap_or("-"),
- if f.domain_keys.is_empty() {
- "-".to_string()
- } else {
- f.domain_keys.join(", ")
- }
- )
- })
- .collect();
-
- let prompt = format!(
- "You are an ontology engineer.\n\
- \n\
- Below are the ontology entries closest in meaning to the unmatched wordings that \
- follow. This is a retrieved slice, not the whole ontology, so \"not in this list\" \
- does not mean \"not in the ontology\" — it means nothing close to it was found:\n{}\n\
- \n\
- During extraction, the LLM repeatedly produced types/relations OUTSIDE this ontology:\n{}\n\
- \n\
- These predicates were taken from the source text because nothing in the ontology fit. \
- Their facts are currently filed under \"related_to\", which says nothing:\n{}\n\
- \n\
- These carried a literal VALUE rather than pointing at another entity, so each one \
- wants an attribute, never a relation. Turning one into a relation manufactures an \
- entity out of the value — a node named \"2015\" that stands for nothing:\n{}\n\
- \n\
- Each wording gets exactly ONE answer. Do not both map it and propose for it, and do \
- not propose it as a relation and as an attribute — a wording listed above as carrying \
- a value is an attribute, full stop.\n\
- \n\
- For each wording, decide one of two things.\n\
- \n\
- **If one of the candidates above already means it, map to it** — name that \
- candidate's key in \"map_to\". Do this whenever the meaning matches even though the \
- spelling differs (\"founding date\" is founding_date; \"headquartered in\" is \
- location). Adding a second entry for a meaning the ontology already carries is the \
- worst outcome available here: it splits the same facts across two keys permanently, \
- and nothing downstream can tell they were the same.\n\
- \n\
- **Otherwise propose a new entry.** Rules:\n\
- - Merge near-duplicates into ONE relation and list every spelling it covers in \
- \"forms\" (e.g. available_on / available_from / \"available through\" are one relation).\n\
- - Skip generic verbs that carry no domain meaning (is, has, includes, provides, brings).\n\
- - A relation is worth adding when the ontology genuinely lacks that meaning, not merely \
- because a word was frequent.\n\
- - \"functional\" must be false unless the relation truly permits at most one object per \
- subject at a time. Getting this wrong makes the temporal engine manufacture conflicts.\n\
- - An attribute needs a \"datatype\": text, number, date or bool. Read it off the example \
- value. Choose text when unsure — a value that will not convert to the declared type \
- is dropped, and a date stored as text is still the value.\n\
- \n\
- Every proposal needs a \"description\" as well as a \"reason\", and they are not the \
- same thing. The reason argues for adding it and is read by a person. **The description \
- is injected verbatim into the extraction prompt and is the only thing telling the model \
- what belongs here** — write it as a definition: say what the type is, then say what it \
- is not and which existing type those cases belong to. A type that arrives with a weak \
- description becomes the next dumping ground.\n\
- \n\
- Output exactly one JSON object:\n\
- {{\"entity_types\":[{{\"key\":\"snake_case\",\"label\":\"Display Name\",\"description\":\"what belongs here, and what does not\",\"reason\":\"why add it\"}}],\n\
- \"relation_types\":[{{\"key\":\"snake_case\",\"label\":\"display label\",\"temporal\":\"state|event|eternal\",\"functional\":false,\"forms\":[\"surface spellings this covers\"],\"description\":\"what this relation asserts, and what it does not\",\"reason\":\"why add it\"}}],\n\
- \"attribute_types\":[{{\"key\":\"snake_case\",\"label\":\"display label\",\"datatype\":\"text|number|date|bool\",\"unit\":\"optional, e.g. CNY\",\"forms\":[\"surface spellings this covers\"],\"description\":\"what this attribute records, and what it does not\",\"reason\":\"why add it\"}}],\n\
- \"map_to\":[{{\"key\":\"an existing key, copied from the candidate list above\",\"forms\":[\"surface spellings that mean it\"],\"reason\":\"why these are the same thing\"}}]}}\n\
- \n\
- Language, and it overrides the skeleton above — that skeleton is written in English \
- only because these instructions are. Write every \"label\" and \"description\" in {}: \
- they become this knowledge base's own ontology, and the description is read by the \
- extraction model while it reads documents in that language. Write every \"reason\" \
- in {}: a person reads it. \"key\" and \"forms\" stay lowercase ASCII either way.",
- candidates_block,
- miss_lines.join("\n"),
- form_lines.join("\n"),
- value_lines.join("\n"),
- lang_name(&kb.ontology_lang),
- lang_name(reason_lang)
- );
-
- let reply = client
- .chat(&[ChatMessage {
- role: "user".into(),
- content: prompt,
- }])
- .await
- .map_err(AppError::Other)?;
- let block = utopia_extract::json_block(&reply).map_err(AppError::Other)?;
- let mut proposals: serde_json::Value =
- serde_json::from_str(&block).map_err(|e| AppError::Other(e.into()))?;
- resolve_map_targets(&mut proposals, &by_name, &kind_of_key);
- // 说法归哪一档,服务端说了算——它手里有事实。实测模型会把同一个
- // \"founded_in\" 既提成关系又提成属性,两条都采纳就是同一批事实被抢两次
- let value_only: std::collections::HashSet<&str> =
- value_forms.iter().map(|f| f.form.as_str()).collect();
- let entity_only: std::collections::HashSet<&str> =
- forms.iter().map(|f| f.form.as_str()).collect();
- keep_forms(&mut proposals, "relation_types", &entity_only, &value_only);
- keep_forms(&mut proposals, "attribute_types", &value_only, &entity_only);
- Ok(proposals)
-}
-
-/// 把 `map_to` 里的 key 对回本体真正的 key,对不上的整条丢掉。
-///
-/// 模型抄错很常见,而且抄的往往是候选行里挨着的那个标签——`acquiredFrom`
-/// 而不是 `acquired_from`。**这一步必须在服务端做**:界面上那个"用已有的"
-/// 按钮承诺的是把一批事实挂到某个已有谓词上,key 对不上时它只会报错,
-/// 而承诺已经说出去了。对不上就不该显示这条。
-/// 顺带标上目标是关系还是属性:采纳时两条路的改写不一样,而模型答的是
-/// 一个 key,看不出这个 key 落在哪张表的哪一档。
-fn resolve_map_targets(
- proposals: &mut serde_json::Value,
- by_name: &std::collections::HashMap<String, String>,
- kind_of_key: &std::collections::HashMap<String, String>,
-) {
- let Some(items) = proposals.get_mut("map_to").and_then(|v| v.as_array_mut()) else {
- return;
- };
- items.retain_mut(|m| {
- let Some(raw) = m.get("key").and_then(|v| v.as_str()) else {
- return false;
- };
- match by_name.get(&normalize_name(raw)) {
- Some(real) => {
- m["kind"] = json!(kind_of_key
- .get(real)
- .map(String::as_str)
- .unwrap_or("relation"));
- m["key"] = json!(real);
- true
- }
- None => {
- tracing::debug!(key = raw, "map_to 指向了候选之外的 key,丢弃");
- false
- }
- }
- });
-}
-
-/// 只保留说法确实属于这一档的提案,并把不属于的那些说法从 `forms` 里剔掉。
-///
-/// **判据在服务端手里**:一个说法带的是字面值还是实体宾语,事实里写着,
-/// 不必问模型。实测模型会把同一个 `founded_in` 既提成关系又提成属性——
-/// 两条都采纳的话,同一批事实被抢两次,先跑的那条赢,结果取决于循环顺序。
-///
-/// `forms` 全被剔光的提案整条丢掉:它承诺的"改写 N 条"已经是零了。
-fn keep_forms(
- proposals: &mut serde_json::Value,
- section: &str,
- mine: &std::collections::HashSet<&str>,
- theirs: &std::collections::HashSet<&str>,
-) {
- let Some(items) = proposals.get_mut(section).and_then(|v| v.as_array_mut()) else {
- return;
- };
- items.retain_mut(|p| {
- let Some(forms) = p.get_mut("forms").and_then(|v| v.as_array_mut()) else {
- // 没有 forms 的提案只是"加一个类型",不改写事实,与归档无关
- return true;
- };
- forms.retain(|f| {
- let Some(s) = f.as_str() else { return false };
- // 另一档明确认领的才剔掉。两边都不认识的说法(比如来自 misses
- // 而不是表层谓词)原样留着——剔掉它等于替模型否决了一条提案
- !theirs.contains(s) || mine.contains(s)
- });
- !forms.is_empty()
- });
-}
-
-/// key 与标签的比较形式:小写,只留字母数字。
-///
-/// **分隔符整个扔掉**,因为要对齐的正是分隔符的差异:`acquiredFrom`、
-/// `acquired_from`、`Acquired From` 都归到 `acquiredfrom`。折成下划线是不够的
-/// ——驼峰里根本没有分隔符可折。
-///
-/// 只做写法对齐,不做同义判断——那是检索与模型的活。
-fn normalize_name(s: &str) -> String {
- s.chars()
- .filter(|c| c.is_alphanumeric())
- .flat_map(|c| c.to_lowercase())
- .collect()
-}
-
-#[derive(Deserialize)]
-pub struct AdoptReq {
- pub key: String,
- /// true = 这个 key 指的是**已有**的关系/属性,别再建一个。
- ///
- /// 这一位是本体消解的落点:检索告诉模型本体里已经有 `founding_date` 了,
- /// 模型说"这些说法就是它",采纳时只做改写那一半。少了这一位,同一个意思
- /// 会长出第二个 key,往后这批事实就永久分在两处。
- #[serde(default)]
- pub existing: bool,
- #[serde(default)]
- pub label: String,
- #[serde(default)]
- pub description: Option<String>,
- #[serde(default = "default_temporal")]
- pub temporal: String,
- /// 缺省 false,且刻意不由建议方决定——本体声明的唯一性会驱动时态引擎自动
- /// 闭合事实,猜错就是成批的假冲突(part_of 就这么烧过一次)
- #[serde(default)]
- pub functional: bool,
- #[serde(default)]
- pub inverse_functional: bool,
- /// 归入这个关系的表层说法("available_on"、"available through"…)
- pub forms: Vec<String>,
- /// `relation`(缺省)或 `attribute`。
- ///
- /// 属性走另一条改写路径:宾语是字面值,要按 datatype 换算过才能落到
- /// 新属性上;domain 也不由请求带,从事实的主语类型里取
- #[serde(default)]
- pub kind: Option<String>,
- /// 属性专用:text | number | date | bool
- #[serde(default)]
- pub datatype: Option<String>,
- #[serde(default)]
- pub unit: Option<String>,
-}
-
-/// 采纳一个表层谓词:建关系类型 **并把等着它的 related_to 事实改写过去**。
-///
-/// 与单纯 create 的区别就在后半句。只建类型的话本体长大了、图没变好——
-/// 那 57 条事实会继续是"有关联"。改写走追加(新行 + supersedes),
-/// 实体历史里读得到"先记成 related to,后精化成 available on"。
-///
-/// `existing` 为真时跳过前半句:本体里已经有这个意思了,这一次只做改写。
-/// 这是消解与增长的分界——同一个入口,因为对图做的事完全一样。
-pub async fn adopt_predicate(
- State(state): State<AppState>,
- AuthUser(user): AuthUser,
- Path(kb_id): Path<Uuid>,
- Json(req): Json<AdoptReq>,
-) -> ApiResult<Json<serde_json::Value>> {
- require_kb(&state, &user, kb_id, Role::Editor).await?;
- let key = req.key.trim();
- if req.forms.is_empty() {
- return Err(AppError::invalid("forms_required", "forms cannot be empty").into());
- }
- if req.kind.as_deref() == Some("attribute") {
- return adopt_attribute(&state, &user, kb_id, &req).await;
- }
- let predicate_id = if req.existing {
- // 按 key 找已有的那一条。找不到就报错而不是退回新建——
- // 前端说的是"映射到已有的",静默改成新建就是它最不想要的结果
- utopia_store::ontology::relation_type_id_by_key(&state.pool, kb_id, key)
- .await?
- .ok_or_else(|| {
- AppError::invalid("unknown_relation_key", "no relation type with that key")
- })?
- } else {
- utopia_store::ontology::create_relation_type(
- &state.pool,
- kb_id,
- key,
- req.label.trim(),
- &req.temporal,
- // 采纳一个提案不替人声明公理。**functional 是唯一的例外**,
- // 因为它是这个表单本来就问过的一项;其余四条要人去关系页显式勾——
- // 推理机的判据必须是人写下来的,不是采纳时顺手带上的
- utopia_core::models::RelationAxioms {
- functional: req.functional,
- inverse_functional: req.inverse_functional,
- ..Default::default()
- },
- req.description.as_deref().unwrap_or("").trim(),
- "relation",
- // 提案与冷启动只建关系,不声明 domain/range —— 留空 = 不限主宾类型
- &[],
- &[],
- None,
- None,
- )
- .await?
- };
- // **人工路径不对调主宾。**
- //
- // 不是因为它不需要——把 `produced_by` 映射到 `produces` 同样该对调——而是
- // 这里的 forms 是人在面板上勾的,完全可能同时勾了 `produced` 和 `produced_by`,
- // 而一个 swap 标志伺候不了混合。自动那条路不存在这个问题:同组说法共享屈折基,
- // 结尾有没有 `by` 必然一致。真要修得让 adopt 逐条判方向,那是另一件事。
- let utopia_store::graph::Adopted {
- batch_id,
- moved: remapped,
- left_off,
- corrected,
- } = utopia_store::graph::adopt_proposed_predicates(
- &state.pool,
- kb_id,
- predicate_id,
- &req.forms,
- false,
- )
- .await?;
- // 采纳同时清掉对应的未匹配统计——本体已经覆盖它们了
- for form in &req.forms {
- let _ = utopia_store::ontology::clear_miss(&state.pool, kb_id, "relation_type", form).await;
- }
- let _ = utopia_store::audit::record(
- &state.pool,
- Some(kb_id),
- user.id,
- "ontology.predicate_adopted",
- "relation_type",
- Some(predicate_id),
- json!({
- "key": key, "label": req.label, "forms": req.forms,
- "facts_remapped": remapped, "facts_left_off": left_off,
- "facts_direction_corrected": corrected, "batch": batch_id,
- }),
- )
- .await;
- state.emit_review(kb_id);
- Ok(Json(json!({
- "id": predicate_id, "remapped": remapped, "left_off": left_off,
- "corrected": corrected, "batch": batch_id,
- })))
-}
-
-/// 撤销一次采纳:新写的行作废、旧行复活。关系类型留着(有事实指向过它,
-/// 而且一个没人用的关系是惰性的)。
-pub async fn unadopt_predicate(
- State(state): State<AppState>,
- AuthUser(user): AuthUser,
- Path((kb_id, batch_id)): Path<(Uuid, Uuid)>,
-) -> ApiResult<Json<serde_json::Value>> {
- require_kb(&state, &user, kb_id, Role::Editor).await?;
- // 归因要按关系类型找回这次撤销,所以先拿到它
- let predicate_id: Option<(Uuid,)> = sqlx::query_as(
- "SELECT predicate_id FROM fact_adoptions WHERE batch_id = $1 AND kb_id = $2 LIMIT 1",
- )
- .bind(batch_id)
- .bind(kb_id)
- .fetch_optional(&state.pool)
- .await
- .map_err(utopia_core::AppError::Db)?;
- // 一次采纳可能同时产生事实改写与实体改类的批次,调用方拿到的是一串
- // 不分种类的批次号——这里两边都试,谁认领谁生效
- if predicate_id.is_none() {
- let n = utopia_store::resolution::unadopt_types(&state.pool, kb_id, batch_id).await?;
- let _ = utopia_store::audit::record(
- &state.pool,
- Some(kb_id),
- user.id,
- "ontology.adoption_reverted",
- "kb",
- Some(kb_id),
- json!({ "batch": batch_id, "entities_reverted": n }),
- )
- .await;
- state.emit_review(kb_id);
- return Ok(Json(json!({ "reverted": n })));
- }
- let reverted = utopia_store::graph::unadopt(&state.pool, kb_id, batch_id).await?;
- let _ = utopia_store::audit::record(
- &state.pool,
- Some(kb_id),
- user.id,
- "ontology.adoption_reverted",
- "relation_type",
- predicate_id.map(|(id,)| id),
- json!({ "batch": batch_id, "facts_reverted": reverted }),
- )
- .await;
- state.emit_review(kb_id);
- Ok(Json(json!({ "reverted": reverted })))
-}
-
-/// 待认领的表层谓词:原文说过、本体没有、事实降级成了 related_to。
-pub async fn proposed_predicates(
- State(state): State<AppState>,
- AuthUser(user): AuthUser,
- Path(kb_id): Path<Uuid>,
-) -> ApiResult<Json<serde_json::Value>> {
- require_kb(&state, &user, kb_id, Role::Viewer).await?;
- let forms = utopia_store::graph::proposed_predicates(&state.pool, kb_id).await?;
- Ok(Json(json!({ "forms": forms })))
-}
-
-/// 最近一次自动扩本体做了什么,以及还能不能撤销。
-///
-/// 默认开启的前提是它的动作**可见且可退**。只记在审计台账里不算可见——
-/// 那是查证用的,不是通知用的。这里给 Ontology 页一条明确的横幅。
-pub async fn last_auto_extension(
- State(state): State<AppState>,
- AuthUser(user): AuthUser,
- Path(kb_id): Path<Uuid>,
-) -> ApiResult<Json<serde_json::Value>> {
- require_kb(&state, &user, kb_id, Role::Viewer).await?;
- let row: Option<(serde_json::Value, chrono::DateTime<chrono::Utc>)> = sqlx::query_as(
- "SELECT detail, created_at FROM audit_events
- WHERE kb_id = $1 AND action = 'ontology.bootstrapped'
- ORDER BY id DESC LIMIT 1",
- )
- .bind(kb_id)
- .fetch_optional(&state.pool)
- .await
- .map_err(AppError::Db)?;
- let Some((detail, at)) = row else {
- return Ok(Json(json!({ "run": null })));
- };
- // 已经被撤销干净的就不再提示——那一轮已经没有任何痕迹留在图上了
- let batches: Vec<Uuid> = detail
- .get("batches")
- .and_then(|v| v.as_array())
- .map(|a| {
- a.iter()
- .filter_map(|s| s.as_str().and_then(|x| x.parse().ok()))
- .collect()
- })
- .unwrap_or_default();
- let (live,): (i64,) = sqlx::query_as(
- "SELECT count(*) FROM fact_adoptions
- WHERE kb_id = $1 AND batch_id = ANY($2) AND reverted_at IS NULL",
- )
- .bind(kb_id)
- .bind(&batches)
- .fetch_one(&state.pool)
- .await
- .map_err(AppError::Db)?;
- if live == 0 {
- return Ok(Json(json!({ "run": null })));
- }
- Ok(Json(json!({ "run": {
- "at": at,
- "relations": detail.get("relations"),
- "classes": detail.get("classes"),
- "facts_remapped": detail.get("facts_remapped"),
- "batches": batches,
- }})))
-}
-
/// OWL 导入:先看会发生什么,确认了才落库。
///
/// **绝不让上传一个文件就不可逆地改掉本体**——预览与落库走同一个 plan,
@@ -1437,304 +702,6 @@ async fn read_upload(
Err(AppError::invalid("no_files", "No file in the upload"))
}
-/// 语言代码 → 提示词里写给模型看的名字。模型认得懂 "Chinese",未必认得懂 "zh"。
-fn lang_name(code: &str) -> &'static str {
- match code {
- "zh" => "Chinese",
- _ => "English",
- }
-}
-
-/// 采纳一个**字面值**说法:建(或指向已有的)属性,并把等着它的事实改挂过去。
-///
-/// 跟关系那条路有三处不同,每一处都是属性特有的:
-///
-/// 1. **domain 从数据里取,不由请求带。** 属性必须声明能挂在哪些类下,而猜错
-/// 的代价是硬的——主语类型对不上就整条丢弃(`attr_domain_mismatch`)。
-/// 这些事实的主语现在是什么类是事实不是判断,直接读。
-/// 2. **值要按 datatype 换算。** 库里存的是抽取当时的原样(字符串 "2015"),
-/// 落到一个 date 属性上得先变成日期。
-/// 3. **换不出来的不改写。** 宁可让它继续没有谓词,等下一次,也不把
-/// 一个换不动的值硬塞进类型化的属性里——那是"宁缺勿脏"的同一条。
-async fn adopt_attribute(
- state: &AppState,
- user: &utopia_core::models::User,
- kb_id: Uuid,
- req: &AdoptReq,
-) -> ApiResult<Json<serde_json::Value>> {
- let spec = AttributeAdoption {
- key: req.key.trim(),
- label: req.label.trim(),
- description: req.description.as_deref().unwrap_or("").trim(),
- datatype: req.datatype.as_deref().unwrap_or("text"),
- unit: req.unit.as_deref().map(str::trim).filter(|s| !s.is_empty()),
- forms: &req.forms,
- existing: req.existing,
- };
- let done = adopt_attribute_core(state, kb_id, &spec).await?;
- let _ = utopia_store::audit::record(
- &state.pool,
- Some(kb_id),
- user.id,
- "ontology.attribute_adopted",
- "relation_type",
- Some(done.attribute_id),
- json!({ "key": spec.key, "forms": req.forms, "existing": req.existing,
- "remapped": done.remapped, "unconvertible": done.unconvertible }),
- )
- .await;
- // 空关系改判会跑判据对账(0062),队列可能刚变过
- state.emit_review(kb_id);
- // unconvertible 要回给调用方:改写了 3 条、丢下 2 条,界面得说得出后半句
- Ok(Json(json!({
- "id": done.attribute_id, "batch": done.batch_id,
- "remapped": done.remapped, "unconvertible": done.unconvertible
- })))
-}
-
-/// 一次属性采纳要的全部输入。
-pub(crate) struct AttributeAdoption<'a> {
- pub key: &'a str,
- pub label: &'a str,
- pub description: &'a str,
- pub datatype: &'a str,
- pub unit: Option<&'a str>,
- pub forms: &'a [String],
- /// true = key 指的是已有属性,只改写、不新建
- pub existing: bool,
-}
-
-pub(crate) struct AttributeAdopted {
- pub attribute_id: Uuid,
- pub batch_id: Uuid,
- pub remapped: u32,
- /// 值换不动那个 datatype、因而**没有**被改写的条数。
- /// 必须往上传:改写了 3 条、丢下 2 条,只报前半句就是报喜不报忧
- pub unconvertible: usize,
-}
-
-/// 建(或指向已有的)属性,并把等着它的字面值事实改挂过去。人工与自动共用。
-/// 等着这个说法的值**是不是清一色的量**;是的话给出它们共同的单位。
-///
-/// 判据要全体一致:混着 `$12 billion` 与 `chief executive` 的一批,
-/// 按 number 落地会把后者整条丢掉(换不动的不改写),那不如老实当 text。
-fn quantity_shape(facts: &[(Uuid, Option<Uuid>, serde_json::Value)]) -> Option<String> {
- if facts.is_empty() {
- return None;
- }
- let mut unit: Option<String> = None;
- let mut agreed = true;
- for (_, _, v) in facts {
- let raw = v.get("value").unwrap_or(v);
- let hit = match raw {
- serde_json::Value::Number(_) => Some((0.0, None)),
- serde_json::Value::String(s) => utopia_extract::parse_leading_quantity(s),
- _ => None,
- }?;
- // 抽取那一步单记的单位更可信(`$5 billion` → `$`),没有再退回解出来的
- let u = v
- .get("unit")
- .and_then(|u| u.as_str())
- .map(str::to_string)
- .or(hit.1);
- match (&unit, u) {
- (None, Some(u)) => unit = Some(u),
- (Some(a), Some(b)) if *a != b => agreed = false,
- _ => {}
- }
- }
- // 全体都是量才走到这里;单位不一致就当没有单位,别挑一个塞上去
- Some(if agreed {
- unit.unwrap_or_default()
- } else {
- String::new()
- })
-}
-
-pub(crate) async fn adopt_attribute_core(
- state: &AppState,
- kb_id: Uuid,
- spec: &AttributeAdoption<'_>,
-) -> Result<AttributeAdopted, AppError> {
- // 先把待改写的事实取出来:它既定 domain,也定值换不换得动
- let facts = utopia_store::graph::value_facts_for_forms(&state.pool, kb_id, spec.forms).await?;
- let attribute_id = if spec.existing {
- utopia_store::ontology::relation_type_id_by_key(&state.pool, kb_id, spec.key)
- .await?
- .ok_or_else(|| {
- AppError::invalid("unknown_relation_key", "no relation type with that key")
- })?
- } else {
- // **domain 从数据里取。** 属性必须声明能挂在哪些类下,猜错的代价是硬的:
- // 主语类型对不上就整条丢弃。这些事实的主语现在是什么类是事实,不是判断
- let mut domains: Vec<Uuid> = facts
- .iter()
- .filter_map(|(_, type_id, _)| *type_id)
- .collect();
- domains.sort_unstable();
- domains.dedup();
- if domains.is_empty() {
- return Err(AppError::invalid(
- "no_facts_for_forms",
- "nothing is waiting on those wordings",
- ));
- }
- /* **datatype 由等着的那些值定,不听模型的。**
- 实测模型给 `valuation` 报的是 text,而等它的四个值全是
- `$12 billion` 这样的量;存成 text 就比不了大小——而"能比大小"
- 正是这些数不该当节点的理由。手里有事实的时候不必去问判断题,
- `keep_forms` 那里已经是同一个原则。
- 单位同理:几条值的单位一致就落在属性上(`$`、`%`、`people`)。 */
- let inferred = quantity_shape(&facts);
- let datatype = inferred.as_ref().map_or(spec.datatype, |_| "number");
- let unit = inferred.as_deref().filter(|u| !u.is_empty()).or(spec.unit);
- match utopia_store::ontology::create_relation_type(
- &state.pool,
- kb_id,
- spec.key,
- spec.label,
- "state",
- // 建议方不替时态引擎做决定:functional 会驱动它自动闭合旧值,
- // 而其余公理会驱动推理机——两者都该由人显式声明
- Default::default(),
- spec.description,
- "attribute",
- &domains,
- &[],
- Some(datatype),
- unit,
- )
- .await
- {
- Ok(id) => id,
- /* 键被一个**空的**同名关系占着:改判它,别放弃。
- 实测就是这么卡住的——`Relation key 'valuation' already exists`,
- 然后 valuation 那几个数额永远没有谓词,而那条关系自己一条事实
- 都没有。有事实的不会被改判(判据在 store 那一侧的 SQL 里),
- 那种是本体与语料的真分歧,照旧报错留给人看 */
- Err(e) => match utopia_store::ontology::attribute_from_unused_relation(
- &state.pool,
- kb_id,
- spec.key,
- &domains,
- datatype,
- unit,
- )
- .await?
- {
- Some(id) => {
- tracing::info!(%kb_id, key = spec.key, "空关系改判成属性");
- id
- }
- None => return Err(e),
- },
- }
- };
-
- // 换算按**库里那一条**的 datatype,不按请求——指向已有属性时请求里根本
- // 没有 datatype,而即便有,也该听本体的
- let datatype = utopia_store::ontology::relation_type_datatype(&state.pool, attribute_id)
- .await?
- .unwrap_or_else(|| "text".to_string());
- let mut rewrites: Vec<(Uuid, serde_json::Value)> = Vec::new();
- let mut unconvertible = 0usize;
- for (fact_id, _, object_value) in &facts {
- // 抽取写进去的形状是 {"value": …},取里面那一层来换算
- let raw = object_value.get("value").unwrap_or(object_value);
- match utopia_extract::normalize_attr_value(&datatype, raw) {
- // **单位跟着值走**。抽取那一步把 `$5 billion` 的 `$` 单记了一格
- // (见 extraction 里那段说明);换算成 5e9 之后符号丢掉的话,
- // 剩下的数就不知道是钱还是别的什么了
- Some(v) => {
- let mut next = json!({ "value": v });
- if let Some(u) = object_value.get("unit") {
- next["unit"] = u.clone();
- }
- rewrites.push((*fact_id, next))
- }
- // 换不动的**不改写**:宁可让它继续没有谓词,等下一次,
- // 也不把一个换不动的值硬塞进类型化的属性里
- None => unconvertible += 1,
- }
- }
- // 属性那一路没有签名可判(宾语是字面值),`left_off` 恒为 0
- let utopia_store::graph::Adopted {
- batch_id,
- moved: remapped,
- ..
- } = utopia_store::graph::adopt_value_facts(&state.pool, kb_id, attribute_id, &rewrites).await?;
- for form in spec.forms {
- let _ =
- utopia_store::ontology::clear_miss(&state.pool, kb_id, "attribute_type", form).await;
- }
- Ok(AttributeAdopted {
- attribute_id,
- batch_id,
- remapped,
- unconvertible,
- })
-}
-
-/// 映射到**已有属性**:不建东西,只把这些说法的字面值事实挂过去。
-pub(crate) async fn adopt_attribute_existing(
- state: &AppState,
- kb_id: Uuid,
- key: &str,
- forms: &[String],
-) -> Result<(Uuid, u32), AppError> {
- let done = adopt_attribute_core(
- state,
- kb_id,
- &AttributeAdoption {
- key,
- label: "",
- description: "",
- datatype: "text",
- unit: None,
- forms,
- existing: true,
- },
- )
- .await?;
- Ok((done.batch_id, done.remapped))
-}
-
-/// 自动扩本体那条路的入口。参数摊开而不是传 `AdoptReq`——那个结构是 HTTP
-/// 请求体,自动路径没有请求。
-#[allow(clippy::too_many_arguments)]
-pub(crate) async fn adopt_attribute_auto(
- state: &AppState,
- kb_id: Uuid,
- key: &str,
- label: &str,
- description: &str,
- datatype: &str,
- unit: Option<&str>,
- forms: &[String],
-) -> Result<(Uuid, u32), AppError> {
- let done = adopt_attribute_core(
- state,
- kb_id,
- &AttributeAdoption {
- key,
- label,
- description,
- datatype,
- unit,
- forms,
- existing: false,
- },
- )
- .await?;
- if done.unconvertible > 0 {
- tracing::info!(
- %kb_id, key, dropped = done.unconvertible,
- "有值换不动这个 datatype,那些事实继续没有谓词"
- );
- }
- Ok((done.batch_id, done.remapped))
-}
-
/// 类型消解的**只算不写**那一步:每个待精化实体的画像与候选类。
///
/// 跟本体导入同一个模式:先看计划,再决定落不落。在这里它还多一层用处——
@@ -1852,105 +819,3 @@ pub async fn approve_refinement(
.await;
Ok(Json(json!({ "moved": moved, "batch": batch })))
}
-
-#[cfg(test)]
-mod tests {
- use super::{normalize_name, resolve_map_targets};
- use serde_json::json;
- use std::collections::{HashMap, HashSet};
-
- #[test]
- fn names_align_across_spellings() {
- // 导进来的本体里 label 常常只是 key 的驼峰写法,两者必须归到一起
- assert_eq!(
- normalize_name("acquiredFrom"),
- normalize_name("acquired_from")
- );
- assert_eq!(
- normalize_name("Acquired From"),
- normalize_name("acquired_from")
- );
- // 不同的名字仍然不同——这一步只对齐写法,不做同义判断
- assert_ne!(normalize_name("acquired_from"), normalize_name("acquires"));
- // 中文标签原样保留(去不掉也不该去)
- assert_eq!(normalize_name("员工数"), "员工数");
- }
-
- #[test]
- fn a_map_target_outside_the_candidates_is_dropped() {
- let by_name: HashMap<String, String> = [
- ("acquiredfrom".to_string(), "acquired_from".to_string()),
- ("员工数".to_string(), "headcount".to_string()),
- ]
- .into_iter()
- .collect();
- let mut p = json!({"map_to": [
- // 抄的是标签而不是 key —— 实测里模型就是这么干的,要能对回去
- {"key": "acquiredFrom", "forms": ["acquired from"]},
- // 按中文标签抄的,同样对得回去
- {"key": "员工数", "forms": ["员工总数"]},
- // 候选表里没有:界面上那个按钮会承诺一件做不到的事,所以整条丢掉
- {"key": "invented_key", "forms": ["whatever"]},
- // 连 key 都没有
- {"forms": ["x"]},
- ]});
- resolve_map_targets(&mut p, &by_name, &HashMap::new());
- let items = p["map_to"].as_array().unwrap();
- assert_eq!(items.len(), 2);
- assert_eq!(items[0]["key"], "acquired_from");
- assert_eq!(items[1]["key"], "headcount");
- }
-
- #[test]
- fn proposals_without_a_map_to_section_are_left_alone() {
- let mut p = json!({"entity_types": [], "relation_types": []});
- resolve_map_targets(&mut p, &HashMap::new(), &HashMap::new());
- assert!(p.get("map_to").is_none());
- }
-
- #[test]
- fn a_wording_that_carries_a_value_cannot_also_become_a_relation() {
- // 实测过的形状:模型把同一个 founded_in 既提成关系又提成属性。
- // 两条都采纳的话同一批事实被抢两次,谁先跑谁赢
- let value_only: HashSet<&str> = ["founded_in", "registered_capital"].into_iter().collect();
- let entity_only: HashSet<&str> = ["acquires"].into_iter().collect();
- let mut p = json!({
- "relation_types": [
- {"key": "founded", "forms": ["founded_in", "founding date"]},
- {"key": "acquires", "forms": ["acquires"]}
- ],
- "attribute_types": [
- {"key": "founding_year", "forms": ["founded_in"]},
- {"key": "registered_capital", "forms": ["registered_capital"]}
- ]
- });
- super::keep_forms(&mut p, "relation_types", &entity_only, &value_only);
- super::keep_forms(&mut p, "attribute_types", &value_only, &entity_only);
-
- let rels = p["relation_types"].as_array().unwrap();
- // founded 只剩 "founding date"——那个说法两边都不认识,不该替模型否决
- assert_eq!(rels.len(), 2);
- assert_eq!(rels[0]["forms"].as_array().unwrap().len(), 1);
- assert_eq!(rels[0]["forms"][0], "founding date");
- assert_eq!(rels[1]["key"], "acquires");
- // 属性那边一条不动
- assert_eq!(p["attribute_types"].as_array().unwrap().len(), 2);
- }
-
- #[test]
- fn a_proposal_left_with_no_wordings_is_dropped() {
- // forms 被剔光 = 它承诺的"改写 N 条"已经是零,留着只会让人点一下什么也没发生
- let value_only: HashSet<&str> = ["founded_in"].into_iter().collect();
- let mut p = json!({"relation_types": [{"key": "founded", "forms": ["founded_in"]}]});
- super::keep_forms(&mut p, "relation_types", &HashSet::new(), &value_only);
- assert!(p["relation_types"].as_array().unwrap().is_empty());
- }
-
- #[test]
- fn a_type_proposal_without_wordings_is_left_alone() {
- // 没有 forms 的提案只是"加一个类型",不改写任何事实,与归档无关
- let mut p = json!({"entity_types": [{"key": "platform", "label": "Platform"}]});
- super::keep_forms(&mut p, "entity_types", &HashSet::new(), &HashSet::new());
- assert_eq!(p["entity_types"].as_array().unwrap().len(), 1);
- }
-}
diff --git a/crates/utopia-server/src/bootstrap_ontology.rs b/crates/utopia-server/src/bootstrap_ontology.rs
deleted file mode 100644
index c380ae7c..00000000
--- a/crates/utopia-server/src/bootstrap_ontology.rs
+++ /dev/null
@@ -1,694 +0,0 @@
-//! 自动扩本体:抽取遇到本体外的说法时,把它补进本体并改写等它的那些事实。
-//!
-//! 新建的库不种任何关系,起点是用户挑的本体包(0008)或者空着。包再大也装不下
-//! 一份语料的全部说法:落不上本体的事实谓词留空、原词记在证据上(0010),
-//! 图上这些边只有原文措辞、没有词表语义,直到有人坐下来点 Suggest、看提案、逐条 Add。
-//! 这个模块把那件事自动化:说法够普遍就建成关系,并把等着它的事实改写过去。
-//!
-//! 敢这么做的前提是采纳可撤销(见 graph::unadopt):错了点一下就回去,
-//! 旧事实从来没被销毁过。所以判断轴不是"有多确信"而是"错了有多贵"。
-//!
-//! **要不要替人做,由人在知识库设置里声明**(`auto_extend_ontology`,缺省开)。
-//! 曾试过从行为推断——"本体有没有被碰过"——那是猜,而且猜错的后果荒唐:在提案上
-//! 点一次 Add 就会永久关掉建议功能。开关一来,猜测没有了,冻结也没有了。
-//!
-//! 关掉它不影响"留意":未匹配统计照常累积、照常在 Unmatched 面板可见,
-//! 只是变成你点一下的提案,信息一条不少。
-//!
-//! 唯独 `functional` 永不自动,开关开着也不行:它驱动时态引擎自动闭合事实、
-//! 生成冲突,等发现时那些闭合本身已是一串 supersede 链——不属于"错了很便宜"那类。
-
-use std::collections::{HashMap, HashSet};
-use uuid::Uuid;
-
-use crate::api::ontology_routes;
-use crate::predicate_match::{merge_key, PredicateIndex};
-use crate::state::AppState;
-
-/// 少于这么多个够格的信号(谓词 + 类型)就不折腾——凑不出像样的提案,
-/// 白烧一次 LLM 调用。
-const MIN_SIGNALS: usize = 3;
-/// 只采纳出现在这么多篇文档里的说法。**只在一篇里出现过的是那篇文档的用词,
-/// 不是这个组织的词汇**——而本体会反馈进抽取提示词,一次偶然会变成长期指令。
-/// 副作用正好:只有一篇文档时什么都够不着门槛,于是什么也不做,下一篇再试。
-const MIN_DOCS: i64 = 2;
-
-/// 一组按屈折基归并的说法——采纳后它们是同一个关系。
-struct RelationGroup {
- /// 规范 key:组里事实最多的那个说法。**不问模型取名**——组里每个说法都是
- /// 原文真实出现过的措辞,挑最常见的那个比编一个新词更贴语料
- key: String,
- /// 组里全部说法,采纳时一并改写
- forms: Vec<String>,
- facts: i64,
- docs: usize,
- /// 本体里已经有等价关系时的落点:`(关系 id, 主宾要不要对调)`。
- /// `None` = 本体里确实没有,按票数新建
- existing: Option<(Uuid, bool)>,
-}
-
-/// 按票数决定采纳哪些关系。**不调模型。**
-///
-/// 三步:把说法按屈折基归并(`sued` 与 `sues` 是同一个关系)、算文档并集、过门槛。
-///
-/// 并集不能用相加:同一篇文档完全可能两种写法都用过,相加会让一篇文档把一个说法
-/// 顶过「≥2 篇」。所以要 `proposed_predicate_documents` 拿到真正的文档 id。
-///
-/// 输出**排过序**——这条路的价值有一半在于确定性,而 HashMap 的遍历顺序不是。
-async fn counted_relation_groups(
- state: &AppState,
- kb_id: Uuid,
-) -> anyhow::Result<Vec<RelationGroup>> {
- let forms = utopia_store::graph::proposed_predicates(&state.pool, kb_id).await?;
- let pairs = utopia_store::graph::proposed_predicate_documents(&state.pool, kb_id).await?;
- // **建之前先问本体。**
- //
- // 少了这一步,采纳只按票数建,从不检查「是不是已经有等价的了」。实测后果:
- // demo-b3 那个库里 `produced_by` 与 `produces`、`developed_by` 与 `develops`
- // 各成一个关系,同一件事的两个方向永久分家。
- //
- // 而 `produces` 有 265 条、`produced_by` 只有 15 条——票多的先进本体,
- // 票少的那个本该被 `predicate_match` 的 `_by` 规则接住,却因为**采纳路径压根
- // 没走匹配器**而长成了独立关系。匹配器只在抽取时用过,这里是它缺席的第二处。
- let mut rtypes = utopia_store::graph::relation_types(&state.pool, kb_id).await?;
- rtypes.retain(|r| !utopia_store::names::is_name_attribute(r));
- let index = PredicateIndex::build(&rtypes);
-
- let mut docs_of: HashMap<String, HashSet<Uuid>> = HashMap::new();
- for (form, doc) in pairs {
- docs_of.entry(form).or_default().insert(doc);
- }
-
- let mut grouped: HashMap<Vec<String>, Vec<utopia_core::models::ProposedPredicate>> =
- HashMap::new();
- for f in forms {
- grouped.entry(merge_key(&f.form)).or_default().push(f);
- }
-
- let mut out = Vec::new();
- for (_, mut members) in grouped {
- // 事实多的在前,同数按字典序——规范 key 的选取不能依赖 HashMap 顺序
- members.sort_by(|a, b| b.fact_count.cmp(&a.fact_count).then(a.form.cmp(&b.form)));
- let mut docs: HashSet<Uuid> = HashSet::new();
- for m in &members {
- if let Some(d) = docs_of.get(&m.form) {
- docs.extend(d);
- }
- }
- if (docs.len() as i64) < MIN_DOCS {
- continue;
- }
- // 组里任一说法能落到本体已有关系上,整组就落过去。同组说法共享屈折基,
- // 结尾有没有 `by` 也必然一致(`produced_by` 与 `produces` 不同组),
- // 所以「要不要对调」是**整组一致**的,不必逐条判
- let existing = members.iter().find_map(|m| index.lookup(&m.form));
- out.push(RelationGroup {
- key: members[0].form.clone(),
- facts: members.iter().map(|m| m.fact_count).sum(),
- forms: members.into_iter().map(|m| m.form).collect(),
- docs: docs.len(),
- existing,
- });
- }
- fold_by_meaning(state, kb_id, &mut out).await;
- out.sort_by(|a, b| b.facts.cmp(&a.facts).then(a.key.cmp(&b.key)));
- Ok(out)
-}
-
-/// 拼写对不上、意思对得上的,也不新建(#560)。
-///
-/// `PredicateIndex` 只认写法、屈折与被动;`comprised` 对 `has_member`、`ceo` 对
-/// `chief_executive` 它看不出来,于是本体里长出第二个关系,图上同一件事两种谓词,
-/// 查询按谓词分组时它们各站一边。这里把没落地的说法嵌一次,与已有关系的向量比,
-/// 够近的就落到那一条上。阈值取得紧:折错一次是把两个关系永久并成一个,
-/// 而不折只是多一个关系,后者便宜得多。每个候选的距离都进日志,好在真语料上校
-const FOLD_DISTANCE: f32 = 0.20;
-
-async fn fold_by_meaning(state: &AppState, kb_id: Uuid, groups: &mut [RelationGroup]) {
- let unmatched: Vec<usize> = groups
- .iter()
- .enumerate()
- .filter(|(_, g)| g.existing.is_none())
- .map(|(i, _)| i)
- .collect();
- if unmatched.is_empty() {
- return;
- }
- let Ok(kb) = utopia_store::kbs::get(&state.pool, kb_id).await else {
- return;
- };
- let Ok(Some(settings)) = utopia_store::settings::get(&state.pool, kb.workspace_id).await else {
- return;
- };
- let Some(client) = crate::llm_util::embed_client(&settings) else {
- return;
- };
- let texts: Vec<String> = unmatched
- .iter()
- .map(|i| groups[*i].key.replace('_', " "))
- .collect();
- let vectors = match client.embed(&texts).await {
- Ok(v) if v.len() == texts.len() => v,
- Ok(_) => return,
- Err(e) => {
- tracing::warn!(%kb_id, error = %e, "说法嵌入失败,按拼写采纳");
- return;
- }
- };
- for (i, vec) in unmatched.into_iter().zip(vectors) {
- let near = utopia_store::ontology::nearest_relation_types(
- &state.pool,
- kb_id,
- &vec,
- 3,
- Some("relation"),
- )
- .await
- .unwrap_or_default();
- for c in &near {
- tracing::debug!(%kb_id, form = %groups[i].key, key = %c.key, distance = c.distance, "同义候选");
- }
- if let Some(hit) = fold_pick(&near, FOLD_DISTANCE) {
- tracing::info!(
- %kb_id, form = %groups[i].key, onto = %hit.key, distance = hit.distance,
- "说法按意思落到已有关系上"
- );
- groups[i].existing = Some((hit.id, false));
- }
- }
-}
-
-/// 最近的一个在阈值内就是它;候选按距离升序来
-fn fold_pick(
- near: &[utopia_core::models::TypeCandidate],
- max_distance: f32,
-) -> Option<&utopia_core::models::TypeCandidate> {
- near.iter()
- .min_by(|a, b| a.distance.total_cmp(&b.distance))
- .filter(|c| c.distance <= max_distance)
-}
-
-pub async fn bootstrap_ontology(state: &AppState, kb_id: Uuid) -> anyhow::Result<()> {
- // 并发的两个抽取任务可能都看到"空闲"而各入队一次;开关也可能刚被关掉
- let kb = utopia_store::kbs::get(&state.pool, kb_id).await?;
- if !kb.auto_extend_ontology {
- tracing::debug!(%kb_id, "自动扩本体已关闭,跳过");
- return Ok(());
- }
- // 门槛看的是"够不够一次 LLM 调用的量",**谓词与类型合起来算**。
- // 此前只数谓词,于是一个只缺实体类型、不缺关系的语料会被整个跳过——
- // proposed_type 里明明堆着 platform ×2、inference_engine ×2 在等。
- let forms: Vec<_> = utopia_store::graph::proposed_predicates(&state.pool, kb_id)
- .await?
- .into_iter()
- .filter(|f| f.doc_count >= MIN_DOCS)
- .collect();
- let types = utopia_store::resolution::proposed_types(&state.pool, kb_id).await?;
- /* **字面值那一档也要算进来。**
- `proposed_predicates` 只数宾语是实体的事实(它数的是"采纳要改写的东西"),
- 于是一个满是数额的语料在这里算出 predicates=0,整个自动扩本体被跳过,
- 下面那段建属性的代码一次都跑不到。实测:12 条带着 `valuation`、
- `investment_amount` 等说法的值事实在库里等着,日志里只有一行
- 「够格的信号太少」。它们同样是"够不够一次 LLM 调用的量"的信号 */
- let value_forms: Vec<_> = utopia_store::graph::proposed_attributes(&state.pool, kb_id)
- .await?
- .into_iter()
- .filter(|f| f.doc_count >= MIN_DOCS)
- .collect();
- if forms.len() + types.len() + value_forms.len() < MIN_SIGNALS {
- tracing::debug!(
- %kb_id, predicates = forms.len(), types = types.len(),
- values = value_forms.len(),
- "够格的信号太少,跳过自动扩本体"
- );
- return Ok(());
- }
-
- // **关系不问模型,按票数采纳。**
- //
- // 从前这一步把候选交给 LLM,让它回答"哪些值得建成关系"。那个问题数据已经
- // 答了——`runs_on` 出现在 8 篇文档、13 条事实里,不是判断题。而模型实测答错:
- // 它漏掉了 `runs_on`,却采纳了只在一篇里出现过的 `pledged_capital`。
- //
- // 换成计数还有一个副作用是关键的:**这一段变成确定性的**。同一份语料重跑得到
- // 同一个本体,测量台第一次能对它做对照。之前 B 与 B3 两组差 3 个百分点,
- // 到底是修复起了作用还是跑次方差,答不上来,就因为中间夹着一次 LLM 调用。
- //
- // 模型没有被撤走,只是换了个问题:见下方的同义归并——"这批新关系里哪些跟
- // 已有的是同一个意思"。那个才需要理解意义,且答错了 unadopt 一键回退。
- let counted = counted_relation_groups(state, kb_id).await?;
- let proposals = ontology_routes::build_proposals(state, kb_id, "en", MIN_DOCS).await?;
- let classes = proposals
- .get("entity_types")
- .and_then(|v| v.as_array())
- .cloned()
- .unwrap_or_default();
-
- let mut added_relations = Vec::new();
- let mut added_classes = Vec::new();
- let mut moved_total = 0u32;
- let mut left_off_total = 0u32;
- let mut batches = Vec::new();
-
- for p in &classes {
- let (Some(key), Some(label)) = (str_of(p, "key"), str_of(p, "label")) else {
- continue;
- };
- match utopia_store::ontology::create_entity_type(
- &state.pool,
- kb_id,
- key,
- label,
- utopia_store::palette::color_for_key(key),
- "circle",
- // 冷启动建的类不挂父:提案里没有层级信息,猜一个父类比不挂更糟
- &[],
- // 描述进抽取提示词,reason 只是给人看的理由——喂错了这个类就成新的倾倒场
- str_of(p, "description")
- .or_else(|| str_of(p, "reason"))
- .unwrap_or(""),
- )
- .await
- {
- Ok(type_id) => {
- added_classes.push(key.to_string());
- // 建类之后要把等它的实体搬过去——只建类型不动实体,
- // 本体长大了图没变好,那些提议过 model 的实体继续挂在 concept 下
- let forms: Vec<String> = p
- .get("forms")
- .and_then(|v| v.as_array())
- .map(|a| {
- a.iter()
- .filter_map(|s| s.as_str().map(String::from))
- .collect()
- })
- // 提案没给 forms 时,至少认领与 key 同名的那些提议
- .unwrap_or_else(|| vec![key.to_string()]);
- match utopia_store::resolution::adopt_proposed_types(
- &state.pool,
- kb_id,
- type_id,
- &forms,
- // 系统的动作,不是谁的决定——与本文件下方审计写 NULL 同一条
- None,
- )
- .await
- {
- Ok((batch, n)) => {
- moved_total += n;
- if n > 0 {
- batches.push(batch);
- }
- }
- Err(e) => tracing::warn!(%kb_id, key, error = %e, "实体改类失败"),
- }
- for form in &forms {
- let _ =
- utopia_store::ontology::clear_miss(&state.pool, kb_id, "entity_type", form)
- .await;
- }
- }
- // key 撞车之类的:跳过这一条,别带垮整批
- Err(e) => tracing::warn!(%kb_id, key, error = %e, "冷启动建类失败"),
- }
- }
-
- // 关系:按票数采纳,一组一个关系。
- //
- // key 与 label 都来自语料自己的措辞,description 留空——它进抽取提示词当语义
- // 指引,而这里没有可信的来源可写。编一句反而是往提示词里塞一个没人负责的断言,
- // 而 key 本身(`runs_on`、`available_on`)已经说清楚了。
- //
- /* **temporal 从提案里取,不再一律 state。**
- 这里原来写死 state,理由是「它只在 functional / inverse_functional 为真时
- 驱动时态引擎,而这条路永不自动设那两位」。那个理由**在 0031(#486)之后
- 已经不成立**:`Validity::under` 现在按谓词的 temporal 规整每一次写入,
- event 会把 `valid_to` 收到与 `valid_from` 同一刻。写死 state 的后果是
- `Meridian invested Kestrel 2025-05-09` 被记成「从那天起一直在投」,
- 时间轴上读出来就是这样——而它是一件发生过的事,不是一个持续的状态。
-
- 答案本来就在手边:提案那一步问模型要的 JSON 里就有
- `"temporal":"state|event|eternal"`,而下面的类、属性、map_to 都在用同一份
- 提案,唯独关系这一档把它丢了。**采纳与否仍旧只由票数决定**(0007),
- 模型只回答「这个关系是哪一种」——那是计数答不了的意义问题。 */
- let temporal_of = proposed_temporal(&proposals);
- for g in &counted {
- // 本体里已经有等价关系就不新建,直接把事实改写过去。
- // 被动形(`produced_by` 对上 `produces`)改写时主宾对调
- let (predicate_id, swap) = match g.existing {
- Some(hit) => hit,
- None => {
- let label = g.key.replace('_', " ");
- let temporal = temporal_of(&g.key, &g.forms);
- match utopia_store::ontology::create_relation_type(
- &state.pool,
- kb_id,
- &g.key,
- &label,
- temporal,
- // 冷启动不替人声明任何公理:推理机的判据必须是人写下来的
- Default::default(),
- "",
- "relation",
- &[],
- &[],
- None,
- None,
- )
- .await
- {
- Ok(id) => {
- added_relations.push(g.key.clone());
- (id, false)
- }
- Err(e) => {
- tracing::warn!(%kb_id, key = %g.key, error = %e, "冷启动建关系失败");
- continue;
- }
- }
- }
- };
- let utopia_store::graph::Adopted {
- batch_id: batch,
- moved,
- left_off,
- corrected,
- } = utopia_store::graph::adopt_proposed_predicates(
- &state.pool,
- kb_id,
- predicate_id,
- &g.forms,
- swap,
- )
- .await?;
- tracing::info!(
- %kb_id, key = %g.key, forms = ?g.forms, docs = g.docs, facts = g.facts, moved, left_off,
- corrected, reused = g.existing.is_some(), swap,
- temporal = temporal_of(&g.key, &g.forms),
- "按票数采纳关系"
- );
- moved_total += moved;
- left_off_total += left_off;
- if moved > 0 {
- batches.push(batch);
- }
- for form in &g.forms {
- let _ =
- utopia_store::ontology::clear_miss(&state.pool, kb_id, "relation_type", form).await;
- }
- }
-
- // 新建的关系要有向量:抽取按分块检索候选靠它,谓词对齐(#558)靠它,下一次
- // 同义归并也靠它。从前 `embed_ontology` 只在导入时跑一次,冷启动建的关系
- // 永远没有向量——这个库里 76 个,`comprised` 是其中之一
- if !added_relations.is_empty() {
- if let Err(e) = crate::ontology_index::refresh_scoped(
- state,
- kb_id,
- Some(utopia_store::ontology::TypeKind::Relation),
- )
- .await
- {
- tracing::warn!(%kb_id, error = %e, "新建关系的向量没补上");
- }
- }
- // 属性那一档:宾语是字面值的说法。
- //
- // domain 不从提案里读——它从这些事实的主语类型里取,见 adopt_attribute。
- // 值换不动的那些不改写,继续没有谓词,等下一次
- let attrs = proposals
- .get("attribute_types")
- .and_then(|v| v.as_array())
- .cloned()
- .unwrap_or_default();
- for p in &attrs {
- let (Some(key), Some(label)) = (str_of(p, "key"), str_of(p, "label")) else {
- continue;
- };
- let forms: Vec<String> = p
- .get("forms")
- .and_then(|v| v.as_array())
- .map(|a| {
- a.iter()
- .filter_map(|s| s.as_str().map(String::from))
- .collect()
- })
- .unwrap_or_default();
- if forms.is_empty() {
- continue;
- }
- match ontology_routes::adopt_attribute_auto(
- state,
- kb_id,
- key,
- label,
- str_of(p, "description")
- .or_else(|| str_of(p, "reason"))
- .unwrap_or(""),
- str_of(p, "datatype").unwrap_or("text"),
- str_of(p, "unit"),
- &forms,
- )
- .await
- {
- Ok((batch, moved)) => {
- added_relations.push(key.to_string());
- moved_total += moved;
- if moved > 0 {
- batches.push(batch);
- }
- }
- Err(e) => tracing::warn!(%kb_id, key, error = %e, "冷启动建属性失败"),
- }
- }
-
- // **映射到已有类型**:本体里已经有这个意思了,只改写事实、不动本体。
- //
- // 自动执行是安全的一档:它不让本体长大,只把一批 related_to 挂到一个
- // 早就存在的谓词上,而且跟新建那条路走同一个批次机制,同样可撤销。
- // 反过来说,漏掉这一档才是危险的——检索告诉模型"已经有 founding_date 了",
- // 模型答"这些说法就是它",我们却什么都不做,那批事实继续是"有关联"。
- let mapped = proposals
- .get("map_to")
- .and_then(|v| v.as_array())
- .cloned()
- .unwrap_or_default();
- for m in &mapped {
- let Some(key) = str_of(m, "key") else {
- continue;
- };
- let forms: Vec<String> = m
- .get("forms")
- .and_then(|v| v.as_array())
- .map(|a| {
- a.iter()
- .filter_map(|s| s.as_str().map(String::from))
- .collect()
- })
- .unwrap_or_default();
- if forms.is_empty() {
- continue;
- }
- // 目标是属性时改写走另一条路:值要按它的 datatype 换算。
- // kind 由服务端在解析提案时标上(模型只答得出一个 key)
- if str_of(m, "kind") == Some("attribute") {
- match ontology_routes::adopt_attribute_existing(state, kb_id, key, &forms).await {
- Ok((batch, moved)) => {
- moved_total += moved;
- if moved > 0 {
- batches.push(batch);
- }
- }
- Err(e) => tracing::warn!(%kb_id, key, error = %e, "映射到已有属性失败"),
- }
- continue;
- }
- // 模型偶尔会把候选清单之外的 key 抄进来(或者干脆编一个)。
- // 找不到就跳过——**不新建**:这条路的前提就是"它已经存在"
- let Some(predicate_id) =
- utopia_store::ontology::relation_type_id_by_key(&state.pool, kb_id, key).await?
- else {
- tracing::warn!(%kb_id, key, "映射目标不在本体里,跳过");
- continue;
- };
- // LLM 的 map_to 提案同样可能混着主动与被动,一个 swap 标志伺候不了
- //(同人工路径,见 ontology_routes 里那段注释)
- let utopia_store::graph::Adopted {
- batch_id: batch,
- moved,
- left_off,
- corrected,
- } = utopia_store::graph::adopt_proposed_predicates(
- &state.pool,
- kb_id,
- predicate_id,
- &forms,
- false,
- )
- .await?;
- tracing::info!(
- %kb_id, key, forms = ?forms, moved, left_off, corrected,
- "按映射提案采纳关系"
- );
- left_off_total += left_off;
- moved_total += moved;
- if moved > 0 {
- batches.push(batch);
- }
- for form in &forms {
- let _ =
- utopia_store::ontology::clear_miss(&state.pool, kb_id, "relation_type", form).await;
- }
- }
-
- // 类先建好、实体后抽出来是常态:把等着已存在类型的那些也收走
- match utopia_store::resolution::sweep_proposed_types(&state.pool, kb_id, None).await {
- Ok(swept) => {
- for (batch, n) in swept {
- moved_total += n;
- batches.push(batch);
- }
- }
- Err(e) => tracing::warn!(%kb_id, error = %e, "已有类型的实体收尾失败"),
- }
-
- if added_relations.is_empty() && added_classes.is_empty() && moved_total == 0 {
- return Ok(());
- }
- // actor 为 NULL:这是系统的动作,不是谁的决定。台账里查得到做了什么、
- // 改了多少条、以及撤销要用的批次号
- let _ = utopia_store::audit::record_opt(
- &state.pool,
- Some(kb_id),
- None,
- "ontology.bootstrapped",
- "kb",
- Some(kb_id),
- serde_json::json!({
- "relations": added_relations,
- "classes": added_classes,
- "facts_remapped": moved_total,
- // 签名两边都对不上、没挂上谓词的(#190)。不报这个数,「改写了 N 条」就是报喜不报忧
- "facts_left_off": left_off_total,
- "batches": batches,
- }),
- )
- .await;
- state.emit_review(kb_id);
- tracing::info!(
- %kb_id,
- relations = added_relations.len(),
- classes = added_classes.len(),
- facts = moved_total,
- "冷启动自动扩本体完成"
- );
- Ok(())
-}
-
-/// 从提案里查一个说法的 temporal,查不到给 `state`。
-///
-/// 按 key 查,再按 forms 查——计数那条路的规范 key 取的是组里事实最多的那个说法,
-/// 模型提案挑的未必是同一个(`acquires` 对 `acquired`),但它们同组,说的是一件事。
-///
-/// **查不到就 state**,与从前一致:这一档只在有明确答案时才偏离缺省,
-/// 而 state 是三者里最保守的——它不会像 event 那样把 `valid_to` 收成一个点。
-fn proposed_temporal(proposals: &serde_json::Value) -> impl Fn(&str, &[String]) -> &'static str {
- let mut by_word: HashMap<String, &'static str> = HashMap::new();
- if let Some(list) = proposals.get("relation_types").and_then(|v| v.as_array()) {
- for p in list {
- let t = match str_of(p, "temporal") {
- Some("event") => "event",
- Some("eternal") => "eternal",
- // 模型偶尔编第四个值;不认识的一律当没说
- _ => continue,
- };
- let mut words: Vec<String> = str_of(p, "key").map(str::to_string).into_iter().collect();
- if let Some(forms) = p.get("forms").and_then(|v| v.as_array()) {
- words.extend(forms.iter().filter_map(|f| f.as_str()).map(str::to_string));
- }
- for w in words {
- by_word.insert(w.to_lowercase(), t);
- }
- }
- }
- move |key: &str, forms: &[String]| {
- std::iter::once(key)
- .chain(forms.iter().map(String::as_str))
- .find_map(|w| by_word.get(&w.to_lowercase()).copied())
- .unwrap_or("state")
- }
-}
-
-fn str_of<'a>(v: &'a serde_json::Value, key: &str) -> Option<&'a str> {
- v.get(key)
- .and_then(|x| x.as_str())
- .map(str::trim)
- .filter(|s| !s.is_empty())
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use utopia_core::models::TypeCandidate;
-
- /// 一个关系是发生过一次,还是持续成立。
- ///
- /// 从前这里写死 `state`,于是「Meridian 2025-05-09 投了 Kestrel」被记成
- /// 从那天起一直在投。答案本来就在提案里,只是没人读。
- #[test]
- fn a_relation_takes_the_temporal_it_was_proposed_with() {
- let proposals = serde_json::json!({
- "relation_types": [
- { "key": "acquired", "temporal": "event", "forms": ["acquires", "acquisition_of"] },
- { "key": "capital_of", "temporal": "eternal", "forms": [] },
- { "key": "employs", "temporal": "state", "forms": ["employed_by"] },
- // 模型偶尔编一个第四值:当没说,落回缺省
- { "key": "sponsors", "temporal": "ongoing", "forms": [] },
- ]
- });
- let t = proposed_temporal(&proposals);
-
- assert_eq!(t("acquired", &[]), "event");
- // 规范 key 取的是组里事实最多的说法,未必是模型挑的那个——按 forms 也要查得到
- assert_eq!(t("acquisition_of", &[]), "event");
- assert_eq!(t("Acquires", &[]), "event", "大小写不该影响");
- assert_eq!(
- t("bought", &["acquires".into()]),
- "event",
- "本名查不到时看同组说法"
- );
- assert_eq!(t("capital_of", &[]), "eternal");
- assert_eq!(t("employs", &[]), "state");
-
- // **查不到一律 state**:三者里最保守的,不会像 event 那样把 valid_to 收成一个点
- assert_eq!(t("sponsors", &[]), "state", "不认识的值当没说");
- assert_eq!(t("never_proposed", &[]), "state");
- assert_eq!(
- proposed_temporal(&serde_json::json!({}))("anything", &[]),
- "state"
- );
- }
-
- fn cand(key: &str, distance: f32) -> TypeCandidate {
- TypeCandidate {
- id: Uuid::now_v7(),
- key: key.into(),
- label: key.into(),
- description: String::new(),
- kind: Some("relation".into()),
- distance,
- }
- }
-
- /// 只认阈值内最近的那个;差一点也不折——折错比不折贵
- #[test]
- fn a_form_folds_only_onto_a_close_enough_relation() {
- let near = vec![cand("has_member", 0.18), cand("member_of", 0.31)];
- assert_eq!(
- fold_pick(&near, 0.20).map(|c| c.key.as_str()),
- Some("has_member")
- );
- assert!(fold_pick(&near, 0.15).is_none());
- assert!(fold_pick(&[], 0.20).is_none());
- }
-}
diff --git a/crates/utopia-server/src/main.rs b/crates/utopia-server/src/main.rs
index d6305c6c..6b1ede67 100644
--- a/crates/utopia-server/src/main.rs
+++ b/crates/utopia-server/src/main.rs
@@ -3,7 +3,6 @@ mod alerting;
mod api;
mod auth;
mod blob;
-mod bootstrap_ontology;
mod client_ctx;
mod docs_corpus;
mod errata;
@@ -29,7 +28,6 @@ mod owl_import;
mod pack_alignment;
mod phrase_alignment;
mod pipeline;
-mod predicate_match;
mod query_engine;
mod rdf;
mod readers;
@@ -452,15 +450,6 @@ async fn dispatch(st: &state::AppState, job: &utopia_store::jobs::Job) -> anyhow
let id = payload_document_id(&job.payload)?;
extraction::extract_document(st, id, payload_proposer(&job.payload)).await
}
- "bootstrap_ontology" => {
- let kb_id: Uuid = job
- .payload
- .get("kb_id")
- .and_then(|v| v.as_str())
- .and_then(|s| s.parse().ok())
- .ok_or_else(|| anyhow::anyhow!("payload 缺少 kb_id"))?;
- bootstrap_ontology::bootstrap_ontology(st, kb_id).await
- }
// 本体向量索引:**后台建,不卡请求**。
// 一份 965 类的本体首次要嵌 2600 行,六到八分钟;放在
// 交互请求里就是导入完之后第一个用到检索的人干等
diff --git a/crates/utopia-server/src/predicate_match.rs b/crates/utopia-server/src/predicate_match.rs
deleted file mode 100644
index eb0e06ca..00000000
--- a/crates/utopia-server/src/predicate_match.rs
+++ /dev/null
@@ -1,442 +0,0 @@
-//! 把模型说出的谓词落到本体里**已经有**的关系上。
-//!
-//! 抽取原本只做精确 key 比对(`rel_ids.get(f.predicate)`),对不上就降级成
-//! `related_to`,原词留在 `fact_evidence.proposed_predicate` 等人来采纳。
-//!
-//! 实测这样漏得很凶。ai-timeline 语料(维基百科的 AI 公司条目)跑到一半时,
-//! 49.8% 的事实是 `related_to`,`ontology_misses` 里 398 个不同的关系名、895 次使用。
-//! 其中一类根本不是缺词汇,是**词汇就在本体里,模型只是说反了或换了时态**:
-//! `produced_by | ChatGPT → OpenAI` 想说的就是已有的 `produces`,主宾对调而已。
-//!
-//! 那个 49.8% 是**测量台把自动扩本体关掉之后**的数字(`run.mjs` 建库即置
-//! `auto_extend_ontology=FALSE`,而列默认值是 true)。产品的实际冷启动路径是
-//! `bootstrap_ontology` 事后补本体,所以别拿这个占比去说产品有多糟——
-//! 它量的是"缺词汇时降级有多频繁",而这正是本模块要减少的那一部分。
-//!
-//! 这里补三段,**顺序即优先级**,宽的永远排在窄的后面:
-//!
-//! 1. 精确 key —— 原有行为,一字不改
-//! 2. 写法对齐 —— `acquiredFrom` / `acquired_from` / `Acquired From` 是同一个
-//! 3. 屈折归一 —— `produced` 与 `produces` 折到同一串(只削时态与单复数,见 `inflect_base`)
-//!
-//! 2、3 各再试一次「去掉结尾的 by」,命中就**把主宾对调**:英语里 `_by` 是被动的
-//! 明确标记,`X produced_by Y` 与 `Y produces X` 是同一条边。
-//!
-//! **撞车就不匹配。** 本体里若同时有 `produces` 和 `produced`,两者折到同一串,
-//! 这时选谁都是猜——宁可降级,让人去采纳。只有精确 key 不受此限,它本来就唯一。
-//!
-//! **量到了什么**(同一份 895 次未匹配,两种词表):种子本体 10 个关系捞回 49 次;
-//! schema.org 629 个关系捞回 59 次。捞回的 9 个说法里 6 个正确、3 个方向可疑
-//!(`addresses→address`、`funds→funding`、`sponsors→sponsor`),各 1 次,
-//! **全部来自光削一个 `s` 那条路**——英语里 `sponsors` 既是动词第三人称也是名词复数,
-//! 靠后缀分不出来。没为这三条再加规则:样本太小,加了就是过拟合。
-//! 它们的反事实也不是「正确」,而是 `related_to`——原词仍在 proposed_predicate 里。
-//!
-//! 不做同义判断(`partners_with` 与 `collaborates_with`):那是检索与模型的活。
-//! 摆在这里会把「写法对齐」悄悄变成「意思大概差不多」,而后者错了没人看得见。
-
-use std::collections::HashMap;
-use utopia_core::models::RelationType;
-use uuid::Uuid;
-
-/// 切词:非字母数字处切,驼峰的大小写交界处也切。
-///
-/// 驼峰那一半不是可选的:OWL 导入的 key 就是 `acquiredFrom`,不切的话它跟手写的
-/// `acquired_from` 折不到一起,而「导入的本体能不能被用上」正是这条路要保的。
-fn words(s: &str) -> Vec<String> {
- let mut out = Vec::new();
- let mut cur = String::new();
- let mut prev_is_lower_or_digit = false;
- for c in s.chars() {
- if !c.is_alphanumeric() {
- if !cur.is_empty() {
- out.push(std::mem::take(&mut cur));
- }
- prev_is_lower_or_digit = false;
- continue;
- }
- if c.is_uppercase() && prev_is_lower_or_digit && !cur.is_empty() {
- out.push(std::mem::take(&mut cur));
- }
- prev_is_lower_or_digit = c.is_lowercase() || c.is_numeric();
- cur.extend(c.to_lowercase());
- }
- if !cur.is_empty() {
- out.push(cur);
- }
- out
-}
-
-/// 写法对齐形:切完的词直接拼起来,分隔符不留。与
-/// `api::ontology_routes::normalize_name` 同规则。
-fn joined(words: &[String]) -> String {
- words.concat()
-}
-
-/// 屈折归一:削掉时态与单复数,**不碰派生后缀**。
-///
-/// 这里一度用的是 Snowball(`rust-stemmers`),实测反了:词表 10 个词时捞回 49 次,
-/// 换成 schema.org 的 629 个词只剩 18 次。因为 Snowball 连派生后缀一起削,
-/// `producer` 与 `produces` 都成了 `produc`,撞车规则于是拒绝匹配——**词表越大越不敢动**。
-/// 而 `producer`(一个人)和 `produces`(一个动作)本来就该是两个关系,
-/// 折到一起是词干器的错,不是撞车规则的错。
-///
-/// 所以只做屈折:`-ies→y`、`-ing`、`-ed`、`-s`(`-ss` 除外),削完再去掉结尾的 `e`。
-/// 最后那一步是为了让 `-s` 和 `-ed` 两条路汇合——`produces→produce→produc`、
-/// `produced→produc`,否则同一个动词的两种时态永远对不上。
-///
-/// 长度守卫看的是**削完之后**的结果,不是中间结果。按中间结果卡会让两条路
-/// 走岔:`uses` 削 `-s` 剩三个字母过关、再掉个 `e` 变两个,而 `used` 削 `-ed`
-/// 当场就是两个、被拦下——同一个动词的两种时态于是永远对不上。
-/// 剩不到两个字母就整个不削(`is` 不该变成 `i`,`led` 不该变成 `l`)。
-///
-/// 中文没有屈折后缀,走这里是恒等变换,所以不必按 `ontology_lang` 分叉。
-fn inflect_base(w: &str) -> String {
- let n = w.len();
- let mut s = if w.ends_with("ies") && n > 3 {
- format!("{}y", &w[..n - 3])
- } else if w.ends_with("ing") && n > 3 {
- w[..n - 3].to_string()
- } else if w.ends_with("ed") && n > 2 {
- w[..n - 2].to_string()
- } else if w.ends_with('s') && !w.ends_with("ss") && n > 1 {
- w[..n - 1].to_string()
- } else {
- w.to_string()
- };
- // 结尾的 e 一律去掉:`produces→produce` 与 `produced→produc` 靠这一步汇合,
- // 顺带让 `note` 与 `notes` 落到一起
- if s.ends_with('e') {
- s.pop();
- }
- if s.chars().count() < 2 {
- return w.to_string();
- }
- s
-}
-
-/// 领头的轻动词:`has_funding` 与 `funding` 是同一个关系,前缀是命名习惯不是意思。
-///
-/// 实测(ai-timeline-ends × schema.org):空谓词事实的原文说法里
-/// `has_funding` ×4 落空,而本体里就有 `funding`;`product` ×2 落空,而本体里有
-/// `has_product`——**差的只是这一个前缀**。
-///
-/// 只在还剩词的时候剥:`has` 单独一个词是它自己,不能剥成空。
-/// 过度归并不会造成错配——`insert` 遇到一个键落到两个关系上就作废(撞车即作废),
-/// 所以最坏情况是退回"匹配不上",而不是匹配到错的那个。
-const LEADING_AUX: &[&str] = &[
- "has", "have", "had", "is", "are", "was", "were", "be", "been",
-];
-
-fn stems(words: &[String]) -> Vec<String> {
- let words = match words.split_first() {
- Some((head, rest)) if !rest.is_empty() && LEADING_AUX.contains(&head.as_str()) => rest,
- _ => words,
- };
- words.iter().map(|w| inflect_base(w)).collect()
-}
-
-/// 一个说法的**归并键**:同一个关系的不同时态落到同一个键上。
-///
-/// 给本体采纳那条路用的。抽取时说法要跟**已有**关系比对(上面那套三段匹配),
-/// 采纳时要做的是另一件事——把彼此之间是同一个意思的说法先并起来再算票数。
-///
-/// 不并就要吃亏,而且吃得见:ai-timeline 跑完后还压在兜底谓词上的说法里,
-/// `sued` 与 `sues` 是两条(各 11 和 5)、`integrated_with` 与 `integrates_with`
-/// 是两条(各 5)、`announced`/`announces`、`supported`/`supports`、
-/// `developed`/`develops` 都是。各自算票,各自够不着「出现在 ≥2 篇」的门槛;
-/// 并起来之后够格的候选从 70 组涨到 85 组、281 条涨到 355 条。
-///
-/// **介词不并**:`integrated_with` 与 `integrated_into` 保持两组。`works_at` 与
-/// `works_in` 确实可能是两回事,这里宁可漏。
-///
-/// **`_by` 也不并**(待做):`founded_by` 与 `founded` 是同一条边的两个方向。
-///
-/// 当初不并的理由是「采纳路径从旧行原样复制主语,对调不了」——**那个理由已经
-/// 不成立**(#109 让 `adopt` 显式绑定主语并支持交换)。现在缺口只剩一处:
-/// 两边都还不在本体里时(`founded_by` 42 条、`founded` 4 条,都够票),
-/// 采纳前的 `PredicateIndex` 查询谁也匹配不上,于是各建一个、方向相反。
-/// 补法是把 `_by` 折进同一组并把整组标成需对调,不再有阻碍。
-pub fn merge_key(form: &str) -> Vec<String> {
- stems(&words(form))
-}
-
-/// 撞车即作废:同一个形式落到两个不同的关系上,选谁都是猜。
-fn insert<K: std::hash::Hash + Eq>(map: &mut HashMap<K, Option<Uuid>>, key: K, id: Uuid) {
- map.entry(key)
- .and_modify(|slot| {
- if *slot != Some(id) {
- *slot = None;
- }
- })
- .or_insert(Some(id));
-}
-
-pub struct PredicateIndex {
- exact: HashMap<String, Uuid>,
- by_joined: HashMap<String, Option<Uuid>>,
- by_stems: HashMap<Vec<String>, Option<Uuid>>,
-}
-
-impl PredicateIndex {
- /// **只收 `kind == "relation"`。** 属性走字面值通道,模糊匹配跨过去,
- /// `founding_date` 就会变成一条指向实体「2015」的边——那正是本体采纳那条路
- /// 已经专门挡掉的东西,这里不能从后门放回来。
- pub fn build(rtypes: &[RelationType]) -> Self {
- let mut exact = HashMap::new();
- let mut by_joined = HashMap::new();
- let mut by_stems = HashMap::new();
- for r in rtypes.iter().filter(|r| r.kind == "relation") {
- exact.insert(r.key.clone(), r.id);
- let w = words(&r.key);
- if w.is_empty() {
- continue;
- }
- insert(&mut by_joined, joined(&w), r.id);
- insert(&mut by_stems, stems(&w), r.id);
- }
- Self {
- exact,
- by_joined,
- by_stems,
- }
- }
-
- fn widened(&self, w: &[String]) -> Option<Uuid> {
- if w.is_empty() {
- return None;
- }
- if let Some(hit) = self.by_joined.get(&joined(w)) {
- return *hit;
- }
- *self.by_stems.get(&stems(w))?
- }
-
- /// 返回 `(关系 id, 主宾是否要对调)`。`None` = 本体里确实没有,该降级。
- pub fn lookup(&self, proposed: &str) -> Option<(Uuid, bool)> {
- if let Some(id) = self.exact.get(proposed) {
- return Some((*id, false));
- }
- let w = words(proposed);
- if let Some(id) = self.widened(&w) {
- return Some((id, false));
- }
- // 被动形:`produced_by` 去掉 by 之后才对得上 `produces`,且主宾要反过来。
- // 要求至少两个词——光一个 `by` 削完是空的。
- if w.len() >= 2 && w[w.len() - 1] == "by" {
- if let Some(id) = self.widened(&w[..w.len() - 1]) {
- return Some((id, true));
- }
- }
- None
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- /// `has_funding` 与 `funding` 是同一个关系,前缀是命名习惯不是意思。
- ///
- /// 实测里这两组各自落空:本体有 `funding`、模型写 `has_funding`(×4);
- /// 本体有 `has_product`、模型写 `product`(×2)。差的只是这一个前缀。
- #[test]
- fn a_leading_auxiliary_does_not_make_a_different_relation() {
- let rels = vec![rel("funding"), rel("has_product")];
- let idx = PredicateIndex::build(&rels);
- assert!(
- idx.lookup("has_funding").is_some(),
- "has_funding 该落到 funding 上"
- );
- assert!(
- idx.lookup("product").is_some(),
- "product 该落到 has_product 上"
- );
- // 两个方向都要通
- assert!(idx.lookup("funding").is_some());
- assert!(idx.lookup("has_product").is_some());
- }
-
- /// **只在还剩词的时候剥。** `has` 单独一个词是它自己,剥成空就什么都匹配了。
- #[test]
- fn a_bare_auxiliary_is_still_a_word() {
- let rels = vec![rel("has")];
- let idx = PredicateIndex::build(&rels);
- assert!(idx.lookup("has").is_some(), "has 自己该匹配得上");
- assert!(idx.lookup("owns").is_none(), "剥成空会让不相干的词也匹配上");
- }
-
- /// 撞车仍然作废:本体同时有 `funding` 与 `has_funding` 时,选谁都是猜。
- /// 过度归并的最坏结果是「匹配不上」,不是「匹配到错的那个」。
- #[test]
- fn folding_the_prefix_never_produces_a_wrong_match() {
- let rels = vec![rel("funding"), rel("has_funding")];
- let idx = PredicateIndex::build(&rels);
- // 精确 key 仍然直达
- assert!(idx.lookup("funding").is_some());
- assert!(idx.lookup("has_funding").is_some());
- // 撞车在**词干**那一层:`funding` 与 `has_funding` 剥掉前缀后都是 ["funding"]。
- // 要测它就得给一个走不到写法对齐、只能落到词干的形式——
- // `has_fundings` 拼起来是 hasfundings,本体里没有,于是往下走到词干,撞车作废
- assert!(
- idx.lookup("has_fundings").is_none(),
- "词干层撞车了却还是选了一个"
- );
- }
-
- fn rel(key: &str) -> RelationType {
- RelationType {
- id: Uuid::new_v4(),
- kb_id: Uuid::nil(),
- key: key.to_string(),
- label: key.to_string(),
- temporal: "state".into(),
- functional: false,
- inverse_functional: false,
- builtin: false,
- description: String::new(),
- iri: None,
- kind: "relation".into(),
- domains: Vec::new(),
- ranges: Vec::new(),
- datatype: None,
- unit: None,
- qualifiers: vec![],
- }
- }
-
- fn attr(key: &str) -> RelationType {
- RelationType {
- kind: "attribute".into(),
- ..rel(key)
- }
- }
-
- #[test]
- fn splits_camel_case_and_separators_the_same_way() {
- assert_eq!(words("acquiredFrom"), ["acquired", "from"]);
- assert_eq!(words("acquired_from"), ["acquired", "from"]);
- assert_eq!(words("Acquired From"), ["acquired", "from"]);
- // 全大写不是驼峰边界,别把 IRI 切成 i/r/i
- assert_eq!(words("IRI"), ["iri"]);
- assert_eq!(words("gpt4Model"), ["gpt4", "model"]);
- }
-
- #[test]
- fn exact_key_still_wins_unchanged() {
- let types = [rel("produces")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("produces"), Some((types[0].id, false)));
- }
-
- #[test]
- fn separator_and_case_differences_align() {
- let types = [rel("acquired_from")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("acquiredFrom"), Some((types[0].id, false)));
- assert_eq!(idx.lookup("Acquired From"), Some((types[0].id, false)));
- }
-
- #[test]
- fn tense_folds_without_swapping() {
- let types = [rel("produces")];
- let idx = PredicateIndex::build(&types);
- // 模型写过去式,说的还是同一条边,方向也没变
- assert_eq!(idx.lookup("produced"), Some((types[0].id, false)));
- }
-
- /// 这条是这个模块存在的理由:`ChatGPT produced_by OpenAI` 与
- /// `OpenAI produces ChatGPT` 是同一条边,只差主宾方向。
- #[test]
- fn passive_form_matches_and_asks_for_a_swap() {
- let types = [rel("produces")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("produced_by"), Some((types[0].id, true)));
- assert_eq!(idx.lookup("producedBy"), Some((types[0].id, true)));
- }
-
- #[test]
- fn multi_word_passive_matches() {
- let types = [rel("invests_in")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("invested_in"), Some((types[0].id, false)));
- }
-
- /// **派生不是屈折。** 这条是换掉 Snowball 的理由:schema.org 里 `producer`
- /// 与 `produces` 并存,词干器把两个都折成 `produc`,于是撞车规则把
- /// `produced` 也一并拒了——词表越大捞得越少(实测 49 次掉到 18 次)。
- /// 只削屈折后缀就不会有这个撞车:`producer` 保持原样。
- #[test]
- fn derivational_forms_stay_separate_from_inflected_ones() {
- let types = [rel("produces"), rel("producer"), rel("production_company")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("produced"), Some((types[0].id, false)));
- assert_eq!(idx.lookup("produced_by"), Some((types[0].id, true)));
- assert_eq!(idx.lookup("producer"), Some((types[1].id, false)));
- // 派生词之间也别互相串
- assert_eq!(idx.lookup("producers"), Some((types[1].id, false)));
- }
-
- /// 单复数与时态两条路要汇合到同一串,否则同一个动词的两种写法永远对不上。
- #[test]
- fn plural_and_past_forms_meet_at_the_same_base() {
- assert_eq!(inflect_base("produces"), inflect_base("produced"));
- assert_eq!(inflect_base("uses"), inflect_base("used"));
- assert_eq!(inflect_base("notes"), inflect_base("note"));
- assert_eq!(inflect_base("studies"), inflect_base("study"));
- // 短词不削:is 不该变成 i
- assert_eq!(inflect_base("is"), "is");
- // -ss 不是复数
- assert_eq!(inflect_base("address"), inflect_base("addresses"));
- }
-
- /// 撞车宁可不匹配:本体里同时有 `produces` 和 `produced` 时,
- /// `producing` 折到两者共同的词干上,选谁都是猜。
- #[test]
- fn ambiguous_stem_declines_rather_than_guesses() {
- let types = [rel("produces"), rel("produced")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("producing"), None);
- // 但精确 key 不受影响,它本来就唯一
- assert_eq!(idx.lookup("produces"), Some((types[0].id, false)));
- assert_eq!(idx.lookup("produced"), Some((types[1].id, false)));
- }
-
- /// 属性不能从这条路被匹配上:宾语是字面值,配不出一条边。
- #[test]
- fn attributes_are_not_reachable() {
- let types = [attr("founding_date")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("founding_date"), None);
- assert_eq!(idx.lookup("foundingDate"), None);
- }
-
- /// 本体里真没有的,照旧降级——这里不做同义判断。
- #[test]
- fn genuinely_missing_vocabulary_still_declines() {
- let types = [rel("produces"), rel("works_at")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("partners_with"), None);
- assert_eq!(idx.lookup("acquired"), None);
- // 介词不同就是不同的关系,别替模型改口
- assert_eq!(idx.lookup("works_in"), None);
- }
-
- #[test]
- fn lone_by_does_not_strip_to_nothing() {
- let types = [rel("produces")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("by"), None);
- assert_eq!(idx.lookup("_by_"), None);
- }
-
- /// 中文 key 走词干器是恒等变换,不该被削也不该错配。
- #[test]
- fn chinese_keys_pass_through() {
- let types = [rel("隶属于"), rel("生产")];
- let idx = PredicateIndex::build(&types);
- assert_eq!(idx.lookup("隶属于"), Some((types[0].id, false)));
- assert_eq!(idx.lookup("生产"), Some((types[1].id, false)));
- assert_eq!(idx.lookup("收购"), None);
- }
-}
diff --git a/crates/utopia-store/src/access.rs b/crates/utopia-store/src/access.rs
index bfa4baaa..f7fd7be3 100644
--- a/crates/utopia-store/src/access.rs
+++ b/crates/utopia-store/src/access.rs
@@ -265,16 +265,3 @@ pub async fn ensure_jwt_secret(pool: &PgPool, generated: &str) -> AppResult<Stri
))
})
}
-
-/// 本体铺进抽取提示词的字符预算。超了改成按分块检索候选。
-///
-/// **放设置而不是常量**,是因为定死它需要一条曲线:每个本体规模下,全量内联
-/// 与按块检索各测一次,看它们在哪里交叉。要重启一次服务才测得了一档的话,
-/// 那条曲线不会有人跑第二遍。
-pub async fn ontology_prompt_budget(pool: &PgPool) -> AppResult<usize> {
- let row: Option<(i32,)> =
- sqlx::query_as("SELECT ontology_prompt_budget FROM deployment_settings LIMIT 1")
- .fetch_optional(pool)
- .await?;
- Ok(row.map(|(v,)| v.max(0) as usize).unwrap_or(24_000))
-}
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index d2bf3d82..7425fad2 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::collections::HashSet;
use utopia_core::models::{
ChunkFactView, EntityFact, EntityHistoryEvent, EntityType, EvidenceView, FactQualifier,
- FactReviewItem, GraphChange, GraphEdge, GraphNode, ProposedPredicate, RelationType,
+ FactReviewItem, GraphChange, GraphEdge, GraphNode, RelationType,
};
use utopia_core::{AppError, AppResult};
use uuid::Uuid;
@@ -18,12 +18,6 @@ type FactSpanRow = (
Option<String>,
);
-/// 采纳时旧事实的去向(`fact_adoptions.mode`):新写一行取代它。
-const ADOPT_SUPERSEDED: &str = "superseded";
-/// 目标断言已存在 → 并进去。旧行被作废且没有后继,实体历史必须据此把它
-/// 读成"并入"而不是"撤回",否则界面会宣称一件没发生的事。
-const ADOPT_MERGED: &str = "merged";
-
// 建库不再播种任何关系,也不再有 `ensure_default_ontology`。
//
// 这里曾经有十条种子关系、一张中文措辞表、一个按语言取措辞的 `localized`,
@@ -2492,566 +2486,6 @@ pub async fn graph_changes(
.await?)
}
-// ---------------------------------------------------------------------------
-// 谓词消解:把没有谓词的事实认领回本体
-// ---------------------------------------------------------------------------
-
-// 视图类型 ProposedPredicate 定义在 utopia-core::models(store 不直接依赖 serde)
-
-/// 没有谓词的事实上,原文用过哪些说法。
-///
-/// 这是本体扩展建议的证据基础——比 `ontology_misses` 的纯计数强的地方在于:
-/// 它连着具体事实,所以采纳一个说法时能直接说"将重新归类 57 条"并真的去改。
-pub async fn proposed_predicates(pool: &PgPool, kb_id: Uuid) -> AppResult<Vec<ProposedPredicate>> {
- Ok(sqlx::query_as(
- // **普遍程度从全量证据里数,不从积压里数。**
- //
- // 下面那些 WHERE 把行集收窄到「还没有谓词、还活着、宾语是实体」
- // ——那是**采纳要改写的东西**,`fact_count` 该这么数。但 `doc_count`
- // 回答的是另一个问题:这个说法在语料里有多普遍。拿残渣去数它会系统性
- // 偏低,而且越用越低——说法一旦被采纳、被谓词匹配接住、或被修正作废,
- // 它的行就离开积压了。一篇一篇往里灌的库尤其吃亏:每轮搬走一批,
- // 剩下的永远攒不够两篇,本体于是永远长不起来。
- //
- // 实测(ai-timeline 348 块):两种口径下 8 个说法分处门槛两侧,
- // 按积压数是「只在 1 篇」、按全量数是「≥2 篇」。
- //
- // 走 CTE 而不是相关子查询:后者每组重扫一遍证据表,同一份数据上
- // 360ms 对 7ms。这个函数每次 Suggest 和每次自动扩本体都要跑。
- // **只看类型化那一层。** 开放陈述(0044)的短语也写在证据的 proposed_predicate
- // 上,好让读路径按它显示;但它不是等着被采纳的说法——把一个开放短语自动
- // 采纳成关系类型,正是那一刀说不做的事(对齐与绑定是第二刀)
- "WITH spread AS (
- SELECT e.proposed_predicate AS form,
- count(DISTINCT e.document_id) AS doc_count
- FROM fact_evidence e
- JOIN facts ff ON ff.id = e.fact_id
- WHERE ff.kb_id = $1 AND ff.layer = 'typed' AND e.proposed_predicate IS NOT NULL
- GROUP BY 1
- )
- SELECT fe.proposed_predicate AS form,
- count(DISTINCT f.id) AS fact_count,
- max(sp.doc_count) AS doc_count,
- (SELECT s.canonical_name || ' → ' || o.canonical_name
- FROM fact_evidence e2
- JOIN facts f2 ON f2.id = e2.fact_id
- JOIN entities s ON s.id = f2.subject_id
- JOIN entities o ON o.id = f2.object_id
- WHERE e2.proposed_predicate = fe.proposed_predicate
- AND f2.kb_id = $1 AND f2.layer = 'typed'
- AND f2.predicate_id IS NULL AND f2.invalidated_at IS NULL
- LIMIT 1) AS example
- FROM fact_evidence fe
- JOIN facts f ON f.id = fe.fact_id
- JOIN spread sp ON sp.form = fe.proposed_predicate
- WHERE f.kb_id = $1 AND f.layer = 'typed' AND f.predicate_id IS NULL
- AND f.invalidated_at IS NULL AND fe.proposed_predicate IS NOT NULL
- -- 字面值宾语的不算:它们同样没有谓词、也带原文说法,但要的是
- -- 一个属性而不是一个关系。混进来提案就会照着建关系,然后
- -- `founding_date` 变成一条指向「2015」的边——正是这条路要修掉的东西
- AND f.object_id IS NOT NULL
- -- 用户拒绝过的说法不再出现在候选里(人工与自动两条路都据此绕开)
- AND NOT EXISTS (SELECT 1 FROM ontology_misses m
- WHERE m.kb_id = $1 AND m.kind = 'relation_type'
- AND m.key = fe.proposed_predicate AND m.dismissed_at IS NOT NULL)
- GROUP BY fe.proposed_predicate
- ORDER BY fact_count DESC, form",
- )
- .bind(kb_id)
- .fetch_all(pool)
- .await?)
-}
-
-/// 每个待认领说法出现在**哪些文档**里。
-///
-/// [`proposed_predicates`] 已经给了 `doc_count`,但采纳那条路要先按屈折基把说法
-/// 归并(`sued` 与 `sues` 是一个关系),归并之后的篇数是**并集**而不是相加——
-/// 同一篇文档完全可能两种写法都用过,相加就成了重复计数,一篇文档能把一个说法
-/// 顶过「≥2 篇」的门槛。
-///
-/// **不筛兜底谓词。** 这条查询与 [`proposed_predicates`] 回答的是两个问题:
-/// 那条问「还有哪些说法等着被采纳」,看的是积压;这条问「这个说法有多普遍」,
-/// 看的是全量证据,条件与它内部那个 `spread` CTE 一致。
-///
-/// 第一版照抄了 `rt.key = 'related_to'`(当时还有那个兜底关系),理由写的是「两处条件要一致」——错的。
-/// 那样数出来的还是残渣:说法一旦被采纳、被谓词匹配接住、或被修正作废,
-/// 它的行就离开积压,篇数随之下降。一篇一篇往里灌的库因此永远攒不够两篇。
-/// 测试当场抓住了(两篇里只回来一篇)。
-pub async fn proposed_predicate_documents(
- pool: &PgPool,
- kb_id: Uuid,
-) -> AppResult<Vec<(String, Uuid)>> {
- Ok(sqlx::query_as(
- "SELECT DISTINCT fe.proposed_predicate, fe.document_id
- FROM fact_evidence fe
- JOIN facts f ON f.id = fe.fact_id
- WHERE f.kb_id = $1 AND f.layer = 'typed'
- AND fe.proposed_predicate IS NOT NULL
- AND fe.document_id IS NOT NULL
- AND NOT EXISTS (SELECT 1 FROM ontology_misses m
- WHERE m.kb_id = $1 AND m.kind = 'relation_type'
- AND m.key = fe.proposed_predicate AND m.dismissed_at IS NOT NULL)",
- )
- .bind(kb_id)
- .fetch_all(pool)
- .await?)
-}
-
-/// 把由 `forms` 认出来的无谓词事实改写到 `predicate_id`。
-/// 返回 (批次 id, 改写条数)——批次 id 是撤销的把手。
-///
-/// **追加而非原地改**:插入带 `supersedes` 的新行并作废旧行,与人工纠正、
-/// 时态闭合走同一条路——认知变更本身是信息,实体历史里读得到
-/// "先记成 related to,后精化成 available on"。
-///
-/// 只改写说法**全部**落在 `forms` 内的事实:一条事实可能积累多种说法
-/// (甲块 "runs on"、乙块 "optimized for"),只认领了其中一种就改写等于替
-/// 另一种也做了决定。实测这类事实占比不到 1%,宁可漏也不猜。
-///
-/// 每条去向都写进 `fact_adoptions`。`supersedes` 一个指针不够用——目标断言
-/// 已存在时走的是"并入",旧行被作废却没有后继,于是既撤不回来、实体历史
-/// 又会把它判成 rejected 而对外宣称"这条被撤回了"(它其实一字未少地并进了
-/// 另一条)。
-/// `swap = true`:这些说法是目标关系的**被动形**,改写时主宾要对调。
-///
-/// `X produced_by Y` 与 `Y produces X` 是同一条边。不对调就会在图上多出一条
-/// 反着的箭头,而且它跟正向那些永远合不到一起——同一件事分在两个方向上。
-pub async fn adopt_proposed_predicates(
- pool: &PgPool,
- kb_id: Uuid,
- predicate_id: Uuid,
- forms: &[String],
- swap: bool,
-) -> AppResult<Adopted> {
- adopt(pool, kb_id, predicate_id, AdoptTargets::ByForm(forms), swap).await
-}
-
-/// 一次采纳的账。`left_off` 必须往上传:改写了 20 条、留下 3 条,只报前半句是报喜不报忧。
-#[derive(Debug, Clone, Copy)]
-pub struct Adopted {
- pub batch_id: Uuid,
- /// 改写过去的条数
- pub moved: u32,
- /// 签名两边都对不上、**没有**挂上谓词的条数(#190)。它们照旧留在空谓词上,
- /// 原文说法还在证据里——与抽取遇到同样情形的处置一致
- pub left_off: u32,
- /// 按签名对调了主宾的条数。
- ///
- /// **掰正不该静默。** 抽取那条路每掰一次就落一条 `direction_corrected`
- /// 丢弃信号(#138 的原话是「绝不静默」:用可能错的声明驱动的自动动作,
- /// 留痕才不属于 0001 判据 2 反对的那一类)。采纳走的是同一道判断,
- /// 也该说出来自己动了几条,否则台账上只剩「改写了 N 条」,看不出其中
- /// 有几条是被本体掉了个头的
- pub corrected: u32,
-}
-
-/// 要改写哪些事实,以及新行的宾语从哪来。
-pub enum AdoptTargets<'a> {
- /// 关系那一路:按表层说法去找,宾语原样搬走。
- ByForm(&'a [String]),
- /// 属性那一路:调用方已经挑好事实、并把值按 datatype 归一化过。
- ///
- /// **归一化必须在调用方做**:那套规则("2015" → 日期、"1,200" → 数字)
- /// 住在抽取模块,store 够不着也不该够得着。更要紧的是它会**失败**——
- /// 一个换算不出来的值不该硬塞进一个日期属性里,那条事实宁可继续没有
- /// 谓词(原词还在证据里)。所以由调用方筛完再交回来。
- WithValues(&'a [(Uuid, serde_json::Value)]),
-}
-
-async fn adopt(
- pool: &PgPool,
- kb_id: Uuid,
- predicate_id: Uuid,
- targets: AdoptTargets<'_>,
- swap: bool,
-) -> AppResult<Adopted> {
- let batch_id = Uuid::now_v7();
- let nothing = Adopted {
- batch_id,
- moved: 0,
- left_off: 0,
- corrected: 0,
- };
- let targets: Vec<(Uuid, Uuid, Option<Uuid>, Option<serde_json::Value>)> = match targets {
- AdoptTargets::ByForm(forms) => {
- if forms.is_empty() {
- return Ok(nothing);
- }
- sqlx::query_as(
- "SELECT f.id, f.subject_id, f.object_id, f.object_value
- FROM facts f
- WHERE f.kb_id = $1 AND f.predicate_id IS NULL AND f.invalidated_at IS NULL
- -- 开放陈述(0044)不采纳:短语挂上谓词是第二刀的对齐做的事
- AND f.layer = 'typed'
- -- **只碰宾语是实体的。** 同一个说法可能既有指向实体的事实
- -- 又有带字面值的(location 两种都用),后者归属性那条路:
- -- 把它改挂到一条关系上,那个值就再也不是值了
- AND f.object_id IS NOT NULL
- AND EXISTS (SELECT 1 FROM fact_evidence e
- WHERE e.fact_id = f.id AND e.proposed_predicate = ANY($2))
- AND NOT EXISTS (SELECT 1 FROM fact_evidence e
- WHERE e.fact_id = f.id AND e.proposed_predicate IS NOT NULL
- AND NOT (e.proposed_predicate = ANY($2)))
- ORDER BY f.recorded_at",
- )
- .bind(kb_id)
- .bind(forms)
- .fetch_all(pool)
- .await?
- }
- AdoptTargets::WithValues(items) => {
- if items.is_empty() {
- return Ok(nothing);
- }
- // 主语要从库里读回来(调用方给的是 fact_id 与新值),顺带确认这些
- // 事实还活着——挑选与采纳之间可能隔着一次重抽
- let ids: Vec<Uuid> = items.iter().map(|(id, _)| *id).collect();
- let live: Vec<(Uuid, Uuid)> = sqlx::query_as(
- "SELECT id, subject_id FROM facts
- WHERE kb_id = $1 AND id = ANY($2) AND invalidated_at IS NULL
- AND layer = 'typed'",
- )
- .bind(kb_id)
- .bind(&ids)
- .fetch_all(pool)
- .await?;
- let subject_of: std::collections::HashMap<Uuid, Uuid> = live.into_iter().collect();
- items
- .iter()
- .filter_map(|(id, value)| {
- Some((*id, *subject_of.get(id)?, None, Some(value.clone())))
- })
- .collect()
- }
- };
-
- let mut moved = 0u32;
- let mut left_off = 0u32;
- let mut corrected = 0u32;
- for (old_id, subject_id, object_id, object_value) in targets {
- // 被动形改写:主宾对调。字面值宾语的事实换不了(值不能当主语),
- // 而 ByForm 那条查询本来就只取 object_id 非空的,所以这里只可能是实体宾语
- let (subject_id, object_id) = match (swap, object_id) {
- (true, Some(o)) => (o, Some(subject_id)),
- _ => (subject_id, object_id),
- };
- // **挂谓词之前过一遍签名**(#190)。抽取写入时按 domain 掰正方向或留空,
- // 而采纳是第二条写谓词的路——从前直接把谓词挂回去,实测把违反率从 0 抬到
- // 12.3%,全在包关系上。同一道判断(`ontology::judge_direction`),同样三种
- // 结果:合就挂,主语不合宾语合就对调着挂,都不合就**不挂**——那条事实留在
- // 空谓词上,原文说法还在证据里,与抽取遇到同样情形的处置一致
- let (subject_id, object_id) = match object_id {
- Some(o) => {
- match crate::ontology::judge_direction(pool, predicate_id, subject_id, o).await? {
- crate::ontology::Fit::Swap => {
- corrected += 1;
- (o, Some(subject_id))
- }
- crate::ontology::Fit::Neither => {
- left_off += 1;
- continue;
- }
- crate::ontology::Fit::Keep | crate::ontology::Fit::Unchecked => {
- (subject_id, Some(o))
- }
- }
- }
- None => (subject_id, None),
- };
- let mut tx = pool.begin().await?;
- // 目标断言可能已存在(同主宾已有一条真关系):那就并进去,别造重复。
- //
- // **宾语两侧都要比。** 字面值事实的 object_id 都是 NULL,只比它就等于
- // 把同主同谓的所有值当成同一条断言——(星云科技, founding_date, 2015) 与
- // (星云科技, founding_date, 2016) 会被并成一条,后一个值静默消失
- let existing: Option<(Uuid,)> = sqlx::query_as(
- "SELECT id FROM facts
- WHERE kb_id = $1 AND subject_id = $2 AND predicate_id = $3
- AND object_id IS NOT DISTINCT FROM $4
- AND object_value IS NOT DISTINCT FROM $5
- AND invalidated_at IS NULL",
- )
- .bind(kb_id)
- .bind(subject_id)
- .bind(predicate_id)
- .bind(object_id)
- .bind(&object_value)
- .fetch_optional(&mut *tx)
- .await?;
-
- let (new_id, mode) = match existing {
- Some((id,)) => (id, ADOPT_MERGED),
- None => {
- let id = Uuid::now_v7();
- // 宾语显式绑定而不是从旧行复制:属性那一路的新值是归一化过的
- //("2015" → 日期),照抄旧行就等于把没换算的原值塞进去。
- // 关系那一路绑的就是旧行的值,行为一字不变
- let inserted: Option<(Uuid,)> = sqlx::query_as(
- "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, object_value,
- valid_from, valid_from_precision,
- valid_to, valid_to_precision, confidence, supersedes,
- attested_from, attested_to, end_derived,
- corrected_ends)
- SELECT $1, kb_id, $6, $3, $4, $5,
- valid_from, valid_from_precision,
- valid_to, valid_to_precision, confidence, id,
- attested_from, attested_to, end_derived, corrected_ends
- FROM facts WHERE id = $2 AND invalidated_at IS NULL
- RETURNING id",
- )
- .bind(id)
- .bind(old_id)
- .bind(predicate_id)
- .bind(object_id)
- .bind(&object_value)
- // 主语也显式绑定,不再从旧行复制——被动形改写要的正是换掉它。
- // 第一版漏了这一处:局部变量换了、SQL 里还写着 subject_id,
- // 于是宾语换了、主语没换,凭空造出一条 `OpenAI produces OpenAI`
- .bind(subject_id)
- .fetch_optional(&mut *tx)
- .await?;
- // 已被并发改写:不重复动手
- let Some((id,)) = inserted else {
- tx.rollback().await?;
- continue;
- };
- (id, ADOPT_SUPERSEDED)
- }
- };
-
- // 证据整体搬过去,表层谓词一并保留——它是这次改写的依据,不该在改写中丢失
- sqlx::query(
- "INSERT INTO fact_evidence (fact_id, chunk_id, quote, proposed_predicate, document_id, doc_version,
- quote_start, quote_end)
- SELECT $1, chunk_id, quote, proposed_predicate, document_id, doc_version,
- quote_start, quote_end
- FROM fact_evidence WHERE fact_id = $2
- ON CONFLICT DO NOTHING",
- )
- .bind(new_id)
- .bind(old_id)
- .execute(&mut *tx)
- .await?;
- /* **边上的属性跟着搬**(0037)。谓词还没被采纳时金额就已经落在旧行上——
- 一句「NVIDIA invested $1.5 billion in SB Energy」在 schema.org 库里
- `invested_in` 是未知说法,钱不能等到采纳那天才有地方放。旧行作废、
- 新行接上,属性照证据的样子整体复制;顺手在关系上补声明——
- 这些属性定义本来就在库里,缺的只是关系上的一条声明 */
- sqlx::query(
- "INSERT INTO fact_qualifiers (fact_id, qualifier_type_id, value, entity_id)
- SELECT $1, qualifier_type_id, value, entity_id
- FROM fact_qualifiers WHERE fact_id = $2
- ON CONFLICT DO NOTHING",
- )
- .bind(new_id)
- .bind(old_id)
- .execute(&mut *tx)
- .await?;
- sqlx::query(
- "INSERT INTO relation_type_qualifiers (relation_type_id, qualifier_type_id)
- SELECT $1, q.qualifier_type_id
- FROM fact_qualifiers q JOIN relation_types r ON r.id = q.qualifier_type_id
- WHERE q.fact_id = $2 AND r.kind = 'attribute' AND r.id <> $1
- ON CONFLICT DO NOTHING",
- )
- .bind(predicate_id)
- .bind(new_id)
- .execute(&mut *tx)
- .await?;
- sqlx::query("UPDATE facts SET invalidated_at = now() WHERE id = $1")
- .bind(old_id)
- .execute(&mut *tx)
- .await?;
- sqlx::query(
- "INSERT INTO fact_adoptions
- (batch_id, kb_id, predicate_id, old_fact_id, new_fact_id, mode)
- VALUES ($1, $2, $3, $4, $5, $6)",
- )
- .bind(batch_id)
- .bind(kb_id)
- .bind(predicate_id)
- .bind(old_id)
- .bind(new_id)
- .bind(mode)
- .execute(&mut *tx)
- .await?;
- tx.commit().await?;
- moved += 1;
- }
- Ok(Adopted {
- batch_id,
- moved,
- left_off,
- corrected,
- })
-}
-
-/// 撤销一次采纳:新写的行作废、旧行复活。
-///
-/// 关系类型**不删**——已有事实指向过它(`delete_relation_type` 也会拒绝),
-/// 而按 append-only 的规矩"它存在过"本身是历史;一个没人用的关系是惰性的。
-/// 证据也不清:新行已作废,其证据随之惰性,删掉反而抹掉"我们曾经这么认为"。
-///
-/// 并入那种(mode = merged)只复活旧行,不动被并入的目标——它本来就在,
-/// 复制过去的证据留着无害(`ON CONFLICT DO NOTHING` 本就可能是它自己的)。
-pub async fn unadopt(pool: &PgPool, kb_id: Uuid, batch_id: Uuid) -> AppResult<u32> {
- let rows: Vec<(Uuid, Uuid, String)> = sqlx::query_as(
- "SELECT old_fact_id, new_fact_id, mode FROM fact_adoptions
- WHERE batch_id = $1 AND kb_id = $2 AND reverted_at IS NULL",
- )
- .bind(batch_id)
- .bind(kb_id)
- .fetch_all(pool)
- .await?;
- if rows.is_empty() {
- return Err(AppError::NotFound);
- }
-
- let mut tx = pool.begin().await?;
- let mut reverted = 0u32;
- for (old_id, new_id, mode) in &rows {
- if mode == ADOPT_SUPERSEDED {
- sqlx::query(
- "UPDATE facts SET invalidated_at = now() WHERE id = $1 AND invalidated_at IS NULL",
- )
- .bind(new_id)
- .execute(&mut *tx)
- .await?;
- }
- sqlx::query("UPDATE facts SET invalidated_at = NULL WHERE id = $1")
- .bind(old_id)
- .execute(&mut *tx)
- .await?;
- reverted += 1;
- }
- // 标记而不是删除:这次采纳发生过,撤销也发生过,两件都是历史
- sqlx::query(
- "UPDATE fact_adoptions SET reverted_at = now()
- WHERE batch_id = $1 AND kb_id = $2 AND reverted_at IS NULL",
- )
- .bind(batch_id)
- .bind(kb_id)
- .execute(&mut *tx)
- .await?;
- tx.commit().await?;
- Ok(reverted)
-}
-
-/// 词表外的**字面值**说法:还没有谓词、宾语是值而不是实体的那些。
-///
-/// 跟 [`proposed_predicates`] 互补,两边用 `object_id` 是否为空严格分开。
-/// 混在一起提案就会把 `founding_date` 提成一条关系,而那正是这条路要修掉的。
-pub async fn proposed_attributes(
- pool: &PgPool,
- kb_id: Uuid,
-) -> AppResult<Vec<utopia_core::models::ProposedAttribute>> {
- Ok(sqlx::query_as(
- // 同 proposed_predicates:普遍程度从全量证据数,改写量从积压数;
- // 走 CTE 而不是相关子查询,后者每组重扫一遍证据表
- // 同 proposed_predicates:开放陈述(0044)的短语不是等着被采纳的说法
- "WITH spread AS (
- SELECT e.proposed_predicate AS form,
- count(DISTINCT e.document_id) AS doc_count
- FROM fact_evidence e
- JOIN facts ff ON ff.id = e.fact_id
- WHERE ff.kb_id = $1 AND ff.layer = 'typed' AND e.proposed_predicate IS NOT NULL
- GROUP BY 1
- )
- SELECT fe.proposed_predicate AS form,
- count(DISTINCT f.id) AS fact_count,
- max(sp.doc_count) AS doc_count,
- (SELECT f2.object_value::text
- FROM fact_evidence e2
- JOIN facts f2 ON f2.id = e2.fact_id
- WHERE e2.proposed_predicate = fe.proposed_predicate
- AND f2.kb_id = $1 AND f2.layer = 'typed' AND f2.predicate_id IS NULL
- AND f2.object_id IS NULL AND f2.invalidated_at IS NULL
- LIMIT 1) AS example,
- -- 主语实际是什么类:属性的 domain 从这里来,不靠猜
- ARRAY(SELECT DISTINCT t.key
- FROM fact_evidence e3
- JOIN facts f3 ON f3.id = e3.fact_id
- JOIN entities s ON s.id = f3.subject_id
- JOIN entity_types t ON t.id = s.type_id
- WHERE e3.proposed_predicate = fe.proposed_predicate
- AND f3.kb_id = $1 AND f3.layer = 'typed' AND f3.predicate_id IS NULL
- AND f3.object_id IS NULL AND f3.invalidated_at IS NULL) AS domain_keys
- FROM fact_evidence fe
- JOIN facts f ON f.id = fe.fact_id
- JOIN spread sp ON sp.form = fe.proposed_predicate
- WHERE f.kb_id = $1 AND f.layer = 'typed' AND f.predicate_id IS NULL
- AND f.invalidated_at IS NULL AND fe.proposed_predicate IS NOT NULL
- AND f.object_id IS NULL
- -- 拒绝过的说法不再出现在候选里
- AND NOT EXISTS (SELECT 1 FROM ontology_misses m
- WHERE m.kb_id = $1 AND m.kind = 'attribute_type'
- AND m.key = fe.proposed_predicate AND m.dismissed_at IS NOT NULL)
- GROUP BY fe.proposed_predicate
- ORDER BY fact_count DESC, form",
- )
- .bind(kb_id)
- .fetch_all(pool)
- .await?)
-}
-
-/// 某几个字面值说法当前挂着的事实:id、主语的类型、原始值。
-///
-/// 给采纳那一步用。**归一化不在这里做**——它按 datatype 把 "2015" 变成日期、
-/// 把 "1,200" 变成数字,那套规则住在抽取模块,store 够不着也不该够得着。
-/// 调用方归一化完,把结果原样交回来。
-pub async fn value_facts_for_forms(
- pool: &PgPool,
- kb_id: Uuid,
- forms: &[String],
-) -> AppResult<Vec<(Uuid, Option<Uuid>, serde_json::Value)>> {
- if forms.is_empty() {
- return Ok(Vec::new());
- }
- // **主语类型是 Option**。列本来就可空——「抽取器抽到了东西,但本体里没有
- // 对应的类」是一个正常状态(0009),不是异常。解成裸 `Uuid` 的时候,批里
- // 只要有一条主语没类型,整次采纳就在解码那一步报错退出:
- // `decoding column 1: unexpected null`,一条也改写不了。实测一个库里攒着
- // 2454 条等谓词的值事实,其中 58 条主语无类型,够把好几个说法卡死。
- // 没类型的那些不参与 domain(属性得声明挂在哪些类下),但照样跟着改写——
- // 把它们一起丢掉等于让一条有名有姓的事实继续没有谓词
- Ok(sqlx::query_as(
- "SELECT DISTINCT f.id, s.type_id, f.object_value
- FROM facts f
- JOIN entities s ON s.id = f.subject_id
- WHERE f.kb_id = $1 AND f.layer = 'typed'
- AND f.predicate_id IS NULL AND f.invalidated_at IS NULL
- AND f.object_id IS NULL AND f.object_value IS NOT NULL
- AND EXISTS (SELECT 1 FROM fact_evidence e
- WHERE e.fact_id = f.id AND e.proposed_predicate = ANY($2))",
- )
- .bind(kb_id)
- .bind(forms)
- .fetch_all(pool)
- .await?)
-}
-
-/// 采纳一批**字面值**说法:把它们的事实改挂到某个属性上。
-///
-/// 与 [`adopt_proposed_predicates`] 共用改写、批次与撤销——对图做的事是同一件,
-/// 只有"新宾语从哪来"不同:这里的值由调用方按属性的 datatype 归一化过,
-/// 换算不出来的那些根本不会传进来(它们继续没有谓词,等下一次)。
-pub async fn adopt_value_facts(
- pool: &PgPool,
- kb_id: Uuid,
- attribute_id: Uuid,
- rewrites: &[(Uuid, serde_json::Value)],
-) -> AppResult<Adopted> {
- // 属性那一路没有对调可言:宾语是字面值,值不能当主语
- adopt(
- pool,
- kb_id,
- attribute_id,
- AdoptTargets::WithValues(rewrites),
- false,
- )
- .await
-}
-
#[cfg(test)]
mod temporal_shape_tests {
use super::*;
diff --git a/crates/utopia-store/src/names.rs b/crates/utopia-store/src/names.rs
index 5231ca5a..40192c17 100644
--- a/crates/utopia-store/src/names.rs
+++ b/crates/utopia-store/src/names.rs
@@ -16,20 +16,13 @@ use crate::graph::{add_evidence, insert_value_fact, Validity};
use crate::resolution::normalize_name;
use sqlx::PgPool;
use std::collections::HashMap;
-use utopia_core::models::{NameView, RelationType};
+use utopia_core::models::NameView;
use utopia_core::AppResult;
use uuid::Uuid;
/// 内建属性的 key。与 `is_a` 同一个做法:`builtin = TRUE`,建库时不铺,第一次要时建
pub const KNOWN_AS: &str = "known_as";
-/// 这条关系类型是不是名字属性。给模型看的清单、本体引导、本体向量索引都要把它拿掉:
-/// 名字走自己的通道(抽取回复里的 `names`,服务端核对它在原文里),不能当一条普通属性
-/// 被模型直接写进来——那样就绕过了核对
-pub fn is_name_attribute(r: &RelationType) -> bool {
- r.builtin && r.key == KNOWN_AS
-}
-
/// 取(没有就建)这个库的 `known_as` 属性。
pub async fn ensure_known_as(pool: &PgPool, kb_id: Uuid) -> AppResult<Uuid> {
if let Some((id,)) =
diff --git a/crates/utopia-store/src/ontology.rs b/crates/utopia-store/src/ontology.rs
index 20ddea89..c37d6b14 100644
--- a/crates/utopia-store/src/ontology.rs
+++ b/crates/utopia-store/src/ontology.rs
@@ -4,8 +4,8 @@ use pgvector::Vector;
use sqlx::{PgConnection, PgPool};
use std::collections::HashSet;
use utopia_core::models::{
- EntityInstance, EntityTypeView, OntologyImportView, OntologyMiss, RelationAxioms,
- RelationTypeView, TypeCandidate,
+ EntityInstance, EntityTypeView, OntologyImportView, RelationAxioms, RelationTypeView,
+ TypeCandidate,
};
use utopia_core::{AppError, AppResult};
use uuid::Uuid;
@@ -881,112 +881,6 @@ pub async fn delete_relation_type(pool: &PgPool, kb_id: Uuid, id: Uuid) -> AppRe
/* ---- 未匹配统计 ---- */
-pub async fn record_miss(
- pool: &PgPool,
- kb_id: Uuid,
- kind: &str,
- key: &str,
- example: Option<&str>,
-) -> AppResult<()> {
- sqlx::query(
- // **被拒绝过的照样累加。**
- //
- // 从前这里带着 `WHERE dismissed_at IS NULL`,理由是"否则计数会把'不要'
- // 重新顶成一个待处理信号"。那个理由针对的是**呈现**,用的手段却是
- // **停止计数**——两件事被绑在一起了,代价是一次点击变成永久失明:
- // 第一篇里出现一次的说法被忽略掉,后面二十篇都在用它,计数仍停在 1,
- // 谁也不知道当初那个判断已经不成立,那批事实永远没有谓词。
- //
- // 用户是对**当时看得见的证据**做的判断,不是对所有时间。所以计数照记,
- // 抑制交给读取侧:`list_misses` 仍然只返回未忽略的,提案与自动扩本体
- // 一步没变;已忽略的连同更新后的计数走 `list_dismissed_misses`,
- // 在面板上单列一处,人看见涨到 40 了可以自己撤回
- "INSERT INTO ontology_misses (kb_id, kind, key, example)
- VALUES ($1, $2, left($3, 80), left($4, 200))
- ON CONFLICT (kb_id, kind, key)
- DO UPDATE SET count = ontology_misses.count + 1,
- example = COALESCE(EXCLUDED.example, ontology_misses.example),
- updated_at = now()",
- )
- .bind(kb_id)
- .bind(kind)
- .bind(key)
- .bind(example)
- .execute(pool)
- .await?;
- Ok(())
-}
-
-pub async fn list_misses(pool: &PgPool, kb_id: Uuid) -> AppResult<Vec<OntologyMiss>> {
- Ok(sqlx::query_as(
- "SELECT kind, key, example, count FROM ontology_misses
- WHERE kb_id = $1 AND dismissed_at IS NULL
- ORDER BY count DESC, updated_at DESC LIMIT 50",
- )
- .bind(kb_id)
- .fetch_all(pool)
- .await?)
-}
-
-/// 已被忽略的说法,连同**它此后继续累积的计数**。
-///
-/// 存在的理由是忽略这个动作曾经是单向门:点下去之后既不再呈现、也不再计数,
-/// 于是"当时只出现过一次"这个判断依据一旦过期,没有任何人看得见。
-/// 这个列表是那扇门上的窗——抑制照旧,但看得见抑制掉的是什么、现在有多重。
-pub async fn list_dismissed_misses(pool: &PgPool, kb_id: Uuid) -> AppResult<Vec<OntologyMiss>> {
- Ok(sqlx::query_as(
- "SELECT kind, key, example, count FROM ontology_misses
- WHERE kb_id = $1 AND dismissed_at IS NOT NULL
- ORDER BY count DESC, updated_at DESC LIMIT 50",
- )
- .bind(kb_id)
- .fetch_all(pool)
- .await?)
-}
-
-/// 撤回一次忽略:这个说法重新进入提案与自动扩本体。
-pub async fn restore_miss(pool: &PgPool, kb_id: Uuid, kind: &str, key: &str) -> AppResult<()> {
- sqlx::query(
- "UPDATE ontology_misses SET dismissed_at = NULL, updated_at = now()
- WHERE kb_id = $1 AND kind = $2 AND key = $3 AND dismissed_at IS NOT NULL",
- )
- .bind(kb_id)
- .bind(kind)
- .bind(key)
- .execute(pool)
- .await?;
- Ok(())
-}
-
-/// 用户说"不要这个"。**标记而非删除**——删掉的话下一次抽取遇到同一个词
-/// 原样插回来,用户的拒绝活不过一轮抽取。自动扩展路径也据此绕开。
-///
-/// 可撤回(见 [`restore_miss`]),且撤回之后计数是连续的——忽略期间照样在记。
-pub async fn dismiss_miss(pool: &PgPool, kb_id: Uuid, kind: &str, key: &str) -> AppResult<()> {
- sqlx::query(
- "UPDATE ontology_misses SET dismissed_at = now()
- WHERE kb_id = $1 AND kind = $2 AND key = $3 AND dismissed_at IS NULL",
- )
- .bind(kb_id)
- .bind(kind)
- .bind(key)
- .execute(pool)
- .await?;
- Ok(())
-}
-
-/// 本体已经覆盖了这个说法(采纳时调用):与"用户拒绝"不同,这条真的可以清掉,
-/// 下次抽取它会命中本体,不再是未匹配。
-pub async fn clear_miss(pool: &PgPool, kb_id: Uuid, kind: &str, key: &str) -> AppResult<()> {
- sqlx::query("DELETE FROM ontology_misses WHERE kb_id = $1 AND kind = $2 AND key = $3")
- .bind(kb_id)
- .bind(kind)
- .bind(key)
- .execute(pool)
- .await?;
- Ok(())
-}
-
/* ---- OWL 导入 ---- */
/// 建一个带 IRI 的类。IRI 是全局身份,重导入据它匹配(见 0001 P2)。
@@ -1499,109 +1393,6 @@ pub async fn nearest_relation_types(
.collect())
}
-/// 按 key 找关系/属性的 id。给"映射到已有类型"那条路用。
-///
-/// 不区分 kind:属性与关系同住一张表且共用 key 命名空间,调用方拿到 id 之后
-/// 该怎么用它自己清楚(改写事实时谓词就是谓词)。
-///
-/// 名字属性找不到(0041):把一批「简称」「former_name」的值事实归并到 `known_as`
-/// 上,等于绕开了名字的核对与配对,所以这条路不给它
-pub async fn relation_type_id_by_key(
- pool: &PgPool,
- kb_id: Uuid,
- key: &str,
-) -> AppResult<Option<Uuid>> {
- let row: Option<(Uuid,)> = sqlx::query_as(
- "SELECT id FROM relation_types
- WHERE kb_id = $1 AND key = $2 AND NOT (builtin AND key = 'known_as')",
- )
- .bind(kb_id)
- .bind(key)
- .fetch_optional(pool)
- .await?;
- Ok(row.map(|(id,)| id))
-}
-
-/// 一个**从没当关系用过**的关系,改判成属性。改成了返回 true。
-///
-/// 冷启动认出某个说法该是属性、去建的时候,键可能已经被一个同名关系占着
-/// (实测:`Relation key 'valuation' already exists`,然后整批放弃,那些数
-/// 永远拿不到谓词)。可占着这个键的关系常常是空的——本体包带进来的、或者
-/// 早先按票数建的,一条事实都没挂上。空的关系改判不破坏任何东西:
-/// 没有边会因此断,撤销也只是再改回去。
-///
-/// **有事实的一律不动**。`invested`、`raised` 这类既连实体又带数额的,
-/// 改判会把已有的边连根拔起;那是本体与语料的真分歧,该留给人看,
-/// 不该由冷启动替人决定。
-pub async fn attribute_from_unused_relation(
- pool: &PgPool,
- kb_id: Uuid,
- key: &str,
- domains: &[Uuid],
- datatype: &str,
- unit: Option<&str>,
-) -> AppResult<Option<Uuid>> {
- validate_attribute_fields("attribute", domains, Some(datatype))?;
- let mut tx = pool.begin().await?;
- // 零事实不等于零判据:公理旗标与 inverse_of/sub_property_of 会让这条关系
- // 成为派生目标(违规的 detail.predicate_id 指到它)。改判清了那些贡献,
- // 按同一条对账规矩走(0062)——先记旧判据下的检出,写完再对账
- let candidate: Option<(Uuid,)> = sqlx::query_as(
- "SELECT id FROM relation_types
- WHERE kb_id = $1 AND key = $2 AND kind = 'relation'
- AND NOT EXISTS (SELECT 1 FROM facts f WHERE f.predicate_id = relation_types.id)",
- )
- .bind(kb_id)
- .bind(key)
- .fetch_optional(&mut *tx)
- .await?;
- let Some((id,)) = candidate else {
- tx.rollback().await?;
- return Ok(None);
- };
- let was_detected = crate::reasoning::detection_keys(&mut tx, kb_id).await?;
- // 属性不声明判据:旗标与双向链接都清掉(axioms() 不按 kind 过滤,
- // 留着它们等於留着一套无人问的公理)
- sqlx::query(
- "UPDATE relation_types SET kind = 'attribute', datatype = $2, unit = $3,
- inverse_of = NULL, sub_property_of = NULL,
- is_transitive = false, is_symmetric = false, is_asymmetric = false,
- is_irreflexive = false, functional = false, inverse_functional = false
- WHERE id = $1",
- )
- .bind(id)
- .bind(datatype)
- .bind(unit)
- .execute(&mut *tx)
- .await?;
- sqlx::query("UPDATE relation_types SET inverse_of = NULL WHERE inverse_of = $1")
- .bind(id)
- .execute(&mut *tx)
- .await?;
- sqlx::query("UPDATE relation_types SET sub_property_of = NULL WHERE sub_property_of = $1")
- .bind(id)
- .execute(&mut *tx)
- .await?;
- // domain / range 在各自的表里,不是列。属性没有 range——值域落在 datatype 上
- set_domains_ranges(&mut tx, id, domains, &[]).await?;
- crate::reasoning::reconcile_ontology(&mut tx, kb_id, &was_detected).await?;
- tx.commit().await?;
- Ok(Some(id))
-}
-
-/// 一个属性声明的 datatype。改写字面值事实时要按它换算。
-///
-/// 以**库里这一条**为准而不是以请求为准:指向已有属性时请求里根本没有
-/// datatype,而即便有,本体说了算。
-pub async fn relation_type_datatype(pool: &PgPool, id: Uuid) -> AppResult<Option<String>> {
- let row: Option<(Option<String>,)> =
- sqlx::query_as("SELECT datatype FROM relation_types WHERE id = $1")
- .bind(id)
- .fetch_optional(pool)
- .await?;
- Ok(row.and_then(|(d,)| d))
-}
-
/// 把一个 IRI 认到已有的**本地**类上(原本没有 IRI 的那种)。
///
/// **只写 IRI 与形状,不动 label、description、颜色。** 认领要解决的是
@@ -2068,35 +1859,6 @@ pub async fn open_proposal(
.await?)
}
-/// 把一轮 Suggest 的结果写下来。
-///
-/// **已经有人表过态的不动。** `WHERE status = 'open'` 那一句是这个函数的全部要点:
-/// 重跑 Suggest 会再次算出被拒绝过的那条提案(原材料还在 `ontology_misses` 里),
-/// 不加这句它就会被刷回 open——等于每跑一次都把人的否决抹掉一次。
-pub async fn save_proposals(
- pool: &PgPool,
- kb_id: Uuid,
- items: &[(String, String, serde_json::Value)],
-) -> AppResult<()> {
- for (section, key, payload) in items {
- sqlx::query(
- "INSERT INTO ontology_proposals (id, kb_id, section, key, payload)
- VALUES ($1, $2, $3, $4, $5)
- ON CONFLICT (kb_id, section, key) DO UPDATE
- SET payload = EXCLUDED.payload, created_at = now()
- WHERE ontology_proposals.status = 'open'",
- )
- .bind(Uuid::now_v7())
- .bind(kb_id)
- .bind(section)
- .bind(key)
- .bind(payload)
- .execute(pool)
- .await?;
- }
- Ok(())
-}
-
/// 还等着人看的提案。新的排前面——旧的那批已经被看过好几眼了。
pub async fn open_proposals(pool: &PgPool, kb_id: Uuid) -> AppResult<Vec<StoredProposal>> {
Ok(sqlx::query_as(
diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rs
index 03d1b305..a38143f6 100644
--- a/crates/utopia-store/src/resolution.rs
+++ b/crates/utopia-store/src/resolution.rs
@@ -2415,49 +2415,6 @@ pub async fn put_verdict(
Ok(())
}
-/// 记下模型提议、但本体装不下的类型。
-///
-/// **只写第一次**:同一实体会被多篇文档提到,第一次的提议就算它的提议;
-/// 后来的覆盖会让"哪些实体在等 model 类"随最后一篇文档抖动。
-/// 采纳了对应的类之后由改写流程清空——那时它已经不是"提议"而是既成事实。
-pub async fn set_proposed_type(pool: &PgPool, entity_id: Uuid, proposed: &str) -> AppResult<()> {
- sqlx::query(
- "UPDATE entities SET proposed_type = left($2, 60)
- WHERE id = $1 AND proposed_type IS NULL",
- )
- .bind(entity_id)
- .bind(proposed)
- .execute(pool)
- .await?;
- Ok(())
-}
-
-/// 待认领的实体类型:模型提议过、本体没有、实体因此降级成了 concept。
-///
-/// 与谓词那边的 `graph::proposed_predicates` 对称——连着具体实体,所以采纳时
-/// 能说清"将重新归类 43 个"并真的去改,而不是只建一个空类。
-pub async fn proposed_types(
- pool: &PgPool,
- kb_id: Uuid,
-) -> AppResult<Vec<utopia_core::models::ProposedType>> {
- Ok(sqlx::query_as(
- "SELECT e.proposed_type AS form,
- count(*) AS entity_count,
- (array_agg(e.canonical_name ORDER BY e.created_at))[1] AS example
- FROM entities e
- WHERE e.kb_id = $1 AND e.merged_into IS NULL AND e.proposed_type IS NOT NULL
- -- 用户拒绝过的类型不再出现在候选里
- AND NOT EXISTS (SELECT 1 FROM ontology_misses m
- WHERE m.kb_id = $1 AND m.kind = 'entity_type'
- AND m.key = e.proposed_type AND m.dismissed_at IS NOT NULL)
- GROUP BY e.proposed_type
- ORDER BY entity_count DESC, form",
- )
- .bind(kb_id)
- .fetch_all(pool)
- .await?)
-}
-
/// 把提议过 `forms` 里那些类型的实体改到 `type_id` 上。返回 (批次 id, 改动数)。
///
/// 与谓词那边的 `graph::adopt_proposed_predicates` 对称:**只建类型不动实体,
@@ -2589,60 +2546,6 @@ pub async fn unadopt_types(pool: &PgPool, kb_id: Uuid, batch_id: Uuid) -> AppRes
Ok(reverted)
}
-/// 把提议的类型规整成 key 的形状:小写、非字母数字换下划线、压缩重复。
-/// "AI Model" → "ai_model",与 validate_key 允许的字符集对齐。
-fn normalize_type_key(s: &str) -> String {
- let mut out = String::with_capacity(s.len());
- let mut last_us = true; // 前导下划线也算重复
- for c in s.trim().chars() {
- if c.is_ascii_alphanumeric() {
- out.push(c.to_ascii_lowercase());
- last_us = false;
- } else if !last_us {
- out.push('_');
- last_us = true;
- }
- }
- while out.ends_with('_') {
- out.pop();
- }
- out.chars().take(40).collect()
-}
-
-/// 认领那些"类型已经在本体里、实体却还挂在 concept 下"的实体。
-///
-/// `adopt_proposed_types` 只在**建类的那一刻**被调用,于是类先建好、实体后被
-/// 抽出来的情形就永远等不到搬运——而这恰恰是常态:本体第一轮建好,后续文档
-/// 继续产出提议。这个扫描把它们收尾。
-///
-/// 只做**规整后精确同名**的匹配,不做近似——猜错就是把实体放进错的类,
-/// 而"再等一轮"的代价接近零。
-pub async fn sweep_proposed_types(
- pool: &PgPool,
- kb_id: Uuid,
- actor: Option<Uuid>,
-) -> AppResult<Vec<(Uuid, u32)>> {
- let pending = proposed_types(pool, kb_id).await?;
- let existing: Vec<(Uuid, String)> =
- sqlx::query_as("SELECT id, key FROM entity_types WHERE kb_id = $1")
- .bind(kb_id)
- .fetch_all(pool)
- .await?;
- let mut out = Vec::new();
- for p in &pending {
- let norm = normalize_type_key(&p.form);
- let Some((type_id, _)) = existing.iter().find(|(_, k)| *k == norm) else {
- continue;
- };
- let (batch, n) =
- adopt_proposed_types(pool, kb_id, *type_id, std::slice::from_ref(&p.form), actor)
- .await?;
- if n > 0 {
- out.push((batch, n));
- }
- }
- Ok(out)
-}
#[cfg(test)]
mod tests {
use super::*;
diff --git a/crates/utopia-store/tests/store/a_kind_word_binds_to_a_class.rs b/crates/utopia-store/tests/store/a_kind_word_binds_to_a_class.rs
index 930bf7e7..2fca4d01 100644
--- a/crates/utopia-store/tests/store/a_kind_word_binds_to_a_class.rs
+++ b/crates/utopia-store/tests/store/a_kind_word_binds_to_a_class.rs
@@ -12,7 +12,7 @@ use sqlx::PgPool;
use std::collections::HashSet;
use std::time::Duration;
use utopia_store::graph::{self, FactObject};
-use utopia_store::{ontology, resolution, type_bindings};
+use utopia_store::{ontology, type_bindings};
use uuid::Uuid;
const ORG: &str = "kind-word-binding-test";
@@ -328,13 +328,6 @@ async fn a_kind_word_is_counted_once_bound_once_and_applied_to_its_entities() ->
0,
"只写第一次,与 set_proposed_type 同一条"
);
- let proposed = resolution::proposed_types(&pool, f.kb).await?;
- let p = proposed
- .iter()
- .find(|p| p.form == "stockholder proposal")
- .expect("the ontology page offers it");
- assert_eq!(p.entity_count, 1);
- assert_eq!(p.example.as_deref(), Some(PROPOSAL));
// 绑定的形状:绑上了就得有类
assert!(type_bindings::decide(
&pool,
diff --git a/crates/utopia-store/tests/store/a_name_is_a_fact.rs b/crates/utopia-store/tests/store/a_name_is_a_fact.rs
index ce5373f7..0d3484d1 100644
--- a/crates/utopia-store/tests/store/a_name_is_a_fact.rs
+++ b/crates/utopia-store/tests/store/a_name_is_a_fact.rs
@@ -307,10 +307,6 @@ async fn the_name_attribute_is_not_an_ordinary_attribute() -> anyhow::Result<()>
let f = seed(&pool).await?;
let run = async {
let known_as = names::ensure_known_as(&pool, f.kb).await?;
- assert_eq!(
- utopia_store::ontology::relation_type_id_by_key(&pool, f.kb, names::KNOWN_AS).await?,
- None
- );
let edited = utopia_store::ontology::update_relation_type(
&pool,
f.kb,
diff --git a/crates/utopia-store/tests/store/a_signature_holds_on_every_path.rs b/crates/utopia-store/tests/store/a_signature_holds_on_every_path.rs
index 624d8645..9be3563f 100644
--- a/crates/utopia-store/tests/store/a_signature_holds_on_every_path.rs
+++ b/crates/utopia-store/tests/store/a_signature_holds_on_every_path.rs
@@ -1,17 +1,14 @@
-//! 关系的签名(domain / range)在**三条写路径**上都得算数(#190 / #196)。
+//! 关系的签名(domain / range)在抽取之外的写路径上也得算数(#190 / #196)。
//!
-//! 抽取写入时按 #138 掰正方向或留空谓词,但写谓词的路不止一条:**采纳**把谓词挂回
-//! 旧事实,**合并**换掉主语的类型。守卫只装在抽取上,另外两条各自绕过去——实测采纳
-//! 把违反率从 0 抬到 12.3%。这里守三件事:
+//! 抽取写入时按 #138 掰正方向或留空谓词,但**合并**会换掉主语的类型,守卫只装在抽取上
+//! 就被它绕过去。这里守两件事:
//!
-//! 1. 采纳走同一道判断:主语不合宾语合 → 对调着挂;两边都不合 → 不挂,事实留在空谓词上。
-//! 2. 合并之后,被换了主语的事实若违反签名 → `axiom_violations` 里多一条 `signature`。
-//! 3. 一致性检查(R0)也能量出签名违规,事实撤了它就被清掉。
+//! 1. 合并之后,被换了主语的事实若违反签名 → `axiom_violations` 里多一条 `signature`。
+//! 2. 一致性检查(R0)也能量出签名违规,事实撤了它就被清掉。
//!
//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
use sqlx::PgPool;
-use utopia_store::graph::Adopted;
use uuid::Uuid;
struct Fixture {
@@ -24,8 +21,6 @@ struct Fixture {
acme: Uuid,
alice: Uuid,
bob: Uuid,
- doc: Uuid,
- chunk: Uuid,
}
async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> {
@@ -130,41 +125,9 @@ async fn seed(pool: &PgPool) -> anyhow::Result<Fixture> {
acme,
alice,
bob,
- doc,
- chunk,
})
}
-/// 一条没有谓词、证据里留着原文说法 `employee` 的事实——采纳要改写的正是这种
-async fn surfaced_fact(
- pool: &PgPool,
- f: &Fixture,
- subject: Uuid,
- object: Uuid,
-) -> anyhow::Result<Uuid> {
- let id = Uuid::now_v7();
- sqlx::query(
- "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, confidence)
- VALUES ($1, $2, $3, NULL, $4, 0.9)",
- )
- .bind(id)
- .bind(f.kb)
- .bind(subject)
- .bind(object)
- .execute(pool)
- .await?;
- sqlx::query(
- "INSERT INTO fact_evidence (fact_id, chunk_id, quote, proposed_predicate, document_id, doc_version)
- VALUES ($1, $2, 'employee of', 'employee', $3, 1)",
- )
- .bind(id)
- .bind(f.chunk)
- .bind(f.doc)
- .execute(pool)
- .await?;
- Ok(id)
-}
-
async fn live_employee_edges(pool: &PgPool, f: &Fixture) -> anyhow::Result<Vec<(Uuid, Uuid)>> {
Ok(sqlx::query_as(
"SELECT subject_id, object_id FROM facts
@@ -191,7 +154,7 @@ async fn open_signature_breaks(pool: &PgPool, kb: Uuid) -> anyhow::Result<Vec<Uu
}
#[tokio::test]
-async fn adoption_and_merge_respect_the_signature() -> anyhow::Result<()> {
+async fn a_merge_that_breaks_the_signature_is_recorded() -> anyhow::Result<()> {
let Some(url) = utopia_store::test_db::url() else {
return Ok(());
};
@@ -199,44 +162,21 @@ async fn adoption_and_merge_respect_the_signature() -> anyhow::Result<()> {
let f = seed(&pool).await?;
let run = async {
- // 模型写的是 "Alice is an employee of Acme" —— 主语 person 违反 domain、宾语 company 符合
- let reversed = surfaced_fact(&pool, &f, f.alice, f.acme).await?;
- // "Bob is an employee of Alice" —— 两边都是 person,这个关系压根不适用
- let hopeless = surfaced_fact(&pool, &f, f.bob, f.alice).await?;
-
- // 1. 采纳:一条对调着挂上,一条不挂
- let Adopted {
- moved, left_off, ..
- } = utopia_store::graph::adopt_proposed_predicates(
- &pool,
- f.kb,
- f.employee,
- &["employee".to_string()],
- false,
+ // Acme employee Alice:主语 company、宾语 person,合签名
+ sqlx::query(
+ "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id)
+ VALUES ($1, $2, $3, $4, $5)",
)
+ .bind(Uuid::now_v7())
+ .bind(f.kb)
+ .bind(f.acme)
+ .bind(f.employee)
+ .bind(f.alice)
+ .execute(&pool)
.await?;
- assert_eq!(moved, 1, "the reversed one is adoptable once swapped");
- assert_eq!(left_off, 1, "the hopeless one must be left without a predicate");
- assert_eq!(
- live_employee_edges(&pool, &f).await?,
- vec![(f.acme, f.alice)],
- "adoption must write the edge in the ontology's direction: Acme employee Alice"
- );
- let (still_bare,): (bool,) =
- sqlx::query_as("SELECT predicate_id IS NULL AND invalidated_at IS NULL FROM facts WHERE id = $1")
- .bind(hopeless)
- .fetch_one(&pool)
- .await?;
- assert!(still_bare, "a fact that fits neither way stays live and predicate-less");
- let (old_gone,): (bool,) =
- sqlx::query_as("SELECT invalidated_at IS NOT NULL FROM facts WHERE id = $1")
- .bind(reversed)
- .fetch_one(&pool)
- .await?;
- assert!(old_gone, "the reversed row was superseded, not left beside the corrected one");
assert!(open_signature_breaks(&pool, f.kb).await?.is_empty());
- // 2. 合并:把 Acme 并进 Bob(person),Acme employee Alice 的主语变成 person
+ // 1. 合并:把 Acme 并进 Bob(person),Acme employee Alice 的主语变成 person
utopia_store::resolution::merge_entities(&pool, f.kb, f.acme, f.bob, None, "test")
.await?;
let edges = live_employee_edges(&pool, &f).await?;
@@ -254,7 +194,7 @@ async fn adoption_and_merge_respect_the_signature() -> anyhow::Result<()> {
"a merge that breaks the signature must show up as an open violation"
);
- // 3. 一致性检查量得出它,撤了事实就清掉
+ // 2. 一致性检查量得出它,撤了事实就清掉
let report = utopia_store::reasoning::run(&pool, f.kb).await?;
assert_eq!(report.found, 1);
assert_eq!(report.inserted, 0, "already recorded by the merge; the check must not duplicate it");
diff --git a/crates/utopia-store/tests/store/adopt_swap.rs b/crates/utopia-store/tests/store/adopt_swap.rs
deleted file mode 100644
index c6e65f30..00000000
--- a/crates/utopia-store/tests/store/adopt_swap.rs
+++ /dev/null
@@ -1,129 +0,0 @@
-//! 采纳时的主宾对调,打在真库上。
-//!
-//! `X produced_by Y` 与 `Y produces X` 是同一条边。采纳被动形时不对调,
-//! 图上就会多出一条反着的箭头,而且它跟正向那些永远合不到一起。
-//!
-//! 这条只能真跑:对调发生在 `adopt` 的 INSERT 里,`cargo check` 看不见 SQL。
-//! 实测中它确实漏过——`demo-b3` 那个库里 `produced_by` 与 `produces`
-//! 各成一个关系,因为采纳路径压根没走匹配器。
-
-use sqlx::PgPool;
-use uuid::Uuid;
-
-#[tokio::test]
-async fn adopting_a_passive_wording_flips_subject_and_object() -> anyhow::Result<()> {
- let Some(url) = utopia_store::test_db::url() else {
- return Ok(());
- };
- let pool = PgPool::connect(&url).await?;
- let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
- let etype = Uuid::now_v7();
- let produces = Uuid::now_v7();
- let (openai, chatgpt) = (Uuid::now_v7(), Uuid::now_v7());
- let (doc, chunk, fact) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
-
- sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'swap-test')")
- .bind(org)
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'swap-test')")
- .bind(ws)
- .bind(org)
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'swap-test')",
- )
- .bind(kb)
- .bind(ws)
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'thing', 'Thing')",
- )
- .bind(etype)
- .bind(kb)
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO relation_types (id, kb_id, key, label) VALUES ($1, $2, $3, $3)")
- .bind(produces)
- .bind(kb)
- .bind("produces")
- .execute(&pool)
- .await?;
- for (id, name) in [(openai, "OpenAI"), (chatgpt, "ChatGPT")] {
- sqlx::query(
- "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1,$2,$3,$4)",
- )
- .bind(id)
- .bind(kb)
- .bind(etype)
- .bind(name)
- .execute(&pool)
- .await?;
- }
- sqlx::query("INSERT INTO documents (id, kb_id, filename, sha256) VALUES ($1,$2,'a.txt','a')")
- .bind(doc)
- .bind(kb)
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1,$2,$3,0,'x')")
- .bind(chunk)
- .bind(kb)
- .bind(doc)
- .execute(&pool)
- .await?;
- // 原文说的是 "ChatGPT produced_by OpenAI",本体里没有这个关系,
- // 于是这条事实**没有谓词**——兜底谓词已经不存在了
- sqlx::query(
- "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id)
- VALUES ($1, $2, $3, NULL, $4)",
- )
- .bind(fact)
- .bind(kb)
- .bind(chatgpt)
- .bind(openai)
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO fact_evidence (fact_id, chunk_id, document_id, proposed_predicate)
- VALUES ($1, $2, $3, 'produced_by')",
- )
- .bind(fact)
- .bind(chunk)
- .bind(doc)
- .execute(&pool)
- .await?;
-
- let run = async {
- let moved = utopia_store::graph::adopt_proposed_predicates(
- &pool,
- kb,
- produces,
- &["produced_by".to_string()],
- true,
- )
- .await?
- .moved;
- assert_eq!(moved, 1);
- // 改写后应该是 OpenAI -[produces]-> ChatGPT,**方向反过来**
- let (s, o): (Uuid, Option<Uuid>) = sqlx::query_as(
- "SELECT subject_id, object_id FROM facts
- WHERE kb_id = $1 AND predicate_id = $2 AND invalidated_at IS NULL",
- )
- .bind(kb)
- .bind(produces)
- .fetch_one(&pool)
- .await?;
- assert_eq!(s, openai, "主语该是 OpenAI(原来是宾语)");
- assert_eq!(o, Some(chatgpt), "宾语该是 ChatGPT(原来是主语)");
- Ok::<_, anyhow::Error>(())
- }
- .await;
-
- sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
- .bind(kb)
- .execute(&pool)
- .await?;
- run
-}
diff --git a/crates/utopia-store/tests/store/an_amount_outlives_adoption.rs b/crates/utopia-store/tests/store/an_amount_outlives_adoption.rs
deleted file mode 100644
index 4a2f976d..00000000
--- a/crates/utopia-store/tests/store/an_amount_outlives_adoption.rs
+++ /dev/null
@@ -1,166 +0,0 @@
-//! 谓词还没被采纳时,金额已经落在事实上;采纳把属性搬到新行、并在关系上补声明(0037)。
-//!
-//! 一句「NVIDIA invested $1.5 billion in SB Energy」在 schema.org 库里 `invested_in`
-//! 是未知说法,钱不能等到采纳那天才有地方放。
-
-use sqlx::PgPool;
-use uuid::Uuid;
-
-#[tokio::test]
-async fn a_qualifier_written_before_adoption_survives_it() -> anyhow::Result<()> {
- let Some(url) = utopia_store::test_db::url() else {
- return Ok(());
- };
- let pool = PgPool::connect(&url).await?;
- let tag = Uuid::now_v7();
- let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
- sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, $2)")
- .bind(org)
- .bind(format!("outlive-{tag}"))
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, $3)")
- .bind(ws)
- .bind(org)
- .bind(format!("outlive-{tag}"))
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, $3)")
- .bind(kb)
- .bind(ws)
- .bind(format!("outlive-{tag}"))
- .execute(&pool)
- .await?;
- let class = Uuid::now_v7();
- sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'org', 'Org')")
- .bind(class)
- .bind(kb)
- .execute(&pool)
- .await?;
- let amount = utopia_store::ontology::create_relation_type(
- &pool,
- kb,
- "amount",
- "amount",
- "state",
- Default::default(),
- "",
- "attribute",
- &[class],
- &[],
- Some("number"),
- Some("$"),
- )
- .await?;
- // 关系此刻还不存在于本体里——先建好但不声明任何属性,模拟"采纳"那一刻
- let invested = utopia_store::ontology::create_relation_type(
- &pool,
- kb,
- "invested_in",
- "invested in",
- "event",
- Default::default(),
- "",
- "relation",
- &[],
- &[],
- None,
- None,
- )
- .await?;
- let (a, b) = (Uuid::now_v7(), Uuid::now_v7());
- for (id, name) in [(a, "NVIDIA"), (b, "SB Energy")] {
- sqlx::query(
- "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
- )
- .bind(id)
- .bind(kb)
- .bind(class)
- .bind(format!("{name}-{tag}"))
- .execute(&pool)
- .await?;
- }
- let (doc, chunk, fact) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
- sqlx::query("INSERT INTO documents (id, kb_id, filename, sha256) VALUES ($1, $2, $3, $4)")
- .bind(doc)
- .bind(kb)
- .bind(format!("{tag}.txt"))
- .bind(tag.to_string())
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1, $2, $3, 0, 'x')",
- )
- .bind(chunk)
- .bind(kb)
- .bind(doc)
- .execute(&pool)
- .await?;
- // 谓词为空的事实 + 说法在证据上 + 金额已经在边上
- sqlx::query(
- "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, confidence)
- VALUES ($1, $2, $3, NULL, $4, 0.9)",
- )
- .bind(fact)
- .bind(kb)
- .bind(a)
- .bind(b)
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO fact_evidence (fact_id, chunk_id, document_id, proposed_predicate)
- VALUES ($1, $2, $3, 'invested_in')",
- )
- .bind(fact)
- .bind(chunk)
- .bind(doc)
- .execute(&pool)
- .await?;
- let value = serde_json::json!({ "value": 1500000000.0, "unit": "$" });
- utopia_store::graph::upsert_fact_qualifier(&pool, fact, amount, &value).await?;
-
- // 采纳:旧行作废、新行接上谓词
- let adopted = utopia_store::graph::adopt_proposed_predicates(
- &pool,
- kb,
- invested,
- &["invested_in".to_string()],
- false,
- )
- .await?;
- assert_eq!(adopted.moved, 1);
-
- // 活着的那一行带着金额
- let live: Vec<(Uuid,)> = sqlx::query_as(
- "SELECT id FROM facts WHERE kb_id = $1 AND predicate_id = $2 AND invalidated_at IS NULL",
- )
- .bind(kb)
- .bind(invested)
- .fetch_all(&pool)
- .await?;
- assert_eq!(live.len(), 1, "采纳后正好一条活着的边");
- let quals = utopia_store::graph::fact_qualifiers_for(&pool, &[live[0].0]).await?;
- let q = &quals[&live[0].0];
- assert_eq!(q.len(), 1);
- assert_eq!(q[0].key, "amount");
- assert_eq!(q[0].value, Some(value), "金额跟着搬到了新行");
-
- // 关系上补了声明
- let declared = utopia_store::graph::relation_types(&pool, kb)
- .await?
- .into_iter()
- .find(|r| r.id == invested)
- .map(|r| r.qualifiers)
- .unwrap_or_default();
- assert_eq!(declared, vec![amount], "采纳时按边上已有的属性补了声明");
-
- sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
- .bind(kb)
- .execute(&pool)
- .await?;
- sqlx::query("DELETE FROM organizations WHERE id = $1")
- .bind(org)
- .execute(&pool)
- .await?;
- Ok(())
-}
diff --git a/crates/utopia-store/tests/store/an_axiom_that_moved_closes_its_findings.rs b/crates/utopia-store/tests/store/an_axiom_that_moved_closes_its_findings.rs
index 48f9a065..ee02cab1 100644
--- a/crates/utopia-store/tests/store/an_axiom_that_moved_closes_its_findings.rs
+++ b/crates/utopia-store/tests/store/an_axiom_that_moved_closes_its_findings.rs
@@ -705,82 +705,3 @@ async fn deleting_a_domain_class_closes_the_signature_finding() -> anyhow::Resul
let cleanup = cleanup(&pool, &f).await;
result.and(cleanup)
}
-
-/// 改判一条零事实的关系成属性:它挂着的公理旗标与双向链接是关系的判据
-/// 贡献,属性不该还留着——`axioms()` 不按 kind 过滤,留着等于留一套
-/// 无人问的公理。同一事务里旗标清掉、链接断开、检出键集照 0062 对账
-#[tokio::test]
-async fn a_demoted_relation_surrenders_its_axioms() -> 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 (manages, supervises) = (Uuid::now_v7(), Uuid::now_v7());
- sqlx::query(
- "INSERT INTO relation_types (id, kb_id, key, label, temporal)
- VALUES ($1, $2, 'supervises', 'supervises', 'state')",
- )
- .bind(supervises)
- .bind(f.kb)
- .execute(&pool)
- .await?;
- // manages:functional + inverse_of=supervises——零事实,够格被改判;
- // supervises.sub_property_of 补一条入向引用,改判也要把它断开
- sqlx::query(
- "INSERT INTO relation_types
- (id, kb_id, key, label, temporal, functional, is_transitive, inverse_of)
- VALUES ($1, $2, 'manages', 'manages', 'state', TRUE, TRUE, $3)",
- )
- .bind(manages)
- .bind(f.kb)
- .bind(supervises)
- .execute(&pool)
- .await?;
- sqlx::query("UPDATE relation_types SET sub_property_of = $2 WHERE id = $1")
- .bind(supervises)
- .bind(manages)
- .execute(&pool)
- .await?;
-
- let converted = ontology::attribute_from_unused_relation(
- &pool,
- f.kb,
- "manages",
- &[f.thing],
- "text",
- None,
- )
- .await?
- .expect("a zero-facts relation should convert");
- anyhow::ensure!(converted == manages);
- let row: (String, bool, bool, Option<Uuid>, Option<Uuid>) = sqlx::query_as(
- "SELECT kind, functional, is_transitive, inverse_of, sub_property_of
- FROM relation_types WHERE id = $1",
- )
- .bind(manages)
- .fetch_one(&pool)
- .await?;
- anyhow::ensure!(row.0 == "attribute", "kind should flip: {}", row.0);
- anyhow::ensure!(!row.1 && !row.2, "axiom flags survived the demotion");
- anyhow::ensure!(
- row.3.is_none() && row.4.is_none(),
- "links survived the demotion"
- );
- // 入向引用同样断开:supervises 不再指向它(axioms() 的归一化不再
- // 把一个属性当派生目标)
- let still_linked: bool = sqlx::query_scalar(
- "SELECT EXISTS(SELECT 1 FROM relation_types WHERE sub_property_of = $1)",
- )
- .bind(manages)
- .fetch_one(&pool)
- .await?;
- anyhow::ensure!(!still_linked, "incoming sub_property_of survived");
- Ok(())
- }
- .await;
- let cleanup = cleanup(&pool, &f).await;
- result.and(cleanup)
-}
diff --git a/crates/utopia-store/tests/store/an_open_statement_keeps_the_documents_words.rs b/crates/utopia-store/tests/store/an_open_statement_keeps_the_documents_words.rs
index c24c985e..17bce2b1 100644
--- a/crates/utopia-store/tests/store/an_open_statement_keeps_the_documents_words.rs
+++ b/crates/utopia-store/tests/store/an_open_statement_keeps_the_documents_words.rs
@@ -291,14 +291,7 @@ async fn an_open_statement_shows_under_its_phrase_and_reuses_its_row() -> anyhow
.await?;
assert!(created && other != fact);
- // 4. 短语不是等着被采纳的说法
- let proposed = graph::proposed_predicates(&pool, f.kb).await?;
- assert!(
- proposed.iter().all(|p| p.form != "acquired"),
- "开放短语不能进采纳候选:{proposed:?}"
- );
-
- // 5. 角色词属性与时间词各自取得回来
+ // 4. 角色词属性与时间词各自取得回来
let qualifiers = graph::statement_qualifiers_for(&pool, &[fact]).await?;
let q = &qualifiers[&fact];
assert_eq!(q.len(), 1);
diff --git a/crates/utopia-store/tests/store/an_untyped_subject_does_not_stall_the_batch.rs b/crates/utopia-store/tests/store/an_untyped_subject_does_not_stall_the_batch.rs
deleted file mode 100644
index 529589c7..00000000
--- a/crates/utopia-store/tests/store/an_untyped_subject_does_not_stall_the_batch.rs
+++ /dev/null
@@ -1,119 +0,0 @@
-//! 主语没有类型的值事实,不该把整批采纳卡死。
-//!
-//! `entities.type_id` 可空——「抽取器抽到了东西,但本体里没有对应的类」是一个
-//! 正常状态(0009)。可 `value_facts_for_forms` 一度把它解成裸 `Uuid`:批里
-//! 只要有一条主语没类型,采纳就在**解码那一步**报错退出,一条也改写不了。
-//!
-//! 这条只能真跑:`cargo check` 看不见 sqlx 的行解码,而那正是出事的地方。
-//! 实测一个库里攒着 2454 条等谓词的值事实,其中 58 条主语无类型。
-
-use sqlx::PgPool;
-use uuid::Uuid;
-
-#[tokio::test]
-async fn an_untyped_subject_still_comes_back_with_the_batch() -> anyhow::Result<()> {
- let Some(url) = utopia_store::test_db::url() else {
- return Ok(());
- };
- let pool = PgPool::connect(&url).await?;
- let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
- let etype = Uuid::now_v7();
- let (infosys, mystery) = (Uuid::now_v7(), Uuid::now_v7());
- let (doc, chunk) = (Uuid::now_v7(), Uuid::now_v7());
- let (f_typed, f_untyped) = (Uuid::now_v7(), Uuid::now_v7());
-
- sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'untyped-subject')")
- .bind(org)
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'untyped-subject')")
- .bind(ws)
- .bind(org)
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'untyped-subject')",
- )
- .bind(kb)
- .bind(ws)
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'org', 'Organization')",
- )
- .bind(etype)
- .bind(kb)
- .execute(&pool)
- .await?;
- // 一个有类型、一个没有——后者正是从前把整批打翻的那种
- sqlx::query("INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1,$2,$3,$4)")
- .bind(infosys)
- .bind(kb)
- .bind(etype)
- .bind("Infosys")
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1,$2,NULL,$3)")
- .bind(mystery)
- .bind(kb)
- .bind("Some Startup")
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO documents (id, kb_id, filename, sha256) VALUES ($1,$2,'a.txt','a')")
- .bind(doc)
- .bind(kb)
- .execute(&pool)
- .await?;
- sqlx::query("INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1,$2,$3,0,'x')")
- .bind(chunk)
- .bind(kb)
- .bind(doc)
- .execute(&pool)
- .await?;
- for (fact, subject) in [(f_typed, infosys), (f_untyped, mystery)] {
- // 值事实:没有谓词、没有宾语实体,原词记在证据上
- sqlx::query(
- "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id, object_value)
- VALUES ($1, $2, $3, NULL, NULL, $4)",
- )
- .bind(fact)
- .bind(kb)
- .bind(subject)
- .bind(serde_json::json!({ "value": "$1 billion", "unit": "$" }))
- .execute(&pool)
- .await?;
- sqlx::query(
- "INSERT INTO fact_evidence (fact_id, chunk_id, document_id, proposed_predicate)
- VALUES ($1, $2, $3, 'pledged_amount')",
- )
- .bind(fact)
- .bind(chunk)
- .bind(doc)
- .execute(&pool)
- .await?;
- }
-
- let run = async {
- let rows =
- utopia_store::graph::value_facts_for_forms(&pool, kb, &["pledged_amount".to_string()])
- .await?;
- assert_eq!(rows.len(), 2, "两条都该回来,没类型的那条不该把批次打翻");
- let typed = rows.iter().filter(|(_, t, _)| t.is_some()).count();
- assert_eq!(typed, 1, "只有一条主语有类型,它是 domain 的唯一来源");
- // 没类型的那条照样在名单里:它不贡献 domain,但要跟着改写,
- // 否则一条有名有姓的事实会继续没有谓词
- assert!(
- rows.iter()
- .any(|(id, t, _)| *id == f_untyped && t.is_none()),
- "没类型的那条该在名单里,且类型是 None"
- );
- Ok::<_, anyhow::Error>(())
- }
- .await;
-
- sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
- .bind(kb)
- .execute(&pool)
- .await?;
- run
-}
diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rs
index 0cbc7470..ab28ed4c 100644
--- a/crates/utopia-store/tests/store/main.rs
+++ b/crates/utopia-store/tests/store/main.rs
@@ -100,10 +100,8 @@ mod a_token_is_the_person_but_not_all_of_them;
mod a_trimmed_description_is_not_stale;
mod a_viewer_never_sees_a_credential;
mod a_wrong_time_can_be_corrected;
-mod adopt_swap;
mod adopting_an_iri_adopts_the_shape;
mod an_agent_can_record;
-mod an_amount_outlives_adoption;
mod an_automatic_merge_is_gated_by_what_it_can_undo;
mod an_axiom_that_moved_closes_its_findings;
mod an_earlier_mention_keeps_the_stated_end;
@@ -116,7 +114,6 @@ mod an_open_statement_keeps_the_documents_words;
mod an_undeclared_name_is_looked_up;
mod an_unknown_date_is_not_an_open_one;
mod an_untyped_name_meets_its_namesake;
-mod an_untyped_subject_does_not_stall_the_batch;
mod an_upload_has_no_date;
mod axioms_judge_the_ledger;
mod axioms_reach_the_database;
@@ -138,11 +135,9 @@ mod human_type_decisions;
mod materialization_is_serial;
mod merge_direction_counts_live_facts_in_each_base;
mod migration_0070_runs_under_any_search_path;
-mod miss_dismissal;
mod negative_binding_definition_edit;
mod no_predicate_still_shows;
mod phrase_signature_evidence;
-mod proposal_counts;
mod review_stages;
mod rss_full_content;
mod rss_ledger_contract;
diff --git a/crates/utopia-store/tests/store/miss_dismissal.rs b/crates/utopia-store/tests/store/miss_dismissal.rs
deleted file mode 100644
index 01530113..00000000
--- a/crates/utopia-store/tests/store/miss_dismissal.rs
+++ /dev/null
@@ -1,105 +0,0 @@
-//! 忽略一个未匹配说法之后会发生什么,打在真库上。
-//!
-//! 这里全是 SQL,`cargo check` 看不见。而这一段的行为**曾经是错的且不可见**:
-//! `record_miss` 带着 `WHERE dismissed_at IS NULL`,于是点一次「忽略」既停止呈现、
-//! 也停止计数。第一篇里出现一次的说法被忽略掉之后,后面二十篇都在用它,
-//! 计数仍停在 1——当初那个判断的依据早就不成立了,而没有任何人看得见。
-//!
-//! 要钉住的是**抑制与计数分开**:
-//!
-//! - 忽略之后 `record_miss` 照样累加
-//! - `list_misses` 不再返回它(提案与自动扩本体一步没变)
-//! - `list_dismissed_misses` 返回它,且带的是**更新后的**计数
-//! - `restore_miss` 撤回之后它回到正常列表,计数是连续的而不是从头来过
-//!
-//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
-
-use sqlx::PgPool;
-use uuid::Uuid;
-
-async fn fresh_kb(pool: &PgPool) -> anyhow::Result<Uuid> {
- let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
- sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'dismissal-test')")
- .bind(org)
- .execute(pool)
- .await?;
- sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'dismissal-test')")
- .bind(ws)
- .bind(org)
- .execute(pool)
- .await?;
- sqlx::query(
- "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'dismissal-test')",
- )
- .bind(kb)
- .bind(ws)
- .execute(pool)
- .await?;
- Ok(kb)
-}
-
-fn count_of(rows: &[utopia_core::models::OntologyMiss], key: &str) -> Option<i32> {
- rows.iter().find(|m| m.key == key).map(|m| m.count)
-}
-
-#[tokio::test]
-async fn dismissing_stops_the_suggestion_but_not_the_counting() -> anyhow::Result<()> {
- let Some(url) = utopia_store::test_db::url() else {
- return Ok(());
- };
- let pool = PgPool::connect(&url).await?;
- let kb = fresh_kb(&pool).await?;
-
- let run = async {
- use utopia_store::ontology as ont;
- // 第一篇文档里出现了一次
- ont::record_miss(&pool, kb, "relation_type", "acquired", Some("A → B")).await?;
- assert_eq!(
- count_of(&ont::list_misses(&pool, kb).await?, "acquired"),
- Some(1)
- );
-
- // 用户看着「出现 1 次」,判断这是一次性措辞
- ont::dismiss_miss(&pool, kb, "relation_type", "acquired").await?;
- assert_eq!(
- count_of(&ont::list_misses(&pool, kb).await?, "acquired"),
- None,
- "忽略之后不该再进建议列表"
- );
-
- // 后面两篇也在说它。**关键断言**:计数必须继续走,否则那个判断
- // 依据过期了也没人知道
- ont::record_miss(&pool, kb, "relation_type", "acquired", Some("C → D")).await?;
- ont::record_miss(&pool, kb, "relation_type", "acquired", Some("E → F")).await?;
- assert_eq!(
- count_of(&ont::list_dismissed_misses(&pool, kb).await?, "acquired"),
- Some(3),
- "忽略期间的出现次数必须照记"
- );
- assert_eq!(
- count_of(&ont::list_misses(&pool, kb).await?, "acquired"),
- None,
- "计数在涨,但抑制照旧"
- );
-
- // 人看见它涨到 3 了,撤回忽略
- ont::restore_miss(&pool, kb, "relation_type", "acquired").await?;
- assert_eq!(
- count_of(&ont::list_misses(&pool, kb).await?, "acquired"),
- Some(3),
- "撤回之后计数是连续的,不是从头来过"
- );
- assert_eq!(
- count_of(&ont::list_dismissed_misses(&pool, kb).await?, "acquired"),
- None
- );
- Ok::<_, anyhow::Error>(())
- }
- .await;
-
- sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
- .bind(kb)
- .execute(&pool)
- .await?;
- run
-}
diff --git a/crates/utopia-store/tests/store/proposal_counts.rs b/crates/utopia-store/tests/store/proposal_counts.rs
deleted file mode 100644
index 95a6db80..00000000
--- a/crates/utopia-store/tests/store/proposal_counts.rs
+++ /dev/null
@@ -1,188 +0,0 @@
-//! `proposed_predicates` 的两个计数,打在真库上。
-//!
-//! 同 [`graph_changes`] 那个测试的理由:这段逻辑整个活在 SQL 字符串里,
-//! `cargo check` 和 clippy 一个字都看不见。这一条尤其需要——它区分的两个数
-//! 长得一模一样(都是 `count(DISTINCT …)`),读代码看不出差别,
-//! 只有在「一部分事实已经离开积压」的数据形状下才分得开。
-//!
-//! 要钉住的行为:
-//!
-//! - **`fact_count` 从积压数** —— 采纳真正会改写的就是那些,多数一条就是承诺
-//! 「将重新归类 N 条」时说了谎
-//! - **`doc_count` 从全量证据数** —— 它回答的是「这个说法在语料里有多普遍」。
-//! 拿积压去数会系统性偏低,而且越用越低:说法被采纳、被谓词匹配接住、
-//! 或被修正作废之后,它的行就离开积压了。一篇一篇往里灌的库因此永远
-//! 攒不够两篇,本体就此冻死
-//!
-//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
-
-use sqlx::PgPool;
-use uuid::Uuid;
-
-/// 造一个刚好能分辨两种口径的账本:
-///
-/// 说法 `acquired` 在**两篇**文档里都出现过,但只有一篇的那条事实**还没有谓词**
-/// ——另一篇那条已经落到真关系上了(谓词匹配接住,或上一轮采纳搬走)。
-///
-/// 于是:积压口径 doc_count = 1(会被 `>=2` 门槛误杀),全量口径 = 2。
-async fn seed(pool: &PgPool) -> anyhow::Result<Uuid> {
- let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
- let etype = Uuid::now_v7();
- let real = Uuid::now_v7();
- let (subj, obj) = (Uuid::now_v7(), Uuid::now_v7());
- let (doc1, doc2) = (Uuid::now_v7(), Uuid::now_v7());
- let (chunk1, chunk2) = (Uuid::now_v7(), Uuid::now_v7());
- let (f_backlog, f_moved) = (Uuid::now_v7(), Uuid::now_v7());
-
- sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'proposal-counts-test')")
- .bind(org)
- .execute(pool)
- .await?;
- sqlx::query(
- "INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'proposal-counts-test')",
- )
- .bind(ws)
- .bind(org)
- .execute(pool)
- .await?;
- sqlx::query(
- "INSERT INTO knowledge_bases (id, workspace_id, name)
- VALUES ($1, $2, 'proposal-counts-test')",
- )
- .bind(kb)
- .bind(ws)
- .execute(pool)
- .await?;
- sqlx::query(
- "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'thing', 'Thing')",
- )
- .bind(etype)
- .bind(kb)
- .execute(pool)
- .await?;
- // 积压的那条不挂任何关系——查询按 predicate_id IS NULL 筛(见 `facts.predicate_id`)
- sqlx::query("INSERT INTO relation_types (id, kb_id, key, label) VALUES ($1, $2, $3, $3)")
- .bind(real)
- .bind(kb)
- .bind("acquired_rel")
- .execute(pool)
- .await?;
- for (id, name) in [(subj, "Anthropic"), (obj, "Humanloop")] {
- sqlx::query(
- "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
- )
- .bind(id)
- .bind(kb)
- .bind(etype)
- .bind(name)
- .execute(pool)
- .await?;
- }
- for (id, name) in [(doc1, "one.txt"), (doc2, "two.txt")] {
- sqlx::query("INSERT INTO documents (id, kb_id, filename, sha256) VALUES ($1, $2, $3, $3)")
- .bind(id)
- .bind(kb)
- .bind(name)
- .execute(pool)
- .await?;
- }
- for (id, doc) in [(chunk1, doc1), (chunk2, doc2)] {
- sqlx::query(
- "INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1, $2, $3, 0, 'x')",
- )
- .bind(id)
- .bind(kb)
- .bind(doc)
- .execute(pool)
- .await?;
- }
-
- // 一条还没有谓词,一条已经落到真关系上
- for (id, pred) in [(f_backlog, None), (f_moved, Some(real))] {
- sqlx::query(
- "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id)
- VALUES ($1, $2, $3, $4, $5)",
- )
- .bind(id)
- .bind(kb)
- .bind(subj)
- .bind(pred)
- .bind(obj)
- .execute(pool)
- .await?;
- }
- // 两条证据都记着同一个原始说法——抽取无条件记它,不只是降级时
- for (fact, chunk, doc) in [(f_backlog, chunk1, doc1), (f_moved, chunk2, doc2)] {
- sqlx::query(
- "INSERT INTO fact_evidence (fact_id, chunk_id, document_id, proposed_predicate)
- VALUES ($1, $2, $3, 'acquired')",
- )
- .bind(fact)
- .bind(chunk)
- .bind(doc)
- .execute(pool)
- .await?;
- }
-
- Ok(kb)
-}
-
-#[tokio::test]
-async fn spread_counts_all_evidence_while_rewrite_count_stays_on_the_backlog() -> anyhow::Result<()>
-{
- let Some(url) = utopia_store::test_db::url() else {
- return Ok(());
- };
- let pool = PgPool::connect(&url).await?;
- let kb = seed(&pool).await?;
-
- let got = utopia_store::graph::proposed_predicates(&pool, kb).await;
-
- sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
- .bind(kb)
- .execute(&pool)
- .await?;
-
- let rows = got?;
- let row = rows
- .iter()
- .find(|r| r.form == "acquired")
- .expect("acquired 应该在提案里");
-
- // 只有一条还没有谓词——采纳会改写的就这一条
- assert_eq!(row.fact_count, 1, "fact_count 应该只数积压");
- // 但这个说法跨了两篇文档。按积压数会得到 1,于是被 `>=2` 的门槛误杀,
- // 而它明明是这个语料的共同词汇
- assert_eq!(row.doc_count, 2, "doc_count 应该数全量证据");
- Ok(())
-}
-
-/// 采纳那条路要按屈折基归并,归并后的篇数得算**并集**——所以它不看
-/// `doc_count`,改问「这个说法出现在哪些文档里」。那是另一条 SQL,
-/// 于是也得另有一个真跑一遍的测试:它一度把 `'relation_type'` 的引号
-/// 丢了(`m.kind = relation_type`),clippy 全绿,任务在运行时才炸。
-#[tokio::test]
-async fn document_ids_come_back_for_each_wording() -> anyhow::Result<()> {
- let Some(url) = utopia_store::test_db::url() else {
- return Ok(());
- };
- let pool = PgPool::connect(&url).await?;
- let kb = seed(&pool).await?;
-
- let got = utopia_store::graph::proposed_predicate_documents(&pool, kb).await;
-
- sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
- .bind(kb)
- .execute(&pool)
- .await?;
-
- let rows = got?;
- let docs: std::collections::HashSet<_> = rows
- .iter()
- .filter(|(form, _)| form == "acquired")
- .map(|(_, doc)| *doc)
- .collect();
- // 两篇文档都用过这个说法,尽管只有一篇那条还没有谓词
- assert_eq!(docs.len(), 2, "两篇都该回来,跟事实落在哪个谓词上无关");
- Ok(())
-}
diff --git a/docs/design/governance.md b/docs/design/governance.md
index 0ff70995..dc1af298 100644
--- a/docs/design/governance.md
+++ b/docs/design/governance.md
@@ -68,9 +68,9 @@ becomes a `derived_contradiction` violation with a clue (stale, wrong merge, uns
repairs (close at a date, retract, go to duplicates, relax the ontology, accept); derived against
derived aggregates per rule pair as `rules_disagree`; a disputed fact is marked where it sits [0017].
-**Switches.** `auto_extend_ontology`, `auto_type_resolution`, `materialize_inferences`, `governance`:
+**Switches.** `auto_type_resolution`, `materialize_inferences`, `governance`:
all default on; every automatic action is visible and reversible, and a switch governs action,
-never attention [0003 d4, 0016 C2, 0002 d6 revised, 0025 d2 revised]. Nods stay with people [0015].
+never attention [0016 C2, 0002 d6 revised, 0025 d2 revised]. Nods stay with people [0015].
**Measured.** `scripts/bench/govern.mjs` against 411 hand-labelled name pairs: 97.7% decided on its
own, 96.7% agreement, 6 wrong merges (4 of them one flipping pair), 12 left for people, 13 minutes
diff --git a/web/src/api.ts b/web/src/api.ts
index 39413a71..614de938 100644
--- a/web/src/api.ts
+++ b/web/src/api.ts
@@ -1,5 +1,5 @@
import type { SourceKind } from "./sourceKinds";
-import { S, lang } from "./i18n";
+import { S } from "./i18n";
import { createParser } from "eventsource-parser";
export class ApiError extends Error {
@@ -1293,12 +1293,6 @@ export interface QuestionReport {
uncovered: { kind_words: number; phrases: number };
}
-export interface OntologyMiss {
- kind: "entity_type" | "relation_type";
- key: string;
- example: string | null;
- count: number;
-}
/** 一条谓词的一端挂着两个以上开放值(#341)。
*
@@ -1413,12 +1407,6 @@ export interface OntologyProposals {
})[];
}
-/** 原文说过、本体里没有、因而事实没有谓词的说法。 */
-export interface ProposedPredicate {
- form: string;
- fact_count: number;
- example: string | null;
-}
/** 一个类/属性在这次导入里的去向。key_taken = key 被另一个 IRI 占着,报告但不动 */
export interface PlannedItem {
@@ -2218,9 +2206,6 @@ export const api = {
request<{
entity_types: EntityTypeView[];
relation_types: RelationTypeView[];
- misses: OntologyMiss[];
- /** 已忽略的,连同它此后继续累积的计数。抑制照旧,只是看得见 */
- dismissed_misses: OntologyMiss[];
}>(`/api/v1/kbs/${kbId}/ontology`),
createEntityType: (kbId: string, body: Record<string, unknown>) =>
request<{ id: string }>(`/api/v1/kbs/${kbId}/ontology/entity-types`, {
@@ -2363,23 +2348,6 @@ export const api = {
/** 两个数(0061 决定 5) */
questionReport: (kbId: string) =>
request<QuestionReport>(`/api/v1/kbs/${kbId}/questions/report`),
- dismissMiss: (kbId: string, kind: string, key: string) =>
- request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/misses/dismiss`, {
- method: "POST",
- body: JSON.stringify({ kind, key }),
- }),
- restoreMiss: (kbId: string, kind: string, key: string) =>
- request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/misses/restore`, {
- method: "POST",
- body: JSON.stringify({ kind, key }),
- }),
- /** reason 只给人看,而人就在这次请求的另一端——所以语言由调用方说,
- 不是后端的设置(docs/decisions/0004)。description 的语言跟知识库走,服务端自己知道 */
- suggestOntology: (kbId: string) =>
- request<OntologyProposals>(`/api/v1/kbs/${kbId}/ontology/suggest`, {
- method: "POST",
- body: JSON.stringify({ locale: lang }),
- }),
/** 上传本体文件只算出计划,一个字节都不写库 */
previewOntologyImport: (kbId: string, file: File) => {
@@ -2404,57 +2372,6 @@ export const api = {
`/api/v1/kbs/${kbId}/ontology/imports`,
),
- proposedPredicates: (kbId: string) =>
- request<{ forms: ProposedPredicate[] }>(
- `/api/v1/kbs/${kbId}/ontology/proposed-predicates`,
- ),
- /** 最近一次自动扩本体做了什么,以及还能不能撤销(撤干净了返回 null) */
- lastAutoExtension: (kbId: string) =>
- request<{
- run: {
- at: string;
- relations: string[] | null;
- classes: string[] | null;
- facts_remapped: number | null;
- batches: string[];
- } | null;
- }>(`/api/v1/kbs/${kbId}/ontology/auto-extension`),
- /** 建关系 **并**把等着它的无谓词事实认过去——后半句才是收益 */
- adoptPredicate: (
- kbId: string,
- body: {
- key: string;
- /** true = key 指的是已有的关系/属性,只改写事实,不建新类型 */
- existing?: boolean;
- /** attribute 走另一条改写路径:值要按 datatype 换算 */
- kind?: "relation" | "attribute";
- datatype?: string;
- unit?: string;
- label?: string;
- temporal?: string;
- functional?: boolean;
- description?: string;
- forms: string[];
- },
- ) =>
- request<{
- id: string;
- remapped: number;
- batch: string;
- /** 值换不动那个 datatype、因而没被改写的条数。改写了 3 条丢下 2 条,
- 只报前半句就是报喜不报忧 */
- unconvertible?: number;
- }>(`/api/v1/kbs/${kbId}/ontology/adopt-predicate`, {
- method: "POST",
- body: JSON.stringify(body),
- }),
- /** 撤销一次采纳:新写的行作废、旧行复活。关系类型留着 */
- unadoptPredicate: (kbId: string, batchId: string) =>
- request<{ reverted: number }>(
- `/api/v1/kbs/${kbId}/ontology/adopt-predicate/${batchId}`,
- { method: "DELETE" },
- ),
-
sources: (kbId: string) =>
request<{ sources: SourceView[] }>(`/api/v1/kbs/${kbId}/sources`),
createSource: (kbId: string, body: Record<string, unknown>) =>
diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts
index fda2c99b..83c780f9 100644
--- a/web/src/i18n/en.ts
+++ b/web/src/i18n/en.ts
@@ -1721,15 +1721,8 @@ export const en = {
? `Closed ${close} · ${review} in Review`
: `Closed ${close}`,
uniquenessSince: (d: string) => `since ${d}`,
- dismiss: "Dismiss",
- dismissed: (n: number) => `Dismissed (${n})`,
/* 数字是**忽略之后**还在涨的那个——这一行的全部意义就在于此:
当初"只出现过一次"的判断依据可能早就不成立了 */
- dismissedHint:
- "Still counted, but kept out of suggestions. If one has grown since you dismissed it, restore it.",
- restore: "Restore",
- suggest: "Suggest with AI",
- suggesting: "Analyzing…",
noMisses: "Nothing the documents say is waiting for a place in the ontology.",
uncovered: (kindWords: number, phrases: number) => {
const what = [
@@ -1746,41 +1739,8 @@ export const en = {
mapOver: "Use existing",
/* 影响面:采纳一个提案会把多少条无谓词事实认过去。
没有这一句,"Add" 只是凭空多一个空关系 */
- willRemap: (n: number) =>
- n === 1 ? "reclassifies 1 fact" : `reclassifies ${n} facts`,
- adopted: (n: number) =>
- n === 1
- ? "Added — 1 fact reclassified"
- : `Added — ${n} facts reclassified`,
- /* 一部分值换不动这个类型就没被改写。只报改写了多少条是报喜不报忧 */
- adoptedPartly: (moved: number, left: number) =>
- `Added — ${moved} reclassified, ${left} left behind (value did not fit the type)`,
- /* 撤销:采纳改写了成批事实,没有回头路的话没人敢点第一下 */
- undoAdopt: (key: string, n: number) =>
- `${key} added, ${n} fact${n === 1 ? "" : "s"} reclassified`,
- undoAdoptBtn: "Undo",
- reverted: (n: number) =>
- n === 1 ? "Reverted — 1 fact restored" : `Reverted — ${n} facts restored`,
- undoKeepsRelation: "The relation stays; only the facts move back.",
- /* 撤销要二次确认:它一次改回成批事实 */
- undoTitle: "Undo this ontology change?",
- undoHint: (n: number) =>
- `${n} fact${n === 1 ? "" : "s"} will go back to “related to”. The relation itself stays — ` +
- `nothing is deleted, and you can adopt it again later.`,
- undoConfirm: "Undo",
- undoCancel: "Keep",
/* 自动扩本体的通知:默认开启的前提是它的动作可见且可退。
只记在审计台账里不算可见——那是查证用的,不是通知用的 */
- autoRanTitle: "Utopia extended this ontology from your documents",
- autoRanBody: (rels: string[], facts: number) =>
- `Added ${rels.join(", ")} · ${facts} fact${facts === 1 ? "" : "s"} reclassified`,
- autoRanOff: "Turn this off in knowledge base settings.",
- /* 批量:常见情形是"这些都对",一条条点是把一个决定拆成八个 */
- addAll: (n: number) => `Add all ${n}`,
- addingAll: "Adding…",
- addAllLabel: "batch",
- addAllPartial: (keys: string[]) =>
- `Some could not be added: ${keys.join(", ")} — the rest went through.`,
proposals: "AI proposals",
keyHint: "lowercase_snake_case",
/* ---- 0061: the ontology agent and its questions ---- */
@@ -2351,14 +2311,6 @@ export const en = {
title: "Knowledge base settings",
general: "General",
members: "Members",
- /* 自动扩本体开关。说明必须讲清关掉之后失去的**只是**代劳,不是留意——
- 否则用户会以为关掉它就看不到未匹配的信号了 */
- autoExtend: "Extend the ontology automatically",
- autoExtendNote:
- "When extraction meets a relation this ontology does not have, add it and reclassify the " +
- "facts that were waiting for it. Every change is listed and can be undone. Turning this " +
- "off does not stop Utopia from noticing — the phrases still collect under Unmatched, they " +
- "just wait for you to approve them.",
materialize: "Materialize inferences",
materializeNote:
"Write facts the ontology entails into the ledger — transitive chains and symmetric pairs. " +
diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.ts
index a5dd7d16..c804902d 100644
--- a/web/src/i18n/zh.ts
+++ b/web/src/i18n/zh.ts
@@ -1507,13 +1507,6 @@ export const zh: Strings = {
uniquenessDone: (close: number, review: number) =>
review > 0 ? `已闭合 ${close} 条 · ${review} 条待审` : `已闭合 ${close} 条`,
uniquenessSince: (d: string) => `自 ${d}`,
- dismiss: "忽略",
- dismissed: (n: number) => `已忽略(${n})`,
- dismissedHint:
- "仍在计数,只是不进建议。如果某一个在你忽略之后涨了,可以撤回。",
- restore: "撤回忽略",
- suggest: "用 AI 建议",
- suggesting: "分析中…",
noMisses: "文档说到的东西里,没有在等本体给位置的。",
uncovered: (kindWords: number, phrases: number) => {
const what = [
@@ -1526,30 +1519,6 @@ export const zh: Strings = {
},
approve: "加入",
mapOver: "用已有的",
- willRemap: (n: number) => `将重新归类 ${n} 条事实`,
- adopted: (n: number) => `已加入——${n} 条事实已重新归类`,
- adoptedPartly: (moved: number, left: number) =>
- `已加入——${moved} 条已重新归类,${left} 条留在原处(值与类型对不上)`,
- undoAdopt: (key: string, n: number) =>
- `已加入 ${key},${n} 条事实已重新归类`,
- undoAdoptBtn: "撤销",
- reverted: (n: number) => `已撤销——${n} 条事实已复原`,
- undoKeepsRelation: "关系保留;只有事实被移回去。",
- undoTitle: "撤销这次本体变更?",
- undoHint: (n: number) =>
- `${n} 条事实会回到「related to」。关系本身保留——` +
- `什么都不会被删除,之后仍可再次采纳。`,
- undoConfirm: "撤销",
- undoCancel: "保持",
- autoRanTitle: "Utopia 依据你的文档扩展了这个本体",
- autoRanBody: (rels: string[], facts: number) =>
- `新增 ${rels.join("、")} · ${facts} 条事实已重新归类`,
- autoRanOff: "可在知识库设置里关掉它。",
- addAll: (n: number) => `全部加入(${n})`,
- addingAll: "加入中…",
- addAllLabel: "批量",
- addAllPartial: (keys: string[]) =>
- `有几个没能加入:${keys.join("、")}——其余的已经成功。`,
proposals: "AI 提案",
keyHint: "小写下划线命名",
/* ---- 0061:本体代理与能力问题 ---- */
@@ -2050,11 +2019,6 @@ export const zh: Strings = {
title: "知识库设置",
general: "常规",
members: "成员",
- autoExtend: "自动扩展本体",
- autoExtendNote:
- "当抽取遇到这个本体里没有的关系时,把它加进来,并把一直等着它的那些事实重新归类。" +
- "每一次变更都会列出,并且可以撤销。关掉它并不会让 Utopia 不再留意——" +
- "那些说法仍然会汇集到「未匹配」下,只是要等你点头。",
materialize: "物化推理",
materializeNote:
"把本体蕴含的事实写进账本——传递链与对称对。它们带派生标记、单列一段,背后的声明要是错了,撤掉只要一下。",
diff --git a/web/src/pages/KbSettings.tsx b/web/src/pages/KbSettings.tsx
index 75377cd0..e35584cc 100644
--- a/web/src/pages/KbSettings.tsx
+++ b/web/src/pages/KbSettings.tsx
@@ -113,7 +113,6 @@ export function KbSettings() {
const [name, setName] = useState("");
const [desc, setDesc] = useState("");
const [visibility, setVisibility] = useState<"open" | "restricted">("open");
- const [autoExtend, setAutoExtend] = useState(true);
// **默认关**,与上面那个相反:推理往账本里写事实,而声明可能是错的
const [materialize, setMaterialize] = useState(false);
// 类型消解自动跑(0016 C2):只自动落地子树内精化的那一档
@@ -130,7 +129,6 @@ export function KbSettings() {
setName(kb.data.name);
setDesc(kb.data.description ?? "");
setVisibility(kb.data.visibility);
- setAutoExtend(kb.data.auto_extend_ontology);
setMaterialize(kb.data.materialize_inferences);
setAutoResolve(kb.data.auto_type_resolution);
setGovernance(kb.data.governance);
@@ -171,7 +169,6 @@ export function KbSettings() {
desc.trim() !== (kb.data.description ?? "");
const visibilityDirty = visibility !== kb.data.visibility;
const automationDirty =
- autoExtend !== kb.data.auto_extend_ontology ||
materialize !== kb.data.materialize_inferences ||
autoResolve !== kb.data.auto_type_resolution ||
governance !== kb.data.governance ||
@@ -327,7 +324,6 @@ export function KbSettings() {
disabled={!automationDirty || saveAutomation.isPending}
onClick={() =>
saveAutomation.mutate({
- auto_extend_ontology: autoExtend,
materialize_inferences: materialize,
auto_type_resolution: autoResolve,
governance,
@@ -341,16 +337,7 @@ export function KbSettings() {
}
>
<div className="space-y-3">
- {/* 自动扩本体:默认开,因为新库的十个默认关系不是任何人选的。
- 说明里要讲清关掉之后失去的**只是**代劳,不是留意 */}
- <Checkbox
- checked={autoExtend}
- onChange={(v) => setAutoExtend(v)}
- label={S.kbset.autoExtend}
- hint={S.kbset.autoExtendNote}
- />
- {/* 物化推理:**默认关**,与上面那个相反。自动扩本体动的是词表,
- 这个动的是账本——它按公理往图里写事实,而声明可能是错的 */}
+ {/* 物化推理:**默认关**。它动的是账本——按公理往图里写事实,而声明可能是错的 */}
<Checkbox
checked={materialize}
onChange={(v) => setMaterialize(v)}
diff --git a/web/src/pages/Ontology.tsx b/web/src/pages/Ontology.tsx
index d5fa0900..0e096282 100644
--- a/web/src/pages/Ontology.tsx
+++ b/web/src/pages/Ontology.tsx
@@ -28,7 +28,6 @@ import {
type EntityTypeView,
type ImportPlan,
type OntologyImportView,
- type OntologyMiss,
type AgentProposalFields,
type CompetencyQuestion,
type PlannedItem,
@@ -53,7 +52,6 @@ import {
import {
Button,
Chip,
- DangerConfirm,
Dialog,
Disclosure,
IconButton,
@@ -270,8 +268,6 @@ export function Ontology() {
const ont = data.data;
const entity_types = ont?.entity_types ?? [];
const relation_types = ont?.relation_types ?? [];
- const misses = ont?.misses ?? [];
- const dismissed_misses = ont?.dismissed_misses ?? [];
// 属性不进 Properties 列表:它们挂在类下,在类详情区编辑
const relations = relation_types.filter((r) => r.kind !== "attribute");
// 面板要演完退场再卸载:一取消选中就 unmount 是瞬间消失(图谱页同一个做法)。
@@ -464,7 +460,6 @@ export function Ontology() {
<RailItem
active={sel?.kind === "misses"}
icon={<Inbox size={14} />}
- count={loading ? undefined : misses.length}
onClick={() => setSel({ kind: "misses" })}
>
{S.ontology.missesShort}
@@ -532,8 +527,6 @@ export function Ontology() {
<div>
<MissesPanel
kbId={kb.id}
- misses={misses}
- dismissedMisses={dismissed_misses ?? []}
onChanged={refresh}
onError={onError}
/>
@@ -1813,38 +1806,17 @@ function UniquenessPanel({
);
}
-/** Suggest 提的那部分:批量加入只吃它们。代理提的每条都是人签的契约(0012),不进「全部加入」 */
-function manualOnly(d: OntologyProposals): OntologyProposals {
- const own = <T extends { proposed_by?: string }>(xs: T[] | undefined) =>
- (xs ?? []).filter((x) => x.proposed_by !== "agent");
- return {
- entity_types: own(d.entity_types),
- relation_types: own(d.relation_types),
- attribute_types: own(d.attribute_types),
- map_to: own(d.map_to),
- };
-}
-function manualCount(d: OntologyProposals): number {
- const m = manualOnly(d);
+/** 库里现在有几条还没人表态的提案:等代理答完的判据,也是「有没有可看的」 */
+function proposalCount(d: OntologyProposals | null | undefined): number {
+ if (!d) return 0;
return (
- m.entity_types.length +
- m.relation_types.length +
- (m.attribute_types?.length ?? 0) +
- (m.map_to?.length ?? 0)
+ (d.map_to?.length ?? 0) +
+ d.entity_types.length +
+ d.relation_types.length +
+ (d.attribute_types?.length ?? 0)
);
}
-/** 库里现在有几条代理提的提案:等代理答完的判据 */
-function agentCount(d: OntologyProposals | null | undefined): number {
- if (!d) return 0;
- return [
- ...(d.map_to ?? []),
- ...d.entity_types,
- ...d.relation_types,
- ...(d.attribute_types ?? []),
- ].filter((p) => p.proposed_by === "agent").length;
-}
-
/** 代理提案多带的几格(0061):定义、会绑的形状、类别词、服务的问题。
Suggest 的一份没有 proposed_by,什么都不画 */
function AgentMeta({
@@ -2119,14 +2091,10 @@ function QuestionsPanel({
function MissesPanel({
kbId,
- misses,
- dismissedMisses,
onChanged,
onError,
}: {
kbId: string;
- misses: OntologyMiss[];
- dismissedMisses: OntologyMiss[];
onChanged: () => void;
onError: (e: unknown) => void;
}) {
@@ -2135,20 +2103,14 @@ function MissesPanel({
queryKey: ["questionReport", kbId],
queryFn: () => api.questionReport(kbId),
}).data?.uncovered;
- // 默认收起:已忽略的是**背景信息**,不该跟待处理的挤在一起抢注意力
- const [showDismissed, setShowDismissed] = useState(false);
const [proposals, setProposals] = useState<OntologyProposals | null>(null);
- // 上次算出来、还没人表态的那些:刷新页面之后从库里捞回来(0049)。
- //
- // 从前这里只有上面那个 useState——刷新一次、切走一次,整批提议就没了,
- // 想再看只能重跑一次模型,而重跑未必给出同一批归并。归并了哪些说法正是
- // 唯一能查证过并的东西(0003 的 optimized_for → runs_on 就是这么抓出来的)
// 叫了代理之后等它:任务在后台跑,隔几秒看一眼库里有没有新提案(0061)。
- // 记的是叫它那一刻代理提案的条数——多出来了就是它答完了;两分钟没动静就不等了
+ // 记的是叫它那一刻提案的条数——多出来了就是它答完了;两分钟没动静就不等了
const [awaitingAgent, setAwaitingAgent] = useState<{
since: number;
had: number;
} | null>(null);
+ // 还没人表态的提案存在库里(0049):刷新页面、切走再回来都从这里捞
const storedProposals = useQuery({
queryKey: ["storedProposals", kbId],
queryFn: () => api.storedProposals(kbId),
@@ -2157,52 +2119,20 @@ function MissesPanel({
useEffect(() => {
if (!awaitingAgent || !storedProposals.data) return;
const d = storedProposals.data;
- const now = agentCount(d);
- if (now > awaitingAgent.had || Date.now() - awaitingAgent.since > 120_000) {
+ if (
+ proposalCount(d) > awaitingAgent.had ||
+ Date.now() - awaitingAgent.since > 120_000
+ ) {
setProposals(d);
setAwaitingAgent(null);
}
}, [awaitingAgent, storedProposals.data]);
useEffect(() => {
- // 只在还没有本地结果时回填。刚点完 Suggest 的那一批更新,不该被覆盖
+ // 只在还没有本地结果时回填:刚表过态的那一批不该被库里的旧快照盖回去
if (proposals === null && storedProposals.data) {
- const d = storedProposals.data;
- const empty =
- !d.entity_types?.length &&
- !d.relation_types?.length &&
- !d.attribute_types?.length &&
- !d.map_to?.length;
- if (!empty) setProposals(d);
+ if (proposalCount(storedProposals.data) > 0) setProposals(storedProposals.data);
}
}, [storedProposals.data, proposals]);
- // 最近一次采纳,供撤销。只留最近一次——旧批次要撤销走审计台账,
- // 那里本来就记着每次采纳动了哪个关系、多少条
- const [lastAdopt, setLastAdopt] = useState<{
- batches: string[];
- key: string;
- moved: number;
- } | null>(null);
- // 撤销要二次确认:一次改回成批事实
- const [confirmUndo, setConfirmUndo] = useState<{
- batches: string[];
- moved: number;
- } | null>(null);
- // 系统自动扩过本体没有——横幅要据此显示,撤干净了后端返回 null
- const autoRun = useQuery({
- queryKey: ["auto-extension", kbId],
- queryFn: () => api.lastAutoExtension(kbId),
- });
- // 待认领的表层谓词:提案的影响面("将改写 57 条")从这里算
- const surface = useQuery({
- queryKey: ["proposed-predicates", kbId],
- queryFn: () => api.proposedPredicates(kbId),
- });
- const factsWaiting = (forms: string[]) => {
- const byForm = new Map(
- (surface.data?.forms ?? []).map((f) => [f.form, f.fact_count]),
- );
- return forms.reduce((n, f) => n + (byForm.get(f) ?? 0), 0);
- };
const askAgent = useMutation({
mutationFn: () => api.proposeOntology(kbId),
@@ -2210,11 +2140,20 @@ function MissesPanel({
toast.info(S.ontology.agentQueued);
setAwaitingAgent({
since: Date.now(),
- had: agentCount(storedProposals.data ?? proposals),
+ had: proposalCount(storedProposals.data ?? proposals),
});
},
onError,
});
+ type Section = "map_to" | "entity_types" | "relation_types" | "attribute_types";
+ const drop = (section: Section, key: string) =>
+ setProposals(
+ (prev) =>
+ prev && {
+ ...prev,
+ [section]: (prev[section] ?? []).filter((x) => x.key !== key),
+ },
+ );
// 拒绝连理由:改状态不删行,理由下一轮代理读得到(0061)
const rejectProposal = useMutation({
mutationFn: ({
@@ -2222,334 +2161,52 @@ function MissesPanel({
key,
reason,
}: {
- section: "map_to" | "entity_types" | "relation_types" | "attribute_types";
+ section: Section;
key: string;
reason?: string;
}) => api.decideProposal(kbId, section, key, "rejected", reason),
onSuccess: (_d, v) => {
toast.success(S.ontology.rejectedProposal);
- setProposals(
- (prev) =>
- prev && {
- ...prev,
- [v.section]: (prev[v.section] ?? []).filter((x) => x.key !== v.key),
- },
- );
+ drop(v.section, v.key);
},
onError,
});
- const reject = (
- section: "map_to" | "entity_types" | "relation_types" | "attribute_types",
- key: string,
- ) => {
+ const reject = (section: Section, key: string) => {
const reason = window.prompt(S.ontology.rejectReasonPrompt) ?? "";
rejectProposal.mutate({ section, key, reason: reason.trim() || undefined });
};
- const suggest = useMutation({
- mutationFn: () => api.suggestOntology(kbId),
- onSuccess: setProposals,
- onError,
- });
- const dismiss = useMutation({
- mutationFn: ({ kind, key }: { kind: string; key: string }) =>
- api.dismissMiss(kbId, kind, key),
- onSuccess: onChanged,
- onError,
- });
- const restore = useMutation({
- mutationFn: ({ kind, key }: { kind: string; key: string }) =>
- api.restoreMiss(kbId, kind, key),
- onSuccess: onChanged,
- onError,
- });
- const approveEntity = useMutation({
- // 代理提的走采纳端点:服务端建类、标记提案、排对齐(0061 决定 3)
- mutationFn: (p: {
- key: string;
- label: string;
- description?: string;
- proposed_by?: string;
- }) =>
- p.proposed_by === "agent"
- ? api.adoptProposal(kbId, "entity_types", p.key)
- : api.createEntityType(kbId, {
- key: p.key,
- label: p.label,
- description: p.description,
- }),
- onSuccess: (_data, p) => {
- // 已采纳:从提案列表移除,并顺带清掉对应的未匹配统计 chip(本体已覆盖)
- toast.success(S.toast.added);
- setProposals(
- (prev) =>
- prev && {
- ...prev,
- entity_types: prev.entity_types.filter((x) => x.key !== p.key),
- },
- );
- api.dismissMiss(kbId, "entity_type", p.key).catch(() => {});
- // 提案表态落库(0049):下一轮 Suggest 不会把它刷回待看
- api.decideProposal(kbId, "entity_types", p.key, "adopted").catch(() => {});
- onChanged();
- },
- onError,
- });
- const approveRelation = useMutation({
- // 带 forms 的提案走 adopt:建关系顺带把等着它的无谓词事实认过去。
- // 只建关系的话本体长大了、图没变好——那些事实会继续是"有关联"
- mutationFn: (p: {
- key: string;
- label: string;
- temporal?: string;
- functional?: boolean;
- description?: string;
- forms?: string[];
- proposed_by?: string;
- }) =>
- p.proposed_by === "agent"
- ? api.adoptProposal(kbId, "relation_types", p.key)
- : p.forms?.length
- ? api.adoptPredicate(kbId, {
- key: p.key,
- label: p.label,
- temporal: p.temporal ?? "state",
- functional: p.functional ?? false,
- description: p.description,
- forms: p.forms,
- })
- : api.createRelationType(kbId, {
- key: p.key,
- label: p.label,
- temporal: p.temporal ?? "state",
- functional: p.functional ?? false,
- description: p.description,
- }),
- onSuccess: (data, p) => {
- const d = data as { remapped?: number; batch?: string };
- const moved = d.remapped ?? 0;
- toast.success(moved > 0 ? S.ontology.adopted(moved) : S.toast.added);
- // 撤销的把手:采纳改写了成批事实,没有回头路的话没人敢点第一下
- if (moved > 0 && d.batch)
- setLastAdopt({ batches: [d.batch], key: p.key, moved });
- setProposals(
- (prev) =>
- prev && {
- ...prev,
- relation_types: prev.relation_types.filter((x) => x.key !== p.key),
- },
- );
- api.dismissMiss(kbId, "relation_type", p.key).catch(() => {});
- api.decideProposal(kbId, "relation_types", p.key, "adopted").catch(() => {});
- onChanged();
- },
- onError,
- });
-
- // 逐条串行而不是加个批量端点:每个谓词各有自己的批次和撤销粒度,
- // 而且部分失败能如实报告("5 个成功,1 个 key 已存在")而不是整批回滚
- // 属性提案:宾语是字面值的那些。走同一个采纳入口,但值要按 datatype
- // 换算,换不动的不改写——所以回执里的 unconvertible 必须说出来
- const approveAttribute = useMutation({
- mutationFn: (p: {
- key: string;
- label: string;
- datatype?: string;
- unit?: string;
- description?: string;
- forms?: string[];
- proposed_by?: string;
- }) =>
- p.proposed_by === "agent"
- ? api
- .adoptProposal(kbId, "attribute_types", p.key)
- .then(() => ({}) as Awaited<ReturnType<typeof api.adoptPredicate>>)
- : api.adoptPredicate(kbId, {
- key: p.key,
- kind: "attribute",
- label: p.label,
- datatype: p.datatype ?? "text",
- unit: p.unit,
- description: p.description,
- forms: p.forms ?? [],
- }),
- onSuccess: (data, p) => {
- const moved = data.remapped ?? 0;
- const left = data.unconvertible ?? 0;
- toast.success(
- left > 0
- ? S.ontology.adoptedPartly(moved, left)
- : moved > 0
- ? S.ontology.adopted(moved)
- : S.toast.added,
- );
- if (moved > 0 && data.batch)
- setLastAdopt({ batches: [data.batch], key: p.key, moved });
- setProposals(
- (prev) =>
- prev && {
- ...prev,
- attribute_types: (prev.attribute_types ?? []).filter(
- (x) => x.key !== p.key,
- ),
- },
- );
- for (const form of p.forms ?? [])
- api.dismissMiss(kbId, "attribute_type", form).catch(() => {});
- api.decideProposal(kbId, "attribute_types", p.key, "adopted").catch(() => {});
- onChanged();
- },
- onError,
- });
- // 映射到已有类型:不建东西,只把这些说法的事实挂过去。
- // 跟新建走同一个采纳入口,因为它对图做的事一模一样——也因此同样可撤销
- const approveMapping = useMutation({
- // 代理提的「已有」:形状各写一条人的绑定判定(0061 cut 1.1),走采纳端点
- mutationFn: (p: {
- key: string;
- kind?: string;
- forms?: string[];
- proposed_by?: string;
- }) =>
- p.proposed_by === "agent"
- ? api
- .adoptProposal(kbId, "map_to", p.key)
- .then(() => ({}) as Awaited<ReturnType<typeof api.adoptPredicate>>)
- : api.adoptPredicate(kbId, {
- key: p.key,
- existing: true,
- // 目标是属性时值要按它的 datatype 换算,服务端据此分道
- kind: p.kind === "attribute" ? "attribute" : "relation",
- forms: p.forms ?? [],
- }),
- onSuccess: (data, p) => {
- const moved = data.remapped ?? 0;
- const left = data.unconvertible ?? 0;
- toast.success(
- left > 0
- ? S.ontology.adoptedPartly(moved, left)
- : moved > 0
- ? S.ontology.adopted(moved)
- : S.toast.saved,
- );
- if (moved > 0 && data.batch)
- setLastAdopt({ batches: [data.batch], key: p.key, moved });
- setProposals(
- (prev) =>
- prev && {
- ...prev,
- map_to: (prev.map_to ?? []).filter((x) => x.key !== p.key),
- },
- );
- for (const form of p.forms ?? [])
- api.dismissMiss(kbId, "relation_type", form).catch(() => {});
- onChanged();
- },
- onError,
- });
- const addAll = useMutation({
- mutationFn: async (all: OntologyProposals) => {
- const batches: string[] = [];
- let moved = 0;
- const failed: string[] = [];
- for (const p of all.entity_types) {
- try {
- await api.createEntityType(kbId, { key: p.key, label: p.label });
- } catch {
- failed.push(p.key);
- }
- }
- for (const p of all.relation_types) {
- try {
- if (p.forms?.length) {
- const r = await api.adoptPredicate(kbId, {
- key: p.key,
- label: p.label,
- temporal: p.temporal ?? "state",
- functional: p.functional ?? false,
- description: p.description,
- forms: p.forms,
- });
- moved += r.remapped;
- if (r.remapped > 0) batches.push(r.batch);
- } else {
- await api.createRelationType(kbId, {
- key: p.key,
- label: p.label,
- temporal: p.temporal ?? "state",
- functional: p.functional ?? false,
- description: p.description,
- });
- }
- } catch {
- failed.push(p.key);
- }
- }
- for (const p of all.attribute_types ?? []) {
- if (!p.forms?.length) continue;
- try {
- const r = await api.adoptPredicate(kbId, {
- key: p.key,
- kind: "attribute",
- label: p.label,
- datatype: p.datatype ?? "text",
- unit: p.unit,
- description: p.description,
- forms: p.forms,
- });
- moved += r.remapped;
- if (r.remapped > 0) batches.push(r.batch);
- } catch {
- failed.push(p.key);
- }
- }
- for (const p of all.map_to ?? []) {
- if (!p.forms?.length) continue;
- try {
- const r = await api.adoptPredicate(kbId, {
- key: p.key,
- existing: true,
- kind: p.kind === "attribute" ? "attribute" : "relation",
- forms: p.forms,
- });
- moved += r.remapped;
- if (r.remapped > 0) batches.push(r.batch);
- } catch {
- failed.push(p.key);
- }
- }
- return { batches, moved, failed };
- },
- onSuccess: (r) => {
- if (r.failed.length) toast.error(S.ontology.addAllPartial(r.failed));
- else toast.success(S.ontology.adopted(r.moved));
- if (r.batches.length)
- setLastAdopt({
- batches: r.batches,
- key: S.ontology.addAllLabel,
- moved: r.moved,
- });
- setProposals(null);
- onChanged();
- },
- onError,
- });
-
- const unadopt = useMutation({
- mutationFn: async (batches: string[]) => {
- let reverted = 0;
- for (const b of batches)
- reverted += (await api.unadoptPredicate(kbId, b)).reverted;
- return { reverted };
- },
- onSuccess: (r) => {
- toast.success(S.ontology.reverted(r.reverted));
- setLastAdopt(null);
- setConfirmUndo(null);
- autoRun.refetch();
+ // 采纳走服务端一个端点:建类或属性(或写下人的绑定)、标记提案、排对齐(0061 决定 3)
+ const adopt = useMutation({
+ mutationFn: ({ section, key }: { section: Section; key: string }) =>
+ api.adoptProposal(kbId, section, key),
+ onSuccess: (_d, v) => {
+ toast.success(v.section === "map_to" ? S.toast.saved : S.toast.added);
+ drop(v.section, v.key);
onChanged();
},
onError,
});
+ const actions = (section: Section, key: string, label: string) => (
+ <>
+ <Button
+ variant="primary"
+ size="sm"
+ className="ml-auto"
+ onClick={() => adopt.mutate({ section, key })}
+ disabled={adopt.isPending}
+ >
+ {label}
+ </Button>
+ <Button
+ size="sm"
+ variant="secondary"
+ onClick={() => reject(section, key)}
+ disabled={rejectProposal.isPending}
+ >
+ {S.ontology.rejectProposal}
+ </Button>
+ </>
+ );
return (
// 整页视图不再套一层卡片:标题是页标题,正文平铺(与 Refine / Rules 同一副样子)
@@ -2558,203 +2215,31 @@ function MissesPanel({
title={S.ontology.misses}
sub={S.ontology.missesHint}
actions={
- <>
- {misses.length > 0 && (
- <Button
- size="sm"
- variant="secondary"
- onClick={() => suggest.mutate()}
- disabled={suggest.isPending}
- >
- {suggest.isPending ? S.ontology.suggesting : S.ontology.suggest}
- </Button>
- )}
- <Button
- size="sm"
- variant="secondary"
- title={S.ontology.askAgentHint}
- onClick={() => askAgent.mutate()}
- disabled={askAgent.isPending || awaitingAgent !== null}
- >
- {awaitingAgent ? S.ontology.agentWaiting : S.ontology.askAgent}
- </Button>
- </>
- }
- />
-
- {misses.length === 0 ? (
- // 两个数还没取回来时不说话:先说一句「没有在等的」再改口,比空着更糟
- uncovered && (
- <p className="text-body text-ink-2">
- {uncovered.kind_words + uncovered.phrases > 0
- ? S.ontology.uncovered(uncovered.kind_words, uncovered.phrases)
- : S.ontology.noMisses}
- </p>
- )
- ) : (
- <div className="flex flex-wrap gap-2">
- {misses.map((m) => (
- <span
- key={`${m.kind}:${m.key}`}
- className="glass rounded-cell px-3 py-1 text-small flex items-center gap-2"
- title={m.example ?? ""}
- >
- <Chip tone={m.kind === "entity_type" ? "info" : "violet"}>
- {m.kind === "entity_type" ? "C" : "P"}
- </Chip>
- <span className="font-mono text-ink-2">{m.key}</span>
- <span className="text-ink-2">×{m.count}</span>
- <IconButton
- size="sm"
- label={S.ontology.dismiss}
- className="-mr-1"
- onClick={() => dismiss.mutate({ kind: m.kind, key: m.key })}
- >
- ✕
- </IconButton>
- </span>
- ))}
- </div>
- )}
- {dismissedMisses.length > 0 && (
- <div className="mt-3 border-t border-line pt-3">
- <Button
- variant="ghost"
- size="sm"
- className="-ml-2"
- onClick={() => setShowDismissed((v) => !v)}
- >
- {showDismissed ? "▾" : "▸"} {S.ontology.dismissed(dismissedMisses.length)}
- </Button>
- {showDismissed && (
- <>
- <p className="text-small text-ink-2 mt-2 mb-2">
- {S.ontology.dismissedHint}
- </p>
- <div className="flex flex-wrap gap-2">
- {dismissedMisses.map((m) => (
- <span
- key={`d:${m.kind}:${m.key}`}
- className="glass rounded-cell px-3 py-1 text-small flex items-center gap-2 opacity-60"
- title={m.example ?? ""}
- >
- <Chip tone={m.kind === "entity_type" ? "info" : "violet"}>
- {m.kind === "entity_type" ? "C" : "P"}
- </Chip>
- <span className="font-mono text-ink-2 line-through">
- {m.key}
- </span>
- <span className="text-ink-2">×{m.count}</span>
- <IconButton
- size="sm"
- label={S.ontology.restore}
- className="-mr-1"
- onClick={() => restore.mutate({ kind: m.kind, key: m.key })}
- >
- ↺
- </IconButton>
- </span>
- ))}
- </div>
- </>
- )}
- </div>
- )}
- {/* 系统自己动了本体,必须让人看见——只记在审计台账里不算可见。
- 默认开启的前提是它的动作可见且可退,这条横幅是"可见"那一半 */}
- {autoRun.data?.run && !lastAdopt && (
- <div className="mt-3 rounded-panel border border-line-strong bg-surface px-3 py-3">
- <div className="flex items-start gap-2">
- <div className="min-w-0 flex-1">
- <p className="text-small text-ink">
- {S.ontology.autoRanTitle}
- </p>
- <p className="mt-1 text-fine text-ink-2">
- {S.ontology.autoRanBody(
- autoRun.data.run.relations ?? [],
- autoRun.data.run.facts_remapped ?? 0,
- )}
- </p>
- <p className="mt-1 text-fine text-ink-2">
- {S.ontology.autoRanOff}
- </p>
- </div>
- <Button
- size="sm"
- variant="secondary"
- disabled={unadopt.isPending}
- onClick={() =>
- setConfirmUndo({
- batches: autoRun.data!.run!.batches,
- moved: autoRun.data!.run!.facts_remapped ?? 0,
- })
- }
- >
- {S.ontology.undoAdoptBtn}
- </Button>
- </div>
- </div>
- )}
-
- {/* 采纳改写了成批事实——没有回头路的话没人敢点第一下 */}
- {lastAdopt && (
- <div className="mt-3 flex items-center gap-2 rounded-panel border border-line bg-surface px-3 py-2">
- <span className="text-small text-ink-2">
- {S.ontology.undoAdopt(lastAdopt.key, lastAdopt.moved)}
- </span>
- <span className="text-fine text-ink-2">
- {S.ontology.undoKeepsRelation}
- </span>
<Button
size="sm"
variant="secondary"
- className="ml-auto"
- disabled={unadopt.isPending}
- onClick={() =>
- setConfirmUndo({
- batches: lastAdopt.batches,
- moved: lastAdopt.moved,
- })
- }
+ title={S.ontology.askAgentHint}
+ onClick={() => askAgent.mutate()}
+ disabled={askAgent.isPending || awaitingAgent !== null}
>
- {S.ontology.undoAdoptBtn}
+ {awaitingAgent ? S.ontology.agentWaiting : S.ontology.askAgent}
</Button>
- </div>
- )}
+ }
+ />
- {/* 撤销一次改回成批事实:轻确认——它本身也是可逆的,不必打字解锁 */}
- {confirmUndo && (
- <DangerConfirm
- title={S.ontology.undoTitle}
- hint={S.ontology.undoHint(confirmUndo.moved)}
- confirmLabel={S.ontology.undoConfirm}
- cancelLabel={S.ontology.undoCancel}
- busy={unadopt.isPending}
- onConfirm={() => unadopt.mutate(confirmUndo.batches)}
- onCancel={() => setConfirmUndo(null)}
- />
+ {/* 两个数还没取回来时不说话:先说一句「没有在等的」再改口,比空着更糟 */}
+ {uncovered && (
+ <p className="text-body text-ink-2">
+ {uncovered.kind_words + uncovered.phrases > 0
+ ? S.ontology.uncovered(uncovered.kind_words, uncovered.phrases)
+ : S.ontology.noMisses}
+ </p>
)}
{proposals && (
<div className="mt-4 border-t border-line pt-3">
- <div className="mb-2 flex items-center gap-2">
- <h4 className="text-small font-semibold text-ink-2">
- {S.ontology.proposals}
- </h4>
- {/* 常见情形是"这些都对"——一条条点是把一个决定拆成八个 */}
- {manualCount(proposals) > 1 && (
- <Button
- size="sm"
- variant="secondary"
- className="ml-auto"
- disabled={addAll.isPending}
- onClick={() => addAll.mutate(manualOnly(proposals))}
- >
- {addAll.isPending
- ? S.ontology.addingAll
- : S.ontology.addAll(manualCount(proposals))}
- </Button>
- )}
- </div>
+ <h4 className="mb-2 text-small font-semibold text-ink-2">
+ {S.ontology.proposals}
+ </h4>
<div className="space-y-2">
{/* 排在最前:它说的是"本体已经有了",而这正是最该先看见的一句。
排在新建后面的话,人一路点下来就把重复建出来了 */}
@@ -2771,36 +2256,11 @@ function MissesPanel({
{p.forms.join(" · ")}
</span>
)}
- {/* 改写多少条是 0003 那条路的账;代理提的走绑定,形状数在 AgentMeta 里 */}
- {!!p.forms?.length && p.proposed_by !== "agent" && (
- <span className="text-small text-accent">
- {S.ontology.willRemap(factsWaiting(p.forms))}
- </span>
- )}
{p.reason && (
- <span className="text-small text-ink-2 truncate">
- {p.reason}
- </span>
+ <span className="text-small text-ink-2 truncate">{p.reason}</span>
)}
<AgentMeta p={p} />
- <Button variant="primary"
- size="sm"
- className="ml-auto"
- onClick={() => approveMapping.mutate(p)}
- disabled={approveMapping.isPending}
- >
- {S.ontology.mapOver}
- </Button>
- {p.proposed_by === "agent" && (
- <Button
- size="sm"
- variant="secondary"
- onClick={() => reject("map_to", p.key)}
- disabled={rejectProposal.isPending}
- >
- {S.ontology.rejectProposal}
- </Button>
- )}
+ {actions("map_to", p.key, S.ontology.mapOver)}
</div>
))}
{proposals.entity_types.map((p) => (
@@ -2809,29 +2269,10 @@ function MissesPanel({
<span className="font-mono text-ink-2">{p.key}</span>
<span className="text-ink">{p.label}</span>
{p.reason && (
- <span className="text-small text-ink-2 truncate">
- {p.reason}
- </span>
+ <span className="text-small text-ink-2 truncate">{p.reason}</span>
)}
<AgentMeta p={p} />
- <Button variant="primary"
- size="sm"
- className="ml-auto"
- onClick={() => approveEntity.mutate(p)}
- disabled={approveEntity.isPending}
- >
- {S.ontology.approve}
- </Button>
- {p.proposed_by === "agent" && (
- <Button
- size="sm"
- variant="secondary"
- onClick={() => reject("entity_types", p.key)}
- disabled={rejectProposal.isPending}
- >
- {S.ontology.rejectProposal}
- </Button>
- )}
+ {actions("entity_types", p.key, S.ontology.approve)}
</div>
))}
{proposals.relation_types.map((p) => (
@@ -2840,40 +2281,11 @@ function MissesPanel({
<span className="font-mono text-ink-2">{p.key}</span>
<span className="text-ink">{p.label}</span>
{p.temporal && <Chip tone="neutral">{p.temporal}</Chip>}
- {/* 影响面:采纳后会改写多少条、归并了哪些写法。没有这个,
- "approve" 就只是凭空多一个空关系 */}
- {!!p.forms?.length && p.proposed_by !== "agent" && (
- <span
- className="text-small text-accent"
- title={p.forms.join(" · ")}
- >
- {S.ontology.willRemap(factsWaiting(p.forms))}
- </span>
- )}
{p.reason && (
- <span className="text-small text-ink-2 truncate">
- {p.reason}
- </span>
+ <span className="text-small text-ink-2 truncate">{p.reason}</span>
)}
<AgentMeta p={p} />
- <Button variant="primary"
- size="sm"
- className="ml-auto"
- onClick={() => approveRelation.mutate(p)}
- disabled={approveRelation.isPending}
- >
- {S.ontology.approve}
- </Button>
- {p.proposed_by === "agent" && (
- <Button
- size="sm"
- variant="secondary"
- onClick={() => reject("relation_types", p.key)}
- disabled={rejectProposal.isPending}
- >
- {S.ontology.rejectProposal}
- </Button>
- )}
+ {actions("relation_types", p.key, S.ontology.approve)}
</div>
))}
{(proposals.attribute_types ?? []).map((p) => (
@@ -2884,46 +2296,16 @@ function MissesPanel({
<span className="text-ink">{p.label}</span>
<Chip tone="neutral">{p.datatype ?? "text"}</Chip>
{p.unit && <Chip tone="neutral">{p.unit}</Chip>}
- {!!p.forms?.length && p.proposed_by !== "agent" && (
- <span
- className="text-small text-accent"
- title={p.forms.join(" · ")}
- >
- {S.ontology.willRemap(factsWaiting(p.forms))}
- </span>
- )}
{p.reason && (
- <span className="text-small text-ink-2 truncate">
- {p.reason}
- </span>
+ <span className="text-small text-ink-2 truncate">{p.reason}</span>
)}
<AgentMeta p={p} />
- <Button variant="primary"
- size="sm"
- className="ml-auto"
- onClick={() => approveAttribute.mutate(p)}
- disabled={approveAttribute.isPending}
- >
- {S.ontology.approve}
- </Button>
- {p.proposed_by === "agent" && (
- <Button
- size="sm"
- variant="secondary"
- onClick={() => reject("attribute_types", p.key)}
- disabled={rejectProposal.isPending}
- >
- {S.ontology.rejectProposal}
- </Button>
- )}
+ {actions("attribute_types", p.key, S.ontology.approve)}
</div>
))}
- {proposals.entity_types.length === 0 &&
- proposals.relation_types.length === 0 &&
- !proposals.attribute_types?.length &&
- !proposals.map_to?.length && (
- <p className="text-body text-ink-2">—</p>
- )}
+ {proposalCount(proposals) === 0 && (
+ <p className="text-body text-ink-2">—</p>
+ )}
</div>
</div>
)}