Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
fix: Address concurrency race conditions and transcription timing issues
altic-devff8b9f55f53b
diff --git a/Sources/Fluid/ContentView.swift b/Sources/Fluid/ContentView.swiftindex efa94b82..be52738f 100644--- a/Sources/Fluid/ContentView.swift+++ b/Sources/Fluid/ContentView.swift@@ -1547,27 +1547,25 @@ struct ContentView: View {}var didTypeExternally = false- await MainActor.run {- let frontmostApp = NSWorkspace.shared.frontmostApplication- let frontmostName = frontmostApp?.localizedName ?? "Unknown"- let isFluidFrontmost = frontmostApp?.bundleIdentifier?.contains("fluid") == true- let shouldTypeExternally = !isFluidFrontmost || self.isTranscriptionFocused == false+ let frontmostApp = NSWorkspace.shared.frontmostApplication+ let frontmostName = frontmostApp?.localizedName ?? "Unknown"+ let isFluidFrontmost = frontmostApp?.bundleIdentifier?.contains("fluid") == true+ let shouldTypeExternally = !isFluidFrontmost || self.isTranscriptionFocused == false++ DebugLogger.shared.debug(+ "Typing decision → frontmost: \(frontmostName), fluidFrontmost: \(isFluidFrontmost), editorFocused: \(self.isTranscriptionFocused), willTypeExternally: \(shouldTypeExternally)",+ source: "ContentView"+ )- DebugLogger.shared.debug(- "Typing decision → frontmost: \(frontmostName), fluidFrontmost: \(isFluidFrontmost), editorFocused: \(self.isTranscriptionFocused), willTypeExternally: \(shouldTypeExternally)",- source: "ContentView"+ if shouldTypeExternally {+ // Await typing completion before proceeding to edit tracker+ // This ensures the tracker window opens after text has been typed+ await self.restoreFocusToRecordingTarget()+ self.asr.typeTextToActiveField(+ finalText,+ preferredTargetPID: NotchContentState.shared.recordingTargetPID)-- if shouldTypeExternally {- Task { @MainActor in- await self.restoreFocusToRecordingTarget()- self.asr.typeTextToActiveField(- finalText,- preferredTargetPID: NotchContentState.shared.recordingTargetPID- )- }- didTypeExternally = true- }+ didTypeExternally = true}if didTypeExternally {@@ -1579,18 +1577,19 @@ struct ContentView: View {])+ // Now that typing is complete, start the edit trackerlet wordsBucket = AnalyticsBuckets.bucketWords(AnalyticsBuckets.wordCount(in: finalText))let modelInfo = self.currentDictationAIModelInfo()- Task {- await PostTranscriptionEditTracker.shared.markTranscriptionCompleted(- mode: AnalyticsMode.dictation.rawValue,- outputMethod: AnalyticsOutputMethod.typed.rawValue,- wordsBucket: wordsBucket,- aiUsed: shouldUseAI,- aiModel: modelInfo.model,- aiProvider: modelInfo.provider- )- }+ await PostTranscriptionEditTracker.shared.markTranscriptionCompleted(+ mode: AnalyticsMode.dictation.rawValue,+ outputMethod: AnalyticsOutputMethod.typed.rawValue,+ wordsBucket: wordsBucket,+ aiUsed: shouldUseAI,+ aiModel: modelInfo.model,+ aiProvider: modelInfo.provider+ )++ NotchOverlayManager.shared.hide()} else if SettingsStore.shared.copyTranscriptionToClipboard == false,SettingsStore.shared.saveTranscriptionHistory{@@ -1602,12 +1601,6 @@ struct ContentView: View {])}-- if didTypeExternally {- Task { @MainActor in- NotchOverlayManager.shared.hide()- }- }}// MARK: - Rewrite Mode Voice Processingdiff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swiftindex 134166f2..543c34be 100644--- a/Sources/Fluid/Services/ASRService.swift+++ b/Sources/Fluid/Services/ASRService.swift@@ -251,27 +251,36 @@ final class ASRService: ObservableObject {self.downloadingModelId = model.id}- defer {- Task { @MainActor [weak self] in- guard let self else { return }+ // Use do-catch to ensure cleanup happens regardless of success/failure+ do {+ DebugLogger.shared.info("Downloading model: \(model.displayName) (without changing active selection)", source: "ASRService")++ // Get a fresh provider for this specific model (uses modelOverride for Whisper)+ let provider = await MainActor.run { self.getProvider(for: model) }++ // Prepare (download) the model+ try await provider.prepare(progressHandler: { progress in+ let clamped = max(0.0, min(1.0, progress))+ progressHandler?(clamped)+ })++ DebugLogger.shared.info("Model download completed: \(model.displayName)", source: "ASRService")++ // Synchronously clear downloadingModelId on success+ await MainActor.run {if self.downloadingModelId == model.id {self.downloadingModelId = nil}}+ } catch {+ // Synchronously clear downloadingModelId on failure+ await MainActor.run {+ if self.downloadingModelId == model.id {+ self.downloadingModelId = nil+ }+ }+ throw error}-- DebugLogger.shared.info("Downloading model: \(model.displayName) (without changing active selection)", source: "ASRService")-- // Get a fresh provider for this specific model (uses modelOverride for Whisper)- let provider = await MainActor.run { self.getProvider(for: model) }-- // Prepare (download) the model- try await provider.prepare(progressHandler: { progress in- let clamped = max(0.0, min(1.0, progress))- progressHandler?(clamped)- })-- DebugLogger.shared.info("Model download completed: \(model.displayName)", source: "ASRService")}}diff --git a/Sources/Fluid/Services/MeetingTranscriptionService.swift b/Sources/Fluid/Services/MeetingTranscriptionService.swiftindex 8ec944aa..2de12414 100644--- a/Sources/Fluid/Services/MeetingTranscriptionService.swift+++ b/Sources/Fluid/Services/MeetingTranscriptionService.swift@@ -345,7 +345,11 @@ final class MeetingTranscriptionService: ObservableObject {}// If already in correct format, just extract samples- if sourceSampleRate == targetSampleRate, sourceChannels == 1 {+ // Must also verify the format is Float32 - if source is Float64, floatChannelData returns nil+ if sourceSampleRate == targetSampleRate,+ sourceChannels == 1,+ sourceFormat.commonFormat == .pcmFormatFloat32+ {guard let channelData = buffer.floatChannelData else {throw NSError(domain: "MeetingTranscriptionService",diff --git a/Sources/Fluid/Services/WhisperProvider.swift b/Sources/Fluid/Services/WhisperProvider.swiftindex d25825a5..e3504068 100644--- a/Sources/Fluid/Services/WhisperProvider.swift+++ b/Sources/Fluid/Services/WhisperProvider.swift@@ -96,8 +96,12 @@ final class WhisperProvider: TranscriptionProvider {}func prepare(progressHandler: ((Double) -> Void)? = nil) async throws {+ // CRITICAL: Capture the target model at start to use consistently throughout this method.+ // This prevents race conditions where SettingsStore could change after await points.+ let targetModel = self.modelOverride ?? SettingsStore.shared.selectedSpeechModel+ let currentModelName = targetModel.whisperModelFile?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "ggml-base.bin"+// Detect model change: if a different model is now selected, force reload- let currentModelName = self.modelNameif self.isReady, self.loadedModelName != currentModelName {DebugLogger.shared.info("WhisperProvider: Model changed from \(self.loadedModelName ?? "nil") to \(currentModelName), forcing reload", source: "WhisperProvider")self.isReady = false@@ -137,8 +141,8 @@ final class WhisperProvider: TranscriptionProvider {}// Check available memory before loading large models- let selectedModel = self.modelOverride ?? SettingsStore.shared.selectedSpeechModel- let requiredMemoryGB = selectedModel.requiredMemoryGB+ // Use the captured targetModel to ensure consistent memory validation+ let requiredMemoryGB = targetModel.requiredMemoryGBlet availableMemoryGB = Self.availableMemoryGB()DebugLogger.shared.info(@@ -148,7 +152,7 @@ final class WhisperProvider: TranscriptionProvider {if availableMemoryGB < requiredMemoryGB {let errorMessage = """- Insufficient memory for \(selectedModel.displayName).+ Insufficient memory for \(targetModel.displayName).Required: \(String(format: "%.1f", requiredMemoryGB)) GBAvailable: \(String(format: "%.1f", availableMemoryGB)) GBdiff --git a/Sources/Fluid/UI/AISettings/VoiceEngineSettingsViewModel.swift b/Sources/Fluid/UI/AISettings/VoiceEngineSettingsViewModel.swiftindex 00ea45e7..16a405ee 100644--- a/Sources/Fluid/UI/AISettings/VoiceEngineSettingsViewModel.swift+++ b/Sources/Fluid/UI/AISettings/VoiceEngineSettingsViewModel.swift@@ -123,9 +123,13 @@ final class VoiceEngineSettingsViewModel: ObservableObject {do {// Download the model WITHOUT changing the active selection+ // Capture the model ID to guard against stale progress callbacks+ let downloadingModelId = model.idtry await self.asr.downloadModel(model, progressHandler: { [weak self] progress inTask { @MainActor in- self?.downloadProgress = progress+ // Only update progress if we're still downloading this specific model+ guard let self, self.downloadingModel?.id == downloadingModelId else { return }+ self.downloadProgress = progress}})