π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[ensu] Add local notes support on Android and iOS
setalpaba8d4a0c1d7
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 79e86dfe11..c00fb92e07 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@@ -17,6 +17,7 @@ import io.ente.ensu.chat.Attachmentimport io.ente.ensu.chat.ChatMessageimport io.ente.ensu.bindings.ConfigDefaultsimport io.ente.ensu.logging.LogLevel+import io.ente.ensu.notes.NotesStoreimport io.ente.ensu.settings.SessionPreferencesDataStoreimport io.ente.ensu.AppStateimport io.ente.ensu.settings.DeveloperSettingsState@@ -45,6 +46,8 @@ class AppStore(private val _state = MutableStateFlow(AppState())val state: StateFlow<AppState> = _state.asStateFlow()+ val notesStore = NotesStore(context, llmProvider, logRepository).also { llmProvider.modelMaintenance = it }+private val messageStore = mutableMapOf<String, MutableList<ChatMessage>>()private val attachmentActions = AttachmentStoreActions(_state, messageStore)private val knowledgeStore = KnowledgeStore(@@ -68,9 +71,11 @@ class AppStore(attachmentActions = attachmentActions,modelSettingsActions = modelSettingsActions,configDefaults = configDefaults,+ notesStore = notesStore,awaitKnowledgeReady = knowledgeStore::awaitEnabledPacksReady)fun bootstrap(scope: CoroutineScope) {+ notesStore.bootstrap(scope)chatActions.setScope(scope)modelSettingsActions.setScope(scope)refreshDeviceCapability(scope)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 a8a2ab4d25..55c514b07a 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@@ -26,6 +26,7 @@ import androidx.navigation.NavHostControllerimport com.google.accompanist.navigation.animation.AnimatedNavHostimport com.google.accompanist.navigation.animation.composableimport io.ente.ensu.chat.ChatView+import io.ente.ensu.notes.NotesSettingsScreenimport io.ente.ensu.settings.AdvancedSettingsDataStoreimport io.ente.ensu.designsystem.EnsuColorimport io.ente.ensu.chat.Attachment@@ -92,6 +93,9 @@ internal fun HomeNavigation(HomeRoute.Settings -> {SimpleTopBar(title = "Settings") { navController.popBackStack() }}+ HomeRoute.Notes -> {+ SimpleTopBar(title = "Your Notes") { navController.popBackStack() }+ }HomeRoute.Knowledge -> {SimpleTopBar(title = "Ensu Packs") { navController.popBackStack() }}@@ -152,6 +156,7 @@ internal fun HomeNavigation(isAdvancedUnlocked = appState.developerSettings.isAdvancedUnlocked,onOpenLogs = { navController.navigate(HomeRoute.Logs) },onOpenKnowledge = { navController.navigate(HomeRoute.Knowledge) },+ onOpenNotes = { navController.navigate(HomeRoute.Notes) },onOpenModelSettings = { navController.navigate(HomeRoute.ModelSettings) },onOpenSystemPromptSettings = { navController.navigate(HomeRoute.SystemPromptSettings) },onUnlockAdvanced = {@@ -161,6 +166,15 @@ internal fun HomeNavigation(onSignIn = onSignIn)}+ composable(+ route = HomeRoute.Notes,+ enterTransition = { forwardEnter() },+ exitTransition = { forwardExit() },+ popEnterTransition = { backEnter() },+ popExitTransition = { backExit() }+ ) {+ NotesSettingsScreen(store.notesStore)+ }composable(route = HomeRoute.Knowledge,enterTransition = { forwardEnter() },@@ -244,6 +258,7 @@ internal object HomeRoute {const val ModelSettings = "model-settings"const val SystemPromptSettings = "system-prompt-settings"const val Knowledge = "knowledge"+ const val Notes = "notes"}internal fun AnimatedContentTransitionScope<NavBackStackEntry>.forwardEnter() =diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.ktindex e445dac765..748db89e75 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt@@ -30,6 +30,7 @@ import androidx.compose.material3.ModalNavigationDrawerimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composable+import androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.runtime.DisposableEffectimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.derivedStateOf@@ -50,6 +51,7 @@ import com.google.accompanist.navigation.animation.rememberAnimatedNavControllerimport io.ente.ensu.chat.SessionDrawerimport io.ente.ensu.components.ImageAttachmentPreviewDialogimport io.ente.ensu.components.NativeChoiceDialog+import io.ente.ensu.notes.LocalNotesStoreimport io.ente.ensu.settings.AdvancedSettingsDataStoreimport io.ente.ensu.logging.FileLogRepositoryimport io.ente.ensu.storage.FilePathManager@@ -82,7 +84,7 @@ fun HomeView(advancedSettingsDataStore: AdvancedSettingsDataStore,appVersion: String,configDefaults: ConfigDefaults-) {+) = CompositionLocalProvider(LocalNotesStore provides store.notesStore) {val drawerState = androidx.compose.material3.rememberDrawerState(DrawerValue.Closed)val scope = rememberCoroutineScope()val context = androidx.compose.ui.platform.LocalContext.current@@ -177,9 +179,13 @@ fun HomeView(DisposableEffect(lifecycleOwner) {val observer = LifecycleEventObserver { _, event ->if (event == Lifecycle.Event.ON_RESUME) {+ latestStore.notesStore.setForeground(true)latestStore.refreshModelDownloadInfo()+ } else if (event == Lifecycle.Event.ON_STOP) {+ latestStore.notesStore.setForeground(false)}}+ latestStore.notesStore.setForeground(lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))lifecycleOwner.lifecycle.addObserver(observer)onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }}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 8608cc0a5d..260e0c70be 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@@ -11,8 +11,6 @@ import androidx.compose.foundation.clickableimport androidx.compose.foundation.gestures.detectTapGesturesimport androidx.compose.foundation.gestures.scrollByimport androidx.compose.foundation.isSystemInDarkTheme-import androidx.compose.foundation.rememberScrollState-import androidx.compose.foundation.verticalScrollimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Box@@ -35,7 +33,6 @@ import androidx.compose.foundation.layout.wrapContentSizeimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.itemsimport androidx.compose.foundation.lazy.rememberLazyListState-import androidx.compose.material3.AlertDialogimport androidx.compose.material3.Buttonimport androidx.compose.material3.ButtonDefaultsimport androidx.compose.material3.DropdownMenuItem@@ -44,7 +41,7 @@ import androidx.compose.material3.Iconimport androidx.compose.material3.LinearProgressIndicatorimport androidx.compose.material3.Surfaceimport androidx.compose.material3.Text-import androidx.compose.material3.TextButton+import androidx.compose.runtime.collectAsStateimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.derivedStateOf@@ -68,6 +65,9 @@ import androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport androidx.compose.ui.window.Popupimport androidx.compose.ui.window.PopupProperties+import io.ente.ensu.components.AttributionDialog+import io.ente.ensu.components.AttributionLink+import io.ente.ensu.components.KnowledgeCardimport io.ente.ensu.components.BranchSwitcherimport io.ente.ensu.components.ImageAttachmentThumbnailimport io.ente.ensu.designsystem.EnsuColor@@ -81,6 +81,7 @@ import io.ente.ensu.chat.ChatMessageimport io.ente.ensu.chat.MessageAuthorimport io.ente.ensu.format.formatBytesimport io.ente.ensu.format.formattedFileSize+import io.ente.ensu.notes.LocalNotesStoreimport io.ente.ensu.platform.rememberHapticsimport kotlinx.coroutines.delayimport kotlinx.coroutines.flow.collect@@ -91,8 +92,8 @@ import java.util.Localeimport kotlin.math.roundToIntimport kotlin.math.roundToLongimport io.ente.ensu.llm.DownloadPhase-import io.ente.ensu.bindings.SourceCitation-import io.ente.ensu.bindings.parseAssistantText+import io.ente.ensu.bindings.GroundedSource+import io.ente.ensu.bindings.parseGroundedAssistantText@Composableinternal fun MessageList(@@ -622,6 +623,7 @@ private fun UserMessageBubble(}}+@OptIn(ExperimentalLayoutApi::class)@Composableprivate fun AssistantMessageBubble(message: ChatMessage,@@ -632,7 +634,7 @@ private fun AssistantMessageBubble(showsMetadata: Boolean) {val clipboard = LocalClipboardManager.current- val parsed = remember(message.text) { parseAssistantText(message.text) }+ val parsed = remember(message.text) { parseGroundedAssistantText(message.text) }val haptic = rememberHaptics()var showMenu by remember { mutableStateOf(false) }var showSources by remember { mutableStateOf(false) }@@ -677,22 +679,29 @@ private fun AssistantMessageBubble(enableSelection = false)- parsed.sourceLabel?.let { label ->+ if (parsed.sourceLabels.isNotEmpty()) {Spacer(modifier = Modifier.height(EnsuSpacing.sm.dp))- Surface(- color = EnsuColor.fillFaint(),- shape = RoundedCornerShape(EnsuCornerRadius.button.dp),- modifier = Modifier.clickable { showSources = true }+ FlowRow(+ horizontalArrangement = Arrangement.spacedBy(EnsuSpacing.sm.dp),+ verticalArrangement = Arrangement.spacedBy(EnsuSpacing.xs.dp)) {- Text(- text = label,- style = EnsuTypography.small,- color = EnsuColor.textPrimary(),- modifier = Modifier.padding(- horizontal = EnsuSpacing.md.dp,- vertical = EnsuSpacing.sm.dp- )- )+ parsed.sourceLabels.forEach { label ->+ Surface(+ onClick = { showSources = true },+ color = EnsuColor.fillFaint(),+ shape = RoundedCornerShape(EnsuCornerRadius.button.dp)+ ) {+ Text(+ text = label,+ style = EnsuTypography.small,+ color = EnsuColor.textPrimary(),+ modifier = Modifier.padding(+ horizontal = EnsuSpacing.md.dp,+ vertical = EnsuSpacing.sm.dp+ )+ )+ }+ }}}@@ -743,98 +752,68 @@ private fun AssistantMessageBubble(if (showSources) {KnowledgeSourcesDialog(- citations = parsed.citations,+ citations = parsed.sources,onDismiss = { showSources = false })}}}+@OptIn(ExperimentalLayoutApi::class)@Composableprivate fun KnowledgeSourcesDialog(- citations: List<SourceCitation>,+ citations: List<GroundedSource>,onDismiss: () -> Unit) {val uriHandler = LocalUriHandler.current- AlertDialog(- onDismissRequest = onDismiss,- title = { Text("Sources", style = EnsuTypography.h3) },- text = {- Column(- modifier = Modifier.verticalScroll(rememberScrollState()),- verticalArrangement = Arrangement.spacedBy(EnsuSpacing.lg.dp)- ) {+ val notes = LocalNotesStore.current+ val notesState = if (citations.any { it is GroundedSource.LocalNote }) notes?.state?.collectAsState()?.value else null+ AttributionDialog(title = "Sources", onDismiss = onDismiss) {+ notesState?.error?.let { error ->+ Text(error, style = EnsuTypography.small, color = EnsuColor.error)+ }+ citations.forEachIndexed { index, source ->+ val note = (source as? GroundedSource.LocalNote)?.reference+ val citation = (source as? GroundedSource.EnsuPack)?.citation+ KnowledgeCard(padding = EnsuSpacing.md.dp, spacing = EnsuSpacing.sm.dp) {Text(- text = if (citations.size == 1) {- "1 source used in this response"- } else {- "${citations.size} sources used in this response"+ text = buildString {+ append("SOURCE ${index + 1} Β· ")+ if (note != null) {+ append("YOUR NOTES")+ note.collectionLabel?.let { append(" Β· ${it.uppercase()}") }+ } else if (citation != null) {+ append("ENSU PACK Β· ${citation.datasetLabel.uppercase()}")+ }},+ style = EnsuTypography.mini,+ color = EnsuColor.textMuted()+ )+ Text(+ text = note?.title ?: citation?.title.orEmpty(),+ style = EnsuTypography.large,+ color = EnsuColor.textPrimary()+ )+ note?.section?.takeIf { it.isNotBlank() }?.let {+ Text(it, style = EnsuTypography.small, color = EnsuColor.textMuted())+ }+ HorizontalDivider(color = EnsuColor.border())+ Text(+ text = note?.documentId ?: citation?.credit.orEmpty(),style = EnsuTypography.small,color = EnsuColor.textMuted())-- citations.forEachIndexed { index, citation ->- Column(- modifier = Modifier- .fillMaxWidth()- .background(- EnsuColor.fillFaint(),- RoundedCornerShape(EnsuCornerRadius.card.dp)- )- .padding(EnsuSpacing.md.dp),- verticalArrangement = Arrangement.spacedBy(EnsuSpacing.sm.dp)- ) {- Text(- text = "SOURCE ${index + 1} Β· ${citation.datasetLabel.uppercase()}",- style = EnsuTypography.mini,- color = EnsuColor.textMuted()- )- Text(- text = citation.title,- style = EnsuTypography.large,- color = EnsuColor.textPrimary()- )- HorizontalDivider(color = EnsuColor.border())- Text(- text = "Attribution",- style = EnsuTypography.mini,- color = EnsuColor.textMuted()- )- Text(- citation.credit,- style = EnsuTypography.small,- color = EnsuColor.textPrimary()- )- Row(horizontalArrangement = Arrangement.spacedBy(EnsuSpacing.lg.dp)) {- SourceAttributionLink("Open source β") {- uriHandler.openUri(citation.sourceUrl)- }- SourceAttributionLink("${citation.licenseLabel} β") {- uriHandler.openUri(citation.licenseUrl)- }- }+ FlowRow(horizontalArrangement = Arrangement.spacedBy(EnsuSpacing.lg.dp)) {+ if (note != null) {+ AttributionLink("Open note β") { notes?.open(note) }+ } else if (citation != null) {+ AttributionLink("Open source β") { uriHandler.openUri(citation.sourceUrl) }+ AttributionLink("${citation.licenseLabel} β") { uriHandler.openUri(citation.licenseUrl) }}}}- },- confirmButton = {- TextButton(onClick = onDismiss) { Text("Done") }- },- containerColor = EnsuColor.backgroundBase()- )-}--@Composable-private fun SourceAttributionLink(label: String, onClick: () -> Unit) {- Text(- text = label,- style = EnsuTypography.small,- color = EnsuColor.accent(),- modifier = Modifier- .clickable(onClick = onClick)- .padding(vertical = EnsuSpacing.xs.dp)- )+ }+ }}private data class MessageAction(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 e5a4f0233b..b8be4da805 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@@ -20,12 +20,14 @@ import io.ente.ensu.bindings.ConfigDefaultsimport io.ente.ensu.logging.LogLevelimport io.ente.ensu.chat.MessageAuthorimport io.ente.ensu.chat.sessionTitleFromText+import io.ente.ensu.notes.NotesStoreimport io.ente.ensu.settings.SessionPreferencesDataStoreimport io.ente.ensu.AppState-import io.ente.ensu.bindings.SourceCitation-import io.ente.ensu.bindings.buildKnowledgePromptContext+import io.ente.ensu.bindings.GroundedSource+import io.ente.ensu.bindings.selectMixedGroundingCandidates+import io.ente.ensu.bindings.buildGroundedPromptContextimport io.ente.ensu.bindings.cleanAssistantText-import io.ente.ensu.bindings.finalizeAssistantText+import io.ente.ensu.bindings.finalizeGroundedAssistantTextimport io.ente.ensu.knowledge.KnowledgeProviderimport java.text.SimpleDateFormatimport java.util.Date@@ -56,6 +58,7 @@ internal class ChatStoreActions(private val attachmentActions: AttachmentStoreActions,private val modelSettingsActions: ModelSettingsActions,private val configDefaults: ConfigDefaults,+ private val notesStore: NotesStore,private val awaitKnowledgeReady: suspend () -> Unit) {private val branchSelections = mutableMapOf<String, MutableMap<String, String>>()@@ -478,29 +481,35 @@ internal class ChatStoreActions(}rebuildChatState(sessionId)+ val notesScope = notesStore.suspendMaintenance()val activeJob = scope.launch {+ notesStore.awaitMaintenance()priorGeneration?.join()priorSummary?.join()val isActive = { isGenerationActive(generationToken, sessionId) }if (!isActive()) return@launchawaitKnowledgeReady()+ notesStore.awaitReady()if (!isActive()) return@launchval progressTracker = DownloadProgressTracker()var embeddingAssetInvalid = falseval enabledDatasets = state.value.knowledge.enabledReadyDatasetsval knowledgeHits = if (- userMessage.text.isNotBlank() && enabledDatasets.isNotEmpty()+ userMessage.text.isNotBlank() &&+ (enabledDatasets.isNotEmpty() || notesStore.state.value.collections.any { it.eligible })) {try {val hits = llmProvider.withChatModelReleasedForRetrieval { embed ->if (!isActive()) throw kotlinx.coroutines.CancellationException()val query = embed(userMessage.text.trim())if (!isActive()) throw kotlinx.coroutines.CancellationException()- knowledgeProvider.search(+ val packs = knowledgeProvider.search(datasets = enabledDatasets,query = query,maxHits = configDefaults.knowledgeEmbedding.maxHits)+ val notes = notesStore.retrieve(query)+ notesStore.verify(selectMixedGroundingCandidates(packs, notes, notes.size.toUInt()))}if (!isActive()) return@launchhits@@ -607,8 +616,8 @@ internal class ChatStoreActions(.coerceAtLeast(0) * 4 - 2).coerceAtLeast(0)val knowledgeContext = runCatching {- buildKnowledgePromptContext(- hits = knowledgeHits,+ buildGroundedPromptContext(+ excerpts = knowledgeHits,maxUtf8Bytes = min(configDefaults.knowledgeEmbedding.maxContextUtf8Bytes.toInt(),remainingKnowledgeBytes@@ -634,7 +643,7 @@ internal class ChatStoreActions(} else {normalHistorySelection}- var activeCitations = if (useKnowledge) knowledgeContext?.citations.orEmpty() else emptyList()+ var activeCitations = if (useKnowledge) knowledgeContext?.sources.orEmpty() else emptyList()val systemPrompt = if (useKnowledge) candidateSystemPrompt ?: normalSystemPrompt else normalSystemPromptval systemMessage = LlmMessage(text = systemPrompt,@@ -724,6 +733,7 @@ internal class ChatStoreActions(}generationJob = activeJobactiveJob.invokeOnCompletion {+ notesScope.close()scope.launch {settleGenerationIfActive(generationToken, sessionId)}@@ -738,11 +748,11 @@ internal class ChatStoreActions(totalTimeMs: Long?,interrupted: Boolean,shouldUpdateUi: Boolean,- citations: List<SourceCitation> = emptyList()+ citations: List<GroundedSource> = emptyList()) {val rawText = buffer.toString().trim()val finalText = if (rawText.isNotEmpty()) {- runCatching { finalizeAssistantText(rawText, citations) }.getOrElse { error ->+ runCatching { finalizeGroundedAssistantText(rawText, citations) }.getOrElse { error ->logRepository.log(LogLevel.Warning,"Assistant source finalization failed",@@ -750,7 +760,7 @@ internal class ChatStoreActions(tag = "Chat",throwable = error)- runCatching { finalizeAssistantText(rawText, emptyList()) }.getOrDefault("")+ runCatching { finalizeGroundedAssistantText(rawText, emptyList()) }.getOrDefault("")}} else {rawText@@ -875,7 +885,9 @@ internal class ChatStoreActions(val summaryInput = buildSessionSummaryInput(sessionId) ?: returnval selection = modelSettingsActions.resolveSelection(state.value.modelSettings)+ val notesScope = notesStore.suspendMaintenance()sessionSummaryJob = scope.launch(Dispatchers.Default) {+ notesStore.awaitMaintenance()val summary = generateSessionSummary(input = summaryInput.text,fallback = summaryInput.fallback,@@ -885,7 +897,7 @@ internal class ChatStoreActions(withContext(Dispatchers.Main) {applySessionSummary(sessionId, summary)}- }+ }.also { it.invokeOnCompletion { notesScope.close() } }}private data class SessionSummaryInput(val text: String, val fallback: String)@@ -928,6 +940,7 @@ internal class ChatStoreActions(val buffer = StringBuilder()try {+ llmProvider.ensureModelReady(selection) {}llmProvider.generateChat(selection = selection,messages = messages,diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.ktindex 417b699c0b..8832143bc4 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt@@ -21,6 +21,9 @@ import io.ente.ensu.bindings.TranscriptionExceptionimport io.ente.ensu.bindings.transcriptionModelAssetimport io.ente.ensu.bindings.voiceActivityModelAssetimport io.ente.ensu.assets.AssetStore+import io.ente.ensu.notes.LocalNotesStore+import io.ente.ensu.llm.ModelMaintenance+import io.ente.ensu.llm.withMaintenanceSuspendedimport kotlinx.coroutines.CancellationExceptionimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchers@@ -74,8 +77,9 @@ internal fun rememberVoiceTranscriptionController(onTranscript: (String) -> Unit): VoiceTranscriptionController {val lifecycleOwner = LocalLifecycleOwner.current- val controller = remember(transcriber) {- VoiceTranscriptionController(assetStore, transcriber, onTranscript)+ val notes = LocalNotesStore.current+ val controller = remember(transcriber, notes) {+ VoiceTranscriptionController(assetStore, transcriber, onTranscript, notes)}DisposableEffect(controller, lifecycleOwner) {val observer = LifecycleEventObserver { _, event ->@@ -97,7 +101,8 @@ internal fun rememberVoiceTranscriptionController(internal class VoiceTranscriptionController(private val assetStore: AssetStore,private val transcriber: Transcriber,- private val onTranscript: (String) -> Unit+ private val onTranscript: (String) -> Unit,+ private val maintenance: ModelMaintenance?) {private val modelAssets = listOf(transcriptionModelAsset(),@@ -280,7 +285,7 @@ internal class VoiceTranscriptionController(audioRecord = null}- private suspend fun transcribeRecording(pcm: ByteArray, sampleRate: Int) {+ private suspend fun transcribeRecording(pcm: ByteArray, sampleRate: Int) = maintenance.withMaintenanceSuspended {try {ensureTranscriptionModelDownloaded()awaitTranscriptionModelPreload()@@ -311,14 +316,14 @@ internal class VoiceTranscriptionController(} catch (error: AssetDownloadException) {if (error is AssetDownloadException.Cancelled) {state = VoiceInputState.Idle- return+ return@withMaintenanceSuspended}Log.w(TAG, "Voice model download failed: ${error.message}", error)state = VoiceInputState.Error(downloadErrorMessage())} catch (error: LlmException) {if (error is LlmException.Cancelled) {state = VoiceInputState.Idle- return+ return@withMaintenanceSuspended}Log.w(TAG, "Voice model download failed: ${error.message}", error)state = VoiceInputState.Error(downloadErrorMessage())@@ -336,7 +341,7 @@ internal class VoiceTranscriptionController(transcriptionPreloadJob?.cancel()transcriptionPreloadJob = scope.launch(Dispatchers.IO) {try {- transcriber.loadModel()+ maintenance.withMaintenanceSuspended { transcriber.loadModel() }} catch (error: CancellationException) {throw error} catch (error: Throwable) {diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/components/ActionButtons.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/components/ActionButtons.ktindex a0944a20aa..b56933a992 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/components/ActionButtons.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/components/ActionButtons.kt@@ -90,6 +90,20 @@ fun TextActionButton(}}+@Composable+fun CompactButton(label: String, onClick: () -> Unit) {+ Button(+ onClick = onClick,+ colors = ButtonDefaults.buttonColors(containerColor = EnsuColor.accent()),+ contentPadding = PaddingValues(+ horizontal = EnsuSpacing.md.dp,+ vertical = EnsuSpacing.xs.dp+ )+ ) {+ Text(label, style = EnsuTypography.mini)+ }+}+@Composablefun PrimaryButton(text: String,diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/components/KnowledgeComponents.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/components/KnowledgeComponents.ktnew file mode 100644index 0000000000..a292399a8e--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/components/KnowledgeComponents.kt@@ -0,0 +1,70 @@+package io.ente.ensu.components++import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.ColumnScope+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.rememberScrollState+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.foundation.verticalScroll+import androidx.compose.material3.AlertDialog+import androidx.compose.material3.Text+import androidx.compose.material3.TextButton+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.dp+import io.ente.ensu.designsystem.EnsuColor+import io.ente.ensu.designsystem.EnsuCornerRadius+import io.ente.ensu.designsystem.EnsuSpacing+import io.ente.ensu.designsystem.EnsuTypography++@Composable+internal fun KnowledgeCard(+ padding: Dp = EnsuSpacing.lg.dp,+ spacing: Dp = 0.dp,+ content: @Composable ColumnScope.() -> Unit+) {+ Column(+ modifier = Modifier+ .fillMaxWidth()+ .background(EnsuColor.fillFaint(), RoundedCornerShape(EnsuCornerRadius.card.dp))+ .padding(padding),+ verticalArrangement = Arrangement.spacedBy(spacing),+ content = content+ )+}++@Composable+internal fun AttributionDialog(+ title: String,+ onDismiss: () -> Unit,+ content: @Composable ColumnScope.() -> Unit+) {+ AlertDialog(+ onDismissRequest = onDismiss,+ title = { Text(title, style = EnsuTypography.h3) },+ text = {+ Column(+ modifier = Modifier.verticalScroll(rememberScrollState()),+ verticalArrangement = Arrangement.spacedBy(EnsuSpacing.md.dp),+ content = content+ )+ },+ confirmButton = { TextButton(onClick = onDismiss) { Text("Done") } },+ containerColor = EnsuColor.backgroundBase()+ )+}++@Composable+internal fun AttributionLink(label: String, onClick: () -> Unit) {+ TextButton(+ onClick = onClick,+ contentPadding = PaddingValues(vertical = EnsuSpacing.xs.dp)+ ) {+ Text(label, style = EnsuTypography.mini, color = EnsuColor.accent())+ }+}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/designsystem/HugeIcons.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/designsystem/HugeIcons.ktindex d28d63c32b..2da14081ef 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/designsystem/HugeIcons.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/designsystem/HugeIcons.kt@@ -9,6 +9,7 @@ object HugeIcons {val Bug01Icon = R.drawable.hugeicon_bug01val Cancel01Icon = R.drawable.hugeicon_cancel01val Copy01Icon = R.drawable.hugeicon_copy01+ val Folder01Icon = R.drawable.hugeicon_folder01val Delete01Icon = R.drawable.hugeicon_delete01val Edit01Icon = R.drawable.hugeicon_edit01val Menu01Icon = R.drawable.hugeicon_menu01diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.ktindex 3df30be935..5e90fda840 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt@@ -54,6 +54,11 @@ class LlmProvider(private val modelLoadMutex = Mutex()private val activeDownloads = AtomicInteger()private val embeddingAsset = knowledgeEmbeddingModelAsset()+ internal var modelMaintenance: ModelMaintenance? = null++ private suspend fun <T> withModelContext(block: suspend () -> T): T = modelMaintenance.withMaintenanceSuspended {+ withContext(ioDispatcher) { block() }+ }class EmbeddingAssetInvalid : Exception("Embedding model asset is invalid")@@ -76,7 +81,7 @@ class LlmProvider(selection: LlmModelSelection,onProgress: (DownloadProgress) -> Unit) {- withContext(ioDispatcher) {+ withModelContext {modelLoadMutex.withLock {ensureModelReadyLocked(selection, onProgress)}@@ -87,7 +92,7 @@ class LlmProvider(selection: LlmModelSelection,onProgress: (DownloadProgress) -> Unit) {- withContext(ioDispatcher) {+ withModelContext {deviceCapabilityProvider.chatCapability().requireChatSupported()val asset = chatAsset(selection)val missingAssets = modelLoadMutex.withLock {@@ -132,7 +137,7 @@ class LlmProvider(temperature: Float,maxTokens: Int?,onToken: (String) -> Unit- ): GenerationSummary = withContext(ioDispatcher) {+ ): GenerationSummary = withModelContext {modelLoadMutex.withLock {deviceCapabilityProvider.chatCapability().requireChatSupported()val context = loadedContext ?: throw IllegalStateException("Model context not loaded")@@ -173,8 +178,16 @@ class LlmProvider(suspend fun <T> withChatModelReleasedForRetrieval(block: suspend (embed: (String) -> List<Float>) -> T+ ): T = withModelContext {+ withEmbeddingContext { embedding -> block(embedding::embed) }+ }++ internal suspend fun <T> withEmbeddingContext(+ checkCancellation: () -> Unit = {},+ block: suspend (LlmContext) -> T): T = withContext(ioDispatcher) {modelLoadMutex.withLock {+ checkCancellation()deviceCapabilityProvider.chatCapability().requireChatSupported()if (!isEmbeddingModelReady()) throw EmbeddingAssetInvalid()unloadTranscriptionModelIfLoaded()@@ -196,7 +209,7 @@ class LlmProvider(try {val threads = max(1, Runtime.getRuntime().availableProcessors() - 1)embeddingContext = embeddingModel.newEmbeddingContext(threads)- block { text -> embeddingContext.embed(text) }+ block(embeddingContext)} finally {embeddingContext?.destroy()embeddingModel.destroy()@@ -205,7 +218,7 @@ class LlmProvider(}suspend fun prewarmImageInference(selection: LlmModelSelection) {- withContext(ioDispatcher) {+ withModelContext {runCatching {modelLoadMutex.withLock {val asset = chatAsset(selection)@@ -244,7 +257,7 @@ class LlmProvider(}suspend fun resetContext() {- withContext(ioDispatcher) {+ withModelContext {modelLoadMutex.withLock {val model = loadedModel ?: return@withLockval contextParams = LlmContextParams(diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelMaintenance.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelMaintenance.ktnew file mode 100644index 0000000000..c1030d750a--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelMaintenance.kt@@ -0,0 +1,17 @@+package io.ente.ensu.llm++interface ModelMaintenance {+ fun suspendMaintenance(): AutoCloseable+ suspend fun awaitMaintenance()+ fun modelReadinessChanged()+}++internal suspend fun <T> ModelMaintenance?.withMaintenanceSuspended(block: suspend () -> T): T {+ val token = this?.suspendMaintenance()+ try {+ this?.awaitMaintenance()+ return block()+ } finally {+ token?.close()+ }+}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.ktindex 95f7c74d9b..598fb40920 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt@@ -60,6 +60,7 @@ internal class ModelSettingsActions(}fun refreshModelDownloadInfo() {+ llmProvider.modelMaintenance?.modelReadinessChanged()if (!state.value.chat.deviceCapability.isChatSupported()) {modelDownloadJob?.cancel()persistModelDownloadRequested(false)diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.ktnew file mode 100644index 0000000000..a30ea2983e--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt@@ -0,0 +1,298 @@+package io.ente.ensu.notes++import android.content.ContentResolver+import android.net.Uri+import android.os.Build+import android.os.CancellationSignal+import android.provider.DocumentsContract+import io.ente.ensu.bindings.NotesCancellationInterface+import io.ente.ensu.bindings.NotesDocument+import io.ente.ensu.bindings.NotesException+import io.ente.ensu.bindings.NotesRead+import io.ente.ensu.bindings.NotesSource+import io.ente.ensu.bindings.notesLimits+import io.ente.ensu.bindings.validateNotesDocumentId+import java.io.ByteArrayOutputStream+import java.io.FileNotFoundException+import java.nio.ByteBuffer+import java.nio.channels.FileChannel+import java.util.concurrent.atomic.AtomicReference++private const val MAX_SCAN_ENTRIES = 250_000++internal class AndroidNotesSource(+ private val resolver: ContentResolver,+ private val tree: Uri,+ private val cancellation: NotesCancellationInterface+) : NotesSource {+ @Volatile private var signal: CancellationSignal? = null+ private val activeRead = AtomicReference<FileChannel?>()+ private val rootId = DocumentsContract.getTreeDocumentId(tree)+ private val limits = notesLimits()+ private val directories = mutableMapOf<String, Map<String, Entry>>()+ private var supportsPath = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O++ data class Entry(+ val id: String,+ val name: String,+ val directory: Boolean,+ val size: Long?,+ val modified: Long?,+ val virtual: Boolean+ )++ fun cancel() {+ cancellation.cancel()+ try {+ signal?.cancel()+ } finally {+ runCatching { activeRead.getAndSet(null)?.close() }+ }+ }++ private fun uri(id: String) = DocumentsContract.buildDocumentUriUsingTree(tree, id)++ private fun query(target: Uri): List<Entry> = access { request ->+ val columns = arrayOf(+ DocumentsContract.Document.COLUMN_DOCUMENT_ID,+ DocumentsContract.Document.COLUMN_DISPLAY_NAME,+ DocumentsContract.Document.COLUMN_MIME_TYPE,+ DocumentsContract.Document.COLUMN_SIZE,+ DocumentsContract.Document.COLUMN_LAST_MODIFIED,+ DocumentsContract.Document.COLUMN_FLAGS+ )+ resolver.query(target, columns, null, null, null, request)?.use { cursor ->+ val entries = mutableListOf<Entry>()+ while (cursor.moveToNext()) {+ request.throwIfCanceled()+ if (entries.size % 64 == 0) cancellation.check()+ if (entries.size >= MAX_SCAN_ENTRIES) throw NotesException.InvalidInput("The Notes folder contains too many entries")+ val id = cursor.getString(0) ?: throw NotesException.SourceRead("Missing document identity")+ val name = cursor.getString(1) ?: throw NotesException.SourceRead("Missing document name")+ entries += Entry(+ id = id,+ name = name,+ directory = cursor.getString(2) == DocumentsContract.Document.MIME_TYPE_DIR,+ size = if (cursor.isNull(3)) null else cursor.getLong(3).takeIf { it >= 0 },+ modified = if (cursor.isNull(4)) null else cursor.getLong(4).takeIf { it > 0 },+ virtual = !cursor.isNull(5) && cursor.getInt(5) and DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT != 0+ )+ }+ if (cursor.extras.containsKey(DocumentsContract.EXTRA_ERROR)) {+ throw NotesException.SourceRead("Could not load the complete Notes folder. Please try again.")+ }+ if (cursor.extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {+ throw NotesException.SourceRead("The folder is still loading. Please try again.")+ }+ entries+ } ?: throw NotesException.SourceRead("Could not read the Notes folder")+ }++ private fun children(id: String): Map<String, Entry> = directories.getOrPut(id) {+ queryChildren(id)+ }++ private fun queryChildren(id: String): Map<String, Entry> {+ val entries = query(DocumentsContract.buildChildDocumentsUriUsingTree(tree, id))+ if (entries.map { it.name }.toSet().size != entries.size) {+ throw NotesException.SourceRead("The folder contains duplicate document names")+ }+ return entries.associateBy { it.name }+ }++ fun sameRoot(other: AndroidNotesSource): Boolean =+ tree.authority == other.tree.authority && rootId == other.rootId++ fun root(): Entry =+ query(uri(rootId)).singleOrNull()?.takeIf { it.directory } ?: throw NotesException.Unavailable()++ fun contains(other: AndroidNotesSource): Boolean {+ if (tree.authority != other.tree.authority) return false+ val pending = ArrayDeque<String>()+ val seen = mutableSetOf<String>()+ var count = 0+ pending.add(rootId)+ while (pending.isNotEmpty()) {+ cancellation.check()+ val id = pending.removeFirst()+ if (id == other.rootId) return true+ if (!seen.add(id)) throw NotesException.SourceRead("Ambiguous directory structure")+ for (child in children(id).values) {+ if (++count > MAX_SCAN_ENTRIES) throw NotesException.InvalidInput("The Notes folder contains too many entries")+ if (child.id == other.rootId) return true+ if (child.directory) pending.add(child.id)+ }+ }+ return false+ }++ override fun listDocuments(): List<NotesDocument> {+ root()+ val pending = ArrayDeque<Pair<String, String>>()+ val seen = mutableSetOf<String>()+ val documents = mutableListOf<NotesDocument>()+ var entries = 0+ var total = 0L+ pending.add(rootId to "")+ while (pending.isNotEmpty()) {+ cancellation.check()+ val (directory, prefix) = pending.removeFirst()+ if (!seen.add(directory)) throw NotesException.SourceRead("Ambiguous directory structure")+ for (child in children(directory).values) {+ if (++entries > MAX_SCAN_ENTRIES) throw NotesException.InvalidInput("The Notes folder contains too many entries")+ if (child.name.startsWith(".")) continue+ if (!child.directory && (child.virtual || !supported(child.name))) continue+ if (child.name.contains("/")) continue+ val id = prefix + child.name+ try {+ validateNotesDocumentId(id)+ } catch (_: NotesException.InvalidInput) {+ continue+ }+ if (child.directory) {+ pending.add(child.id to "$id/")+ } else {+ if (child.size != null && child.size > limits.maxSourceBytes.toLong()) continue+ val size = child.size ?: try {+ bytes(child.id).size.toLong()+ } catch (_: TooLarge) {+ continue+ }+ total += size+ if (documents.size.toULong() >= limits.maxCollectionDocuments || total.toULong() > limits.maxCollectionSourceBytes) {+ throw NotesException.InvalidInput("The Notes folder is too large to index")+ }+ documents += NotesDocument(id, size.toULong(), child.modified)+ }+ }+ }+ return documents.sortedBy { it.documentId }+ }++ private fun resolve(documentId: String): Entry {+ validateNotesDocumentId(documentId)+ root()+ var directory = rootId+ val expectedPath = mutableListOf(rootId)+ val parts = documentId.split("/")+ for ((index, name) in parts.withIndex()) {+ if (name.startsWith(".")) throw NotesException.SourceChanged()+ val expected = children(directory)[name] ?: throw NotesException.SourceChanged()+ val entry = query(uri(expected.id)).singleOrNull() ?: throw NotesException.SourceChanged()+ if (entry.id != expected.id || entry.name != expected.name || entry.directory != expected.directory) {+ throw NotesException.SourceChanged()+ }+ expectedPath += entry.id+ if (index == parts.lastIndex) {+ if (entry.directory || entry.virtual || !supported(name)) throw NotesException.SourceChanged()+ if (!verifyPath(entry.id, expectedPath)) {+ verifyListedPath(expectedPath, parts)+ }+ return entry+ }+ if (!entry.directory) throw NotesException.SourceChanged()+ directory = entry.id+ }+ throw NotesException.SourceChanged()+ }++ private fun verifyPath(id: String, expected: List<String>): Boolean {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || !supportsPath) return false+ return access {+ try {+ val path = DocumentsContract.findDocumentPath(resolver, uri(id))+ ?: throw NotesException.SourceRead("Could not verify the note location. Please try again.")+ if (path.path != expected) throw NotesException.SourceChanged()+ true+ } catch (_: UnsupportedOperationException) {+ supportsPath = false+ false+ }+ }+ }++ private fun verifyListedPath(expected: List<String>, names: List<String>) {+ for ((index, name) in names.withIndex()) {+ val entry = queryChildren(expected[index])[name] ?: throw NotesException.SourceChanged()+ if (entry.id != expected[index + 1]) throw NotesException.SourceChanged()+ }+ }++ override fun readDocument(documentId: String): NotesRead {+ val before = resolve(documentId)+ val bytes = try {+ bytes(before.id)+ } catch (_: TooLarge) {+ throw NotesException.SourceChanged()+ }+ val after = resolve(documentId)+ if (before != after || (after.size != null && after.size != bytes.size.toLong())) {+ throw NotesException.SourceChanged()+ }+ return NotesRead(bytes, NotesDocument(documentId, bytes.size.toULong(), after.modified))+ }++ private fun bytes(id: String): ByteArray = access { request ->+ val descriptor = resolver.openAssetFileDescriptor(uri(id), "r", request)+ ?: throw NotesException.SourceRead("Could not read the note")+ descriptor.use {+ cancellation.check()+ it.createInputStream().use { input ->+ val channel = input.channel+ activeRead.set(channel)+ try {+ cancellation.check()+ val output = ByteArrayOutputStream()+ val buffer = ByteArray(8192)+ val destination = ByteBuffer.wrap(buffer)+ var remaining = descriptor.declaredLength.takeIf { length -> length >= 0 } ?: Long.MAX_VALUE+ while (remaining > 0) {+ cancellation.check()+ destination.clear()+ destination.limit(minOf(buffer.size.toLong(), remaining).toInt())+ val count = channel.read(destination)+ if (count < 0) {+ if (descriptor.parcelFileDescriptor.canDetectErrors()) {+ descriptor.parcelFileDescriptor.checkError()+ }+ break+ }+ if (output.size() + count > limits.maxSourceBytes.toLong()) throw TooLarge()+ output.write(buffer, 0, count)+ remaining -= count+ }+ output.toByteArray()+ } finally {+ activeRead.compareAndSet(channel, null)+ }+ }+ }+ }++ private fun <T> access(block: (CancellationSignal) -> T): T {+ cancellation.check()+ val request = CancellationSignal()+ signal = request+ try {+ cancellation.check()+ return block(request).also { cancellation.check() }+ } catch (e: NotesException) {+ throw e+ } catch (e: TooLarge) {+ throw e+ } catch (_: SecurityException) {+ throw NotesException.Unavailable()+ } catch (_: FileNotFoundException) {+ cancellation.check()+ throw NotesException.SourceRead("The note is currently unavailable. Please try again.")+ } catch (_: Exception) {+ cancellation.check()+ throw NotesException.SourceRead("Could not access the Notes folder. Please try again.")+ } finally {+ signal = null+ }+ }++ private class TooLarge : Exception()+ private fun supported(name: String) = name.endsWith(".md", true) || name.endsWith(".markdown", true)+}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesMaintenanceGate.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesMaintenanceGate.ktnew file mode 100644index 0000000000..803ae190c3--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesMaintenanceGate.kt@@ -0,0 +1,29 @@+package io.ente.ensu.notes++internal class NotesMaintenanceGate<T> {+ private var suspensions = 0+ @Volatile var active: T? = null+ private set++ val available: Boolean+ @Synchronized get() = suspensions == 0 && active == null++ @Synchronized fun admit(run: T): Boolean {+ if (!available) return false+ active = run+ return true+ }++ @Synchronized fun suspend(): T? {+ suspensions+++ return active+ }++ @Synchronized fun resume() {+ suspensions--+ }++ @Synchronized fun finish(run: T) {+ if (active === run) active = null+ }+}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.ktnew file mode 100644index 0000000000..ad56218c60--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesProvider.kt@@ -0,0 +1,224 @@+package io.ente.ensu.notes++import android.content.Context+import android.content.Intent+import android.net.Uri+import android.provider.DocumentsContract+import android.util.AtomicFile+import androidx.core.content.FileProvider+import io.ente.ensu.bindings.LlmContext+import io.ente.ensu.bindings.NoteSourceReference+import io.ente.ensu.bindings.NotesCancellation+import io.ente.ensu.bindings.NotesCollection+import io.ente.ensu.bindings.NotesException+import io.ente.ensu.bindings.NotesIndexOptions+import io.ente.ensu.bindings.NotesProgressCallback+import io.ente.ensu.bindings.notesContentRevision+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.withContext+import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.withLock+import org.json.JSONArray+import org.json.JSONObject+import java.io.ByteArrayOutputStream+import java.io.File+import java.util.UUID++internal data class NotesRegistration(+ val id: String,+ val label: String+)++class NotesProvider(private val context: Context) {+ private data class Registration(val id: String, val label: String, val tree: String) {+ fun summary() = NotesRegistration(id, label)+ }++ private val registrations = linkedMapOf<String, Registration>()+ private val root = File(context.noBackupFilesDir, "notes")+ private val registry = AtomicFile(File(root, "collections.json"))+ private val diskGate = Mutex()++ private suspend fun <T> withAccess(block: suspend () -> T): T = withContext(Dispatchers.IO) {+ diskGate.withLock { block() }+ }++ internal suspend fun load(): List<NotesRegistration> = withAccess {+ val records = loadRegistry()+ registrations.clear()+ registrations.putAll(records.associateBy { it.id })+ records.map { it.summary() }+ }++ internal suspend fun inspect(id: String) = withAccess {+ registration(id)+ handle(id).use { it.inspect() }+ }++ internal suspend fun add(tree: Uri): NotesRegistration = withAccess {+ val alreadyGranted = context.contentResolver.persistedUriPermissions.any {+ it.uri == tree && it.isReadPermission+ }+ var committed = false+ try {+ context.contentResolver.takePersistableUriPermission(tree, Intent.FLAG_GRANT_READ_URI_PERMISSION)+ NotesCancellation().use { token ->+ val source = AndroidNotesSource(context.contentResolver, tree, token)+ val label = source.root().name+ for (registered in registrations.values) {+ val other = AndroidNotesSource(context.contentResolver, Uri.parse(registered.tree), token)+ val overlaps = source.sameRoot(other) || source.contains(other) || try {+ other.contains(source)+ } catch (_: NotesException.Unavailable) {+ false+ } catch (_: NotesException.SourceRead) {+ false+ }+ if (overlaps) {+ throw NotesException.InvalidInput("This folder overlaps a folder already added to Your Notes.")+ }+ }+ val record = Registration(UUID.randomUUID().toString(), label, tree.toString())+ save(registrations.values.toList() + record)+ committed = true+ registrations[record.id] = record+ record.summary()+ }+ } catch (error: Exception) {+ if (!committed && !alreadyGranted) {+ runCatching {+ context.contentResolver.releasePersistableUriPermission(tree, Intent.FLAG_GRANT_READ_URI_PERMISSION)+ }+ }+ throw error+ }+ }++ internal suspend fun remove(id: String) = withAccess {+ val record = registration(id)+ val remaining = registrations.values.filter { it.id != id }+ save(remaining)+ registrations.remove(id)+ runCatching { handle(id).use { it.remove() } }+ if (remaining.none { it.tree == record.tree }) {+ runCatching {+ context.contentResolver.releasePersistableUriPermission(+ Uri.parse(record.tree), Intent.FLAG_GRANT_READ_URI_PERMISSION+ )+ }+ }+ }++ internal suspend fun search(id: String, query: List<Float>) = withAccess {+ registration(id)+ handle(id).use { it.search(query) }+ }++ internal suspend fun inspectFreshness(id: String, cancel: NotesCancellation) = withAccess {+ withSource(registration(id), cancel) { source ->+ handle(id).use { it.inspectFreshness(source, cancel) }+ }+ }++ internal suspend fun index(+ id: String,+ embedding: LlmContext,+ cancel: NotesCancellation,+ progress: NotesProgressCallback,+ options: NotesIndexOptions+ ) = withAccess {+ withSource(registration(id), cancel) { source ->+ handle(id).use { it.index(source, embedding, cancel, progress, options) }+ }+ }++ private suspend fun <T> withSource(+ record: Registration,+ cancel: NotesCancellation,+ block: (AndroidNotesSource) -> T+ ): T {+ val source = AndroidNotesSource(context.contentResolver, Uri.parse(record.tree), cancel)+ return runNotesRead(source::cancel) { block(source) }+ }++ internal suspend fun verify(reference: NoteSourceReference) = withAccess {+ readVerified(registration(reference.collectionId), reference)+ Unit+ }++ private suspend fun readVerified(record: Registration, reference: NoteSourceReference): ByteArray =+ NotesCancellation().use { cancel ->+ val bytes = withSource(record, cancel) { it.readDocument(reference.documentId) }.bytes+ if (notesContentRevision(bytes) != reference.indexedRevision) throw NotesException.SourceChanged()+ bytes+ }++ internal suspend fun preview(reference: NoteSourceReference): Uri = withAccess {+ val bytes = readVerified(registration(reference.collectionId), reference)+ val previews = File(context.cacheDir, "note-previews").apply { mkdirs() }+ previews.listFiles()?.filter { it.lastModified() < System.currentTimeMillis() - 86_400_000L }+ ?.forEach { it.deleteRecursively() }+ val folder = File(previews, UUID.randomUUID().toString()).apply { mkdirs() }+ val file = File(folder, reference.documentId.substringAfterLast('/')).apply { writeBytes(bytes) }+ FileProvider.getUriForFile(context, context.packageName + ".fileprovider", file)+ }++ private fun registration(id: String): Registration =+ registrations[id] ?: throw NotesException.Unavailable()++ private fun handle(id: String) = NotesCollection(root.absolutePath, id)++ private fun loadRegistry(): List<Registration> {+ if (!registry.baseFile.exists() && !File(registry.baseFile.path + ".bak").exists()) return emptyList()+ val bytes = registry.openRead().use { input ->+ val output = ByteArrayOutputStream()+ val buffer = ByteArray(8192)+ while (true) {+ val count = input.read(buffer)+ if (count < 0) break+ require(output.size() + count <= 1024 * 1024)+ output.write(buffer, 0, count)+ }+ output.toByteArray()+ }+ val json = JSONObject(bytes.toString(Charsets.UTF_8))+ require(json.getInt("schemaVersion") == 1)+ val records = json.getJSONArray("collections")+ val result = (0 until records.length()).map { i ->+ val row = records.getJSONObject(i)+ val id = row.getString("id")+ require(UUID.fromString(id).toString() == id)+ val tree = row.getString("tree")+ val uri = Uri.parse(tree)+ require(uri.scheme == "content" && DocumentsContract.isTreeUri(uri))+ Registration(id, row.getString("label"), tree)+ }+ val registeredIds = result.map { it.id }.toSet()+ require(registeredIds.size == result.size)+ root.listFiles()?.forEach { child ->+ if (child.name !in registeredIds && runCatching { UUID.fromString(child.name).toString() == child.name }.getOrDefault(false)) {+ runCatching { handle(child.name).use { it.remove() } }+ }+ }+ return result+ }++ private fun save(records: List<Registration>) {+ root.mkdirs()+ val json = JSONObject().put("schemaVersion", 1).put("collections", JSONArray().apply {+ records.forEach {+ put(JSONObject().put("id", it.id).put("label", it.label).put("tree", it.tree))+ }+ })+ val bytes = json.toString().toByteArray()+ require(bytes.size <= 1024 * 1024)+ val stream = registry.startWrite()+ try {+ stream.write(bytes)+ registry.finishWrite(stream)+ } catch (error: Exception) {+ registry.failWrite(stream)+ throw error+ }+ }+}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesRead.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesRead.ktnew file mode 100644index 0000000000..f2b9c35f12--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesRead.kt@@ -0,0 +1,29 @@+package io.ente.ensu.notes++import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.NonCancellable+import kotlinx.coroutines.async+import kotlinx.coroutines.coroutineScope+import kotlinx.coroutines.currentCoroutineContext+import kotlinx.coroutines.ensureActive+import kotlinx.coroutines.withContext++internal suspend fun <T> runNotesRead(cancel: () -> Unit, read: () -> T): T = coroutineScope {+ val worker = async(Dispatchers.IO) {+ try {+ read()+ } catch (error: Exception) {+ currentCoroutineContext().ensureActive()+ throw error+ }+ }+ try {+ worker.await()+ } finally {+ try {+ cancel()+ } finally {+ withContext(NonCancellable) { worker.join() }+ }+ }+}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesSettingsScreen.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesSettingsScreen.ktnew file mode 100644index 0000000000..49ec53a693--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesSettingsScreen.kt@@ -0,0 +1,142 @@+package io.ente.ensu.notes++import androidx.activity.compose.rememberLauncherForActivityResult+import androidx.activity.result.contract.ActivityResultContracts+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.items+import androidx.compose.material3.AlertDialog+import androidx.compose.material3.Icon+import androidx.compose.material3.IconButton+import androidx.compose.material3.LinearProgressIndicator+import androidx.compose.material3.Text+import androidx.compose.material3.TextButton+import androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.painterResource+import androidx.compose.ui.unit.dp+import io.ente.ensu.components.KnowledgeCard+import io.ente.ensu.components.CompactButton+import io.ente.ensu.designsystem.EnsuColor+import io.ente.ensu.designsystem.EnsuSpacing+import io.ente.ensu.designsystem.EnsuTypography+import io.ente.ensu.designsystem.HugeIcons+import java.text.DateFormat+import java.util.Date++@Composable+fun NotesSettingsScreen(store: NotesStore) {+ val state by store.state.collectAsState()+ var removing by remember { mutableStateOf<NoteCollectionState?>(null) }+ val picker = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->+ uri?.let(store::add)+ }+ LazyColumn(+ modifier = Modifier.padding(horizontal = EnsuSpacing.pageHorizontal.dp),+ verticalArrangement = Arrangement.spacedBy(EnsuSpacing.md.dp)+ ) {+ items(state.collections, key = { it.id }) { collection ->+ KnowledgeCard(spacing = EnsuSpacing.sm.dp) {+ Row(verticalAlignment = Alignment.CenterVertically) {+ Column(+ modifier = Modifier.weight(1f),+ verticalArrangement = Arrangement.spacedBy(EnsuSpacing.xs.dp)+ ) {+ Text(collection.label, style = EnsuTypography.large, color = EnsuColor.textPrimary())+ Text(+ text = buildString {+ if (collection.documentCount == 0L && (collection.status == NotesStatus.Pending || collection.status == NotesStatus.Indexing)) {+ append("Preparing notesβ¦")+ } else {+ append("${collection.documentCount} ${if (collection.documentCount == 1L) "note indexed" else "notes indexed"}")+ }+ collection.lastUpdatedAtMs?.let {+ append(" Β· Updated at ")+ append(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(Date(it)))+ }+ },+ style = EnsuTypography.mini,+ color = EnsuColor.textMuted()+ )+ }+ IconButton(onClick = { removing = collection }, modifier = Modifier.size(48.dp)) {+ Icon(+ painter = painterResource(HugeIcons.Delete01Icon),+ contentDescription = "Remove ${collection.label}",+ modifier = Modifier.size(18.dp),+ tint = EnsuColor.textMuted()+ )+ }+ }+ collection.progress?.let { progress ->+ Row(+ horizontalArrangement = Arrangement.spacedBy(EnsuSpacing.sm.dp),+ verticalAlignment = Alignment.CenterVertically+ ) {+ LinearProgressIndicator(+ progress = { progress.coerceIn(0, 100) / 100f },+ modifier = Modifier.weight(1f),+ color = EnsuColor.action(),+ trackColor = EnsuColor.border()+ )+ Text("$progress%", style = EnsuTypography.mini, color = EnsuColor.textMuted())+ }+ }+ if (collection.status == NotesStatus.Pending) {+ Text(+ "Indexing will continue when the app and model are ready.",+ style = EnsuTypography.small,+ color = EnsuColor.textMuted()+ )+ }+ collection.error?.let {+ Text(it, style = EnsuTypography.small, color = EnsuColor.error)+ CompactButton(label = "Retry", onClick = { store.retry(collection.id) })+ }+ }+ }+ item {+ CompactButton(+ label = "Add notes folder",+ onClick = { picker.launch(null) }+ )+ }+ item {+ Text(+ "Ensu reads and indexes markdown files in the selected folder. Source files are never modified.",+ style = EnsuTypography.small,+ color = EnsuColor.textMuted()+ )+ }+ state.error?.let { error ->+ item { Text(error, style = EnsuTypography.small, color = EnsuColor.error) }+ }+ }+ removing?.let { collection ->+ AlertDialog(+ onDismissRequest = { removing = null },+ title = { Text("Remove ${collection.label}?") },+ text = { Text("Remove this folder from Your Notes? Your original files will be kept.") },+ confirmButton = {+ TextButton(onClick = {+ store.remove(collection.id)+ removing = null+ }) {+ Text("Remove")+ }+ },+ dismissButton = { TextButton(onClick = { removing = null }) { Text("Cancel") } },+ containerColor = EnsuColor.backgroundBase()+ )+ }+}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.ktnew file mode 100644index 0000000000..6f860369d1--- /dev/null+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/NotesStore.kt@@ -0,0 +1,486 @@+package io.ente.ensu.notes++import android.content.Context+import android.content.Intent+import android.net.Uri+import androidx.compose.runtime.staticCompositionLocalOf+import io.ente.ensu.bindings.GroundedExcerpt+import io.ente.ensu.bindings.GroundedSource+import io.ente.ensu.bindings.NoteSourceReference+import io.ente.ensu.bindings.NotesCancellation+import io.ente.ensu.bindings.NotesException+import io.ente.ensu.bindings.NotesHit+import io.ente.ensu.bindings.NotesIndexOptions+import io.ente.ensu.bindings.NotesProgress+import io.ente.ensu.bindings.NotesProgressCallback+import io.ente.ensu.bindings.NotesSummary+import io.ente.ensu.bindings.notesLimits+import io.ente.ensu.bindings.withNotesCollectionLabel+import io.ente.ensu.llm.LlmProvider+import io.ente.ensu.llm.ModelMaintenance+import io.ente.ensu.llm.withMaintenanceSuspended+import io.ente.ensu.logging.FileLogRepository+import io.ente.ensu.logging.LogLevel+import kotlinx.coroutines.CancellationException+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.CoroutineStart+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.Job+import kotlinx.coroutines.NonCancellable+import kotlinx.coroutines.currentCoroutineContext+import kotlinx.coroutines.delay+import kotlinx.coroutines.ensureActive+import kotlinx.coroutines.isActive+import kotlinx.coroutines.launch+import kotlinx.coroutines.withContext+import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.StateFlow+import kotlinx.coroutines.flow.asStateFlow+import kotlinx.coroutines.flow.update+import java.util.concurrent.atomic.AtomicBoolean++enum class NotesStatus { Pending, Indexing, Updating, Ready, Unavailable, Error }++data class NoteCollectionState(+ val id: String,+ val label: String,+ val status: NotesStatus = NotesStatus.Pending,+ val documentCount: Long = 0,+ val indexAvailable: Boolean = false,+ val progress: Int? = null,+ val error: String? = null,+ val completedEmpty: Boolean = false,+ val lastUpdatedAtMs: Long? = null+) {+ val eligible: Boolean+ get() = indexAvailable && status != NotesStatus.Error && status != NotesStatus.Unavailable+}++data class NotesState(+ val collections: List<NoteCollectionState> = emptyList(),+ val error: String? = null+)++val LocalNotesStore = staticCompositionLocalOf<NotesStore?> { null }++class NotesStore(+ private val context: Context,+ private val llm: LlmProvider,+ private val logRepository: FileLogRepository,+ private val provider: NotesProvider = NotesProvider(context)+) : ModelMaintenance {+ private data class Pending(+ val forced: Set<String>,+ val rebuild: Boolean,+ val due: Long+ )++ private class Run(+ val cancel: NotesCancellation,+ val job: Job+ )++ private val limits = notesLimits()+ private val registrations = linkedMapOf<String, NotesRegistration>()+ private val pending = linkedMapOf<String, Pending>()+ private val scans = linkedSetOf<String>()+ private val failed = mutableSetOf<String>()+ private val maintenance = NotesMaintenanceGate<Run>()+ private var scope: CoroutineScope? = null+ private var bootstrapJob: Job? = null+ private val active get() = maintenance.active+ private var wake: Job? = null+ private var foreground = false+ private var loaded = false+ private var disabled = false+ private val _state = MutableStateFlow(NotesState())+ val state: StateFlow<NotesState> = _state.asStateFlow()++ fun bootstrap(owner: CoroutineScope) {+ if (bootstrapJob != null) return+ scope = owner+ bootstrapJob = owner.launch {+ try {+ val records = provider.load()+ registrations.putAll(records.associateBy { it.id })+ val states = records.map { record ->+ try {+ val summary = provider.inspect(record.id)+ if (!summary.initialComplete) enqueue(record.id, immediate = true)+ fromSummary(record, summary)+ } catch (e: NotesException.RebuildRequired) {+ enqueue(record.id, rebuild = true, immediate = true)+ NoteCollectionState(record.id, record.label)+ } catch (e: Exception) {+ logRepository.log(LogLevel.Error, "Checkpoint load failed collection=${record.id}", tag = "Notes", throwable = e)+ NoteCollectionState(record.id, record.label, NotesStatus.Error, error = message(e))+ }+ }+ _state.value = NotesState(states)+ loaded = true+ scans.addAll(registrations.keys)+ } catch (e: Exception) {+ logRepository.log(LogLevel.Error, "Collection registry load failed", tag = "Notes", throwable = e)+ disabled = true+ _state.value = NotesState(error = "Could not load Your Notes. Existing indexes have been preserved.")+ }+ pump()+ }+ }++ suspend fun awaitReady() {+ bootstrapJob?.join()+ }++ fun setForeground(value: Boolean) {+ if (foreground == value) return+ foreground = value+ if (!value) {+ wake?.cancel()+ active?.job?.cancel()+ } else {+ failed.clear()+ if (loaded) scans.addAll(registrations.keys)+ pump()+ }+ }++ override fun modelReadinessChanged() {+ pump()+ }++ override fun suspendMaintenance(): AutoCloseable {+ maintenance.suspend()?.job?.cancel()+ val closed = AtomicBoolean()+ return AutoCloseable {+ if (closed.compareAndSet(false, true)) {+ maintenance.resume()+ scope?.launch { pump() }+ }+ }+ }++ override suspend fun awaitMaintenance() {+ active?.job?.join()+ }++ fun add(tree: Uri) {+ scope?.launch {+ awaitReady()+ if (disabled) return@launch+ withMaintenanceSuspended {+ try {+ val record = provider.add(tree)+ registrations[record.id] = record+ _state.update {+ it.copy(collections = it.collections + NoteCollectionState(record.id, record.label), error = null)+ }+ enqueue(record.id, immediate = true)+ } catch (error: CancellationException) {+ throw error+ } catch (error: Exception) {+ _state.update { it.copy(error = message(error)) }+ }+ }+ }+ }++ fun remove(id: String) {+ scope?.launch {+ withMaintenanceSuspended {+ if (id !in registrations) return@withMaintenanceSuspended+ try {+ provider.remove(id)+ registrations.remove(id)+ pending.remove(id)+ scans.remove(id)+ failed.remove(id)+ _state.update {+ it.copy(collections = it.collections.filter { row -> row.id != id }, error = null)+ }+ } catch (error: CancellationException) {+ throw error+ } catch (error: Exception) {+ _state.update { it.copy(error = message(error)) }+ }+ }+ }+ }++ fun retry(id: String) {+ if (!loaded || disabled || id !in registrations) return+ failed.remove(id)+ pending[id]?.let { pending[id] = it.copy(due = 0) }+ scans.add(id)+ update(id) { it.copy(status = NotesStatus.Pending, progress = null, error = null) }+ pump()+ }++ private fun clearError() {+ _state.update { it.copy(error = null) }+ }++ suspend fun retrieve(query: List<Float>): List<NotesHit> = withContext(Dispatchers.Main.immediate) {+ val records = _state.value.collections.filter { it.eligible }.mapNotNull { registrations[it.id] }+ val hits = mutableListOf<NotesHit>()+ for (record in records) {+ currentCoroutineContext().ensureActive()+ try {+ hits += provider.search(record.id, query)+ } catch (error: CancellationException) {+ throw error+ } catch (error: NotesException.RebuildRequired) {+ enqueue(record.id, rebuild = true, immediate = true)+ update(record.id) { it.copy(status = NotesStatus.Pending, indexAvailable = false) }+ } catch (error: Exception) {+ fail(record.id, error)+ }+ }+ hits+ }++ suspend fun verify(excerpts: List<GroundedExcerpt>): List<GroundedExcerpt> =+ withContext(Dispatchers.Main.immediate) {+ val records = registrations.toMap()+ var accepted = 0+ val result = mutableListOf<GroundedExcerpt>()+ for (excerpt in excerpts) {+ currentCoroutineContext().ensureActive()+ val note = (excerpt.source as? GroundedSource.LocalNote)?.reference+ if (note == null) {+ result += excerpt+ continue+ }+ if (accepted.toUInt() >= limits.maxGroundingHits) continue+ val record = records[note.collectionId] ?: continue+ try {+ provider.verify(note)+ result += excerpt.copy(source = GroundedSource.LocalNote(withNotesCollectionLabel(note, record.label)))+ accepted+++ } catch (error: CancellationException) {+ throw error+ } catch (error: NotesException.SourceChanged) {+ enqueue(record.id, forced = setOf(note.documentId), immediate = true)+ } catch (error: Exception) {+ fail(record.id, error)+ }+ }+ result+ }++ fun open(reference: NoteSourceReference) {+ scope?.launch {+ clearError()+ try {+ val uri = withMaintenanceSuspended {+ provider.preview(reference)+ }+ context.startActivity(Intent(Intent.ACTION_VIEW).apply {+ setDataAndType(uri, "text/plain")+ addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)+ })+ clearError()+ } catch (e: Exception) {+ val error = when (e) {+ is NotesException.SourceChanged -> "This note changed after the answer was generated."+ is NotesException.Unavailable -> "This note is unavailable."+ else -> "Could not open this note. Check folder access and that a text viewer is installed."+ }+ _state.update { it.copy(error = error) }+ }+ }+ }++ private fun enqueue(+ id: String,+ rebuild: Boolean = false,+ forced: Set<String> = emptySet(),+ immediate: Boolean = false+ ) {+ if (id !in registrations) return+ val old = pending[id]+ pending[id] = Pending(+ forced = old?.forced.orEmpty() + forced,+ rebuild = rebuild || old?.rebuild == true,+ due = if (immediate) 0 else old?.due ?: (System.currentTimeMillis() + SCAN_INTERVAL_MS)+ )+ }++ private fun pump() {+ val owner = scope ?: return+ if (!owner.isActive || !loaded || disabled || !foreground || !maintenance.available) return+ wake?.cancel()+ val now = System.currentTimeMillis()+ val embeddingReady = llm.isEmbeddingModelReady()+ val request = if (embeddingReady) {+ pending.entries.firstOrNull { it.key !in failed && it.value.due <= now }+ } else null+ val id = request?.key ?: scans.firstOrNull { it !in pending && it !in failed }+ if (id == null) {+ val due = if (embeddingReady) {+ pending.asSequence().filter { it.key !in failed }.minOfOrNull { it.value.due }+ } else null+ wake = owner.launch {+ delay(minOf(SCAN_INTERVAL_MS, due?.minus(now)?.coerceAtLeast(1) ?: SCAN_INTERVAL_MS))+ scans.addAll(_state.value.collections.filter {+ it.status != NotesStatus.Unavailable && (it.status != NotesStatus.Error || it.completedEmpty)+ }.map { it.id })+ pump()+ }+ return+ }+ val record = registrations[id] ?: run {+ pending.remove(id)+ scans.remove(id)+ pump()+ return+ }+ val snapshot = request?.value+ val cancel = NotesCancellation()+ val hadIndex = _state.value.collections.firstOrNull { it.id == id }?.indexAvailable == true+ val job = owner.launch(start = CoroutineStart.LAZY) {+ scans.remove(id)+ try {+ cancel.check()+ if (snapshot == null) {+ val result = provider.inspectFreshness(id, cancel)+ update(id) { fromSummary(record, result.summary).copy(progress = if (result.summary.initialComplete) null else it.progress) }+ if (result.changed) {+ enqueue(id, forced = result.forcedDocumentIds.toSet(), immediate = !result.summary.initialComplete)+ update(id) { it.copy(status = NotesStatus.Pending, error = null) }+ }+ } else {+ update(id) { it.copy(status = if (hadIndex) NotesStatus.Updating else NotesStatus.Indexing, error = null) }+ val result = llm.withEmbeddingContext(checkCancellation = cancel::check) { embedding ->+ provider.index(+ id,+ embedding,+ cancel,+ object : NotesProgressCallback {+ override fun onProgress(progress: NotesProgress) {+ owner.launch {+ if (active?.cancel === cancel) {+ update(id) {+ it.copy(progress = progress.percentage.toInt(), documentCount = progress.indexedDocumentCount.toLong())+ }+ }+ }+ }+ },+ NotesIndexOptions(snapshot.forced.toList(), snapshot.rebuild)+ )+ }+ val changedId = result.changedDocumentId+ if (pending[id] === snapshot) {+ if (changedId == null) {+ pending.remove(id)+ } else {+ pending[id] = snapshot.copy(+ forced = result.uncheckedDocumentIds.toSet() + changedId,+ rebuild = false,+ due = System.currentTimeMillis() + SCAN_INTERVAL_MS+ )+ }+ } else if (changedId != null) {+ enqueue(id, forced = result.uncheckedDocumentIds.toSet() + changedId)+ }+ update(id) {+ if (id !in pending) {+ fromSummary(record, result.summary)+ } else {+ it.copy(+ status = NotesStatus.Pending,+ documentCount = result.summary.documentCount.toLong(),+ indexAvailable = hadIndex && result.summary.initialComplete && result.summary.documentCount > 0uL+ )+ }+ }+ }+ } catch (e: Exception) {+ when (e) {+ is NotesException.Cancelled, is CancellationException -> {+ if (snapshot == null) scans.add(id)+ val saved = withContext(NonCancellable) {+ runCatching { provider.inspect(id) }.getOrNull()+ }+ update(id) {+ it.copy(+ status = NotesStatus.Pending,+ indexAvailable = hadIndex && saved?.initialComplete == true && saved.documentCount > 0uL+ )+ }+ if (e is CancellationException) throw e+ }+ is NotesException.RebuildRequired -> {+ if (snapshot == null) {+ enqueue(id, rebuild = true, immediate = true)+ update(id) { it.copy(status = NotesStatus.Pending, indexAvailable = false) }+ } else fail(id, e)+ }+ else -> fail(id, e)+ }+ }+ }+ val run = Run(cancel, job)+ val admitted = maintenance.admit(run)+ job.invokeOnCompletion {+ cancel.destroy()+ maintenance.finish(run)+ if (admitted) owner.launch { pump() }+ }+ if (admitted) job.start() else job.cancel()+ }++ private fun fail(id: String, e: Exception) {+ if (id !in registrations) return+ logRepository.log(LogLevel.Error, "Notes update failed collection=$id", tag = "Notes", throwable = e)+ failed.add(id)+ scans.remove(id)+ update(id) {+ it.copy(+ status = if (e is NotesException.Unavailable) NotesStatus.Unavailable else NotesStatus.Error,+ progress = null,+ error = message(e),+ completedEmpty = false+ )+ }+ }++ private fun update(id: String, transform: (NoteCollectionState) -> NoteCollectionState) {+ _state.update { it.copy(collections = it.collections.map { row -> if (row.id == id) transform(row) else row }) }+ }++ private fun fromSummary(record: NotesRegistration, summary: NotesSummary): NoteCollectionState {+ val ready = summary.initialComplete && summary.documentCount > 0uL+ val empty = summary.initialComplete && !ready+ return NoteCollectionState(+ id = record.id,+ label = record.label,+ status = when {+ ready -> NotesStatus.Ready+ empty -> NotesStatus.Error+ else -> NotesStatus.Pending+ },+ documentCount = summary.documentCount.toLong(),+ indexAvailable = ready,+ error = if (empty) EMPTY_NOTES else null,+ completedEmpty = empty,+ lastUpdatedAtMs = summary.lastUpdatedAtMs+ )+ }++ private fun message(e: Exception): String = when (e) {+ is NotesException.Unavailable -> "This Notes folder is unavailable. Check access and try again."+ is NotesException.SourceChanged -> "The folder changed. Try again after it finishes updating."+ is NotesException.SourceRead -> e.detail+ is NotesException.InvalidInput -> e.detail+ is NotesException.Storage -> "Could not save the Notes index. Check storage and try again."+ is NotesException.RebuildRequired -> "The Notes index needs rebuilding."+ else -> "Could not update Your Notes. Please try again."+ }++ companion object {+ private const val SCAN_INTERVAL_MS = 300_000L+ private const val EMPTY_NOTES = "No supported non-empty UTF-8 notes were found"+ }+}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/KnowledgeSettingsScreen.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/KnowledgeSettingsScreen.ktindex 90d0f495c2..72d969fe02 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/KnowledgeSettingsScreen.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/KnowledgeSettingsScreen.kt@@ -1,10 +1,8 @@package io.ente.ensu.settings-import androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.PaddingValuesimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.Spacerimport androidx.compose.foundation.layout.fillMaxWidth@@ -13,10 +11,6 @@ import androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.items-import androidx.compose.foundation.shape.RoundedCornerShape-import androidx.compose.material3.Button-import androidx.compose.material3.ButtonDefaults-import androidx.compose.material3.AlertDialogimport androidx.compose.material3.HorizontalDividerimport androidx.compose.material.icons.Iconsimport androidx.compose.material.icons.outlined.Info@@ -25,7 +19,6 @@ import androidx.compose.material3.IconButtonimport androidx.compose.material3.LinearProgressIndicatorimport androidx.compose.material3.Switchimport androidx.compose.material3.Text-import androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOf@@ -36,8 +29,11 @@ import androidx.compose.ui.Modifierimport androidx.compose.ui.platform.LocalUriHandlerimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.unit.dp+import io.ente.ensu.components.AttributionDialog+import io.ente.ensu.components.AttributionLink+import io.ente.ensu.components.CompactButton+import io.ente.ensu.components.KnowledgeCardimport io.ente.ensu.designsystem.EnsuColor-import io.ente.ensu.designsystem.EnsuCornerRadiusimport io.ente.ensu.designsystem.EnsuSpacingimport io.ente.ensu.designsystem.EnsuTypographyimport io.ente.ensu.designsystem.HugeIcons@@ -90,15 +86,7 @@ private fun KnowledgePackCard(onSetEnabled: (Boolean) -> Unit,onOpenAttribution: () -> Unit) {- Column(- modifier = Modifier- .fillMaxWidth()- .background(- EnsuColor.fillFaint(),- RoundedCornerShape(EnsuCornerRadius.card.dp)- )- .padding(EnsuSpacing.lg.dp)- ) {+ KnowledgeCard {Row(verticalAlignment = Alignment.CenterVertically) {Column(modifier = Modifier.weight(1f),@@ -114,7 +102,7 @@ private fun KnowledgePackCard(pack.status == KnowledgeReconciliationStatus.DOWNLOAD &&!pack.isMutating) {- CompactPackButton(label = "Download", onClick = onDownloadOrUpdate)+ CompactButton(label = "Download", onClick = onDownloadOrUpdate)}}@@ -148,7 +136,7 @@ private fun KnowledgePackCard(pack.status == KnowledgeReconciliationStatus.UPDATE_AVAILABLE) {Spacer(Modifier.height(EnsuSpacing.md.dp))- CompactPackButton(label = "Update", onClick = onDownloadOrUpdate)+ CompactButton(label = "Update", onClick = onDownloadOrUpdate)}pack.errorMessage?.let {@@ -193,20 +181,6 @@ private fun PackMetadataLine(}}-@Composable-private fun CompactPackButton(label: String, onClick: () -> Unit) {- Button(- onClick = onClick,- colors = ButtonDefaults.buttonColors(containerColor = EnsuColor.accent()),- contentPadding = PaddingValues(- horizontal = EnsuSpacing.md.dp,- vertical = EnsuSpacing.xs.dp- )- ) {- Text(label, style = EnsuTypography.mini)- }-}-@Composableprivate fun PackAttributionDialog(config: KnowledgeDatasetConfig,@@ -215,71 +189,41 @@ private fun PackAttributionDialog(val attribution = config.attributionval uriHandler = LocalUriHandler.current- AlertDialog(- onDismissRequest = onDismiss,- title = { Text("Attribution", style = EnsuTypography.h3) },- text = {- Column(- modifier = Modifier- .fillMaxWidth()- .background(- EnsuColor.fillFaint(),- RoundedCornerShape(EnsuCornerRadius.card.dp)- )- .padding(EnsuSpacing.md.dp),- verticalArrangement = Arrangement.spacedBy(EnsuSpacing.sm.dp)+ AttributionDialog(title = "Attribution", onDismiss = onDismiss) {+ KnowledgeCard(padding = EnsuSpacing.md.dp, spacing = EnsuSpacing.sm.dp) {+ Text(+ text = config.label,+ style = EnsuTypography.large,+ color = EnsuColor.textPrimary()+ )+ HorizontalDivider(color = EnsuColor.border())+ Text(+ text = "From ${attribution.credit}",+ style = EnsuTypography.small,+ color = EnsuColor.textPrimary()+ )+ Text(+ text = attribution.modificationNotice,+ style = EnsuTypography.small,+ color = EnsuColor.textPrimary()+ )+ Row(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.spacedBy(EnsuSpacing.md.dp)) {- Text(- text = config.label,- style = EnsuTypography.large,- color = EnsuColor.textPrimary()- )- HorizontalDivider(color = EnsuColor.border())- Text(- text = "From ${attribution.credit}",- style = EnsuTypography.small,- color = EnsuColor.textPrimary()- )- Text(- text = attribution.modificationNotice,- style = EnsuTypography.small,- color = EnsuColor.textPrimary()- )- Row(- modifier = Modifier.fillMaxWidth(),- horizontalArrangement = Arrangement.spacedBy(EnsuSpacing.md.dp)- ) {- AttributionLink("Source β") {- uriHandler.openUri(attribution.publicPackUrl)- }- AttributionLink("License β") {- uriHandler.openUri(attribution.licenseUrl)- }+ AttributionLink("Source β") {+ uriHandler.openUri(attribution.publicPackUrl)+ }+ AttributionLink("License β") {+ uriHandler.openUri(attribution.licenseUrl)}- HorizontalDivider(color = EnsuColor.border())- Text(- text = "Wikimedia and Ensu are not affiliated. Wikimedia project names identify the source material only.",- style = EnsuTypography.small,- color = EnsuColor.textMuted()- )}- },- confirmButton = {- TextButton(onClick = onDismiss) { Text("Done") }- },- containerColor = EnsuColor.backgroundBase()- )-}--@Composable-private fun AttributionLink(label: String, onClick: () -> Unit) {- Text(- text = label,- style = EnsuTypography.mini,- color = EnsuColor.accent(),- maxLines = 1,- modifier = Modifier- .clickable(onClick = onClick)- .padding(vertical = EnsuSpacing.xs.dp)- )+ HorizontalDivider(color = EnsuColor.border())+ Text(+ text = "Wikimedia and Ensu are not affiliated. Wikimedia project names identify the source material only.",+ style = EnsuTypography.small,+ color = EnsuColor.textMuted()+ )+ }+ }}diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/SettingsScreen.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/SettingsScreen.ktindex 68fcb248a5..48a562103e 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/SettingsScreen.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/settings/SettingsScreen.kt@@ -51,6 +51,7 @@ fun SettingsScreen(isAdvancedUnlocked: Boolean,onOpenLogs: () -> Unit,onOpenKnowledge: () -> Unit,+ onOpenNotes: () -> Unit,onOpenModelSettings: () -> Unit,onOpenSystemPromptSettings: () -> Unit,onUnlockAdvanced: () -> Unit,@@ -61,7 +62,7 @@ fun SettingsScreen(var lastBuildVersionTapAt by remember { mutableStateOf<Long?>(null) }val context = LocalContext.current- val allItems = remember(context, onOpenLogs, onOpenKnowledge, onSignIn) {+ val allItems = remember(context, onOpenLogs, onOpenKnowledge, onOpenNotes, onSignIn) {buildList {add(SettingsItem(@@ -71,6 +72,8 @@ fun SettingsScreen())+ add(SettingsItem(title = "Your Notes", iconRes = HugeIcons.Folder01Icon, onClick = onOpenNotes))+if (IS_ENSU_PACKS_ENABLED) {add(SettingsItem(diff --git a/android/apps/ensu/app/src/main/res/drawable/hugeicon_folder01.xml b/android/apps/ensu/app/src/main/res/drawable/hugeicon_folder01.xmlnew file mode 100644index 0000000000..fc8263a2ef--- /dev/null+++ b/android/apps/ensu/app/src/main/res/drawable/hugeicon_folder01.xml@@ -0,0 +1,12 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp"+ android:height="24dp"+ android:viewportWidth="24"+ android:viewportHeight="24">+ <path+ android:fillColor="#00000000"+ android:strokeColor="#FF000000"+ android:strokeWidth="2"+ android:strokeLineCap="round"+ android:pathData="M8 7H16.75C18.8567 7 19.91 7 20.6667 7.50559C20.9943 7.72447 21.2755 8.00572 21.4944 8.33329C22 9.08996 22 10.1433 22 12.25C22 15.7612 22 17.5167 21.1573 18.7779C20.7926 19.3238 20.3238 19.7926 19.7779 20.1573C18.5167 21 16.7612 21 13.25 21H12C7.28595 21 4.92893 21 3.46447 19.5355C2 18.0711 2 15.714 2 11V7.94427C2 6.1278 2 5.21956 2.38032 4.53806C2.65142 4.05227 3.05227 3.65142 3.53806 3.38032C4.21956 3 5.1278 3 6.94427 3C8.10802 3 8.6899 3 9.19926 3.19101C10.3622 3.62712 10.8418 4.68358 11.3666 5.73313L12 7" />+</vector>diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesMaintenanceGateTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesMaintenanceGateTest.ktnew file mode 100644index 0000000000..6665589325--- /dev/null+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesMaintenanceGateTest.kt@@ -0,0 +1,42 @@+package io.ente.ensu.notes++import java.util.concurrent.CyclicBarrier+import java.util.concurrent.Executors+import java.util.concurrent.TimeUnit+import org.junit.Assert.assertFalse+import org.junit.Assert.assertNull+import org.junit.Assert.assertSame+import org.junit.Assert.assertTrue+import org.junit.Test++class NotesMaintenanceGateTest {+ @Test+ fun concurrentSuspensionEitherFindsOrPreventsTheRun() {+ val executor = Executors.newFixedThreadPool(2)+ try {+ val gate = NotesMaintenanceGate<Any>()+ val run = Any()+ val start = CyclicBarrier(2)+ val admission = executor.submit<Boolean> {+ start.await(5, TimeUnit.SECONDS)+ gate.admit(run)+ }+ val suspension = executor.submit<Any?> {+ start.await(5, TimeUnit.SECONDS)+ gate.suspend()+ }++ if (admission.get(5, TimeUnit.SECONDS)) {+ assertSame(run, suspension.get(5, TimeUnit.SECONDS))+ } else {+ assertNull(suspension.get(5, TimeUnit.SECONDS))+ }+ gate.finish(run)+ assertFalse(gate.available)+ gate.resume()+ assertTrue(gate.available)+ } finally {+ executor.shutdownNow()+ }+ }+}diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesReadTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesReadTest.ktnew file mode 100644index 0000000000..66c2818bb2--- /dev/null+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesReadTest.kt@@ -0,0 +1,54 @@+package io.ente.ensu.notes++import java.util.concurrent.CountDownLatch+import java.util.concurrent.TimeUnit+import java.util.concurrent.atomic.AtomicBoolean+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.Job+import kotlinx.coroutines.cancelAndJoin+import kotlinx.coroutines.launch+import kotlinx.coroutines.runBlocking+import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.withLock+import kotlinx.coroutines.withContext+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Test++class NotesReadTest {+ @Test+ fun ownerCancellationKeepsResourcesUntilTheWorkerDrains() = runBlocking {+ val owner = Job()+ val entered = CountDownLatch(1)+ val cancelled = CountDownLatch(1)+ val drain = CountDownLatch(1)+ val released = AtomicBoolean()+ val gate = Mutex()+ launch(owner) {+ gate.withLock {+ try {+ runNotesRead(cancelled::countDown) {+ entered.countDown()+ check(drain.await(5, TimeUnit.SECONDS))+ }+ } finally {+ released.set(true)+ }+ }+ }++ try {+ assertTrue(withContext(Dispatchers.IO) { entered.await(5, TimeUnit.SECONDS) })+ owner.cancel()+ assertTrue(withContext(Dispatchers.IO) { cancelled.await(5, TimeUnit.SECONDS) })+ assertFalse(released.get())+ assertTrue(gate.isLocked)+ } finally {+ drain.countDown()+ owner.cancelAndJoin()+ }++ assertTrue(released.get())+ assertFalse(gate.isLocked)+ }+}diff --git a/apple/apps/ensu/Ensu.xcodeproj/project.pbxproj b/apple/apps/ensu/Ensu.xcodeproj/project.pbxprojindex 129c6080b1..40b3ec3b1e 100644--- a/apple/apps/ensu/Ensu.xcodeproj/project.pbxproj+++ b/apple/apps/ensu/Ensu.xcodeproj/project.pbxproj@@ -7,6 +7,7 @@objects = {/* Begin PBXBuildFile section */+ A20000012F11111100AA0001 = {isa = PBXBuildFile; fileRef = A20000022F11111100AA0001; };A10000012F11111100AA0001 /* LlmIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10000022F11111100AA0001 /* LlmIntegrationTests.swift */; };DAE20A022F00000100000002 /* EnteOnnxRuntime in Frameworks */ = {isa = PBXBuildFile; productRef = DAE20A012F00000100000001 /* EnteOnnxRuntime */; };DAF584672E61BB8300F8A08F /* ZIPFoundation in Frameworks */ = {isa = PBXBuildFile; productRef = DAF584662E61BB8300F8A08F /* ZIPFoundation */; };@@ -25,6 +26,7 @@/* End PBXContainerItemProxy section *//* Begin PBXFileReference section */+ A20000022F11111100AA0001 = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotesIntegrationTests.swift; sourceTree = "<group>"; };A10000022F11111100AA0001 /* LlmIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LlmIntegrationTests.swift; sourceTree = "<group>"; };A10000032F11111100AA0001 /* EnsuTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EnsuTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };DA72BC612E5F87A000D128FF /* Ensu.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Ensu.app; sourceTree = BUILT_PRODUCTS_DIR; };@@ -77,6 +79,7 @@isa = PBXGroup;children = (A10000022F11111100AA0001 /* LlmIntegrationTests.swift */,+ A20000022F11111100AA0001,);path = EnsuTests;sourceTree = "<group>";@@ -244,6 +247,7 @@buildActionMask = 2147483647;files = (A10000012F11111100AA0001 /* LlmIntegrationTests.swift in Sources */,+ A20000012F11111100AA0001,);runOnlyForDeploymentPostprocessing = 0;};diff --git a/apple/apps/ensu/Ensu/Assets.xcassets/Folder01Icon.imageset/Contents.json b/apple/apps/ensu/Ensu/Assets.xcassets/Folder01Icon.imageset/Contents.jsonnew file mode 100644index 0000000000..03d6c4e663--- /dev/null+++ b/apple/apps/ensu/Ensu/Assets.xcassets/Folder01Icon.imageset/Contents.json@@ -0,0 +1,16 @@+{+ "images" : [+ {+ "filename" : "Folder01Icon.svg",+ "idiom" : "universal"+ }+ ],+ "info" : {+ "author" : "xcode",+ "version" : 1+ },+ "properties" : {+ "preserves-vector-representation" : true,+ "template-rendering-intent" : "template"+ }+}diff --git a/apple/apps/ensu/Ensu/Assets.xcassets/Folder01Icon.imageset/Folder01Icon.svg b/apple/apps/ensu/Ensu/Assets.xcassets/Folder01Icon.imageset/Folder01Icon.svgnew file mode 100644index 0000000000..58b23c3065--- /dev/null+++ b/apple/apps/ensu/Ensu/Assets.xcassets/Folder01Icon.imageset/Folder01Icon.svg@@ -0,0 +1,3 @@+<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">+ <path d="M8 7H16.75C18.8567 7 19.91 7 20.6667 7.50559C20.9943 7.72447 21.2755 8.00572 21.4944 8.33329C22 9.08996 22 10.1433 22 12.25C22 15.7612 22 17.5167 21.1573 18.7779C20.7926 19.3238 20.3238 19.7926 19.7779 20.1573C18.5167 21 16.7612 21 13.25 21H12C7.28595 21 4.92893 21 3.46447 19.5355C2 18.0711 2 15.714 2 11V7.94427C2 6.1278 2 5.21956 2.38032 4.53806C2.65142 4.05227 3.05227 3.65142 3.53806 3.38032C4.21956 3 5.1278 3 6.94427 3C8.10802 3 8.6899 3 9.19926 3.19101C10.3622 3.62712 10.8418 4.68358 11.3666 5.73313L12 7" stroke-linecap="round" />+</svg>diff --git a/apple/apps/ensu/Ensu/Chat/ChatView.swift b/apple/apps/ensu/Ensu/Chat/ChatView.swiftindex 3fbadb02c1..6c1d597569 100644--- a/apple/apps/ensu/Ensu/Chat/ChatView.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatView.swift@@ -135,6 +135,7 @@ struct ChatView: View {viewModel.refreshModelDownloadInfo()}.onChange(of: scenePhase) { newValue in+ viewModel.notesStore.setForeground(newValue == .active)if newValue == .active {viewModel.refreshModelDownloadInfo()} else {@@ -142,9 +143,12 @@ struct ChatView: View {}}}+ .environmentObject(viewModel.notesStore)+ .onAppear { viewModel.notesStore.setForeground(scenePhase == .active) }.sheet(isPresented: $viewState.showSettings) {SettingsView(knowledgeStore: viewModel.knowledgeStore,+ notesStore: viewModel.notesStore,onSignIn: {viewState.pendingSignInRequest = trueviewState.showSettings = falsediff --git a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swiftindex 397d0ea388..5e64cc6409 100644--- a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift@@ -75,6 +75,7 @@ final class ChatViewModel: ObservableObject {@Published var voiceInputState: VoiceInputState = .idle@Published var draftCursorMoveToken = UUID()let knowledgeStore: KnowledgeStore+ let notesStore: NotesStoreprivate let provider: LlmProviderprivate let knowledgeEmbedding: KnowledgeEmbeddingConfig@@ -125,7 +126,10 @@ final class ChatViewModel: ObservableObject {vadModelPath: assetStore.voiceActivityModelPath(voiceActivity).path)let provider = LlmProvider(assetStore: assetStore, transcriber: transcriber, knowledgeEmbedding: config.knowledgeEmbedding)+ let notesStore = NotesStore(provider: provider)+ provider.modelMaintenance = notesStorelet voiceTranscriber = VoiceTranscriptionService(transcriber: transcriber, assetStore: assetStore)+ voiceTranscriber.modelMaintenance = notesStorelet knowledgeProvider = KnowledgeProvider(assetStore: assetStore)let knowledgeStore = KnowledgeStore(datasets: config.knowledgeDatasets, provider: knowledgeProvider)@@ -166,6 +170,7 @@ final class ChatViewModel: ObservableObject {self.knowledgeEmbedding = config.knowledgeEmbeddingself.knowledgeProvider = knowledgeProviderself.knowledgeStore = knowledgeStore+ self.notesStore = notesStoreself.voiceTranscriber = voiceTranscriberself.chatDb = chatDbself.attachmentsDir = attachmentsDir@@ -189,7 +194,9 @@ final class ChatViewModel: ObservableObject {refreshDeviceCapability()refreshModelDownloadInfo()Task {- await knowledgeStore.bootstrap()+ async let knowledge: Void = knowledgeStore.bootstrap()+ async let notes: Void = notesStore.bootstrap()+ _ = await (knowledge, notes)}}@@ -682,6 +689,7 @@ final class ChatViewModel: ObservableObject {}func refreshModelDownloadInfo() {+ notesStore.modelReadinessChanged()guard !isChatUnsupported else {isDownloading = falsedownloadToast = nil@@ -1009,32 +1017,39 @@ final class ChatViewModel: ObservableObject {seedDownloadProgressMemory()rebuildMessages(for: userNode.sessionId)+ let notesScope = notesStore.suspendMaintenance()generationTask = Task {defer {+ notesScope.close()settleGenerationIfActive(generationId: generationId, sessionId: userNode.sessionId)}+ await notesStore.awaitMaintenance()_ = await priorGeneration?.result_ = await priorSummary?.resultguard !Task.isCancelled, activeGenerationId == generationId else { return }await knowledgeStore.bootstrap()+ await notesStore.bootstrap()guard !Task.isCancelled, activeGenerationId == generationId else { return }var embeddingAssetInvalid = false- var knowledgeHits: [KnowledgeSearchHit] = []+ var knowledgeHits: [GroundedExcerpt] = []let enabledDatasets = knowledgeStore.enabledReadyDatasets- if !userNode.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,- !enabledDatasets.isEmpty {+ let queryText = userNode.text.trimmingCharacters(in: .whitespacesAndNewlines)+ if !queryText.isEmpty,+ (!enabledDatasets.isEmpty || notesStore.collections.contains(where: { $0.eligible })) {do {let hits = try await provider.withChatModelReleasedForRetrieval { embed intry Task.checkCancellation()- let query = try embed(- userNode.text.trimmingCharacters(in: .whitespacesAndNewlines)- )+ let query = try embed(queryText)try Task.checkCancellation()- return await knowledgeProvider.search(- datasets: enabledDatasets,- query: query,- maxHits: knowledgeEmbedding.maxHits+ let packs = await knowledgeProvider.search(+ datasets: enabledDatasets, query: query, maxHits: knowledgeEmbedding.maxHits)+ let notes = try await notesStore.retrieve(query: query)+ let candidates = try selectMixedGroundingCandidates(+ packHits: packs.map { KnowledgePromptHit(datasetId: $0.dataset.stableId, hit: $0.hit) },+ notesHits: notes, notesLimit: UInt32(notes.count)+ )+ return try await notesStore.verify(candidates)}try Task.checkCancellation()knowledgeHits = hits@@ -1120,10 +1135,8 @@ final class ChatViewModel: ObservableObject {0,(normalHistorySelection.inputBudget - normalHistorySelection.inputTokens) * 4 - 2)- let knowledgeContext = try? buildKnowledgePromptContext(- hits: knowledgeHits.map {- KnowledgePromptHit(datasetId: $0.dataset.stableId, hit: $0.hit)- },+ let knowledgeContext = try? buildGroundedPromptContext(+ excerpts: knowledgeHits,maxUtf8Bytes: UInt32(min(Int(knowledgeEmbedding.maxContextUtf8Bytes),remainingKnowledgeBytes@@ -1145,7 +1158,7 @@ final class ChatViewModel: ObservableObject {let useKnowledge = knowledgeHistorySelection?.wasTrimmed == falselet historySelection = useKnowledge ?(knowledgeHistorySelection ?? normalHistorySelection) : normalHistorySelection- var activeCitations = useKnowledge ? (knowledgeContext?.citations ?? []) : []+ var activeCitations = useKnowledge ? (knowledgeContext?.sources ?? []) : []let effectiveSystemPrompt = useKnowledge ?(candidateSystemPrompt ?? normalSystemPrompt) : normalSystemPromptlet userMessage = LlmMessage(@@ -1266,22 +1279,22 @@ final class ChatViewModel: ObservableObject {}}- private func finishGeneration(parent: MessageNode, response: String, tokenCount: Int, totalTimeMs: Int64?, interrupted: Bool, generationId: UUID, citations: [SourceCitation] = []) {+ private func finishGeneration(parent: MessageNode, response: String, tokenCount: Int, totalTimeMs: Int64?, interrupted: Bool, generationId: UUID, citations: [GroundedSource] = []) {let rawText = response.trimmingCharacters(in: .whitespacesAndNewlines)let trimmed: Stringif rawText.isEmpty {trimmed = rawText} else {do {- trimmed = try finalizeAssistantText(+ trimmed = try finalizeGroundedAssistantText(rawAssistantText: rawText,- citations: citations+ sources: citations)} catch {logger.error("Assistant source finalization failed", error)- trimmed = (try? finalizeAssistantText(+ trimmed = (try? finalizeGroundedAssistantText(rawAssistantText: rawText,- citations: []+ sources: [])) ?? ""}}@@ -1736,7 +1749,11 @@ final class ChatViewModel: ObservableObject {let selection = modelSettings.currentSelection()let provider = provider+ let notes = notesStore+ let notesScope = notes.suspendMaintenance()sessionSummaryTask = Task.detached(priority: .utility) { [weak self] in+ defer { notesScope.close() }+ await notes.awaitMaintenance()guard let self else { return }do {try await Task.sleep(nanoseconds: 200_000_000)@@ -1851,6 +1868,7 @@ final class ChatViewModel: ObservableObject {var buffer = ""do {+ try await provider.ensureModelReady(selection) { _ in }_ = try await provider.generateChat(selection,messages: messages,diff --git a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swiftindex 9e0fb2d391..e04d8cf5d9 100644--- a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift+++ b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift@@ -249,23 +249,17 @@ struct AssistantMessageBubbleView: View {@State private var showSources = falsevar body: some View {- let parsed = parseAssistantText(storedText: message.text)+ let parsed = parseGroundedAssistantText(storedText: message.text)HStack(alignment: .bottom) {VStack(alignment: .leading, spacing: 0) {VStack(alignment: .leading, spacing: EnsuSpacing.sm) {AssistantMessageRenderer(text: parsed.text, isStreaming: false, storageId: message.id.uuidString)- if let sourceLabel = parsed.sourceLabel {- Button(sourceLabel) {- showSources = true+ if !parsed.sourceLabels.isEmpty {+ ViewThatFits(in: .horizontal) {+ HStack(spacing: EnsuSpacing.sm) { sourceChips(parsed.sourceLabels) }+ VStack(alignment: .leading, spacing: EnsuSpacing.sm) { sourceChips(parsed.sourceLabels) }}- .font(EnsuTypography.small)- .foregroundStyle(EnsuColor.textPrimary)- .padding(.horizontal, EnsuSpacing.md)- .padding(.vertical, EnsuSpacing.sm)- .background(EnsuColor.fillFaint)- .clipShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.button))- .buttonStyle(.plain)}if message.isInterrupted {@@ -313,84 +307,108 @@ struct AssistantMessageBubbleView: View {.frame(maxWidth: .infinity, alignment: .leading)}.sheet(isPresented: $showSources) {- KnowledgeSourcesSheet(citations: parsed.citations)+ KnowledgeSourcesSheet(citations: parsed.sources)+ }+ }++ private func sourceChips(_ labels: [String]) -> some View {+ ForEach(Array(labels.enumerated()), id: \.offset) { _, label in+ SourceChip(label: label) { showSources = true }}}}private struct KnowledgeSourcesSheet: View {- let citations: [SourceCitation]- @Environment(\.dismiss) private var dismiss+ let citations: [GroundedSource]+ @EnvironmentObject private var notes: NotesStorevar body: some View {- NavigationStack {- ScrollView {- VStack(alignment: .leading, spacing: EnsuSpacing.lg) {- Text(sourceCountLabel)- .font(EnsuTypography.small)+ AttributionSheet(title: "Sources") {+ if hasLocalNotes, let error = notes.operationError {+ Text(error)+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.error)+ }+ ForEach(Array(citations.enumerated()), id: \.offset) { index, source in+ EnsuCard(padding: EnsuSpacing.md) {+ Text(sourceHeader(source, number: index + 1))+ .font(EnsuTypography.mini).foregroundStyle(EnsuColor.textMuted)- ForEach(Array(citations.enumerated()), id: \.offset) { index, citation in- VStack(alignment: .leading, spacing: EnsuSpacing.sm) {- Text("SOURCE \(index + 1) Β· \(citation.datasetLabel.uppercased())")- .font(EnsuTypography.mini)- .foregroundStyle(EnsuColor.textMuted)-- Text(citation.title)- .font(EnsuTypography.large)- .foregroundStyle(EnsuColor.textPrimary)-- Divider()+ Text(sourceTitle(source))+ .font(EnsuTypography.large)+ .foregroundStyle(EnsuColor.textPrimary)- Text("Attribution")- .font(EnsuTypography.mini)- .foregroundStyle(EnsuColor.textMuted)+ if case .localNote(let reference) = source, let section = reference.section {+ Text(section)+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.textMuted)+ }- Text(citation.credit)- .font(EnsuTypography.small)- .foregroundStyle(EnsuColor.textPrimary)+ Divider()- HStack(spacing: EnsuSpacing.lg) {- Link(destination: URL(string: citation.sourceUrl)!) {- Label("Open source", systemImage: "arrow.up.right.square")- }- Link(destination: URL(string: citation.licenseUrl)!) {- Label(citation.licenseLabel, systemImage: "doc.text")- }- }+ switch source {+ case .localNote(let reference):+ Text(reference.documentId).font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.textMuted)+ Button { notes.open(reference) } label: {+ Label("Open note", systemImage: "arrow.up.right.square")+ .frame(minHeight: 44)}- .padding(EnsuSpacing.md)- .frame(maxWidth: .infinity, alignment: .leading)- .background(EnsuColor.fillFaint)- .clipShape(- RoundedRectangle(- cornerRadius: EnsuCornerRadius.card,- style: .continuous- )- )+ .font(EnsuTypography.small)+ case .ensuPack(let citation):+ Text(citation.credit)+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.textMuted)++ ViewThatFits(in: .horizontal) {+ HStack(spacing: EnsuSpacing.lg) { sourceLinks(citation) }+ VStack(alignment: .leading, spacing: EnsuSpacing.sm) { sourceLinks(citation) }+ }+ .font(EnsuTypography.small)}}- .padding(EnsuSpacing.lg)- }- .background(EnsuColor.backgroundBase)- .navigationTitle("Sources")- .navigationBarTitleDisplayMode(.inline)- .toolbar {- ToolbarItem(placement: .primaryAction) {- Button("Done") { dismiss() }- }}}- .presentationDetents([.medium, .large])- .presentationDragIndicator(.visible)+ .sheet(item: $notes.preview) { preview in NotesPreviewView(url: preview.url) }}- private var sourceCountLabel: String {- citations.count == 1- ? "1 source used in this response"- : "\(citations.count) sources used in this response"+ private var hasLocalNotes: Bool {+ citations.contains {+ if case .localNote = $0 { return true }+ return false+ }}++ private func sourceHeader(_ source: GroundedSource, number: Int) -> String {+ switch source {+ case .localNote(let reference):+ return "SOURCE \(number) Β· YOUR NOTES" + (reference.collectionLabel.map { " Β· \($0.uppercased())" } ?? "")+ case .ensuPack(let citation):+ return "SOURCE \(number) Β· ENSU PACK Β· \(citation.datasetLabel.uppercased())"+ }+ }++ private func sourceTitle(_ source: GroundedSource) -> String {+ switch source {+ case .localNote(let reference): return reference.title+ case .ensuPack(let citation): return citation.title+ }+ }++ @ViewBuilder+ private func sourceLinks(_ citation: SourceCitation) -> some View {+ Link(destination: URL(string: citation.sourceUrl)!) {+ Label("Open source", systemImage: "arrow.up.right.square")+ .frame(minHeight: 44)+ }+ Link(destination: URL(string: citation.licenseUrl)!) {+ Label(citation.licenseLabel, systemImage: "arrow.up.right.square")+ .frame(minHeight: 44)+ }+ }+}private enum StreamingCursor {diff --git a/apple/apps/ensu/Ensu/Chat/VoiceTranscriptionService.swift b/apple/apps/ensu/Ensu/Chat/VoiceTranscriptionService.swiftindex 7dac4dbfa4..8f223e224a 100644--- a/apple/apps/ensu/Ensu/Chat/VoiceTranscriptionService.swift+++ b/apple/apps/ensu/Ensu/Chat/VoiceTranscriptionService.swift@@ -82,6 +82,7 @@ final class VoiceTranscriptionService {typealias StateHandler = @MainActor @Sendable (VoiceInputState) -> Voidtypealias TranscriptHandler = @MainActor @Sendable (String) -> Void+ weak var modelMaintenance: (any ModelMaintenance)?private let transcriber: Transcriberprivate let assetStore: AssetStoreprivate let modelAssets: [Asset]@@ -148,7 +149,11 @@ final class VoiceTranscriptionService {let sampleRate = recording.sampleRatelet pcm = recording.pcm+ let maintenance = modelMaintenance+ let maintenanceScope = maintenance?.suspendMaintenance()transcriptionTask = Task.detached(priority: .userInitiated) { [weak self] in+ defer { maintenanceScope?.close() }+ await maintenance?.awaitMaintenance()do {try await self?.downloadModelsIfNeeded(taskId: taskId,@@ -218,7 +223,11 @@ final class VoiceTranscriptionService {let taskId = beginVoiceTask()let downloadId = beginDownload(taskId: taskId)+ let maintenance = modelMaintenance+ let maintenanceScope = maintenance?.suspendMaintenance()transcriptionTask = Task.detached(priority: .userInitiated) { [weak self] in+ defer { maintenanceScope?.close() }+ await maintenance?.awaitMaintenance()do {try await self?.downloadModelsIfNeeded(taskId: taskId,@@ -285,7 +294,11 @@ final class VoiceTranscriptionService {private func preloadTranscriptionModel() {let transcriber = transcriberpreloadTask?.cancel()+ let maintenance = modelMaintenance+ let maintenanceScope = maintenance?.suspendMaintenance()preloadTask = Task.detached(priority: .utility) {+ defer { maintenanceScope?.close() }+ await maintenance?.awaitMaintenance()do {try transcriber.loadModel()} catch is CancellationError {diff --git a/apple/apps/ensu/Ensu/Components/EnsuComponents.swift b/apple/apps/ensu/Ensu/Components/EnsuComponents.swiftindex 6fc8892d6a..8fe0921543 100644--- a/apple/apps/ensu/Ensu/Components/EnsuComponents.swift+++ b/apple/apps/ensu/Ensu/Components/EnsuComponents.swift@@ -1,6 +1,81 @@import SwiftUIimport UIKit+struct EnsuCard<Content: View>: View {+ var padding: CGFloat = EnsuSpacing.lg+ @ViewBuilder var content: () -> Content++ var body: some View {+ VStack(alignment: .leading, spacing: EnsuSpacing.sm, content: content)+ .padding(padding)+ .frame(maxWidth: .infinity, alignment: .leading)+ .background(EnsuColor.fillFaint)+ .clipShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.card, style: .continuous))+ }+}++struct SourceChip: View {+ let label: String+ let action: () -> Void++ var body: some View {+ Button(action: action) {+ Text(label)+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.textPrimary)+ .padding(.horizontal, EnsuSpacing.md)+ .padding(.vertical, EnsuSpacing.sm)+ .frame(minHeight: 44)+ .background(EnsuColor.fillFaint)+ .clipShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.button))+ }+ .buttonStyle(.plain)+ .accessibilityHint("View sources used in this response")+ }+}++struct AttributionSheet<Content: View>: View {+ let title: String+ @ViewBuilder var content: () -> Content+ @Environment(\.dismiss) private var dismiss++ var body: some View {+ NavigationStack {+ ScrollView {+ VStack(alignment: .leading, spacing: EnsuSpacing.lg, content: content)+ .padding(EnsuSpacing.lg)+ .frame(maxWidth: .infinity, alignment: .leading)+ }+ .background(EnsuColor.backgroundBase)+ .navigationTitle(title)+ .navigationBarTitleDisplayMode(.inline)+ .toolbar {+ ToolbarItem(placement: .primaryAction) {+ Button("Done") { dismiss() }+ }+ }+ }+ .presentationDetents([.medium, .large])+ .presentationDragIndicator(.visible)+ }+}++struct CompactButton: View {+ let text: String+ let action: () -> Void++ var body: some View {+ Button(action: action) {+ Text(text)+ .font(EnsuTypography.small)+ .padding(.horizontal, EnsuSpacing.xs)+ }+ .buttonStyle(.borderedProminent)+ .controlSize(.small)+ .tint(EnsuColor.accent)+ }+}+struct PrimaryButton: View {let text: Stringvar isLoading: Bool = false@@ -64,6 +139,7 @@ struct ActionButton: View {var isSystemSymbol: Bool = falsevar tooltip: String? = nilvar color: Color = EnsuColor.textMuted+ var size: CGFloat = 36let action: () -> Voidvar body: some View {@@ -82,7 +158,7 @@ struct ActionButton: View {.frame(width: 16, height: 16)}}- .frame(width: 36, height: 36)+ .frame(width: size, height: size)}.buttonStyle(ActionButtonStyle(color: color)).accessibilityLabel(tooltip ?? "")diff --git a/apple/apps/ensu/Ensu/Services/IOSNotesSource.swift b/apple/apps/ensu/Ensu/Services/IOSNotesSource.swiftnew file mode 100644index 0000000000..78c8eb3b7c--- /dev/null+++ b/apple/apps/ensu/Ensu/Services/IOSNotesSource.swift@@ -0,0 +1,212 @@+import Foundation+import Darwin++final class NotesReadOperation: @unchecked Sendable {+ let cancellation = NotesCancellation()+ let coordinator = NSFileCoordinator()++ func cancel() {+ cancellation.cancel()+ coordinator.cancel()+ }++ func perform<T: Sendable>(_ read: @escaping @Sendable () throws -> T) async throws -> T {+ try await withTaskCancellationHandler {+ let worker = Task.detached { [self] in+ try cancellation.check()+ return try read()+ }+ let result = await worker.result+ try Task.checkCancellation()+ return try result.get()+ } onCancel: { [self] in+ cancel()+ }+ }+}++final class IOSNotesSource: NotesSource, @unchecked Sendable {+ let root: URL+ private let cancellation: NotesCancellation+ private let coordinator: NSFileCoordinator+ private static let limits = notesLimits()+ private static let byteLimit = Int(limits.maxSourceBytes)+ private static let entryLimit = 250_000++ init(bookmark: Data, cancellation: NotesCancellation, coordinator: NSFileCoordinator = NSFileCoordinator()) throws {+ var stale = false+ let url: URL+ do { url = try URL(resolvingBookmarkData: bookmark, bookmarkDataIsStale: &stale) }+ catch { throw NotesError.Unavailable }+ guard !stale, url.startAccessingSecurityScopedResource() else { throw NotesError.Unavailable }+ root = url+ self.cancellation = cancellation+ self.coordinator = coordinator+ do {+ let values = try url.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])+ guard values.isDirectory == true, values.isSymbolicLink != true else { throw NotesError.Unavailable }+ } catch {+ url.stopAccessingSecurityScopedResource()+ throw NotesError.Unavailable+ }+ }++ deinit { root.stopAccessingSecurityScopedResource() }++ private func coordinate<T>(_ url: URL, _ body: (URL) throws -> T) throws -> T {+ try cancellation.check()+ var error: NSError?+ var result: Result<T, Error>?+ coordinator.coordinate(readingItemAt: url, options: [], error: &error) { readable in+ result = Result { try body(readable) }+ }+ try cancellation.check()+ if let error { throw error }+ guard let result else { throw NotesError.SourceRead(detail: "Could not read the Notes folder.") }+ return try result.get()+ }++ func listDocuments() throws -> [NotesDocument] {+ do { return try inventory() } catch { throw Self.accessError(error) }+ }++ private func inventory() throws -> [NotesDocument] {+ var pending: [(URL, String)] = [(root, "")]+ var seen = Set<String>()+ var documents: [NotesDocument] = []+ var count = 0+ var total: UInt64 = 0+ let keys: Set<URLResourceKey> = [.isDirectoryKey, .isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey, .contentModificationDateKey]+ while let (directory, prefix) = pending.popLast() {+ try cancellation.check()+ let identity = directory.standardizedFileURL.resolvingSymlinksInPath().path+ guard seen.insert(identity).inserted else { throw NotesError.SourceRead(detail: "Ambiguous directory structure.") }+ let children = try coordinate(directory) {+ try FileManager.default.contentsOfDirectory(at: $0, includingPropertiesForKeys: Array(keys))+ }+ var names = Set<String>()+ for child in children {+ try cancellation.check()+ count += 1+ guard count <= Self.entryLimit else { throw NotesError.InvalidInput(detail: "The Notes folder contains too many entries.") }+ let name = child.lastPathComponent+ guard names.insert(name).inserted else { throw NotesError.SourceRead(detail: "Duplicate document names.") }+ if name.hasPrefix(".") { continue }+ let id = prefix + name+ let values = try child.resourceValues(forKeys: keys)+ if values.isSymbolicLink == true { continue }+ if values.isDirectory != true && values.isRegularFile != true { continue }+ guard values.isDirectory == true || Self.supported(name) else { continue }+ do { try validateNotesDocumentId(documentId: id) }+ catch NotesError.InvalidInput { continue }+ if values.isDirectory == true {+ pending.append((child, id + "/"))+ } else {+ let size: Int+ if let advertised = values.fileSize { size = advertised }+ else {+ do { size = try readBounded(documentId: id).bytes.count }+ catch ReadError.tooLarge { continue }+ }+ guard size <= Self.byteLimit else { continue }+ total += UInt64(size)+ guard documents.count < Int(Self.limits.maxCollectionDocuments), total <= Self.limits.maxCollectionSourceBytes else {+ throw NotesError.InvalidInput(detail: "The Notes folder is too large to index.")+ }+ documents.append(NotesDocument(documentId: id, size: UInt64(size),+ modifiedAtMs: values.contentModificationDate.map { Int64($0.timeIntervalSince1970 * 1000) }))+ }+ }+ }+ return documents.sorted { $0.documentId < $1.documentId }+ }++ func readDocument(documentId: String) throws -> NotesRead {+ do { return try readBounded(documentId: documentId) }+ catch ReadError.tooLarge { throw NotesError.SourceChanged }+ catch { throw Self.documentReadError(error, root: root) }+ }++ private enum ReadError: Error { case tooLarge }++ private func readBounded(documentId: String) throws -> NotesRead {+ try validateNotesDocumentId(documentId: documentId)+ let parts = documentId.split(separator: "/").map(String.init)+ guard parts.allSatisfy({ !$0.hasPrefix(".") }), Self.supported(parts.last ?? "") else { throw NotesError.SourceChanged }+ let url = parts.reduce(root) { $0.appendingPathComponent($1) }+ return try coordinate(url) { coordinated in+ guard coordinated.standardizedFileURL.path == url.standardizedFileURL.path else { throw NotesError.SourceChanged }+ var fd = Darwin.open(root.path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW)+ guard fd >= 0 else { throw NotesError.Unavailable }+ for (index, part) in parts.enumerated() {+ let flags = O_RDONLY | O_NOFOLLOW | O_CLOEXEC |+ (index == parts.count - 1 ? O_NONBLOCK : O_DIRECTORY)+ let next = openat(fd, part, flags)+ let code = errno+ close(fd)+ guard next >= 0 else { throw Self.fileError(code) }+ fd = next+ }+ let file = FileHandle(fileDescriptor: fd, closeOnDealloc: true)+ defer { try? file.close() }+ var before = stat()+ guard fstat(fd, &before) == 0, before.st_mode & S_IFMT == S_IFREG else { throw NotesError.SourceChanged }+ let flags = fcntl(fd, F_GETFL)+ guard flags >= 0, fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == 0 else {+ throw Self.fileError(errno)+ }+ guard before.st_size <= Self.byteLimit else { throw ReadError.tooLarge }+ var bytes = Data()+ while true {+ try cancellation.check()+ guard let chunk = try file.read(upToCount: 8192), !chunk.isEmpty else { break }+ guard bytes.count + chunk.count <= Self.byteLimit else { throw ReadError.tooLarge }+ bytes.append(chunk)+ }+ var after = stat()+ guard fstat(fd, &after) == 0, before.st_ino == after.st_ino, before.st_size == after.st_size,+ before.st_mtimespec.tv_sec == after.st_mtimespec.tv_sec,+ before.st_mtimespec.tv_nsec == after.st_mtimespec.tv_nsec,+ bytes.count == after.st_size else { throw NotesError.SourceChanged }+ return NotesRead(bytes: bytes, source: NotesDocument(documentId: documentId, size: UInt64(bytes.count),+ modifiedAtMs: Int64(after.st_mtimespec.tv_sec) * 1000 + Int64(after.st_mtimespec.tv_nsec) / 1_000_000))+ }+ }++ private static func supported(_ name: String) -> Bool {+ let name = name.lowercased()+ return name.hasSuffix(".md") || name.hasSuffix(".markdown")+ }+ private static func fileError(_ code: Int32) -> Error {+ if code == EACCES || code == EPERM { return NotesError.Unavailable }+ if code == ELOOP || code == ENOTDIR { return NotesError.SourceChanged }+ return NSError(domain: NSPOSIXErrorDomain, code: Int(code))+ }++ static func documentReadError(_ error: Error, root: URL) -> Error {+ guard isMissingFile(error) else { return accessError(error) }+ let fd = Darwin.open(root.path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW)+ guard fd >= 0 else {+ let code = errno+ if code == ENOENT || code == ENOTDIR || code == ELOOP || code == EACCES || code == EPERM {+ return NotesError.Unavailable+ }+ return accessError(error)+ }+ close(fd)+ return NotesError.SourceChanged+ }++ private static func isMissingFile(_ error: Error) -> Bool {+ let ns = error as NSError+ if ns.domain == NSPOSIXErrorDomain && ns.code == Int(ENOENT) { return true }+ if ns.domain == NSCocoaErrorDomain && (ns.code == NSFileNoSuchFileError || ns.code == NSFileReadNoSuchFileError) { return true }+ return (ns.userInfo[NSUnderlyingErrorKey] as? Error).map(isMissingFile) ?? false+ }+ private static func accessError(_ error: Error) -> Error {+ if let error = error as? NotesError { return error }+ let ns = error as NSError+ if ns.domain == NSCocoaErrorDomain && ns.code == NSFileReadNoPermissionError { return NotesError.Unavailable }+ return NotesError.SourceRead(detail: "Could not access the Notes folder. Please try again.")+ }+}diff --git a/apple/apps/ensu/Ensu/Services/LlmProvider.swift b/apple/apps/ensu/Ensu/Services/LlmProvider.swiftindex d1ab9cba39..b2f74003a8 100644--- a/apple/apps/ensu/Ensu/Services/LlmProvider.swift+++ b/apple/apps/ensu/Ensu/Services/LlmProvider.swift@@ -125,6 +125,15 @@ final class LlmProvider {private var backendInitialized = falseprivate var currentJobId: Int64?private let modelLoadGate = AsyncSerialGate()+ weak var modelMaintenance: (any ModelMaintenance)?++ private func withModelLock<T>(_ operation: () async throws -> T) async throws -> T {+ let maintenance = modelMaintenance+ let scope = await maintenance?.suspendMaintenance()+ defer { scope?.close() }+ await maintenance?.awaitMaintenance()+ return try await modelLoadGate.withLock(operation)+ }init(assetStore: AssetStore, transcriber: Transcriber, knowledgeEmbedding: KnowledgeEmbeddingConfig) {self.assetStore = assetStore@@ -174,7 +183,7 @@ final class LlmProvider {}let asset = chatAsset(selection)- let missingAssets: [Asset] = try await modelLoadGate.withLock {+ let missingAssets: [Asset] = try await withModelLock {let embeddingReady = isEmbeddingModelReady()if isEnsuPacksEnabled && !embeddingReady {_ = assetStore.removeDownloaded(embeddingAsset)@@ -190,7 +199,7 @@ final class LlmProvider {try await downloadAssets(missingAssets, onProgress: onProgress)}- try await modelLoadGate.withLock {+ try await withModelLock {guard !isEnsuPacksEnabled || isEmbeddingModelReady() else {_ = assetStore.removeDownloaded(embeddingAsset)throw RequiredModelValidationError(modelId: knowledgeEmbedding.targetId)@@ -211,7 +220,7 @@ final class LlmProvider {_ selection: LlmModelSelection,onProgress: @escaping (DownloadProgress) -> Void) async throws {- try await modelLoadGate.withLock {+ try await withModelLock {try await ensureModelReadyLocked(selection, onProgress: onProgress, allowRecovery: true)}}@@ -283,7 +292,7 @@ final class LlmProvider {maxTokens: Int?,onToken: @escaping (String) -> Void) async throws -> GenerationSummary {- try await modelLoadGate.withLock {+ try await withModelLock {try await generateChatLocked(selection,messages: messages,@@ -373,8 +382,22 @@ final class LlmProvider {func withChatModelReleasedForRetrieval<T>(_ operation: (_ embed: (String) throws -> [Float]) async throws -> T+ ) async throws -> T {+ let maintenance = modelMaintenance+ let scope = await maintenance?.suspendMaintenance()+ defer { scope?.close() }+ await maintenance?.awaitMaintenance()+ return try await withEmbeddingContext { context in+ try await operation { try context.embed(text: $0) }+ }+ }++ func withEmbeddingContext<T>(+ checkCancellation: () throws -> Void = {},+ _ operation: (LlmContext) async throws -> T) async throws -> T {try await modelLoadGate.withLock {+ try checkCancellation()let capability = currentChatDeviceCapability()if !capability.isChatSupported {throw UnsupportedDeviceMemoryError(capability: capability)@@ -416,9 +439,7 @@ final class LlmProvider {guard let context = embeddingContext else {throw EmbeddingAssetInvalidError()}- return try await operation { text in- try context.embed(text: text)- }+ return try await operation(context)}}@@ -436,7 +457,7 @@ final class LlmProvider {do {try await Task.detached(priority: .utility) { [weak self] inguard let self else { return }- try await self.modelLoadGate.withLock {+ try await self.withModelLock {let asset = self.chatAsset(selection)guard self.assetStore.isDownloaded(asset) else { return }guard let mmprojPath = self.assetStore.llmMmprojPath(asset),@@ -462,7 +483,7 @@ final class LlmProvider {}func resetContext() async {- try? await modelLoadGate.withLock {+ try? await withModelLock {guard let model = loadedModel else { return }let contextParams = LlmContextParams(contextSize: currentContextLength.map(Int32.init), nThreads: nil, nBatch: nil)loadedContext = nildiff --git a/apple/apps/ensu/Ensu/Services/ModelMaintenance.swift b/apple/apps/ensu/Ensu/Services/ModelMaintenance.swiftnew file mode 100644index 0000000000..b2d0aab141--- /dev/null+++ b/apple/apps/ensu/Ensu/Services/ModelMaintenance.swift@@ -0,0 +1,31 @@+import Foundation++@MainActor+protocol ModelMaintenance: AnyObject {+ func suspendMaintenance() -> ModelUseScope+ func awaitMaintenance() async+}++extension ModelMaintenance {+ func withMaintenanceSuspended<T>(_ body: () async throws -> T) async throws -> T {+ let token = suspendMaintenance()+ defer { token.close() }+ await awaitMaintenance()+ try Task.checkCancellation()+ return try await body()+ }+}++final class ModelUseScope: @unchecked Sendable {+ private let lock = NSLock()+ private var release: (@Sendable () -> Void)?+ init(_ release: @escaping @Sendable () -> Void) { self.release = release }+ func close() {+ lock.lock()+ let action = release+ release = nil+ lock.unlock()+ action?()+ }+ deinit { close() }+}diff --git a/apple/apps/ensu/Ensu/Services/NotesProvider.swift b/apple/apps/ensu/Ensu/Services/NotesProvider.swiftnew file mode 100644index 0000000000..6db6731ed0--- /dev/null+++ b/apple/apps/ensu/Ensu/Services/NotesProvider.swift@@ -0,0 +1,297 @@+import Foundation++struct NotesCollectionInfo: Sendable {+ let id: String+ let label: String+}++actor NotesProvider {+ private struct Registration: Codable, Sendable {+ let id: String+ let label: String+ let bookmark: Data+ var canonicalPath: String?++ var info: NotesCollectionInfo {+ NotesCollectionInfo(id: id, label: label)+ }+ }++ private struct Registry: Codable {+ let schemaVersion: Int+ let collections: [Registration]+ }++ private let root: URL+ private let diskGate = AsyncSerialGate()+ private var registrations: [Registration] = []++ init(root: URL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]+ .appendingPathComponent("notes", isDirectory: true)) {+ self.root = root+ }++ func load() async throws -> [NotesCollectionInfo] {+ try await diskGate.withLock {+ let root = self.root+ let records = try await Task.detached {+ try Self.load(root: root)+ }.value+ self.registrations = records+ return records.map(\.info)+ }+ }++ func add(_ url: URL) async throws -> NotesCollectionInfo {+ try await diskGate.withLock {+ let root = self.root+ let records = self.registrations+ let record = try await Task.detached {+ guard url.startAccessingSecurityScopedResource() else {+ throw NotesError.Unavailable+ }+ defer { url.stopAccessingSecurityScopedResource() }++ let canonical = url.standardizedFileURL.resolvingSymlinksInPath().path+ let selectedPrefix = canonical.hasSuffix("/") ? canonical : canonical + "/"+ for registered in records {+ guard let path = Self.identityPath(registered) else { continue }+ let existingPrefix = path.hasSuffix("/") ? path : path + "/"+ guard canonical != path,+ !canonical.hasPrefix(existingPrefix),+ !path.hasPrefix(selectedPrefix) else {+ throw NotesError.InvalidInput(+ detail: "This folder overlaps a folder already added to Your Notes."+ )+ }+ }++ let bookmark = try url.bookmarkData(+ options: .minimalBookmark,+ includingResourceValuesForKeys: [.pathKey],+ relativeTo: nil+ )+ let source = try IOSNotesSource(bookmark: bookmark, cancellation: NotesCancellation())+ let record = Registration(+ id: UUID().uuidString.lowercased(),+ label: source.root.lastPathComponent,+ bookmark: bookmark,+ canonicalPath: canonical+ )+ try Self.save(records + [record], root: root)+ return record+ }.value+ self.registrations.append(record)+ return record.info+ }+ }++ func remove(_ id: String) async throws {+ try await diskGate.withLock {+ let root = self.root+ let records = self.registrations.filter { $0.id != id }+ try await Task.detached {+ try Self.save(records, root: root)+ }.value+ self.registrations = records+ await Task.detached {+ try? Self.handle(root, id).remove()+ }.value+ }+ }++ func inspect(_ id: String) async throws -> NotesSummary {+ try await diskGate.withLock {+ let root = self.root+ return try await Task.detached {+ try Self.handle(root, id).inspect()+ }.value+ }+ }++ func inspectFreshness(_ id: String, operation: NotesReadOperation) async throws -> NotesFreshness {+ try await diskGate.withLock {+ let root = self.root+ let record = try self.registration(id)+ return try await operation.perform {+ let source = try Self.source(record, operation: operation)+ return try Self.handle(root, id).inspectFreshness(+ source: source,+ cancellation: operation.cancellation+ )+ }+ }+ }++ func index(+ _ id: String,+ context: LlmContext,+ operation: NotesReadOperation,+ options: NotesIndexOptions,+ onProgress: @escaping @Sendable (NotesProgress) -> Void+ ) async throws -> NotesOutcome {+ try await diskGate.withLock {+ let root = self.root+ let record = try self.registration(id)+ return try await operation.perform {+ let source = try Self.source(record, operation: operation)+ return try Self.handle(root, id).index(+ source: source,+ context: context,+ cancellation: operation.cancellation,+ progress: NativeNotesProgress(onProgress),+ options: options+ )+ }+ }+ }++ func search(_ id: String, query: [Float]) async throws -> [NotesHit] {+ try await diskGate.withLock {+ let root = self.root+ return try await Task.detached {+ try Self.handle(root, id).search(query: query)+ }.value+ }+ }++ func verify(_ reference: NoteSourceReference) async throws -> NoteSourceReference {+ try await diskGate.withLock {+ let record = try self.registration(reference.collectionId)+ let operation = NotesReadOperation()+ try await operation.perform {+ _ = try Self.readVerified(record, reference, operation: operation)+ }+ return withNotesCollectionLabel(reference: reference, label: record.label)+ }+ }++ func preview(_ reference: NoteSourceReference) async throws -> URL {+ try await diskGate.withLock {+ let record = try self.registration(reference.collectionId)+ let operation = NotesReadOperation()+ return try await operation.perform {+ let bytes = try Self.readVerified(record, reference, operation: operation)+ let folder = FileManager.default.temporaryDirectory+ .appendingPathComponent("note-preview-" + UUID().uuidString)+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)+ do {+ let url = folder.appendingPathComponent((reference.documentId as NSString).lastPathComponent)+ try bytes.write(to: url, options: [.atomic, .completeFileProtection])+ return url+ } catch {+ try? FileManager.default.removeItem(at: folder)+ throw error+ }+ }+ }+ }++ private func registration(_ id: String) throws -> Registration {+ guard let record = registrations.first(where: { $0.id == id }) else {+ throw NotesError.Unavailable+ }+ return record+ }++ private static func source(_ record: Registration, operation: NotesReadOperation) throws -> IOSNotesSource {+ try IOSNotesSource(+ bookmark: record.bookmark,+ cancellation: operation.cancellation,+ coordinator: operation.coordinator+ )+ }++ private static func readVerified(+ _ record: Registration,+ _ reference: NoteSourceReference,+ operation: NotesReadOperation+ ) throws -> Data {+ let source = try source(record, operation: operation)+ let bytes = try source.readDocument(documentId: reference.documentId).bytes+ guard notesContentRevision(bytes: bytes) == reference.indexedRevision else {+ throw NotesError.SourceChanged+ }+ return bytes+ }++ private static func identityPath(_ record: Registration) -> String? {+ var stale = false+ if let url = try? URL(+ resolvingBookmarkData: record.bookmark,+ options: [.withoutUI, .withoutMounting],+ bookmarkDataIsStale: &stale+ ) {+ return url.standardizedFileURL.resolvingSymlinksInPath().path+ }+ if let path = record.canonicalPath {+ return path+ }+ return NSURL.resourceValues(forKeys: [.pathKey], fromBookmarkData: record.bookmark)?[.pathKey] as? String+ }++ private static func handle(_ root: URL, _ id: String) throws -> NotesCollection {+ try NotesCollection(indexRoot: root.path, collectionId: id)+ }++ private static func load(root: URL) throws -> [Registration] {+ let url = root.appendingPathComponent("collections.json")+ guard FileManager.default.fileExists(atPath: url.path) else { return [] }+ let file = try FileHandle(forReadingFrom: url)+ defer { try? file.close() }+ let data = try file.read(upToCount: 1024 * 1024 + 1) ?? Data()+ guard data.count <= 1024 * 1024 else {+ throw NotesError.InvalidInput(detail: "Notes registry is too large.")+ }+ let registry = try JSONDecoder().decode(Registry.self, from: data)+ let registeredIds = Set(registry.collections.map(\.id))+ guard registry.schemaVersion == 1,+ registeredIds.count == registry.collections.count,+ registry.collections.allSatisfy({ UUID(uuidString: $0.id)?.uuidString.lowercased() == $0.id }) else {+ throw NotesError.InvalidInput(detail: "Invalid Notes registry.")+ }+ for child in (try? FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: nil)) ?? [] {+ let id = child.lastPathComponent+ if !registeredIds.contains(id), UUID(uuidString: id)?.uuidString.lowercased() == id {+ try? handle(root, id).remove()+ }+ }+ var records = registry.collections+ for index in records.indices where records[index].canonicalPath == nil {+ records[index].canonicalPath = identityPath(records[index])+ }+ if zip(registry.collections, records).contains(where: { $0.canonicalPath != $1.canonicalPath }) {+ try? save(records, root: root)+ }+ return records+ }++ private static func save(_ records: [Registration], root: URL) throws {+ try FileManager.default.createDirectory(+ at: root,+ withIntermediateDirectories: true,+ attributes: [.protectionKey: FileProtectionType.complete]+ )+ var root = root+ var values = URLResourceValues()+ values.isExcludedFromBackup = true+ try root.setResourceValues(values)+ let data = try JSONEncoder().encode(Registry(schemaVersion: 1, collections: records))+ guard data.count <= 1024 * 1024 else {+ throw NotesError.InvalidInput(detail: "Notes registry is too large.")+ }+ try data.write(to: root.appendingPathComponent("collections.json"), options: [.atomic, .completeFileProtection])+ }+}++private final class NativeNotesProgress: NotesProgressCallback {+ private let callback: @Sendable (NotesProgress) -> Void++ init(_ callback: @escaping @Sendable (NotesProgress) -> Void) {+ self.callback = callback+ }++ func onProgress(progress: NotesProgress) {+ callback(progress)+ }+}diff --git a/apple/apps/ensu/Ensu/Services/NotesStore.swift b/apple/apps/ensu/Ensu/Services/NotesStore.swiftnew file mode 100644index 0000000000..01a48ef3cf--- /dev/null+++ b/apple/apps/ensu/Ensu/Services/NotesStore.swift@@ -0,0 +1,427 @@+import Foundation+import SwiftUI++enum NotesStatus: String {+ case pending, indexing, updating, ready, unavailable, error+}++struct NoteCollectionState: Identifiable {+ let id: String+ let label: String+ var status: NotesStatus = .pending+ var documentCount: UInt64 = 0+ var lastUpdatedAtMs: Int64?+ var indexAvailable = false+ var progress: UInt8?+ var error: String?+ var completedEmpty = false+ var eligible: Bool {+ indexAvailable && status != .error && status != .unavailable+ }+}++@MainActor+final class NotesStore: ObservableObject, ModelMaintenance {+ @Published private(set) var collections: [NoteCollectionState] = []+ @Published var operationError: String?+ @Published var preview: NotesPreview?++ private struct Pending {+ let generation: UInt64+ let forced: Set<String>+ let rebuild: Bool+ let due: Date+ }+ private struct Run {+ let operation: NotesReadOperation+ let task: Task<Void, Never>+ }++ private let modelProvider: LlmProvider+ private let provider: NotesProvider+ private let limits = notesLimits()+ private var pending: [String: Pending] = [:]+ private var scans = Set<String>()+ private var failed = Set<String>()+ private var generation: UInt64 = 0+ private var active: Run?+ private var scopes = 0+ private var foreground = false+ private var loaded = false+ private var disabled = false+ private var bootstrapTask: Task<Void, Never>?+ private var wake: Task<Void, Never>?+ private static let emptyNotes = "No supported non-empty UTF-8 notes were found"++ init(provider: LlmProvider, notesProvider: NotesProvider = NotesProvider()) {+ modelProvider = provider+ self.provider = notesProvider+ }++ func bootstrap() async {+ if let bootstrapTask {+ await bootstrapTask.value+ return+ }+ let task = Task { @MainActor in+ do {+ let records = try await provider.load()+ for record in records {+ do {+ let summary = try await provider.inspect(record.id)+ collections.append(Self.state(record, summary))+ if !summary.initialComplete {+ enqueue(record.id, due: .distantPast)+ }+ } catch NotesError.RebuildRequired {+ collections.append(NoteCollectionState(id: record.id, label: record.label))+ enqueue(record.id, rebuild: true, due: .distantPast)+ } catch {+ collections.append(NoteCollectionState(id: record.id, label: record.label, status: .error,+ error: Self.message(error)))+ }+ }+ loaded = true+ scans.formUnion(records.map(\.id))+ pump()+ } catch {+ disabled = true+ operationError = "Could not load Your Notes. Existing indexes have been preserved."+ }+ }+ bootstrapTask = task+ await task.value+ }++ func setForeground(_ value: Bool) {+ guard foreground != value else { return }+ foreground = value+ if value {+ failed.removeAll()+ scans.formUnion(collections.map(\.id))+ pump()+ } else {+ wake?.cancel()+ active?.operation.cancel()+ }+ }+ func modelReadinessChanged() {+ pump()+ }++ func suspendMaintenance() -> ModelUseScope {+ scopes += 1+ active?.operation.cancel()+ return ModelUseScope { [weak self] in+ Task { @MainActor in+ guard let self else { return }+ self.scopes -= 1+ self.pump()+ }+ }+ }+ func awaitMaintenance() async {+ await active?.task.value+ }++ func add(_ url: URL) {+ Task {+ await bootstrap()+ guard !disabled else { return }+ do {+ try await withMaintenanceSuspended {+ let record = try await provider.add(url)+ collections.append(NoteCollectionState(id: record.id, label: record.label))+ operationError = nil+ enqueue(record.id, due: .distantPast)+ }+ } catch {+ operationError = Self.message(error)+ }+ }+ }++ func remove(_ id: String) {+ Task {+ await bootstrap()+ guard !disabled else { return }+ do {+ try await withMaintenanceSuspended {+ try await provider.remove(id)+ collections.removeAll { $0.id == id }+ pending.removeValue(forKey: id)+ scans.remove(id)+ failed.remove(id)+ operationError = nil+ }+ } catch {+ operationError = Self.message(error)+ }+ }+ }++ func retry(_ id: String) {+ guard let collection = collections.first(where: { $0.id == id }),+ collection.status == .error || collection.status == .unavailable else {+ return+ }+ failed.remove(id)+ scans.insert(id)+ if pending[id] != nil {+ enqueue(id, due: .distantPast)+ }+ update(id) {+ $0.status = .pending+ $0.error = nil+ $0.progress = nil+ }+ pump()+ }++ func retrieve(query: [Float]) async throws -> [NotesHit] {+ var hits: [NotesHit] = []+ for collection in collections where collection.eligible {+ try Task.checkCancellation()+ do {+ hits += try await provider.search(collection.id, query: query)+ } catch NotesError.RebuildRequired {+ enqueue(collection.id, rebuild: true, due: .distantPast)+ update(collection.id) {+ $0.status = .pending+ $0.indexAvailable = false+ }+ } catch is CancellationError {+ throw CancellationError()+ } catch {+ fail(collection.id, error)+ }+ }+ return hits+ }++ func verify(_ excerpts: [GroundedExcerpt]) async throws -> [GroundedExcerpt] {+ var result: [GroundedExcerpt] = []+ var accepted = 0+ for excerpt in excerpts {+ try Task.checkCancellation()+ guard case .localNote(let reference) = excerpt.source else {+ result.append(excerpt)+ continue+ }+ guard accepted < Int(limits.maxGroundingHits),+ collections.contains(where: { $0.id == reference.collectionId }) else {+ continue+ }+ do {+ let reference = try await provider.verify(reference)+ result.append(GroundedExcerpt(+ score: excerpt.score,+ source: .localNote(reference: reference),+ text: excerpt.text+ ))+ accepted += 1+ } catch is CancellationError {+ throw CancellationError()+ } catch NotesError.SourceChanged {+ enqueue(reference.collectionId, forced: [reference.documentId], due: .distantPast)+ } catch {+ fail(reference.collectionId, error)+ }+ }+ return result+ }++ func open(_ reference: NoteSourceReference) {+ Task {+ operationError = nil+ do {+ let url = try await withMaintenanceSuspended {+ try await provider.preview(reference)+ }+ preview = NotesPreview(url: url)+ operationError = nil+ } catch NotesError.SourceChanged {+ operationError = "This note changed after the answer was generated."+ } catch NotesError.Unavailable {+ operationError = "This note is unavailable."+ } catch {+ operationError = "Could not open this note. Check folder access and try again."+ }+ }+ }++ private func enqueue(_ id: String, forced: Set<String> = [], rebuild: Bool = false, due: Date? = nil) {+ guard collections.contains(where: { $0.id == id }) else { return }+ let old = pending[id]+ generation &+= 1+ pending[id] = Pending(+ generation: generation,+ forced: (old?.forced ?? []).union(forced),+ rebuild: rebuild || old?.rebuild == true,+ due: due ?? old?.due ?? Date().addingTimeInterval(300)+ )+ }++ private func pump() {+ guard loaded, !disabled, foreground, scopes == 0, active == nil else { return }+ wake?.cancel()+ let embeddingReady = modelProvider.isEmbeddingModelReady()+ let request = embeddingReady ?+ pending.filter { !failed.contains($0.key) && $0.value.due <= Date() }.min { $0.key < $1.key } : nil+ guard let id = request?.key ?? scans.filter({ pending[$0] == nil && !failed.contains($0) }).min(),+ let collection = collections.first(where: { $0.id == id }) else {+ let due = embeddingReady ? pending.filter { !failed.contains($0.key) }.values.map(\.due).min() : nil+ let seconds = min(300, max(0.1, due?.timeIntervalSinceNow ?? 300))+ wake = Task { [weak self] in+ do {+ try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))+ } catch {+ return+ }+ guard let self else { return }+ self.scans.formUnion(self.collections.filter { $0.status != .unavailable &&+ ($0.status != .error || $0.completedEmpty) }.map(\.id))+ self.pump()+ }+ return+ }+ scans.remove(id)+ let snapshot = request?.value+ let operation = NotesReadOperation()+ let cancellation = operation.cancellation+ let record = NotesCollectionInfo(id: collection.id, label: collection.label)+ let hadIndex = collection.indexAvailable+ let task = Task { @MainActor in+ defer {+ if active?.operation === operation {+ active = nil+ }+ pump()+ }+ do {+ try cancellation.check()+ if let snapshot {+ update(id) {+ $0.status = hadIndex ? .updating : .indexing+ $0.error = nil+ }+ let result = try await modelProvider.withEmbeddingContext(checkCancellation: cancellation.check) { context in+ try await self.provider.index(+ id,+ context: context,+ operation: operation,+ options: NotesIndexOptions(+ forcedDocumentIds: Array(snapshot.forced),+ rebuild: snapshot.rebuild+ )+ ) { [weak self] progress in+ Task { @MainActor in+ guard let self, self.active?.operation === operation else { return }+ self.update(id) {+ $0.progress = progress.percentage+ $0.documentCount = progress.indexedDocumentCount+ }+ }+ }+ }+ let same = pending[id]?.generation == snapshot.generation+ if same {+ pending.removeValue(forKey: id)+ }+ if let changed = result.changedDocumentId {+ enqueue(+ id,+ forced: Set(result.uncheckedDocumentIds).union([changed]),+ due: same ? Date().addingTimeInterval(300) : nil+ )+ }+ update(id) {+ if pending[id] == nil {+ $0 = Self.state(record, result.summary)+ } else {+ $0.status = .pending+ $0.documentCount = result.summary.documentCount+ $0.lastUpdatedAtMs = result.summary.lastUpdatedAtMs+ $0.indexAvailable = hadIndex && result.summary.initialComplete && result.summary.documentCount > 0+ }+ }+ } else {+ let result = try await provider.inspectFreshness(id, operation: operation)+ update(id) {+ $0 = Self.state(record, result.summary, progress: $0.progress)+ }+ if result.changed {+ enqueue(id, forced: Set(result.forcedDocumentIds), due: result.summary.initialComplete ? nil : .distantPast)+ update(id) {+ $0.status = .pending+ $0.error = nil+ }+ }+ }+ } catch NotesError.Cancelled {+ if snapshot == nil { scans.insert(id) }+ let saved = try? await provider.inspect(id)+ update(id) {+ $0.status = .pending+ if let saved { $0.lastUpdatedAtMs = saved.lastUpdatedAtMs }+ $0.indexAvailable = hadIndex && saved?.initialComplete == true && (saved?.documentCount ?? 0) > 0+ }+ } catch NotesError.RebuildRequired {+ if snapshot == nil {+ enqueue(id, rebuild: true, due: .distantPast)+ update(id) {+ $0.status = .pending+ $0.indexAvailable = false+ }+ } else {+ fail(id, NotesError.RebuildRequired)+ }+ } catch {+ fail(id, error)+ }+ }+ active = Run(operation: operation, task: task)+ }++ private func fail(_ id: String, _ error: Error) {+ guard collections.contains(where: { $0.id == id }) else { return }+ failed.insert(id)+ scans.remove(id)+ update(id) {+ $0.status = (error as? NotesError) == .Unavailable ? .unavailable : .error+ $0.progress = nil+ $0.error = Self.message(error)+ $0.completedEmpty = false+ }+ }+ private static func message(_ error: Error) -> String {+ guard let error = error as? NotesError else { return "Could not update Your Notes. Please try again." }+ switch error {+ case .Unavailable: return "This Notes folder is unavailable. Check access and try again. If access has expired, remove the folder and add it again."+ case .SourceChanged: return "The folder changed. Retry to update Your Notes."+ case .SourceRead(let detail), .InvalidInput(let detail): return detail+ case .Storage: return "Could not save the Notes index. Check storage and try again."+ case .RebuildRequired: return "The Notes index needs rebuilding. Retry to update Your Notes."+ default: return "Could not update Your Notes. Please try again."+ }+ }++ private func update(_ id: String, _ body: (inout NoteCollectionState) -> Void) {+ guard let index = collections.firstIndex(where: { $0.id == id }) else { return }+ body(&collections[index])+ }+ private static func state(_ record: NotesCollectionInfo, _ summary: NotesSummary, progress: UInt8? = nil) -> NoteCollectionState {+ let ready = summary.initialComplete && summary.documentCount > 0+ return NoteCollectionState(id: record.id, label: record.label,+ status: ready ? .ready : summary.initialComplete ? .error : .pending,+ documentCount: summary.documentCount, lastUpdatedAtMs: summary.lastUpdatedAtMs, indexAvailable: ready,+ progress: summary.initialComplete ? nil : progress,+ error: summary.initialComplete && !ready ? emptyNotes : nil,+ completedEmpty: summary.initialComplete && !ready)+ }+}++struct NotesPreview: Identifiable {+ let id = UUID()+ let url: URL+}diff --git a/apple/apps/ensu/Ensu/Settings/KnowledgeSettingsView.swift b/apple/apps/ensu/Ensu/Settings/KnowledgeSettingsView.swiftindex 3864e0123d..23c66325b6 100644--- a/apple/apps/ensu/Ensu/Settings/KnowledgeSettingsView.swift+++ b/apple/apps/ensu/Ensu/Settings/KnowledgeSettingsView.swift@@ -24,7 +24,7 @@ struct KnowledgeSettingsView: View {@ViewBuilderprivate func packCard(_ pack: KnowledgePackState) -> some View {let attribution = pack.config.attribution- VStack(alignment: .leading, spacing: EnsuSpacing.sm) {+ EnsuCard {HStack {VStack(alignment: .leading, spacing: EnsuSpacing.xs) {Text(pack.config.label)@@ -64,16 +64,9 @@ struct KnowledgeSettingsView: View {.labelsHidden().tint(EnsuColor.accent)} else if store.downloadsAllowed && pack.status == .download && !pack.isMutating {- Button {+ CompactButton(text: "Download") {store.downloadOrUpdate(stableId: pack.id)- } label: {- Text("Download")- .font(EnsuTypography.small)- .padding(.horizontal, EnsuSpacing.xs)}- .buttonStyle(.borderedProminent)- .controlSize(.small)- .tint(EnsuColor.accent)}}@@ -96,16 +89,9 @@ struct KnowledgeSettingsView: View {.accessibilityLabel("Cancel download")}} else if store.downloadsAllowed && pack.status == .updateAvailable {- Button {+ CompactButton(text: "Update") {store.downloadOrUpdate(stableId: pack.id)- } label: {- Text("Update")- .font(EnsuTypography.small)- .padding(.horizontal, EnsuSpacing.xs)}- .buttonStyle(.borderedProminent)- .controlSize(.small)- .tint(EnsuColor.accent)}if let error = pack.errorMessage {@@ -114,76 +100,50 @@ struct KnowledgeSettingsView: View {.foregroundStyle(EnsuColor.error)}}- .padding(EnsuSpacing.lg)- .frame(maxWidth: .infinity, alignment: .leading)- .background(EnsuColor.fillFaint)- .clipShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.card, style: .continuous))}}private struct PackAttributionSheet: View {let config: KnowledgeDatasetConfig- @Environment(\.dismiss) private var dismissvar body: some View {let attribution = config.attribution- NavigationStack {- ScrollView {- VStack(alignment: .leading, spacing: EnsuSpacing.sm) {- Text(config.label)- .font(EnsuTypography.large)- .foregroundStyle(EnsuColor.textPrimary)+ AttributionSheet(title: "Attribution") {+ EnsuCard(padding: EnsuSpacing.md) {+ Text(config.label)+ .font(EnsuTypography.large)+ .foregroundStyle(EnsuColor.textPrimary)- Divider()+ Divider()- Text("From \(attribution.credit)")- .font(EnsuTypography.body)- .foregroundStyle(EnsuColor.textPrimary)+ Text("From \(attribution.credit)")+ .font(EnsuTypography.body)+ .foregroundStyle(EnsuColor.textPrimary)- Text(attribution.modificationNotice)- .font(EnsuTypography.body)- .foregroundStyle(EnsuColor.textPrimary)+ Text(attribution.modificationNotice)+ .font(EnsuTypography.body)+ .foregroundStyle(EnsuColor.textPrimary)- HStack(spacing: EnsuSpacing.md) {- Link(destination: URL(string: attribution.publicPackUrl)!) {- Label("Source", systemImage: "arrow.up.right.square")- }- Link(destination: URL(string: attribution.licenseUrl)!) {- Label("License", systemImage: "doc.text")- }+ HStack(spacing: EnsuSpacing.md) {+ Link(destination: URL(string: attribution.publicPackUrl)!) {+ Label("Source", systemImage: "arrow.up.right.square")+ .frame(minHeight: 44)+ }+ Link(destination: URL(string: attribution.licenseUrl)!) {+ Label("License", systemImage: "doc.text")+ .frame(minHeight: 44)}- .font(EnsuTypography.mini)- .lineLimit(1)- .frame(maxWidth: .infinity, alignment: .leading)-- Divider()-- Text("Wikimedia and Ensu are not affiliated. Wikimedia project names identify the source material only.")- .font(EnsuTypography.small)- .foregroundStyle(EnsuColor.textMuted)}- .padding(EnsuSpacing.md)+ .font(EnsuTypography.mini)+ .lineLimit(1).frame(maxWidth: .infinity, alignment: .leading)- .background(EnsuColor.fillFaint)- .clipShape(- RoundedRectangle(- cornerRadius: EnsuCornerRadius.card,- style: .continuous- )- )- .padding(EnsuSpacing.lg)- .frame(maxWidth: .infinity, alignment: .leading)- }- .background(EnsuColor.backgroundBase)- .navigationTitle("Attribution")- .navigationBarTitleDisplayMode(.inline)- .toolbar {- ToolbarItem(placement: .primaryAction) {- Button("Done") { dismiss() }- }++ Divider()++ Text("Wikimedia and Ensu are not affiliated. Wikimedia project names identify the source material only.")+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.textMuted)}}- .presentationDetents([.medium, .large])- .presentationDragIndicator(.visible)}}diff --git a/apple/apps/ensu/Ensu/Settings/NotesSettingsView.swift b/apple/apps/ensu/Ensu/Settings/NotesSettingsView.swiftnew file mode 100644index 0000000000..8dec591358--- /dev/null+++ b/apple/apps/ensu/Ensu/Settings/NotesSettingsView.swift@@ -0,0 +1,136 @@+import SwiftUI+import UniformTypeIdentifiers+import QuickLook++struct NotesSettingsView: View {+ @ObservedObject var store: NotesStore+ @State private var showPicker = false+ @State private var removing: NoteCollectionState?++ var body: some View {+ ScrollView {+ VStack(alignment: .leading, spacing: EnsuSpacing.md) {+ ForEach(store.collections) { collection in+ EnsuCard {+ HStack {+ VStack(alignment: .leading, spacing: EnsuSpacing.xs) {+ Text(collection.label)+ .font(EnsuTypography.large)+ .foregroundStyle(EnsuColor.textPrimary)+ .fixedSize(horizontal: false, vertical: true)+ Text(summary(collection))+ .font(EnsuTypography.mini)+ .foregroundStyle(EnsuColor.textMuted)+ .fixedSize(horizontal: false, vertical: true)+ }+ Spacer()+ ActionButton(icon: "Delete01Icon", tooltip: "Remove \(collection.label)", size: 44) {+ removing = collection+ }+ }+ if let progress = collection.progress {+ HStack(spacing: EnsuSpacing.sm) {+ ProgressView(value: Double(progress), total: 100)+ .tint(EnsuColor.action)+ Text("\(progress)%")+ .font(EnsuTypography.mini)+ .foregroundStyle(EnsuColor.textMuted)+ }+ } else if collection.status == .indexing || collection.status == .updating {+ ProgressView().tint(EnsuColor.action)+ }+ if collection.status == .pending {+ Text("Indexing will continue when the app and model are ready.")+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.textMuted)+ }+ if let error = collection.error {+ Text(error)+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.error)+ }+ if collection.status == .error || collection.status == .unavailable {+ CompactButton(text: "Retry") {+ store.retry(collection.id)+ }+ }+ }+ }+ CompactButton(text: "Add notes folder") {+ showPicker = true+ }+ Text("Ensu reads and indexes markdown files in the selected folder. Source files are never modified.")+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.textMuted)+ if let error = store.operationError {+ Text(error)+ .font(EnsuTypography.small)+ .foregroundStyle(EnsuColor.error)+ }+ }+ .padding(EnsuSpacing.lg)+ }+ .background(EnsuColor.backgroundBase)+ .navigationTitle("Your Notes")+ .navigationBarTitleDisplayMode(.inline)+ .sheet(isPresented: $showPicker) { NotesFolderPicker { store.add($0) } }+ .alert("Remove folder?", isPresented: Binding(get: { removing != nil }, set: { if !$0 { removing = nil } })) {+ Button("Remove", role: .destructive) {+ if let collection = removing { store.remove(collection.id) }+ removing = nil+ }+ Button("Cancel", role: .cancel) { removing = nil }+ } message: { Text("Remove this folder from Your Notes? Your original files will be kept.") }+ }++ private func summary(_ collection: NoteCollectionState) -> String {+ let count = collection.documentCount+ let indexing = collection.status == .indexing || collection.status == .updating+ var text = indexing && count == 0 ? "Preparing notesβ¦" : "\(count) \(count == 1 ? "note indexed" : "notes indexed")"+ if let timestamp = collection.lastUpdatedAtMs {+ let date = Date(timeIntervalSince1970: Double(timestamp) / 1000)+ text += " Β· Updated at \(date.formatted(date: .abbreviated, time: .shortened))"+ }+ return text+ }+}++private struct NotesFolderPicker: UIViewControllerRepresentable {+ let selected: (URL) -> Void+ func makeCoordinator() -> Coordinator { Coordinator(selected: selected) }+ func makeUIViewController(context: Context) -> UIDocumentPickerViewController {+ let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.folder], asCopy: false)+ picker.allowsMultipleSelection = false+ picker.delegate = context.coordinator+ return picker+ }+ func updateUIViewController(_ controller: UIDocumentPickerViewController, context: Context) {}+ final class Coordinator: NSObject, UIDocumentPickerDelegate {+ let selected: (URL) -> Void+ init(selected: @escaping (URL) -> Void) { self.selected = selected }+ func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {+ if let url = urls.first { selected(url) }+ }+ }+}++struct NotesPreviewView: UIViewControllerRepresentable {+ let url: URL+ func makeCoordinator() -> Coordinator { Coordinator(url: url) }+ func makeUIViewController(context: Context) -> QLPreviewController {+ let preview = QLPreviewController()+ preview.dataSource = context.coordinator+ return preview+ }+ func updateUIViewController(_ controller: QLPreviewController, context: Context) {}+ static func dismantleUIViewController(_ controller: QLPreviewController, coordinator: Coordinator) {+ controller.dataSource = nil+ try? FileManager.default.removeItem(at: coordinator.url.deletingLastPathComponent())+ }+ final class Coordinator: NSObject, QLPreviewControllerDataSource {+ let url: URL+ init(url: URL) { self.url = url }+ func numberOfPreviewItems(in controller: QLPreviewController) -> Int { 1 }+ func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { url as NSURL }+ }+}diff --git a/apple/apps/ensu/Ensu/Settings/SettingsView.swift b/apple/apps/ensu/Ensu/Settings/SettingsView.swiftindex e6c477d31b..a7d6291365 100644--- a/apple/apps/ensu/Ensu/Settings/SettingsView.swift+++ b/apple/apps/ensu/Ensu/Settings/SettingsView.swift@@ -5,6 +5,7 @@ let isEnsuPacksEnabled = truestruct SettingsView: View {@ObservedObject var knowledgeStore: KnowledgeStore+ @ObservedObject var notesStore: NotesStorelet onSignIn: () -> Void@Environment(\.dismiss) private var dismiss@@ -165,7 +166,11 @@ struct SettingsView: View {private var signInTitle: String { "Sign In to Backup" }private var allItems: [SettingsItem] {- var items: [SettingsItem] = []+ var items = [SettingsItem(+ title: "Your Notes",+ iconName: "Folder01Icon",+ destination: AnyView(NotesSettingsView(store: notesStore))+ )]if isEnsuPacksEnabled {items.append(SettingsItem(title: "Ensu Packs",diff --git a/apple/apps/ensu/EnsuTests/NotesIntegrationTests.swift b/apple/apps/ensu/EnsuTests/NotesIntegrationTests.swiftnew file mode 100644index 0000000000..a70a75c3a1--- /dev/null+++ b/apple/apps/ensu/EnsuTests/NotesIntegrationTests.swift@@ -0,0 +1,49 @@+import XCTest+@testable import Ensu++private final class UnavailableNotesSource: NotesSource {+ func listDocuments() throws -> [NotesDocument] {+ throw NotesError.Unavailable+ }+ func readDocument(documentId: String) throws -> NotesRead {+ throw NotesError.Unavailable+ }+}++final class NotesIntegrationTests: XCTestCase {+ private let collectionID = "123e4567-e89b-12d3-a456-426614174000"++ func testMissingDocumentRequiresReconciliationOnlyWhileRootIsAvailable() throws {+ let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)+ defer { try? FileManager.default.removeItem(at: root) }+ XCTAssertThrowsError(try Data(contentsOf: root.appendingPathComponent("missing.md"))) {+ XCTAssertEqual(IOSNotesSource.documentReadError($0, root: root) as? NotesError, .SourceChanged)+ }+ let missing = NSError(domain: NSPOSIXErrorDomain, code: Int(ENOENT))+ let coordinated = NSError(domain: NSCocoaErrorDomain, code: NSFileReadUnknownError,+ userInfo: [NSUnderlyingErrorKey: missing])+ XCTAssertEqual(IOSNotesSource.documentReadError(coordinated, root: root) as? NotesError, .SourceChanged)+ let transient = NSError(domain: NSCocoaErrorDomain, code: NSFileReadUnknownError)+ guard case .SourceRead = IOSNotesSource.documentReadError(transient, root: root) as? NotesError else {+ return XCTFail("A provider failure must remain a read error")+ }+ try FileManager.default.removeItem(at: root)+ XCTAssertEqual(IOSNotesSource.documentReadError(coordinated, root: root) as? NotesError, .Unavailable)+ }++ func testNativeSourceFailureAndCancellationCrossTheBridge() throws {+ let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: root) }+ let collection = try NotesCollection(indexRoot: root.path, collectionId: collectionID)+ let source = UnavailableNotesSource()+ XCTAssertThrowsError(try collection.inspectFreshness(source: source, cancellation: NotesCancellation())) {+ XCTAssertEqual($0 as? NotesError, .Unavailable)+ }+ let cancellation = NotesCancellation()+ cancellation.cancel()+ XCTAssertThrowsError(try collection.inspectFreshness(source: source, cancellation: cancellation)) {+ XCTAssertEqual($0 as? NotesError, .Cancelled)+ }+ }+}diff --git a/rust/apps/ensu/src-tauri/src/commands/notes.rs b/rust/apps/ensu/src-tauri/src/commands/notes.rsindex 615ac39572..45fa4a9484 100644--- a/rust/apps/ensu/src-tauri/src/commands/notes.rs+++ b/rust/apps/ensu/src-tauri/src/commands/notes.rs@@ -14,7 +14,7 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH};use ente_ensu::model;use ente_ensu::notes::{NoteSourceReference, NotesCollectionIndex, NotesError, NotesIndexOutcome, NotesIndexProgress,- cleanup_unreferenced_notes_shards, notes_content_revision,+ cleanup_unreferenced_notes_shards, notes_content_revision, remove_notes_collection,};use notify::RecommendedWatcher;use serde::Serialize;@@ -47,7 +47,6 @@ pub(crate) use watcher::{mark_collection_dirty, mark_reference_stale};const NOTES_DIRECTORY: &str = "notes";const NOTES_INDEX_DIRECTORY: &str = "indexes";const NOTES_OWNERSHIP_LOCK_FILE: &str = ".owner.lock";-const NOTES_EMBEDDING_TITLE_MAX_UTF8_BYTES: usize = 512;const NOTES_QUIET_PERIOD_MS: i64 = 5 * 60 * 1_000;const NOTES_STATE_CHANGED_EVENT: &str = "notes-state-changed";const NOTES_MAX_SCAN_ENTRIES: usize = 250_000;@@ -861,7 +860,6 @@ pub async fn notes_remove_collection(fn remove_collection(notes_state: &State, collection_id: &str) -> Result<(), ApiError> {let collection = notes_state.registered_collection(collection_id)?;- let derived_directory = notes_state.index_root.join(&collection.id);{let mut store = notes_state.registry@@ -890,18 +888,12 @@ fn remove_collection(notes_state: &State, collection_id: &str) -> Result<(), Api.lock().unwrap_or_else(PoisonError::into_inner).remove(collection_id);- let cleanup_result = (|| -> Result<(), ApiError> {- if path_exists(&derived_directory)? {- remove_owned_entry(&derived_directory)?;- }- Ok(())- })();- if let Err(error) = cleanup_result {+ if let Err(error) = remove_notes_collection(¬es_state.index_root, &collection.id) {crate::logging::log("Notes",format!("removed collection but could not clean derived index collection={} error={}",- collection_id, error.message+ collection_id, error),);}diff --git a/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs b/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rsindex 17f57b4566..5634c6f967 100644--- a/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs+++ b/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs@@ -4,7 +4,7 @@ use ente_ensu::llm;use ente_ensu::notes::{NotesCollectionIndex, NotesDocumentLoad, NotesError, NotesIndexInput, NotesIndexOutcome,NotesIndexProgress, NotesIndexingError, NotesRevisionStatus, index_notes_collection,- notes_content_revision, prepare_notes_document,+ notes_content_revision, prepare_notes_document, remove_notes_collection,};use crate::commands::common::ApiError;@@ -13,10 +13,7 @@ use super::registry::RegisteredCollection;use super::source::{canonical_source_root, inventory_source_root, read_collection_source, source_root_is_available,};-use super::{- NOTES_EMBEDDING_TITLE_MAX_UTF8_BYTES, UpdateSnapshot, check_cancelled, notes_error,- path_exists, remove_owned_entry,-};+use super::{UpdateSnapshot, check_cancelled, notes_error};pub(super) fn index_collection(index_root: &Path,@@ -39,10 +36,7 @@ pub(super) fn index_collection(check_cancelled(cancellation_epoch, retrieval_epoch)})?;if update.rebuild_derived_index {- let derived_directory = index_root.join(&collection.id);- if path_exists(&derived_directory)? {- remove_owned_entry(&derived_directory)?;- }+ remove_notes_collection(index_root, &collection.id).map_err(notes_error)?;}let mut embedding_context = None;index_notes_collection(@@ -71,26 +65,13 @@ pub(super) fn index_collection(let context = embedding_context.as_ref().expect("embedding context was initialized");- let mut embeddings = Vec::with_capacity(prepared.chunks.len());- for chunk in &prepared.chunks {- check_cancelled(cancellation_epoch, retrieval_epoch)?;- let full_embedding_title = match chunk.section.as_deref() {- Some(section) if section != prepared.title => {- format!("{} β {section}", prepared.title)- }- _ => prepared.title.clone(),- };- let embedding_title = truncate_utf8_bytes(- &full_embedding_title,- NOTES_EMBEDDING_TITLE_MAX_UTF8_BYTES,- );- match context.embed_document(embedding_title, &chunk.text) {- Ok(embedding) => embeddings.push(embedding),- Err(llm::Error::PromptTooLong { .. }) => return Ok(None),- Err(error) => return Err(crate::commands::llm::llm_api_error(error)),- }- }- Ok(Some(embeddings))+ prepared+ .embed(|title, text| {+ check_cancelled(cancellation_epoch, retrieval_epoch)+ .map_err(|_| llm::Error::Cancelled)?;+ context.embed_document(title, text)+ })+ .map_err(crate::commands::llm::llm_api_error)},|document_id, revision| {verify_notes_document_revision(&canonical_root, document_id, revision)@@ -162,14 +143,3 @@ fn notes_indexing_error(error: NotesIndexingError<ApiError>) -> ApiError {NotesIndexingError::Adapter(error) => error,}}--fn truncate_utf8_bytes(value: &str, max_bytes: usize) -> &str {- if value.len() <= max_bytes {- return value;- }- let mut end = max_bytes;- while !value.is_char_boundary(end) {- end -= 1;- }- &value[..end]-}diff --git a/rust/bindings/uniffi/ensu/src/lib.rs b/rust/bindings/uniffi/ensu/src/lib.rsindex 036e13e6b7..116bea45d1 100644--- a/rust/bindings/uniffi/ensu/src/lib.rs+++ b/rust/bindings/uniffi/ensu/src/lib.rs@@ -6,5 +6,6 @@ pub mod db;pub mod image;pub mod llm;pub mod log;+pub mod notes;pub mod retrieval;pub mod transcription;diff --git a/rust/bindings/uniffi/ensu/src/llm.rs b/rust/bindings/uniffi/ensu/src/llm.rsindex cb10e30742..75003ad4fd 100644--- a/rust/bindings/uniffi/ensu/src/llm.rs+++ b/rust/bindings/uniffi/ensu/src/llm.rs@@ -122,7 +122,7 @@ impl LlmModel {#[derive(uniffi::Object)]pub struct LlmContext {- handle: llm::ContextRef,+ pub(crate) handle: llm::ContextRef,}#[uniffi::export]diff --git a/rust/bindings/uniffi/ensu/src/notes.rs b/rust/bindings/uniffi/ensu/src/notes.rsnew file mode 100644index 0000000000..2a6c89933d--- /dev/null+++ b/rust/bindings/uniffi/ensu/src/notes.rs@@ -0,0 +1,413 @@+use std::collections::BTreeSet;+use std::path::{Path, PathBuf};+use std::sync::{+ Arc,+ atomic::{AtomicBool, Ordering},+};++use ente_ensu::{llm, notes as core};+use thiserror::Error;++use crate::llm::LlmContext;++#[derive(Debug, Error, uniffi::Error)]+pub enum NotesError {+ #[error("Notes operation cancelled")]+ Cancelled,+ #[error("The note changed during reading")]+ SourceChanged,+ #[error("The Notes folder is unavailable")]+ Unavailable,+ #[error("Notes collection is not ready")]+ NotReady,+ #[error("The Notes index needs rebuilding")]+ RebuildRequired,+ #[error("{detail}")]+ SourceRead { detail: String },+ #[error("{detail}")]+ InvalidInput { detail: String },+ #[error("{detail}")]+ Storage { detail: String },+ #[error("{detail}")]+ Other { detail: String },+}++impl From<core::NotesError> for NotesError {+ fn from(error: core::NotesError) -> Self {+ match error {+ core::NotesError::NotReady => Self::NotReady,+ core::NotesError::InvalidIndex(_)+ | core::NotesError::IncompatibleIndex+ | core::NotesError::Json(_) => Self::RebuildRequired,+ core::NotesError::InvalidInput(detail)+ | core::NotesError::CollectionTooLarge(detail) => Self::InvalidInput { detail },+ core::NotesError::Io(error) => Self::Storage {+ detail: error.to_string(),+ },+ }+ }+}++impl From<core::NotesIndexingError<Self>> for NotesError {+ fn from(error: core::NotesIndexingError<Self>) -> Self {+ match error {+ core::NotesIndexingError::Notes(error) => error.into(),+ core::NotesIndexingError::Adapter(error) => error,+ }+ }+}++#[derive(Default, uniffi::Object)]+pub struct NotesCancellation {+ cancelled: AtomicBool,+}++#[uniffi::export]+impl NotesCancellation {+ #[uniffi::constructor]+ pub fn new() -> Arc<Self> {+ Arc::new(Self::default())+ }++ pub fn cancel(&self) {+ self.cancelled.store(true, Ordering::SeqCst);+ }++ pub fn check(&self) -> Result<(), NotesError> {+ if self.cancelled.load(Ordering::SeqCst) {+ Err(NotesError::Cancelled)+ } else {+ Ok(())+ }+ }+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesDocument {+ pub document_id: String,+ pub size: u64,+ pub modified_at_ms: Option<i64>,+}++impl From<NotesDocument> for core::NotesSourceDocument {+ fn from(value: NotesDocument) -> Self {+ Self {+ document_id: value.document_id,+ size: value.size,+ modified_at_ms: value.modified_at_ms,+ }+ }+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesRead {+ pub bytes: Vec<u8>,+ pub source: NotesDocument,+}++#[uniffi::export(callback_interface)]+pub trait NotesSource: Send + Sync {+ fn list_documents(&self) -> Result<Vec<NotesDocument>, NotesError>;+ fn read_document(&self, document_id: String) -> Result<NotesRead, NotesError>;+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesProgress {+ pub percentage: u8,+ pub indexed_document_count: u64,+}++#[uniffi::export(callback_interface)]+pub trait NotesProgressCallback: Send + Sync {+ fn on_progress(&self, progress: NotesProgress);+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesSummary {+ pub initial_complete: bool,+ pub document_count: u64,+ pub last_updated_at_ms: Option<i64>,+}++impl From<&core::NotesIndexWriter> for NotesSummary {+ fn from(writer: &core::NotesIndexWriter) -> Self {+ Self {+ initial_complete: writer.initial_inventory_complete(),+ document_count: writer.indexed_document_count() as u64,+ last_updated_at_ms: writer.last_updated_at_ms(),+ }+ }+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesFreshness {+ pub summary: NotesSummary,+ pub changed: bool,+ pub forced_document_ids: Vec<String>,+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesLimits {+ pub max_source_bytes: u64,+ pub max_collection_source_bytes: u64,+ pub max_collection_documents: u64,+ pub max_grounding_hits: u32,+}++#[uniffi::export]+pub fn notes_limits() -> NotesLimits {+ NotesLimits {+ max_source_bytes: core::NOTES_MAX_SOURCE_BYTES as u64,+ max_collection_source_bytes: core::NOTES_MAX_COLLECTION_SOURCE_BYTES,+ max_collection_documents: core::NOTES_MAX_COLLECTION_DOCUMENTS as u64,+ max_grounding_hits: ente_ensu::retrieval::MAX_NOTES_GROUNDING_HITS as u32,+ }+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesIndexOptions {+ pub forced_document_ids: Vec<String>,+ pub rebuild: bool,+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesOutcome {+ pub summary: NotesSummary,+ pub changed_document_id: Option<String>,+ pub unchecked_document_ids: Vec<String>,+}++#[derive(Clone, Debug, uniffi::Record)]+pub struct NotesHit {+ pub collection_id: String,+ pub document_id: String,+ pub revision: String,+ pub score: f32,+ pub title: String,+ pub section: Option<String>,+ pub text: String,+}++impl From<core::NotesSearchHit> for NotesHit {+ fn from(value: core::NotesSearchHit) -> Self {+ Self {+ collection_id: value.collection_id,+ document_id: value.document_id,+ revision: value.revision,+ score: value.score,+ title: value.title,+ section: value.section,+ text: value.text,+ }+ }+}++impl From<NotesHit> for core::NotesSearchHit {+ fn from(value: NotesHit) -> Self {+ Self {+ collection_id: value.collection_id,+ document_id: value.document_id,+ revision: value.revision,+ score: value.score,+ title: value.title,+ section: value.section,+ text: value.text,+ }+ }+}++#[derive(uniffi::Object)]+pub struct NotesCollection {+ root: PathBuf,+ id: String,+}++#[uniffi::export]+impl NotesCollection {+ #[uniffi::constructor]+ pub fn new(index_root: String, collection_id: String) -> Result<Arc<Self>, NotesError> {+ let id = uuid::Uuid::parse_str(&collection_id).map_err(|_| NotesError::InvalidInput {+ detail: "Invalid Notes collection ID".into(),+ })?;+ if id.hyphenated().to_string() != collection_id || !Path::new(&index_root).is_absolute() {+ return Err(NotesError::InvalidInput {+ detail: "Invalid Notes index location".into(),+ });+ }+ Ok(Arc::new(Self {+ root: index_root.into(),+ id: collection_id,+ }))+ }++ pub fn inspect(&self) -> Result<NotesSummary, NotesError> {+ let writer = core::NotesIndexWriter::open(&self.root, self.id.clone())?;+ Ok((&writer).into())+ }++ pub fn inspect_freshness(+ &self,+ source: Box<dyn NotesSource>,+ cancellation: Arc<NotesCancellation>,+ ) -> Result<NotesFreshness, NotesError> {+ cancellation.check()?;+ let inventory = inventory(source.as_ref(), &cancellation, &self.id)?;+ let writer = core::NotesIndexWriter::open(&self.root, self.id.clone())?;+ let freshness = core::inspect_notes_freshness(+ &writer,+ &inventory,+ || cancellation.check(),+ |id| match read(source.as_ref(), id, &cancellation) {+ Ok(value) => Ok(core::NotesDocumentContent::Bytes(value.bytes)),+ Err(NotesError::SourceChanged) => Ok(core::NotesDocumentContent::Changed),+ Err(error) => Err(error),+ },+ )?;+ Ok(NotesFreshness {+ summary: (&writer).into(),+ changed: freshness.changed,+ forced_document_ids: freshness.forced_document_ids,+ })+ }++ pub fn index(+ &self,+ source: Box<dyn NotesSource>,+ context: Arc<LlmContext>,+ cancellation: Arc<NotesCancellation>,+ progress: Box<dyn NotesProgressCallback>,+ options: NotesIndexOptions,+ ) -> Result<NotesOutcome, NotesError> {+ let NotesIndexOptions {+ forced_document_ids,+ rebuild,+ } = options;+ cancellation.check()?;+ let inventory = inventory(source.as_ref(), &cancellation, &self.id)?;+ cancellation.check()?;+ let remove = core::notes_index_needs_rebuild(&self.root, &self.id, rebuild)?;+ cancellation.check()?;+ if remove {+ self.remove()?;+ }+ let outcome = core::index_notes_collection(+ core::NotesIndexInput {+ index_root: &self.root,+ collection_id: self.id.clone(),+ inventory,+ forced_document_ids: &forced_document_ids,+ force_full_hash: false,+ },+ || cancellation.check(),+ |id| match read(source.as_ref(), id, &cancellation) {+ Ok(value) => {+ let source = value.source.into();+ Ok(match core::prepare_notes_document(id, &value.bytes) {+ Ok(document) => core::NotesDocumentLoad::Prepared { document, source },+ Err(_) => core::NotesDocumentLoad::Unindexable { source },+ })+ }+ Err(NotesError::SourceChanged) => Ok(core::NotesDocumentLoad::Changed),+ Err(error) => Err(error),+ },+ |prepared| {+ prepared+ .embed(|title, text| {+ cancellation.check().map_err(|_| llm::Error::Cancelled)?;+ context.handle.embed_document(title, text)+ })+ .map_err(|error| match error {+ llm::Error::Cancelled => NotesError::Cancelled,+ error => NotesError::Other {+ detail: error.to_string(),+ },+ })+ },+ |id, revision| match read(source.as_ref(), id, &cancellation) {+ Ok(value) if core::notes_content_revision(&value.bytes) == revision => {+ Ok(core::NotesRevisionStatus::Matches {+ source: value.source.into(),+ })+ }+ Ok(_) | Err(NotesError::SourceChanged) => Ok(core::NotesRevisionStatus::Changed),+ Err(error) => Err(error),+ },+ |p| {+ progress.on_progress(NotesProgress {+ percentage: p.percentage,+ indexed_document_count: p.indexed_document_count,+ })+ },+ )?;+ Ok(NotesOutcome {+ summary: self.inspect()?,+ changed_document_id: outcome.changed_during_indexing,+ unchecked_document_ids: outcome.unchecked_document_ids,+ })+ }++ 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()),+ }+ }++ pub fn remove(&self) -> Result<(), NotesError> {+ core::remove_notes_collection(&self.root, &self.id).map_err(Into::into)+ }+}++fn inventory(+ source: &dyn NotesSource,+ cancellation: &NotesCancellation,+ id: &str,+) -> Result<Vec<core::NotesSourceDocument>, NotesError> {+ let inventory: Vec<core::NotesSourceDocument> = source+ .list_documents()?+ .into_iter()+ .map(Into::into)+ .collect();+ cancellation.check()?;+ core::NotesIndexWriter::validate_inventory_capacity(id, &inventory)?;+ let ids: BTreeSet<_> = inventory.iter().map(|v| &v.document_id).collect();+ if ids.len() != inventory.len() {+ return Err(NotesError::InvalidInput {+ detail: "Ambiguous Notes document IDs".into(),+ });+ }+ Ok(inventory)+}++fn read(+ source: &dyn NotesSource,+ id: &str,+ cancellation: &NotesCancellation,+) -> Result<NotesRead, NotesError> {+ cancellation.check()?;+ core::validate_document_id(id)?;+ let value = source.read_document(id.into())?;+ cancellation.check()?;+ if value.source.document_id != id+ || value.source.size != value.bytes.len() as u64+ || value.bytes.len() > core::NOTES_MAX_SOURCE_BYTES+ {+ return Err(NotesError::SourceChanged);+ }+ Ok(value)+}++#[uniffi::export]+pub fn notes_content_revision(bytes: Vec<u8>) -> String {+ core::notes_content_revision(&bytes)+}++#[uniffi::export]+pub fn validate_notes_document_id(document_id: String) -> Result<(), NotesError> {+ core::validate_document_id(&document_id).map_err(Into::into)+}diff --git a/rust/bindings/uniffi/ensu/src/retrieval.rs b/rust/bindings/uniffi/ensu/src/retrieval.rsindex 06a9a4242a..94a97e62af 100644--- a/rust/bindings/uniffi/ensu/src/retrieval.rs+++ b/rust/bindings/uniffi/ensu/src/retrieval.rs@@ -281,3 +281,174 @@ pub fn knowledge_pack_asset(stable_id: String) -> Result<Arc<Asset>, KnowledgeRedetail: format!("invalid retrieval input: {error}"),})}++#[derive(Debug, Clone, uniffi::Record)]+pub struct NoteSourceReference {+ pub collection_id: String,+ pub collection_label: Option<String>,+ pub document_id: String,+ pub indexed_revision: String,+ pub title: String,+ pub section: Option<String>,+}++impl From<ente_ensu::notes::NoteSourceReference> for NoteSourceReference {+ fn from(value: ente_ensu::notes::NoteSourceReference) -> Self {+ Self {+ collection_id: value.collection_id,+ collection_label: value.collection_label,+ document_id: value.document_id,+ indexed_revision: value.indexed_revision,+ title: value.title,+ section: value.section,+ }+ }+}++impl From<NoteSourceReference> for ente_ensu::notes::NoteSourceReference {+ fn from(value: NoteSourceReference) -> Self {+ Self {+ collection_id: value.collection_id,+ collection_label: value.collection_label,+ document_id: value.document_id,+ indexed_revision: value.indexed_revision,+ title: value.title,+ section: value.section,+ }+ }+}++#[uniffi::export]+pub fn with_notes_collection_label(+ reference: NoteSourceReference,+ label: String,+) -> NoteSourceReference {+ ente_ensu::notes::NoteSourceReference::from(reference)+ .with_collection_label(label)+ .into()+}++#[derive(Debug, Clone, uniffi::Enum)]+pub enum GroundedSource {+ EnsuPack { citation: SourceCitation },+ LocalNote { reference: NoteSourceReference },+}++impl From<core::GroundedSource> for GroundedSource {+ fn from(value: core::GroundedSource) -> Self {+ match value {+ core::GroundedSource::EnsuPack { citation } => Self::EnsuPack {+ citation: citation.into(),+ },+ core::GroundedSource::LocalNote { reference } => Self::LocalNote {+ reference: reference.into(),+ },+ }+ }+}++impl From<GroundedSource> for core::GroundedSource {+ fn from(value: GroundedSource) -> Self {+ match value {+ GroundedSource::EnsuPack { citation } => Self::EnsuPack {+ citation: citation.into(),+ },+ GroundedSource::LocalNote { reference } => Self::LocalNote {+ reference: reference.into(),+ },+ }+ }+}++#[derive(Debug, Clone, uniffi::Record)]+pub struct GroundedExcerpt {+ pub score: f32,+ pub source: GroundedSource,+ pub text: String,+}++impl From<core::GroundedExcerpt> for GroundedExcerpt {+ fn from(value: core::GroundedExcerpt) -> Self {+ Self {+ score: value.score,+ source: value.source.into(),+ text: value.text,+ }+ }+}++impl From<GroundedExcerpt> for core::GroundedExcerpt {+ fn from(value: GroundedExcerpt) -> Self {+ Self {+ score: value.score,+ source: value.source.into(),+ text: value.text,+ }+ }+}++#[derive(Debug, Clone, uniffi::Record)]+pub struct GroundedPromptContext {+ pub text: String,+ pub sources: Vec<GroundedSource>,+}++impl From<core::GroundedPromptContext> for GroundedPromptContext {+ fn from(value: core::GroundedPromptContext) -> Self {+ Self {+ text: value.text,+ sources: value.sources.into_iter().map(Into::into).collect(),+ }+ }+}++#[uniffi::export]+pub fn select_mixed_grounding_candidates(+ pack_hits: Vec<KnowledgePromptHit>,+ notes_hits: Vec<crate::notes::NotesHit>,+ notes_limit: u32,+) -> Result<Vec<GroundedExcerpt>, KnowledgeRetrievalError> {+ let packs = pack_hits.into_iter().map(Into::into).collect::<Vec<_>>();+ let notes = notes_hits.into_iter().map(Into::into).collect::<Vec<_>>();+ core::select_mixed_grounding_candidates(&packs, ¬es, notes_limit as usize)+ .map(|hits| hits.into_iter().map(Into::into).collect())+ .map_err(Into::into)+}++#[uniffi::export]+pub fn build_grounded_prompt_context(+ excerpts: Vec<GroundedExcerpt>,+ max_utf8_bytes: u32,+) -> Result<Option<GroundedPromptContext>, KnowledgeRetrievalError> {+ let excerpts = excerpts.into_iter().map(Into::into).collect::<Vec<_>>();+ core::build_grounded_prompt_context(&excerpts, max_utf8_bytes as usize)+ .map(|context| context.map(Into::into))+ .map_err(Into::into)+}++#[derive(Debug, Clone, uniffi::Record)]+pub struct ParsedGroundedAssistantText {+ pub text: String,+ pub sources: Vec<GroundedSource>,+ pub source_labels: Vec<String>,+}++#[uniffi::export]+pub fn parse_grounded_assistant_text(stored_text: String) -> ParsedGroundedAssistantText {+ let parsed = core::parse_grounded_assistant_text(&stored_text);+ let source_labels = core::grounded_source_chip_labels(&parsed.sources);+ ParsedGroundedAssistantText {+ text: parsed.text,+ sources: parsed.sources.into_iter().map(Into::into).collect(),+ source_labels,+ }+}++#[uniffi::export]+pub fn finalize_grounded_assistant_text(+ raw_assistant_text: String,+ sources: Vec<GroundedSource>,+) -> Result<String, KnowledgeRetrievalError> {+ let sources = sources.into_iter().map(Into::into).collect::<Vec<_>>();+ core::finalize_grounded_assistant_text(&raw_assistant_text, &sources).map_err(Into::into)+}diff --git a/rust/crates/ensu/src/notes.rs b/rust/crates/ensu/src/notes.rsindex 1ea826d8d9..ef7cd03d35 100644--- a/rust/crates/ensu/src/notes.rs+++ b/rust/crates/ensu/src/notes.rs@@ -2,6 +2,7 @@ mod chunk;mod document;mod index;mod indexing;+mod maintenance;mod manifest;mod reconcile;mod shard;@@ -51,9 +52,15 @@ pub use indexing::{NotesDocumentLoad, NotesIndexInput, NotesIndexOutcome, NotesIndexProgress, NotesIndexingError,NotesRevisionStatus, index_notes_collection,};+pub use maintenance::{+ NotesDocumentContent, NotesFreshness, inspect_notes_freshness, notes_index_needs_rebuild,+};pub use reconcile::NotesReconciliationPlan;pub use source::NoteSourceReference;-pub use writer::{NotesIndexWriter, ValidatedNotesDocument, cleanup_unreferenced_notes_shards};+pub use writer::{+ NotesIndexWriter, ValidatedNotesDocument, cleanup_unreferenced_notes_shards,+ remove_notes_collection,+};pub fn notes_content_revision(bytes: &[u8]) -> String {sha256_hex(bytes)diff --git a/rust/crates/ensu/src/notes/document.rs b/rust/crates/ensu/src/notes/document.rsindex 44010f52c2..7c71efeaef 100644--- a/rust/crates/ensu/src/notes/document.rs+++ b/rust/crates/ensu/src/notes/document.rs@@ -4,6 +4,8 @@ use super::{validate_document_id,};+const EMBEDDING_TITLE_MAX_UTF8_BYTES: usize = 512;+#[derive(Debug, Clone, PartialEq, Eq)]pub struct NotesSourceDocument {pub document_id: String,@@ -37,6 +39,36 @@ pub struct PreparedNotesChunk {pub text: String,}+impl PreparedNotesDocument {+ pub fn embed(+ &self,+ mut embed_chunk: impl FnMut(&str, &str) -> Result<Vec<f32>, crate::llm::Error>,+ ) -> Result<Option<Vec<Vec<f32>>>, crate::llm::Error> {+ let mut embeddings = Vec::with_capacity(self.chunks.len());+ for chunk in &self.chunks {+ match embed_chunk(&self.embedding_title(chunk), &chunk.text) {+ Ok(vector) => embeddings.push(vector),+ Err(crate::llm::Error::PromptTooLong { .. }) => return Ok(None),+ Err(error) => return Err(error),+ }+ }+ Ok(Some(embeddings))+ }++ fn embedding_title(&self, chunk: &PreparedNotesChunk) -> String {+ let mut title = match chunk.section.as_deref() {+ Some(section) if section != self.title => format!("{} β {section}", self.title),+ _ => self.title.clone(),+ };+ let mut end = title.len().min(EMBEDDING_TITLE_MAX_UTF8_BYTES);+ while !title.is_char_boundary(end) {+ end -= 1;+ }+ title.truncate(end);+ title+ }+}+pub fn prepare_notes_document(document_id: &str,bytes: &[u8],@@ -77,6 +109,28 @@ mod tests {NOTES_CHUNK_OVERLAP_UTF8_BYTES, NOTES_CHUNK_UTF8_BYTES, NOTES_HEADING_MAX_UTF8_BYTES,};+ #[test]+ fn oversized_chunks_discard_partial_document_embeddings() {+ let prepared =+ prepare_notes_document("note.md", b"# Note\n\nFirst\n\n## Second\n\nSecond").unwrap();+ let mut calls = 0;+ let result = prepared+ .embed(|_, _| {+ calls += 1;+ if calls == 1 {+ Ok(vec![1.0])+ } else {+ Err(crate::llm::Error::PromptTooLong {+ tokens: 2,+ context_size: 1,+ })+ }+ })+ .unwrap();+ assert_eq!(calls, 2);+ assert!(result.is_none());+ }+#[test]fn prepares_and_chunks_markdown() {let text = "preamble\r\n\r\n# Local Notes\r\n\r\nintro\r\n\r\n## Indexing\r\n\r\nfirst paragraph\r\n\r\nsecond paragraph\r\n";diff --git a/rust/crates/ensu/src/notes/indexing.rs b/rust/crates/ensu/src/notes/indexing.rsindex 0382fd7c55..8e9b049027 100644--- a/rust/crates/ensu/src/notes/indexing.rs+++ b/rust/crates/ensu/src/notes/indexing.rs@@ -25,11 +25,6 @@ pub enum NotesRevisionStatus {Changed,}-struct PreparedSummary {- revision_and_chunks: Option<(String, u32)>,- source: NotesSourceDocument,-}-#[derive(Debug)]pub enum NotesIndexingError<E> {Notes(NotesError),@@ -89,72 +84,13 @@ pub fn index_notes_collection<E>(} = input;check_for_cancellation().map_err(NotesIndexingError::Adapter)?;NotesIndexWriter::validate_inventory_capacity(&collection_id, &inventory)?;- let mut writer = NotesIndexWriter::open(index_root, collection_id.clone())?;+ let mut writer = NotesIndexWriter::open(index_root, collection_id)?;let index_was_ready = writer.initial_inventory_complete();let plan = writer.plan_reconciliation(&inventory, forced_document_ids, force_full_hash)?;writer.commit_deletions(&plan.deleted_document_ids)?;let source_document_count = inventory.len() as u64;let requested = plan.content_required_document_ids;- let mut prepared_summaries = BTreeMap::<String, PreparedSummary>::new();- let mut prepared_chunk_counts = BTreeMap::<String, u32>::new();- for document_id in &requested {- check_for_cancellation().map_err(NotesIndexingError::Adapter)?;- let (prepared, source) =- match load_document(document_id).map_err(NotesIndexingError::Adapter)? {- NotesDocumentLoad::Prepared { document, source } => (Some(document), source),- NotesDocumentLoad::Unindexable { source } => (None, source),- NotesDocumentLoad::Changed => {- return changed_during_indexing_outcome(- &mut writer,- index_was_ready,- document_id,- &requested,- 0,- );- }- };- if source.document_id != *document_id {- return Err(NotesError::InvalidInput(- "loaded source metadata does not match its document ID".to_string(),- )- .into());- }- let revision_and_chunks = prepared- .map(|prepared| {- let chunk_count = u32::try_from(prepared.chunks.len()).map_err(|_| {- NotesError::CollectionTooLarge(- "The folder contains too many note chunks to index".to_string(),- )- })?;- Ok::<_, NotesError>((prepared.revision, chunk_count))- })- .transpose()?;- let chunk_count = revision_and_chunks.as_ref().map_or(0, |(_, count)| *count);- prepared_chunk_counts.insert(document_id.clone(), chunk_count);- prepared_summaries.insert(- document_id.clone(),- PreparedSummary {- revision_and_chunks,- source,- },- );- }- let mut verified_inventory = inventory.clone();- let inventory_positions = verified_inventory- .iter()- .enumerate()- .map(|(index, source)| (source.document_id.clone(), index))- .collect::<BTreeMap<_, _>>();- for (document_id, summary) in &prepared_summaries {- let index = *inventory_positions- .get(document_id)- .expect("reconciliation returns a source inventory document");- verified_inventory[index] = summary.source.clone();- }- NotesIndexWriter::validate_inventory_capacity(&collection_id, &verified_inventory)?;- writer.validate_reconciliation_capacity(&verified_inventory, &prepared_chunk_counts)?;-let mut checkpointed_document_count =source_document_count.saturating_sub(requested.len() as u64);report_indexing_progress(@@ -163,17 +99,18 @@ pub fn index_notes_collection<E>(source_document_count,&mut on_progress,);- let mut processed_requested = 0_usize;+ let mut source_bytes = inventory.iter().map(|source| source.size).sum::<u64>();+ let source_sizes = inventory+ .into_iter()+ .map(|source| (source.document_id, source.size))+ .collect::<BTreeMap<_, _>>();+let mut documents_since_checkpoint = 0_usize;let mut chunks_since_checkpoint = 0_usize;let document_checkpoint = checkpoint_interval(requested.len(), MIN_CHECKPOINT_DOCUMENTS);- let requested_chunk_count = prepared_chunk_counts- .values()- .fold(0_usize, |total, count| {- total.saturating_add(*count as usize)- });- let chunk_checkpoint = checkpoint_interval(requested_chunk_count, MIN_CHECKPOINT_CHUNKS);- for document_id in &requested {+ let mut processed_chunks = 0_usize;+ let mut first_checkpoint = true;+ for (processed_requested, document_id) in requested.iter().enumerate() {check_for_cancellation().map_err(NotesIndexingError::Adapter)?;let (prepared, source_metadata) =match load_document(document_id).map_err(NotesIndexingError::Adapter)? {@@ -189,62 +126,48 @@ pub fn index_notes_collection<E>();}};- let prepared_matches_preflight =- prepared_summaries.get(document_id).is_some_and(|summary| {- summary.source == source_metadata- && match (&summary.revision_and_chunks, prepared.as_ref()) {- (None, None) => true,- (Some((revision, chunk_count)), Some(prepared)) => {- prepared.revision == *revision- && prepared.chunks.len() == *chunk_count as usize- }- _ => false,- }- });- if !prepared_matches_preflight {- return changed_during_indexing_outcome(- &mut writer,- index_was_ready,- document_id,- &requested,- processed_requested,- );+ if source_metadata.document_id != *document_id {+ return Err(NotesError::InvalidInput(+ "loaded source metadata does not match its document ID".to_string(),+ )+ .into());+ }+ source_metadata.validate()?;+ source_bytes = source_bytes - source_sizes[document_id] + source_metadata.size;+ if source_bytes > super::NOTES_MAX_COLLECTION_SOURCE_BYTES {+ return Err(NotesError::CollectionTooLarge(+ "The folder contains too much note content to index".to_string(),+ )+ .into());}let Some(prepared) = prepared else {writer.commit_deletions(std::slice::from_ref(document_id))?;- processed_requested += 1;continue;};let validated = match writer.validate_document(&prepared, &source_metadata) {Ok(validated) => validated,Err(NotesError::InvalidInput(_)) => {writer.commit_deletions(std::slice::from_ref(document_id))?;- processed_requested += 1;continue;}Err(error) => return Err(error.into()),};- let Some(embeddings) = embed_document(&prepared).map_err(NotesIndexingError::Adapter)?- else {- writer.commit_deletions(std::slice::from_ref(document_id))?;- processed_requested += 1;- continue;+ let embeddings = if validated.needs_embedding() {+ let Some(embeddings) =+ embed_document(&prepared).map_err(NotesIndexingError::Adapter)?+ else {+ writer.commit_deletions(std::slice::from_ref(document_id))?;+ continue;+ };+ Some(embeddings)+ } else {+ None};check_for_cancellation().map_err(NotesIndexingError::Adapter)?;- let NotesRevisionStatus::Matches {- source: verified_source,- } = verify_revision(document_id, &prepared.revision)- .map_err(NotesIndexingError::Adapter)?- else {- return changed_during_indexing_outcome(- &mut writer,- index_was_ready,- document_id,- &requested,- processed_requested,- );- };- if verified_source != source_metadata {+ let revision = verify_revision(document_id, &prepared.revision)+ .map_err(NotesIndexingError::Adapter)?;+ if !matches!(revision, NotesRevisionStatus::Matches { source } if source == source_metadata)+ {return changed_during_indexing_outcome(&mut writer,index_was_ready,@@ -253,12 +176,17 @@ pub fn index_notes_collection<E>(processed_requested,);}- writer.commit_validated_document(validated, &embeddings)?;- processed_requested += 1;+ writer.commit_validated_document(validated, embeddings.as_deref())?;documents_since_checkpoint += 1;chunks_since_checkpoint += prepared.chunks.len();- if processed_requested < requested.len()- && (documents_since_checkpoint >= document_checkpoint+ processed_chunks += prepared.chunks.len();+ let chunk_checkpoint = checkpoint_interval(+ processed_chunks.saturating_mul(requested.len()) / (processed_requested + 1),+ MIN_CHECKPOINT_CHUNKS,+ );+ if processed_requested + 1 < requested.len()+ && (first_checkpoint+ || documents_since_checkpoint >= document_checkpoint|| chunks_since_checkpoint >= chunk_checkpoint){writer.publish(index_was_ready)?;@@ -270,6 +198,7 @@ pub fn index_notes_collection<E>(source_document_count,&mut on_progress,);+ first_checkpoint = false;documents_since_checkpoint = 0;chunks_since_checkpoint = 0;}@@ -385,6 +314,7 @@ mod tests {},|| Ok(()),|_| {+ assert!(!progress.borrow().is_empty());Ok(NotesDocumentLoad::Prepared {document: prepared.clone(),source: verified_source.clone(),@@ -431,88 +361,220 @@ mod tests {}#[test]- fn preserves_the_previous_publication_when_a_source_changes() {+ fn resumes_a_cancelled_checkpoint_without_reembedding_committed_documents() {let temp = tempfile::tempdir().unwrap();- let previous = prepare_notes_document("note.md", b"previous note").unwrap();- let mut writer = NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();- writer- .commit_document(&previous, &embedding(&previous), &source())- .unwrap();- writer.publish(true).unwrap();-- let prepared = prepare_notes_document("note.md", b"updated note").unwrap();- let updated_source = source_at(2);- let load_count = Cell::new(0);- let outcome = index_notes_collection::<()>(- NotesIndexInput {- index_root: temp.path(),- collection_id: COLLECTION_ID.to_string(),- inventory: vec![updated_source.clone()],- forced_document_ids: &[],- force_full_hash: false,- },- || Ok(()),- |_| {- load_count.set(load_count.get() + 1);- Ok(if load_count.get() == 1 {- NotesDocumentLoad::Prepared {- document: prepared.clone(),- source: updated_source.clone(),- }+ let source_for = |document_id: &str| NotesSourceDocument {+ document_id: document_id.to_string(),+ ..source()+ };+ let inventory = (0..12)+ .map(|index| source_for(&format!("note-{index:02}.md")))+ .collect::<Vec<_>>();+ let forced_document_ids = inventory+ .iter()+ .map(|source| source.document_id.clone())+ .collect::<Vec<_>>();+ let input = NotesIndexInput {+ index_root: temp.path(),+ collection_id: COLLECTION_ID.to_string(),+ inventory: inventory.clone(),+ forced_document_ids: &forced_document_ids,+ force_full_hash: false,+ };+ let load_document = |document_id: &str| {+ Ok(NotesDocumentLoad::Prepared {+ document: prepare_notes_document(document_id, b"note").unwrap(),+ source: source_for(document_id),+ })+ };+ let verify_revision = |document_id: &str, _: &str| {+ Ok(NotesRevisionStatus::Matches {+ source: source_for(document_id),+ })+ };+ let cancelled = Cell::new(false);+ let mut embedded = Vec::new();+ let result = index_notes_collection(+ input.clone(),+ || {+ if cancelled.get() {+ Err("cancelled")} else {- NotesDocumentLoad::Changed- })+ Ok(())+ }},- |document| Ok(Some(embedding(document))),- |_, _| {- Ok(NotesRevisionStatus::Matches {- source: updated_source.clone(),- })+ load_document,+ |document| {+ embedded.push(document.document_id.clone());+ Ok(Some(embedding(document)))},- |_| {},+ verify_revision,+ |progress| {+ if progress.processed_document_count > 0+ && progress.processed_document_count < progress.total_document_count+ {+ cancelled.set(true);+ }+ },+ );++ assert!(matches!(+ result,+ Err(NotesIndexingError::Adapter("cancelled"))+ ));+ assert_eq!(embedded.len(), 1);+ {+ let writer = NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();+ assert!(!writer.initial_inventory_complete());+ assert_eq!(writer.indexed_document_ids().collect::<Vec<_>>(), embedded);+ }+ let resumed_progress = RefCell::new(Vec::new());+ let outcome = index_notes_collection(+ input,+ || Ok(()),+ load_document,+ |document| {+ embedded.push(document.document_id.clone());+ Ok(Some(embedding(document)))+ },+ verify_revision,+ |progress| resumed_progress.borrow_mut().push(progress),).unwrap();- assert_eq!(outcome.changed_during_indexing.as_deref(), Some("note.md"));- assert_eq!(outcome.unchecked_document_ids, ["note.md"]);+ assert!(outcome.ready());+ assert_eq!(resumed_progress.borrow()[0].processed_document_count, 0);+ let document_ids = inventory+ .iter()+ .map(|source| source.document_id.as_str())+ .collect::<Vec<_>>();+ assert_eq!(embedded, document_ids);let index = NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()).unwrap();- let hits = index.search(&embedding(&previous)[0]).unwrap();- assert_eq!(hits.len(), 1);- assert_eq!(hits[0].text, "previous note");+ assert_eq!(index.document_count(), inventory.len());}#[test]- fn stops_before_embedding_when_source_metadata_changes_after_preflight() {+ fn preserves_the_previous_publication_when_a_source_changes() {+ for phase in ["load", "revision", "metadata"] {+ let temp = tempfile::tempdir().unwrap();+ let previous = prepare_notes_document("note.md", b"previous note").unwrap();+ let mut writer =+ NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();+ writer+ .commit_document(&previous, &embedding(&previous), &source())+ .unwrap();+ writer.publish(true).unwrap();++ let prepared = prepare_notes_document("note.md", b"updated note").unwrap();+ let updated_source = source_at(2);+ let embedded = Cell::new(false);+ let outcome = index_notes_collection::<()>(+ NotesIndexInput {+ index_root: temp.path(),+ collection_id: COLLECTION_ID.to_string(),+ inventory: vec![updated_source.clone()],+ forced_document_ids: &[],+ force_full_hash: false,+ },+ || Ok(()),+ |_| {+ Ok(if phase == "load" {+ NotesDocumentLoad::Changed+ } else {+ NotesDocumentLoad::Prepared {+ document: prepared.clone(),+ source: updated_source.clone(),+ }+ })+ },+ |document| {+ embedded.set(true);+ Ok(Some(embedding(document)))+ },+ |_, _| {+ Ok(if phase == "revision" {+ NotesRevisionStatus::Changed+ } else {+ NotesRevisionStatus::Matches {+ source: source_at(3),+ }+ })+ },+ |_| {},+ )+ .unwrap();++ assert_eq!(embedded.get(), phase != "load", "{phase}");+ assert_eq!(outcome.changed_during_indexing.as_deref(), Some("note.md"));+ assert_eq!(outcome.unchecked_document_ids, ["note.md"]);+ let index = NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()).unwrap();+ let hits = index.search(&embedding(&previous)[0]).unwrap();+ assert_eq!(hits.len(), 1);+ assert_eq!(hits[0].text, "previous note");+ }+ }++ #[test]+ fn stops_before_embedding_when_updated_inventory_exceeds_capacity() {let temp = tempfile::tempdir().unwrap();- let prepared = prepare_notes_document("note.md", b"note").unwrap();- let load_count = Cell::new(0);- let embed_count = Cell::new(0);- let outcome = index_notes_collection::<()>(+ let inventory = (0..65)+ .map(|i| NotesSourceDocument {+ document_id: format!("note-{i:02}.md"),+ size: match i {+ 0 => 4,+ 1 => super::super::NOTES_MAX_SOURCE_BYTES as u64 - 4,+ _ => super::super::NOTES_MAX_SOURCE_BYTES as u64,+ },+ modified_at_ms: Some(1),+ })+ .collect::<Vec<_>>();+ let embedded = Cell::new(0);+ let result = index_notes_collection::<()>(NotesIndexInput {index_root: temp.path(),collection_id: COLLECTION_ID.to_string(),- inventory: vec![source()],+ inventory: inventory.clone(),forced_document_ids: &[],force_full_hash: false,},|| Ok(()),- |_| {- load_count.set(load_count.get() + 1);+ |id| {+ let mut source = inventory+ .iter()+ .find(|source| source.document_id == id)+ .unwrap()+ .clone();+ if id == "note-01.md" {+ source.size += 1;+ }+ let bytes = vec![b'x'; source.size as usize];Ok(NotesDocumentLoad::Prepared {- document: prepared.clone(),- source: source_at(load_count.get()),+ document: prepare_notes_document(id, &bytes).unwrap(),+ source,})},- |_| {- embed_count.set(embed_count.get() + 1);- Ok(Some(embedding(&prepared)))+ |document| {+ embedded.set(embedded.get() + 1);+ Ok(Some(embedding(document)))+ },+ |id, _| {+ Ok(NotesRevisionStatus::Matches {+ source: inventory+ .iter()+ .find(|source| source.document_id == id)+ .unwrap()+ .clone(),+ })},- |_, _| Ok(NotesRevisionStatus::Matches { source: source() }),|_| {},- )- .unwrap();-- assert_eq!(outcome.changed_during_indexing.as_deref(), Some("note.md"));- assert_eq!(embed_count.get(), 0);+ );+ assert!(matches!(+ result,+ Err(NotesIndexingError::Notes(NotesError::CollectionTooLarge(_)))+ ));+ assert_eq!(embedded.get(), 1);+ let writer = NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();+ assert_eq!(writer.indexed_document_count(), 1);+ assert!(!writer.initial_inventory_complete());}}diff --git a/rust/crates/ensu/src/notes/maintenance.rs b/rust/crates/ensu/src/notes/maintenance.rsnew file mode 100644index 0000000000..cfc9a791c6--- /dev/null+++ b/rust/crates/ensu/src/notes/maintenance.rs@@ -0,0 +1,181 @@+use std::path::Path;++use super::{+ NotesError, NotesIndexWriter, NotesIndexingError, NotesSourceDocument, notes_content_revision,+ prepare_notes_document,+};++pub enum NotesDocumentContent {+ Bytes(Vec<u8>),+ Changed,+}++pub struct NotesFreshness {+ pub changed: bool,+ pub forced_document_ids: Vec<String>,+}++pub fn inspect_notes_freshness<E>(+ writer: &NotesIndexWriter,+ inventory: &[NotesSourceDocument],+ mut check_for_cancellation: impl FnMut() -> Result<(), E>,+ mut read_document: impl FnMut(&str) -> Result<NotesDocumentContent, E>,+) -> Result<NotesFreshness, NotesIndexingError<E>> {+ check_for_cancellation().map_err(NotesIndexingError::Adapter)?;+ let plan = writer.plan_reconciliation(inventory, &[], false)?;+ let mut changed = !plan.deleted_document_ids.is_empty() || !writer.initial_inventory_complete();+ let mut forced_document_ids = Vec::new();+ for document in inventory {+ check_for_cancellation().map_err(NotesIndexingError::Adapter)?;+ match read_document(&document.document_id).map_err(NotesIndexingError::Adapter)? {+ NotesDocumentContent::Bytes(bytes) => {+ let revision = notes_content_revision(&bytes);+ if let Some(saved) = writer.indexed_revision(&document.document_id) {+ if revision != saved {+ forced_document_ids.push(document.document_id.clone());+ changed = true;+ }+ } else if prepare_notes_document(&document.document_id, &bytes).is_ok() {+ changed = true;+ }+ }+ NotesDocumentContent::Changed => {+ changed = true;+ forced_document_ids.push(document.document_id.clone());+ }+ }+ }+ Ok(NotesFreshness {+ changed,+ forced_document_ids,+ })+}++pub fn notes_index_needs_rebuild(+ index_root: &Path,+ collection_id: &str,+ rebuild: bool,+) -> Result<bool, NotesError> {+ if !rebuild {+ return Ok(false);+ }+ match NotesIndexWriter::open(index_root, collection_id.to_owned()) {+ Ok(writer) => Ok(writer.initial_inventory_complete()),+ Err(NotesError::InvalidIndex(_) | NotesError::IncompatibleIndex | NotesError::Json(_)) => {+ Ok(true)+ }+ Err(error) => Err(error),+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ const ID: &str = "123e4567-e89b-12d3-a456-426614174000";++ fn source(text: &[u8], modified_at_ms: Option<i64>) -> NotesSourceDocument {+ NotesSourceDocument {+ document_id: "note.md".into(),+ size: text.len() as u64,+ modified_at_ms,+ }+ }++ fn fixture(root: &Path, text: Option<&[u8]>, complete: bool) -> NotesIndexWriter {+ let mut writer = NotesIndexWriter::open(root, ID.into()).unwrap();+ if let Some(text) = text {+ let prepared = prepare_notes_document("note.md", text).unwrap();+ let mut vector = vec![0.0; 512];+ vector[0] = 1.0;+ writer+ .commit_document(+ &prepared,+ &vec![vector; prepared.chunks.len()],+ &source(text, Some(1)),+ )+ .unwrap();+ }+ writer.publish(complete).unwrap();+ writer+ }++ #[test]+ fn freshness_uses_revisions_for_missing_or_unchanged_metadata() {+ let root = tempfile::tempdir().unwrap();+ let writer = fixture(root.path(), Some(b"first"), true);+ let inspect = |text: &[u8], mtime| {+ inspect_notes_freshness(+ &writer,+ &[source(text, mtime)],+ || Ok::<_, ()>(()),+ |_| Ok(NotesDocumentContent::Bytes(text.into())),+ )+ .unwrap()+ };+ assert!(!inspect(b"first", None).changed);+ assert!(!inspect(b"first", Some(2)).changed);+ let changed = inspect(b"other", Some(1));+ assert!(changed.changed);+ assert_eq!(changed.forced_document_ids, ["note.md"]);+ }++ #[test]+ fn unchanged_unindexable_source_does_not_schedule_another_index() {+ let root = tempfile::tempdir().unwrap();+ let writer = fixture(root.path(), None, true);+ let result = inspect_notes_freshness(+ &writer,+ &[source(b" \n", None)],+ || Ok::<_, ()>(()),+ |_| Ok(NotesDocumentContent::Bytes(b" \n".to_vec())),+ )+ .unwrap();+ assert!(!result.changed);+ }++ #[test]+ fn freshness_preserves_source_failures_and_cancellation() {+ let root = tempfile::tempdir().unwrap();+ let writer = fixture(root.path(), Some(b"note"), true);+ let inventory = [source(b"note", None)];+ let changed = inspect_notes_freshness(+ &writer,+ &inventory,+ || Ok::<_, &str>(()),+ |_| Ok(NotesDocumentContent::Changed),+ )+ .unwrap();+ assert!(changed.changed);+ assert_eq!(changed.forced_document_ids, ["note.md"]);+ assert!(matches!(+ inspect_notes_freshness(&writer, &inventory, || Ok(()), |_| Err("offline")),+ Err(NotesIndexingError::Adapter("offline"))+ ));+ assert!(matches!(+ inspect_notes_freshness(+ &writer,+ &inventory,+ || Err("cancelled"),+ |_| { panic!("cancelled scans must not read content") }+ ),+ Err(NotesIndexingError::Adapter("cancelled"))+ ));+ assert_eq!(writer.indexed_document_count(), 1);+ }++ #[test]+ fn rebuild_preserves_partial_checkpoints() {+ let root = tempfile::tempdir().unwrap();+ fixture(root.path(), Some(b"note"), true);+ assert!(notes_index_needs_rebuild(root.path(), ID, true).unwrap());+ fixture(root.path(), Some(b"note"), false);+ assert!(!notes_index_needs_rebuild(root.path(), ID, true).unwrap());+ assert_eq!(+ NotesIndexWriter::open(root.path(), ID.into())+ .unwrap()+ .indexed_document_count(),+ 1+ );+ }+}diff --git a/rust/crates/ensu/src/notes/manifest.rs b/rust/crates/ensu/src/notes/manifest.rsindex 1e1901e437..27be315d5d 100644--- a/rust/crates/ensu/src/notes/manifest.rs+++ b/rust/crates/ensu/src/notes/manifest.rs@@ -122,22 +122,7 @@ pub(super) fn validate_manifest(let mut total_source_bytes = 0_u64;let mut total_chunks = 0_u64;for (document_id, document) in &manifest.documents {- validate_document_id(document_id).map_err(invalid_index_input)?;- validate_revision(&document.revision).map_err(invalid_index_input)?;- validate_revision(&document.shard_sha256).map_err(invalid_index_input)?;- validate_revision(&document.vectors_sha256).map_err(invalid_index_input)?;- if document.source_size > NOTES_MAX_SOURCE_BYTES as u64 {- return Err(NotesError::InvalidIndex(- "manifest source size exceeds the supported limit".to_string(),- ));- }- if document.chunk_count == 0- || u64::from(document.chunk_count) > document.source_size.max(1)- {- return Err(NotesError::InvalidIndex(- "manifest document chunk count is invalid".to_string(),- ));- }+ validate_manifest_document(document_id, document)?;total_source_bytes = total_source_bytes.checked_add(document.source_size).ok_or_else(|| {@@ -159,6 +144,46 @@ pub(super) fn validate_manifest(Ok(())}+fn validate_manifest_document(+ document_id: &str,+ document: &NotesManifestDocument,+) -> Result<(), NotesError> {+ validate_document_id(document_id).map_err(invalid_index_input)?;+ validate_revision(&document.revision).map_err(invalid_index_input)?;+ validate_revision(&document.shard_sha256).map_err(invalid_index_input)?;+ validate_revision(&document.vectors_sha256).map_err(invalid_index_input)?;+ if document.source_size > NOTES_MAX_SOURCE_BYTES as u64 {+ return Err(NotesError::InvalidIndex(+ "manifest source size exceeds the supported limit".to_string(),+ ));+ }+ if document.chunk_count == 0 || u64::from(document.chunk_count) > document.source_size.max(1) {+ return Err(NotesError::InvalidIndex(+ "manifest document chunk count is invalid".to_string(),+ ));+ }+ Ok(())+}++pub(super) fn manifest_document_entry_size(+ document_id: &str,+ document: &NotesManifestDocument,+) -> Result<u64, NotesError> {+ validate_manifest_document(document_id, document)?;+ let bytes = serde_json::to_vec_pretty(&BTreeMap::from([(document_id, document)]))?;+ let nested_indent_bytes = 2 * bytes.iter().filter(|&&byte| byte == b'\n').count();+ Ok((bytes.len() + nested_indent_bytes - 4) as u64)+}++pub(super) fn validate_manifest_size(bytes: u64) -> Result<(), NotesError> {+ if bytes > MAX_MANIFEST_BYTES {+ return Err(NotesError::CollectionTooLarge(+ "The folder contains too many notes to index".to_string(),+ ));+ }+ Ok(())+}+pub(super) fn serialize_manifest_for_publish(manifest: &NotesManifest,collection_id: &str,@@ -166,11 +191,7 @@ pub(super) fn serialize_manifest_for_publish(validate_manifest(manifest, collection_id, false)?;let mut bytes = serde_json::to_vec_pretty(manifest)?;bytes.push(b'\n');- if bytes.len() as u64 > MAX_MANIFEST_BYTES {- return Err(NotesError::CollectionTooLarge(- "The folder contains too many notes to index".to_string(),- ));- }+ validate_manifest_size(bytes.len() as u64)?;Ok(bytes)}@@ -218,7 +239,7 @@ pub(super) fn load_published_manifest(}}-fn manifest_updated_at_ms(path: &Path) -> Option<i64> {+pub(super) fn manifest_updated_at_ms(path: &Path) -> Option<i64> {fs::metadata(path).ok()?.modified()diff --git a/rust/crates/ensu/src/notes/source.rs b/rust/crates/ensu/src/notes/source.rsindex 8cf18ad9c9..6ea6f758a3 100644--- a/rust/crates/ensu/src/notes/source.rs+++ b/rust/crates/ensu/src/notes/source.rs@@ -17,6 +17,11 @@ pub struct NoteSourceReference {}impl NoteSourceReference {+ pub fn with_collection_label(mut self, label: String) -> Self {+ self.collection_label = is_valid_label(&label).then_some(label);+ self+ }+pub fn validate(&self) -> Result<(), NotesError> {validate_collection_id(&self.collection_id)?;if let Some(label) = &self.collection_label {diff --git a/rust/crates/ensu/src/notes/writer.rs b/rust/crates/ensu/src/notes/writer.rsindex 146d97a2ab..6a0609bcd7 100644--- a/rust/crates/ensu/src/notes/writer.rs+++ b/rust/crates/ensu/src/notes/writer.rs@@ -10,7 +10,8 @@ use uuid::Uuid;use super::manifest::{NOTES_DOCUMENTS_DIRECTORY, NOTES_MANIFEST_BACKUP_FILE, NOTES_MANIFEST_FILE, NotesManifest,NotesManifestDocument, collection_directory, empty_manifest, load_manifest_file,- load_published_manifest, notes_index_contract, serialize_manifest_for_publish, shard_directory,+ load_published_manifest, manifest_document_entry_size, manifest_updated_at_ms,+ notes_index_contract, serialize_manifest_for_publish, shard_directory, validate_manifest_size,};use super::reconcile::{IndexedNotesDocument, plan_notes_reconciliation};use super::shard::{@@ -31,6 +32,7 @@ pub struct NotesIndexWriter {manifest: NotesManifest,source_bytes: u64,chunk_count: u64,+ manifest_bytes: u64,}pub struct ValidatedNotesDocument<'a> {@@ -39,6 +41,24 @@ pub struct ValidatedNotesDocument<'a> {source_metadata: &'a NotesSourceDocument,shard_bytes: Vec<u8>,shard_sha256: String,+ cached_vectors_sha256: Option<String>,+}++impl ValidatedNotesDocument<'_> {+ pub fn needs_embedding(&self) -> bool {+ self.cached_vectors_sha256.is_none()+ }+}++pub fn remove_notes_collection(+ index_root: impl AsRef<Path>,+ collection_id: &str,+) -> Result<(), NotesError> {+ validate_collection_id(collection_id)?;+ match remove_owned_entry(&collection_directory(index_root.as_ref(), collection_id)) {+ Err(NotesError::Io(error)) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),+ result => result,+ }}pub fn cleanup_unreferenced_notes_shards(@@ -88,56 +108,6 @@ impl NotesIndexWriter {serialize_manifest_for_publish(&manifest, collection_id).map(|_| ())}- pub fn validate_reconciliation_capacity(- &self,- inventory: &[NotesSourceDocument],- prepared_chunk_counts: &BTreeMap<String, u32>,- ) -> Result<(), NotesError> {- let digest = "a".repeat(64);- let mut manifest = empty_manifest(&self.collection_id);- let mut source_bytes = 0_u64;- let mut chunks = 0_u64;- for source in inventory {- source.validate()?;- let chunk_count = prepared_chunk_counts- .get(&source.document_id)- .copied()- .or_else(|| {- self.manifest- .documents- .get(&source.document_id)- .map(|document| document.chunk_count)- })- .ok_or_else(|| {- NotesError::InvalidInput(- "source document has no prepared or indexed chunk count".to_string(),- )- })?;- if chunk_count == 0 {- continue;- }- source_bytes = source_bytes- .checked_add(source.size)- .ok_or_else(source_content_too_large)?;- chunks = chunks- .checked_add(u64::from(chunk_count))- .ok_or_else(chunk_count_too_large)?;- manifest.documents.insert(- source.document_id.clone(),- NotesManifestDocument {- revision: digest.clone(),- source_size: source.size,- source_modified_at_ms: source.modified_at_ms,- chunk_count,- shard_sha256: digest.clone(),- vectors_sha256: digest.clone(),- },- );- }- ensure_collection_capacity(source_bytes, chunks)?;- serialize_manifest_for_publish(&manifest, &self.collection_id).map(|_| ())- }-pub fn open(index_root: impl AsRef<Path>, collection_id: String) -> Result<Self, NotesError> {validate_collection_id(&collection_id)?;ensure_directory(index_root.as_ref())?;@@ -154,6 +124,8 @@ impl NotesIndexWriter {empty_manifest(&collection_id)};let (source_bytes, chunk_count) = manifest_capacity(&manifest)?;+ let manifest_bytes =+ serialize_manifest_for_publish(&manifest, &collection_id)?.len() as u64;let _ = cleanup_unreferenced_shards(&collection_directory, &manifest.documents);Ok(Self {collection_id,@@ -161,6 +133,7 @@ impl NotesIndexWriter {manifest,source_bytes,chunk_count,+ manifest_bytes,})}@@ -168,6 +141,12 @@ impl NotesIndexWriter {self.manifest.initial_inventory_complete}+ pub fn last_updated_at_ms(&self) -> Option<i64> {+ self.initial_inventory_complete()+ .then(|| manifest_updated_at_ms(&self.collection_directory.join(NOTES_MANIFEST_FILE)))+ .flatten()+ }+pub fn indexed_document_ids(&self) -> impl Iterator<Item = &str> {self.manifest.documents.keys().map(String::as_str)}@@ -210,13 +189,13 @@ impl NotesIndexWriter {source_metadata: &NotesSourceDocument,) -> Result<(), NotesError> {let validated = self.validate_document(prepared, source_metadata)?;- self.commit_validated_document(validated, embeddings)+ self.commit_validated_document(validated, Some(embeddings))}pub fn commit_validated_document(&mut self,validated: ValidatedNotesDocument<'_>,- embeddings: &[Vec<f32>],+ embeddings: Option<&[Vec<f32>]>,) -> Result<(), NotesError> {let ValidatedNotesDocument {collection_id,@@ -224,6 +203,7 @@ impl NotesIndexWriter {source_metadata,shard_bytes,shard_sha256,+ cached_vectors_sha256,} = validated;if collection_id != self.collection_id {return Err(NotesError::InvalidInput(@@ -232,6 +212,18 @@ impl NotesIndexWriter {}validate_prepared_document(prepared, source_metadata)?;self.validate_document_capacity(prepared, source_metadata)?;+ let Some(embeddings) = embeddings else {+ let vectors_sha256 = cached_vectors_sha256.ok_or_else(|| {+ NotesError::InvalidInput("document requires embeddings".to_string())+ })?;+ self.validate_existing_shard(prepared, &shard_sha256, &vectors_sha256)?;+ return self.activate_document(+ prepared,+ source_metadata,+ &shard_sha256,+ &vectors_sha256,+ );+ };if embeddings.len() != prepared.chunks.len() {return Err(NotesError::InvalidInput("embedding count does not match prepared chunks".to_string(),@@ -270,32 +262,23 @@ impl NotesIndexWriter {&prepared.revision,);if path_exists(&final_directory)? {- match load_and_validate_shard(- &final_directory,- &self.collection_id,- &prepared.document_id,- &prepared.revision,- prepared.chunks.len(),- NotesShardIntegrity {- shard_sha256: &shard_sha256,- vectors_sha256: &vectors_sha256,- },- Some(prepared),- ) {- Ok(_) => {- self.activate_document(+ match self.validate_existing_shard(prepared, &shard_sha256, &vectors_sha256) {+ Ok(()) => {+ return self.activate_document(prepared,source_metadata,&shard_sha256,&vectors_sha256,- )?;- return Ok(());+ );}Err(NotesError::InvalidIndex(_)| NotesError::IncompatibleIndex| NotesError::Json(_),) => remove_owned_entry(&final_directory)?,+ Err(NotesError::Io(error)) if error.kind() == std::io::ErrorKind::NotFound => {+ remove_owned_entry(&final_directory)?;+ }Err(error) => return Err(error),}}@@ -328,19 +311,7 @@ impl NotesIndexWriter {match fs::rename(&temporary_directory, &final_directory) {Ok(()) => Ok(()),Err(_) if path_exists(&final_directory)? => {- load_and_validate_shard(- &final_directory,- &self.collection_id,- &prepared.document_id,- &prepared.revision,- prepared.chunks.len(),- NotesShardIntegrity {- shard_sha256: &shard_sha256,- vectors_sha256: &vectors_sha256,- },- Some(prepared),- )?;- Ok(())+ self.validate_existing_shard(prepared, &shard_sha256, &vectors_sha256)}Err(rename_error) => Err(NotesError::Io(rename_error)),}@@ -362,15 +333,59 @@ impl NotesIndexWriter {let shard_bytes =serialize_shard(&NotesShard::from_prepared(&self.collection_id, prepared))?;let shard_sha256 = sha256_hex(&shard_bytes);+ let cached_vectors_sha256 = if let Some(existing) =+ self.manifest.documents.get(&prepared.document_id)+ && existing.revision == prepared.revision+ && existing.shard_sha256 == shard_sha256+ && existing.chunk_count as usize == prepared.chunks.len()+ {+ match self.validate_existing_shard(prepared, &shard_sha256, &existing.vectors_sha256) {+ Ok(()) => Some(existing.vectors_sha256.clone()),+ Err(+ NotesError::InvalidIndex(_)+ | NotesError::IncompatibleIndex+ | NotesError::Json(_),+ ) => None,+ Err(NotesError::Io(error)) if error.kind() == std::io::ErrorKind::NotFound => None,+ Err(error) => return Err(error),+ }+ } else {+ None+ };Ok(ValidatedNotesDocument {collection_id: self.collection_id.clone(),prepared,source_metadata,shard_bytes,shard_sha256,+ cached_vectors_sha256,})}+ fn validate_existing_shard(+ &self,+ prepared: &PreparedNotesDocument,+ shard_sha256: &str,+ vectors_sha256: &str,+ ) -> Result<(), NotesError> {+ load_and_validate_shard(+ &shard_directory(+ &self.collection_directory,+ &prepared.document_id,+ &prepared.revision,+ ),+ &self.collection_id,+ &prepared.document_id,+ &prepared.revision,+ prepared.chunks.len(),+ NotesShardIntegrity {+ shard_sha256,+ vectors_sha256,+ },+ Some(prepared),+ )+ }+fn validate_document_capacity(&self,prepared: &PreparedNotesDocument,@@ -389,7 +404,51 @@ impl NotesIndexWriter {.checked_sub(existing.map_or(0, |document| u64::from(document.chunk_count))).and_then(|total| total.checked_add(document_chunks)).ok_or_else(chunk_count_too_large)?;- ensure_collection_capacity(source_bytes, chunks)+ ensure_collection_capacity(source_bytes, chunks)?;+ if self+ .manifest+ .documents+ .len()+ .saturating_add(usize::from(existing.is_none()))+ > NOTES_MAX_COLLECTION_DOCUMENTS+ {+ return Err(NotesError::CollectionTooLarge(+ "The folder contains too many notes to index".to_string(),+ ));+ }+ let document = manifest_document(+ prepared,+ source_metadata,+ &prepared.revision,+ &prepared.revision,+ )?;+ validate_manifest_size(self.updated_manifest_size(&prepared.document_id, Some(&document))?)+ }++ fn updated_manifest_size(+ &self,+ document_id: &str,+ document: Option<&NotesManifestDocument>,+ ) -> Result<u64, NotesError> {+ let existing = self.manifest.documents.get(document_id);+ let existing_bytes = existing+ .map(|entry| manifest_document_entry_size(document_id, entry))+ .transpose()?+ .unwrap_or(0);+ let document_bytes = document+ .map(|entry| manifest_document_entry_size(document_id, entry))+ .transpose()?+ .unwrap_or(0);+ let document_count = self.manifest.documents.len() - usize::from(existing.is_some())+ + usize::from(document.is_some());+ let old_object_spacing = 2 * u64::from(!self.manifest.documents.is_empty());+ let new_object_spacing = 2 * u64::from(document_count > 0);+ self.manifest_bytes+ .checked_sub(existing_bytes + old_object_spacing)+ .and_then(|bytes| bytes.checked_add(document_bytes + new_object_spacing))+ .ok_or_else(|| {+ NotesError::InvalidIndex("manifest byte total is inconsistent".to_string())+ })}pub fn commit_deletions(&mut self, document_ids: &[String]) -> Result<(), NotesError> {@@ -412,14 +471,18 @@ impl NotesIndexWriter {.ok_or_else(|| {NotesError::InvalidIndex("manifest chunk total is inconsistent".to_string())})?;+ let manifest_bytes = self.updated_manifest_size(document_id, None)?;self.manifest.documents.remove(document_id);self.source_bytes = source_bytes;self.chunk_count = chunk_count;+ self.manifest_bytes = manifest_bytes;}Ok(())}pub fn publish(&mut self, initial_complete: bool) -> Result<(), NotesError> {+ self.manifest_bytes += u64::from(self.manifest.initial_inventory_complete);+ self.manifest_bytes -= u64::from(initial_complete);self.manifest.initial_inventory_complete = initial_complete;publish_manifest_atomically(&self.collection_directory,@@ -437,11 +500,7 @@ impl NotesIndexWriter {shard_sha256: &str,vectors_sha256: &str,) -> Result<(), NotesError> {- validate_revision(shard_sha256)?;- validate_revision(vectors_sha256)?;- let chunk_count = u32::try_from(prepared.chunks.len()).map_err(|_| {- NotesError::InvalidInput("prepared document has too many chunks".to_string())- })?;+ let document = manifest_document(prepared, source_metadata, shard_sha256, vectors_sha256)?;let existing = self.manifest.documents.get(&prepared.document_id);let source_bytes = self.source_bytes@@ -451,26 +510,40 @@ impl NotesIndexWriter {let total_chunks = self.chunk_count.checked_sub(existing.map_or(0, |document| u64::from(document.chunk_count)))- .and_then(|total| total.checked_add(u64::from(chunk_count)))+ .and_then(|total| total.checked_add(u64::from(document.chunk_count))).ok_or_else(chunk_count_too_large)?;ensure_collection_capacity(source_bytes, total_chunks)?;- self.manifest.documents.insert(- prepared.document_id.clone(),- NotesManifestDocument {- revision: prepared.revision.clone(),- source_size: source_metadata.size,- source_modified_at_ms: source_metadata.modified_at_ms,- chunk_count,- shard_sha256: shard_sha256.to_string(),- vectors_sha256: vectors_sha256.to_string(),- },- );+ let manifest_bytes = self.updated_manifest_size(&prepared.document_id, Some(&document))?;+ validate_manifest_size(manifest_bytes)?;+ self.manifest+ .documents+ .insert(prepared.document_id.clone(), document);self.source_bytes = source_bytes;self.chunk_count = total_chunks;+ self.manifest_bytes = manifest_bytes;Ok(())}}+fn manifest_document(+ prepared: &PreparedNotesDocument,+ source_metadata: &NotesSourceDocument,+ shard_sha256: &str,+ vectors_sha256: &str,+) -> Result<NotesManifestDocument, NotesError> {+ validate_revision(shard_sha256)?;+ validate_revision(vectors_sha256)?;+ let chunk_count = u32::try_from(prepared.chunks.len()).map_err(|_| chunk_count_too_large())?;+ Ok(NotesManifestDocument {+ revision: prepared.revision.clone(),+ source_size: source_metadata.size,+ source_modified_at_ms: source_metadata.modified_at_ms,+ chunk_count,+ shard_sha256: shard_sha256.to_string(),+ vectors_sha256: vectors_sha256.to_string(),+ })+}+fn manifest_capacity(manifest: &NotesManifest) -> Result<(u64, u64), NotesError> {let (source_bytes, chunks) = manifest.documents.values().try_fold((0_u64, 0_u64),@@ -756,6 +829,18 @@ mod tests {collection_directory(index_root, &collection())}+ #[test]+ fn collection_removal_is_confined_to_canonical_uuid_children() {+ let root = tempfile::tempdir().unwrap();+ assert!(remove_notes_collection(root.path(), "../source").is_err());+ let directory = publish_ready_manifest(root.path());+ fs::write(root.path().join("source.md"), b"keep").unwrap();+ remove_notes_collection(root.path(), &collection()).unwrap();+ assert!(root.path().join("source.md").exists());+ assert!(!directory.exists());+ remove_notes_collection(root.path(), &collection()).unwrap();+ }+#[test]fn writes_opens_and_searches_documents() {let temp = tempfile::tempdir().unwrap();@@ -763,6 +848,7 @@ mod tests {let first = prepared("first.md", first_text);let second = prepared("second.md", "second document");let mut writer = NotesIndexWriter::open(temp.path(), collection()).unwrap();+ assert!(writer.last_updated_at_ms().is_none());writer.commit_document(&first,@@ -782,6 +868,7 @@ mod tests {let index = NotesCollectionIndex::open(temp.path(), collection()).unwrap();assert_eq!(index.document_count(), 2);assert!(index.last_updated_at_ms().is_some());+ assert_eq!(writer.last_updated_at_ms(), index.last_updated_at_ms());let mut query = vec![0.0; 512];query[0] = 1.0;let hits = index.search(&query).unwrap();@@ -805,6 +892,37 @@ mod tests {));}+ #[test]+ fn repairs_missing_shard_files_in_complete_and_partial_indexes() {+ for complete in [false, true] {+ for missing in [NOTES_SHARD_FILE, NOTES_VECTORS_FILE] {+ let temp = tempfile::tempdir().unwrap();+ let document = prepared("note.md", "note");+ let vectors = embeddings(&document, &[1.0]);+ let source = metadata(&document, 4);+ let mut writer = NotesIndexWriter::open(temp.path(), collection()).unwrap();+ writer+ .commit_document(&document, &vectors, &source)+ .unwrap();+ writer.publish(complete).unwrap();+ let directory = shard_directory(+ &collection_directory(temp.path(), &collection()),+ &document.document_id,+ &document.revision,+ );+ fs::remove_file(directory.join(missing)).unwrap();+ let mut writer = NotesIndexWriter::open(temp.path(), collection()).unwrap();+ assert_eq!(writer.initial_inventory_complete(), complete);+ writer+ .commit_document(&document, &vectors, &source)+ .unwrap();+ writer.publish(true).unwrap();+ let index = NotesCollectionIndex::open(temp.path(), collection()).unwrap();+ assert_eq!(index.search(&vectors[0]).unwrap()[0].document_id, "note.md");+ }+ }+ }+#[test]fn maintains_cached_capacity_totals_across_replacement_and_deletion() {let temp = tempfile::tempdir().unwrap();@@ -863,23 +981,71 @@ mod tests {Err(NotesError::CollectionTooLarge(_))));- let inventory = (0..129)- .map(|index| NotesSourceDocument {- document_id: format!("note-{index:03}.md"),- size: 7_168,- modified_at_ms: Some(1),- })- .collect::<Vec<_>>();- NotesIndexWriter::validate_inventory_capacity(&collection(), &inventory).unwrap();- let prepared_chunk_counts = inventory- .iter()- .map(|source| (source.document_id.clone(), 1_024))- .collect::<BTreeMap<_, _>>();let temp = tempfile::tempdir().unwrap();- let writer = NotesIndexWriter::open(temp.path(), collection()).unwrap();+ let mut writer = NotesIndexWriter::open(temp.path(), collection()).unwrap();+ let document = prepared("note.md", "note");+ let source = metadata(&document, 4);+ writer.chunk_count = NOTES_MAX_COLLECTION_CHUNKS;+ assert!(matches!(+ writer.validate_document(&document, &source),+ Err(NotesError::CollectionTooLarge(_))+ ));+ writer.chunk_count = 0;+ writer.source_bytes = NOTES_MAX_COLLECTION_SOURCE_BYTES;+ assert!(matches!(+ writer.validate_document(&document, &source),+ Err(NotesError::CollectionTooLarge(_))+ ));+ writer.source_bytes = 0;+ let entry =+ manifest_document(&document, &source, &document.revision, &document.revision).unwrap();+ for index in 0..NOTES_MAX_COLLECTION_DOCUMENTS {+ writer+ .manifest+ .documents+ .insert(format!("note-{index:05}.md"), entry.clone());+ }+ assert!(matches!(+ writer.validate_document(&document, &source),+ Err(NotesError::CollectionTooLarge(_))+ ));+ (writer.source_bytes, writer.chunk_count) = manifest_capacity(&writer.manifest).unwrap();+ writer.manifest_bytes = serialize_manifest_for_publish(&writer.manifest, &collection())+ .unwrap()+ .len() as u64;+ let replacement = prepared("note-00000.md", "new note");+ writer+ .validate_document(&replacement, &metadata(&replacement, 8))+ .unwrap();+ writer.manifest.documents.clear();+ let long_id = |index| format!("{}-{index:04}.md", "a".repeat(4_000));+ writer.manifest.documents.insert(long_id(0), entry.clone());+ let first_size = serialize_manifest_for_publish(&writer.manifest, &collection())+ .unwrap()+ .len();+ writer.manifest.documents.insert(long_id(1), entry.clone());+ let entry_size = serialize_manifest_for_publish(&writer.manifest, &collection())+ .unwrap()+ .len()+ - first_size;+ let fitting_count = 1 + (8 * 1024 * 1024 - first_size) / entry_size;+ for index in 2..fitting_count {+ writer+ .manifest+ .documents+ .insert(long_id(index), entry.clone());+ }+ (writer.source_bytes, writer.chunk_count) = manifest_capacity(&writer.manifest).unwrap();+ writer.manifest_bytes = serialize_manifest_for_publish(&writer.manifest, &collection())+ .unwrap()+ .len() as u64;+ let document = PreparedNotesDocument {+ document_id: long_id(fitting_count),+ ..document+ };assert!(matches!(- writer.validate_reconciliation_capacity(&inventory, &prepared_chunk_counts),+ writer.validate_document(&document, &metadata(&document, 4)),Err(NotesError::CollectionTooLarge(_))));}diff --git a/rust/crates/ensu/src/retrieval.rs b/rust/crates/ensu/src/retrieval.rsindex 494cd15a5b..000054b018 100644--- a/rust/crates/ensu/src/retrieval.rs+++ b/rust/crates/ensu/src/retrieval.rs@@ -42,7 +42,8 @@ const END_CONTEXT_SENTINEL: &str = "----- END KNOWLEDGE CONTEXT -----";pub use citation::{MAX_GROUNDED_SOURCES, ParsedAssistantText, ParsedGroundedAssistantText, SourceCitation,clean_assistant_text, finalize_assistant_text, finalize_grounded_assistant_text,- knowledge_source_chip_label, parse_assistant_text, parse_grounded_assistant_text,+ grounded_source_chip_labels, knowledge_source_chip_label, parse_assistant_text,+ parse_grounded_assistant_text,};pub use grounded::{GroundedExcerpt, GroundedPromptContext, GroundedSource, MAX_NOTES_GROUNDING_HITS,diff --git a/rust/crates/ensu/src/retrieval/citation.rs b/rust/crates/ensu/src/retrieval/citation.rsindex 133daab065..5a771e178c 100644--- a/rust/crates/ensu/src/retrieval/citation.rs+++ b/rust/crates/ensu/src/retrieval/citation.rs@@ -137,16 +137,47 @@ pub fn parse_grounded_assistant_text(stored_text: &str) -> ParsedGroundedAssista}pub fn knowledge_source_chip_label(citations: &[SourceCitation]) -> Option<String> {- let first = citations.first()?;- let mut distinct = HashSet::new();- for citation in citations {- distinct.insert(citation.dataset_id.as_str());- }- let additional = distinct.len().saturating_sub(1);+ source_chip_label(citations.iter().map(|citation| {+ (+ citation.dataset_id.as_str(),+ citation.dataset_label.as_str(),+ )+ }))+}++pub fn grounded_source_chip_labels(sources: &[GroundedSource]) -> Vec<String> {+ let packs = source_chip_label(sources.iter().filter_map(|source| match source {+ GroundedSource::EnsuPack { citation } => Some((+ citation.dataset_id.as_str(),+ citation.dataset_label.as_str(),+ )),+ GroundedSource::LocalNote { .. } => None,+ }));+ let notes = source_chip_label(sources.iter().filter_map(|source| {+ match source {+ GroundedSource::LocalNote { reference } => Some((+ reference.collection_id.as_str(),+ reference+ .collection_label+ .as_deref()+ .unwrap_or("Your Notes"),+ )),+ GroundedSource::EnsuPack { .. } => None,+ }+ }));+ packs.into_iter().chain(notes).collect()+}++fn source_chip_label<'a>(sources: impl IntoIterator<Item = (&'a str, &'a str)>) -> Option<String> {+ let mut sources = sources.into_iter();+ let (first_id, label) = sources.next()?;+ let mut distinct = HashSet::from([first_id]);+ distinct.extend(sources.map(|(id, _)| id));+ let additional = distinct.len() - 1;if additional == 0 {- Some(first.dataset_label.clone())+ Some(label.to_owned())} else {- Some(format!("{} +{additional}", first.dataset_label))+ Some(format!("{label} +{additional}"))}}