hex/ios/HEXShared/KeyboardCaptureWatch.swift
Ryan Vogel 2d7dff5feb
iOS keyboard: a notification no longer ends an in-keyboard dictation (#2472)
A tester on build 147 (iPhone19,7, iOS 27.2.1): "Dictation stops whenever I
get any notification... Calls stopping it are fine." In-keyboard dictation
records through WebKit in a hidden web view. When iOS gives the audio
session to another sound, WebKit interrupts the page's AudioContext (no
more audio) and mutes the microphone track. It unmutes the track when
WebKit hears the interruption end, but resumes the context only when iOS
says it may (ShouldResume). Some captures fail and end the track. The
page's only defence was a 250 ms ctx.resume(). So:
- a context that didn't come back within 6 s hit the keyboard's watchdog
  ("capture silent"), which finished the dictation with the words so far;
- a track left muted or ended under a running context sent silence for
  good: the dictation went on, but nothing after the sound was heard.

- The capture page reports each track mute/unmute/ended and each context
  statechange (and any change its 250 ms check sees) to the keyboard, with
  the page's time. The keyboard writes every one to the probe log.
- Every 250 ms the page still resumes a context that isn't running. Once
  it runs, it opens the microphone again for a track that ended or stayed
  muted for 3 s (backing off 1, 2, 4, then 8 s), into the same context and
  ScriptProcessor. The same job, file, live words and Type as I speak
  carry on; the gap is missing audio.
- KeyboardCaptureWatch (HEXShared) keeps the recording's clock. While the
  page says it's interrupted, the 6 s quiet rule doesn't count, and quiet
  counts again from when it's back. Interrupted past
  KeyboardJobWatchdog.captureInterruption (15 s: a call, an alarm), it
  finishes with what was said, as before.
- Simulator only (#if targetEnvironment(simulator)): capture-debug.js in
  the App Group runs once in the capture page, and each recording is kept
  as capture-debug.wav, so interruptions can be driven by hand where there
  is no microphone or interruption.
- KeyboardSpeech and KeyboardProbeLog move unchanged to their own files to
  make room: KeyboardLocalDictation.swift 1,398 -> 1,389 lines.
- The app's own recorder (AudioBackend, finishing on interruptionBegan) is
  unchanged.
- ios/docs/dictation.md describes the behaviour.

Verified:
- HEXMobile suite on an iPhone 17 Pro iOS 27.2 simulator (Mac Studio): 952
  tests, 0 failures, including the new KeyboardBridgeTests case (interrupted
  time isn't quiet, quiet restarts at the end, the 15 s limit, a muted
  track's silence counts as interrupted).
- Release simulator build of Hex in a text field. A recorded voice (two
  sentences, 9 s apart) stood in for the microphone, and each interruption
  was driven 5 s in. The kept recordings were transcribed:
  - context suspended 2 s, track muted then unmuted 2 s, both together,
    track muted for good (reopened at +3 s), track stopped (reopened at
    once): each kept both sentences in one dictation;
  - context held suspended: finished at 15 s with the first sentence only.
- Nothing was run on an iPhone. Which of these a notification causes there
  is unverified.
2026-11-23 00:04:17 -05:00

60 lines
2.3 KiB
Swift

import Foundation
/// What the keyboard's capture page last said of its audio context and microphone track
/// (`KeyboardLocalDictation`), with the page's own time.
nonisolated struct KeyboardCapturePageState: Equatable, Sendable {
/// What made the page say it: a track's `mute`, `unmute` or `ended`, the context's
/// `statechange`, a change seen on its 250 ms check, or opening the microphone again.
var cause: String
/// The `AudioContext`'s state: `running`, `suspended`, `interrupted` or `closed`.
var context: String
var muted: Bool
var ended: Bool
var at: TimeInterval
/// No audio is coming from the microphone: iOS gave the audio session to another sound (a
/// notification, an alarm, a call), and WebKit suspends or interrupts the context and mutes
/// or ends the track.
var isInterrupted: Bool { context != "running" || muted || ended }
var summary: String {
"context \(context), track \(ended ? "ended" : muted ? "muted" : "live")"
}
}
/// One recording's watchdog clock (`KeyboardJobWatchdog`): when its audio last came, and since
/// when its page has said the capture is interrupted. The same dictation keeps it across an
/// interruption: its quiet is counted again only from when the page says it's back.
nonisolated struct KeyboardCaptureWatch: Equatable, Sendable {
enum Change: Equatable, Sendable {
case interrupted
case resumed(after: TimeInterval)
}
private(set) var lastAudio: TimeInterval
private(set) var interruptedSince: TimeInterval?
init(startedAt: TimeInterval) {
lastAudio = startedAt
}
mutating func heardAudio(at time: TimeInterval) {
lastAudio = max(lastAudio, time)
}
mutating func page(_ state: KeyboardCapturePageState, at time: TimeInterval) -> Change? {
if state.isInterrupted {
guard interruptedSince == nil else { return nil }
interruptedSince = time
return .interrupted
}
guard let since = interruptedSince else { return nil }
interruptedSince = nil
lastAudio = max(lastAudio, time)
return .resumed(after: time - since)
}
func isStalled(now: TimeInterval) -> Bool {
KeyboardJobWatchdog.isStalled(.recording, lastProgress: lastAudio, interruptedSince: interruptedSince, now: now)
}
}