Browse Source

fix(v3.2): zero-key gateway v2 prewarm (eliminates 700ms delay) & eliminate false RETRY on healthy sessions

main
Ali Alavi 14 hours ago
parent
commit
afb2e84837
  1. 2
      Info.plist
  2. 11
      src/AppDelegate.swift
  3. 94
      src/SonioxClient.swift

2
Info.plist

@ -19,7 +19,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>3.1</string>
<string>3.2</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1</string> <string>1</string>
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>

11
src/AppDelegate.swift

@ -403,11 +403,10 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
session.setRecordingActive(false) session.setRecordingActive(false)
let audioSeconds = Double(pcmData.count) / 32000.0 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 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 confirmedPrefix = session.confirmedTranscriptPrefix()
let tailAudio = session.untranscribedTailAudioData() let tailAudio = session.untranscribedTailAudioData()
let bestEffort = session.currentBestTranscript().trimmingCharacters(in: .whitespacesAndNewlines) let bestEffort = session.currentBestTranscript().trimmingCharacters(in: .whitespacesAndNewlines)
@ -465,8 +464,8 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
// Trigger WebSocket finalization // Trigger WebSocket finalization
session.finalizeStream() 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 let watchdog = DispatchWorkItem { [weak self] in
guard let self = self, self.currentGeneration() == myGen else { return } guard let self = self, self.currentGeneration() == myGen else { return }
lock.lock() lock.lock()

94
src/SonioxClient.swift

@ -80,6 +80,7 @@ public final class SonioxLiveSession {
lock.lock() lock.lock()
isRecordingActive = active isRecordingActive = active
if active { if active {
hasSilentStall = false
lastDataTime = Date() lastDataTime = Date()
segmentConnectTime = Date() segmentConnectTime = Date()
} }
@ -137,7 +138,7 @@ public final class SonioxLiveSession {
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
guard let self = self else { return } guard let self = self else { return }
self.heartbeatTimer?.invalidate() 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 } guard let self = self else { return }
self.lock.lock() self.lock.lock()
guard !self.isClosed, !self.isFinalizing, !self.isAborted, let task = self.webSocketTask else { guard !self.isClosed, !self.isFinalizing, !self.isAborted, let task = self.webSocketTask else {
@ -159,7 +160,7 @@ public final class SonioxLiveSession {
SonioxSessionPool.shared.prewarmNextSession() SonioxSessionPool.shared.prewarmNextSession()
} else { } else {
let rtt = Date().timeIntervalSince(t0) let rtt = Date().timeIntervalSince(t0)
if rtt > 0.95 {
if rtt > 2.5 {
self.lock.lock() self.lock.lock()
self.hasSilentStall = true self.hasSilentStall = true
self.lock.unlock() self.lock.unlock()
@ -559,7 +560,7 @@ public final class SonioxLiveSession {
self.lock.lock() self.lock.lock()
self.sentAudioBytes += data.count self.sentAudioBytes += data.count
let sendDur = Date().timeIntervalSince(t0) let sendDur = Date().timeIntervalSince(t0)
if sendDur > 0.70 {
if sendDur > 2.5 {
self.hasSilentStall = true self.hasSilentStall = true
} }
self.lock.unlock() self.lock.unlock()
@ -594,14 +595,14 @@ public final class SonioxLiveSession {
sendFinalizeFramesOnTask(task) 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 } guard let self = self else { return }
self.lock.lock() self.lock.lock()
let stillOpen = !self.isClosed && !self.isAborted let stillOpen = !self.isClosed && !self.isAborted
self.lock.unlock() self.lock.unlock()
if stillOpen { if stillOpen {
print("SonioxLiveSession: Finalize timeout (4.5s) reached — completing.")
print("SonioxLiveSession: Finalize timeout (6.0s) reached — completing.")
self.completeWithCurrentText() self.completeWithCurrentText()
} }
} }
@ -867,7 +868,14 @@ public final class SonioxLiveSession {
return return
} }
let tailCount = max(0, currentSegmentAudioBuffer.count - lastConfirmedWordAudioByteOffset) 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 let needsTailRecovery = hadDrop && tailCount > 4800
isClosed = true 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 final class SonioxSessionPool {
public static let shared = SonioxSessionPool() public static let shared = SonioxSessionPool()
private var prewarmedSession: SonioxLiveSession? private var prewarmedSession: SonioxLiveSession?
private var prewarmedSessionCreatedAt: Date = Date() private var prewarmedSessionCreatedAt: Date = Date()
private var isPrewarming = false
private let lock = NSLock() private let lock = NSLock()
private init() { private init() {
setupSleepWakeObservers() setupSleepWakeObservers()
prewarmNextSession()
} }
private func setupSleepWakeObservers() { 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 NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in
self?.discardPrewarmedSession() self?.discardPrewarmedSession()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self?.prewarmNextSession()
}
} }
} }
@ -1425,20 +1438,70 @@ public final class SonioxSessionPool {
prewarmedSession?.stopKeepAliveHeartbeat() prewarmedSession?.stopKeepAliveHeartbeat()
prewarmedSession?.abort() prewarmedSession?.abort()
prewarmedSession = nil prewarmedSession = nil
isPrewarming = false
lock.unlock() 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() { 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() { public func reconnectAll() {
discardPrewarmedSession() discardPrewarmedSession()
prewarmNextSession()
} }
/// Non-blocking session creation (0ms main-thread wait!).
/// Non-blocking session acquisition (0ms main-thread wait!).
public func acquireSession() -> SonioxLiveSession { public func acquireSession() -> SonioxLiveSession {
lock.lock() lock.lock()
let session = prewarmedSession let session = prewarmedSession
@ -1446,9 +1509,14 @@ public final class SonioxSessionPool {
prewarmedSession = nil prewarmedSession = nil
lock.unlock() lock.unlock()
DispatchQueue.global(qos: .utility).async { [weak self] in
self?.prewarmNextSession()
}
let age = Date().timeIntervalSince(createdAt) 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() session.stopKeepAliveHeartbeat()
print("SonioxSessionPool: Acquired HOT zero-key prewarmed session (age: \(String(format: "%.1f", age))s, 0ms connect delay!)")
return session return session
} }

Loading…
Cancel
Save