Skip to content

World's first open-source enterprise world model.

Make Stop cancel chat generation and preserve the partial answer (#969)

WaylandYangd9c3aabc5251
diff --git a/crates/utopia-cli/src/main.rs b/crates/utopia-cli/src/main.rs
index 8e824185..a35a8ab3 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 = 85;
+const CURRENT_SCHEMA_VERSION: u32 = 86;
fn main() -> anyhow::Result<()> {
dotenvy::dotenv().ok();
diff --git a/crates/utopia-core/src/models.rs b/crates/utopia-core/src/models.rs
index 49f1b351..ed0f7272 100644
--- a/crates/utopia-core/src/models.rs
+++ b/crates/utopia-core/src/models.rs
@@ -430,6 +430,7 @@ pub struct ConversationMessage {
pub content: String,
pub steps: serde_json::Value,
pub sources: serde_json::Value,
+ pub stopped: bool,
pub created_at: DateTime<Utc>,
}
diff --git a/crates/utopia-server/src/api/agent.rs b/crates/utopia-server/src/api/agent.rs
index 739ca676..1bca29ae 100644
--- a/crates/utopia-server/src/api/agent.rs
+++ b/crates/utopia-server/src/api/agent.rs
@@ -398,7 +398,8 @@ pub fn dynamic_tools(shared: &Arc<Shared>) -> Vec<DynamicTool> {
#[derive(Clone)]
pub struct Policy {
pub shared: Arc<Shared>,
- /// The next logical call hands off before provider I/O; it cannot run tools.
+ pub cancellation: crate::live::Cancellation,
+ /// 下一次逻辑调用在访问模型服务前交接,不再执行工具。
pub max_rounds: usize,
}
@@ -412,9 +413,11 @@ impl AgentHook for Policy {
self.shared
.finalizing
.store(turn > self.max_rounds, Ordering::Relaxed);
- let action = if turn > self.max_rounds {
- // Rig 0.42 resolves this hook before model selection or provider I/O.
- // The route consumes this per-run state only with PromptCancelled.
+ let action = if self.cancellation.is_cancelled() {
+ CompletionCallAction::Stop("Stopped by the user".into())
+ } else if turn > self.max_rounds {
+ // Rig 0.42 在选择模型或访问服务前执行此钩子。
+ // 路由只在 PromptCancelled 时读取本轮标记。
self.shared.answer_requested.store(true, Ordering::Relaxed);
CompletionCallAction::Stop("Evidence gathering complete".into())
} else if !self.shared.gate_passed.load(Ordering::Relaxed) {
@@ -529,7 +532,9 @@ impl AgentHook for Policy {
) -> impl std::future::Future<Output = ToolCallAction> + Send {
// **说不清自己要做什么的调用不执行。** 把话回给模型,让它重来;
// 界面上照样显示成一次没做成的调用
- let action = if self.shared.finalizing() {
+ let action = if self.cancellation.is_cancelled() {
+ ToolCallAction::Stop("Stopped by the user".into())
+ } else if self.shared.finalizing() {
ToolCallAction::Stop(FINAL_TOOL_CALL.into())
} else {
match super::chat::check_call(&self.shared.schema, event.tool_name, event.args) {
diff --git a/crates/utopia-server/src/api/chat.rs b/crates/utopia-server/src/api/chat.rs
index c5b3e65b..50e67397 100644
--- a/crates/utopia-server/src/api/chat.rs
+++ b/crates/utopia-server/src/api/chat.rs
@@ -5,6 +5,11 @@
#[path = "chat_finalization.rs"]
mod finalization;
+#[path = "chat_generation.rs"]
+mod generation;
+
+use generation::{Answer, ProducerEvent};
+
use super::agent;
use super::rig_model::{self, RigModel};
use crate::live::Frame;
@@ -40,11 +45,6 @@ const KNOWN_ENTITY_LIMIT: usize = 20;
const MAX_HISTORY: usize = 20;
const MAX_ROUNDS: usize = 6;
-enum ProducerEvent {
- Progress(Frame),
- Outcome(Result<Uuid, Failure>),
-}
-
/// 一次没答成的生成(0004):`code` 给界面去 `err.*` 表里查措辞,英文原句留给日志、
/// MCP 和不做本地化的客户端。**服务端不出显示文本**——`error` 帧与请求被拒用的是
/// 同一个信封 `{error, code}`,前端用同一个函数读
@@ -127,34 +127,16 @@ pub struct ChatReq {
pub retry_message_id: Option<Uuid>,
}
-/// 要重答的那一问
-struct Retry {
- conversation_id: Uuid,
- question_id: Uuid,
- question: String,
-}
-
-/// 重答(#936)之前的检查:要答的必须是这场对话最后那条消息,而且是一个问题,
-/// 这场对话也没有在跑的生成。
+/// 已占住会话后检查重试资格,避免检查与登记之间另一轮已经写入回答。
+/// 只接受本会话最后一条未回答的用户消息;拒绝时不改历史,也不调用模型。
///
-/// 每一步都只读不写:在这里被拒,库里什么也没变,模型也没被调用。从前想再问一次
-/// 只能重发,同一个问题就在历史里存了两遍,没有回答的那一遍还会在之后每一问里
-/// 作为「没回答的一轮」回放给模型
+/// 为什么要有重答(#936):从前想再问一次只能重发,同一个问题就在历史里存了两遍,
+/// 没有回答的那一遍还会在之后每一问里作为「没回答的一轮」回放给模型
async fn retry_question(
state: &AppState,
- kb_id: Uuid,
- user_id: Uuid,
- conversation_id: Option<Uuid>,
+ conversation_id: Uuid,
question_id: Uuid,
-) -> Result<Retry, AppError> {
- let Some(conversation_id) = conversation_id else {
- return Err(AppError::invalid(
- "retry_needs_conversation",
- "A retry names the conversation its question is in",
- ));
- };
- utopia_store::conversations::require_owned(&state.pool, kb_id, user_id, conversation_id)
- .await?;
+) -> Result<String, AppError> {
let target =
utopia_store::conversations::retry_target(&state.pool, conversation_id, question_id)
.await?
@@ -172,22 +154,7 @@ async fn retry_question(
.into(),
});
}
- if state.live.is_running(conversation_id).await {
- return Err(answer_running());
- }
- Ok(Retry {
- conversation_id,
- question_id,
- question: target.content,
- })
-}
-
-/// 这场对话里正有一个回答在写。#961 给「生成中又来一个新问题」的也是 409;后落地的那个沿用先落地的 code
-fn answer_running() -> AppError {
- AppError::CodedConflict {
- code: "answer_running",
- message: "An answer is already being written in this conversation".into(),
- }
+ Ok(target.content)
}
/// 工具清单。**MCP 也用这一份**(`mcp.rs`):名字、描述、参数 schema 抄成
@@ -693,55 +660,38 @@ pub async fn chat(
let client = llm_util::chat_client(&settings)
.ok_or_else(|| AppError::invalid("no_chat_model", NO_MODEL))?;
- // 重答(#936)问的是库里那一句还没有回答的问题,不是请求里的正文
- let retry = match req.retry_message_id {
- Some(question_id) => {
- Some(retry_question(&state, kb_id, user.id, req.conversation_id, question_id).await?)
- }
- None => None,
- };
- let retrying = retry.is_some();
- let query = match &retry {
- Some(retry) => retry.question.trim().to_string(),
- None => req.message.trim().to_string(),
- };
- if query.is_empty() {
+ let query = req.message.trim().to_string();
+ if req.retry_message_id.is_none() && query.is_empty() {
return Err(AppError::Validation("Missing user message".into()).into());
}
- // 语义层:跟这个问题有关的那几条确认口径进 system prompt——问数优先用确认口径,
- // 而不是每次从 schema 猜。按问题挑而不是全塞:二十七条的上界 17/18 是在
- // 三十条的上限之下量的,一百条口径靠字典序截断就不成立了(#574)
- let mappings = if mounted_sources.is_empty() {
- Vec::new()
- } else {
- crate::mapping_index::relevant(
- &state,
- kb_id,
- kb.workspace_id,
- &query,
- crate::mapping_index::DEFINITIONS_IN_PROMPT,
- )
- .await
- .map_err(AppError::Other)?
+ let conversation_id = match req.conversation_id {
+ Some(id) => {
+ utopia_store::conversations::require_owned(&state.pool, kb_id, user.id, id).await?;
+ id
+ }
+ None if req.retry_message_id.is_some() => {
+ return Err(AppError::invalid(
+ "retry_needs_conversation",
+ "A retry names the conversation its question is in",
+ )
+ .into());
+ }
+ None => utopia_store::conversations::create(&state.pool, kb_id, user.id, &query).await?,
};
-
- // 会话持久化:有 id 则校验归属,无则以首句为题新建;用户消息即刻落库,
- // 上下文由服务端从库里拼——前端只送新消息
+ // 会话持久化:用户消息即刻落库,上下文由服务端从库里拼——前端只送新消息。
// 重答的那一问已经在库里,就地回答它,不再存一遍
- let (conversation_id, user_message_id) = match retry {
- Some(retry) => (retry.conversation_id, retry.question_id),
+ // 新问题和重试共用入口:先占住会话,再查重试资格或存问题。
+ // 准备失败时 Drop 自动释放;被拒的新问题不会混入历史。
+ let handle = state.live.begin(conversation_id).await?;
+ let generation_id = handle.generation_id();
+ let cancellation = handle.cancellation();
+ let (query, user_message_id) = match req.retry_message_id {
+ Some(question_id) => {
+ let question = retry_question(&state, conversation_id, question_id).await?;
+ (question.trim().to_string(), question_id)
+ }
None => {
- let conversation_id = match req.conversation_id {
- Some(id) => {
- utopia_store::conversations::require_owned(&state.pool, kb_id, user.id, id)
- .await?;
- id
- }
- None => {
- utopia_store::conversations::create(&state.pool, kb_id, user.id, &query).await?
- }
- };
- let user_message_id = utopia_store::conversations::append_message(
+ let question_id = utopia_store::conversations::append_message(
&state.pool,
conversation_id,
"user",
@@ -749,9 +699,12 @@ pub async fn chat(
&utopia_store::conversations::TurnRecord::empty(),
)
.await?;
- (conversation_id, user_message_id)
+ (query, question_id)
}
};
+ if query.is_empty() {
+ return Err(AppError::Validation("Missing user message".into()).into());
+ }
let history = utopia_store::conversations::recent_context(
&state.pool,
conversation_id,
@@ -775,6 +728,15 @@ pub async fn chat(
// 注册表在生成器之前取出来:下面那个 `async_stream!` 会把 `state` 整个搬走
let live = state.live.clone();
+ let save_pool = state.pool.clone();
+ let previous_answer = history
+ .turns
+ .iter()
+ .rev()
+ .find(|(role, _)| role == "assistant")
+ .map(|(_, content)| content.clone())
+ .unwrap_or_default();
+ let previous_sources = history.last_sources.clone();
// 生成过程不挂在这条连接上。
//
@@ -790,6 +752,31 @@ pub async fn chat(
// 代价说清楚:**没人看的时候仍然在花钱**。这是有意的——丢答案比多跑一轮贵,
// 而 `MAX_ROUNDS` 已经给了上限。send 失败(接收端没了)不中断,那正是要点。
let producer = async_stream::stream! {
+ // 依赖模型的准备工作之前先发送身份,早到的 Stop 也能指向本轮。
+ // 会话 id 先行下发(新会话由此告知前端);这一问存下的 id 一起下发:
+ // 答到一半失败了,界面凭它重答(#936)
+ yield ProducerEvent::Progress(Frame::new("conversation", json!({
+ "id": conversation_id, "message_id": user_message_id, "generation_id": generation_id,
+ }).to_string()));
+ // 语义层:跟这个问题有关的那几条确认口径进 system prompt——问数优先用确认口径,
+ // 而不是每次从 schema 猜。按问题挑而不是全塞:二十七条的上界 17/18 是在
+ // 三十条的上限之下量的,一百条口径靠字典序截断就不成立了(#574)。
+ // 挑口径要调嵌入模型,所以放在生成器里:Stop 丢掉生成器,这个请求跟着取消(0063)。
+ // 代价是它失败得晚了:从前是什么都没写的 500,现在问题已经存下,失败是一帧 error
+ let mappings = if mounted_sources.is_empty() {
+ Vec::new()
+ } else {
+ match crate::mapping_index::relevant(
+ &state, kb_id, workspace_id, &query, crate::mapping_index::DEFINITIONS_IN_PROMPT,
+ ).await {
+ Ok(mappings) => mappings,
+ Err(error) => {
+ tracing::warn!(%error, "Could not retrieve conversation mappings");
+ yield ProducerEvent::Outcome(Err(Failure::new("search_failed", "Could not retrieve relevant mappings.")));
+ return;
+ }
+ }
+ };
let ds_names: Vec<String> = mounted_sources.iter().map(|d| d.name.clone()).collect();
let tools = tools_schema(can_write, &ds_names);
let mut system_prompt = if can_write && REMEMBER_ENABLED {
@@ -849,10 +836,6 @@ pub async fn chat(
}
}
- // 会话 id 先行下发(新会话由此告知前端)
- // 这一问存下的 id 一起下发:答到一半失败了,界面凭它重答(#936)
- yield ProducerEvent::Progress(Frame::new("conversation", json!({ "id": conversation_id, "message_id": user_message_id }).to_string()));
-
// 循环是 rig 的(#546):工具、策略钩子、历史、实体清单都交给它;
// 这里只把它的事件翻成前端认得的帧,并在结束时落库
let shared = agent::Shared::new(
@@ -869,6 +852,7 @@ pub async fn chat(
let policy = agent::Policy {
shared: shared.clone(),
max_rounds: MAX_ROUNDS,
+ cancellation,
};
let tool_server = ToolServer::new()
.dynamic_tools(agent::dynamic_tools(&shared))
@@ -989,6 +973,7 @@ pub async fn chat(
let text = rig_model::tool_result_text(&tool_result.content);
// 闸门工具不留轨迹:「你好」下面挂一条「声明不用查」是噪音
let mut is_error = None;
+ let mut published_step = None;
if tool_result.name != agent::NO_EVIDENCE_TOOL {
let mut step = match shared.take_step(&internal_call_id) {
Some((step, failed)) => { is_error = Some(failed); step }
@@ -1009,7 +994,7 @@ pub async fn chat(
obj.insert("at".into(), json!(answer_acc.encode_utf16().count()));
}
steps_acc.push(step.clone());
- yield ProducerEvent::Progress(Frame::new("step", serde_json::to_string(&step).unwrap_or_default()));
+ published_step = Some(step);
}
// cite() only appends: document reads can add citations too, regardless
// of the UI step kind. Release the sink before yielding to subscribers.
@@ -1022,12 +1007,23 @@ pub async fn chat(
None
}
};
- if let Some(sources) = sources {
- 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); }
exchange_acc.push(recorded);
+ // 先保留已完成工具的历史,再把步骤发给可能立即按 Stop 的客户端。
+ // 保存停止的答案时,生成驱动会移除尚未完成的并行调用。
+ let resolved = shared.sink.lock().await.resolved.clone();
+ yield ProducerEvent::Context {
+ resolved,
+ tool_exchange: exchange_acc.clone(),
+ gathered: !steps_acc.is_empty(),
+ };
+ if let Some(step) = published_step {
+ yield ProducerEvent::Progress(Frame::new("step", step.to_string()));
+ }
+ if let Some(sources) = sources {
+ yield ProducerEvent::Progress(Frame::new("sources", serde_json::to_string(&sources).unwrap_or_else(|_| "[]".into())));
+ }
}
// 钩子把这一回合退了回去。已经流给用户的话收不回来,接下来的正文
// 另起一段;还扣着的(写成正文的工具调用)随这一回合丢掉
@@ -1059,7 +1055,6 @@ pub async fn chat(
state.clone(),
kb_id,
workspace_id,
- conversation_id,
query.clone(),
history.turns.clone(),
client.clone(),
@@ -1143,79 +1138,30 @@ pub async fn chat(
.unwrap_or_default();
sources = carried_sources(&answer_acc, previous, &history.last_sources);
}
- let saved = utopia_store::conversations::append_message(
- &state.pool, conversation_id, "assistant", &answer_acc,
- &utopia_store::conversations::TurnRecord {
+ yield ProducerEvent::Outcome(Ok(Answer {
+ content: answer_acc,
+ record: utopia_store::conversations::TurnRecord {
+ stopped: false,
steps: serde_json::Value::Array(steps_acc),
- sources: serde_json::Value::Array(sources.clone()),
+ sources: serde_json::Value::Array(sources),
resolved: serde_json::Value::Array(resolved),
tool_exchange: serde_json::Value::Array(exchange_acc),
},
- ).await;
- let saved_id = match saved {
- Ok(id) => id,
- Err(error) => {
- tracing::error!(%error, %conversation_id, "Could not persist final answer");
- yield ProducerEvent::Outcome(Err(Failure::new("answer_not_saved", "Could not save the answer. Please try again later.")));
- 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));
+ final_text: shared.finalizing().then_some(turn_text),
+ }));
};
// 生成登记在案,然后**这条连接也只是去「接上」它**——与刷新之后
// 那条重连走的是同一段代码。两条路分开写的话,迟早只有一条是对的
- let handle = if retrying {
- // 上面检查过没有在跑的生成,但两次检查之间别的请求可能已经开了一次(比如连按
- // 两下重试)。查和登记在一把锁下再做一次;这条路到这里还没写过任何东西
- live.begin_if_idle(conversation_id)
- .await
- .ok_or_else(answer_running)?
- } else {
- live.begin(conversation_id).await
- };
let attached = live.attach(conversation_id).await;
- tokio::spawn(async move {
- let mut producer = std::pin::pin!(producer);
- let mut outcome = None;
- while let Some(event) = producer.next().await {
- // 没有订阅者是常态(人走了)。**照发不误**:这里中断就等于
- // 把「切走一次丢一个回答」原样搬回来
- match event {
- ProducerEvent::Progress(frame) => {
- if matches!(frame.event, "done" | "error") {
- outcome = Some(Err(Failure::new(
- "answer_failed",
- "Producer sent a terminal as progress",
- )));
- break;
- }
- handle.emit(frame).await;
- }
- ProducerEvent::Outcome(result) => {
- outcome = Some(result);
- break;
- }
- }
- }
- let terminal = match outcome {
- Some(Ok(_saved_id)) => done_event(),
- Some(Err(failure)) => error_event(
- failure.code,
- if failure.message.trim().is_empty() {
- "Answer failed"
- } else {
- &failure.message
- },
- ),
- None => error_event("stream_ended", "Answer stream ended unexpectedly"),
- };
- handle.emit(terminal).await;
- // 注销之后再接上的人得到「没有在跑的」,那时答案已经落库
- handle.finish().await;
- });
+ tokio::spawn(generation::run(
+ save_pool,
+ conversation_id,
+ handle,
+ producer,
+ previous_answer,
+ previous_sources,
+ ));
Ok(sse_from(attached))
}
@@ -1250,7 +1196,6 @@ fn legacy_rag(
state: AppState,
kb_id: Uuid,
workspace_id: Uuid,
- conversation_id: Uuid,
query: String,
turns: Vec<(String, String)>,
client: utopia_llm::LlmClient,
@@ -1291,22 +1236,14 @@ fn legacy_rag(
yield ProducerEvent::Outcome(Err(Failure::new("answer_empty", "Model returned an empty answer")));
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()),
+ yield ProducerEvent::Outcome(Ok(Answer {
+ content: answer_acc,
+ record: utopia_store::conversations::TurnRecord {
sources: serde_json::Value::Array(legacy_sources),
- resolved: serde_json::Value::Array(Vec::new()),
- tool_exchange: serde_json::Value::Array(Vec::new()),
+ ..utopia_store::conversations::TurnRecord::empty()
},
- ).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(Failure::new("answer_not_saved", "Could not confirm that the answer was saved.")));
- }
- }
+ final_text: None,
+ }));
}
Err(e) => yield ProducerEvent::Outcome(Err(Failure::model(&e, e.to_string()))),
}
@@ -1376,6 +1313,25 @@ pub async fn reattach(
Ok(sse_from(state.live.attach(conversation_id).await))
}
+#[derive(Deserialize)]
+pub struct StopReq {
+ pub generation_id: Uuid,
+}
+
+/// 只通知生成的持有者,由它保存部分答案并发送终态。
+pub async fn stop(
+ State(state): State<AppState>,
+ AuthUser(user): AuthUser,
+ Path((kb_id, conversation_id)): Path<(Uuid, Uuid)>,
+ Json(req): Json<StopReq>,
+) -> ApiResult<Json<serde_json::Value>> {
+ utopia_store::access::require_kb(&state.pool, &user, kb_id, Role::Viewer).await?;
+ utopia_store::conversations::require_owned(&state.pool, kb_id, user.id, conversation_id)
+ .await?;
+ state.live.stop(conversation_id, req.generation_id).await;
+ Ok(Json(json!({ "ok": true })))
+}
+
/// 生成器产出的是 `Frame`,不是 `axum` 的 `Event`。
/// **广播与快照都要读回事件的内容**,而 `Event` 读不回来(见 `live`)
fn delta_event(text: &str) -> Frame {
@@ -1385,10 +1341,6 @@ fn delta_event(text: &str) -> Frame {
)
}
-fn done_event() -> Frame {
- Frame::new("done", "{}".into())
-}
-
/// 与请求被拒同一个信封:`error` 是英文原句,`code` 给界面查措辞(0004)
fn error_event(code: &str, message: &str) -> Frame {
Frame::new(
diff --git a/crates/utopia-server/src/api/chat_generation.rs b/crates/utopia-server/src/api/chat_generation.rs
new file mode 100644
index 00000000..82c694f3
--- /dev/null
+++ b/crates/utopia-server/src/api/chat_generation.rs
@@ -0,0 +1,252 @@
+//! 驱动可取消的生成,再不受取消打断地保存唯一结果。
+use super::{carried_sources, delta_event, error_event, Failure};
+use crate::live::{Frame, Handle};
+use futures_util::{Stream, StreamExt};
+use serde_json::{json, Value};
+use std::collections::HashSet;
+use utopia_store::conversations::{append_message, TurnRecord};
+use uuid::Uuid;
+
+pub(super) enum ProducerEvent {
+ Progress(Frame),
+ /// 仅供历史使用,不通过 SSE 发送;停止时只保留已完成的调用。
+ Context {
+ resolved: Vec<Value>,
+ tool_exchange: Vec<Value>,
+ gathered: bool,
+ },
+ Outcome(Result<Answer, Failure>),
+}
+
+pub(super) struct Answer {
+ pub content: String,
+ pub record: TurnRecord,
+ /// 验证过的最终回答只有保存成功后才发布。
+ pub final_text: Option<String>,
+}
+
+pub(super) async fn run(
+ pool: sqlx::PgPool,
+ conversation_id: Uuid,
+ handle: Handle,
+ producer: impl Stream<Item = ProducerEvent>,
+ previous_answer: String,
+ previous_sources: Vec<Value>,
+) {
+ // collect 持有模型和工具的 future,在持久化前将它们丢弃。
+ // 迟到的 Stop 不能打断提交,也不能再创建一条助手消息。
+ let result = collect(&handle, producer, &previous_answer, &previous_sources).await;
+ let terminal = match result {
+ Ok(answer) => save(&pool, conversation_id, &handle, answer).await,
+ Err(failure) => error_event(failure.code, &failure.message),
+ };
+ handle.complete(terminal).await;
+}
+
+async fn collect(
+ handle: &Handle,
+ producer: impl Stream<Item = ProducerEvent>,
+ previous_answer: &str,
+ previous_sources: &[Value],
+) -> Result<Answer, Failure> {
+ let mut producer = Box::pin(producer);
+ let cancellation = handle.cancellation();
+ let mut resolved = Vec::new();
+ let mut tool_exchange = Vec::new();
+ let mut gathered = false;
+
+ loop {
+ let event = tokio::select! {
+ biased;
+ _ = cancellation.cancelled() => {
+ // 先丢弃生成器,保存部分答案期间不能继续执行模型或工具。
+ drop(producer);
+ let mut snapshot = handle.snapshot().await;
+ if !gathered && snapshot.sources.is_empty() {
+ snapshot.sources = carried_sources(
+ &snapshot.content, previous_answer, previous_sources,
+ );
+ }
+ return Ok(Answer {
+ content: snapshot.content,
+ record: TurnRecord {
+ stopped: true,
+ steps: json!(snapshot.steps),
+ sources: json!(snapshot.sources),
+ resolved: json!(resolved),
+ tool_exchange: json!(completed_exchange(tool_exchange)),
+ },
+ final_text: None,
+ });
+ }
+ event = producer.next() => event,
+ };
+ match event {
+ Some(ProducerEvent::Progress(frame)) => {
+ if matches!(frame.event, "done" | "error") {
+ return Err(Failure::new(
+ "answer_failed",
+ "Producer sent a terminal as progress",
+ ));
+ }
+ // 旧 RAG 路径即使没搜到结果也会发布 sources。
+ gathered |= matches!(frame.event, "step" | "sources");
+ handle.emit(frame).await;
+ }
+ Some(ProducerEvent::Context {
+ resolved: entities,
+ tool_exchange: exchange,
+ gathered: did_gather,
+ }) => {
+ resolved = entities;
+ tool_exchange = exchange;
+ gathered |= did_gather;
+ }
+ Some(ProducerEvent::Outcome(result)) if !cancellation.is_cancelled() => return result,
+ Some(ProducerEvent::Outcome(_)) => continue,
+ None => {
+ return Err(Failure::new(
+ "stream_ended",
+ "Answer stream ended unexpectedly",
+ ))
+ }
+ }
+ }
+}
+
+async fn save(
+ pool: &sqlx::PgPool,
+ conversation_id: Uuid,
+ handle: &Handle,
+ answer: Answer,
+) -> Frame {
+ let saved = append_message(
+ pool,
+ conversation_id,
+ "assistant",
+ &answer.content,
+ &answer.record,
+ )
+ .await;
+ if let Err(error) = saved {
+ tracing::error!(%error, %conversation_id, "Could not persist chat answer");
+ return error_event(
+ "answer_not_saved",
+ "Could not confirm that the answer was saved.",
+ );
+ }
+
+ handle
+ .emit(Frame::new("sources", answer.record.sources.to_string()))
+ .await;
+ if let Some(text) = answer.final_text {
+ handle.emit(delta_event(&text)).await;
+ }
+ Frame::new(
+ "done",
+ json!({"stopped": answer.record.stopped}).to_string(),
+ )
+}
+
+/// 并行工具可能只完成一部分就被停止。不能回放没有结果的 tool_call:
+/// 模型服务要求每个保留的调用都有对应结果。
+fn completed_exchange(mut exchange: Vec<Value>) -> Vec<Value> {
+ // 模型服务可能在后续轮次复用 ID。倒序遍历,让每条助手消息只匹配
+ // 它之后、下一条助手消息之前的工具结果。
+ let mut answered = HashSet::new();
+ for message in exchange.iter_mut().rev() {
+ if message["role"] == "tool" {
+ if let Some(id) = message["tool_call_id"].as_str() {
+ answered.insert(id.to_owned());
+ }
+ continue;
+ }
+ if let Some(calls) = message["tool_calls"].as_array_mut() {
+ calls.retain(|call| call["id"].as_str().is_some_and(|id| answered.contains(id)));
+ }
+ answered.clear();
+ }
+ exchange.retain(|message| {
+ if message["role"] != "assistant" {
+ return true;
+ }
+ message["tool_calls"]
+ .as_array()
+ .is_some_and(|calls| !calls.is_empty())
+ });
+ exchange
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn a_stopped_parallel_batch_replays_only_answered_calls() {
+ let exchange = vec![
+ json!({"role":"assistant", "tool_calls":[{"id":"finished"},{"id":"pending"}]}),
+ json!({"role":"tool", "tool_call_id":"finished", "content":"found"}),
+ json!({"role":"assistant", "tool_calls":[{"id":"never_started"}]}),
+ ];
+ let kept = completed_exchange(exchange);
+ assert_eq!(kept.len(), 2);
+ assert_eq!(kept[0]["tool_calls"], json!([{"id":"finished"}]));
+ assert_eq!(kept[1]["tool_call_id"], "finished");
+ }
+
+ #[test]
+ fn a_reused_call_id_only_matches_results_in_its_own_round() {
+ let exchange = vec![
+ json!({"role":"assistant", "tool_calls":[{"id":"reused"}]}),
+ json!({"role":"tool", "tool_call_id":"reused", "content":"old result"}),
+ json!({"role":"assistant", "tool_calls":[{"id":"reused"},{"id":"finished"}]}),
+ json!({"role":"tool", "tool_call_id":"finished", "content":"new result"}),
+ ];
+ let kept = completed_exchange(exchange);
+ assert_eq!(kept.len(), 4);
+ assert_eq!(kept[0]["tool_calls"], json!([{"id":"reused"}]));
+ assert_eq!(kept[2]["tool_calls"], json!([{"id":"finished"}]));
+ }
+
+ #[tokio::test]
+ async fn stopping_only_carries_sources_when_this_turn_did_not_gather() {
+ let source = json!({"n":1, "document_id":"previous-document"});
+ let previous_sources = vec![source.clone()];
+ let cases = [
+ (None, json!([source])),
+ (
+ Some(ProducerEvent::Progress(Frame::new("sources", "[]".into()))),
+ json!([]),
+ ),
+ (
+ Some(ProducerEvent::Context {
+ resolved: vec![],
+ tool_exchange: vec![],
+ gathered: true,
+ }),
+ json!([]),
+ ),
+ ];
+ for (before_text, expected_sources) in cases {
+ let registry = std::sync::Arc::new(crate::live::Registry::default());
+ let handle = registry.begin(Uuid::now_v7()).await.unwrap();
+ let cancellation = handle.cancellation();
+ let producer = async_stream::stream! {
+ if let Some(event) = before_text { yield event; }
+ yield ProducerEvent::Progress(delta_event("Target: 95% [1].\n"));
+ cancellation.cancel();
+ std::future::pending::<()>().await;
+ };
+ let answer = collect(
+ &handle,
+ producer,
+ "The target is 95% [1].",
+ &previous_sources,
+ )
+ .await
+ .unwrap_or_else(|failure| panic!("{}", failure.message));
+ assert!(answer.record.stopped);
+ assert_eq!(answer.record.sources, expected_sources);
+ }
+ }
+}
diff --git a/crates/utopia-server/src/api/chat_history_tests.rs b/crates/utopia-server/src/api/chat_history_tests.rs
index b42378f6..a0be4e26 100644
--- a/crates/utopia-server/src/api/chat_history_tests.rs
+++ b/crates/utopia-server/src/api/chat_history_tests.rs
@@ -117,6 +117,7 @@ async fn earlier_entities_sit_right_before_the_question() -> anyhow::Result<()>
"assistant",
"Acme was founded by Ada.",
&utopia_store::conversations::TurnRecord {
+ stopped: false,
steps: json!([]),
sources: json!([]),
resolved: json!([{ "id": Uuid::now_v7(), "name": "Acme", "type": "Organization" }]),
diff --git a/crates/utopia-server/src/api/chat_registry_tests.rs b/crates/utopia-server/src/api/chat_registry_tests.rs
index 990d7fdc..ede365b3 100644
--- a/crates/utopia-server/src/api/chat_registry_tests.rs
+++ b/crates/utopia-server/src/api/chat_registry_tests.rs
@@ -1,93 +1,470 @@
-//! Two real chat producers share a conversation while a local upstream gates their
-//! answers independently. No scheduler sleeps and no production-only test hooks.
+//! 用真实生成流程和可控的本地模型验证 Stop,与 SSE 断开分开验证。
use super::*;
+use axum::http::StatusCode;
+use axum::response::Response;
use tokio::sync::{broadcast, Notify};
-#[derive(Clone, Default)]
+const PARTIAL: &str = "Checking the available evidence.\n";
+const DRAFT: &str = "Unpublished final draft.";
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum Pause {
+ BeforeText,
+ BeforeTool,
+ DuringTool,
+ AfterTool,
+ Legacy,
+ FinalAnswer,
+}
+
+#[derive(Clone)]
struct Gates {
+ pause: Pause,
ready: [Arc<Notify>; 2],
release: [Arc<Notify>; 2],
requests: Arc<Mutex<Vec<serde_json::Value>>>,
+ tool_started: Arc<Notify>,
+ tool_disconnected: Arc<Notify>,
}
-async fn upstream(
- State(gates): State<Gates>,
- Json(body): Json<serde_json::Value>,
-) -> impl IntoResponse {
+async fn upstream(State(gates): State<Gates>, Json(body): Json<serde_json::Value>) -> Response {
let messages = body["messages"].as_array().expect("messages");
- let question = messages.iter().rev().find(|m| m["role"] == "user").unwrap();
- let index = usize::from(question["content"].to_string().contains("second greeting"));
- let tool_ran = messages.iter().any(|m| m["role"] == "tool");
- gates.requests.lock().unwrap().push(body);
+ let question = messages.iter().rposition(|m| m["role"] == "user").unwrap();
+ let index = usize::from(
+ messages[question]["content"]
+ .to_string()
+ .contains("second greeting"),
+ );
+ let tool_ran = messages[question + 1..].iter().any(|m| m["role"] == "tool");
+ let has_tools = body.get("tools").is_some();
+ let request_number = {
+ let mut requests = gates.requests.lock().unwrap();
+ requests.push(body);
+ requests.len()
+ };
+ if gates.pause == Pause::Legacy && has_tools {
+ return (
+ StatusCode::UNPROCESSABLE_ENTITY,
+ Json(json!({"error":{"message":"tools unsupported"}})),
+ )
+ .into_response();
+ }
+ let tool_turn = match gates.pause {
+ Pause::Legacy => false,
+ Pause::FinalAnswer => has_tools,
+ _ => !tool_ran,
+ };
+ let paused = match gates.pause {
+ Pause::BeforeText | Pause::BeforeTool => tool_turn,
+ Pause::DuringTool => false,
+ _ => !tool_turn,
+ };
let stream = async_stream::stream! {
- let payload = if tool_ran {
+ let text = if gates.pause == Pause::FinalAnswer && !tool_turn { DRAFT } else { PARTIAL };
+ let delta = json!({"choices":[{"delta":{"content":text}}]});
+ if gates.pause != Pause::BeforeText {
+ yield Ok::<_, Infallible>(Event::default().data(delta.to_string()));
+ }
+ if paused {
gates.ready[index].notify_one();
gates.release[index].notified().await;
- json!({"choices":[{"delta":{"content": if index == 0 {"First answer."} else {"Second answer."}}, "finish_reason":"stop"}]})
+ }
+ let payload = if tool_turn {
+ let (name, arguments) = match gates.pause {
+ Pause::FinalAnswer => ("find_entities", r#"{"name":"Acme"}"#),
+ Pause::DuringTool => ("search_chunks", r#"{"query":"Acme"}"#),
+ _ => ("no_evidence_needed", r#"{"reason":"greeting"}"#),
+ };
+ json!({"choices":[{"delta":{"tool_calls":[{"index":0,"id":format!("call_{request_number}"),"function":{"name":name,"arguments":arguments}}]},"finish_reason":"tool_calls"}]})
} else {
- json!({"choices":[{"delta":{"tool_calls":[{"index":0,"id":format!("greeting_{index}"),"function":{"name":"no_evidence_needed","arguments":"{\"reason\":\"greeting\"}"}}]},"finish_reason":"tool_calls"}]})
+ json!({"choices":[{"delta":{"content":if index == 0 {"First answer."} else {"Second answer."}},"finish_reason":"stop"}]})
};
yield Ok::<_, Infallible>(Event::default().data(payload.to_string()));
yield Ok::<_, Infallible>(Event::default().data("[DONE]"));
};
- Sse::new(stream)
+ Sse::new(stream).into_response()
}
-#[tokio::test]
-async fn finishing_old_chat_preserves_reattachment_to_new_chat() -> anyhow::Result<()> {
+struct ConnectionDropped(Arc<Notify>);
+
+impl Drop for ConnectionDropped {
+ fn drop(&mut self) {
+ self.0.notify_one();
+ }
+}
+
+async fn embedding(State(gates): State<Gates>, Json(body): Json<serde_json::Value>) -> Response {
+ assert_eq!(body["input"], json!(["Acme"]));
+ let stream = async_stream::stream! {
+ let _connection = ConnectionDropped(gates.tool_disconnected.clone());
+ yield Ok::<_, Infallible>(axum::body::Bytes::from_static(b"{\"data\":["));
+ // 服务端已发出部分响应;真实 search_chunks 工具仍停在 embed(),
+ // 等待这个 JSON 文档的剩余部分。
+ gates.tool_started.notify_one();
+ gates.release[0].notified().await;
+ yield Ok::<_, Infallible>(axum::body::Bytes::from_static(b"]}"));
+ };
+ (
+ [("content-type", "application/json")],
+ axum::body::Body::from_stream(stream),
+ )
+ .into_response()
+}
+
+type ModelServer = tokio::task::JoinHandle<std::io::Result<()>>;
+
+async fn setup(pause: Pause) -> anyhow::Result<Option<(Fx, Gates, ModelServer)>> {
let Some(f) = fixture(Scripted::new(vec![])).await? else {
- return Ok(());
+ return Ok(None);
+ };
+ let gates = Gates {
+ pause,
+ ready: Default::default(),
+ release: Default::default(),
+ requests: Default::default(),
+ tool_started: Default::default(),
+ tool_disconnected: Default::default(),
};
- let gates = Gates::default();
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let base = format!("http://{}", listener.local_addr()?);
let app = axum::Router::new()
.route("/chat/completions", axum::routing::post(upstream))
+ .route("/embeddings", axum::routing::post(embedding))
.with_state(gates.clone());
let server = tokio::spawn(async move { axum::serve(listener, app).await });
sqlx::query("UPDATE llm_settings SET chat_base_url=$1 WHERE workspace_id=(SELECT workspace_id FROM knowledge_bases WHERE id=$2)")
.bind(base).bind(f.kb).execute(&f.pool).await?;
+ if pause == Pause::DuringTool {
+ sqlx::query("UPDATE llm_settings SET embed_base_url=chat_base_url, embed_model='gated-embedding' WHERE workspace_id=(SELECT workspace_id FROM knowledge_bases WHERE id=$1)")
+ .bind(f.kb).execute(&f.pool).await?;
+ }
+ Ok(Some((f, gates, server)))
+}
+
+async fn start(f: &Fx, id: Uuid, question: &str) -> ApiResult<Response> {
+ chat(
+ State(f.state.clone()),
+ AuthUser(f.user.clone()),
+ Path(f.kb),
+ Json(ChatReq {
+ conversation_id: Some(id),
+ message: question.into(),
+ retry_message_id: None,
+ }),
+ )
+ .await
+ .map(IntoResponse::into_response)
+}
+async fn request_stop(f: &Fx, id: Uuid, generation_id: Uuid) -> anyhow::Result<()> {
+ let _ = stop(
+ State(f.state.clone()),
+ AuthUser(f.user.clone()),
+ Path((f.kb, id)),
+ Json(StopReq { generation_id }),
+ )
+ .await
+ .map_err(|_| anyhow::anyhow!("stop refused"))?;
+ Ok(())
+}
+
+async fn finished(rx: &mut broadcast::Receiver<Frame>) -> anyhow::Result<Vec<Frame>> {
+ let mut frames = Vec::new();
+ loop {
+ match rx.recv().await {
+ Ok(frame) => frames.push(frame),
+ Err(broadcast::error::RecvError::Closed) => return Ok(frames),
+ Err(error) => return Err(error.into()),
+ }
+ }
+}
+
+fn assert_done(frames: &[Frame], stopped: bool) {
+ let terminals: Vec<_> = frames
+ .iter()
+ .filter(|f| matches!(f.event, "done" | "error"))
+ .collect();
+ assert_eq!(terminals.len(), 1, "{frames:?}");
+ assert_eq!(terminals[0].event, "done", "{frames:?}");
+ let data: serde_json::Value = serde_json::from_str(&terminals[0].data).unwrap();
+ assert_eq!(data["stopped"].as_bool().unwrap_or(false), stopped);
+}
+
+#[tokio::test]
+async fn stop_saves_partial_text_and_stops_model_requests_at_each_stage() -> anyhow::Result<()> {
+ for (pause, expected_requests) in [
+ (Pause::BeforeText, 1),
+ (Pause::BeforeTool, 1),
+ (Pause::AfterTool, 2),
+ (Pause::Legacy, 3),
+ (Pause::FinalAnswer, 7),
+ ] {
+ let Some((f, gates, server)) = setup(pause).await? else {
+ return Ok(());
+ };
+ let result = tokio::time::timeout(std::time::Duration::from_secs(20), async {
+ let id = utopia_store::conversations::create(&f.pool, f.kb, f.user.id, "stop").await?;
+ let response = start(&f, id, "first greeting")
+ .await
+ .map_err(|_| anyhow::anyhow!("chat refused"))?;
+ let (snapshot, mut rx) = f.state.live.attach(id).await.unwrap();
+ gates.ready[0].notified().await;
+ // 上游可能在响应字节到达生成器前发信号。
+ // 等待正文真正发布,不依赖固定睡眠。
+ if pause != Pause::BeforeText && snapshot.content.is_empty() {
+ while rx.recv().await?.event != "delta" {}
+ }
+ request_stop(&f, id, snapshot.generation_id).await?;
+ request_stop(&f, id, snapshot.generation_id).await?;
+ let frames = finished(&mut rx).await?;
+ assert_done(&frames, true);
+ let body = axum::body::to_bytes(response.into_body(), 65536).await?;
+ let sse = String::from_utf8_lossy(&body);
+ anyhow::ensure!(
+ sse.matches("event: done").count() == 1 && !sse.contains("event: error"),
+ "{pause:?}: {sse}"
+ );
+ let messages = utopia_store::conversations::messages(&f.pool, id).await?;
+ anyhow::ensure!(messages.len() == 2 && messages[0].role == "user");
+ let answer = &messages[1];
+ anyhow::ensure!(answer.role == "assistant" && answer.stopped);
+ if pause == Pause::BeforeText {
+ anyhow::ensure!(answer.content.is_empty(), "stopped before the first token");
+ } else {
+ anyhow::ensure!(answer.content.contains(PARTIAL), "{pause:?}: {answer:?}");
+ }
+ anyhow::ensure!(
+ !answer.content.contains("First answer.") && !answer.content.contains(DRAFT)
+ );
+ anyhow::ensure!(f.state.live.attach(id).await.is_none());
+ anyhow::ensure!(
+ gates.requests.lock().unwrap().len() == expected_requests,
+ "{pause:?}: no request may start after Stop"
+ );
+ if pause == Pause::AfterTool {
+ let followup = start(&f, id, "second greeting")
+ .await
+ .map_err(|_| anyhow::anyhow!("follow-up refused after Stop"))?;
+ gates.ready[1].notified().await;
+ gates.release[1].notify_one();
+ let body = axum::body::to_bytes(followup.into_body(), 65536).await?;
+ anyhow::ensure!(String::from_utf8_lossy(&body).contains("Second answer."));
+ let messages = utopia_store::conversations::messages(&f.pool, id).await?;
+ anyhow::ensure!(messages.len() == 4 && messages[1].stopped && !messages[3].stopped);
+ }
+ Ok::<_, anyhow::Error>(())
+ })
+ .await;
+ gates.release[0].notify_one();
+ gates.release[1].notify_one();
+ server.abort();
+ let _ = server.await;
+ f.cleanup().await?;
+ result??;
+ }
+ Ok(())
+}
+
+#[tokio::test]
+async fn stop_during_a_real_search_tool_closes_its_http_request_and_saves_partial(
+) -> anyhow::Result<()> {
+ for retry in [false, true] {
+ let Some((f, gates, server)) = setup(Pause::DuringTool).await? else {
+ return Ok(());
+ };
+ let result = tokio::time::timeout(std::time::Duration::from_secs(20), async {
+ let id =
+ utopia_store::conversations::create(&f.pool, f.kb, f.user.id, "tool in flight")
+ .await?;
+ // 无回答的问题代表上一轮失败;重试必须沿用这个 id,不能再插入问题。
+ let question_id = if retry {
+ Some(
+ utopia_store::conversations::append_message(
+ &f.pool,
+ id,
+ "user",
+ "first greeting",
+ &utopia_store::conversations::TurnRecord::empty(),
+ )
+ .await?,
+ )
+ } else {
+ None
+ };
+ let response = chat(
+ State(f.state.clone()),
+ AuthUser(f.user.clone()),
+ Path(f.kb),
+ Json(ChatReq {
+ conversation_id: Some(id),
+ message: "first greeting".into(),
+ retry_message_id: question_id,
+ }),
+ )
+ .await
+ .map_err(|_| anyhow::anyhow!("chat refused"))?
+ .into_response();
+ let (snapshot, mut rx) = f.state.live.attach(id).await.unwrap();
+ gates.tool_started.notified().await;
+
+ let stored_question = utopia_store::conversations::messages(&f.pool, id).await?[0].id;
+ for retry_message_id in [None, Some(stored_question)] {
+ let busy = chat(
+ State(f.state.clone()),
+ AuthUser(f.user.clone()),
+ Path(f.kb),
+ Json(ChatReq {
+ conversation_id: Some(id),
+ message: "overlapping question".into(),
+ retry_message_id,
+ }),
+ )
+ .await
+ .map(IntoResponse::into_response)
+ .expect_err("new questions and retries share admission")
+ .into_response();
+ anyhow::ensure!(busy.status() == StatusCode::CONFLICT);
+ let body = axum::body::to_bytes(busy.into_body(), 4096).await?;
+ anyhow::ensure!(
+ serde_json::from_slice::<serde_json::Value>(&body)?["code"] == "answer_running"
+ );
+ }
+ request_stop(&f, id, snapshot.generation_id).await?;
+ // 保持 embedding 响应阻塞,只有取消才能关闭它。
+ gates.tool_disconnected.notified().await;
+ assert_done(&finished(&mut rx).await?, true);
+ let body = axum::body::to_bytes(response.into_body(), 65536).await?;
+ let sse = String::from_utf8_lossy(&body);
+ anyhow::ensure!(
+ sse.matches("event: done").count() == 1 && !sse.contains("event: error"),
+ "{sse}"
+ );
+ anyhow::ensure!(
+ !sse.contains("event: step"),
+ "the interrupted tool did not complete"
+ );
+ anyhow::ensure!(sse.contains(&format!("\"message_id\":\"{stored_question}\"")));
+ anyhow::ensure!(
+ sse.contains(&format!("\"generation_id\":\"{}\"", snapshot.generation_id))
+ );
+ let messages = utopia_store::conversations::messages(&f.pool, id).await?;
+ if let Some(question_id) = question_id {
+ anyhow::ensure!(messages[0].id == question_id);
+ }
+ anyhow::ensure!(messages.len() == 2 && messages[1].stopped);
+ anyhow::ensure!(messages[1].content == PARTIAL && messages[1].steps == json!([]));
+ let exchange: serde_json::Value =
+ sqlx::query_scalar("SELECT tool_exchange FROM conversation_messages WHERE id=$1")
+ .bind(messages[1].id)
+ .fetch_one(&f.pool)
+ .await?;
+ anyhow::ensure!(
+ exchange == json!([]),
+ "no unfinished tool call may enter the next turn"
+ );
+ anyhow::ensure!(
+ gates.requests.lock().unwrap().len() == 1,
+ "the stopped tool must not trigger another model turn"
+ );
+ anyhow::ensure!(f.state.live.attach(id).await.is_none());
+ Ok::<_, anyhow::Error>(())
+ })
+ .await;
+ gates.release[0].notify_one();
+ server.abort();
+ let _ = server.await;
+ f.cleanup().await?;
+ result??;
+ }
+ Ok(())
+}
+
+#[tokio::test]
+async fn disconnect_keeps_running_busy_does_not_append_and_stale_stop_spares_followup(
+) -> anyhow::Result<()> {
+ let Some((f, gates, server)) = setup(Pause::AfterTool).await? else {
+ return Ok(());
+ };
let result = tokio::time::timeout(std::time::Duration::from_secs(20), async {
- let first = chat(
- State(f.state.clone()), AuthUser(f.user.clone()), Path(f.kb),
- Json(ChatReq { conversation_id: None, message: "first greeting".into(), retry_message_id: None }),
- ).await.map_err(|_| anyhow::anyhow!("first chat refused"))?;
+ let id =
+ utopia_store::conversations::create(&f.pool, f.kb, f.user.id, "disconnect").await?;
+ let response = start(&f, id, "first greeting")
+ .await
+ .map_err(|_| anyhow::anyhow!("chat refused"))?;
gates.ready[0].notified().await;
- let id: Uuid = sqlx::query_scalar("SELECT id FROM conversations WHERE kb_id=$1")
- .bind(f.kb).fetch_one(&f.pool).await?;
- let (_, mut old_events) = f.state.live.attach(id).await.unwrap();
- let second = chat(
- State(f.state.clone()), AuthUser(f.user.clone()), Path(f.kb),
- Json(ChatReq { conversation_id: Some(id), message: "second greeting".into(), retry_message_id: None }),
- ).await.map_err(|_| anyhow::anyhow!("second chat refused"))?;
- gates.ready[1].notified().await;
+ let (first, mut rx) = f.state.live.attach(id).await.unwrap();
+ drop(response);
+ let busy = start(&f, id, "second greeting")
+ .await
+ .expect_err("one generation per conversation")
+ .into_response();
+ anyhow::ensure!(busy.status() == StatusCode::CONFLICT);
+ let body = axum::body::to_bytes(busy.into_body(), 4096).await?;
+ let body: serde_json::Value = serde_json::from_slice(&body)?;
+ anyhow::ensure!(body["code"] == "answer_running");
+ anyhow::ensure!(
+ utopia_store::conversations::messages(&f.pool, id)
+ .await?
+ .len()
+ == 1,
+ "409 must not write the follow-up question"
+ );
gates.release[0].notify_one();
- let first_body = axum::body::to_bytes(first.into_response().into_body(), 65536).await?;
- anyhow::ensure!(String::from_utf8_lossy(&first_body).contains("First answer."));
- // The old sender closing proves its producer has actually called finish;
- // merely observing done would still leave a scheduling window before it.
- while !matches!(old_events.recv().await, Err(broadcast::error::RecvError::Closed)) {}
- let attached = reattach(
- State(f.state.clone()), AuthUser(f.user.clone()), Path((f.kb, id)),
- ).await.map_err(|_| anyhow::anyhow!("reattach refused"))?;
+ assert_done(&finished(&mut rx).await?, false);
+
+ let response = start(&f, id, "second greeting")
+ .await
+ .map_err(|_| anyhow::anyhow!("follow-up refused after completion"))?;
+ gates.ready[1].notified().await;
+ let (second, mut rx) = f.state.live.attach(id).await.unwrap();
+ anyhow::ensure!(first.generation_id != second.generation_id);
+ request_stop(&f, id, first.generation_id).await?;
+ let mut other = f.user.clone();
+ other.id = Uuid::now_v7();
+ let unauthorized = stop(
+ State(f.state.clone()),
+ AuthUser(other),
+ Path((f.kb, id)),
+ Json(StopReq {
+ generation_id: second.generation_id,
+ }),
+ )
+ .await;
+ anyhow::ensure!(
+ unauthorized
+ .expect_err("only conversation owner may stop")
+ .into_response()
+ .status()
+ == StatusCode::NOT_FOUND
+ );
+ let wrong_base = stop(
+ State(f.state.clone()),
+ AuthUser(f.user.clone()),
+ Path((Uuid::now_v7(), id)),
+ Json(StopReq {
+ generation_id: second.generation_id,
+ }),
+ )
+ .await;
+ anyhow::ensure!(
+ wrong_base
+ .expect_err("conversation belongs to its own base")
+ .into_response()
+ .status()
+ == StatusCode::NOT_FOUND
+ );
gates.release[1].notify_one();
- let attached_body = axum::body::to_bytes(attached.into_response().into_body(), 65536).await?;
- let attached_text = String::from_utf8_lossy(&attached_body);
- let second_body = axum::body::to_bytes(second.into_response().into_body(), 65536).await?;
- let second_text = String::from_utf8_lossy(&second_body);
- anyhow::ensure!(second_text.contains("Second answer."), "{second_text}");
- anyhow::ensure!(attached_text.contains("event: snapshot"), "new answer must remain attachable: {attached_text}");
- anyhow::ensure!(attached_text.contains("Second answer."), "{attached_text}");
- anyhow::ensure!(attached_text.contains("event: done"), "{attached_text}");
- anyhow::ensure!(!attached_text.contains("First answer."));
- anyhow::ensure!(!attached_text.contains("event: idle"));
- anyhow::ensure!(gates.requests.lock().unwrap().len() == 4, "one tool turn and one answer per generation");
- let answers: Vec<String> = sqlx::query_scalar("SELECT content FROM conversation_messages WHERE conversation_id=$1 AND role='assistant' ORDER BY created_at")
- .bind(id).fetch_all(&f.pool).await?;
- anyhow::ensure!(answers == ["First answer.", "Second answer."], "both answers must persist: {answers:?}");
+ assert_done(&finished(&mut rx).await?, false);
+ let body = axum::body::to_bytes(response.into_body(), 65536).await?;
+ anyhow::ensure!(String::from_utf8_lossy(&body).contains("Second answer."));
+ let messages = utopia_store::conversations::messages(&f.pool, id).await?;
+ let roles: Vec<_> = messages.iter().map(|m| m.role.as_str()).collect();
+ anyhow::ensure!(roles == ["user", "assistant", "user", "assistant"]);
+ anyhow::ensure!(messages[1].content.contains("First answer.") && !messages[1].stopped);
+ anyhow::ensure!(messages[3].content.contains("Second answer.") && !messages[3].stopped);
Ok::<_, anyhow::Error>(())
- }).await;
+ })
+ .await;
gates.release[0].notify_one();
gates.release[1].notify_one();
server.abort();
@@ -96,3 +473,130 @@ async fn finishing_old_chat_preserves_reattachment_to_new_chat() -> anyhow::Resu
result??;
Ok(())
}
+
+#[tokio::test]
+async fn stop_after_save_starts_keeps_one_complete_answer_and_holds_busy_until_commit(
+) -> anyhow::Result<()> {
+ let Some((f, gates, server)) = setup(Pause::AfterTool).await? else {
+ return Ok(());
+ };
+ let result = tokio::time::timeout(std::time::Duration::from_secs(20), async {
+ let id =
+ utopia_store::conversations::create(&f.pool, f.kb, f.user.id, "commit race").await?;
+ let response = start(&f, id, "first greeting")
+ .await
+ .map_err(|_| anyhow::anyhow!("chat refused"))?;
+ gates.ready[0].notified().await;
+ let (snapshot, mut rx) = f.state.live.attach(id).await.unwrap();
+
+ // 让助手消息 INSERT 成功,再阻塞 append_message 的 UPDATE。
+ // 观察真实数据库锁等待,确认发送 Stop 前保存已经开始。
+ let mut lock = f.pool.begin().await?;
+ sqlx::query("SELECT id FROM conversations WHERE id=$1 FOR NO KEY UPDATE")
+ .bind(id)
+ .execute(&mut *lock)
+ .await?;
+ let blocker: i32 = sqlx::query_scalar("SELECT pg_backend_pid()")
+ .fetch_one(&mut *lock)
+ .await?;
+ gates.release[0].notify_one();
+ while !sqlx::query_scalar::<_, bool>(
+ "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE $1 = ANY(pg_blocking_pids(pid)))",
+ )
+ .bind(blocker)
+ .fetch_one(&f.pool)
+ .await?
+ {}
+
+ request_stop(&f, id, snapshot.generation_id).await?;
+ let busy = start(&f, id, "second greeting")
+ .await
+ .expect_err("saving still owns the conversation")
+ .into_response();
+ anyhow::ensure!(busy.status() == StatusCode::CONFLICT);
+ anyhow::ensure!(
+ utopia_store::conversations::messages(&f.pool, id)
+ .await?
+ .len()
+ == 1,
+ "the unfinished save is invisible and the refused follow-up was not appended"
+ );
+
+ lock.commit().await?;
+ assert_done(&finished(&mut rx).await?, false);
+ let body = axum::body::to_bytes(response.into_body(), 65536).await?;
+ anyhow::ensure!(String::from_utf8_lossy(&body).contains("First answer."));
+ let messages = utopia_store::conversations::messages(&f.pool, id).await?;
+ anyhow::ensure!(
+ messages.len() == 2,
+ "completion and Stop must not both write an answer"
+ );
+ anyhow::ensure!(messages[1].role == "assistant" && !messages[1].stopped);
+ anyhow::ensure!(messages[1].content.ends_with("First answer."));
+ anyhow::ensure!(f.state.live.attach(id).await.is_none());
+ anyhow::ensure!(gates.requests.lock().unwrap().len() == 2);
+ Ok::<_, anyhow::Error>(())
+ })
+ .await;
+ gates.release[0].notify_one();
+ server.abort();
+ let _ = server.await;
+ f.cleanup().await?;
+ result??;
+ Ok(())
+}
+
+#[tokio::test]
+async fn stopped_answer_save_failure_emits_error_and_releases_the_conversation(
+) -> anyhow::Result<()> {
+ let Some((f, gates, server)) = setup(Pause::AfterTool).await? else {
+ return Ok(());
+ };
+ let trigger = format!("reject_stopped_{}", f.kb.simple());
+ sqlx::raw_sql(&format!("CREATE FUNCTION {trigger}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.role='assistant' AND EXISTS(SELECT 1 FROM conversations WHERE id=NEW.conversation_id AND kb_id='{}') THEN RAISE EXCEPTION 'private stop persistence diagnostic'; END IF; RETURN NEW; END $$; CREATE TRIGGER {trigger} BEFORE INSERT ON conversation_messages FOR EACH ROW EXECUTE FUNCTION {trigger}();", f.kb)).execute(&f.pool).await?;
+ let result = tokio::time::timeout(std::time::Duration::from_secs(20), async {
+ let id =
+ utopia_store::conversations::create(&f.pool, f.kb, f.user.id, "save failure").await?;
+ let response = start(&f, id, "first greeting")
+ .await
+ .map_err(|_| anyhow::anyhow!("chat refused"))?;
+ gates.ready[0].notified().await;
+ let (snapshot, mut rx) = f.state.live.attach(id).await.unwrap();
+ request_stop(&f, id, snapshot.generation_id).await?;
+ let frames = finished(&mut rx).await?;
+ let terminals: Vec<_> = frames
+ .iter()
+ .filter(|f| matches!(f.event, "done" | "error"))
+ .collect();
+ anyhow::ensure!(
+ terminals.len() == 1 && terminals[0].event == "error",
+ "{frames:?}"
+ );
+ let error: serde_json::Value = serde_json::from_str(&terminals[0].data)?;
+ anyhow::ensure!(error["code"] == "answer_not_saved");
+ anyhow::ensure!(!terminals[0]
+ .data
+ .contains("private stop persistence diagnostic"));
+ anyhow::ensure!(f.state.live.attach(id).await.is_none());
+ anyhow::ensure!(
+ utopia_store::conversations::messages(&f.pool, id)
+ .await?
+ .len()
+ == 1
+ );
+ drop(response);
+ Ok::<_, anyhow::Error>(())
+ })
+ .await;
+ gates.release[0].notify_one();
+ server.abort();
+ let _ = server.await;
+ sqlx::raw_sql(&format!(
+ "DROP TRIGGER {trigger} ON conversation_messages; DROP FUNCTION {trigger}();"
+ ))
+ .execute(&f.pool)
+ .await?;
+ f.cleanup().await?;
+ result??;
+ Ok(())
+}
diff --git a/crates/utopia-server/src/api/chat_restated_tests.rs b/crates/utopia-server/src/api/chat_restated_tests.rs
index 18e695cc..1940a830 100644
--- a/crates/utopia-server/src/api/chat_restated_tests.rs
+++ b/crates/utopia-server/src/api/chat_restated_tests.rs
@@ -100,6 +100,64 @@ async fn a_restatement_keeps_the_sources_it_repeats() -> anyhow::Result<()> {
f.cleanup().await
}
+#[tokio::test]
+async fn a_stopped_restatement_keeps_its_sources_live_and_after_reload() -> anyhow::Result<()> {
+ let document = Uuid::now_v7();
+ let Some(f) = fixture(Scripted::new(vec![Reply::Document(document), FIRST])).await? else {
+ return Ok(());
+ };
+ seed_document(&f, document).await?;
+ let first = f.ask("What is the documented target?").await?;
+ assert!(first.contains("event: done"), "{first}");
+ let id: Uuid = sqlx::query_scalar("SELECT id FROM conversations WHERE kb_id = $1")
+ .bind(f.kb)
+ .fetch_one(&f.pool)
+ .await?;
+ let history = utopia_store::conversations::recent_context(&f.pool, id, 20).await?;
+ let previous_answer = history.turns.last().unwrap().1.clone();
+ let expected_sources = json!(history.last_sources);
+ assert!(!expected_sources.as_array().unwrap().is_empty());
+
+ let handle = f.state.live.begin(id).await?;
+ let cancellation = handle.cancellation();
+ let response = sse_from(f.state.live.attach(id).await);
+ utopia_store::conversations::append_message(
+ &f.pool,
+ id,
+ "user",
+ "Say it shorter",
+ &utopia_store::conversations::TurnRecord::empty(),
+ )
+ .await?;
+ // 改写正文的增量发出后触发真实的停止与保存流程;上游保持等待,
+ // 因此正常完成的答案不能让这个测试误通过。
+ let producer = async_stream::stream! {
+ yield ProducerEvent::Progress(delta_event("Target: 95% [1].\n"));
+ cancellation.cancel();
+ std::future::pending::<()>().await;
+ };
+ generation::run(
+ f.pool.clone(),
+ id,
+ handle,
+ producer,
+ previous_answer,
+ history.last_sources,
+ )
+ .await;
+
+ let body = axum::body::to_bytes(response.into_response().into_body(), 65536).await?;
+ let sse = String::from_utf8_lossy(&body);
+ assert!(sse.contains("\"stopped\":true"), "{sse}");
+ assert_eq!(live_sources(&sse), expected_sources);
+ let messages = utopia_store::conversations::messages(&f.pool, id).await?;
+ assert_eq!(messages.len(), 4);
+ assert!(messages[3].stopped);
+ assert_eq!(messages[3].content, "Target: 95% [1].\n");
+ assert_eq!(messages[3].sources, expected_sources);
+ f.cleanup().await
+}
+
#[tokio::test]
async fn a_restatement_citing_a_number_the_last_answer_did_not_keeps_none() -> anyhow::Result<()> {
let document = Uuid::now_v7();
diff --git a/crates/utopia-server/src/api/chat_retry_tests.rs b/crates/utopia-server/src/api/chat_retry_tests.rs
index 96e3d4e2..c37c8bbd 100644
--- a/crates/utopia-server/src/api/chat_retry_tests.rs
+++ b/crates/utopia-server/src/api/chat_retry_tests.rs
@@ -203,7 +203,7 @@ async fn a_question_is_not_answered_twice_at_once() -> anyhow::Result<()> {
let conversation_id =
utopia_store::conversations::create(&f.pool, f.kb, f.user.id, QUESTION).await?;
let question = append(&f, conversation_id, "user", QUESTION).await?;
- let running = f.state.live.begin(conversation_id).await;
+ let running = f.state.live.begin(conversation_id).await.unwrap();
let Outcome::Refused(status, body) = post(&f, retry(conversation_id, question)).await? else {
panic!("a retry started while an answer was running");
};
@@ -215,7 +215,7 @@ async fn a_question_is_not_answered_twice_at_once() -> anyhow::Result<()> {
assert!(f.requests().is_empty(), "the model is not asked");
assert_eq!(stored(&f, conversation_id).await?.len(), 1);
- running.finish().await;
+ drop(running);
let Outcome::Streamed(sse) = post(&f, retry(conversation_id, question)).await? else {
panic!("the retry was refused after the running answer finished");
};
diff --git a/crates/utopia-server/src/api/chat_stream_tests.rs b/crates/utopia-server/src/api/chat_stream_tests.rs
index 8e1311f6..29f5fc35 100644
--- a/crates/utopia-server/src/api/chat_stream_tests.rs
+++ b/crates/utopia-server/src/api/chat_stream_tests.rs
@@ -4,17 +4,17 @@ use std::sync::Arc;
#[tokio::test]
async fn reattachment_preserves_terminal_frames_on_both_sides_of_emit() {
- for event in ["done", "error"] {
+ for (event, data) in [("done", r#"{"stopped":true}"#), ("error", "safe outcome")] {
let registry = Arc::new(crate::live::Registry::default());
let id = Uuid::now_v7();
- let handle = registry.begin(id).await;
+ let handle = registry.begin(id).await.unwrap();
handle.emit(delta_event("partial")).await;
let before = sse_from(registry.attach(id).await);
- handle.emit(Frame::new(event, "safe outcome".into())).await;
- // Finish closes the late receiver on the old implementation, so this
- // counterexample completes without timing out even when terminal is lost.
+ handle.emit(Frame::new(event, data.into())).await;
+ // 释放把手会关闭旧实现中的迟到接收端,
+ // 即使丢失终态,这个反例也能结束而不必等超时。
let after = sse_from(registry.attach(id).await);
- handle.finish().await;
+ drop(handle);
for response in [before, after] {
let body = axum::body::to_bytes(response.into_response().into_body(), 65536)
.await
@@ -25,7 +25,7 @@ async fn reattachment_preserves_terminal_frames_on_both_sides_of_emit() {
1,
"{text}"
);
- assert!(text.contains("partial") && text.contains("safe outcome"));
+ assert!(text.contains("partial") && text.contains(data));
}
let idle = axum::body::to_bytes(
sse_from(registry.attach(id).await)
@@ -43,12 +43,12 @@ async fn reattachment_preserves_terminal_frames_on_both_sides_of_emit() {
async fn lagged_subscribers_receive_an_error_not_done() {
let registry = Arc::new(crate::live::Registry::default());
let id = Uuid::now_v7();
- let handle = registry.begin(id).await;
+ let handle = registry.begin(id).await.unwrap();
let stream = sse_from(registry.attach(id).await);
for _ in 0..300 {
handle.emit(delta_event("x")).await;
}
- handle.finish().await;
+ drop(handle);
let body = axum::body::to_bytes(stream.into_response().into_body(), 65536)
.await
.unwrap();
@@ -60,10 +60,10 @@ async fn lagged_subscribers_receive_an_error_not_done() {
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 handle = registry.begin(id).await.unwrap();
let stream = sse_from(registry.attach(id).await);
handle.emit(delta_event("partial")).await;
- handle.finish().await;
+ drop(handle);
let body = tokio::time::timeout(
std::time::Duration::from_secs(2),
axum::body::to_bytes(stream.into_response().into_body(), 65536),
@@ -82,16 +82,16 @@ async fn producer_disappearing_without_an_outcome_ends_in_one_error() {
async fn first_terminal_freezes_the_snapshot_and_broadcast() {
let registry = Arc::new(crate::live::Registry::default());
let id = Uuid::now_v7();
- let handle = registry.begin(id).await;
+ let handle = registry.begin(id).await.unwrap();
handle.emit(delta_event("kept")).await;
handle
.emit(error_event("answer_failed", "original error"))
.await;
handle.emit(delta_event("discarded")).await;
- handle.emit(done_event()).await;
+ handle.emit(Frame::new("done", "{}".into())).await;
let (snapshot, _) = registry.attach(id).await.unwrap();
assert_eq!(snapshot.content, "kept");
assert_eq!(snapshot.terminal().unwrap().event, "error");
assert!(!snapshot.to_frame().data.contains("terminal"));
- handle.finish().await;
+ drop(handle);
}
diff --git a/crates/utopia-server/src/api/mod.rs b/crates/utopia-server/src/api/mod.rs
index a1ab68d5..bc9b1a08 100644
--- a/crates/utopia-server/src/api/mod.rs
+++ b/crates/utopia-server/src/api/mod.rs
@@ -415,6 +415,7 @@ pub fn router(state: AppState, cfg: &AppConfig) -> Router {
)
.route("/kbs/{id}/search", post(search_routes::search))
.route("/kbs/{id}/chat", post(chat::chat))
+ .route("/kbs/{id}/chat/{conversation_id}/stop", post(chat::stop))
// 刷新页面后重新接上正在生成的那个回答(见 `live`)
.route(
"/kbs/{id}/conversations/{conversation_id}/stream",
diff --git a/crates/utopia-server/src/live.rs b/crates/utopia-server/src/live.rs
index e8a7e508..f92deedb 100644
--- a/crates/utopia-server/src/live.rs
+++ b/crates/utopia-server/src/live.rs
@@ -13,8 +13,9 @@
//! 不必去想"我重放到哪一条了"。
use serde_json::json;
use std::collections::HashMap;
-use std::sync::Arc;
-use tokio::sync::{broadcast, RwLock};
+use std::sync::{Arc, Mutex};
+use tokio::sync::{broadcast, watch, RwLock};
+use utopia_core::{AppError, AppResult};
use uuid::Uuid;
/// 一个 SSE 事件:事件名 + 已经序列化好的 data。
@@ -36,6 +37,7 @@ impl Frame {
/// 到此刻为止这个回答长什么样。接上的人先拿到它。
#[derive(Clone, Default, Debug)]
pub struct Snapshot {
+ pub generation_id: Uuid,
pub content: String,
pub steps: Vec<serde_json::Value>,
pub sources: Vec<serde_json::Value>,
@@ -77,6 +79,7 @@ impl Snapshot {
Frame::new(
"snapshot",
json!({
+ "generation_id": self.generation_id,
"content": self.content,
"steps": self.steps,
"sources": self.sources,
@@ -87,29 +90,65 @@ impl Snapshot {
}
struct Entry {
+ generation_id: Uuid,
tx: broadcast::Sender<Frame>,
snap: Arc<RwLock<Snapshot>>,
+ cancellation: Cancellation,
}
-/// 登记还在、终态还没发出,才算在跑。发出终态之后到生成者注销之间还有一小段,
-/// 那时的登记已经说完了:出错之后马上按重试,不该被它挡住
-async fn running(entry: &Entry) -> bool {
- entry.snap.read().await.terminal.is_none()
+/// 保留取消信号:Stop 可能早于生成器开始等待。
+#[derive(Clone)]
+pub struct Cancellation(watch::Sender<bool>);
+
+impl Default for Cancellation {
+ fn default() -> Self {
+ Self(watch::channel(false).0)
+ }
}
-/// 进行中的生成,按会话查。
+impl Cancellation {
+ pub fn cancel(&self) {
+ self.0.send_replace(true);
+ }
+
+ pub fn is_cancelled(&self) -> bool {
+ *self.0.borrow()
+ }
+
+ pub async fn cancelled(&self) {
+ let mut receiver = self.0.subscribe();
+ let _ = receiver.wait_for(|cancelled| *cancelled).await;
+ }
+}
+
+/// 按会话登记进行中的生成,在写问题前占位。注册表锁不跨越 await;
+/// 使用同步锁,让准备失败时的 Drop 也能释放占位。
#[derive(Default)]
-pub struct Registry(RwLock<HashMap<Uuid, Entry>>);
+pub struct Registry(Mutex<HashMap<Uuid, Entry>>);
/// 一次生成期间握着的把手。发事件、结束时注销。
pub struct Handle {
conversation_id: Uuid,
+ generation_id: Uuid,
tx: broadcast::Sender<Frame>,
snap: Arc<RwLock<Snapshot>>,
+ cancellation: Cancellation,
registry: Arc<Registry>,
}
impl Handle {
+ pub fn generation_id(&self) -> Uuid {
+ self.generation_id
+ }
+
+ pub fn cancellation(&self) -> Cancellation {
+ self.cancellation.clone()
+ }
+
+ pub async fn snapshot(&self) -> Snapshot {
+ self.snap.read().await.clone()
+ }
+
/// 发一个事件:记进快照,然后广播。
///
/// **广播时仍然握着快照的写锁**,这一点是必需的。只保证「先写后发」
@@ -118,8 +157,8 @@ impl Handle {
/// 于是接上的时刻要么整个在这次 emit 之前,要么整个在它之后
pub async fn emit(&self, frame: Frame) {
let mut snap = self.snap.write().await;
- // The snapshot and the subscription boundary must include the terminal:
- // a subscriber arriving after this broadcast still needs the same outcome.
+ // 终态也必须记进快照并遵守订阅边界:
+ // 广播后才接入的订阅者仍需要收到同一个结果。
if snap.terminal.is_some() {
return;
}
@@ -131,12 +170,20 @@ impl Handle {
let _ = self.tx.send(frame);
}
- /// 生成结束,只注销自己仍持有的登记;新一轮可能已经接替了它。
- /// 当前生成注销后,接上的人得到「没有在跑的」,答案从库里读。
- pub async fn finish(self) {
- let mut entries = self.registry.0.write().await;
- // A newer begin may have replaced this conversation while we were running.
- // Check identity and remove under one lock, so an old producer only retires itself.
+ /// 持久化完成后,先释放会话,再通知订阅者可以继续提问;
+ /// 快照和订阅仍保持同一个边界。
+ pub async fn complete(self, terminal: Frame) {
+ let mut snap = self.snap.write().await;
+ self.retire();
+ if snap.terminal.is_some() {
+ return;
+ }
+ snap.terminal = Some(terminal.clone());
+ let _ = self.tx.send(terminal);
+ }
+
+ fn retire(&self) {
+ let mut entries = self.registry.0.lock().expect("live registry lock");
if entries
.get(&self.conversation_id)
.is_some_and(|entry| Arc::ptr_eq(&entry.snap, &self.snap))
@@ -146,53 +193,56 @@ impl Handle {
}
}
-impl Registry {
- /// 登记一次生成。同一个会话重复登记会顶掉旧的——正常情况下不会发生,
- /// 真发生了也是新的那次说了算
- pub async fn begin(self: &Arc<Self>, conversation_id: Uuid) -> Handle {
- let mut entries = self.0.write().await;
- self.register(&mut entries, conversation_id)
- }
-
- /// 只在这个会话没有在跑的生成时登记(#936 的重答)。**查和登记在同一把写锁下**:
- /// 连按两次重试,只有一次拿得到把手,另一次得到 `None`
- pub async fn begin_if_idle(self: &Arc<Self>, conversation_id: Uuid) -> Option<Handle> {
- let mut entries = self.0.write().await;
- if let Some(entry) = entries.get(&conversation_id) {
- if running(entry).await {
- return None;
- }
- }
- Some(self.register(&mut entries, conversation_id))
+impl Drop for Handle {
+ fn drop(&mut self) {
+ self.retire();
}
+}
- /// 这个会话此刻有没有在跑的生成
- pub async fn is_running(&self, conversation_id: Uuid) -> bool {
- match self.0.read().await.get(&conversation_id) {
- Some(entry) => running(entry).await,
- None => false,
+impl Registry {
+ /// 检查和占位在同一把锁下,新问题与重试都只允许一个生成者。
+ pub async fn begin(self: &Arc<Self>, conversation_id: Uuid) -> AppResult<Handle> {
+ let mut entries = self.0.lock().expect("live registry lock");
+ if entries.contains_key(&conversation_id) {
+ return Err(AppError::CodedConflict {
+ code: "answer_running",
+ message: "This conversation is still generating an answer.".into(),
+ });
}
- }
-
- fn register(
- self: &Arc<Self>,
- entries: &mut HashMap<Uuid, Entry>,
- conversation_id: Uuid,
- ) -> Handle {
+ let generation_id = Uuid::now_v7();
let (tx, _) = broadcast::channel(256);
- let snap = Arc::new(RwLock::new(Snapshot::default()));
+ let snap = Arc::new(RwLock::new(Snapshot {
+ generation_id,
+ ..Snapshot::default()
+ }));
+ let cancellation = Cancellation::default();
entries.insert(
conversation_id,
Entry {
+ generation_id,
tx: tx.clone(),
snap: snap.clone(),
+ cancellation: cancellation.clone(),
},
);
- Handle {
+ Ok(Handle {
conversation_id,
+ generation_id,
tx,
snap,
+ cancellation,
registry: self.clone(),
+ })
+ }
+
+ /// 延迟或重复的 Stop 不能取消下一轮生成。
+ pub async fn stop(&self, conversation_id: Uuid, generation_id: Uuid) {
+ let entries = self.0.lock().expect("live registry lock");
+ let Some(entry) = entries.get(&conversation_id) else {
+ return;
+ };
+ if entry.generation_id == generation_id {
+ entry.cancellation.cancel();
}
}
@@ -203,13 +253,16 @@ impl Registry {
&self,
conversation_id: Uuid,
) -> Option<(Snapshot, broadcast::Receiver<Frame>)> {
- let map = self.0.read().await;
- let entry = map.get(&conversation_id)?;
+ let (snap, tx) = {
+ let map = self.0.lock().expect("live registry lock");
+ let entry = map.get(&conversation_id)?;
+ (entry.snap.clone(), entry.tx.clone())
+ };
// **握着快照的读锁再订阅。** `emit` 是握着写锁广播的,所以这一段
// 与任何一次 emit 互斥:拿到的快照与订阅起点严丝合缝,
// 中间那一小段既不会漏、也不会重
- let guard = entry.snap.read().await;
- let rx = entry.tx.subscribe();
+ let guard = snap.read().await;
+ let rx = tx.subscribe();
Some((guard.clone(), rx))
}
}
@@ -223,17 +276,18 @@ mod tests {
}
#[tokio::test]
- async fn replaced_handle_cannot_unregister_or_write_into_current_generation() {
+ async fn retired_handle_cannot_unregister_or_write_into_current_generation() {
let registry = Arc::new(Registry::default());
let id = Uuid::now_v7();
- let old = registry.begin(id).await;
+ let old = registry.begin(id).await.unwrap();
old.emit(delta("old")).await;
- let current = registry.begin(id).await;
+ old.retire();
+ let current = registry.begin(id).await.unwrap();
current.emit(delta("new")).await;
let (snapshot, mut rx) = registry.attach(id).await.unwrap();
assert_eq!(snapshot.content, "new");
old.emit(delta("late old text")).await;
- old.finish().await;
+ drop(old);
let (snapshot, _) = registry
.attach(id)
.await
@@ -245,7 +299,7 @@ mod tests {
));
current.emit(delta(" answer")).await;
assert_eq!(rx.recv().await.unwrap().data, delta(" answer").data);
- current.finish().await;
+ drop(current);
assert!(registry.attach(id).await.is_none());
}
@@ -262,15 +316,19 @@ mod tests {
let registry = Arc::new(Registry::default());
let id = Uuid::now_v7();
let other_id = Uuid::now_v7();
- let other = registry.begin(other_id).await;
+ let other = registry.begin(other_id).await.unwrap();
other.emit(delta("unrelated")).await;
let mut handles = Vec::new();
- for _ in 0..3 {
- handles.push(Some(registry.begin(id).await));
+ for index in 0..3 {
+ let handle = registry.begin(id).await.unwrap();
+ if index < 2 {
+ handle.retire();
+ }
+ handles.push(Some(handle));
}
let mut current_finished = false;
for index in order {
- handles[index].take().unwrap().finish().await;
+ drop(handles[index].take().unwrap());
current_finished |= index == 2;
assert_eq!(
registry.attach(id).await.is_none(),
@@ -282,7 +340,7 @@ mod tests {
"unrelated"
);
}
- other.finish().await;
+ drop(other);
assert!(registry.attach(other_id).await.is_none());
}
}
@@ -291,9 +349,9 @@ mod tests {
async fn snapshot_and_subscription_partition_concurrent_emission() {
let registry = Arc::new(Registry::default());
let id = Uuid::now_v7();
- let handle = registry.begin(id).await;
- // Either lock acquisition order is legal: each delta must occur exactly once
- // across the snapshot and the subscription, never in both or neither.
+ let handle = registry.begin(id).await.unwrap();
+ // 两种拿锁顺序都合法:每个增量必须恰好出现在快照或订阅中一次,
+ // 既不能重复,也不能遗漏。
for index in 0..64 {
let attached = if index % 2 == 0 {
tokio::join!(handle.emit(delta("x")), registry.attach(id)).1
@@ -311,6 +369,54 @@ mod tests {
}
assert_eq!(combined, registry.attach(id).await.unwrap().0.content);
}
- handle.finish().await;
+ drop(handle);
+ }
+
+ #[tokio::test]
+ async fn admission_is_exclusive_and_dropping_a_reservation_releases_it() {
+ let registry = Arc::new(Registry::default());
+ let id = Uuid::now_v7();
+ let (first, second) = tokio::join!(registry.begin(id), registry.begin(id));
+ assert_ne!(first.is_ok(), second.is_ok());
+ let (handle, error) = match (first, second) {
+ (Ok(handle), Err(error)) | (Err(error), Ok(handle)) => (handle, error),
+ _ => unreachable!(),
+ };
+ assert!(matches!(
+ error,
+ AppError::CodedConflict {
+ code: "answer_running",
+ ..
+ }
+ ));
+ let old_generation = handle.generation_id();
+ drop(handle);
+ assert!(registry.attach(id).await.is_none());
+
+ let current = registry.begin(id).await.unwrap();
+ let cancellation = current.cancellation();
+ registry.stop(id, old_generation).await;
+ assert!(!cancellation.is_cancelled());
+ registry.stop(id, current.generation_id()).await;
+ registry.stop(id, current.generation_id()).await;
+ tokio::time::timeout(std::time::Duration::from_secs(1), cancellation.cancelled())
+ .await
+ .expect("Stop is retained even before the first waiter");
+ }
+
+ #[tokio::test]
+ async fn the_terminal_is_sent_after_the_conversation_is_available() {
+ let registry = Arc::new(Registry::default());
+ let id = Uuid::now_v7();
+ let handle = registry.begin(id).await.unwrap();
+ let (_, mut receiver) = registry.attach(id).await.unwrap();
+ handle
+ .complete(Frame::new("done", json!({"stopped":true}).to_string()))
+ .await;
+ assert_eq!(receiver.recv().await.unwrap().event, "done");
+ let _next = registry
+ .begin(id)
+ .await
+ .expect("a subscriber may immediately follow up");
}
}
diff --git a/crates/utopia-store/src/conversations.rs b/crates/utopia-store/src/conversations.rs
index bfdd1291..adc1fabb 100644
--- a/crates/utopia-store/src/conversations.rs
+++ b/crates/utopia-store/src/conversations.rs
@@ -116,7 +116,7 @@ pub async fn require_owned(
pub async fn messages(pool: &PgPool, conversation_id: Uuid) -> AppResult<Vec<ConversationMessage>> {
let rows: Vec<ConversationMessage> = sqlx::query_as(
- "SELECT id, role, content, steps, sources, created_at
+ "SELECT id, role, content, steps, sources, stopped, created_at
FROM conversation_messages WHERE conversation_id = $1 ORDER BY created_at",
)
.bind(conversation_id)
@@ -200,11 +200,12 @@ struct ContextRow {
resolved: serde_json::Value,
tool_exchange: serde_json::Value,
sources: serde_json::Value,
+ stopped: bool,
}
pub async fn recent_context(pool: &PgPool, conversation_id: Uuid, n: i64) -> AppResult<History> {
let mut rows: Vec<ContextRow> = sqlx::query_as(
- "SELECT id, role, content, resolved, tool_exchange, sources FROM conversation_messages
+ "SELECT id, role, content, resolved, tool_exchange, sources, stopped FROM conversation_messages
WHERE conversation_id = $1 ORDER BY created_at DESC LIMIT $2",
)
.bind(conversation_id)
@@ -236,7 +237,15 @@ pub async fn recent_context(pool: &PgPool, conversation_id: Uuid, n: i64) -> App
turn_ids: rows.iter().map(|row| row.id).collect(),
turns: rows
.into_iter()
- .map(|row| (row.role, row.content))
+ .map(|mut row| {
+ if row.stopped {
+ row.content = format!(
+ "[This response was stopped by the user before completion.]\n\n{}",
+ row.content
+ );
+ }
+ (row.role, row.content)
+ })
.collect(),
entities,
last_tool_exchange,
@@ -250,6 +259,8 @@ pub async fn recent_context(pool: &PgPool, conversation_id: Uuid, n: i64) -> App
/// 一条能落库、也能读回来、只是内容张冠李戴的记录。与 `RelationAxioms` 同一条理由。
#[derive(Default)]
pub struct TurnRecord {
+ /// 用户明确停止了这一轮,正文可能只有一部分或为空。
+ pub stopped: bool,
/// 行动轨迹:调了什么、拿到多少(界面显示)
pub steps: serde_json::Value,
/// 引用清单
@@ -265,6 +276,7 @@ impl TurnRecord {
/// 用户消息:四样都空。
pub fn empty() -> Self {
Self {
+ stopped: false,
steps: serde_json::json!([]),
sources: serde_json::json!([]),
resolved: serde_json::json!([]),
@@ -284,8 +296,8 @@ pub async fn append_message(
let mut tx = pool.begin().await?;
sqlx::query(
"INSERT INTO conversation_messages
- (id, conversation_id, role, content, steps, sources, resolved, tool_exchange)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
+ (id, conversation_id, role, content, steps, sources, resolved, tool_exchange, stopped)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
)
.bind(id)
.bind(conversation_id)
@@ -295,6 +307,7 @@ pub async fn append_message(
.bind(&rec.sources)
.bind(&rec.resolved)
.bind(&rec.tool_exchange)
+ .bind(rec.stopped)
.execute(&mut *tx)
.await?;
sqlx::query("UPDATE conversations SET updated_at = now() WHERE id = $1")
diff --git a/crates/utopia-store/tests/store/a_stopped_chat_is_replayed_as_incomplete.rs b/crates/utopia-store/tests/store/a_stopped_chat_is_replayed_as_incomplete.rs
new file mode 100644
index 00000000..97dc0d2b
--- /dev/null
+++ b/crates/utopia-store/tests/store/a_stopped_chat_is_replayed_as_incomplete.rs
@@ -0,0 +1,89 @@
+//! 停止标记随原文回放,模型上下文另外说明回答未完成(#934)。
+use serde_json::json;
+use sqlx::PgPool;
+use utopia_store::conversations::{self, TurnRecord};
+use uuid::Uuid;
+
+#[tokio::test]
+async fn a_stopped_chat_is_replayed_as_incomplete() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ let (org, ws, kb, user) = (
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ );
+ sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'stopped-chat-test')")
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ let result = async {
+ sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'stopped-chat-test')")
+ .bind(ws)
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'stopped-chat-test')")
+ .bind(kb)
+ .bind(ws)
+ .execute(&pool)
+ .await?;
+ sqlx::query("INSERT INTO users (id, org_id, email, password_hash, display_name) VALUES ($1, $2, $3, '', 'Stopped Chat')")
+ .bind(user)
+ .bind(org)
+ .bind(format!("{user}@stopped-chat.test"))
+ .execute(&pool)
+ .await?;
+
+ for (content, stopped, expected_context) in [
+ ("A complete answer.", false, "A complete answer."),
+ (
+ "已生成的部分",
+ true,
+ "[This response was stopped by the user before completion.]\n\n已生成的部分",
+ ),
+ ("", true, "[This response was stopped by the user before completion.]\n\n"),
+ ] {
+ let conversation = conversations::create(&pool, kb, user, "question").await?;
+ let question = conversations::append_message(
+ &pool, conversation, "user", "question", &TurnRecord::empty(),
+ ).await?;
+ let record = TurnRecord {
+ stopped,
+ sources: json!([{"number": 1, "title": "Record"}]),
+ tool_exchange: json!([
+ {"role": "assistant", "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "no_evidence_needed", "arguments": "{}"}}]},
+ {"role": "tool", "tool_call_id": "call_1", "content": "ok"},
+ ]),
+ ..TurnRecord::empty()
+ };
+ let answer = conversations::append_message(
+ &pool, conversation, "assistant", content, &record,
+ ).await?;
+
+ let messages = conversations::messages(&pool, conversation).await?;
+ assert_eq!(messages.len(), 2);
+ assert!(!messages[0].stopped);
+ assert_eq!(messages[1].content, content, "历史保留原文");
+ assert_eq!(messages[1].stopped, stopped);
+ assert_eq!(serde_json::to_value(&messages[1])?["stopped"], stopped);
+
+ let history = conversations::recent_context(&pool, conversation, 20).await?;
+ assert_eq!(history.turn_ids, [question, answer]);
+ assert_eq!(history.turns[0], ("user".into(), "question".into()));
+ assert_eq!(history.turns[1], ("assistant".into(), expected_context.into()));
+ assert_eq!(history.last_sources, record.sources.as_array().unwrap().clone());
+ assert_eq!(history.last_tool_exchange, record.tool_exchange.as_array().unwrap().clone());
+ }
+ Ok::<_, anyhow::Error>(())
+ }.await;
+
+ sqlx::query("DELETE FROM organizations WHERE id = $1")
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ result
+}
diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rs
index 29254b24..91142b89 100644
--- a/crates/utopia-store/tests/store/main.rs
+++ b/crates/utopia-store/tests/store/main.rs
@@ -80,6 +80,7 @@ mod a_secret_is_sealed_at_rest;
mod a_signature_holds_on_every_path;
mod a_source_kind_is_listed_once;
mod a_source_reaches_only_where_it_was_granted;
+mod a_stopped_chat_is_replayed_as_incomplete;
mod a_time_mention_is_resolved_against_its_document;
mod a_time_mention_is_words_not_a_date;
mod a_timeline_holds_whatever_the_order;
diff --git a/docs/decisions/0063-stopping-a-chat-ends-the-generation.md b/docs/decisions/0063-stopping-a-chat-ends-the-generation.md
new file mode 100644
index 00000000..bab2aeb1
--- /dev/null
+++ b/docs/decisions/0063-stopping-a-chat-ends-the-generation.md
@@ -0,0 +1,42 @@
+# 0063 · Stopping a chat ends the generation
+
+- **Status**: Implemented 2026-09-27 ([#961](https://github.com/deeplethe/utopia/pull/961), migration 0095)
+- **Written**: 2026-09-26 (conventions in the [README](README.md))
+- **Related**: [#934](https://github.com/deeplethe/utopia/issues/934), [0042](0042-the-chat-loop-is-a-runner-with-hooks.md)
+
+## Problem
+
+Stop currently closes the browser stream while the server continues generating and saving
+the answer. A follow-up can start before the first answer finishes, interleaving messages
+and building its context without that answer. Refresh must still allow generation to continue.
+
+## Decision
+
+- **Stop is explicit.** The authenticated cancel route checks ownership and signals the active
+ generation. Losing an SSE connection does not cancel it. The runner checks cancellation before
+ model and tool calls and interrupts an in-flight call when stopped.
+- **Keep the question and published partial answer.** Save one assistant message with
+ `stopped = true`, including when its content is empty. Keep the steps, sources and complete
+ tool exchanges already obtained. Do not save an unvalidated, buffered final-answer candidate.
+ History displays the stopped flag; model context explicitly identifies the answer as incomplete.
+- **One generation owns the conversation until it finishes saving.** Reserve it before appending
+ the question or reading context. Another submission or retry receives HTTP 409 with `answer_running`;
+ requests are neither queued nor used to replace the active generation.
+- **One owner saves and ends the turn.** The cancel route does not save a second answer or emit
+ a competing terminal. The producer saves once, then emits `done` with `stopped: true` for a
+ successful cancellation. A persistence failure remains `error`. Once normal completion has
+ entered its save, it wins over a late cancellation.
+- **Acknowledgement is not completion.** The browser sends the cancel request and keeps waiting
+ for the terminal outcome. It enables follow-up only after stopping has settled. Reattachment
+ observes the same outcome; stale cancellation must not stop a later generation.
+
+This uses the existing registry, message table and SSE terminal contract. It adds a boolean
+column, not a separate job table. Cancellation stops local work and closes pending calls;
+it does not reverse completed tool side effects or guarantee an upstream billing adjustment.
+
+## Verification
+
+Cover stop before output and during generation, partial and empty stopped histories, immediate
+follow-up, concurrent submissions, disconnect and reattach, duplicate and stale cancellation,
+completion races, ownership checks, and failed persistence. Each turn has one persisted outcome
+and at most one terminal event; a failed save cannot report successful completion.
diff --git a/docs/decisions/README.md b/docs/decisions/README.md
index f3db49e0..439eaeab 100644
--- a/docs/decisions/README.md
+++ b/docs/decisions/README.md
@@ -83,6 +83,7 @@ The test for writing one: if someone (including us) looks at a piece of code in
| 0060 | [A rule's definition has a history](0060-a-rule-definition-has-a-history.md) | Implemented 2026-09-25 (#912, migration 0076) · A business rule was edited in place and a derivation pointed at the row, so an invalidated conclusion pointed at a rule that now said something else. Every edit that changes what a rule says opens a **version**, a full snapshot with a record time; a derivation names the version it was drawn under, a kept conclusion moves to the new one, and the proof, the rules panel and a versions endpoint read the history. Name, description and the switch open nothing. Exporting rule bodies per version is now honest and stays #902's second cut |
| 0061 | [The ontology is proposed from the open graph and judged by its questions](0061-the-ontology-is-proposed-from-the-open-graph-and-judged-by-its-questions.md) | Accepted · cut 1 built (#947): questions, agent proposals, adoption · cut 1.1 (#950): the agent remembers what it saw, existing answers become map_to · cut 3 first slice (#955): proposed questions, the two numbers, `competency.mjs`; second slice (#962): answers record whether they went through the graph · cut 2, rest of 3, 4 open · A base's unbound signatures and classless kind words are what its documents say and its ontology cannot hold, and nothing says what the ontology is *for*. **Competency questions** become rows of the base and the standard an element is judged by; an **ontology agent** reads the unbound signatures and kind words against them and proposes types, properties and rules with a definition, the statements they would bind and the questions they serve; **approval** writes the element with its structure and re-decides those signatures through 0053's basis, and every approved element carries **regression cases** rerun when its definition changes. Measured by questions answered correctly and by the share of proposals people change. No automatic adoption; the 0003 `Suggest` retires once this replaces it |
| 0062 | [The export says what is contested](0062-the-export-says-what-is-contested.md) | Proposed 2026-09-25 · #564: the RDF export carries `fact_conflicts` and `axiom_violations` as two distinct resource classes — conflicts point at the prior and incoming statements, violations at the statements they are about and (open rows only) the governing criterion and its relation; an ordered `rdf:List` keeps cycle paths; precondition is the current-ontology invariant: `status = 'open'` means reconciled against the current ontology, so criterion-changing ontology edits settle superseded open rows with `criterion_changed` and re-detect in the same transaction
+| 0063 | [Stopping a chat ends the generation](0063-stopping-a-chat-ends-the-generation.md) | Implemented 2026-09-27 (#961, migration 0095) · explicit cancellation, saved partial answers with a stopped flag, one active generation per conversation, and one terminal after persistence |
| | Record | Domain | Status |
|---|---|---|---|
@@ -143,6 +144,7 @@ The test for writing one: if someone (including us) looks at a piece of code in
| 0060 | [A rule's definition has a history](0060-a-rule-definition-has-a-history.md) | rules | current |
| 0061 | [The ontology is proposed from the open graph and judged by its questions](0061-the-ontology-is-proposed-from-the-open-graph-and-judged-by-its-questions.md) | ontology | current |
| 0062 | [The export says what is contested](0062-the-export-says-what-is-contested.md) | ontology | proposed |
+| 0063 | [Stopping a chat ends the generation](0063-stopping-a-chat-ends-the-generation.md) | chat-and-mcp | current |
The status word is whether a later record has overtaken this one; what is built is in the record's own status line. Domains are the files of [../design/](../design/README.md), where every record is dated and the status words are defined.
diff --git a/docs/design/chat-and-mcp.md b/docs/design/chat-and-mcp.md
index 5fcdfe46..c56467cc 100644
--- a/docs/design/chat-and-mcp.md
+++ b/docs/design/chat-and-mcp.md
@@ -2,7 +2,8 @@
Records: [0042] (the loop), [0014] (MCP tools and scope), [0015] (`remember` and the nod), [0020]
(the read contract), [0021] (rule tools), [0019] and [0022] (timed reads), [0035] (retrieval),
-[0011] and [0036] (mappings in the prompt), [0040] (origin in results), [0046] (where an app gets built).
+[0011] and [0036] (mappings in the prompt), [0040] (origin in results), [0046] (where an app gets built),
+[0063] (stopping a generation).
## What it does today
@@ -23,6 +24,13 @@ only on a 400 or 422 to the first request with tools [0042 d2]. A question whose
stored without one; a retry names it (`retry_message_id`) and answers it in place, only while it is
the conversation's last message and no answer is being written there (#936).
+**Stop** explicitly cancels pending model and tool work, then saves the published partial answer
+once with `stopped: true` before sending `done`. The browser waits for that terminal outcome before
+allowing a follow-up; navigation and SSE disconnections let generation continue. A conversation
+admits one generation through persistence, for both new questions and retries; another request
+receives HTTP 409 with `answer_running`. A generation ID keeps a delayed Stop from cancelling a
+later turn [0063].
+
**Tools** live once in `tools.rs` and serve chat and MCP alike: `search_chunks`, `search_docs`,
`find_entities`, `entity_facts` (names marked as names, derived rows with their rule, `as_of` and
`at` respected), `changes`, `list_rules`, `rule_matches`, `remember`; MCP results carry
diff --git a/migrations/0095_a_stopped_chat_keeps_its_partial_answer.sql b/migrations/0095_a_stopped_chat_keeps_its_partial_answer.sql
new file mode 100644
index 00000000..5be3e7c7
--- /dev/null
+++ b/migrations/0095_a_stopped_chat_keeps_its_partial_answer.sql
@@ -0,0 +1,3 @@
+-- 主动停止保留已发布正文;普通消息与旧历史默认完整(0063 / #961)。
+ALTER TABLE conversation_messages
+ ADD COLUMN stopped BOOLEAN NOT NULL DEFAULT FALSE;
diff --git a/web/src/api.ts b/web/src/api.ts
index 55127b24..11ab2d39 100644
--- a/web/src/api.ts
+++ b/web/src/api.ts
@@ -1535,6 +1535,7 @@ export interface ConversationMessage {
content: string;
steps: ChatStep[];
sources: Source[];
+ stopped: boolean;
created_at: string;
}
@@ -2801,6 +2802,11 @@ export const conversationsApi = {
request<{ messages: ConversationMessage[] }>(
`/api/v1/kbs/${kbId}/conversations/${id}`,
),
+ stop: (kbId: string, id: string, generationId: string) =>
+ request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/chat/${id}/stop`, {
+ method: "POST",
+ body: JSON.stringify({ generation_id: generationId }),
+ }),
remove: (kbId: string, id: string) =>
request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/conversations/${id}`, {
method: "DELETE",
@@ -2809,14 +2815,14 @@ export const conversationsApi = {
export interface ChatHandlers {
/** `messageId`:这一问存下的 id。答到一半失败了,凭它重答(#936);老的服务端不发 */
- onConversation: (id: string, messageId?: string) => void;
+ onConversation: (id: string, messageId?: string, generationId?: string) => void;
onSources: (s: Source[]) => void;
onStep: (s: ChatStep) => void;
onDelta: (text: string) => void;
- onDone: () => void;
- onError: (message: string) => void;
+ onDone: (stopped: boolean) => void;
+ onError: (message: string, error?: ApiError) => void;
/** 接上一个已经在跑的回答:这是它此刻的样子,**覆盖,不是追加** */
- onSnapshot?: (s: { content: string; steps: ChatStep[]; sources: Source[] }) => void;
+ onSnapshot?: (s: { generation_id: string; content: string; steps: ChatStep[]; sources: Source[] }) => void;
/** 这个会话没有在跑的生成——最常见的答案,不是错误 */
onIdle?: () => void;
}
@@ -2868,10 +2874,10 @@ function consumeChatStream(
const controller = new AbortController();
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
let terminal = false;
- const fail = (message: string) => {
+ const fail = (message: string, error?: ApiError) => {
if (terminal || controller.signal.aborted) return;
terminal = true;
- handlers.onError(message);
+ handlers.onError(message, error);
};
(async () => {
try {
@@ -2882,10 +2888,13 @@ function consumeChatStream(
}
if (!res.ok || !res.body) {
let message = res.statusText;
+ let code: string | undefined;
try {
- message = refusalMessage((await res.json()) as Refusal, message);
+ const body = (await res.json()) as Refusal;
+ code = body.code;
+ message = refusalMessage(body, message);
} catch { /* keep the HTTP status */ }
- fail(message);
+ fail(message, new ApiError(res.status, message, code));
return;
}
reader = res.body.getReader();
@@ -2893,14 +2902,18 @@ function consumeChatStream(
let trailingCr = false;
const parser = createParser({ onEvent: ({ event, data: value }) => {
if (terminal || controller.signal.aborted) return;
- if (event === "done") { terminal = true; handlers.onDone(); }
+ if (event === "done") {
+ const stopped = JSON.parse(value).stopped === true;
+ terminal = true;
+ handlers.onDone(stopped);
+ }
else if (event === "error") fail(streamFailure(value));
else if (event === "idle") {
if (allowIdle) { terminal = true; handlers.onIdle?.(); }
else fail(S.ask.streamInterrupted);
} else if (event === "conversation") {
const frame = JSON.parse(value);
- handlers.onConversation(frame.id, frame.message_id);
+ handlers.onConversation(frame.id, frame.message_id, frame.generation_id);
}
else if (event === "sources") handlers.onSources(JSON.parse(value));
else if (event === "step") handlers.onStep(JSON.parse(value));
diff --git a/web/src/chatStream.test.ts b/web/src/chatStream.test.ts
index f29e617a..d2b1fc88 100644
--- a/web/src/chatStream.test.ts
+++ b/web/src/chatStream.test.ts
@@ -1,11 +1,12 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-import { reattachChat, streamChat, type ChatHandlers } from "./api";
+import { ApiError, reattachChat, streamChat, type ChatHandlers } from "./api";
vi.mock("./i18n", () => ({
S: {
ask: { streamInterrupted: "Stream interrupted" },
err: {
no_chat_model: "Worded: configure a chat model",
model_out_of_credit: "Worded: the model account cannot pay",
+ answer_running: "Worded: this conversation already has an answer in progress",
},
errDetail: (message: string, detail: string) => `${message} (${detail})`,
},
@@ -49,6 +50,19 @@ async function refuse(status: number, body: unknown) {
}
describe("a refused chat request", () => {
+ it("preserves the HTTP status and code for an already-running conversation", async () => {
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({
+ error: "Conversation already has an active answer", code: "answer_running",
+ }, { status: 409 })));
+ const onError = vi.fn();
+ streamChat("kb", { conversation_id: "conversation", message: "follow-up" }, {
+ onConversation: vi.fn(), onSources: vi.fn(), onStep: vi.fn(), onDelta: vi.fn(), onDone: vi.fn(), onError,
+ });
+ await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(1));
+ expect(onError.mock.calls[0][1]).toBeInstanceOf(ApiError);
+ expect(onError.mock.calls[0][0]).toBe("Worded: this conversation already has an answer in progress");
+ expect(onError.mock.calls[0][1]).toMatchObject({ status: 409, code: "answer_running" });
+ });
it("is worded from its stable code, like every other request", async () => {
expect(await refuse(422, { error: "Chat model not configured. Go to Settings → Models.", code: "no_chat_model" }))
.toEqual([["error", "Worded: configure a chat model"]]);
@@ -82,6 +96,29 @@ describe("a chat stream that fails", () => {
});
describe("application chat terminal outcomes", () => {
+ it.each([false, true])("reads the generation identity and stopped outcome on reattach=%s", async (attach) => {
+ const snapshot = { generation_id: "generation", content: "partial", steps: [], sources: [] };
+ const body = new ReadableStream<Uint8Array>({ start(c) {
+ c.enqueue(encoder.encode(
+ 'event: conversation\ndata: {"id":"conversation","generation_id":"generation"}\n\n' +
+ `event: snapshot\ndata: ${JSON.stringify(snapshot)}\n\n` +
+ 'event: done\ndata: {"stopped":true}\n\nevent: delta\ndata: {"text":"late"}\n\n',
+ ));
+ c.close();
+ } });
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(body)));
+ const handlers = {
+ onConversation: vi.fn(), onSnapshot: vi.fn(), onSources: vi.fn(), onStep: vi.fn(),
+ onDelta: vi.fn(), onDone: vi.fn(), onError: vi.fn(),
+ };
+ if (attach) reattachChat("kb", "conversation", handlers);
+ else streamChat("kb", { message: "hello" }, handlers);
+ await vi.waitFor(() => expect(handlers.onDone).toHaveBeenCalledWith(true));
+ expect(handlers.onConversation).toHaveBeenCalledWith("conversation", undefined, "generation");
+ expect(handlers.onSnapshot).toHaveBeenCalledWith(snapshot);
+ expect(handlers.onError).not.toHaveBeenCalled();
+ expect(handlers.onDelta).not.toHaveBeenCalled();
+ });
it.each(["\n", "\r\n", "\r"])("reads %j line endings once, including split UTF-8", async (nl) => {
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]]);
@@ -115,7 +152,7 @@ describe("application chat terminal outcomes", () => {
streamChat("kb", {message:"hello"}, h);
await vi.waitFor(() => expect(body.locked).toBe(false));
await vi.waitFor(() => expect(cancel).toHaveBeenCalledTimes(1));
- expect(h.onDone).toHaveBeenCalledTimes(1); expect(h.onError).not.toHaveBeenCalled();
+ expect(h.onDone).toHaveBeenCalledExactlyOnceWith(false); expect(h.onError).not.toHaveBeenCalled();
});
// 这一问存下的 id 随 conversation 帧一起到:答到一半失败时,页面凭它重答(#936)
it("hands the stored id of the question to the page with the conversation", async () => {
@@ -125,7 +162,7 @@ describe("application chat terminal outcomes", () => {
const h = {onConversation:vi.fn(),onSources:vi.fn(),onStep:vi.fn(),onDelta:vi.fn(),onDone:vi.fn(),onError:vi.fn()};
streamChat("kb", {message:"hello"}, h);
await vi.waitFor(() => expect(h.onDone).toHaveBeenCalledTimes(1));
- expect(h.onConversation).toHaveBeenCalledWith("c1", "m1");
+ expect(h.onConversation).toHaveBeenCalledWith("c1", "m1", undefined);
expect(h.onError).not.toHaveBeenCalled();
});
it("active abort is silent and cancels the reader", async () => {
diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts
index 3b3a30a3..37a51ea2 100644
--- a/web/src/i18n/en.ts
+++ b/web/src/i18n/en.ts
@@ -815,6 +815,8 @@ export const en = {
scopeLabel: "Knowledge base",
send: "Send",
stop: "Stop",
+ stopping: "Stopping…",
+ stopped: "Stopped",
thinking: "Thinking…",
/* 轨迹上每一步的话(#942)。服务端只给字段:状态、数、时刻,怎么说在这里定。
查询、实体名、来源名、问数的目的、记下的那句话是数据,原样显示;
diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.ts
index b3b67331..c8e55729 100644
--- a/web/src/i18n/zh.ts
+++ b/web/src/i18n/zh.ts
@@ -747,6 +747,8 @@ export const zh: Strings = {
scopeLabel: "知识库",
send: "发送",
stop: "停止",
+ stopping: "正在停止…",
+ stopped: "已停止",
thinking: "思考中…",
step: {
failed: "失败",
diff --git a/web/src/liveAnswer.stream.test.ts b/web/src/liveAnswer.stream.test.ts
index 9f733af2..04068de7 100644
--- a/web/src/liveAnswer.stream.test.ts
+++ b/web/src/liveAnswer.stream.test.ts
@@ -1,14 +1,18 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { streamChat } from "./api";
-import { liveAnswer, type Turn } from "./liveAnswer";
+import { liveAnswer, type LiveHandle, type Turn } from "./liveAnswer";
const turns = (): Turn[] => [{ role: "assistant", content: "" }];
+const handles: LiveHandle[] = [];
+const begin = (...args: Parameters<typeof liveAnswer.begin>) => {
+ const handle = liveAnswer.begin(...args);
+ handles.push(handle);
+ return handle;
+};
afterEach(() => {
+ for (const handle of handles.splice(0)) handle.discard();
vi.unstubAllGlobals();
- for (const entry of liveAnswer.get()) {
- liveAnswer.stop(entry.kbId, entry.conversationId);
- }
});
describe("live answer generation ownership", () => {
@@ -18,7 +22,7 @@ describe("live answer generation ownership", () => {
const cancel = vi.fn(() => new Promise<void>((resolve) => { finishCleanup = resolve; }));
const body = new ReadableStream<Uint8Array>({ start(c) { wire = c; }, cancel });
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(body)));
- const previous = liveAnswer.begin("kb", "conversation", turns(), () => {});
+ const previous = begin("kb", "conversation", turns(), () => {});
const done = vi.fn(() => previous.finish());
streamChat("kb", { conversation_id: "conversation", message: "first" }, {
onConversation: (id) => previous.identify(id),
@@ -30,7 +34,7 @@ describe("live answer generation ownership", () => {
});
wire.enqueue(new TextEncoder().encode('event: done\ndata: {}\n\n'));
await vi.waitFor(() => expect(done).toHaveBeenCalledTimes(1));
- const followUp = liveAnswer.begin("kb", "conversation", turns(), () => {});
+ const followUp = begin("kb", "conversation", turns(), () => {});
await vi.waitFor(() => expect(cancel).toHaveBeenCalledTimes(1));
finishCleanup();
await vi.waitFor(() => expect(body.locked).toBe(false));
@@ -40,27 +44,28 @@ describe("live answer generation ownership", () => {
});
it("ignores every stale handle operation after the conversation slot is replaced", () => {
- const old = liveAnswer.begin("kb", "same", turns(), () => {});
+ const old = begin("kb", "same", turns(), () => {});
old.finish();
const abort = vi.fn();
- const current = liveAnswer.begin("kb", "same", turns(), abort);
+ const current = begin("kb", "same", turns(), abort);
const staleAbort = vi.fn();
old.patchLast((t) => ({ ...t, content: "old response" }));
old.setAbort(staleAbort);
old.identify("wrong");
old.finish();
+ old.discard();
current.patchLast((t) => ({ ...t, content: t.content + "new response" }));
current.finish();
expect(liveAnswer.entry("kb", "same")?.turns[0].content).toBe("new response");
expect(liveAnswer.entry("kb", "wrong")).toBeNull();
- liveAnswer.stop("kb", "same");
+ current.discard();
expect(abort).toHaveBeenCalledTimes(1);
expect(staleAbort).not.toHaveBeenCalled();
});
it("keeps pending identification and other conversations independent", () => {
- const a = liveAnswer.begin("kb", null, turns(), () => {});
- const b = liveAnswer.begin("other-kb", null, turns(), () => {});
+ const a = begin("kb", null, turns(), () => {});
+ const b = begin("other-kb", null, turns(), () => {});
a.identify("a");
b.identify("b");
a.patchLast((t) => ({ ...t, content: "A" }));
@@ -72,3 +77,70 @@ describe("live answer generation ownership", () => {
b.finish();
});
});
+
+describe("explicit server stop", () => {
+ it("queues Stop before identity and waits for done after the HTTP acknowledgement", async () => {
+ const fetch = vi.fn().mockResolvedValue(Response.json({ ok: true }));
+ vi.stubGlobal("fetch", fetch);
+ const abort = vi.fn();
+ const handle = begin("kb", null, [{ role: "assistant", content: "partial", steps: [], sources: [] }], abort);
+ liveAnswer.stop("kb", null);
+ expect(fetch).not.toHaveBeenCalled();
+ expect(liveAnswer.entry("kb", null)).toMatchObject({ streaming: true, stopping: true });
+
+ handle.identify("conversation", undefined, "generation");
+ await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(1));
+ expect(fetch).toHaveBeenCalledWith("/api/v1/kbs/kb/chat/conversation/stop", expect.objectContaining({
+ method: "POST", body: JSON.stringify({ generation_id: "generation" }),
+ }));
+ expect(liveAnswer.entry("kb", "conversation")).toMatchObject({ streaming: true, stopping: true });
+ expect(abort).not.toHaveBeenCalled();
+ liveAnswer.stop("kb", "conversation");
+ expect(fetch).toHaveBeenCalledTimes(1);
+
+ handle.finish(true);
+ expect(liveAnswer.entry("kb", "conversation")).toMatchObject({
+ streaming: false, stopping: false, turns: [{ content: "partial", stopped: true, steps: [], sources: [] }],
+ });
+ handle.patchLast((turn) => ({ ...turn, content: "late" }));
+ liveAnswer.stop("kb", "conversation");
+ expect(liveAnswer.entry("kb", "conversation")?.turns[0].content).toBe("partial");
+ expect(fetch).toHaveBeenCalledTimes(1);
+ });
+
+ it("keeps the stream open when Stop fails and lets the user retry", async () => {
+ const fetch = vi.fn()
+ .mockRejectedValueOnce(new Error("offline"))
+ .mockResolvedValue(Response.json({ ok: true }));
+ vi.stubGlobal("fetch", fetch);
+ const handle = begin("kb", "conversation", turns(), vi.fn());
+ const other = begin("kb", "other", turns(), vi.fn());
+ handle.identify("conversation", undefined, "generation");
+ liveAnswer.stop("kb", "conversation");
+ await vi.waitFor(() => expect(liveAnswer.entry("kb", "conversation")?.stopError).toBe("offline"));
+ expect(liveAnswer.entry("kb", "conversation")).toMatchObject({ streaming: true, stopping: false });
+ expect(liveAnswer.entry("kb", "conversation")?.turns[0].error).toBeUndefined();
+ expect(liveAnswer.entry("kb", "other")).toMatchObject({ streaming: true, stopping: false });
+
+ liveAnswer.stop("kb", "conversation");
+ await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
+ expect(liveAnswer.entry("kb", "conversation")).toMatchObject({ streaming: true, stopping: true, stopError: undefined });
+ handle.finish(true);
+ other.finish();
+ });
+
+ it("ignores a late stop failure after another generation owns the conversation", async () => {
+ let rejectStop!: (error: Error) => void;
+ vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>((_, reject) => { rejectStop = reject; })));
+ const old = begin("kb", "same", turns(), () => {});
+ old.identify("same", undefined, "old-generation");
+ liveAnswer.stop("kb", "same");
+ old.finish(true);
+ const current = begin("kb", "same", turns(), () => {});
+ current.identify("same", undefined, "new-generation");
+ rejectStop(new Error("late failure"));
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(liveAnswer.entry("kb", "same")).toMatchObject({ generationId: "new-generation", streaming: true, stopping: false });
+ expect(liveAnswer.entry("kb", "same")?.stopError).toBeUndefined();
+ });
+});
diff --git a/web/src/liveAnswer.test.ts b/web/src/liveAnswer.test.ts
index 67ca3f1f..2535b908 100644
--- a/web/src/liveAnswer.test.ts
+++ b/web/src/liveAnswer.test.ts
@@ -102,5 +102,6 @@ describe("answeredWithoutSources (#547)", () => {
expect(answeredWithoutSources({ role: "user", content: "hi" }, false)).toBe(false);
expect(answeredWithoutSources(answer({ content: "", error: "boom" }), false)).toBe(false);
expect(answeredWithoutSources(answer({ error: "stopped" }), false)).toBe(true);
+ expect(answeredWithoutSources(answer({ content: "", stopped: true }), false)).toBe(false);
});
});
diff --git a/web/src/liveAnswer.ts b/web/src/liveAnswer.ts
index eb60f25f..812c03d2 100644
--- a/web/src/liveAnswer.ts
+++ b/web/src/liveAnswer.ts
@@ -18,7 +18,7 @@
//
// 于是改成一张表:谁开场谁拿句柄,读谁写谁都有名有姓。`send` 的守卫不用改——
// 它本来问的就是「这一场在不在流」,现在这个问题终于只关于这一场。
-import type { ChatStep, Source } from "./api";
+import { conversationsApi, type ChatStep, type Source } from "./api";
import { citedNumbers } from "./citations";
export interface Turn {
@@ -30,6 +30,7 @@ export interface Turn {
steps?: ChatStep[];
sources?: Source[];
error?: string;
+ stopped?: boolean;
}
/** 正文里真正引到的那几条来源。
@@ -52,7 +53,7 @@ export function citedSources(turn: Turn): Source[] {
* 只有报错、一个字没说的那条也不挂,它不是回答,红字已经交代了 */
export function answeredWithoutSources(turn: Turn, live: boolean): boolean {
if (turn.role !== "assistant" || live) return false;
- if (turn.error && !turn.content) return false;
+ if ((turn.error || turn.stopped) && !turn.content) return false;
return citedSources(turn).length === 0;
}
@@ -61,8 +62,11 @@ export interface Live {
kbId: string;
/** 新会话在服务端回 id 之前是 null;kbId 用来区分两个都还没拿到 id 的新会话 */
conversationId: string | null;
+ generationId: string | null;
turns: Turn[];
streaming: boolean;
+ stopping: boolean;
+ stopError?: string;
}
interface Slot {
@@ -110,13 +114,26 @@ function flush() {
notify();
}
+async function requestStop(slot: Slot) {
+ const { kbId, conversationId, generationId } = slot.live;
+ if (!conversationId || !generationId) return; // 首帧前的停止意图由 identify 补发。
+ try {
+ await conversationsApi.stop(kbId, conversationId, generationId);
+ // HTTP 只确认收到请求;done 才表示生成结束、部分答案已保存。
+ } catch (error) {
+ if (lives.get(conversationId) !== slot || !slot.live.streaming) return;
+ slot.live = { ...slot.live, stopping: false, stopError: error instanceof Error ? error.message : String(error) };
+ flush();
+ }
+}
+
// 还没拿到 id 的新会话用内部 token 占位;identify 到真 id 时重映射
let pendingSeq = 0;
export interface LiveHandle {
/** 新会话从服务端拿到 id:把这个条目从占位 token 重映射到真 id。
* `questionId` 是这一问存下的 id,记到最后那条用户消息上 */
- identify: (conversationId: string, questionId?: string) => void;
+ identify: (conversationId: string, questionId?: string, generationId?: string) => void;
/** 改这场回答的最后一条(助手那一轮)。生成期间只有它在变 */
patchLast: (f: (t: Turn) => Turn) => void;
/** 结束(正常、出错、或人按了停止)。
@@ -129,7 +146,9 @@ export interface LiveHandle {
* 那一刻这里是唯一还握着这份内容的地方,所以留着:只把 `streaming`
* 落下来。下一次 `begin` 会清掉已结束的条目(见 begin),切到别的会话时
* 认领不上自然去读库。 */
- finish: () => void;
+ finish: (stopped?: boolean) => void;
+ /** 请求未被接受时丢弃乐观展示;只断开连接,不取消服务端生成。 */
+ discard: () => void;
/** streamChat 的 abort 要等它返回才有:begin 先给占位,拿到真 abort 再换上 */
setAbort: (abort: () => void) => void;
}
@@ -160,14 +179,17 @@ export const liveAnswer = {
): LiveHandle => {
for (const [k, s] of lives) if (!s.live.streaming) lives.delete(k);
let key = conversationId ?? `__pending__${++pendingSeq}`;
- const slot: Slot = { live: { kbId, conversationId, turns, streaming: true }, abort };
+ const slot: Slot = {
+ live: { kbId, conversationId, generationId: null, turns, streaming: true, stopping: false },
+ abort,
+ };
lives.set(key, slot);
flush();
// A follow-up reuses the conversation key. Late callbacks from the old
// stream must still belong to its original slot, not the replacement.
const owned = () => (lives.get(key) === slot ? slot : undefined);
return {
- identify: (id: string, questionId?: string) => {
+ identify: (id, questionId, generationId) => {
const current = owned();
if (!current) return;
lives.delete(key);
@@ -180,22 +202,33 @@ export const liveAnswer = {
turns[at] = { ...turns[at], id: questionId };
}
}
- current.live = { ...current.live, conversationId: id, turns };
+ current.live = { ...current.live, conversationId: id, generationId: generationId ?? null, turns };
lives.set(key, current);
flush();
+ if (current.live.stopping) void requestStop(current);
},
patchLast: (f) => {
const current = owned();
- if (!current || current.live.turns.length === 0) return;
+ if (!current?.live.streaming || current.live.turns.length === 0) return;
const turns = [...current.live.turns];
turns[turns.length - 1] = f(turns[turns.length - 1]);
current.live = { ...current.live, turns };
emit();
},
- finish: () => {
+ finish: (stopped = false) => {
const current = owned();
if (!current || !current.live.streaming) return;
- current.live = { ...current.live, streaming: false };
+ const turns = stopped
+ ? current.live.turns.map((turn, i, all) => i === all.length - 1 ? { ...turn, stopped: true } : turn)
+ : current.live.turns;
+ current.live = { ...current.live, turns, streaming: false, stopping: false, stopError: undefined };
+ flush();
+ },
+ discard: () => {
+ const current = owned();
+ if (!current) return;
+ current.abort();
+ lives.delete(key);
flush();
},
setAbort: (a) => {
@@ -204,15 +237,12 @@ export const liveAnswer = {
},
};
},
- /** 停止按钮专用:abort + finish 正在看的这一场。别的场照常写它们自己的条目 */
+ /** 停止按钮专用:请求取消这一代生成,保留连接等服务端收尾。 */
stop: (kbId: string, conversationId: string | null) => {
- for (const s of lives.values()) {
- if (s.live.kbId === kbId && s.live.conversationId === conversationId) {
- s.abort();
- s.live = { ...s.live, streaming: false };
- flush();
- return;
- }
- }
+ const slot = [...lives.values()].find((s) => s.live.kbId === kbId && s.live.conversationId === conversationId);
+ if (!slot?.live.streaming || slot.live.stopping) return;
+ slot.live = { ...slot.live, stopping: true, stopError: undefined };
+ flush();
+ void requestStop(slot);
},
};
diff --git a/web/src/pages/Chat.tsx b/web/src/pages/Chat.tsx
index c97ddf2f..328fd7bb 100644
--- a/web/src/pages/Chat.tsx
+++ b/web/src/pages/Chat.tsx
@@ -95,6 +95,7 @@ const historyTurns = (messages: ConversationMessage[]): Turn[] => messages.map((
content: m.content,
steps: m.steps.length ? m.steps : undefined,
sources: m.sources.length ? m.sources : undefined,
+ stopped: m.stopped,
}));
const viewKey = (kbId: string, id: string | null) => `${kbId}/${id ?? ""}`;
@@ -342,13 +343,14 @@ export function Chat() {
],
abort,
);
+ handle.identify(id, undefined, s.generation_id);
},
onSources: (sources) => handle?.patchLast((t) => ({ ...t, sources })),
onStep: (step) =>
handle?.patchLast((t) => ({ ...t, steps: [...(t.steps ?? []), step] })),
onDelta: (text) => handle?.patchLast((t) => ({ ...t, content: t.content + text })),
- onDone: () => {
- handle?.finish();
+ onDone: (stopped) => {
+ handle?.finish(stopped);
invalidateList();
},
onError: (message) => {
@@ -459,7 +461,8 @@ export function Chat() {
const send = () => {
const q = input.trim();
- if (!q || streaming || !kb || kb.id !== kbId || loadingHistory || historyError) return;
+ if (!q || !kb || kb.id !== kbId || loadingHistory || historyError) return;
+ if (liveAnswer.entry(kb.id, activeId)?.streaming) return;
const owner = claimView(activeId);
following.current = true;
setIdleHistoryKey(null);
@@ -482,7 +485,8 @@ export function Chat() {
* 若挂着一条出错的回答,换成一条空的,带着它存下的 id 重新开流 */
const retry = () => {
const at = retryableQuestion(shown);
- if (at < 0 || streaming || !kb || kb.id !== kbId || !activeId || loadingHistory || historyError) return;
+ if (at < 0 || !kb || kb.id !== kbId || !activeId || loadingHistory || historyError) return;
+ if (liveAnswer.entry(kb.id, activeId)?.streaming) return;
const question = shown[at];
const owner = claimView(activeId);
following.current = true;
@@ -507,8 +511,8 @@ export function Chat() {
) => {
const handle = liveAnswer.begin(kbNow, activeId, start, () => {});
const abort = streamChat(kbNow, body, {
- onConversation: (id, questionId) => {
- handle.identify(id, questionId);
+ onConversation: (id, questionId, generationId) => {
+ handle.identify(id, questionId, generationId);
invalidateList();
if (!ownsView(owner)) return;
// 先 identify 生成句柄再换 URL;layout effect 重置视图后,loadConversation 会认领该句柄。
@@ -526,11 +530,22 @@ export function Chat() {
handle.patchLast((t) => ({ ...t, steps: [...(t.steps ?? []), step] })),
onDelta: (text) =>
handle.patchLast((t) => ({ ...t, content: t.content + text })),
- onDone: () => {
- handle.finish();
+ onDone: (stopped) => {
+ handle.finish(stopped);
invalidateList();
},
- onError: (message) => {
+ onError: (message, error) => {
+ if (error?.status === 409 && error.code === "answer_running" && body.conversation_id) {
+ handle.discard();
+ if (!ownsView(owner)) return;
+ if (!body.retry_message_id) {
+ const draft = [body.message, inputRef.current?.value ?? sessionStorage.getItem(DRAFT_KEY)].filter(Boolean).join("\n");
+ sessionStorage.setItem(DRAFT_KEY, draft);
+ setInput(draft);
+ }
+ void loadConversation(body.conversation_id);
+ return;
+ }
handle.patchLast((t) => ({ ...t, error: message }));
handle.finish();
},
@@ -563,6 +578,9 @@ export function Chat() {
}
}}
/>
+ {liveHere?.stopError && (
+ <div role="alert" className="text-small text-danger">{liveHere.stopError}</div>
+ )}
<div className="flex items-center justify-between gap-3 pt-1">
<div className="flex items-center gap-3 min-w-0">
{/* 作用域 chip:提问点位可见"在问哪个库",切库沿用现有语义(开新会话) */}
@@ -625,7 +643,8 @@ export function Chat() {
// 别场照常写它们自己的条目
if (kb) liveAnswer.stop(kb.id, currentId);
}}
- label={S.ask.stop}
+ label={liveHere?.stopping ? S.ask.stopping : S.ask.stop}
+ disabled={liveHere?.stopping}
variant="secondary"
className="shrink-0"
>
@@ -1091,6 +1110,7 @@ function TurnView({
),
)}
{thinking && <Thinking step={lastStep} />}
+ {turn.stopped && <div className="text-small text-ink-2">{S.ask.stopped}</div>}
{turn.error && <div className="text-danger">{turn.error}</div>}
</div>
{/* **引用等答案说完再出。**
diff --git a/web/tests/chat-view.test.mjs b/web/tests/chat-view.test.mjs
index cdc6a762..0da63bf1 100644
--- a/web/tests/chat-view.test.mjs
+++ b/web/tests/chat-view.test.mjs
@@ -152,6 +152,128 @@ test(
await page.waitForFunction(() => !!window.__go);
return { page, errors, close: () => context.close() };
}
+ await t.test("Stop before identity waits for server completion and survives reload", async () => {
+ const cancellations = [];
+ const acknowledged = deferred();
+ const f = await open("/kb/one/chat", async (route, p) => {
+ if (p.endsWith("/chat/a/stop")) {
+ cancellations.push(route.request().postDataJSON());
+ await route.fulfill({ json: { ok: true } });
+ acknowledged.resolve();
+ return true;
+ }
+ if (p.endsWith("/conversations/a")) {
+ await route.fulfill({ json: { messages: [
+ message("Question", "user"), { ...message("Partial answer"), stopped: true },
+ ] } });
+ return true;
+ }
+ }, () => {
+ const original = window.fetch;
+ window.__posts = 0;
+ window.fetch = (url, init) => {
+ if (String(url).endsWith("/chat") && init?.method === "POST") {
+ window.__posts++;
+ return Promise.resolve(new Response(new ReadableStream({ start(c) { window.__wire = c; } })));
+ }
+ return original(url, init);
+ };
+ });
+ try {
+ await f.page.locator("textarea").fill("Question");
+ await f.page.getByRole("button", { name: "Send", exact: true }).click();
+ await f.page.getByRole("button", { name: "Stop", exact: true }).click();
+ assert.equal(await f.page.getByRole("button", { name: "Stopping…", exact: true }).isDisabled(), true);
+ assert.deepEqual(cancellations, []);
+ await f.page.evaluate(() => window.__wire.enqueue(new TextEncoder().encode(
+ 'event: conversation\ndata: {"id":"a","generation_id":"generation-a"}\n\n' +
+ 'event: delta\ndata: {"text":"Partial answer"}\n\n',
+ )));
+ await f.page.getByText("Partial answer", { exact: true }).waitFor();
+ await f.page.waitForFunction(() => window.__live.entry("one", "a")?.stopping);
+ await acknowledged.promise;
+ assert.deepEqual(cancellations, [{ generation_id: "generation-a" }]);
+ await f.page.locator("textarea").fill("Next question");
+ await f.page.locator("textarea").press("Enter");
+ assert.equal(await f.page.evaluate(() => window.__posts), 1);
+ assert.equal(await f.page.getByRole("button", { name: "Stopping…", exact: true }).isDisabled(), true);
+ await f.page.evaluate(() => window.__wire.enqueue(new TextEncoder().encode('event: done\ndata: {"stopped":true}\n\n')));
+ await f.page.getByText("Stopped", { exact: true }).waitFor();
+ assert.equal(await f.page.getByRole("button", { name: "Send", exact: true }).isEnabled(), true);
+ await f.page.reload();
+ await f.page.getByText("Stopped", { exact: true }).waitFor();
+ await f.page.getByText("Partial answer", { exact: true }).waitFor();
+ assert.deepEqual(f.errors, []);
+ } finally {
+ await f.close();
+ }
+ });
+ await t.test("a busy conversation restores the draft and attaches without a ghost question", async () => {
+ const pending = deferred();
+ let reads = 0;
+ const f = await open("/kb/one/chat/a", async (route, p) => {
+ if (p.endsWith("/chat") && route.request().method() === "POST") {
+ pending.resolve(route);
+ return true;
+ }
+ if (p.endsWith("/conversations/a")) {
+ reads++;
+ await route.fulfill({ json: { messages: reads === 1
+ ? [message("Old answer")]
+ : [message("Old answer"), message("Other tab question", "user")],
+ } });
+ return true;
+ }
+ if (p.endsWith("/conversations/a/stream")) {
+ await route.fulfill({ contentType: "text/event-stream", body:
+ 'event: snapshot\ndata: {"generation_id":"other-generation","content":"Other tab answer","steps":[],"sources":[]}\n\n' +
+ 'event: done\ndata: {"stopped":false}\n\n',
+ });
+ return true;
+ }
+ });
+ try {
+ await f.page.getByText("Old answer", { exact: true }).waitFor();
+ await f.page.locator("textarea").fill("Rejected follow-up");
+ await f.page.getByRole("button", { name: "Send", exact: true }).click();
+ const request = await pending.promise;
+ await f.page.locator("textarea").fill("More draft text");
+ await request.fulfill({ status: 409, json: { error: "Conversation is busy", code: "answer_running" } });
+ await f.page.getByText("Other tab answer", { exact: true }).waitFor();
+ assert.equal(await f.page.locator("textarea").inputValue(), "Rejected follow-up\nMore draft text");
+ assert.equal(await f.page.getByText("Rejected follow-up", { exact: true }).count(), 0);
+ assert.equal(await f.page.getByText("Other tab question", { exact: true }).count(), 1);
+ assert.equal(reads, 2);
+ assert.deepEqual(f.errors, []);
+ } finally {
+ await f.close();
+ }
+ });
+ await t.test("a late busy response cannot replace another conversation's draft", async () => {
+ const pending = deferred();
+ const f = await open("/kb/one/chat/a", async (route, p) => {
+ if (p.endsWith("/chat") && route.request().method() === "POST") {
+ pending.resolve(route);
+ return true;
+ }
+ });
+ try {
+ await f.page.getByText("Answer alpha", { exact: true }).waitFor();
+ await f.page.locator("textarea").fill("Old request");
+ await f.page.getByRole("button", { name: "Send", exact: true }).click();
+ const request = await pending.promise;
+ await f.page.evaluate(() => window.__go("/kb/one/chat/b"));
+ await f.page.getByText("Answer beta", { exact: true }).waitFor();
+ await f.page.locator("textarea").fill("New view draft");
+ await request.fulfill({ status: 409, json: { error: "Conversation is busy", code: "answer_running" } });
+ await f.page.waitForFunction(() => window.__live.entry("one", "a") === null);
+ assert.equal(await f.page.locator("textarea").inputValue(), "New view draft");
+ assert.equal(await f.page.evaluate(() => sessionStorage.getItem("chat:draft")), "New view draft");
+ assert.deepEqual(f.errors, []);
+ } finally {
+ await f.close();
+ }
+ });
for (const fail of [false, true])
await t.test(
`late A ${fail ? "failure" : "success"} cannot replace B`,
@@ -974,6 +1096,60 @@ test(
await f.close();
}
});
+ await t.test("a failed answer retries its stored question and Stop targets the retry generation", async () => {
+ const cancellations = [];
+ const acknowledged = deferred();
+ const f = await open("/kb/one/chat/b", async (route, p) => {
+ if (!p.endsWith("/chat/b/stop")) return;
+ cancellations.push(route.request().postDataJSON());
+ await route.fulfill({ json: { ok: true } });
+ acknowledged.resolve();
+ return true;
+ }, () => {
+ const original = window.fetch;
+ window.__asked = [];
+ window.fetch = (url, init) => {
+ if (!String(url).endsWith("/chat") || init?.method !== "POST") return original(url, init);
+ const body = JSON.parse(init.body);
+ window.__asked.push(body);
+ if (!body.retry_message_id) return Promise.resolve(new Response(
+ 'event: conversation\ndata: {"id":"b","message_id":"q1","generation_id":"failed"}\n\n' +
+ 'event: error\ndata: {"error":"Failed"}\n\n',
+ ));
+ return Promise.resolve(new Response(new ReadableStream({ start(c) {
+ window.__wire = c;
+ c.enqueue(new TextEncoder().encode(
+ 'event: conversation\ndata: {"id":"b","message_id":"q1","generation_id":"retry"}\n\n' +
+ 'event: delta\ndata: {"text":"Partial retry"}\n\n',
+ ));
+ } })));
+ };
+ });
+ try {
+ await f.page.getByText("Answer beta", { exact: true }).waitFor();
+ await f.page.locator("textarea").fill("Question");
+ await f.page.locator("textarea").press("Enter");
+ await f.page.getByRole("button", { name: "Retry", exact: true }).evaluate((button) => {
+ button.click();
+ button.click();
+ });
+ await f.page.getByText("Partial retry", { exact: true }).waitFor();
+ await f.page.getByRole("button", { name: "Stop", exact: true }).click();
+ await f.page.waitForFunction(() => window.__live.entry("one", "b")?.stopping);
+ await f.page.evaluate(() => window.__wire.enqueue(new TextEncoder().encode('event: done\ndata: {"stopped":true}\n\n')));
+ await f.page.getByText("Stopped", { exact: true }).waitFor();
+ await acknowledged.promise;
+ assert.deepEqual(cancellations, [{ generation_id: "retry" }]);
+ assert.deepEqual(await f.page.evaluate(() => window.__asked), [
+ { conversation_id: "b", message: "Question" },
+ { conversation_id: "b", message: "Question", retry_message_id: "q1" },
+ ]);
+ assert.equal(await f.page.getByText("Question", { exact: true }).count(), 1);
+ assert.deepEqual(f.errors, []);
+ } finally {
+ await f.close();
+ }
+ });
await t.test("a conversation's title is copied from its menu, with or without the Clipboard API", async () => {
for (const init of [withApi, withoutApi]) {
const f = await open("/kb/one/chat/b", undefined, init);