💚 End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[ensu] Harden long conversation preparation and simplify tests
setalp9739eb0e0280
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.ktindex ac6b5bbd97..deba524767 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.kt@@ -933,6 +933,7 @@ private fun StreamingMessageBubble(renderedText = ""return@LaunchedEffect}+ // Throttle markdown re-rendering while streaming to reduce dropped frames.delay(33)renderedText = stripHiddenMessageParts(text)}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.ktindex 03f5006c45..36612da12d 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt@@ -4,7 +4,6 @@ import io.ente.ensu.AppStateimport io.ente.ensu.bindings.AssetDownloadExceptionimport io.ente.ensu.bindings.ConfigDefaultsimport io.ente.ensu.bindings.ConversationException-import io.ente.ensu.bindings.ConversationPreparationimport io.ente.ensu.bindings.ConversationProgressCallbackimport io.ente.ensu.bindings.ConversationRequestimport io.ente.ensu.bindings.DbException@@ -69,7 +68,7 @@ internal class ChatStoreActions(@Volatile private var stopRequested = falseprivate var isForeground = trueprivate var preparingGenerationToken: Long? = null- @Volatile private var activePreparation: ConversationPreparation? = null+ @Volatile private var activePreparation: ConversationPreparationControl? = nullprivate var streamingParentId: String? = nullprivate var activeGenerationToken = 0Lprivate var pendingOverflow: PendingOverflow? = null@@ -569,6 +568,9 @@ internal class ChatStoreActions(hits} catch (error: kotlinx.coroutines.CancellationException) {throw error+ } catch (_: LlmProvider.EmbeddingAssetInvalid) {+ embeddingAssetInvalid = true+ emptyList()} catch (error: Throwable) {if (!isActive() ||@@ -578,11 +580,9 @@ internal class ChatStoreActions() {return@launch}- embeddingAssetInvalid = error is LlmProvider.EmbeddingAssetInvalidlogRepository.log(LogLevel.Warning,- "Source search failed",- details = error.message,+ "Context retrieval failed",tag = "Chat",throwable = error,)@@ -858,9 +858,7 @@ internal class ChatStoreActions() {val buffer = StringBuilder()var tokens = 0- val path =- buildSelectedPath(sessionId).takeWhile { it.id != userMessage.id }.map { it.id } +- userMessage.id+ val path = conversationPath(messageStore[sessionId].orEmpty(), userMessage)val system = buildSystemPrompt()try {llmProvider.withConversationContext(selection) { context ->@@ -882,24 +880,26 @@ internal class ChatStoreActions(searched = searched,),)- activePreparation = preparation.work- if (!isActive() || stopRequested) preparation.work.cancel()- val progress =- object : ConversationProgressCallback {- override fun onProgress() {- if (!isActive() || stopRequested) preparation.work.cancel()- else- state.update {- it.copy(- chat =- it.chat.copy(- preparationStatus = "Remembering earlier messages"- )- )- }- }- }+ val preparationControl = ConversationPreparationControl(preparation.work)+ activePreparation = preparationControltry {+ if (!isActive() || stopRequested) preparationControl.cancel()+ val progress =+ object : ConversationProgressCallback {+ override fun onProgress() {+ if (!isActive() || stopRequested) preparationControl.cancel()+ else+ state.update {+ it.copy(+ chat =+ it.chat.copy(+ preparationStatus =+ "Remembering earlier messages"+ )+ )+ }+ }+ }state.update {it.copy(chat = it.chat.copy(preparationStatus = "Preparing conversation"))}@@ -951,8 +951,7 @@ internal class ChatStoreActions(generated.getOrThrow()} finally {activePreparation = null- preparation.work.cancel()- preparation.work.destroy()+ preparationControl.close()}}} catch (error: kotlinx.coroutines.CancellationException) {@@ -970,7 +969,7 @@ internal class ChatStoreActions(tag = "Chat",throwable = error,)- if (isActive() && buffer.isEmpty())+ if (isActive() && buffer.isEmpty()) {state.update {it.copy(chat =@@ -983,6 +982,8 @@ internal class ChatStoreActions())}+ rebuildChatState(sessionId)+ }}} finally {if (isActive()) state.update { it.copy(chat = it.chat.copy(preparationStatus = null)) }diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ConversationPath.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ConversationPath.ktnew file mode 100644index 0000000000..73e598315a--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ConversationPath.kt@@ -0,0 +1,13 @@+package io.ente.ensu.chat++internal fun conversationPath(messages: List<ChatMessage>, target: ChatMessage): List<String> {+ val byId = messages.associateBy { it.id }+ val path = mutableListOf<String>()+ val visited = mutableSetOf<String>()+ var current: ChatMessage? = target+ while (current != null && visited.add(current.id)) {+ path.add(current.id)+ current = current.parentId?.let(byId::get)+ }+ return path.asReversed()+}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ConversationPreparationControl.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ConversationPreparationControl.ktnew file mode 100644index 0000000000..70dee0c3fc--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ConversationPreparationControl.kt@@ -0,0 +1,24 @@+package io.ente.ensu.chat++import io.ente.ensu.bindings.ConversationPreparation++internal class ConversationPreparationControl(preparation: ConversationPreparation) :+ AutoCloseable {+ private var preparation: ConversationPreparation? = preparation++ @Synchronized+ fun cancel() {+ preparation?.cancel()+ }++ @Synchronized+ override fun close() {+ val current = preparation ?: return+ preparation = null+ try {+ current.cancel()+ } finally {+ current.destroy()+ }+ }+}diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/chat/ConversationPathTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/chat/ConversationPathTest.ktnew file mode 100644index 0000000000..33fe2233ec--- /dev/null+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/chat/ConversationPathTest.kt@@ -0,0 +1,53 @@+package io.ente.ensu.chat++import org.junit.Assert.assertEquals+import org.junit.Test++class ConversationPathTest {+ @Test+ fun followsTheRequestedBranchThroughDuplicateEdits() {+ val root = message("root")+ val editedRoot = root.copy(id = "edited-root", timestampMillis = 1)+ val answer = message("answer", editedRoot.id, MessageAuthor.Assistant)+ val user = message("user", answer.id)+ val editedUser = user.copy(id = "edited-user", timestampMillis = 1)+ val descendant = message("descendant", editedUser.id, MessageAuthor.Assistant)+ val messages = listOf(root, editedRoot, answer, user, editedUser, descendant)++ assertEquals(listOf(editedRoot.id), conversationPath(messages, editedRoot))+ assertEquals(+ listOf(editedRoot.id, answer.id, editedUser.id),+ conversationPath(messages, editedUser),+ )+ }++ @Test+ fun stopsAtAMissingParentWithoutChangingMessageIds() {+ val root = message("orphan", "deleted-parent")+ val target = message("target", root.id)++ assertEquals(listOf(root.id, target.id), conversationPath(listOf(root, target), target))+ }++ @Test+ fun terminatesWhenParentLinksCycle() {+ val first = message("first", "second")+ val second = message("second", first.id)++ assertEquals(listOf(first.id, second.id), conversationPath(listOf(first, second), second))+ }++ private fun message(+ id: String,+ parentId: String? = null,+ author: MessageAuthor = MessageAuthor.User,+ ) =+ ChatMessage(+ id = id,+ sessionId = "session",+ parentId = parentId,+ author = author,+ text = "Same text",+ timestampMillis = 0,+ )+}diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/chat/ConversationPreparationControlTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/chat/ConversationPreparationControlTest.ktnew file mode 100644index 0000000000..a8c6946eb7--- /dev/null+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/chat/ConversationPreparationControlTest.kt@@ -0,0 +1,84 @@+package io.ente.ensu.chat++import io.ente.ensu.bindings.ConversationPreparation+import io.ente.ensu.bindings.NoHandle+import java.util.concurrent.CountDownLatch+import java.util.concurrent.Executors+import java.util.concurrent.TimeUnit+import java.util.concurrent.atomic.AtomicBoolean+import java.util.concurrent.atomic.AtomicInteger+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Test++class ConversationPreparationControlTest {+ @Test+ fun cancellationAfterCloseDoesNotReachDestroyedWork() {+ val work = RecordingPreparation()+ val control = ConversationPreparationControl(work)++ control.close()+ control.cancel()+ control.close()++ assertEquals(1, work.cancelCalls.get())+ assertEquals(1, work.destroyCalls.get())+ }++ @Test+ fun closeWaitsForCancellationAlreadyInProgress() {+ val cancelEntered = CountDownLatch(1)+ val releaseCancel = CountDownLatch(1)+ val closeStarted = CountDownLatch(1)+ val closeFinished = CountDownLatch(1)+ val work = RecordingPreparation { call ->+ if (call == 1) {+ cancelEntered.countDown()+ assertTrue(releaseCancel.await(5, TimeUnit.SECONDS))+ }+ }+ val control = ConversationPreparationControl(work)+ val executor = Executors.newFixedThreadPool(2)+ try {+ val cancellation = executor.submit { control.cancel() }+ assertTrue(cancelEntered.await(5, TimeUnit.SECONDS))+ val cleanup = executor.submit {+ closeStarted.countDown()+ control.close()+ closeFinished.countDown()+ }+ assertTrue(closeStarted.await(5, TimeUnit.SECONDS))+ assertFalse(closeFinished.await(100, TimeUnit.MILLISECONDS))+ assertEquals(0, work.destroyCalls.get())++ releaseCancel.countDown()+ cancellation.get(5, TimeUnit.SECONDS)+ cleanup.get(5, TimeUnit.SECONDS)+ assertEquals(2, work.cancelCalls.get())+ assertEquals(1, work.destroyCalls.get())+ } finally {+ releaseCancel.countDown()+ executor.shutdownNow()+ }+ }++ private class RecordingPreparation(private val onCancel: (Int) -> Unit = {}) :+ ConversationPreparation(NoHandle) {+ val cancelCalls = AtomicInteger()+ val destroyCalls = AtomicInteger()+ private val destroyed = AtomicBoolean()++ override fun cancel() {+ check(!destroyed.get()) { "Preparation was already destroyed" }+ onCancel(cancelCalls.incrementAndGet())+ check(!destroyed.get()) { "Preparation was destroyed during cancellation" }+ }++ override fun destroy() {+ destroyed.set(true)+ destroyCalls.incrementAndGet()+ super.destroy()+ }+ }+}diff --git a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swiftindex 6aec1580cd..fe5759b2d4 100644--- a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift@@ -133,6 +133,7 @@ final class ChatViewModel: ObservableObject {?? FileManager.default.temporaryDirectorylet config = ConfigDefaults.shared+ // Must initialize after the asset store migrates the persisted selection.self.modelSettings = ModelSettingsStore.sharedlet transcription = transcriptionModelAsset()let transcriber = Transcriber(@@ -1141,7 +1142,6 @@ final class ChatViewModel: ObservableObject {return}embeddingAssetInvalid = error is EmbeddingAssetInvalidError- logger.warning("Source search failed", details: "\(error)")}}@@ -1319,18 +1319,11 @@ final class ChatViewModel: ObservableObject {do {let runGeneration: () async throws -> GenerationSummary = {if prompt.imageFiles.isEmpty {- let path =- self.buildSelectedPath(- for: userNode.sessionId,- childrenMap: self.childrenByParent(sessionId: userNode.sessionId)- ).prefix { $0.id != userNode.id }.map { $0.id.uuidString } + [- userNode.id.uuidString- ]return try await self.generatePreparedChat(selection,request: ConversationRequest(sessionUuid: userNode.sessionId.uuidString,- path: path,+ path: self.buildConversationPath(for: userNode),system: normalSystemPrompt,current: prompt.text,expectedUserText: userNode.text,@@ -1444,7 +1437,6 @@ final class ChatViewModel: ObservableObject {try control.checkCancellation()return try preparation.run(callback: sink)}.value- try control.checkCancellation()try control.beginAnswer()onPrepared(preparation)try await Task.detached { try preparation.validate() }.value@@ -1791,6 +1783,7 @@ final class ChatViewModel: ObservableObject {await MainActor.run {guard self.messageStore[sessionId] != nil else { return }self.messageStore[sessionId] = nodes+ // Branch selection is computed in-memory.self.branchSelections[sessionId] = [:]self.invalidateChildrenCache(for: sessionId)if self.currentSessionId == sessionId {@@ -1855,6 +1848,19 @@ final class ChatViewModel: ObservableObject {return UUID(uuid: uuid)}+ private func buildConversationPath(for target: MessageNode) -> [String] {+ let byId = Dictionary(+ uniqueKeysWithValues: (messageStore[target.sessionId] ?? []).map { ($0.id, $0) })+ var path: [String] = []+ var visited = Set<UUID>()+ var current: MessageNode? = target+ while let node = current, visited.insert(node.id).inserted {+ path.append(node.id.uuidString)+ current = node.parentId.flatMap { byId[$0] }+ }+ return path.reversed()+ }+private func buildSelectedPath(for sessionId: UUID, childrenMap: [UUID: [MessageNode]])-> [MessageNode]{diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex 059baf4b8c..5b7bc5c631 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2536,7 +2536,6 @@ dependencies = ["self_cell","serde","serde_json",- "sha2 0.11.0","tempfile","thiserror 2.0.18","tokio",diff --git a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs b/rust/apps/ensu/src-tauri/src/commands/chat_db.rsindex 10ff54d6fd..6abf918ee3 100644--- a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs+++ b/rust/apps/ensu/src-tauri/src/commands/chat_db.rs@@ -504,6 +504,7 @@ pub async fn chat_db_compress_attachment_image_file() -> Result<Vec<u8>, ApiError> {let scope = app.fs_scope();async_runtime::spawn_blocking(move || {+ // Read the same canonical path checked against the filesystem scope.let canonical_path = fs::canonicalize(&path).map_err(|_| {ApiError::new("io_scope",diff --git a/rust/apps/ensu/src-tauri/src/commands/conversation.rs b/rust/apps/ensu/src-tauri/src/commands/conversation.rsindex 76f2c49e9c..b2e47dce74 100644--- a/rust/apps/ensu/src-tauri/src/commands/conversation.rs+++ b/rust/apps/ensu/src-tauri/src/commands/conversation.rs@@ -115,14 +115,6 @@ fn preparation_error(cause: PrepareError) -> ApiError {_ => error(cause.to_string()),}}-fn request(messages: Vec<llm::ChatMessage>, output: i32) -> llm::ChatRequest {- llm::ChatRequest {- messages,- max_tokens: Some(output),- temperature: Some(0.0),- ..Default::default()- }-}struct NativeEffects<'a> {app: AppHandle,@@ -162,8 +154,10 @@ impl Effects for NativeEffects<'_> {}fn measure(&mut self, messages: &[llm::ChatMessage]) -> Result<usize, PrepareError> {self.context- .measure_text_chat_prompt(&request(messages.to_vec(), 1))- .map(|m| m.prompt_tokens)+ .measure_text_chat_prompt(&llm::ChatRequest {+ messages: messages.to_vec(),+ ..Default::default()+ }).map_err(|e| PrepareError::Backend(e.to_string()))}fn summarize(@@ -176,12 +170,8 @@ impl Effects for NativeEffects<'_> {"conversation-progress",serde_json::json!({"preparationToken": self.token}),);- let stops = conversation::summary_stop_sequences(self.measure(&messages)?, output);- let mut generation = request(- messages,- i32::try_from(output).map_err(|_| PrepareError::CannotFit)?,- );- generation.stop_sequences = Some(stops);+ let prompt_tokens = self.measure(&messages)?;+ let generation = conversation::summary_request(messages, output, prompt_tokens)?;let mut text = String::new();let epoch = self.epoch.clone();let expected = self.expected_epoch;diff --git a/rust/apps/ensu/src-tauri/src/commands/llm.rs b/rust/apps/ensu/src-tauri/src/commands/llm.rsindex 0996b62daf..b9fff4f1c7 100644--- a/rust/apps/ensu/src-tauri/src/commands/llm.rs+++ b/rust/apps/ensu/src-tauri/src/commands/llm.rs@@ -466,6 +466,7 @@ pub async fn llm_load_model("LLM",format!("load model requested model_path={}", params.model_path),);+ // llama.cpp clamps this to the model's layer count.const DEFAULT_GPU_LAYERS: i32 = 999;let mut params = params;if params.n_gpu_layers.is_none() {diff --git a/rust/bindings/uniffi/ensu/src/conversation.rs b/rust/bindings/uniffi/ensu/src/conversation.rsindex 466802a534..ef5b8a1bfd 100644--- a/rust/bindings/uniffi/ensu/src/conversation.rs+++ b/rust/bindings/uniffi/ensu/src/conversation.rs@@ -81,15 +81,6 @@ pub struct ConversationPreparation {summary_job: AtomicI64,}-fn request(messages: Vec<llm::ChatMessage>, max_tokens: usize) -> llm::ChatRequest {- llm::ChatRequest {- messages,- max_tokens: Some(max_tokens as i32),- temperature: Some(0.0),- ..Default::default()- }-}-#[uniffi::export]impl EnsuDb {pub fn prepare_conversation(@@ -283,8 +274,10 @@ impl core::Effects for NativeEffects<'_> {fn measure(&mut self, messages: &[llm::ChatMessage]) -> Result<usize, core::PrepareError> {self.owner.context- .measure_text_chat_prompt(&request(messages.to_vec(), 1))- .map(|result| result.prompt_tokens)+ .measure_text_chat_prompt(&llm::ChatRequest {+ messages: messages.to_vec(),+ ..Default::default()+ }).map_err(|cause| core::PrepareError::Backend(cause.to_string()))}@@ -296,9 +289,8 @@ impl core::Effects for NativeEffects<'_> {self.check_current()?;self.callback.on_progress();self.check_cancelled()?;- let stops = core::summary_stop_sequences(self.measure(&messages)?, output);- let mut generation = request(messages, output);- generation.stop_sequences = Some(stops);+ let prompt_tokens = self.measure(&messages)?;+ let generation = core::summary_request(messages, output, prompt_tokens)?;let mut text = String::new();let result = self.ownerdiff --git a/rust/checks/lint-exceptions/suppressions.json b/rust/checks/lint-exceptions/suppressions.jsonindex 6209a451e9..3e7f8425cb 100644--- a/rust/checks/lint-exceptions/suppressions.json+++ b/rust/checks/lint-exceptions/suppressions.json@@ -35,6 +35,7 @@"rust/crates/core/src/crypto/types.rs": ["clippy::expect_used"],"rust/crates/ensu/src/config.rs": ["clippy::expect_used"],"rust/crates/ensu/src/config/knowledge.rs": ["clippy::expect_used"],+ "rust/crates/ensu/src/conversation/lookup.rs": ["clippy::expect_used"],"rust/crates/ensu/src/llm/context.rs": ["unsafe_code"],"rust/crates/ensu/src/llm/generate.rs": ["clippy::expect_used"],"rust/crates/ensu/src/model/mod.rs": ["clippy::expect_used"],diff --git a/rust/crates/ensu/Cargo.toml b/rust/crates/ensu/Cargo.tomlindex 4e03e510ea..087b289d1c 100644--- a/rust/crates/ensu/Cargo.toml+++ b/rust/crates/ensu/Cargo.toml@@ -14,30 +14,37 @@ rusqlite = { workspace = true, default-features = true, optional = true }self_cell = "1.0.0"serde.workspace = trueserde_json.workspace = true-sha2.workspace = truethiserror.workspace = truetokio = { workspace = true, features = ["rt", "time"] }transcribe-rs = { git = "https://github.com/cjpais/transcribe-rs.git", rev = "efc66111d519a434e28d51d94740008a7ce29360", default-features = false, features = ["onnx", "vad-silero"], optional = true }url.workspace = true-uuid = { workspace = true, features = ["v4", "rng-getrandom", "serde"] }+uuid = { workspace = true, features = ["v4", "rng-getrandom"] }zeroize.workspace = truezstd = "0.13.3"[dev-dependencies]tempfile.workspace = true+# Keep `mtmd` on every platform for multimodal support.+# macOS disables OpenMP because Apple's clang lacks libomp. llama.cpp uses its+# own thread pool, Accelerate, and Metal on arm64 instead.[target.'cfg(target_os = "macos")'.dependencies]llama-cpp-2 = { version = "=0.1.144", default-features = false, features = ["mtmd"] }+# Linux Vulkan needs a system Vulkan loader and glslc.[target.'cfg(target_os = "linux")'.dependencies]llama-cpp-2 = { version = "=0.1.144", features = ["mtmd", "vulkan"] }+# Other platforms stay on CPU. Android keeps the shared C++ runtime; Windows+# avoids Vulkan's VULKAN_SDK requirement.[target.'cfg(all(not(target_os = "macos"), not(target_os = "linux")))'.dependencies]llama-cpp-2 = { version = "=0.1.144", features = ["mtmd"] }[target.'cfg(target_os = "android")'.dependencies]ort = { workspace = true, features = ["load-dynamic"], optional = true }+# The iOS app links Ente's custom ONNX Runtime XCFramework via SPM, so ort-sys+# must not download/link pyke's binaries.[target.'cfg(target_os = "ios")'.dependencies]ort-sys = { workspace = true, features = ["disable-linking"], optional = true }diff --git a/rust/crates/ensu/src/conversation.rs b/rust/crates/ensu/src/conversation.rsindex cffa7c3cd4..49986054a3 100644--- a/rust/crates/ensu/src/conversation.rs+++ b/rust/crates/ensu/src/conversation.rs@@ -4,6 +4,7 @@ mod lookup;mod prepare;mod state;mod turn;+mod uuid_text;pub use budget::{GenerationBudget, resolve_generation_budget};pub use grounding::{FittedGrounding, GroundingCandidates, MAX_GROUNDING_BYTES};@@ -20,11 +21,10 @@ use std::collections::HashSet;use std::ops::Range;use serde::{Deserialize, Serialize};-use sha2::{Digest, Sha256};use uuid::Uuid;use crate::db::{AttachmentKind, Message, Sender};-use crate::llm::{ChatMessage, FinishReason, strip_hidden_parts_text};+use crate::llm::{ChatMessage, ChatRequest, FinishReason, strip_hidden_parts_text};pub(crate) const MAX_STATE_BYTES: usize = 32 * 1024;const MAX_SUMMARY_BYTES: usize = 16 * 1024;@@ -33,12 +33,22 @@ pub const MAX_HISTORY_BYTES: usize = 32 * 1024 * 1024;const SAFETY_TOKENS: usize = 256;const SUMMARY_OUTPUT_TOKENS: usize = 512;-pub fn summary_stop_sequences(prompt_tokens: usize, output: usize) -> Vec<String> {- if prompt_tokens > output.saturating_mul(2) {- vec!["{\"fragment_start_byte\":".into()]- } else {- Vec::new()- }+pub fn summary_request(+ messages: Vec<ChatMessage>,+ output: usize,+ prompt_tokens: usize,+) -> Result<ChatRequest, Error> {+ Ok(ChatRequest {+ messages,+ max_tokens: Some(i32::try_from(output).map_err(|_| Error::InvalidLimits)?),+ temperature: Some(0.0),+ stop_sequences: Some(if prompt_tokens > output.saturating_mul(2) {+ vec!["{\"fragment_start_byte\":".into()]+ } else {+ Vec::new()+ }),+ ..Default::default()+ })}fn summary_instruction(retry: bool) -> String {@@ -73,6 +83,7 @@ pub enum Error {#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]pub struct Summary {+ #[serde(with = "uuid_text")]pub covered_boundary_message_uuid: Uuid,pub covered_prefix_fingerprint: String,pub text: String,@@ -111,7 +122,7 @@ impl ConversationState {}}-pub fn fingerprint(messages: &[Message]) -> String {+fn fingerprint(messages: &[Message]) -> String {let rows: Vec<_> = messages.iter().map(|m| {@@ -143,7 +154,7 @@ pub fn fingerprint(messages: &[Message]) -> String {let bytes = serde_json::json!(["ensu-history-v1", rows]).to_string().into_bytes();- Sha256::digest(bytes)+ ente_ensu_crypto::sha256(&bytes).iter().map(|byte| format!("{byte:02x}")).collect()@@ -153,8 +164,10 @@ pub(crate) fn validate_path(messages: &[Message]) -> Result<(), Error> {if messages.len() > MAX_HISTORY_MESSAGES {return Err(Error::HistoryLimit);}- let mut seen = HashSet::new();- let mut previous = None;+ let mut previous = messages+ .first()+ .and_then(|message| message.parent_message_uuid);+ let mut seen = previous.into_iter().collect::<HashSet<_>>();let mut bytes = 0usize;for message in messages {if !seen.insert(message.uuid)diff --git a/rust/crates/ensu/src/conversation/budget.rs b/rust/crates/ensu/src/conversation/budget.rsindex 37ac19184e..f4ca6ae31b 100644--- a/rust/crates/ensu/src/conversation/budget.rs+++ b/rust/crates/ensu/src/conversation/budget.rs@@ -26,36 +26,39 @@ pub fn resolve_generation_budget(mod tests {use super::*;- #[derive(serde::Deserialize)]- struct Case {- name: String,- context: usize,- configured: Option<usize>,- output: Option<usize>,- input: Option<usize>,- }-#[test]- fn shared_generation_budget_fixtures() {- let cases: Vec<Case> = serde_json::from_str(include_str!(- "../../tests/fixtures/generation-budgets-v1.json"- ))- .unwrap();- for case in cases {- let result = resolve_generation_budget(case.context, case.configured);- if let Some(output) = case.output {+ fn resolves_generation_budgets() {+ for (name, context, configured, expected) in [+ ("native Auto", 12000, None, Some((2048, 9696))),+ ("native small", 2048, None, Some((512, 1280))),+ ("Auto impossible", 300, None, None),+ (+ "preserve long output",+ 12000,+ Some(6000),+ Some((6000, 5744)),+ ),+ ("reject oversized override", 2048, Some(2048), None),+ ("zero output", 12000, Some(0), None),+ ("too small context", 257, Some(1), None),+ ("small explicit valid", 258, Some(1), Some((1, 1))),+ ("no prompt room", 4096, Some(3840), None),+ ("last token room", 4096, Some(3839), Some((3839, 1))),+ ("context too large", 2147483648, None, None),+ ] {+ let result = resolve_generation_budget(context, configured);+ if let Some((output, input)) = expected {assert_eq!(result.unwrap(),GenerationBudget {- context: case.context,+ context,output,- input: case.input.unwrap()+ input,},- "{}",- case.name+ "{name}");} else {- assert!(result.is_err(), "{}", case.name);+ assert!(result.is_err(), "{name}");}}}diff --git a/rust/crates/ensu/src/conversation/grounding_tests.rs b/rust/crates/ensu/src/conversation/grounding_tests.rsindex 2f09919b61..08546fcbc2 100644--- a/rust/crates/ensu/src/conversation/grounding_tests.rs+++ b/rust/crates/ensu/src/conversation/grounding_tests.rs@@ -37,7 +37,6 @@ fn measured_repack_keeps_text_and_citations_together() {),] {let candidates = GroundingCandidates::new(vec![hit(&raw, "one.md")], 6000).unwrap();- let original = serde_json::to_string(&candidates).unwrap();let fit = fit_grounding(&candidates, "System", "Question", 4096, measure).unwrap();assert!(fit.repacked);assert!(fit.evidence_tokens <= 1024);@@ -45,7 +44,6 @@ fn measured_repack_keeps_text_and_citations_together() {assert_eq!(context.sources, vec![candidates.searched[0].source.clone()]);assert!(context.text.contains(prefix));assert!(context.text.len() < raw.len());- assert_eq!(serde_json::to_string(&candidates).unwrap(), original);}}#[test]@@ -64,19 +62,12 @@ fn mandatory_input_and_insufficient_evidence_budget_are_explicit() {);}#[test]-fn candidate_limits_and_cancellation_are_enforced() {+fn candidate_limits_are_enforced() {let hit = hit("text", "one.md");assert!(GroundingCandidates::new(vec![hit.clone(); 8], 6000).is_err());let mut candidates = GroundingCandidates::new(vec![hit], 6000).unwrap();candidates.searched[0].text = "x".repeat(6001);assert!(candidates.validate().is_err());- candidates.searched[0].text = "text".into();- assert!(matches!(- fit_grounding(&candidates, "System", "Question", 4096, |_| Err(- PrepareError::Cancelled- )),- Err(PrepareError::Cancelled)- ));}fn prior_history() -> Vec<Message> {vec![Message {diff --git a/rust/crates/ensu/src/conversation/lookup.rs b/rust/crates/ensu/src/conversation/lookup.rsindex 1f3a079fe7..f3ec08bd4b 100644--- a/rust/crates/ensu/src/conversation/lookup.rs+++ b/rust/crates/ensu/src/conversation/lookup.rs@@ -11,6 +11,7 @@ const LOOKUP_EXCERPT_BYTES: usize = 512;#[derive(Debug, Clone, Serialize)]pub(super) struct HistoryExcerpt {+ #[serde(serialize_with = "super::uuid_text::serialize")]pub message_uuid: Uuid,pub speaker: String,pub start_utf8: usize,@@ -304,81 +305,109 @@ mod tests {}#[test]- fn shared_history_lookup_semantics() {- #[derive(Deserialize)]- struct Query {- query: String,- lookup: Vec<usize>,- required: Option<Vec<usize>>,- }- #[derive(Deserialize)]- struct Fixture {- name: String,- history: Vec<(String, String)>,- queries: Vec<Query>,- }- let fixtures: Vec<Fixture> =- serde_json::from_str(include_str!("../../tests/fixtures/history-lookup-v1.json"))- .unwrap();- for fixture in fixtures {- let rows = fixture- .history- .iter()- .map(|(sender, text)| (text.as_str(), sender.parse().unwrap()))- .collect::<Vec<_>>();+ fn history_lookup_preserves_ranking_and_query_intent() {+ for (rows, queries) in [+ (+ vec![+ ("The reservation code is OLD-11.", Sender::SelfUser),+ ("First choice noted.", Sender::Other),+ ("The reservation code is MIDDLE-22.", Sender::SelfUser),+ ("Second choice noted.", Sender::Other),+ ("The reservation code is NEW-33.", Sender::SelfUser),+ ("Final choice noted.", Sender::Other),+ ],+ vec![+ ("reservation code", vec![5, 6, 3, 4], None),+ ("What did you say earlier?", vec![], None),+ (+ "Recall the reservation code",+ vec![5, 6, 3, 4],+ Some(vec![5, 6]),+ ),+ ("याद reservation code", vec![5, 6, 3, 4], Some(vec![5, 6])),+ ("earlier-version reservation code", vec![5, 6, 3, 4], None),+ ],+ ),+ (+ vec![+ ("The launch ticket is OPS-4812.", Sender::SelfUser),+ ("The original ticket is recorded.", Sender::Other),+ ("A launch rehearsal is scheduled.", Sender::SelfUser),+ ("The rehearsal is recorded.", Sender::Other),+ ("A launch checklist is ready.", Sender::SelfUser),+ ("The checklist is recorded.", Sender::Other),+ ],+ vec![+ ("launch OPS-4812", vec![1, 2, 5, 6], None),+ ("OPS-481", vec![], None),+ ("Recall unknown penguins", vec![], None),+ ],+ ),+ (+ vec![+ (+ "The café label is Café‑Bleu, with reference RÉF-823. हिन्दी🙂",+ Sender::SelfUser,+ ),+ ("The spelling is confirmed.", Sender::Other),+ ("Another café label is available.", Sender::SelfUser),+ ("A later alternative.", Sender::Other),+ ],+ vec![+ ("café \"Café‑Bleu\"", vec![1, 2, 3, 4], None),+ ("réf-823", vec![1, 2], None),+ ("हिन्दी", vec![1, 2], None),+ ("हिन्द", vec![], None),+ ],+ ),+ (+ vec![+ ("The original destination is Kyoto.", Sender::SelfUser),+ ("The original destination is recorded.", Sender::Other),+ ("The later destination is Lisbon.", Sender::SelfUser),+ ("The later destination is recorded.", Sender::Other),+ ],+ vec![+ (+ "message 00000000-0000-0000-0000-000000000001",+ vec![1, 2],+ Some(vec![1, 2]),+ ),+ (+ "message 00000000-0000-0000-0000-000000000003-extra",+ vec![],+ None,+ ),+ ],+ ),+ ] {let history = history(&rows);let indices = |found: &HistoryLookup| {found.excerpts.iter()- .map(|excerpt| excerpt.message_uuid.as_u128() as usize)+ .map(|excerpt| excerpt.message_uuid.as_u128()).collect::<Vec<_>>()};- for case in fixture.queries {- let found = lookup(&history, &case.query);- assert_eq!(- indices(&found),- case.lookup,- "{}: {}",- fixture.name,- case.query- );- let required = requested_history(&history, &case.query, || Ok(())).unwrap();+ for (query, expected, required) in queries {+ let found = lookup(&history, query);+ assert_eq!(indices(&found), expected, "{query}");assert_eq!(- required.as_ref().map(indices),- case.required,- "{}: {}",- fixture.name,- case.query+ requested_history(&history, query, || Ok(()))+ .unwrap()+ .as_ref()+ .map(indices),+ required,+ "{query}");- if !found.excerpts.is_empty() {- let rendered = found.prepend_required(&case.query);- let payload: serde_json::Value = serde_json::from_str(- rendered.lines().find(|line| line.starts_with('{')).unwrap(),- )- .unwrap();- let excerpts = payload["excerpts"].as_array().unwrap();- let mut chronological = case.lookup.clone();- chronological.sort_unstable();- assert_eq!(- excerpts- .iter()- .map(|excerpt| Uuid::parse_str(- excerpt["message_uuid"].as_str().unwrap()- )- .unwrap()- .as_u128() as usize)- .collect::<Vec<_>>(),- chronological- );- for excerpt in &found.excerpts {- let original = &history[excerpt.message_uuid.as_u128() as usize - 1];- assert_eq!(excerpt.speaker, role(original.sender));- assert_eq!(- excerpt.text,- original.text[excerpt.start_utf8..excerpt.end_utf8]- );- }+ let rendered = found.prepend_required(query);+ let mut chronological = expected;+ chronological.sort_unstable();+ let mut previous = 0;+ for index in chronological {+ let position = rendered.find(&Uuid::from_u128(index).to_string()).unwrap();+ assert!(position > previous);+ previous = position;}}}@@ -482,9 +511,5 @@ mod tests {.unwrap().is_none());- assert!(matches!(- requested_history(&h, "Recall reservation", || Err(PrepareError::Cancelled)),- Err(PrepareError::Cancelled)- ));}}diff --git a/rust/crates/ensu/src/conversation/state.rs b/rust/crates/ensu/src/conversation/state.rsindex 967d8db96f..68a36bfb07 100644--- a/rust/crates/ensu/src/conversation/state.rs+++ b/rust/crates/ensu/src/conversation/state.rs@@ -8,6 +8,7 @@ pub const ENVELOPE_VERSION: u32 = 2;#[derive(Debug, Serialize)]pub struct ConversationEnvelope {pub format_version: u32,+ #[serde(serialize_with = "super::uuid_text::serialize")]pub session_uuid: Uuid,pub summary: Option<Summary>,}diff --git a/rust/crates/ensu/src/conversation/tests.rs b/rust/crates/ensu/src/conversation/tests.rsindex fef1dd899b..7b217d3620 100644--- a/rust/crates/ensu/src/conversation/tests.rs+++ b/rust/crates/ensu/src/conversation/tests.rs@@ -13,77 +13,68 @@ fn message(index: u128, sender: Sender, text: &str) -> Message {}#[test]-fn shared_conversation_fingerprints_and_exchange_boundaries() {- #[derive(Deserialize)]- struct Attachment {- id: String,- kind: AttachmentKind,- size: i64,- name: String,- }- #[derive(Deserialize)]- struct Row {- uuid: Uuid,- session_uuid: Uuid,- parent_message_uuid: Option<Uuid>,- sender: String,- text: String,- attachments: Vec<Attachment>,- }- #[derive(Deserialize)]- struct Fixture {- name: String,- messages: Vec<Row>,- fingerprint: String,- exchange_ends: Vec<usize>,- }- let fixtures: Vec<Fixture> = serde_json::from_str(include_str!(- "../../tests/fixtures/conversation-core-v1.json"- ))- .unwrap();- for fixture in fixtures {- let history: Vec<_> = fixture- .messages- .into_iter()- .map(|row| Message {- uuid: row.uuid,- session_uuid: row.session_uuid,- parent_message_uuid: row.parent_message_uuid,- sender: row.sender.parse().unwrap(),- text: row.text,- attachments: row- .attachments- .into_iter()- .map(|attachment| crate::db::AttachmentMeta {- id: attachment.id,- kind: attachment.kind,- size: attachment.size,- name: attachment.name,- })- .collect(),- created_at: 0,- })- .collect();- assert_eq!(- fingerprint(&history),- fixture.fingerprint,- "{}",- fixture.name- );- assert_eq!(- exchanges(&history)- .into_iter()- .map(|range| range.end)- .collect::<Vec<_>>(),- fixture.exchange_ends,- "{}",- fixture.name- );- assert!(validate_path(&history).is_ok());- let mut invalid = history;- invalid[1].parent_message_uuid = None;- assert!(validate_path(&invalid).is_err());- }+fn history_fingerprints_and_exchange_boundaries() {+ let mut history = vec![+ message(1, Sender::SelfUser, "Café🙂"),+ message(2, Sender::Other, &"x".repeat(20)),+ ];+ history[0].attachments.push(crate::db::AttachmentMeta {+ id: "file".into(),+ kind: AttachmentKind::Document,+ name: "notes.md".into(),+ size: 12,+ });+ assert_eq!(+ fingerprint(&history),+ "711773b8c54ac4b3b13bd9f2ff6da94956549422b10a66ca105c3f775f28740e"+ );+ assert_eq!(exchanges(&history), vec![0..2]);+ assert!(validate_path(&history).is_ok());+ let state = ConversationState::new(Uuid::from_u128(100), &history, "Memory".into()).unwrap();+ history[0].attachments[0].name = "renamed.md".into();+ assert!(state.coverage(&history).is_none());+ history[1].parent_message_uuid = None;+ assert!(validate_path(&history).is_err());++ let history = vec![+ message(1, Sender::Other, "Legacy prefix"),+ message(2, Sender::SelfUser, "Unanswered"),+ message(3, Sender::SelfUser, "Next"),+ message(4, Sender::Other, "Partial"),+ ];+ assert_eq!(exchanges(&history), vec![0..1, 1..2, 2..4]);+ assert!(validate_path(&history).is_ok());+}++#[test]+fn saved_memory_preserves_uuid_strings() {+ let session = Uuid::from_u128(100);+ let encoded = concat!(+ "{\"format_version\":2,\"session_uuid\":\"00000000-0000-0000-0000-000000000064\",",+ "\"summary\":{\"covered_boundary_message_uuid\":\"00000000-0000-0000-0000-000000000002\",",+ "\"covered_prefix_fingerprint\":\"711773b8c54ac4b3b13bd9f2ff6da94956549422b10a66ca105c3f775f28740e\",",+ "\"text\":\"Memory\"}}"+ );+ let restored = ConversationEnvelope::decode(encoded.as_bytes(), session).unwrap();+ assert_eq!(+ restored+ .summary+ .as_ref()+ .unwrap()+ .covered_boundary_message_uuid,+ Uuid::from_u128(2)+ );+ assert_eq!(+ serde_json::to_value(&restored).unwrap(),+ serde_json::from_str::<serde_json::Value>(encoded).unwrap()+ );+ let invalid = encoded.replace("00000000-0000-0000-0000-000000000002", "invalid");+ assert!(+ ConversationEnvelope::decode(invalid.as_bytes(), session)+ .unwrap()+ .summary+ .is_none()+ );}#[test]@@ -217,6 +208,34 @@ fn work(history: Vec<Message>, state: Option<ConversationState>) -> Preparation.unwrap()}+#[test]+fn orphan_history_compacts_without_rewriting_parent_metadata() {+ let mut history = long_history(6, 800);+ history[0].parent_message_uuid = Some(Uuid::from_u128(999));+ history.push(message(13, Sender::SelfUser, "Next question"));+ let mut effects = Fake::default();+ let mut prepared = prepare_turn(turn_input(history.clone()), &mut effects).unwrap();+ prepared.preparation.run(&mut effects).unwrap();+ assert!(!effects.checkpoints.is_empty());+ let state = effects.checkpoints.last().unwrap();+ assert!(state.coverage(&history).is_some());+ history[0].parent_message_uuid = None;+ assert!(state.coverage(&history).is_none());+}++#[test]+fn orphan_roots_do_not_allow_self_references_or_cycles() {+ let mut history = vec![+ message(2, Sender::SelfUser, "Question"),+ message(3, Sender::Other, "Answer"),+ ];+ assert!(validate_path(&history).is_ok());+ for parent in [history[0].uuid, history[1].uuid] {+ history[0].parent_message_uuid = Some(parent);+ assert!(validate_path(&history).is_err());+ }+}+#[test]fn required_original_excerpt_survives_compaction_without_duplicate_lookup() {let mut history = long_history(8, 800);@@ -341,7 +360,7 @@ fn rebuilds_context_when_a_branch_extends_a_summarized_unanswered_user() {}#[test]-fn recovers_omitted_details_within_measured_allowance_without_new_summary_or_tail_duplication() {+fn recovers_omitted_details_without_resummarizing_or_duplicating_tail() {let mut history = vec![message(1,@@ -413,7 +432,7 @@ fn recovers_omitted_details_within_measured_allowance_without_new_summary_or_tai}#[test]-fn recovery_never_displaces_current_input_or_triggers_extra_compaction_to_fit() {+fn history_recovery_respects_remaining_input_budget() {let history = vec![message(1,Sender::SelfUser,diff --git a/rust/crates/ensu/src/conversation/uuid_text.rs b/rust/crates/ensu/src/conversation/uuid_text.rsnew file mode 100644index 0000000000..9f3ae6f2fe--- /dev/null+++ b/rust/crates/ensu/src/conversation/uuid_text.rs@@ -0,0 +1,12 @@+use serde::{Deserialize, Deserializer, Serializer};+use uuid::Uuid;++pub(super) fn serialize<S: Serializer>(uuid: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {+ serializer.collect_str(uuid)+}++pub(super) fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Uuid, D::Error> {+ String::deserialize(deserializer)?+ .parse()+ .map_err(serde::de::Error::custom)+}diff --git a/rust/crates/ensu/src/db/chat/conversation.rs b/rust/crates/ensu/src/db/chat/conversation.rsindex 6b43456386..57b5c53bcc 100644--- a/rust/crates/ensu/src/db/chat/conversation.rs+++ b/rust/crates/ensu/src/db/chat/conversation.rs@@ -56,6 +56,11 @@ impl<B: Backend> ChatDb<B> {}rows.push(row);}+ if has_existing_parent(tx, &rows)? {+ return Err(Error::UnsupportedOperation(+ "Conversation history omits an existing ancestor".into(),+ ));+ }Ok((envelope, rows))})?;let messages = rows@@ -214,6 +219,16 @@ fn message_row<T: BackendTx>(tx: &T, id: Uuid) -> Result<Option<crate::db::Row>>)}+fn has_existing_parent<T: BackendTx>(tx: &T, rows: &[crate::db::Row]) -> Result<bool> {+ let Some(root) = rows.first() else {+ return Ok(false);+ };+ let Some(parent) = root.get_optional_string(2)? else {+ return Ok(false);+ };+ Ok(message_row(tx, Uuid::parse_str(&parent)?)?.is_some())+}+pub(super) fn snapshot_matches<T: BackendTx>(tx: &T,snapshot: &ConversationSnapshot,@@ -222,7 +237,9 @@ pub(super) fn snapshot_matches<T: BackendTx>(let Some(envelope) = session_envelope(tx, snapshot.session_uuid)? else {return Ok(false);};- if compare_envelope && envelope != snapshot.envelope {+ if (compare_envelope && envelope != snapshot.envelope)+ || has_existing_parent(tx, &snapshot.rows)?+ {return Ok(false);}for (message, expected) in snapshot.messages.iter().zip(&snapshot.rows) {@@ -233,8 +250,8 @@ pub(super) fn snapshot_matches<T: BackendTx>(Ok(true)}-#[cfg(all(test, feature = "sqlite"))]-#[expect(clippy::unwrap_used, reason = "Test fixture setup must succeed")]+#[cfg(test)]+#[cfg(feature = "sqlite")]mod tests {use super::*;fn seeded() -> (ChatDb<crate::db::SqliteBackend>, Uuid, Vec<Message>) {@@ -279,18 +296,51 @@ mod tests {}#[test]- fn encrypted_summary_survives_reopen_and_branch_selection() {+ fn migrated_orphan_history_supports_summary_restore_and_continuation() {let dir = tempfile::tempdir().unwrap();let file = dir.path().join("conversation.db");let db = ChatDb::open_sqlite_with_defaults(&file, vec![7; 32]).unwrap();let session = db.create_session("Memory").unwrap().uuid;+ let deleted = db+ .insert_message(session, "other", "Deleted parent", None, vec![])+ .unwrap();let question = db- .insert_message(session, "self", "Keep data offline", None, vec![])+ .insert_message(+ session,+ "self",+ "Keep data offline",+ Some(deleted.uuid),+ vec![],+ ).unwrap();let answer = db.insert_message(session, "other", "Understood", Some(question.uuid), vec![]).unwrap();let full = vec![question.clone(), answer];+ db.backend+ .execute_batch(+ "ALTER TABLE sessions DROP COLUMN conversation_state;+ ALTER TABLE sessions ADD COLUMN deleted_at INTEGER;+ ALTER TABLE messages ADD COLUMN deleted_at INTEGER;+ PRAGMA user_version = 4;",+ )+ .unwrap();+ db.backend+ .execute(+ "UPDATE messages SET deleted_at = 1 WHERE message_uuid = ?",+ &[Value::Text(deleted.uuid.to_string())],+ )+ .unwrap();+ drop(db);+ let db = ChatDb::open_sqlite_with_defaults(&file, vec![7; 32]).unwrap();+ assert!(db.get_message(deleted.uuid).unwrap().is_none());+ assert_eq!(+ db.get_message(question.uuid)+ .unwrap()+ .unwrap()+ .parent_message_uuid,+ Some(deleted.uuid)+ );let mut captured = snapshot(&db, session, &full);let memory = ConversationState::new(session, &full, "Keep data offline".into()).unwrap();assert!(@@ -302,6 +352,24 @@ mod tests {let db = ChatDb::open_sqlite_with_defaults(&file, vec![7; 32]).unwrap();let reopened = snapshot(&db, session, &full);assert_eq!(reopened.state(), Some(&memory));+ let user = db+ .insert_message(session, "self", "Continue", Some(full[1].uuid), vec![])+ .unwrap();+ let mut continued = full.clone();+ continued.push(user.clone());+ let captured = snapshot(&db, session, &continued);+ assert_eq!(captured.state(), Some(&memory));+ assert!(+ db.insert_message_guarded(+ session,+ "other",+ "Reply",+ Some(user.uuid),+ vec![],+ Some(&captured)+ )+ .is_ok()+ );let sibling = db.insert_message(session,@@ -315,6 +383,57 @@ mod tests {assert_eq!(snapshot(&db, session, &full).state(), Some(&memory));}+ #[test]+ fn snapshots_reject_omitted_ancestors_and_malformed_paths() {+ let (db, session, messages) = seeded();+ let other = db.create_session("Other").unwrap().uuid;+ let foreign = db+ .insert_message(other, "self", "Other root", None, vec![])+ .unwrap();+ for path in [+ vec![messages[1].uuid, messages[2].uuid],+ vec![messages[0].uuid, messages[2].uuid],+ vec![messages[0].uuid, messages[0].uuid],+ vec![messages[0].uuid, foreign.uuid],+ ] {+ assert!(db.conversation_snapshot(session, &path).is_err());+ }+ }++ #[test]+ fn restored_parent_invalidates_orphan_snapshots_and_guarded_writes() {+ let (db, session, mut messages) = seeded();+ let parent = messages.remove(0);+ let row = message_row(&db.backend, parent.uuid).unwrap().unwrap();+ db.backend+ .execute(+ "DELETE FROM messages WHERE message_uuid = ?",+ &[Value::Text(parent.uuid.to_string())],+ )+ .unwrap();+ let mut captured = snapshot(&db, session, &messages);+ assert!(db.conversation_snapshot_matches(&captured).unwrap());+ db.backend+ .execute(+ &format!("INSERT INTO messages ({MESSAGE_COLUMNS}) VALUES (?, ?, ?, ?, ?, ?, ?)"),+ &row,+ )+ .unwrap();+ assert!(!db.conversation_snapshot_matches(&captured).unwrap());+ assert!(!db.replace_conversation_state(&mut captured, None).unwrap());+ assert!(+ db.insert_message_guarded(+ session,+ "other",+ "Stale reply",+ Some(messages[1].uuid),+ vec![],+ Some(&captured)+ )+ .is_err()+ );+ }+#[test]fn encrypted_memory_survives_titles_appends_and_recent_edits() {let (db, session, messages) = seeded();diff --git a/rust/crates/ensu/src/llm/generate.rs b/rust/crates/ensu/src/llm/generate.rsindex 14565bed3a..f84fce3e16 100644--- a/rust/crates/ensu/src/llm/generate.rs+++ b/rust/crates/ensu/src/llm/generate.rs@@ -199,12 +199,6 @@ fn tokenize_text_prompt(model: &LlamaModel, prompt: &str) -> Result<Vec<LlamaTokOk(tokens)}-#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct TextPromptMeasurement {- pub prompt_tokens: usize,- pub context_size: u32,-}-fn drain_utf8(pending: &mut Vec<u8>) -> String {let mut output = String::new();loop {@@ -337,6 +331,7 @@ impl GenerationJob<'_> {check_cancelled(self.cancel_flag)?;let token = sampler.sample(ctx, logits_index);+ sampler.accept(token);check_cancelled(self.cancel_flag)?;if ctx.model.is_eog_token(token) {@@ -420,11 +415,7 @@ impl GenerationJob<'_> {}}-fn build_sampler(- model: &LlamaModel,- request: &SamplingParams,- prompt_tokens: &[LlamaToken],-) -> Result<LlamaSampler, Error> {+fn build_sampler(model: &LlamaModel, request: &SamplingParams) -> Result<LlamaSampler, Error> {let mut samplers = Vec::new();let mut repeat_penalty = request.repeat_penalty.unwrap_or(1.0);@@ -444,10 +435,12 @@ fn build_sampler(|| frequency_penalty != 0.0|| presence_penalty != 0.0{- let mut penalties =- LlamaSampler::penalties(-1, repeat_penalty, frequency_penalty, presence_penalty);- penalties.accept_many(prompt_tokens.iter());- samplers.push(penalties);+ samplers.push(LlamaSampler::penalties(+ -1,+ repeat_penalty,+ frequency_penalty,+ presence_penalty,+ ));}if let Some(grammar) = request.grammar.as_deref() {@@ -484,10 +477,7 @@ fn build_sampler(}impl Context {- pub fn measure_text_chat_prompt(- &self,- request: &ChatRequest,- ) -> Result<TextPromptMeasurement, Error> {+ pub fn measure_text_chat_prompt(&self, request: &ChatRequest) -> Result<usize, Error> {if request.image_paths.as_ref()@@ -504,10 +494,7 @@ impl Context {request.template_override.clone(),request.add_assistant.unwrap_or(true),)?;- Ok(TextPromptMeasurement {- prompt_tokens: tokenize_text_prompt(ctx.model, &prompt)?.len(),- context_size: ctx.n_ctx(),- })+ Ok(tokenize_text_prompt(ctx.model, &prompt)?.len())})}@@ -684,7 +671,8 @@ fn generate_chat_stream(token_offset = end;}- let mut sampler = build_sampler(ctx.model, &sampler_request, &prompt_tokens)?;+ let mut sampler = build_sampler(ctx.model, &sampler_request)?;+ sampler.accept_many(prompt_tokens.iter());let pos = prompt_tokens.len() as i32;job.run(ctx, &mut sampler, Some(cached_tokens), pos, logits_index)?;@@ -770,6 +758,7 @@ fn generate_chat_stream(})?;check_cancelled(&cancel_flag)?;+ let mut sampler = build_sampler(ctx.model, &sampler_request)?;let mut prompt_tokens = Vec::new();for index in 0..chunks.len() {if let Some(chunk) = chunks.get(index)@@ -778,7 +767,7 @@ fn generate_chat_stream(prompt_tokens.extend_from_slice(tokens);}}- let mut sampler = build_sampler(ctx.model, &sampler_request, &prompt_tokens)?;+ sampler.accept_many(prompt_tokens.iter());job.run(ctx, &mut sampler, None, n_past, -1)?;diff --git a/rust/crates/ensu/src/llm/history.rs b/rust/crates/ensu/src/llm/history.rsindex d8122ff777..8534baab1c 100644--- a/rust/crates/ensu/src/llm/history.rs+++ b/rust/crates/ensu/src/llm/history.rs@@ -38,16 +38,47 @@ mod tests {use super::*;#[test]- fn shared_hidden_text_fixtures() {- let fixtures: serde_json::Value =- serde_json::from_str(include_str!("../../tests/fixtures/history-text.json")).unwrap();- for case in fixtures.as_array().unwrap() {- assert_eq!(- strip_hidden_parts_text(case["input"].as_str().unwrap()),- case["expected"].as_str().unwrap(),- "{}",- case["name"]- );+ fn strips_hidden_parts_preserving_visible_text() {+ for (name, input, expected) in [+ ("plain_unicode", " माया owns SQLite. ", "माया owns SQLite."),+ (+ "closed_reasoning",+ "<think>Upload the data.</think>Keep data offline.",+ "Keep data offline.",+ ),+ (+ "interrupted_reasoning",+ "Visible fact.<think>Unfinished private reasoning",+ "Visible fact.",+ ),+ (+ "control_block",+ "<todo_list>Invent a purchase.</todo_list>No purchase approved.",+ "No purchase approved.",+ ),+ (+ "nested_blocks",+ "<think>outer<todo_list>inner</todo_list>hidden</think>Visible",+ "Visible",+ ),+ (+ "mismatched_close",+ "Before<think>hidden</todo_list>still hidden",+ "Before",+ ),+ ("nul", "\0Safe\0 text", "Safe text"),+ (+ "unknown_markup",+ "<example>Keep this</example>",+ "<example>Keep this</example>",+ ),+ (+ "multiple_blocks",+ "A<think>x</think>B<todo_list>y</todo_list>C",+ "ABC",+ ),+ ] {+ assert_eq!(strip_hidden_parts_text(input), expected, "{name}");}}}diff --git a/rust/crates/ensu/tests/fixtures/conversation-core-v1.json b/rust/crates/ensu/tests/fixtures/conversation-core-v1.jsondeleted file mode 100644index 4e6ed8140f..0000000000--- a/rust/crates/ensu/tests/fixtures/conversation-core-v1.json+++ /dev/null@@ -1,77 +0,0 @@-[- {- "name": "unicode_and_attachment",- "messages": [- {- "uuid": "00000000-0000-0000-0000-000000000001",- "session_uuid": "00000000-0000-0000-0000-000000000064",- "parent_message_uuid": null,- "sender": "self",- "text": "Café🙂",- "attachments": [- {- "id": "file",- "kind": "document",- "name": "notes.md",- "size": 12- }- ]- },- {- "uuid": "00000000-0000-0000-0000-000000000002",- "session_uuid": "00000000-0000-0000-0000-000000000064",- "parent_message_uuid": "00000000-0000-0000-0000-000000000001",- "sender": "other",- "text": "xxxxxxxxxxxxxxxxxxxx",- "attachments": []- }- ],- "fingerprint": "711773b8c54ac4b3b13bd9f2ff6da94956549422b10a66ca105c3f775f28740e",- "exchange_ends": [- 2- ]- },- {- "name": "leading_assistant_and_unanswered",- "messages": [- {- "uuid": "00000000-0000-0000-0000-000000000001",- "session_uuid": "00000000-0000-0000-0000-000000000064",- "parent_message_uuid": null,- "sender": "other",- "text": "Legacy prefix",- "attachments": []- },- {- "uuid": "00000000-0000-0000-0000-000000000002",- "session_uuid": "00000000-0000-0000-0000-000000000064",- "parent_message_uuid": "00000000-0000-0000-0000-000000000001",- "sender": "self",- "text": "Unanswered",- "attachments": []- },- {- "uuid": "00000000-0000-0000-0000-000000000003",- "session_uuid": "00000000-0000-0000-0000-000000000064",- "parent_message_uuid": "00000000-0000-0000-0000-000000000002",- "sender": "self",- "text": "Next",- "attachments": []- },- {- "uuid": "00000000-0000-0000-0000-000000000004",- "session_uuid": "00000000-0000-0000-0000-000000000064",- "parent_message_uuid": "00000000-0000-0000-0000-000000000003",- "sender": "other",- "text": "Partial",- "attachments": []- }- ],- "fingerprint": "08a9f5af99f0ef903ced70c105dcc0334bde0412eb9b26fbe5128159dc6e0b25",- "exchange_ends": [- 1,- 2,- 4- ]- }-]diff --git a/rust/crates/ensu/tests/fixtures/generation-budgets-v1.json b/rust/crates/ensu/tests/fixtures/generation-budgets-v1.jsondeleted file mode 100644index 5afe2e855c..0000000000--- a/rust/crates/ensu/tests/fixtures/generation-budgets-v1.json+++ /dev/null@@ -1,79 +0,0 @@-[- {- "name": "native Auto",- "context": 12000,- "configured": null,- "output": 2048,- "input": 9696- },- {- "name": "native small",- "context": 2048,- "configured": null,- "output": 512,- "input": 1280- },- {- "name": "Auto impossible",- "context": 300,- "configured": null,- "output": null,- "input": null- },- {- "name": "preserve long output",- "context": 12000,- "configured": 6000,- "output": 6000,- "input": 5744- },- {- "name": "reject oversized override",- "context": 2048,- "configured": 2048,- "output": null,- "input": null- },- {- "name": "zero output",- "context": 12000,- "configured": 0,- "output": null,- "input": null- },- {- "name": "too small context",- "context": 257,- "configured": 1,- "output": null,- "input": null- },- {- "name": "small explicit valid",- "context": 258,- "configured": 1,- "output": 1,- "input": 1- },- {- "name": "no prompt room",- "context": 4096,- "configured": 3840,- "output": null,- "input": null- },- {- "name": "last token room",- "context": 4096,- "configured": 3839,- "output": 3839,- "input": 1- },- {- "name": "context too large",- "context": 2147483648,- "configured": null,- "output": null,- "input": null- }-]diff --git a/rust/crates/ensu/tests/fixtures/history-lookup-v1.json b/rust/crates/ensu/tests/fixtures/history-lookup-v1.jsondeleted file mode 100644index cbac091bac..0000000000--- a/rust/crates/ensu/tests/fixtures/history-lookup-v1.json+++ /dev/null@@ -1,93 +0,0 @@-[- {- "name": "latest equally relevant exchanges win",- "history": [- ["self", "The reservation code is OLD-11."],- ["other", "First choice noted."],- ["self", "The reservation code is MIDDLE-22."],- ["other", "Second choice noted."],- ["self", "The reservation code is NEW-33."],- ["other", "Final choice noted."]- ],- "queries": [- { "query": "reservation code", "lookup": [5, 6, 3, 4], "required": null },- { "query": "What did you say earlier?", "lookup": [], "required": null },- {- "query": "Recall the reservation code",- "lookup": [5, 6, 3, 4],- "required": [5, 6]- },- {- "query": "याद reservation code",- "lookup": [5, 6, 3, 4],- "required": [5, 6]- },- {- "query": "यादगार reservation code",- "lookup": [5, 6, 3, 4],- "required": null- },- {- "query": "earlier-version reservation code",- "lookup": [5, 6, 3, 4],- "required": null- }- ]- },- {- "name": "exact identifiers beat newer generic matches",- "history": [- ["self", "The launch ticket is OPS-4812."],- ["other", "The original ticket is recorded."],- ["self", "A launch rehearsal is scheduled."],- ["other", "The rehearsal is recorded."],- ["self", "A launch checklist is ready."],- ["other", "The checklist is recorded."]- ],- "queries": [- { "query": "launch OPS-4812", "lookup": [1, 2, 5, 6], "required": null },- { "query": "OPS-481", "lookup": [], "required": null },- { "query": "Recall unknown penguins", "lookup": [], "required": null }- ]- },- {- "name": "quoted unicode phrases and identifiers preserve original text",- "history": [- ["self", "The café label is Café‑Bleu, with reference RÉF-823. हिन्दी🙂"],- ["other", "The spelling is confirmed."],- ["self", "Another café label is available."],- ["other", "A later alternative."]- ],- "queries": [- {- "query": "café \"Café‑Bleu\"",- "lookup": [1, 2, 3, 4],- "required": null- },- { "query": "réf-823", "lookup": [1, 2], "required": null },- { "query": "हिन्दी", "lookup": [1, 2], "required": null },- { "query": "हिन्द", "lookup": [], "required": null }- ]- },- {- "name": "message UUIDs select the original exchange",- "history": [- ["self", "The original destination is Kyoto."],- ["other", "The original destination is recorded."],- ["self", "The later destination is Lisbon."],- ["other", "The later destination is recorded."]- ],- "queries": [- {- "query": "message 00000000-0000-0000-0000-000000000001",- "lookup": [1, 2],- "required": [1, 2]- },- {- "query": "message 00000000-0000-0000-0000-000000000003-extra",- "lookup": [],- "required": null- }- ]- }-]diff --git a/rust/crates/ensu/tests/fixtures/history-text.json b/rust/crates/ensu/tests/fixtures/history-text.jsondeleted file mode 100644index 2cc9c56f9d..0000000000--- a/rust/crates/ensu/tests/fixtures/history-text.json+++ /dev/null@@ -1,47 +0,0 @@-[- {- "name": "plain_unicode",- "input": " \u092e\u093e\u092f\u093e owns SQLite. ",- "expected": "\u092e\u093e\u092f\u093e owns SQLite."- },- {- "name": "closed_reasoning",- "input": "<think>Upload the data.</think>Keep data offline.",- "expected": "Keep data offline."- },- {- "name": "interrupted_reasoning",- "input": "Visible fact.<think>Unfinished private reasoning",- "expected": "Visible fact."- },- {- "name": "control_block",- "input": "<todo_list>Invent a purchase.</todo_list>No purchase approved.",- "expected": "No purchase approved."- },- {- "name": "nested_blocks",- "input": "<think>outer<todo_list>inner</todo_list>hidden</think>Visible",- "expected": "Visible"- },- {- "name": "mismatched_close",- "input": "Before<think>hidden</todo_list>still hidden",- "expected": "Before"- },- {- "name": "nul",- "input": "\u0000Safe\u0000 text",- "expected": "Safe text"- },- {- "name": "unknown_markup",- "input": "<example>Keep this</example>",- "expected": "<example>Keep this</example>"- },- {- "name": "multiple_blocks",- "input": "A<think>x</think>B<todo_list>y</todo_list>C",- "expected": "ABC"- }-]diff --git a/web/apps/ensu/package.json b/web/apps/ensu/package.jsonindex b41db31b5d..a1ffd9f79d 100644--- a/web/apps/ensu/package.json+++ b/web/apps/ensu/package.json@@ -2,9 +2,6 @@"name": "ensu","version": "0.0.0","private": true,- "scripts": {- "test": "vitest --run"- },"dependencies": {"@hugeicons/core-free-icons": "1.2.1","@hugeicons/react": "1.1.6",@@ -26,7 +23,6 @@"remark-math": "6.0.0"},"devDependencies": {- "ente-build-config": "*",- "vitest": "4.1.11"+ "ente-build-config": "*"}}diff --git a/web/apps/ensu/src/components/chat/ChatDialogs.tsx b/web/apps/ensu/src/components/chat/ChatDialogs.tsxindex 5d6d3d6624..a8bb1677f0 100644--- a/web/apps/ensu/src/components/chat/ChatDialogs.tsx+++ b/web/apps/ensu/src/components/chat/ChatDialogs.tsx@@ -245,13 +245,14 @@ export const ChatDialogs = memo(}: ChatDialogsProps) => {const openExternalUrl = async (url: string) => {if (isTauriRuntime || detectTauriAppRuntime()) {- const opened = await import("@tauri-apps/plugin-opener")- .then(({ openUrl }) => openUrl(url))- .then(- () => true,- () => false,- );- if (opened) return;+ try {+ const { openUrl } =+ await import("@tauri-apps/plugin-opener");+ await openUrl(url);+ return;+ } catch {+ // Fall through to window.open.+ }}if (typeof window !== "undefined") {diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsxindex 405b1061e6..4e4ddc7842 100644--- a/web/apps/ensu/src/pages/chat.tsx+++ b/web/apps/ensu/src/pages/chat.tsx@@ -9,6 +9,7 @@ import { useFileInput } from "@/components/utils/use-file-input";import { useNotesCollections } from "@/hooks/use-notes-collections";import { handleManualAppUpdateCheck } from "@/services/app-update";import {+ buildConversationPath,buildSelectedPath,ROOT_SELECTION_KEY,STREAMING_SELECTION_KEY,@@ -305,6 +306,7 @@ const buildDocumentBlocks = (documents: DocumentAttachment[]) => {return documents.map((doc, index) => {const name = doc.name || `Document ${index + 1}`;+ // Remove null bytes which are invalid in C strings used by llama.cppconst content = doc.text.replace(/\0/g, "").trim();return `----- BEGIN DOCUMENT: ${name} -----\n${content}\n----- END DOCUMENT: ${name} -----`;})@@ -1070,9 +1072,12 @@ const Page: React.FC = () => {setMaxTokens(settings.maxTokens);};+ // Older builds persisted a model URL selection; llm_migrate_models+ // converts it to a model id once.const loadSettings = async () => {let raw = window.localStorage.getItem(MODEL_SETTINGS_STORAGE_KEY);if (!raw) {+ // Settings from even older builds live in IndexedDB.const kvRaw = await getKV(MODEL_SETTINGS_STORAGE_KEY).catch(() => undefined,);@@ -1930,7 +1935,15 @@ const Page: React.FC = () => {async (paths: string[]) => {if (!isTauriRuntime || paths.length === 0) return;const { remove } = await import("@tauri-apps/plugin-fs");- await Promise.allSettled(paths.map((path) => remove(path)));+ await Promise.all(+ paths.map(async (path) => {+ try {+ await remove(path);+ } catch {+ // Deleting the temporary file is best effort.+ }+ }),+ );},[isTauriRuntime],);@@ -2925,7 +2938,7 @@ const Page: React.FC = () => {const startGeneration = useCallback(async ({promptText,- parentMessageUuid,+ parentMessage,historyPath,stopAtMessageUuid,resetContext = false,@@ -2934,7 +2947,7 @@ const Page: React.FC = () => {mediaMarker,}: {promptText: string;- parentMessageUuid: string;+ parentMessage: ChatMessage;historyPath: ChatMessage[];stopAtMessageUuid?: string | null;resetContext?: boolean;@@ -2942,6 +2955,7 @@ const Page: React.FC = () => {imagePaths?: string[];mediaMarker?: string;}): Promise<void> => {+ const parentMessageUuid = parentMessage.messageUuid;const activeSessionId =sessionUuid ?? currentSessionIdRef.current ?? currentSessionId;if (!chatKey || !activeSessionId) return;@@ -3043,10 +3057,13 @@ const Page: React.FC = () => {...history,{ role: "user", content: promptText },];- const normalPromptTokenEstimate = normalMessages.reduce(- (total, message) => total + approxTokens(message.content),- 0,- );+ const countTokens = (messages: LlmMessage[]) =>+ messages.reduce(+ (total, message) =>+ total + approxTokens(message.content),+ 0,+ );+ const normalPromptTokenEstimate = countTokens(normalMessages);if (provider.getBackendKind() === "wasm" &&normalPromptTokenEstimate > inputBudget@@ -3122,6 +3139,9 @@ const Page: React.FC = () => {) {return;}+ if (name === "embedding_missing") {+ throw error;+ }log.warn("Knowledge retrieval failed; continuing without source context",error,@@ -3165,13 +3185,7 @@ const Page: React.FC = () => {normalMessages.length - 2,...history,);- if (- normalMessages.reduce(- (total, message) =>- total + approxTokens(message.content),- 0,- ) > inputBudget- ) {+ if (countTokens(normalMessages) > inputBudget) {throw new Error("Prompt exceeds the loaded model context window. Reduce history, lower max output, or increase context length.",);@@ -3188,28 +3202,19 @@ const Page: React.FC = () => {},...normalMessages.slice(1),];- if (- candidateMessages.reduce(- (total, message) =>- total + approxTokens(message.content),- 0,- ) <= inputBudget- ) {+ if (countTokens(candidateMessages) <= inputBudget) {messages = candidateMessages;activeSources = knowledgeContext.sources;}}if (useConversationMemory) {- const selectedPath = slicePathUntil(- historyPath,- stopAtMessageUuid,- ).map((message) => message.messageUuid);- if (selectedPath.at(-1) !== parentMessageUuid)- selectedPath.push(parentMessageUuid);const prepared = await prepareDesktopConversation({sessionUuid: activeSessionId,- path: selectedPath,+ path: buildConversationPath(+ allMessages,+ parentMessage,+ ),system: normalSystemPrompt,current: promptText,historyQuery: knowledgeQuery,@@ -3425,7 +3430,7 @@ const Page: React.FC = () => {ensureProvider,getModelSettings,buildHistory,- slicePathUntil,+ allMessages,branchSelections,updateBranchSelectionState,appendMessageToState,@@ -3483,7 +3488,7 @@ const Page: React.FC = () => {const historyPath = slicePathUntil(messageState.path, parentUuid);await startGeneration({promptText: parentMessage.text,- parentMessageUuid: parentUuid,+ parentMessage,historyPath,stopAtMessageUuid: parentUuid,resetContext: true,@@ -4427,7 +4432,7 @@ const Page: React.FC = () => {await startGeneration({promptText,- parentMessageUuid: newUserMessage.messageUuid,+ parentMessage: newUserMessage,historyPath,sessionUuid: activeSessionId,imagePaths: inferenceImagePaths,@@ -4462,7 +4467,7 @@ const Page: React.FC = () => {await startGeneration({promptText,- parentMessageUuid: userMessage.messageUuid,+ parentMessage: userMessage,historyPath: basePath,sessionUuid: activeSessionId,imagePaths: inferenceImagePaths,diff --git a/web/apps/ensu/src/services/chat/branching.ts b/web/apps/ensu/src/services/chat/branching.tsindex 89b26c0916..07e289b52c 100644--- a/web/apps/ensu/src/services/chat/branching.ts+++ b/web/apps/ensu/src/services/chat/branching.ts@@ -24,6 +24,26 @@ export interface PathBuildResult {const DEDUPE_WINDOW_US = 2_000_000;+export const buildConversationPath = (+ messages: ChatMessage[],+ target: ChatMessage,+): string[] => {+ const byId = new Map(+ messages.map((message) => [message.messageUuid, message]),+ );+ const path: string[] = [];+ const visited = new Set<string>();+ let current: ChatMessage | undefined = target;+ while (current && !visited.has(current.messageUuid)) {+ visited.add(current.messageUuid);+ path.push(current.messageUuid);+ current = current.parentMessageUuid+ ? byId.get(current.parentMessageUuid)+ : undefined;+ }+ return path.reverse();+};+export const buildSelectedPath = (messages: ChatMessage[],selections: BranchSelections,diff --git a/web/apps/ensu/src/services/llm/conversation.ts b/web/apps/ensu/src/services/llm/conversation.tsindex adb22923fc..8a7cf48d30 100644--- a/web/apps/ensu/src/services/llm/conversation.ts+++ b/web/apps/ensu/src/services/llm/conversation.ts@@ -14,22 +14,6 @@ export interface PreparedReply {saveAnswer: (text: string) => Promise<ChatMessage>;}-const createPreparedReply = (- messages: LlmMessage[],- answer: PreparedAnswer,- provider: LlmProvider,-): PreparedReply => {- let saved: Promise<ChatMessage> | undefined;- return {- generateChatStream: (request, onEvent) =>- provider.generateChatStream(- { ...request, messages, preparationToken: answer.token },- onEvent,- ),- saveAnswer: (text) => (saved ??= addPreparedAnswer(answer, text)),- };-};-interface PreparedConversation {preparationToken: string;messages: LlmMessage[];@@ -46,7 +30,7 @@ export const prepareDesktopConversation = async (groundingCandidates?: unknown;maxTokens: number;},- provider: LlmProvider,+ provider: Pick<LlmProvider, "generateChatStream">,callbacks: { isCurrent: () => boolean; onProgress: () => void },): Promise<PreparedReply | undefined> => {const preparationToken = crypto.randomUUID();@@ -77,16 +61,25 @@ export const prepareDesktopConversation = async (},);if (!callbacks.isCurrent()) return;- return createPreparedReply(- result.messages,- {- token: result.preparationToken,- sessionUuid: input.sessionUuid,- parentMessageUuid: input.path.at(-1)!,- sources: result.groundedContext?.sources ?? [],- },- provider,- );+ const answer: PreparedAnswer = {+ token: result.preparationToken,+ sessionUuid: input.sessionUuid,+ parentMessageUuid: input.path.at(-1)!,+ sources: result.groundedContext?.sources ?? [],+ };+ let saved: Promise<ChatMessage> | undefined;+ return {+ generateChatStream: (request, onEvent) =>+ provider.generateChatStream(+ {+ ...request,+ messages: result.messages,+ preparationToken: answer.token,+ },+ onEvent,+ ),+ saveAnswer: (text) => (saved ??= addPreparedAnswer(answer, text)),+ };} finally {unlisten();}diff --git a/web/apps/ensu/src/services/llm/inference.ts b/web/apps/ensu/src/services/llm/inference.tsindex 8e2d452a2f..59193d92ff 100644--- a/web/apps/ensu/src/services/llm/inference.ts+++ b/web/apps/ensu/src/services/llm/inference.ts@@ -481,7 +481,7 @@ class WasmInference implements InferenceBackend {stopTokens.push(first);}} catch {- continue;+ // Skip stop sequences that fail to tokenize.}}diff --git a/web/apps/ensu/src/services/llm/provider.ts b/web/apps/ensu/src/services/llm/provider.tsindex f72fe45011..ba66db35f1 100644--- a/web/apps/ensu/src/services/llm/provider.ts+++ b/web/apps/ensu/src/services/llm/provider.ts@@ -15,6 +15,7 @@ import type {ModelSettings,} from "./types";+// These fallback values must stay in sync with rust/crates/ensu/src/config.rs.export const DEFAULT_MODEL: ModelInfo = {id: "lfm-vl-1.6b",name: "LFM 2.5 VL 1.6B (Q4_0)",@@ -347,7 +348,12 @@ export class LlmProvider {}return inFlight.promise;}- await this.ensureInFlight.promise.catch(() => undefined);+ try {+ await this.ensureInFlight.promise;+ } catch {+ // Wait only for settlement; the failure belongs to the+ // original caller.+ }}const ensurePromise = this.withExclusiveModelOperation(async () => {diff --git a/web/apps/ensu/tests/conversation.test.ts b/web/apps/ensu/tests/conversation.test.tsdeleted file mode 100644index 15856d26a2..0000000000--- a/web/apps/ensu/tests/conversation.test.ts+++ /dev/null@@ -1,103 +0,0 @@-import { beforeEach, describe, expect, it, vi } from "vitest";-import type { GroundedSource } from "../src/services/knowledge";-import { prepareDesktopConversation } from "../src/services/llm/conversation";-import type { LlmProvider } from "../src/services/llm/provider";--const { invoke, listen, unlisten, addPreparedAnswer } = vi.hoisted(() => ({- invoke: vi.fn(),- listen: vi.fn(),- unlisten: vi.fn(),- addPreparedAnswer: vi.fn(),-}));-vi.mock("@tauri-apps/api/core", () => ({ invoke }));-vi.mock("@tauri-apps/api/event", () => ({ listen }));-vi.mock("../src/services/chat/store", () => ({ addPreparedAnswer }));--const generateChatStream = vi.fn();-const provider = { generateChatStream } as unknown as LlmProvider;--const input = {- sessionUuid: "session",- path: ["user"],- system: "system",- current: "question",- maxTokens: 1024,-};-const ready = {- preparationToken: "token",- messages: [{ role: "user", content: "question" }],-};-const callbacks = () => ({ isCurrent: vi.fn(() => true), onProgress: vi.fn() });--describe("desktop conversation preparation", () => {- beforeEach(() => {- vi.clearAllMocks();- listen.mockResolvedValue(unlisten);- });-- it("binds a grounded preparation to generation and saves a stopped answer once", async () => {- const sources: GroundedSource[] = [- {- type: "localNote",- reference: {- collectionId: "collection",- documentId: "trip.md",- indexedRevision: "revision",- title: "Trip",- },- },- ];- invoke- .mockResolvedValueOnce(7)- .mockResolvedValueOnce({- ...ready,- groundedContext: { text: "Selected passage", sources },- });- const handlers = callbacks();- const groundedInput = {- ...input,- current: "Fresh source text\n\nquestion",- historyQuery: "question",- groundingCandidates: { searched: [], maxUtf8Bytes: 6000 },- };- const prepared = await prepareDesktopConversation(- groundedInput,- provider,- handlers,- );- await prepared!.generateChatStream({ messages: [], maxTokens: 100 });- expect(generateChatStream).toHaveBeenCalledExactlyOnceWith(- {- messages: ready.messages,- preparationToken: ready.preparationToken,- maxTokens: 100,- },- undefined,- );- const first = invoke.mock.calls[1]![1] as { input: object };- expect(first).toMatchObject({- input: {- ...groundedInput,- expectedUserText: groundedInput.current,- cancellationEpoch: 7,- },- });- expect(invoke).toHaveBeenCalledTimes(2);- expect(unlisten).toHaveBeenCalledOnce();- handlers.isCurrent.mockReturnValue(false);- const answer = { messageUuid: "answer" };- addPreparedAnswer.mockResolvedValueOnce(answer);- const stopped = prepared!.saveAnswer("Partial answer");- expect(prepared!.saveAnswer("Completed answer")).toBe(stopped);- expect(await stopped).toBe(answer);- expect(addPreparedAnswer).toHaveBeenCalledExactlyOnceWith(- {- token: ready.preparationToken,- sessionUuid: input.sessionUuid,- parentMessageUuid: input.path[0],- sources,- },- "Partial answer",- );- });-});diff --git a/web/apps/ensu/tests/generation-budget.test.ts b/web/apps/ensu/tests/generation-budget.test.tsdeleted file mode 100644index 8fe6848f32..0000000000--- a/web/apps/ensu/tests/generation-budget.test.ts+++ /dev/null@@ -1,34 +0,0 @@-import { describe, expect, it } from "vitest";-import fixtures from "../../../../rust/crates/ensu/tests/fixtures/generation-budgets-v1.json";-import { resolveGenerationBudget } from "../src/services/llm/budget";--describe("shared generation budgets", () => {- for (const fixture of fixtures) {- it(fixture.name, () => {- const resolve = () =>- resolveGenerationBudget(- fixture.context,- fixture.configured ?? undefined,- );- if (fixture.output === null) expect(resolve).toThrow();- else- expect(resolve()).toEqual({- contextSize: fixture.context,- maxTokens: fixture.output,- inputBudget: fixture.input,- });- });- }- it("rejects malformed JavaScript settings instead of using Auto", () => {- for (const value of [- -1,- 1.5,- NaN,- Infinity,- Number.MAX_SAFE_INTEGER + 1,- ]) {- expect(() => resolveGenerationBudget(value)).toThrow();- expect(() => resolveGenerationBudget(12000, value)).toThrow();- }- });-});diff --git a/web/apps/ensu/tests/history-text.test.ts b/web/apps/ensu/tests/history-text.test.tsdeleted file mode 100644index b7da2f614b..0000000000--- a/web/apps/ensu/tests/history-text.test.ts+++ /dev/null@@ -1,11 +0,0 @@-import { describe, expect, it } from "vitest";-import fixtures from "../../../../rust/crates/ensu/tests/fixtures/history-text.json";-import { stripHiddenPartsText } from "../src/services/llm/history-text";--describe("shared assistant history cleanup", () => {- for (const fixture of fixtures) {- it(fixture.name, () => {- expect(stripHiddenPartsText(fixture.input)).toBe(fixture.expected);- });- }-});diff --git a/web/apps/ensu/tests/provider-budget.test.ts b/web/apps/ensu/tests/provider-budget.test.tsdeleted file mode 100644index 65ee041d89..0000000000--- a/web/apps/ensu/tests/provider-budget.test.ts+++ /dev/null@@ -1,124 +0,0 @@-import { beforeEach, describe, expect, it, vi } from "vitest";-import { LlmProvider } from "../src/services/llm/provider";--const { backend, invoke } = vi.hoisted(() => ({- backend: {- kind: "tauri",- initBackend: vi.fn(),- createContext: vi.fn(),- loadModel: vi.fn(),- freeContext: vi.fn(),- freeModel: vi.fn(),- generateChatStream: vi.fn(),- },- invoke: vi.fn(),-}));-vi.mock("../src/services/llm/inference", () => ({- createInferenceBackend: () => backend,-}));-vi.mock("ente-base/log", () => ({ default: { info: vi.fn(), warn: vi.fn() } }));-vi.mock("@tauri-apps/api/core", () => ({ invoke }));--describe("provider loaded-context budgets", () => {- beforeEach(() => {- vi.resetAllMocks();- backend.kind = "tauri";- backend.createContext.mockResolvedValue(12032);- invoke.mockImplementation((command: string) => {- if (command === "system_info") return Promise.resolve({});- if (command === "desktop_model_policy")- return Promise.reject(new Error("use fallback catalog"));- if (command === "llm_model_status")- return Promise.resolve({- downloaded: true,- modelPath: "/synthetic/model.gguf",- });- return Promise.resolve(1);- });- });-- it("uses the rounded loaded size without reloading on every turn", async () => {- const provider = new LlmProvider();- expect(provider.resolveRuntimeSettings({})).toMatchObject({- contextSize: 12000,- maxTokens: 2048,- });- await provider.ensureModelReady({});- expect(provider.resolveRuntimeSettings({})).toMatchObject({- contextSize: 12032,- maxTokens: 2048,- inputBudget: 9728,- });- await provider.ensureModelReady({});- expect(backend.createContext).toHaveBeenCalledOnce();- await provider.generateChatStream({ messages: [] });- expect(backend.generateChatStream).toHaveBeenCalledWith(- { messages: [], maxTokens: 2048 },- undefined,- );- });-- it("recomputes Auto on a smaller loaded context and rejects an oversized saved override", async () => {- backend.createContext.mockResolvedValue(2048);- const provider = new LlmProvider();- await provider.ensureModelReady({});- expect(provider.resolveRuntimeSettings({})).toMatchObject({- contextSize: 2048,- maxTokens: 512,- inputBudget: 1280,- });- expect(- provider.resolveRuntimeSettings({ maxTokens: 1024 }).maxTokens,- ).toBe(1024);- expect(() =>- provider.resolveRuntimeSettings({ maxTokens: 6000 }),- ).toThrow(/no room/);- await expect(- provider.generateChatStream({ messages: [], maxTokens: 6000 }),- ).rejects.toThrow(/no room/);- expect(backend.generateChatStream).not.toHaveBeenCalled();- expect(- provider.resolveRuntimeSettings({- contextLength: 8192,- maxTokens: 6000,- }).maxTokens,- ).toBe(6000);- });-- it("restores capacity after no-op retrieval but forgets it when native model state changes", async () => {- const provider = new LlmProvider();- await provider.ensureModelReady({});- await provider.withKnowledgeRetrieval(- () => Promise.resolve(undefined),- () => true,- );- expect(provider.resolveRuntimeSettings({}).contextSize).toBe(12032);- invoke- .mockResolvedValueOnce(1)- .mockResolvedValueOnce(1)- .mockResolvedValueOnce(2);- await provider.withKnowledgeRetrieval(- () => Promise.resolve(undefined),- () => true,- );- expect(provider.resolveRuntimeSettings({}).contextSize).toBe(12000);- backend.createContext.mockResolvedValue(4096);- await provider.ensureModelReady({});- expect(provider.resolveRuntimeSettings({}).maxTokens).toBe(1024);- });-- it("updates capacity on reset and discards it after a failed context load", async () => {- const provider = new LlmProvider();- await provider.ensureModelReady({});- backend.createContext.mockResolvedValueOnce(2048);- await provider.resetContext(12000);- expect(provider.resolveRuntimeSettings({}).maxTokens).toBe(512);- backend.createContext.mockRejectedValueOnce(- new Error("allocation failed"),- );- await expect(provider.resetContext(12000)).rejects.toThrow(- "allocation failed",- );- expect(provider.resolveRuntimeSettings({}).contextSize).toBe(12000);- });-});diff --git a/web/apps/ensu/tsconfig.json b/web/apps/ensu/tsconfig.jsonindex 6a2a667238..2ff1f734c6 100644--- a/web/apps/ensu/tsconfig.json+++ b/web/apps/ensu/tsconfig.json@@ -9,7 +9,6 @@},"include": ["src",- "tests","next-env.d.ts","next.config.mjs","../../packages/base/global-electron.d.ts",diff --git a/web/package-lock.json b/web/package-lock.jsonindex 0f729cde2e..48e1689ec8 100644--- a/web/package-lock.json+++ b/web/package-lock.json@@ -190,8 +190,7 @@"remark-math": "6.0.0"},"devDependencies": {- "ente-build-config": "*",- "vitest": "4.1.11"+ "ente-build-config": "*"}},"apps/ensu/node_modules/katex": {diff --git a/web/package.json b/web/package.jsonindex 350016cbf6..3a0f9309ea 100644--- a/web/package.json+++ b/web/package.json@@ -52,7 +52,7 @@"dev:twoof3": "npm exec --workspace twoof3 -- next dev --webpack -p 3012","lint": "node scripts/lint.mjs","lint:fix": "node scripts/lint.mjs --fix",- "test": "npm run test --workspace ensu && npm run test --workspace ente-wasm-tests && npm run test --workspace ente-prelogin-wasm && npm run test --workspace ente-locker-wasm && npm run test --workspace ente-paste-wasm && npm run test --workspace ente-contacts && npm run test --workspace locker && npm run test --workspace paste && npm run test --workspace photos && npm run test --workspace space"+ "test": "npm run test --workspace ente-wasm-tests && npm run test --workspace ente-prelogin-wasm && npm run test --workspace ente-locker-wasm && npm run test --workspace ente-paste-wasm && npm run test --workspace ente-contacts && npm run test --workspace locker && npm run test --workspace paste && npm run test --workspace photos && npm run test --workspace space"},"overrides": {"@types/react": "19.2.17",