Add luxe Notifier: macOS menu bar app that tells you when luxe needs you

A luxe hook runs luxe-notify, which sends one JSON line over a Unix socket to
a SwiftUI menu bar app. The app shows a banner, keeps a badge count and
history, and brings the terminal forward when you click a banner.

Notifies on approvals (PermissionRequested), question prompts
(BeforeToolUse, matched to the question tool), errors, and turns that run at
least --min-seconds (default 30, timed from BeforeAgentStart to AgentEnd).

The menu warns when macOS has notifications blocked for the app. The app
writes status.txt beside its socket for debugging.

Known limits are in README.md and PLAN.md: a crowded menu bar can hide the
icon, and the first test build after a change can fail with a TestingMacros
error under Command Line Tools, which make test retries once.
This commit is contained in:
2026-10-24 22:41:42 -04:00
commit ced6a44ed0
30 changed files with 1314 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
import Foundation
import Testing
@testable import NotifierCore
/// Collects listener callbacks and lets a test wait for a count, without XCTest expectations.
final class Inbox: @unchecked Sendable {
private let lock = NSLock()
private(set) var events: [NotifyEvent] = []
private(set) var problems: [String] = []
func add(_ event: NotifyEvent) { lock.lock(); events.append(event); lock.unlock() }
func add(problem: String) { lock.lock(); problems.append(problem); lock.unlock() }
private func reached(_ wanted: Int, _ wantedProblems: Int) -> Bool {
lock.lock()
defer { lock.unlock() }
return events.count >= wanted && problems.count >= wantedProblems
}
func waitFor(events wanted: Int = 0, problems wantedProblems: Int = 0) async -> Bool {
for _ in 0..<200 {
if reached(wanted, wantedProblems) { return true }
try? await Task.sleep(nanoseconds: 10_000_000)
}
return false
}
}
struct SocketRoundTripTests {
// Unix socket paths cap near 104 bytes, so keep them short.
private func freshPath() -> String { "/tmp/luxe-notifier-\(UUID().uuidString.prefix(8)).sock" }
private func startListener(_ path: String, _ inbox: Inbox) throws -> NotifyListener {
let listener = NotifyListener(path: path, onEvent: { inbox.add($0) }, onProblem: { inbox.add(problem: $0) })
try listener.start()
return listener
}
@Test func eventSentByClientArrivesAtListener() async throws {
let path = freshPath()
let inbox = Inbox()
let listener = try startListener(path, inbox)
defer { listener.stop() }
let sent = NotifyEvent(kind: .permission, title: "t", message: "m", cwd: "/x", timestamp: Date(timeIntervalSince1970: 1_700_000_000))
try NotifyClient.send(sent, to: path)
#expect(await inbox.waitFor(events: 1))
#expect(inbox.events.first == sent)
}
@Test func malformedLineIsReportedAndListenerKeepsWorking() async throws {
let path = freshPath()
let inbox = Inbox()
let listener = try startListener(path, inbox)
defer { listener.stop() }
try rawSend("garbage\n", to: path)
#expect(await inbox.waitFor(problems: 1))
try NotifyClient.send(NotifyEvent(kind: .generic, title: "t", message: "m"), to: path)
#expect(await inbox.waitFor(events: 1))
}
@Test func sendingWithNoListenerThrowsAConnectError() {
let path = freshPath()
#expect(throws: SocketError.self) {
try NotifyClient.send(NotifyEvent(kind: .generic, title: "t", message: "m"), to: path)
}
}
@Test func overlongSocketPathIsRejectedClearly() {
#expect(throws: SocketError.self) { try NotifySocket.makeAddress("/tmp/" + String(repeating: "a", count: 200)) }
}
private func rawSend(_ text: String, to path: String) throws {
let fd = socket(AF_UNIX, SOCK_STREAM, 0)
defer { close(fd) }
var address = try NotifySocket.makeAddress(path)
_ = withUnsafePointer(to: &address) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) { connect(fd, $0, socklen_t(MemoryLayout<sockaddr_un>.size)) }
}
_ = text.withCString { write(fd, $0, strlen($0)) }
}
}