World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
An errata agent reviews each document's typed facts, structure first, through the gate (#885)
WaylandYangef6495cb5e02
diff --git a/crates/utopia-cli/src/main.rs b/crates/utopia-cli/src/main.rsindex 9ea25e36..920d5877 100644--- a/crates/utopia-cli/src/main.rs+++ b/crates/utopia-cli/src/main.rs@@ -82,7 +82,7 @@ struct ManifestDataDir {/// not a side effect of a code change.// 是迁移文件的**个数**,不是最大的编号(守卫 `schema_version_policy_compares_against_current`// 按个数比):编号有空缺时两者不同——0071 由一个开放 PR 占着,0072 先落,个数是 71-const CURRENT_SCHEMA_VERSION: u32 = 72;+const CURRENT_SCHEMA_VERSION: u32 = 73;fn main() -> anyhow::Result<()> {dotenvy::dotenv().ok();diff --git a/crates/utopia-core/src/models.rs b/crates/utopia-core/src/models.rsindex df5111d2..08251fef 100644--- a/crates/utopia-core/src/models.rs+++ b/crates/utopia-core/src/models.rs@@ -1481,6 +1481,8 @@ pub struct ReviewCounts {pub violations: i64,/// 对齐器两票不一致的签名与类别词(#725 对齐队列)pub alignment: i64,+ /// 勘误 agent 被闸门拦下、等人答的动作(0044 决定 7)+ pub errata: i64,pub defects: i64,pub merges: i64,/// agent 写下、等人回答的建议(0025)@@ -1585,6 +1587,7 @@ pub struct ReviewWaiting {pub violations: QueueWait,pub defects: QueueWait,pub alignment: QueueWait,+ pub errata: QueueWait,}/// 办过的:近 7 天与近 30 天两个窗口,加近 14 天每天一根柱diff --git a/crates/utopia-extract/src/errata.rs b/crates/utopia-extract/src/errata.rsnew file mode 100644index 00000000..e3fc11cb--- /dev/null+++ b/crates/utopia-extract/src/errata.rs@@ -0,0 +1,351 @@+//! 勘误 agent 的提示词与回复解析(0044 决定 7):一份文档、本体的属性、从它读出的事实,+//! 结构报了的带着理由;模型对每一条说 keep / retract / revise,全部答完才许 add。+//! 撤、改、加都得引文档的原话——引文在不在文档里由调用方验,这里只认形状。++use utopia_llm::ChatMessage;++/// 送去看的一条事实+#[derive(Debug, Clone)]+pub struct ErrataFact<'a> {+ pub id: i64,+ pub subject: &'a str,+ pub subject_class: Option<&'a str>,+ pub property: &'a str,+ pub object: &'a str,+ pub object_class: Option<&'a str>,+ /// 结构报的理由:domain / range / name_absent / no_date;空 = 抽样+ pub flag: Option<&'a str>,+ pub quote: Option<&'a str>,+}++/// 本体里的一条属性,给模型当词汇表+#[derive(Debug, Clone)]+pub struct ErrataProperty<'a> {+ pub key: &'a str,+ pub label: &'a str,+ pub description: &'a str,+ /// relation(宾语是东西)| attribute(宾语是值)+ pub kind: &'a str,+ pub domains: Vec<&'a str>,+ pub ranges: Vec<&'a str>,+ pub datatype: Option<&'a str>,+}++pub const ERRATA_SYSTEM: &str = "You review facts that were read from ONE document into a knowledge graph. \+You get the document, the ontology's properties, and a numbered list of facts. Some facts carry a FLAG from a structural check: \+domain = the subject is not a kind of thing the property allows; range = the object is not; \+name_absent = a name in the fact does not occur in the document; no_date = a date property holds something that is not a date.\n\+For EVERY fact answer one of:\n\+- keep: the document states it and the property fits.\n\+- retract: the document does not state it, or no property in the ontology fits what it states.\n\+- revise: the document states something close; give the property key and/or the object the document supports (a name that occurs in the document, or a value).\n\+Only after you have answered every fact may you add facts the document states plainly, the ontology can hold, and the list lacks. \+Do not add a fact whose subject or object is not named in the document.\n\+Every retract, revise and add must quote the document's own words: one contiguous span, verbatim. Never quote words that are not in the document. \+Prefer keeping a fact over retracting it when the document supports it; removing a correct fact costs more than leaving a doubtful one.\n\+Answer with JSON only, no prose:\n\+{\"a\":[[id,\"keep\"],[id,\"retract\",reason,quote],[id,\"revise\",{\"property\":key|null,\"object\":text|null},reason,quote],\+[null,\"add\",{\"subject\":name,\"property\":key,\"object\":name_or_value},reason,quote]]}";++fn property_line(p: &ErrataProperty<'_>) -> String {+ let mut line = format!("- {} ({}): {}", p.key, p.label, p.description.trim());+ match p.kind {+ "attribute" => {+ line.push_str(&format!(+ " [value{}]",+ p.datatype.map(|d| format!(": {d}")).unwrap_or_default()+ ));+ }+ _ => line.push_str(" [thing]"),+ }+ if !p.domains.is_empty() {+ line.push_str(&format!(" subject: {}", p.domains.join("|")));+ }+ if !p.ranges.is_empty() {+ line.push_str(&format!(" object: {}", p.ranges.join("|")));+ }+ line+}++fn fact_line(f: &ErrataFact<'_>) -> String {+ let class = |c: Option<&str>| c.map(|c| format!(" ({c})")).unwrap_or_default();+ let mut line = format!(+ "{}: {}{} —{}→ {}{}",+ f.id,+ f.subject,+ class(f.subject_class),+ f.property,+ f.object,+ class(f.object_class)+ );+ if let Some(flag) = f.flag {+ line.push_str(&format!(" FLAG {flag}"));+ }+ if let Some(q) = f.quote.map(str::trim).filter(|q| !q.is_empty()) {+ line.push_str(&format!(" · read from: \"{q}\""));+ }+ line+}++pub fn build_errata_messages(+ document: &str,+ properties: &[ErrataProperty<'_>],+ facts: &[ErrataFact<'_>],+) -> Vec<ChatMessage> {+ let props = properties+ .iter()+ .map(property_line)+ .collect::<Vec<_>>()+ .join("\n");+ let list = facts.iter().map(fact_line).collect::<Vec<_>>().join("\n");+ vec![+ ChatMessage {+ role: "system".into(),+ content: ERRATA_SYSTEM.to_string(),+ },+ ChatMessage {+ role: "user".into(),+ content: format!("DOCUMENT:\n{document}\n\nPROPERTIES:\n{props}\n\nFACTS:\n{list}"),+ },+ ]+}++/// 模型对一条事实的说法+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum Verdict {+ Keep,+ Retract,+ Revise {+ property: Option<String>,+ object: Option<String>,+ },+}++#[derive(Debug, Clone, PartialEq, Eq)]+pub struct FactVerdict {+ pub id: i64,+ pub verdict: Verdict,+ pub reason: String,+ pub quote: Option<String>,+}++#[derive(Debug, Clone, PartialEq, Eq)]+pub struct Addition {+ pub subject: String,+ pub property: String,+ pub object: String,+ pub reason: String,+ pub quote: String,+}++#[derive(Debug, Default, PartialEq, Eq)]+pub struct ErrataResponse {+ pub verdicts: Vec<FactVerdict>,+ pub additions: Vec<Addition>,+ /// 形状不对的行:不认识的 id、重复的 id、撤改加没带引文、改了等于没改+ pub malformed: usize,+ /// 没把给它的事实答完就加的:加的全部不算(「先查完给它的,再加」是契约)+ pub additions_refused: usize,+}++fn text_of(v: Option<&serde_json::Value>) -> Option<String> {+ v.and_then(|x| match x {+ serde_json::Value::String(s) => Some(s.trim().to_string()),+ serde_json::Value::Number(n) => Some(n.to_string()),+ _ => None,+ })+ .filter(|s| !s.is_empty())+}++/// 解析勘误的回复。给出的事实一条一票,多的、错的计数不算;加的只在全部答完时算+pub fn parse_errata_response(+ raw: &str,+ facts: &[ErrataFact<'_>],+) -> Result<ErrataResponse, String> {+ let v: serde_json::Value =+ serde_json::from_str(extract_json(raw)).map_err(|e| e.to_string())?;+ let rows = v+ .get("a")+ .and_then(|x| x.as_array())+ .ok_or_else(|| "no \"a\" array".to_string())?;+ let mut out = ErrataResponse::default();+ let mut seen = std::collections::HashSet::new();+ let mut additions = Vec::new();+ for row in rows {+ let Some(arr) = row.as_array() else {+ out.malformed += 1;+ continue;+ };+ let action = arr.get(1).and_then(|x| x.as_str()).unwrap_or("");+ if action == "add" {+ let parsed = (|| {+ let spec = arr.get(2)?.as_object()?;+ Some(Addition {+ subject: text_of(spec.get("subject"))?,+ property: text_of(spec.get("property"))?,+ object: text_of(spec.get("object"))?,+ reason: text_of(arr.get(3)).unwrap_or_default(),+ quote: text_of(arr.get(4))?,+ })+ })();+ match parsed {+ Some(a) => additions.push(a),+ None => out.malformed += 1,+ }+ continue;+ }+ let parsed = (|| {+ let id = arr.first()?.as_i64()?;+ facts.iter().find(|f| f.id == id)?;+ if !seen.insert(id) {+ return None;+ }+ match action {+ "keep" => Some(FactVerdict {+ id,+ verdict: Verdict::Keep,+ reason: text_of(arr.get(2)).unwrap_or_default(),+ quote: None,+ }),+ "retract" => Some(FactVerdict {+ id,+ verdict: Verdict::Retract,+ reason: text_of(arr.get(2)).unwrap_or_default(),+ quote: Some(text_of(arr.get(3))?),+ }),+ "revise" => {+ let spec = arr.get(2)?.as_object()?;+ let property = text_of(spec.get("property"));+ let object = text_of(spec.get("object"));+ if property.is_none() && object.is_none() {+ return None;+ }+ Some(FactVerdict {+ id,+ verdict: Verdict::Revise { property, object },+ reason: text_of(arr.get(3)).unwrap_or_default(),+ quote: Some(text_of(arr.get(4))?),+ })+ }+ _ => None,+ }+ })();+ match parsed {+ Some(v) => out.verdicts.push(v),+ None => out.malformed += 1,+ }+ }+ if facts.iter().all(|f| seen.contains(&f.id)) {+ out.additions = additions;+ } else {+ out.additions_refused = additions.len();+ }+ Ok(out)+}++fn extract_json(raw: &str) -> &str {+ match (raw.find('{'), raw.rfind('}')) {+ (Some(a), Some(b)) if b > a => &raw[a..=b],+ _ => raw,+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn facts() -> Vec<ErrataFact<'static>> {+ vec![+ ErrataFact {+ id: 0,+ subject: "Acme",+ subject_class: Some("organization"),+ property: "based_in",+ object: "Paris",+ object_class: Some("place"),+ flag: Some("name_absent"),+ quote: Some("Acme is based in London"),+ },+ ErrataFact {+ id: 1,+ subject: "Acme",+ subject_class: Some("organization"),+ property: "based_in",+ object: "London",+ object_class: Some("place"),+ flag: None,+ quote: None,+ },+ ]+ }++ #[test]+ fn the_prompt_carries_the_document_the_flags_and_the_properties() {+ let props = vec![ErrataProperty {+ key: "based_in",+ label: "based in",+ description: "where an organization is based",+ kind: "relation",+ domains: vec!["organization"],+ ranges: vec!["place"],+ datatype: None,+ }];+ let m = build_errata_messages("Acme is based in London.", &props, &facts());+ assert_eq!(m.len(), 2);+ assert!(m[0].content.contains("JSON only"));+ let u = &m[1].content;+ assert!(u.contains("DOCUMENT:\nAcme is based in London."));+ assert!(u.contains("- based_in (based in): where an organization is based [thing] subject: organization object: place"));+ assert!(u.contains("0: Acme (organization) —based_in→ Paris (place) FLAG name_absent · read from: \"Acme is based in London\""));+ assert!(+ u.contains("1: Acme (organization) —based_in→ London (place)\n")+ || u.ends_with("1: Acme (organization) —based_in→ London (place)")+ );+ }++ #[test]+ fn every_fact_gets_one_verdict_and_bad_rows_are_counted_not_believed() {+ let raw = r#"Sure: {"a":[[0,"retract","not in the document","Acme is based in London"],[1,"keep"],[0,"keep"],[7,"keep"],[1,"revise",{"property":null,"object":null},"x","q"],["nope"]]}"#;+ let r = parse_errata_response(raw, &facts()).unwrap();+ assert_eq!(r.verdicts.len(), 2);+ assert_eq!(r.verdicts[0].verdict, Verdict::Retract);+ assert_eq!(+ r.verdicts[0].quote.as_deref(),+ Some("Acme is based in London")+ );+ assert_eq!(r.verdicts[1].verdict, Verdict::Keep);+ // 重复的 0、不认识的 7、改了等于没改的、不是数组的:四条坏行+ assert_eq!(r.malformed, 4);+ }++ #[test]+ fn a_retraction_without_a_quote_is_malformed() {+ let raw = r#"{"a":[[0,"retract","no quote"],[1,"keep"]]}"#;+ let r = parse_errata_response(raw, &facts()).unwrap();+ assert_eq!(r.verdicts.len(), 1);+ assert_eq!(r.malformed, 1);+ }++ #[test]+ fn additions_count_only_after_every_given_fact_is_answered() {+ let add = r#"[null,"add",{"subject":"Acme","property":"ceo","object":"Jane Roe"},"stated","Jane Roe runs Acme"]"#;+ let partial = format!(r#"{{"a":[[0,"keep"],{add}]}}"#);+ let r = parse_errata_response(&partial, &facts()).unwrap();+ assert!(r.additions.is_empty());+ assert_eq!(r.additions_refused, 1);+ let full = format!(+ r#"{{"a":[[0,"keep"],[1,"keep"],{add},[null,"add",{{"subject":"Acme"}},"half","q"]]}}"#+ );+ let r = parse_errata_response(&full, &facts()).unwrap();+ assert_eq!(r.additions.len(), 1);+ assert_eq!(r.additions[0].object, "Jane Roe");+ assert_eq!(r.additions[0].quote, "Jane Roe runs Acme");+ assert_eq!(r.malformed, 1);+ }++ #[test]+ fn no_array_is_an_error_not_an_empty_answer() {+ assert!(parse_errata_response("{}", &facts()).is_err());+ assert!(parse_errata_response("not json", &facts()).is_err());+ }+}diff --git a/crates/utopia-extract/src/lib.rs b/crates/utopia-extract/src/lib.rsindex b4ce86f9..a1b5e05d 100644--- a/crates/utopia-extract/src/lib.rs+++ b/crates/utopia-extract/src/lib.rs@@ -10,6 +10,7 @@ use serde::Deserialize;use utopia_llm::ChatMessage;pub mod align;+pub mod errata;pub mod governor;pub mod implication;pub mod open;diff --git a/crates/utopia-llm/src/lib.rs b/crates/utopia-llm/src/lib.rsindex e50e0233..a9a8d97f 100644--- a/crates/utopia-llm/src/lib.rs+++ b/crates/utopia-llm/src/lib.rs@@ -303,6 +303,15 @@ pub struct Reply {/// 端点给的收尾原因(`stop` / `length` / …)。流里没有这一项就是 `None`:/// 有的实现只发 `[DONE]`,缺席不代表答案是完整的pub finish_reason: Option<String>,+ /// 端点报的用量(最后一帧)。不报就是 `None`——账上不编数字+ pub usage: Option<Usage>,+}++/// 一次调用的 token 用量,端点自己报的+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]+pub struct Usage {+ pub prompt_tokens: u64,+ pub completion_tokens: u64,}impl Reply {@@ -501,6 +510,7 @@ impl LlmClient {let (mut buf, mut answer) = (Vec::new(), String::new());let (mut saw_frame, mut ended) = (false, false);let mut finish_reason: Option<String> = None;+ let mut usage: Option<Usage> = None;while let Some(part) = bytes.next().await {let part = part.map_err(Unreachable)?;// 网络片段可能断在 UTF-8 字符中间,等完整 SSE 帧到齐再解码。@@ -515,6 +525,7 @@ impl LlmClient {&mut saw_frame,&mut ended,&mut finish_reason,+ &mut usage,);}}@@ -527,6 +538,7 @@ impl LlmClient {&mut saw_frame,&mut ended,&mut finish_reason,+ &mut usage,);}if !saw_frame {@@ -542,6 +554,7 @@ impl LlmClient {Ok(Reply {text: strip_reasoning(&answer).to_string(),finish_reason,+ usage,})}@@ -553,6 +566,7 @@ impl LlmClient {saw_frame: &mut bool,ended: &mut bool,finish_reason: &mut Option<String>,+ usage: &mut Option<Usage>,) {for line in frame.lines() {let Some(data) = line.strip_prefix("data:").map(str::trim) else {@@ -581,6 +595,11 @@ impl LlmClient {// 用量只在最后一帧(choices 为空)出现if !v["usage"].is_null() {log_usage(&self.model, &v);+ let u = &v["usage"];+ *usage = Some(Usage {+ prompt_tokens: u["prompt_tokens"].as_u64().unwrap_or(0),+ completion_tokens: u["completion_tokens"].as_u64().unwrap_or(0),+ });}}}diff --git a/crates/utopia-server/src/api/mod.rs b/crates/utopia-server/src/api/mod.rsindex 0693d1d2..01fd55a7 100644--- a/crates/utopia-server/src/api/mod.rs+++ b/crates/utopia-server/src/api/mod.rs@@ -510,6 +510,11 @@ pub fn router(state: AppState, cfg: &AppConfig) -> Router {"/kbs/{id}/review/alignment/kind-words/{kind_word}",post(review_routes::decide_alignment_kind_word),)+ // 勘误队列(0044 决定 7):人批或否 agent 被闸门拦下的一笔+ .route(+ "/kbs/{id}/review/errata/{action_id}",+ post(review_routes::decide_errata),+ )// 人批或驳一条蕴含规则(0044 决定 3 第五片).route("/kbs/{id}/review/alignment/rules/{rule_id}",diff --git a/crates/utopia-server/src/api/review_routes.rs b/crates/utopia-server/src/api/review_routes.rsindex 9c29f352..c87ebb43 100644--- a/crates/utopia-server/src/api/review_routes.rs+++ b/crates/utopia-server/src/api/review_routes.rs@@ -121,6 +121,8 @@ pub async fn list("alignment" => {json!(utopia_store::alignment_queue::list(&state.pool, kb_id, limit, offset).await?)}+ // 勘误 agent 留给人的动作(0044 决定 7):闸门拦下的撤、改、加+ "errata" => json!(utopia_store::errata::held(&state.pool, kb_id, limit, offset).await?),"violations" => {json!(utopia_store::reasoning::open_violations(&state.pool, kb_id, limit, offset).await?@@ -1417,3 +1419,37 @@ pub async fn decide_alignment_kind_word(state.emit_graph(kb_id);Ok(Json(json!({ "ok": true })))}++#[derive(Deserialize)]+pub struct DecideErrataReq {+ pub approve: bool,+}++/// 人答勘误 agent 留下的一笔(0044 决定 7):批了就执行那个动作,否了只记一笔+pub async fn decide_errata(+ State(state): State<AppState>,+ AuthUser(user): AuthUser,+ Path((kb_id, action_id)): Path<(Uuid, Uuid)>,+ Json(req): Json<DecideErrataReq>,+) -> ApiResult<Json<serde_json::Value>> {+ require_kb(&state, &user, kb_id, Role::Editor).await?;+ let found =+ utopia_store::errata::decide_held(&state.pool, kb_id, action_id, req.approve, user.id)+ .await?;+ if !found {+ return Err(utopia_core::AppError::NotFound.into());+ }+ let _ = utopia_store::audit::record(+ &state.pool,+ Some(kb_id),+ user.id,+ "errata.decided",+ "errata_action",+ Some(action_id),+ json!({ "approve": req.approve }),+ )+ .await;+ state.emit_review(kb_id);+ state.emit_graph(kb_id);+ Ok(Json(json!({ "ok": true })))+}diff --git a/crates/utopia-server/src/api/review_routes_phrase_tests.rs b/crates/utopia-server/src/api/review_routes_phrase_tests.rsindex 32921828..6eee079d 100644--- a/crates/utopia-server/src/api/review_routes_phrase_tests.rs+++ b/crates/utopia-server/src/api/review_routes_phrase_tests.rs@@ -321,3 +321,81 @@ async fn a_rule_decision_is_accepted_with_its_job() -> anyhow::Result<()> {f.cleanup().await?;run}++/// 勘误队列(0044 决定 7):人批闸门留下的一笔,动作执行、答 200;答过的再答 404;别的库 404;Viewer 403+#[tokio::test]+async fn a_held_errata_action_is_decided_by_a_person() -> anyhow::Result<()> {+ let Some(f) = Fx::new().await? else {+ return Ok(());+ };+ let run = async {+ let (doc, chunk, typed_fact, run_id, action) = (+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ );+ let (subject, property): (Uuid, Uuid) = sqlx::query_as(+ "SELECT e.id, r.id FROM entities e, relation_types r+ WHERE e.kb_id=$1 AND e.canonical_name='Acme' AND r.kb_id=$1 AND r.key='based_in'",+ )+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ let object: Uuid =+ sqlx::query_scalar("SELECT id FROM entities WHERE kb_id=$1 AND canonical_name='London'")+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ sqlx::raw_sql(&format!(+ "INSERT INTO documents(id,kb_id,filename,sha256) VALUES ('{doc}','{}','acme.txt','x');+ INSERT INTO chunks(id,kb_id,document_id,seq,text) VALUES ('{chunk}','{}','{doc}',0,'Acme is based in London.');+ INSERT INTO facts(id,kb_id,subject_id,predicate_id,object_id,layer) VALUES+ ('{typed_fact}','{}','{subject}','{property}','{object}','typed');+ INSERT INTO errata_runs(id,kb_id,document_id) VALUES ('{run_id}','{}','{doc}');+ INSERT INTO errata_actions(id,kb_id,run_id,document_id,fact_id,predicate_id,action,reason,quote,proposed,status,detail)+ VALUES ('{action}','{}','{run_id}','{doc}','{typed_fact}','{property}','retract','wrong','Acme is based in London',+ '{{\"subject\":\"Acme\",\"property\":\"based_in\",\"object\":\"London\",\"subject_id\":\"{subject}\",\"predicate_id\":\"{property}\",\"object_id\":\"{object}\"}}',+ 'held','derived 1');",+ f.kb, f.kb, f.kb, f.kb, f.kb+ ))+ .execute(&f.pool)+ .await?;+ let path = format!("/api/v1/kbs/{}/review/errata/{}", f.kb, action);+ let (status, _) = f+ .call(&f.viewer, "POST", &path, Some(json!({ "approve": true })))+ .await?;+ assert_eq!(status, StatusCode::FORBIDDEN);+ let other = format!("/api/v1/kbs/{}/review/errata/{}", f.other_kb, action);+ let (status, _) = f+ .call(&f.editor, "POST", &other, Some(json!({ "approve": true })))+ .await?;+ assert_eq!(status, StatusCode::NOT_FOUND);+ // 队列里有它+ let (status, body) = f+ .call(&f.editor, "GET", &format!("/api/v1/kbs/{}/review?queue=errata&limit=10&offset=0", f.kb), None)+ .await?;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["counts"]["errata"], 1);+ assert_eq!(body["items"][0]["proposed"]["object"], "London");+ assert_eq!(body["items"][0]["detail"], "derived 1");+ let (status, body) = f+ .call(&f.editor, "POST", &path, Some(json!({ "approve": true })))+ .await?;+ assert_eq!(status, StatusCode::OK, "{body}");+ let live: bool = sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id=$1")+ .bind(typed_fact)+ .fetch_one(&f.pool)+ .await?;+ assert!(!live, "approving a held retraction retracts");+ let (status, _) = f+ .call(&f.editor, "POST", &path, Some(json!({ "approve": false })))+ .await?;+ assert_eq!(status, StatusCode::NOT_FOUND, "answered once");+ anyhow::Ok(())+ }+ .await;+ f.cleanup().await?;+ run+}diff --git a/crates/utopia-server/src/errata.rs b/crates/utopia-server/src/errata.rsnew file mode 100644index 00000000..01dd689e--- /dev/null+++ b/crates/utopia-server/src/errata.rs@@ -0,0 +1,294 @@+//! 勘误 agent(0044 决定 7,第六刀):抽取之后按文档复审类型化图谱。+//!+//! 一份文档一次:结构报了的事实先送去看,其余抽样;一次请求最多 [`FACTS_PER_REQUEST`] 条,+//! 一份文档最多 [`REQUESTS_PER_DOCUMENT`] 次——这就是「按文档的预算」。模型走 JSON 动作+//! 协议(`utopia_extract::errata`),每一笔在库里记成动作(`utopia_store::errata`),撤、改、+//! 加带着文档的原话;会牵动图外东西的留给人(0027 的闸门)。+//!+//! 度量记在 `errata_runs` 上:看了几条、问了几次、端点报的 token。精度换了多少、撤错了多少,+//! 由 typed 基准拿这些数与评判比出来。++use std::collections::HashMap;+use utopia_extract::errata::{+ build_errata_messages, parse_errata_response, ErrataFact, ErrataProperty, Verdict,+};+use utopia_store::errata::{self, ActionInput, Candidate, Proposed};+use uuid::Uuid;++use crate::extraction::chat_retrying_rate_limits_at;+use crate::llm_util;+use crate::state::AppState;++/// 一次任务最多看几份文档;还有剩的再排一次+pub const DOCS_PER_JOB: i64 = 20;+/// 一份文档最多送去看几条:结构报的先占,抽样的补到这个数+pub const FACTS_PER_DOCUMENT: usize = 40;+/// 抽样最多几条(没报的)+pub const SAMPLE: usize = 10;+/// 一次请求最多几条+pub const FACTS_PER_REQUEST: usize = 20;+/// 一份文档最多问几次+pub const REQUESTS_PER_DOCUMENT: usize = 2;+/// 正文最多带多少字;再长的文档截断,截掉的部分这一轮看不到+pub const DOC_CHARS: usize = 16_000;++/// 一份文档看完的账+#[derive(Debug, Default, Clone, PartialEq, Eq)]+pub struct DocumentOutcome {+ pub reviewed: usize,+ pub applied: usize,+ pub held: usize,+ pub refused: usize,+ pub requests: usize,+}++/// `errata_review` 任务:有类型化行还没看过的文档,一份一份看;看不完再排一次+pub async fn review(state: &AppState, kb_id: Uuid) -> anyhow::Result<()> {+ let pool = &state.pool;+ let due = errata::documents_due(pool, kb_id, DOCS_PER_JOB).await?;+ if due.is_empty() {+ tracing::info!(%kb_id, "没有待勘误的文档");+ return Ok(());+ }+ let kb = utopia_store::kbs::get(pool, kb_id).await?;+ let settings = utopia_store::settings::get(pool, kb.workspace_id)+ .await?+ .ok_or_else(|| anyhow::anyhow!("Chat model not configured; cannot review errata"))?;+ let client = llm_util::chat_client(&settings)+ .ok_or_else(|| anyhow::anyhow!("Chat model not configured; cannot review errata"))?;+ let props = utopia_store::ontology::relation_type_views(pool, kb_id).await?;+ let classes = utopia_store::graph::entity_types(pool, kb_id).await?;+ let class_key: HashMap<Uuid, &str> = classes.iter().map(|c| (c.id, c.key.as_str())).collect();+ let glossary: Vec<ErrataProperty<'_>> = props+ .iter()+ .map(|p| ErrataProperty {+ key: &p.key,+ label: &p.label,+ description: &p.description,+ kind: &p.kind,+ domains: p+ .domains+ .iter()+ .filter_map(|d| class_key.get(d).copied())+ .collect(),+ ranges: p+ .ranges+ .iter()+ .filter_map(|r| class_key.get(r).copied())+ .collect(),+ datatype: p.datatype.as_deref(),+ })+ .collect();+ let mut failed = 0usize;+ let (mut applied, mut held) = (0usize, 0usize);+ for document_id in &due {+ match review_document(state, &settings, &client, kb_id, *document_id, &glossary).await {+ Ok(o) => {+ applied += o.applied;+ held += o.held;+ }+ Err(e) => {+ tracing::warn!(%kb_id, %document_id, error = %e, "这份文档的勘误没跑完,任务重试时再来");+ failed += 1;+ }+ }+ }+ if applied > 0 || held > 0 {+ let _ = utopia_store::audit::record(+ pool,+ Some(kb_id),+ Uuid::nil(),+ "errata.reviewed",+ "knowledge_base",+ Some(kb_id),+ serde_json::json!({ "documents": due.len(), "applied": applied, "held": held }),+ )+ .await;+ state.emit_review(kb_id);+ state.emit_graph(kb_id);+ }+ if failed > 0 {+ anyhow::bail!("{failed} documents failed errata review; the job retries");+ }+ if !errata::documents_due(pool, kb_id, 1).await?.is_empty() {+ utopia_store::jobs::enqueue_unless_queued(+ pool,+ errata::JOB_KIND,+ serde_json::json!({ "kb_id": kb_id }),+ )+ .await?;+ }+ Ok(())+}++/// 一份文档:挑要看的,按预算问,每一票记一笔+pub async fn review_document(+ state: &AppState,+ settings: &utopia_core::models::LlmSettings,+ client: &utopia_llm::LlmClient,+ kb_id: Uuid,+ document_id: Uuid,+ glossary: &[ErrataProperty<'_>],+) -> anyhow::Result<DocumentOutcome> {+ let pool = &state.pool;+ let all = errata::candidates(pool, kb_id, document_id).await?;+ let chosen = choose(&all);+ let mut outcome = DocumentOutcome::default();+ if chosen.is_empty() {+ return Ok(outcome);+ }+ let flagged = chosen.iter().filter(|c| c.flag.is_some()).count();+ let run = errata::start_run(+ pool,+ kb_id,+ document_id,+ flagged as i32,+ (chosen.len() - flagged) as i32,+ )+ .await?;+ let text = errata::document_text(pool, document_id).await?;+ let document = truncate(&text, DOC_CHARS);+ let (mut prompt_tokens, mut completion_tokens, mut saw_usage) = (0u64, 0u64, false);+ let mut error = None;+ for batch in chosen.chunks(FACTS_PER_REQUEST).take(REQUESTS_PER_DOCUMENT) {+ let items: Vec<ErrataFact<'_>> = batch+ .iter()+ .enumerate()+ .map(|(i, c)| ErrataFact {+ id: i as i64,+ subject: &c.subject,+ subject_class: c.subject_class.as_deref(),+ property: &c.property,+ object: &c.object,+ object_class: c.object_class.as_deref(),+ flag: c.flag.as_deref(),+ quote: c.quote.as_deref(),+ })+ .collect();+ let messages = build_errata_messages(document, glossary, &items);+ outcome.requests += 1;+ let reply =+ match chat_retrying_rate_limits_at(state, settings, client, &messages, Some(0.0)).await+ {+ Ok(r) => r,+ Err(e) => {+ error = Some(e);+ break;+ }+ };+ if let Some(u) = reply.usage {+ saw_usage = true;+ prompt_tokens += u.prompt_tokens;+ completion_tokens += u.completion_tokens;+ }+ let parsed = match parse_errata_response(&reply.text, &items) {+ Ok(p) => p,+ Err(e) => {+ error = Some(anyhow::anyhow!("errata reply unreadable: {e}"));+ break;+ }+ };+ if parsed.malformed > 0 || parsed.additions_refused > 0 {+ tracing::info!(%kb_id, %document_id, malformed = parsed.malformed,+ additions_refused = parsed.additions_refused, "勘误回复里有坏项");+ }+ for v in &parsed.verdicts {+ let Some(c) = usize::try_from(v.id).ok().and_then(|i| batch.get(i)) else {+ continue;+ };+ let proposed = match &v.verdict {+ Verdict::Keep => Proposed::Keep,+ Verdict::Retract => Proposed::Retract,+ Verdict::Revise { property, object } => Proposed::Revise {+ property: property.clone(),+ object: object.clone(),+ },+ };+ let r = errata::record(+ pool,+ kb_id,+ ActionInput {+ run_id: run,+ document_id,+ candidate: Some(c),+ proposed,+ reason: &v.reason,+ quote: v.quote.as_deref(),+ document_text: &text,+ },+ )+ .await?;+ outcome.reviewed += 1;+ count(&mut outcome, r.status, &v.verdict);+ }+ for a in &parsed.additions {+ let r = errata::record(+ pool,+ kb_id,+ ActionInput {+ run_id: run,+ document_id,+ candidate: None,+ proposed: Proposed::Add {+ subject: a.subject.clone(),+ property: a.property.clone(),+ object: a.object.clone(),+ },+ reason: &a.reason,+ quote: Some(&a.quote),+ document_text: &text,+ },+ )+ .await?;+ count(&mut outcome, r.status, &Verdict::Retract);+ }+ }+ errata::finish_run(+ pool,+ run,+ outcome.requests as i32,+ saw_usage.then_some(prompt_tokens as i64),+ saw_usage.then_some(completion_tokens as i64),+ )+ .await?;+ tracing::info!(%kb_id, %document_id, reviewed = outcome.reviewed, applied = outcome.applied,+ held = outcome.held, refused = outcome.refused, requests = outcome.requests, "勘误看完一份文档");+ match error {+ Some(e) => Err(e),+ None => Ok(outcome),+ }+}++/// keep 落地不算「动了图」;撤改加落地算+fn count(o: &mut DocumentOutcome, status: &str, verdict: &Verdict) {+ match status {+ "applied" if *verdict != Verdict::Keep => o.applied += 1,+ "held" => o.held += 1,+ "refused" => o.refused += 1,+ _ => {}+ }+}++/// 结构报了的全要(到上限为止),没报的抽前几条补上+fn choose(all: &[Candidate]) -> Vec<Candidate> {+ let mut out: Vec<Candidate> = all+ .iter()+ .filter(|c| c.flag.is_some())+ .take(FACTS_PER_DOCUMENT)+ .cloned()+ .collect();+ let room = FACTS_PER_DOCUMENT.saturating_sub(out.len()).min(SAMPLE);+ out.extend(all.iter().filter(|c| c.flag.is_none()).take(room).cloned());+ out+}++fn truncate(s: &str, max_chars: usize) -> &str {+ match s.char_indices().nth(max_chars) {+ Some((i, _)) => &s[..i],+ None => s,+ }+}++#[cfg(test)]+#[path = "errata_tests.rs"]+mod tests;diff --git a/crates/utopia-server/src/errata_tests.rs b/crates/utopia-server/src/errata_tests.rsnew file mode 100644index 00000000..f8c78376--- /dev/null+++ b/crates/utopia-server/src/errata_tests.rs@@ -0,0 +1,273 @@+//! 勘误 agent 走脚本化的模型端点:结构报了的先送去看,撤落地、keep 记账、引文不是原话的加+//! 被拒;账上记着请求数与端点报的 token;看完的文档不再问。没有 `UTOPIA_DATABASE_URL` 时跳过。+use super::*;+use axum::{extract::State, response::IntoResponse, routing::post, Json, Router};+use serde_json::{json, Value};+use std::sync::{Arc, Mutex};++#[derive(Clone)]+struct Model {+ replies: Arc<Mutex<Vec<Value>>>,+ requests: Arc<Mutex<Vec<Value>>>,+}+/// 回一段流,最后一帧带用量——和 OpenAI 协议的 `stream_options.include_usage` 一样+async fn reply(State(m): State<Model>, Json(body): Json<Value>) -> impl IntoResponse {+ m.requests.lock().unwrap().push(body);+ let text = {+ let mut replies = m.replies.lock().unwrap();+ if replies.is_empty() {+ panic!("unexpected model request");+ }+ replies.remove(0).to_string()+ };+ let frame = json!({"choices":[{"delta":{"content":text}}]});+ let done = json!({"choices":[{"delta":{},"finish_reason":"stop"}]});+ let usage = json!({"choices":[],"usage":{"prompt_tokens":123,"completion_tokens":45}});+ (+ [("content-type", "text/event-stream")],+ format!("data: {frame}\n\ndata: {done}\n\ndata: {usage}\n\ndata: [DONE]\n\n"),+ )+}++struct Fx {+ pool: sqlx::PgPool,+ state: AppState,+ org: Uuid,+ kb: Uuid,+ doc: Uuid,+ fine: Uuid,+ absent: Uuid,+ model: Model,+ _server: tokio::task::JoinHandle<()>,+ _dir: tempfile::TempDir,+}++impl Fx {+ /// 一个库:organization / place / person;based_in、ceo;文档「Acme is based in London. Jane Roe runs Acme.」;+ /// 两条类型化行:Acme —based_in→ London(对)、Acme —based_in→ Paris(Paris 不在文档里)+ async fn new(replies: Vec<Value>) -> anyhow::Result<Option<Self>> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(None);+ };+ let pool = sqlx::PgPool::connect(&url).await?;+ utopia_store::db::migrate(&pool).await?;+ let ids: Vec<Uuid> = (0..18).map(|_| Uuid::now_v7()).collect();+ let (org, ws, kb, doc, chunk, organization, place, person, based_in, ceo) = (+ ids[0], ids[1], ids[2], ids[3], ids[4], ids[5], ids[6], ids[7], ids[8], ids[9],+ );+ let (acme, london, paris, jane, s1, t1, s2, t2) = (+ ids[10], ids[11], ids[12], ids[13], ids[14], ids[15], ids[16], ids[17],+ );+ sqlx::raw_sql(&format!(+ "INSERT INTO organizations(id,name) VALUES ('{org}','errata-server');+ INSERT INTO workspaces(id,org_id,name) VALUES ('{ws}','{org}','errata-server');+ INSERT INTO knowledge_bases(id,workspace_id,name) VALUES ('{kb}','{ws}','errata-server');+ INSERT INTO documents(id,kb_id,filename,sha256) VALUES ('{doc}','{kb}','acme.txt','x');+ INSERT INTO chunks(id,kb_id,document_id,seq,text) VALUES+ ('{chunk}','{kb}','{doc}',0,'Acme is based in London. Jane Roe runs Acme.');+ INSERT INTO entity_types(id,kb_id,key,label,color,shape) VALUES+ ('{organization}','{kb}','organization','Organization','#000','circle'),+ ('{place}','{kb}','place','Place','#000','circle'),+ ('{person}','{kb}','person','Person','#000','circle');+ INSERT INTO relation_types(id,kb_id,key,label,kind,temporal,functional,description) VALUES+ ('{based_in}','{kb}','based_in','based in','relation','state',false,'where an organization is based'),+ ('{ceo}','{kb}','ceo','chief executive','relation','state',true,'who runs it');+ INSERT INTO relation_type_domains(relation_type_id,entity_type_id) VALUES+ ('{based_in}','{organization}'), ('{ceo}','{organization}');+ INSERT INTO relation_type_ranges(relation_type_id,entity_type_id) VALUES+ ('{based_in}','{place}'), ('{ceo}','{person}');+ INSERT INTO entities(id,kb_id,canonical_name,type_id) VALUES+ ('{acme}','{kb}','Acme','{organization}'), ('{london}','{kb}','London','{place}'),+ ('{paris}','{kb}','Paris','{place}'), ('{jane}','{kb}','Jane Roe','{person}');+ INSERT INTO facts(id,kb_id,subject_id,object_id,layer,phrase) VALUES+ ('{s1}','{kb}','{acme}','{london}','open','based in'),+ ('{s2}','{kb}','{acme}','{paris}','open','based in');+ INSERT INTO facts(id,kb_id,subject_id,predicate_id,object_id,layer,from_statement_id) VALUES+ ('{t1}','{kb}','{acme}','{based_in}','{london}','typed','{s1}'),+ ('{t2}','{kb}','{acme}','{based_in}','{paris}','typed','{s2}');+ INSERT INTO typed_fact_sources(fact_id,statement_id) VALUES ('{t1}','{s1}'), ('{t2}','{s2}');+ INSERT INTO fact_evidence(fact_id,chunk_id,quote,document_id,doc_version) VALUES+ ('{s1}','{chunk}','based in London','{doc}',1), ('{t1}','{chunk}','based in London','{doc}',1),+ ('{s2}','{chunk}','based in','{doc}',1), ('{t2}','{chunk}','based in','{doc}',1);"+ ))+ .execute(&pool)+ .await?;+ let model = Model {+ replies: Arc::new(Mutex::new(replies)),+ requests: Arc::new(Mutex::new(Vec::new())),+ };+ 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());+ let server = tokio::spawn(async move { axum::serve(listener, router).await.unwrap() });+ utopia_store::settings::upsert(+ &pool,+ ws,+ Some(&endpoint),+ None,+ Some("scripted"),+ None,+ None,+ None,+ None,+ )+ .await?;+ let dir = tempfile::tempdir()?;+ let cfg = 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(), &cfg, search, "test-only".into());+ Ok(Some(Self {+ pool,+ state,+ org,+ kb,+ doc,+ fine: t1,+ absent: t2,+ model,+ _server: server,+ _dir: dir,+ }))+ }+ async fn cleanup(&self) -> anyhow::Result<()> {+ sqlx::query("DELETE FROM jobs WHERE payload->>'kb_id'=$1")+ .bind(self.kb.to_string())+ .execute(&self.pool)+ .await?;+ sqlx::query("DELETE FROM organizations WHERE id=$1")+ .bind(self.org)+ .execute(&self.pool)+ .await?;+ Ok(())+ }+ fn prompt_of(&self, n: usize) -> String {+ self.model.requests.lock().unwrap()[n]["messages"][1]["content"]+ .as_str()+ .unwrap_or("")+ .to_string()+ }+ async fn live(&self, fact: Uuid) -> anyhow::Result<bool> {+ Ok(+ sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id=$1")+ .bind(fact)+ .fetch_one(&self.pool)+ .await?,+ )+ }+}++#[tokio::test]+async fn the_agent_reviews_flagged_facts_first_and_each_verdict_is_a_recorded_action(+) -> anyhow::Result<()> {+ // 0 是报了的 Paris 行(在前),1 是对的 London 行;加一条 ceo(原话在),再加一条原话不在的+ let script = json!({"a":[+ [0,"retract","Paris is not in the document","Acme is based in London"],+ [1,"keep"],+ [null,"add",{"subject":"Acme","property":"ceo","object":"Jane Roe"},"stated","Jane Roe runs Acme"],+ [null,"add",{"subject":"Acme","property":"ceo","object":"Jane Roe"},"made up","Jane Roe owns Acme"]+ ]});+ let Some(f) = Fx::new(vec![script]).await? else {+ return Ok(());+ };+ let run = async {+ review(&f.state, f.kb).await?;+ let prompt = f.prompt_of(0);+ assert!(prompt.contains("DOCUMENT:\nAcme is based in London. Jane Roe runs Acme."), "{prompt}");+ assert!(prompt.contains("0: Acme (organization) —based_in→ Paris (place) FLAG name_absent"), "{prompt}");+ assert!(prompt.contains("1: Acme (organization) —based_in→ London (place)"), "{prompt}");+ assert!(prompt.contains("- ceo (chief executive): who runs it [thing] subject: organization object: person"), "{prompt}");+ assert!(!f.live(f.absent).await?, "the retraction landed");+ assert!(f.live(f.fine).await?);+ let actions: Vec<(String, String, Option<String>)> = sqlx::query_as(+ "SELECT action, status, detail FROM errata_actions WHERE kb_id=$1 ORDER BY created_at, id",+ )+ .bind(f.kb)+ .fetch_all(&f.pool)+ .await?;+ assert_eq!(+ actions,+ vec![+ ("retract".into(), "applied".into(), None),+ ("keep".into(), "applied".into(), None),+ ("add".into(), "applied".into(), None),+ ("add".into(), "refused".into(), Some("the quote is not in the document".into())),+ ]+ );+ let ceo: i64 = sqlx::query_scalar(+ "SELECT count(*) FROM facts f JOIN relation_types r ON r.id = f.predicate_id+ WHERE f.kb_id=$1 AND r.key='ceo' AND f.invalidated_at IS NULL",+ )+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ assert_eq!(ceo, 1, "one Jane Roe row: the refused add wrote nothing");+ // 账:一次请求,端点报的用量+ let run_row: (i32, i32, i32, Option<i64>, Option<i64>, bool) = sqlx::query_as(+ "SELECT flagged, sampled, requests, prompt_tokens, completion_tokens, finished_at IS NOT NULL+ FROM errata_runs WHERE kb_id=$1",+ )+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ assert_eq!(run_row, (1, 1, 1, Some(123), Some(45), true));+ // 看完了:再跑不问模型(脚本空了,问了就 panic),也不排下一次+ review(&f.state, f.kb).await?;+ assert_eq!(f.model.requests.lock().unwrap().len(), 1);+ let queued: i64 = sqlx::query_scalar(+ "SELECT count(*) FROM jobs WHERE kind=$1 AND payload->>'kb_id'=$2",+ )+ .bind(utopia_store::errata::JOB_KIND)+ .bind(f.kb.to_string())+ .fetch_one(&f.pool)+ .await?;+ assert_eq!(queued, 0);+ let _ = f.doc;+ anyhow::Ok(())+ }+ .await;+ f.cleanup().await?;+ run+}++#[tokio::test]+async fn an_unreadable_reply_leaves_the_facts_unreviewed_and_the_job_retries() -> anyhow::Result<()>+{+ let Some(f) = Fx::new(vec![json!("not the protocol")]).await? else {+ return Ok(());+ };+ let run = async {+ let err = review(&f.state, f.kb)+ .await+ .expect_err("the job reports the failure");+ assert!(err.to_string().contains("failed errata review"), "{err}");+ let actions: i64 = sqlx::query_scalar("SELECT count(*) FROM errata_actions WHERE kb_id=$1")+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ assert_eq!(actions, 0);+ assert!(f.live(f.absent).await? && f.live(f.fine).await?);+ // 账还是记的:问了一次,没看成+ let run_row: (i32, bool) = sqlx::query_as(+ "SELECT requests, finished_at IS NOT NULL FROM errata_runs WHERE kb_id=$1",+ )+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ assert_eq!(run_row, (1, true));+ assert_eq!(+ utopia_store::errata::documents_due(&f.pool, f.kb, 10).await?,+ vec![f.doc]+ );+ anyhow::Ok(())+ }+ .await;+ f.cleanup().await?;+ run+}diff --git a/crates/utopia-server/src/main.rs b/crates/utopia-server/src/main.rsindex 7e5a1cac..fc29957c 100644--- a/crates/utopia-server/src/main.rs+++ b/crates/utopia-server/src/main.rs@@ -6,6 +6,7 @@ mod blob;mod bootstrap_ontology;mod client_ctx;mod docs_corpus;+mod errata;mod error;mod extraction;mod extraction_open;@@ -529,6 +530,15 @@ async fn dispatch(st: &state::AppState, job: &utopia_store::jobs::Job) -> anyhow.await;st.emit_graph(kb_id);}+ // 有新行才值得勘误看一眼;没新行的重算不排+ if typed.added > 0 {+ utopia_store::jobs::enqueue_unless_queued(+ &st.pool,+ utopia_store::errata::JOB_KIND,+ serde_json::json!({ "kb_id": kb_id }),+ )+ .await?;+ }// 队列卡片按绑定的状态显示,重算完了才算这条判定「落地」st.emit_review(kb_id);Ok(())@@ -548,6 +558,16 @@ async fn dispatch(st: &state::AppState, job: &utopia_store::jobs::Job) -> anyhow.ok_or_else(|| anyhow::anyhow!("payload 缺少 kb_id"))?;implication::read_phrases(st, kb_id).await}+ // 勘误 agent(0044 决定 7):物化出了新行的文档,按文档复审类型化图谱+ utopia_store::errata::JOB_KIND => {+ 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"))?;+ errata::review(st, kb_id).await+ }"align_types" => {let kb_id: Uuid = job.payloaddiff --git a/crates/utopia-store/src/errata.rs b/crates/utopia-store/src/errata.rsnew file mode 100644index 00000000..f211767b--- /dev/null+++ b/crates/utopia-store/src/errata.rs@@ -0,0 +1,784 @@+//! 勘误(0044 决定 7,第六刀):抽取之后按文档复审类型化图谱的那个 agent 在库里留下的东西。+//!+//! 结构先报([`candidates`]):主语或宾语在属性声明的类之外、文档里找不到的名字、日期属性+//! 没有日期。这几条不问模型就报得出,先看它们,其余抽样——0044 §7 的次序。+//!+//! 每一条看过的事实记一笔(keep 也记):「没看过的」就是没行的,复审不重复;撤、改、加+//! 记成动作,带着文档的原话。动作走 0027 的闸门:撤的事实有派生靠着、主语在回答里被认+//! 过,或者写的事实会让一个只许一个值的谓词有两个值——留给人,agent 不动手。+//!+//! 撤销要站得住:物化下一轮会把活着的陈述再算成同一条类型化行。所以动作记着(陈述, 属性),+//! 物化与蕴含都跳过被勘误撤过的那一对(见 `materialize`)。别的文档说了同一件事照样算——+//! 勘误看的是这份文档,撤的是这份文档产生的那条。++use chrono::{DateTime, Utc};+use serde::Serialize;+use serde_json::{json, Value};+use sqlx::PgPool;+use utopia_core::{AppError, AppResult};+use uuid::Uuid;++use crate::execution_gate::{self, Impact};+use crate::graph::{FactObject, Validity};++/// 排队的任务名:一个库一份,复审有类型化行还没看过的文档+pub const JOB_KIND: &str = "errata_review";++/// 结构报出来的四种理由,与迁移里的 CHECK 同一份+pub const FLAGS: &[&str] = &["domain", "range", "name_absent", "no_date"];++/// 一条送去看的类型化事实:两端的名字与类、属性、来源陈述、结构报的理由、这份文档里的引文+#[derive(Debug, Clone, Serialize, sqlx::FromRow)]+pub struct Candidate {+ pub fact_id: Uuid,+ pub statement_id: Option<Uuid>,+ pub predicate_id: Uuid,+ pub subject_id: Uuid,+ pub subject: String,+ pub subject_class: Option<String>,+ pub property: String,+ pub property_label: String,+ pub object_id: Option<Uuid>,+ pub object: String,+ pub object_class: Option<String>,+ /// 空 = 结构没报,抽样看+ pub flag: Option<String>,+ pub quote: Option<String>,+}++/// 类的祖先闭包在 SQL 里算:属性声明在 legal_entity 上,organization 是它的子类就在域内。+/// 没有类的一端不报——「不知道是什么」不是「在类之外」+const CANDIDATES_SQL: &str = r#"+WITH RECURSIVE up AS (+ SELECT id AS child, id AS anc FROM entity_types WHERE kb_id = $1+ UNION+ SELECT up.child, p.parent_id FROM up JOIN entity_type_parents p ON p.child_id = up.anc+),+closure AS (SELECT child, array_agg(anc) AS ancs FROM up GROUP BY child),+doc AS (+ SELECT lower(string_agg(text, ' ' ORDER BY seq)) AS text+ FROM chunks WHERE document_id = $2 AND superseded_at IS NULL+),+sources AS (+ SELECT src.fact_id, src.statement_id FROM typed_fact_sources src+ JOIN fact_evidence fe ON fe.fact_id = src.statement_id AND fe.document_id = $2+ UNION+ SELECT i.fact_id, i.statement_id FROM implied_fact_sources i+ JOIN fact_evidence fe ON fe.fact_id = i.statement_id AND fe.document_id = $2+ WHERE i.statement_id IS NOT NULL+),+typed AS (SELECT DISTINCT ON (fact_id) fact_id, statement_id FROM sources ORDER BY fact_id, statement_id)+SELECT * FROM (+SELECT t.id AS fact_id, ty.statement_id, t.predicate_id, t.subject_id, t.recorded_at,+ s.canonical_name AS subject, st.key AS subject_class,+ r.key AS property, r.label AS property_label,+ t.object_id,+ COALESCE(o.canonical_name, t.object_value #>> '{value}', t.object_value::text, '') AS object,+ ot.key AS object_class,+ CASE+ WHEN s.type_id IS NOT NULL+ AND EXISTS (SELECT 1 FROM relation_type_domains d WHERE d.relation_type_id = r.id)+ AND NOT EXISTS (SELECT 1 FROM relation_type_domains d+ WHERE d.relation_type_id = r.id AND d.entity_type_id = ANY(sc.ancs))+ THEN 'domain'+ WHEN o.type_id IS NOT NULL+ AND EXISTS (SELECT 1 FROM relation_type_ranges g WHERE g.relation_type_id = r.id)+ AND NOT EXISTS (SELECT 1 FROM relation_type_ranges g+ WHERE g.relation_type_id = r.id AND g.entity_type_id = ANY(oc.ancs))+ THEN 'range'+ WHEN r.datatype = 'date' AND t.object_id IS NULL+ AND NOT COALESCE((t.object_value #>> '{value}') ~ '^\d{4}(-\d{2}(-\d{2})?)?', false)+ THEN 'no_date'+ WHEN position(lower(s.canonical_name) IN doc.text) = 0+ OR (o.id IS NOT NULL AND position(lower(o.canonical_name) IN doc.text) = 0)+ THEN 'name_absent'+ END AS flag,+ (SELECT fe.quote FROM fact_evidence fe+ WHERE fe.fact_id = t.id AND fe.document_id = $2 AND fe.quote IS NOT NULL+ ORDER BY fe.chunk_id LIMIT 1) AS quote+ FROM typed ty+ JOIN facts t ON t.id = ty.fact_id+ JOIN entities s ON s.id = t.subject_id+ LEFT JOIN entity_types st ON st.id = s.type_id+ LEFT JOIN closure sc ON sc.child = s.type_id+ JOIN relation_types r ON r.id = t.predicate_id+ LEFT JOIN entities o ON o.id = t.object_id+ LEFT JOIN entity_types ot ON ot.id = o.type_id+ LEFT JOIN closure oc ON oc.child = o.type_id+ CROSS JOIN doc+ WHERE t.kb_id = $1 AND t.layer = 'typed' AND t.invalidated_at IS NULL+ AND NOT EXISTS (SELECT 1 FROM errata_actions ea WHERE ea.fact_id = t.id)+) c+ORDER BY (c.flag IS NULL), c.recorded_at, c.fact_id+"#;++/// 这份文档产生的、活着的、还没看过的类型化事实,结构报了的在前+pub async fn candidates(+ pool: &PgPool,+ kb_id: Uuid,+ document_id: Uuid,+) -> AppResult<Vec<Candidate>> {+ Ok(sqlx::query_as(CANDIDATES_SQL)+ .bind(kb_id)+ .bind(document_id)+ .fetch_all(pool)+ .await?)+}++/// 文档当前版本的正文,按分块顺序接起来+pub async fn document_text(pool: &PgPool, document_id: Uuid) -> AppResult<String> {+ let text: Option<String> = sqlx::query_scalar(+ "SELECT string_agg(text, ' ' ORDER BY seq) FROM chunks+ WHERE document_id = $1 AND superseded_at IS NULL",+ )+ .bind(document_id)+ .fetch_one(pool)+ .await?;+ Ok(text.unwrap_or_default())+}++/// 还有活着的类型化行没看过的文档,最早摄入的在前+pub async fn documents_due(pool: &PgPool, kb_id: Uuid, limit: i64) -> AppResult<Vec<Uuid>> {+ Ok(sqlx::query_scalar(+ "SELECT d.id FROM documents d+ WHERE d.kb_id = $1 AND d.deleted_at IS NULL+ AND EXISTS (+ SELECT 1 FROM fact_evidence fe+ JOIN (SELECT fact_id, statement_id FROM typed_fact_sources+ UNION ALL+ SELECT fact_id, statement_id FROM implied_fact_sources+ WHERE statement_id IS NOT NULL) src ON src.statement_id = fe.fact_id+ JOIN facts t ON t.id = src.fact_id+ WHERE fe.document_id = d.id AND t.invalidated_at IS NULL+ AND NOT EXISTS (SELECT 1 FROM errata_actions ea WHERE ea.fact_id = t.id))+ ORDER BY d.created_at, d.id+ LIMIT $2",+ )+ .bind(kb_id)+ .bind(limit)+ .fetch_all(pool)+ .await?)+}++/// 一次复审开账+pub async fn start_run(+ pool: &PgPool,+ kb_id: Uuid,+ document_id: Uuid,+ flagged: i32,+ sampled: i32,+) -> AppResult<Uuid> {+ let id = Uuid::now_v7();+ sqlx::query(+ "INSERT INTO errata_runs (id, kb_id, document_id, flagged, sampled) VALUES ($1, $2, $3, $4, $5)",+ )+ .bind(id)+ .bind(kb_id)+ .bind(document_id)+ .bind(flagged)+ .bind(sampled)+ .execute(pool)+ .await?;+ Ok(id)+}++/// 结账:问了几次、端点报了多少用量(没报就空着)+pub async fn finish_run(+ pool: &PgPool,+ run_id: Uuid,+ requests: i32,+ prompt_tokens: Option<i64>,+ completion_tokens: Option<i64>,+) -> AppResult<()> {+ sqlx::query(+ "UPDATE errata_runs SET requests = $2, prompt_tokens = $3, completion_tokens = $4,+ finished_at = now() WHERE id = $1",+ )+ .bind(run_id)+ .bind(requests)+ .bind(prompt_tokens)+ .bind(completion_tokens)+ .execute(pool)+ .await?;+ Ok(())+}++/// agent 对一条事实的说法,或它想加的一条+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum Proposed {+ Keep,+ Retract,+ /// 换属性(键)、换宾语(名字或值),至少一样+ Revise {+ property: Option<String>,+ object: Option<String>,+ },+ Add {+ subject: String,+ property: String,+ object: String,+ },+}++impl Proposed {+ pub fn action(&self) -> &'static str {+ match self {+ Proposed::Keep => "keep",+ Proposed::Retract => "retract",+ Proposed::Revise { .. } => "revise",+ Proposed::Add { .. } => "add",+ }+ }+}++pub struct ActionInput<'a> {+ pub run_id: Uuid,+ pub document_id: Uuid,+ /// keep / retract / revise 看的那条;add 没有+ pub candidate: Option<&'a Candidate>,+ pub proposed: Proposed,+ pub reason: &'a str,+ pub quote: Option<&'a str>,+ /// 文档正文,引文按它验+ pub document_text: &'a str,+}++/// 一笔记下来的动作:落了地、留给人了、还是没过验证+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]+pub struct Recorded {+ pub id: Uuid,+ pub status: &'static str,+ /// 留给人的理由,或没过验证的理由+ pub detail: Option<String>,+ pub new_fact_id: Option<Uuid>,+}++/// 引文是不是文档的原话:折掉大小写与空白后的连续一段。空引文不算+pub fn quote_in(document: &str, quote: &str) -> bool {+ let q = squash(quote);+ !q.is_empty() && squash(document).contains(&q)+}++fn squash(s: &str) -> String {+ s.split_whitespace()+ .collect::<Vec<_>>()+ .join(" ")+ .to_lowercase()+}++/// 动作指向的那条事实:名字给队列卡片看,id 给执行用+struct Target {+ subject_id: Uuid,+ predicate_id: Uuid,+ object_id: Option<Uuid>,+ object_value: Option<Value>,+ subject: String,+ property: String,+ object: String,+}++impl Target {+ fn json(&self) -> Value {+ json!({+ "subject": self.subject, "property": self.property, "object": self.object,+ "subject_id": self.subject_id, "predicate_id": self.predicate_id,+ "object_id": self.object_id, "object_value": self.object_value,+ })+ }+ fn from_json(v: &Value) -> Option<Self> {+ Some(Self {+ subject_id: v.get("subject_id")?.as_str()?.parse().ok()?,+ predicate_id: v.get("predicate_id")?.as_str()?.parse().ok()?,+ object_id: v+ .get("object_id")+ .and_then(|x| x.as_str())+ .and_then(|s| s.parse().ok()),+ object_value: v.get("object_value").filter(|x| !x.is_null()).cloned(),+ subject: v["subject"].as_str().unwrap_or("").to_string(),+ property: v["property"].as_str().unwrap_or("").to_string(),+ object: v["object"].as_str().unwrap_or("").to_string(),+ })+ }+}++/// 属性键在这个库里对应的行:id、是关系还是属性+async fn property_by_key(+ pool: &PgPool,+ kb_id: Uuid,+ key: &str,+) -> AppResult<Option<(Uuid, String)>> {+ Ok(+ sqlx::query_as("SELECT id, kind FROM relation_types WHERE kb_id = $1 AND key = $2")+ .bind(kb_id)+ .bind(key.trim())+ .fetch_optional(pool)+ .await?,+ )+}++/// 宾语的文本按属性的种类解:关系要库里已有的名字(勘误不造东西),属性落成值+async fn object_of(+ pool: &PgPool,+ kb_id: Uuid,+ kind: &str,+ text: &str,+) -> AppResult<Result<(Option<Uuid>, Option<Value>), String>> {+ let text = text.trim();+ if text.is_empty() {+ return Ok(Err("empty object".into()));+ }+ if kind == "relation" {+ match crate::resolution::existing_by_name(pool, kb_id, text).await? {+ Some(id) => Ok(Ok((Some(id), None))),+ None => Ok(Err(format!("no thing named \"{text}\""))),+ }+ } else {+ Ok(Ok((None, Some(json!({ "value": text })))))+ }+}++/// 记一笔并(能的话)执行。keep 只记;撤、改、加先验引文,再过闸门,过了才动图。+/// 没过验证的也记(refused):这条事实算看过了,agent 的说法留着给人看,但不再问+pub async fn record(pool: &PgPool, kb_id: Uuid, input: ActionInput<'_>) -> AppResult<Recorded> {+ let id = Uuid::now_v7();+ let action = input.proposed.action();+ let c = input.candidate;+ if !matches!(input.proposed, Proposed::Add { .. }) && c.is_none() {+ return Err(AppError::Validation(format!(+ "{action} needs the fact it is about"+ )));+ }+ let (fact_id, statement_id, flag) = match c {+ Some(c) => (Some(c.fact_id), c.statement_id, c.flag.clone()),+ None => (None, None, None),+ };+ // 动作指向的那条事实,与验证的结果+ let resolved: Result<Option<Target>, String> = match &input.proposed {+ Proposed::Keep => Ok(None),+ Proposed::Retract => {+ let c = c.unwrap();+ Ok(Some(Target {+ subject_id: c.subject_id,+ predicate_id: c.predicate_id,+ object_id: c.object_id,+ object_value: None,+ subject: c.subject.clone(),+ property: c.property.clone(),+ object: c.object.clone(),+ }))+ }+ Proposed::Revise { property, object } => {+ let c = c.unwrap();+ let prop = match property.as_deref().map(str::trim).filter(|p| !p.is_empty()) {+ None => Ok((c.predicate_id, None)),+ Some(key) => match property_by_key(pool, kb_id, key).await? {+ Some((id, kind)) => Ok((id, Some(kind))),+ None => Err(format!("no property with key \"{key}\"")),+ },+ };+ match prop {+ Err(e) => Err(e),+ Ok((predicate_id, kind)) => {+ let kind = match kind {+ Some(k) => k,+ None => {+ sqlx::query_scalar("SELECT kind FROM relation_types WHERE id = $1")+ .bind(predicate_id)+ .fetch_one(pool)+ .await?+ }+ };+ let old: (Option<Uuid>, Option<Value>) =+ sqlx::query_as("SELECT object_id, object_value FROM facts WHERE id = $1")+ .bind(c.fact_id)+ .fetch_one(pool)+ .await?;+ let obj = match object.as_deref() {+ Some(text) => object_of(pool, kb_id, &kind, text).await?,+ None => Ok(old.clone()),+ };+ match obj {+ Err(e) => Err(e),+ Ok((object_id, object_value))+ if predicate_id == c.predicate_id+ && object_id == old.0+ && object_value == old.1 =>+ {+ Err("the revision changes nothing".into())+ }+ Ok((object_id, object_value)) => {+ let property = match property {+ Some(k) => k.trim().to_string(),+ None => c.property.clone(),+ };+ Ok(Some(Target {+ subject_id: c.subject_id,+ predicate_id,+ object_id,+ object_value,+ subject: c.subject.clone(),+ property,+ object: object.clone().unwrap_or_else(|| c.object.clone()),+ }))+ }+ }+ }+ }+ }+ Proposed::Add {+ subject,+ property,+ object,+ } => {+ let subject_id =+ crate::resolution::existing_by_name(pool, kb_id, subject.trim()).await?;+ match (subject_id, property_by_key(pool, kb_id, property).await?) {+ (None, _) => Err(format!("no thing named \"{}\"", subject.trim())),+ (_, None) => Err(format!("no property with key \"{}\"", property.trim())),+ (Some(subject_id), Some((predicate_id, kind))) => {+ match object_of(pool, kb_id, &kind, object).await? {+ Err(e) => Err(e),+ Ok((object_id, object_value)) => Ok(Some(Target {+ subject_id,+ predicate_id,+ object_id,+ object_value,+ subject: subject.trim().to_string(),+ property: property.trim().to_string(),+ object: object.trim().to_string(),+ })),+ }+ }+ }+ }+ };+ // 撤、改、加都得引文档的原话;不是原话的说法记下来,不执行+ let resolved = match resolved {+ Ok(t) => {+ if input.proposed != Proposed::Keep+ && !input+ .quote+ .is_some_and(|q| quote_in(input.document_text, q))+ {+ Err("the quote is not in the document".to_string())+ } else {+ Ok(t)+ }+ }+ e => e,+ };+ let target_json = match &resolved {+ Ok(Some(t)) => Some(t.json()),+ _ => None,+ };+ // 0027 的闸门:撤的那条有什么靠着它,写的那条会不会立刻开出违规+ let hold = match &resolved {+ Ok(Some(t)) => {+ let mut impact = Impact::default();+ if let Some(fact) = fact_id.filter(|_| input.proposed != Proposed::Keep) {+ impact = execution_gate::impact_of_fact(pool, kb_id, fact).await?;+ }+ if !matches!(input.proposed, Proposed::Retract) {+ let replacing =+ fact_id.filter(|_| matches!(input.proposed, Proposed::Revise { .. }));+ let w = execution_gate::impact_of_write(+ pool,+ kb_id,+ t.subject_id,+ t.predicate_id,+ t.object_id,+ replacing,+ )+ .await?;+ impact.contradictions.extend(w.contradictions);+ }+ execution_gate::hold(&impact)+ }+ _ => None,+ };+ let (status, detail): (&'static str, Option<String>) = match (&resolved, &hold) {+ (Err(e), _) => ("refused", Some(e.clone())),+ (Ok(_), Some(h)) => ("held", Some(h.to_string())),+ (Ok(_), None) => ("applied", None),+ };+ sqlx::query(+ "INSERT INTO errata_actions+ (id, kb_id, run_id, document_id, fact_id, statement_id, predicate_id, flag,+ action, reason, quote, proposed, status, detail)+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)",+ )+ .bind(id)+ .bind(kb_id)+ .bind(input.run_id)+ .bind(input.document_id)+ .bind(fact_id)+ .bind(statement_id)+ .bind(c.map(|c| c.predicate_id))+ .bind(&flag)+ .bind(action)+ .bind(input.reason)+ .bind(input.quote)+ .bind(&target_json)+ .bind(status)+ .bind(&detail)+ .execute(pool)+ .await?;+ let mut new_fact_id = None;+ if status == "applied" {+ if let Ok(Some(t)) = &resolved {+ new_fact_id = apply(+ pool,+ kb_id,+ action,+ fact_id,+ input.document_id,+ t,+ input.quote,+ )+ .await?;+ if new_fact_id.is_some() {+ sqlx::query("UPDATE errata_actions SET new_fact_id = $2 WHERE id = $1")+ .bind(id)+ .bind(new_fact_id)+ .execute(pool)+ .await?;+ }+ }+ }+ Ok(Recorded {+ id,+ status,+ detail,+ new_fact_id,+ })+}++/// 动图:撤是作废(`reject_fact`,证据不动,台账留痕);改是撤旧写新,新行接旧行的时间;+/// 加是写一行,锚在文档的日期上。新行的证据是引文所在的分块。+/// 已经不在的行(别处先撤了)不算错——要的结果已经在了+async fn apply(+ pool: &PgPool,+ kb_id: Uuid,+ action: &str,+ fact_id: Option<Uuid>,+ document_id: Uuid,+ target: &Target,+ quote: Option<&str>,+) -> AppResult<Option<Uuid>> {+ let mut validity_row: Option<ValidityRow> = None;+ if matches!(action, "retract" | "revise") {+ let fact = fact_id.ok_or_else(|| AppError::Validation("no fact to retract".into()))?;+ validity_row = sqlx::query_as(+ "SELECT valid_from, valid_from_precision, valid_from_grade, valid_to, valid_to_precision,+ attested_from FROM facts WHERE id = $1",+ )+ .bind(fact)+ .fetch_optional(pool)+ .await?;+ match crate::graph::reject_fact(pool, kb_id, fact).await {+ Ok(()) | Err(AppError::NotFound) => {}+ Err(e) => return Err(e),+ }+ }+ if action == "retract" {+ return Ok(None);+ }+ let doc_time: Option<DateTime<Utc>> =+ sqlx::query_scalar("SELECT doc_time FROM documents WHERE id = $1")+ .bind(document_id)+ .fetch_optional(pool)+ .await?+ .flatten();+ let v = validity_row.unwrap_or_default();+ let validity = Validity {+ from: v.valid_from,+ from_precision: v.valid_from_precision.as_deref(),+ from_grade: v.valid_from_grade.as_deref(),+ to: v.valid_to,+ to_precision: v.valid_to_precision.as_deref(),+ attested_at: v.attested_from.or(doc_time),+ };+ let object = match (target.object_id, &target.object_value) {+ (Some(o), _) => FactObject::Entity(o),+ (None, Some(val)) => FactObject::Value(val),+ _ => return Err(AppError::Validation("a fact needs an object".into())),+ };+ let mut conn = pool.acquire().await?;+ let (new, _) = crate::graph::insert_fact_on(+ &mut conn,+ kb_id,+ target.subject_id,+ Some(target.predicate_id),+ object,+ validity,+ 0.9,+ )+ .await?;+ // 证据:引文所在的分块;引文跨块时退到文档的第一块+ let placed = sqlx::query(+ "INSERT INTO fact_evidence (fact_id, chunk_id, quote, document_id, doc_version)+ SELECT $1, c.id, $3, c.document_id, c.doc_version FROM chunks c+ WHERE c.document_id = $2 AND c.superseded_at IS NULL+ AND position(lower($3) IN lower(c.text)) > 0+ ORDER BY c.seq LIMIT 1+ ON CONFLICT DO NOTHING",+ )+ .bind(new)+ .bind(document_id)+ .bind(quote.unwrap_or(""))+ .execute(&mut *conn)+ .await?+ .rows_affected();+ if placed == 0 {+ sqlx::query(+ "INSERT INTO fact_evidence (fact_id, chunk_id, quote, document_id, doc_version)+ SELECT $1, c.id, $3, c.document_id, c.doc_version FROM chunks c+ WHERE c.document_id = $2 AND c.superseded_at IS NULL+ ORDER BY c.seq LIMIT 1+ ON CONFLICT DO NOTHING",+ )+ .bind(new)+ .bind(document_id)+ .bind(quote.unwrap_or(""))+ .execute(&mut *conn)+ .await?;+ }+ Ok(Some(new))+}++#[derive(Debug, Default, sqlx::FromRow)]+struct ValidityRow {+ valid_from: Option<DateTime<Utc>>,+ valid_from_precision: Option<String>,+ valid_from_grade: Option<String>,+ valid_to: Option<DateTime<Utc>>,+ valid_to_precision: Option<String>,+ attested_from: Option<DateTime<Utc>>,+}++/// 闸门留给人的一笔:看的是哪份文档的哪条事实、agent 想怎么办、凭哪句原话、为什么留下+#[derive(Debug, Clone, Serialize, sqlx::FromRow)]+pub struct HeldItem {+ pub id: Uuid,+ pub document_id: Uuid,+ pub document: String,+ pub action: String,+ pub flag: Option<String>,+ pub fact_id: Option<Uuid>,+ pub proposed: Option<Value>,+ pub reason: String,+ pub quote: Option<String>,+ pub detail: Option<String>,+ pub created_at: DateTime<Utc>,+}++pub async fn held(pool: &PgPool, kb_id: Uuid, limit: i64, offset: i64) -> AppResult<Vec<HeldItem>> {+ Ok(sqlx::query_as(+ "SELECT ea.id, ea.document_id, d.filename AS document, ea.action, ea.flag, ea.fact_id,+ ea.proposed, ea.reason, ea.quote, ea.detail, ea.created_at+ FROM errata_actions ea JOIN documents d ON d.id = ea.document_id+ WHERE ea.kb_id = $1 AND ea.status = 'held'+ ORDER BY ea.created_at, ea.id+ LIMIT $2 OFFSET $3",+ )+ .bind(kb_id)+ .bind(limit)+ .bind(offset)+ .fetch_all(pool)+ .await?)+}++/// 等人的几笔,最老的从什么时候起等+pub async fn waiting(pool: &PgPool, kb_id: Uuid) -> AppResult<(i64, Option<DateTime<Utc>>)> {+ Ok(sqlx::query_as(+ "SELECT count(*), min(created_at) FROM errata_actions WHERE kb_id = $1 AND status = 'held'",+ )+ .bind(kb_id)+ .fetch_one(pool)+ .await?)+}++#[derive(sqlx::FromRow)]+struct HeldRow {+ action: String,+ fact_id: Option<Uuid>,+ document_id: Uuid,+ proposed: Option<Value>,+ quote: Option<String>,+}++/// 人答留给人的一笔:批了就执行,否了就记 rejected。不是 held 的(已经答过、别的库的)回 false+pub async fn decide_held(+ pool: &PgPool,+ kb_id: Uuid,+ action_id: Uuid,+ approve: bool,+ actor: Uuid,+) -> AppResult<bool> {+ let row: Option<HeldRow> = sqlx::query_as(+ "SELECT action, fact_id, document_id, proposed, quote FROM errata_actions+ WHERE id = $1 AND kb_id = $2 AND status = 'held'",+ )+ .bind(action_id)+ .bind(kb_id)+ .fetch_optional(pool)+ .await?;+ let Some(HeldRow {+ action,+ fact_id,+ document_id,+ proposed,+ quote,+ }) = row+ else {+ return Ok(false);+ };+ let mut new_fact_id = None;+ if approve {+ let target = proposed+ .as_ref()+ .and_then(Target::from_json)+ .ok_or_else(|| AppError::Validation("the held action has no target".into()))?;+ new_fact_id = apply(+ pool,+ kb_id,+ &action,+ fact_id,+ document_id,+ &target,+ quote.as_deref(),+ )+ .await?;+ }+ sqlx::query(+ "UPDATE errata_actions SET status = $3, new_fact_id = $4, decided_by = $5, decided_at = now()+ WHERE id = $1 AND kb_id = $2 AND status = 'held'",+ )+ .bind(action_id)+ .bind(kb_id)+ .bind(if approve { "applied" } else { "rejected" })+ .bind(new_fact_id)+ .bind(actor)+ .execute(pool)+ .await?;+ Ok(true)+}++#[cfg(test)]+mod quote_tests {+ use super::quote_in;++ #[test]+ fn a_quote_is_the_documents_words_up_to_case_and_spacing() {+ let doc = "Acme is based in London.\n Jane Roe runs Acme.";+ assert!(quote_in(doc, "based in London"));+ assert!(quote_in(doc, "jane roe runs acme"));+ assert!(!quote_in(doc, "based in Paris"));+ assert!(!quote_in(doc, ""));+ assert!(!quote_in(doc, " "));+ }+}++#[cfg(test)]+#[path = "errata_tests.rs"]+mod tests;diff --git a/crates/utopia-store/src/errata_tests.rs b/crates/utopia-store/src/errata_tests.rsnew file mode 100644index 00000000..267c8dfc--- /dev/null+++ b/crates/utopia-store/src/errata_tests.rs@@ -0,0 +1,495 @@+//! 勘误(0044 决定 7):结构先报、撤销经得住物化、闸门留给人、改与加带着原话。+//! 没有 `UTOPIA_DATABASE_URL` 时跳过。+use super::*;+use crate::materialize;+use sqlx::PgPool;+use std::collections::HashMap;++struct Fx {+ org: Uuid,+ kb: Uuid,+ doc: Uuid,+ chunk: Uuid,+ organization: Uuid,+ place: Uuid,+ person: Uuid,+ based_in: Uuid,+ ceo: Uuid,+ founded: Uuid,+ acme: Uuid,+ london: Uuid,+ paris: Uuid,+ jane: Uuid,+ nobody: Uuid,+ /// 答留给人那几笔的人+ user: Uuid,+ text: &'static str,+}++const TEXT: &str = "Acme is based in London. Jane Roe runs Acme. Acme was founded in 1999.";++/// 一个库:organization / place / person;based_in(organization → place)、ceo(organization →+/// person,只许一个)、founded(日期属性);一份文档一块正文;Acme、London、Paris、Jane Roe、+/// Nobody 五样东西+async fn seed(pool: &PgPool) -> anyhow::Result<Fx> {+ let ids: Vec<Uuid> = (0..17).map(|_| Uuid::now_v7()).collect();+ let (org, ws, kb, doc, chunk, organization, place, person, based_in, ceo, founded) = (+ ids[0], ids[1], ids[2], ids[3], ids[4], ids[5], ids[6], ids[7], ids[8], ids[9], ids[10],+ );+ let (acme, london, paris, jane, nobody, user) =+ (ids[11], ids[12], ids[13], ids[14], ids[15], ids[16]);+ sqlx::raw_sql(&format!(+ "INSERT INTO organizations(id,name) VALUES ('{org}','errata-test');+ INSERT INTO workspaces(id,org_id,name) VALUES ('{ws}','{org}','errata-test');+ INSERT INTO knowledge_bases(id,workspace_id,name) VALUES ('{kb}','{ws}','errata-test');+ INSERT INTO users(id,org_id,email,display_name,password_hash) VALUES ('{user}','{org}','{user}@errata.test','reviewer','unused');+ INSERT INTO documents(id,kb_id,filename,sha256,doc_time) VALUES ('{doc}','{kb}','acme.txt','x','2020-01-01T00:00:00Z');+ INSERT INTO chunks(id,kb_id,document_id,seq,text) VALUES ('{chunk}','{kb}','{doc}',0,'{TEXT}');+ INSERT INTO entity_types(id,kb_id,key,label,color,shape) VALUES+ ('{organization}','{kb}','organization','Organization','#000','circle'),+ ('{place}','{kb}','place','Place','#000','circle'),+ ('{person}','{kb}','person','Person','#000','circle');+ INSERT INTO relation_types(id,kb_id,key,label,kind,temporal,functional,datatype) VALUES+ ('{based_in}','{kb}','based_in','based in','relation','state',false,NULL),+ ('{ceo}','{kb}','ceo','chief executive','relation','state',true,NULL),+ ('{founded}','{kb}','founded','founded','attribute','eternal',false,'date');+ INSERT INTO relation_type_domains(relation_type_id,entity_type_id) VALUES+ ('{based_in}','{organization}'), ('{ceo}','{organization}'), ('{founded}','{organization}');+ INSERT INTO relation_type_ranges(relation_type_id,entity_type_id) VALUES+ ('{based_in}','{place}'), ('{ceo}','{person}');+ INSERT INTO entities(id,kb_id,canonical_name,type_id) VALUES+ ('{acme}','{kb}','Acme','{organization}'), ('{london}','{kb}','London','{place}'),+ ('{paris}','{kb}','Paris','{place}'), ('{jane}','{kb}','Jane Roe','{person}'),+ ('{nobody}','{kb}','Nobody','{person}');"+ ))+ .execute(pool)+ .await?;+ Ok(Fx {+ org,+ kb,+ doc,+ chunk,+ organization,+ place,+ person,+ based_in,+ ceo,+ founded,+ acme,+ london,+ paris,+ jane,+ nobody,+ user,+ text: TEXT,+ })+}++async fn cleanup(pool: &PgPool, f: &Fx) -> anyhow::Result<()> {+ sqlx::query("DELETE FROM jobs WHERE payload->>'kb_id'=$1")+ .bind(f.kb.to_string())+ .execute(pool)+ .await?;+ sqlx::query("DELETE FROM organizations WHERE id=$1")+ .bind(f.org)+ .execute(pool)+ .await?;+ Ok(())+}++/// 一条开放陈述加一条从它算出的类型化行,证据落在这份文档上。返回 (陈述, 类型化行)+async fn typed(+ pool: &PgPool,+ f: &Fx,+ subject: Uuid,+ phrase: &str,+ property: Uuid,+ object: Option<Uuid>,+ value: Option<&str>,+) -> anyhow::Result<(Uuid, Uuid)> {+ let (s, t) = (Uuid::now_v7(), Uuid::now_v7());+ let value = value.map(|v| serde_json::json!({ "value": v }));+ sqlx::query(+ "INSERT INTO facts(id,kb_id,subject_id,object_id,object_value,layer,phrase) VALUES ($1,$2,$3,$4,$5,'open',$6)",+ )+ .bind(s)+ .bind(f.kb)+ .bind(subject)+ .bind(object)+ .bind(&value)+ .bind(phrase)+ .execute(pool)+ .await?;+ sqlx::query(+ "INSERT INTO facts(id,kb_id,subject_id,predicate_id,object_id,object_value,layer,from_statement_id)+ VALUES ($1,$2,$3,$4,$5,$6,'typed',$7)",+ )+ .bind(t)+ .bind(f.kb)+ .bind(subject)+ .bind(property)+ .bind(object)+ .bind(&value)+ .bind(s)+ .execute(pool)+ .await?;+ sqlx::query("INSERT INTO typed_fact_sources(fact_id,statement_id) VALUES ($1,$2)")+ .bind(t)+ .bind(s)+ .execute(pool)+ .await?;+ for fact in [s, t] {+ sqlx::query(+ "INSERT INTO fact_evidence(fact_id,chunk_id,quote,document_id,doc_version) VALUES ($1,$2,$3,$4,1)",+ )+ .bind(fact)+ .bind(f.chunk)+ .bind(phrase)+ .bind(f.doc)+ .execute(pool)+ .await?;+ }+ Ok((s, t))+}++async fn live(pool: &PgPool, fact: Uuid) -> anyhow::Result<bool> {+ Ok(+ sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id=$1")+ .bind(fact)+ .fetch_one(pool)+ .await?,+ )+}++async fn status_of(pool: &PgPool, action: Uuid) -> anyhow::Result<(String, Option<String>)> {+ Ok(+ sqlx::query_as("SELECT status, detail FROM errata_actions WHERE id=$1")+ .bind(action)+ .fetch_one(pool)+ .await?,+ )+}++fn input<'a>(+ f: &Fx,+ run: Uuid,+ c: Option<&'a Candidate>,+ proposed: Proposed,+ quote: Option<&'a str>,+) -> ActionInput<'a> {+ ActionInput {+ run_id: run,+ document_id: f.doc,+ candidate: c,+ proposed,+ reason: "because",+ quote,+ document_text: f.text,+ }+}++#[tokio::test]+async fn structure_flags_facts_and_the_flagged_come_first() -> anyhow::Result<()> {+ let Some(url) = crate::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ crate::db::migrate(&pool).await?;+ let f = seed(&pool).await?;+ let run = async {+ let (_, fine) = typed(+ &pool,+ &f,+ f.acme,+ "based in",+ f.based_in,+ Some(f.london),+ None,+ )+ .await?;+ let (_, absent) = typed(+ &pool,+ &f,+ f.acme,+ "based in",+ f.based_in,+ Some(f.paris),+ None,+ )+ .await?;+ let (_, domain) = typed(+ &pool,+ &f,+ f.jane,+ "based in",+ f.based_in,+ Some(f.london),+ None,+ )+ .await?;+ let (_, range) = typed(&pool, &f, f.acme, "run by", f.ceo, Some(f.london), None).await?;+ let (_, no_date) = typed(+ &pool,+ &f,+ f.acme,+ "founded",+ f.founded,+ None,+ Some("the nineties"),+ )+ .await?;+ let (_, dated) = typed(+ &pool,+ &f,+ f.acme,+ "founded in",+ f.founded,+ None,+ Some("1999"),+ )+ .await?;+ let (_, nobody) = typed(&pool, &f, f.acme, "run by", f.ceo, Some(f.nobody), None).await?;+ let c = candidates(&pool, f.kb, f.doc).await?;+ let flags: HashMap<Uuid, Option<String>> =+ c.iter().map(|c| (c.fact_id, c.flag.clone())).collect();+ assert_eq!(flags[&fine], None);+ assert_eq!(flags[&dated], None);+ assert_eq!(flags[&absent].as_deref(), Some("name_absent"));+ assert_eq!(flags[&domain].as_deref(), Some("domain"));+ assert_eq!(flags[&range].as_deref(), Some("range"));+ assert_eq!(flags[&no_date].as_deref(), Some("no_date"));+ assert_eq!(flags[&nobody].as_deref(), Some("name_absent"));+ // 报了的在前,没报的在后+ let order: Vec<bool> = c.iter().map(|c| c.flag.is_some()).collect();+ assert_eq!(order, vec![true, true, true, true, true, false, false]);+ let first = &c[0];+ assert_eq!(+ (first.subject.as_str(), first.property.as_str()),+ ("Acme", "based_in")+ );+ assert_eq!(first.quote.as_deref(), Some("based in"));+ assert!(first.statement_id.is_some());+ // 这份文档待看;看过一条(keep)之后它还待看,全看完才不+ assert_eq!(documents_due(&pool, f.kb, 10).await?, vec![f.doc]);+ let run = start_run(&pool, f.kb, f.doc, 5, 2).await?;+ let r = record(+ &pool,+ f.kb,+ input(&f, run, Some(first), Proposed::Keep, None),+ )+ .await?;+ assert_eq!(r.status, "applied");+ assert_eq!(candidates(&pool, f.kb, f.doc).await?.len(), 6);+ assert_eq!(documents_due(&pool, f.kb, 10).await?, vec![f.doc]);+ finish_run(&pool, run, 1, Some(100), Some(20)).await?;+ let usage: (i32, Option<i64>) =+ sqlx::query_as("SELECT requests, prompt_tokens FROM errata_runs WHERE id=$1")+ .bind(run)+ .fetch_one(&pool)+ .await?;+ assert_eq!(usage, (1, Some(100)));+ anyhow::Ok(())+ }+ .await;+ cleanup(&pool, &f).await?;+ run+}++#[tokio::test]+async fn a_retraction_sticks_through_materialisation() -> anyhow::Result<()> {+ let Some(url) = crate::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ crate::db::migrate(&pool).await?;+ let f = seed(&pool).await?;+ let run = async {+ // 一条陈述 Acme —based in→ Paris,签名绑到 based_in;物化算出类型化行+ let s = Uuid::now_v7();+ sqlx::query("INSERT INTO facts(id,kb_id,subject_id,object_id,layer,phrase) VALUES ($1,$2,$3,$4,'open','based in')")+ .bind(s).bind(f.kb).bind(f.acme).bind(f.paris).execute(&pool).await?;+ sqlx::query("INSERT INTO fact_evidence(fact_id,chunk_id,quote,document_id,doc_version) VALUES ($1,$2,'based in',$3,1)")+ .bind(s).bind(f.chunk).bind(f.doc).execute(&pool).await?;+ sqlx::query(+ "INSERT INTO phrase_bindings (id, kb_id, phrase, subject_type_id, object_type_id, object_is_value,+ relation_type_id, direction, status)+ VALUES ($1, $2, 'based in', $3, $4, false, $5, 'forward', 'bound')",+ )+ .bind(Uuid::now_v7()).bind(f.kb).bind(f.organization).bind(f.place).bind(f.based_in)+ .execute(&pool).await?;+ let o = materialize::materialize(&pool, f.kb).await?;+ assert_eq!(o.added, 1);+ let c = candidates(&pool, f.kb, f.doc).await?;+ assert_eq!(c.len(), 1);+ assert_eq!(c[0].flag.as_deref(), Some("name_absent"), "Paris is not in the document");+ assert_eq!(c[0].statement_id, Some(s));+ let typed_row = c[0].fact_id;+ let run = start_run(&pool, f.kb, f.doc, 1, 0).await?;+ let r = record(&pool, f.kb, input(&f, run, Some(&c[0]), Proposed::Retract, Some("Acme is based in London"))).await?;+ assert_eq!((r.status, r.detail), ("applied", None));+ assert!(!live(&pool, typed_row).await?);+ // 陈述还活着、绑定还在:物化再跑,那一对不再算出来+ let o = materialize::materialize(&pool, f.kb).await?;+ assert_eq!((o.added, o.merged, o.retired), (0, 0, 0));+ let live_typed: i64 = sqlx::query_scalar(+ "SELECT count(*) FROM facts WHERE kb_id=$1 AND layer='typed' AND invalidated_at IS NULL",+ )+ .bind(f.kb)+ .fetch_one(&pool)+ .await?;+ assert_eq!(live_typed, 0);+ assert!(documents_due(&pool, f.kb, 10).await?.is_empty(), "nothing left to look at");+ // 引文不是原话的撤:记下来,不执行+ let (_, other) = typed(&pool, &f, f.acme, "based in", f.based_in, Some(f.london), None).await?;+ let c = candidates(&pool, f.kb, f.doc).await?;+ let r = record(&pool, f.kb, input(&f, run, Some(&c[0]), Proposed::Retract, Some("Acme is based in Paris"))).await?;+ assert_eq!(r.status, "refused");+ assert_eq!(r.detail.as_deref(), Some("the quote is not in the document"));+ assert!(live(&pool, other).await?);+ anyhow::Ok(())+ }+ .await;+ cleanup(&pool, &f).await?;+ run+}++#[tokio::test]+async fn a_retraction_something_rests_on_is_held_until_a_person_decides() -> anyhow::Result<()> {+ let Some(url) = crate::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ crate::db::migrate(&pool).await?;+ let f = seed(&pool).await?;+ let run = async {+ let (_, t1) = typed(&pool, &f, f.acme, "based in", f.based_in, Some(f.paris), None).await?;+ let (_, t2) = typed(&pool, &f, f.acme, "run by", f.ceo, Some(f.nobody), None).await?;+ // 两条各有一条派生靠着+ let rule = Uuid::now_v7();+ sqlx::query("INSERT INTO rules(id,kb_id,predicate_id,kind) VALUES ($1,$2,$3,'transitive')")+ .bind(rule).bind(f.kb).bind(f.based_in).execute(&pool).await?;+ for (premise, object) in [(t1, f.london), (t2, f.paris)] {+ let d = Uuid::now_v7();+ sqlx::query("INSERT INTO derived_facts(id,kb_id,subject_id,predicate_id,object_id,rule_id) VALUES ($1,$2,$3,$4,$5,$6)")+ .bind(d).bind(f.kb).bind(f.acme).bind(f.based_in).bind(object).bind(rule).execute(&pool).await?;+ sqlx::query("INSERT INTO fact_derivations(derived_fact_id,premise_fact_id,seq) VALUES ($1,$2,0)")+ .bind(d).bind(premise).execute(&pool).await?;+ }+ let c = candidates(&pool, f.kb, f.doc).await?;+ let run = start_run(&pool, f.kb, f.doc, 2, 0).await?;+ let mut held_ids = Vec::new();+ for cand in &c {+ let r = record(&pool, f.kb, input(&f, run, Some(cand), Proposed::Retract, Some("Acme is based in London"))).await?;+ assert_eq!(r.status, "held");+ assert_eq!(r.detail.as_deref(), Some("derived 1"));+ held_ids.push(r.id);+ }+ assert!(live(&pool, t1).await? && live(&pool, t2).await?, "held means untouched");+ let queue = held(&pool, f.kb, 10, 0).await?;+ assert_eq!(queue.len(), 2);+ assert_eq!(queue[0].document, "acme.txt");+ assert_eq!(queue[0].detail.as_deref(), Some("derived 1"));+ assert_eq!(queue[0].proposed.as_ref().unwrap()["subject"], "Acme");+ assert_eq!(waiting(&pool, f.kb).await?.0, 2);+ // 人否第一笔:事实还在;批第二笔:撤了+ let actor = f.user;+ assert!(decide_held(&pool, f.kb, held_ids[0], false, actor).await?);+ assert!(decide_held(&pool, f.kb, held_ids[1], true, actor).await?);+ assert!(!decide_held(&pool, f.kb, held_ids[1], true, actor).await?, "answered once");+ assert_eq!(status_of(&pool, held_ids[0]).await?.0, "rejected");+ assert_eq!(status_of(&pool, held_ids[1]).await?.0, "applied");+ assert!(live(&pool, c[0].fact_id).await?);+ assert!(!live(&pool, c[1].fact_id).await?);+ assert_eq!(waiting(&pool, f.kb).await?.0, 0);+ anyhow::Ok(())+ }+ .await;+ cleanup(&pool, &f).await?;+ run+}++#[tokio::test]+async fn a_revision_and_an_addition_write_typed_facts_with_the_documents_words(+) -> anyhow::Result<()> {+ let Some(url) = crate::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ crate::db::migrate(&pool).await?;+ let f = seed(&pool).await?;+ let run = async {+ let (_, wrong_ceo) = typed(&pool, &f, f.acme, "run by", f.ceo, Some(f.london), None).await?;+ let c = candidates(&pool, f.kb, f.doc).await?;+ assert_eq!(c[0].flag.as_deref(), Some("range"));+ let run = start_run(&pool, f.kb, f.doc, 1, 0).await?;+ // 改宾语:London → Jane Roe。旧行撤了,新行活着、带引文+ let r = record(+ &pool,+ f.kb,+ input(&f, run, Some(&c[0]), Proposed::Revise { property: None, object: Some("Jane Roe".into()) }, Some("Jane Roe runs Acme")),+ )+ .await?;+ assert_eq!((r.status, r.detail.clone()), ("applied", None));+ let new = r.new_fact_id.expect("a revision writes a row");+ assert!(!live(&pool, wrong_ceo).await?);+ let row: (Uuid, Uuid, Option<Uuid>, bool) = sqlx::query_as(+ "SELECT subject_id, predicate_id, object_id, invalidated_at IS NULL FROM facts WHERE id=$1",+ )+ .bind(new)+ .fetch_one(&pool)+ .await?;+ assert_eq!(row, (f.acme, f.ceo, Some(f.jane), true));+ let quote: (Uuid, Option<String>) =+ sqlx::query_as("SELECT chunk_id, quote FROM fact_evidence WHERE fact_id=$1")+ .bind(new)+ .fetch_one(&pool)+ .await?;+ assert_eq!(quote, (f.chunk, Some("Jane Roe runs Acme".into())));+ // 加一条日期属性:值落下+ let r = record(+ &pool,+ f.kb,+ input(&f, run, None, Proposed::Add { subject: "Acme".into(), property: "founded".into(), object: "1999".into() }, Some("founded in 1999")),+ )+ .await?;+ assert_eq!(r.status, "applied");+ let value: Option<serde_json::Value> =+ sqlx::query_scalar("SELECT object_value FROM facts WHERE id=$1")+ .bind(r.new_fact_id.unwrap())+ .fetch_one(&pool)+ .await?;+ assert_eq!(value, Some(serde_json::json!({ "value": "1999" })));+ // 名字不在库里:勘误不造东西+ let r = record(+ &pool,+ f.kb,+ input(&f, run, None, Proposed::Add { subject: "Acme".into(), property: "ceo".into(), object: "Bob".into() }, Some("runs Acme")),+ )+ .await?;+ assert_eq!(r.status, "refused");+ assert_eq!(r.detail.as_deref(), Some("no thing named \"Bob\""));+ // 只许一个值的谓词已经有 Jane:再加一个 London 会开出违规,留给人+ let r = record(+ &pool,+ f.kb,+ input(&f, run, None, Proposed::Add { subject: "Acme".into(), property: "ceo".into(), object: "London".into() }, Some("based in London")),+ )+ .await?;+ assert_eq!(r.status, "held");+ assert_eq!(r.detail.as_deref(), Some("contradiction chief executive"));+ // 属性不存在+ let r = record(+ &pool,+ f.kb,+ input(&f, run, None, Proposed::Add { subject: "Acme".into(), property: "owner".into(), object: "Jane Roe".into() }, Some("runs Acme")),+ )+ .await?;+ assert_eq!(r.status, "refused");+ assert_eq!(r.detail.as_deref(), Some("no property with key \"owner\""));+ let _ = (f.person, f.place, f.organization, f.paris, f.founded);+ anyhow::Ok(())+ }+ .await;+ cleanup(&pool, &f).await?;+ run+}diff --git a/crates/utopia-store/src/execution_gate.rs b/crates/utopia-store/src/execution_gate.rsindex bdd19a8f..cb44c2c7 100644--- a/crates/utopia-store/src/execution_gate.rs+++ b/crates/utopia-store/src/execution_gate.rs@@ -149,6 +149,73 @@ pub async fn impact_of(pool: &PgPool, kb_id: Uuid, a: Uuid, b: Uuid) -> AppResul})}+/// 撤一条事实会牵动什么(0044 决定 7 的勘误 agent 走这同一道闸门):以它为前提、还成立的+/// 派生;把它的主语认下过的回答(回答记的是它认下的东西,不是引的事实——主语被问过,+/// 关于它的一条事实就可能进过答案;与合并同一个口径)。矛盾一栏空着:撤掉一条不会开出违规+pub async fn impact_of_fact(pool: &PgPool, kb_id: Uuid, fact_id: Uuid) -> AppResult<Impact> {+ let derived: i64 = sqlx::query_scalar(+ "SELECT count(DISTINCT d.id) FROM fact_derivations fd+ JOIN derived_facts d ON d.id = fd.derived_fact_id+ WHERE fd.premise_fact_id = $2 AND d.kb_id = $1 AND d.invalidated_at IS NULL",+ )+ .bind(kb_id)+ .bind(fact_id)+ .fetch_one(pool)+ .await?;+ let answered: i64 = sqlx::query_scalar(+ "SELECT count(*) FROM conversation_messages m+ JOIN conversations c ON c.id = m.conversation_id+ WHERE c.kb_id = $1 AND m.role = 'assistant'+ AND EXISTS (SELECT 1 FROM jsonb_array_elements(m.resolved) e+ WHERE e->>'id' = (SELECT subject_id::text FROM facts WHERE id = $2))",+ )+ .bind(kb_id)+ .bind(fact_id)+ .fetch_one(pool)+ .await?;+ Ok(Impact {+ contradictions: vec![],+ derived,+ answered,+ })+}++/// 写一条事实会牵动什么:谓词只许一个值而主语已经有另一个东西——一致性检查下一次跑就+/// 开出 `functional` 违规。与 0027 §5 同一个口径:只看实体宾语的边,不看时间,不看字面值+pub async fn impact_of_write(+ pool: &PgPool,+ kb_id: Uuid,+ subject_id: Uuid,+ predicate_id: Uuid,+ object_id: Option<Uuid>,+ // 这次写是要取代的那一行:改一条事实时旧行还活着,它不算撞+ replacing: Option<Uuid>,+) -> AppResult<Impact> {+ let Some(object) = object_id else {+ return Ok(Impact::default());+ };+ let clash: Option<String> = sqlx::query_scalar(+ "SELECT r.label FROM relation_types r+ WHERE r.id = $2 AND r.kb_id = $1 AND r.functional+ AND EXISTS (SELECT 1 FROM facts f+ WHERE f.kb_id = $1 AND f.subject_id = $3 AND f.predicate_id = $2+ AND f.invalidated_at IS NULL AND f.object_id IS NOT NULL+ AND f.object_id <> $4 AND f.id IS DISTINCT FROM $5)",+ )+ .bind(kb_id)+ .bind(predicate_id)+ .bind(subject_id)+ .bind(object)+ .bind(replacing)+ .fetch_optional(pool)+ .await?;+ Ok(Impact {+ contradictions: clash.into_iter().collect(),+ derived: 0,+ answered: 0,+ })+}+#[cfg(test)]mod tests {use super::*;diff --git a/crates/utopia-store/src/lib.rs b/crates/utopia-store/src/lib.rsindex 3c520c9f..0ba5c895 100644--- a/crates/utopia-store/src/lib.rs+++ b/crates/utopia-store/src/lib.rs@@ -11,6 +11,7 @@ pub mod conversations;pub mod datasources;pub mod db;pub mod documents;+pub mod errata;pub mod execution_gate;pub mod exploration_runs;pub mod export;diff --git a/crates/utopia-store/src/materialize.rs b/crates/utopia-store/src/materialize.rsindex b05c4e79..349772b6 100644--- a/crates/utopia-store/src/materialize.rs+++ b/crates/utopia-store/src/materialize.rs@@ -185,6 +185,10 @@ async fn materialize_in_tx(AND (b.direction = 'forward' OR s.object_id IS NOT NULL)AND NOT EXISTS (SELECT 1 FROM statement_qualifiers qWHERE q.fact_id = s.id AND q.role = 'mood')+ -- 勘误撤过的(陈述, 属性)不再算(0044 决定 7):撤销要站得住+ AND NOT EXISTS (SELECT 1 FROM errata_actions ea+ WHERE ea.statement_id = s.id AND ea.predicate_id = b.relation_type_id+ AND ea.status = 'applied' AND ea.action IN ('retract', 'revise'))AND NOT EXISTS (SELECT 1 FROM typed_fact_sources src JOIN facts t ON t.id = src.fact_idWHERE src.statement_id = s.id AND t.invalidated_at IS NULLAND t.predicate_id = b.relation_type_id)@@ -357,6 +361,9 @@ async fn imply_in_tx(AND {RULE_MATCH}AND NOT EXISTS (SELECT 1 FROM statement_qualifiers q WHERE q.fact_id = s.id AND q.role = 'mood')AND (r.reading IS NULL OR pr.entity_id IS NOT NULL OR pr.value IS NOT NULL)+ AND NOT EXISTS (SELECT 1 FROM errata_actions ea+ WHERE ea.statement_id = s.id AND ea.predicate_id = r.conclude_property_id+ AND ea.status = 'applied' AND ea.action IN ('retract', 'revise'))AND NOT EXISTS (SELECT 1 FROM implied_fact_sources i JOIN facts t ON t.id = i.fact_idWHERE i.rule_id = r.id AND i.statement_id = s.id AND t.invalidated_at IS NULL)ORDER BY s.id",diff --git a/crates/utopia-store/src/review.rs b/crates/utopia-store/src/review.rsindex 12323ce7..bcf8953f 100644--- a/crates/utopia-store/src/review.rs+++ b/crates/utopia-store/src/review.rs@@ -69,7 +69,8 @@ pub async fn counts(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewCounts> {WHERE d.target_kind = 'review' AND d.target_id = rr.idAND d.status = 'proposed')) AS agent_queue,(SELECT count(*) FROM (SELECT 1 FROM phrase_bindings WHERE kb_id = $1 AND status = 'undecided'- UNION ALL SELECT 1 FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment",+ UNION ALL SELECT 1 FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment,+ (SELECT count(*) FROM errata_actions WHERE kb_id = $1 AND status = 'held') AS errata",unconfirmed = UNCONFIRMED_FACT,same = crate::resolution::TypeFilter::Same.clause(),conflict = crate::resolution::TypeFilter::Conflict.clause(),diff --git a/crates/utopia-store/src/review_summary.rs b/crates/utopia-store/src/review_summary.rsindex 0c31aa8f..d10c67fe 100644--- a/crates/utopia-store/src/review_summary.rs+++ b/crates/utopia-store/src/review_summary.rs@@ -63,6 +63,8 @@ struct WaitingRow {defects_oldest: Option<DateTime<Utc>>,alignment: i64,alignment_oldest: Option<DateTime<Utc>>,+ errata: i64,+ errata_oldest: Option<DateTime<Utc>>,}async fn waiting(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewWaiting> {@@ -102,7 +104,9 @@ async fn waiting(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewWaiting> {(SELECT count(*) FROM (SELECT 1 FROM phrase_bindings WHERE kb_id = $1 AND status = 'undecided'UNION ALL SELECT 1 FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment,(SELECT min(decided_at) FROM (SELECT decided_at FROM phrase_bindings WHERE kb_id = $1 AND status = 'undecided'- UNION ALL SELECT decided_at FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment_oldest",+ UNION ALL SELECT decided_at FROM type_bindings WHERE kb_id = $1 AND status = 'undecided') a) AS alignment_oldest,+ (SELECT count(*) FROM errata_actions WHERE kb_id = $1 AND status = 'held') AS errata,+ (SELECT min(created_at) FROM errata_actions WHERE kb_id = $1 AND status = 'held') AS errata_oldest",unconfirmed = UNCONFIRMED_FACT,);let r: WaitingRow = sqlx::query_as(&sql)@@ -120,6 +124,7 @@ async fn waiting(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewWaiting> {violations: wait(r.violations, r.violations_oldest),defects: wait(r.defects, r.defects_oldest),alignment: wait(r.alignment, r.alignment_oldest),+ errata: wait(r.errata, r.errata_oldest),})}diff --git a/docs/decisions/0044-the-ontology-is-a-view-over-what-documents-say.md b/docs/decisions/0044-the-ontology-is-a-view-over-what-documents-say.mdindex a26d90a3..78501c07 100644--- a/docs/decisions/0044-the-ontology-is-a-view-over-what-documents-say.md+++ b/docs/decisions/0044-the-ontology-is-a-view-over-what-documents-say.md@@ -1,6 +1,6 @@# 0044 · The ontology is a view over what documents say-- **Status**: Accepted 2026-09-17 · cut 1 built: extraction writes open statements (#731), memory documents take the same path (#735), the typed path is deleted (#736), kind words bind to classes with two votes (#741), the contract's rules for things, phrases, tables and moods (#743, #744, #745) · cut 2 in progress: bindings (#751), materialisation (0067, 0068), the human decision with its job (0051, #876), the decision basis (0053, #878), **implication rules with cached readings (migration 0073, PR #882)** — the parity run against the bound pass waits for the typed-graph bench (#880) to be run with a model; identity profiles (cut 4) in progress in a separate track; the errata agent (cut 6) not built · current state in [design/extraction](../design/extraction.md) and [design/ontology](../design/ontology.md) · replaces the staged-reading draft of this record (skim card, graded mentions, statements bound at write time), which the prototype below did not bear out · prototype scripts and measurements from 2026-09-15 are summarised in [What the prototype measured](#what-the-prototype-measured)+- **Status**: Accepted 2026-09-17 · cut 1 built: extraction writes open statements (#731), memory documents take the same path (#735), the typed path is deleted (#736), kind words bind to classes with two votes (#741), the contract's rules for things, phrases, tables and moods (#743, #744, #745) · cut 2 in progress: bindings (#751), materialisation (0067, 0068), the human decision with its job (0051, #876), the decision basis (0053, #878), **implication rules with cached readings (migration 0073, PR #882)** — the parity run against the bound pass waits for the typed-graph bench (#880) to be run with a model; identity profiles (cut 4) in progress in a separate track; **the errata agent (cut 6) built: structural flags, a JSON action protocol with a per-document budget, actions through the 0027 gate (migration 0074, PR #885)** — its measure (precision gained against correct facts removed, tokens) is the `--errata` run of the typed-graph bench, not yet run with a model · current state in [design/extraction](../design/extraction.md) and [design/ontology](../design/ontology.md) · replaces the staged-reading draft of this record (skim card, graded mentions, statements bound at write time), which the prototype below did not bear out · prototype scripts and measurements from 2026-09-15 are summarised in [What the prototype measured](#what-the-prototype-measured)- **Written**: 2026-09-16 (conventions in the [README](README.md))- **Related**: [0022](0022-an-unknown-date-is-not-an-open-one.md) put a document's date in `attested_at` beside the world and record axes; [0025](0025-governance-reads-the-ledger-before-it-decides.md) and [0027](0027-an-automatic-merge-is-gated-by-what-it-can-undo.md) put agent decisions through a gate that weighs what they can undo; [0041](0041-a-name-is-a-claim-about-an-entity.md) made names facts and identity a matter of evidence; [0043](0043-every-review-queue-is-governed.md) sent every review queue through the governor; #714 found upload time used as the document date in extraction.diff --git a/docs/design/extraction.md b/docs/design/extraction.mdindex 0b8819e6..1371e02e 100644--- a/docs/design/extraction.md+++ b/docs/design/extraction.md@@ -199,7 +199,8 @@ and clauses [0012]. The ledger rules those records made stand (see [ledger](ledginterpretation against the document [0045 cuts 1 to 3] were on this list and are built.- A second pass asking which figures and dates are not yet in a statement (dense sentences drop theamount); the period column of a financial-table cell [#729].-- The errata agent over the typed graph [0044 d7].+- The errata agent over the typed graph [0044 d7] was on this list and is built: see+ [design/ontology](ontology.md).## Open questionsdiff --git a/docs/design/governance.md b/docs/design/governance.mdindex f058bf3d..0ff70995 100644--- a/docs/design/governance.md+++ b/docs/design/governance.md@@ -119,7 +119,8 @@ for 589 pairs [0025 d10].unconfirmed queues (unconfirmed becomes an automatic re-attach with an alert), mappings leave theReview page. Kept: `agent_decisions`, `execution_gate`, the temporal engine, name facts,`pending_facts`, the audit ledger. Sequence: after 0044 cuts 1 and 2, with no legacy layer.-- The errata agent on the typed graph through the gate [0044 d7].+- The errata agent on the typed graph through the gate [0044 d7] is built (migration 0074): its+ held actions are the errata queue on the Review page; see [design/ontology](ontology.md).- Decision memory as retrieval and corrections aggregated into ontology signals [0001 P4b, P4c].## Open questionsdiff --git a/docs/design/ontology.md b/docs/design/ontology.mdindex 6f3431c0..0a9fe7c8 100644--- a/docs/design/ontology.md+++ b/docs/design/ontology.md@@ -86,7 +86,30 @@ them on the alignment queue, and the decision commits with its job. Readings areonce per distinct phrase by the `read_phrases` job and cached in `phrase_readings`, including "noanswer"; materialisation never calls a model — it reads the cache, writes the implied facts with thetriggering statement's evidence, marks them `implied` (the export carries the flag), and retires-them by source like any other typed row. Even one edit can reopen all older automatic negative bindings on that side of the+them by source like any other typed row.++**An errata agent reviews the typed graph after extraction** [0044 decision 7, migration 0074]. Once+materialisation has written new rows, the `errata_review` job takes each document with typed facts+nobody has looked at and sends them to the model with the document, the ontology's properties and,+for each fact, the structural flag it earned: `domain` or `range` (an end outside the property's+declared kinds, through the class hierarchy), `name_absent` (a name that does not occur in the+document), `no_date` (a date property holding something that is not a date). Flagged facts go+first, the rest is sampled, and a document gets a budget of two requests. The model answers a JSON+action protocol — keep, retract, revise, or add once every given fact is answered — and every+retract, revise and add must quote the document's own words; a quote that is not in the document,+a name that is not in the base or a property that does not exist is recorded as refused and never+applied (the agent creates nothing). Each verdict is a row in `errata_actions`, keep included, so+a fact is reviewed once. An action passes the 0027 gate before it touches the graph: a fact with a+derived fact resting on it or whose subject was named in an answer, or a write that would give a+one-value property two values, is held for a person on the errata queue of the Review page, where+the card shows the document, the proposed change, the quote and the reason it was held. A+retraction sticks: materialisation and implication skip a (statement, property) pair an applied+errata action retracted or revised, while another document's statement of the same thing still+materialises. `errata_runs` keeps the per-document account (facts flagged and sampled, requests,+the endpoint's token usage) for the measure 0044 names: precision gained against correct facts+removed, at what cost.++Even one edit can reopen all older automatic negative bindings on that side of thebase, requiring two votes per eligible item through batched model requests; debouncing reducesthe number of runs, not the items reconsidered. A burst of ontology edits debounces into one runrather than one run each [#757];diff --git a/migrations/0074_an_errata_agent_reviews_the_typed_graph.sql b/migrations/0074_an_errata_agent_reviews_the_typed_graph.sqlnew file mode 100644index 00000000..2e316af3--- /dev/null+++ b/migrations/0074_an_errata_agent_reviews_the_typed_graph.sql@@ -0,0 +1,63 @@+-- 勘误 agent(0044 决定 7,第六刀):抽取之后,一个 agent 按文档复审类型化图谱。+-- 结构先报的(主语或宾语在属性声明的类之外、文档里找不到的名字、日期属性没有日期)+-- 先看,其余抽样;每一次撤、改、加记成一笔动作,带着文档的原话当证据;会牵动图外+-- 东西的动作留给人(0027 那道闸门),agent 不动手。+--+-- `errata_runs`:一份文档一次复审的账——看了几条、问了几次、花了多少 token。度量+-- 「精度换来多少、撤错多少、花了多少」按次数算,不按行数。+--+-- `errata_actions`:每一条看过的事实一行(keep 也记:「没看过的」就是没行的,复审不重复),+-- 加的事实一行。`statement_id` + `predicate_id` 是撤销站得住的关键:物化下一轮会把+-- 活着的陈述再算成同一条类型化行,除非它知道这条(陈述, 属性)被勘误撤过。别的文档+-- 说了同一件事照样算——勘误看的是这份文档,撤的是这份文档产生的那条。+--+-- status:applied 落了地;held 闸门留给人;refused agent 的说法没过验证(引文不在+-- 文档里、名字不在库里、属性不存在),记下来不执行;rejected 人否了留给人的那笔。++CREATE TABLE errata_runs (+ id UUID PRIMARY KEY,+ kb_id UUID NOT NULL REFERENCES knowledge_bases(id) ON DELETE CASCADE,+ document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,+ -- 这一次送去看的:结构报出来的几条、抽样的几条+ flagged INT NOT NULL DEFAULT 0,+ sampled INT NOT NULL DEFAULT 0,+ requests INT NOT NULL DEFAULT 0,+ -- 端点报的用量;端点不报就空着,不编数字+ prompt_tokens BIGINT,+ completion_tokens BIGINT,+ started_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ finished_at TIMESTAMPTZ+);+CREATE INDEX errata_runs_document_idx ON errata_runs (document_id, started_at DESC);++CREATE TABLE errata_actions (+ id UUID PRIMARY KEY,+ kb_id UUID NOT NULL REFERENCES knowledge_bases(id) ON DELETE CASCADE,+ run_id UUID NOT NULL REFERENCES errata_runs(id) ON DELETE CASCADE,+ document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,+ -- 看的那条类型化事实;add 没有+ fact_id UUID REFERENCES facts(id) ON DELETE CASCADE,+ -- 这份文档里产生它的那条陈述与它的属性:物化按这一对跳过+ statement_id UUID REFERENCES facts(id) ON DELETE CASCADE,+ predicate_id UUID REFERENCES relation_types(id) ON DELETE CASCADE,+ -- 为什么送去看:结构报的哪一条;空 = 抽样+ flag TEXT CHECK (flag IN ('domain', 'range', 'name_absent', 'no_date')),+ action TEXT NOT NULL CHECK (action IN ('keep', 'retract', 'revise', 'add')),+ reason TEXT NOT NULL DEFAULT '',+ -- 文档的原话:撤、改、加都得引一句;keep 不用+ quote TEXT,+ -- 动作指向的那条事实(改成什么、加什么;撤的就是看的那条),名字与 id 都在,+ -- 队列卡片读名字,执行读 id+ proposed JSONB,+ new_fact_id UUID REFERENCES facts(id) ON DELETE SET NULL,+ status TEXT NOT NULL CHECK (status IN ('applied', 'held', 'refused', 'rejected')),+ -- 闸门的理由(0027 的写法:`derived 2` / `answered 1` / `contradiction CEO of`)或拒绝的理由+ detail TEXT,+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ decided_at TIMESTAMPTZ,+ decided_by UUID REFERENCES users(id) ON DELETE SET NULL+);+CREATE INDEX errata_actions_kb_status_idx ON errata_actions (kb_id, status, created_at);+CREATE INDEX errata_actions_fact_idx ON errata_actions (fact_id) WHERE fact_id IS NOT NULL;+CREATE INDEX errata_actions_statement_idx ON errata_actions (statement_id, predicate_id)+ WHERE status = 'applied' AND action IN ('retract', 'revise');diff --git a/scripts/bench/README.md b/scripts/bench/README.mdindex 52d4ee19..0d8cae27 100644--- a/scripts/bench/README.md+++ b/scripts/bench/README.md@@ -508,6 +508,11 @@ node scripts/bench/typed.mjs --label run2 --judge 200 # 再来一组node scripts/bench/typed.mjs --kb <id> --score # 只重新打分```+加 `--errata` 就在对齐之后排一次勘误 agent(0044 决定 7):报它看了多少、撤改加各几条、留给人几条、+花了多少 token,再打一次分;带 `--judge` 时裁判也判撤掉的行——判 stated 的就是撤错的。0044 §7 的+度量正是这两个数:precision gained against correct facts removed(原型:76.1% → 89.5%,撤 278 条,+约四分之一是对的)。+报的数:gold recall(**同句与跨句分开**,门槛只看同句;跨句的等派生规则)、judged precision(裁判抽样;金标漏标严重,精度只信裁判)、entity-pair recall(开放陈述那一层)、绑定的bound / none / undecided。0044 的门槛:裁判精度不低于完整原型的 75.3%,同句召回不低于完整原型,diff --git a/scripts/bench/typed.mjs b/scripts/bench/typed.mjsindex 71470090..747a7428 100644--- a/scripts/bench/typed.mjs+++ b/scripts/bench/typed.mjs@@ -20,6 +20,8 @@// node scripts/bench/typed.mjs --label run1 --judge 200 # 加裁判抽样 200 条// node scripts/bench/typed.mjs --kb <id> --score # 只对已有的库重新打分// node scripts/bench/typed.mjs --label dry --dry-run # 建库、装本体、灌语料、等解析,不抽取:验管线+// node scripts/bench/typed.mjs --label run1 --judge 200 --errata # 对齐之后再跑勘误 agent,报前后两份分与撤错多少+// node scripts/bench/typed.mjs --kb <id> --score --errata --judge 200 # 已有的库:跑勘误、打分// 环境:BENCH_BASE(默认 http://localhost:1516)、BENCH_EMAIL / BENCH_PASSWORD(lib.mjs)、// BENCH_PSQL(指向应用库的 psql 命令行)、BENCH_JUDGE_BASE / _KEY / _MODEL(裁判端点)@@ -162,6 +164,64 @@ async function align(KB) {if (failed) log(`注意:${failed} 个对齐任务失败(看 jobs.last_error)`);}+// 勘误(0044 决定 7):物化之后排一次 errata_review,等它把每篇文档看完。度量按 errata_runs 与+// errata_actions 报:撤了几条、改了几条、加了几条、留给人几条、拒了几条、花了多少 token+async function errata(KB) {+ psql(`INSERT INTO jobs (kind, payload) VALUES ('errata_review', '{"kb_id":"${KB}"}')`);+ await until(() => {+ const left = num(`SELECT count(*) FROM jobs WHERE kind='errata_review' AND status IN ('queued','running') AND payload->>'kb_id'='${KB}'`);+ const seen = num(`SELECT count(*) FROM errata_actions WHERE kb_id='${KB}'`);+ log(`勘误:${left} 个任务在跑,记了 ${seen} 笔`);+ return left === 0 ? true : seen;+ }, 15000, 30 * 60000);+ const failed = num(`SELECT count(*) FROM jobs WHERE kind='errata_review' AND status='failed' AND payload->>'kb_id'='${KB}'`);+ if (failed) log(`注意:${failed} 个勘误任务失败(看 jobs.last_error)`);+ const count = (where) => num(`SELECT count(*) FROM errata_actions WHERE kb_id='${KB}' AND ${where}`);+ const out = {+ documents: num(`SELECT count(DISTINCT document_id) FROM errata_runs WHERE kb_id='${KB}'`),+ reviewed: count(`fact_id IS NOT NULL`),+ flagged: count(`flag IS NOT NULL`),+ retracted: count(`action='retract' AND status='applied'`),+ revised: count(`action='revise' AND status='applied'`),+ added: count(`action='add' AND status='applied'`),+ held: count(`status='held'`),+ refused: count(`status='refused'`),+ requests: num(`SELECT coalesce(sum(requests),0) FROM errata_runs WHERE kb_id='${KB}'`),+ prompt_tokens: num(`SELECT coalesce(sum(prompt_tokens),0) FROM errata_runs WHERE kb_id='${KB}'`),+ completion_tokens: num(`SELECT coalesce(sum(completion_tokens),0) FROM errata_runs WHERE kb_id='${KB}'`),+ };+ console.log(`勘误看了 ${out.documents} 篇 ${out.reviewed} 条(结构报的 ${out.flagged}):撤 ${out.retracted},改 ${out.revised},加 ${out.added},留给人 ${out.held},拒 ${out.refused};${out.requests} 次请求,token ${out.prompt_tokens}+${out.completion_tokens}`);+ return out;+}++// 撤掉的行里有多少是对的(0044 §7 的另一半:precision gained against correct facts removed):+// 把勘误撤掉的类型化行交给裁判,按原文判 stated 的就是撤错的+async function judgeRetracted(KB) {+ const ep = judgeEndpoint(KB);+ const all = rows(`+ SELECT f.id, d.filename, s.canonical_name, r.label, coalesce(o.canonical_name, f.object_value->>'value', f.object_value#>>'{}', '')+ FROM errata_actions ea JOIN facts f ON f.id=ea.fact_id+ JOIN relation_types r ON r.id=f.predicate_id JOIN entities s ON s.id=f.subject_id LEFT JOIN entities o ON o.id=f.object_id+ JOIN documents d ON d.id=ea.document_id+ WHERE ea.kb_id='${KB}' AND ea.action IN ('retract','revise') AND ea.status='applied' ORDER BY f.id`);+ const text = Object.fromEntries(corpus.docs.map((d) => [d.filename, d.text]));+ const byFile = new Map();+ for (const r of all) (byFile.get(r[1]) || byFile.set(r[1], []).get(r[1])).push(r);+ const counts = { stated: 0, misworded: 0, not_stated: 0, unjudged: 0 };+ for (const [file, items] of byFile) {+ const list = items.map((r, i) => `${i}. ${r[2]} — ${r[3]} — ${r[4]}`).join("\n");+ let verdicts = {};+ try {+ const reply = await chat(ep, [{ role: "system", content: JUDGE }, { role: "user", content: `Document:\n${text[file]}\n\nFacts:\n${list}` }]);+ const m = reply.match(/\{[\s\S]*\}/);+ for (const r of (m ? JSON.parse(m[0]).results : [])) verdicts[r.i] = r.verdict;+ } catch (e) { log(`裁判失败 ${file}: ${String(e).slice(0, 120)}`); }+ items.forEach((_, i) => { counts[["stated", "misworded", "not_stated"].includes(verdicts[i]) ? verdicts[i] : "unjudged"] += 1; });+ }+ console.log(`撤改掉的 ${all.length} 条里裁判判 stated ${counts.stated}(撤错的),misworded ${counts.misworded},not_stated ${counts.not_stated}(原型撤了 278 条,约四分之一是对的)`);+ return { removed: all.length, ...counts };+}+// ---- 打分 ----const norm = (s) => String(s).toLowerCase().replace(/[\s ]+/g, " ").replace(/[.,;:!?"'()\[\]]/g, "").trim();const years = (s) => new Set(String(s).match(/\b1\d{3}\b|\b20\d{2}\b/g) || []);@@ -307,6 +367,17 @@ if (!KB) {}const result = score(KB);if (args.judge) result.judge = await judge(KB, Number(args.judge) || 200, Number(args.seed || 1));+if (args.errata) {+ // 勘误前的分留着,勘误后再打一次:0044 §7 的度量是两份分的差,与撤错了多少+ result.before_errata = { gold_recall: result.gold_recall, gold_recall_same_sentence: result.gold_recall_same_sentence, typed_facts: result.typed_facts, judge: result.judge };+ result.errata = await errata(KB);+ const after = score(KB);+ result.after_errata = { gold_recall: after.gold_recall, gold_recall_same_sentence: after.gold_recall_same_sentence, typed_facts: after.typed_facts };+ if (args.judge) {+ result.after_errata.judge = await judge(KB, Number(args.judge) || 200, Number(args.seed || 1));+ result.errata.removed = await judgeRetracted(KB);+ }+}result.minutes = Math.round((Date.now() - started) / 60000);fs.writeFileSync(OUT, JSON.stringify(result, null, 1));console.log(`结果写到 ${OUT}(${result.minutes} 分钟)`);diff --git a/web/src/api.ts b/web/src/api.tsindex 2dfe65ff..6a6bc086 100644--- a/web/src/api.ts+++ b/web/src/api.ts@@ -479,6 +479,8 @@ export type ReviewQueue =| "defects"// 对齐器两票不一致的签名与类别词(#725,0044 决定 3)| "alignment"+ // 勘误 agent 被闸门拦下、等人答的动作(0044 决定 7)+ | "errata"| "merges"// agent 的每一笔(0025):建议、自动裁决与人的回答| "agent";@@ -502,6 +504,8 @@ export interface ReviewCounts {defects: number;/** 对齐器拿不定的签名与类别词(#725) */alignment: number;+ /** 勘误 agent 留给人的动作(0044 决定 7) */+ errata: number;merges: number;/** agent 写下、等人回答的建议(0025) */agent: number;@@ -755,6 +759,23 @@ export type AlignmentItem =votes: { agent?: { property: string; reading: string | null } | null } | null;decided_at: string;};+/** 勘误 agent 被闸门拦下的一笔(0044 决定 7):它想撤、改或加什么,凭哪句原话,为什么留给人 */+export interface ErrataItem {+ id: string;+ document_id: string;+ document: string;+ action: "retract" | "revise" | "add";+ /** 结构报的理由;空 = 抽样看到的 */+ flag: "domain" | "range" | "name_absent" | "no_date" | null;+ fact_id: string | null;+ /** 动作指向的那条事实:撤的就是看的那条,改的是改成的,加的是加的 */+ proposed: { subject: string; property: string; object: string } | null;+ reason: string;+ quote: string | null;+ /** 闸门的理由:`derived 2` / `answered 1` / `contradiction CEO of`(0027 的写法) */+ detail: string | null;+ created_at: string;+}export interface AlignmentVote {property: string;direction: "forward" | "reverse";@@ -902,7 +923,8 @@ export interface ReviewSummary {| "lowconf"| "violations"| "defects"- | "alignment",+ | "alignment"+ | "errata",QueueWait>;decided: {@@ -2350,6 +2372,12 @@ export const api = {`/api/v1/kbs/${kbId}/review/alignment/rules/${ruleId}`,{ method: "POST", body: JSON.stringify({ approve }) },),+ /** 人答勘误 agent 留下的一笔(0044 决定 7):批了就执行,否了只记 */+ decideErrata: (kbId: string, actionId: string, approve: boolean) =>+ request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/review/errata/${actionId}`, {+ method: "POST",+ body: JSON.stringify({ approve }),+ }),/** 人定一个类别词:类,或没有。它名下的实体换类,短语签名跟着重判 */decideAlignmentKindWord: (kbId: string, kindWord: string, cls: string | null) =>request<{ ok: boolean }>(diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.tsindex 75461758..e81d9e07 100644--- a/web/src/i18n/en.ts+++ b/web/src/i18n/en.ts@@ -1794,6 +1794,7 @@ export const en = {railViolations: "Axioms",railDefects: "Ontology",railAlignment: "Alignment",+ railErrata: "Errata",railDecisions: "Decisions",railMerges: "Merges",railAgent: "Agent",@@ -1960,6 +1961,32 @@ export const en = {alignmentApprove: "Approve rule",alignmentReject: "Reject",alignmentRuleAccepted: "Saved. Implied facts are being computed in the background.",+ // 勘误队列(0044 决定 7)+ errata: "The errata agent held these for you",+ errataHint:+ "After extraction an agent rereads each document's typed facts, structural flags first, and retracts, revises or adds with the document's own words as evidence. An action that would reach outside the graph (a derived fact rests on it, someone asked about it, or it would give a one-value property two values) waits here for a person.",+ errataRetract: "wants to retract",+ errataRevise: "wants to revise to",+ errataAdd: "wants to add",+ errataFlag: (flag: string) =>+ ({+ domain: "flagged: subject outside the property's kinds",+ range: "flagged: object outside the property's kinds",+ name_absent: "flagged: a name not in the document",+ no_date: "flagged: date property without a date",+ })[flag] ?? flag,+ errataHeld: (detail: string) => {+ const [kind, ...rest] = detail.split(" ");+ const value = rest.join(" ");+ if (kind === "derived") return `Held: ${value} derived fact(s) rest on it`;+ if (kind === "answered") return `Held: it was named in ${value} answer(s)`;+ if (kind === "contradiction") return `Held: "${value}" allows one value and would get two`;+ return `Held: ${detail}`;+ },+ errataQuote: "Document says:",+ errataApprove: "Apply",+ errataReject: "Reject",+ errataDecided: "Saved.",alignmentTooMany: (n: number) => `${n} properties could apply; too many to ask the model. Pick one or leave it open.`,alignmentConflict: "This decision conflicts with the current state. Refresh and review it before trying again.",alignmentKindWordBusy: "This kind word is being updated by another operation. Please try again shortly.",diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.tsindex a9e52196..85599626 100644--- a/web/src/i18n/zh.ts+++ b/web/src/i18n/zh.ts@@ -1575,6 +1575,7 @@ export const zh: Strings = {railViolations: "公理",railDefects: "本体",railAlignment: "对齐",+ railErrata: "勘误",railDecisions: "决定",railMerges: "合并",railAgent: "Agent",@@ -1719,6 +1720,32 @@ export const zh: Strings = {alignmentApprove: "批准规则",alignmentReject: "驳回",alignmentRuleAccepted: "已保存,隐含事实正在后台计算。",+ // 勘误队列(0044 决定 7)+ errata: "勘误 agent 留给你的",+ errataHint:+ "抽取之后,一个 agent 按文档复读类型化事实,结构报了的先看,撤、改、加都以文档原话为证据。会牵动图外东西的动作(有派生靠着它、有人问过它、会让只许一个值的属性有两个值)留在这里等人。",+ errataRetract: "想撤掉",+ errataRevise: "想改成",+ errataAdd: "想加上",+ errataFlag: (flag: string) =>+ ({+ domain: "结构报的:主语不在属性允许的类里",+ range: "结构报的:宾语不在属性允许的类里",+ name_absent: "结构报的:名字不在文档里",+ no_date: "结构报的:日期属性没有日期",+ })[flag] ?? flag,+ errataHeld: (detail: string) => {+ const [kind, ...rest] = detail.split(" ");+ const value = rest.join(" ");+ if (kind === "derived") return `留下的原因:有 ${value} 条派生靠着它`;+ if (kind === "answered") return `留下的原因:它在 ${value} 次回答里被提到`;+ if (kind === "contradiction") return `留下的原因:「${value}」只许一个值,这样会有两个`;+ return `留下的原因:${detail}`;+ },+ errataQuote: "文档原话:",+ errataApprove: "执行",+ errataReject: "否",+ errataDecided: "已保存。",alignmentTooMany: (n: number) => `有 ${n} 条属性都可能对得上,多到没法问模型。请选一条或留在开放图谱。`,alignmentConflict: "此决定与当前状态冲突。请刷新并核对后再试。",alignmentKindWordBusy: "这个类别词正在被其他操作更新,请稍后重试。",diff --git a/web/src/pages/Review.tsx b/web/src/pages/Review.tsxindex 8ecc2ea3..9a29ff49 100644--- a/web/src/pages/Review.tsx+++ b/web/src/pages/Review.tsx@@ -12,6 +12,7 @@ import {type ReviewTypeFilter,type OntologyDefect,type AlignmentItem,+ type ErrataItem,type EntityTypeView,type RelationTypeView,type ConflictItem,@@ -707,6 +708,50 @@ function voteText(v: { property: string; direction: string } | string | null | u}/** 对齐器提的一条蕴含规则(0044 决定 3 第五片):这种形状还蕴含哪条属性、宾语怎么来;人批或驳 */+/** 勘误 agent 留给人的一笔(0044 决定 7):哪份文档、想对哪条事实做什么、凭哪句原话、为什么留下 */+function ErrataRow({+ item,+ busy,+ onDecide,+}: {+ item: ErrataItem;+ busy: boolean;+ onDecide: (approve: boolean) => void;+}) {+ const verb =+ item.action === "retract"+ ? S.review.errataRetract+ : item.action === "revise"+ ? S.review.errataRevise+ : S.review.errataAdd;+ const p = item.proposed;+ return (+ <div className="glass rounded-panel p-3">+ <div className="text-small text-ink-2">{item.document}</div>+ <div className="mt-1 text-body">+ <span className="font-medium">{verb}</span>{" "}+ {p ? `${p.subject} —${p.property}→ ${p.object}` : ""}+ </div>+ {item.flag && <div className="mt-1 text-small text-ink-2">{S.review.errataFlag(item.flag)}</div>}+ {item.reason && <div className="mt-1 text-small">{item.reason}</div>}+ {item.quote && (+ <div className="mt-1 text-small text-ink-2">+ {S.review.errataQuote} “{item.quote}”+ </div>+ )}+ {item.detail && <div className="mt-1 text-small text-warn">{S.review.errataHeld(item.detail)}</div>}+ <div className={CARD_ACTIONS}>+ <Button size="sm" disabled={busy} onClick={() => onDecide(true)}>+ {S.review.errataApprove}+ </Button>+ <Button size="sm" disabled={busy} onClick={() => onDecide(false)}>+ {S.review.errataReject}+ </Button>+ </div>+ </div>+ );+}+function AlignmentRuleRow({item,busy,@@ -1180,6 +1225,8 @@ type Sel =| "defects"// 对齐器两票不一致的签名与类别词(#725,0044 决定 3):问的是「这个说法是本体的哪个属性」| "alignment"+ // 勘误 agent 被闸门拦下的动作(0044 决定 7)+ | "errata"// agent 的每一笔(0025):建议等人答,自动裁的可撤。它不是七档之一——// 七档问「这条知识对不对」,这一档问「机器替你办的对不对」| "agent"@@ -1196,6 +1243,7 @@ const QUEUE_FETCHED: ReviewQueue[] = ["violations","defects","alignment",+ "errata","merges","agent",];@@ -1209,6 +1257,7 @@ const QUEUE_ORDER: Sel[] = ["violations","defects","alignment",+ "errata",];/** 有内容区、要翻页的那些档——总览不翻页 */type Paged = Exclude<Sel, "overview">;@@ -1222,6 +1271,7 @@ const PAGE_SIZE: Record<Paged, number> = {violations: FACT_PAGE,defects: FACT_PAGE,alignment: FACT_PAGE,+ errata: FACT_PAGE,merges: MERGE_PAGE,decisions: 20,agent: 20,@@ -1419,6 +1469,13 @@ export function Review() {),onSettled: invalidate,});+ const errataAction = useMutation({+ mutationFn: ({ id, approve }: { id: string; approve: boolean }) =>+ api.decideErrata(kb!.id, id, approve),+ onSuccess: () => toast.success(S.review.errataDecided),+ onError: (e) => toast.error(e instanceof Error ? e.message : String(e)),+ onSettled: invalidate,+ });const violationAction = useMutation({mutationFn: ({id,@@ -1490,6 +1547,7 @@ export function Review() {violations: c?.violations ?? 0,defects: c?.defects ?? 0,alignment: c?.alignment ?? 0,+ errata: c?.errata ?? 0,merges: c?.merges ?? 0,decisions: history.data?.total ?? 0,agent: c?.agent ?? 0,@@ -1504,6 +1562,7 @@ export function Review() {const asViolations = () => rows as AxiomViolation[];const asDefects = () => rows as OntologyDefect[];const asAlignment = () => rows as AlignmentItem[];+ const asErrata = () => rows as ErrataItem[];// 对齐卡片要列本体的类与属性给人选;只在这一档拉const ontology = useQuery({queryKey: ["ontology", kb?.id],@@ -1547,6 +1606,7 @@ export function Review() {},defects: { title: S.review.defects, hint: S.review.defectsHint },alignment: { title: S.review.alignment, hint: S.review.alignmentHint },+ errata: { title: S.review.errata, hint: S.review.errataHint },decisions: { title: S.review.decisionsTitle, hint: S.review.decisionsHint },merges: { title: S.review.mergeHistory, hint: null },agent: { title: S.review.agentTitle, hint: S.review.agentHint },@@ -1627,6 +1687,13 @@ export function Review() {>{S.review.railAlignment}</RailItem>+ <RailItem+ active={active === "errata"}+ count={counts.errata}+ onClick={() => select("errata")}+ >+ {S.review.railErrata}+ </RailItem>{/* agent 的队列(0025):徽标是等人回答的建议数 */}<RailItemactive={active === "agent"}@@ -1949,6 +2016,24 @@ export function Review() {</div>)}+ {active === "errata" && (+ <div className="space-y-3">+ {counts.errata === 0 && (+ <div className="glass rounded-panel p-8 text-center text-body text-ink-2">+ {S.review.categoryEmpty}+ </div>+ )}+ {asErrata().map((item) => (+ <ErrataRow+ key={item.id}+ item={item}+ busy={errataAction.isPending && errataAction.variables?.id === item.id}+ onDecide={(approve) => errataAction.mutate({ id: item.id, approve })}+ />+ ))}+ </div>+ )}+{active === "alignment" && (<div className="space-y-3">{counts.alignment === 0 && (