// // InferenceQueue.swift // Prisme // // Author: Simon-Pierre Boucher // import Foundation /// Priority of an inference request. User gestures preempt page enrichment, /// which preempts background work (CLAUDE.md §8). enum InferencePriority: Int, Comparable, Sendable { case background = 0 case activePage = 1 case userGesture = 2 static func < (lhs: Self, rhs: Self) -> Bool { lhs.rawValue < rhs.rawValue } } /// Caps in-flight model requests and wakes waiters by priority. The Neural /// Engine serializes inference anyway — an unbounded fan-out only hides the /// real queue and wastes energy. actor InferenceQueue { private let maxConcurrent: Int private var inFlight = 0 private var waiters: [(priority: InferencePriority, order: Int, continuation: CheckedContinuation)] = [] private var counter = 0 init(maxConcurrent: Int) { self.maxConcurrent = maxConcurrent } func run( _ priority: InferencePriority, operation: @Sendable () async throws -> T ) async rethrows -> T { await acquire(priority) defer { release() } return try await operation() } private func acquire(_ priority: InferencePriority) async { if inFlight < maxConcurrent { inFlight += 1 return } counter += 1 let order = counter await withCheckedContinuation { continuation in waiters.append((priority, order, continuation)) } } private func release() { if let index = waiters.indices.max(by: { lhs, rhs in (waiters[lhs].priority, -waiters[lhs].order) < (waiters[rhs].priority, -waiters[rhs].order) }) { let next = waiters.remove(at: index) next.continuation.resume() } else { inFlight -= 1 } } }