hex/ios/HEXMobile/DictationController+LaunchFixtures.swift
Ryan Vogel 94838248f6 iOS: say why the keyboard couldn't use its speech model, and offer Try again
What: Settings › Dictation's speech model line said "The keyboard can't use it right now,
so it uses Apple's speech recognition." for six causes. The keyboard now records why with
.unavailable (KeyboardParakeetSettings.UnavailableReason: shortOfMemory, noNeuralEngine,
notOnNeuralEngine, loadFailed), and the line says it and what happens next:
- short of memory at the keyboard's last open: it tries again the next time it opens;
- a keyboard limit under 131 MB (hold .keyboardTooSmall, for good): this iPhone's keyboard
  doesn't have the memory for it;
- no Neural Engine: this iPhone can't run it in the keyboard;
- a load that threw or wasn't on the Neural Engine: the keyboard couldn't load it, with
  Try again (purgesCompiled, and the keyboard now clears its per-process set-aside when it
  sees that flag, so it really loads again);
- an older keyboard's reasonless .unavailable: couldn't use it when it last opened, tries
  again next time.
Getting ready now adds that it takes longer after a download or an update. The keyboard no
longer writes .unavailable for a load still going after loadPatience (180 s); it only logs
it. Removed: the "can't use it right now" string, which also stayed up after the cause had
passed, since nothing but the keyboard's next load, the switch or a model removal clears it.
Fixture scenes settings-dictation-model-english-<preparing|short|noengine|failed|unknown|toosmall>
for screenshots. zh-Hans for every new line.

Why: a post on X (2026-11-25) showed the sentence with no way to tell why.

Verified: DictationSSOTTests (each reason's line, the reason round trip, Try again, no line
says right now); suite and screenshots in a simbox, separately.
2026-11-25 11:50:33 -05:00

