World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Read duplicate reviews in batches and make summary counts cheaper (#1119)
Nash Aschenbathe04896ed3c3e
diff --git a/crates/utopia-store/src/names.rs b/crates/utopia-store/src/names.rsindex 1ba7afcf..5231ca5a 100644--- a/crates/utopia-store/src/names.rs+++ b/crates/utopia-store/src/names.rs@@ -15,6 +15,7 @@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::AppResult;use uuid::Uuid;@@ -236,22 +237,41 @@ pub async fn pair_shared_name(/// 本名以外的现行名字,按首次记下的先后。给裁决器与审阅卡的「又名」那一行pub async fn other_names(pool: &PgPool, entity_id: Uuid) -> AppResult<Vec<String>> {- let rows: Vec<(String,)> = sqlx::query_as(- "SELECT v FROM (- SELECT DISTINCT ON (lower(f.object_value->>'value'))- f.object_value->>'value' AS v, f.recorded_at- FROM facts f+ Ok(other_names_many(pool, &[entity_id])+ .await?+ .remove(&entity_id)+ .unwrap_or_default())+}++/// Keep deduplication and first-seen ordering within each entity. The KB join+/// lets the existing (kb_id, subject_id) indexes serve even a single-ID lookup.+pub(crate) async fn other_names_many(+ pool: &PgPool,+ entity_ids: &[Uuid],+) -> AppResult<HashMap<Uuid, Vec<String>>> {+ if entity_ids.is_empty() {+ return Ok(HashMap::new());+ }+ let rows: Vec<(Uuid, String)> = sqlx::query_as(+ "SELECT entity_id, v FROM (+ SELECT DISTINCT ON (f.subject_id, lower(f.object_value->>'value'))+ f.subject_id AS entity_id, f.object_value->>'value' AS v, f.recorded_at+ FROM entities e+ JOIN facts f ON f.subject_id = e.id AND f.kb_id = e.kb_idJOIN relation_types r ON r.id = f.predicate_id- JOIN entities e ON e.id = f.subject_id- WHERE f.subject_id = $1 AND r.builtin AND r.key = $2+ WHERE e.id = ANY($1) AND r.builtin AND r.key = $2AND f.invalidated_at IS NULLAND lower(f.object_value->>'value') <> lower(e.canonical_name)- ORDER BY lower(f.object_value->>'value'), f.recorded_at- ) n ORDER BY n.recorded_at",+ ORDER BY f.subject_id, lower(f.object_value->>'value'), f.recorded_at+ ) n ORDER BY n.entity_id, n.recorded_at",)- .bind(entity_id)+ .bind(entity_ids).bind(KNOWN_AS).fetch_all(pool).await?;- Ok(rows.into_iter().map(|(v,)| v).collect())+ let mut names: HashMap<Uuid, Vec<String>> = HashMap::new();+ for (id, name) in rows {+ names.entry(id).or_default().push(name);+ }+ Ok(names)}diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rsindex 54f56426..03d1b305 100644--- a/crates/utopia-store/src/resolution.rs+++ b/crates/utopia-store/src/resolution.rs@@ -11,7 +11,7 @@use chrono::{DateTime, Utc};use pgvector::Vector;use sqlx::PgPool;-use std::collections::HashSet;+use std::collections::{HashMap, HashSet};use utopia_core::models::{MergeLogView, ReviewBatchOutcome, ReviewItem, ReviewSide};use utopia_core::{AppError, AppResult};use uuid::Uuid;@@ -1424,7 +1424,11 @@ pub(crate) struct ReviewRow {created_at: DateTime<Utc>,}-async fn review_side(pool: &PgPool, kb_id: Uuid, entity_id: Uuid) -> AppResult<ReviewSide> {+async fn review_sides(+ pool: &PgPool,+ kb_id: Uuid,+ entity_ids: &[Uuid],+) -> AppResult<HashMap<Uuid, ReviewSide>> {#[derive(sqlx::FromRow)]struct SideRow {id: Uuid,@@ -1434,7 +1438,7 @@ async fn review_side(pool: &PgPool, kb_id: Uuid, entity_id: Uuid) -> AppResult<Rdisambiguator: Option<String>,degree: i64,}- let row: SideRow = sqlx::query_as(&format!(+ let rows: Vec<SideRow> = sqlx::query_as(&format!("SELECT e.id, e.canonical_name AS name, t.label AS type_label,coalesce(t.color, '#94a3b8') AS color, e.disambiguator,(SELECT count(*) FROM facts f@@ -1443,24 +1447,31 @@ async fn review_side(pool: &PgPool, kb_id: Uuid, entity_id: Uuid) -> AppResult<R-- LEFT JOIN:没判出类型的实体照样要能进审核(0009)。-- 内连接会让它整条审核项取不出来,而漂移审核恰恰最常发生在它们身上FROM entities e LEFT JOIN entity_types t ON t.id = e.type_id- WHERE e.kb_id = $1 AND e.id = $2",+ WHERE e.kb_id = $1 AND e.id = ANY($2)",not_name = crate::names::not_a_name("f"),)).bind(kb_id)- .bind(entity_id)- .fetch_optional(pool)- .await?- .ok_or(AppError::NotFound)?;-- Ok(ReviewSide {- id: row.id,- name: row.name,- type_label: row.type_label,- color: row.color,- disambiguator: row.disambiguator,- degree: row.degree,- top_facts: entity_fact_lines(pool, kb_id, entity_id, 4).await?,- })+ .bind(entity_ids)+ .fetch_all(pool)+ .await?;+ let mut lines = entity_fact_lines_many(pool, kb_id, entity_ids, 4).await?;+ Ok(rows+ .into_iter()+ .map(|row| {+ (+ row.id,+ ReviewSide {+ id: row.id,+ name: row.name,+ type_label: row.type_label,+ color: row.color,+ disambiguator: row.disambiguator,+ degree: row.degree,+ top_facts: lines.remove(&row.id).unwrap_or_default(),+ },+ )+ })+ .collect())}/// 实体的事实摘要行:"works at → 星云科技 (2023-01 → now)",裁决 prompt 与审核 UI 共用。@@ -1470,8 +1481,26 @@ pub async fn entity_fact_lines(entity_id: Uuid,limit: i64,) -> AppResult<Vec<String>> {+ Ok(entity_fact_lines_many(pool, kb_id, &[entity_id], limit)+ .await?+ .remove(&entity_id)+ .unwrap_or_default())+}++/// The lateral limit applies to each entity, not to the whole page. Keep this+/// path shared with the single-entity reader used by adjudication.+async fn entity_fact_lines_many(+ pool: &PgPool,+ kb_id: Uuid,+ entity_ids: &[Uuid],+ limit: i64,+) -> AppResult<HashMap<Uuid, Vec<String>>> {+ if entity_ids.is_empty() {+ return Ok(HashMap::new());+ }#[derive(sqlx::FromRow)]struct Line {+ entity_id: Uuid,direction: String,predicate_label: String,other_name: Option<String>,@@ -1483,24 +1512,29 @@ pub async fn entity_fact_lines(// 开放陈述(0044)按它照抄的短语读:`phrase` 在关系标签之后、证据众数之前——// 类型化行的 phrase 是 NULL,它们的显示一字不变let rows: Vec<Line> = sqlx::query_as(&format!(- "SELECT CASE WHEN f.subject_id = $2 THEN 'out' ELSE 'in' END AS direction,+ "SELECT requested.id AS entity_id, line.*+ FROM unnest($2::uuid[]) AS requested(id)+ CROSS JOIN LATERAL (+ SELECT CASE WHEN f.subject_id = requested.id THEN 'out' ELSE 'in' END AS direction,COALESCE(r.label, f.phrase, fact_surface_predicate(f.id)) AS predicate_label,o.canonical_name AS other_name,- f.valid_from, f.valid_to+ f.valid_from, f.valid_to, f.confidence, f.recorded_atFROM facts fLEFT JOIN relation_types r ON r.id = f.predicate_idLEFT JOIN entities o- ON o.id = CASE WHEN f.subject_id = $2 THEN f.object_id ELSE f.subject_id END+ ON o.id = CASE WHEN f.subject_id = requested.id THEN f.object_id ELSE f.subject_id ENDWHERE f.kb_id = $1 AND f.invalidated_at IS NULL- AND (f.subject_id = $2 OR f.object_id = $2)+ AND (f.subject_id = requested.id OR f.object_id = requested.id)AND COALESCE(r.label, f.phrase, fact_surface_predicate(f.id)) IS NOT NULLAND {not_name}ORDER BY f.confidence DESC, f.recorded_at DESC- LIMIT $3",+ LIMIT $3+ ) line+ ORDER BY requested.id, line.confidence DESC, line.recorded_at DESC",not_name = crate::names::not_a_name("f"),)).bind(kb_id)- .bind(entity_id)+ .bind(entity_ids).bind(limit).fetch_all(pool).await?;@@ -1508,28 +1542,28 @@ pub async fn entity_fact_lines(// 本名以外的名字单独打头一行(0041):「海洋探测器1号」对「海探1」,裁决器要看得见// 前者也叫海探1,否则两边的事实各说各的,它只会判「不是同一个」。本名不列——// 两个张伟各有一条「known as 张伟」,摆出来像共同证据,其实什么也分不出- let also_known_as = crate::names::other_names(pool, entity_id).await?;- let head =- (!also_known_as.is_empty()).then(|| format!("also known as: {}", also_known_as.join(", ")));-- Ok(head+ let names = crate::names::other_names_many(pool, entity_ids).await?;+ let mut lines: HashMap<Uuid, Vec<String>> = names.into_iter()- .chain(rows.into_iter().map(|l| {- let other = l.other_name.unwrap_or_else(|| "?".into());- let core = if l.direction == "out" {- format!("{} → {}", l.predicate_label, other)- } else {- format!("{} ← {}", l.predicate_label, other)- };- match (l.valid_from, l.valid_to) {- (Some(f), Some(t)) => {- format!("{core} ({} → {})", f.format("%Y-%m"), t.format("%Y-%m"))- }- (Some(f), None) => format!("{core} ({} → now)", f.format("%Y-%m")),- _ => core,+ .map(|(id, names)| (id, vec![format!("also known as: {}", names.join(", "))]))+ .collect();+ for l in rows {+ let other = l.other_name.unwrap_or_else(|| "?".into());+ let core = if l.direction == "out" {+ format!("{} → {}", l.predicate_label, other)+ } else {+ format!("{} ← {}", l.predicate_label, other)+ };+ let text = match (l.valid_from, l.valid_to) {+ (Some(f), Some(t)) => {+ format!("{core} ({} → {})", f.format("%Y-%m"), t.format("%Y-%m"))}- }))- .collect())+ (Some(f), None) => format!("{core} ({} → now)", f.format("%Y-%m")),+ _ => core,+ };+ lines.entry(l.entity_id).or_default().push(text);+ }+ Ok(lines)}pub(crate) async fn assemble_reviews(@@ -1537,9 +1571,18 @@ pub(crate) async fn assemble_reviews(kb_id: Uuid,rows: Vec<ReviewRow>,) -> AppResult<Vec<ReviewItem>> {+ if rows.is_empty() {+ return Ok(Vec::new());+ }+ // An entity can occur on either side of several pairs. Read it once for+ // this page, then reuse its summary without changing the review order.+ let mut entity_ids: Vec<Uuid> = rows.iter().flat_map(|r| [r.left_id, r.right_id]).collect();+ entity_ids.sort_unstable();+ entity_ids.dedup();+ let sides = review_sides(pool, kb_id, &entity_ids).await?;// 这一页上开着的建议(0025):一趟查完,按审核行挂上去let ids: Vec<Uuid> = rows.iter().map(|r| r.id).collect();- let proposals: Vec<(Uuid, utopia_core::models::ReviewProposal)> =+ let proposals: HashMap<Uuid, utopia_core::models::ReviewProposal> =sqlx::query_as::<_, (Uuid, Uuid, String, f32, Option<String>)>("SELECT target_id, id, action, confidence, reason FROM agent_decisionsWHERE target_kind = 'review' AND target_id = ANY($1) AND status = 'proposed'",@@ -1562,18 +1605,15 @@ pub(crate) async fn assemble_reviews(.collect();let mut items = Vec::with_capacity(rows.len());for r in rows {- let proposal = proposals- .iter()- .find(|(t, _)| *t == r.id)- .map(|(_, p)| p.clone());+ let proposal = proposals.get(&r.id).cloned();items.push(ReviewItem {id: r.id,score: r.score,reason: r.reason,stage: r.stage,created_at: r.created_at,- left: review_side(pool, kb_id, r.left_id).await?,- right: review_side(pool, kb_id, r.right_id).await?,+ left: sides.get(&r.left_id).cloned().ok_or(AppError::NotFound)?,+ right: sides.get(&r.right_id).cloned().ok_or(AppError::NotFound)?,proposal,});}diff --git a/crates/utopia-store/src/review_summary.rs b/crates/utopia-store/src/review_summary.rsindex ea470d33..dabebe8f 100644--- a/crates/utopia-store/src/review_summary.rs+++ b/crates/utopia-store/src/review_summary.rs@@ -317,16 +317,21 @@ async fn agent(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewAgent> {async fn health(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewHealth> {// 只数在世的事实(invalidated_at IS NULL):作废的不算库的成色。- // 「有争议」= 挂在一条还开着的冲突的任一端+ // 「有争议」= 挂在一条还开着的冲突的任一端。先合并两端,避免每条事实+ // 再探测一次冲突表;UNION 去重,同一事实挂在几条冲突上也只计一次。let sql = format!("SELECT count(*) AS facts,count(*) FILTER (WHERE f.confidence < $2 AND f.derived_by_rule IS NULL) AS low_confidence,count(*) FILTER (WHERE {unconfirmed}) AS unconfirmed,- count(*) FILTER (WHERE EXISTS (- SELECT 1 FROM fact_conflicts c- WHERE c.status = 'open'- AND (c.old_fact_id = f.id OR c.new_fact_id = f.id))) AS contested+ count(c.fact_id) AS contestedFROM facts f+ LEFT JOIN (+ SELECT old_fact_id AS fact_id FROM fact_conflicts+ WHERE kb_id = $1 AND status = 'open'+ UNION+ SELECT new_fact_id AS fact_id FROM fact_conflicts+ WHERE kb_id = $1 AND status = 'open'+ ) c ON c.fact_id = f.idWHERE f.kb_id = $1 AND f.invalidated_at IS NULL",unconfirmed = UNCONFIRMED_FACT,);diff --git a/crates/utopia-store/tests/store/a_review_has_a_summary.rs b/crates/utopia-store/tests/store/a_review_has_a_summary.rsindex ef07103d..fa049e94 100644--- a/crates/utopia-store/tests/store/a_review_has_a_summary.rs+++ b/crates/utopia-store/tests/store/a_review_has_a_summary.rs@@ -18,6 +18,7 @@ struct Seed {kb: Uuid,org: Uuid,ada: Uuid,+ facts: [Uuid; 3],conflict_at: DateTime<Utc>,review_at: DateTime<Utc>,}@@ -138,6 +139,7 @@ async fn seed(pool: &PgPool) -> anyhow::Result<Seed> {kb,org,ada,+ facts: [f1, f2, f3],conflict_at,review_at,})@@ -157,6 +159,97 @@ async fn teardown(pool: &PgPool, s: &Seed) -> anyhow::Result<()> {Ok(())}+#[tokio::test]+async fn shared_conflict_endpoints_do_not_multiply_health_counts() -> anyhow::Result<()> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ let s = seed(&pool).await?;+ let other = seed(&pool).await?;+ let run = async {+ let [f1, f2, f3] = s.facts;+ let (resolved_only, invalidated) = (Uuid::now_v7(), Uuid::now_v7());+ for (id, confidence, retired) in [(resolved_only, 0.9f32, false), (invalidated, 0.5, true)]+ {+ sqlx::query(+ "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id,+ confidence, invalidated_at)+ SELECT $1, kb_id, subject_id, predicate_id, object_id, $2,+ CASE WHEN $3 THEN now() END+ FROM facts WHERE id = $4",+ )+ .bind(id)+ .bind(confidence)+ .bind(retired)+ .bind(f2)+ .execute(&pool)+ .await?;+ }+ // f1 occurs on both ends and in several open conflicts. Every health+ // aggregate must still count the fact once, including low/unconfirmed.+ for (old, new, status) in [+ (f3, f1, "open"),+ (f2, f1, "open"),+ (resolved_only, f1, "resolved"),+ ] {+ sqlx::query(+ "INSERT INTO fact_conflicts (id, kb_id, old_fact_id, new_fact_id, reason, status)+ VALUES ($1, $2, $3, $4, 'simultaneous', $5)",+ )+ .bind(Uuid::now_v7())+ .bind(s.kb)+ .bind(old)+ .bind(new)+ .bind(status)+ .execute(&pool)+ .await?;+ }+ let (doc, chunk) = (Uuid::now_v7(), Uuid::now_v7());+ sqlx::query(+ "INSERT INTO documents (id, kb_id, filename, sha256) VALUES ($1, $2, 'old.txt', $3)",+ )+ .bind(doc)+ .bind(s.kb)+ .bind(doc.to_string())+ .execute(&pool)+ .await?;+ sqlx::query(+ "INSERT INTO chunks (id, kb_id, document_id, seq, text, superseded_at)+ VALUES ($1, $2, $3, 0, 'old evidence', now())",+ )+ .bind(chunk)+ .bind(s.kb)+ .bind(doc)+ .execute(&pool)+ .await?;+ sqlx::query(+ "INSERT INTO fact_evidence (fact_id, chunk_id, document_id) VALUES ($1, $2, $3)",+ )+ .bind(f1)+ .bind(chunk)+ .bind(doc)+ .execute(&pool)+ .await?;+ Ok::<_, anyhow::Error>(utopia_store::review_summary::summary(&pool, s.kb).await?)+ }+ .await;+ teardown(&pool, &s).await?;+ teardown(&pool, &other).await?;+ let sum = run?;+ assert_eq!(sum.health.facts, 4, "only this KB's live facts");+ assert_eq!(+ sum.health.contested, 3,+ "distinct live endpoints of open conflicts"+ );+ assert_eq!(sum.health.low_confidence, 1);+ assert_eq!(sum.health.unconfirmed, 1);+ assert_eq!(sum.waiting.conflicts.count, 3);+ assert_eq!(sum.waiting.lowconf.count, 1);+ assert_eq!(sum.waiting.unconfirmed.count, 1);+ Ok(())+}+fn close(a: Option<DateTime<Utc>>, b: DateTime<Utc>) -> bool {a.is_some_and(|a| (a - b).num_seconds().abs() < 2)}diff --git a/crates/utopia-store/tests/store/a_review_page_is_assembled_in_batches.rs b/crates/utopia-store/tests/store/a_review_page_is_assembled_in_batches.rsnew file mode 100644index 00000000..7449117b--- /dev/null+++ b/crates/utopia-store/tests/store/a_review_page_is_assembled_in_batches.rs@@ -0,0 +1,287 @@+//! Shared entities keep their own aliases, degree and top-four limit when a+//! review page is assembled in batches. Exercise the SQL against a real DB.++use chrono::{Duration, TimeZone, Utc};+use sqlx::PgPool;+use utopia_core::models::ReviewItem;+use utopia_store::resolution::{self, TypeFilter};+use uuid::Uuid;++fn ids(items: &[ReviewItem]) -> Vec<Uuid> {+ items.iter().map(|item| item.id).collect()+}++#[tokio::test]+async fn shared_sides_keep_their_summaries_and_the_page_keeps_its_order() -> anyhow::Result<()> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ let (org, ws, kb, other_kb) = (+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ );+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'review-page-test')")+ .bind(org)+ .execute(&pool)+ .await?;+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'review-page-test')")+ .bind(ws)+ .bind(org)+ .execute(&pool)+ .await?;++ let run = async {+ for base in [kb, other_kb] {+ sqlx::query(+ "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'review-page-test')",+ )+ .bind(base)+ .bind(ws)+ .execute(&pool)+ .await?;+ }+ let (person, company, relation) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());+ for (id, key, label) in [(person, "person", "Person"), (company, "company", "Company")] {+ sqlx::query(+ "INSERT INTO entity_types (id, kb_id, key, label, color) VALUES ($1, $2, $3, $4, '#123456')",+ )+ .bind(id)+ .bind(kb)+ .bind(key)+ .bind(label)+ .execute(&pool)+ .await?;+ }+ sqlx::query(+ "INSERT INTO relation_types (id, kb_id, key, label) VALUES ($1, $2, 'knows', 'Knows')",+ )+ .bind(relation)+ .bind(kb)+ .execute(&pool)+ .await?;+ let [a, b, c, d, neighbor, other_a, other_b] = std::array::from_fn(|_| Uuid::now_v7());+ for (id, base, ty, name, suffix) in [+ (a, kb, Some(person), "Alpha", Some("North")),+ (b, kb, Some(person), "Beta", None),+ (c, kb, Some(company), "Gamma", None),+ (d, kb, None, "Untyped", None),+ (neighbor, kb, None, "Neighbor", None),+ (other_a, other_kb, None, "Alpha", None),+ (other_b, other_kb, None, "Beta", None),+ ] {+ sqlx::query(+ "INSERT INTO entities (id, kb_id, type_id, canonical_name, disambiguator)+ VALUES ($1, $2, $3, $4, $5)",+ )+ .bind(id)+ .bind(base)+ .bind(ty)+ .bind(name)+ .bind(suffix)+ .execute(&pool)+ .await?;+ }++ let at = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap();+ let known_as = utopia_store::names::ensure_known_as(&pool, kb).await?;+ for (entity, canonical) in [(a, "alpha"), (b, "BETA")] {+ // Two entities share the same aliases; deduplication must stay per entity.+ for (i, name) in ["Common", "Alternate", "common", canonical, "Retired"]+ .into_iter()+ .enumerate()+ {+ sqlx::query(+ "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_value,+ recorded_at, invalidated_at)+ VALUES ($1, $2, $3, $4, $5, $6, $7)",+ )+ .bind(Uuid::now_v7())+ .bind(kb)+ .bind(entity)+ .bind(known_as)+ .bind(serde_json::json!({"value": name}))+ .bind(at + Duration::seconds(i as i64))+ .bind((name == "Retired").then_some(at + Duration::days(1)))+ .execute(&pool)+ .await?;+ }++ // Six live facts per entity, including a self-loop. A seventh, higher+ // confidence fact is invalidated. Equal confidence uses recorded_at.+ for (i, confidence) in [0.9f32, 0.9, 0.8, 0.7, 0.6, 0.5, 1.0]+ .into_iter()+ .enumerate()+ {+ let subject = if i == 0 { neighbor } else { entity };+ let object = match i {+ 0 | 5 => Some(entity),+ 3 => None,+ _ => Some(neighbor),+ };+ sqlx::query(+ "INSERT INTO facts (id, kb_id, subject_id, predicate_id, phrase, object_id,+ object_value, confidence, recorded_at, invalidated_at,+ valid_from, valid_from_precision, valid_to, valid_to_precision)+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10,+ CASE WHEN $11 < 2 THEN '2020-01-01'::timestamptz END,+ CASE WHEN $11 < 2 THEN 'month' END,+ CASE WHEN $11 = 0 THEN '2021-02-01'::timestamptz END,+ CASE WHEN $11 = 0 THEN 'month' END)",+ )+ .bind(Uuid::now_v7())+ .bind(kb)+ .bind(subject)+ .bind((i != 2).then_some(relation))+ .bind((i == 2).then_some("works alongside"))+ .bind(object)+ .bind((i == 3).then(|| serde_json::json!({"value": "text"})))+ .bind(confidence)+ .bind(at + Duration::seconds(10 - i as i64))+ .bind((i == 6).then_some(at + Duration::days(1)))+ .bind(i as i32)+ .execute(&pool)+ .await?;+ }+ }++ let mut reviews: [Uuid; 6] = std::array::from_fn(|_| Uuid::now_v7());+ reviews.sort_unstable();+ // The newest two share a timestamp, so the ID tie-breaker matters.+ for (i, (base, left, right, stage, status, seconds)) in [+ (kb, a, b, "human", "pending", 3),+ (kb, c, a, "adjudicating", "pending", 3),+ (kb, b, c, "human", "pending", 2),+ (kb, a, d, "adjudicating", "pending", 1),+ (kb, b, d, "human", "kept", 4),+ (other_kb, other_a, other_b, "human", "pending", 5),+ ]+ .into_iter()+ .enumerate()+ {+ sqlx::query(+ "INSERT INTO resolution_reviews (id, kb_id, left_id, right_id, stage, status,+ score, reason, created_at)+ VALUES ($1, $2, $3, $4, $5, $6, 0.8, 'namesake', $7)",+ )+ .bind(reviews[i])+ .bind(base)+ .bind(left)+ .bind(right)+ .bind(stage)+ .bind(status)+ .bind(at + Duration::seconds(seconds))+ .execute(&pool)+ .await?;+ }+ let merge_proposal = Uuid::now_v7();+ let keep_proposal = Uuid::now_v7();+ for (id, review, action, status) in [+ (merge_proposal, reviews[0], "merge", "proposed"),+ (keep_proposal, reviews[2], "keep", "proposed"),+ (Uuid::now_v7(), reviews[0], "keep", "superseded"),+ (Uuid::now_v7(), reviews[1], "keep", "accepted"),+ ] {+ sqlx::query(+ "INSERT INTO agent_decisions (id, kb_id, run_id, target_kind, target_id,+ action, confidence, reason, status)+ VALUES ($1, $2, $3, 'review', $4, $5, 0.75, 'evidence', $6)",+ )+ .bind(id)+ .bind(kb)+ .bind(Uuid::now_v7())+ .bind(review)+ .bind(action)+ .bind(status)+ .execute(&pool)+ .await?;+ }++ let all = resolution::list_reviews(&pool, kb, TypeFilter::Any, 10, 0).await?;+ assert_eq!(ids(&all), vec![reviews[1], reviews[0], reviews[2], reviews[3]]);+ let expected = vec![+ "also known as: Common, Alternate",+ "Knows ← Neighbor (2020-01 → 2021-02)",+ "Knows → Neighbor (2020-01 → now)",+ "works alongside → Neighbor",+ "Knows → ?",+ ];+ for item in &all {+ assert_eq!(item.score, 0.8);+ assert_eq!(item.reason.as_deref(), Some("namesake"));+ for side in [&item.left, &item.right] {+ if side.id == a || side.id == b {+ assert_eq!(side.top_facts, expected, "four facts per entity, plus aliases");+ assert_eq!(side.degree, 6, "names and invalidated facts do not count; a self-loop counts once");+ assert_eq!(side.type_label.as_deref(), Some("Person"));+ assert_eq!(side.color, "#123456");+ } else {+ assert!(side.top_facts.is_empty());+ assert_eq!(side.degree, 0);+ }+ }+ }+ assert_eq!(all[0].left.id, c);+ assert_eq!(all[0].left.type_label.as_deref(), Some("Company"));+ assert_eq!(all[0].right.name, "Alpha");+ assert_eq!(all[0].right.disambiguator.as_deref(), Some("North"));+ assert_eq!(all[1].right.name, "Beta");+ assert_eq!(all[1].right.disambiguator, None);+ assert_eq!(all[3].right.id, d);+ assert_eq!(all[3].right.type_label, None);+ assert_eq!(all[3].right.color, "#94a3b8");+ for (item, proposal_id, action) in [+ (&all[1], merge_proposal, "merge"),+ (&all[2], keep_proposal, "keep"),+ ] {+ let proposal = item.proposal.as_ref().expect("open proposal");+ assert_eq!(proposal.id, proposal_id);+ assert_eq!(proposal.action, action);+ assert_eq!(proposal.confidence, 0.75);+ assert_eq!(proposal.reason.as_deref(), Some("evidence"));+ }+ assert!(all[0].proposal.is_none());+ assert!(all[3].proposal.is_none());+ for entity in [a, b] {+ assert_eq!(resolution::entity_fact_lines(&pool, kb, entity, 4).await?, expected);+ assert_eq!(+ resolution::entity_fact_lines(&pool, kb, entity, 1).await?,+ expected[..2],+ "the alias heading does not consume the fact limit"+ );+ }++ assert_eq!(+ ids(&resolution::list_reviews(&pool, kb, TypeFilter::Any, 2, 1).await?),+ vec![reviews[0], reviews[2]]+ );+ assert_eq!(+ ids(&resolution::list_reviews(&pool, kb, TypeFilter::Same, 10, 0).await?),+ vec![reviews[0]]+ );+ assert_eq!(+ ids(&resolution::list_reviews(&pool, kb, TypeFilter::Conflict, 10, 0).await?),+ vec![reviews[1], reviews[2]]+ );+ let pending = resolution::pending_adjudications(&pool, kb, 10).await?;+ assert_eq!(ids(&pending), vec![reviews[3], reviews[1]]);+ assert!(pending.iter().all(|item| item.stage == "adjudicating"));+ assert_eq!(pending[1].right.top_facts, expected);+ assert!(resolution::list_reviews(&pool, kb, TypeFilter::Any, 10, 4).await?.is_empty());+ assert!(resolution::list_reviews(&pool, kb, TypeFilter::Any, 0, 0).await?.is_empty());+ Ok::<_, anyhow::Error>(())+ }+ .await;++ sqlx::query("DELETE FROM knowledge_bases WHERE id = ANY($1)")+ .bind([kb, other_kb])+ .execute(&pool)+ .await?;+ sqlx::query("DELETE FROM organizations WHERE id = $1")+ .bind(org)+ .execute(&pool)+ .await?;+ run+}diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rsindex 029c0ba2..0cbc7470 100644--- a/crates/utopia-store/tests/store/main.rs+++ b/crates/utopia-store/tests/store/main.rs@@ -78,6 +78,7 @@ mod a_remembered_episode_strips_nul;mod a_retired_account;mod a_retraction_leaves_the_graph;mod a_review_has_a_summary;+mod a_review_page_is_assembled_in_batches;mod a_rule_computes_what_it_concludes;mod a_rule_concludes_a_type;mod a_rule_reads_what_a_rule_concluded;