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.

QuestionQueue: questions from several sessions wait side by side

NikitaBulichev20f72efea314
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 1eb4bca1..a0e7716d 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -33,6 +33,9 @@ jobs:
- name: Test approval queue
run: bash scripts/test-approval-queue.sh
+ - name: Test question queue
+ run: bash scripts/test-question-queue.sh
+
- name: Test Flow dictation keys
run: bash scripts/test-flow-keys.sh
diff --git a/NotchBuddy/Sources/App/QuestionQueue.swift b/NotchBuddy/Sources/App/QuestionQueue.swift
new file mode 100644
index 00000000..3d09fbfc
--- /dev/null
+++ b/NotchBuddy/Sources/App/QuestionQueue.swift
@@ -0,0 +1,99 @@
+import Foundation
+
+// MARK: - QuestionQueue
+// Pure FIFO queue of pending AskUserQuestions, one per session. No AppKit, no SwiftUI, no I/O:
+// HookServer owns the sockets, timers and UI, and mutates this value on the main actor only.
+// The head is the question shown on the card; every question keeps its own connection, so an
+// answer always goes back to the session that asked.
+
+struct QueuedQuestion: Equatable, Sendable {
+ let id: UUID
+ /// Client socket, held open until the user answers. Closed by the owner's dispatch source.
+ let fd: Int32
+ /// The session's id, or "<pill>+<cwd>" for a session that sends none.
+ let sessionId: String
+ /// "integration_claude", "agent_cursor" or "agent_codex".
+ let pillId: String
+ let projectName: String
+ let cwd: String
+ let question: AskQuestion
+ /// Which iPhone record carries the question: 1 is the pill's own, 2, 3… one of its own
+ /// (see `phoneRecordId`). Given by the queue, kept for the question's whole life.
+ var slot: Int = 1
+ /// When the question arrived (answers sent before it are not for it).
+ var received = Date()
+
+ /// The iPhone record that shows this question: the pill's own for the first question
+ /// behind a pill, "<pill>#<slot>" for the others, so each session's question is answered
+ /// on its own. The iPhone sends this id back with its answer.
+ var phoneRecordId: String { slot == 1 ? pillId : "\(pillId)#\(slot)" }
+}
+
+struct QuestionQueue: Sendable {
+ /// Upper bound of simultaneously pending questions. Each one holds a socket and a 120 s
+ /// timer; beyond this the newest goes back to its terminal.
+ static let maxPending = 5
+
+ enum EnqueueResult: Equatable, Sendable {
+ /// Queued. `replaced` is the same session's earlier question, now taken out of the
+ /// queue: the caller hands it back to its terminal.
+ case accepted(replaced: QueuedQuestion?)
+ case rejectedFull
+ }
+
+ private(set) var entries: [QueuedQuestion] = []
+
+ var head: QueuedQuestion? { entries.first }
+ var count: Int { entries.count }
+ var isEmpty: Bool { entries.isEmpty }
+
+ func contains(id: UUID) -> Bool { entries.contains { $0.id == id } }
+
+ func entry(id: UUID) -> QueuedQuestion? { entries.first { $0.id == id } }
+
+ func hasEntries(forPill pillId: String) -> Bool {
+ entries.contains { $0.pillId == pillId }
+ }
+
+ /// Queues `entry` behind the others and gives it the lowest record slot free on its pill.
+ /// A session waits on one question at a time: a newer one from the same session takes
+ /// the older one's place and slot, and the older is returned so its connection can be answered.
+ mutating func enqueue(_ entry: QueuedQuestion, maxPending: Int = QuestionQueue.maxPending) -> EnqueueResult {
+ var entry = entry
+ if let idx = entries.firstIndex(where: { $0.sessionId == entry.sessionId && $0.pillId == entry.pillId }) {
+ let replaced = entries[idx]
+ entry.slot = replaced.slot
+ entries[idx] = entry
+ return .accepted(replaced: replaced)
+ }
+ guard entries.count < maxPending else { return .rejectedFull }
+ let taken = Set(entries.filter { $0.pillId == entry.pillId }.map(\.slot))
+ entry.slot = (1...).first { !taken.contains($0) }!
+ entries.append(entry)
+ return .accepted(replaced: nil)
+ }
+
+ /// Removes only the entry with this id. Other entries keep their order and slots.
+ @discardableResult
+ mutating func remove(id: UUID) -> QueuedQuestion? {
+ guard let idx = entries.firstIndex(where: { $0.id == id }) else { return nil }
+ return entries.remove(at: idx)
+ }
+
+ /// Empties the queue, oldest first.
+ mutating func removeAll() -> [QueuedQuestion] {
+ defer { entries = [] }
+ return entries
+ }
+
+ /// The waiting question an iPhone answer is for: the same question (`fingerprint`, as
+ /// `fingerprintOf` computes it) on the record the answer names. An answer that names no
+ /// record goes to the oldest such question. nil when none waits any more.
+ static func question(answering fingerprint: String, onRecord recordId: String,
+ in entries: [QueuedQuestion],
+ fingerprintOf: (AskQuestion) -> String) -> QueuedQuestion? {
+ let same = entries.filter { fingerprintOf($0.question) == fingerprint }
+ if recordId.isEmpty { return same.first }
+ return same.first { $0.phoneRecordId == recordId }
+ }
+}
diff --git a/scripts/test-question-queue.sh b/scripts/test-question-queue.sh
new file mode 100755
index 00000000..d3fe0da6
--- /dev/null
+++ b/scripts/test-question-queue.sh
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+set -euo pipefail
+cd "$(dirname "$0")/.."
+TEST_DIR="$(mktemp -d "${TMPDIR:-/tmp}/coucou-question-queue.XXXXXX")"
+trap 'rm -rf "$TEST_DIR"' EXIT
+swiftc NotchBuddy/Sources/App/QuestionQueue.swift NotchBuddy/Sources/App/AskQuestion.swift \
+ tests/QuestionQueueTests.swift -o "$TEST_DIR/question-queue-tests"
+"$TEST_DIR/question-queue-tests"
diff --git a/tests/QuestionQueueTests.swift b/tests/QuestionQueueTests.swift
new file mode 100644
index 00000000..bed00fe0
--- /dev/null
+++ b/tests/QuestionQueueTests.swift
@@ -0,0 +1,103 @@
+import Foundation
+
+@main
+enum QuestionQueueTests {
+ static func question(_ text: String) -> AskQuestion {
+ AskQuestion(questions: [AskQuestionItem(question: text, header: "", options: [
+ AskQuestionOption(label: "Yes", description: ""), AskQuestionOption(label: "No", description: ""),
+ ], multiSelect: false)])
+ }
+
+ static func entry(_ n: Int, session: String, pill: String = "integration_claude") -> QueuedQuestion {
+ QueuedQuestion(id: UUID(), fd: Int32(n), sessionId: session, pillId: pill,
+ projectName: "p\(n)", cwd: "/", question: question("q\(n)"))
+ }
+
+ static func accepted(_ result: QuestionQueue.EnqueueResult) -> QueuedQuestion?? {
+ if case .accepted(let replaced) = result { return .some(replaced) }
+ return nil
+ }
+
+ static func main() {
+ // Two sessions on one pill both wait: neither pushes the other away, FIFO
+ var q = QuestionQueue()
+ let a = entry(1, session: "s1"), b = entry(2, session: "s2")
+ precondition(accepted(q.enqueue(a)) == .some(nil))
+ precondition(accepted(q.enqueue(b)) == .some(nil))
+ precondition(q.count == 2 && q.head?.id == a.id)
+ precondition(q.hasEntries(forPill: "integration_claude") && !q.hasEntries(forPill: "agent_codex"))
+
+ // Slots: the pill's own iPhone record first, then one record per extra question
+ precondition(q.entries.map(\.slot) == [1, 2])
+ precondition(q.entries.map(\.phoneRecordId) == ["integration_claude", "integration_claude#2"])
+
+ // Another pill counts its own slots
+ let c = entry(3, session: "s3", pill: "agent_codex")
+ _ = q.enqueue(c)
+ precondition(q.entry(id: c.id)?.phoneRecordId == "agent_codex")
+
+ // Answering the head leaves the others where they are: same order, same slot
+ precondition(q.remove(id: a.id)?.id == a.id)
+ precondition(q.head?.id == b.id && q.entry(id: b.id)?.slot == 2)
+ precondition(q.remove(id: a.id) == nil) // already gone: no-op
+
+ // A freed slot is reused by the next question on that pill
+ let d = entry(4, session: "s4")
+ _ = q.enqueue(d)
+ precondition(q.entry(id: d.id)?.slot == 1 && q.entries.last?.id == d.id)
+
+ // A newer question from the same session takes its older one's place and slot
+ let order = q.entries.map(\.sessionId)
+ let b2 = entry(5, session: "s2")
+ guard case .accepted(let replaced)? = Optional(q.enqueue(b2)) else { preconditionFailure() }
+ precondition(replaced?.id == b.id && !q.contains(id: b.id) && q.contains(id: b2.id))
+ precondition(q.entry(id: b2.id)?.slot == 2 && q.count == 3)
+ precondition(q.entries.map(\.sessionId) == order && q.head?.id == b2.id)
+ // Same session id on another pill is another session
+ let codexS1 = entry(6, session: "s4", pill: "agent_codex")
+ precondition(accepted(q.enqueue(codexS1)) == .some(nil) && q.entry(id: codexS1.id)?.slot == 2)
+
+ // Cap: the newest goes back to its terminal, the queue is unchanged
+ var full = QuestionQueue()
+ let all = (1...QuestionQueue.maxPending).map { entry($0, session: "f\($0)") }
+ for e in all { precondition(accepted(full.enqueue(e)) == .some(nil)) }
+ precondition(full.enqueue(entry(99, session: "f99")) == .rejectedFull)
+ precondition(full.entries.map(\.id) == all.map(\.id))
+ precondition(full.entries.map(\.slot) == Array(1...QuestionQueue.maxPending))
+ // …but a session asking again is not a new question: it takes its own place back
+ let again = entry(98, session: "f3")
+ guard case .accepted(let old)? = Optional(full.enqueue(again)) else { preconditionFailure() }
+ precondition(old?.id == all[2].id && full.count == QuestionQueue.maxPending)
+ precondition(full.entry(id: again.id)?.slot == 3)
+
+ // removeAll hands every question back, oldest first
+ let gone = full.removeAll()
+ precondition(gone.count == QuestionQueue.maxPending && gone.first?.id == all[0].id && full.isEmpty)
+
+ // An iPhone answer goes to the question on the record it names, even when two
+ // sessions asked the very same question
+ var same = QuestionQueue()
+ let s1 = QueuedQuestion(id: UUID(), fd: 1, sessionId: "a", pillId: "integration_claude",
+ projectName: "a", cwd: "/a", question: question("Same?"))
+ let s2 = QueuedQuestion(id: UUID(), fd: 2, sessionId: "b", pillId: "integration_claude",
+ projectName: "b", cwd: "/b", question: question("Same?"))
+ let other = QueuedQuestion(id: UUID(), fd: 3, sessionId: "c", pillId: "agent_codex",
+ projectName: "c", cwd: "/c", question: question("Other?"))
+ for e in [s1, s2, other] { _ = same.enqueue(e) }
+ let fp: (AskQuestion) -> String = { $0.questions.map(\.question).joined() }
+ func answered(_ fingerprint: String, _ record: String) -> UUID? {
+ QuestionQueue.question(answering: fingerprint, onRecord: record, in: same.entries, fingerprintOf: fp)?.id
+ }
+ precondition(answered("Same?", "integration_claude") == s1.id)
+ precondition(answered("Same?", "integration_claude#2") == s2.id)
+ precondition(answered("Other?", "agent_codex") == other.id)
+ precondition(answered("Same?", "") == s1.id) // no record named: the oldest
+ precondition(answered("Same?", "agent_codex") == nil) // not the question on that record
+ precondition(answered("Gone?", "integration_claude") == nil)
+ _ = same.remove(id: s1.id)
+ precondition(answered("Same?", "integration_claude") == nil) // answered already
+ precondition(answered("Same?", "integration_claude#2") == s2.id)
+
+ print("QuestionQueue tests: all passed")
+ }
+}