From efdb4e27eeeb2b681dadb5e4a5bea1b57630c494 Mon Sep 17 00:00:00 2001 From: Virtue039 Date: Sun, 11 Oct 2026 23:59:56 +0800 Subject: [PATCH 1/2] Measure both namesake failure paths with fixed model replies in CI Signed-off-by: Virtue039 --- .github/workflows/checks.yml | 44 ++ crates/utopia-server/src/main.rs | 2 + .../src/namesake_scripted_cases.rs | 403 ++++++++++++++++++ scripts/bench/namesake-scripted.md | 58 +++ 4 files changed, 507 insertions(+) create mode 100644 crates/utopia-server/src/namesake_scripted_cases.rs create mode 100644 scripts/bench/namesake-scripted.md diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index cb6c2a72c..a3f9149c8 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -86,6 +86,50 @@ jobs: UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia UTOPIA_TEST_REQUIRE_DB: "1" + # Keep the current gold failures visible while a smaller fix is discussed. + # Budget success means no new/worse failures, not correct identity resolution. + - name: Fixed namesake cases (known-failure budget) + shell: bash + run: cargo test --locked -p utopia-server namesake_scripted_cases -- --nocapture 2>&1 | tee namesake-scripted-results.log + env: + UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia + UTOPIA_TEST_REQUIRE_DB: "1" + - name: Report namesake gold disagreements + if: always() + run: | + python3 - <<'PY' + import json, os, pathlib + log = pathlib.Path('namesake-scripted-results.log') + rows = [] + totals = [] + if log.exists(): + for line in log.read_text().splitlines(): + if 'NAMESAKE_CASE ' in line: + rows.append(json.loads(line.partition('NAMESAKE_CASE ')[2])) + if 'NAMESAKE_SUMMARY ' in line: + totals.append(json.loads(line.partition('NAMESAKE_SUMMARY ')[2])) + summary = ['## Fixed namesake gold results', '', + 'Known-failure budget checks for regression; it does not certify identity correctness.', '', + '| Case | Order | Gold violations | Budget violations | Entities | Human | Model calls |', + '|---|---|---|---|---:|---:|---:|'] + for row in rows: + summary.append(f"| {row['id']} | {row['order']} | {', '.join(row['violations']) or 'none'} | {', '.join(row['budget_violations']) or 'none'} | {row['entities']} | {row['pending_human']} | {row['model_calls']} |") + summary.extend(['', f"Gold failures: {sum(bool(r['violations']) for r in rows)} / {len(rows)} cases.", '', + 'Strict check: `UTOPIA_NAMESAKE_STRICT=1 cargo test -p utopia-server namesake_scripted_cases -- --nocapture`', '']) + with open(os.environ['GITHUB_STEP_SUMMARY'], 'a') as output: + output.write('\n'.join(summary)) + if len(rows) != 8 or len(totals) != 1 or totals[0]['cases'] != 8: + raise SystemExit('Incomplete namesake measurement; inspect the test log') + if totals[0]['unaccepted'] or totals[0]['budget_violations']: + raise SystemExit('Namesake regression exceeds the declared ceiling') + PY + - name: Preserve namesake measurement log + if: always() + uses: actions/upload-artifact@v4 + with: + name: namesake-scripted-results + path: namesake-scripted-results.log + build: runs-on: ubuntu-latest steps: diff --git a/crates/utopia-server/src/main.rs b/crates/utopia-server/src/main.rs index 6b1ede67d..03e98a174 100644 --- a/crates/utopia-server/src/main.rs +++ b/crates/utopia-server/src/main.rs @@ -19,6 +19,8 @@ mod live; mod llm_util; mod mapping_index; mod mappings; +#[cfg(test)] +mod namesake_scripted_cases; mod notion; mod object_storage; mod ontology_agent; diff --git a/crates/utopia-server/src/namesake_scripted_cases.rs b/crates/utopia-server/src/namesake_scripted_cases.rs new file mode 100644 index 000000000..288bcb144 --- /dev/null +++ b/crates/utopia-server/src/namesake_scripted_cases.rs @@ -0,0 +1,403 @@ +//! Scripted namesake diagnostics: source identity is the oracle, never the model verdict. +//! Default CI checks an explicit known-debt ceiling; it does not certify gold correctness. +//! UTOPIA_NAMESAKE_STRICT=1 makes every gold violation fail, including existing defects. +use crate::state::AppState; +use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::post, Json, Router}; +use serde_json::{json, Value}; +use sha2::{Digest, Sha256}; +use std::{ + collections::VecDeque, + sync::{Arc, Mutex}, + time::Duration, +}; +use utopia_store::resolution::ReviewStage; +use uuid::Uuid; + +struct Case { + id: &'static str, + gold: [&'static str; 2], + verdict: Option<&'static str>, + // Only this named violation is allowed, at most once per case/order. Controls have none. + debt: Option<&'static str>, +} + +const CASES: [Case; 4] = [ + Case { + id: "resolver_distinct", + gold: ["employee_E17", "employee_F42"], + verdict: None, + debt: Some("false_merge"), + }, + Case { + id: "adjudicator_distinct_same", + gold: ["employee_E17", "employee_F42"], + verdict: Some("same"), + debt: Some("false_merge"), + }, + Case { + id: "adjudicator_true_same", + gold: ["employee_E17", "employee_E17"], + verdict: Some("same"), + debt: None, + }, + Case { + id: "adjudicator_distinct_different", + gold: ["employee_E17", "employee_F42"], + verdict: Some("different"), + debt: None, + }, +]; + +struct Step { + prompt_has: String, + text: String, + stream: bool, +} + +#[derive(Clone, Default)] +struct Model { + steps: Arc>>, + requests: Arc>>, + errors: Arc>>, +} + +async fn reply(State(m): State, Json(body): Json) -> axum::response::Response { + m.requests.lock().unwrap().push(body.clone()); + let step = m.steps.lock().unwrap().pop_front(); + let error = match &step { + None => Some("unexpected model request: script exhausted".to_string()), + Some(s) if !body["messages"].to_string().contains(&s.prompt_has) => { + Some(format!("unexpected prompt: missing {}", s.prompt_has)) + } + Some(s) if (body["stream"] == true) != s.stream => { + Some("unexpected chat transport".to_string()) + } + _ => None, + }; + if let Some(error) = error { + // Handler panics alone can be lost in a detached task; the caller checks this ledger. + m.errors.lock().unwrap().push(error.clone()); + return (StatusCode::BAD_REQUEST, error).into_response(); + } + let step = step.unwrap(); + if !step.stream { + return Json( + json!({"choices":[{"message":{"role":"assistant","content":step.text}, + "finish_reason":"stop"}]}), + ) + .into_response(); + } + let frame = json!({"choices":[{"delta":{"content":step.text}}]}); + let done = json!({"choices":[{"delta":{},"finish_reason":"stop"}]}); + ( + [("content-type", "text/event-stream")], + format!("data: {frame}\n\ndata: {done}\n\ndata: [DONE]\n\n"), + ) + .into_response() +} + +struct Fx { + pool: sqlx::PgPool, + state: AppState, + org: Uuid, + kb: Uuid, + docs: [Uuid; 2], + chunks: [Uuid; 2], + quotes: [String; 2], + model: Model, + server: tokio::task::JoinHandle<()>, + _dir: tempfile::TempDir, +} + +impl Fx { + async fn new(pool: &sqlx::PgPool, case: &Case, org: Uuid) -> anyhow::Result { + let (ws, kb) = (Uuid::now_v7(), Uuid::now_v7()); + sqlx::query("INSERT INTO organizations(id,name) VALUES ($1,'scripted-namesake')") + .bind(org) + .execute(pool) + .await?; + sqlx::query("INSERT INTO workspaces(id,org_id,name) VALUES ($1,$2,'scripted-namesake')") + .bind(ws) + .bind(org) + .execute(pool) + .await?; + sqlx::query( + "INSERT INTO knowledge_bases(id,workspace_id,name) VALUES ($1,$2,'scripted-namesake')", + ) + .bind(kb) + .bind(ws) + .execute(pool) + .await?; + let docs = [Uuid::now_v7(), Uuid::now_v7()]; + let chunks = [Uuid::now_v7(), Uuid::now_v7()]; + // Employee numbers are explicit source identities, not an ontology inference or a + // production merge rule. Different workers can share a name and employer. + let quotes = std::array::from_fn(|i| { + format!( + "Directory {}: Alex Lee (employee {}) works for Acme.", + i + 1, + case.gold[i].strip_prefix("employee_").unwrap() + ) + }); + for i in 0..2 { + sqlx::query("INSERT INTO documents(id,kb_id,filename,sha256) VALUES ($1,$2,$3,$4)") + .bind(docs[i]) + .bind(kb) + .bind(format!("directory-{i}.txt")) + .bind( + Sha256::digest(quotes[i].as_bytes()) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect::(), + ) + .execute(pool) + .await?; + // Fixed context vectors exercise the real resolver without downloading an + // embedding model or involving the independent name-vector recall channel. + sqlx::query("INSERT INTO chunks(id,kb_id,document_id,seq,text,embedding) VALUES ($1,$2,$3,0,$4,'[1,0,0]'::vector)") + .bind(chunks[i]).bind(kb).bind(docs[i]).bind("es[i]).execute(pool).await?; + } + let model = Model::default(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?; + let endpoint = format!("http://{}", listener.local_addr()?); + let router = Router::new() + .route("/chat/completions", post(reply)) + .with_state(model.clone()); + utopia_store::settings::upsert( + pool, + ws, + Some(&endpoint), + None, + Some("scripted"), + None, + None, + None, + None, + ) + .await?; + let dir = tempfile::tempdir()?; + let config = utopia_core::config::AppConfig { + data_dir: dir.path().to_string_lossy().into_owned(), + ..Default::default() + }; + let search = Arc::new(utopia_search::SearchIndex::open( + &dir.path().join("search"), + )?); + let state = AppState::new(pool.clone(), &config, search, "test-only".into()); + // Start only after fallible fixture initialization, so errors cannot leak a server. + let server = tokio::spawn(async move { axum::serve(listener, router).await.unwrap() }); + Ok(Self { + pool: pool.clone(), + state, + org, + kb, + docs, + chunks, + quotes, + model, + server, + _dir: dir, + }) + } + + async fn seed(&self, i: usize) -> anyhow::Result { + let id = Uuid::now_v7(); + sqlx::query("INSERT INTO entities(id,kb_id,canonical_name,profile_embedding,profile_n) VALUES ($1,$2,'Alex Lee','[1,0,0]'::vector,1)") + .bind(id).bind(self.kb).execute(&self.pool).await?; + let fact = Uuid::now_v7(); + sqlx::query("INSERT INTO facts(id,kb_id,subject_id,layer,phrase,object_value) VALUES ($1,$2,$3,'open','works for','{\"value\":\"Acme\"}'::jsonb)") + .bind(fact).bind(self.kb).bind(id).execute(&self.pool).await?; + sqlx::query("INSERT INTO fact_evidence(fact_id,chunk_id,quote,document_id,doc_version) VALUES ($1,$2,$3,$4,1)") + .bind(fact).bind(self.chunks[i]).bind(&self.quotes[i]).bind(self.docs[i]).execute(&self.pool).await?; + Ok(id) + } + + async fn run(&self, case: &Case, reverse: bool) -> anyhow::Result { + let order = if reverse { [1, 0] } else { [0, 1] }; + let first = self.seed(order[0]).await?; + if let Some(verdict) = case.verdict { + let second = self.seed(order[1]).await?; + utopia_store::resolution::create_review( + &self.pool, + self.kb, + first, + second, + 0.7, + "scripted_same_name", + ReviewStage::Adjudicating, + ) + .await?; + self.model.steps.lock().unwrap().push_back(Step { + prompt_has: "Alex Lee".into(), + stream: false, + text: json!({"verdicts":[{"i":0,"verdict":verdict,"confidence":0.99, + "why":"same name and employer"}]}) + .to_string(), + }); + crate::adjudication::adjudicate_entities(&self.state, self.kb).await?; + } else { + // Two bare-name candidates already exist. The wrong holder has the + // uniquely highest profile score; source identity must still win. + // Equal vectors would exercise the existing tie guard instead. + sqlx::query("INSERT INTO entities(id,kb_id,canonical_name,profile_embedding,profile_n) VALUES ($1,$2,'Alex Lee','[0,1,0]'::vector,1)") + .bind(Uuid::now_v7()).bind(self.kb).execute(&self.pool).await?; + let i = order[1]; + self.model.steps.lock().unwrap().push_back(Step { + prompt_has: self.quotes[i].clone(), + stream: true, + text: json!({"e":[["Alex Lee","person",1]], + "s":[[self.quotes[i],"Alex Lee","works for",null,"Acme",null,null,null]], + "n":[]}) + .to_string(), + }); + crate::extraction::extract_document(&self.state, self.docs[i], Default::default()) + .await?; + let status: String = + sqlx::query_scalar("SELECT graph_status FROM documents WHERE id=$1") + .bind(self.docs[i]) + .fetch_one(&self.pool) + .await?; + anyhow::ensure!(status == "done", "extraction did not complete: {status}"); + } + anyhow::ensure!( + self.model.errors.lock().unwrap().is_empty(), + "script errors: {:?}", + self.model.errors.lock().unwrap() + ); + anyhow::ensure!( + self.model.steps.lock().unwrap().is_empty(), + "unconsumed model script" + ); + let model_calls = self.model.requests.lock().unwrap().len(); + anyhow::ensure!( + model_calls == 1, + "expected exactly one scripted request, got {model_calls}" + ); + + let mut actual = Vec::new(); + for doc in self.docs { + // Read each source's fact owner after production resolution/merging, including + // duplicate facts invalidated by merge deduplication: their moved subject + // still records source identity attribution. + // Missing/duplicate extraction is a harness/coverage failure, never known debt. + let owners: Vec = sqlx::query_scalar( + "SELECT DISTINCT COALESCE(e.merged_into,e.id) FROM facts f + JOIN fact_evidence fe ON fe.fact_id=f.id JOIN entities e ON e.id=f.subject_id + WHERE f.kb_id=$1 AND fe.document_id=$2 AND f.layer='open' + AND f.phrase='works for'", + ) + .bind(self.kb) + .bind(doc) + .fetch_all(&self.pool) + .await?; + anyhow::ensure!( + owners.len() == 1, + "source {doc} has {} fact owners", + owners.len() + ); + actual.push(owners[0]); + } + let mut violations = Vec::new(); + if case.gold[0] == case.gold[1] && actual[0] != actual[1] { + violations.push("false_split"); + } + if case.gold[0] != case.gold[1] && actual[0] == actual[1] { + violations.push("false_merge"); + } + let entities: i64 = sqlx::query_scalar( + "SELECT count(*) FROM entities WHERE kb_id=$1 AND merged_into IS NULL", + ) + .bind(self.kb) + .fetch_one(&self.pool) + .await?; + let pending_human: i64 = sqlx::query_scalar("SELECT count(*) FROM resolution_reviews WHERE kb_id=$1 AND stage='human' AND status='pending'") + .bind(self.kb).fetch_one(&self.pool).await?; + let budget_violations = if pending_human > 0 { + vec!["unexpected_human_review"] + } else { + Vec::new() + }; + Ok( + json!({"id":case.id,"order":if reverse {"reverse"} else {"forward"}, + "gold":case.gold,"actual":actual,"violations":violations, + "budget_violations":budget_violations, + "entities":entities,"pending_human":pending_human,"model_calls":model_calls}), + ) + } + + async fn cleanup(&self) -> anyhow::Result<()> { + self.server.abort(); + sqlx::query( + "DELETE FROM jobs WHERE payload->>'kb_id'=$1 OR payload->>'document_id'=ANY($2)", + ) + .bind(self.kb.to_string()) + .bind(self.docs.map(|d| d.to_string()).to_vec()) + .execute(&self.pool) + .await?; + sqlx::query("DELETE FROM organizations WHERE id=$1") + .bind(self.org) + .execute(&self.pool) + .await?; + Ok(()) + } +} + +#[tokio::test] +async fn scripted_namesake_cases_report_gold_violations_without_relaxing_the_oracle( +) -> anyhow::Result<()> { + let Some(url) = utopia_store::test_db::url() else { + return Ok(()); + }; + let pool = sqlx::PgPool::connect(&url).await?; + // CI migrates before workspace tests; this fixture does not race a global migration. + let strict = std::env::var("UTOPIA_NAMESAKE_STRICT").as_deref() == Ok("1"); + let mut failures = Vec::new(); + let mut total = 0; + let mut budget_total = 0; + for case in &CASES { + for reverse in [false, true] { + let org = Uuid::now_v7(); + let f = match Fx::new(&pool, case, org).await { + Ok(f) => f, + Err(error) => { + sqlx::query("DELETE FROM organizations WHERE id=$1") + .bind(org) + .execute(&pool) + .await?; + return Err(error); + } + }; + let run = tokio::time::timeout(Duration::from_secs(15), f.run(case, reverse)).await; + f.cleanup().await?; + let row = run.map_err(|_| anyhow::anyhow!("{} timed out", case.id))??; + println!("NAMESAKE_CASE {row}"); + let violations = row["violations"].as_array().unwrap(); + let budget_violations = row["budget_violations"].as_array().unwrap(); + total += violations.len(); + budget_total += budget_violations.len(); + // Improvements are accepted. A new type, control failure or more than one + // allowed failure is rejected; no assertion requires wrong behavior to remain. + let within_debt = violations.len() <= usize::from(case.debt.is_some()) + && violations.iter().all(|v| v.as_str() == case.debt); + if !within_debt || (strict && !violations.is_empty()) || !budget_violations.is_empty() { + failures.push(format!( + "{} / {}: gold={} budget={}", + case.id, row["order"], row["violations"], row["budget_violations"] + )); + } + } + } + println!( + "NAMESAKE_SUMMARY {}", + json!({"cases":8,"gold_violations":total, + "budget_violations":budget_total, + "mode":if strict {"strict_gold"} else {"known_debt_ceiling"}, + "gold_passed":total == 0,"unaccepted":failures}) + ); + anyhow::ensure!( + failures.is_empty(), + "namesake gold violations outside acceptance: {}", + failures.join("; ") + ); + Ok(()) +} diff --git a/scripts/bench/namesake-scripted.md b/scripts/bench/namesake-scripted.md new file mode 100644 index 000000000..bd1234650 --- /dev/null +++ b/scripts/bench/namesake-scripted.md @@ -0,0 +1,58 @@ +# Fixed namesake cases + +This is the test-only measurement requested in #1136 and the reviews of #1192 +and #1199. It runs real extraction/resolution and adjudication against local, +fixed chat replies (streaming extraction and JSON adjudication). It makes no +provider request and needs no API key or embedding download. It does not introduce an identity gate or an unresolved +entity lifecycle. + +Each source directory identifies an employee independently of the model reply. +E17 and F42 are different people with the same name and employer; two E17 +sources concern one person. The source-to-identity labels are the oracle, not +canonical names, model confidence or ontology uniqueness declarations. The +graph contains ordinary open employer facts, with no declared unique relation. + +| Case | Source identities | Script | Gold identity outcome | +|---|---|---|---| +| `resolver_distinct` | E17, F42 | Extract a bare Alex Lee mention with two existing candidates | Separate source fact owners | +| `adjudicator_distinct_same` | E17, F42 | High-confidence `same` | Separate source fact owners | +| `adjudicator_true_same` | E17, E17 | High-confidence `same` | One source fact owner | +| `adjudicator_distinct_different` | E17, F42 | High-confidence `different` | Separate source fact owners | + +The resolver case uses two bare-name candidates and fixed profile vectors. +The first source's holder has `[1,0,0]`; the other candidate has `[0,1,0]` and +no attached facts. The new mention has context `[1,0,0]`, so the wrong holder +has the uniquely highest score. This tests profile preference overriding +source identity, not the existing equal-score tie guard or a degree tie-break. + +Every case runs in both source/seed orders, each in a fresh KB. The output records +source identities, actual fact owners, gold violations, surviving entities, +pending human reviews and exact scripted request counts. Ownership follows +the moved subjects of source facts, including duplicate facts invalidated by +merge deduplication; dropping those would lose one source from the measurement. +This measures source identity attribution, not live-fact coverage or query recall. Unexpected/missing +requests, incomplete extraction, missing source ownership and database errors +are hard failures, not accepted identity debt. + +After migrating a test database, run: + +```sh +UTOPIA_TEST_REQUIRE_DB=1 cargo test -p utopia-server namesake_scripted_cases -- --nocapture +UTOPIA_TEST_REQUIRE_DB=1 UTOPIA_NAMESAKE_STRICT=1 cargo test -p utopia-server namesake_scripted_cases -- --nocapture +``` + +Both use `UTOPIA_DATABASE_URL`, as the workspace tests do. The strict mode fails +on every gold violation. The default CI mode has an explicit debt ceiling: +at most one `false_merge` in each order of the first two cases, and no identity +violations in the controls. Improvements are allowed; new/worse violations fail. +Human-review growth is reported and checked separately as a budget regression, +not treated as an identity gold label. No tests are ignored and no assertion +requires a wrong merge to persist. + +CI executes all cases, publishes a table of gold and budget violations, and +preserves the measurement log. A green budget check means no regression against +the declared ceiling; it does **not** mean all identities are correct. Reviewers +can use the unchanged gold labels and strict command to judge a smaller fix. + +These are small synthetic cases, not a replacement for the real-model identity +bench or a proposed general rule that parses employee numbers from prose. From 8050a8ce8fe40a8118372ea3627dabbc8b3ccbe7 Mon Sep 17 00:00:00 2001 From: Virtue039 Date: Tue, 13 Oct 2026 22:06:40 +0800 Subject: [PATCH 2/2] Give namesake fixtures distinguishing clues and require their named failures to remain observable Signed-off-by: Virtue039 --- .github/workflows/checks.yml | 44 --- .../src/namesake_scripted_cases.rs | 358 ++++++++++-------- scripts/bench/namesake-scripted.md | 58 --- 3 files changed, 202 insertions(+), 258 deletions(-) delete mode 100644 scripts/bench/namesake-scripted.md diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index a3f9149c8..cb6c2a72c 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -86,50 +86,6 @@ jobs: UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia UTOPIA_TEST_REQUIRE_DB: "1" - # Keep the current gold failures visible while a smaller fix is discussed. - # Budget success means no new/worse failures, not correct identity resolution. - - name: Fixed namesake cases (known-failure budget) - shell: bash - run: cargo test --locked -p utopia-server namesake_scripted_cases -- --nocapture 2>&1 | tee namesake-scripted-results.log - env: - UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia - UTOPIA_TEST_REQUIRE_DB: "1" - - name: Report namesake gold disagreements - if: always() - run: | - python3 - <<'PY' - import json, os, pathlib - log = pathlib.Path('namesake-scripted-results.log') - rows = [] - totals = [] - if log.exists(): - for line in log.read_text().splitlines(): - if 'NAMESAKE_CASE ' in line: - rows.append(json.loads(line.partition('NAMESAKE_CASE ')[2])) - if 'NAMESAKE_SUMMARY ' in line: - totals.append(json.loads(line.partition('NAMESAKE_SUMMARY ')[2])) - summary = ['## Fixed namesake gold results', '', - 'Known-failure budget checks for regression; it does not certify identity correctness.', '', - '| Case | Order | Gold violations | Budget violations | Entities | Human | Model calls |', - '|---|---|---|---|---:|---:|---:|'] - for row in rows: - summary.append(f"| {row['id']} | {row['order']} | {', '.join(row['violations']) or 'none'} | {', '.join(row['budget_violations']) or 'none'} | {row['entities']} | {row['pending_human']} | {row['model_calls']} |") - summary.extend(['', f"Gold failures: {sum(bool(r['violations']) for r in rows)} / {len(rows)} cases.", '', - 'Strict check: `UTOPIA_NAMESAKE_STRICT=1 cargo test -p utopia-server namesake_scripted_cases -- --nocapture`', '']) - with open(os.environ['GITHUB_STEP_SUMMARY'], 'a') as output: - output.write('\n'.join(summary)) - if len(rows) != 8 or len(totals) != 1 or totals[0]['cases'] != 8: - raise SystemExit('Incomplete namesake measurement; inspect the test log') - if totals[0]['unaccepted'] or totals[0]['budget_violations']: - raise SystemExit('Namesake regression exceeds the declared ceiling') - PY - - name: Preserve namesake measurement log - if: always() - uses: actions/upload-artifact@v4 - with: - name: namesake-scripted-results - path: namesake-scripted-results.log - build: runs-on: ubuntu-latest steps: diff --git a/crates/utopia-server/src/namesake_scripted_cases.rs b/crates/utopia-server/src/namesake_scripted_cases.rs index 288bcb144..8e245ab67 100644 --- a/crates/utopia-server/src/namesake_scripted_cases.rs +++ b/crates/utopia-server/src/namesake_scripted_cases.rs @@ -1,6 +1,18 @@ -//! Scripted namesake diagnostics: source identity is the oracle, never the model verdict. -//! Default CI checks an explicit known-debt ceiling; it does not certify gold correctness. -//! UTOPIA_NAMESAKE_STRICT=1 makes every gold violation fail, including existing defects. +//! Fixed, DB-backed namesake cases for #1136 and #1202; no provider or embedding calls. +//! Run with a migrated UTOPIA_DATABASE_URL and UTOPIA_TEST_REQUIRE_DB=1: +//! cargo test --locked -p utopia-server namesake_scripted_cases -- --nocapture +//! Normal cargo test --workspace runs this too; no CI flags or ignored tests. +//! +//! Resolver pairs have two existing candidates with different department facts. +//! The incoming passage names one department: it conflicts with the profile preference +//! in the failing case and agrees in the control. Gold is that candidate's exact ID, +//! so a resolver that always creates another entity fails the control. +//! +//! Adjudicator pairs test the real HTTP request, not a scripted model's judgement: +//! scalar employee numbers and entity-valued departments must reach their own Record +//! A/B sections. Fixed different replies only complete the production caller. +//! Both seed orders run independently. The named list requires today's known failures +//! to fail and every other check to pass; a fix removes its names from that list. use crate::state::AppState; use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::post, Json, Router}; use serde_json::{json, Value}; @@ -13,41 +25,49 @@ use std::{ use utopia_store::resolution::ReviewStage; use uuid::Uuid; +// Both profiles are viable: cosine 1.0 versus 0.8, outside the existing tie guard. +const DEPARTMENTS: [&str; 2] = ["Platform Engineering", "Finance"]; +const EMPLOYEES: [&str; 2] = ["E17", "F42"]; + +#[derive(Clone, Copy)] +enum Check { + Resolver { target: usize }, + Adjudicator { literal: bool }, +} + struct Case { id: &'static str, - gold: [&'static str; 2], - verdict: Option<&'static str>, - // Only this named violation is allowed, at most once per case/order. Controls have none. - debt: Option<&'static str>, + check: Check, } const CASES: [Case; 4] = [ Case { - id: "resolver_distinct", - gold: ["employee_E17", "employee_F42"], - verdict: None, - debt: Some("false_merge"), + id: "resolver_passage_over_profile", + check: Check::Resolver { target: 1 }, }, Case { - id: "adjudicator_distinct_same", - gold: ["employee_E17", "employee_F42"], - verdict: Some("same"), - debt: Some("false_merge"), + id: "resolver_passage_agrees_with_profile", + check: Check::Resolver { target: 0 }, }, Case { - id: "adjudicator_true_same", - gold: ["employee_E17", "employee_E17"], - verdict: Some("same"), - debt: None, + id: "adjudicator_literal_clues_reach_request", + check: Check::Adjudicator { literal: true }, }, Case { - id: "adjudicator_distinct_different", - gold: ["employee_E17", "employee_F42"], - verdict: Some("different"), - debt: None, + id: "adjudicator_entity_clues_reach_request", + check: Check::Adjudicator { literal: false }, }, ]; +// #1136: a listed case MUST fail today; every other case MUST pass. A fix removes +// the corresponding names with its code change, so success cannot silently go stale. +const KNOWN_FAILURES: [(&str, &str); 4] = [ + ("resolver_passage_over_profile", "forward"), + ("resolver_passage_over_profile", "reverse"), + ("adjudicator_literal_clues_reach_request", "forward"), + ("adjudicator_literal_clues_reach_request", "reverse"), +]; + struct Step { prompt_has: String, text: String, @@ -101,9 +121,10 @@ struct Fx { state: AppState, org: Uuid, kb: Uuid, - docs: [Uuid; 2], - chunks: [Uuid; 2], - quotes: [String; 2], + docs: [Uuid; 3], + chunks: [Uuid; 3], + quotes: [String; 3], + departments: [Uuid; 2], model: Model, server: tokio::task::JoinHandle<()>, _dir: tempfile::TempDir, @@ -128,18 +149,34 @@ impl Fx { .bind(ws) .execute(pool) .await?; - let docs = [Uuid::now_v7(), Uuid::now_v7()]; - let chunks = [Uuid::now_v7(), Uuid::now_v7()]; - // Employee numbers are explicit source identities, not an ontology inference or a - // production merge rule. Different workers can share a name and employer. - let quotes = std::array::from_fn(|i| { - format!( - "Directory {}: Alex Lee (employee {}) works for Acme.", + let docs = std::array::from_fn(|_| Uuid::now_v7()); + let chunks = std::array::from_fn(|_| Uuid::now_v7()); + let departments = [Uuid::now_v7(), Uuid::now_v7()]; + for i in 0..2 { + sqlx::query("INSERT INTO entities(id,kb_id,canonical_name) VALUES ($1,$2,$3)") + .bind(departments[i]) + .bind(kb) + .bind(DEPARTMENTS[i]) + .execute(pool) + .await?; + } + let quotes = std::array::from_fn(|i| match (case.check, i) { + (Check::Adjudicator { literal: true }, 0..=1) => format!( + "Directory {}: Alex Lee has employee number {}.", + i + 1, + EMPLOYEES[i] + ), + (_, 0..=1) => format!( + "Directory {}: Alex Lee works for {}.", i + 1, - case.gold[i].strip_prefix("employee_").unwrap() - ) + DEPARTMENTS[i] + ), + (Check::Resolver { target }, _) => { + format!("Alex Lee from {} submitted a report.", DEPARTMENTS[target]) + } + _ => "Unused incoming passage.".into(), }); - for i in 0..2 { + for i in 0..3 { sqlx::query("INSERT INTO documents(id,kb_id,filename,sha256) VALUES ($1,$2,$3,$4)") .bind(docs[i]) .bind(kb) @@ -194,19 +231,26 @@ impl Fx { docs, chunks, quotes, + departments, model, server, _dir: dir, }) } - async fn seed(&self, i: usize) -> anyhow::Result { + async fn seed(&self, i: usize, check: Check) -> anyhow::Result { let id = Uuid::now_v7(); - sqlx::query("INSERT INTO entities(id,kb_id,canonical_name,profile_embedding,profile_n) VALUES ($1,$2,'Alex Lee','[1,0,0]'::vector,1)") - .bind(id).bind(self.kb).execute(&self.pool).await?; + let profile = if i == 0 { "[1,0,0]" } else { "[0.8,0.6,0]" }; + sqlx::query("INSERT INTO entities(id,kb_id,canonical_name,profile_embedding,profile_n) VALUES ($1,$2,'Alex Lee',$3::vector,1)") + .bind(id).bind(self.kb).bind(profile).execute(&self.pool).await?; let fact = Uuid::now_v7(); - sqlx::query("INSERT INTO facts(id,kb_id,subject_id,layer,phrase,object_value) VALUES ($1,$2,$3,'open','works for','{\"value\":\"Acme\"}'::jsonb)") - .bind(fact).bind(self.kb).bind(id).execute(&self.pool).await?; + if matches!(check, Check::Adjudicator { literal: true }) { + sqlx::query("INSERT INTO facts(id,kb_id,subject_id,layer,phrase,object_value) VALUES ($1,$2,$3,'open','employee number',$4)") + .bind(fact).bind(self.kb).bind(id).bind(json!({"value":EMPLOYEES[i]})).execute(&self.pool).await?; + } else { + sqlx::query("INSERT INTO facts(id,kb_id,subject_id,layer,phrase,object_id) VALUES ($1,$2,$3,'open','works for',$4)") + .bind(fact).bind(self.kb).bind(id).bind(self.departments[i]).execute(&self.pool).await?; + } sqlx::query("INSERT INTO fact_evidence(fact_id,chunk_id,quote,document_id,doc_version) VALUES ($1,$2,$3,$4,1)") .bind(fact).bind(self.chunks[i]).bind(&self.quotes[i]).bind(self.docs[i]).execute(&self.pool).await?; Ok(id) @@ -214,51 +258,91 @@ impl Fx { async fn run(&self, case: &Case, reverse: bool) -> anyhow::Result { let order = if reverse { [1, 0] } else { [0, 1] }; - let first = self.seed(order[0]).await?; - if let Some(verdict) = case.verdict { - let second = self.seed(order[1]).await?; - utopia_store::resolution::create_review( - &self.pool, - self.kb, - first, - second, - 0.7, - "scripted_same_name", - ReviewStage::Adjudicating, - ) - .await?; - self.model.steps.lock().unwrap().push_back(Step { - prompt_has: "Alex Lee".into(), - stream: false, - text: json!({"verdicts":[{"i":0,"verdict":verdict,"confidence":0.99, - "why":"same name and employer"}]}) - .to_string(), - }); - crate::adjudication::adjudicate_entities(&self.state, self.kb).await?; - } else { - // Two bare-name candidates already exist. The wrong holder has the - // uniquely highest profile score; source identity must still win. - // Equal vectors would exercise the existing tie guard instead. - sqlx::query("INSERT INTO entities(id,kb_id,canonical_name,profile_embedding,profile_n) VALUES ($1,$2,'Alex Lee','[0,1,0]'::vector,1)") - .bind(Uuid::now_v7()).bind(self.kb).execute(&self.pool).await?; - let i = order[1]; - self.model.steps.lock().unwrap().push_back(Step { - prompt_has: self.quotes[i].clone(), - stream: true, - text: json!({"e":[["Alex Lee","person",1]], - "s":[[self.quotes[i],"Alex Lee","works for",null,"Acme",null,null,null]], - "n":[]}) - .to_string(), - }); - crate::extraction::extract_document(&self.state, self.docs[i], Default::default()) - .await?; - let status: String = - sqlx::query_scalar("SELECT graph_status FROM documents WHERE id=$1") - .bind(self.docs[i]) - .fetch_one(&self.pool) - .await?; - anyhow::ensure!(status == "done", "extraction did not complete: {status}"); + let mut holders = [Uuid::nil(); 2]; + for i in order { + holders[i] = self.seed(i, case.check).await?; } + let (passed, observation) = match case.check { + Check::Resolver { target } => { + self.model.steps.lock().unwrap().push_back(Step { + prompt_has: self.quotes[2].clone(), stream: true, + text: json!({"e":[["Alex Lee","person",1]], + "s":[[self.quotes[2],"Alex Lee","submitted",null,"a report",null,null,null]], + "n":[]}).to_string(), + }); + crate::extraction::extract_document(&self.state, self.docs[2], Default::default()) + .await?; + let status: String = + sqlx::query_scalar("SELECT graph_status FROM documents WHERE id=$1") + .bind(self.docs[2]) + .fetch_one(&self.pool) + .await?; + anyhow::ensure!(status == "done", "extraction did not complete: {status}"); + let owners: Vec = sqlx::query_scalar( + "SELECT DISTINCT f.subject_id FROM facts f JOIN fact_evidence fe ON fe.fact_id=f.id + WHERE f.kb_id=$1 AND fe.document_id=$2 AND f.layer='open' AND f.phrase='submitted'") + .bind(self.kb).bind(self.docs[2]).fetch_all(&self.pool).await?; + anyhow::ensure!( + owners.len() == 1, + "incoming source has {} owners", + owners.len() + ); + // Exact pre-existing ID: creating a new entity is not successful disambiguation. + ( + owners[0] == holders[target], + json!({"expected_owner":holders[target], + "actual_owner":owners[0],"passage":self.quotes[2]}), + ) + } + Check::Adjudicator { literal } => { + utopia_store::resolution::create_review( + &self.pool, + self.kb, + holders[order[0]], + holders[order[1]], + 0.7, + "scripted_same_name", + ReviewStage::Adjudicating, + ) + .await?; + self.model.steps.lock().unwrap().push_back(Step { + prompt_has: "Alex Lee".into(), + stream: false, + // The reply only completes the caller. We test its INPUT, not model judgement. + text: json!({"verdicts":[{"i":0,"verdict":"different","confidence":0.99, + "why":"scripted transport reply"}]}) + .to_string(), + }); + crate::adjudication::adjudicate_entities(&self.state, self.kb).await?; + let requests = self.model.requests.lock().unwrap(); + let user = requests + .first() + .and_then(|r| r["messages"].as_array()) + .and_then(|m| m.iter().find(|m| m["role"] == "user")) + .and_then(|m| m["content"].as_str()) + .ok_or_else(|| anyhow::anyhow!("missing user request"))?; + let pair = user + .strip_prefix("Pair 0:\nRecord A: ") + .ok_or_else(|| anyhow::anyhow!("missing first record"))?; + let (left, right) = pair + .split_once("\nRecord B: ") + .ok_or_else(|| anyhow::anyhow!("missing second record"))?; + let (phrase, clues) = if literal { + ("employee number", EMPLOYEES) + } else { + ("works for", DEPARTMENTS) + }; + let expected = order.map(|i| format!("{phrase} → {}", clues[i])); + // Check each real HTTP side independently: overall contains would allow swaps. + let visible = left.contains(&expected[0]) && right.contains(&expected[1]); + let correctly_owned = + !left.contains(clues[order[1]]) && !right.contains(clues[order[0]]); + ( + visible && correctly_owned, + json!({"expected_facts":expected,"request_sides":[left,right]}), + ) + } + }; anyhow::ensure!( self.model.errors.lock().unwrap().is_empty(), "script errors: {:?}", @@ -273,55 +357,15 @@ impl Fx { model_calls == 1, "expected exactly one scripted request, got {model_calls}" ); - - let mut actual = Vec::new(); - for doc in self.docs { - // Read each source's fact owner after production resolution/merging, including - // duplicate facts invalidated by merge deduplication: their moved subject - // still records source identity attribution. - // Missing/duplicate extraction is a harness/coverage failure, never known debt. - let owners: Vec = sqlx::query_scalar( - "SELECT DISTINCT COALESCE(e.merged_into,e.id) FROM facts f - JOIN fact_evidence fe ON fe.fact_id=f.id JOIN entities e ON e.id=f.subject_id - WHERE f.kb_id=$1 AND fe.document_id=$2 AND f.layer='open' - AND f.phrase='works for'", - ) - .bind(self.kb) - .bind(doc) - .fetch_all(&self.pool) - .await?; - anyhow::ensure!( - owners.len() == 1, - "source {doc} has {} fact owners", - owners.len() - ); - actual.push(owners[0]); - } - let mut violations = Vec::new(); - if case.gold[0] == case.gold[1] && actual[0] != actual[1] { - violations.push("false_split"); - } - if case.gold[0] != case.gold[1] && actual[0] == actual[1] { - violations.push("false_merge"); - } - let entities: i64 = sqlx::query_scalar( - "SELECT count(*) FROM entities WHERE kb_id=$1 AND merged_into IS NULL", - ) - .bind(self.kb) - .fetch_one(&self.pool) - .await?; let pending_human: i64 = sqlx::query_scalar("SELECT count(*) FROM resolution_reviews WHERE kb_id=$1 AND stage='human' AND status='pending'") .bind(self.kb).fetch_one(&self.pool).await?; - let budget_violations = if pending_human > 0 { - vec!["unexpected_human_review"] - } else { - Vec::new() - }; + anyhow::ensure!( + pending_human == 0, + "unexpected pending Human reviews: {pending_human}" + ); Ok( json!({"id":case.id,"order":if reverse {"reverse"} else {"forward"}, - "gold":case.gold,"actual":actual,"violations":violations, - "budget_violations":budget_violations, - "entities":entities,"pending_human":pending_human,"model_calls":model_calls}), + "passed":passed,"observation":observation,"pending_human":pending_human,"model_calls":model_calls}), ) } @@ -343,19 +387,18 @@ impl Fx { } #[tokio::test] -async fn scripted_namesake_cases_report_gold_violations_without_relaxing_the_oracle( -) -> anyhow::Result<()> { +async fn scripted_namesake_cases_have_only_the_named_known_failures() -> anyhow::Result<()> { let Some(url) = utopia_store::test_db::url() else { return Ok(()); }; let pool = sqlx::PgPool::connect(&url).await?; - // CI migrates before workspace tests; this fixture does not race a global migration. - let strict = std::env::var("UTOPIA_NAMESAKE_STRICT").as_deref() == Ok("1"); - let mut failures = Vec::new(); - let mut total = 0; - let mut budget_total = 0; + // CI migrates first; this fixture must not race a global migration. + let mut mismatches = Vec::new(); + let mut seen = Vec::new(); + let mut failed = 0; for case in &CASES { for reverse in [false, true] { + let order = if reverse { "reverse" } else { "forward" }; let org = Uuid::now_v7(); let f = match Fx::new(&pool, case, org).await { Ok(f) => f, @@ -371,33 +414,36 @@ async fn scripted_namesake_cases_report_gold_violations_without_relaxing_the_ora f.cleanup().await?; let row = run.map_err(|_| anyhow::anyhow!("{} timed out", case.id))??; println!("NAMESAKE_CASE {row}"); - let violations = row["violations"].as_array().unwrap(); - let budget_violations = row["budget_violations"].as_array().unwrap(); - total += violations.len(); - budget_total += budget_violations.len(); - // Improvements are accepted. A new type, control failure or more than one - // allowed failure is rejected; no assertion requires wrong behavior to remain. - let within_debt = violations.len() <= usize::from(case.debt.is_some()) - && violations.iter().all(|v| v.as_str() == case.debt); - if !within_debt || (strict && !violations.is_empty()) || !budget_violations.is_empty() { - failures.push(format!( - "{} / {}: gold={} budget={}", - case.id, row["order"], row["violations"], row["budget_violations"] + let passed = row["passed"].as_bool().unwrap(); + failed += usize::from(!passed); + let name = (case.id, order); + seen.push(name); + if KNOWN_FAILURES.contains(&name) == passed { + mismatches.push(format!( + "{} / {order}: {}", + case.id, + if passed { + "unexpected pass: remove this name with its fix" + } else { + "unlisted failure" + } )); } } } + anyhow::ensure!( + KNOWN_FAILURES.iter().all(|name| seen.contains(name)), + "unused known-failure name" + ); println!( "NAMESAKE_SUMMARY {}", - json!({"cases":8,"gold_violations":total, - "budget_violations":budget_total, - "mode":if strict {"strict_gold"} else {"known_debt_ceiling"}, - "gold_passed":total == 0,"unaccepted":failures}) + json!({"cases":seen.len(),"failed_checks":failed, + "known_failures":KNOWN_FAILURES,"mismatches":mismatches}) ); anyhow::ensure!( - failures.is_empty(), - "namesake gold violations outside acceptance: {}", - failures.join("; ") + mismatches.is_empty(), + "namesake expectation mismatch: {}", + mismatches.join("; ") ); Ok(()) } diff --git a/scripts/bench/namesake-scripted.md b/scripts/bench/namesake-scripted.md deleted file mode 100644 index bd1234650..000000000 --- a/scripts/bench/namesake-scripted.md +++ /dev/null @@ -1,58 +0,0 @@ -# Fixed namesake cases - -This is the test-only measurement requested in #1136 and the reviews of #1192 -and #1199. It runs real extraction/resolution and adjudication against local, -fixed chat replies (streaming extraction and JSON adjudication). It makes no -provider request and needs no API key or embedding download. It does not introduce an identity gate or an unresolved -entity lifecycle. - -Each source directory identifies an employee independently of the model reply. -E17 and F42 are different people with the same name and employer; two E17 -sources concern one person. The source-to-identity labels are the oracle, not -canonical names, model confidence or ontology uniqueness declarations. The -graph contains ordinary open employer facts, with no declared unique relation. - -| Case | Source identities | Script | Gold identity outcome | -|---|---|---|---| -| `resolver_distinct` | E17, F42 | Extract a bare Alex Lee mention with two existing candidates | Separate source fact owners | -| `adjudicator_distinct_same` | E17, F42 | High-confidence `same` | Separate source fact owners | -| `adjudicator_true_same` | E17, E17 | High-confidence `same` | One source fact owner | -| `adjudicator_distinct_different` | E17, F42 | High-confidence `different` | Separate source fact owners | - -The resolver case uses two bare-name candidates and fixed profile vectors. -The first source's holder has `[1,0,0]`; the other candidate has `[0,1,0]` and -no attached facts. The new mention has context `[1,0,0]`, so the wrong holder -has the uniquely highest score. This tests profile preference overriding -source identity, not the existing equal-score tie guard or a degree tie-break. - -Every case runs in both source/seed orders, each in a fresh KB. The output records -source identities, actual fact owners, gold violations, surviving entities, -pending human reviews and exact scripted request counts. Ownership follows -the moved subjects of source facts, including duplicate facts invalidated by -merge deduplication; dropping those would lose one source from the measurement. -This measures source identity attribution, not live-fact coverage or query recall. Unexpected/missing -requests, incomplete extraction, missing source ownership and database errors -are hard failures, not accepted identity debt. - -After migrating a test database, run: - -```sh -UTOPIA_TEST_REQUIRE_DB=1 cargo test -p utopia-server namesake_scripted_cases -- --nocapture -UTOPIA_TEST_REQUIRE_DB=1 UTOPIA_NAMESAKE_STRICT=1 cargo test -p utopia-server namesake_scripted_cases -- --nocapture -``` - -Both use `UTOPIA_DATABASE_URL`, as the workspace tests do. The strict mode fails -on every gold violation. The default CI mode has an explicit debt ceiling: -at most one `false_merge` in each order of the first two cases, and no identity -violations in the controls. Improvements are allowed; new/worse violations fail. -Human-review growth is reported and checked separately as a budget regression, -not treated as an identity gold label. No tests are ignored and no assertion -requires a wrong merge to persist. - -CI executes all cases, publishes a table of gold and budget violations, and -preserves the measurement log. A green budget check means no regression against -the declared ceiling; it does **not** mean all identities are correct. Reviewers -can use the unchanged gold labels and strict command to judge a smaller fix. - -These are small synthetic cases, not a replacement for the real-model identity -bench or a proposed general rule that parses employee numbers from prose.