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
Questions from several sessions queue up instead of pushing each other away
NikitaBulichev0c7234e00e41
diff --git a/NotchBuddy/Sources/App/AppState.swift b/NotchBuddy/Sources/App/AppState.swiftindex 28803fed..e61b6395 100644--- a/NotchBuddy/Sources/App/AppState.swift+++ b/NotchBuddy/Sources/App/AppState.swift@@ -414,6 +414,10 @@ final class AppState: ObservableObject {didSet { QuestionLayout.height = pendingQuestion?.estimatedIslandHeight }}+ /// Every AskUserQuestion waiting for an answer, oldest first: the first one is on the+ /// card (`pendingQuestion`). Written by HookServer only; empty for a demo question.+ @Published var waitingQuestions: [QueuedQuestion] = []+// Per-pill flat list of FileDiffs, in order of reception.// Not @Published — steps[] changes already trigger redraws.var sessionDiffs: [String: [FileDiff]] = [:]diff --git a/NotchBuddy/Sources/App/DemoEngine.swift b/NotchBuddy/Sources/App/DemoEngine.swiftindex 169fbf32..a1d4b3d5 100644--- a/NotchBuddy/Sources/App/DemoEngine.swift+++ b/NotchBuddy/Sources/App/DemoEngine.swift@@ -187,7 +187,7 @@ final class DemoEngine: ObservableObject {if s.pendingApproval?.sessionId == "demo_session" {s.pendingApproval = nil}- // Clear demo question (real questions always have pendingQuestionFD >= 0)+ // Clear demo question (real questions always hold an nb-hook connection in the queue)if !HookServer.shared.hasRealPendingQuestion {s.pendingQuestion = nil}diff --git a/NotchBuddy/Sources/App/HookServer.swift b/NotchBuddy/Sources/App/HookServer.swiftindex e52b1dde..10e52ec1 100644--- a/NotchBuddy/Sources/App/HookServer.swift+++ b/NotchBuddy/Sources/App/HookServer.swift@@ -44,15 +44,18 @@ final class HookServer: @unchecked Sendable {/// One read source per queued request: it notices the client hanging up, and its/// cancel handler is the only place the request's fd is closed.private var approvalSources: [UUID: any DispatchSourceRead] = [:]- private var pendingQuestionFD: Int32 = -1 // held open while user answers AskUserQuestion- private var questionFDSource: (any DispatchSourceRead)? = nil // monitors pendingQuestionFD+ /// AskUserQuestions waiting for an answer, oldest first, one per session; the head is on+ /// the card. Each one holds its nb-hook connection open until it is answered or dropped.+ private var questions = QuestionQueue()+ /// One read source per waiting question: it notices the client hanging up, and its+ /// cancel handler is the only place the question's fd is closed.+ private var questionSources: [UUID: any DispatchSourceRead] = [:]+ private var shownQuestion: UUID? = nil // the question the card shows/// True when a real nb-hook connection is holding the approval fd open.@MainActor var hasRealPendingApproval: Bool { !approvals.isEmpty }- /// True when a real nb-hook connection is holding the question fd open.- @MainActor var hasRealPendingQuestion: Bool { pendingQuestionFD >= 0 }- private var questionPillId: String = "" // pill that owns the pending question- private var questionSessionId: String = "" // sessionId for the pending question (recap tracking)+ /// True when a real nb-hook connection is holding a question fd open.+ @MainActor var hasRealPendingQuestion: Bool { !questions.isEmpty }private var focusBeforeQuestion: String? = nil // saved focus to restore after questionprivate var activeSessionId: String? = nil // current Claude Code sessionprivate var focusBeforeApproval: String? = nil // saved focus to restore after approval@@ -166,110 +169,142 @@ final class HookServer: @unchecked Sendable {}}- // MARK: - Question fd helpers+ // MARK: - Question queue helpers- /// True while a question holds its nb-hook connection open on `pill`. The pill keeps its- /// question state meanwhile: the iPhone and the Live Activity read the question from it.+ /// True while a question from a session behind `pill` waits for its answer. The pill keeps+ /// its question state meanwhile: the iPhone and the Live Activity read the question from it.@MainActorprivate func questionWaits(onPill pill: String) -> Bool {- pendingQuestionFD >= 0 && questionPillId == pill+ questions.hasEntries(forPill: pill)}+ /// Puts the oldest waiting question on the card. When the card already shows it, only+ /// the list the card and the iPhone read is refreshed (no sound, no focus change).@MainActor- private func cancelQuestionFDSource() {- questionFDSource?.cancel()- questionFDSource = nil+ private func showQuestionHead() {+ let state = AppState.shared+ state.waitingQuestions = questions.entries+ guard let head = questions.head, shownQuestion != head.id else { return }+ shownQuestion = head.id+ state.pendingQuestion = head.question+ state.isPinned = true+ if focusBeforeQuestion == nil { focusBeforeQuestion = state.focusId }+ withAnimation(.spring(response: 0.5, dampingFraction: 0.72)) { state.focusId = head.pillId }+ expandIfNeeded(to: .question)+ }++ /// Takes one question out of the queue. With `answer`, the line is written to its nb-hook+ /// first; without, the connection just closes and the agent asks again itself. The cancel+ /// handler of its source closes the fd — never close it directly. The next question then+ /// takes the card; when none is left the card closes.+ @MainActor+ private func removeQuestion(id: UUID, answer json: String? = nil) {+ guard let entry = questions.remove(id: id) else { return }+ let source = questionSources.removeValue(forKey: id)+ if let json {+ let fd = entry.fd+ Task.detached { [weak self] in+ // Write while the fd is still valid, then cancel → the cancel handler closes it.+ self?.sendLine(fd: fd, text: json)+ DispatchQueue.main.async { source?.cancel() }+ }+ } else {+ source?.cancel()+ }++ let state = AppState.shared+ if !questions.hasEntries(forPill: entry.pillId) {+ let approval = approvals.hasEntries(forPill: entry.pillId)+ state.updateTask(id: entry.pillId, state: approval ? .approval : .working)+ if !approval { clearPillBadge(id: entry.pillId) }+ }+ if questions.head != nil {+ showQuestionHead()+ return+ }+ state.waitingQuestions = []+ closeQuestionCard(pill: entry.pillId)}+ /// No question left: close the card and give the focus back.@MainActor- private func dismissQuestionCard(note: String) {- cancelQuestionFDSource()- pendingQuestionFD = -1+ private func closeQuestionCard(pill: String?) {let state = AppState.shared- let pillId = questionPillId+ shownQuestion = nilstate.pendingQuestion = nilstate.isPinned = false- state.updateTask(id: pillId, state: .working)- clearPillBadge(id: pillId)if let prev = focusBeforeQuestion {focusBeforeQuestion = nil- if state.focusId == pillId, state.tasks.contains(where: { $0.id == prev }) {+ if let pill, state.focusId == pill, state.tasks.contains(where: { $0.id == prev }) {withAnimation(.spring(response: 0.5, dampingFraction: 0.72)) { state.focusId = prev }}}- if !note.isEmpty {- state.noteMessage = note- state.view = .note- DispatchQueue.main.asyncAfter(deadline: .now() + 3) {- NotificationCenter.default.post(name: .islandCollapse, object: nil)- }- } else {- state.view = state.tasks.isEmpty ? .empty : .overview+ state.view = state.tasks.isEmpty ? .empty : .overview+ }++ /// The question `id` (the one the card or the iPhone showed), else the card's.+ /// nil when that question no longer waits.+ @MainActor+ private func waitingQuestion(_ id: UUID?) -> QueuedQuestion? {+ guard let id else { return questions.head }+ guard let entry = questions.entry(id: id) else {+ nbLog("Answer for a question no longer waiting, ignored")+ return nil}+ return entry}- /// Called by QuestionView. Sends answers JSON and cleans up.+ /// Called by QuestionView and the iPhone. Sends the answers to the session that asked+ /// question `id` (the card's when nil), then the next question takes the card.@MainActor- func sendQuestionAnswers(_ answers: [String: Any]) {+ func sendQuestionAnswers(_ answers: [String: Any], for id: UUID? = nil) {// Only intercept a demo question — real questions always have a live fd.- if DemoEngine.shared.isActive, pendingQuestionFD < 0 {+ if DemoEngine.shared.isActive, questions.isEmpty {AppState.shared.pendingQuestion = nilAppState.shared.isPinned = falseAppState.shared.view = AppState.shared.tasks.isEmpty ? .empty : .overviewDemoEngine.shared.handleQuestionAnswered(answers: answers)return}- let fd = pendingQuestionFD- pendingQuestionFD = -1- let source = questionFDSource- questionFDSource = nil- let sid = questionSessionId- questionSessionId = ""- if fd >= 0, let data = try? JSONSerialization.data(withJSONObject: ["permissionDecision": "answer", "answers": answers], options: .withoutEscapingSlashes),- let json = String(data: data, encoding: .utf8) {- Task.detached { [weak self] in- self?.sendLine(fd: fd, text: json)- DispatchQueue.main.async { source?.cancel() }- }- } else {- source?.cancel()+ guard !questions.isEmpty else { closeQuestionCard(pill: nil); return }+ guard let entry = waitingQuestion(id) else { return }+ guard let data = try? JSONSerialization.data(withJSONObject: ["permissionDecision": "answer", "answers": answers], options: .withoutEscapingSlashes),+ let json = String(data: data, encoding: .utf8) else {+ removeQuestion(id: entry.id)+ return}- if !sid.isEmpty { RecapStore.shared.recordQuestionAnswered(sessionId: sid) }- dismissQuestionCard(note: "")+ RecapStore.shared.recordQuestionAnswered(sessionId: entry.sessionId)+ removeQuestion(id: entry.id, answer: json)}- /// Called by QuestionView.onDisappear — card left screen without an explicit answer.- /// Sends "ask" immediately to unblock nb-hook; does NOT navigate (view already changed).+ /// Called by QuestionView.onDisappear — the card left the screen without an answer.+ /// Every waiting question goes back to its terminal ("ask") at once, since none of them+ /// is on screen any more; does NOT navigate (view already changed).@MainActorfunc releaseQuestionFD() {- guard pendingQuestionFD >= 0 else { return }- let fd = pendingQuestionFD- pendingQuestionFD = -1- let source = questionFDSource- questionFDSource = nil- AppState.shared.pendingQuestion = nil- Task.detached { [weak self] in- self?.sendLine(fd: fd, text: #"{"permissionDecision":"ask"}"#)- DispatchQueue.main.async { source?.cancel() }- }- }-- /// Called by QuestionView "Reply in terminal" button.- @MainActor- func sendQuestionAsk() {- let fd = pendingQuestionFD- pendingQuestionFD = -1- let source = questionFDSource- questionFDSource = nil- if fd >= 0 {+ guard !questions.isEmpty else { return }+ let state = AppState.shared+ for entry in questions.removeAll() {+ let source = questionSources.removeValue(forKey: entry.id)+ let fd = entry.fdTask.detached { [weak self] inself?.sendLine(fd: fd, text: #"{"permissionDecision":"ask"}"#)DispatchQueue.main.async { source?.cancel() }}- } else {- source?.cancel()+ if !approvals.hasEntries(forPill: entry.pillId) { state.updateTask(id: entry.pillId, state: .working) }}- dismissQuestionCard(note: "")+ shownQuestion = nil+ state.waitingQuestions = []+ state.pendingQuestion = nil+ }++ /// Called by QuestionView "Reply in terminal" button: question `id` (the card's when nil)+ /// goes back to its terminal.+ @MainActor+ func sendQuestionAsk(for id: UUID? = nil) {+ guard !questions.isEmpty else { closeQuestionCard(pill: nil); return }+ guard let entry = waitingQuestion(id) else { return }+ removeQuestion(id: entry.id, answer: #"{"permissionDecision":"ask"}"#)}/// Returns the tool_input serialized as sorted-keys JSON, "" if absent or empty.@@ -1052,22 +1087,31 @@ final class HookServer: @unchecked Sendable {return}- // Displace any previous question waiting for an answer.- if pendingQuestionFD >= 0 {- let old = pendingQuestionFD- let oldSrc = questionFDSource- questionFDSource = nil+ // Several sessions can wait on a question at once: they queue, the card shows the+ // oldest, and each answer goes back to the session that asked. A session asking again+ // replaces its own older question; past the cap, the newest goes back to its terminal.+ let entry = QueuedQuestion(id: UUID(), fd: fd,+ sessionId: (sessionId == "unknown" || sessionId.isEmpty) ? "\(pillId)+\(cwd)" : sessionId,+ pillId: pillId, projectName: projectName, cwd: cwd, question: parsed)+ switch questions.enqueue(entry) {+ case .rejectedFull:+ nbLog("Question: \(QuestionQueue.maxPending) already waiting, asked in the terminal")Task.detached { [weak self] in- self?.sendLine(fd: old, text: #"{"permissionDecision":"ask"}"#)- DispatchQueue.main.async { oldSrc?.cancel() }+ self?.sendLine(fd: fd, text: #"{"permissionDecision":"ask"}"#)+ close(fd)}+ return+ case .accepted(let replaced?):+ let oldSource = questionSources.removeValue(forKey: replaced.id)+ let oldFD = replaced.fd+ Task.detached { [weak self] in+ self?.sendLine(fd: oldFD, text: #"{"permissionDecision":"ask"}"#)+ DispatchQueue.main.async { oldSource?.cancel() }+ }+ case .accepted(nil):+ break}- pendingQuestionFD = fdactiveSessionId = sessionId- questionPillId = pillId- questionSessionId = (sessionId == "unknown" || sessionId.isEmpty)- ? "\(pillId)+\(cwd)"- : sessionIdupsertTask(pill: pillId,display: PillSessionRegistry.Display(session: sessionId, projectName: projectName,@@ -1075,37 +1119,27 @@ final class HookServer: @unchecked Sendable {bundleId: bundleId),working: true, externalAgent: nil)state.updateTask(id: pillId, state: .question)- state.pendingQuestion = parsed- state.isPinned = trueSoundEngine.shared.play("approval")- if focusBeforeQuestion == nil { focusBeforeQuestion = state.focusId }- withAnimation(.spring(response: 0.5, dampingFraction: 0.72)) { state.focusId = pillId }- expandIfNeeded(to: .question)-+ // Monitor fd: if the agent closes the connection (stopped or answered in its own UI),+ // drop the question. The cancel handler closes the fd — never close it anywhere else.+ let entryId = entry.idlet source = DispatchSource.makeReadSource(fileDescriptor: fd, queue: .main)source.setEventHandler { [weak self] in- guard let self, self.pendingQuestionFD == fd else { return }- self.dismissQuestionCard(note: "")+ guard let self, self.questions.contains(id: entryId) else { return }+ self.removeQuestion(id: entryId)}source.setCancelHandler { close(fd) }source.resume()- questionFDSource = source+ questionSources[entryId] = source- let captured = fdDispatchQueue.main.asyncAfter(deadline: .now() + 120) { [weak self] in- guard let self, self.pendingQuestionFD == captured else { return }+ guard let self, self.questions.contains(id: entryId) else { return }// Send "ask" so nb-hook exits cleanly; Claude Code re-asks in terminal.- let askFD = self.pendingQuestionFD- self.pendingQuestionFD = -1- let src = self.questionFDSource- self.questionFDSource = nil- Task.detached { [weak self] in- self?.sendLine(fd: askFD, text: #"{"permissionDecision":"ask"}"#)- DispatchQueue.main.async { src?.cancel() }- }- self.dismissQuestionCard(note: "")+ self.removeQuestion(id: entryId, answer: #"{"permissionDecision":"ask"}"#)}++ showQuestionHead()}/// "VS Code", "Warp"… — where the Claude Code pill's current session runs.diff --git a/NotchBuddy/Sources/App/IslandViewContent.swift b/NotchBuddy/Sources/App/IslandViewContent.swiftindex 2a45844c..40e44219 100644--- a/NotchBuddy/Sources/App/IslandViewContent.swift+++ b/NotchBuddy/Sources/App/IslandViewContent.swift@@ -432,9 +432,19 @@ struct QuestionView: View {// Per-question "Other…" mode active@State private var showOther: [Bool] = []@FocusState private var otherFieldFocused: Bool+ // The queued question this card's choices belong to: an answer goes to that one only.+ @State private var shownId: UUID?var question: AskQuestion? { state.pendingQuestion }+ /// With questions from several sessions waiting, the pill named after the session that asks.+ private var asker: AgentTask? {+ guard state.waitingQuestions.count > 1, let head = state.waitingQuestions.first,+ var task = state.tasks.first(where: { $0.id == head.pillId }) else { return nil }+ if !head.projectName.isEmpty { task.name = head.projectName }+ return task+ }+var body: some View {ZStack {CardBackground(wash: .cyan)@@ -451,14 +461,21 @@ struct QuestionView: View {VStack(alignment: .leading, spacing: 4) {// Header row: agent name + question counter + "Reply in terminal" linkHStack(spacing: 4) {- AgentWho(task: nil, label: "Claude Code is asking")+ AgentWho(task: asker, label: "Claude Code is asking")+ // Questions from other sessions waiting behind this one: "+2".+ if state.waitingQuestions.count > 1 {+ Text(verbatim: "+\(state.waitingQuestions.count - 1)")+ .font(.system(size: 10))+ .foregroundColor(Color(hex: "#6B7079"))+ .monospacedDigit()+ }Spacer(minLength: 4)if q.questions.count > 1 {Text("\(qi + 1)/\(q.questions.count)").font(.system(size: 10)).foregroundColor(Color(hex: "#6B7079"))}- Button("Reply in terminal") { HookServer.shared.sendQuestionAsk() }+ Button("Reply in terminal") { HookServer.shared.sendQuestionAsk(for: shownId) }.buttonStyle(.plain).font(.system(size: 10)).foregroundColor(Color(hex: "#6B7079"))@@ -591,10 +608,13 @@ struct QuestionView: View {}.onAppear { resetQuestionState() }.onChange(of: state.pendingQuestion) { _, _ in resetQuestionState() }+ // The next session's question can read the same as the one just answered.+ .onChange(of: state.waitingQuestions.first?.id) { _, _ in resetQuestionState() }.onDisappear { HookServer.shared.releaseQuestionFD() }}private func resetQuestionState() {+ shownId = state.waitingQuestions.first?.idquestionIndex = 0let count = state.pendingQuestion?.questions.count ?? 0selections = Array(repeating: [], count: count)@@ -638,7 +658,7 @@ struct QuestionView: View {private func sendAnswers(q: AskQuestion) {let answers = AskQuestion.buildAnswers(questions: q.questions, selections: selections)- HookServer.shared.sendQuestionAnswers(answers)+ HookServer.shared.sendQuestionAnswers(answers, for: shownId)}}