diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index 47651085..9789ad84 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -8,6 +8,10 @@ private let menubarPeriodDefaultsKey = "CodeBurnMenubarPeriod" struct CachedPayload { let payload: MenubarPayload let fetchedAt: Date + /// This scoped payload still disagreed with its all-provider slice after a + /// resident-bypassing one-shot re-parse. It is the best answer the CLI can + /// give, so it is served rather than discarded, with the UI saying so. + var contradictsAll: Bool = false var isFresh: Bool { Date().timeIntervalSince(fetchedAt) < cacheTTLSeconds } } @@ -209,7 +213,6 @@ final class AppStore { private var antigravityRefreshGen: Int = 0 private var cache: [PayloadCacheKey: CachedPayload] = [:] - private var selectionFallbackPayload: MenubarPayload? private var cacheDate: String = "" private var switchTask: Task? private var payloadRefreshGeneration: UInt64 = 0 @@ -282,10 +285,21 @@ final class AppStore { ) } + /// Capture the keys that must describe one selection before any suspension + /// point. Reading `periodAllKey` before an await and `currentKey` afterward + /// can otherwise pair evidence from two different user selections. + private func selectionRefreshKeys() -> ( + scoped: PayloadCacheKey, + local: PayloadCacheKey, + all: PayloadCacheKey + ) { + (scoped: currentKey, local: localCurrentKey, all: periodAllKey) + } + var payload: MenubarPayload { if effectiveSelectedScope == .combined { let combinedPayload = cache[currentKey]?.payload - if let localPayload = cache[localCurrentKey]?.payload { + if let localPayload = consistentCachedPayload(for: localCurrentKey) { if let combined = combinedPayload?.combined { return MenubarPayload( generated: combinedPayload?.generated ?? localPayload.generated, @@ -302,7 +316,11 @@ final class AppStore { return combinedPayload } } - return cache[currentKey]?.payload ?? selectionFallbackPayload ?? .empty + // No fallback to the PREVIOUS selection's payload. It belonged to a + // different provider, so serving it under this tab printed another + // provider's dollar figure as if it were this one's. Empty here means + // `hasCachedData` is false and the popover renders its loading state. + return consistentCachedPayload(for: currentKey) ?? .empty } /// Today (across all providers) backs day-specific views in the popover. @@ -404,7 +422,8 @@ final class AppStore { } var hasCachedData: Bool { - cache[currentKey] != nil || (effectiveSelectedScope == .combined && cache[localCurrentKey] != nil) + consistentCachedPayload(for: currentKey) != nil + || (effectiveSelectedScope == .combined && consistentCachedPayload(for: localCurrentKey) != nil) } var hasStaleLoading: Bool { @@ -446,7 +465,10 @@ final class AppStore { if effectiveSelectedScope == .combined { requiredKeys.insert(localCurrentKey) } - return requiredKeys.contains { cache[$0]?.isFresh != true } || hasStaleLoading + return requiredKeys.contains { key in + guard let cached = cache[key], cached.isFresh else { return true } + return providerPayloadContradictsAll(cached.payload, for: key) + } || hasStaleLoading } /// True if any cached payload reports at least one provider. Used to keep the @@ -478,6 +500,26 @@ final class AppStore { cache[PayloadCacheKey(scope: scope, period: period, provider: provider, day: day, days: days, claudeConfigSourceId: claudeConfigSourceId)]?.payload } + func providerPayloadContradictsAllForTesting(_ payload: MenubarPayload, + scope: MenubarScope = .local, + period: Period, + provider: ProviderFilter, + day: String? = nil, + days: Set = [], + claudeConfigSourceId: String? = nil) -> Bool { + providerPayloadContradictsAll( + payload, + for: PayloadCacheKey( + scope: scope, + period: period, + provider: provider, + day: day, + days: days, + claudeConfigSourceId: claudeConfigSourceId + ) + ) + } + func setLastErrorForTesting(_ error: String, scope: MenubarScope = .local, period: Period, @@ -492,13 +534,47 @@ final class AppStore { provider: ProviderFilter, day: String? = nil, insertedAt: Date) { - inFlightKeys[PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)] = insertedAt + inFlightKeys[PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)] = + InFlightSlot(startedAt: insertedAt, token: UUID()) + } + + func setCacheDateToTodayForTesting() { + cacheDate = currentCacheDate() } func isInFlightForTesting(scope: MenubarScope = .local, period: Period, provider: ProviderFilter, day: String? = nil) -> Bool { inFlightKeys[PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)] != nil } + func refreshQuietlyForTesting(scope: MenubarScope = .local, + period: Period, + provider: ProviderFilter, + day: String? = nil) async -> Bool { + await refreshQuietly( + key: PayloadCacheKey(scope: scope, period: period, provider: provider, day: day), + includeOptimize: false + ) + } + + func inFlightWaiterCountForTesting(scope: MenubarScope = .local, + period: Period, + provider: ProviderFilter, + day: String? = nil) -> Int { + inFlightWaiters[PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)]?.count ?? 0 + } + + func finishInFlightForTesting(scope: MenubarScope = .local, + period: Period, + provider: ProviderFilter, + day: String? = nil) { + forceFinishInFlight(for: PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)) + } + + func selectionRefreshKeysForTesting() -> (scoped: PayloadCacheKey, all: PayloadCacheKey) { + let snapshot = selectionRefreshKeys() + return (snapshot.scoped, snapshot.all) + } + func suppressRefreshesForTesting() { refreshSuppressedForTesting = true } @@ -581,9 +657,6 @@ final class AppStore { /// Existing content stays mounted while the background fetch runs, so a tab /// click never becomes a loading gate or resets unrelated in-flight work. func switchTo(provider: ProviderFilter) { - if provider != selectedProvider, hasCachedData { - selectionFallbackPayload = payload - } selectedProvider = provider // A Claude config scope only applies to All/Claude views; picking any // other provider tab clears it (the CLI rejects the contradictory combo). @@ -630,33 +703,107 @@ final class AppStore { let key = PayloadCacheKey(scope: scope, period: period, provider: provider, day: day, days: days, claudeConfigSourceId: claudeConfigSourceId) let localKey = PayloadCacheKey(scope: .local, period: period, provider: provider, day: day, days: days, claudeConfigSourceId: claudeConfigSourceId) let allKey = PayloadCacheKey(scope: .local, period: period, provider: .all, day: day, days: days, claudeConfigSourceId: claudeConfigSourceId) - switchTask = Task { + switchTask = Task { [weak self] in + guard let self else { return } + // The all-provider slice is the evidence that lets a scoped false + // zero be rejected, so it must be present BEFORE the scoped result + // is accepted. It does not have to be present before the scoped + // fetch STARTS: both run together and only the acceptance is + // ordered, via the gate passed as acceptAfter. + let allTask: Task? = provider == .all + ? nil + : self.startAllProviderEvidence(key: allKey, force: false) + let gate: (@Sendable () async -> Void)? = allTask.map { task in { @Sendable in _ = await task.value } } if scope == .combined { - async let local = refresh(key: localKey, includeOptimize: false, force: false, showLoading: false) - async let combined = refresh(key: key, includeOptimize: false, force: false, showLoading: false) - if provider == .all { - _ = await (local, combined) - } else { - async let all = refreshQuietly(key: allKey, includeOptimize: false, force: false) - _ = await (local, combined, all) - } - } else if provider == .all { - await refresh(key: key, includeOptimize: false, force: false, showLoading: false) + async let local = self.refresh(key: localKey, includeOptimize: false, force: false, showLoading: false, acceptAfter: gate) + async let combined = self.refresh(key: key, includeOptimize: false, force: false, showLoading: false, acceptAfter: gate) + _ = await (local, combined) } else { - async let main = refresh(key: key, includeOptimize: false, force: false, showLoading: false) - async let all = refreshQuietly(key: allKey, includeOptimize: false, force: false) - _ = await (main, all) + await self.refresh(key: key, includeOptimize: false, force: false, showLoading: false, acceptAfter: gate) } + _ = await allTask?.value } } - private var inFlightKeys: [PayloadCacheKey: Date] = [:] + /// Start the all-provider fetch immediately and hand back its task, so a + /// scoped fetch can run alongside it and merely AWAIT it before accepting. + private func startAllProviderEvidence( + key: PayloadCacheKey, + force: Bool, + qualityOfService: QualityOfService = .userInitiated + ) -> Task { + Task { [weak self] in + guard let self else { return false } + return await self.refreshQuietly( + key: key, + includeOptimize: false, + force: force, + qualityOfService: qualityOfService + ) + } + } + + /// One occupancy of a key's in-flight slot. The token identifies WHICH + /// fetch owns the slot right now: the watchdog and the display-sleep reset + /// can evict an occupant, after which a later fetch legitimately claims the + /// same key. Without the token the evicted fetch's `defer` would then clear + /// the NEW owner's slot and resume its waiters, so a task parked on the + /// live fetch woke to an empty cache and reported failure. + private struct InFlightSlot { + let startedAt: Date + let token: UUID + } + + private var inFlightKeys: [PayloadCacheKey: InFlightSlot] = [:] + private var inFlightWaiters: [PayloadCacheKey: [CheckedContinuation]] = [:] + + private func waitForInFlight(_ key: PayloadCacheKey) async { + guard inFlightKeys[key] != nil else { return } + await withCheckedContinuation { continuation in + guard inFlightKeys[key] != nil else { + continuation.resume() + return + } + inFlightWaiters[key, default: []].append(continuation) + } + } + + private func claimInFlight(_ key: PayloadCacheKey) -> UUID { + let token = UUID() + inFlightKeys[key] = InFlightSlot(startedAt: Date(), token: token) + return token + } + + /// Release the slot only if this fetch still owns it. A fetch whose slot was + /// evicted has nothing left to release and no waiters of its own. + private func finishInFlight(for key: PayloadCacheKey, token: UUID) { + guard inFlightKeys[key]?.token == token else { return } + forceFinishInFlight(for: key) + } + + /// Evict whoever holds the slot and wake everyone parked on it. Only the + /// watchdog and the pipeline resets use this: they are declaring the slot + /// dead, not reporting a fetch that ended. + private func forceFinishInFlight(for key: PayloadCacheKey) { + inFlightKeys[key] = nil + let waiters = inFlightWaiters.removeValue(forKey: key) ?? [] + for waiter in waiters { + waiter.resume() + } + } + + private func finishAllInFlight() { + let keys = Set(inFlightKeys.keys).union(inFlightWaiters.keys) + for key in keys { + forceFinishInFlight(for: key) + } + } func resetLoadingState() { payloadRefreshGeneration &+= 1 loadingCountsByKey.removeAll() loadingStartedAtByKey.removeAll() - inFlightKeys.removeAll() + finishAllInFlight() attemptedKeys.removeAll() } @@ -668,7 +815,6 @@ final class AppStore { lastErrorByKey.removeAll() if clearCache { cache.removeAll() - selectionFallbackPayload = nil } } @@ -680,8 +826,8 @@ final class AppStore { let staleLoading = loadingStartedAtByKey.filter { now.timeIntervalSince($0.value) > loadingWatchdogSeconds } - let staleInFlight = inFlightKeys.filter { (key, insertedAt) in - now.timeIntervalSince(insertedAt) > loadingWatchdogSeconds && + let staleInFlight = inFlightKeys.filter { (key, slot) in + now.timeIntervalSince(slot.startedAt) > loadingWatchdogSeconds && loadingStartedAtByKey[key] == nil } guard !staleLoading.isEmpty || !staleInFlight.isEmpty else { return false } @@ -691,15 +837,15 @@ final class AppStore { Int(now.timeIntervalSince(started)), key.label, key.provider.rawValue) loadingCountsByKey[key] = nil loadingStartedAtByKey[key] = nil - inFlightKeys[key] = nil + forceFinishInFlight(for: key) if cache[key] == nil { lastErrorByKey[key] = "Refresh took longer than expected. CodeBurn will keep retrying in the background." } } - for (key, insertedAt) in staleInFlight { + for (key, slot) in staleInFlight { NSLog("CodeBurn: orphaned in-flight key stuck for %ds on %@/%@ — clearing", - Int(now.timeIntervalSince(insertedAt)), key.label, key.provider.rawValue) - inFlightKeys[key] = nil + Int(now.timeIntervalSince(slot.startedAt)), key.label, key.provider.rawValue) + forceFinishInFlight(for: key) } return true } @@ -732,10 +878,9 @@ final class AppStore { if cacheDate != today { payloadRefreshGeneration &+= 1 cache.removeAll() - selectionFallbackPayload = nil loadingCountsByKey.removeAll() loadingStartedAtByKey.removeAll() - inFlightKeys.removeAll() + finishAllInFlight() attemptedKeys.removeAll() lastErrorByKey.removeAll() cacheDate = today @@ -745,7 +890,6 @@ final class AppStore { func invalidateCache() { cache.removeAll() - selectionFallbackPayload = nil } private func reconcileClaudeConfigSelection(from payload: MenubarPayload, for key: PayloadCacheKey) { @@ -757,6 +901,128 @@ final class AppStore { } } + /// A cached payload the refresh paths may keep instead of refetching. A + /// contradicting one is refetched; one already VERIFIED as contradicting is + /// not, or every poll would re-run the expensive one-shot parse forever. + private func cachedPayloadIsUsable(_ cached: CachedPayload, for key: PayloadCacheKey) -> Bool { + cached.contradictsAll || !providerPayloadContradictsAll(cached.payload, for: key) + } + + private func consistentCachedPayload(for key: PayloadCacheKey) -> MenubarPayload? { + guard let cached = cache[key] else { return nil } + // A payload already verified by a resident-bypassing re-parse is served + // even though it still disagrees: it is the CLI's real answer, and + // `selectedPayloadMayBeIncomplete` is what tells the user so. + if cached.contradictsAll { return cached.payload } + guard !providerPayloadContradictsAll(cached.payload, for: key) else { return nil } + return cached.payload + } + + /// The visible payload was verified against its all-provider slice and + /// still disagreed. The popover shows one subdued line rather than an error. + var selectedPayloadMayBeIncomplete: Bool { + if cache[currentKey]?.contradictsAll == true { return true } + return effectiveSelectedScope == .combined && cache[localCurrentKey]?.contradictsAll == true + } + + /// An all-provider payload and its scoped sibling describe the same period, + /// day selection, scope, and Claude config. If the all-provider slice has a + /// positive contribution for the selected provider, a scoped 0/0 payload is + /// stale or incomplete rather than an honest answer. This is the exact + /// contract that prevents a provider tab advertising spend and then opening + /// onto a fabricated zero. + private func providerPayloadContradictsAll(_ payload: MenubarPayload, for key: PayloadCacheKey) -> Bool { + guard key.scope == .local, + key.provider != .all else { return false } + + let allKey = PayloadCacheKey( + scope: .local, + period: key.period, + provider: .all, + day: key.day, + days: key.days, + claudeConfigSourceId: key.claudeConfigSourceId + ) + guard let all = cache[allKey]?.payload else { return false } + let providerKeys = Set(key.provider.providerKeys + [key.provider.cliArg]) + let detail = all.current.providerDetails.first(where: { + providerKeys.contains($0.id.lowercased()) || providerKeys.contains($0.label.lowercased()) + }) + let allCost = detail?.cost ?? key.provider.providerKeys.reduce(0.0) { sum, providerKey in + sum + (all.current.providers[providerKey] ?? 0) + } + let allCalls = detail?.calls + let allHasUsage = detail?.hasUsage ?? (allCost > 0) + + // Cost and call dimensions are independent. A scoped result that keeps + // its calls but loses the spend (or vice versa) is still stale. + if allCost > 0, payload.current.cost == 0 { return true } + if let allCalls, allCalls > 0, payload.current.calls == 0 { return true } + + // `hasUsage` is authoritative for subscription/token-only providers, + // where both cost and behavioral calls may legitimately be zero. + let scopedDetail = payload.current.providerDetails.first(where: { + providerKeys.contains($0.id.lowercased()) || providerKeys.contains($0.label.lowercased()) + }) + let scopedHasUsage = scopedDetail?.hasUsage ?? ( + payload.current.cost > 0 + || payload.current.calls > 0 + || payload.current.sessions > 0 + || payload.current.inputTokens > 0 + || payload.current.outputTokens > 0 + ) + return allHasUsage && !scopedHasUsage + } + + private func fetchPayload( + for key: PayloadCacheKey, + includeOptimize: Bool, + qualityOfService: QualityOfService, + acceptAfter: (@Sendable () async -> Void)? = nil + ) async throws -> (payload: MenubarPayload, contradictsAll: Bool) { + let fresh = try await DataClient.fetch( + period: key.period, + day: key.day, + days: key.days, + provider: key.provider, + includeOptimize: includeOptimize, + scope: key.scope, + claudeConfigSourceId: key.claudeConfigSourceId, + qualityOfService: qualityOfService + ) + // The all-provider slice is the evidence providerPayloadContradictsAll + // reads. It is awaited HERE, after this fetch has already run, so the + // two parses overlap and only the ACCEPTANCE is ordered. Waiting for it + // before starting cost a full serialized parse on every tab click. + await acceptAfter?() + guard providerPayloadContradictsAll(fresh, for: key) else { return (fresh, false) } + + NSLog("CodeBurn: resident %@ payload contradicted its all-provider slice; verifying with a one-shot parse", + key.provider.rawValue) + let verified = try await DataClient.fetch( + period: key.period, + day: key.day, + days: key.days, + provider: key.provider, + includeOptimize: includeOptimize, + scope: key.scope, + claudeConfigSourceId: key.claudeConfigSourceId, + bypassResident: true, + qualityOfService: qualityOfService + ) + // A second contradiction is not an error to show the user. The one-shot + // parse bypassed the resident child, so this IS what the CLI reports for + // this provider right now; throwing here replaced a real number with an + // error banner and left the tab with nothing at all. Serve it, flagged, + // and let the popover add one subdued line saying it may be incomplete. + let stillContradicts = providerPayloadContradictsAll(verified, for: key) + if stillContradicts { + NSLog("CodeBurn: verified %@ payload still contradicted its all-provider slice; serving it as possibly incomplete", + key.provider.rawValue) + } + return (verified, stillContradicts) + } + @discardableResult func recoverFromStuckLoading() async -> Bool { guard prepareStuckLoadingRecovery() else { return false } @@ -792,29 +1058,39 @@ final class AppStore { showLoading: Bool = false, qualityOfService: QualityOfService = .userInitiated ) async -> Bool { - if effectiveSelectedScope == .combined { + let keys = selectionRefreshKeys() + // Runs concurrently with the scoped fetch below; only the acceptance is + // ordered behind it (see fetchPayload's acceptAfter). + let allTask: Task? = keys.scoped.provider == .all + ? nil + : startAllProviderEvidence(key: keys.all, force: force, qualityOfService: qualityOfService) + let gate: (@Sendable () async -> Void)? = allTask.map { task in { @Sendable in _ = await task.value } } + if keys.scoped.scope == .combined { async let local = refreshQuietly( - key: localCurrentKey, + key: keys.local, includeOptimize: includeOptimize, force: force, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: gate ) async let combined = refresh( - key: currentKey, + key: keys.scoped, includeOptimize: includeOptimize, force: force, showLoading: showLoading, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: gate ) let (localSucceeded, combinedSucceeded) = await (local, combined) return localSucceeded && combinedSucceeded } else { return await refresh( - key: currentKey, + key: keys.scoped, includeOptimize: includeOptimize, force: force, showLoading: showLoading, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: gate ) } } @@ -828,13 +1104,34 @@ final class AppStore { days: selectedDays, claudeConfigSourceId: selectedClaudeConfigSourceId ) + let localKey = PayloadCacheKey( + scope: .local, + period: scopedKey.period, + provider: scopedKey.provider, + day: scopedKey.day, + days: scopedKey.days, + claudeConfigSourceId: scopedKey.claudeConfigSourceId + ) + let allKey = PayloadCacheKey( + scope: .local, + period: scopedKey.period, + provider: .all, + day: scopedKey.day, + days: scopedKey.days, + claudeConfigSourceId: scopedKey.claudeConfigSourceId + ) + let allTask: Task? = scopedKey.provider == .all + ? nil + : startAllProviderEvidence(key: allKey, force: force) + let gate: (@Sendable () async -> Void)? = allTask.map { task in { @Sendable in _ = await task.value } } if scope == .combined { - async let local = refreshQuietly(key: localCurrentKey, includeOptimize: false, force: false) - async let combined = refreshQuietly(key: scopedKey, includeOptimize: false, force: force) + async let local = refreshQuietly(key: localKey, includeOptimize: false, force: false, acceptAfter: gate) + async let combined = refreshQuietly(key: scopedKey, includeOptimize: false, force: force, acceptAfter: gate) _ = await (local, combined) } else { - await refreshQuietly(key: scopedKey, includeOptimize: false, force: force) + await refreshQuietly(key: scopedKey, includeOptimize: false, force: force, acceptAfter: gate) } + _ = await allTask?.value } @discardableResult @@ -843,15 +1140,26 @@ final class AppStore { includeOptimize: Bool, force: Bool = false, showLoading: Bool = false, - qualityOfService: QualityOfService = .userInitiated + qualityOfService: QualityOfService = .userInitiated, + acceptAfter: (@Sendable () async -> Void)? = nil ) async -> Bool { invalidateStaleDayCache() let cacheDateAtStart = cacheDate let generationAtStart = payloadRefreshGeneration if Task.isCancelled { return false } - if !force, cache[key]?.isFresh == true { return true } - if inFlightKeys[key] != nil { return false } - inFlightKeys[key] = Date() + if !force, + let cached = cache[key], + cached.isFresh, + cachedPayloadIsUsable(cached, for: key) { return true } + // Join an in-flight fetch instead of reporting failure. Returning false + // here made recoverFromStuckLoading() announce a failed recovery while a + // perfectly healthy fetch was still running, which is what the quiet + // path already avoided by waiting. + if inFlightKeys[key] != nil { + await waitForInFlight(key) + return consistentCachedPayload(for: key) != nil + } + let inFlightToken = claimInFlight(key) attemptedKeys.insert(key) lastErrorByKey[key] = nil let didShowLoading = showLoading || cache[key] == nil @@ -871,7 +1179,7 @@ final class AppStore { } defer { let abandonedAttempt = Task.isCancelled || generationAtStart != payloadRefreshGeneration - inFlightKeys[key] = nil + finishInFlight(for: key, token: inFlightToken) if didShowLoading { finishLoading(for: key) } @@ -881,15 +1189,11 @@ final class AppStore { } var succeeded = false do { - let fresh = try await DataClient.fetch( - period: key.period, - day: key.day, - days: key.days, - provider: key.provider, + let fresh = try await fetchPayload( + for: key, includeOptimize: includeOptimize, - scope: key.scope, - claudeConfigSourceId: key.claudeConfigSourceId, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: acceptAfter ) if generationAtStart != payloadRefreshGeneration { NSLog("CodeBurn: dropping fetch result for \(key.label)/\(key.provider.rawValue) — refresh pipeline reset mid-fetch") @@ -912,11 +1216,8 @@ final class AppStore { NSLog("CodeBurn: dropping fetch result for \(key.label)/\(key.provider.rawValue) — calendar rolled mid-fetch") return false } - cache[key] = CachedPayload(payload: fresh, fetchedAt: Date()) - if key == currentKey || (effectiveSelectedScope == .combined && key == localCurrentKey) { - selectionFallbackPayload = nil - } - reconcileClaudeConfigSelection(from: fresh, for: key) + cache[key] = CachedPayload(payload: fresh.payload, fetchedAt: Date(), contradictsAll: fresh.contradictsAll) + reconcileClaudeConfigSelection(from: fresh.payload, for: key) lastSuccessByKey[key] = Date() lastErrorByKey[key] = nil succeeded = true @@ -925,14 +1226,9 @@ final class AppStore { NSLog("CodeBurn: fetch failed for \(key.label)/\(key.provider.rawValue): \(error)") if includeOptimize, cache[key] == nil { do { - let fallback = try await DataClient.fetch( - period: key.period, - day: key.day, - days: key.days, - provider: key.provider, + let fallback = try await fetchPayload( + for: key, includeOptimize: false, - scope: key.scope, - claudeConfigSourceId: key.claudeConfigSourceId, qualityOfService: qualityOfService ) guard !Task.isCancelled else { return false } @@ -941,8 +1237,8 @@ final class AppStore { invalidateStaleDayCache() return false } - cache[key] = CachedPayload(payload: fallback, fetchedAt: Date()) - reconcileClaudeConfigSelection(from: fallback, for: key) + cache[key] = CachedPayload(payload: fallback.payload, fetchedAt: Date(), contradictsAll: fallback.contradictsAll) + reconcileClaudeConfigSelection(from: fallback.payload, for: key) lastSuccessByKey[key] = Date() lastErrorByKey[key] = nil return true @@ -1000,12 +1296,19 @@ final class AppStore { key: PayloadCacheKey, includeOptimize: Bool, force: Bool = false, - qualityOfService: QualityOfService = .userInitiated + qualityOfService: QualityOfService = .userInitiated, + acceptAfter: (@Sendable () async -> Void)? = nil ) async -> Bool { invalidateStaleDayCache() - if !force, cache[key]?.isFresh == true { return true } - if inFlightKeys[key] != nil { return false } - inFlightKeys[key] = Date() + if !force, + let cached = cache[key], + cached.isFresh, + cachedPayloadIsUsable(cached, for: key) { return true } + if inFlightKeys[key] != nil { + await waitForInFlight(key) + return consistentCachedPayload(for: key) != nil + } + let inFlightToken = claimInFlight(key) attemptedKeys.insert(key) let cacheDateAtStart = cacheDate let generationAtStart = payloadRefreshGeneration @@ -1013,18 +1316,14 @@ final class AppStore { NSLog("CodeBurn: refreshing stale today status payload after %ds", age) } defer { - inFlightKeys[key] = nil + finishInFlight(for: key, token: inFlightToken) } do { - let fresh = try await DataClient.fetch( - period: key.period, - day: key.day, - days: key.days, - provider: key.provider, + let fresh = try await fetchPayload( + for: key, includeOptimize: includeOptimize, - scope: key.scope, - claudeConfigSourceId: key.claudeConfigSourceId, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: acceptAfter ) if generationAtStart != payloadRefreshGeneration { NSLog("CodeBurn: dropping quiet fetch result for \(key.label) — refresh pipeline reset mid-fetch") @@ -1036,8 +1335,8 @@ final class AppStore { invalidateStaleDayCache() return false } - cache[key] = CachedPayload(payload: fresh, fetchedAt: Date()) - reconcileClaudeConfigSelection(from: fresh, for: key) + cache[key] = CachedPayload(payload: fresh.payload, fetchedAt: Date(), contradictsAll: fresh.contradictsAll) + reconcileClaudeConfigSelection(from: fresh.payload, for: key) lastSuccessByKey[key] = Date() lastErrorByKey[key] = nil } catch { diff --git a/mac/Sources/CodeBurnMenubar/Data/DataClient.swift b/mac/Sources/CodeBurnMenubar/Data/DataClient.swift index 4af4a8a5..3a8dc246 100644 --- a/mac/Sources/CodeBurnMenubar/Data/DataClient.swift +++ b/mac/Sources/CodeBurnMenubar/Data/DataClient.swift @@ -47,6 +47,14 @@ struct CLIDecodeFailure: Error, CustomStringConvertible { /// Runs the CLI via argv (no shell interpretation). See `CodeburnCLI` for why we never route /// commands through `/bin/zsh -c` anymore. struct DataClient { +#if DEBUG + /// Test seam. The in-flight slot and the all-provider acceptance ordering + /// are only observable when two REAL fetches overlap, which needs a fetch + /// whose completion the test controls rather than a spawned CLI. + @MainActor + static var fetchHookForTesting: (@Sendable (ProviderFilter, Bool) async throws -> MenubarPayload)? +#endif + static func fetch(period: Period, day: String? = nil, days: Set = [], @@ -54,7 +62,13 @@ struct DataClient { includeOptimize: Bool, scope: MenubarScope = .local, claudeConfigSourceId: String? = nil, + bypassResident: Bool = false, qualityOfService: QualityOfService = .userInitiated) async throws -> MenubarPayload { +#if DEBUG + if let hook = await MainActor.run(body: { fetchHookForTesting }) { + return try await hook(provider, bypassResident) + } +#endif let subcommand = statusSubcommand( period: period, day: day, @@ -64,7 +78,11 @@ struct DataClient { scope: scope, claudeConfigSourceId: claudeConfigSourceId ) - let result = try await runCLI(subcommand: subcommand, qualityOfService: qualityOfService) + let result = try await runCLI( + subcommand: subcommand, + bypassResident: bypassResident, + qualityOfService: qualityOfService + ) guard result.exitCode == 0 else { throw DataClientError.nonZeroExit(code: result.exitCode, stderr: result.stderr) } @@ -133,10 +151,12 @@ struct DataClient { private static func runCLI( subcommand: [String], + bypassResident: Bool = false, qualityOfService: QualityOfService = .userInitiated ) async throws -> ProcessResult { try await runCLI( subcommand: subcommand, + bypassResident: bypassResident, serveRequest: { args in try await ServeConnection.shared.request(args: args) }, @@ -164,6 +184,7 @@ struct DataClient { /// the shared resident and globally limited one-shot closures above. static func runCLI( subcommand: [String], + bypassResident: Bool = false, serveRequest: ([String]) async throws -> Data, spawnFallback: () async throws -> ProcessResult ) async throws -> ProcessResult { @@ -172,7 +193,7 @@ struct DataClient { // Transport/protocol failures fall back to the spawn path below, so // the resident remains an optimization. Resource-policy failures stay // terminal and cannot bypass the resident output ceiling. - if ServeConnection.isEligible(subcommand) { + if !bypassResident, ServeConnection.isEligible(subcommand) { do { let stdout = try await serveRequest(subcommand) return ProcessResult(stdout: stdout, stderr: "", exitCode: 0) diff --git a/mac/Sources/CodeBurnMenubar/Views/AgentTabStrip.swift b/mac/Sources/CodeBurnMenubar/Views/AgentTabStrip.swift index b19e23ac..a166cedd 100644 --- a/mac/Sources/CodeBurnMenubar/Views/AgentTabStrip.swift +++ b/mac/Sources/CodeBurnMenubar/Views/AgentTabStrip.swift @@ -113,7 +113,11 @@ struct AgentTabStrip: View { } private var periodAll: MenubarPayload { - store.periodAllPayload ?? store.payload + // `store.payload` is the SCOPED selection. Falling back to it while the + // selection has no cached data of its own used to surface the previous + // provider's totals here; the same hasCachedData gate the popover body + // uses keeps that out of the tab strip. + store.periodAllPayload ?? (store.hasCachedData ? store.payload : .empty) } private var visibleFilters: [ProviderFilter] { diff --git a/mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift b/mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift index 1ad37af7..fe34eafd 100644 --- a/mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift +++ b/mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift @@ -20,6 +20,14 @@ struct MenuBarContent: View { ScrollView(.vertical, showsIndicators: false) { VStack(spacing: 0) { HeroSection() + if store.selectedPayloadMayBeIncomplete { + Text("This total may be incomplete.") + .font(.system(size: 11)) + .foregroundStyle(Color.secondary.opacity(0.75)) + .frame(maxWidth: .infinity, alignment: .leading) + .padding(.horizontal, 14) + .padding(.bottom, 6) + } Divider().opacity(0.5) PeriodSegmentedControl() ScopeSegmentedControl() @@ -119,7 +127,7 @@ struct MenuBarContent: View { // visible. Without this, the strip disappears for one frame on a period // switch when the new key's payload is still empty. if store.hasAnyProvidersInCache { return true } - let payload = store.todayPayload ?? store.payload + let payload = store.todayPayload ?? (store.hasCachedData ? store.payload : .empty) return !payload.current.providers.isEmpty } diff --git a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift new file mode 100644 index 00000000..cac80726 --- /dev/null +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift @@ -0,0 +1,270 @@ +import Foundation +import Testing +@testable import CodeBurnMenubar + +/// A fetch whose completion the test controls, so two real refresh tasks can be +/// held in flight at once. Every attempt records the provider it was issued for +/// and parks until the test releases it by index. +@MainActor +private final class ScriptedFetch { + private var parked: [Int: CheckedContinuation] = [:] + private(set) var attempts: [(index: Int, provider: ProviderFilter)] = [] + private(set) var released: [Int] = [] + var payloadForProvider: (ProviderFilter) -> MenubarPayload = { _ in scriptedPayload(cost: 1) } + + func install() { + DataClient.fetchHookForTesting = { [weak self] provider, _ in + guard let self else { return scriptedPayload(cost: 0) } + let index = await self.begin(provider) + await self.park(index) + return await self.payload(for: provider) + } + } + + func uninstall() { + DataClient.fetchHookForTesting = nil + for (_, continuation) in parked { continuation.resume() } + parked.removeAll() + } + + private func begin(_ provider: ProviderFilter) -> Int { + let index = attempts.count + attempts.append((index: index, provider: provider)) + return index + } + + private func park(_ index: Int) async { + await withCheckedContinuation { parked[index] = $0 } + } + + private func payload(for provider: ProviderFilter) -> MenubarPayload { + payloadForProvider(provider) + } + + func isParked(_ index: Int) -> Bool { parked[index] != nil } + + func release(_ index: Int) { + guard let continuation = parked.removeValue(forKey: index) else { return } + released.append(index) + continuation.resume() + } + + func indices(for provider: ProviderFilter) -> [Int] { + attempts.filter { $0.provider == provider }.map(\.index) + } +} + +private func scriptedPayload(cost: Double, + calls: Int = 1, + providers: [String: Double]? = nil, + providerDetails: [ProviderDetail] = []) -> MenubarPayload { + MenubarPayload( + generated: "test", + current: CurrentBlock( + label: "Today", + cost: cost, + calls: calls, + sessions: 1, + oneShotRate: nil, + inputTokens: 1, + outputTokens: 1, + cacheHitPercent: 0, + codexCredits: nil, + topActivities: [], + topModels: [], + localModelSavings: LocalModelSavings(totalUSD: 0, calls: 0, byModel: [], byProvider: []), + providers: providers ?? ["claude": cost], + providerDetails: providerDetails, + topProjects: [], + modelEfficiency: [], + topSessions: [], + retryTax: RetryTax(totalUSD: 0, retries: 0, editTurns: 0, byModel: []), + routingWaste: RoutingWaste(totalSavingsUSD: 0, baselineModel: "", baselineCostPerEdit: 0, byModel: []), + tools: [], + skills: [], + subagents: [], + mcpServers: [] + ), + optimize: OptimizeBlock(findingCount: 0, savingsUSD: 0, topFindings: []), + history: HistoryBlock(daily: []), + combined: nil, + claudeConfigs: nil + ) +} + +/// Yield repeatedly until `condition` holds. The tasks under test suspend on +/// real continuations, so progress needs actual scheduler turns, not a sleep. +@MainActor +private func settle(until condition: () -> Bool, turns: Int = 500) async -> Bool { + for _ in 0..= 2 })) + for index in 0.. ClaudeConfigSele } private func menubarPayload(cost: Double, + calls: Int = 1, + sessions: Int = 1, + inputTokens: Int = 1, + outputTokens: Int = 1, + providers: [String: Double]? = nil, + providerDetails: [ProviderDetail] = [], combined: CombinedUsage? = nil, claudeConfigs: ClaudeConfigSelector? = nil) -> MenubarPayload { MenubarPayload( @@ -53,17 +59,18 @@ private func menubarPayload(cost: Double, current: CurrentBlock( label: "Today", cost: cost, - calls: 1, - sessions: 1, + calls: calls, + sessions: sessions, oneShotRate: nil, - inputTokens: 1, - outputTokens: 1, + inputTokens: inputTokens, + outputTokens: outputTokens, cacheHitPercent: 0, codexCredits: nil, topActivities: [], topModels: [], localModelSavings: LocalModelSavings(totalUSD: 0, calls: 0, byModel: [], byProvider: []), - providers: ["claude": cost], + providers: providers ?? ["claude": cost], + providerDetails: providerDetails, topProjects: [], modelEfficiency: [], topSessions: [], @@ -84,8 +91,308 @@ private func menubarPayload(cost: Double, @Suite("AppStore refresh recovery") @MainActor struct AppStoreRefreshRecoveryTests { - @Test("provider switches retain the last rendered payload until the target loads") - func providerSwitchRetainsLastRenderedPayload() { + @Test("an active all-provider slice rejects a contradictory scoped zero") + func activeAllProviderSliceRejectsScopedZero() { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 11.5, "hermes agent": 0.5], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 11.5, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0.5, calls: 7, hasUsage: true), + ] + ) + let falseZero = menubarPayload( + cost: 0, + calls: 0, + sessions: 0, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false), + ] + ) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + store.suppressRefreshesForTesting() + store.switchTo(provider: .hermes) + store.setCachedPayloadForTesting(falseZero, period: .today, provider: .hermes, fetchedAt: Date()) + + #expect(store.providerPayloadContradictsAllForTesting(falseZero, period: .today, provider: .hermes)) + // The Hermes tab must show NO dollar figure. It used to fall back to the + // previous selection's payload and print $12, which is Claude's spend + // plus Hermes', under a tab labelled Hermes. + #expect(!store.hasCachedData) + #expect(store.payload.current.cost == 0) + #expect(store.payload.current.calls == 0) + #expect(store.needsInteractivePayloadRefresh) + } + + @Test("positive provider spend cannot disappear when scoped calls survive") + func positiveSpendCannotDisappearWhenScopedCallsSurvive() { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 11.5, "hermes agent": 0.5], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 11.5, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0.5, calls: 7, hasUsage: true), + ] + ) + let missingSpend = menubarPayload( + cost: 0, + calls: 7, + sessions: 7, + inputTokens: 100, + outputTokens: 20, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 7, hasUsage: true), + ] + ) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + + #expect(store.providerPayloadContradictsAllForTesting(missingSpend, period: .today, provider: .hermes)) + } + + @Test("flat-rate activity rejects a contradictory scoped zero") + func flatRateActivityRejectsScopedZero() { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 12, "hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 12, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 7, hasUsage: true), + ] + ) + let falseZero = menubarPayload( + cost: 0, + calls: 0, + sessions: 0, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false), + ] + ) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + + #expect(store.providerPayloadContradictsAllForTesting(falseZero, period: .today, provider: .hermes)) + } + + @Test("token-only activity accepts a scoped payload with matching usage") + func tokenOnlyActivityAcceptsMatchingScopedUsage() { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 12, "hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 12, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: true), + ] + ) + let tokenOnly = menubarPayload( + cost: 0, + calls: 0, + sessions: 1, + inputTokens: 100, + outputTokens: 20, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: true), + ] + ) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + + #expect(!store.providerPayloadContradictsAllForTesting(tokenOnly, period: .today, provider: .hermes)) + } + + @Test("scoped providerDetails hasUsage remains authoritative with empty headline totals") + func scopedProviderDetailsUsageRemainsAuthoritative() { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 12, "hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 12, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: true), + ] + ) + let providerDetailOnly = menubarPayload( + cost: 0, + calls: 0, + sessions: 0, + inputTokens: 0, + outputTokens: 0, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: true), + ] + ) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + + #expect(!store.providerPayloadContradictsAllForTesting(providerDetailOnly, period: .today, provider: .hermes)) + } + + @Test("token-only activity rejects a completely empty scoped payload") + func tokenOnlyActivityRejectsEmptyScopedPayload() { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 12, "hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 12, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: true), + ] + ) + let empty = menubarPayload( + cost: 0, + calls: 0, + sessions: 0, + inputTokens: 0, + outputTokens: 0, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false), + ] + ) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + + #expect(store.providerPayloadContradictsAllForTesting(empty, period: .today, provider: .hermes)) + } + + @Test("late all-provider evidence invalidates an already cached scoped zero") + func lateAllProviderEvidenceInvalidatesScopedZero() { + let store = AppStore() + let falseZero = menubarPayload( + cost: 0, + calls: 0, + sessions: 0, + inputTokens: 0, + outputTokens: 0, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false), + ] + ) + let all = menubarPayload( + cost: 12, + providers: ["claude": 11.5, "hermes agent": 0.5], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 11.5, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0.5, calls: 7, hasUsage: true), + ] + ) + + store.suppressRefreshesForTesting() + store.switchTo(provider: .hermes) + store.setCachedPayloadForTesting(falseZero, period: .today, provider: .hermes, fetchedAt: Date()) + #expect(store.hasCachedData) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + + #expect(!store.hasCachedData) + #expect(store.needsInteractivePayloadRefresh) + } + + @Test("quiet refresh joins matching in-flight evidence instead of racing ahead") + func quietRefreshJoinsMatchingInFlightEvidence() async { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 11.5, "hermes agent": 0.5], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 11.5, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0.5, calls: 7, hasUsage: true), + ] + ) + store.setCacheDateToTodayForTesting() + store.seedInFlightForTesting(period: .today, provider: .all, insertedAt: Date()) + + let joined = Task { @MainActor in + await store.refreshQuietlyForTesting(period: .today, provider: .all) + } + while store.inFlightWaiterCountForTesting(period: .today, provider: .all) == 0 { + await Task.yield() + } + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + store.finishInFlightForTesting(period: .today, provider: .all) + + #expect(await joined.value) + } + + @Test("refresh pipeline reset releases matching in-flight waiters") + func refreshPipelineResetReleasesInFlightWaiters() async { + let store = AppStore() + store.setCacheDateToTodayForTesting() + store.seedInFlightForTesting(period: .today, provider: .all, insertedAt: Date()) + + let joined = Task { @MainActor in + await store.refreshQuietlyForTesting(period: .today, provider: .all) + } + while store.inFlightWaiterCountForTesting(period: .today, provider: .all) == 0 { + await Task.yield() + } + + store.resetLoadingState() + + #expect(!(await joined.value)) + #expect(!store.isInFlightForTesting(period: .today, provider: .all)) + } + + @Test("selection refresh snapshots scoped and all-provider keys together") + func selectionRefreshSnapshotsMatchingKeys() { + let store = AppStore() + store.suppressRefreshesForTesting() + store.switchTo(period: .month) + store.switchTo(provider: .hermes) + + let snapshot = store.selectionRefreshKeysForTesting() + store.switchTo(period: .today) + store.switchTo(provider: .cursor) + + #expect(snapshot.scoped.period == .month) + #expect(snapshot.scoped.provider == .hermes) + #expect(snapshot.all.period == .month) + #expect(snapshot.all.provider == .all) + #expect(snapshot.scoped.day == snapshot.all.day) + #expect(snapshot.scoped.days == snapshot.all.days) + #expect(snapshot.scoped.claudeConfigSourceId == snapshot.all.claudeConfigSourceId) + } + + @Test("a genuine provider zero remains valid when the all-provider slice is also zero") + func genuineProviderZeroRemainsValid() { + let store = AppStore() + let all = menubarPayload( + cost: 12, + providers: ["claude": 12, "hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "claude", label: "Claude", cost: 12, calls: 4, hasUsage: true), + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false), + ] + ) + let zero = menubarPayload( + cost: 0, + calls: 0, + sessions: 0, + providers: ["hermes agent": 0], + providerDetails: [ + ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false), + ] + ) + + store.setCachedPayloadForTesting(all, period: .today, provider: .all, fetchedAt: Date()) + + #expect(!store.providerPayloadContradictsAllForTesting(zero, period: .today, provider: .hermes)) + } + + @Test("a provider switch shows no figure until the target provider loads") + func providerSwitchShowsNoStaleFigure() { let store = AppStore() store.suppressRefreshesForTesting() store.setCachedPayloadForTesting( @@ -97,7 +404,16 @@ struct AppStoreRefreshRecoveryTests { store.switchTo(provider: .cursor) + // Keeping the all-provider payload mounted under the Cursor tab is not + // continuity, it is a wrong number: $12.34 is every provider's spend. + // Empty here is what puts the popover on its loading state instead. #expect(store.selectedProvider == .cursor) + #expect(!store.hasCachedData) + #expect(store.payload.current.cost == 0) + + // The all-provider selection still reads its own cached payload. + store.switchTo(provider: .all) + #expect(store.hasCachedData) #expect(store.payload.current.cost == 12.34) } diff --git a/mac/Tests/CodeBurnMenubarTests/ServeConnectionTests.swift b/mac/Tests/CodeBurnMenubarTests/ServeConnectionTests.swift index 5114eb27..92136e70 100644 --- a/mac/Tests/CodeBurnMenubarTests/ServeConnectionTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/ServeConnectionTests.swift @@ -1057,6 +1057,29 @@ struct ServeConnectionTests { #expect(await fallback.snapshot() == 1) } + @Test("a verified-fresh request can bypass the resident worker") + func verifiedFreshRequestBypassesResident() async throws { + let resident = FallbackRecorder() + let fallback = FallbackRecorder() + + let result = try await DataClient.runCLI( + subcommand: ["status", "--format", "menubar-json", "--provider", "hermes"], + bypassResident: true, + serveRequest: { _ in + await resident.record() + return Data("stale".utf8) + }, + spawnFallback: { + await fallback.record() + return DataClient.ProcessResult(stdout: Data("fresh".utf8), stderr: "", exitCode: 0) + } + ) + + #expect(String(decoding: result.stdout, as: UTF8.self) == "fresh") + #expect(await resident.snapshot() == 0) + #expect(await fallback.snapshot() == 1) + } + @Test("the first real request is the only cold-start query") func firstRequestIsTheWarmup() async throws { let dir = NSTemporaryDirectory() + "serve-connection-test-" + UUID().uuidString diff --git a/tests/menubar-provider-alias-contract.test.ts b/tests/menubar-provider-alias-contract.test.ts new file mode 100644 index 00000000..741a507d --- /dev/null +++ b/tests/menubar-provider-alias-contract.test.ts @@ -0,0 +1,62 @@ +import { readFileSync } from 'fs' +import { join } from 'path' + +import { describe, expect, it } from 'vitest' + +import { allProviderNames } from '../src/providers/index.js' + +// The menubar's ProviderFilter.cliArg is what gets passed to `--provider`, and +// it is also the id the payload-consistency guard matches against +// providerDetails. A case whose cliArg is not a real registry name silently +// disables that guard for its tab: the lookup finds no detail row, so no +// contradiction is ever detected. Nothing in either language can see both +// sides, so the contract is checked here, where the registry is importable and +// the Swift source is readable. +const APP_STORE = join(process.cwd(), 'mac/Sources/CodeBurnMenubar/AppStore.swift') + +function providerFilterSource(): string { + const source = readFileSync(APP_STORE, 'utf-8') + const start = source.indexOf('enum ProviderFilter') + expect(start).toBeGreaterThan(-1) + return source.slice(start) +} + +/** Every `case foo = "Bar"` declared on the enum. */ +function enumCases(source: string): string[] { + const body = source.slice(0, source.indexOf('var id: String')) + return [...body.matchAll(/^\s*case (\w+) = "/gm)].map(m => m[1]!) +} + +/** The `case .foo: "bar"` table inside `var cliArg`. */ +function cliArgTable(source: string): Record { + const start = source.indexOf('var cliArg: String {') + expect(start).toBeGreaterThan(-1) + const body = source.slice(start, source.indexOf('\n }', start)) + return Object.fromEntries( + [...body.matchAll(/case \.(\w+): "([^"]+)"/g)].map(m => [m[1]!, m[2]!]), + ) +} + +describe('menubar ProviderFilter to CLI provider registry', () => { + const source = providerFilterSource() + const cases = enumCases(source) + const cliArgs = cliArgTable(source) + const registry = new Set(allProviderNames()) + + it('declares a cliArg for every enum case', () => { + expect(cases.length).toBeGreaterThan(20) + expect(cases.filter(name => !(name in cliArgs))).toEqual([]) + }) + + it.each(Object.entries(cliArgs).filter(([name]) => name !== 'all'))( + 'maps .%s to a provider the CLI registry knows', + (_name, cliArg) => { + expect(registry.has(cliArg)).toBe(true) + }, + ) + + it('keeps .all outside the registry, since it is the no-filter sentinel', () => { + expect(cliArgs['all']).toBe('all') + expect(registry.has('all')).toBe(false) + }) +})