Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Packages/AstraUI/Sources/Arena/ArenaController.swift10// Module : Arena11// Repository : https://git.spboucher.ai/orix-astra12// Created : 2026-11-2113// Last Modified : 2026-11-2214//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : Proprietary, all rights reserved (see license file)80// License File : LICENSE81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Observation90import AstraCore91import AstraPersistence9293/// One competitor on screen: the stored entry plus what only exists while its answer is arriving.94struct ArenaLane: Identifiable, Equatable {95 var entry: ArenaEntryRecord96 /// Text of the answer while it streams; the stored text takes over when the lane finishes.97 var liveText = ""98 /// What `ParameterNegotiator` dropped or adjusted for this model.99 var notes: [String] = []100 var isActive = false101 var isLoadingModel = false102 var startedAt: Date?103 var id: String { entry.id }104 var displayText: String { isActive ? liveText : entry.text }105}106107/// What a run would send and cost, per competitor, before anything is sent.108struct ArenaPreflight: Equatable {109 struct Row: Identifiable, Equatable {110 var ref: ModelRef111 var info: ModelInfo?112 var notes: [String]113 var inputTokens: Int114 var outputCap: Int115 var estimate: CostEstimate116 var fits: Bool117 var unavailable: AstraError?118 var id: String { ref.id }119 }120 var rows: [Row] = []121 /// Sum of the upper bounds that could be computed.122 var knownUpperBoundUSD: Double { rows.compactMap { $0.estimate.basis == .estimatedFromText ? $0.estimate.amountUSD : nil }.reduce(0, +) }123 var pricedCount: Int { rows.filter { $0.estimate.basis == .estimatedFromText }.count }124 var unknownCount: Int { rows.filter { $0.estimate.basis == .unavailable }.count }125 var onDeviceCount: Int { rows.filter { $0.estimate.basis == .onDevice }.count }126127 @MainActor static func make(prompt: String, system: String, parameters: GenerationParameters, competitors: [ModelRef], library: ModelLibrary) -> ArenaPreflight {128 let trimmedSystem = system.trimmingCharacters(in: .whitespacesAndNewlines)129 let messages = [PromptMessage.user(prompt)]130 let input = TokenEstimator.estimate(trimmedSystem) + messages.reduce(0) { $0 + TokenEstimator.estimate($1) }131 return ArenaPreflight(rows: competitors.map { ref in132 let info = library.info(for: ref)133 var negotiated = parameters134 var notes: [String] = []135 if let info {136 let result = ParameterNegotiator.negotiate(parameters, tools: [], images: false, for: info)137 negotiated = result.parameters; notes = result.notes138 }139 let cap = negotiated.maxOutputTokens ?? info?.maxOutputTokens ?? 1_024140 let window = ArenaController.window(for: ref, info: info)141 let plan = ContextBudget(window: window, reservedForOutput: min(cap, max(256, window / 2)), strategy: .strict)142 .plan(system: trimmedSystem.isEmpty ? nil : trimmedSystem, messages: messages)143 return Row(ref: ref, info: info, notes: notes, inputTokens: input, outputCap: cap,144 estimate: UsageAccounting.preflight(inputTokens: input, maxOutputTokens: cap, pricing: info?.pricing, provider: ref.provider),145 fits: plan.fits, unavailable: library.availability(of: ref))146 })147 }148}149150/// Runs one prompt against several models at once. Every competitor has its own task, its own state and its own failure;151/// nothing here compares or scores the answers.152@MainActor @Observable final class ArenaController {153 private(set) var session: ArenaSessionRecord?154 /// Display order (`revealOrder`): shuffled for a blind session.155 private(set) var lanes: [ArenaLane] = []156 /// False while a blind session still hides which model is which.157 private(set) var revealed = true158 private(set) var isLoading = false159 var failure: AstraError?160161 private let store: AstraStore162 private let library: ModelLibrary163 private let orchestrator: InferenceOrchestrator164 private var tasks: [String: Task<Void, Never>] = [:]165 private var tokens: [String: UUID] = [:]166167 init(store: AstraStore, library: ModelLibrary, orchestrator: InferenceOrchestrator) {168 self.store = store; self.library = library; self.orchestrator = orchestrator169 }170171 var isRunning: Bool { lanes.contains { $0.isActive } }172 var isBlindHidden: Bool { (session?.blind ?? false) && !revealed }173 var winnerID: String? { lanes.first { $0.entry.isWinner }?.id }174175 static func window(for ref: ModelRef, info: ModelInfo?) -> Int { info?.contextLength ?? (ref.provider.isLocal ? 4_096 : 32_000) }176177 static func title(from prompt: String) -> String {178 let line = prompt.split(whereSeparator: \.isNewline).first.map(String.init)?.trimmingCharacters(in: .whitespaces) ?? ""179 return line.count > 60 ? String(line.prefix(57)) + "…" : line180 }181182 /// The name to show for a lane: "Model A" while a blind session is hidden, the model's name otherwise.183 func label(for lane: ArenaLane) -> String { isBlindHidden ? lane.entry.blindLabel : library.displayName(for: lane.entry.model) }184 func info(for lane: ArenaLane) -> ModelInfo? { library.info(for: lane.entry.model) }185186 // MARK: Running187188 /// Creates the session and starts every competitor. Returns false (with `failure` set) when the session could not be saved.189 @discardableResult190 func start(prompt: String, system: String, parameters: GenerationParameters, blind: Bool, templateID: String?, competitors: [ModelRef]) async -> Bool {191 stopAll()192 failure = nil193 do {194 let created = try await store.createArenaSession(title: Self.title(from: prompt), prompt: prompt, system: system, parameters: parameters,195 blind: blind, templateID: templateID, competitors: competitors)196 tokens = [:]; tasks = [:]197 session = created.session198 lanes = created.entries.sorted { ($0.revealOrder, $0.ordinal) < ($1.revealOrder, $1.ordinal) }.map { ArenaLane(entry: $0) }199 revealed = !blind200 for lane in lanes { launch(lane.id) }201 return true202 } catch {203 failure = AstraError.wrap(error)204 return false205 }206 }207208 /// Opens a stored session. Entries that were still running when the app last quit are shown as stopped.209 func load(sessionID: String) async {210 isLoading = true; defer { isLoading = false }211 do {212 guard let stored = try await store.arenaSession(id: sessionID) else { failure = .invalidRequest("This comparison no longer exists."); return }213 var entries = try await store.arenaEntries(sessionID: sessionID)214 for index in entries.indices where entries[index].status == .pending || entries[index].status == .streaming {215 entries[index].status = .stopped; entries[index].stopReason = .cancelled216 try? await store.updateArenaEntry(entries[index])217 }218 session = stored219 lanes = entries.map { ArenaLane(entry: $0) }220 revealed = !stored.blind || entries.contains { $0.isWinner }221 failure = nil222 } catch { failure = AstraError.wrap(error) }223 }224225 func stop(_ id: String) { tasks[id]?.cancel() }226 func stopAll() { for task in tasks.values { task.cancel() } }227228 /// Sends the same prompt to one competitor again. Its previous answer is replaced; the tokens it used stay in the ledger.229 func retry(_ id: String) {230 guard let lane = lanes.first(where: { $0.id == id }), !lane.isActive else { return }231 launch(id)232 }233234 private func launch(_ id: String) {235 guard let session, let index = lanes.firstIndex(where: { $0.id == id }) else { return }236 let model = lanes[index].entry.model237 let info = library.info(for: model)238 let token = UUID()239 tokens[id] = token240241 var entry = lanes[index].entry242 entry.text = ""; entry.status = .streaming; entry.usage = nil; entry.timeToFirstToken = nil; entry.totalTime = nil243 entry.tokensPerSecond = nil; entry.cost = nil; entry.error = nil; entry.stopReason = nil; entry.reportedModel = nil; entry.servedBy = nil244 lanes[index].entry = entry245 lanes[index].liveText = ""; lanes[index].notes = []; lanes[index].isActive = true246 // A blind session never says which competitor is the on-device one, not even through its loading message.247 lanes[index].isLoadingModel = model.provider == .mlx && !isBlindHidden; lanes[index].startedAt = Date()248249 if let unavailable = library.availability(of: model) { fail(id, token: token, unavailable); return }250251 var parameters = session.parameters252 if let info {253 let negotiated = ParameterNegotiator.negotiate(parameters, tools: [], images: false, for: info)254 parameters = negotiated.parameters255 lanes[index].notes = negotiated.notes256 } else {257 lanes[index].notes = ["No metadata is known for this model, so the parameters are sent exactly as chosen."]258 }259260 let system = session.system.trimmingCharacters(in: .whitespacesAndNewlines)261 let messages = [PromptMessage.user(session.prompt)]262 let window = Self.window(for: model, info: info)263 let reserve = min(parameters.maxOutputTokens ?? info?.maxOutputTokens ?? 1_024, max(256, window / 2))264 let plan = ContextBudget(window: window, reservedForOutput: reserve, strategy: .strict).plan(system: system.isEmpty ? nil : system, messages: messages)265 guard plan.fits else {266 fail(id, token: token, .contextTooLong("The prompt is estimated at \(plan.estimatedInputTokens) tokens, more than the \(plan.inputBudget) this model can take once the answer is reserved."))267 return268 }269 library.noteUsed(model)270271 let request = GenerationRequest(model: model, system: system.isEmpty ? nil : system, messages: messages, parameters: parameters)272 let orchestrator = orchestrator, store = store273 // The task keeps the controller alive until the entry is saved, so an answer that finishes after its screen was274 // closed is still stored and its usage still recorded.275 tasks[id] = Task {276 var outcome = GenerationOutcome()277 var lastPublish = Date.distantPast, lastPersist = Date.distantPast278 for await event in orchestrator.stream(request) {279 outcome.apply(event)280 let now = Date()281 switch event {282 case .messageStarted(let start): self.noteStart(id, token: token, start)283 case .textDelta, .reasoningSummaryDelta:284 // Publish at most ~25 times a second per competitor so four parallel streams never thrash the layout.285 if now.timeIntervalSince(lastPublish) > 0.04 {286 lastPublish = now287 self.publish(id, token: token, text: outcome.text, timeToFirstToken: outcome.timing.timeToFirstToken)288 }289 if now.timeIntervalSince(lastPersist) > 0.6 {290 lastPersist = now291 if var partial = self.currentEntry(id, token: token) {292 partial.text = outcome.text293 try? await store.updateArenaEntry(partial)294 }295 }296 default: break297 }298 }299 if outcome.error == nil, outcome.stopReason == .unknown, Task.isCancelled { outcome.apply(.error(.cancelled)) }300 await self.finish(id, token: token, outcome, info: info)301 }302 }303304 private func index(_ id: String, token: UUID) -> Int? {305 guard tokens[id] == token else { return nil }306 return lanes.firstIndex { $0.id == id }307 }308309 private func currentEntry(_ id: String, token: UUID) -> ArenaEntryRecord? { index(id, token: token).map { lanes[$0].entry } }310311 private func noteStart(_ id: String, token: UUID, _ start: MessageStart) {312 guard let i = index(id, token: token) else { return }313 lanes[i].entry.reportedModel = start.reportedModel314 lanes[i].entry.servedBy = start.servedBy315 }316317 private func publish(_ id: String, token: UUID, text: String, timeToFirstToken: TimeInterval?) {318 guard let i = index(id, token: token) else { return }319 lanes[i].liveText = text320 lanes[i].isLoadingModel = false321 if lanes[i].entry.timeToFirstToken == nil { lanes[i].entry.timeToFirstToken = timeToFirstToken }322 }323324 private func fail(_ id: String, token: UUID, _ error: AstraError) {325 guard let i = index(id, token: token) else { return }326 lanes[i].entry.status = .failed; lanes[i].entry.error = error; lanes[i].entry.stopReason = .error327 lanes[i].isActive = false; lanes[i].isLoadingModel = false328 let entry = lanes[i].entry329 let store = store330 Task { try? await store.updateArenaEntry(entry) }331 }332333 private func finish(_ id: String, token: UUID, _ outcome: GenerationOutcome, info: ModelInfo?) async {334 guard let i = index(id, token: token) else { return }335 var entry = lanes[i].entry336 entry.text = outcome.text337 entry.reportedModel = outcome.start.reportedModel ?? entry.reportedModel338 entry.servedBy = outcome.start.servedBy ?? entry.servedBy339 entry.usage = outcome.usage340 entry.timeToFirstToken = outcome.timing.timeToFirstToken341 entry.totalTime = outcome.timing.totalTime342 entry.tokensPerSecond = outcome.timing.tokensPerSecond(outputTokens: outcome.usage?.outputTokens, reasoningTokens: outcome.usage?.reasoningTokens)343 entry.stopReason = outcome.stopReason344 if let error = outcome.error {345 if error == .cancelled { entry.status = .stopped; entry.error = nil } else { entry.status = .failed; entry.error = error }346 } else { entry.status = .complete; entry.error = nil }347 // A cost exists only when the provider or the runtime reported usage; otherwise it stays unknown.348 entry.cost = outcome.usage == nil ? nil : UsageAccounting.cost(usage: outcome.usage, pricing: info?.pricing, provider: entry.model.provider)349 lanes[i].entry = entry350 lanes[i].isActive = false; lanes[i].isLoadingModel = false; lanes[i].liveText = ""351 tasks[id] = nil352 do { try await store.finishArenaEntry(entry) } catch { failure = AstraError.wrap(error) }353 }354355 // MARK: The person's own choices356357 /// Marks the answer the person prefers (nil clears it). Picking one reveals a blind session.358 func pickWinner(_ id: String?) async {359 guard let session else { return }360 do {361 try await store.setArenaWinner(sessionID: session.id, entryID: id)362 for i in lanes.indices { lanes[i].entry.isWinner = lanes[i].id == id }363 if id != nil { revealed = true }364 } catch { failure = AstraError.wrap(error) }365 }366367 func reveal() { revealed = true }368369 func rename(_ title: String) async {370 guard let id = session?.id else { return }371 let clean = title.trimmingCharacters(in: .whitespacesAndNewlines)372 do { try await store.renameArenaSession(id: id, title: clean); session?.title = clean } catch { failure = AstraError.wrap(error) }373 }374375 func export(_ format: ExportFormat) async throws -> String {376 guard let session else { throw AstraError.invalidRequest("There is no comparison to export.") }377 return try await store.exportArenaSession(id: session.id, format: format, anonymized: isBlindHidden)378 }379}380