mirror of
https://github.com/Louis-CFM/coucou.git
synced 2026-12-06 23:24:20 +09:00
Pills stay one per client (VS Code, the Claude app, Codex…), but every
event now goes to its session on the SessionBoard and the pill shows the
board:
- name, steps, state and last message are those of the session the pill
shows, the most urgent else the one active last, no longer the oldest
(PillSessionRegistry.displayOwner), and the steps are that session's
only, not a feed shared by every session
- every session finishes on its own: its sound and its "finished",
even while a sibling works ("N session(s) still working" swallowed
them), and for 5 s the pill shows the session that finished; a failure
shows for 30 s
- with several sessions behind it, the pill carries their lines for the
open card (title, state, last step)
- a session that starts waiting for a permission or an answer while it
shares the pill pops out into "<pill>#2"…, named after it, right after
its client's pill; its card, focus and iPhone record are that pill's
(QueuedApproval / QueuedQuestion .shownPill). It folds back after the
answer, a timeout or a denial (3 s at least), or at SessionEnd. A
session alone in its pill waits there, as before
- the Claude app's pill takes the session's title; other agents' pills
keep the agent's name
The approval/question "defer" that held the pill's state is gone: a
waiting session's state comes from the queues. Sessions the registry let
go leave the board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
5.0 KiB
Swift
108 lines
5.0 KiB
Swift
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 pill the question shows on when its session popped out of `pillId` (SessionBoard),
|
|
/// nil when it shows on `pillId` itself.
|
|
var shownPill: String? = nil
|
|
|
|
/// Where the card and the focus find the question.
|
|
var displayPill: String { shownPill ?? pillId }
|
|
|
|
/// The iPhone record that shows this question: the popped-out pill's own when its session
|
|
/// popped out, else the pill's own for the first question behind a pill, "<pill>#q<slot>"
|
|
/// for the others, so each session's question is answered on its own (a popped-out pill is
|
|
/// "<pill>#<n>": the two never meet). The iPhone sends this id back with its answer.
|
|
var phoneRecordId: String { shownPill ?? (slot == 1 ? pillId : "\(pillId)#q\(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 }
|
|
// A popped-out question has its pill's record: the slots count the others only.
|
|
let taken = Set(entries.filter { $0.pillId == entry.pillId && $0.shownPill == nil }.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 }
|
|
}
|
|
}
|