diff --git a/Info.plist b/Info.plist
index e2a219f..9efca7d 100644
--- a/Info.plist
+++ b/Info.plist
@@ -19,7 +19,7 @@
CFBundlePackageType
APPL
CFBundleShortVersionString
- 3.1
+ 3.2
CFBundleVersion
1
LSMinimumSystemVersion
diff --git a/src/AppDelegate.swift b/src/AppDelegate.swift
index 0c439d7..b964f89 100644
--- a/src/AppDelegate.swift
+++ b/src/AppDelegate.swift
@@ -403,11 +403,10 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
session.setRecordingActive(false)
let audioSeconds = Double(pcmData.count) / 32000.0
- // FAST PATH FOR SILENT STALL / OFFLINE NETWORK / UNTRANSCRIBED TAIL AUDIO:
- // If user spoke (>0.8s) and socket is dead, disconnected, or has untranscribed tail audio from a mid-speech drop,
- // do NOT wait 4.0s-10s for finalize timeout — jump straight into Smart Audio Recovery in 0ms!
+ // FAST PATH FOR SILENT STALL / OFFLINE NETWORK:
+ // Only abort into Smart Audio Recovery immediately if network is genuinely offline or socket failed.
if hasSpokenInCurrentSession && audioSeconds >= 0.8 {
- if !PingMonitor.shared.isPathSatisfied || !session.isAlive || session.hasUntranscribedTailAudio || (session.hasSilentStall && session.totalReceivedTokensCount == 0) {
+ if !PingMonitor.shared.isPathSatisfied || !session.isAlive || session.hasNetworkFailure || (session.hasSilentStall && session.totalReceivedTokensCount == 0) {
let confirmedPrefix = session.confirmedTranscriptPrefix()
let tailAudio = session.untranscribedTailAudioData()
let bestEffort = session.currentBestTranscript().trimmingCharacters(in: .whitespacesAndNewlines)
@@ -465,8 +464,8 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
// Trigger WebSocket finalization
session.finalizeStream()
- // Dynamic timeout scaled to audio length: 3.5s min up to 8.5s
- let finalizeTimeout = min(8.5, max(3.5, 2.5 + (audioSeconds * 0.25)))
+ // Dynamic timeout scaled to audio length: 6.2s min up to 9.5s (aligned above gateway v2's 5.0s drain)
+ let finalizeTimeout = min(9.5, max(6.2, 4.5 + (audioSeconds * 0.25)))
let watchdog = DispatchWorkItem { [weak self] in
guard let self = self, self.currentGeneration() == myGen else { return }
lock.lock()
diff --git a/src/SonioxClient.swift b/src/SonioxClient.swift
index 1542d6d..78756f9 100644
--- a/src/SonioxClient.swift
+++ b/src/SonioxClient.swift
@@ -80,6 +80,7 @@ public final class SonioxLiveSession {
lock.lock()
isRecordingActive = active
if active {
+ hasSilentStall = false
lastDataTime = Date()
segmentConnectTime = Date()
}
@@ -137,7 +138,7 @@ public final class SonioxLiveSession {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.heartbeatTimer?.invalidate()
- self.heartbeatTimer = Timer.scheduledTimer(withTimeInterval: 6.0, repeats: true) { [weak self] _ in
+ self.heartbeatTimer = Timer.scheduledTimer(withTimeInterval: 18.0, repeats: true) { [weak self] _ in
guard let self = self else { return }
self.lock.lock()
guard !self.isClosed, !self.isFinalizing, !self.isAborted, let task = self.webSocketTask else {
@@ -159,7 +160,7 @@ public final class SonioxLiveSession {
SonioxSessionPool.shared.prewarmNextSession()
} else {
let rtt = Date().timeIntervalSince(t0)
- if rtt > 0.95 {
+ if rtt > 2.5 {
self.lock.lock()
self.hasSilentStall = true
self.lock.unlock()
@@ -559,7 +560,7 @@ public final class SonioxLiveSession {
self.lock.lock()
self.sentAudioBytes += data.count
let sendDur = Date().timeIntervalSince(t0)
- if sendDur > 0.70 {
+ if sendDur > 2.5 {
self.hasSilentStall = true
}
self.lock.unlock()
@@ -594,14 +595,14 @@ public final class SonioxLiveSession {
sendFinalizeFramesOnTask(task)
}
- // Safety timeout fallback: allow up to 4.5s for STT inference & network transit
- DispatchQueue.global().asyncAfter(deadline: .now() + 4.5) { [weak self] in
+ // Safety timeout fallback: allow up to 6.0s (aligned above gateway v2's 5.0s finalize drain)
+ DispatchQueue.global().asyncAfter(deadline: .now() + 6.0) { [weak self] in
guard let self = self else { return }
self.lock.lock()
let stillOpen = !self.isClosed && !self.isAborted
self.lock.unlock()
if stillOpen {
- print("SonioxLiveSession: Finalize timeout (4.5s) reached — completing.")
+ print("SonioxLiveSession: Finalize timeout (6.0s) reached — completing.")
self.completeWithCurrentText()
}
}
@@ -867,7 +868,14 @@ public final class SonioxLiveSession {
return
}
let tailCount = max(0, currentSegmentAudioBuffer.count - lastConfirmedWordAudioByteOffset)
- let hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || !isConnected || (isFinalizing && !didReceiveTermination)
+ // If server cleanly terminated the stream (`didReceiveTermination`) without a network error,
+ // NEVER discard the valid transcript due to a transient mid-speech jitter flag!
+ let hadDrop: Bool
+ if didReceiveTermination && !hasNetworkFailure {
+ hadDrop = false
+ } else {
+ hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || !isConnected || (isFinalizing && !didReceiveTermination)
+ }
let needsTailRecovery = hadDrop && tailCount > 4800
isClosed = true
@@ -1397,17 +1405,19 @@ public final class SonioxLiveSession {
}
}
-// MARK: - On-Demand Session Factory (Zero Idle Key Burn + Non-Blocking Acquisition)
+// MARK: - Zero-Key Pre-Warmed Session Pool (Gateway v2.0 Zero-Key Idle Prewarm Compatible)
public final class SonioxSessionPool {
public static let shared = SonioxSessionPool()
private var prewarmedSession: SonioxLiveSession?
private var prewarmedSessionCreatedAt: Date = Date()
+ private var isPrewarming = false
private let lock = NSLock()
private init() {
setupSleepWakeObservers()
+ prewarmNextSession()
}
private func setupSleepWakeObservers() {
@@ -1417,6 +1427,9 @@ public final class SonioxSessionPool {
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in
self?.discardPrewarmedSession()
+ DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
+ self?.prewarmNextSession()
+ }
}
}
@@ -1425,20 +1438,70 @@ public final class SonioxSessionPool {
prewarmedSession?.stopKeepAliveHeartbeat()
prewarmedSession?.abort()
prewarmedSession = nil
+ isPrewarming = false
lock.unlock()
}
+ /// Pre-warms a WebSocket connection to Gateway v2.0 (`live_ws.py`), which holds ZERO Soniox KeyPool keys
+ /// until the first binary audio frame arrives — eliminating ~700ms TLS+WS handshake delay on every dictation!
public func prewarmNextSession() {
- // Intentional no-op while idle: opening an idle WebSocket burns single-use KeyPool temp keys
- // and risks half-open upstream timeouts. AudioRecorder captures from t=0ms into currentSegmentAudioBuffer,
- // which flushes in strict FIFO order the instant the on-demand WebSocket connects!
+ guard PingMonitor.shared.isPathSatisfied else { return }
+ lock.lock()
+ if let existing = prewarmedSession, existing.isReady {
+ let age = Date().timeIntervalSince(prewarmedSessionCreatedAt)
+ if age < 45.0 {
+ lock.unlock()
+ return
+ }
+ }
+ if isPrewarming {
+ lock.unlock()
+ return
+ }
+ isPrewarming = true
+ prewarmedSession?.stopKeepAliveHeartbeat()
+ prewarmedSession?.abort()
+ prewarmedSession = nil
+ lock.unlock()
+
+ DispatchQueue.global(qos: .utility).async { [weak self] in
+ guard let self = self else { return }
+ let hints = SonioxSettings.shared.language.hints
+ let target = SonioxSettings.shared.targetLanguage.isEnabled ? SonioxSettings.shared.targetLanguage.rawValue : nil
+ let session = SonioxLiveSession(languageHints: hints, targetLanguage: target)
+
+ session.onConnectionStateChanged = { [weak self, weak session] connected in
+ guard let self = self, let session = session else { return }
+ self.lock.lock()
+ if connected {
+ session.startKeepAliveHeartbeat()
+ self.prewarmedSession = session
+ self.prewarmedSessionCreatedAt = Date()
+ self.isPrewarming = false
+ print("SonioxSessionPool: Zero-key prewarmed gateway WS ready (0ms connect delay).")
+ } else {
+ self.isPrewarming = false
+ }
+ self.lock.unlock()
+ }
+
+ DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 10.0) { [weak self] in
+ guard let self = self else { return }
+ self.lock.lock()
+ if self.isPrewarming && self.prewarmedSession == nil {
+ self.isPrewarming = false
+ }
+ self.lock.unlock()
+ }
+ }
}
public func reconnectAll() {
discardPrewarmedSession()
+ prewarmNextSession()
}
- /// Non-blocking session creation (0ms main-thread wait!).
+ /// Non-blocking session acquisition (0ms main-thread wait!).
public func acquireSession() -> SonioxLiveSession {
lock.lock()
let session = prewarmedSession
@@ -1446,9 +1509,14 @@ public final class SonioxSessionPool {
prewarmedSession = nil
lock.unlock()
+ DispatchQueue.global(qos: .utility).async { [weak self] in
+ self?.prewarmNextSession()
+ }
+
let age = Date().timeIntervalSince(createdAt)
- if let session = session, session.isReady && age < 12.0 {
+ if let session = session, session.isReady && age < 55.0 {
session.stopKeepAliveHeartbeat()
+ print("SonioxSessionPool: Acquired HOT zero-key prewarmed session (age: \(String(format: "%.1f", age))s, 0ms connect delay!)")
return session
}