Skip to content

World's first open-source enterprise world model.

Keep older chat handles from unregistering newer generations (#848)

Maya Kid8594a9c055b9
diff --git a/crates/utopia-server/src/api/chat_empty_reply_tests.rs b/crates/utopia-server/src/api/chat_empty_reply_tests.rs
index 98d53fa8..722c2bdc 100644
--- a/crates/utopia-server/src/api/chat_empty_reply_tests.rs
+++ b/crates/utopia-server/src/api/chat_empty_reply_tests.rs
@@ -305,3 +305,6 @@ async fn a_reply_that_stays_empty_is_an_error_after_one_retry() -> anyhow::Resul
);
f.cleanup().await
}
+
+#[path = "chat_registry_tests.rs"]
+mod registry_tests;
diff --git a/crates/utopia-server/src/api/chat_registry_tests.rs b/crates/utopia-server/src/api/chat_registry_tests.rs
new file mode 100644
index 00000000..2deba455
--- /dev/null
+++ b/crates/utopia-server/src/api/chat_registry_tests.rs
@@ -0,0 +1,98 @@
+//! Two real chat producers share a conversation while a local upstream gates their
+//! answers independently. No scheduler sleeps and no production-only test hooks.
+use super::*;
+use tokio::sync::{broadcast, Notify};
+
+#[derive(Clone, Default)]
+struct Gates {
+ ready: [Arc<Notify>; 2],
+ release: [Arc<Notify>; 2],
+ requests: Arc<Mutex<Vec<serde_json::Value>>>,
+}
+
+async fn upstream(
+ State(gates): State<Gates>,
+ Json(body): Json<serde_json::Value>,
+) -> impl IntoResponse {
+ 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 stream = async_stream::stream! {
+ let payload = if tool_ran {
+ 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"}]})
+ } else {
+ json!({"choices":[{"delta":{"tool_calls":[{"index":0,"id":format!("greeting_{index}"),"function":{"name":"no_evidence_needed","arguments":"{\"reason\":\"greeting\"}"}}]},"finish_reason":"tool_calls"}]})
+ };
+ yield Ok::<_, Infallible>(Event::default().data(payload.to_string()));
+ yield Ok::<_, Infallible>(Event::default().data("[DONE]"));
+ };
+ Sse::new(stream)
+}
+
+#[tokio::test]
+async fn finishing_old_chat_preserves_reattachment_to_new_chat() -> anyhow::Result<()> {
+ let Some(f) = fixture(Scripted::new(vec![])).await? else {
+ return Ok(());
+ };
+ 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))
+ .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?;
+
+ 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() }),
+ ).await.map_err(|_| anyhow::anyhow!("first 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() }),
+ ).await.map_err(|_| anyhow::anyhow!("second chat refused"))?;
+ gates.ready[1].notified().await;
+ 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"))?;
+ 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:?}");
+ Ok::<_, anyhow::Error>(())
+ }).await;
+ gates.release[0].notify_one();
+ gates.release[1].notify_one();
+ server.abort();
+ let _ = server.await;
+ f.cleanup().await?;
+ result??;
+ Ok(())
+}
diff --git a/crates/utopia-server/src/live.rs b/crates/utopia-server/src/live.rs
index aa743b73..1333942c 100644
--- a/crates/utopia-server/src/live.rs
+++ b/crates/utopia-server/src/live.rs
@@ -112,10 +112,18 @@ impl Handle {
let _ = self.tx.send(frame);
}
- /// 生成结束。**注销之后再接上的人得到的是「没有在跑的」**,
- /// 那时答案已经落库,从库里读就是了
+ /// 生成结束,只注销自己仍持有的登记;新一轮可能已经接替了它。
+ /// 当前生成注销后,接上的人得到「没有在跑的」,答案从库里读。
pub async fn finish(self) {
- self.registry.0.write().await.remove(&self.conversation_id);
+ 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.
+ if entries
+ .get(&self.conversation_id)
+ .is_some_and(|entry| Arc::ptr_eq(&entry.snap, &self.snap))
+ {
+ entries.remove(&self.conversation_id);
+ }
}
}
@@ -157,3 +165,104 @@ impl Registry {
Some((guard.clone(), rx))
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn delta(text: &str) -> Frame {
+ Frame::new("delta", json!({"text": text}).to_string())
+ }
+
+ #[tokio::test]
+ async fn replaced_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;
+ old.emit(delta("old")).await;
+ let current = registry.begin(id).await;
+ 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;
+ let (snapshot, _) = registry
+ .attach(id)
+ .await
+ .expect("new generation still running");
+ assert_eq!(snapshot.content, "new");
+ assert!(matches!(
+ rx.try_recv(),
+ Err(broadcast::error::TryRecvError::Empty)
+ ));
+ current.emit(delta(" answer")).await;
+ assert_eq!(rx.recv().await.unwrap().data, delta(" answer").data);
+ current.finish().await;
+ assert!(registry.attach(id).await.is_none());
+ }
+
+ #[tokio::test]
+ async fn only_current_owner_can_remove_entry_in_any_finish_order() {
+ for order in [
+ [0, 1, 2],
+ [0, 2, 1],
+ [1, 0, 2],
+ [1, 2, 0],
+ [2, 0, 1],
+ [2, 1, 0],
+ ] {
+ let registry = Arc::new(Registry::default());
+ let id = Uuid::now_v7();
+ let other_id = Uuid::now_v7();
+ let other = registry.begin(other_id).await;
+ other.emit(delta("unrelated")).await;
+ let mut handles = Vec::new();
+ for _ in 0..3 {
+ handles.push(Some(registry.begin(id).await));
+ }
+ let mut current_finished = false;
+ for index in order {
+ handles[index].take().unwrap().finish().await;
+ current_finished |= index == 2;
+ assert_eq!(
+ registry.attach(id).await.is_none(),
+ current_finished,
+ "{order:?}"
+ );
+ assert_eq!(
+ registry.attach(other_id).await.unwrap().0.content,
+ "unrelated"
+ );
+ }
+ other.finish().await;
+ assert!(registry.attach(other_id).await.is_none());
+ }
+ }
+
+ #[tokio::test]
+ 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.
+ for index in 0..64 {
+ let attached = if index % 2 == 0 {
+ tokio::join!(handle.emit(delta("x")), registry.attach(id)).1
+ } else {
+ tokio::join!(registry.attach(id), handle.emit(delta("x"))).0
+ };
+ let (snapshot, mut rx) = attached.unwrap();
+ let mut combined = snapshot.content;
+ while let Ok(frame) = rx.try_recv() {
+ combined.push_str(
+ serde_json::from_str::<serde_json::Value>(&frame.data).unwrap()["text"]
+ .as_str()
+ .unwrap(),
+ );
+ }
+ assert_eq!(combined, registry.attach(id).await.unwrap().0.content);
+ }
+ handle.finish().await;
+ }
+}