💚 End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[ensu] Reuse verified passages in historical follow-ups
setalp04d559ee22c8
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/AppStore.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/AppStore.ktindex 5646f47e57..128426d052 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/AppStore.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/AppStore.kt@@ -193,7 +193,8 @@ class AppStore(modelSettingsActions.cancelModelDownload()}- fun retryAssistantMessage(messageId: String) = chatActions.retryAssistantMessage(messageId)+ fun retryAssistantMessage(messageId: String, searchAsWritten: Boolean = false) =+ chatActions.retryAssistantMessage(messageId, searchAsWritten)fun sendMessage() = chatActions.sendMessage()diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeNavigation.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeNavigation.ktindex 8a15c355a9..a0efe20cc9 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeNavigation.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeNavigation.kt@@ -15,6 +15,7 @@ import androidx.compose.material3.DrawerStateimport androidx.compose.material3.DrawerValueimport androidx.compose.material3.Scaffoldimport androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsStateimport androidx.compose.runtime.getValueimport androidx.compose.ui.Modifierimport androidx.navigation.NavBackStackEntry@@ -54,6 +55,7 @@ internal fun HomeNavigation(onAttachmentSelected: (AttachmentType) -> Unit,onOpenAttachment: (Attachment) -> Unit,) {+ val notesState by store.notesStore.state.collectAsState()Scaffold(containerColor = EnsuColor.backgroundBase(),contentWindowInsets = WindowInsets(0, 0, 0, 0),@@ -123,6 +125,15 @@ internal fun HomeNavigation(onRemoveAttachment = store::removeAttachment,onEditMessage = { message -> store.beginEditing(message.id) },onRetryMessage = { message -> store.retryAssistantMessage(message.id) },+ onSearchMessage =+ if (+ appState.knowledge.enabledReadyDatasets.isNotEmpty() ||+ notesState.collections.isNotEmpty()+ ) {+ { message ->+ store.retryAssistantMessage(message.id, searchAsWritten = true)+ }+ } else null,onCancelEdit = store::cancelEditing,onBranchChange = store::updateBranchSelection,onOpenAttachment = onOpenAttachment,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 deba524767..671bc38395 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@@ -107,6 +107,7 @@ internal fun MessageList(branchSelections: Map<String, Int>,onEditMessage: (ChatMessage) -> Unit,onRetryMessage: (ChatMessage) -> Unit,+ onSearchMessage: ((ChatMessage) -> Unit)?,onBranchChange: (String, Int) -> Unit,onOpenAttachment: (Attachment) -> Unit,onStartDownload: (Boolean) -> Unit,@@ -319,6 +320,7 @@ internal fun MessageList(isLastMessage = message == messages.last(),branchSelections = branchSelections,onRetry = { onRetryMessage(message) },+ onSearch = onSearchMessage?.let { search -> { search(message) } },onBranchChange = onBranchChange,showsMetadata = true,)@@ -639,6 +641,7 @@ private fun AssistantMessageBubble(isLastMessage: Boolean,branchSelections: Map<String, Int>,onRetry: () -> Unit,+ onSearch: (() -> Unit)?,onBranchChange: (String, Int) -> Unit,showsMetadata: Boolean,) {@@ -668,6 +671,14 @@ private fun AssistantMessageBubble(onRetry()})+ onSearch?.let { search ->+ add(+ MessageAction("Search as written", HugeIcons.Search01Icon) {+ haptic.perform(HapticFeedbackType.TextHandleMove)+ search()+ }+ )+ }}}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatRepository.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatRepository.ktindex e186c9f545..c76fb2e984 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatRepository.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatRepository.kt@@ -136,6 +136,11 @@ class ChatRepository(internal class PreparedConversation(val owner: EnsuDb, val work: ConversationPreparation)+ internal fun startFollowup(sessionId: String, path: List<String>, userText: String) =+ withDbRecovery {+ db.startConversationFollowup(sessionId, path, userText, userText)+ }+internal fun prepareConversation(context: LlmContext,request: ConversationRequest,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 4ac3e26490..d6d5f90f72 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,15 +4,19 @@ 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.ConversationFollowupimport io.ente.ensu.bindings.ConversationProgressCallbackimport io.ente.ensu.bindings.ConversationRequestimport io.ente.ensu.bindings.DbExceptionimport io.ente.ensu.bindings.GroundedExcerptimport io.ente.ensu.bindings.GroundedSource+import io.ente.ensu.bindings.IncludedPassageimport io.ente.ensu.bindings.LlmException+import io.ente.ensu.bindings.PassageLocatorimport io.ente.ensu.bindings.buildGroundedPromptContextimport io.ente.ensu.bindings.cleanAssistantTextimport io.ente.ensu.bindings.finalizeGroundedAssistantText+import io.ente.ensu.bindings.parseGroundedAssistantTextimport io.ente.ensu.bindings.selectMixedGroundingCandidatesimport io.ente.ensu.device.isChatSupportedimport io.ente.ensu.knowledge.KnowledgeProvider@@ -68,6 +72,7 @@ internal class ChatStoreActions(private var sessionSummaryJob: Job? = null@Volatile private var stopRequested = false@Volatile private var activePreparation: ConversationPreparationControl? = null+ @Volatile private var activeFollowup: ConversationFollowup? = nullprivate var streamingParentId: String? = nullprivate var activeGenerationToken = 0Lprivate var pendingOverflow: PendingOverflow? = null@@ -307,12 +312,13 @@ internal class ChatStoreActions(fun stopGeneration() {if (state.value.chat.preparationStatus != null) activePreparation?.cancel()+ activeFollowup?.cancel()stopRequested = truellmProvider.stopGeneration()generationJob?.cancel()}- fun retryAssistantMessage(messageId: String) {+ fun retryAssistantMessage(messageId: String, searchAsWritten: Boolean = false) {val priorGeneration = generationJobval priorSummary = sessionSummaryJobif (state.value.chat.isGenerating) {@@ -341,7 +347,7 @@ internal class ChatStoreActions(priorGeneration?.join()priorSummary?.join()llmProvider.resetContext()- startGeneration(sessionId, parent)+ startGeneration(sessionId, parent, searchAsWritten)}}@@ -477,13 +483,18 @@ internal class ChatStoreActions(}}- private fun startGeneration(sessionId: String, userMessage: ChatMessage) {+ private fun startGeneration(+ sessionId: String,+ userMessage: ChatMessage,+ searchAsWritten: Boolean = false,+ ) {val scope = scope ?: returnif (!state.value.chat.deviceCapability.isChatSupported()) returnval prompt = buildPrompt(userMessage.text, userMessage.attachments)val priorGeneration = generationJobval priorSummary = sessionSummaryJobactivePreparation?.cancel()+ activeFollowup?.cancel()priorGeneration?.cancel()priorSummary?.cancel()llmProvider.stopGeneration()@@ -514,6 +525,7 @@ internal class ChatStoreActions(rebuildChatState(sessionId)val notesScope = notesStore.suspendMaintenance()+ var followup: ConversationFollowup? = nullval activeJob = scope.launch {notesStore.awaitMaintenance()priorGeneration?.join()@@ -530,6 +542,55 @@ internal class ChatStoreActions(val progressTracker = DownloadProgressTracker()var embeddingAssetInvalid = falseval enabledDatasets = state.value.knowledge.enabledReadyDatasets+ try {+ if (prompt.imageFiles.isEmpty()) {+ val history = buildSelectedPath(sessionId).takeWhile { it.id != userMessage.id }+ val hasCurrentSources =+ searchAsWritten ||+ enabledDatasets.isNotEmpty() ||+ notesStore.state.value.collections.isNotEmpty()+ val turn =+ withContext(Dispatchers.IO) {+ if (+ hasCurrentSources ||+ history.any {+ it.author == MessageAuthor.Assistant &&+ parseGroundedAssistantText(it.text).sources.isNotEmpty()+ }+ ) {+ chatRepository.startFollowup(+ sessionId,+ history.map { it.id } + userMessage.id,+ userMessage.text,+ )+ } else null+ }+ if (!isActive() || stopRequested) {+ turn?.cancel()+ return@launch+ }+ followup = turn+ activeFollowup = turn+ if (turn != null)+ state.update {+ it.copy(chat = it.chat.copy(preparationStatus = "Finding sources"))+ }+ }+ } catch (error: kotlinx.coroutines.CancellationException) {+ throw error+ } catch (_: ConversationException.Cancelled) {+ return@launch+ } catch (error: Throwable) {+ if (!isActive() || stopRequested) return@launch+ logRepository.log(+ LogLevel.Warning,+ "Unable to prepare source search",+ details = error.message,+ tag = "Chat",+ throwable = error,+ )+ if (showFollowupError(error, userMessage.id)) return@launch+ }val retrievalQuery = userMessage.text.trim()val knowledgeHits =if (@@ -580,6 +641,40 @@ internal class ChatStoreActions(emptyList()}+ val reloaded =+ try {+ followup+ ?.let { turn ->+ val resolution =+ withContext(Dispatchers.IO) {+ if (searchAsWritten) turn.searchAsWritten()+ else turn.searchWithHistory(knowledgeHits)+ }+ if (!isActive() || stopRequested) return@launch+ resolution.referencedPassages.mapNotNull { reloadPassage(it) }+ }+ .orEmpty()+ } catch (error: kotlinx.coroutines.CancellationException) {+ throw error+ } catch (_: ConversationException.Cancelled) {+ return@launch+ } catch (error: Throwable) {+ if (!isActive() || stopRequested) return@launch+ logRepository.log(+ LogLevel.Warning,+ "Unable to resolve earlier sources",+ details = error.message,+ tag = "Chat",+ throwable = error,+ )+ if (showFollowupError(error, userMessage.id)) return@launch+ activeFollowup = null+ followup?.cancel()+ followup?.destroy()+ followup = null+ emptyList()+ }+if (!isActive()) return@launchvar modelReady = falsevar loadFailure: Throwable? = null@@ -654,6 +749,8 @@ internal class ChatStoreActions(selection,modelSettingsActions.resolveTemperature(settings),knowledgeHits,+ followup,+ reloaded,{ isActive() && state.value.modelSettings == settings },)if (embeddingAssetInvalid) modelSettingsActions.refreshModelDownloadInfo()@@ -830,11 +927,60 @@ internal class ChatStoreActions(}generationJob = activeJobactiveJob.invokeOnCompletion {+ if (activeFollowup === followup) activeFollowup = null+ followup?.cancel()+ followup?.destroy()notesScope.close()scope.launch { settleGenerationIfActive(generationToken, sessionId) }}}+ private fun showFollowupError(error: Throwable, messageId: String): Boolean {+ val message =+ when (error) {+ is ConversationException.Stale -> "Conversation changed. Retry the message."+ is DbException -> "Conversation could not be read. Retry the message."+ else -> return false+ }+ state.update {+ it.copy(+ chat =+ it.chat.copy(+ transientAssistantError = message,+ transientAssistantParentId = messageId,+ )+ )+ }+ return true+ }++ private suspend fun reloadPassage(reference: IncludedPassage): GroundedExcerpt? {+ return try {+ when (val locator = reference.locator) {+ is PassageLocator.EnsuPack ->+ knowledgeProvider.reload(locator, state.value.knowledge.enabledReadyDatasets)+ is PassageLocator.LocalNote -> notesStore.reload(locator)+ }+ } catch (error: kotlinx.coroutines.CancellationException) {+ throw error+ } catch (error: ConversationException.Cancelled) {+ throw error+ } catch (error: ConversationException.Stale) {+ throw error+ } catch (error: DbException) {+ throw error+ } catch (error: Exception) {+ logRepository.log(+ LogLevel.Warning,+ "Unable to reload source",+ details = error.message,+ tag = "Chat",+ throwable = error,+ )+ null+ }+ }+private suspend fun generateTextConversation(sessionId: String,userMessage: ChatMessage,@@ -842,6 +988,8 @@ internal class ChatStoreActions(selection: LlmModelSelection,temperature: Float,searched: List<GroundedExcerpt>,+ followup: ConversationFollowup?,+ reloaded: List<GroundedExcerpt>,isActive: () -> Boolean,) {val buffer = StringBuilder()@@ -863,6 +1011,8 @@ internal class ChatStoreActions(historyQuery = userMessage.text,maxTokens = null,searched = searched,+ followup = followup,+ reloaded = reloaded,),)val preparationControl = ConversationPreparationControl(preparation.work)@@ -1649,6 +1799,7 @@ internal class ChatStoreActions(private fun cancelGeneration() {clearTransientAssistantError()activePreparation?.cancel()+ activeFollowup?.cancel()generationJob?.cancel()llmProvider.stopGeneration()invalidateGenerationToken()diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.ktindex b8a9e4b0d4..fb48753507 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.kt@@ -66,6 +66,7 @@ fun ChatView(onRemoveAttachment: (Attachment) -> Unit,onEditMessage: (ChatMessage) -> Unit,onRetryMessage: (ChatMessage) -> Unit,+ onSearchMessage: ((ChatMessage) -> Unit)?,onCancelEdit: () -> Unit,onBranchChange: (String, Int) -> Unit,onOpenAttachment: (Attachment) -> Unit,@@ -236,6 +237,7 @@ fun ChatView(branchSelections = chatState.branchSelections,onEditMessage = onEditMessage,onRetryMessage = onRetryMessage,+ onSearchMessage = onSearchMessage,onBranchChange = onBranchChange,onOpenAttachment = onOpenAttachment,onStartDownload = onStartDownload,diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/knowledge/KnowledgeProvider.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/knowledge/KnowledgeProvider.ktindex de2c49dafd..1e2b955ea9 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/knowledge/KnowledgeProvider.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/knowledge/KnowledgeProvider.kt@@ -4,8 +4,10 @@ import io.ente.ensu.assets.AssetStoreimport io.ente.ensu.bindings.KnowledgeDatasetConfigimport io.ente.ensu.bindings.KnowledgePromptHitimport io.ente.ensu.bindings.KnowledgeReconciliation+import io.ente.ensu.bindings.PassageLocatorimport io.ente.ensu.bindings.RetrievalIndeximport io.ente.ensu.bindings.knowledgePackAsset+import io.ente.ensu.bindings.selectMixedGroundingCandidatesimport kotlinx.coroutines.CancellationExceptionimport kotlinx.coroutines.Deferredimport kotlinx.coroutines.Dispatchers@@ -157,6 +159,22 @@ class KnowledgeProvider(private val assetStore: AssetStore) {}}+ suspend fun reload(locator: PassageLocator.EnsuPack, datasets: List<KnowledgeDatasetConfig>) =+ withContext(Dispatchers.IO) {+ indexGate.withLock {+ if (datasets.none { it.stableId == locator.datasetId }) return@withLock null+ val hit =+ indexes[locator.datasetId]?.index?.reloadPassage(locator)+ ?: return@withLock null+ selectMixedGroundingCandidates(+ listOf(KnowledgePromptHit(locator.datasetId, hit)),+ emptyList(),+ 0u,+ )+ .singleOrNull()+ }+ }+private fun reconcileAndOpenLocked(dataset: KnowledgeDatasetConfig): KnowledgeReconciliation {val previous = indexes[dataset.stableId]val result = assetStore.reconcileKnowledge(dataset.stableId)diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesProvider.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesProvider.ktindex 2ad389a93e..e314addb01 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesProvider.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesProvider.kt@@ -13,6 +13,7 @@ import io.ente.ensu.bindings.NotesCollectionimport io.ente.ensu.bindings.NotesExceptionimport io.ente.ensu.bindings.NotesIndexOptionsimport io.ente.ensu.bindings.NotesProgressCallback+import io.ente.ensu.bindings.PassageLocatorimport io.ente.ensu.bindings.notesContentRevisionimport java.io.ByteArrayOutputStreamimport java.io.File@@ -131,6 +132,11 @@ class NotesProvider(private val context: Context) {handle(id).use { it.search(query) }}+ internal suspend fun reload(locator: PassageLocator.LocalNote) = withAccess {+ registration(locator.collectionId)+ handle(locator.collectionId).use { it.reloadPassage(locator) }+ }+internal suspend fun inspectFreshness(id: String, cancel: NotesCancellation) = withAccess {withSource(registration(id), cancel) { source ->handle(id).use { it.inspectFreshness(source, cancel) }diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesStore.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesStore.ktindex c888f7802d..ca4970c808 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesStore.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesStore.kt@@ -14,7 +14,9 @@ import io.ente.ensu.bindings.NotesIndexOptionsimport io.ente.ensu.bindings.NotesProgressimport io.ente.ensu.bindings.NotesProgressCallbackimport io.ente.ensu.bindings.NotesSummary+import io.ente.ensu.bindings.PassageLocatorimport io.ente.ensu.bindings.notesLimits+import io.ente.ensu.bindings.selectMixedGroundingCandidatesimport io.ente.ensu.bindings.withNotesCollectionLabelimport io.ente.ensu.coroutines.runCatchingCancellableimport io.ente.ensu.llm.LlmProvider@@ -282,6 +284,22 @@ class NotesStore(hits}+ suspend fun reload(locator: PassageLocator.LocalNote): GroundedExcerpt? =+ withContext(Dispatchers.Main.immediate) {+ if (_state.value.collections.none { it.id == locator.collectionId && it.eligible })+ return@withContext null+ try {+ val hit = provider.reload(locator) ?: return@withContext null+ verify(selectMixedGroundingCandidates(emptyList(), listOf(hit), 1u)).singleOrNull()+ } catch (_: NotesException.RebuildRequired) {+ enqueue(locator.collectionId, rebuild = true, immediate = true)+ update(locator.collectionId) {+ it.copy(status = NotesStatus.Pending, indexAvailable = false)+ }+ null+ }+ }+suspend fun verify(excerpts: List<GroundedExcerpt>): List<GroundedExcerpt> =withContext(Dispatchers.Main.immediate) {val records = registrations.toMap()diff --git a/apple/apps/ensu/Ensu/Chat/ChatView.swift b/apple/apps/ensu/Ensu/Chat/ChatView.swiftindex e9488c39af..09c0fc5ac2 100644--- a/apple/apps/ensu/Ensu/Chat/ChatView.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatView.swift@@ -302,6 +302,10 @@ struct ChatView: View {onRetry: { message inviewModel.retryAssistantResponse(message)},+ onSearch: viewModel.canSearchSources+ ? { message in+ viewModel.retryAssistantResponse(message, searchAsWritten: true)+ } : nil,onBranchChange: { message, delta inviewModel.changeBranch(for: message, delta: delta)},diff --git a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swiftindex f1efecae80..1f51be1a64 100644--- a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift@@ -980,7 +980,11 @@ final class ChatViewModel: ObservableObject {startModelDownload(userInitiated: true)}- func retryAssistantResponse(_ message: RenderedChatMessage) {+ var canSearchSources: Bool {+ !knowledgeStore.enabledReadyDatasets.isEmpty || !notesStore.collections.isEmpty+ }++ func retryAssistantResponse(_ message: RenderedChatMessage, searchAsWritten: Bool = false) {let priorGeneration = generationTasklet priorSummary = sessionSummaryTaskguard message.role == .assistant else { return }@@ -1017,7 +1021,7 @@ final class ChatViewModel: ObservableObject {_ = await priorSummary?.resultguard let self else { return }await provider.resetContext()- startGeneration(for: userNode)+ startGeneration(for: userNode, searchAsWritten: searchAsWritten)}}@@ -1060,7 +1064,7 @@ final class ChatViewModel: ObservableObject {return session.id}- private func startGeneration(for userNode: MessageNode) {+ private func startGeneration(for userNode: MessageNode, searchAsWritten: Bool = false) {guard !isChatUnsupported else {showUnsupportedDeviceDialog = truereturn@@ -1103,6 +1107,46 @@ final class ChatViewModel: ObservableObject {var embeddingAssetInvalid = falsevar knowledgeHits: [GroundedExcerpt] = []let enabledDatasets = knowledgeStore.enabledReadyDatasets+ var followup: ConversationFollowup?+ var reloaded: [GroundedExcerpt] = []+ defer { followup?.cancel() }+ do {+ if prompt.imageFiles.isEmpty {+ let history = Array(+ buildSelectedPath(+ for: userNode.sessionId,+ childrenMap: childrenByParent(sessionId: userNode.sessionId)+ ).prefix { $0.id != userNode.id })+ let path = history.map { $0.id.uuidString } + [userNode.id.uuidString]+ let assistantTexts = history.filter { $0.role == .assistant }.map { $0.text }+ let hasCurrentSources =+ searchAsWritten || !enabledDatasets.isEmpty+ || !notesStore.collections.isEmpty+ followup = try await Task.detached {+ [chatDb] () throws -> ConversationFollowup? in+ guard+ hasCurrentSources+ || assistantTexts.contains(where: {+ !parseGroundedAssistantText(storedText: $0).sources.isEmpty+ })+ else { return nil }+ return try chatDb.startConversationFollowup(+ sessionUuid: userNode.sessionId.uuidString, path: path,+ question: userNode.text, expectedUserText: userNode.text+ )+ }.value+ try Task.checkCancellation()+ guard activeGenerationId == generationId else { return }+ if followup != nil { conversationStatus = "Finding sources" }+ }+ } catch {+ guard !isCancellation(error), activeGenerationId == generationId else { return }+ if case ConversationError.Stale = error {+ generationErrorMessage = "Conversation changed. Retry the message."+ return+ }+ logger.warning("Source search preparation failed", details: "\(error)")+ }let queryText = userNode.text.trimmingCharacters(in: .whitespacesAndNewlines)if !queryText.isEmpty,(!enabledDatasets.isEmpty@@ -1136,6 +1180,35 @@ final class ChatViewModel: ObservableObject {}}+ do {+ if let turn = followup {+ let searched = knowledgeHits+ let resolution = try await Task.detached {+ try searchAsWritten+ ? turn.searchAsWritten() : turn.searchWithHistory(searched: searched)+ }.value+ try Task.checkCancellation()+ guard activeGenerationId == generationId else { return }+ for reference in resolution.referencedPassages {+ do {+ if let hit = try await reloadPassage(reference) { reloaded.append(hit) }+ } catch {+ if isCancellation(error) { throw error }+ logger.warning("Source reload failed", details: "\(error)")+ }+ }+ }+ } catch {+ guard !isCancellation(error), activeGenerationId == generationId else { return }+ if case ConversationError.Stale = error {+ generationErrorMessage = "Conversation changed. Retry the message."+ return+ }+ logger.warning("Source history search failed", details: "\(error)")+ followup?.cancel()+ followup = nil+ }+do {try await self.ensureChatModelReady(selection)} catch {@@ -1319,7 +1392,9 @@ final class ChatViewModel: ObservableObject {expectedUserText: userNode.text,historyQuery: userNode.text,maxTokens: nil,- searched: knowledgeHits+ searched: knowledgeHits,+ followup: followup,+ reloaded: reloaded),temperature: self.resolveTemperature(),onProgress: {@@ -1443,6 +1518,16 @@ final class ChatViewModel: ObservableObject {}}+ private func reloadPassage(_ reference: IncludedPassage) async throws -> GroundedExcerpt? {+ switch reference.locator {+ case .ensuPack:+ return try await knowledgeProvider.reload(+ reference.locator, datasets: knowledgeStore.enabledReadyDatasets)+ case .localNote:+ return try await notesStore.reload(reference.locator)+ }+ }+private func finishGeneration(parent: MessageNode, response: String, tokenCount: Int, totalTimeMs: Int64?,interrupted: Bool, generationId: UUID, citations: [GroundedSource] = [],diff --git a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swiftindex 25908d5ab5..ae32450803 100644--- a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift+++ b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift@@ -246,6 +246,7 @@ struct AssistantMessageBubbleView: View {let isLastMessage: Boollet onCopy: () -> Voidlet onRetry: () -> Void+ let onSearch: (() -> Void)?let onBranchChange: (Int) -> Voidlet onOpenAttachment: (ChatAttachment) -> Voidlet showsMetadata: Bool@@ -290,6 +291,12 @@ struct AssistantMessageBubbleView: View {hapticMedium()onRetry()}+ if let onSearch {+ Button("Search as written") {+ hapticMedium()+ onSearch()+ }+ }}}diff --git a/apple/apps/ensu/Ensu/Chat/MessageListView.swift b/apple/apps/ensu/Ensu/Chat/MessageListView.swiftindex 7ea5e43077..72b243ae7f 100644--- a/apple/apps/ensu/Ensu/Chat/MessageListView.swift+++ b/apple/apps/ensu/Ensu/Chat/MessageListView.swift@@ -14,6 +14,7 @@ struct MessageListView: View {let onEdit: (RenderedChatMessage) -> Voidlet onCopy: (RenderedChatMessage) -> Voidlet onRetry: (RenderedChatMessage) -> Void+ let onSearch: ((RenderedChatMessage) -> Void)?let onBranchChange: (RenderedChatMessage, Int) -> Voidlet onDismissKeyboard: () -> Void@@ -204,6 +205,7 @@ struct MessageListView: View {isLastMessage: message.id == messages.last?.id,onCopy: { onCopy(message) },onRetry: { onRetry(message) },+ onSearch: onSearch.map { search in { search(message) } },onBranchChange: { delta in onBranchChange(message, delta) },onOpenAttachment: openAttachment,showsMetadata: truediff --git a/apple/apps/ensu/Ensu/Services/KnowledgeProvider.swift b/apple/apps/ensu/Ensu/Services/KnowledgeProvider.swiftindex 3326ea0efe..e0965986f7 100644--- a/apple/apps/ensu/Ensu/Services/KnowledgeProvider.swift+++ b/apple/apps/ensu/Ensu/Services/KnowledgeProvider.swift@@ -175,6 +175,24 @@ actor KnowledgeProvider {}}+ func reload(_ locator: PassageLocator, datasets: [KnowledgeDatasetConfig]) async throws+ -> GroundedExcerpt?+ {+ guard case .ensuPack(let datasetId, _, _) = locator,+ datasets.contains(where: { $0.stableId == datasetId })+ else { return nil }+ return try await withIndexGate {+ guard let index = indexes[datasetId]?.index else { return nil }+ return try await Task.detached {+ guard let hit = try index.reloadPassage(locator: locator) else { return nil }+ return try selectMixedGroundingCandidates(+ packHits: [KnowledgePromptHit(datasetId: datasetId, hit: hit)],+ notesHits: [], notesLimit: 0+ ).first+ }.value+ }+ }+private func reconcileAndActivateLocked(dataset: KnowledgeDatasetConfig) async throws -> KnowledgeReconciliation {diff --git a/apple/apps/ensu/Ensu/Services/NotesProvider.swift b/apple/apps/ensu/Ensu/Services/NotesProvider.swiftindex 9b792a5c14..d21040809e 100644--- a/apple/apps/ensu/Ensu/Services/NotesProvider.swift+++ b/apple/apps/ensu/Ensu/Services/NotesProvider.swift@@ -162,6 +162,17 @@ actor NotesProvider {}}+ func reload(_ locator: PassageLocator) async throws -> NotesHit? {+ guard case .localNote(let collectionId, _, _, _, _) = locator else { return nil }+ return try await diskGate.withLock {+ _ = try self.registration(collectionId)+ let root = self.root+ return try await Task.detached {+ try Self.handle(root, collectionId).reloadPassage(locator: locator)+ }.value+ }+ }+func verify(_ reference: NoteSourceReference) async throws -> NoteSourceReference {try await diskGate.withLock {let record = try self.registration(reference.collectionId)diff --git a/apple/apps/ensu/Ensu/Services/NotesStore.swift b/apple/apps/ensu/Ensu/Services/NotesStore.swiftindex d19d73cdfb..2b0c3dcf63 100644--- a/apple/apps/ensu/Ensu/Services/NotesStore.swift+++ b/apple/apps/ensu/Ensu/Services/NotesStore.swift@@ -202,6 +202,25 @@ final class NotesStore: ObservableObject, ModelMaintenance {return hits}+ func reload(_ locator: PassageLocator) async throws -> GroundedExcerpt? {+ guard case .localNote(let collectionId, _, _, _, _) = locator,+ collections.contains(where: { $0.id == collectionId && $0.eligible })+ else { return nil }+ do {+ guard let hit = try await provider.reload(locator) else { return nil }+ return try await verify(+ selectMixedGroundingCandidates(packHits: [], notesHits: [hit], notesLimit: 1)+ ).first+ } catch NotesError.RebuildRequired {+ enqueue(collectionId, rebuild: true, due: .distantPast)+ update(collectionId) {+ $0.status = .pending+ $0.indexAvailable = false+ }+ return nil+ }+ }+func verify(_ excerpts: [GroundedExcerpt]) async throws -> [GroundedExcerpt] {var result: [GroundedExcerpt] = []var accepted = 0@@ -218,12 +237,9 @@ final class NotesStore: ObservableObject, ModelMaintenance {}do {let reference = try await provider.verify(reference)- result.append(- GroundedExcerpt(- score: excerpt.score,- source: .localNote(reference: reference),- text: excerpt.text- ))+ var verifiedExcerpt = excerpt+ verifiedExcerpt.source = .localNote(reference: reference)+ result.append(verifiedExcerpt)accepted += 1} catch is CancellationError {throw CancellationError()diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex c97d1165bb..e98c5ba014 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2536,6 +2536,7 @@ dependencies = ["self_cell","serde","serde_json",+ "sha2 0.11.0","tempfile","thiserror 2.0.18","tokio",diff --git a/rust/apps/ensu/changes/historical-passage-reuse.md b/rust/apps/ensu/changes/historical-passage-reuse.mdnew file mode 100644index 0000000000..328c0f68bf--- /dev/null+++ b/rust/apps/ensu/changes/historical-passage-reuse.md@@ -0,0 +1 @@+- Improved follow-up questions about notes and packs, with checks for changed or unavailable sources.diff --git a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs b/rust/apps/ensu/src-tauri/src/commands/chat_db.rsindex 6abf918ee3..4a916fc14d 100644--- a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs+++ b/rust/apps/ensu/src-tauri/src/commands/chat_db.rs@@ -172,6 +172,8 @@ pub struct ChatMessageDto {created_at: i64,attachments: Vec<ChatAttachmentDto>,sources: Vec<GroundedSourceDto>,+ #[serde(skip_serializing_if = "Option::is_none")]+ evidence_saved: Option<bool>,}impl From<db::Message> for ChatMessageDto {@@ -189,6 +191,7 @@ impl From<db::Message> for ChatMessageDto {}};Self {+ evidence_saved: None,message_uuid: message.uuid.to_string(),session_uuid: message.session_uuid.to_string(),parent_message_uuid: message.parent_message_uuid.map(|value| value.to_string()),@@ -382,14 +385,29 @@ pub async fn chat_db_insert_message(input.text};with_chat_db_async(&state, move |db| {- Ok(ChatMessageDto::from(db.insert_message_guarded(- session_uuid,- sender,- &text,- parent,- attachments,- prepared.as_ref(),- )?))+ let snapshot = prepared.as_ref().map(|(snapshot, _)| snapshot);+ let message =+ db.insert_message_guarded(session_uuid, sender, &text, parent, attachments, snapshot)?;+ let evidence_saved =+ prepared+ .filter(|(_, passages)| !passages.is_empty())+ .map(|(snapshot, passages)| {+ match db.save_answer_evidence(&snapshot, message.uuid, passages) {+ Ok(saved) => saved,+ Err(error) => {+ logging::log(+ "ChatDb",+ format!(+ "Reply saved but source reference persistence failed: {error}"+ ),+ );+ false+ }+ }+ });+ let mut result = ChatMessageDto::from(message);+ result.evidence_saved = evidence_saved;+ Ok(result)}).await}diff --git a/rust/apps/ensu/src-tauri/src/commands/conversation.rs b/rust/apps/ensu/src-tauri/src/commands/conversation.rsindex b2e47dce74..0917d9fcad 100644--- a/rust/apps/ensu/src-tauri/src/commands/conversation.rs+++ b/rust/apps/ensu/src-tauri/src/commands/conversation.rs@@ -9,6 +9,7 @@ use ente_ensu::{conversation::{self, Effects, PrepareError},db::{ChatDb, SqliteBackend, chat::ConversationSnapshot},llm,+ retrieval::IncludedPassage,};use serde::{Deserialize, Serialize};use tauri::{AppHandle, Emitter, Manager, State as TauriState, WebviewWindow, async_runtime};@@ -22,6 +23,7 @@ struct Slot {token: String,snapshot: ConversationSnapshot,db: Arc<ChatDb<SqliteBackend>>,+ grounded: Option<ente_ensu::retrieval::GroundedPromptContext>,model_epoch: u64,cancel_epoch: u64,}@@ -64,7 +66,7 @@ impl State {app: &AppHandle,window: &str,token: &str,- ) -> Result<ConversationSnapshot, ApiError> {+ ) -> Result<(ConversationSnapshot, Vec<IncludedPassage>), ApiError> {let mut slots = self.slots.lock()@@ -78,7 +80,14 @@ impl State {}slots.remove(window)- .map(|slot| slot.snapshot)+ .map(|slot| {+ (+ slot.snapshot,+ slot.grounded+ .map(|context| context.included_passages)+ .unwrap_or_default(),+ )+ }).ok_or_else(|| error("Preparation has expired"))}}@@ -96,6 +105,7 @@ pub struct Request {max_tokens: usize,preparation_token: String,cancellation_epoch: u64,+ resolution_token: Option<String>,}#[derive(Serialize)]@@ -257,9 +267,18 @@ pub async fn conversation_prepare(let mut snapshot = db.conversation_snapshot(input.session_uuid, &input.path).map_err(ApiError::from)?;+ if let Some(token) = &input.resolution_token {+ app.state::<super::followup::State>().validate(+ work_window.label(),+ token,+ &db,+ &snapshot,+ input.grounding_candidates.as_ref(),+ )?;+ }let candidates = match input.grounding_candidates.take() {Some(candidates) => candidates,- None => conversation::GroundingCandidates::new(vec![], conversation::MAX_GROUNDING_BYTES)+ None => conversation::GroundingCandidates::new(vec![], vec![], conversation::MAX_GROUNDING_BYTES).map_err(preparation_error)?,};let turn = conversation::TurnInput {@@ -298,12 +317,13 @@ pub async fn conversation_prepare(Response {preparation_token: token.clone(),messages: result.messages,- grounded_context: grounded.map(Into::into),+ grounded_context: grounded.clone().map(Into::into),},Slot {token,snapshot,db,+ grounded,model_epoch,cancel_epoch: expected_epoch,},diff --git a/rust/apps/ensu/src-tauri/src/commands/followup.rs b/rust/apps/ensu/src-tauri/src/commands/followup.rsnew file mode 100644index 0000000000..444cafc6a7--- /dev/null+++ b/rust/apps/ensu/src-tauri/src/commands/followup.rs@@ -0,0 +1,285 @@+use super::{chat_db::ChatDbState, common::ApiError};+use ente_ensu::{+ conversation as core,+ db::{ChatDb, SqliteBackend, chat::ConversationSnapshot},+};+use serde::{Deserialize, Serialize};+use std::collections::HashMap;+use std::sync::{Arc, Mutex, atomic::Ordering};+use tauri::{AppHandle, Manager, State as TauriState, WebviewWindow};+use uuid::Uuid;++#[derive(Default)]+pub struct State {+ guards: Mutex<HashMap<String, Guard>>,+}+struct Guard {+ token: String,+ fingerprint: String,+ db: Arc<ChatDb<SqliteBackend>>,+ candidates: Option<String>,+}++fn error(message: impl Into<String>) -> ApiError {+ ApiError::new("followup", message)+}++impl State {+ pub(crate) fn validate(+ &self,+ window: &str,+ token: &str,+ db: &Arc<ChatDb<SqliteBackend>>,+ snapshot: &ConversationSnapshot,+ candidates: Option<&core::GroundingCandidates>,+ ) -> Result<(), ApiError> {+ let guards = self+ .guards+ .lock()+ .map_err(|_| error("Follow-up state is unavailable"))?;+ if guards.get(window).is_some_and(|guard| {+ guard.token == token+ && guard.candidates.as_ref().is_some_and(|expected| {+ candidates+ .and_then(|c| serde_json::to_string(c).ok())+ .as_ref()+ == Some(expected)+ })+ && Arc::ptr_eq(&guard.db, db)+ && guard.fingerprint == core::fingerprint(snapshot.messages())+ }) {+ Ok(())+ } else {+ Err(ApiError::new(+ "stale",+ "Conversation changed after selecting sources; retry the reply",+ ))+ }+ }+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct Request {+ token: String,+ session_uuid: Uuid,+ path: Vec<Uuid>,+ question: String,+ enabled_stable_ids: Vec<String>,+ cancellation_epoch: u64,+ candidates: Option<core::GroundingCandidates>,+}+#[derive(Serialize)]+#[serde(rename_all = "camelCase")]+pub struct Response {+ token: String,+ candidates: core::GroundingCandidates,+}++#[tauri::command]+pub async fn conversation_resolve_followup(+ app: AppHandle,+ window: WebviewWindow,+ state: TauriState<'_, State>,+ llm_state: TauriState<'_, super::llm::State>,+ input: Request,+) -> Result<Response, ApiError> {+ if Uuid::parse_str(&input.token).is_err()+ || input.path.is_empty()+ || input.path.len() > core::MAX_HISTORY_MESSAGES+ || input.question.len() > core::MAX_HISTORY_BYTES+ {+ return Err(error(+ "Please restate the source question with its subject or document name",+ ));+ }+ let db = app.state::<ChatDbState>().database()?;+ let snapshot = db+ .conversation_snapshot(input.session_uuid, &input.path)+ .map_err(ApiError::from)?;+ let question = snapshot+ .messages()+ .last()+ .ok_or_else(|| error("Missing current question"))?;+ if question.sender != ente_ensu::db::Sender::SelfUser {+ return Err(error("Follow-up must end at a user question"));+ }+ {+ let mut guards = state+ .guards+ .lock()+ .map_err(|_| error("Follow-up state is unavailable"))?;+ if guards+ .get(window.label())+ .is_some_and(|guard| guard.token == input.token)+ {+ return Err(error(+ "This source selection was already attempted; retry the reply",+ ));+ }+ guards.insert(+ window.label().to_owned(),+ Guard {+ token: input.token.clone(),+ fingerprint: core::fingerprint(snapshot.messages()),+ db: db.clone(),+ candidates: None,+ },+ );+ }+ let epoch = llm_state.retrieval_epoch();+ let expected = input.cancellation_epoch;+ let check = || {+ if epoch.load(Ordering::Relaxed) == expected {+ Ok(())+ } else {+ Err(ApiError::new("cancelled", "Follow-up cancelled"))+ }+ };+ check()?;+ let evidence: Vec<_> = snapshot.evidence().into_iter().cloned().collect();+ let (searched, max_utf8_bytes) = input+ .candidates+ .map_or((Vec::new(), core::MAX_GROUNDING_BYTES), |fresh| {+ (fresh.searched, fresh.max_utf8_bytes)+ });+ let mut candidates = core::GroundingCandidates::new(Vec::new(), searched, max_utf8_bytes)+ .map_err(|cause| error(cause.to_string()))?;+ let references = core::direct_followup_references(+ snapshot.messages(),+ &evidence,+ &input.question,+ &candidates.searched,+ )+ .unwrap_or_default();+ for reference in references {+ check()?;+ match super::knowledge::reload_for_followup(+ &app,+ reference,+ input.enabled_stable_ids.clone(),+ expected,+ )+ .await+ {+ Ok(Some(passage)) => candidates.referenced.push(passage),+ Ok(None) => {}+ Err(error) if error.name == Some("cancelled") => return Err(error),+ Err(error) => crate::logging::log(+ "Conversation",+ format!("Source reload skipped: {}", error.message),+ ),+ }+ }+ candidates+ .validate()+ .map_err(|cause| error(cause.to_string()))?;+ check()?;+ if !db+ .conversation_snapshot_matches(&snapshot)+ .map_err(ApiError::from)?+ || !Arc::ptr_eq(&db, &app.state::<ChatDbState>().database()?)+ {+ return Err(ApiError::new(+ "stale",+ "Conversation changed while selecting sources; retry the reply",+ ));+ }+ let mut guards = state+ .guards+ .lock()+ .map_err(|_| error("Follow-up state is unavailable"))?;+ let guard = guards+ .get_mut(window.label())+ .filter(|guard| guard.token == input.token)+ .ok_or_else(|| ApiError::new("stale", "Source question was superseded"))?;+ guard.candidates =+ Some(serde_json::to_string(&candidates).map_err(|cause| error(cause.to_string()))?);+ Ok(Response {+ token: input.token,+ candidates,+ })+}++#[cfg(test)]+mod tests {+ use super::*;+ fn candidates() -> core::GroundingCandidates {+ core::GroundingCandidates::new(vec![], vec![], 6000).unwrap()+ }+ fn fixture() -> (Arc<ChatDb<SqliteBackend>>, ConversationSnapshot) {+ let db = Arc::new(ChatDb::open_in_memory(vec![4; 32]).unwrap());+ let session = db.create_session("Synthetic source question").unwrap();+ let question = db+ .insert_message(+ session.uuid,+ "self",+ "Where does she track it?",+ None,+ vec![],+ )+ .unwrap();+ let snapshot = db+ .conversation_snapshot(session.uuid, &[question.uuid])+ .unwrap();+ (db, snapshot)+ }+ #[test]+ fn preparation_requires_matching_completed_source_selection() {+ let (db, snapshot) = fixture();+ let state = State::default();+ let valid = candidates();+ state.guards.lock().unwrap().insert(+ "window".into(),+ Guard {+ token: "token".into(),+ fingerprint: core::fingerprint(snapshot.messages()),+ db: db.clone(),+ candidates: None,+ },+ );+ assert!(+ state+ .validate("window", "token", &db, &snapshot, Some(&valid))+ .is_err()+ );+ state+ .guards+ .lock()+ .unwrap()+ .get_mut("window")+ .unwrap()+ .candidates = Some(serde_json::to_string(&valid).unwrap());+ assert!(+ state+ .validate("window", "token", &db, &snapshot, Some(&valid))+ .is_ok()+ );+ let mut changed = candidates();+ changed.max_utf8_bytes = 3000;+ let (other_db, _) = fixture();+ for (token, db, candidates) in [+ ("token", &db, Some(&changed)),+ ("token", &db, None),+ ("another-token", &db, Some(&valid)),+ ("token", &other_db, Some(&valid)),+ ] {+ assert!(+ state+ .validate("window", token, db, &snapshot, candidates)+ .is_err()+ );+ }+ let question = snapshot.messages()[0].uuid;+ db.update_message_text(question, "Actually, use another source")+ .unwrap();+ let edited = db+ .conversation_snapshot(snapshot.session_uuid(), &[question])+ .unwrap();+ assert!(+ state+ .validate("window", "token", &db, &edited, Some(&valid))+ .is_err()+ );+ }+}diff --git a/rust/apps/ensu/src-tauri/src/commands/knowledge.rs b/rust/apps/ensu/src-tauri/src/commands/knowledge.rsindex 88b0236e81..a1778ee741 100644--- a/rust/apps/ensu/src-tauri/src/commands/knowledge.rs+++ b/rust/apps/ensu/src-tauri/src/commands/knowledge.rs@@ -661,20 +661,128 @@ pub async fn knowledge_retrieve(}check_cancelled()?;- let candidates = ente_ensu::conversation::GroundingCandidates::new(excerpts, context_budget as usize)- .map_err(|error| ApiError::new("conversation", error.to_string()))?;+ let candidates = ente_ensu::conversation::GroundingCandidates::new(vec![], excerpts, context_budget as usize)+ .map_err(|error| ApiError::new("followup", error.to_string()))?;candidates.pack(context_budget as usize).map(|context| context.map(|context| {let mut result = GroundedPromptContextDto::from(context);result.candidates = Some(candidates);result}))- .map_err(|error| ApiError::new("conversation", error.to_string()))+ .map_err(|error| ApiError::new("followup", error.to_string()))}).await.map_err(|_| ApiError::new("llm_thread", "Knowledge retrieval task failed"))?}+pub(crate) async fn reload_for_followup(+ app: &AppHandle,+ reference: retrieval::IncludedPassage,+ enabled_stable_ids: Vec<String>,+ retrieval_epoch: u64,+) -> Result<Option<retrieval::ReferencedPassage>, ApiError> {+ let app = app.clone();+ let indexes = Arc::clone(&app.state::<State>().indexes);+ let (collection_ids, notes) = app+ .try_state::<crate::commands::notes::State>()+ .map_or_else(+ || (Vec::new(), None),+ |state| {+ (+ state.available_index_collection_ids(),+ Some(state.retrieval_handle()),+ )+ },+ );+ let cancellation_epoch = app.state::<crate::commands::llm::State>().retrieval_epoch();+ async_runtime::spawn_blocking(move || {+ let check_cancelled = || {+ if cancellation_epoch.load(Ordering::Relaxed) == retrieval_epoch {+ Ok(())+ } else {+ Err(ApiError::new("cancelled", "Knowledge reload cancelled"))+ }+ };+ check_cancelled()?;+ let mut packs = Vec::new();+ let mut note_hits = Vec::new();+ match &reference.locator {+ retrieval::PassageLocator::EnsuPack { dataset_id, .. } => {+ if !enabled_stable_ids.contains(dataset_id) {+ return Ok(None);+ }+ let indexes = indexes.lock().unwrap_or_else(PoisonError::into_inner);+ let Some(open) = indexes.get(dataset_id) else {+ return Ok(None);+ };+ let Some(hit) = open+ .index+ .reload_passage(&reference.locator)+ .map_err(retrieval_error)?+ else {+ return Ok(None);+ };+ packs.push(retrieval::KnowledgePromptHit {+ dataset_id: dataset_id.clone(),+ hit,+ });+ }+ retrieval::PassageLocator::LocalNote { collection_id, .. } => {+ if !collection_ids.contains(collection_id) {+ return Ok(None);+ }+ let Some(notes) = ¬es else { return Ok(None) };+ match notes.reload_passage(&reference.locator) {+ Ok(Some(hit)) => note_hits.push(hit),+ Ok(None) => return Ok(None),+ Err(error) => {+ crate::commands::notes::mark_index_unreadable(&app, collection_id);+ logging::log(+ "Knowledge",+ format!("Notes passage reload unavailable: {error}"),+ );+ return Ok(None);+ }+ }+ }+ }+ check_cancelled()?;+ let mut selected = select_verified_mixed_grounding(&packs, ¬e_hits, |source| {+ check_cancelled()?;+ let valid = notes+ .as_ref()+ .is_some_and(|notes| notes.verify_source_reference(source));+ if !valid {+ crate::commands::notes::mark_reference_stale(+ &app,+ &source.collection_id,+ source.document_id.clone(),+ );+ }+ Ok(valid)+ })?;+ let Some(mut excerpt) = selected.pop() else {+ return Ok(None);+ };+ let Some(passages) = reference.verified_spans(&excerpt.text) else {+ return Ok(None);+ };+ if let (Some(notes), retrieval::GroundedSource::LocalNote { reference }) =+ (¬es, &mut excerpt.source)+ {+ reference.collection_label = notes.collection_label(&reference.collection_id);+ }+ check_cancelled()?;+ Ok(Some(retrieval::ReferencedPassage {+ reference,+ source: excerpt.source,+ passages,+ }))+ })+ .await+ .map_err(|_| ApiError::new("retrieval_thread", "Knowledge reload task failed"))?+}+pub(crate) fn clear_for_exit(app: &AppHandle) {let Some(state) = app.try_state::<State>() else {return;@@ -703,6 +811,13 @@ mod tests {fn note(index: usize) -> NotesSearchHit {NotesSearchHit {+ locator: retrieval::PassageLocator::LocalNote {+ collection_id: COLLECTION_ID.to_owned(),+ document_id: format!("note-{index}.md"),+ indexed_revision: "a".repeat(64),+ shard_sha256: "b".repeat(64),+ chunk_index: 0,+ },collection_id: COLLECTION_ID.to_string(),document_id: format!("note-{index}.md"),revision: "a".repeat(64),diff --git a/rust/apps/ensu/src-tauri/src/commands/mod.rs b/rust/apps/ensu/src-tauri/src/commands/mod.rsindex 7a6ba0c7f0..f365f4e84b 100644--- a/rust/apps/ensu/src-tauri/src/commands/mod.rs+++ b/rust/apps/ensu/src-tauri/src/commands/mod.rs@@ -22,3 +22,5 @@ pub fn cleanup_for_exit(app: &AppHandle) {chat_db::clear_for_exit(app);logging::log("App", "cleanup_for_exit complete");}++pub mod followup;diff --git a/rust/apps/ensu/src-tauri/src/commands/notes.rs b/rust/apps/ensu/src-tauri/src/commands/notes.rsindex 79e896dd16..cfc74f8b83 100644--- a/rust/apps/ensu/src-tauri/src/commands/notes.rs+++ b/rust/apps/ensu/src-tauri/src/commands/notes.rs@@ -698,6 +698,34 @@ impl RetrievalHandle {Ok(hits)}+ pub(crate) fn reload_passage(+ &self,+ locator: &ente_ensu::retrieval::PassageLocator,+ ) -> Result<Option<ente_ensu::notes::NotesSearchHit>, NotesError> {+ let ente_ensu::retrieval::PassageLocator::LocalNote { collection_id, .. } = locator else {+ return Ok(None);+ };+ {+ let mut cache = self+ .cached_indexes+ .lock()+ .unwrap_or_else(PoisonError::into_inner);+ cache.clock = cache.clock.wrapping_add(1);+ let clock = cache.clock;+ if let Some(entry) = cache.entries.get_mut(collection_id) {+ entry.last_used = clock;+ return entry.index.reload_passage(locator);+ }+ }+ let index = NotesCollectionIndex::open(&self.index_root, collection_id.clone())?;+ let hit = index.reload_passage(locator)?;+ self.cached_indexes+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .insert(collection_id.clone(), index);+ Ok(hit)+ }+pub(crate) fn verify_source_reference(&self, reference: &NoteSourceReference) -> bool {let collection = self.registrydiff --git a/rust/apps/ensu/src-tauri/src/main.rs b/rust/apps/ensu/src-tauri/src/main.rsindex 0f70ed39aa..0912a4af48 100644--- a/rust/apps/ensu/src-tauri/src/main.rs+++ b/rust/apps/ensu/src-tauri/src/main.rs@@ -25,6 +25,7 @@ fn main() {.manage(commands::chat_db::ChatDbState::default()).manage(commands::knowledge::State::default()).manage(commands::conversation::State::default())+ .manage(commands::followup::State::default()).setup(|app| {logging::init_logging(app.handle());logging::log("App", "setup started");@@ -96,6 +97,7 @@ fn main() {commands::llm::llm_prewarm_multimodal_context,commands::llm::llm_generate_chat_stream,commands::conversation::conversation_prepare,+ commands::followup::conversation_resolve_followup,commands::llm::llm_cancel,commands::llm::llm_retrieval_epoch,commands::llm::llm_model_state_epoch,diff --git a/rust/bindings/uniffi/ensu/src/conversation.rs b/rust/bindings/uniffi/ensu/src/conversation.rsindex ef5b8a1bfd..f7f0914464 100644--- a/rust/bindings/uniffi/ensu/src/conversation.rs+++ b/rust/bindings/uniffi/ensu/src/conversation.rs@@ -47,6 +47,8 @@ pub struct ConversationRequest {pub history_query: Option<String>,pub max_tokens: Option<u32>,pub searched: Vec<GroundedExcerpt>,+ pub followup: Option<Arc<crate::followup::ConversationFollowup>>,+ pub reloaded: Vec<GroundedExcerpt>,}#[derive(Debug, Clone, uniffi::Record)]@@ -125,7 +127,7 @@ impl ConversationPreparation {fn start(&self,- input: ConversationRequest,+ mut input: ConversationRequest,callback: &dyn ConversationProgressCallback,) -> Result<PreparedWork, ConversationError> {self.check_cancelled()?;@@ -141,7 +143,29 @@ impl ConversationPreparation {.conversation_snapshot(session, &path).map_err(error)?;self.check_cancelled()?;+ let mut referenced = Vec::new();+ if let Some(turn) = &input.followup {+ let (query, references) = turn.prepare(&self.db, &snapshot, &input.searched)?;+ input.history_query = Some(query);+ for reference in references {+ self.check_cancelled()?;+ let Some(hit) = input.reloaded.iter().find(|hit| {+ retrieval::PassageLocator::from(hit.locator.clone()) == reference.locator+ }) else {+ continue;+ };+ let Some(passages) = reference.verified_spans(&hit.text) else {+ continue;+ };+ referenced.push(retrieval::ReferencedPassage {+ passages,+ source: hit.source.clone().into(),+ reference,+ });+ }+ }let candidates = core::GroundingCandidates::new(+ referenced,input.searched.into_iter().map(Into::into).collect(),core::MAX_GROUNDING_BYTES,)?;@@ -240,6 +264,13 @@ impl ConversationPreparation {).map_err(error)?;work.saved = true;+ if let Some(grounded) = &work.grounded {+ let _ = self.db.save_answer_evidence(+ &work.snapshot,+ answer.uuid,+ grounded.included_passages.clone(),+ );+ }Ok(to_message(answer))}}diff --git a/rust/bindings/uniffi/ensu/src/followup.rs b/rust/bindings/uniffi/ensu/src/followup.rsnew file mode 100644index 0000000000..ae64860e1a--- /dev/null+++ b/rust/bindings/uniffi/ensu/src/followup.rs@@ -0,0 +1,319 @@+use crate::{+ conversation::{ConversationError, error},+ db::EnsuDb,+ retrieval::{GroundedExcerpt, IncludedPassage},+};+use ente_ensu::{conversation as core, db, retrieval};+use std::sync::{+ Arc, Mutex,+ atomic::{AtomicBool, Ordering},+};+use uuid::Uuid;++#[derive(Clone, uniffi::Record)]+pub struct ConversationFollowupResult {+ pub referenced_passages: Vec<IncludedPassage>,+}++#[derive(uniffi::Object)]+pub struct ConversationFollowup {+ db: Arc<db::ChatDb<db::SqliteBackend>>,+ snapshot: db::chat::ConversationSnapshot,+ question: String,+ selection: Mutex<Option<SourceSelection>>,+ cancelled: AtomicBool,+}++struct SourceSelection {+ references: Vec<retrieval::IncludedPassage>,+ searched: Option<Vec<retrieval::GroundedExcerpt>>,+}++#[uniffi::export]+impl EnsuDb {+ pub fn start_conversation_followup(+ &self,+ session_uuid: String,+ path: Vec<String>,+ question: String,+ expected_user_text: String,+ ) -> Result<Arc<ConversationFollowup>, ConversationError> {+ if path.is_empty()+ || path.len() > core::MAX_HISTORY_MESSAGES+ || question.len() > core::MAX_HISTORY_BYTES+ {+ return Err(error(+ "Please name the subject or document in a shorter source question",+ ));+ }+ let path = path+ .iter()+ .map(|id| Uuid::parse_str(id))+ .collect::<Result<Vec<_>, _>>()+ .map_err(error)?;+ let snapshot = self+ .inner+ .conversation_snapshot(Uuid::parse_str(&session_uuid).map_err(error)?, &path)+ .map_err(error)?;+ if !snapshot+ .messages()+ .last()+ .is_some_and(|m| m.sender == db::Sender::SelfUser && m.text == expected_user_text)+ {+ return Err(ConversationError::Stale);+ }+ Ok(Arc::new(ConversationFollowup {+ db: self.inner.clone(),+ snapshot,+ question,+ selection: Mutex::new(None),+ cancelled: AtomicBool::new(false),+ }))+ }+}++impl ConversationFollowup {+ fn check(&self) -> Result<(), ConversationError> {+ if self.cancelled.load(Ordering::Acquire) {+ return Err(ConversationError::Cancelled);+ }+ if !self+ .db+ .conversation_snapshot_matches(&self.snapshot)+ .map_err(error)?+ {+ return Err(ConversationError::Stale);+ }+ Ok(())+ }++ pub(crate) fn prepare(+ &self,+ db: &Arc<db::ChatDb<db::SqliteBackend>>,+ snapshot: &db::chat::ConversationSnapshot,+ searched: &[GroundedExcerpt],+ ) -> Result<(String, Vec<retrieval::IncludedPassage>), ConversationError> {+ self.check()?;+ if !Arc::ptr_eq(db, &self.db)+ || snapshot.session_uuid() != self.snapshot.session_uuid()+ || core::fingerprint(snapshot.messages()) != core::fingerprint(self.snapshot.messages())+ {+ return Err(ConversationError::Stale);+ }+ let stored = self.selection.lock().map_err(error)?;+ let selected = stored+ .as_ref()+ .ok_or_else(|| error("Source selection is not complete"))?;+ if let Some(expected) = &selected.searched {+ let actual = core::GroundingCandidates::new(+ Vec::new(),+ searched.iter().cloned().map(Into::into).collect(),+ core::MAX_GROUNDING_BYTES,+ )+ .map_err(error)?+ .searched;+ if actual != *expected {+ return Err(error(+ "Source search changed after selecting earlier passages; retry the reply",+ ));+ }+ }+ Ok((self.question.clone(), selected.references.clone()))+ }++ fn direct(+ &self,+ searched: Option<Vec<GroundedExcerpt>>,+ ) -> Result<ConversationFollowupResult, ConversationError> {+ self.check()?;+ let mut stored = self.selection.lock().map_err(error)?;+ if stored.is_some() {+ return Err(error("Source selection was already attempted"));+ }+ let searched = searched+ .map(|hits| {+ core::GroundingCandidates::new(+ Vec::new(),+ hits.into_iter().map(Into::into).collect(),+ core::MAX_GROUNDING_BYTES,+ )+ .map(|candidates| candidates.searched)+ .map_err(error)+ })+ .transpose()?;+ let references = if let Some(searched) = &searched {+ let evidence = self+ .snapshot+ .evidence()+ .into_iter()+ .cloned()+ .collect::<Vec<_>>();+ core::direct_followup_references(+ self.snapshot.messages(),+ &evidence,+ &self.question,+ searched,+ )+ .unwrap_or_default()+ } else {+ Vec::new()+ };+ let result = ConversationFollowupResult {+ referenced_passages: references.iter().cloned().map(Into::into).collect(),+ };+ *stored = Some(SourceSelection {+ references,+ searched,+ });+ Ok(result)+ }+}++#[uniffi::export]+impl ConversationFollowup {+ pub fn cancel(&self) {+ self.cancelled.store(true, Ordering::Release);+ }++ pub fn search_as_written(&self) -> Result<ConversationFollowupResult, ConversationError> {+ self.direct(None)+ }++ pub fn search_with_history(+ &self,+ searched: Vec<GroundedExcerpt>,+ ) -> Result<ConversationFollowupResult, ConversationError> {+ self.direct(Some(searched))+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn source_selection_is_bound_to_search_history_and_cancellation() {+ let db = EnsuDb {+ inner: Arc::new(db::ChatDb::open_in_memory(vec![7; 32]).unwrap()),+ };+ let session = db.inner.create_session("Direct follow-up").unwrap();+ let user = db+ .inner+ .insert_message(session.uuid, "self", "What does Cedar cover?", None, vec![])+ .unwrap();+ let source = retrieval::GroundedExcerpt {+ locator: retrieval::PassageLocator::LocalNote {+ collection_id: Uuid::from_u128(10).to_string(),+ document_id: "cedar.md".into(),+ indexed_revision: "a".repeat(64),+ shard_sha256: "b".repeat(64),+ chunk_index: 0,+ },+ score: 0.9,+ text: "Cedar requires 30 days.".into(),+ source: retrieval::GroundedSource::LocalNote {+ reference: ente_ensu::notes::NoteSourceReference {+ collection_id: Uuid::from_u128(10).to_string(),+ collection_label: None,+ document_id: "cedar.md".into(),+ indexed_revision: "a".repeat(64),+ title: "Cedar".into(),+ section: None,+ },+ },+ };+ let grounded =+ retrieval::build_grounded_prompt_context(std::slice::from_ref(&source), 6000)+ .unwrap()+ .unwrap();+ let before = db+ .inner+ .conversation_snapshot(session.uuid, &[user.uuid])+ .unwrap();+ let text =+ retrieval::finalize_grounded_assistant_text("Cedar covers returns.", &grounded.sources)+ .unwrap();+ let answer = db+ .inner+ .insert_message(session.uuid, "other", &text, Some(user.uuid), vec![])+ .unwrap();+ assert!(+ db.inner+ .save_answer_evidence(&before, answer.uuid, grounded.included_passages.clone())+ .unwrap()+ );+ let current = db+ .inner+ .insert_message(+ session.uuid,+ "self",+ "And the deadline?",+ Some(answer.uuid),+ vec![],+ )+ .unwrap();+ let snapshot = db+ .inner+ .conversation_snapshot(session.uuid, &[user.uuid, answer.uuid, current.uuid])+ .unwrap();+ let start = || {+ db.start_conversation_followup(+ session.uuid.to_string(),+ vec![+ user.uuid.to_string(),+ answer.uuid.to_string(),+ current.uuid.to_string(),+ ],+ current.text.clone(),+ current.text.clone(),+ )+ .unwrap()+ };+ let turn = start();+ assert!(turn.prepare(&db.inner, &snapshot, &[]).is_err());+ let result = turn.search_with_history(vec![]).unwrap();+ assert_eq!(+ turn.prepare(&db.inner, &snapshot, &[]).unwrap().1,+ grounded.included_passages+ );+ assert_eq!(result.referenced_passages.len(), 1);+ assert!(+ turn.prepare(&db.inner, &snapshot, &[source.into()])+ .is_err()+ );+ assert!(turn.search_with_history(vec![]).is_err());+ let direct = start();+ assert!(+ direct+ .search_as_written()+ .unwrap()+ .referenced_passages+ .is_empty()+ );+ assert!(+ direct+ .prepare(&db.inner, &snapshot, &[])+ .unwrap()+ .1+ .is_empty()+ );+ let other = Arc::new(db::ChatDb::open_in_memory(vec![7; 32]).unwrap());+ assert!(matches!(+ turn.prepare(&other, &snapshot, &[]),+ Err(ConversationError::Stale)+ ));+ let cancelled = start();+ cancelled.cancel();+ assert!(matches!(+ cancelled.search_with_history(vec![]),+ Err(ConversationError::Cancelled)+ ));+ db.inner+ .update_message_text(user.uuid, "Edited original")+ .unwrap();+ assert!(matches!(+ turn.prepare(&db.inner, &snapshot, &[]),+ Err(ConversationError::Stale)+ ));+ }+}diff --git a/rust/bindings/uniffi/ensu/src/lib.rs b/rust/bindings/uniffi/ensu/src/lib.rsindex 5d65a1a96e..917d5f5a46 100644--- a/rust/bindings/uniffi/ensu/src/lib.rs+++ b/rust/bindings/uniffi/ensu/src/lib.rs@@ -4,6 +4,7 @@ pub mod assets;pub mod config;pub mod conversation;pub mod db;+pub mod followup;pub mod image;pub mod llm;pub mod log;diff --git a/rust/bindings/uniffi/ensu/src/notes.rs b/rust/bindings/uniffi/ensu/src/notes.rsindex 2a6c89933d..cec73c9013 100644--- a/rust/bindings/uniffi/ensu/src/notes.rs+++ b/rust/bindings/uniffi/ensu/src/notes.rs@@ -179,6 +179,7 @@ pub struct NotesOutcome {#[derive(Clone, Debug, uniffi::Record)]pub struct NotesHit {+ pub locator: crate::retrieval::PassageLocator,pub collection_id: String,pub document_id: String,pub revision: String,@@ -194,6 +195,7 @@ impl From<core::NotesSearchHit> for NotesHit {collection_id: value.collection_id,document_id: value.document_id,revision: value.revision,+ locator: value.locator.into(),score: value.score,title: value.title,section: value.section,@@ -208,6 +210,7 @@ impl From<NotesHit> for core::NotesSearchHit {collection_id: value.collection_id,document_id: value.document_id,revision: value.revision,+ locator: value.locator.into(),score: value.score,title: value.title,section: value.section,@@ -346,16 +349,21 @@ impl NotesCollection {})}+ pub fn reload_passage(+ &self,+ locator: crate::retrieval::PassageLocator,+ ) -> Result<Option<NotesHit>, NotesError> {+ core::NotesCollectionIndex::open(&self.root, self.id.clone())+ .and_then(|index| index.reload_passage(&locator.into()))+ .map(|hit| hit.map(Into::into))+ .map_err(index_read_error)+ }+pub fn search(&self, query: Vec<f32>) -> Result<Vec<NotesHit>, NotesError> {- let result = core::NotesCollectionIndex::open(&self.root, self.id.clone())- .and_then(|index| index.search(&query));- match result {- Ok(hits) => Ok(hits.into_iter().map(Into::into).collect()),- Err(core::NotesError::Io(error)) if error.kind() == std::io::ErrorKind::NotFound => {- Err(NotesError::RebuildRequired)- }- Err(error) => Err(error.into()),- }+ core::NotesCollectionIndex::open(&self.root, self.id.clone())+ .and_then(|index| index.search(&query))+ .map(|hits| hits.into_iter().map(Into::into).collect())+ .map_err(index_read_error)}pub fn remove(&self) -> Result<(), NotesError> {@@ -363,6 +371,15 @@ impl NotesCollection {}}+fn index_read_error(error: core::NotesError) -> NotesError {+ match error {+ core::NotesError::Io(error) if error.kind() == std::io::ErrorKind::NotFound => {+ NotesError::RebuildRequired+ }+ error => error.into(),+ }+}+fn inventory(source: &dyn NotesSource,cancellation: &NotesCancellation,diff --git a/rust/bindings/uniffi/ensu/src/retrieval.rs b/rust/bindings/uniffi/ensu/src/retrieval.rsindex 94a97e62af..d8eda806c7 100644--- a/rust/bindings/uniffi/ensu/src/retrieval.rs+++ b/rust/bindings/uniffi/ensu/src/retrieval.rs@@ -27,8 +27,137 @@ fn knowledge_dataset(})}+#[derive(Debug, Clone, uniffi::Enum)]+pub enum PassageLocator {+ EnsuPack {+ dataset_id: String,+ revision_sha256: String,+ row: u64,+ },+ LocalNote {+ collection_id: String,+ document_id: String,+ indexed_revision: String,+ shard_sha256: String,+ chunk_index: u64,+ },+}++impl From<core::PassageLocator> for PassageLocator {+ fn from(value: core::PassageLocator) -> Self {+ match value {+ core::PassageLocator::EnsuPack {+ dataset_id,+ revision_sha256,+ row,+ } => Self::EnsuPack {+ dataset_id,+ revision_sha256,+ row,+ },+ core::PassageLocator::LocalNote {+ collection_id,+ document_id,+ indexed_revision,+ shard_sha256,+ chunk_index,+ } => Self::LocalNote {+ collection_id,+ document_id,+ indexed_revision,+ shard_sha256,+ chunk_index,+ },+ }+ }+}++impl From<PassageLocator> for core::PassageLocator {+ fn from(value: PassageLocator) -> Self {+ match value {+ PassageLocator::EnsuPack {+ dataset_id,+ revision_sha256,+ row,+ } => Self::EnsuPack {+ dataset_id,+ revision_sha256,+ row,+ },+ PassageLocator::LocalNote {+ collection_id,+ document_id,+ indexed_revision,+ shard_sha256,+ chunk_index,+ } => Self::LocalNote {+ collection_id,+ document_id,+ indexed_revision,+ shard_sha256,+ chunk_index,+ },+ }+ }+}++#[derive(Debug, Clone, uniffi::Record)]+pub struct PassageSpan {+ pub start_utf8: u32,+ pub end_utf8: u32,+ pub text_sha256: String,+}++impl From<core::PassageSpan> for PassageSpan {+ fn from(value: core::PassageSpan) -> Self {+ Self {+ start_utf8: value.start_utf8,+ end_utf8: value.end_utf8,+ text_sha256: value.text_sha256,+ }+ }+}++impl From<PassageSpan> for core::PassageSpan {+ fn from(value: PassageSpan) -> Self {+ Self {+ start_utf8: value.start_utf8,+ end_utf8: value.end_utf8,+ text_sha256: value.text_sha256,+ }+ }+}++#[derive(Debug, Clone, uniffi::Record)]+pub struct IncludedPassage {+ pub locator: PassageLocator,+ pub cleaning_version: u32,+ pub spans: Vec<PassageSpan>,+}++impl From<core::IncludedPassage> for IncludedPassage {+ fn from(value: core::IncludedPassage) -> Self {+ Self {+ locator: value.locator.into(),+ cleaning_version: value.cleaning_version,+ spans: value.spans.into_iter().map(Into::into).collect(),+ }+ }+}++impl From<IncludedPassage> for core::IncludedPassage {+ fn from(value: IncludedPassage) -> Self {+ Self {+ locator: value.locator.into(),+ cleaning_version: value.cleaning_version,+ spans: value.spans.into_iter().map(Into::into).collect(),+ }+ }+}+#[derive(Debug, Clone, uniffi::Record)]pub struct RetrievalHit {+ pub locator: PassageLocator,pub score: f32,pub text: String,pub title: String,@@ -39,6 +168,7 @@ pub struct RetrievalHit {impl From<core::RetrievalHit> for RetrievalHit {fn from(value: core::RetrievalHit) -> Self {Self {+ locator: value.locator.into(),score: value.score,text: value.text,title: value.title,@@ -51,6 +181,7 @@ impl From<core::RetrievalHit> for RetrievalHit {impl From<RetrievalHit> for core::RetrievalHit {fn from(value: RetrievalHit) -> Self {Self {+ locator: value.locator.into(),score: value.score,text: value.text,title: value.title,@@ -91,6 +222,16 @@ impl RetrievalIndex {Ok(Arc::new(Self { inner }))}+ pub fn reload_passage(+ &self,+ locator: PassageLocator,+ ) -> Result<Option<RetrievalHit>, KnowledgeRetrievalError> {+ self.inner+ .reload_passage(&locator.into())+ .map(|hit| hit.map(Into::into))+ .map_err(Into::into)+ }+pub fn search(&self,query: Vec<f32>,@@ -362,6 +503,7 @@ impl From<GroundedSource> for core::GroundedSource {#[derive(Debug, Clone, uniffi::Record)]pub struct GroundedExcerpt {+ pub locator: PassageLocator,pub score: f32,pub source: GroundedSource,pub text: String,@@ -370,6 +512,7 @@ pub struct GroundedExcerpt {impl From<core::GroundedExcerpt> for GroundedExcerpt {fn from(value: core::GroundedExcerpt) -> Self {Self {+ locator: value.locator.into(),score: value.score,source: value.source.into(),text: value.text,@@ -380,6 +523,7 @@ impl From<core::GroundedExcerpt> for GroundedExcerpt {impl From<GroundedExcerpt> for core::GroundedExcerpt {fn from(value: GroundedExcerpt) -> Self {Self {+ locator: value.locator.into(),score: value.score,source: value.source.into(),text: value.text,diff --git a/rust/crates/ensu/Cargo.toml b/rust/crates/ensu/Cargo.tomlindex 087b289d1c..da2d21135b 100644--- a/rust/crates/ensu/Cargo.toml+++ b/rust/crates/ensu/Cargo.toml@@ -14,6 +14,7 @@ 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 }diff --git a/rust/crates/ensu/src/conversation.rs b/rust/crates/ensu/src/conversation.rsindex 49986054a3..570dedda7b 100644--- a/rust/crates/ensu/src/conversation.rs+++ b/rust/crates/ensu/src/conversation.rs@@ -1,4 +1,5 @@mod budget;+mod followup;mod grounding;mod lookup;mod prepare;@@ -7,11 +8,15 @@ mod turn;mod uuid_text;pub use budget::{GenerationBudget, resolve_generation_budget};+pub use followup::direct_followup_references;pub use grounding::{FittedGrounding, GroundingCandidates, MAX_GROUNDING_BYTES};use grounding::{fit_grounding_with_history, grounded_system};use lookup::{HistoryLookup, lookup_history, requested_history};pub use prepare::{Effects, Preparation, PreparationResult, PrepareError};-pub(crate) use state::ConversationEnvelope;+pub use state::AnswerEvidence;+pub(crate) use state::{ConversationEnvelope, valid_answer_passages};+#[cfg(test)]+pub(crate) use state::{MAX_ANSWER_PASSAGES, MAX_EVIDENCE_ANSWERS, MAX_EVIDENCE_BYTES};pub use turn::{PreparedTurn, TurnInput, prepare_turn};#[cfg(test)]@@ -122,7 +127,7 @@ impl ConversationState {}}-fn fingerprint(messages: &[Message]) -> String {+pub fn fingerprint(messages: &[Message]) -> String {let rows: Vec<_> = messages.iter().map(|m| {diff --git a/rust/crates/ensu/src/conversation/followup.rs b/rust/crates/ensu/src/conversation/followup.rsnew file mode 100644index 0000000000..37edf4dcde--- /dev/null+++ b/rust/crates/ensu/src/conversation/followup.rs@@ -0,0 +1,38 @@+use super::{AnswerEvidence, valid_answer_passages};+use crate::db::Message;+use crate::retrieval::IncludedPassage;++pub fn direct_followup_references(+ history: &[Message],+ evidence: &[AnswerEvidence],+ question: &str,+ searched: &[crate::retrieval::GroundedExcerpt],+) -> Option<Vec<IncludedPassage>> {+ let Some((message, entry)) = history.iter().rev().skip(1).find_map(|message| {+ evidence+ .iter()+ .find(|entry| entry.assistant_message_uuid == message.uuid && entry.matches(history))+ .map(|entry| (message, entry))+ }) else {+ return Some(Vec::new());+ };+ if !valid_answer_passages(&entry.passages) {+ return None;+ }+ let prior = crate::retrieval::parse_grounded_assistant_text(&message.text).sources;+ let named = super::grounding::named_source_keys(+ question,+ prior.iter().chain(searched.iter().map(|hit| &hit.source)),+ );+ if !named.is_empty()+ && named.iter().all(|key| {+ searched+ .iter()+ .any(|hit| super::grounding::source_key(&hit.source) == *key)+ })+ {+ Some(Vec::new())+ } else {+ Some(entry.passages.clone())+ }+}diff --git a/rust/crates/ensu/src/conversation/grounding.rs b/rust/crates/ensu/src/conversation/grounding.rsindex 6b3f5857c0..3568fdfba8 100644--- a/rust/crates/ensu/src/conversation/grounding.rs+++ b/rust/crates/ensu/src/conversation/grounding.rs@@ -1,5 +1,5 @@use super::*;-use crate::retrieval::{self, GroundedExcerpt, GroundedPromptContext};+use crate::retrieval::{self, GroundedExcerpt, GroundedPromptContext, ReferencedPassage};pub const MAX_GROUNDING_BYTES: usize = 6000;pub const MAX_CANDIDATE_BYTES: usize = 64 * 1024;@@ -7,24 +7,164 @@ pub const MAX_CANDIDATE_BYTES: usize = 64 * 1024;#[derive(Debug, Clone, Serialize, Deserialize)]#[serde(rename_all = "camelCase", deny_unknown_fields)]pub struct GroundingCandidates {+ pub referenced: Vec<ReferencedPassage>,pub searched: Vec<GroundedExcerpt>,pub max_utf8_bytes: usize,}+pub(super) fn source_key(source: &retrieval::GroundedSource) -> (u8, String, String) {+ match source {+ retrieval::GroundedSource::LocalNote { reference } => (+ 0,+ reference.collection_id.clone(),+ reference.document_id.clone(),+ ),+ retrieval::GroundedSource::EnsuPack { citation } => {+ (1, citation.dataset_id.clone(), citation.source_url.clone())+ }+ }+}++pub(super) fn named_source_keys<'a>(+ query: &str,+ sources: impl Iterator<Item = &'a retrieval::GroundedSource>,+) -> std::collections::BTreeSet<(u8, String, String)> {+ use std::collections::{BTreeMap, BTreeSet};+ let words = |text: &str| -> BTreeSet<String> {+ text.split(|c: char| !c.is_alphanumeric())+ .filter(|s| s.chars().count() >= 3)+ .map(str::to_lowercase)+ .filter(|s| !"the and source sources note notes policy policies document documents return access equipment".split_whitespace().any(|w| w == s))+ .collect()+ };+ let tokens = |text: &str| -> Vec<String> {+ text.split(|c: char| !c.is_alphanumeric())+ .filter(|s| !s.is_empty())+ .map(str::to_lowercase)+ .collect()+ };+ let query_tokens = tokens(query);+ let mut title_matches = Vec::new();+ let mut documents = BTreeMap::<_, BTreeSet<String>>::new();+ for source in sources {+ let title = match source {+ retrieval::GroundedSource::LocalNote { reference } => &reference.title,+ retrieval::GroundedSource::EnsuPack { citation } => &citation.title,+ };+ if !words(title).is_empty() {+ let title = tokens(title);+ for (start, window) in query_tokens.windows(title.len()).enumerate() {+ if window == title {+ title_matches.push((source_key(source), start, start + title.len()));+ }+ }+ }+ let label = match source {+ retrieval::GroundedSource::LocalNote { reference } => {+ format!("{} {}", reference.title, reference.document_id)+ }+ retrieval::GroundedSource::EnsuPack { citation } => citation.title.clone(),+ };+ documents+ .entry(source_key(source))+ .or_default()+ .extend(words(&label));+ }+ let query_words = words(query);+ documents+ .iter()+ .filter(|(key, label)| {+ title_matches.iter().any(|(matched, start, end)| {+ matched == *key+ && !title_matches.iter().any(|(_, other_start, other_end)| {+ other_start <= start+ && other_end >= end+ && other_end - other_start > end - start+ })+ }) || label.iter().any(|word| {+ query_words.contains(word)+ && documents+ .values()+ .filter(|other| other.contains(word))+ .count()+ == 1+ })+ })+ .map(|(key, _)| key.clone())+ .collect()+}+impl GroundingCandidates {+ pub fn protect_named_sources(&mut self, query: &str) -> Result<(), PrepareError> {+ self.validate()?;+ if query.len() > super::lookup::LOOKUP_MAX_QUERY_BYTES {+ return Ok(());+ }+ let named = named_source_keys(+ query,+ self.referenced+ .iter()+ .map(|r| &r.source)+ .chain(self.searched.iter().map(|r| &r.source)),+ );+ let mut required: std::collections::BTreeSet<_> = self+ .referenced+ .iter()+ .map(|r| source_key(&r.source))+ .collect();+ for hit in &self.searched {+ let id = source_key(&hit.source);+ if !named.contains(&id) || !required.insert(id) {+ continue;+ }+ let Some(reference) =+ retrieval::build_grounded_prompt_context(std::slice::from_ref(hit), usize::MAX)+ .ok()+ .flatten()+ .and_then(|context| context.included_passages.into_iter().next())+ else {+ continue;+ };+ let Some(passages) = reference.verified_spans(&hit.text) else {+ continue;+ };+ self.referenced.push(ReferencedPassage {+ reference,+ source: hit.source.clone(),+ passages,+ });+ if self.validate().is_err() {+ self.referenced.pop();+ }+ }+ Ok(())+ }+pub fn new(+ mut referenced: Vec<ReferencedPassage>,mut searched: Vec<GroundedExcerpt>,max_utf8_bytes: usize,) -> Result<Self, PrepareError> {for hit in &mut searched {- hit.text = retrieval::sanitize_excerpt(&hit.text);+ hit.text = retrieval::clean_passage_text(&hit.text);let mut end = hit.text.len().min(MAX_GROUNDING_BYTES);while !hit.text.is_char_boundary(end) {end -= 1;}hit.text.truncate(end);}+ referenced.truncate(retrieval::MAX_GROUNDING_HITS);+ while referenced+ .iter()+ .flat_map(|r| &r.passages)+ .map(String::len)+ .sum::<usize>()+ > MAX_GROUNDING_BYTES+ {+ referenced.pop();+ }let result = Self {+ referenced,searched,max_utf8_bytes: max_utf8_bytes.min(MAX_GROUNDING_BYTES),};@@ -38,7 +178,8 @@ impl GroundingCandidates {.iter().filter(|hit| matches!(hit.source, retrieval::GroundedSource::EnsuPack { .. })).count();- if self.searched.len() > retrieval::MAX_GROUNDING_HITS+ if self.referenced.len() > retrieval::MAX_GROUNDING_HITS+ || self.searched.len() > retrieval::MAX_GROUNDING_HITS|| packs > retrieval::MAX_PACK_HITS|| self.searched.len().saturating_sub(packs) > retrieval::MAX_NOTES_GROUNDING_HITS|| self.max_utf8_bytes > MAX_GROUNDING_BYTES@@ -46,6 +187,13 @@ impl GroundingCandidates {.searched.iter().any(|h| h.text.len() > MAX_GROUNDING_BYTES || !h.score.is_finite())+ || self+ .referenced+ .iter()+ .flat_map(|r| &r.passages)+ .map(String::len)+ .sum::<usize>()+ > MAX_GROUNDING_BYTES|| serde_json::to_vec(self).map_err(|e| PrepareError::Backend(e.to_string()))?.len()@@ -59,8 +207,12 @@ impl GroundingCandidates {}pub fn pack(&self, budget: usize) -> Result<Option<GroundedPromptContext>, PrepareError> {- retrieval::build_grounded_prompt_context(&self.searched, budget.min(self.max_utf8_bytes))- .map_err(|e| PrepareError::Backend(e.to_string()))+ retrieval::build_followup_prompt_context(+ &self.referenced,+ &self.searched,+ budget.min(self.max_utf8_bytes),+ )+ .map_err(|e| PrepareError::Backend(e.to_string()))}}@@ -125,21 +277,47 @@ fn fit_grounding(current,))};- let initial = candidates.pack(candidates.max_utf8_bytes).ok().flatten();- let allowance = 1500.min(input_budget / 4).min(room);+ let original_references = candidates.referenced.len();+ let mut candidates = candidates.clone();+ let (initial, required, required_tokens) = loop {+ let packed = candidates.pack(candidates.max_utf8_bytes);+ let required = retrieval::build_followup_prompt_context(+ &candidates.referenced,+ &[],+ candidates.max_utf8_bytes,+ );+ if let (Ok(initial), Ok(required)) = (packed, required) {+ let tokens = count_context(required.as_ref())?.saturating_sub(base);+ if tokens <= room {+ break (initial, required, tokens);+ }+ }+ if candidates.referenced.pop().is_none() {+ break (None, None, 0);+ }+ };+ let allowance = 1500.min(input_budget / 4).max(required_tokens).min(room);let initial_count = count_context(initial.as_ref())?;let initial_tokens = initial_count.saturating_sub(base);if initial_count <= input_budget && initial_tokens <= allowance {return Ok(FittedGrounding {context: initial,- repacked: false,+ repacked: candidates.referenced.len() != original_references,evidence_tokens: initial_tokens,history_included: false,});}+ let required_bytes = required.as_ref().map_or(0, |c| c.text.len());let initial_bytes = initial.as_ref().map_or(0, |c| c.text.len());- let smaller =- initial_bytes.saturating_mul(allowance).saturating_mul(4) / initial_tokens.max(1) / 5;+ let extra_tokens = initial_tokens.saturating_sub(required_tokens).max(1);+ let extra_allowance = allowance.saturating_sub(required_tokens);+ let smaller = required_bytes+ + initial_bytes+ .saturating_sub(required_bytes)+ .saturating_mul(extra_allowance)+ .saturating_mul(4)+ / extra_tokens+ / 5;let packed = candidates.pack(smaller.min(initial_bytes.saturating_sub(1))).ok()@@ -157,9 +335,9 @@ fn fit_grounding(}}Ok(FittedGrounding {- context: None,+ context: required,repacked: true,- evidence_tokens: 0,+ evidence_tokens: required_tokens,history_included: false,})}diff --git a/rust/crates/ensu/src/conversation/grounding_tests.rs b/rust/crates/ensu/src/conversation/grounding_tests.rsindex 08546fcbc2..1f7276582b 100644--- a/rust/crates/ensu/src/conversation/grounding_tests.rs+++ b/rust/crates/ensu/src/conversation/grounding_tests.rs@@ -1,9 +1,16 @@use super::*;use crate::notes::NoteSourceReference;-use crate::retrieval::GroundedSource;+use crate::retrieval::{GroundedSource, PassageLocator};fn hit(text: &str, id: &str) -> GroundedExcerpt {GroundedExcerpt {+ locator: PassageLocator::LocalNote {+ collection_id: "123e4567-e89b-12d3-a456-426614174000".into(),+ document_id: id.into(),+ indexed_revision: "a".repeat(64),+ shard_sha256: "b".repeat(64),+ chunk_index: 0,+ },source: GroundedSource::LocalNote {reference: NoteSourceReference {collection_id: "123e4567-e89b-12d3-a456-426614174000".into(),@@ -22,7 +29,7 @@ fn measure(messages: &[ChatMessage]) -> Result<usize, PrepareError> {Ok(messages.iter().map(|m| m.content.chars().count() + 8).sum())}#[test]-fn measured_repack_keeps_text_and_citations_together() {+fn measured_repack_keeps_text_citations_and_exact_spans_together() {for (raw, prefix) in [(format!("Fact first. {}", "हिन्दी🙂 paragraph. ".repeat(600)),@@ -36,20 +43,55 @@ fn measured_repack_keeps_text_and_citations_together() {"First",),] {- let candidates = GroundingCandidates::new(vec![hit(&raw, "one.md")], 6000).unwrap();+ let candidates = GroundingCandidates::new(vec![], 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);let context = fit.context.unwrap();- assert_eq!(context.sources, vec![candidates.searched[0].source.clone()]);- assert!(context.text.contains(prefix));- assert!(context.text.len() < raw.len());+ assert_eq!(context.sources.len(), context.included_passages.len());+ assert_eq!(context.included_passages.len(), 1);+ for reference in &context.included_passages {+ let spans = reference.verified_spans(&raw).unwrap();+ assert!(context.text.contains(&spans[0]));+ assert!(spans[0].starts_with(prefix));+ }+ assert_eq!(serde_json::to_string(&candidates).unwrap(), original);}}#[test]+fn requested_old_spans_raise_soft_allowance_and_are_never_cut() {+ let old = hit(&"Old fact. ".repeat(90), "old.md");+ let reference = retrieval::build_grounded_prompt_context(std::slice::from_ref(&old), 6000)+ .unwrap()+ .unwrap()+ .included_passages+ .remove(0);+ let passages = reference.verified_spans(&old.text).unwrap();+ let required = ReferencedPassage {+ reference: reference.clone(),+ source: old.source,+ passages,+ };+ let candidates = GroundingCandidates::new(+ vec![required],+ vec![hit(&"New fact. ".repeat(600), "new.md")],+ 6000,+ )+ .unwrap();+ let fit = fit_grounding(&candidates, "System", "Question", 3000, measure).unwrap();+ assert!(fit.repacked && fit.evidence_tokens > 750);+ assert_eq!(fit.context.unwrap().included_passages[0], reference);+ assert!(+ fit_grounding(&candidates, "System", "Question", 700, measure)+ .unwrap()+ .repacked+ );+}+#[test]fn mandatory_input_and_insufficient_evidence_budget_are_explicit() {let candidates =- GroundingCandidates::new(vec![hit(&"word ".repeat(1000), "one.md")], 6000).unwrap();+ GroundingCandidates::new(vec![], vec![hit(&"word ".repeat(1000), "one.md")], 6000).unwrap();assert!(matches!(fit_grounding(&candidates, "System", &"x".repeat(1000), 100, measure),Err(PrepareError::CurrentInputTooLarge)@@ -64,11 +106,138 @@ fn mandatory_input_and_insufficient_evidence_budget_are_explicit() {#[test]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();+ assert!(GroundingCandidates::new(vec![], vec![hit.clone(); 8], 6000).is_err());+ let mut candidates = GroundingCandidates::new(vec![], vec![hit], 6000).unwrap();candidates.searched[0].text = "x".repeat(6001);assert!(candidates.validate().is_err());}+#[test]+fn named_comparison_keeps_both_complete_passages_before_optional_hits() {+ let cedar = hit(+ &format!("{}Cedar permits 30 days.", "Cedar details. ".repeat(45)),+ "cedar-policy.md",+ );+ let juniper = hit(+ &format!("{}Juniper permits 21 days.", "Juniper details. ".repeat(25)),+ "juniper-policy.md",+ );+ let mut candidates = GroundingCandidates::new(+ vec![],+ vec![+ cedar.clone(),+ juniper.clone(),+ hit(&"Unrelated details. ".repeat(200), "glacier-policy.md"),+ ],+ 6000,+ )+ .unwrap();+ candidates+ .protect_named_sources("Is the Juniper period longer than Cedar?")+ .unwrap();+ assert_eq!(candidates.referenced.len(), 2);+ let fit = fit_grounding(+ &candidates,+ "System",+ "Compare Cedar and Juniper.",+ 2500,+ measure,+ )+ .unwrap();+ assert!(fit.repacked);+ let context = fit.context.unwrap();+ for hit in [&cedar, &juniper] {+ assert!(context.text.contains(&hit.text));+ let reference = context+ .included_passages+ .iter()+ .find(|r| r.locator == hit.locator)+ .unwrap();+ assert_eq!(+ reference.verified_spans(&hit.text).unwrap(),+ vec![hit.text.clone()]+ );+ }+ assert!(+ fit_grounding(+ &candidates,+ "System",+ "Compare Cedar and Juniper.",+ 900,+ measure+ )+ .unwrap()+ .repacked+ );+}++#[test]+fn ordinary_questions_and_shared_label_words_do_not_promote_every_source() {+ let mut partial = GroundingCandidates::new(+ vec![],+ vec![hit(" ", "cedar.md"), hit("21 days", "juniper.md")],+ 6000,+ )+ .unwrap();+ partial+ .protect_named_sources("Compare Cedar and Juniper")+ .unwrap();+ let context = fit_grounding(+ &partial,+ "System",+ "Compare Cedar and Juniper",+ 4096,+ measure,+ )+ .unwrap()+ .context+ .unwrap();+ assert_eq!(context.sources.len(), 1);+ assert!(context.text.contains("21 days"));+ let base = GroundingCandidates::new(+ vec![],+ vec![+ hit("30 days", "cedar-policy.md"),+ hit("21 days", "juniper-policy.md"),+ ],+ 6000,+ )+ .unwrap();+ for query in ["Compare the policies in my notes", "Explain photography"] {+ let mut c = base.clone();+ c.protect_named_sources(query).unwrap();+ assert!(c.referenced.is_empty());+ assert_eq!(c.pack(6000).unwrap(), base.pack(6000).unwrap());+ }+}++#[test]+fn an_old_reference_and_new_named_document_are_both_protected() {+ let old = hit("Cedar 30 days.", "cedar.md");+ let packed = retrieval::build_grounded_prompt_context(std::slice::from_ref(&old), 6000)+ .unwrap()+ .unwrap();+ let reference = packed.included_passages[0].clone();+ let required = ReferencedPassage {+ passages: reference.verified_spans(&old.text).unwrap(),+ source: old.source,+ reference: reference.clone(),+ };+ let juniper = hit("Juniper 21 days.", "juniper.md");+ let mut second_chunk = juniper.clone();+ if let PassageLocator::LocalNote { chunk_index, .. } = &mut second_chunk.locator {+ *chunk_index = 1;+ }+ let mut c =+ GroundingCandidates::new(vec![required], vec![juniper, second_chunk], 6000).unwrap();+ c.protect_named_sources("Is Juniper longer than that?")+ .unwrap();+ assert_eq!(c.referenced.len(), 2);+ assert_eq!(c.referenced[0].reference, reference);+ c.protect_named_sources("Compare Cedar and Juniper.")+ .unwrap();+ assert_eq!(c.referenced.len(), 2);+}+fn prior_history() -> Vec<Message> {vec![Message {uuid: Uuid::from_u128(1),@@ -92,6 +261,7 @@ fn explicit_history_displaces_optional_search_without_losing_original_text() {let current = required.prepend_required(query);let budget = measure(&answer_messages("System", None, &[], ¤t)).unwrap() + 1;let candidates = GroundingCandidates::new(+ vec![],vec![hit(&"Optional search. ".repeat(100), "extra.md")],6000,)@@ -126,8 +296,12 @@ fn source_system_and_required_history_are_measured_in_final_roles() {let history = requested_history(&prior_history(), query, || Ok(())).unwrap().unwrap();- let c = GroundingCandidates::new(vec![hit("The new rule is 21 days.", "current.md")], 6000)- .unwrap();+ let c = GroundingCandidates::new(+ vec![],+ vec![hit("The new rule is 21 days.", "current.md")],+ 6000,+ )+ .unwrap();let fitted =fit_grounding_with_history(&c, Some(&history), "System", query, 4096, |messages| {if messages[0].content.contains("BEGIN KNOWLEDGE CONTEXT") {@@ -149,3 +323,158 @@ fn source_system_and_required_history_are_measured_in_final_roles() {assert!(final_text.contains("ZX-82Q"));assert!(final_text.ends_with(query));}++#[test]+fn overlapping_pack_and_note_titles_preserve_both_sources_without_blocking_switches() {+ let mut pack = hit("Mars is the fourth planet.", "unused.md");+ pack.locator = PassageLocator::EnsuPack {+ dataset_id: "wiki".into(),+ revision_sha256: "a".repeat(64),+ row: 0,+ };+ pack.source = GroundedSource::EnsuPack {+ citation: crate::retrieval::SourceCitation {+ dataset_id: "wiki".into(),+ dataset_label: "Wiki".into(),+ credit: "Wiki".into(),+ title: "Mars".into(),+ source_url: "https://example.com/mars".into(),+ license_label: "CC BY-SA 4.0".into(),+ license_url: "https://creativecommons.org/licenses/by-sa/4.0/".into(),+ },+ };+ let mut note = hit("Check-in code COPPER-29.", "mars-expedition.md");+ if let GroundedSource::LocalNote { reference } = &mut note.source {+ reference.title = "Mars expedition checklist".into();+ }+ let query = "Using the Mars encyclopedia entry and the Mars expedition checklist, give its position from the Sun and our expedition check-in code. Answer briefly.";+ let mut c = GroundingCandidates::new(vec![], vec![pack.clone(), note.clone()], 6000).unwrap();+ c.protect_named_sources(query).unwrap();+ assert_eq!(c.referenced.len(), 2);+ assert_eq!(c.pack(6000).unwrap().unwrap().sources.len(), 2);+ assert!(c.pack(100).is_err());++ let context = retrieval::build_grounded_prompt_context(&[pack], 6000)+ .unwrap()+ .unwrap();+ let mut history: Vec<_> = (1..=3)+ .map(|i| Message {+ uuid: Uuid::from_u128(i),+ session_uuid: Uuid::from_u128(100),+ parent_message_uuid: (i > 1).then(|| Uuid::from_u128(i - 1)),+ sender: if i == 2 {+ Sender::Other+ } else {+ Sender::SelfUser+ },+ text: query.into(),+ attachments: vec![],+ created_at: i as i64,+ })+ .collect();+ history[1].text =+ retrieval::finalize_grounded_assistant_text("Fourth planet", &context.sources).unwrap();+ let evidence = vec![AnswerEvidence {+ assistant_message_uuid: history[1].uuid,+ prefix_fingerprint: fingerprint(&history[..2]),+ passages: context.included_passages.clone(),+ }];+ assert_eq!(+ direct_followup_references(&history, &evidence, query, std::slice::from_ref(¬e))+ .unwrap(),+ context.included_passages+ );+ assert!(+ direct_followup_references(+ &history,+ &evidence,+ "What is the check-in code in the Mars expedition checklist?",+ &[note]+ )+ .unwrap()+ .is_empty()+ );+}++#[test]+fn direct_followups_reuse_saved_spans_but_named_search_can_change_sources() {+ let prior: Vec<_> = ["cedar", "pine", "birch", "oak", "maple"]+ .iter()+ .map(|name| hit("Archived policy fact.", &format!("{name}.md")))+ .collect();+ let context = retrieval::build_grounded_prompt_context(&prior, 6000)+ .unwrap()+ .unwrap();+ let mut history: Vec<_> = (1..=5)+ .map(|i| Message {+ uuid: Uuid::from_u128(i),+ session_uuid: Uuid::from_u128(100),+ parent_message_uuid: (i > 1).then(|| Uuid::from_u128(i - 1)),+ sender: if i % 2 == 0 {+ Sender::Other+ } else {+ Sender::SelfUser+ },+ text: "Please clarify the subject.".into(),+ attachments: vec![],+ created_at: i as i64,+ })+ .collect();+ history[1].text =+ retrieval::finalize_grounded_assistant_text("Saved facts", &context.sources).unwrap();+ let evidence = vec![AnswerEvidence {+ assistant_message_uuid: history[1].uuid,+ prefix_fingerprint: fingerprint(&history[..2]),+ passages: context.included_passages.clone(),+ }];+ let atlas = hit("Warranty lasts two years.", "atlas.md");+ assert_eq!(+ direct_followup_references(&history, &evidence, "And the deadline?", &[]).unwrap(),+ context.included_passages+ );+ assert!(+ direct_followup_references(+ &history,+ &evidence,+ "How long is the Atlas warranty?",+ std::slice::from_ref(&atlas)+ )+ .unwrap()+ .is_empty()+ );+ let mut candidates = GroundingCandidates::new(vec![], vec![atlas.clone()], 6000).unwrap();+ candidates+ .protect_named_sources("How long is the Atlas warranty?")+ .unwrap();+ assert!(+ candidates+ .pack(6000)+ .unwrap()+ .unwrap()+ .text+ .contains(&atlas.text)+ );+ let mut updated = prior[0].clone();+ updated.text = "Cedar now requires seven days.".into();+ assert!(+ direct_followup_references(+ &history,+ &evidence,+ "What is Cedar's current deadline?",+ &[updated]+ )+ .unwrap()+ .is_empty()+ );+ assert_eq!(+ direct_followup_references(&history, &evidence, "Compare Cedar and Atlas", &[atlas])+ .unwrap(),+ context.included_passages+ );+ history[0].text = "Edited history".into();+ assert!(+ direct_followup_references(&history, &evidence, "And the deadline?", &[])+ .unwrap()+ .is_empty()+ );+}diff --git a/rust/crates/ensu/src/conversation/state.rs b/rust/crates/ensu/src/conversation/state.rsindex 68a36bfb07..105bc2a8a0 100644--- a/rust/crates/ensu/src/conversation/state.rs+++ b/rust/crates/ensu/src/conversation/state.rs@@ -1,16 +1,54 @@-use serde::Serialize;+use serde::{Deserialize, Serialize};use uuid::Uuid;-use super::{ConversationState, MAX_STATE_BYTES, Summary, validate_summary_text};+use super::{ConversationState, MAX_STATE_BYTES, Summary, fingerprint, validate_summary_text};+use crate::db::{Message, Sender};+use crate::retrieval::IncludedPassage;pub const ENVELOPE_VERSION: u32 = 2;+pub const MAX_EVIDENCE_ANSWERS: usize = 8;+pub const MAX_EVIDENCE_BYTES: usize = 12 * 1024;+pub(crate) use crate::retrieval::MAX_GROUNDING_HITS as MAX_ANSWER_PASSAGES;-#[derive(Debug, Serialize)]+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct AnswerEvidence {+ #[serde(with = "super::uuid_text")]+ pub assistant_message_uuid: Uuid,+ pub prefix_fingerprint: String,+ pub passages: Vec<IncludedPassage>,+}++impl AnswerEvidence {+ pub fn matches(&self, history: &[Message]) -> bool {+ history+ .iter()+ .position(|m| m.uuid == self.assistant_message_uuid)+ .is_some_and(|index| {+ history[index].sender == Sender::Other+ && fingerprint(&history[..=index]) == self.prefix_fingerprint+ })+ }++ fn valid(&self) -> bool {+ self.prefix_fingerprint.len() == 64+ && !self.passages.is_empty()+ && valid_answer_passages(&self.passages)+ }+}++pub(crate) fn valid_answer_passages(passages: &[IncludedPassage]) -> bool {+ passages.len() <= MAX_ANSWER_PASSAGES+ && passages.iter().all(IncludedPassage::valid_metadata)+ && serde_json::to_vec(passages).is_ok_and(|bytes| bytes.len() <= MAX_EVIDENCE_BYTES)+}++#[derive(Debug, Clone, Serialize)]pub struct ConversationEnvelope {pub format_version: u32,#[serde(serialize_with = "super::uuid_text::serialize")]pub session_uuid: Uuid,pub summary: Option<Summary>,+ pub evidence: Vec<AnswerEvidence>,}impl ConversationEnvelope {@@ -19,6 +57,7 @@ impl ConversationEnvelope {format_version: ENVELOPE_VERSION,session_uuid: session,summary: None,+ evidence: Vec::new(),}}@@ -38,17 +77,67 @@ impl ConversationEnvelope {.cloned().and_then(|value| serde_json::from_value::<Summary>(value).ok()).filter(|summary| validate_summary_text(&summary.text).is_ok());+ let mut evidence = Vec::new();+ if let Some(entries) = value.get("evidence").and_then(|value| value.as_array())+ && entries.len() <= MAX_EVIDENCE_ANSWERS+ {+ for value in entries {+ if let Ok(entry) = serde_json::from_value::<AnswerEvidence>(value.clone())+ && entry.valid()+ && !evidence.iter().any(|old: &AnswerEvidence| {+ old.assistant_message_uuid == entry.assistant_message_uuid+ })+ {+ evidence.push(entry);+ }+ }+ }+ if serde_json::to_vec(&evidence).ok()?.len() > MAX_EVIDENCE_BYTES {+ evidence.clear();+ }Some(Self {format_version: ENVELOPE_VERSION,session_uuid: session,summary,+ evidence,})}- pub fn summary_state(self) -> Option<ConversationState> {- self.summary.map(|summary| ConversationState {+ pub fn summary_state(&self) -> Option<ConversationState> {+ self.summary.clone().map(|summary| ConversationState {session_uuid: self.session_uuid,summary,})}++ pub fn add_evidence(&mut self, history: &[Message], passages: Vec<IncludedPassage>) -> bool {+ let Some(answer) = history.last() else {+ return false;+ };+ if history.len() < 2+ || answer.sender != Sender::Other+ || answer.session_uuid != self.session_uuid+ || passages.is_empty()+ || !valid_answer_passages(&passages)+ || super::validate_path(history).is_err()+ {+ return false;+ }+ self.evidence+ .retain(|entry| entry.assistant_message_uuid != answer.uuid);+ self.evidence.push(AnswerEvidence {+ assistant_message_uuid: answer.uuid,+ prefix_fingerprint: fingerprint(history),+ passages,+ });+ while self.evidence.len() > MAX_EVIDENCE_ANSWERS+ || serde_json::to_vec(&self.evidence)+ .map_or(true, |bytes| bytes.len() > MAX_EVIDENCE_BYTES)+ {+ self.evidence.remove(0);+ }+ self.evidence+ .last()+ .is_some_and(|entry| entry.assistant_message_uuid == answer.uuid)+ }}diff --git a/rust/crates/ensu/src/conversation/tests.rs b/rust/crates/ensu/src/conversation/tests.rsindex 7b217d3620..d327029078 100644--- a/rust/crates/ensu/src/conversation/tests.rs+++ b/rust/crates/ensu/src/conversation/tests.rs@@ -64,10 +64,9 @@ fn saved_memory_preserves_uuid_strings() {.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 mut expected = serde_json::from_str::<serde_json::Value>(encoded).unwrap();+ expected["evidence"] = serde_json::json!([]);+ assert_eq!(serde_json::to_value(&restored).unwrap(), expected);let invalid = encoded.replace("00000000-0000-0000-0000-000000000002", "invalid");assert!(ConversationEnvelope::decode(invalid.as_bytes(), session)@@ -622,7 +621,7 @@ fn turn_input(messages: Vec<Message>) -> TurnInput {context: 2048,output: None,state: None,- candidates: GroundingCandidates::new(vec![], MAX_GROUNDING_BYTES).unwrap(),+ candidates: GroundingCandidates::new(vec![], vec![], MAX_GROUNDING_BYTES).unwrap(),}}diff --git a/rust/crates/ensu/src/conversation/turn.rs b/rust/crates/ensu/src/conversation/turn.rsindex 5fb6af0a0e..9a91c3da39 100644--- a/rust/crates/ensu/src/conversation/turn.rs+++ b/rust/crates/ensu/src/conversation/turn.rs@@ -44,6 +44,9 @@ pub fn prepare_turn(.transpose()?.flatten();effects.check_cancelled()?;+ input+ .candidates+ .protect_named_sources(input.history_query.as_deref().unwrap_or(&input.current))?;let grounding = fit_grounding_with_history(&input.candidates,required.as_ref(),diff --git a/rust/crates/ensu/src/db/chat/conversation.rs b/rust/crates/ensu/src/db/chat/conversation.rsindex 937abbd464..59ed836bed 100644--- a/rust/crates/ensu/src/db/chat/conversation.rs+++ b/rust/crates/ensu/src/db/chat/conversation.rs@@ -1,5 +1,7 @@use super::*;-use crate::conversation::{self, ConversationEnvelope, ConversationState, MAX_STATE_BYTES};+use crate::conversation::{+ self, AnswerEvidence, ConversationEnvelope, ConversationState, MAX_STATE_BYTES,+};use std::collections::HashMap;const MESSAGE_COLUMNS: &str =@@ -10,6 +12,7 @@ pub struct ConversationSnapshot {pub(super) session_uuid: Uuid,pub(super) messages: Vec<Message>,pub(super) state: Option<ConversationState>,+ derived: ConversationEnvelope,rows: Vec<crate::db::Row>,envelope: Value,}@@ -21,6 +24,13 @@ impl ConversationSnapshot {pub fn state(&self) -> Option<&ConversationState> {self.state.as_ref()}+ pub fn evidence(&self) -> Vec<&AnswerEvidence> {+ self.derived+ .evidence+ .iter()+ .filter(|entry| entry.matches(&self.messages))+ .collect()+ }pub fn session_uuid(&self) -> Uuid {self.session_uuid}@@ -77,13 +87,15 @@ impl<B: Backend> ChatDb<B> {}conversation::validate_path(&messages).map_err(|e| Error::UnsupportedOperation(e.to_string()))?;- let state = self+ let derived = self.decode_conversation(&envelope, session)- .and_then(ConversationEnvelope::summary_state);+ .unwrap_or_else(|| ConversationEnvelope::empty(session));+ let state = derived.summary_state();Ok(ConversationSnapshot {session_uuid: session,messages,state,+ derived,rows,envelope,})@@ -122,19 +134,20 @@ impl<B: Backend> ChatDb<B> {.validate().map_err(|_| Error::UnsupportedOperation("Invalid conversation state".into()))?;}- let mut derived = ConversationEnvelope::empty(snapshot.session_uuid);+ let mut derived = snapshot.derived.clone();derived.summary = state.as_ref().map(|state| state.summary.clone());let envelope = self.encode_conversation(&derived)?;let applied = self.replace_conversation_envelope(snapshot, envelope.clone())?;if applied {snapshot.envelope = envelope;snapshot.state = state;+ snapshot.derived = derived;}Ok(applied)}fn encode_conversation(&self, derived: &ConversationEnvelope) -> Result<Value> {- if derived.summary.is_none() {+ if derived.summary.is_none() && derived.evidence.is_empty() {return Ok(Value::Null);}let bytes = serde_json::to_vec(derived)?;@@ -146,6 +159,43 @@ impl<B: Backend> ChatDb<B> {Ok(Value::Blob(crypto::encrypt_blob(&bytes, &self.key)?))}+ pub fn save_answer_evidence(+ &self,+ snapshot: &ConversationSnapshot,+ answer: Uuid,+ passages: Vec<crate::retrieval::IncludedPassage>,+ ) -> Result<bool> {+ if !conversation::valid_answer_passages(&passages) || passages.is_empty() {+ return Ok(false);+ }+ let mut path: Vec<_> = snapshot.messages.iter().map(|m| m.uuid).collect();+ path.push(answer);+ let current = self.conversation_snapshot(snapshot.session_uuid, &path)?;+ if current.envelope != snapshot.envelope+ || current.rows[..snapshot.rows.len()] != snapshot.rows+ {+ return Ok(false);+ }+ let saved = current+ .messages+ .last()+ .ok_or(Error::UnsupportedOperation("Missing saved answer".into()))?;+ let sources = crate::retrieval::parse_grounded_assistant_text(&saved.text).sources;+ if !passages.iter().all(|passage| {+ sources+ .iter()+ .any(|source| passage.locator.matches_source(source))+ }) {+ return Ok(false);+ }+ let mut derived = current.derived.clone();+ if !derived.add_evidence(¤t.messages, passages) {+ return Ok(false);+ }+ let envelope = self.encode_conversation(&derived)?;+ self.replace_conversation_envelope(¤t, envelope)+ }+fn replace_conversation_envelope(&self,snapshot: &ConversationSnapshot,@@ -174,18 +224,37 @@ impl<B: Backend> ChatDb<B> {if envelope == Value::Null {return Ok(());}- let clear = match self- .decode_conversation(&envelope, session)- .and_then(|envelope| envelope.summary)- {- Some(summary) => depends_on(tx, summary.covered_boundary_message_uuid, edited)?,- None => true,- };- if clear {+ let Some(mut derived) = self.decode_conversation(&envelope, session) else {tx.execute("UPDATE sessions SET conversation_state = NULL WHERE session_uuid = ?",&[Value::Text(session.to_string())],)?;+ return Ok(());+ };+ let mut changed = false;+ if let Some(summary) = &derived.summary+ && depends_on(tx, summary.covered_boundary_message_uuid, edited)?+ {+ derived.summary = None;+ changed = true;+ }+ let mut retained = Vec::new();+ for entry in derived.evidence.drain(..) {+ if depends_on(tx, entry.assistant_message_uuid, edited)? {+ changed = true;+ } else {+ retained.push(entry);+ }+ }+ derived.evidence = retained;+ if changed {+ tx.execute(+ "UPDATE sessions SET conversation_state = ? WHERE session_uuid = ?",+ &[+ self.encode_conversation(&derived)?,+ Value::Text(session.to_string()),+ ],+ )?;}Ok(())}@@ -349,6 +418,41 @@ mod tests {.unwrap()}+ fn passages() -> Vec<crate::retrieval::IncludedPassage> {+ let fixtures: serde_json::Value = serde_json::from_str(include_str!(+ "../../../tests/fixtures/passage-references-v1.json"+ ))+ .unwrap();+ vec![serde_json::from_value(fixtures[0]["reference"].clone()).unwrap()]+ }++ fn sourced_answer(+ db: &ChatDb<crate::db::SqliteBackend>,+ captured: &ConversationSnapshot,+ ) -> Message {+ let source = crate::retrieval::GroundedSource::LocalNote {+ reference: crate::notes::NoteSourceReference {+ collection_id: "123e4567-e89b-12d3-a456-426614174000".into(),+ collection_label: None,+ document_id: "trip.md".into(),+ indexed_revision: "a".repeat(64),+ title: "Trip".into(),+ section: None,+ },+ };+ let text =+ crate::retrieval::finalize_grounded_assistant_text("Saved answer", &[source]).unwrap();+ db.insert_message_guarded(+ captured.session_uuid,+ "other",+ &text,+ captured.messages.last().map(|m| m.uuid),+ vec![],+ Some(captured),+ )+ .unwrap()+ }+#[test]fn migrated_orphan_history_supports_summary_restore_and_continuation() {let dir = tempfile::tempdir().unwrap();@@ -488,6 +592,283 @@ mod tests {);}+ #[test]+ fn evidence_and_summary_survive_encrypted_reopen_and_branch_selection() {+ let dir = tempfile::tempdir().unwrap();+ let file = dir.path().join("evidence.db");+ let db = ChatDb::open_sqlite_with_defaults(&file, vec![7; 32]).unwrap();+ let session = db.create_session("Sources").unwrap().uuid;+ let question = db+ .insert_message(session, "self", "Tell me about my trip", None, vec![])+ .unwrap();+ let captured = snapshot(&db, session, std::slice::from_ref(&question));+ let answer = sourced_answer(&db, &captured);+ let before = db.get_session(session).unwrap();+ assert!(+ db.save_answer_evidence(&captured, answer.uuid, passages())+ .unwrap()+ );+ assert_eq!(db.get_session(session).unwrap(), before);+ let full = vec![question.clone(), answer];+ let mut restored = snapshot(&db, session, &full);+ assert!(restored.state().is_none());+ assert_eq!(restored.evidence()[0].passages, passages());+ let Value::Blob(bytes) = &restored.envelope else {+ panic!("encrypted")+ };+ assert!(!bytes.windows(7).any(|w| w == b"trip.md"));+ let memory = ConversationState::new(session, &full, "Trip discussed".into()).unwrap();+ assert!(+ db.replace_conversation_state(&mut restored, Some(memory.clone()))+ .unwrap()+ );+ drop(restored);+ drop(captured);+ drop(db);+ let db = ChatDb::open_sqlite_with_defaults(&file, vec![7; 32]).unwrap();+ let reopened = snapshot(&db, session, &full);+ assert_eq!(reopened.evidence().len(), 1);+ assert_eq!(reopened.state(), Some(&memory));+ let sibling = db+ .insert_message(+ session,+ "other",+ "Other branch",+ Some(question.uuid),+ vec![],+ )+ .unwrap();+ assert!(+ snapshot(&db, session, &[question, sibling])+ .evidence()+ .is_empty()+ );+ assert_eq!(snapshot(&db, session, &full).evidence().len(), 1);+ }++ #[test]+ fn summary_updates_preserve_evidence_and_edits_invalidate_only_dependents() {+ let (db, session, mut messages) = seeded();+ let captured = snapshot(&db, session, &messages);+ let answer = sourced_answer(&db, &captured);+ assert!(+ db.save_answer_evidence(&captured, answer.uuid, passages())+ .unwrap()+ );+ messages.push(answer);+ let mut current = snapshot(&db, session, &messages);+ assert!(+ db.replace_conversation_state(&mut current, Some(state(session, &messages, "Memory")))+ .unwrap()+ );+ assert_eq!(snapshot(&db, session, &messages).evidence().len(), 1);+ assert!(db.replace_conversation_state(&mut current, None).unwrap());+ assert_eq!(snapshot(&db, session, &messages).evidence().len(), 1);+ assert!(+ db.replace_conversation_state(&mut current, Some(state(session, &messages, "Memory")))+ .unwrap()+ );+ db.update_message_text(messages[2].uuid, "Edited question")+ .unwrap();+ let current = snapshot(&db, session, &messages);+ assert!(current.state().is_some());+ assert!(current.evidence().is_empty());+ }++ #[test]+ fn evidence_write_races_and_failures_keep_the_answer_and_prior_state() {+ let (db, session, messages) = seeded();+ let captured = snapshot(&db, session, &messages);+ let answer = sourced_answer(&db, &captured);+ let mut other_writer = snapshot(&db, session, &messages);+ assert!(+ db.replace_conversation_state(+ &mut other_writer,+ Some(state(session, &messages, "Other writer"))+ )+ .unwrap()+ );+ assert!(+ !db.save_answer_evidence(&captured, answer.uuid, passages())+ .unwrap()+ );+ assert_eq!(db.get_message(answer.uuid).unwrap().unwrap(), answer);+ db.backend.execute("CREATE TRIGGER fail_evidence BEFORE UPDATE OF conversation_state ON sessions BEGIN SELECT RAISE(ABORT, 'storage failure'); END", &[]).unwrap();+ assert!(+ db.save_answer_evidence(&other_writer, answer.uuid, passages())+ .is_err()+ );+ assert_eq!(db.get_message(answer.uuid).unwrap().unwrap(), answer);+ assert_eq!(+ snapshot(&db, session, &messages)+ .state()+ .unwrap()+ .summary+ .text,+ "Other writer"+ );+ }++ #[test]+ fn evidence_rejects_changed_history_missing_citations_and_deleted_sessions() {+ let (db, session, messages) = seeded();+ let captured = snapshot(&db, session, &messages);+ let uncited = db+ .insert_message_guarded(+ session,+ "other",+ "No sources",+ Some(messages[2].uuid),+ vec![],+ Some(&captured),+ )+ .unwrap();+ assert!(+ !db.save_answer_evidence(&captured, uncited.uuid, passages())+ .unwrap()+ );+ let answer = sourced_answer(&db, &captured);+ db.update_message_text(messages[0].uuid, "Changed history")+ .unwrap();+ assert!(+ !db.save_answer_evidence(&captured, answer.uuid, passages())+ .unwrap()+ );+ db.delete_session(session).unwrap();+ assert!(+ db.save_answer_evidence(&captured, answer.uuid, passages())+ .is_err()+ );+ assert!(db.get_session(session).unwrap().is_none());+ }++ #[test]+ fn malformed_components_do_not_destroy_valid_siblings() {+ let (db, session, messages) = seeded();+ let memory = state(session, &messages, "Memory");+ let mut captured = snapshot(&db, session, &messages);+ assert!(+ db.replace_conversation_state(&mut captured, Some(memory.clone()))+ .unwrap()+ );+ let answer = sourced_answer(&db, &captured);+ assert!(+ db.save_answer_evidence(&captured, answer.uuid, passages())+ .unwrap()+ );+ let mut path = messages;+ path.push(answer);+ let restored = snapshot(&db, session, &path);+ let mut value = serde_json::to_value(&restored.derived).unwrap();+ assert!(+ ConversationEnvelope::decode(&serde_json::to_vec(&value).unwrap(), Uuid::new_v4())+ .is_none()+ );+ value["format_version"] = 1.into();+ assert!(+ ConversationEnvelope::decode(&serde_json::to_vec(&value).unwrap(), session).is_none()+ );+ value = serde_json::to_value(&restored.derived).unwrap();+ value["summary"] = serde_json::json!({"bad": true});+ let decoded =+ ConversationEnvelope::decode(&serde_json::to_vec(&value).unwrap(), session).unwrap();+ assert!(decoded.summary.is_none());+ assert_eq!(decoded.evidence.len(), 1);+ value = serde_json::to_value(&restored.derived).unwrap();+ value["evidence"][0]["passages"][0]["spans"][0]["textSha256"] = "bad".into();+ let decoded =+ ConversationEnvelope::decode(&serde_json::to_vec(&value).unwrap(), session).unwrap();+ assert!(decoded.evidence.is_empty());+ assert_eq!(decoded.summary_state().unwrap(), memory);+ }++ #[test]+ fn evidence_retention_is_bounded_and_keeps_newest_answers() {+ let (db, session, mut messages) = seeded();+ let mut first_answer = None;+ for index in 0..10 {+ let captured = snapshot(&db, session, &messages);+ let answer = sourced_answer(&db, &captured);+ first_answer.get_or_insert(answer.uuid);+ assert!(+ db.save_answer_evidence(&captured, answer.uuid, passages())+ .unwrap()+ );+ messages.push(answer);+ let current = snapshot(&db, session, &messages);+ assert!(current.derived.evidence.len() <= conversation::MAX_EVIDENCE_ANSWERS);+ assert!(serde_json::to_vec(¤t.derived).unwrap().len() <= MAX_STATE_BYTES);+ if index == 9 {+ assert_eq!(current.evidence().len(), 8);+ assert!(+ !current+ .evidence()+ .iter()+ .any(|entry| Some(entry.assistant_message_uuid) == first_answer)+ );+ } else {+ let question = db+ .insert_message(+ session,+ "self",+ "Next",+ Some(messages.last().unwrap().uuid),+ vec![],+ )+ .unwrap();+ messages.push(question);+ }+ }+ }++ #[test]+ fn evidence_byte_limit_evicts_older_records_and_rejects_oversized_inputs() {+ let (db, session, mut messages) = seeded();+ let mut large = Vec::new();+ for chunk in 0..conversation::MAX_ANSWER_PASSAGES {+ let mut reference = passages().remove(0);+ if let crate::retrieval::PassageLocator::LocalNote { chunk_index, .. } =+ &mut reference.locator+ {+ *chunk_index = chunk as u64;+ }+ large.push(reference);+ }+ assert!(!conversation::valid_answer_passages(&vec![+ large[0].clone();+ 8+ ]));+ let mut future = large.clone();+ future[0].cleaning_version = 999;+ assert!(!conversation::valid_answer_passages(&future));+ for _ in 0..6 {+ let captured = snapshot(&db, session, &messages);+ let answer = sourced_answer(&db, &captured);+ assert!(+ db.save_answer_evidence(&captured, answer.uuid, large.clone())+ .unwrap()+ );+ messages.push(answer);+ let question = db+ .insert_message(+ session,+ "self",+ "Next",+ Some(messages.last().unwrap().uuid),+ vec![],+ )+ .unwrap();+ messages.push(question);+ }+ let current = snapshot(&db, session, &messages);+ assert!(current.evidence().len() < 6);+ assert!(!current.evidence().is_empty());+ assert!(+ serde_json::to_vec(¤t.derived.evidence).unwrap().len()+ <= conversation::MAX_EVIDENCE_BYTES+ );+ }+#[test]fn encrypted_memory_survives_titles_appends_and_recent_edits() {let (db, session, messages) = seeded();@@ -592,12 +973,6 @@ mod tests {).unwrap());- let mut encoded = ConversationEnvelope::empty(session);- encoded.summary = captured.state().map(|state| state.summary.clone());- encoded.format_version = 1;- assert!(- ConversationEnvelope::decode(&serde_json::to_vec(&encoded).unwrap(), session).is_none()- );for text in [" \n".to_owned(), "x".repeat(MAX_STATE_BYTES)] {let mut invalid = state(session, &messages, "Valid memory");invalid.summary.text = text;diff --git a/rust/crates/ensu/src/notes/index.rs b/rust/crates/ensu/src/notes/index.rsindex dde364b2c2..a95496dac3 100644--- a/rust/crates/ensu/src/notes/index.rs+++ b/rust/crates/ensu/src/notes/index.rs@@ -13,6 +13,7 @@ const MAX_HITS_PER_DOCUMENT: usize = 3;#[derive(Debug, Clone, PartialEq)]pub struct NotesSearchHit {+ pub locator: crate::retrieval::PassageLocator,pub collection_id: String,pub document_id: String,pub revision: String,@@ -165,6 +166,77 @@ impl NotesCollectionIndex {}))}+ fn passage_locator(+ &self,+ document: &IndexedDocument,+ chunk_index: usize,+ ) -> crate::retrieval::PassageLocator {+ crate::retrieval::PassageLocator::LocalNote {+ collection_id: self.collection_id.clone(),+ document_id: document.document_id.clone(),+ indexed_revision: document.revision.clone(),+ shard_sha256: document.shard_sha256.clone(),+ chunk_index: chunk_index as u64,+ }+ }++ pub fn reload_passage(+ &self,+ locator: &crate::retrieval::PassageLocator,+ ) -> Result<Option<NotesSearchHit>, NotesError> {+ let crate::retrieval::PassageLocator::LocalNote {+ collection_id,+ document_id,+ indexed_revision,+ shard_sha256,+ chunk_index,+ } = locator+ else {+ return Ok(None);+ };+ if collection_id != &self.collection_id {+ return Ok(None);+ }+ let Some(document) = self.documents.iter().find(|document| {+ &document.document_id == document_id+ && &document.revision == indexed_revision+ && &document.shard_sha256 == shard_sha256+ }) else {+ return Ok(None);+ };+ let Ok(chunk_index) = usize::try_from(*chunk_index) else {+ return Ok(None);+ };+ if chunk_index >= document.chunk_count {+ return Ok(None);+ }+ let directory = shard_directory(+ self.read_pin.collection_directory(),+ document_id,+ indexed_revision,+ );+ let shard = load_and_validate_shard_metadata(+ &directory,+ collection_id,+ document_id,+ indexed_revision,+ document.chunk_count,+ shard_sha256,+ None,+ )?;+ let chunk = &shard.chunks[chunk_index];+ Ok(Some(NotesSearchHit {+ locator: locator.clone(),+ collection_id: collection_id.clone(),+ document_id: document_id.clone(),+ revision: indexed_revision.clone(),+ score: 0.0,+ title: shard.title,+ section: chunk.section.clone(),+ text: chunk.text.clone(),+ }))+ }+pub fn search(&self, query: &[f32]) -> Result<Vec<NotesSearchHit>, NotesError> {validate_search(query, self.dimension)?;let mut ranked = Vec::with_capacity(MAX_HITS_PER_COLLECTION);@@ -244,6 +316,7 @@ impl NotesCollectionIndex {NotesError::InvalidIndex("selected shard chunk is missing".to_string())})?;hits.push(NotesSearchHit {+ locator: self.passage_locator(document, candidate.chunk_index),collection_id: self.collection_id.clone(),document_id: document.document_id.clone(),revision: document.revision.clone(),@@ -344,6 +417,56 @@ mod tests {embedding}+ #[test]+ fn passage_reload_checks_revision_chunk_and_shard_integrity() {+ use crate::retrieval::PassageLocator;+ let temp = tempfile::tempdir().unwrap();+ let doc = document("trip.md", 2);+ let mut writer = NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();+ writer+ .commit_document(&doc, &[embedding(0, 1.0), embedding(0, 0.9)], &source(&doc))+ .unwrap();+ writer.publish(true).unwrap();+ let index = NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()).unwrap();+ let hits = index.search(&embedding(0, 1.0)).unwrap();+ assert_eq!(hits.len(), 2);+ assert_ne!(hits[0].locator, hits[1].locator);+ drop(index);+ let index = NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()).unwrap();+ for hit in &hits {+ let loaded = index.reload_passage(&hit.locator).unwrap().unwrap();+ assert_eq!(loaded.text, hit.text);+ assert_eq!(loaded.locator, hit.locator);+ }+ for field in ["revision", "shard", "chunk", "collection", "document"] {+ let mut wrong = hits[0].locator.clone();+ if let PassageLocator::LocalNote {+ indexed_revision,+ shard_sha256,+ chunk_index,+ collection_id,+ document_id,+ } = &mut wrong+ {+ match field {+ "revision" => *indexed_revision = "c".repeat(64),+ "shard" => *shard_sha256 = "c".repeat(64),+ "chunk" => *chunk_index = u64::MAX,+ "collection" => *collection_id = "other".to_string(),+ _ => *document_id = "other.md".to_string(),+ }+ }+ assert!(index.reload_passage(&wrong).unwrap().is_none(), "{field}");+ }+ let directory = shard_directory(+ &collection_directory(temp.path(), COLLECTION_ID),+ &doc.document_id,+ &doc.revision,+ );+ fs::write(directory.join(NOTES_SHARD_FILE), b"corrupt").unwrap();+ assert!(index.reload_passage(&hits[0].locator).is_err());+ }+#[test]fn search_loads_metadata_only_for_selected_documents() {let temp = tempfile::tempdir().unwrap();diff --git a/rust/crates/ensu/src/retrieval.rs b/rust/crates/ensu/src/retrieval.rsindex 94911c8201..720131fad6 100644--- a/rust/crates/ensu/src/retrieval.rs+++ b/rust/crates/ensu/src/retrieval.rs@@ -2,6 +2,7 @@ mod citation;mod grounded;mod index;mod pack;+mod passage;mod prompt;use thiserror::Error;@@ -47,7 +48,7 @@ pub use citation::{};pub use grounded::{GroundedExcerpt, GroundedPromptContext, GroundedSource, MAX_NOTES_GROUNDING_HITS,- build_grounded_prompt_context, select_mixed_grounding_candidates,+ ReferencedPassage, build_grounded_prompt_context, select_mixed_grounding_candidates,};pub use index::{RetrievalHit, RetrievalIndex};pub use pack::{@@ -57,4 +58,6 @@ pub use pack::{};pub use prompt::{KnowledgePromptContext, KnowledgePromptHit, build_knowledge_prompt_context};-pub(crate) use grounded::{MAX_GROUNDING_HITS, MAX_PACK_HITS, sanitize_excerpt};+pub(crate) use grounded::{MAX_GROUNDING_HITS, MAX_PACK_HITS, build_followup_prompt_context};+pub(crate) use passage::clean_passage_text;+pub use passage::{IncludedPassage, PassageLocator, PassageSpan};diff --git a/rust/crates/ensu/src/retrieval/grounded.rs b/rust/crates/ensu/src/retrieval/grounded.rsindex 2b1b403272..a14343d2c7 100644--- a/rust/crates/ensu/src/retrieval/grounded.rs+++ b/rust/crates/ensu/src/retrieval/grounded.rs@@ -23,6 +23,7 @@ pub enum GroundedSource {#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct GroundedExcerpt {+ pub locator: super::PassageLocator,pub score: f32,pub source: GroundedSource,pub text: String,@@ -32,6 +33,7 @@ pub struct GroundedExcerpt {pub struct GroundedPromptContext {pub text: String,pub sources: Vec<GroundedSource>,+ pub included_passages: Vec<super::IncludedPassage>,}pub fn select_mixed_grounding_candidates(@@ -69,6 +71,7 @@ pub fn select_mixed_grounding_candidates(})?;let title = display_title(&item.hit.title, item.hit.section.as_deref())?;Ok(GroundedExcerpt {+ locator: item.hit.locator.clone(),score: item.hit.score,source: GroundedSource::EnsuPack {citation: SourceCitation {@@ -120,6 +123,7 @@ pub fn select_mixed_grounding_candidates(.validate().map_err(|error| RetrievalError::InvalidInput(error.to_string()))?;selected.push(GroundedExcerpt {+ locator: hit.locator.clone(),score: hit.score,source: GroundedSource::LocalNote { reference },text: hit.text.clone(),@@ -152,7 +156,13 @@ pub fn build_grounded_prompt_context(let footer = format!("\n{END_CONTEXT_SENTINEL}");let mut text = header;let mut sources = Vec::new();+ let mut included_passages = Vec::new();for excerpt in excerpts {+ if !excerpt.locator.matches_source(&excerpt.source) {+ return Err(RetrievalError::InvalidInput(+ "passage locator does not match its source".to_string(),+ ));+ }let label = grounded_label(&excerpt.source)?;let prefix = format!("\n\n# {label}\n");let reserved = text@@ -165,7 +175,7 @@ pub fn build_grounded_prompt_context(let Some(available) = max_utf8_bytes.checked_sub(reserved) else {continue;};- let sanitized = sanitize_excerpt(&excerpt.text);+ let sanitized = super::clean_passage_text(&excerpt.text);let passage = truncate_utf8(&sanitized, available).trim();if passage.is_empty() {continue;@@ -173,13 +183,21 @@ pub fn build_grounded_prompt_context(text.push_str(&prefix);text.push_str(passage);sources.push(excerpt.source.clone());+ included_passages.push(super::IncludedPassage::prefix(+ excerpt.locator.clone(),+ passage,+ )?);}if sources.is_empty() {return Ok(None);}text.push_str(&footer);debug_assert!(text.len() <= max_utf8_bytes);- Ok(Some(GroundedPromptContext { text, sources }))+ Ok(Some(GroundedPromptContext {+ text,+ sources,+ included_passages,+ }))}fn display_title(title: &str, section: Option<&str>) -> Result<String, RetrievalError> {@@ -224,30 +242,116 @@ fn grounded_label(source: &GroundedSource) -> Result<String, RetrievalError> {}}-pub(crate) fn sanitize_excerpt(value: &str) -> String {- value- .replace("\r\n", "\n")- .replace('\r', "\n")- .lines()- .map(|line| {- let line = line- .chars()- .map(|character| {- if character == '\t' || !character.is_control() {- character- } else {- ' '- }- })- .collect::<String>();- if matches!(line.as_str(), BEGIN_CONTEXT_SENTINEL | END_CONTEXT_SENTINEL) {- format!("[source] {line}")- } else {- line+#[derive(Debug, Clone, Serialize, Deserialize)]+pub struct ReferencedPassage {+ pub reference: super::IncludedPassage,+ pub source: GroundedSource,+ pub passages: Vec<String>,+}++pub(crate) fn build_followup_prompt_context(+ referenced: &[ReferencedPassage],+ searched: &[GroundedExcerpt],+ max_utf8_bytes: usize,+) -> Result<Option<GroundedPromptContext>, RetrievalError> {+ if referenced.is_empty() {+ return build_grounded_prompt_context(searched, max_utf8_bytes);+ }+ let invalid = || {+ RetrievalError::InvalidInput("Previously requested evidence does not fit or is invalid; identify a narrower source or increase context".into())+ };+ if referenced.len() > MAX_GROUNDING_HITS {+ return Err(invalid());+ }+ let mut text = format!("{BEGIN_CONTEXT_SENTINEL}\n{CONTEXT_WARNING}");+ let footer = format!("\n{END_CONTEXT_SENTINEL}");+ let mut sources = Vec::new();+ let mut included_passages = Vec::new();+ let mut packs = 0;+ let mut notes = 0;+ for item in referenced {+ if included_passages.contains(&item.reference) {+ continue;+ }+ if !item.reference.valid_metadata()+ || !item.reference.locator.matches_source(&item.source)+ || item.reference.spans.len() != item.passages.len()+ {+ return Err(invalid());+ }+ for (span, passage) in item.reference.spans.iter().zip(&item.passages) {+ if passage.len() != (span.end_utf8 - span.start_utf8) as usize+ || super::passage::digest(passage.as_bytes()) != span.text_sha256+ {+ return Err(invalid());}- })- .collect::<Vec<_>>()- .join("\n")+ }+ match &item.source {+ GroundedSource::EnsuPack { .. } => packs += 1,+ GroundedSource::LocalNote { .. } => notes += 1,+ }+ if packs > MAX_PACK_HITS || notes > MAX_NOTES_GROUNDING_HITS {+ return Err(invalid());+ }+ let prefix = format!("\n\n# {}\n", grounded_label(&item.source)?);+ let passages = item.passages.join("\n[omitted source text]\n");+ if text+ .len()+ .saturating_add(prefix.len())+ .saturating_add(passages.len())+ .saturating_add(footer.len())+ > max_utf8_bytes+ {+ return Err(invalid());+ }+ text.push_str(&prefix);+ text.push_str(&passages);+ sources.push(item.source.clone());+ included_passages.push(item.reference.clone());+ }+ for item in searched {+ if included_passages+ .iter()+ .any(|reference| reference.locator == item.locator)+ {+ continue;+ }+ let is_pack = matches!(&item.source, GroundedSource::EnsuPack { .. });+ if (is_pack && packs >= MAX_PACK_HITS) || (!is_pack && notes >= MAX_NOTES_GROUNDING_HITS) {+ continue;+ }+ let prefix = format!("\n\n# {}\n", grounded_label(&item.source)?);+ let Some(available) = max_utf8_bytes.checked_sub(text.len() + prefix.len() + footer.len())+ else {+ continue;+ };+ if !item.score.is_finite() || !item.locator.matches_source(&item.source) {+ return Err(invalid());+ }+ let cleaned = super::clean_passage_text(&item.text);+ let passage = truncate_utf8(&cleaned, available).trim_end();+ if passage.is_empty() {+ continue;+ }+ text.push_str(&prefix);+ text.push_str(passage);+ sources.push(item.source.clone());+ included_passages.push(super::IncludedPassage::prefix(+ item.locator.clone(),+ passage,+ )?);+ if is_pack {+ packs += 1;+ } else {+ notes += 1;+ }+ }+ text.push_str(&footer);+ Ok(Some(GroundedPromptContext {+ text,+ sources,+ included_passages,+ }))}#[cfg(test)]@@ -261,6 +365,11 @@ mod tests {KnowledgePromptHit {dataset_id: dataset_id.to_string(),hit: RetrievalHit {+ locator: crate::retrieval::PassageLocator::EnsuPack {+ dataset_id: dataset_id.to_owned(),+ revision_sha256: "b".repeat(64),+ row: 0,+ },score,text: format!("{title} passage"),title: title.to_string(),@@ -272,6 +381,13 @@ mod tests {fn note(document_id: &str, score: f32) -> NotesSearchHit {NotesSearchHit {+ locator: crate::retrieval::PassageLocator::LocalNote {+ collection_id: COLLECTION.to_owned(),+ document_id: document_id.to_owned(),+ indexed_revision: "a".repeat(64),+ shard_sha256: "b".repeat(64),+ chunk_index: 0,+ },collection_id: COLLECTION.to_string(),document_id: document_id.to_string(),revision: "a".repeat(64),@@ -282,6 +398,14 @@ mod tests {}}+ #[test]+ fn mismatched_source_and_locator_are_rejected() {+ let candidates = select_mixed_grounding_candidates(&[], &[note("one.md", 0.9)], 5).unwrap();+ let mut wrong = candidates;+ wrong[0].locator = note("other.md", 0.9).locator;+ assert!(build_grounded_prompt_context(&wrong, 6000).is_err());+ }+#[test]fn selects_and_formats_mixed_grounding() {let packs = vec![@@ -348,4 +472,81 @@ mod tests {assert!(truncated.text.len() <= budget);assert!(!truncated.text.contains('🙂'));}++ fn previous() -> ReferencedPassage {+ let fixtures: serde_json::Value = serde_json::from_str(include_str!(+ "../../tests/fixtures/passage-references-v1.json"+ ))+ .unwrap();+ let reference: super::super::IncludedPassage =+ serde_json::from_value(fixtures[0]["reference"].clone()).unwrap();+ let source = GroundedSource::LocalNote {+ reference: NoteSourceReference {+ collection_id: "123e4567-e89b-12d3-a456-426614174000".into(),+ collection_label: None,+ document_id: "trip.md".into(),+ indexed_revision: "a".repeat(64),+ title: "Trip".into(),+ section: None,+ },+ };+ ReferencedPassage {+ passages: reference+ .verified_spans(fixtures[0]["raw"].as_str().unwrap())+ .unwrap(),+ reference,+ source,+ }+ }+ #[test]+ fn old_spans_keep_original_offsets_and_optional_search_hits_are_deduplicated() {+ let old = previous();+ let duplicate = GroundedExcerpt {+ locator: old.reference.locator.clone(),+ source: old.source.clone(),+ score: 1.0,+ text: "UNSUPPLIED source remainder".into(),+ };+ let mut fresh = duplicate.clone();+ if let super::super::PassageLocator::LocalNote { chunk_index, .. } = &mut fresh.locator {+ *chunk_index += 1;+ }+ fresh.text = "Fresh café🙂 context".into();+ let full = build_followup_prompt_context(+ std::slice::from_ref(&old),+ &[duplicate, fresh.clone()],+ 6000,+ )+ .unwrap()+ .unwrap();+ assert_eq!(full.included_passages.len(), 2);+ assert_eq!(full.included_passages[0], old.reference);+ assert!(full.text.contains("[omitted source text]"));+ assert!(!full.text.contains("UNSUPPLIED"));+ let required = build_followup_prompt_context(std::slice::from_ref(&old), &[], 6000)+ .unwrap()+ .unwrap();+ for budget in [+ required.text.len(),+ required.text.len() + 8,+ full.text.len() - 1,+ ] {+ let packed = build_followup_prompt_context(+ std::slice::from_ref(&old),+ std::slice::from_ref(&fresh),+ budget,+ )+ .unwrap()+ .unwrap();+ assert!(packed.text.len() <= budget);+ assert_eq!(packed.included_passages[0], old.reference);+ }+ assert!(build_followup_prompt_context(&[old], &[], required.text.len() - 1).is_err());+ }+ #[test]+ fn referenced_text_must_match_the_saved_digest() {+ let mut old = previous();+ old.passages[0] = "Changed text".into();+ assert!(build_followup_prompt_context(&[old], &[], 6000).is_err());+ }}diff --git a/rust/crates/ensu/src/retrieval/index.rs b/rust/crates/ensu/src/retrieval/index.rsindex b7b91074da..e29b096fdf 100644--- a/rust/crates/ensu/src/retrieval/index.rs+++ b/rust/crates/ensu/src/retrieval/index.rs@@ -19,6 +19,7 @@ const MAX_MANIFEST_BYTES: u64 = 1_048_576;const MAX_METADATA_FRAME_BYTES: u64 = 1_048_576;#[derive(Debug, Clone, PartialEq)]pub struct RetrievalHit {+ pub locator: super::PassageLocator,pub score: f32,pub text: String,pub title: String,@@ -54,6 +55,8 @@ struct MetadataRow {pub struct RetrievalIndex {dataset_identity: String,+ stable_id: String,+ revision_sha256: String,count: usize,dim: usize,scale: f32,@@ -98,7 +101,8 @@ impl RetrievalIndex {"manifest size is outside the supported range".to_string(),));}- let manifest: Manifest = serde_json::from_slice(&fs::read(&manifest_path)?)?;+ let manifest_bytes = fs::read(&manifest_path)?;+ let manifest: Manifest = serde_json::from_slice(&manifest_bytes)?;validate_manifest(&manifest, directory_identity, expected_pack)?;let count = usize::try_from(manifest.count).map_err(|_| {@@ -161,7 +165,6 @@ impl RetrievalIndex {.collect::<Vec<_>>();validate_offsets(&offsets, metadata_len)?;- // Native callers prevent file swaps or deletion while mapped.#[expect(unsafe_code,reason = "File-backed memory mapping requires an unsafe call"@@ -174,6 +177,8 @@ impl RetrievalIndex {let metadata = unsafe { MmapOptions::new().map(&metadata_file)? };Ok(Self {+ stable_id: expected_pack.stable_id.clone(),+ revision_sha256: text_revision_digest(&manifest_bytes, &metadata, &offsets_raw),dataset_identity: manifest.dataset,count,dim,@@ -254,6 +259,30 @@ impl RetrievalIndex {self.load_hits(&ranked)}+ pub fn reload_passage(+ &self,+ locator: &super::PassageLocator,+ ) -> Result<Option<RetrievalHit>, RetrievalError> {+ let super::PassageLocator::EnsuPack {+ dataset_id,+ revision_sha256,+ row,+ } = locator+ else {+ return Ok(None);+ };+ if dataset_id != &self.stable_id || revision_sha256 != &self.revision_sha256 {+ return Ok(None);+ }+ let Ok(row) = usize::try_from(*row) else {+ return Ok(None);+ };+ if row >= self.count {+ return Ok(None);+ }+ Ok(self.load_hits(&[RankedRow { score: 0.0, row }])?.pop())+ }+fn load_hits(&self, ranked: &[RankedRow]) -> Result<Vec<RetrievalHit>, RetrievalError> {let mut by_block = BTreeMap::<usize, Vec<(usize, RankedRow)>>::new();for (rank, selected) in ranked.iter().copied().enumerate() {@@ -306,6 +335,11 @@ impl RetrievalIndex {));}hits[rank] = Some(RetrievalHit {+ locator: super::PassageLocator::EnsuPack {+ dataset_id: self.stable_id.clone(),+ revision_sha256: self.revision_sha256.clone(),+ row: selected.row as u64,+ },score: selected.score,text,title,@@ -507,6 +541,21 @@ fn python_quote(value: &str) -> String {encoded}+fn text_revision_digest(manifest: &[u8], metadata: &[u8], offsets: &[u8]) -> String {+ use sha2::{Digest, Sha256};+ let mut revision = Sha256::new();+ revision.update(b"ensu-pack-text-revision-v1\0");+ for bytes in [manifest, metadata, offsets] {+ revision.update((bytes.len() as u64).to_le_bytes());+ revision.update(bytes);+ }+ revision+ .finalize()+ .iter()+ .map(|byte| format!("{byte:02x}"))+ .collect()+}+fn sanitize_prompt_text(value: &str) -> String {let normalized_line_endings = value.replace("\r\n", "\n").replace('\r', "\n");let normalized = normalized_line_endings@@ -615,6 +664,56 @@ pub(super) mod tests {}}+ #[test]+ fn passage_reload_survives_reopen_and_rejects_changed_text_artifacts() {+ use crate::retrieval::PassageLocator;+ let pack = synthetic_pack("simplewiki-test");+ let index = RetrievalIndex::open(&pack.revision, &pack.expected).unwrap();+ let mut query = vec![0.0; 512];+ query[0] = 1.0;+ let hits = index.search(&query, 3, 0.0).unwrap();+ assert_ne!(hits[0].locator, hits[1].locator);+ drop(index);+ let index = RetrievalIndex::open(&pack.revision, &pack.expected).unwrap();+ for hit in &hits {+ let reloaded = index.reload_passage(&hit.locator).unwrap().unwrap();+ assert_eq!(reloaded.locator, hit.locator);+ assert_eq!(reloaded.text, hit.text);+ assert_eq!(reloaded.source_url, hit.source_url);+ }+ let mut wrong = hits[0].locator.clone();+ if let PassageLocator::EnsuPack { row, .. } = &mut wrong {+ *row = u64::MAX;+ }+ assert!(index.reload_passage(&wrong).unwrap().is_none());+ wrong = hits[0].locator.clone();+ if let PassageLocator::EnsuPack { dataset_id, .. } = &mut wrong {+ *dataset_id = "wikibooks".to_string();+ }+ assert!(index.reload_passage(&wrong).unwrap().is_none());+ drop(index);++ let metadata_path = pack.revision.join(KNOWLEDGE_META_FILE);+ let bytes = fs::read(&metadata_path).unwrap();+ let decoded = zstd::stream::decode_all(bytes.as_slice()).unwrap();+ let changed = String::from_utf8(decoded)+ .unwrap()+ .replace("first passage", "new passage");+ let compressed = zstd::stream::encode_all(changed.as_bytes(), 1).unwrap();+ fs::write(metadata_path, &compressed).unwrap();+ fs::write(+ pack.revision.join(KNOWLEDGE_OFFSETS_FILE),+ [0_u64, compressed.len() as u64]+ .into_iter()+ .flat_map(u64::to_le_bytes)+ .collect::<Vec<_>>(),+ )+ .unwrap();+ let index = RetrievalIndex::open(&pack.revision, &pack.expected).unwrap();+ assert!(index.reload_passage(&hits[0].locator).unwrap().is_none());+ assert_eq!(index.search(&query, 1, 0.0).unwrap()[0].text, "new passage");+ }+#[test]fn opens_and_searches_a_synthetic_pack_in_rank_order() {let pack = synthetic_pack("simplewiki-test");diff --git a/rust/crates/ensu/src/retrieval/passage.rs b/rust/crates/ensu/src/retrieval/passage.rsnew file mode 100644index 0000000000..4d66d3b9a8--- /dev/null+++ b/rust/crates/ensu/src/retrieval/passage.rs@@ -0,0 +1,259 @@+use serde::{Deserialize, Serialize};+use sha2::{Digest, Sha256};++use super::{BEGIN_CONTEXT_SENTINEL, END_CONTEXT_SENTINEL, GroundedSource, RetrievalError};++#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(+ tag = "type",+ rename_all = "camelCase",+ rename_all_fields = "camelCase"+)]+pub enum PassageLocator {+ EnsuPack {+ dataset_id: String,+ revision_sha256: String,+ row: u64,+ },+ LocalNote {+ collection_id: String,+ document_id: String,+ indexed_revision: String,+ shard_sha256: String,+ chunk_index: u64,+ },+}++impl PassageLocator {+ pub(crate) fn matches_source(&self, source: &GroundedSource) -> bool {+ match (self, source) {+ (Self::EnsuPack { dataset_id, .. }, GroundedSource::EnsuPack { citation }) => {+ *dataset_id == citation.dataset_id+ }+ (+ Self::LocalNote {+ collection_id,+ document_id,+ indexed_revision,+ ..+ },+ GroundedSource::LocalNote { reference },+ ) => {+ *collection_id == reference.collection_id+ && *document_id == reference.document_id+ && *indexed_revision == reference.indexed_revision+ }+ _ => false,+ }+ }+}++#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct PassageSpan {+ pub start_utf8: u32,+ pub end_utf8: u32,+ pub text_sha256: String,+}++#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct IncludedPassage {+ pub locator: PassageLocator,+ pub cleaning_version: u32,+ pub spans: Vec<PassageSpan>,+}++const PASSAGE_CLEANING_VERSION: u32 = 1;+const MAX_PASSAGE_SPANS: usize = 8;+const MAX_PASSAGE_UTF8_BYTES: usize = 64 * 1024;++impl IncludedPassage {+ pub(crate) fn valid_metadata(&self) -> bool {+ let sha = |value: &str| {+ value.len() == 64+ && value+ .bytes()+ .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))+ };+ if self.cleaning_version != PASSAGE_CLEANING_VERSION+ || self.spans.is_empty()+ || self.spans.len() > MAX_PASSAGE_SPANS+ {+ return false;+ }+ let valid_locator = match &self.locator {+ PassageLocator::EnsuPack {+ dataset_id,+ revision_sha256,+ ..+ } => !dataset_id.is_empty() && dataset_id.len() <= 256 && sha(revision_sha256),+ PassageLocator::LocalNote {+ collection_id,+ document_id,+ indexed_revision,+ shard_sha256,+ ..+ } => {+ uuid::Uuid::parse_str(collection_id).is_ok()+ && crate::notes::validate_document_id(document_id).is_ok()+ && sha(indexed_revision)+ && sha(shard_sha256)+ }+ };+ let mut previous_end = 0;+ valid_locator+ && self.spans.iter().all(|span| {+ let valid = span.start_utf8 >= previous_end+ && span.start_utf8 < span.end_utf8+ && span.end_utf8 as usize <= MAX_PASSAGE_UTF8_BYTES+ && sha(&span.text_sha256);+ previous_end = span.end_utf8;+ valid+ })+ }++ pub(super) fn prefix(locator: PassageLocator, text: &str) -> Result<Self, RetrievalError> {+ let end_utf8 = u32::try_from(text.len()).map_err(|_| {+ RetrievalError::InvalidInput("included passage is too large".to_string())+ })?;+ Ok(Self {+ locator,+ cleaning_version: PASSAGE_CLEANING_VERSION,+ spans: vec![PassageSpan {+ start_utf8: 0,+ end_utf8,+ text_sha256: digest(text.as_bytes()),+ }],+ })+ }++ pub fn verified_spans(&self, reloaded_text: &str) -> Option<Vec<String>> {+ if self.cleaning_version != PASSAGE_CLEANING_VERSION+ || self.spans.is_empty()+ || self.spans.len() > MAX_PASSAGE_SPANS+ || reloaded_text.len() > MAX_PASSAGE_UTF8_BYTES+ {+ return None;+ }+ let cleaned = clean_passage_text(reloaded_text);+ let mut end = 0;+ let mut texts = Vec::with_capacity(self.spans.len());+ for span in &self.spans {+ let start = usize::try_from(span.start_utf8).ok()?;+ let next_end = usize::try_from(span.end_utf8).ok()?;+ if start < end || start >= next_end {+ return None;+ }+ let text = cleaned.get(start..next_end)?;+ if digest(text.as_bytes()) != span.text_sha256 {+ return None;+ }+ texts.push(text.to_owned());+ end = next_end;+ }+ Some(texts)+ }+}++pub(super) fn digest(bytes: &[u8]) -> String {+ Sha256::digest(bytes)+ .iter()+ .map(|byte| format!("{byte:02x}"))+ .collect()+}++pub(crate) fn clean_passage_text(value: &str) -> String {+ value+ .replace("\r\n", "\n")+ .replace('\r', "\n")+ .lines()+ .map(|line| {+ let line = line+ .chars()+ .map(|character| {+ if character == '\t' || !character.is_control() {+ character+ } else {+ ' '+ }+ })+ .collect::<String>();+ let start = line.trim_start();+ if start.starts_with(BEGIN_CONTEXT_SENTINEL) || start.starts_with(END_CONTEXT_SENTINEL)+ {+ format!("[source] {line}")+ } else {+ line+ }+ })+ .collect::<Vec<_>>()+ .join("\n")+ .trim()+ .to_owned()+}++#[cfg(test)]+mod tests {+ use super::*;++ #[derive(Deserialize)]+ struct Fixture {+ name: String,+ raw: String,+ cleaned: String,+ reference: IncludedPassage,+ texts: Vec<String>,+ }++ #[test]+ fn shared_passage_reference_fixtures() {+ let fixtures: Vec<Fixture> = serde_json::from_str(include_str!(+ "../../tests/fixtures/passage-references-v1.json"+ ))+ .unwrap();+ for fixture in fixtures {+ assert_eq!(+ clean_passage_text(&fixture.raw),+ fixture.cleaned,+ "{}",+ fixture.name+ );+ assert_eq!(+ fixture.reference.verified_spans(&fixture.raw),+ Some(fixture.texts),+ "{}",+ fixture.name+ );+ }+ }++ #[test]+ fn refuses_changed_text_invalid_offsets_and_unknown_versions() {+ let fixtures: Vec<Fixture> = serde_json::from_str(include_str!(+ "../../tests/fixtures/passage-references-v1.json"+ ))+ .unwrap();+ let fixture = &fixtures[0];+ assert!(+ fixture+ .reference+ .verified_spans(&fixture.raw.replace("Café", "Cafe"))+ .is_none()+ );+ for (start, end) in [(0, 4), (4, 9), (9, 0), (0, u32::MAX)] {+ let mut reference = fixture.reference.clone();+ reference.spans[0].start_utf8 = start;+ reference.spans[0].end_utf8 = end;+ assert!(reference.verified_spans(&fixture.raw).is_none());+ }+ let mut reference = fixture.reference.clone();+ reference.cleaning_version = 2;+ assert!(reference.verified_spans(&fixture.raw).is_none());+ reference = fixture.reference.clone();+ reference.spans[1] = reference.spans[0].clone();+ assert!(reference.verified_spans(&fixture.raw).is_none());+ reference.spans = vec![reference.spans[0].clone(); 9];+ assert!(reference.verified_spans(&fixture.raw).is_none());+ }+}diff --git a/rust/crates/ensu/src/retrieval/prompt.rs b/rust/crates/ensu/src/retrieval/prompt.rsindex dbc37f164c..7e45c1f899 100644--- a/rust/crates/ensu/src/retrieval/prompt.rs+++ b/rust/crates/ensu/src/retrieval/prompt.rs@@ -113,6 +113,11 @@ mod tests {KnowledgePromptHit {dataset_id: dataset_id.to_owned(),hit: RetrievalHit {+ locator: crate::retrieval::PassageLocator::EnsuPack {+ dataset_id: dataset_id.to_owned(),+ revision_sha256: "b".repeat(64),+ row: 0,+ },score: 0.9,text: text.to_owned(),title: "Example title".to_owned(),diff --git a/rust/crates/ensu/tests/fixtures/passage-references-v1.json b/rust/crates/ensu/tests/fixtures/passage-references-v1.jsonnew file mode 100644index 0000000000..ad039dbc94--- /dev/null+++ b/rust/crates/ensu/tests/fixtures/passage-references-v1.json@@ -0,0 +1,86 @@+[+ {+ "name": "multilingual-disjoint",+ "raw": " \r\nCafé🙂\r\nबीच\tend \u0000\r\n",+ "cleaned": "Café🙂\nबीच\tend",+ "reference": {+ "locator": {+ "type": "localNote",+ "collectionId": "123e4567-e89b-12d3-a456-426614174000",+ "documentId": "trip.md",+ "indexedRevision": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ "shardSha256": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",+ "chunkIndex": 2+ },+ "cleaningVersion": 1,+ "spans": [+ {+ "startUtf8": 0,+ "endUtf8": 9,+ "textSha256": "894473efc373309a7c6d18cbe8504c8bb91e09cba4e1ab147ff97af12d481702"+ },+ {+ "startUtf8": 20,+ "endUtf8": 23,+ "textSha256": "361e48d0308f20e32dba5fb56328baf18d72ef0ccb43b84f5c262d2a6a1fc6c8"+ }+ ]+ },+ "texts": [+ "Café🙂",+ "end"+ ]+ },+ {+ "name": "sentinel",+ "raw": "\r\n----- BEGIN KNOWLEDGE CONTEXT -----\r\nx\r\n",+ "cleaned": "[source] ----- BEGIN KNOWLEDGE CONTEXT -----\nx",+ "reference": {+ "locator": {+ "type": "localNote",+ "collectionId": "123e4567-e89b-12d3-a456-426614174000",+ "documentId": "trip.md",+ "indexedRevision": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ "shardSha256": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",+ "chunkIndex": 2+ },+ "cleaningVersion": 1,+ "spans": [+ {+ "startUtf8": 0,+ "endUtf8": 43,+ "textSha256": "73c093471df3c5f2f0e96999d8d5ebc745a803f55d3f36c4523c502c2e055e02"+ }+ ]+ },+ "texts": [+ "[source] ----- BEGIN KNOWLEDGE CONTEXT ----"+ ]+ },+ {+ "name": "controls",+ "raw": " a\u0001b\tc\rd ",+ "cleaned": "a b\tc\nd",+ "reference": {+ "locator": {+ "type": "localNote",+ "collectionId": "123e4567-e89b-12d3-a456-426614174000",+ "documentId": "trip.md",+ "indexedRevision": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ "shardSha256": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",+ "chunkIndex": 2+ },+ "cleaningVersion": 1,+ "spans": [+ {+ "startUtf8": 0,+ "endUtf8": 7,+ "textSha256": "1205bffe81cb4550cdf063c07e75e290c2ebaf60d77780a338e18eb99d26bd4c"+ }+ ]+ },+ "texts": [+ "a b\tc\nd"+ ]+ }+]diff --git a/web/apps/ensu/src/components/chat/ChatMessageList.tsx b/web/apps/ensu/src/components/chat/ChatMessageList.tsxindex 3009a31270..508c6580c0 100644--- a/web/apps/ensu/src/components/chat/ChatMessageList.tsx+++ b/web/apps/ensu/src/components/chat/ChatMessageList.tsx@@ -18,6 +18,7 @@ import {Copy01Icon,Edit01Icon,RepeatIcon,+ Search01Icon,} from "@hugeicons/core-free-icons";import { HugeiconsIcon } from "@hugeicons/react";import {@@ -102,7 +103,8 @@ export interface ChatMessageListProps {) => void;onEditMessage: (message: ChatMessage) => void;onCopyMessage: (text: string) => void;- onRetryMessage: (message: ChatMessage) => void;+ onRetryMessage: (message: ChatMessage, searchAsWritten?: boolean) => void;+ canSearchSources: boolean;onPrevBranch: (switcher: BranchSwitcher) => void;onNextBranch: (switcher: BranchSwitcher) => void;onRequestPreview: (attachment: ChatAttachment, sessionUuid: string) => void;@@ -417,7 +419,8 @@ interface MessageRowProps {) => void;onEditMessage: (message: ChatMessage) => void;onCopyMessage: (text: string) => void;- onRetryMessage: (message: ChatMessage) => void;+ onRetryMessage: (message: ChatMessage, searchAsWritten?: boolean) => void;+ canSearchSources: boolean;onPrevBranch: (switcher: BranchSwitcher) => void;onNextBranch: (switcher: BranchSwitcher) => void;attachmentPreviews: Record<string, string>;@@ -451,6 +454,7 @@ const MessageRow = memo(onEditMessage,onCopyMessage,onRetryMessage,+ canSearchSources,onPrevBranch,onNextBranch,attachmentPreviews,@@ -776,6 +780,24 @@ const MessageRow = memo(</IconButton></>)}+ {!isStreaming &&+ !isSelf &&+ (!isSynthetic || isLastMessage) &&+ canSearchSources && (+ <IconButton+ aria-label="Search as written"+ title="Search as written"+ sx={actionButtonSx}+ onClick={() =>+ onRetryMessage(message, true)+ }+ >+ <HugeiconsIcon+ icon={Search01Icon}+ {...actionIconProps}+ />+ </IconButton>+ )}</Stack>{isStreaming || isSynthetic ? null : (@@ -937,6 +959,7 @@ export const ChatMessageList = memo(onEditMessage,onCopyMessage,onRetryMessage,+ canSearchSources,onPrevBranch,onNextBranch,onRequestPreview,@@ -1003,6 +1026,7 @@ export const ChatMessageList = memo(onEditMessage={onEditMessage}onCopyMessage={onCopyMessage}onRetryMessage={onRetryMessage}+ canSearchSources={canSearchSources}onPrevBranch={onPrevBranch}onNextBranch={onNextBranch}attachmentPreviews={attachmentPreviews}@@ -1029,6 +1053,7 @@ export const ChatMessageList = memo(assistantMarkdownSx,attachmentPreviews,branchSwitchers,+ canSearchSources,dialogCloseButtonSx,dialogCloseIconProps,dialogTitleSx,diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsxindex 4d7ef2c6ff..add8c1c578 100644--- a/web/apps/ensu/src/pages/chat.tsx+++ b/web/apps/ensu/src/pages/chat.tsx@@ -52,7 +52,9 @@ import {import { DEFAULT_WEB_CONTEXT_SIZE } from "@/services/llm/budget";import {prepareDesktopConversation,+ resolveDesktopSourceFollowup,type PreparedReply,+ type ResolvedSourceFollowup,} from "@/services/llm/conversation";import { stripHiddenPartsText } from "@/services/llm/history-text";import {@@ -2848,6 +2850,12 @@ const Page: React.FC = () => {activeKnowledgeSources,));+ if (assistantMessage.evidenceSaved === false) {+ log.warn(+ "Reply saved, but its source references could not be retained",+ );+ }+await updateBranchSelectionState(parentMessageUuid,assistantMessage.messageUuid,@@ -2932,6 +2940,7 @@ const Page: React.FC = () => {sessionUuid,imagePaths,mediaMarker,+ searchAsWritten = false,}: {promptText: string;parentMessage: ChatMessage;@@ -2941,6 +2950,7 @@ const Page: React.FC = () => {sessionUuid?: string;imagePaths?: string[];mediaMarker?: string;+ searchAsWritten?: boolean;}): Promise<void> => {const parentMessageUuid = parentMessage.messageUuid;const activeSessionId =@@ -3092,6 +3102,16 @@ const Page: React.FC = () => {.text.replaceAll(MEDIA_MARKER, "").replace(/\[\d+ image attachments? provided\]/gi, "").trim();+ const selectedMessages = slicePathUntil(+ historyPath,+ stopAtMessageUuid,+ );+ const selectedPath = selectedMessages.map(+ (message) => message.messageUuid,+ );+ if (selectedPath.at(-1) !== parentMessageUuid)+ selectedPath.push(parentMessageUuid);+ let followup: ResolvedSourceFollowup | undefined;const remainingKnowledgeBytes = useConversationMemory? 6000: Math.max(@@ -3138,6 +3158,47 @@ const Page: React.FC = () => {}if (!isActiveGeneration()) return;+ if (+ useConversationMemory &&+ !searchAsWritten &&+ selectedMessages.some((message) => message.sources?.length)+ ) {+ setConversationStatus("Finding sources");+ try {+ followup = await provider.withKnowledgeRetrieval(+ (cancellationEpoch) =>+ resolveDesktopSourceFollowup({+ sessionUuid: activeSessionId,+ path: selectedPath,+ question: knowledgeQuery,+ enabledStableIds: enabledReadyPackIds,+ cancellationEpoch,+ candidates: knowledgeContext?.candidates,+ }),+ isActiveGeneration,+ );+ if (!isActiveGeneration()) return;+ knowledgeContext = {+ text: "",+ sources: [],+ candidates: followup.candidates,+ };+ } catch (error) {+ const { name } = tauriCommandError(error);+ if (+ !isActiveGeneration() ||+ name === "cancelled" ||+ name === "stale"+ )+ return;+ log.warn(+ "Source followup failed; continuing with available sources",+ error,+ );+ }+ setConversationStatus(null);+ }+await provider.ensureModelReady(settings);setLoadedModelName(provider.getCurrentModel()?.name ?? null);setIsDownloading(false);@@ -3207,6 +3268,7 @@ const Page: React.FC = () => {historyQuery: knowledgeQuery,maxTokens,groundingCandidates: knowledgeContext?.candidates,+ resolutionToken: followup?.token,},provider,{@@ -3355,6 +3417,12 @@ const Page: React.FC = () => {));if (!isActiveGeneration()) return;+ if (assistantMessage.evidenceSaved === false) {+ log.warn(+ "Reply saved, but its source references could not be retained",+ );+ }+void updateBranchSelectionState(parentMessageUuid,assistantMessage.messageUuid,@@ -3433,11 +3501,12 @@ const Page: React.FC = () => {enabledKnowledgePackIds,knowledgePacks,loadEnabledKnowledgeCatalogOnce,+ slicePathUntil,],);const handleRetryMessage = useCallback(- async (message: ChatMessage) => {+ async (message: ChatMessage, searchAsWritten = false) => {if (message.sender !== "assistant") return;if (!chatKey || !currentSessionId) return;if (isDownloading) {@@ -3480,6 +3549,7 @@ const Page: React.FC = () => {stopAtMessageUuid: parentUuid,resetContext: true,sessionUuid: currentSessionId,+ searchAsWritten,});},[@@ -4654,6 +4724,19 @@ const Page: React.FC = () => {onEditMessage={handleEditMessage}onCopyMessage={handleCopyMessage}onRetryMessage={handleRetryMessage}+ canSearchSources={+ isTauriRuntime &&+ (notesCollections.length > 0 ||+ knowledgePacks.some(+ (pack) =>+ enabledKnowledgePackIds.has(+ pack.stableId,+ ) &&+ (pack.status === "ready" ||+ pack.status ===+ "updateAvailable"),+ ))+ }onPrevBranch={handlePrevBranch}onNextBranch={handleNextBranch}onRequestPreview={loadAttachmentPreview}diff --git a/web/apps/ensu/src/services/chat/store.ts b/web/apps/ensu/src/services/chat/store.tsindex 4b7ae133a2..a5ee07c269 100644--- a/web/apps/ensu/src/services/chat/store.ts+++ b/web/apps/ensu/src/services/chat/store.ts@@ -116,6 +116,7 @@ export interface ChatMessage {createdAt: number;attachments?: ChatAttachment[];sources?: GroundedSource[];+ evidenceSaved?: boolean;isSynthetic?: boolean;}@@ -143,6 +144,7 @@ interface NativeMessage {createdAt: number;attachments?: NativeAttachment[];sources?: GroundedSource[];+ evidenceSaved?: boolean;}const nowMicros = () => Date.now() * 1000;@@ -428,6 +430,7 @@ const listMessagesNative = async (size: attachment.size,})),sources: message.sources,+ evidenceSaved: message.evidenceSaved,}));};@@ -500,6 +503,7 @@ const addMessageNative = async (size: attachment.size,})),sources: message.sources,+ evidenceSaved: message.evidenceSaved,};};diff --git a/web/apps/ensu/src/services/llm/conversation.ts b/web/apps/ensu/src/services/llm/conversation.tsindex 8a7cf48d30..932c5e38b1 100644--- a/web/apps/ensu/src/services/llm/conversation.ts+++ b/web/apps/ensu/src/services/llm/conversation.ts@@ -29,6 +29,7 @@ export const prepareDesktopConversation = async (historyQuery?: string;groundingCandidates?: unknown;maxTokens: number;+ resolutionToken?: string;},provider: Pick<LlmProvider, "generateChatStream">,callbacks: { isCurrent: () => boolean; onProgress: () => void },@@ -84,3 +85,20 @@ export const prepareDesktopConversation = async (unlisten();}};++export interface ResolvedSourceFollowup {+ token: string;+ candidates: unknown;+}++export const resolveDesktopSourceFollowup = (input: {+ sessionUuid: string;+ path: string[];+ question: string;+ enabledStableIds: string[];+ cancellationEpoch: number;+ candidates?: unknown;+}): Promise<ResolvedSourceFollowup> =>+ invoke<ResolvedSourceFollowup>("conversation_resolve_followup", {+ input: { ...input, token: crypto.randomUUID() },+ });