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.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
Voice: fix wake ignored after first command (stale voiceResult view)
Louis Raille854a23c7813b
diff --git a/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj b/NotchBuddy/NotchBuddy.xcodeproj/project.pbxprojindex 9ac9aa23..0888b512 100644--- a/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj+++ b/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj@@ -164,6 +164,7 @@8C785917FD0740DAE56420A4 /* ServiceSnapshot.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84ECC59BE7CDDEE348FC7096 /* ServiceSnapshot.swift */; };8C9AF392DAE8F2098A7BE7B6 /* ServiceDetail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65F64746B2740D5FE26B7E5A /* ServiceDetail.swift */; };8D1C01F283FA7806119A1C85 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C1A7D5CBE2AC084A10C5FF3 /* SettingsView.swift */; };+ 8DE75D1E260A1314D1C47885 /* VoiceWakeFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABD7A2C29B15EFFAF67FF2DE /* VoiceWakeFilter.swift */; };8DF63B85A54090AEA72A246C /* KitPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49E39852175E81C9632A59C9 /* KitPreviewView.swift */; };8E9A18333EE821A3CF9F7E34 /* DiffEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = B32FB972D02162720E566674 /* DiffEngine.swift */; };8FC5907E9506DC79EB1DDC3C /* ChatMarkdown.swift in Sources */ = {isa = PBXBuildFile; fileRef = D797CC384B5BC70D34F121BC /* ChatMarkdown.swift */; };@@ -219,6 +220,7 @@B7C93262AA9D5DEAA0B79C7C /* MacStubs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B66D0C664FB9FF74517AAF5 /* MacStubs.swift */; };B7ED198C941104866BB3BC57 /* Haptics.swift in Sources */ = {isa = PBXBuildFile; fileRef = F86B99CF65C5E4BC887888C6 /* Haptics.swift */; };BA65FC4A40F7227837C26214 /* DesktopMochiLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = C086E1932F86B9C2B559BE03 /* DesktopMochiLogic.swift */; };+ BBCA973A1AE1385BBE79A834 /* VoiceWakeFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABD7A2C29B15EFFAF67FF2DE /* VoiceWakeFilter.swift */; };BC6819C32C715E203254B9C6 /* TurnShare.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E156CF1C215B8FE713B3A0 /* TurnShare.swift */; };BCB4B7AEA1140868C36F39CC /* SessionStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5108F622EF0C553F053716C2 /* SessionStatus.swift */; };BDD8A210DD6D9DA2D15BC5EE /* PillCatalog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C64C4C5D705CE486221B74 /* PillCatalog.swift */; };@@ -427,6 +429,7 @@A83D5410BD030B243ADDCFF4 /* ClaudeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClaudeService.swift; sourceTree = "<group>"; };A87F39805C78722F614295B4 /* VoiceAudio.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VoiceAudio.swift; sourceTree = "<group>"; };AB78996E9F9A2E8A80D45E54 /* QuestionRelay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuestionRelay.swift; sourceTree = "<group>"; };+ ABD7A2C29B15EFFAF67FF2DE /* VoiceWakeFilter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VoiceWakeFilter.swift; sourceTree = "<group>"; };ADA6A002ABEC0CC57667FBE0 /* MochiLiveActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MochiLiveActivity.swift; sourceTree = "<group>"; };AEF5BD66B00BB64BB80AF38C /* DemoEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoEngine.swift; sourceTree = "<group>"; };B1DDC96E7967032F5D0BD585 /* DesktopMochi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DesktopMochi.swift; sourceTree = "<group>"; };@@ -715,6 +718,7 @@22E03C8E267399FA25C75877 /* VoiceIslandView.swift */,7C432FE1286945E42D3E11D7 /* VoiceResultView.swift */,D4543CB771E0B723783F529E /* VoiceSettings.swift */,+ ABD7A2C29B15EFFAF67FF2DE /* VoiceWakeFilter.swift */,A12EC8F605BBC37F8223D7AA /* WakePhrase.swift */,BC1DE3225F5239394398E6B6 /* WakeSpotter.swift */,68C3405BCC9BF5F378094BFB /* WakeWindowBackoff.swift */,@@ -1021,6 +1025,7 @@9088C7BADF166685E471D248 /* VoiceIslandView.swift in Sources */,CE5ACDD856C76B717ABA295F /* VoiceResultView.swift in Sources */,502F2B0D607C511DD3B836B3 /* VoiceSettings.swift in Sources */,+ BBCA973A1AE1385BBE79A834 /* VoiceWakeFilter.swift in Sources */,C2212AA8BF5834B62308235B /* WakePhrase.swift in Sources */,63B87D9E49F228ADCEC4C293 /* WakeSpotter.swift in Sources */,4CA7948E8518229C04C6BE5A /* WakeWindowBackoff.swift in Sources */,@@ -1202,6 +1207,7 @@C3BA3FA2A4025194FC088C5A /* VoiceIslandView.swift in Sources */,F9685A58729FE64B23FCCFC5 /* VoiceResultView.swift in Sources */,035F9A85BF71B21F9AA5E004 /* VoiceSettings.swift in Sources */,+ 8DE75D1E260A1314D1C47885 /* VoiceWakeFilter.swift in Sources */,3451DB0668FAEEF09503D808 /* WakePhrase.swift in Sources */,B5206E9D10AD77C0C24463AB /* WakeSpotter.swift in Sources */,A0ED41FB1AC8845BE1E30A66 /* WakeWindowBackoff.swift in Sources */,diff --git a/NotchBuddy/Sources/App/IslandWindowController.swift b/NotchBuddy/Sources/App/IslandWindowController.swiftindex 0b3e2e7a..4bfe1bbe 100644--- a/NotchBuddy/Sources/App/IslandWindowController.swift+++ b/NotchBuddy/Sources/App/IslandWindowController.swift@@ -265,7 +265,9 @@ final class IslandWindowController: NSWindowController {// setMode BEFORE changing view: onChange(of: state.view) guards on .expanded,// so setting view while already compact won't trigger a spurious open animation.self.setMode(.compact)- if from == .coucou { self.state.view = self.defaultView() }+ // Reset view when leaving .coucou or .listening so stale views+ // (e.g. .voiceResult) never linger on a collapsed island.+ if from == .coucou || from == .listening { self.state.view = self.defaultView() }// Start 60s hide timer if mouse is not currently over the islandif !self.wasInIsland { self.fsm.mouseLeft() }@@ -1363,8 +1365,11 @@ extension IslandWindowController {private func scheduleVoiceDismiss(delay: TimeInterval) {voiceResultWork?.cancel()let item = DispatchWorkItem { [weak self] in+ guard let self else { return }AppState.shared.voiceResult = nil- self?.fsm.voiceFinished()+ // Reset view before collapsing so shouldIgnoreWake never sees a stale .voiceResult.+ AppState.shared.view = self.defaultView()+ self.fsm.voiceFinished()}voiceResultWork = itemDispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: item)diff --git a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift b/NotchBuddy/Sources/App/Voice/VoiceAudio.swiftindex b4b433e9..37494fe9 100644--- a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift+++ b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift@@ -75,6 +75,7 @@ final class VoiceAudio: @unchecked Sendable {sampleRate: 16_000, channels: 1, interleaved: false)!if let conv = AVAudioConverter(from: fmt, to: target) {conv.primeMethod = .none+ conv.downmix = true // collapse multi-channel mic (e.g. 3ch) to monoconverter = convappendAppLog("nb.log","[Voice] audio format: input \(Int(fmt.sampleRate)) Hz \(fmt.channelCount)ch → 16000 Hz 1ch")diff --git a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift b/NotchBuddy/Sources/App/Voice/VoiceEngine.swiftindex ae8160ac..1f59d122 100644--- a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift+++ b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift@@ -434,9 +434,22 @@ final class VoiceEngine: ObservableObject {// MARK: - Wake detectionprivate func wakeDetected(transcript: String) {- guard isEnabled, !isPaused else { spotter?.endWindow(); return }- guard !isListeningForCommand else { spotter?.endWindow(); return }- guard !shouldIgnoreWake() else { spotter?.endWindow(); return }+ guard isEnabled, !isPaused else {+ let reason = isPaused ? "paused" : "disabled"+ appendAppLog("nb.log", "[Voice] wake ignored: \(reason)")+ spotter?.endWindow(); return+ }+ guard !isListeningForCommand else {+ appendAppLog("nb.log", "[Voice] wake ignored: already listening")+ spotter?.endWindow(); return+ }+ let s = AppState.shared+ if let reason = VoiceWakeFilter.wakeBlocked(+ view: s.view, mode: s.mode, pendingApproval: s.pendingApproval != nil+ ) {+ appendAppLog("nb.log", "[Voice] wake ignored: \(reason)")+ spotter?.endWindow(); return+ }wakeWindowWork?.cancel(); wakeWindowWork = nilunavailableWork?.cancel(); unavailableWork = nilfirstUnavailableAt = nil@@ -507,12 +520,6 @@ final class VoiceEngine: ObservableObject {// MARK: - Helpers- private func shouldIgnoreWake() -> Bool {- let v = AppState.shared.view- return v == .approval || v == .question || v == .prompt || v == .voiceResult- || AppState.shared.pendingApproval != nil- }-private func suitableLocale() -> Locale? {var candidates = MacDictation.automaticLocales()candidates += [Locale(identifier: "fr-FR"), Locale(identifier: "en-US")]diff --git a/NotchBuddy/Sources/App/Voice/VoiceWakeFilter.swift b/NotchBuddy/Sources/App/Voice/VoiceWakeFilter.swiftnew file mode 100644index 00000000..86247784--- /dev/null+++ b/NotchBuddy/Sources/App/Voice/VoiceWakeFilter.swift@@ -0,0 +1,29 @@+// MARK: - VoiceWakeFilter+//+// Pure decision function: should the engine ignore a new wake phrase?+//+// Rules:+// - If a permission approval is pending → always block (the user must act on it first).+// - If the island is NOT expanded → never block (collapsed island → always accept a wake).+// - If the island IS expanded → block only when showing approval, question, or the chat+// prompt (views that require focused interaction). voiceResult is NOT a blocker — a new+// "OK Coucou" replaces the transient result card immediately.+//+// The function is intentionally free of any AppKit / ObservableObject dependency so that it+// can be compiled and unit-tested without a full app context.++enum VoiceWakeFilter {+ /// Returns a human-readable reason string if the wake should be blocked, or nil if allowed.+ static func wakeBlocked(view: IslandView,+ mode: IslandMode,+ pendingApproval: Bool) -> String? {+ if pendingApproval { return "approval shown" }+ guard mode == .expanded else { return nil }+ switch view {+ case .approval: return "approval shown"+ case .question: return "question shown"+ case .prompt: return "chat shown"+ default: return nil+ }+ }+}diff --git a/scripts/test-voice-wake-blocked.sh b/scripts/test-voice-wake-blocked.shnew file mode 100755index 00000000..d125b04e--- /dev/null+++ b/scripts/test-voice-wake-blocked.sh@@ -0,0 +1,11 @@+#!/usr/bin/env bash+set -euo pipefail+ROOT="$(cd "$(dirname "$0")/.." && pwd)"+OUT=/tmp/wake-blocked-tests+echo "=== VoiceWakeFilter tests ==="+swiftc -swift-version 6 -strict-concurrency=complete \+ "$ROOT/tests/IslandTypeStubs.swift" \+ "$ROOT/NotchBuddy/Sources/App/Voice/VoiceWakeFilter.swift" \+ "$ROOT/tests/WakeBlockedTests.swift" \+ -o "$OUT"+"$OUT"diff --git a/tests/IslandTypeStubs.swift b/tests/IslandTypeStubs.swiftnew file mode 100644index 00000000..12c37164--- /dev/null+++ b/tests/IslandTypeStubs.swift@@ -0,0 +1,15 @@+// Minimal stubs for IslandMode and IslandView — used only by standalone test compilation.+// The real definitions live in Sources/CoucouKit/IslandTypes.swift.+import Foundation++enum IslandMode: String, CaseIterable {+ case hidden, compact, expanded+}++enum IslandView: String, CaseIterable {+ case overview, empty, approval, question, error, finished+ case confused, upload, uploading, choose, mail, prompt+ case searching, result, note, settings, greeting, wardrobe, recap+ case listening+ case voiceResult+}diff --git a/tests/WakeBlockedTests.swift b/tests/WakeBlockedTests.swiftnew file mode 100644index 00000000..5e2c6e38--- /dev/null+++ b/tests/WakeBlockedTests.swift@@ -0,0 +1,103 @@+import Foundation++@main+enum WakeBlockedTests {+ static func main() {+ testNeverBlockWhenCollapsed()+ testApprovalViewExpandedBlocks()+ testQuestionViewExpandedBlocks()+ testPromptViewExpandedBlocks()+ testVoiceResultNeverBlocks()+ testPendingApprovalAlwaysBlocks()+ testOverviewExpandedAllowed()+ testListeningExpandedAllowed()+ testNonRegression_wakeAfterResult()+ print("WakeBlockedTests: all cases passed")+ }++ // MARK: - Collapsed island never blocks (the key fix)++ static func testNeverBlockWhenCollapsed() {+ for view in IslandView.allCases {+ let r = VoiceWakeFilter.wakeBlocked(view: view, mode: .compact, pendingApproval: false)+ precondition(r == nil,+ "compact mode must never block, got '\(r ?? "nil")' for view \(view)")+ }+ for view in IslandView.allCases {+ let r = VoiceWakeFilter.wakeBlocked(view: view, mode: .hidden, pendingApproval: false)+ precondition(r == nil,+ "hidden mode must never block, got '\(r ?? "nil")' for view \(view)")+ }+ }++ // MARK: - Expanded + blocking views++ static func testApprovalViewExpandedBlocks() {+ let r = VoiceWakeFilter.wakeBlocked(view: .approval, mode: .expanded, pendingApproval: false)+ precondition(r != nil, "expanded+approval must block")+ precondition(r == "approval shown", "reason must be 'approval shown', got '\(r!)'")+ }++ static func testQuestionViewExpandedBlocks() {+ let r = VoiceWakeFilter.wakeBlocked(view: .question, mode: .expanded, pendingApproval: false)+ precondition(r != nil, "expanded+question must block")+ precondition(r == "question shown", "reason must be 'question shown', got '\(r!)'")+ }++ static func testPromptViewExpandedBlocks() {+ let r = VoiceWakeFilter.wakeBlocked(view: .prompt, mode: .expanded, pendingApproval: false)+ precondition(r != nil, "expanded+prompt must block")+ precondition(r == "chat shown", "reason must be 'chat shown', got '\(r!)'")+ }++ // MARK: - voiceResult never blocks (the root-cause fix)++ static func testVoiceResultNeverBlocks() {+ // Expanded + voiceResult → still accept (a new wake replaces the result card)+ let expanded = VoiceWakeFilter.wakeBlocked(view: .voiceResult, mode: .expanded, pendingApproval: false)+ precondition(expanded == nil,+ "voiceResult + expanded must not block, got '\(expanded ?? "nil")'")+ // Compact + voiceResult → also accept+ let compact = VoiceWakeFilter.wakeBlocked(view: .voiceResult, mode: .compact, pendingApproval: false)+ precondition(compact == nil,+ "voiceResult + compact must not block, got '\(compact ?? "nil")'")+ }++ // MARK: - pendingApproval always blocks regardless of mode/view++ static func testPendingApprovalAlwaysBlocks() {+ for mode in IslandMode.allCases {+ for view in IslandView.allCases {+ let r = VoiceWakeFilter.wakeBlocked(view: view, mode: mode, pendingApproval: true)+ precondition(r != nil,+ "pendingApproval=true must always block (mode=\(mode), view=\(view))")+ }+ }+ }++ // MARK: - Non-blocking expanded views++ static func testOverviewExpandedAllowed() {+ let r = VoiceWakeFilter.wakeBlocked(view: .overview, mode: .expanded, pendingApproval: false)+ precondition(r == nil, "expanded+overview must not block")+ }++ static func testListeningExpandedAllowed() {+ // Shouldn't reach wakeDetected while already listening, but the filter itself is permissive.+ let r = VoiceWakeFilter.wakeBlocked(view: .listening, mode: .expanded, pendingApproval: false)+ precondition(r == nil, "expanded+listening must not block at filter level")+ }++ // MARK: - Non-regression: wake accepted after voiceResult+compact (the original bug)++ static func testNonRegression_wakeAfterResult() {+ // Simulate: command ran → island showed .voiceResult → island collapsed+ // (state.view was left as .voiceResult while mode became .compact)+ // All 5 subsequent wakes must be accepted.+ for i in 1...5 {+ let r = VoiceWakeFilter.wakeBlocked(view: .voiceResult, mode: .compact, pendingApproval: false)+ precondition(r == nil,+ "wake \(i) after voiceResult+compact must be accepted, got '\(r ?? "nil")'")+ }+ }+}