Skip to content

World's first open-source enterprise world model.

Read a phrase reply in whatever shape the model wrote it, and ask again a bounded number of times (#895)

WaylandYangb6ae250253d7
diff --git a/crates/utopia-extract/src/phrase_align.rs b/crates/utopia-extract/src/phrase_align.rs
index fef87fdd..4c9a40e8 100644
--- a/crates/utopia-extract/src/phrase_align.rs
+++ b/crates/utopia-extract/src/phrase_align.rs
@@ -13,6 +13,14 @@
//! 回复是紧凑 JSON:`{"b": [[id, "key" | null, "forward" | "reverse" | null]]}`。解析同
//! 类别词那边:坏的一条计数、不毁掉整批;键不在候选里、id 不在批里、绑了却没方向、
//! 同一个 id 的第二次都算坏;没答到的 id 是「再问」,不是 null。
+//!
+//! **形状也宽容**(同类别词那边的教训):模型(实测 DeepSeek-V3.2)并不总照样例写。它会
+//! 把整段答成按 id 作键的对象(`{"0": ["headquartered_in", "forward"], "1": null}`),
+//! 会把一条写成 `{"id": 0, "key": ..., "direction": ...}`,会把 `b` 写成对象、不要外层
+//! 对象只给数组、或在值里先抄一遍 id。这些说的都是同一件事,读法只有一种;温度为零时
+//! 同一段提示词回来的形状还是同一个,读不出就是每一轮都读不出——从前这种回复解出来是
+//! 「零条、零坏」,调用方当成「有一票没答到」静静跳过,没有日志也不再问。读不出的键与
+//! id 算坏项,不当缺席。
use std::collections::{HashMap, HashSet};
@@ -179,15 +187,11 @@ pub fn parse_phrase_response(
) -> anyhow::Result<(Vec<PhraseChoice>, usize)> {
let value = parse_value(raw)?;
let by_id: HashMap<i64, &PhraseItem<'_>> = items.iter().map(|i| (i.id, i)).collect();
- let triples = value
- .get("b")
- .and_then(Value::as_array)
- .map_or(&[][..], Vec::as_slice);
let mut choices = Vec::new();
let mut malformed = 0usize;
let mut seen = HashSet::new();
- for t in triples {
- match parse_triple(t, &by_id) {
+ for (id, key, direction) in answers(&value) {
+ match parse_triple(&id, &key, &direction, &by_id) {
Some(choice) if seen.insert(choice.id) => choices.push(choice),
_ => malformed += 1,
}
@@ -195,21 +199,113 @@ pub fn parse_phrase_response(
Ok((choices, malformed))
}
-/// `[id, key | null, direction | null]`:绑了就得有方向,方向不认识算坏
-fn parse_triple(v: &Value, by_id: &HashMap<i64, &PhraseItem<'_>>) -> Option<PhraseChoice> {
- let arr = v.as_array()?;
- if arr.len() < 2 {
- return None;
+/// 回复里的每一条答案:(id,键,方向)三个原始值,形状还没验。
+///
+/// 认这几种写法,说的都是「这个 id 选了这个键、这个方向」:`{"b": [[id, key, dir]]}`
+/// (样例)、`{"b": {"id": [key, dir]}}`、没有 `b` 的顶层对象 `{"id": [key, dir]}`、
+/// 顶层数组 `[[id, key, dir]]`;一条也可以写成 `{"id": .., "key": .., "direction": ..}`,
+/// 按 id 作键时值也可以是 `{"key": .., "direction": ..}`、`null`(不绑)或先抄一遍 id 的
+/// `[id, key, dir]`。`b` 在就只看 `b`,顶层别的键是模型的旁白,不是答案。缺了 `b` 又
+/// 不是对象或数组的,一条都没有
+fn answers(value: &Value) -> Vec<(Value, Value, Value)> {
+ let listed = value.get("b").unwrap_or(value);
+ match listed {
+ Value::Array(entries) => entries
+ .iter()
+ .map(|entry| match entry {
+ Value::Array(list) if list.len() >= 2 => {
+ let (key, direction) = key_and_direction(&list[1..]);
+ (list[0].clone(), key, direction)
+ }
+ Value::Object(map) => {
+ let (key, direction) = fields(map);
+ (
+ map.get("id").cloned().unwrap_or(Value::Null),
+ key,
+ direction,
+ )
+ }
+ other => (other.clone(), Value::Null, Value::Null),
+ })
+ .collect(),
+ Value::Object(map) => map
+ .iter()
+ .map(|(id, v)| {
+ let (key, direction) = match v {
+ Value::Array(list) => {
+ // 值里先抄一遍 id 再给键:`{"0": [0, "headquartered_in", "forward"]}`
+ let copied = list
+ .first()
+ .and_then(item_id)
+ .is_some_and(|first| id.trim().parse::<i64>().ok() == Some(first));
+ let list = if copied { &list[1..] } else { &list[..] };
+ key_and_direction(list)
+ }
+ Value::Object(inner) => fields(inner),
+ other => (other.clone(), Value::Null),
+ };
+ (Value::String(id.clone()), key, direction)
+ })
+ .collect(),
+ _ => Vec::new(),
+ }
+}
+
+/// `[key, dir]` 的两格;只有一格就没有方向(null 的答案常只写一格)。空数组是「什么
+/// 都没写」,读成 null 键会把没答到说成不绑——留成对象,调用方算坏
+fn key_and_direction(list: &[Value]) -> (Value, Value) {
+ match list {
+ [] => (Value::Array(Vec::new()), Value::Null),
+ [key] => (key.clone(), Value::Null),
+ [key, direction, ..] => (key.clone(), direction.clone()),
}
- let id = item_id(&arr[0])?;
+}
+
+/// 一条写成对象时的两个字段:键叫 key / property / p,方向叫 direction / dir / d
+fn fields(map: &serde_json::Map<String, Value>) -> (Value, Value) {
+ let pick = |names: &[&str]| {
+ names
+ .iter()
+ .find_map(|n| map.get(*n))
+ .cloned()
+ .unwrap_or(Value::Null)
+ };
+ (
+ pick(&["key", "property", "p"]),
+ pick(&["direction", "dir", "d"]),
+ )
+}
+
+/// 一条答案:id 得是这批里的;键是 null(不绑)或候选里的一个,绑了就得有方向,方向
+/// 不认识算坏。键包在数组里的(`["headquartered_in", "forward"]` 塞在第二格)照样读,
+/// 方向从数组里取
+fn parse_triple(
+ id: &Value,
+ key: &Value,
+ direction: &Value,
+ by_id: &HashMap<i64, &PhraseItem<'_>>,
+) -> Option<PhraseChoice> {
+ let id = item_id(id)?;
let item = by_id.get(&id)?;
- let property = match &arr[1] {
+ let (key, direction) = match key {
+ Value::Array(list) if !list.is_empty() => {
+ let (k, d) = key_and_direction(list);
+ let d = if d.is_null() { direction.clone() } else { d };
+ (k, d)
+ }
+ other => (other.clone(), direction.clone()),
+ };
+ let property = match &key {
Value::Null => None,
Value::String(written) => {
let key = candidate_key(item, written)?;
- let direction = match arr.get(2).and_then(Value::as_str).map(str::trim) {
- Some("forward") | Some("Forward") => Direction::Forward,
- Some("reverse") | Some("Reverse") => Direction::Reverse,
+ let direction = match direction
+ .as_str()
+ .map(|d| d.trim().to_lowercase())
+ .as_deref()
+ {
+ Some("forward") => Direction::Forward,
+ Some("reverse") => Direction::Reverse,
_ => return None,
};
Some((key, direction))
@@ -402,4 +498,165 @@ mod tests {
assert_eq!(choices.len(), 1);
assert_eq!(choices[0].id, 0);
}
+
+ /// 三条签名:owns(应反向绑 subsidiary_of)、said(应 null)、revenue(值)
+ fn three_items<'a>(examples: &'a [String], quotes: &'a [String]) -> Vec<PhraseItem<'a>> {
+ let mk = |id: i64, phrase: &'static str, value: bool| PhraseItem {
+ id,
+ phrase,
+ subject_class: Some("organization"),
+ object_class: (!value).then_some("organization"),
+ object_is_value: value,
+ statement_count: 1,
+ examples,
+ quotes,
+ candidates: candidates(),
+ };
+ vec![
+ mk(0, "owns", false),
+ mk(1, "said", false),
+ mk(2, "revenue", true),
+ ]
+ }
+
+ fn bound(id: i64, key: &str, direction: Direction) -> PhraseChoice {
+ PhraseChoice {
+ id,
+ property: Some((key.to_string(), direction)),
+ }
+ }
+
+ fn unbound(id: i64) -> PhraseChoice {
+ PhraseChoice { id, property: None }
+ }
+
+ /// 类别词那边实测的写法搬到短语上:整段是按 id 作键的对象,没有 `b`,值是
+ /// `[key, dir]`。解出来必须和样例形状一模一样
+ #[test]
+ fn an_id_keyed_object_with_pairs_is_read_as_the_triple_list() {
+ let (examples, quotes) = (strings(&[]), strings(&[]));
+ let items = three_items(&examples, &quotes);
+ let raw = r#"{ "0": ["subsidiary_of", "reverse"], "1": [null, null], "2": ["revenue", "forward"] }"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(
+ choices,
+ vec![
+ bound(0, "subsidiary_of", Direction::Reverse),
+ unbound(1),
+ bound(2, "revenue", Direction::Forward),
+ ]
+ );
+ // 不绑的写成裸 null 或只有一格;绑了却只有一格是没方向,算坏
+ let raw = r#"{"0": ["subsidiary_of"], "1": null, "2": [null]}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 1, "bound without a direction");
+ assert_eq!(choices, vec![unbound(1), unbound(2)]);
+ // 值里先抄一遍 id 再给键与方向
+ let raw = r#"{"0": [0, "subsidiary_of", "reverse"], "1": [1, null, null]}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(
+ choices,
+ vec![bound(0, "subsidiary_of", Direction::Reverse), unbound(1)]
+ );
+ // 空数组不是一个答案
+ let raw = r#"{"0": [], "1": [null]}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 1);
+ assert_eq!(choices, vec![unbound(1)]);
+ }
+
+ /// 按 id 作键、值是对象:`{"key": .., "direction": ..}`,字段名的几种别名都认
+ #[test]
+ fn an_id_keyed_object_with_object_values_parses() {
+ let (examples, quotes) = (strings(&[]), strings(&[]));
+ let items = three_items(&examples, &quotes);
+ let raw = r#"{"0": {"key": "subsidiary_of", "direction": "Reverse"}, "1": {"key": null, "direction": null}, "2": {"property": "revenue", "dir": "FORWARD"}}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(
+ choices,
+ vec![
+ bound(0, "subsidiary_of", Direction::Reverse),
+ unbound(1),
+ bound(2, "revenue", Direction::Forward),
+ ]
+ );
+ }
+
+ /// `b` 下面是对象而不是数组:一样读;`b` 在就只看 `b`,顶层别的键是旁白
+ #[test]
+ fn b_as_an_object_parses_and_other_top_level_keys_are_ignored() {
+ let (examples, quotes) = (strings(&[]), strings(&[]));
+ let items = three_items(&examples, &quotes);
+ let raw = r#"{"note": "done", "b": {"0": ["subsidiary_of", "reverse"], "1": null}}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(
+ choices,
+ vec![bound(0, "subsidiary_of", Direction::Reverse), unbound(1)]
+ );
+ let raw = r#"{"b": {"0": {"key": "subsidiary_of", "direction": "reverse"}}}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(choices, vec![bound(0, "subsidiary_of", Direction::Reverse)]);
+ }
+
+ /// 数组里的一条写成对象、或整段不要外层对象只给数组:照读
+ #[test]
+ fn object_triples_and_a_bare_array_parse() {
+ let (examples, quotes) = (strings(&[]), strings(&[]));
+ let items = three_items(&examples, &quotes);
+ let raw = r#"{"b": [{"id": 0, "key": "subsidiary_of", "direction": "reverse"}, {"id": "1", "key": null, "direction": null}]}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(
+ choices,
+ vec![bound(0, "subsidiary_of", Direction::Reverse), unbound(1)]
+ );
+ let raw = concat!(
+ "```json\n",
+ r#"[[0, "subsidiary_of", "reverse"], [2, "revenue", "forward"]]"#,
+ "\n```"
+ );
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(
+ choices,
+ vec![
+ bound(0, "subsidiary_of", Direction::Reverse),
+ bound(2, "revenue", Direction::Forward)
+ ]
+ );
+ // 样例形状里第二格又包了一层:`[id, [key, dir]]`
+ let raw = r#"{"b": [[0, ["subsidiary_of", "reverse"]], [1, [null]]]}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert_eq!(malformed, 0);
+ assert_eq!(
+ choices,
+ vec![bound(0, "subsidiary_of", Direction::Reverse), unbound(1)]
+ );
+ }
+
+ /// `b` 是空的:一项都没答,不是坏项。没有 `b`、键又不是 id 的:那是读不出的答案,
+ /// 算坏项——从前这种回复算「一项都没答」,调用方静静跳过,什么痕迹都不留
+ #[test]
+ fn an_empty_b_answers_nothing_but_an_unreadable_reply_is_malformed() {
+ let (examples, quotes) = (strings(&[]), strings(&[]));
+ let items = three_items(&examples, &quotes);
+ for raw in [r#"{"b": []}"#, r#"{"b": {}}"#] {
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert!(choices.is_empty(), "{raw}");
+ assert_eq!(malformed, 0, "{raw}");
+ }
+ let raw = r#"{"answer": "subsidiary_of", "direction": "reverse"}"#;
+ let (choices, malformed) = parse_phrase_response(raw, &items).unwrap();
+ assert!(choices.is_empty());
+ assert_eq!(malformed, 2);
+ // `b` 是标量:一条都没有
+ let (choices, malformed) = parse_phrase_response(r#"{"b": 1}"#, &items).unwrap();
+ assert!(choices.is_empty());
+ assert_eq!(malformed, 0);
+ }
}
diff --git a/crates/utopia-server/src/main.rs b/crates/utopia-server/src/main.rs
index 7db51c81..bdaad212 100644
--- a/crates/utopia-server/src/main.rs
+++ b/crates/utopia-server/src/main.rs
@@ -596,7 +596,14 @@ async fn dispatch(st: &state::AppState, job: &utopia_store::jobs::Job) -> anyhow
.and_then(|v| v.as_str())
.and_then(|s| s.parse().ok())
.ok_or_else(|| anyhow::anyhow!("payload 缺少 kb_id"))?;
- phrase_alignment::align_phrases(st, kb_id).await
+ // 自己再排的那份带着第几次;文档、本体、类别词对齐排的没有这一项
+ let reask = job
+ .payload
+ .get("reask")
+ .and_then(|v| v.as_u64())
+ .and_then(|n| u32::try_from(n).ok())
+ .unwrap_or(0);
+ phrase_alignment::align_phrases_reasking(st, kb_id, reask).await
}
"resolve_time" => {
let id = payload_document_id(&job.payload)?;
diff --git a/crates/utopia-server/src/phrase_alignment.rs b/crates/utopia-server/src/phrase_alignment.rs
index a8bdb39b..09c56a42 100644
--- a/crates/utopia-server/src/phrase_alignment.rs
+++ b/crates/utopia-server/src/phrase_alignment.rs
@@ -156,8 +156,30 @@ fn consider<'a>(
.collect()
}
+/// 日志里放得下的一段回复:空白折成一个空格,最多这么多字符。
+const SNIPPET_CHARS: usize = 240;
+
+fn snippet(text: &str) -> String {
+ let flat = text.split_whitespace().collect::<Vec<_>>().join(" ");
+ let mut out: String = flat.chars().take(SNIPPET_CHARS).collect();
+ if flat.chars().count() > SNIPPET_CHARS {
+ out.push('…');
+ }
+ out
+}
+
+/// 一轮里没判完的(调用失败、回复读不出、模型漏答)自己再排几次;超过这个数就等
+/// 下一篇文档或本体的改动再问。不设上限的话,温度为零下一段每次都读不出的回复会让
+/// 任务每隔几十秒把同一段提示词再送一遍,没有尽头(同类别词对齐)
+pub(crate) const MAX_REASK: u32 = 3;
+
/// 对一个库跑一遍:新出现的和过期的签名各判一次。
-pub async fn align_phrases(state: &AppState, kb_id: Uuid) -> anyhow::Result<()> {
+/// `reask` 是这份任务已经是第几次自己排的(文档、本体、类别词对齐排的是 0)。
+pub async fn align_phrases_reasking(
+ state: &AppState,
+ kb_id: Uuid,
+ reask: u32,
+) -> anyhow::Result<()> {
let pool = &state.pool;
let kb = utopia_store::kbs::get(pool, kb_id).await?;
let settings = utopia_store::settings::get(pool, kb.workspace_id)
@@ -178,7 +200,7 @@ pub async fn align_phrases(state: &AppState, kb_id: Uuid) -> anyhow::Result<()>
tracing::info!(%kb_id, "短语对齐已有一份在跑,这次跳过");
return Ok(());
}
- let result = align_phrases_locked(state, kb_id, &settings, &client).await;
+ let result = align_phrases_locked(state, kb_id, reask, &settings, &client).await;
let _ = sqlx::query("SELECT pg_advisory_unlock(hashtext('align_phrases'), hashtext($1))")
.bind(kb_id.to_string())
.execute(&mut *guard)
@@ -189,6 +211,7 @@ pub async fn align_phrases(state: &AppState, kb_id: Uuid) -> anyhow::Result<()>
async fn align_phrases_locked(
state: &AppState,
kb_id: Uuid,
+ reask: u32,
settings: &utopia_core::models::LlmSettings,
client: &utopia_llm::LlmClient,
) -> anyhow::Result<()> {
@@ -252,6 +275,8 @@ async fn align_phrases_locked(
let (mut bound, mut none, mut undecided, mut skipped) = (0usize, 0usize, 0usize, 0usize);
// 调用或解析失败的批次:这轮跳过,结束时自己再排一次
let mut failed = 0usize;
+ // 问了、模型也答了、却没答到的签名:两票缺一票就不下结论
+ let mut unanswered = 0usize;
for batch in todo.chunks(BATCH) {
// 候选超过上限的不问模型:记成 undecided 交给人,指纹照记——属性少下去指纹就变,
// 到时再问。从前超限和无候选一样静默跳过,签名永远排着又永远不可执行(#807)
@@ -339,6 +364,24 @@ async fn align_phrases_locked(
}
};
skipped += malformed;
+ if choices.is_empty() {
+ // 解出来了却一条都没读到:回复的形状不是我们认得的。这和解析失败是一回事,
+ // 按失败算、留到下次。从前这里什么都不说,每一条签名都当「有一票没答到」
+ // 静静跳过,日志里只有一串「完成 bound=0」——回复的开头要进日志,下次才
+ // 知道它长什么样(同类别词对齐)
+ tracing::warn!(
+ %kb_id,
+ pass,
+ items = items.len(),
+ malformed,
+ finish_reason = ?reply.finish_reason,
+ chars = reply.text.chars().count(),
+ reply = %snippet(&reply.text),
+ "短语对齐回复读不出一条,这一批留到下次"
+ );
+ failed += 1;
+ continue;
+ }
for c in choices {
let Ok(i) = usize::try_from(c.id) else {
continue;
@@ -398,6 +441,7 @@ async fn align_phrases_locked(
let (ans_a, ans_b) = answered[i];
if !ans_a || !ans_b {
// 有一票没答到:不下结论,下次再问
+ unanswered += 1;
continue;
}
let show = |v: &Vote| {
@@ -469,7 +513,10 @@ async fn align_phrases_locked(
}
}
}
- tracing::info!(%kb_id, bound, none, undecided, skipped, failed, "短语对齐完成");
+ tracing::info!(%kb_id, bound, none, undecided, skipped, failed, unanswered, "短语对齐完成");
+ if unanswered > 0 {
+ tracing::warn!(%kb_id, unanswered, "短语对齐有签名模型没答到,这些签名这轮没有结论");
+ }
// 提规则(0044 决定 3 第五片):本轮刚判过的签名,和带类别词的东西,问模型「这种形状
// 还蕴含什么」。只问本轮判过的:指纹没变的形状上一轮已经问过,答案(提案或代理驳回)
// 还在 implication_rules 里;指纹变了它就在 todo 里,自然再问
@@ -565,11 +612,11 @@ async fn align_phrases_locked(
state.emit_graph(kb_id);
}
// 这一轮跑着的时候世界没停:新文档带来新签名,改了的属性、动了的父边让刚判的绑定
- // 过期,本轮没排上的触发也都落在这里。有失败的批次、有没试过的新签名、有本轮判完
- // 指纹又变了的绑定(请求途中的编辑,#795),就再排一次。
+ // 过期,本轮没排上的触发也都落在这里。有没试过的新签名、有本轮判完指纹又变了的绑定
+ // (请求途中的编辑,#795),就再排一次(从头算一份,新签名换了提示词)。
// 只看**活着的**签名:端点的类换了,旧签名的行没有陈述可判,它永远「过期」却永远
// 不可执行——从前 `stale` 把这种孤儿每轮交回来,一条孤儿排一次 job,三轮三次(#807)
- let again = failed > 0 || {
+ let changed = {
// **重新加载**,不是拿开跑时的快照比:快照就是判定写下的那份指纹,跟它比永远
// 相等。模型答着的时候改了定义(#795)、加了父边、来了新文档,只有再读一遍才看得见
let props = utopia_store::ontology::relation_type_views(pool, kb_id).await?;
@@ -591,13 +638,30 @@ async fn align_phrases_locked(
}
})
};
- if again {
+ // 本轮没判完的(调用失败、回复读不出、模型漏答了几条)自己再排,最多 MAX_REASK 次,
+ // 每次多等一会。从前只有失败的批次会再排,读不出的回复解成「零条、零坏」不算失败,
+ // 漏答的签名就只能等下一篇文档来排——最后一篇之后没有下一篇,它们就永远没有结论;
+ // 而漏答不写任何行,审核队列也看不见(同类别词对齐)
+ let unfinished = failed > 0 || unanswered > 0;
+ if changed {
utopia_store::jobs::enqueue_unless_queued(
pool,
"align_phrases",
serde_json::json!({ "kb_id": kb_id }),
)
.await?;
+ } else if unfinished && reask < MAX_REASK {
+ let delay = std::time::Duration::from_secs(20 * u64::from(reask + 1));
+ tracing::info!(%kb_id, failed, unanswered, reask = reask + 1, delay_secs = delay.as_secs(), "短语对齐没判完,稍后再问");
+ utopia_store::jobs::enqueue_unless_pending(
+ pool,
+ "align_phrases",
+ serde_json::json!({ "kb_id": kb_id, "reask": reask + 1 }),
+ delay,
+ )
+ .await?;
+ } else if unfinished {
+ tracing::warn!(%kb_id, failed, unanswered, reask, "短语对齐问了几轮仍没判完,等下一篇文档或本体改动再问");
}
Ok(())
}
diff --git a/crates/utopia-server/src/phrase_alignment_tests.rs b/crates/utopia-server/src/phrase_alignment_tests.rs
index 1edd42b9..874ed63f 100644
--- a/crates/utopia-server/src/phrase_alignment_tests.rs
+++ b/crates/utopia-server/src/phrase_alignment_tests.rs
@@ -149,7 +149,7 @@ impl Fx {
*self.model.replies.lock().unwrap() = replies;
}
async fn run(&self) -> anyhow::Result<()> {
- align_phrases(&self.state, self.kb).await
+ align_phrases_reasking(&self.state, self.kb, 0).await
}
fn requests(&self) -> Vec<Value> {
self.model.requests.lock().unwrap().clone()
@@ -418,7 +418,7 @@ async fn an_edit_during_the_model_request_leaves_the_decision_stale() -> anyhow:
f.script(none());
let state = f.state.clone();
let kb = f.kb;
- let worker = tokio::spawn(async move { align_phrases(&state, kb).await });
+ let worker = tokio::spawn(async move { align_phrases_reasking(&state, kb, 0).await });
tokio::time::timeout(std::time::Duration::from_secs(10), f.model.entered.notified()).await?;
// 模型还在答,定义改了:两票读的都是旧定义
sqlx::query("UPDATE relation_types SET description='NEW definition', updated_at=clock_timestamp() WHERE id=$1")
@@ -457,7 +457,7 @@ async fn a_person_decision_made_during_the_request_is_not_overwritten() -> anyho
f.script(none());
let state = f.state.clone();
let kb = f.kb;
- let worker = tokio::spawn(async move { align_phrases(&state, kb).await });
+ let worker = tokio::spawn(async move { align_phrases_reasking(&state, kb, 0).await });
tokio::time::timeout(
std::time::Duration::from_secs(10),
f.model.entered.notified(),
@@ -495,3 +495,86 @@ async fn a_person_decision_made_during_the_request_is_not_overwritten() -> anyho
f.cleanup().await?;
run
}
+
+/// 类别词那边 bench 里「12 篇文档一个词都没绑上」的回复形状搬到短语上:DeepSeek-V3.2
+/// 把整段答成按 id 作键的对象,第二票把 `b` 写成对象、值写成 `{"key","direction"}`。
+/// 两票都得读出来,签名才绑得上
+#[tokio::test]
+async fn an_id_keyed_reply_still_binds_the_phrase() -> anyhow::Result<()> {
+ let Some(f) = Fx::new().await? else {
+ return Ok(());
+ };
+ let run = async {
+ f.script(vec![
+ json!({"0": ["based_in", "forward"]}),
+ json!({"b": {"0": {"key": "based_in", "direction": "forward"}}}),
+ nothing_implied(),
+ ]);
+ f.run().await?;
+ assert_eq!(f.requests().len(), 2, "two votes, both read");
+ let b = f.binding().await?;
+ assert_eq!(
+ (b.status.as_str(), b.decided_by.as_str()),
+ ("bound", "agent")
+ );
+ assert_eq!(b.relation_type_id, Some(f.based_in));
+ assert_eq!(b.direction.as_deref(), Some("forward"));
+ assert_eq!(f.typed().await?, 1, "the projection follows");
+ assert!(!f.requeued().await?, "nothing left to ask");
+ anyhow::Ok(())
+ }
+ .await;
+ f.cleanup().await?;
+ run
+}
+
+/// 读不出的回复不再是「一项都没答」然后没了:这一轮不下结论、不写任何行,任务自己
+/// 再排一份(带 reask),排够 MAX_REASK 次就停,等下一篇文档或本体改动
+#[tokio::test]
+async fn an_unreadable_reply_is_asked_again_a_bounded_number_of_times() -> anyhow::Result<()> {
+ let Some(f) = Fx::new().await? else {
+ return Ok(());
+ };
+ let run = async {
+ f.script(vec![
+ json!({"answer": "based_in", "direction": "forward"}),
+ json!({"answer": "based_in", "direction": "forward"}),
+ json!({"answer": "based_in", "direction": "forward"}),
+ json!({"answer": "based_in", "direction": "forward"}),
+ ]);
+ f.run().await?;
+ assert_eq!(f.requests().len(), 2, "two votes, neither readable");
+ assert!(
+ phrase_bindings::bindings(&f.pool, f.kb).await?.is_empty(),
+ "an unreadable reply writes no decision"
+ );
+ let reasks: Vec<(String, Option<i64>)> = sqlx::query_as(
+ "SELECT status, (payload->>'reask')::bigint FROM jobs
+ WHERE kind='align_phrases' AND payload->>'kb_id'=$1 ORDER BY id",
+ )
+ .bind(f.kb.to_string())
+ .fetch_all(&f.pool)
+ .await?;
+ assert_eq!(
+ reasks,
+ vec![("queued".to_string(), Some(1))],
+ "after the first round it queues itself once with reask=1"
+ );
+ f.clear_jobs().await?;
+ // 已经是最后一次自己排的:不再排
+ align_phrases_reasking(&f.state, f.kb, MAX_REASK).await?;
+ assert_eq!(f.requests().len(), 4, "the last allowed round still asks");
+ let after: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM jobs WHERE kind='align_phrases' AND payload->>'kb_id'=$1",
+ )
+ .bind(f.kb.to_string())
+ .fetch_one(&f.pool)
+ .await?;
+ assert_eq!(after, 0, "past MAX_REASK it stops queueing itself");
+ assert!(phrase_bindings::bindings(&f.pool, f.kb).await?.is_empty());
+ anyhow::Ok(())
+ }
+ .await;
+ f.cleanup().await?;
+ run
+}