Skip to content

A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.

Voice: generation counter in WakeSpotter to filter stale task callbacks

Louis Raille8ef1006657ae
diff --git a/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj b/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj
index c9b5452d..b18e454e 100644
--- a/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj
+++ b/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj
@@ -118,6 +118,7 @@
694AB4504CD65662E1681E5C /* ServicePublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F0C229BD085D76EB3DD94DF /* ServicePublisher.swift */; };
6A1AC92C431FA842928E8115 /* NotificationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDB831F5623E699A2506698E /* NotificationViewController.swift */; };
6B4A9D635FD529CE1C2692B8 /* ColorHex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8ACB4350729C6513824D1ADF /* ColorHex.swift */; };
+ 6BB32D6B5E627555994DC3CB /* SpotterGeneration.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EF48A44D82B5422E631FE0 /* SpotterGeneration.swift */; };
6CCEC73ED68E9F253C22D692 /* ShortcutLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA51E5B140C20812641168EA /* ShortcutLogic.swift */; };
6D158183870434FEDF07E050 /* LocalChat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8790ACE9F73BD74640A0F20C /* LocalChat.swift */; };
6D71A1FA10C766E465F326D4 /* BotEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66E6F5FC81429871B6F766A8 /* BotEngine.swift */; };
@@ -208,6 +209,7 @@
B3EC9CF3C8BD629C3530E8F1 /* sounds in Resources */ = {isa = PBXBuildFile; fileRef = C066E2B991AA4F56B2724263 /* sounds */; };
B4F12F8F978FCA7E5309DE28 /* EnergyVAD.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0E2C3ACD815230A0ECAB014 /* EnergyVAD.swift */; };
B5206E9D10AD77C0C24463AB /* WakeSpotter.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC1DE3225F5239394398E6B6 /* WakeSpotter.swift */; };
+ B555DFF3451AFB22CAF8E594 /* SpotterGeneration.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EF48A44D82B5422E631FE0 /* SpotterGeneration.swift */; };
B5D8D34DD7A72F340030EF46 /* MacStubs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B66D0C664FB9FF74517AAF5 /* MacStubs.swift */; };
B605F976F4141435F27CF266 /* IslandRootView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A1A8206F73376F4CAF2057 /* IslandRootView.swift */; };
B69DE31E2B0C2E35594CAA87 /* RecapStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78FB882D49084DEA6A10DB4D /* RecapStore.swift */; };
@@ -468,6 +470,7 @@
F39753CFF90B494D449CCD95 /* IntentParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntentParser.swift; sourceTree = "<group>"; };
F4263B550838CC5614569D13 /* ClaudePlanCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClaudePlanCardView.swift; sourceTree = "<group>"; };
F4A13AEE6E06F46CEC9D908C /* UserIdentity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserIdentity.swift; sourceTree = "<group>"; };
+ F4EF48A44D82B5422E631FE0 /* SpotterGeneration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpotterGeneration.swift; sourceTree = "<group>"; };
F56C7E1EBC74664A7F1689CF /* NowPlayingViews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NowPlayingViews.swift; sourceTree = "<group>"; };
F86B99CF65C5E4BC887888C6 /* Haptics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Haptics.swift; sourceTree = "<group>"; };
F8D3AD61802A32C4833F3EDA /* GitHubPulse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHubPulse.swift; sourceTree = "<group>"; };
@@ -700,6 +703,7 @@
EDC1DEBFCAEBE6F0EEB865F3 /* EntityResolver.swift */,
F39753CFF90B494D449CCD95 /* IntentParser.swift */,
54E47E39D2D52584FA219FA1 /* LiveVoiceControls.swift */,
+ F4EF48A44D82B5422E631FE0 /* SpotterGeneration.swift */,
A2737EC4885ADB8FDDA1C610 /* VoiceActionRunner.swift */,
A87F39805C78722F614295B4 /* VoiceAudio.swift */,
B328028D18F590D6680B8A2B /* VoiceEngine.swift */,
@@ -971,6 +975,7 @@
60E361FFE84C387E5E3ED1AA /* SoundEngine.swift in Sources */,
E2414533EDC7565286141198 /* SpotifyController.swift in Sources */,
4B99E2AD54926078AB087B43 /* SpotifyViews.swift in Sources */,
+ 6BB32D6B5E627555994DC3CB /* SpotterGeneration.swift in Sources */,
1558BB00C530B721172F6E65 /* StripePoller.swift in Sources */,
9EA178EA395B116336C1FA10 /* TerminalTarget+AppKit.swift in Sources */,
1D4CE0CCD2B5A0FFE50C7F06 /* TerminalTarget.swift in Sources */,
@@ -1150,6 +1155,7 @@
78B3C6814431B2DD9D1F4949 /* SoundEngine.swift in Sources */,
2540E4198492CC2B9237ED99 /* SpotifyController.swift in Sources */,
03538CF0E693300A14029EDE /* SpotifyViews.swift in Sources */,
+ B555DFF3451AFB22CAF8E594 /* SpotterGeneration.swift in Sources */,
E4B9986674F4D425826F15A8 /* StripePoller.swift in Sources */,
11F14DB97C93CF84DF9D5C43 /* TerminalTarget+AppKit.swift in Sources */,
6726F74966EDE7F82BFE89B2 /* TerminalTarget.swift in Sources */,
diff --git a/NotchBuddy/Sources/App/Voice/SpotterGeneration.swift b/NotchBuddy/Sources/App/Voice/SpotterGeneration.swift
new file mode 100644
index 00000000..b7d14586
--- /dev/null
+++ b/NotchBuddy/Sources/App/Voice/SpotterGeneration.swift
@@ -0,0 +1,43 @@
+import Foundation
+
+// MARK: - SpotterGeneration
+//
+// Thread-safe generation counter for WakeSpotter task validation.
+// No AppKit/AVFoundation dependency — testable with standalone swiftc.
+//
+// Usage inside WakeSpotter (all mutations under NSLock):
+// • beginWindow: taskGen = gen.bump() — capture generation at task creation
+// • endWindow: gen.bump() — invalidate pending callbacks
+// • handleResult: guard gen.isValid(capturedGen) else { ignore stale callback }
+
+struct SpotterGeneration {
+ private(set) var current: Int = 0
+
+ /// Increment the generation (call on beginWindow and endWindow).
+ /// Returns the new value so callers can capture it.
+ @discardableResult
+ mutating func bump() -> Int {
+ current += 1
+ return current
+ }
+
+ /// True only if `gen` was produced by the most recent `bump()`.
+ func isValid(_ gen: Int) -> Bool {
+ gen == current
+ }
+}
+
+// MARK: - Expected-error classification
+//
+// Returns `true` for errors that represent a normal/expected end of a recognition
+// session: no-speech timeout or voluntary cancellation. These do NOT count as
+// backoff failures in VoiceEngine.
+
+func spotterIsExpectedError(_ error: Error) -> Bool {
+ let e = error as NSError
+ // kAFAssistantErrorDomain/1110 — "no speech detected" (window closed with no audio)
+ if e.domain == "kAFAssistantErrorDomain" && e.code == 1110 { return true }
+ // NSUserCancelledError (3072) — task.cancel() from endWindow()
+ if e.code == NSUserCancelledError { return true }
+ return false
+}
diff --git a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
index 87d3e24c..7b1644f5 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
@@ -15,8 +15,6 @@ import AVFoundation
// - Bypass (`bypassVAD = true`): every buffer is delivered via `onBuffer` regardless of
// silence; `onMicLevel` fires at ~20 Hz with a smoothed level.
//
-// AEC: `setVoiceProcessingEnabled(true)` is applied on macOS 14+ to remove
-// acoustic echo (Mac speakers → mic feedback). Does NOT duck other apps' audio.
final class VoiceAudio: @unchecked Sendable {
private let engine = AVAudioEngine()
diff --git a/NotchBuddy/Sources/App/Voice/WakeSpotter.swift b/NotchBuddy/Sources/App/Voice/WakeSpotter.swift
index 8957af72..e0251c3f 100644
--- a/NotchBuddy/Sources/App/Voice/WakeSpotter.swift
+++ b/NotchBuddy/Sources/App/Voice/WakeSpotter.swift
@@ -13,12 +13,17 @@ import Speech
// Recognition callbacks arrive on an internal Apple thread.
// All shared state is protected by a single NSLock.
//
-// Logging (to nb.log):
+// Stale-callback filtering: every task captures a generation number at creation.
+// `endWindow()` bumps the generation, so callbacks from cancelled/superseded tasks
+// are silently ignored when they arrive after a new window has opened.
+//
+// Logging (to nb.log, prefix [Voice] for one-grep coverage):
// [Voice] spotter task start
// [Voice] spotter task refused (already active)
// [Voice] spotter task refused (unavailable)
// [Voice] spotter task end: final, partials N
// [Voice] spotter task end: error <domain>/<code>, partials N
+// [Voice] spotter stale callback ignored (gen X/Y)
final class WakeSpotter: @unchecked Sendable {
// MARK: - Callbacks (all delivered on the main thread)
@@ -27,7 +32,8 @@ final class WakeSpotter: @unchecked Sendable {
var onCommandUpdate: ((String) -> Void)?
var onCommandEnd: (() -> Void)?
/// Fired when the session ends in wake phase (no command captured).
- /// `wasError` is true when the session ended with an error (not a clean final result).
+ /// `wasError` is true for real errors; false for clean finals, no-speech
+ /// timeout (1110) and cancellations — those do not count as backoff failures.
var onWakeWindowEnded: ((Bool) -> Void)?
// MARK: - State (all guarded by `lock`)
@@ -39,6 +45,7 @@ final class WakeSpotter: @unchecked Sendable {
private var active = false
private var phase: Phase = .wake
private var partialCount = 0
+ private var gen = SpotterGeneration() // stale-callback filter
private enum Phase { case wake, command }
@@ -51,9 +58,10 @@ final class WakeSpotter: @unchecked Sendable {
@discardableResult
func beginWindow(locale: Locale, preroll: [AVAudioPCMBuffer] = []) -> Bool {
enum Refusal { case alreadyActive, unavailable }
- var refusal: Refusal? = nil
- var rSnap: SFSpeechRecognizer?
- var reqSnap: SFSpeechAudioBufferRecognitionRequest?
+ var refusal: Refusal? = nil
+ var rSnap: SFSpeechRecognizer?
+ var reqSnap: SFSpeechAudioBufferRecognitionRequest?
+ var taskGen = 0
lock.withLock {
guard !active else { refusal = .alreadyActive; return }
@@ -72,8 +80,9 @@ final class WakeSpotter: @unchecked Sendable {
active = true
phase = .wake
partialCount = 0
- rSnap = r
- reqSnap = req
+ taskGen = gen.bump() // new generation for this task
+ rSnap = r
+ reqSnap = req
}
if let refusal {
@@ -89,8 +98,9 @@ final class WakeSpotter: @unchecked Sendable {
guard let r = rSnap, let req = reqSnap else { return false }
appendAppLog("nb.log", "[Voice] spotter task start")
- let t = r.recognitionTask(with: req) { [weak self] result, error in
- self?.handleResult(result, error: error)
+ // Capture taskGen by value so callbacks from this specific task carry its generation.
+ let t = r.recognitionTask(with: req) { [weak self, taskGen] result, error in
+ self?.handleResult(result, error: error, expectedGen: taskGen)
}
lock.withLock { task = t }
return true
@@ -100,6 +110,8 @@ final class WakeSpotter: @unchecked Sendable {
lock.withLock { request }?.append(buffer)
}
+ /// Close the window. Bumps the generation so any in-flight callbacks from the
+ /// cancelled task are ignored even if they arrive after the next `beginWindow`.
func endWindow() {
lock.withLock {
request?.endAudio()
@@ -110,6 +122,7 @@ final class WakeSpotter: @unchecked Sendable {
active = false
phase = .wake
partialCount = 0
+ gen.bump() // invalidate callbacks from the task we just cancelled
}
}
@@ -122,12 +135,20 @@ final class WakeSpotter: @unchecked Sendable {
case wakeWindowEnded(wasError: Bool)
}
- private func handleResult(_ result: SFSpeechRecognitionResult?, error: Error?) {
+ private func handleResult(_ result: SFSpeechRecognitionResult?,
+ error: Error?,
+ expectedGen: Int) {
var logMsg: String? = nil
let action: RecogAction? = lock.withLock { () -> RecogAction? in
guard active else { return nil }
+ // Stale-callback guard: ignore results from superseded tasks.
+ guard gen.isValid(expectedGen) else {
+ logMsg = "[Voice] spotter stale callback ignored (gen \(expectedGen)/\(gen.current))"
+ return nil
+ }
+
if let transcript = result?.bestTranscription.formattedString {
partialCount += 1
switch phase {
@@ -146,7 +167,8 @@ final class WakeSpotter: @unchecked Sendable {
if result?.isFinal == true || error != nil {
let wasCommand = phase == .command
let n = partialCount
- let wasError = error != nil
+ // 1110 (no speech) and cancellations are normal ends, not backoff failures.
+ let wasError = error.map { !spotterIsExpectedError($0) } ?? false
request = nil
task = nil
recognizer = nil
diff --git a/scripts/test-voice-spotter-gen.sh b/scripts/test-voice-spotter-gen.sh
new file mode 100755
index 00000000..a08be492
--- /dev/null
+++ b/scripts/test-voice-spotter-gen.sh
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+ROOT="$(cd "$(dirname "$0")/.." && pwd)"
+OUT=/tmp/spotter-gen-tests
+
+echo "=== SpotterGeneration tests ==="
+swiftc -o "$OUT" \
+ "$ROOT/NotchBuddy/Sources/App/Voice/SpotterGeneration.swift" \
+ "$ROOT/tests/SpotterGenerationTests.swift"
+"$OUT"
diff --git a/tests/SpotterGenerationTests.swift b/tests/SpotterGenerationTests.swift
new file mode 100644
index 00000000..56927fd6
--- /dev/null
+++ b/tests/SpotterGenerationTests.swift
@@ -0,0 +1,113 @@
+import Foundation
+
+// MARK: - SpotterGeneration + spotterIsExpectedError tests
+//
+// Pure test — no AppKit, no AVFoundation. Compiles with:
+// swiftc -o /tmp/spotter-gen-tests \
+// NotchBuddy/Sources/App/Voice/SpotterGeneration.swift \
+// tests/SpotterGenerationTests.swift
+
+@main
+enum SpotterGenerationTests {
+
+ static var pass = 0
+ static var fail = 0
+
+ static func main() {
+
+ // ── Generation counter basics ─────────────────────────────────────────
+
+ var gen = SpotterGeneration()
+ check("initial current is 0", gen.current, 0)
+ check("isValid(0) before bump", gen.isValid(0), true)
+
+ let g1 = gen.bump()
+ check("bump returns 1", g1, 1)
+ check("current is 1", gen.current, 1)
+ check("isValid(1) after bump", gen.isValid(1), true)
+ check("isValid(0) after bump", gen.isValid(0), false)
+
+ let g2 = gen.bump()
+ check("second bump returns 2", g2, 2)
+ check("isValid(2)", gen.isValid(2), true)
+ check("isValid(1) stale", gen.isValid(1), false)
+ check("isValid(0) stale", gen.isValid(0), false)
+
+ // ── beginWindow / endWindow simulation ───────────────────────────────
+ //
+ // Scenario: task A starts (gen=1), window ends/cancelled (gen=2),
+ // task B starts (gen=3). A's callback arrives — must be ignored.
+
+ var s = SpotterGeneration()
+ let genA = s.bump() // beginWindow A → gen 1
+ check("genA == 1", genA, 1)
+
+ s.bump() // endWindow → gen 2 (invalidates A)
+ check("genA stale after endWindow", s.isValid(genA), false)
+
+ let genB = s.bump() // beginWindow B → gen 3
+ check("genB == 3", genB, 3)
+ check("genB valid", s.isValid(genB), true)
+ check("genA still stale", s.isValid(genA), false)
+
+ // ── spotterIsExpectedError ────────────────────────────────────────────
+
+ // 1110: no-speech timeout (normal window close, never a backoff failure)
+ let err1110 = NSError(domain: "kAFAssistantErrorDomain", code: 1110)
+ check("1110 is expected", spotterIsExpectedError(err1110), true)
+
+ // NSUserCancelledError (3072): task.cancel() from endWindow
+ let errCancel = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError)
+ check("NSUserCancelledError is expected", spotterIsExpectedError(errCancel), true)
+
+ // A real recognition error (e.g. 1700 = recognition failed)
+ let errReal = NSError(domain: "kAFAssistantErrorDomain", code: 1700)
+ check("code 1700 is NOT expected", spotterIsExpectedError(errReal), false)
+
+ // Different domain, same code as 1110
+ let errWrongDomain = NSError(domain: "com.apple.avfoundation", code: 1110)
+ check("wrong domain + 1110 is NOT expected", spotterIsExpectedError(errWrongDomain), false)
+
+ // ── 20-second restart scenario ────────────────────────────────────────
+ //
+ // Task A opened, window times out after 20 s (endWindow called → gen bumped),
+ // task B opened immediately. Task A's 1110 callback arrives → stale + expected.
+ // Task B must remain valid.
+
+ var s2 = SpotterGeneration()
+ let genA2 = s2.bump() // beginWindow A
+ s2.bump() // endWindow (20s later) — invalidates A
+ let genB2 = s2.bump() // beginWindow B
+
+ // Simulate A's 1110 callback arriving now:
+ let stale = !s2.isValid(genA2) // true → should be ignored
+ let normal = spotterIsExpectedError(err1110) // true → not a backoff error
+ check("20s restart: A's callback is stale", stale, true)
+ check("20s restart: 1110 is expected error", normal, true)
+ check("20s restart: B still valid", s2.isValid(genB2), true)
+
+ // ── Late partial from A containing "ok coucou" ───────────────────────
+ //
+ // After endWindow, A sends a partial with a wake phrase. Because genA is
+ // stale, the callback must be dropped before wake detection.
+
+ var s3 = SpotterGeneration()
+ let genA3 = s3.bump() // beginWindow A
+ s3.bump() // endWindow
+ let genB3 = s3.bump() // beginWindow B
+
+ let latePartialFromA = s3.isValid(genA3) // false → callback dropped
+ check("late partial from A → stale (dropped)", latePartialFromA, false)
+ check("B unaffected by A's late partial", s3.isValid(genB3), true)
+
+ // ── Summary ──────────────────────────────────────────────────────────
+ let total = pass + fail
+ if fail == 0 { print("\n\(total)/\(total) passed.") }
+ else { print("\n\(fail) FAILED / \(total) total"); exit(1) }
+ }
+
+ static func check<T: Equatable>(_ label: String, _ got: T, _ want: T) {
+ if got == want { print("✓ \(label)"); pass += 1 }
+ else { print("✗ \(label) — got \(got), want \(want)"); fail += 1 }
+ }
+}