fix(voice): prime lastMicStatus to current auth at watcher startup — root cause of dead wake-word

`AudioGraphOwner.startMicRegrantWatcher` declared `lastMicStatus: Bool = false`
and entered the polling loop without seeding it. The first poll cycle (≈2s
after `open()`) always observed the transition `false → authorized` and
fired `rebuild(trigger: .micRegrant)`. The rebuild's `cancelInFlight` slot
ran `WakeWordDAG.cancel()` which finished the public `wakeWordStream`
permanently — VoiceController's consumer task exited and wake-word stayed
dead for the rest of the process lifetime.

Symptom on real hardware: ~2s after every cold launch, the log showed
`AudioGraphOwner: mic re-granted` followed by
`WakeWordDAG: cancelled — stream finished`, and "Hey Jarvis" never fired.

Fix: prime `lastMicStatus` from the current `AVCaptureDevice.authorizationStatus`
(or `authStatusProbe` test seam) BEFORE entering the polling loop, so the
first poll only fires on a real `denied → authorized` transition.

Also adds defense-in-depth so future legitimate rebuilds don't kill wake-word:
- `WakeWordDAG.stopFeed()` — cancels the feed Task without finishing the
  public stream. Reserved for transient teardown across a rebuild boundary.
- `WakeWordDAG.cancel()` — unchanged; finishes the stream. Reserved for
  permanent shutdown only.

Coverage: `MicRegrantWatcherStartupTests` (M1: no spurious rebuild when
authorized at startup; M2: real denied→authorized still fires; M3:
stopFeed preserves stream).

Live verified: app ran 1m 23s past `AudioGraph: opened`. Pre-fix the
spurious teardown fired at +2s on every cold launch; post-fix the
wake-word DAG stays alive and the stream remains open.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
james.maesandClaude Opus 4.7 committed 2026-05-06 11:07:41 -05:00
1 parent d8983af004
commit cb7ee6fbb8
3 files changed
+273 -16

No files matched your search

