feat(flow): add whole-utterance batch ASR fallback (P1)

- Accumulate utterance PCM in FlowContinuousCapture for batch retry
- Run full-utterance transcribeChunk when stitched final lags partial
- Refactor Mac MLX tail drain to shared FlowUtteranceEndCoordinator
- Add FlowUtterancePCMStore, UtteranceBatchFallbackPolicy, and tests

Co-authored-by: Rocky <hkgood@users.noreply.github.com>
This commit is contained in:
Cursor Agent
2026-07-26 10:15:31 +00:00
parent c7ee891a90
commit f0240c3088
9 changed files with 256 additions and 3 deletions
@@ -0,0 +1,41 @@
// UtteranceBatchFallbackPolicy.swift
// OSGKeyboard · Shared
//
// Decides when to re-run ASR on the full utterance PCM after pipelined
// chunking, and how to pick the best transcript among candidates.
import Foundation
public enum UtteranceBatchFallbackPolicy {
public static let defaultCharacterAdvantage = UtteranceTranscriptGuard.defaultPartialAdvantage
/// True when chunked output likely lost trailing content vs the live partial.
public static func shouldRunBatchFallback(
stitchedFinal: String,
partialSnapshot: String,
minimumCharacterAdvantage: Int = defaultCharacterAdvantage
) -> Bool {
let final = stitchedFinal.trimmingCharacters(in: .whitespacesAndNewlines)
let partial = partialSnapshot.trimmingCharacters(in: .whitespacesAndNewlines)
if final.isEmpty, !partial.isEmpty { return true }
if partial.isEmpty { return false }
return partial.count >= final.count + minimumCharacterAdvantage
}
/// Prefer the longest non-empty transcript after batch ASR completes.
public static func preferredTranscript(
batch: String,
stitchedFinal: String,
partialSnapshot: String,
current: String
) -> String {
let candidates = [batch, current, stitchedFinal, partialSnapshot]
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
guard let best = candidates.max(by: { $0.count < $1.count }) else {
return current.trimmingCharacters(in: .whitespacesAndNewlines)
}
return best
}
}