290 lines
14 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
import SwiftUI
extension DictationController {
// MARK: - Launch Fixtures for Simulator Verification (`--snap-state <name>`)
static func applyLaunchFixtureIfPresent(to controller: DictationController) -> Bool {
let args = ProcessInfo.processInfo.arguments
guard let flagIndex = args.firstIndex(of: "--snap-state"),
flagIndex + 1 < args.count else {
return false
}
let scene = args[flagIndex + 1]
controller.engine = .appleSpeech
controller.transcriptionLanguage = "English"
controller.transcriptionLanguageID = "en-US"
controller.keyboardBridge.setModelInstalled(true)
controller.isModelReady = true
switch scene {
case "setup", "setup-sheet", "setup-sheet-microphone", "setup-sheet-noaccess", "paste-setting", _ where scene.hasPrefix("onboarding-") || scene.hasPrefix("settings-") || scene.hasPrefix("model-card-"):
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.setWarmSession(.cold)
if scene.hasPrefix("model-card-") { KeyboardParakeetDownload.shared.applyFixture(scene) }
if scene.hasPrefix("settings-dictation-model-") { controller.applySpeechModelFixture(scene) }
case "language-unavailable":
// Russian picked by hand on an iPhone whose Apple Speech can't install it.
let unavailable = SpeechModelUnavailable(
language: "ru-RU",
reason: .notOnThisIPhone,
fallback: "en-US",
automatic: false
)
controller.isModelReady = false
controller.engine = .appleSpeech
controller.applyFixturePhase(.failed(UserFacingError.message(for: unavailable, context: .model)))
controller.languageOffer = "en-US"
case "language-switched":
// Automatic moved on from Russian to English.
let unavailable = SpeechModelUnavailable(language: "ru-RU", reason: .notOnThisIPhone, fallback: "en-US")
controller.applyFixturePhase(.ready)
controller.languageNotice = UserFacingError.message(for: unavailable, context: .model)
case "ready":
controller.applyFixturePhase(.ready)
controller.warmDuration = .off
controller.setWarmSession(.cold)
controller.transcript = "Meet at ten on Thursday and bring the updated design notes."
case "warm":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 42)
controller.transcript = "Meet at ten on Thursday and bring the updated design notes."
case "recording":
controller.applyFixturePhase(.recording)
controller.warmDuration = .fifteenMinutes
controller.audioLevel = 0.74
controller.loudnessDB = -31.0
controller.recordingDuration = 6.4
controller.applyFixtureWarmSession(remaining: 14 * 60 + 54)
case "transcribing":
controller.applyFixturePhase(.transcribing)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 52)
case "handoff-listening", "handoff-no-home-bar":
controller.applyFixturePhase(.recording)
controller.audioLevel = 0.68
controller.loudnessDB = -32.5
controller.recordingDuration = 3.2
controller.applyFixtureWarmSession(remaining: 14 * 60 + 57)
Task { @MainActor in
let start = Date().timeIntervalSince1970 - 3.0
var simTime: Float = 0.0
while !Task.isCancelled {
controller.loudnessDB = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
controller.recordingDuration = Date().timeIntervalSince1970 - start
simTime += 0.033
try? await Task.sleep(for: .milliseconds(33))
}
}
case "handoff-tour":
controller.applyFixturePhase(.recording)
controller.audioLevel = 0.68
controller.loudnessDB = -32.0
controller.recordingDuration = 0.0
controller.applyFixtureWarmSession(remaining: 14 * 60 + 58)
Task { @MainActor in
let boot = Date().timeIntervalSince1970
var simTime: Float = 0.0
while !Task.isCancelled {
let elapsed = Date().timeIntervalSince1970 - boot
if elapsed < 3.4 {
if controller.phase != .recording {
withAnimation(HexMotion.stage) {
controller.applyFixturePhase(.recording)
}
}
controller.loudnessDB = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
controller.recordingDuration = elapsed
simTime += 0.033
} else if elapsed < 4.7 {
if controller.phase != .transcribing {
withAnimation(HexMotion.stage) {
controller.applyFixturePhase(.transcribing)
}
}
controller.loudnessDB = -120.0
} else {
if controller.phase != .ready {
withAnimation(HexMotion.stage) {
controller.applyFixturePhase(.ready)
}
}
}
try? await Task.sleep(for: .milliseconds(33))
}
}
case "hosted-keyboard-warm":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 42)
Task { @MainActor in
while !Task.isCancelled {
let t = Date().timeIntervalSince1970
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .ready,
heartbeat: t,
expiresAt: t + 14 * 60 + 42
)
)
try? await Task.sleep(for: .milliseconds(250))
}
}
case "hosted-keyboard-recording", "hosted-keyboard-light":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 55)
let startWall = Date().timeIntervalSince1970 - 4.0
Task { @MainActor in
var simTime: Float = 0.4
while !Task.isCancelled {
let t = Date().timeIntervalSince1970
let db = -28.5 + 5.2 * sin(simTime * 19.0) + 2.4 * cos(simTime * 7.3)
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .recording,
heartbeat: t,
expiresAt: t + 15 * 60,
recordingStartedAt: startWall,
audioLevel: 0.78,
loudnessDB: db,
jobID: "hosted-demo-job"
)
)
simTime += 0.033
try? await Task.sleep(for: .milliseconds(33))
}
}
case "hosted-keyboard-tour":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 58)
Task { @MainActor in
let boot = Date().timeIntervalSince1970
let freshResultID = "hosted-tour-\(UUID().uuidString)"
var simTime: Float = 0.0
while !Task.isCancelled {
let t = Date().timeIntervalSince1970
let elapsed = t - boot
if elapsed < 2.5 {
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .ready,
heartbeat: t,
expiresAt: t + 15 * 60
)
)
} else if elapsed < 5.2 {
let db = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .recording,
heartbeat: t,
expiresAt: t + 15 * 60,
recordingStartedAt: boot + 2.5,
audioLevel: 0.78,
loudnessDB: db,
jobID: "hosted-tour-job"
)
)
simTime += 0.033
} else if elapsed < 6.4 {
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .transcribing,
heartbeat: t,
expiresAt: t + 15 * 60,
jobID: "hosted-tour-job"
)
)
} else {
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .ready,
heartbeat: t,
expiresAt: t + 14 * 60 + 54,
resultID: freshResultID,
resultExpiresAt: boot + 30,
transcript: "See you at ten."
)
)
}
try? await Task.sleep(for: .milliseconds(33))
}
}
default:
return false
}
return true
}
private func applyFixtureWarmSession(remaining: TimeInterval) {
setWarmSession(.warm(expiresAt: Date().timeIntervalSince1970 + remaining), remaining: remaining)
}
/// `settings-dictation-model-<english|multilingual>-<waiting|downloading|ready>`: Settings ›
/// Dictation › Model with that model picked and its download in that state. Waiting is the
/// real rule: pass `--simulate-cellular` and the keyboard has measured room for the model,
/// so only the connection holds it. The other states leave the keyboard unmeasured, so
/// nothing really downloads on the simulator's Wi-Fi. English also has what the keyboard
/// said about a downloaded model, preparing|short|noengine|failed|unknown (unknown: an
/// older keyboard's unavailable without a reason), and toosmall, a keyboard limit under
/// KeyboardParakeetBudget.requiredLimit.
private func applySpeechModelFixture(_ scene: String) {
let multilingual = scene.contains("-multilingual-")
let step = scene.split(separator: "-").last.map(String.init) ?? "waiting"
let keyboardSaid: [String: (KeyboardParakeetSettings.KeyboardState, KeyboardParakeetSettings.UnavailableReason?)] = [
"preparing": (.preparing, nil),
"short": (.unavailable, .shortOfMemory),
"noengine": (.unavailable, .noNeuralEngine),
"failed": (.unavailable, .loadFailed),
"unknown": (.unavailable, nil),
]
let said = multilingual ? nil : keyboardSaid[step]
let card = multilingual ? "waiting" : said == nil ? step : "ready"
KeyboardParakeetDownload.shared.applyFixture("model-card-\(card)")
if step == "waiting" {
KeyboardParakeetSettings().recordKeyboardLimit(KeyboardParakeetBudget.requiredLimit)
} else if step == "toosmall" {
KeyboardParakeetSettings().recordKeyboardLimit(KeyboardParakeetBudget.requiredLimit - 32 * KeyboardParakeetBudget.megabyte)
}
if let said {
KeyboardParakeetSettings().setKeyboardState(said.0, reason: said.1)
}
MultilingualParakeetSettings().isEnabled = multilingual
downloadNetwork.forgetMobileData()
guard multilingual else { return }
ParakeetUltraModel.remove()
switch step {
case "downloading":
multilingualDownload.applyFixture(share: 0.42)
case "ready":
// Files of the right sizes (sparse), so it reads as on the iPhone; nothing loads them.
for file in ParakeetUltraModel.files {
let url = ParakeetUltraModel.directory.appendingPathComponent(file.path)
try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
FileManager.default.createFile(atPath: url.path, contents: nil)
let handle = try? FileHandle(forWritingTo: url)
try? handle?.truncate(atOffset: UInt64(file.size))
try? handle?.close()
}
KeyboardParakeetSettings().setKeyboardState(.ready, multilingual: true)
default:
startMultilingualDownloadIfWanted()
}
}
}