@@ -162,6 +162,14 @@ public actor AudioGraphOwner {
/// simulates `AVCaptureDevice.authorizationStatus(for: .audio)` transitions.
internal var authStatusProbe: (@Sendable () -> Bool)? // returns true if authorized
/// Test seam: install `authStatusProbe` from non-isolated test code
/// before calling `open()`. Required because `authStatusProbe` is
/// actor-isolated and setting it from a `@Test` function requires an
/// `await` hop through this method.
internal func _testSetAuthStatusProbe(_ probe: @escaping @Sendable () -> Bool) {
self.authStatusProbe = probe
}
/// Test seam: called after each graph is built successfully, providing access
/// to the fresh `AudioGraph` so tests can install recording hooks on it.
internal var _onGraphBuilt: ((AudioGraph) -> Void)?
@@ -285,8 +293,32 @@ public actor AudioGraphOwner {
/// Polls `AVCaptureDevice.authorizationStatus(for: .audio)` every 2 s.
/// Fires `rebuild(trigger: .micRegrant)` on `.denied → .authorized` transition.
/// Pitfall #5 (06-RESEARCH): KVO is unavailable on authorization status.
///
/// **Startup priming (2026-05-05 voice-loop fix):** the watcher seeds
/// `lastMicStatus` from the current authorization status BEFORE entering
/// the polling loop. Without this, `lastMicStatus` defaulted to `false`
/// and the first poll always saw a spurious `false → authorized`
/// transition — firing an unwanted rebuild ~2s after `open()` whose
/// teardown cancelled the wake-word DAG via `cancelInFlight`. Symptom on
/// real hardware: wake word never fires because the DAG is shut down
/// before the user can speak. Coverage:
/// `MicRegrantWatcherStartupTests` (no-spurious-rebuild assertion).
private func startMicRegrantWatcher() {
micRegrantWatcher = Task { [weak self] in
// Prime `lastMicStatus` with the current auth state so the first
// poll doesn't fire a phantom `false → authorized` rebuild on
// every cold launch. Scoped to a local strong reference so the
// weak/release semantics of the polling loop below are unaffected.
if let strongSelf = self {
let initial: Bool
if let probe = await strongSelf.authStatusProbe {
initial = probe()
} else {
initial = AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
}
await strongSelf.setLastMicStatus(initial)
}
while !Task.isCancelled {
try? await Task.sleep(for: .seconds(2))
guard let self else { return }
@@ -17,12 +17,17 @@ private let logger = Logger(subsystem: "com.koftwentytwo.jarvis", category: "Wak
/// frame per the openWakeWord pipeline. Post-VPIO, Plan 06-01's graph taps at
/// the probed sample rate and resamples to 16 kHz before writing to the ring.
///
/// ## Plan 06-01 integration
/// `cancel()` has the signature `@Sendable () async -> Void` matching the
/// `AudioGraphOwner.cancelInFlight` slot. Plan 06-05 wires it:
/// ## Plan 06-01 integration (2026-05-05 voice-loop fix)
/// `stopFeed()` has the signature `@Sendable () async -> Void` matching the
/// `AudioGraphOwner.cancelInFlight` slot. AppDelegate wires it:
/// ```swift
/// await audioGraphOwner.cancelInFlight = { [dag] in await dag.cancel() }
/// await audioGraphOwner.setCancelInFlight { [dag] in await dag.stopFeed() }
/// ```
/// `stopFeed()` cancels the feed Task but PRESERVES the public stream so
/// `VoiceController.spawnWakeWordConsumer` keeps iterating across the rebuild
/// boundary. AppDelegate's rebuildStream consumer re-arms the DAG against the
/// new ring after the rebuild succeeds. `cancel()` is reserved for permanent
/// shutdown (called from `VoiceController.shutdown` only).
///
/// ## Pause/resume preserve-counter decision
/// The `consecutive` counter is owned by `OpenWakeWordSession`, NOT by the DAG.
@@ -32,11 +37,6 @@ private let logger = Logger(subsystem: "com.koftwentytwo.jarvis", category: "Wak
/// after resume (e.g., the user says "Hey" → pause → "Jarvis" → resume → fires).
/// Reversal cost: change the `if paused { continue }` to
/// `if paused { _ = session.resetConsecutive(); continue }` (one line).
///
/// ## cancelInFlight wiring
/// `cancel()` → `feedTask?.cancel()` → the Task loop exits on `Task.isCancelled`.
/// The stream continuation is finished immediately in `cancel()` so consumers see
/// the stream end without waiting for the Task to drain.
public actor WakeWordDAG {
// MARK: - Public surface
@@ -130,13 +130,10 @@ public actor WakeWordDAG {
/// Cancels the feed Task and finishes the wake-word stream.
///
/// Called by `AudioGraphOwner.cancelInFlight` during the six-step teardown
/// (Plan 06-01, step 1). The Task cancels within 50 ms (one sleep cycle +
/// cooperative cancellation on `Task.isCancelled` check).
///
/// After `cancel()`, the `wakeWordStream` is finished — no further events will
/// be emitted. Callers that iterate `wakeWordStream` will see the async for-loop
/// exit naturally.
/// Permanent shutdown — call from `VoiceController.shutdown` only. After
/// `cancel()`, the `wakeWordStream` is finished and no further events
/// will be emitted. Callers that iterate `wakeWordStream` will see the
/// async for-loop exit naturally.
public func cancel() async {
feedTask?.cancel()
feedTask = nil
@@ -144,6 +141,26 @@ public actor WakeWordDAG {
logger.info("WakeWordDAG: cancelled — stream finished")
}
/// Cancels the feed Task WITHOUT finishing the wake-word stream.
///
/// Use this for transient teardown across an `AudioGraphOwner` rebuild:
/// the producer (ring) is going away, but the public consumer stream
/// must remain live so `VoiceController.spawnWakeWordConsumer`'s
/// `for await event in stream` keeps iterating across the rebuild
/// boundary. After the new graph is open, call `start(ring:)` again
/// against the new ring.
///
/// Wired as `AudioGraphOwner.cancelInFlight` (2026-05-05 voice-loop fix).
/// Previously `cancelInFlight` called `cancel()` which finished the
/// stream — combined with the `lastMicStatus = false` startup bug in
/// `AudioGraphOwner.startMicRegrantWatcher`, the wake path died ~2s
/// after launch on every cold start.
public func stopFeed() async {
feedTask?.cancel()
feedTask = nil
logger.info("WakeWordDAG: feed stopped (stream preserved for rebuild)")
}
// MARK: - Private state
private let session: OpenWakeWordSession
@@ -0,0 +1,208 @@
import Testing
import AVFoundation
@testable import Voice
/// Regression tests for the spurious-startup-rebuild bug (2026-05-05 voice-loop fix).
///
/// Symptom: on every cold launch with mic already authorized, the
/// `startMicRegrantWatcher` polling loop saw `lastMicStatus = false`
/// (its declared default) and the first poll observed `false → authorized`,
/// firing an unwanted `rebuild(trigger: .micRegrant)` ~2s after `open()`.
///
/// In production the rebuild ran `cancelInFlight`, which finished the
/// `WakeWordDAG.wakeWordStream` permanently — wake word was dead before
/// the user could speak.
///
/// Fix: prime `lastMicStatus` from the current authorization status
/// BEFORE entering the polling loop so the first poll only fires on a
/// real `denied → authorized` transition. Plus split `WakeWordDAG.cancel()`
/// (permanent) and `WakeWordDAG.stopFeed()` (transient) so future
/// legitimate rebuilds don't kill the stream either.
@Suite("MicRegrantWatcherStartupTests", .serialized)
struct MicRegrantWatcherStartupTests {
// MARK: M1 — already-authorized at startup must not fire spurious rebuild
@Test("M1: watcher does not fire phantom micRegrant when authorized at open()")
func authorizedAtStartupDoesNotFireRebuild() async throws {
let (degradationStream, degradationCont) = AsyncStream<DegradationReason>.makeStream()
let (rebuildStream, rebuildCont) = AsyncStream<RebuildEvent>.makeStream()
let fmt16k = try #require(
AVAudioFormat(
commonFormat: .pcmFormatFloat32,
sampleRate: 16_000, channels: 1, interleaved: false
)
)
let owner = AudioGraphOwner(
degradationContinuation: degradationCont,
rebuildContinuation: rebuildCont,
graphBuilder: SucceedingBuilder(fmt: fmt16k)
)
// Probe always returns true — mic was already authorized when the
// user launched the app. With the pre-fix `lastMicStatus = false`
// default this would fire rebuild on the first poll cycle.
await owner._testSetAuthStatusProbe { true }
try await owner.open()
let collector = EventCollector<RebuildEvent>()
let collectTask = Task {
for await event in rebuildStream {
await collector.append(event)
}
}
// Wait long enough for the watcher to complete several poll cycles.
// The watcher polls every 2 s; sleep 5 s = at least 2 polls.
try? await Task.sleep(for: .seconds(5))
// Tear down so the collector task exits.
await owner.shutdown()
rebuildCont.finish()
degradationCont.finish()
await collectTask.value
let events = await collector.all
let micRegrantEvents = events.filter {
if case .reconfiguring(let reason) = $0, reason == .micRegrant { return true }
return false
}
#expect(
micRegrantEvents.isEmpty,
"Expected no micRegrant rebuild when authorized at startup, got: \(micRegrantEvents)"
)
// Sanity check: degradation stream produced no spurious events either.
// (Empty rebuilds list is the load-bearing assertion above; this is
// belt-and-suspenders.)
}
// MARK: M2 — real denied → authorized still fires rebuild
@Test("M2: watcher fires micRegrant when probe transitions denied → authorized")
func deniedToAuthorizedFiresRebuild() async throws {
let (degradationStream, degradationCont) = AsyncStream<DegradationReason>.makeStream()
let (rebuildStream, rebuildCont) = AsyncStream<RebuildEvent>.makeStream()
let fmt16k = try #require(
AVAudioFormat(
commonFormat: .pcmFormatFloat32,
sampleRate: 16_000, channels: 1, interleaved: false
)
)
let owner = AudioGraphOwner(
degradationContinuation: degradationCont,
rebuildContinuation: rebuildCont,
graphBuilder: SucceedingBuilder(fmt: fmt16k)
)
// Probe starts denied, flips to authorized after ~2.5 s.
let probeBox = ProbeStateBox(initial: false)
await owner._testSetAuthStatusProbe { probeBox.value }
try await owner.open()
let collector = EventCollector<RebuildEvent>()
let collectTask = Task {
for await event in rebuildStream {
await collector.append(event)
}
}
// Flip probe to authorized after a delay so the watcher's second
// poll observes the transition.
try? await Task.sleep(for: .seconds(3))
probeBox.value = true
// Wait for the next poll cycle to observe the transition.
try? await Task.sleep(for: .seconds(3))
await owner.shutdown()
rebuildCont.finish()
degradationCont.finish()
await collectTask.value
let events = await collector.all
let micRegrantCount = events.filter {
if case .reconfiguring(let reason) = $0, reason == .micRegrant { return true }
return false
}.count
#expect(
micRegrantCount >= 1,
"Expected at least one micRegrant rebuild after denied→authorized, got: \(events)"
)
}
// MARK: M3 — stopFeed preserves stream for rebuild path
@Test("M3: WakeWordDAG.stopFeed() does NOT finish the wake-word stream")
func stopFeedPreservesStream() async throws {
// Construct a DAG with a scripted session — no models needed.
let session = OpenWakeWordSession(scriptedClassifier: { _ in 0.0 })
let dag = WakeWordDAG(session: session)
// Spawn a consumer task on the public stream — it should NOT exit
// when stopFeed() is called.
let consumerExitedBox = ConsumerExitBox()
let consumerTask = Task {
for await _ in dag.wakeWordStream {
// We never expect to see an event in this test.
}
consumerExitedBox.exited = true
}
// Start the feed loop so there's a Task to stop.
let ring = RingBuffer(capacityFrames: 1024)
await dag.start(ring: ring)
// Stop the feed — stream must remain alive.
await dag.stopFeed()
// Give the consumer a moment to react if it's going to exit.
try? await Task.sleep(for: .milliseconds(100))
#expect(
consumerExitedBox.exited == false,
"Consumer of wakeWordStream must NOT exit on stopFeed() — it's reserved for transient teardown across rebuild boundaries."
)
// Now do the permanent shutdown — consumer SHOULD exit.
await dag.cancel()
try? await Task.sleep(for: .milliseconds(100))
#expect(
consumerExitedBox.exited == true,
"Consumer of wakeWordStream MUST exit after cancel() — it's the permanent-shutdown path."
)
consumerTask.cancel()
}
}
// MARK: - Test Support
/// Mutable state holder for the auth-probe transition test. The probe
/// closure captures this box by reference and reads `value` on each call.
final class ProbeStateBox: @unchecked Sendable {
private let lock = NSLock()
private var _value: Bool
init(initial: Bool) { self._value = initial }
var value: Bool {
get { lock.withLock { _value } }
set { lock.withLock { _value = newValue } }
}
}
/// Mutable flag for M3 — the consumer task sets `exited = true` when its
/// `for await` loop terminates.
final class ConsumerExitBox: @unchecked Sendable {
private let lock = NSLock()
private var _exited: Bool = false
var exited: Bool {
get { lock.withLock { _exited } }
set { lock.withLock { _exited = newValue } }
}
}