World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
fix(chat): emit one earned terminal per turn (#903)
Danielf4ea6a8bbe62
diff --git a/crates/utopia-server/src/api/chat.rs b/crates/utopia-server/src/api/chat.rsindex be9f6a32..d126a22c 100644--- a/crates/utopia-server/src/api/chat.rs+++ b/crates/utopia-server/src/api/chat.rs@@ -39,6 +39,11 @@ const KNOWN_ENTITY_LIMIT: usize = 20;const MAX_HISTORY: usize = 20;const MAX_ROUNDS: usize = 6;+enum ProducerEvent {+ Progress(Frame),+ Outcome(Result<Uuid, String>),+}+/// `remember` 曾整个停用过一段(见 `docs/decisions/0015`):它那时会把一句话直接/// 变成图上一条活边,实测里「记住 Acme 把总部搬到了深圳」落成的是一条**空谓词、/// 0.9 置信**的边,而助手宣称的和图里得到的不是一回事。@@ -687,7 +692,7 @@ pub async fn chat(}// 会话 id 先行下发(新会话由此告知前端)- yield Frame::new("conversation", json!({ "id": conversation_id }).to_string());+ yield ProducerEvent::Progress(Frame::new("conversation", json!({ "id": conversation_id }).to_string()));// 循环是 rig 的(#546):工具、策略钩子、历史、实体清单都交给它;// 这里只把它的事件翻成前端认得的帧,并在结束时落库@@ -749,14 +754,14 @@ pub async fn chat(// validation after streaming cannot retract protocol garbage.if shared.finalizing() {if turn_text.len().saturating_add(t.text.len()) > agent::MAX_FINAL_ANSWER_BYTES {- yield error_event("Model final answer exceeded the size limit");+ yield ProducerEvent::Outcome(Err("Model final answer exceeded the size limit".into()));return;}turn_text.push_str(&t.text);} else {answer_acc.push_str(&t.text);turn_text.push_str(&t.text);- yield delta_event(&t.text);+ yield ProducerEvent::Progress(delta_event(&t.text));}}Ok(MultiTurnStreamItem::StreamAssistantItem(StreamedAssistantContent::ToolCall {@@ -779,7 +784,7 @@ pub async fn chat(// 工具轮带了叙述文本:与后续轮次的正文之间补一个段落分隔if !turn_text.is_empty() {answer_acc.push_str("\n\n");- yield delta_event("\n\n");+ yield ProducerEvent::Progress(delta_event("\n\n"));}exchange_acc.push(json!({"role": "assistant",@@ -815,7 +820,7 @@ pub async fn chat(obj.insert("at".into(), json!(answer_acc.encode_utf16().count()));}steps_acc.push(step.clone());- yield Frame::new("step", serde_json::to_string(&step).unwrap_or_default());+ yield ProducerEvent::Progress(Frame::new("step", serde_json::to_string(&step).unwrap_or_default()));}// cite() only appends: document reads can add citations too, regardless// of the UI step kind. Release the sink before yielding to subscribers.@@ -829,7 +834,7 @@ pub async fn chat(}};if let Some(sources) = sources {- yield Frame::new("sources", serde_json::to_string(&sources).unwrap_or_else(|_| "[]".into()));+ yield ProducerEvent::Progress(Frame::new("sources", serde_json::to_string(&sources).unwrap_or_else(|_| "[]".into())));}let mut recorded = tool_result_message(tool_result.call.as_str(), &text);if let Some(failed) = is_error { recorded["is_error"] = json!(failed); }@@ -840,7 +845,7 @@ pub async fn chat(Ok(MultiTurnStreamItem::ModelTurnRetried { .. }) => {if !turn_text.is_empty() {answer_acc.push_str("\n\n");- yield delta_event("\n\n");+ yield ProducerEvent::Progress(delta_event("\n\n"));}turn_text.clear();turn_calls.clear();@@ -873,7 +878,7 @@ pub async fn chat(}return;}- yield error_event(&message);+ yield ProducerEvent::Outcome(Err(message));return;}}@@ -893,23 +898,23 @@ pub async fn chat(};match finalization::answer(&client, input).await {Ok(answer) => { turn_text = answer; turn_calls.clear(); finished = true; }- Err(e) => { yield error_event(&format!("Model could not produce a final answer: {e}")); return; }+ Err(e) => { yield ProducerEvent::Outcome(Err(format!("Model could not produce a final answer: {e}"))); return; }}}if !finished {- yield error_event("LLM stream ended unexpectedly");+ yield ProducerEvent::Outcome(Err("LLM stream ended unexpectedly".into()));return;}// Check the terminal candidate, not earlier narration. The hook is the// policy boundary; this is the last guard before publication and storage.if shared.finalizing() {if let Some(reason) = agent::finalization_error(&turn_text, !turn_calls.is_empty(), &query) {- yield error_event(reason);+ yield ProducerEvent::Outcome(Err(reason.into()));return;}answer_acc.push_str(&turn_text);} else if turn_text.trim().is_empty() {- yield error_event("Model returned an empty answer");+ yield ProducerEvent::Outcome(Err("Model returned an empty answer".into()));return;}let (sources, resolved) = {@@ -925,14 +930,17 @@ pub async fn chat(tool_exchange: serde_json::Value::Array(exchange_acc),},).await;- if let Err(error) = saved {- tracing::error!(%error, %conversation_id, "Could not persist final answer");- yield error_event("Could not save the answer. Please try again later.");- return;- }- yield Frame::new("sources", serde_json::to_string(&sources).unwrap_or_else(|_| "[]".into()));- if shared.finalizing() { yield delta_event(&turn_text); }- yield done_event();+ let saved_id = match saved {+ Ok(id) => id,+ Err(error) => {+ tracing::error!(%error, %conversation_id, "Could not persist final answer");+ yield ProducerEvent::Outcome(Err("Could not save the answer. Please try again later.".into()));+ return;+ }+ };+ yield ProducerEvent::Progress(Frame::new("sources", serde_json::to_string(&sources).unwrap_or_else(|_| "[]".into())));+ if shared.finalizing() { yield ProducerEvent::Progress(delta_event(&turn_text)); }+ yield ProducerEvent::Outcome(Ok(saved_id));};// 生成登记在案,然后**这条连接也只是去「接上」它**——与刷新之后@@ -941,11 +949,34 @@ pub async fn chat(let attached = live.attach(conversation_id).await;tokio::spawn(async move {let mut producer = std::pin::pin!(producer);- while let Some(frame) = producer.next().await {+ let mut outcome = None;+ while let Some(event) = producer.next().await {// 没有订阅者是常态(人走了)。**照发不误**:这里中断就等于// 把「切走一次丢一个回答」原样搬回来- handle.emit(frame).await;+ match event {+ ProducerEvent::Progress(frame) => {+ if matches!(frame.event, "done" | "error") {+ outcome = Some(Err("Producer sent a terminal as progress".into()));+ break;+ }+ handle.emit(frame).await;+ }+ ProducerEvent::Outcome(result) => {+ outcome = Some(result);+ break;+ }+ }}+ let terminal = match outcome {+ Some(Ok(_saved_id)) => done_event(),+ Some(Err(message)) => error_event(if message.trim().is_empty() {+ "Answer failed"+ } else {+ &message+ }),+ None => error_event("Answer stream ended unexpectedly"),+ };+ handle.emit(terminal).await;// 注销之后再接上的人得到「没有在跑的」,那时答案已经落库handle.finish().await;});@@ -982,13 +1013,13 @@ fn legacy_rag(query: String,turns: Vec<(String, String)>,client: utopia_llm::LlmClient,-) -> impl Stream<Item = Frame> {+) -> impl Stream<Item = ProducerEvent> {async_stream::stream! {let chunks = match retrieval::hybrid(&state, kb_id, workspace_id, &query, 8, None).await {Ok(chunks) => chunks,Err(error) => {tracing::warn!(%error, "fallback document retrieval failed");- yield error_event("Could not search the documents.");+ yield ProducerEvent::Outcome(Err("Could not search the documents.".into()));return;}};@@ -997,10 +1028,10 @@ fn legacy_rag(.enumerate().map(|(i, c)| source_json(i + 1, c)).collect();- yield Frame::new(+ yield ProducerEvent::Progress(Frame::new("sources",serde_json::to_string(&legacy_sources).unwrap_or_else(|_| "[]".into()),- );+ ));let mut lmsgs = vec![json!({ "role": "system", "content": legacy_system_prompt(&chunks) })];for (role, content) in &turns {lmsgs.push(json!({ "role": role, "content": content }));@@ -1011,11 +1042,15 @@ fn legacy_rag(let mut deltas = std::pin::pin!(deltas);while let Some(item) = deltas.next().await {match item {- Ok(text) => { answer_acc.push_str(&text); yield delta_event(&text); }- Err(e) => { yield error_event(&e.to_string()); return; }+ Ok(text) => { answer_acc.push_str(&text); yield ProducerEvent::Progress(delta_event(&text)); }+ Err(e) => { yield ProducerEvent::Outcome(Err(e.to_string())); return; }}}- if let Err(error) = utopia_store::conversations::append_message(+ if answer_acc.trim().is_empty() {+ yield ProducerEvent::Outcome(Err("Model returned an empty answer".into()));+ return;+ }+ let saved = utopia_store::conversations::append_message(&state.pool, conversation_id, "assistant", &answer_acc,&utopia_store::conversations::TurnRecord {steps: serde_json::Value::Array(Vec::new()),@@ -1023,14 +1058,16 @@ fn legacy_rag(resolved: serde_json::Value::Array(Vec::new()),tool_exchange: serde_json::Value::Array(Vec::new()),},- ).await {- tracing::error!(%error, "fallback answer persistence was not confirmed");- yield error_event("Could not confirm that the answer was saved.");- return;+ ).await;+ match saved {+ Ok(id) => yield ProducerEvent::Outcome(Ok(id)),+ Err(error) => {+ tracing::error!(%error, "fallback answer persistence was not confirmed");+ yield ProducerEvent::Outcome(Err("Could not confirm that the answer was saved.".into()));+ }}- yield done_event();}- Err(e) => yield error_event(&e.to_string()),+ Err(e) => yield ProducerEvent::Outcome(Err(e.to_string())),}}}@@ -1062,8 +1099,10 @@ fn sse_from(yield to_event(&frame);if done { return; }}- // 生成结束、发送端销毁:正常收尾- Err(tokio::sync::broadcast::error::RecvError::Closed) => return,+ Err(tokio::sync::broadcast::error::RecvError::Closed) => {+ yield to_event(&error_event("Answer stream ended unexpectedly"));+ return;+ }// 这个客户端读得太慢,被广播缓冲甩下了。**说出来**——// 静默继续会让它少掉中间一段而毫不知情Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {diff --git a/crates/utopia-server/src/api/chat_empty_reply_tests.rs b/crates/utopia-server/src/api/chat_empty_reply_tests.rsindex fa6879c5..927ddc67 100644--- a/crates/utopia-server/src/api/chat_empty_reply_tests.rs+++ b/crates/utopia-server/src/api/chat_empty_reply_tests.rs@@ -252,7 +252,7 @@ impl Fx {Ok(String::from_utf8_lossy(&body).into_owned())}- async fn stored_answer(&self) -> anyhow::Result<Option<String>> {+ pub(super) async fn stored_answer(&self) -> anyhow::Result<Option<String>> {Ok(sqlx::query_scalar("SELECT m.content FROM conversation_messages mJOIN conversations c ON c.id = m.conversation_id@@ -264,6 +264,10 @@ impl Fx {.await?)}+ pub(super) fn requests(&self) -> Vec<serde_json::Value> {+ self.fake.requests()+ }+pub(super) async fn cleanup(self) -> anyhow::Result<()> {sqlx::query("DELETE FROM organizations WHERE id=$1").bind(self.org)diff --git a/crates/utopia-server/src/api/chat_stream_tests.rs b/crates/utopia-server/src/api/chat_stream_tests.rsindex 6feb862b..dcf4b843 100644--- a/crates/utopia-server/src/api/chat_stream_tests.rs+++ b/crates/utopia-server/src/api/chat_stream_tests.rs@@ -56,6 +56,27 @@ async fn lagged_subscribers_receive_an_error_not_done() {assert!(text.contains("event: error") && !text.contains("event: done"));}+#[tokio::test]+async fn producer_disappearing_without_an_outcome_ends_in_one_error() {+ let registry = Arc::new(crate::live::Registry::default());+ let id = Uuid::now_v7();+ let handle = registry.begin(id).await;+ let stream = sse_from(registry.attach(id).await);+ handle.emit(delta_event("partial")).await;+ handle.finish().await;+ let body = tokio::time::timeout(+ std::time::Duration::from_secs(2),+ axum::body::to_bytes(stream.into_response().into_body(), 65536),+ )+ .await+ .expect("closed producer must end the stream")+ .unwrap();+ let text = String::from_utf8_lossy(&body);+ assert_eq!(text.matches("event: error").count(), 1, "{text}");+ assert!(!text.contains("event: done"), "{text}");+ assert!(text.contains("Answer stream ended unexpectedly"), "{text}");+}+#[tokio::test]async fn first_terminal_freezes_the_snapshot_and_broadcast() {let registry = Arc::new(crate::live::Registry::default());diff --git a/crates/utopia-server/src/api/chat_terminal_tests.rs b/crates/utopia-server/src/api/chat_terminal_tests.rsindex 707e0985..b183bfa0 100644--- a/crates/utopia-server/src/api/chat_terminal_tests.rs+++ b/crates/utopia-server/src/api/chat_terminal_tests.rs@@ -14,12 +14,13 @@//! **新增一条会失败的路,代价是加一行**;加不出那一行,说明这条路自己也没想清楚//! 该怎么收尾。评审该盯的就是「新开了会失败的路却没加行」。//!-//! ## 还没进表的注入点+//! ## 不在表里的路//!-//! 下面这几个的注入手段随对应 PR 一起到:检索中途出错要在工具请求落地后关掉夹具-//! 连接池(#850);助手 INSERT 被拒要一个夹具作用域的触发器(#852);流在答案-//! 中途被切、以及终结广播早于注册项移除丢失,要 Registry 那一层的夹具(#851)。-//! 每一个落地后就是这张表多一行,而不是多一个测试文件。+//! 有几条路的注入手段是夹具级的,进不了这张按回复脚本排的表,各自在同目录的定向测试里:+//! 检索中途出错要关掉夹具连接池,助手 INSERT 被拒要夹具作用域的触发器——+//! `chat_persistence_tests.rs`;降级回答的请求形状——`chat_fallback_tests.rs`;+//! 流在答案中途被切、终结广播早于注册项移除丢失——`chat_registry_tests.rs`。+//! 新开一条这样的路,先看这三个文件里有没有位置,再考虑新文件。use super::chat_empty_reply_tests::{fixture, Reply, Scripted};@@ -40,9 +41,7 @@ struct Case {replies: Vec<Reply>,/// 该怎么收尾ends: Ends,- /// 还没修的话,是哪条 PR 在修。**那条 PR 落地时删掉这个字段**,这一行- /// 就开始被强制——留着 `Some` 而不是把行删掉,是为了让「已知没修」看得见- pending: Option<&'static str>,+ fallback: bool,}/// 数一数这条 SSE 里出现了几个终结。@@ -119,14 +118,20 @@ fn table() -> Vec<Case> {what: "模型正常作答",replies: vec![TOOL, Reply::Text("Acme 去年第四季度换了 CFO。")],ends: Ends::Done,- pending: None,+ fallback: false,},// 已修:重试之后仍然是空正文Case {what: "重试之后正文仍然为空",replies: vec![TOOL, Reply::Empty, Reply::Empty],ends: Ends::Error,- pending: None,+ fallback: false,+ },+ Case {+ what: "降级模型仍然返回空答案",+ replies: vec![Reply::Http(400), Reply::Http(400), Reply::Empty],+ ends: Ends::Error,+ fallback: true,},// #845:端点在预算耗尽后把工具控制文本当正文吐出来Case {@@ -135,7 +140,7 @@ fn table() -> Vec<Case> {"<DSMLcalls><DSMLinvoke name=\"search\"></DSMLinvoke></DSMLcalls>",)),ends: Ends::Error,- pending: None,+ fallback: false,},// #845:同上,但前面先有一段像样的叙述——分帧边界不该影响判断Case {@@ -144,37 +149,43 @@ fn table() -> Vec<Case> {"我去核对一下证据。\n<DSMLcalls><DSMLinvoke name=\"search\"></DSMLinvoke></DSMLcalls>",)),ends: Ends::Error,- pending: None,+ fallback: false,},]}#[tokio::test]async fn a_turn_ends_in_exactly_one_earned_terminal() -> anyhow::Result<()> {- let mut skipped: Vec<&str> = Vec::new();let mut ran = 0usize;for case in table() {- if let Some(pr) = case.pending {- skipped.push(pr);- eprintln!("跳过「{}」:等 {pr} 落地", case.what);- continue;- }let Some(f) = fixture(Scripted::new(case.replies.clone())).await? else {eprintln!("没有 UTOPIA_DATABASE_URL,整张表跳过");return Ok(());};let sse = f.ask("Acme 去年第四季度有什么变化?").await?;assert_one_earned_terminal(case.what, case.ends, &sse);+ if case.fallback {+ let requests = f.requests();+ assert!(+ requests.len() > 1 && requests.last().is_some_and(|r| r.get("tools").is_none()),+ "{}:必须真的进入无工具的降级回答请求:{requests:?}",+ case.what+ );+ }+ if case.ends == Ends::Error {+ assert!(+ f.stored_answer().await?.is_none(),+ "{}:失败时不能保存助手消息",+ case.what+ );+ }eprintln!("verified terminal contract: {}", case.what);f.cleanup().await?;ran += 1;}assert!(ran > 0, "整张表被跳空了,等于没测");- if !skipped.is_empty() {- eprintln!("还没强制的行,等这些 PR:{skipped:?}");- }Ok(())}diff --git a/web/package.json b/web/package.jsonindex 77b5b318..968621bc 100644--- a/web/package.json+++ b/web/package.json@@ -22,6 +22,7 @@"class-variance-authority": "^0.7.1","cmdk": "^1.1.1","cn": "^0.2.6",+ "eventsource-parser": "3.1.1","graphology": "^0.26.0","graphology-layout": "^0.6.1","graphology-layout-forceatlas2": "^0.10.1",diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yamlindex 2c16cb9d..e1a1e65f 100644--- a/web/pnpm-lock.yaml+++ b/web/pnpm-lock.yaml@@ -35,6 +35,9 @@ importers:cn:specifier: ^0.2.6version: 0.2.6+ eventsource-parser:+ specifier: 3.1.1+ version: 3.1.1graphology:specifier: ^0.26.0version: 0.26.0([email protected])diff --git a/web/src/api.ts b/web/src/api.tsindex 6a6bc086..783f7ac7 100644--- a/web/src/api.ts+++ b/web/src/api.ts@@ -1,5 +1,6 @@import type { SourceKind } from "./sourceKinds";import { S, lang } from "./i18n";+import { createParser } from "eventsource-parser";export class ApiError extends Error {status: number;@@ -2675,13 +2676,9 @@ function consumeChatStream(}reader = res.body.getReader();const decoder = new TextDecoder();- let line = "";- let skipLf = false;- let event = "message";- let data: string[] = [];- const dispatch = () => {- if (data.length === 0) return;- const value = data.join("\n");+ let trailingCr = false;+ const parser = createParser({ onEvent: ({ event, data: value }) => {+ if (terminal || controller.signal.aborted) return;if (event === "done") { terminal = true; handlers.onDone(); }else if (event === "error") fail(value);else if (event === "idle") {@@ -2692,36 +2689,18 @@ function consumeChatStream(else if (event === "step") handlers.onStep(JSON.parse(value));else if (event === "delta") handlers.onDelta(JSON.parse(value).text);else if (event === "snapshot") handlers.onSnapshot?.(JSON.parse(value));- };- const finishLine = () => {- if (line === "") {- dispatch();- event = "message";- data = [];- } else {- const colon = line.indexOf(":");- const field = colon < 0 ? line : line.slice(0, colon);- let value = colon < 0 ? "" : line.slice(colon + 1);- if (value.startsWith(" ")) value = value.slice(1);- if (field === "event") event = value;- else if (field === "data") data.push(value);- }- line = "";- };+ } });while (!terminal && !controller.signal.aborted) {const { done, value } = await reader.read();- if (done || controller.signal.aborted) break;- // CR is a complete line ending, even when its optional LF arrives in the- // next byte chunk. TextDecoder independently preserves split UTF-8.- for (const char of decoder.decode(value, { stream: true })) {- if (skipLf && char === "\n") { skipLf = false; continue; }- skipLf = false;- if (char === "\r" || char === "\n") {- finishLine();- skipLf = char === "\r";- } else line += char;- if (terminal || controller.signal.aborted) break;+ if (done) {+ // v3 holds a final CR until the next character confirms its line ending.+ if (trailingCr) parser.feed("\n");+ break;}+ if (controller.signal.aborted) break;+ const chunk = decoder.decode(value, { stream: true });+ if (chunk) trailingCr = chunk.endsWith("\r");+ parser.feed(chunk);}// EOF never dispatches an incomplete frame and is not an application done.fail(S.ask.streamInterrupted);diff --git a/web/src/chatStream.test.ts b/web/src/chatStream.test.tsindex 94c8acd0..033b7a9e 100644--- a/web/src/chatStream.test.ts+++ b/web/src/chatStream.test.ts@@ -31,8 +31,9 @@ describe("application chat terminal outcomes", () => {const text = 'event: delta\ndata: {"text":"中文🙂"}\n\nevent: done\ndata: {}\n\n'.replaceAll("\n", nl);expect(await replay(text, false, true)).toEqual([["delta","中文🙂"],["done",null]]);});- it("preserves multiline error data and ignores everything after it", async () => {- expect(await replay("event: error\ndata: first\ndata: second\n\nevent: done\ndata: {}\n\nevent: delta\ndata: not-json\n\n"))+ it.each(["\n", "\r\n", "\r"])("preserves multiline error data with %j and ignores later events", async (nl) => {+ const text = "event: error\ndata: first\ndata: second\n\nevent: done\ndata: {}\n\nevent: delta\ndata: not-json\n\n".replaceAll("\n", nl);+ expect(await replay(text, false, true)).toEqual([["error","first\n second"]]);});it("does not turn partial output plus EOF into success", async () => {@@ -45,7 +46,7 @@ describe("application chat terminal outcomes", () => {it("ignores frames after the first done", async () => {expect(await replay('event: done\ndata: {}\n\nevent: delta\ndata: {"text":"late"}\n\nevent: error\ndata: late error\n\n')).toEqual([["done",null]]);});- it.each(["event: done\ndata: {}\n", "event: done\ndata: {}", "", "event: delta\ndata: {broken}\n\n"])("requires a complete terminal frame: %s", async (s) => {+ it.each(["event: done\ndata: {}\n", "event: done\ndata: {}\r", "event: done\ndata: {}", "", "event: delta\ndata: {broken}\n\n"])("requires a complete terminal frame: %s", async (s) => {const result = await replay(s);expect(result).toHaveLength(1); expect(result[0][0]).toBe("error");});