Skip to content

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

chore(diagnostics): trace stop path timing end to end

altic-dev974cad2b61f4
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swift
index d9832316..0d8d4059 100644
--- a/Sources/Fluid/Services/ASRService.swift
+++ b/Sources/Fluid/Services/ASRService.swift
@@ -1386,10 +1386,12 @@ final class ASRService: ObservableObject {
) async {
switch self.activeAudioCaptureBackend {
case .directCoreAudio:
+ let captureAwaitStartedAt = ProcessInfo.processInfo.systemUptime
let report = await self.directAudioLifecycleController.stop(
retainPrepared: retainDirectPreparedCapture,
reason: reason
)
+ DebugLogger.shared.info("CLOSE_DETAIL audioCallerResumed uptime=\(ProcessInfo.processInfo.systemUptime) awaitMs=\((ProcessInfo.processInfo.systemUptime - captureAwaitStartedAt) * 1000)", source: "StopTiming")
if report.status != noErr {
DebugLogger.shared.warning(
"Direct Core Audio stop returned OSStatus \(report.status)",
@@ -1403,10 +1405,16 @@ final class ASRService: ObservableObject {
)
}
case .audioEngine:
+ let tapStopStartedAt = ProcessInfo.processInfo.systemUptime
self.removeEngineTap()
+ let tapStopFinishedAt = ProcessInfo.processInfo.systemUptime
if let engine = self.engineStorage as? AVAudioEngine, engine.isRunning {
engine.stop()
}
+ DebugLogger.shared.info(
+ "STOP_TRACE backend=audioEngine removeTapMs=\(Int((tapStopFinishedAt - tapStopStartedAt) * 1000)) engineStopMs=\(Int((ProcessInfo.processInfo.systemUptime - tapStopFinishedAt) * 1000))",
+ source: "StopTiming"
+ )
case .none:
break
}
@@ -2696,6 +2704,17 @@ final class ASRService: ObservableObject {
}
self.lastCompletedAudioSnapshot = nil
let stopStartedAt = Date().timeIntervalSince1970
+ let traceStartedAt = ProcessInfo.processInfo.systemUptime
+ var tracePreviousAt = traceStartedAt
+ func traceStop(_ phase: String) {
+ let now = ProcessInfo.processInfo.systemUptime
+ DebugLogger.shared.info(
+ "STOP_TRACE phase=\(phase) deltaMs=\(Int((now - tracePreviousAt) * 1000)) totalMs=\(Int((now - traceStartedAt) * 1000))",
+ source: "StopTiming"
+ )
+ tracePreviousAt = now
+ }
+ defer { traceStop("stop_return") }
self.benchmarkLog("stop_start ageMs=\(self.elapsedMilliseconds(since: self.benchmarkRecordingStartedAt)) bufferedSamples=\(self.audioBuffer.count)")
if self.isStarting, self.isRunning == false {
@@ -2732,7 +2751,9 @@ final class ASRService: ObservableObject {
self.isDictionaryTrainingCaptureActive = false
}
+ traceStop("route_cleanup_begin")
await self.cancelAudioRouteRecoveryAndWait()
+ traceStop("route_cleanup_end")
guard self.isRunning, self.benchmarkSessionID == stoppingSessionID else {
self.recordingBufferHandoffGate.complete(bufferHandoffToken)
completedBufferHandoff = true
@@ -2753,7 +2774,9 @@ final class ASRService: ObservableObject {
DebugLogger.shared.debug("✅ Device monitoring stopped", source: "ASRService")
self.benchmarkLog("capture_stop_await_begin")
+ traceStop("capture_stop_begin")
await self.stopActiveAudioCapture(reason: "recording_stop")
+ traceStop("capture_stop_end")
self.benchmarkLog("capture_stop_await_return")
self.audioCapturePipeline.finishRecording()
self.benchmarkLog("capture_pipeline_finished")
@@ -2795,6 +2818,7 @@ final class ASRService: ObservableObject {
// cancellation into incremental provider state.
DebugLogger.shared.debug("⏳ Awaiting active streaming work...", source: "ASRService")
let streamingStopStartedAt = Date().timeIntervalSince1970
+ traceStop("streaming_drain_begin")
guard await self.drainActiveStreamingWork(sessionID: stoppingSessionID) else {
self.beginStreamingDrainRecovery(sessionID: stoppingSessionID, token: bufferHandoffToken)
completedBufferHandoff = true // Recovery owns the PCM handoff until real work ends.
@@ -2803,6 +2827,7 @@ final class ASRService: ObservableObject {
return ""
}
self.benchmarkLog("stop_streaming_wait elapsedMs=\(self.elapsedMilliseconds(since: streamingStopStartedAt))")
+ traceStop("streaming_drain_end")
DebugLogger.shared.debug("✅ Active streaming work completed", source: "ASRService")
self.isProcessingChunk = false
@@ -2814,6 +2839,7 @@ final class ASRService: ObservableObject {
var pcm = self.audioBuffer.getAll()
self.audioBuffer.clear()
let capturedPCM = pcm
+ traceStop("buffer_copied")
let hasRecognizedStreamingPreview = !self.partialTranscription
.trimmingCharacters(in: .whitespacesAndNewlines)
.isEmpty
@@ -2855,6 +2881,7 @@ final class ASRService: ObservableObject {
)
if silenceAssessment.shouldSkipTranscription {
+ traceStop("silence_skipped")
DebugLogger.shared.info(
"Final ASR result | provider=\(self.transcriptionProvider.name) | samples=\(pcm.count) | textChars=0 | confidence=nil | reason=short_silence",
source: "ASRService"
@@ -2884,6 +2911,7 @@ final class ASRService: ObservableObject {
}
do {
+ traceStop("readiness_begin")
var provider = self.transcriptionProvider
let ensureStartedAt = Date().timeIntervalSince1970
if self.isAsrReady, provider.isReady {
@@ -2909,6 +2937,7 @@ final class ASRService: ObservableObject {
DebugLogger.shared.debug("Starting transcription with \(pcm.count) samples (\(Float(pcm.count) / 16_000.0) seconds)", source: "ASRService")
let finalStartedAt = Date().timeIntervalSince1970
+ traceStop("final_asr_begin")
let result: ASRTranscriptionResult
let finalSource: String
if useDictionaryTrainingPath {
@@ -2952,6 +2981,7 @@ final class ASRService: ObservableObject {
finalSource = "full"
}
let finalElapsedMs = self.elapsedMilliseconds(since: finalStartedAt)
+ traceStop("final_asr_end")
if !useDictionaryTrainingPath {
self.lastFinalTranscriptionDurationMs = finalElapsedMs
}
diff --git a/Sources/Fluid/Services/DirectCoreAudioInput.swift b/Sources/Fluid/Services/DirectCoreAudioInput.swift
index 58566de4..62c713e4 100644
--- a/Sources/Fluid/Services/DirectCoreAudioInput.swift
+++ b/Sources/Fluid/Services/DirectCoreAudioInput.swift
@@ -498,9 +498,15 @@ private final nonisolated class DirectCoreAudioInput: DirectCoreAudioInputContro
return poisonedStopStatus
}
guard let capture else { return noErr }
+ let stopStartedAt = ProcessInfo.processInfo.systemUptime
let status = fv_core_audio_capture_stop(capture)
+ let hardwareStoppedAt = ProcessInfo.processInfo.systemUptime
fv_core_audio_capture_wake(capture)
self.workerGroup.wait()
+ DebugLogger.shared.info(
+ "STOP_TRACE backend=direct hardwareStopMs=\(Int((hardwareStoppedAt - stopStartedAt) * 1000)) workerDrainMs=\(Int((ProcessInfo.processInfo.systemUptime - hardwareStoppedAt) * 1000))",
+ source: "StopTiming"
+ )
if status != noErr {
self.poisonedStopStatus = status
}
@@ -915,8 +921,12 @@ final nonisolated class DirectCoreAudioLifecycleController: @unchecked Sendable
}
func stop(retainPrepared: Bool, reason: String) async -> StopReport {
- await withCheckedContinuation { continuation in
+ let requestedAt = ProcessInfo.processInfo.systemUptime
+ return await withCheckedContinuation { continuation in
self.lifecycleQueue.async {
+ var trace = OverlayCloseTrace("audio.lifecycleQueue")
+ trace.mark("queueWait", since: requestedAt)
+ defer { trace.finish() }
guard let input = self.input else {
continuation.resume(
returning: StopReport(
@@ -940,6 +950,7 @@ final nonisolated class DirectCoreAudioLifecycleController: @unchecked Sendable
level: .info
)
let status = input.stop()
+ trace.mark("snapshotAndHardwareStop")
let droppedPackets = input.droppedPacketCount
if status != noErr {
self.isPoisoned = true
@@ -965,6 +976,8 @@ final nonisolated class DirectCoreAudioLifecycleController: @unchecked Sendable
"retained=\(retainPrepared && self.input != nil)",
level: .info
)
+ trace.mark("postStopState")
+ DebugLogger.shared.info("CLOSE_DETAIL audioContinuationResume uptime=\(ProcessInfo.processInfo.systemUptime)", source: "StopTiming")
continuation.resume(
returning: StopReport(
status: status,
diff --git a/Sources/Fluid/Services/GlobalHotkeyManager.swift b/Sources/Fluid/Services/GlobalHotkeyManager.swift
index e5586c58..d9392598 100644
--- a/Sources/Fluid/Services/GlobalHotkeyManager.swift
+++ b/Sources/Fluid/Services/GlobalHotkeyManager.swift
@@ -2348,7 +2348,7 @@ final class GlobalHotkeyManager: NSObject {
return false
}
guard !self.isProcessingStop else {
- DebugLogger.shared.debug("Ignoring \(label) - stop already processing", source: "GlobalHotkeyManager")
+ DebugLogger.shared.info("CLOSE_DETAIL shortcutRejected stopLocked=true uptime=\(ProcessInfo.processInfo.systemUptime)", source: "StopTiming")
return false
}
guard !self.asrService.isDictionaryTrainingCaptureActive else {
@@ -2458,8 +2458,8 @@ final class GlobalHotkeyManager: NSObject {
return
}
- self.isProcessingStop = true
- defer { isProcessingStop = false }
+ let closeStartedAt = self.traceStopLocked()
+ defer { self.traceStopUnlocked(since: closeStartedAt) }
if let callback = stopAndProcessCallback {
await callback(toggleStopRequestedAt)
@@ -2532,3 +2532,16 @@ final class GlobalHotkeyManager: NSObject {
cleanupEventTap()
}
}
+
+private extension GlobalHotkeyManager {
+ func traceStopLocked() -> TimeInterval {
+ OverlayCloseRunLoopProbe.begin()
+ self.isProcessingStop = true
+ return ProcessInfo.processInfo.systemUptime
+ }
+
+ func traceStopUnlocked(since startedAt: TimeInterval) {
+ self.isProcessingStop = false
+ DebugLogger.shared.info("CLOSE_DETAIL shortcutUnlocked uptime=\(ProcessInfo.processInfo.systemUptime) heldMs=\((ProcessInfo.processInfo.systemUptime - startedAt) * 1000)", source: "StopTiming")
+ }
+}
diff --git a/Sources/Fluid/Services/TypingService.swift b/Sources/Fluid/Services/TypingService.swift
index 9e356d57..3d7c5eaa 100644
--- a/Sources/Fluid/Services/TypingService.swift
+++ b/Sources/Fluid/Services/TypingService.swift
@@ -464,6 +464,8 @@ final class TypingService {
preserveTranscriptOnClipboard: Bool = false
) async -> TextDeliveryResult {
let requestedAt = ProcessInfo.processInfo.systemUptime
+ var closeTrace = OverlayCloseTrace("typing.delivery")
+ defer { closeTrace.finish() }
let text = plan.plainText
let mode = self.textInsertionMode
let textReadyAge = textReadyAt.map { Self.elapsedMs(from: $0, to: requestedAt) }