World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
A fact's evidence is read only inside its own base, so a role in one base no longer reads another base's quotes (#1261)
Szabo Bogdan15fd7d5454c4
diff --git a/crates/utopia-server/src/api/graph_routes.rs b/crates/utopia-server/src/api/graph_routes.rsindex e3684399..eed176b9 100644--- a/crates/utopia-server/src/api/graph_routes.rs+++ b/crates/utopia-server/src/api/graph_routes.rs@@ -454,7 +454,7 @@ pub async fn fact_evidence(Path((kb_id, fact_id)): Path<(Uuid, Uuid)>,) -> ApiResult<Json<serde_json::Value>> {require_kb(&state, &user, kb_id, Role::Viewer).await?;- let evidence = utopia_store::graph::fact_evidence(&state.pool, fact_id).await?;+ let evidence = utopia_store::graph::fact_evidence(&state.pool, kb_id, fact_id).await?;Ok(Json(json!({ "evidence": evidence })))}diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rsindex f10cb230..1ef3c1a8 100644--- a/crates/utopia-store/src/graph.rs+++ b/crates/utopia-store/src/graph.rs@@ -2160,7 +2160,18 @@ pub async fn first_live_evidence(/// 证据回放路径:不过滤 superseded——它的职责就是能看旧版。/// stale = 证据版本落后于文档当前版本(UI 标 "from v{n}")。-pub async fn fact_evidence(pool: &PgPool, fact_id: Uuid) -> AppResult<Vec<EvidenceView>> {+/// 事实不在本 KB 时 NotFound:路由只校验了路径里的 kb,事实 id 是调用方给的。+pub async fn fact_evidence(+ pool: &PgPool,+ kb_id: Uuid,+ fact_id: Uuid,+) -> AppResult<Vec<EvidenceView>> {+ sqlx::query("SELECT 1 FROM facts WHERE id = $1 AND kb_id = $2")+ .bind(fact_id)+ .bind(kb_id)+ .fetch_optional(pool)+ .await?+ .ok_or(AppError::NotFound)?;let rows: Vec<EvidenceView> = sqlx::query_as("SELECT fe.quote, fe.proposed_predicate, fe.chunk_id, c.document_id, d.filename, c.seq,c.doc_version,diff --git a/crates/utopia-store/src/reasoning.rs b/crates/utopia-store/src/reasoning.rsindex 61bdd4e1..48374d4f 100644--- a/crates/utopia-store/src/reasoning.rs+++ b/crates/utopia-store/src/reasoning.rs@@ -2811,7 +2811,7 @@ pub async fn proof(let Some(derived) = derived_one(pool, kb_id, derived_id).await? else {return Ok(None);};- let steps = premise_steps(pool, derived_id, 0).await?;+ let steps = premise_steps(pool, kb_id, derived_id, 0).await?;Ok(Some(utopia_core::models::Proof { derived, steps }))}@@ -2821,6 +2821,7 @@ pub async fn proof(/// 到底了就停在那一步上——它自己的三元组还是列出来的,只是不再往下问。fn premise_steps<'a>(pool: &'a PgPool,+ kb_id: Uuid,derived_id: Uuid,depth: usize,) -> std::pin::Pin<@@ -2840,11 +2841,12 @@ fn premise_steps<'a>(.await?;// 断言那几步一次取完(每一步还要各取一次证据),派生那几步逐条问let asserted: Vec<Uuid> = rows.iter().filter_map(|(_, f, _)| *f).collect();- let mut by_fact: HashMap<Uuid, utopia_core::models::ProofStep> = steps_for(pool, &asserted)- .await?- .into_iter()- .map(|s| (s.fact_id, s))- .collect();+ let mut by_fact: HashMap<Uuid, utopia_core::models::ProofStep> =+ steps_for(pool, kb_id, &asserted)+ .await?+ .into_iter()+ .map(|s| (s.fact_id, s))+ .collect();let mut steps = Vec::with_capacity(rows.len());for (seq, fact, derived) in rows {@@ -2860,7 +2862,7 @@ fn premise_steps<'a>(};step.seq = seq;if depth + 1 < utopia_reason::MAX_DEPTH {- step.premises = premise_steps(pool, d, depth + 1).await?;+ step.premises = premise_steps(pool, kb_id, d, depth + 1).await?;}steps.push(step);}@@ -2948,6 +2950,7 @@ async fn derived_step(/// 走——前提是同一种东西,证明链没有理由长两个样async fn steps_for(pool: &PgPool,+ kb_id: Uuid,premises: &[Uuid],) -> AppResult<Vec<utopia_core::models::ProofStep>> {#[allow(clippy::type_complexity)]@@ -2996,7 +2999,7 @@ async fn steps_for() in rows{// 一条链最多 MAX_DEPTH 步,逐条取证据是可数的几次往返- let evidence = crate::graph::fact_evidence(pool, fact_id).await?;+ let evidence = crate::graph::fact_evidence(pool, kb_id, fact_id).await?;steps.push(utopia_core::models::ProofStep {seq: seq as i32,fact_id,@@ -3082,7 +3085,7 @@ pub async fn blocked_proof(.await?;match path {None => Ok(None),- Some((p,)) => Ok(Some(steps_for(pool, &p).await?)),+ Some((p,)) => Ok(Some(steps_for(pool, kb_id, &p).await?)),}}diff --git a/crates/utopia-store/tests/store/evidence_stays_inside_its_base.rs b/crates/utopia-store/tests/store/evidence_stays_inside_its_base.rsnew file mode 100644index 00000000..bf32a632--- /dev/null+++ b/crates/utopia-store/tests/store/evidence_stays_inside_its_base.rs@@ -0,0 +1,56 @@+//! 证据只在事实自己的库里读:别库事实的 id 拿到本库路径下问,答 NotFound,+//! 不吐出别库的原句。++use sqlx::PgPool;+use uuid::Uuid;++#[tokio::test]+async fn another_bases_fact_is_not_found() -> anyhow::Result<()> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ utopia_store::db::migrate(&pool).await?;+ let (org, ws, a, b) = (+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ );+ let (doc, chunk, ent, fact) = (+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ );+ sqlx::raw_sql(&format!(+ "INSERT INTO organizations (id, name) VALUES ('{org}', 'evidence-test');+ INSERT INTO workspaces (id, org_id, name) VALUES ('{ws}', '{org}', 'evidence-test');+ INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ('{a}', '{ws}', 'a'), ('{b}', '{ws}', 'b');+ INSERT INTO documents (id, kb_id, filename, sha256, status, external_key)+ VALUES ('{doc}', '{b}', 'b.md', '{doc}', 'ready', 'file:///b.md');+ INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ('{chunk}', '{b}', '{doc}', 0, 'x');+ INSERT INTO entities (id, kb_id, canonical_name) VALUES ('{ent}', '{b}', 'e');+ INSERT INTO facts (id, kb_id, subject_id, object_id, confidence)+ VALUES ('{fact}', '{b}', '{ent}', '{ent}', 0.9);+ INSERT INTO fact_evidence (fact_id, chunk_id, quote, document_id, doc_version)+ VALUES ('{fact}', '{chunk}', 'b only', '{doc}', 1);"+ ))+ .execute(&pool)+ .await?;++ let from_a = utopia_store::graph::fact_evidence(&pool, a, fact).await;+ let from_b = utopia_store::graph::fact_evidence(&pool, b, fact).await?;++ sqlx::query("DELETE FROM knowledge_bases WHERE id = ANY($1)")+ .bind([a, b])+ .execute(&pool)+ .await?;+ sqlx::query("DELETE FROM organizations WHERE id = $1")+ .bind(org)+ .execute(&pool)+ .await?;+ assert!(matches!(from_a, Err(utopia_core::AppError::NotFound)));+ assert_eq!(from_b.len(), 1);+ Ok(())+}diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rsindex 7b2516d5..44d9280c 100644--- a/crates/utopia-store/tests/store/main.rs+++ b/crates/utopia-store/tests/store/main.rs@@ -126,6 +126,7 @@ mod concurrent_chunk_replacement;mod cross_kb_provenance_fails_closed;mod derived_facts_are_second_class;mod ended_when_unknown;+mod evidence_stays_inside_its_base;mod exploration_describes_the_data;mod export_surfaces;mod exported_references_never_cross_a_kb;