π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[ensu][desktop] Fix Notes lifecycle follow-ups
setalpe0017fe857cf
diff --git a/rust/apps/ensu/src-tauri/src/commands/llm.rs b/rust/apps/ensu/src-tauri/src/commands/llm.rsindex 8fd018ea87..564abbe89c 100644--- a/rust/apps/ensu/src-tauri/src/commands/llm.rs+++ b/rust/apps/ensu/src-tauri/src/commands/llm.rs@@ -19,6 +19,7 @@ pub struct State {context: Mutex<Option<llm::ContextRef>>,lifecycle: async_runtime::Mutex<()>,retrieval_epoch: Arc<AtomicU64>,+ model_state_epoch: AtomicU64,}impl State {@@ -33,6 +34,14 @@ impl State {fn cancel_retrieval(&self) {self.retrieval_epoch.fetch_add(1, Ordering::Relaxed);}++ fn model_state_epoch(&self) -> u64 {+ self.model_state_epoch.load(Ordering::SeqCst)+ }++ fn mark_model_state_changed(&self) {+ self.model_state_epoch.fetch_add(1, Ordering::SeqCst);+ }}#[tauri::command]@@ -40,6 +49,11 @@ pub fn llm_retrieval_epoch(state: TauriState<'_, State>) -> u64 {state.retrieval_epoch.load(Ordering::Relaxed)}+#[tauri::command]+pub fn llm_model_state_epoch(state: TauriState<'_, State>) -> u64 {+ state.model_state_epoch()+}+pub struct ModelDownloadState {store: Arc<AssetStore>,assets_dir: PathBuf,@@ -143,6 +157,9 @@ pub(crate) fn replace_state(*model_guard = model;*context_guard = context;+ drop(context_guard);+ drop(model_guard);+ state.mark_model_state_changed();Ok(())}@@ -519,6 +536,8 @@ pub async fn llm_create_context(.lock().map_err(|_| ApiError::new("lock", "Failed to lock LLM context store"))?;*context_guard = Some(context);+ drop(context_guard);+ state.mark_model_state_changed();logging::log("LLM", "create context succeeded");Ok(())@@ -532,6 +551,8 @@ pub async fn llm_free_context(state: TauriState<'_, State>) -> Result<(), ApiErr.lock().map_err(|_| ApiError::new("lock", "Failed to lock LLM context store"))?;*context_guard = None;+ drop(context_guard);+ state.mark_model_state_changed();Ok(())}@@ -647,3 +668,18 @@ pub(crate) fn clear_for_exit(app: &AppHandle) {logging::log("App", "LLM state unavailable during exit");}}++#[cfg(test)]+mod tests {+ use super::{State, replace_state};++ #[test]+ fn replacing_model_state_advances_epoch() {+ let state = State::default();+ let before = state.model_state_epoch();++ replace_state(&state, None, None).expect("replace empty model state");++ assert_eq!(state.model_state_epoch(), before + 1);+ }+}diff --git a/rust/apps/ensu/src-tauri/src/main.rs b/rust/apps/ensu/src-tauri/src/main.rsindex ce750b2646..9897cfb488 100644--- a/rust/apps/ensu/src-tauri/src/main.rs+++ b/rust/apps/ensu/src-tauri/src/main.rs@@ -90,6 +90,7 @@ fn main() {commands::llm::llm_generate_chat_stream,commands::llm::llm_cancel,commands::llm::llm_retrieval_epoch,+ commands::llm::llm_model_state_epoch,commands::system::system_info,commands::config::desktop_model_policy,commands::llm::llm_model_status,diff --git a/web/apps/ensu/src/hooks/use-notes-collections.ts b/web/apps/ensu/src/hooks/use-notes-collections.tsindex 7b381b45d8..929770ac0a 100644--- a/web/apps/ensu/src/hooks/use-notes-collections.ts+++ b/web/apps/ensu/src/hooks/use-notes-collections.ts@@ -25,7 +25,7 @@ const NOTES_REMOVE_WAIT_ERROR =interface UseNotesCollectionsOptions {isTauriRuntime: boolean;isGenerating: boolean;- isGenerationStarting: () => boolean;+ isGenerationActive: () => boolean;modelReady: boolean;ensureProvider: () => Promise<LlmProvider>;cancelIndexing: () => void;@@ -35,7 +35,7 @@ interface UseNotesCollectionsOptions {export const useNotesCollections = ({isTauriRuntime,isGenerating,- isGenerationStarting,+ isGenerationActive,modelReady,ensureProvider,cancelIndexing,@@ -129,11 +129,19 @@ export const useNotesCollections = ({);const startIndex = useCallback(- (collectionId: string, force = false, automatic = false) => {- const blocked =- !modelReady || isGenerating || isGenerationStarting();+ (+ collectionId: string,+ force = false,+ automatic = false,+ automaticRequestKey?: number,+ ) => {+ const blocked = !modelReady || isGenerationActive();const request = automatic- ? controller.ensureIndex(collectionId, blocked)+ ? controller.ensureIndex(+ collectionId,+ blocked,+ automaticRequestKey,+ ): controller.requestIndex(collectionId, force, blocked);if (request === "ignored") return;lifecycleChanged();@@ -148,41 +156,43 @@ export const useNotesCollections = ({force,});+ const ownsIndex = () => controller.canContinueIndex(collectionId);+ const canStartIndex = () => ownsIndex() && !isGenerationActive();void ensureProvider().then((provider) =>- provider.withKnowledgeRetrieval(- async (retrievalEpoch) => {- let needsRerun = true;- let bypassQuietPeriod = force;- while (- needsRerun &&- controller.canContinueIndex(collectionId)- ) {- const result = await indexNotesCollection(+ provider.withKnowledgeRetrieval(async (retrievalEpoch) => {+ let needsRerun = true;+ let bypassQuietPeriod = force;+ while (needsRerun && ownsIndex()) {+ if (isGenerationActive()) {+ controller.queueIndex(collectionId,bypassQuietPeriod,- retrievalEpoch,);- if (- !controller.canContinueIndex(collectionId)- ) {- return;- }- bypassQuietPeriod = false;- replaceCollection(result.collection);- needsRerun = result.needsRerun;- if (needsRerun) {- await new Promise<void>((resolve) =>- window.setTimeout(- resolve,- NOTES_INDEX_RERUN_YIELD_MS,- ),- );- }+ lifecycleChanged();+ return;}- },- () => controller.canContinueIndex(collectionId),- ),+ const result = await indexNotesCollection(+ collectionId,+ bypassQuietPeriod,+ retrievalEpoch,+ );+ if (!ownsIndex()) {+ return;+ }+ bypassQuietPeriod = false;+ replaceCollection(result.collection);+ needsRerun = result.needsRerun;+ if (needsRerun) {+ await new Promise<void>((resolve) =>+ window.setTimeout(+ resolve,+ NOTES_INDEX_RERUN_YIELD_MS,+ ),+ );+ }+ }+ }, canStartIndex),).catch((error: unknown) => {if (controller.isRemoving(collectionId)) return;@@ -215,8 +225,7 @@ export const useNotesCollections = ({[controller,ensureProvider,- isGenerating,- isGenerationStarting,+ isGenerationActive,lifecycleChanged,modelReady,refresh,@@ -231,7 +240,7 @@ export const useNotesCollections = ({);useEffect(() => {- const blocked = !modelReady || isGenerating || isGenerationStarting();+ const blocked = !modelReady || isGenerationActive();const queued = controller.drainQueued(blocked);if (queued.length > 0) {lifecycleChanged();@@ -242,7 +251,7 @@ export const useNotesCollections = ({}, [controller,isGenerating,- isGenerationStarting,+ isGenerationActive,lifecycleChanged,lifecycleRevision,modelReady,@@ -294,7 +303,13 @@ export const useNotesCollections = ({) {timeouts.push(window.setTimeout(- () => runIndex(collection.id),+ () =>+ startIndex(+ collection.id,+ false,+ true,+ collection.updateDueAtMs ?? undefined,+ ),Math.max(0, collection.updateDueAtMs - now),),);@@ -303,7 +318,7 @@ export const useNotesCollections = ({return () => {for (const timeout of timeouts) window.clearTimeout(timeout);};- }, [collections, isTauriRuntime, modelReady, runIndex]);+ }, [collections, isTauriRuntime, modelReady, startIndex]);useEffect(() => {if (!isTauriRuntime || !modelReady) return;@@ -375,9 +390,6 @@ export const useNotesCollections = ({const collectionViews = useMemo<NotesCollectionView[]>(() =>collections.map((collection) => {- if (collection.status !== "pending") {- return { ...collection, activity: null };- }const lifecycleActivity = controller.indexActivity(collection.id,);@@ -389,14 +401,20 @@ export const useNotesCollections = ({} else if (lifecycleActivity === "queued") {activity = !modelReady? "waitingForModel"- : isGenerating || isGenerationStarting()+ : isGenerating || isGenerationActive()? "waitingForGeneration": "starting";- } else if (collection.updateDueAtMs != null) {+ } else if (+ collection.updateDueAtMs != null &&+ (collection.status === "ready" ||+ collection.status === "pending")+ ) {activity = "scheduled";+ } else if (collection.status !== "pending") {+ activity = null;} else if (!modelReady) {activity = "waitingForModel";- } else if (isGenerating || isGenerationStarting()) {+ } else if (isGenerating || isGenerationActive()) {activity = "waitingForGeneration";} else {activity = "starting";@@ -407,7 +425,7 @@ export const useNotesCollections = ({collections,controller,isGenerating,- isGenerationStarting,+ isGenerationActive,lifecycleRevision,modelReady,],diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsxindex ab9cec8044..e74ef5166a 100644--- a/web/apps/ensu/src/pages/chat.tsx+++ b/web/apps/ensu/src/pages/chat.tsx@@ -572,6 +572,8 @@ const Page: React.FC = () => {>(undefined);const [chatNotificationOpen, setChatNotificationOpen] = useState(false);const [isGenerating, setIsGenerating] = useState(false);+ const [isSessionSummaryGenerating, setIsSessionSummaryGenerating] =+ useState(false);const [isStreamingOutro, setIsStreamingOutro] = useState(false);const [loadingPhrase, setLoadingPhrase] = useState<string | null>(null);const [loadingDots, setLoadingDots] = useState(1);@@ -620,6 +622,7 @@ const Page: React.FC = () => {KnowledgePack[]> | null>(null);const generationStartingRef = useRef(false);+ const generationActiveRef = useRef(false);const generationStoppingRef = useRef(false);const pendingGenerationStopsRef = useRef(0);const modelGateRequestRef = useRef(0);@@ -629,6 +632,7 @@ const Page: React.FC = () => {previousSelection?: string | null;} | null>(null);const sessionSummaryPromiseRef = useRef<Promise<void> | null>(null);+ const sessionSummaryActiveRef = useRef(false);const sessionSummaryEpochRef = useRef(0);const manuallyRenamedSessionIdsRef = useRef(new Set<string>());const pendingSessionRenamesRef = useRef(new Set<string>());@@ -1413,6 +1417,7 @@ const Page: React.FC = () => {const jobId = currentJobIdRef.current;currentJobIdRef.current = null;activeKnowledgeSourcesRef.current = [];+ generationActiveRef.current = false;setIsGenerating(false);setIsStreamingOutro(false);setIsDownloading(false);@@ -1477,6 +1482,7 @@ const Page: React.FC = () => {streamingBufferRef.current = "";streamingChunksRef.current = [];streamingCreatedAtRef.current = null;+ generationActiveRef.current = false;setIsGenerating(false);setIsStreamingOutro(false);resetPendingImages();@@ -1786,8 +1792,8 @@ const Page: React.FC = () => {return providerRef.current;}, []);- const isNotesGenerationStarting = useCallback(- () => generationStartingRef.current,+ const isNotesGenerationActive = useCallback(+ () => generationActiveRef.current || sessionSummaryActiveRef.current,[],);const cancelNotesIndexing = useCallback(() => {@@ -1822,8 +1828,8 @@ const Page: React.FC = () => {runIndex: runNotesIndex,} = useNotesCollections({isTauriRuntime,- isGenerating,- isGenerationStarting: isNotesGenerationStarting,+ isGenerating: isGenerating || isSessionSummaryGenerating,+ isGenerationActive: isNotesGenerationActive,modelReady: modelGateStatus === "ready",ensureProvider,cancelIndexing: cancelNotesIndexing,@@ -1915,41 +1921,48 @@ const Page: React.FC = () => {const generateSessionSummary = useCallback(async (input: string) => {- const provider = await ensureProvider();- const settings = getModelSettings();- const availability =- await provider.checkModelAvailability(settings);- const mmprojReady =- availability.mmprojAvailable === undefined ||- availability.mmprojAvailable;+ sessionSummaryActiveRef.current = true;+ setIsSessionSummaryGenerating(true);+ try {+ const provider = await ensureProvider();+ const settings = getModelSettings();+ const availability =+ await provider.checkModelAvailability(settings);+ const mmprojReady =+ availability.mmprojAvailable === undefined ||+ availability.mmprojAvailable;++ if (!availability.modelAvailable || !mmprojReady) {+ return null;+ }- if (!availability.modelAvailable || !mmprojReady) {- return null;- }+ await provider.ensureModelReady(settings);- await provider.ensureModelReady(settings);+ let summary = "";- let summary = "";-- await provider.generateChatStream(- {- messages: [- { role: "system", content: SESSION_TITLE_PROMPT },- { role: "user", content: input },- ],- maxTokens: 64,- temperature: 0.2,- topP: 0.9,- repeatPenalty: REPEAT_PENALTY,- },- (event) => {- if (event.type === "text") {- summary += event.text;- }- },- );+ await provider.generateChatStream(+ {+ messages: [+ { role: "system", content: SESSION_TITLE_PROMPT },+ { role: "user", content: input },+ ],+ maxTokens: 64,+ temperature: 0.2,+ topP: 0.9,+ repeatPenalty: REPEAT_PENALTY,+ },+ (event) => {+ if (event.type === "text") {+ summary += event.text;+ }+ },+ );- return summary;+ return summary;+ } finally {+ sessionSummaryActiveRef.current = false;+ setIsSessionSummaryGenerating(false);+ }},[ensureProvider, getModelSettings],);@@ -2427,6 +2440,7 @@ const Page: React.FC = () => {streamingBufferRef.current = "";streamingChunksRef.current = [];streamingCreatedAtRef.current = null;+ generationActiveRef.current = false;setIsGenerating(false);setIsStreamingOutro(false);setIsDraftSession(true);@@ -2786,6 +2800,7 @@ const Page: React.FC = () => {const last = lastGenerationRef.current;lastGenerationRef.current = null;+ generationActiveRef.current = false;setIsGenerating(false);setIsStreamingOutro(false);setIsDownloading(false);@@ -2921,6 +2936,7 @@ const Page: React.FC = () => {return;}generationStartingRef.current = true;+ generationActiveRef.current = true;setIsGenerating(true);currentJobIdRef.current = null;lastGenerationRef.current = null;@@ -2984,6 +3000,7 @@ const Page: React.FC = () => {} finally {generationStartingRef.current = false;if (!startupCompleted && isActiveGeneration()) {+ generationActiveRef.current = false;setIsGenerating(false);}}@@ -3282,6 +3299,7 @@ const Page: React.FC = () => {} finally {if (isActiveGeneration()) {activeKnowledgeSourcesRef.current = [];+ generationActiveRef.current = false;setIsGenerating(false);setIsStreamingOutro(false);setIsDownloading(false);diff --git a/web/apps/ensu/src/services/llm/provider.ts b/web/apps/ensu/src/services/llm/provider.tsindex df647a5fc4..d045f8512a 100644--- a/web/apps/ensu/src/services/llm/provider.ts+++ b/web/apps/ensu/src/services/llm/provider.ts@@ -159,6 +159,23 @@ export class LlmProvider {promise: Promise<void>;emitsProgress: boolean;};+ private modelOperationTail: Promise<void> = Promise.resolve();++ private async withExclusiveModelOperation<T>(+ operation: () => Promise<T>,+ ): Promise<T> {+ const previous = this.modelOperationTail;+ let release: () => void = () => undefined;+ this.modelOperationTail = new Promise<void>((resolve) => {+ release = resolve;+ });+ await previous;+ try {+ return await operation();+ } finally {+ release();+ }+ }public async initialize() {if (this.initialized) return;@@ -314,7 +331,7 @@ export class LlmProvider {}}- const ensurePromise = (async () => {+ const ensurePromise = this.withExclusiveModelOperation(async () => {log.info("LLM ensureModelReady", {backend: this.backend.kind,modelId: model.id,@@ -377,7 +394,7 @@ export class LlmProvider {if (emitProgress) {this.emitProgress({ percent: 100, status: "Ready" });}- })();+ });this.ensureInFlight = {key: ensureKey,@@ -460,14 +477,49 @@ export class LlmProvider {"Knowledge retrieval is only available in the desktop app",);}- const { invoke } = await import("@tauri-apps/api/core");- const retrievalEpoch = await invoke<number>("llm_retrieval_epoch");- await this.ensureInFlight?.promise.catch(() => undefined);- if (!shouldContinue()) {- throw namedError("cancelled", "Knowledge retrieval cancelled");- }- this.invalidateModelState();- return operation(retrievalEpoch);+ return this.withExclusiveModelOperation(async () => {+ const { invoke } = await import("@tauri-apps/api/core");+ const retrievalEpoch = await invoke<number>("llm_retrieval_epoch");+ if (!shouldContinue()) {+ throw namedError("cancelled", "Knowledge retrieval cancelled");+ }++ const modelStateEpoch = await invoke<number>(+ "llm_model_state_epoch",+ );+ const previousModelState = {+ currentModel: this.currentModel,+ currentModelPath: this.currentModelPath,+ currentMmprojPath: this.currentMmprojPath,+ currentContextKey: this.currentContextKey,+ modelReady: this.modelReady,+ };+ this.invalidateModelState();+ try {+ return await operation(retrievalEpoch);+ } finally {+ try {+ const currentModelStateEpoch = await invoke<number>(+ "llm_model_state_epoch",+ );+ if (currentModelStateEpoch === modelStateEpoch) {+ this.currentModel = previousModelState.currentModel;+ this.currentModelPath =+ previousModelState.currentModelPath;+ this.currentMmprojPath =+ previousModelState.currentMmprojPath;+ this.currentContextKey =+ previousModelState.currentContextKey;+ this.modelReady = previousModelState.modelReady;+ }+ } catch (error) {+ log.warn(+ "Failed to reconcile model state after knowledge retrieval",+ { error },+ );+ }+ }+ });}public cancelDownload() {diff --git a/web/apps/ensu/src/services/notes-lifecycle.ts b/web/apps/ensu/src/services/notes-lifecycle.tsindex 031636c1d7..1faec2bac2 100644--- a/web/apps/ensu/src/services/notes-lifecycle.ts+++ b/web/apps/ensu/src/services/notes-lifecycle.ts@@ -28,6 +28,7 @@ export class NotesLifecycleController {private readonly active = new Set<string>();private readonly queued = new Map<string, boolean>();private readonly failed = new Set<string>();+ private readonly automaticRequestKeys = new Map<string, number>();private readonly removing = new Set<string>();private removalWaitCollection: string | null = null;private mutation = 0;@@ -53,16 +54,30 @@ export class NotesLifecycleController {return "start";}- ensureIndex(collectionId: string, blocked: boolean): NotesIndexRequest {+ ensureIndex(+ collectionId: string,+ blocked: boolean,+ requestKey?: number,+ ): NotesIndexRequest {if (this.disposed ||this.removing.has(collectionId) ||- this.failed.has(collectionId) ||this.active.has(collectionId) ||this.queued.has(collectionId)) {return "ignored";}+ if (+ this.failed.has(collectionId) &&+ (requestKey === undefined ||+ this.automaticRequestKeys.get(collectionId) === requestKey)+ ) {+ return "ignored";+ }+ if (requestKey !== undefined) {+ this.automaticRequestKeys.set(collectionId, requestKey);+ this.failed.delete(collectionId);+ }if (blocked) {this.queueIndex(collectionId, false);return "queued";@@ -140,6 +155,7 @@ export class NotesLifecycleController {finishRemoval(collectionId: string) {this.removing.delete(collectionId);this.failed.delete(collectionId);+ this.automaticRequestKeys.delete(collectionId);if (this.removalWaitCollection === collectionId) {this.removalWaitCollection = null;}@@ -187,6 +203,7 @@ export class NotesLifecycleController {this.active.clear();this.queued.clear();this.failed.clear();+ this.automaticRequestKeys.clear();this.removing.clear();this.cancelRefreshes();}