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.

perf: idle CPU and smoothness pass (no visual change)

Claudeb4343d9dfacb
diff --git a/NotchBuddy/Sources/App/AppLog.swift b/NotchBuddy/Sources/App/AppLog.swift
index 64797a6c..b94c3a97 100644
--- a/NotchBuddy/Sources/App/AppLog.swift
+++ b/NotchBuddy/Sources/App/AppLog.swift
@@ -14,36 +14,73 @@ func escapedForLog(_ s: String) -> String {
/// - Log directory is created at mode 0700.
/// - Log file is set to mode 0600 on first creation and after each rotation.
/// - File is rotated (truncated) when it reaches 1 MB.
+/// The time is taken now; the write happens on a background serial queue, in order, so a
+/// log line never blocks the main thread (it used to cost ~8 file operations per line).
func appendAppLog(_ fileName: String, _ message: String,
timestampFormat: String = "yyyy-MM-dd HH:mm:ss") {
- let fm = FileManager.default
- let logsDir = fm.urls(for: .libraryDirectory, in: .userDomainMask)[0]
+ let date = Date()
+ AppLogWriter.queue.async {
+ AppLogWriter.write(fileName, message, date: date, format: timestampFormat)
+ }
+}
+
+private enum AppLogWriter {
+ static let queue = DispatchQueue(label: "fr.louisraille.NotchBuddy.applog", qos: .utility)
+ static let maxLogBytes = 1_048_576 // 1 MB
+
+ // Touched only on `queue`.
+ nonisolated(unsafe) static var dirReady = false
+ nonisolated(unsafe) static var sizes: [String: Int] = [:]
+ nonisolated(unsafe) static var formatters: [String: DateFormatter] = [:]
+
+ static let logsDir = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
.appendingPathComponent("Logs/NotchBuddy")
- try? fm.createDirectory(at: logsDir, withIntermediateDirectories: true)
- try? fm.setAttributes([.posixPermissions: 0o700 as NSNumber], ofItemAtPath: logsDir.path)
- let logFile = logsDir.appendingPathComponent(fileName)
- let f = DateFormatter(); f.dateFormat = timestampFormat
- let line = "\(f.string(from: Date())) \(escapedForLog(message))\n"
- guard let data = line.data(using: .utf8) else { return }
- let maxLogBytes = 1_048_576 // 1 MB
- if fm.fileExists(atPath: logFile.path) {
- // Rotate when the file reaches the limit
- let size = (try? fm.attributesOfItem(atPath: logFile.path)[.size] as? Int) ?? 0
- if size >= maxLogBytes {
+
+ static func write(_ fileName: String, _ message: String, date: Date, format: String) {
+ let fm = FileManager.default
+ if !dirReady {
+ try? fm.createDirectory(at: logsDir, withIntermediateDirectories: true)
+ try? fm.setAttributes([.posixPermissions: 0o700 as NSNumber], ofItemAtPath: logsDir.path)
+ dirReady = true
+ }
+ let logFile = logsDir.appendingPathComponent(fileName)
+ let f: DateFormatter
+ if let cached = formatters[format] { f = cached } else {
+ f = DateFormatter(); f.dateFormat = format; formatters[format] = f
+ }
+ let line = "\(f.string(from: date)) \(escapedForLog(message))\n"
+ guard let data = line.data(using: .utf8) else { return }
+
+ // Size known from our own writes; read from disk once per file (or if it vanished).
+ var size = sizes[fileName]
+ if size == nil || !fm.fileExists(atPath: logFile.path) {
+ size = fm.fileExists(atPath: logFile.path)
+ ? ((try? fm.attributesOfItem(atPath: logFile.path)[.size] as? Int) ?? 0) : nil
+ }
+ guard let current = size else {
+ // New file
+ try? data.write(to: logFile, options: .atomic)
+ try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
+ sizes[fileName] = data.count
+ return
+ }
+ if current >= maxLogBytes {
+ // Rotate when the file reaches the limit
try? fm.removeItem(at: logFile)
try? data.write(to: logFile, options: .atomic)
try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
+ sizes[fileName] = data.count
return
}
+ if sizes[fileName] == nil {
+ // First write to an existing file this run: make sure of its permissions.
+ try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
+ }
if let handle = try? FileHandle(forWritingTo: logFile) {
handle.seekToEndOfFile()
handle.write(data)
try? handle.close()
}
- // Ensure permissions even on existing files (idempotent)
- try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
- } else {
- try? data.write(to: logFile, options: .atomic)
- try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
+ sizes[fileName] = current + data.count
}
}
diff --git a/NotchBuddy/Sources/App/AppState.swift b/NotchBuddy/Sources/App/AppState.swift
index c4d830fc..a8129020 100644
--- a/NotchBuddy/Sources/App/AppState.swift
+++ b/NotchBuddy/Sources/App/AppState.swift
@@ -98,18 +98,19 @@ final class AppState: ObservableObject {
}
// Transient: outfit preview while hovering in wardrobe (overrides resolvedOutfit in BotCanvasView)
var wardrobePreviewOutfit: Outfit? = nil
- // Per-day seasonal cache — avoids recomputing Easter and date math on every frame
- private var _seasonalCache: (dayOfYear: Int, year: Int, outfit: Outfit)?
+ // Per-day seasonal cache — read on every Mochi frame: valid until the next midnight,
+ // so a frame costs one date comparison instead of calendar math.
+ private var _seasonalCache: (validUntil: Date, outfit: Outfit)?
var resolvedOutfit: Outfit {
if let preview = wardrobePreviewOutfit { return preview }
guard mochiOutfitSelection == .auto else { return mochiOutfitSelection }
- let cal = Calendar.current
let now = Date()
- let day = cal.ordinality(of: .day, in: .year, for: now) ?? 0
- let year = cal.component(.year, from: now)
- if let c = _seasonalCache, c.dayOfYear == day && c.year == year { return c.outfit }
+ if let c = _seasonalCache, now < c.validUntil { return c.outfit }
+ let cal = Calendar.current
let outfit = Outfit.seasonal(for: now, calendar: cal)
- _seasonalCache = (dayOfYear: day, year: year, outfit: outfit)
+ let midnight = cal.date(byAdding: .day, value: 1, to: cal.startOfDay(for: now))
+ ?? now.addingTimeInterval(3600)
+ _seasonalCache = (validUntil: midnight, outfit: outfit)
return outfit
}
@@ -463,10 +464,19 @@ final class AppState: ObservableObject {
// Which card showingPlanDetail opens
@Published var planDetailIsCodex: Bool = false
+ /// Rate-limited on the attempt, not only on success: without Codex installed or
+ /// signed in, every appearance of the pill used to spawn `codex app-server` again.
+ private static var lastCodexAttempt: Date = .distantPast
+ private static var codexFetchInFlight = false
+
func refreshCodexPlanUsage() {
if let u = codexPlanUsage, Date().timeIntervalSince(u.updatedAt) < 60 { return }
+ guard !Self.codexFetchInFlight, Date().timeIntervalSince(Self.lastCodexAttempt) > 60 else { return }
+ Self.codexFetchInFlight = true
+ Self.lastCodexAttempt = Date()
Task {
if let u = await CodexPlanGauge.fetch() { codexPlanUsage = u }
+ Self.codexFetchInFlight = false
}
}
@@ -764,6 +774,16 @@ struct VercelDeployment: Identifiable {
}
}
+// MARK: - Publish only on change
+
+extension AppState {
+ /// A @Published set notifies every view that observes AppState, even with the same
+ /// value: pollers use this so an unchanged answer redraws nothing.
+ func setIfChanged<T: Equatable>(_ keyPath: ReferenceWritableKeyPath<AppState, T>, _ value: T) {
+ if self[keyPath: keyPath] != value { self[keyPath: keyPath] = value }
+ }
+}
+
// MARK: - Resend
struct ResendEmail: Identifiable {
diff --git a/NotchBuddy/Sources/App/BotCanvasView.swift b/NotchBuddy/Sources/App/BotCanvasView.swift
index 50dcc041..5e862989 100644
--- a/NotchBuddy/Sources/App/BotCanvasView.swift
+++ b/NotchBuddy/Sources/App/BotCanvasView.swift
@@ -1,4 +1,5 @@
import SwiftUI
+import AppKit
/// SwiftUI wrapper: TimelineView drives a Canvas that calls BotEngine.draw().
/// Uses a shared engine per-task; the main bot uses AppState's shared engine.
@@ -8,18 +9,21 @@ struct BotCanvasView: View {
/// When set, overrides island-based eye-tracking (used by desktop Mochi).
/// CGPoint in the same coord space as state.mousePosition (DesktopSpace, y-down).
var lookOriginOverride: CGPoint? = nil
+ /// Not drawn while true (e.g. the notch copy while Mochi is on the desktop).
+ var paused: Bool = false
// One engine per view instance (main bot)
@StateObject private var engine = BotEngine()
var body: some View {
- TimelineView(.animation(paused: state.mode == .hidden)) { timeline in
+ TimelineView(.animation(paused: state.mode == .hidden || paused)) { timeline in
Canvas { context, size in
let now = timeline.date.timeIntervalSinceReferenceDate
let dtRaw = min(0.05, now - engine.lastTime)
let dt = dtRaw
- engine.lookX = lookX(state: state, size: size)
- engine.lookY = lookY(state: state, size: size)
+ let look = lookXY(state: state)
+ engine.lookX = look.x
+ engine.lookY = look.y
engine.particleOverhang = particleOverhang
// Widen slot when file is hovering over the mailbox (morph > 0.5)
// Open mouth (hover=0.20R) when file dragged over box; close when not
@@ -177,44 +181,34 @@ struct BotCanvasView: View {
}
}
- private func lookX(state: AppState, size: CGSize) -> CGFloat {
+ /// Where Mochi looks (the pointer), both axes at once: the island geometry and the
+ /// screen are looked up once per frame instead of twice each.
+ private func lookXY(state: AppState) -> (x: CGFloat, y: CGFloat) {
if let origin = lookOriginOverride {
- return tanh((state.mousePosition.x - origin.x) / 260)
+ return (tanh((state.mousePosition.x - origin.x) / 260),
+ -tanh((state.mousePosition.y - origin.y) / 200))
}
let (islandW, islandH) = islandSize(mode: state.mode, view: state.view,
progress: state.uploadProgress,
nw: state.notchWidth, nh: state.notchHeight)
- let (botCx, botCy, _, _) = botPosition(mode: state.mode, view: state.view,
- islandW: islandW, islandH: islandH,
- uploadProgress: state.uploadProgress)
- let bot = islandBotPoint(islandW: islandW, botCx: botCx, botCy: botCy)
- return tanh((state.mousePosition.x - bot.x) / 260)
- }
-
- /// Bot centre in DesktopSpace, like state.mousePosition. The island is centred at the
- /// top of its screen, which can be any display, anywhere in the arrangement.
- private func islandBotPoint(islandW: CGFloat, botCx: CGFloat, botCy: CGFloat) -> CGPoint {
let screen = IslandWindowController.islandScreen().frame
- return DesktopSpace.topDown(CGPoint(x: screen.midX - islandW / 2 + botCx,
- y: screen.maxY - botCy),
- desktopTop: IslandWindowController.desktopTop)
- }
-
- private func lookY(state: AppState, size: CGSize) -> CGFloat {
- if let origin = lookOriginOverride {
- return -tanh((state.mousePosition.y - origin.y) / 200)
+ let desktopTop = IslandWindowController.desktopTop
+ func botPoint(islandH: CGFloat) -> CGPoint {
+ let (botCx, botCy, _, _) = botPosition(mode: state.mode, view: state.view,
+ islandW: islandW, islandH: islandH,
+ uploadProgress: state.uploadProgress)
+ return DesktopSpace.topDown(CGPoint(x: screen.midX - islandW / 2 + botCx,
+ y: screen.maxY - botCy),
+ desktopTop: desktopTop)
}
- let (islandW, islandH) = islandSize(mode: state.mode, view: state.view,
- progress: state.uploadProgress,
- nw: state.notchWidth, nh: state.notchHeight)
+ let botX = botPoint(islandH: islandH)
+ // The chat view grows with the conversation: vertical look uses its real height.
let actualH: CGFloat = (state.mode == .expanded && state.view == .prompt)
? min(300, 240 + CGFloat(state.chatHistory.count) * 40)
: islandH
- let (botCx, botCy, _, _) = botPosition(mode: state.mode, view: state.view,
- islandW: islandW, islandH: actualH,
- uploadProgress: state.uploadProgress)
- let bot = islandBotPoint(islandW: islandW, botCx: botCx, botCy: botCy)
- return -tanh((state.mousePosition.y - bot.y) / 200)
+ let botY = actualH == islandH ? botX : botPoint(islandH: actualH)
+ return (tanh((state.mousePosition.x - botX.x) / 260),
+ -tanh((state.mousePosition.y - botY.y) / 200))
}
}
@@ -235,13 +229,26 @@ struct MiniBotCanvasView: View {
}())
}
+ @Environment(\.islandViewActive) private var viewActive
+
+ /// The rate the mini Mochis used to be drawn at (the built-in display's).
+ private static let displayFPS: Double =
+ Double(min(120, max(60, NSScreen.screens.first?.maximumFramesPerSecond ?? 60)))
+
var body: some View {
- TimelineView(.animation) { timeline in
+ // A 12–20 pt Mochi: 30 fps looks the same as the display rate and costs a quarter
+ // on ProMotion. Paused in island views that are not shown.
+ TimelineView(.animation(minimumInterval: 1.0 / 30.0, paused: !viewActive)) { _ in
Canvas { context, size in
- let now = timeline.date.timeIntervalSinceReferenceDate
- let dt = min(0.05, now - engine.lastTime)
engine.setDancing(isDancing)
- engine.update(dt: dt)
+ // The engine moves 0.05 per step, one step per display frame as before:
+ // same motion, only fewer drawings.
+ let elapsed = min(1.0 / 15.0, max(0, CACurrentMediaTime() - engine.lastTime))
+ engine.stepDebt += elapsed * Self.displayFPS
+ let steps = min(8, Int(engine.stepDebt))
+ engine.stepDebt -= Double(steps)
+ if steps == 0 { engine.lastTime = CACurrentMediaTime() }
+ for _ in 0..<steps { engine.update(dt: 0.05) }
var ctx = context
engine.applyDance(&ctx, size: size)
engine.draw(context: ctx, size: size)
diff --git a/NotchBuddy/Sources/App/CalcomPoller.swift b/NotchBuddy/Sources/App/CalcomPoller.swift
index c04f6ea7..5fe10c77 100644
--- a/NotchBuddy/Sources/App/CalcomPoller.swift
+++ b/NotchBuddy/Sources/App/CalcomPoller.swift
@@ -8,7 +8,7 @@ final class CalcomPoller: @unchecked Sendable {
func start() {
guard timer == nil else { return }
let t = DispatchSource.makeTimerSource(queue: .global(qos: .background))
- t.schedule(deadline: .now() + 8, repeating: 300)
+ t.schedule(deadline: .now() + 8, repeating: 300, leeway: .seconds(30))
t.setEventHandler { [weak self] in self?.poll() }
t.resume()
timer = t
@@ -46,7 +46,7 @@ final class CalcomPoller: @unchecked Sendable {
if code == 401 { msg = "Invalid API key (401)" }
else if code == 0 { msg = error?.localizedDescription ?? "No connection" }
else { msg = "API error \(code)" }
- DispatchQueue.main.async { AppState.shared.calcomError = msg }
+ DispatchQueue.main.async { AppState.shared.setIfChanged(\.calcomError, msg) }
return
}
guard let data,
@@ -55,9 +55,9 @@ final class CalcomPoller: @unchecked Sendable {
let parsed = rawList.compactMap { self.parseBooking($0) }
DispatchQueue.main.async {
- AppState.shared.calcomError = nil
- AppState.shared.calcomBookings = parsed
- AppState.shared.calcomLoaded = true
+ AppState.shared.setIfChanged(\.calcomError, nil)
+ AppState.shared.setIfChanged(\.calcomBookings, parsed)
+ AppState.shared.setIfChanged(\.calcomLoaded, true)
}
}.resume()
}
diff --git a/NotchBuddy/Sources/App/ClaudePlanGauge.swift b/NotchBuddy/Sources/App/ClaudePlanGauge.swift
index e9258adc..e946d181 100644
--- a/NotchBuddy/Sources/App/ClaudePlanGauge.swift
+++ b/NotchBuddy/Sources/App/ClaudePlanGauge.swift
@@ -2,7 +2,7 @@ import Foundation
// MARK: - Plan window
-struct PlanWindow: Codable {
+struct PlanWindow: Codable, Equatable {
let usedPct: Double // 0–100, clamped
let resetsAt: Date
}
diff --git a/NotchBuddy/Sources/App/DesktopMochi.swift b/NotchBuddy/Sources/App/DesktopMochi.swift
index 829d5391..88dfadb9 100644
--- a/NotchBuddy/Sources/App/DesktopMochi.swift
+++ b/NotchBuddy/Sources/App/DesktopMochi.swift
@@ -487,22 +487,44 @@ final class DesktopMochiController {
}
}
- // MARK: - 60 Hz polling (only while panel is live)
+ // MARK: - 30 Hz polling (only while panel is live and the screen is on)
+ // Mochi is drawn at 30 fps (10 asleep): polling faster bought nothing. It also feeds
+ // the eye tracking (AppState.mousePosition), so the island poll can idle meanwhile.
+
+ /// True between startPolling and stopPolling, whatever the screen does meanwhile.
+ private var pollingWanted = false
private func startPolling() {
frameTimer?.invalidate()
- frameTimer = Timer.scheduledTimer(withTimeInterval: 1.0/60.0, repeats: true) { [weak self] _ in
- guard let self else { return }
- Task { @MainActor in self.pollFrame() }
+ frameTimer = nil
+ pollingWanted = true
+ guard !screenSleeping else { return }
+ let timer = Timer(timeInterval: 1.0 / 30.0, repeats: true) { [weak self] _ in
+ // On the main run loop: already on the main actor, no Task per tick.
+ MainActor.assumeIsolated { self?.pollFrame() }
}
- RunLoop.main.add(frameTimer!, forMode: .common)
+ timer.tolerance = 0.005
+ RunLoop.main.add(timer, forMode: .common)
+ frameTimer = timer
}
private func stopPolling() {
+ pollingWanted = false
frameTimer?.invalidate()
frameTimer = nil
}
+ private func pausePollingForScreen() {
+ frameTimer?.invalidate()
+ frameTimer = nil
+ }
+
+ /// Only when it was polling before the screen went off.
+ private func resumePollingForScreen() {
+ guard pollingWanted, panel != nil, frameTimer == nil else { return }
+ startPolling()
+ }
+
private func pollFrame() {
guard let p = panel else { return }
let mouse = NSEvent.mouseLocation
@@ -519,6 +541,9 @@ final class DesktopMochiController {
// Update eye-tracking origin every frame
viewState?.lookOrigin = lookOriginFor(panel: p)
+ let pos = DesktopSpace.topDown(mouse, desktopTop: IslandWindowController.desktopTop)
+ let cur = AppState.shared.mousePosition
+ if abs(pos.x - cur.x) > 1 || abs(pos.y - cur.y) > 1 { AppState.shared.mousePosition = pos }
// Sleep detection
let agentActive = AppState.shared.effectiveState != .idle &&
@@ -661,6 +686,7 @@ final class DesktopMochiController {
Task { @MainActor in
self?.screenSleeping = true
self?.viewState?.paused = true
+ self?.pausePollingForScreen()
}
}
NSWorkspace.shared.notificationCenter.addObserver(
@@ -669,6 +695,7 @@ final class DesktopMochiController {
Task { @MainActor in
self?.screenSleeping = false
self?.viewState?.paused = false
+ self?.resumePollingForScreen()
}
}
}
@@ -682,6 +709,7 @@ final class DesktopMochiController {
Task { @MainActor in
self?.screenSleeping = true
self?.viewState?.paused = true
+ self?.pausePollingForScreen()
}
}
DistributedNotificationCenter.default().addObserver(
@@ -690,6 +718,7 @@ final class DesktopMochiController {
Task { @MainActor in
self?.screenSleeping = false
self?.viewState?.paused = false
+ self?.resumePollingForScreen()
}
}
}
diff --git a/NotchBuddy/Sources/App/GithubPoller.swift b/NotchBuddy/Sources/App/GithubPoller.swift
index 2307d263..2435a179 100644
--- a/NotchBuddy/Sources/App/GithubPoller.swift
+++ b/NotchBuddy/Sources/App/GithubPoller.swift
@@ -15,7 +15,7 @@ final class GithubPoller: @unchecked Sendable {
guard timer == nil else { return }
// Stats poll: every 5 minutes, starting 7 s after launch
let t = DispatchSource.makeTimerSource(queue: .global(qos: .background))
- t.schedule(deadline: .now() + 7, repeating: 300)
+ t.schedule(deadline: .now() + 7, repeating: 300, leeway: .seconds(30))
t.setEventHandler { [weak self] in self?.pollStats() }
t.resume()
timer = t
diff --git a/NotchBuddy/Sources/App/HookServer.swift b/NotchBuddy/Sources/App/HookServer.swift
index a1c537f2..6ff4b70b 100644
--- a/NotchBuddy/Sources/App/HookServer.swift
+++ b/NotchBuddy/Sources/App/HookServer.swift
@@ -649,9 +649,13 @@ final class HookServer: @unchecked Sendable {
@MainActor
private func processStatusLine(payload: [String: Any]) {
- if let usage = ClaudePlanGauge.parse(payload: payload) {
- AppState.shared.claudePlanUsage = usage
- }
+ guard let usage = ClaudePlanGauge.parse(payload: payload) else { return }
+ // Claude Code calls the status line several times a second: publish (and save to
+ // UserDefaults) only when the numbers change, or every 30 s for "updated … ago".
+ if let cur = AppState.shared.claudePlanUsage,
+ cur.fiveHour == usage.fiveHour, cur.sevenDay == usage.sevenDay,
+ usage.updatedAt.timeIntervalSince(cur.updatedAt) < 30 { return }
+ AppState.shared.claudePlanUsage = usage
}
// MARK: - Permission request (blocking — Claude Code waits for decision)
diff --git a/NotchBuddy/Sources/App/IslandRootView.swift b/NotchBuddy/Sources/App/IslandRootView.swift
index dbfbd3e2..6dadd930 100644
--- a/NotchBuddy/Sources/App/IslandRootView.swift
+++ b/NotchBuddy/Sources/App/IslandRootView.swift
@@ -320,14 +320,15 @@ struct BotPlacement: View {
let t = min(1.0, max(0, elapsed / state.uploadDuration))
// cx = 36 + 526*t: bot center at fill right edge (bar left=36, width=526)
let uploadCx = 36 + CGFloat(t * (2 - t)) * 526
- BotCanvasView(state: state, particleOverhang: 0)
+ BotCanvasView(state: state, particleOverhang: 0, paused: state.mochiOnDesktop)
.frame(width: canvasSize, height: canvasSize)
.opacity(state.isDraggingBot || state.mochiOnDesktop ? 0 : opacity)
.position(x: uploadCx, y: cy)
}
.transition(.scale(scale: 0.01, anchor: .center).combined(with: .opacity))
} else {
- BotCanvasView(state: state, particleOverhang: overhang)
+ // Invisible while Mochi lives on the desktop: no drawing either.
+ BotCanvasView(state: state, particleOverhang: overhang, paused: state.mochiOnDesktop)
.frame(width: canvasSize, height: canvasSize + overhang)
.opacity(state.isDraggingBot || state.mochiOnDesktop ? 0 : opacity)
.position(x: cx, y: cy - overhang / 2)
@@ -398,6 +399,21 @@ func botPosition(mode: IslandMode, view: IslandView, islandW: CGFloat, islandH:
}
}
+// MARK: - Active island view
+
+private struct IslandViewActiveKey: EnvironmentKey {
+ static let defaultValue = true
+}
+
+extension EnvironmentValues {
+ /// False inside the island views that are mounted but not shown (opacity 0):
+ /// TimelineViews in them pause. True everywhere else.
+ var islandViewActive: Bool {
+ get { self[IslandViewActiveKey.self] }
+ set { self[IslandViewActiveKey.self] = newValue }
+ }
+}
+
// MARK: - Countdown bar
struct CountdownBar: View {
@@ -414,19 +430,33 @@ struct CountdownBar: View {
.cornerRadius(2)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
}
- .onAppear { startTimer() }
- .onDisappear { timer?.invalidate() }
+ // The bar only exists in the open island: its 10 Hz timer runs only then
+ // (CLAUDE.md: 0 % CPU when the island is hidden).
+ .onAppear { if state.mode == .expanded { startTimer() } }
+ .onChange(of: state.mode) { _, mode in
+ if mode == .expanded { startTimer() } else { stopTimer() }
+ }
+ .onDisappear { stopTimer() }
}
private func startTimer() {
- timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
- updateBar()
+ guard timer == nil else { return }
+ let t = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
+ MainActor.assumeIsolated { updateBar() }
}
+ t.tolerance = 0.02
+ timer = t
+ }
+
+ private func stopTimer() {
+ timer?.invalidate()
+ timer = nil
+ if barWidth != 0 { barWidth = 0 }
}
private func updateBar() {
guard state.mode == .expanded && !state.isPinned else {
- barWidth = 0
+ if barWidth != 0 { barWidth = 0 }
return
}
let autoClose = state.autoCloseInterval
@@ -464,6 +494,9 @@ struct IslandContentView: View {
? .spring(response: 0.4, dampingFraction: 0.8).delay(0.16)
: .easeIn(duration: 0.16)
IslandViewContent(view: v, state: state)
+ // Views behind the active one stay mounted (for the cross-fade)
+ // but stop their endless animations: nobody sees them.
+ .environment(\.islandViewActive, active)
.frame(maxWidth: .infinity)
.frame(height: isTall ? nil : 98)
.frame(minHeight: (isTall && !active) ? 0 : nil, maxHeight: isTall ? .infinity : nil)
diff --git a/NotchBuddy/Sources/App/IslandViewContent.swift b/NotchBuddy/Sources/App/IslandViewContent.swift
index 35de743a..1235b498 100644
--- a/NotchBuddy/Sources/App/IslandViewContent.swift
+++ b/NotchBuddy/Sources/App/IslandViewContent.swift
@@ -895,7 +895,8 @@ struct UploadingView: View {
var body: some View {
// TimelineView fires at display refresh rate — progress derived from elapsed wall time,
// not from @Published uploadProgress (which only flips to 1.0 at completion).
- TimelineView(.animation) { tl in
+ // Only runs while this view is shown (it stays mounted behind the others).
+ TimelineView(.animation(paused: state.view != .uploading || state.mode != .expanded)) { tl in
let elapsed: Double = {
guard let start = state.uploadStartTime else { return 0 }
return tl.date.timeIntervalSince(start)
@@ -1190,10 +1191,22 @@ struct MailView: View {
send m
end tell
"""
- var err: NSDictionary?
- NSAppleScript(source: script)?.executeAndReturnError(&err)
- if err == nil { onSuccess(recipient: to) }
- else { statusMsg = "Mail error: \(err?["NSAppleScriptErrorMessage"] as? String ?? "unknown")" }
+ // Off the main thread: the script waits for Mail (and has a 1 s delay), which used
+ // to freeze the whole island until the mail was sent.
+ isSending = true
+ statusMsg = ""
+ let recipient = to
+ Task {
+ let message: String? = await Task.detached(priority: .userInitiated) {
+ var err: NSDictionary?
+ NSAppleScript(source: script)?.executeAndReturnError(&err)
+ return err.map { "Mail error: \($0["NSAppleScriptErrorMessage"] as? String ?? "unknown")" }
+ }.value
+ await MainActor.run {
+ isSending = false
+ if let message { statusMsg = message } else { onSuccess(recipient: recipient) }
+ }
+ }
#endif
}
@@ -3778,7 +3791,7 @@ struct TickerRowView: View {
// Filename + counts
HStack(spacing: 0) {
ZStack(alignment: .leading) {
- TickerShimmerText(text: dp.filename)
+ TickerShimmerText(text: dp.filename, paused: !isActive || phase >= 1)
.opacity(shimmerOpacity)
Text(dp.filename)
.font(.system(size: 13, weight: .medium))
@@ -3821,7 +3834,8 @@ struct TickerRowView: View {
// Text: shimmer fades out, dim completed text fades in (overlapping cross-fade)
ZStack(alignment: .leading) {
- TickerShimmerText(text: text)
+ // A completed row (phase 1) or an idle task never shows the shimmer.
+ TickerShimmerText(text: text, paused: !isActive || phase >= 1)
.opacity(shimmerOpacity)
Text(text)
.font(.system(size: 13, weight: .medium))
@@ -3838,9 +3852,12 @@ struct TickerRowView: View {
struct TickerShimmerText: View {
let text: String
+ var paused: Bool = false
+ @Environment(\.islandViewActive) private var viewActive
var body: some View {
- TimelineView(.animation) { tl in
+ // A 2.2 s sweep: 60 fps is as smooth as the display rate, and it stops when hidden.
+ TimelineView(.animation(minimumInterval: 1.0 / 60.0, paused: paused || !viewActive)) { tl in
let t = tl.date.timeIntervalSinceReferenceDate
let p = CGFloat(t.truncatingRemainder(dividingBy: 2.2) / 2.2)
// phase sweeps -0.1 → 1.1 so white peak enters from left and exits right
diff --git a/NotchBuddy/Sources/App/IslandWindowController.swift b/NotchBuddy/Sources/App/IslandWindowController.swift
index 8fd83837..96730fe0 100644
--- a/NotchBuddy/Sources/App/IslandWindowController.swift
+++ b/NotchBuddy/Sources/App/IslandWindowController.swift
@@ -168,6 +168,7 @@ final class IslandWindowController: NSWindowController {
panel.contentView = container
startPolling()
+ observeScreenForPolling()
startKeyMonitor()
startLocalKeyMonitor()
startHotKeys()
@@ -380,22 +381,32 @@ final class IslandWindowController: NSWindowController {
private static let idlePoll: TimeInterval = 1.0 / 8.0
private var pollInterval: TimeInterval = 0
+ /// Screen asleep or locked: nothing to hover, the poll stops entirely.
+ private var screenOff = false
+
private func startPolling(interval: TimeInterval = IslandWindowController.fastPoll) {
frameTimer?.invalidate()
+ frameTimer = nil
pollInterval = interval
+ guard !screenOff else { return }
let timer = Timer(timeInterval: interval, repeats: true) { [weak self] _ in
// Scheduled on the main run loop: already on the main actor, no Task per tick.
MainActor.assumeIsolated { self?.pollFrame() }
}
- timer.tolerance = interval == Self.idlePoll ? 0.04 : 0
+ // A few ms of slack lets macOS group our wakeups with others; hover is unaffected.
+ timer.tolerance = interval == Self.idlePoll ? 0.04 : 0.004
RunLoop.main.add(timer, forMode: .common)
frameTimer = timer
}
/// Picks the polling rate for the next ticks (see startPolling).
- private func adjustPollRate(mouse: NSPoint, panelFrame: NSRect) {
- let nearIsland = panelFrame.insetBy(dx: -120, dy: -120).contains(mouse)
- let busy = state.mode != .hidden || state.mochiOnDesktop || inAttachDrag || attachDragStart != nil
+ /// "Near" is measured from the island itself, not from the big transparent panel
+ /// (720×560): with the panel, most of the top of the screen counted as near and a
+ /// hidden island polled at 60 Hz nearly all the time. Desktop Mochi has its own poll.
+ private func adjustPollRate(mouse: NSPoint, panelFrame: NSRect, islandRect: NSRect) {
+ let island = islandRect.offsetBy(dx: panelFrame.minX, dy: panelFrame.minY)
+ let nearIsland = island.insetBy(dx: -200, dy: -160).contains(mouse)
+ let busy = state.mode != .hidden || inAttachDrag || attachDragStart != nil
|| fsm.state != .hidden || nearIsland
let wanted = busy ? Self.fastPoll : Self.idlePoll
if wanted != pollInterval { startPolling(interval: wanted) }
@@ -476,10 +487,42 @@ final class IslandWindowController: NSWindowController {
updateWindowHighlight()
}
- adjustPollRate(mouse: mouse, panelFrame: pf)
+ adjustPollRate(mouse: mouse, panelFrame: pf, islandRect: islandRect)
+ }
+
+ /// Screen asleep or locked: stop polling; back at the idle rate when it wakes.
+ private func observeScreenForPolling() {
+ let ws = NSWorkspace.shared.notificationCenter
+ let dc = DistributedNotificationCenter.default()
+ ws.addObserver(forName: NSWorkspace.screensDidSleepNotification, object: nil, queue: .main) { [weak self] _ in
+ Task { @MainActor in self?.pausePollingForScreenOff() }
+ }
+ ws.addObserver(forName: NSWorkspace.screensDidWakeNotification, object: nil, queue: .main) { [weak self] _ in
+ Task { @MainActor in self?.resumePollingAfterScreenOff() }
+ }
+ dc.addObserver(forName: NSNotification.Name("com.apple.screenIsLocked"), object: nil, queue: .main) { [weak self] _ in
+ Task { @MainActor in self?.pausePollingForScreenOff() }
+ }
+ dc.addObserver(forName: NSNotification.Name("com.apple.screenIsUnlocked"), object: nil, queue: .main) { [weak self] _ in
+ Task { @MainActor in self?.resumePollingAfterScreenOff() }
+ }
+ }
+
+ private func pausePollingForScreenOff() {
+ screenOff = true
+ frameTimer?.invalidate()
+ frameTimer = nil
+ }
+
+ private func resumePollingAfterScreenOff() {
+ guard screenOff else { return }
+ screenOff = false
+ startPolling(interval: Self.idlePoll)
}
private var lastMouse: CGPoint = .zero
+ private var lastHighlightMouse: CGPoint = .zero
+ private var lastHighlightScan: CFTimeInterval = 0
// MARK: - Bot-head hover (love emote — mirrors prototype botHover())
@@ -782,14 +825,14 @@ final class IslandWindowController: NSWindowController {
private func startKeyMonitor() {
keyMonitor = NSEvent.addGlobalMonitorForEvents(matching: .keyDown) { [weak self] event in
+ // Every key typed anywhere lands here: only Escape goes further (no Task per key).
+ guard event.keyCode == 53 else { return }
Task { @MainActor in
guard let self = self else { return }
- if event.keyCode == 53 { // Escape
- // Escape typed in another app (Claude Code's own interrupt, an editor…)
- // never folds a pending approval away: only Escape in the notch does.
- if self.state.mode == .expanded && !self.state.isPinned {
- self.collapse()
- }
+ // Escape typed in another app (Claude Code's own interrupt, an editor…)
+ // never folds a pending approval away: only Escape in the notch does.
+ if self.state.mode == .expanded && !self.state.isPinned {
+ self.collapse()
}
}
}
@@ -1046,6 +1089,13 @@ final class IslandWindowController: NSWindowController {
private func updateWindowHighlight() {
let mouse = NSEvent.mouseLocation
+ // Listing every window is costly: skip while the pointer stays put (a window
+ // moving under a still pointer is caught within a quarter second).
+ let now = CACurrentMediaTime()
+ if hypot(mouse.x - lastHighlightMouse.x, mouse.y - lastHighlightMouse.y) < 2,
+ now - lastHighlightScan < 0.25 { return }
+ lastHighlightMouse = mouse
+ lastHighlightScan = now
guard let (appKitBounds, pid) = windowBoundsAtScreenPoint(mouse) else {
// Fade out + close if no window under cursor
if let old = highlightPanel {
diff --git a/NotchBuddy/Sources/App/N8nPoller.swift b/NotchBuddy/Sources/App/N8nPoller.swift
index 1a8f4c5d..c96643c2 100644
--- a/NotchBuddy/Sources/App/N8nPoller.swift
+++ b/NotchBuddy/Sources/App/N8nPoller.swift
@@ -15,7 +15,7 @@ final class N8nPoller: @unchecked Sendable {
func start() {
guard timer == nil else { return }
let t = DispatchSource.makeTimerSource(queue: .global(qos: .background))
- t.schedule(deadline: .now() + 3, repeating: 15)
+ t.schedule(deadline: .now() + 3, repeating: 15, leeway: .seconds(3))
t.setEventHandler { [weak self] in self?.poll() }
t.resume()
timer = t
@@ -27,8 +27,7 @@ final class N8nPoller: @unchecked Sendable {
guard !DemoEngine.isPollerPaused else { return }
guard let apiKey = KeychainStore.shared.get("n8n-api-key"),
let rawBase = KeychainStore.shared.get("n8n-url") else {
- n8nLog("No API key or URL configured")
- return
+ return // not configured: nothing to do, nothing to log (this runs every 15 s)
}
let base = rawBase.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
let endpoints = [
@@ -46,7 +45,6 @@ final class N8nPoller: @unchecked Sendable {
var req = URLRequest(url: url, timeoutInterval: 10)
req.setValue(apiKey, forHTTPHeaderField: "X-N8N-API-KEY")
req.setValue("application/json", forHTTPHeaderField: "Accept")
- n8nLog("Polling \(url.host ?? "?")\(url.path)")
URLSession.shared.dataTask(with: req) { [weak self] data, response, error in
guard let self else { return }
@@ -60,8 +58,9 @@ final class N8nPoller: @unchecked Sendable {
self.tryList(urls, apiKey: apiKey, base: base, idx: idx + 1)
return
}
- self.n8nLog("HTTP \(code) · \(data.count) bytes")
+ // Only failures and changes are logged: a quiet poll writes nothing to disk.
guard code == 200 else {
+ self.n8nLog("HTTP \(code) · \(data.count) bytes")
self.tryList(urls, apiKey: apiKey, base: base, idx: idx + 1)
return
}
@@ -85,10 +84,7 @@ final class N8nPoller: @unchecked Sendable {
else if let n = first["id"] as? Int { id = "\(n)" }
else { self.n8nLog("No id in execution"); return }
- guard id != self.lastExecutionId else {
- self.n8nLog("Same id=\(id) — no change")
- return
- }
+ guard id != self.lastExecutionId else { return }
// Terminal check: use status field — more reliable than the `finished` bool
// (published workflows often have finished=false on error)
diff --git a/NotchBuddy/Sources/App/NotionPoller.swift b/NotchBuddy/Sources/App/NotionPoller.swift
index b92e0f08..a75c73db 100644
--- a/NotchBuddy/Sources/App/NotionPoller.swift
+++ b/NotchBuddy/Sources/App/NotionPoller.swift
@@ -8,7 +8,7 @@ final class NotionPoller: @unchecked Sendable {
func start() {
guard timer == nil else { return }
let t = DispatchSource.makeTimerSource(queue: .global(qos: .background))
- t.schedule(deadline: .now() + 9, repeating: 300)
+ t.schedule(deadline: .now() + 9, repeating: 300, leeway: .seconds(30))
t.setEventHandler { [weak self] in self?.poll() }
t.resume()
timer = t
@@ -39,7 +39,7 @@ final class NotionPoller: @unchecked Sendable {
if code == 401 { msg = "Invalid API key (401)" }
else if code == 0 { msg = error?.localizedDescription ?? "No connection" }
else { msg = "API error \(code)" }
- DispatchQueue.main.async { AppState.shared.notionError = msg }
+ DispatchQueue.main.async { AppState.shared.setIfChanged(\.notionError, msg) }
return
}
guard let data,
@@ -48,9 +48,9 @@ final class NotionPoller: @unchecked Sendable {
let pages = results.compactMap { self.parsePage($0) }
DispatchQueue.main.async {
- AppState.shared.notionError = nil
+ AppState.shared.setIfChanged(\.notionError, nil)
AppState.shared.notionPages = pages
- AppState.shared.notionLoaded = true
+ AppState.shared.setIfChanged(\.notionLoaded, true)
}
}.resume()
}
diff --git a/NotchBuddy/Sources/App/PhoneLink/CloudProbe.swift b/NotchBuddy/Sources/App/PhoneLink/CloudProbe.swift
index 24a65075..eb174cce 100644
--- a/NotchBuddy/Sources/App/PhoneLink/CloudProbe.swift
+++ b/NotchBuddy/Sources/App/PhoneLink/CloudProbe.swift
@@ -32,7 +32,7 @@ final class CloudProbe {
private var changeToken: CKServerChangeToken?
private var seenPongs = Set<String>()
private var fetching = false
- private var lastPushAt: Date?
+ private(set) var lastPushAt: Date?
private var started = false
private var pingTask: Task<Void, Never>?
private var pollTask: Task<Void, Never>?
@@ -186,6 +186,8 @@ final class CloudProbe {
lastPushAt = Date()
// A request from the iPhone (a service to read, an action) may be waiting.
Task { await ServiceDetailRunner.shared.checkNow() }
+ // An instruction from the iPhone may be waiting too: no need to wait for the poll.
+ Task { await InstructionRunner.shared.checkNow() }
guard pingTask != nil else { return } // the Pong fetch is only for the ping test
log("push received")
Task { await fetchChanges(source: "push") }
diff --git a/NotchBuddy/Sources/App/PhoneLink/InstructionRunner.swift b/NotchBuddy/Sources/App/PhoneLink/InstructionRunner.swift
index b7aa07df..0f8fd7a8 100644
--- a/NotchBuddy/Sources/App/PhoneLink/InstructionRunner.swift
+++ b/NotchBuddy/Sources/App/PhoneLink/InstructionRunner.swift
@@ -41,11 +41,14 @@ final class InstructionRunner {
func start() {
guard pollTask == nil else { return }
- log("on: checking for instructions every 15 s")
+ log("on: checking for instructions (push, else every 15 s)")
pollTask = Task { [weak self] in
while !Task.isCancelled {
await self?.check()
- try? await Task.sleep(for: .seconds(15))
+ // CloudKit pushes reach this Mac (one came in the last half hour): they
+ // trigger the check, the poll is only a safety net. Otherwise every 15 s.
+ let pushesWork = CloudProbe.shared.lastPushAt.map { Date().timeIntervalSince($0) < 1800 } ?? false
+ try? await Task.sleep(for: .seconds(pushesWork ? 90 : 15), tolerance: .seconds(5))
}
}
}
@@ -57,9 +60,30 @@ final class InstructionRunner {
log("off")
}
+ /// A CloudKit push arrived: look now instead of at the next poll.
+ func checkNow() async {
+ guard pollTask != nil else { return }
+ await check()
+ }
+
// MARK: Reading
+ /// One check at a time: a push and the poll must never read the same changes twice
+ /// (an instruction would run twice).
+ private var checking = false
+ private var checkAgain = false
+
private func check() async {
+ guard !checking else { checkAgain = true; return }
+ checking = true
+ repeat {
+ checkAgain = false
+ await readAndRun()
+ } while checkAgain
+ checking = false
+ }
+
+ private func readAndRun() async {
var found: [CKRecord] = []
do {
var more = true
diff --git a/NotchBuddy/Sources/App/ResendPoller.swift b/NotchBuddy/Sources/App/ResendPoller.swift
index 6fbfaced..15989fd8 100644
--- a/NotchBuddy/Sources/App/ResendPoller.swift
+++ b/NotchBuddy/Sources/App/ResendPoller.swift
@@ -8,7 +8,7 @@ final class ResendPoller: @unchecked Sendable {
func start() {
guard timer == nil else { return }
let t = DispatchSource.makeTimerSource(queue: .global(qos: .background))
- t.schedule(deadline: .now() + 6, repeating: 60)
+ t.schedule(deadline: .now() + 6, repeating: 60, leeway: .seconds(10))
t.setEventHandler { [weak self] in self?.poll() }
t.resume()
timer = t
diff --git a/NotchBuddy/Sources/App/StripePoller.swift b/NotchBuddy/Sources/App/StripePoller.swift
index ba86eda8..69e9d79a 100644
--- a/NotchBuddy/Sources/App/StripePoller.swift
+++ b/NotchBuddy/Sources/App/StripePoller.swift
@@ -15,7 +15,7 @@ final class StripePoller: @unchecked Sendable {
func start() {
guard timer == nil else { return }
let t = DispatchSource.makeTimerSource(queue: .global(qos: .background))
- t.schedule(deadline: .now() + 6, repeating: 30)
+ t.schedule(deadline: .now() + 6, repeating: 30, leeway: .seconds(5))
t.setEventHandler { [weak self] in self?.poll() }
t.resume()
timer = t
@@ -49,7 +49,7 @@ final class StripePoller: @unchecked Sendable {
else if code == 403 { errMsg = "Use secret key (sk_live_… not pk_live_…)" }
else if code == 0 { errMsg = error?.localizedDescription ?? "No connection" }
else { errMsg = "API error \(code)" }
- DispatchQueue.main.async { AppState.shared.stripeError = errMsg }
+ DispatchQueue.main.async { AppState.shared.setIfChanged(\.stripeError, errMsg) }
return
}
guard let data,
@@ -64,15 +64,16 @@ final class StripePoller: @unchecked Sendable {
DispatchQueue.main.async {
let state = AppState.shared
- state.stripeError = nil
- state.stripeCurrency = currency
- state.stripeBalance = amount
+ // Every @Published set redraws every island view: only on change.
+ state.setIfChanged(\.stripeError, nil)
+ state.setIfChanged(\.stripeCurrency, currency)
+ state.setIfChanged(\.stripeBalance, amount)
// Always sync display balance if not yet showing a real value
// (charges may have set stripeLoaded=true before balance arrived)
if state.stripeDisplayBalance == 0 {
state.stripeDisplayBalance = amount
}
- state.stripeLoaded = true
+ state.setIfChanged(\.stripeLoaded, true)
}
}.resume()
}
diff --git a/NotchBuddy/Sources/App/VercelPoller.swift b/NotchBuddy/Sources/App/VercelPoller.swift
index 8cfe6088..c5c109fd 100644
--- a/NotchBuddy/Sources/App/VercelPoller.swift
+++ b/NotchBuddy/Sources/App/VercelPoller.swift
@@ -14,7 +14,7 @@ final class VercelPoller: @unchecked Sendable {
func start() {
guard timer == nil else { return }
let t = DispatchSource.makeTimerSource(queue: .global(qos: .background))
- t.schedule(deadline: .now() + 5, repeating: 30)
+ t.schedule(deadline: .now() + 5, repeating: 30, leeway: .seconds(5))
t.setEventHandler { [weak self] in self?.poll() }
t.resume()
timer = t
diff --git a/NotchBuddy/Sources/App/Voice/EnergyVAD.swift b/NotchBuddy/Sources/App/Voice/EnergyVAD.swift
index 83acc1b1..a5fe7f59 100644
--- a/NotchBuddy/Sources/App/Voice/EnergyVAD.swift
+++ b/NotchBuddy/Sources/App/Voice/EnergyVAD.swift
@@ -16,13 +16,36 @@ struct EnergyVAD {
enum Event { case none, start, end }
- // MARK: - Tunables
+ // MARK: - Tunables (frame counts at the reference 43 frames/s)
+ static let referenceRate = 43.0
static let calibFrames = 22 // ≈ 0.5 s at 43 Hz before triggering
static let riseRatio = 8.0 // power/noise to start a segment
static let fallRatio = 2.0 // power/noise to count silence
static let silenceFrames = 35 // ≈ 800 ms at 43 Hz
static let maxActiveFrames = 1300 // ≈ 30 s safety limit
+ // Frame counts and smoothing for the real buffer rate (setFrameRate): macOS often
+ // delivers 100 ms buffers instead of 1024 frames, and counting frames as if they were
+ // 23 ms made the silence tail ~3.5 s, keeping speech recognition running on noise.
+ private var calibLimit = EnergyVAD.calibFrames
+ private var silenceLimit = EnergyVAD.silenceFrames
+ private var activeLimit = EnergyVAD.maxActiveFrames
+ private var kCalib = 0.15, kIdle = 0.005, kSilent = 0.001
+ private(set) var frameRate = EnergyVAD.referenceRate
+
+ /// Buffers per second actually delivered. Keeps the same durations (0.5 s calibration,
+ /// 0.8 s silence, 30 s max) and the same smoothing per second.
+ mutating func setFrameRate(_ fps: Double) {
+ guard fps > 1 else { return }
+ frameRate = fps
+ let r = fps / Self.referenceRate
+ calibLimit = max(1, Int((Double(Self.calibFrames) * r).rounded()))
+ silenceLimit = max(1, Int((Double(Self.silenceFrames) * r).rounded()))
+ activeLimit = max(1, Int((Double(Self.maxActiveFrames) * r).rounded()))
+ func k(_ perRefFrame: Double) -> Double { 1 - pow(1 - perRefFrame, 1 / r) }
+ kCalib = k(0.15); kIdle = k(0.005); kSilent = k(0.001)
+ }
+
// MARK: - Readable state
private(set) var isActive: Bool = false
private(set) var noisePower: Double = 1e-7
@@ -52,14 +75,14 @@ struct EnergyVAD {
/// Feed one frame's mean-square power. Returns whether a segment started/ended.
mutating func feed(_ power: Double) -> Event {
// Phase 1 — fast calibration: converge noisePower to ambient in ~0.5 s.
- if calibCount < Self.calibFrames {
+ if calibCount < calibLimit {
calibCount += 1
- noisePower = noisePower * 0.85 + power * 0.15
+ noisePower = noisePower * (1 - kCalib) + power * kCalib
return .none
}
if !isActive {
- noisePower = noisePower * 0.995 + power * 0.005
+ noisePower = noisePower * (1 - kIdle) + power * kIdle
if power > noisePower * Self.riseRatio {
isActive = true
silentCount = 0
@@ -73,7 +96,7 @@ struct EnergyVAD {
activeCount += 1
segmentPowerSum += power
segmentPowerCount += 1
- if activeCount >= Self.maxActiveFrames {
+ if activeCount >= activeLimit {
// Update noise floor to the average power of this forced-end segment
// (continuous music/noise → treat it as new ambient level).
if segmentPowerCount > 0 {
@@ -88,8 +111,8 @@ struct EnergyVAD {
}
if power < noisePower * Self.fallRatio {
silentCount += 1
- noisePower = noisePower * 0.999 + power * 0.001
- if silentCount >= Self.silenceFrames {
+ noisePower = noisePower * (1 - kSilent) + power * kSilent
+ if silentCount >= silenceLimit {
isActive = false
silentCount = 0
activeCount = 0
diff --git a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
index 37494fe9..909e244e 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
@@ -1,5 +1,6 @@
#if !APPSTORE
import AVFoundation
+import Accelerate
// MARK: - VoiceAudio
//
@@ -39,7 +40,10 @@ final class VoiceAudio: @unchecked Sendable {
// Read on main (via `drainPreroll()`), written on audio tap thread.
private let prerollLock = NSLock()
private var prerollBuffers: [AVAudioPCMBuffer] = []
- private static let prerollCapacity = 22 // ~500 ms at 43 Hz
+ /// ~500 ms of buffers, whatever their length (set from the first buffer).
+ private var prerollCapacity = 22
+ /// Frame length the VAD and preroll are tuned for (audio tap thread).
+ private var tunedFrameLength: AVAudioFrameCount = 0
/// Last time a buffer was processed in the tap (audio tap thread).
/// Read on main for stall detection — nonisolated for cross-thread access.
@@ -133,6 +137,15 @@ final class VoiceAudio: @unchecked Sendable {
private func processTap(_ buf: AVAudioPCMBuffer, time: AVAudioTime) {
lastBufferTime = Date()
+ // Tune durations to the real buffer length (often 100 ms on macOS, not 1024 frames).
+ if buf.frameLength > 0, buf.frameLength != tunedFrameLength {
+ tunedFrameLength = buf.frameLength
+ let fps = buf.format.sampleRate / Double(buf.frameLength)
+ vad.setFrameRate(fps)
+ prerollLock.withLock { prerollCapacity = max(2, Int((0.5 * fps).rounded(.up))) }
+ appendAppLog("nb.log", "[Voice] tap buffers: \(buf.frameLength) frames (\(Int(fps.rounded()))/s)")
+ }
+
if pendingVADReset {
pendingVADReset = false
vad.reset()
@@ -144,7 +157,7 @@ final class VoiceAudio: @unchecked Sendable {
let deliverBuf = convertToMono16k(buf) ?? buf
prerollLock.withLock {
- if prerollBuffers.count >= Self.prerollCapacity { prerollBuffers.removeFirst() }
+ while prerollBuffers.count >= prerollCapacity { prerollBuffers.removeFirst() }
prerollBuffers.append(deliverBuf)
}
@@ -201,14 +214,22 @@ final class VoiceAudio: @unchecked Sendable {
private extension AVAudioPCMBuffer {
var meanSquarePower: Double {
guard let data = floatChannelData, frameLength > 0 else { return 0 }
- let frames = Int(frameLength)
+ let frames = vDSP_Length(frameLength)
let chans = Int(format.channelCount)
+ guard chans > 0 else { return 0 }
+ // Vectorised mean of squares: one run over interleaved samples, else per channel.
+ if format.isInterleaved {
+ var ms: Float = 0
+ vDSP_measqv(data[0], 1, &ms, frames * vDSP_Length(chans))
+ return Double(ms)
+ }
var sum: Double = 0
for ch in 0..<chans {
- let p = data[ch]
- for i in 0..<frames { let s = Double(p[i]); sum += s * s }
+ var ms: Float = 0
+ vDSP_measqv(data[ch], 1, &ms, frames)
+ sum += Double(ms)
}
- return sum / Double(frames * max(1, chans))
+ return sum / Double(chans)
}
}
#endif
diff --git a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
index 058e3a81..9df24a32 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
@@ -31,7 +31,9 @@ final class VoiceEngine: ObservableObject {
@Published private(set) var commandTranscript: String = ""
@Published private(set) var recognizerUnavailable: Bool = false
@Published private(set) var audioError: String? = nil
- @Published private(set) var micLevel: Double = 0
+ /// Read by Mochi's canvas every frame; not published, so ~20 updates/s while I talk
+ /// don't re-render Settings or the island views.
+ private(set) var micLevel: Double = 0
// MARK: - Pause reasons
@@ -354,7 +356,8 @@ final class VoiceEngine: ObservableObject {
stopAudioStallTask()
stallTask = Task { @MainActor [weak self] in
while !Task.isCancelled {
- try? await Task.sleep(nanoseconds: 500_000_000)
+ // Tolerance lets macOS batch this wakeup with others (stall seen in 2–3 s).
+ try? await Task.sleep(for: .milliseconds(500), tolerance: .milliseconds(400))
guard let self, let audio = self.audio else { return }
// Grace period: don't fire within stallTimeout of pipeline start.
let sinceStart = Date().timeIntervalSince(self.pipelineStartTime)
@@ -465,6 +468,9 @@ final class VoiceEngine: ObservableObject {
// A short noise (cough, click) legitimately gives no text: only count windows
// where speech went on for a while.
guard elapsed >= 3 else { return }
+ // Music without words gives no text either: not a stuck recognizer. Rebuilding
+ // for it cost a restart and half a second of deafness each time.
+ if AppState.shared.musicPlaying || SpotifyController.shared.isPlaying { return }
deafWindows += 1
if deafWindows >= 2 {
deafWindows = 0
@@ -603,6 +609,8 @@ final class VoiceEngine: ObservableObject {
private func commandUpdate(_ command: String) {
// Semi-duplex: ignore updates while Coucou is speaking.
guard !VoiceSpeaker.shared.isBusy else { return }
+ // The recognizer often repeats the same partial: no update, no re-render.
+ guard command != commandTranscript else { return }
commandTranscript = command
let wc = command.split(separator: " ").count
if wc > lastWordCount {
diff --git a/NotchBuddy/Sources/App/Voice/VoiceSpeaker.swift b/NotchBuddy/Sources/App/Voice/VoiceSpeaker.swift
index 759da222..f8f6776d 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceSpeaker.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceSpeaker.swift
@@ -133,6 +133,30 @@ final class VoiceSpeaker: NSObject, AVSpeechSynthesizerDelegate, AVAudioPlayerDe
/// Premium and Siri voices often report no gender: their name tells it instead.
static func bestVoice(for locale: Locale?, gender: String) -> AVSpeechSynthesisVoice? {
guard let loc = locale else { return nil }
+ // Listing the system voices (150+) for every sentence cost milliseconds between
+ // sentences: one lookup per language and voice type, redone when voices change.
+ let key = "\(loc.identifier)|\(gender)"
+ if let hit = voiceCache[key] { return hit }
+ observeVoiceChanges()
+ let v = findBestVoice(for: loc, gender: gender)
+ voiceCache[key] = .some(v)
+ return v
+ }
+
+ private static var voiceCache: [String: AVSpeechSynthesisVoice?] = [:]
+ private static var observingVoices = false
+
+ private static func observeVoiceChanges() {
+ guard !observingVoices else { return }
+ observingVoices = true
+ NotificationCenter.default.addObserver(
+ forName: AVSpeechSynthesizer.availableVoicesDidChangeNotification, object: nil, queue: .main
+ ) { _ in
+ MainActor.assumeIsolated { VoiceSpeaker.voiceCache.removeAll() }
+ }
+ }
+
+ private static func findBestVoice(for loc: Locale, gender: String) -> AVSpeechSynthesisVoice? {
let lang = loc.language.languageCode?.identifier ?? ""
guard !lang.isEmpty else { return nil }
let exact = loc.region.map { "\(lang)-\($0.identifier)" }
diff --git a/NotchBuddy/Sources/App/Voice/WakeSpotter.swift b/NotchBuddy/Sources/App/Voice/WakeSpotter.swift
index d2eb7cc8..33c1fd2d 100644
--- a/NotchBuddy/Sources/App/Voice/WakeSpotter.swift
+++ b/NotchBuddy/Sources/App/Voice/WakeSpotter.swift
@@ -44,6 +44,9 @@ final class WakeSpotter: @unchecked Sendable {
private let lock = NSLock()
private var recognizer: SFSpeechRecognizer?
+ /// One recognizer per locale for the life of this spotter (a pipeline rebuild makes a
+ /// new spotter): creating one per window cost a speech-service round trip at each wake.
+ private var cachedRecognizer: (id: String, recognizer: SFSpeechRecognizer)?
private var request: SFSpeechAudioBufferRecognitionRequest?
private var task: SFSpeechRecognitionTask?
private var active = false
@@ -79,9 +82,15 @@ final class WakeSpotter: @unchecked Sendable {
var reqSnap: SFSpeechAudioBufferRecognitionRequest?
var taskGen = 0
+ let made: SFSpeechRecognizer? = lock.withLock {
+ if let c = cachedRecognizer, c.id == locale.identifier { return c.recognizer }
+ return nil
+ } ?? SFSpeechRecognizer(locale: locale)
+
lock.withLock {
guard !active else { refusal = .alreadyActive; return }
- guard let r = SFSpeechRecognizer(locale: locale),
+ if let made { cachedRecognizer = (locale.identifier, made) }
+ guard let r = made,
r.supportsOnDeviceRecognition,
r.isAvailable else { refusal = .unavailable; return }
diff --git a/NotchBuddy/Sources/CoucouKit/BotEngine.swift b/NotchBuddy/Sources/CoucouKit/BotEngine.swift
index f69a7211..063d8e9a 100644
--- a/NotchBuddy/Sources/CoucouKit/BotEngine.swift
+++ b/NotchBuddy/Sources/CoucouKit/BotEngine.swift
@@ -253,6 +253,8 @@ final class BotEngine: ObservableObject {
// Timing
var lastTime: Double = CACurrentMediaTime()
+ /// Fractional engine steps owed when drawing slower than the display (Mac mini Mochis).
+ var stepDebt: Double = 0
var t0: Double = CACurrentMediaTime() - Double.random(in: 0...5)
var nextBlink: Double = CACurrentMediaTime() + 1.5 + Double.random(in: 0...2)
var waveUntil: Double = 0
diff --git a/tests/EnergyVADTests.swift b/tests/EnergyVADTests.swift
index 6d89a417..b096a86c 100644
--- a/tests/EnergyVADTests.swift
+++ b/tests/EnergyVADTests.swift
@@ -129,6 +129,17 @@ enum EnergyVADTests {
check("music (~60 s): no immediate re-trigger after forced end", prevEvent != .start || segments <= 2)
}
+ // MARK: 9. 100 ms buffers (10/s): same durations — 0.8 s of silence is 8 buffers
+ do {
+ var vad = EnergyVAD()
+ vad.setFrameRate(10)
+ for _ in 0..<5 { _ = vad.feed(1e-5) } // 0.5 s calibration = 5 buffers
+ check("10/s: starts on speech after 0.5 s", vad.feed(1e-3) == .start)
+ var endAt = -1
+ for i in 1...20 where endAt < 0 { if vad.feed(1e-5) == .end { endAt = i } }
+ check("10/s: ends after 0.8 s of silence (8 buffers), got \(endAt)", endAt == 8)
+ }
+
// Summary
if failures == 0 { print("\n\(total)/\(total) tests passed.") }
else { print("\n\(failures) test(s) FAILED."); exit(1) }