// // PassReadiness.swift // Museion // // ## Scenario: pass readiness over the provider descriptor // From #181 // // Given the pass profiles' providers read through the settings seam // When pass readiness refreshes // Then each pass surface maps to ready / missingKey / unknown through the // ONE credential resolution (provider key, else the Portal session key // for keyless providers) — the session model stores the verdicts and // itself reads no keys and no auth files. import Foundation /// #181: per-pass readiness — ready / missingKey / unknown, computed /// over the provider descriptor. Formerly the session model's #137 /// nested enum: the profile reads (surface provider, `.env` key, Portal /// auth store) live HERE now; MentorSessionModel stores the verdicts its /// pane observes. nonisolated enum PassReadiness: Equatable { case ready case missingKey(providerLabel: String) case unknown /// Read each pass surface's provider (readSurface, one task group), /// then resolve each DISTINCT provider's credential through the ONE /// rail (ProviderCredential — provider key, else the Portal session /// key for keyless providers). A surface we can't read maps .unknown /// (our own read failure never blocks — the spawn's honest failure row /// owns that path); a refused credential maps .missingKey naming the /// provider. Nil/empty `base_url` means the provider's default family /// (the family the session key is allowed to ride). static func refresh( writer: any SettingsWriting, portalAuthPath: String, ) async -> [PassKind: PassReadiness] { let surfaces = [ProfileSurface.pass1, .pass2] var reads: [ProfileSurface: (config: SurfaceModelConfig, baseURL: String?)] = [:] await withTaskGroup(of: (ProfileSurface, (config: SurfaceModelConfig, baseURL: String?)?).self) { group in for surface in surfaces { group.addTask { await (surface, try? writer.readSurface(surface)) } } for await (surface, read) in group { if let read { reads[surface] = read } } } var envKeys: [ModelProvider: String] = [:] for provider in Set(reads.values.map(\.config.provider)) { envKeys[provider] = await (try? writer.readProviderKey(provider: provider)) ?? "" } var verdicts: [PassKind: PassReadiness] = [:] for surface in surfaces { let kind: PassKind = surface == .pass1 ? .pass1 : .pass2 guard let read = reads[surface] else { verdicts[kind] = .unknown continue } let provider = read.config.provider let endpoint = read.baseURL.flatMap { $0.isEmpty ? nil : $0 } ?? provider.defaultEndpoint switch await ProviderCredential.resolve( provider: provider, envKey: envKeys[provider] ?? "", endpoint: endpoint, portalAuthPath: portalAuthPath, ) { case .key: verdicts[kind] = .ready case .refused: verdicts[kind] = .missingKey(providerLabel: provider.label) } } return verdicts } }