From e9a55bb2f802d923f02266178c7025abc363295b Mon Sep 17 00:00:00 2001 From: Aditya Vikram Singh <247195684+avs-io@users.noreply.github.com> Date: Wed, 2 Sep 2026 15:10:33 +0530 Subject: [PATCH 1/9] prevent provider-scoped false zeros --- mac/Sources/CodeBurnMenubar/AppStore.swift | 148 +++++++++++++++--- .../CodeBurnMenubar/Data/DataClient.swift | 12 +- .../AppStoreRefreshRecoveryTests.swift | 94 ++++++++++- .../ServeConnectionTests.swift | 23 +++ 4 files changed, 246 insertions(+), 31 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index 1076c7d8..d49c3f14 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -41,6 +41,14 @@ struct PayloadCacheKey: Hashable { } } +private struct ProviderPayloadConsistencyError: Error, CustomStringConvertible { + let provider: ProviderFilter + + var description: String { + "Fresh \(provider.rawValue) verification still contradicted the matching all-provider payload." + } +} + @MainActor @Observable final class AppStore { @@ -285,7 +293,7 @@ final class AppStore { 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 +310,7 @@ final class AppStore { return combinedPayload } } - return cache[currentKey]?.payload ?? selectionFallbackPayload ?? .empty + return consistentCachedPayload(for: currentKey) ?? selectionFallbackPayload ?? .empty } /// Today (across all providers) backs day-specific views in the popover. @@ -446,7 +454,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 +489,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, @@ -757,6 +788,80 @@ final class AppStore { } } + private func consistentCachedPayload(for key: PayloadCacheKey) -> MenubarPayload? { + guard let payload = cache[key]?.payload, + !providerPayloadContradictsAll(payload, for: key) else { return nil } + return payload + } + + /// 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, + payload.current.cost == 0, + payload.current.calls == 0 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]) + if let detail = all.current.providerDetails.first(where: { + providerKeys.contains($0.id.lowercased()) || providerKeys.contains($0.label.lowercased()) + }) { + return detail.cost > 0 || detail.calls > 0 + } + return key.provider.providerKeys.reduce(0.0) { sum, providerKey in + sum + (all.current.providers[providerKey] ?? 0) + } > 0 + } + + private func fetchPayload( + for key: PayloadCacheKey, + includeOptimize: Bool, + qualityOfService: QualityOfService + ) async throws -> MenubarPayload { + 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 + ) + guard providerPayloadContradictsAll(fresh, for: key) else { return fresh } + + 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 + ) + guard !providerPayloadContradictsAll(verified, for: key) else { + throw ProviderPayloadConsistencyError(provider: key.provider) + } + return verified + } + @discardableResult func recoverFromStuckLoading() async -> Bool { guard prepareStuckLoadingRecovery() else { return false } @@ -849,7 +954,10 @@ final class AppStore { let cacheDateAtStart = cacheDate let generationAtStart = payloadRefreshGeneration if Task.isCancelled { return false } - if !force, cache[key]?.isFresh == true { return true } + if !force, + let cached = cache[key], + cached.isFresh, + !providerPayloadContradictsAll(cached.payload, for: key) { return true } if inFlightKeys[key] != nil { return false } inFlightKeys[key] = Date() attemptedKeys.insert(key) @@ -881,14 +989,9 @@ 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 ) if generationAtStart != payloadRefreshGeneration { @@ -925,14 +1028,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 } @@ -1003,7 +1101,10 @@ final class AppStore { qualityOfService: QualityOfService = .userInitiated ) async -> Bool { invalidateStaleDayCache() - if !force, cache[key]?.isFresh == true { return true } + if !force, + let cached = cache[key], + cached.isFresh, + !providerPayloadContradictsAll(cached.payload, for: key) { return true } if inFlightKeys[key] != nil { return false } inFlightKeys[key] = Date() attemptedKeys.insert(key) @@ -1016,14 +1117,9 @@ final class AppStore { inFlightKeys[key] = nil } 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 ) if generationAtStart != payloadRefreshGeneration { diff --git a/mac/Sources/CodeBurnMenubar/Data/DataClient.swift b/mac/Sources/CodeBurnMenubar/Data/DataClient.swift index 4af4a8a5..fe86e171 100644 --- a/mac/Sources/CodeBurnMenubar/Data/DataClient.swift +++ b/mac/Sources/CodeBurnMenubar/Data/DataClient.swift @@ -54,6 +54,7 @@ struct DataClient { includeOptimize: Bool, scope: MenubarScope = .local, claudeConfigSourceId: String? = nil, + bypassResident: Bool = false, qualityOfService: QualityOfService = .userInitiated) async throws -> MenubarPayload { let subcommand = statusSubcommand( period: period, @@ -64,7 +65,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 +138,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 +171,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 +180,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/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift index d869e65a..2dd6703b 100644 --- a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift @@ -46,6 +46,10 @@ private func claudeConfigSelector(selectedId: String? = nil) -> ClaudeConfigSele } private func menubarPayload(cost: Double, + calls: Int = 1, + sessions: Int = 1, + providers: [String: Double]? = nil, + providerDetails: [ProviderDetail] = [], combined: CombinedUsage? = nil, claudeConfigs: ClaudeConfigSelector? = nil) -> MenubarPayload { MenubarPayload( @@ -53,8 +57,8 @@ 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, @@ -63,7 +67,8 @@ private func menubarPayload(cost: Double, topActivities: [], topModels: [], localModelSavings: LocalModelSavings(totalUSD: 0, calls: 0, byModel: [], byProvider: []), - providers: ["claude": cost], + providers: providers ?? ["claude": cost], + providerDetails: providerDetails, topProjects: [], modelEfficiency: [], topSessions: [], @@ -84,6 +89,89 @@ private func menubarPayload(cost: Double, @Suite("AppStore refresh recovery") @MainActor struct AppStoreRefreshRecoveryTests { + @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)) + #expect(store.payload.current.cost == 12) + #expect(store.needsInteractivePayloadRefresh) + } + + @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("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("provider switches retain the last rendered payload until the target loads") func providerSwitchRetainsLastRenderedPayload() { let store = AppStore() 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 From 5ef7387a333812717ff5df6d2322e516a946428e Mon Sep 17 00:00:00 2001 From: Aditya Vikram Singh <247195684+avs-io@users.noreply.github.com> Date: Wed, 2 Sep 2026 15:45:03 +0530 Subject: [PATCH 2/9] close provider reconciliation races --- mac/Sources/CodeBurnMenubar/AppStore.swift | 61 +++++++-- .../AppStoreRefreshRecoveryTests.swift | 124 +++++++++++++++++- 2 files changed, 170 insertions(+), 15 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index d49c3f14..b7450c44 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -412,7 +412,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 { @@ -674,9 +675,12 @@ final class AppStore { } else if provider == .all { await refresh(key: key, includeOptimize: false, force: false, showLoading: false) } 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) + // Establish the matching all-provider slice first. Otherwise a + // fast scoped zero can be accepted before the positive all + // result arrives, creating a brief false-zero flash and a + // cached lie until the next refresh. + _ = await refreshQuietly(key: allKey, includeOptimize: false, force: false) + await refresh(key: key, includeOptimize: false, force: false, showLoading: false) } } } @@ -802,9 +806,7 @@ final class AppStore { /// onto a fabricated zero. private func providerPayloadContradictsAll(_ payload: MenubarPayload, for key: PayloadCacheKey) -> Bool { guard key.scope == .local, - key.provider != .all, - payload.current.cost == 0, - payload.current.calls == 0 else { return false } + key.provider != .all else { return false } let allKey = PayloadCacheKey( scope: .local, @@ -816,14 +818,28 @@ final class AppStore { ) guard let all = cache[allKey]?.payload else { return false } let providerKeys = Set(key.provider.providerKeys + [key.provider.cliArg]) - if let detail = all.current.providerDetails.first(where: { + let detail = all.current.providerDetails.first(where: { providerKeys.contains($0.id.lowercased()) || providerKeys.contains($0.label.lowercased()) - }) { - return detail.cost > 0 || detail.calls > 0 - } - return key.provider.providerKeys.reduce(0.0) { sum, providerKey in + }) + let allCost = detail?.cost ?? key.provider.providerKeys.reduce(0.0) { sum, providerKey in sum + (all.current.providers[providerKey] ?? 0) - } > 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 scopedHasUsage = 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( @@ -914,6 +930,14 @@ final class AppStore { let (localSucceeded, combinedSucceeded) = await (local, combined) return localSucceeded && combinedSucceeded } else { + if currentKey.provider != .all { + _ = await refreshQuietly( + key: periodAllKey, + includeOptimize: false, + force: force, + qualityOfService: qualityOfService + ) + } return await refresh( key: currentKey, includeOptimize: includeOptimize, @@ -938,6 +962,17 @@ final class AppStore { async let combined = refreshQuietly(key: scopedKey, includeOptimize: false, force: force) _ = await (local, combined) } else { + if scopedKey.provider != .all { + let allKey = PayloadCacheKey( + scope: .local, + period: scopedKey.period, + provider: .all, + day: scopedKey.day, + days: scopedKey.days, + claudeConfigSourceId: scopedKey.claudeConfigSourceId + ) + _ = await refreshQuietly(key: allKey, includeOptimize: false, force: force) + } await refreshQuietly(key: scopedKey, includeOptimize: false, force: force) } } diff --git a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift index 2dd6703b..5e4bee4f 100644 --- a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift @@ -48,6 +48,8 @@ private func claudeConfigSelector(selectedId: String? = nil) -> 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, @@ -60,8 +62,8 @@ private func menubarPayload(cost: Double, calls: calls, sessions: sessions, oneShotRate: nil, - inputTokens: 1, - outputTokens: 1, + inputTokens: inputTokens, + outputTokens: outputTokens, cacheHitPercent: 0, codexCredits: nil, topActivities: [], @@ -120,6 +122,34 @@ struct AppStoreRefreshRecoveryTests { #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() @@ -146,6 +176,96 @@ struct AppStoreRefreshRecoveryTests { #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("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("a genuine provider zero remains valid when the all-provider slice is also zero") func genuineProviderZeroRemainsValid() { let store = AppStore() From c9c5bc00f852b7e837ae2c1d5c2110c3393a6a58 Mon Sep 17 00:00:00 2001 From: Aditya Vikram Singh <247195684+avs-io@users.noreply.github.com> Date: Wed, 2 Sep 2026 16:15:21 +0530 Subject: [PATCH 3/9] join provider evidence refreshes --- mac/Sources/CodeBurnMenubar/AppStore.swift | 163 ++++++++++++++---- .../AppStoreRefreshRecoveryTests.swift | 94 ++++++++++ 2 files changed, 224 insertions(+), 33 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index b7450c44..81bcbb37 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -290,6 +290,17 @@ 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 @@ -527,10 +538,43 @@ final class AppStore { inFlightKeys[PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)] = insertedAt } + 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) { + finishInFlight(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 } @@ -664,14 +708,12 @@ final class AppStore { let allKey = PayloadCacheKey(scope: .local, period: period, provider: .all, day: day, days: days, claudeConfigSourceId: claudeConfigSourceId) switchTask = Task { if scope == .combined { + if provider != .all { + _ = await refreshQuietly(key: allKey, includeOptimize: false, force: false) + } 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) - } + _ = await (local, combined) } else if provider == .all { await refresh(key: key, includeOptimize: false, force: false, showLoading: false) } else { @@ -686,12 +728,39 @@ final class AppStore { } private var inFlightKeys: [PayloadCacheKey: Date] = [:] + 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 finishInFlight(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 { + finishInFlight(for: key) + } + } func resetLoadingState() { payloadRefreshGeneration &+= 1 loadingCountsByKey.removeAll() loadingStartedAtByKey.removeAll() - inFlightKeys.removeAll() + finishAllInFlight() attemptedKeys.removeAll() } @@ -726,7 +795,7 @@ final class AppStore { Int(now.timeIntervalSince(started)), key.label, key.provider.rawValue) loadingCountsByKey[key] = nil loadingStartedAtByKey[key] = nil - inFlightKeys[key] = nil + finishInFlight(for: key) if cache[key] == nil { lastErrorByKey[key] = "Refresh took longer than expected. CodeBurn will keep retrying in the background." } @@ -734,7 +803,7 @@ final class AppStore { for (key, insertedAt) 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 + finishInFlight(for: key) } return true } @@ -770,7 +839,7 @@ final class AppStore { selectionFallbackPayload = nil loadingCountsByKey.removeAll() loadingStartedAtByKey.removeAll() - inFlightKeys.removeAll() + finishAllInFlight() attemptedKeys.removeAll() lastErrorByKey.removeAll() cacheDate = today @@ -834,11 +903,16 @@ final class AppStore { // `hasUsage` is authoritative for subscription/token-only providers, // where both cost and behavioral calls may legitimately be zero. - let scopedHasUsage = payload.current.cost > 0 - || payload.current.calls > 0 - || payload.current.sessions > 0 - || payload.current.inputTokens > 0 - || payload.current.outputTokens > 0 + 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 } @@ -913,15 +987,24 @@ final class AppStore { showLoading: Bool = false, qualityOfService: QualityOfService = .userInitiated ) async -> Bool { - if effectiveSelectedScope == .combined { + let keys = selectionRefreshKeys() + if keys.scoped.scope == .combined { + if keys.scoped.provider != .all { + _ = await refreshQuietly( + key: keys.all, + includeOptimize: false, + force: force, + qualityOfService: qualityOfService + ) + } async let local = refreshQuietly( - key: localCurrentKey, + key: keys.local, includeOptimize: includeOptimize, force: force, qualityOfService: qualityOfService ) async let combined = refresh( - key: currentKey, + key: keys.scoped, includeOptimize: includeOptimize, force: force, showLoading: showLoading, @@ -930,16 +1013,16 @@ final class AppStore { let (localSucceeded, combinedSucceeded) = await (local, combined) return localSucceeded && combinedSucceeded } else { - if currentKey.provider != .all { + if keys.scoped.provider != .all { _ = await refreshQuietly( - key: periodAllKey, + key: keys.all, includeOptimize: false, force: force, qualityOfService: qualityOfService ) } return await refresh( - key: currentKey, + key: keys.scoped, includeOptimize: includeOptimize, force: force, showLoading: showLoading, @@ -957,20 +1040,31 @@ 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 + ) if scope == .combined { - async let local = refreshQuietly(key: localCurrentKey, includeOptimize: false, force: false) + if scopedKey.provider != .all { + _ = await refreshQuietly(key: allKey, includeOptimize: false, force: force) + } + async let local = refreshQuietly(key: localKey, includeOptimize: false, force: false) async let combined = refreshQuietly(key: scopedKey, includeOptimize: false, force: force) _ = await (local, combined) } else { if scopedKey.provider != .all { - let allKey = PayloadCacheKey( - scope: .local, - period: scopedKey.period, - provider: .all, - day: scopedKey.day, - days: scopedKey.days, - claudeConfigSourceId: scopedKey.claudeConfigSourceId - ) _ = await refreshQuietly(key: allKey, includeOptimize: false, force: force) } await refreshQuietly(key: scopedKey, includeOptimize: false, force: force) @@ -1014,7 +1108,7 @@ final class AppStore { } defer { let abandonedAttempt = Task.isCancelled || generationAtStart != payloadRefreshGeneration - inFlightKeys[key] = nil + finishInFlight(for: key) if didShowLoading { finishLoading(for: key) } @@ -1140,7 +1234,10 @@ final class AppStore { let cached = cache[key], cached.isFresh, !providerPayloadContradictsAll(cached.payload, for: key) { return true } - if inFlightKeys[key] != nil { return false } + if inFlightKeys[key] != nil { + await waitForInFlight(key) + return consistentCachedPayload(for: key) != nil + } inFlightKeys[key] = Date() attemptedKeys.insert(key) let cacheDateAtStart = cacheDate @@ -1149,7 +1246,7 @@ final class AppStore { NSLog("CodeBurn: refreshing stale today status payload after %ds", age) } defer { - inFlightKeys[key] = nil + finishInFlight(for: key) } do { let fresh = try await fetchPayload( diff --git a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift index 5e4bee4f..1472b444 100644 --- a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift @@ -204,6 +204,34 @@ struct AppStoreRefreshRecoveryTests { #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() @@ -266,6 +294,72 @@ struct AppStoreRefreshRecoveryTests { #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() From 216ead74aef53fac4086c3d6f191b3b6ef91a32d Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Wed, 2 Sep 2026 07:52:18 -0700 Subject: [PATCH 4/9] fix(menubar): refresh(key:) reported failure instead of joining an in-flight fetch The interactive refresh path bailed with false whenever the key was already being fetched. recoverFromStuckLoading() reads that return value, so a healthy fetch that was simply still running was announced as a failed recovery, and a second interactive refresh threw away a result that was moments from landing. Join the in-flight fetch and report on its outcome, which is what the quiet path already did. Adds a DataClient fetch hook under DEBUG: slot ownership and acceptance ordering are only observable when two REAL fetches overlap, which needs a fetch whose completion the test controls rather than a spawned CLI. --- mac/Sources/CodeBurnMenubar/AppStore.swift | 9 +- .../CodeBurnMenubar/Data/DataClient.swift | 13 ++ .../AppStoreRefreshConcurrencyTests.swift | 140 ++++++++++++++++++ 3 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index b31eeebe..da7beec2 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -1087,7 +1087,14 @@ final class AppStore { let cached = cache[key], cached.isFresh, !providerPayloadContradictsAll(cached.payload, for: key) { return true } - if inFlightKeys[key] != nil { return false } + // 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 + } inFlightKeys[key] = Date() attemptedKeys.insert(key) lastErrorByKey[key] = nil diff --git a/mac/Sources/CodeBurnMenubar/Data/DataClient.swift b/mac/Sources/CodeBurnMenubar/Data/DataClient.swift index fe86e171..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 = [], @@ -56,6 +64,11 @@ struct DataClient { 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, diff --git a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift new file mode 100644 index 00000000..b65708ab --- /dev/null +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift @@ -0,0 +1,140 @@ +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.. Date: Wed, 2 Sep 2026 07:53:03 -0700 Subject: [PATCH 5/9] fix(menubar): an evicted in-flight fetch cleared its successor's slot inFlightKeys held only a timestamp, so the slot had no notion of WHICH fetch owned it. The watchdog and the display-sleep reset both evict an occupant, and a later fetch then legitimately claims the same key. When the evicted fetch finally returned, its defer cleared the new owner's slot and resumed every task parked on it, so a waiter woke on an empty cache and reported failure while the real fetch was still running. The slot now carries a token. A fetch releases it only if it still owns it; the watchdog and the resets take an explicit force path, because they are declaring the slot dead rather than reporting a fetch that ended. --- mac/Sources/CodeBurnMenubar/AppStore.swift | 58 ++++++++++++++----- .../AppStoreRefreshConcurrencyTests.swift | 50 ++++++++++++++++ 2 files changed, 93 insertions(+), 15 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index da7beec2..1077abac 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -535,7 +535,8 @@ 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() { @@ -567,7 +568,7 @@ final class AppStore { period: Period, provider: ProviderFilter, day: String? = nil) { - finishInFlight(for: PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)) + forceFinishInFlight(for: PayloadCacheKey(scope: scope, period: period, provider: provider, day: day)) } func selectionRefreshKeysForTesting() -> (scoped: PayloadCacheKey, all: PayloadCacheKey) { @@ -727,7 +728,18 @@ final class AppStore { } } - private var inFlightKeys: [PayloadCacheKey: Date] = [:] + /// 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 { @@ -741,7 +753,23 @@ final class AppStore { } } - private func finishInFlight(for key: PayloadCacheKey) { + 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 { @@ -752,7 +780,7 @@ final class AppStore { private func finishAllInFlight() { let keys = Set(inFlightKeys.keys).union(inFlightWaiters.keys) for key in keys { - finishInFlight(for: key) + forceFinishInFlight(for: key) } } @@ -784,8 +812,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 } @@ -795,15 +823,15 @@ final class AppStore { Int(now.timeIntervalSince(started)), key.label, key.provider.rawValue) loadingCountsByKey[key] = nil loadingStartedAtByKey[key] = nil - finishInFlight(for: key) + 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) - finishInFlight(for: key) + Int(now.timeIntervalSince(slot.startedAt)), key.label, key.provider.rawValue) + forceFinishInFlight(for: key) } return true } @@ -1095,7 +1123,7 @@ final class AppStore { await waitForInFlight(key) return consistentCachedPayload(for: key) != nil } - inFlightKeys[key] = Date() + let inFlightToken = claimInFlight(key) attemptedKeys.insert(key) lastErrorByKey[key] = nil let didShowLoading = showLoading || cache[key] == nil @@ -1115,7 +1143,7 @@ final class AppStore { } defer { let abandonedAttempt = Task.isCancelled || generationAtStart != payloadRefreshGeneration - finishInFlight(for: key) + finishInFlight(for: key, token: inFlightToken) if didShowLoading { finishLoading(for: key) } @@ -1245,7 +1273,7 @@ final class AppStore { await waitForInFlight(key) return consistentCachedPayload(for: key) != nil } - inFlightKeys[key] = Date() + let inFlightToken = claimInFlight(key) attemptedKeys.insert(key) let cacheDateAtStart = cacheDate let generationAtStart = payloadRefreshGeneration @@ -1253,7 +1281,7 @@ final class AppStore { NSLog("CodeBurn: refreshing stale today status payload after %ds", age) } defer { - finishInFlight(for: key) + finishInFlight(for: key, token: inFlightToken) } do { let fresh = try await fetchPayload( diff --git a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift index b65708ab..4a7bda54 100644 --- a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift @@ -109,6 +109,56 @@ private func settle(until condition: () -> Bool, turns: Int = 500) async -> Bool @MainActor struct AppStoreRefreshConcurrencyTests { + @Test("a fetch whose in-flight slot was evicted cannot release its successor's slot") + func evictedFetchCannotReleaseSuccessorSlot() async { + let script = ScriptedFetch() + script.install() + defer { script.uninstall() } + + let store = AppStore() + store.setCacheDateToTodayForTesting() + + // A claims the slot for (today, claude) and parks inside its fetch. + let taskA = Task { await store.refreshQuietlyForTesting(period: .today, provider: .claude) } + #expect(await settle(until: { script.attempts.count == 1 })) + #expect(store.isInFlightForTesting(period: .today, provider: .claude)) + + // The display-sleep reset evicts the slot out from under A. + store.resetLoadingState() + #expect(!store.isInFlightForTesting(period: .today, provider: .claude)) + + // B legitimately claims the same key afterwards. + let taskB = Task { await store.refreshQuietlyForTesting(period: .today, provider: .claude) } + #expect(await settle(until: { script.attempts.count == 2 })) + #expect(store.isInFlightForTesting(period: .today, provider: .claude)) + + // C finds the key busy and parks as a waiter on B. + var cFinished = false + let taskC = Task { + let result = await store.refreshQuietlyForTesting(period: .today, provider: .claude) + cFinished = true + return result + } + #expect(await settle(until: { store.inFlightWaiterCountForTesting(period: .today, provider: .claude) == 1 })) + + // A finishes late. Its slot is gone, so its release must be a no-op: + // B keeps the slot and C stays parked. Untokened, A's defer cleared B's + // slot and woke C onto an empty cache. + script.release(0) + _ = await taskA.value + #expect(store.isInFlightForTesting(period: .today, provider: .claude)) + #expect(store.inFlightWaiterCountForTesting(period: .today, provider: .claude) == 1) + #expect(!cFinished) + + // B finishes and C resumes off B's result. + script.release(1) + #expect(await taskB.value) + #expect(await taskC.value) + #expect(cFinished) + #expect(!store.isInFlightForTesting(period: .today, provider: .claude)) + #expect(store.inFlightWaiterCountForTesting(period: .today, provider: .claude) == 0) + } + @Test("an interactive refresh joins an in-flight fetch instead of reporting failure") func interactiveRefreshJoinsInFlightFetch() async { let script = ScriptedFetch() From 68f6d1ced34d33134d94255b8f89259dc5758d2b Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Wed, 2 Sep 2026 07:54:03 -0700 Subject: [PATCH 6/9] fix(menubar): the all-provider and scoped fetches were serialized on every tab click To guarantee the all-provider slice was cached before a scoped result could be accepted, the code simply awaited the all-provider fetch BEFORE starting the scoped one. That is two full parses back to back on every tab click, and the ordering it bought is only needed at acceptance time, not at start time. Both fetches now start together. fetchPayload awaits the all-provider task after its own fetch has already returned and before the consistency check reads it, so the evidence is still present when the scoped result is judged. The resident-bypassing cold fetch was already conditional on a contradiction actually being detected; it is unchanged. --- mac/Sources/CodeBurnMenubar/AppStore.swift | 115 +++++++++++------- .../AppStoreRefreshConcurrencyTests.swift | 42 +++++++ 2 files changed, 110 insertions(+), 47 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index 1077abac..4666cc01 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -707,24 +707,43 @@ 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 { - if provider != .all { - _ = await refreshQuietly(key: allKey, includeOptimize: false, force: false) - } - async let local = refresh(key: localKey, includeOptimize: false, force: false, showLoading: false) - async let combined = 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 if provider == .all { - await refresh(key: key, includeOptimize: false, force: false, showLoading: false) } else { - // Establish the matching all-provider slice first. Otherwise a - // fast scoped zero can be accepted before the positive all - // result arrives, creating a brief false-zero flash and a - // cached lie until the next refresh. - _ = await refreshQuietly(key: allKey, includeOptimize: false, force: false) - await refresh(key: key, includeOptimize: false, force: false, showLoading: false) + await self.refresh(key: key, includeOptimize: false, force: false, showLoading: false, acceptAfter: gate) } + _ = await allTask?.value + } + } + + /// 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 + ) } } @@ -947,7 +966,8 @@ final class AppStore { private func fetchPayload( for key: PayloadCacheKey, includeOptimize: Bool, - qualityOfService: QualityOfService + qualityOfService: QualityOfService, + acceptAfter: (@Sendable () async -> Void)? = nil ) async throws -> MenubarPayload { let fresh = try await DataClient.fetch( period: key.period, @@ -959,6 +979,11 @@ final class AppStore { 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 } NSLog("CodeBurn: resident %@ payload contradicted its all-provider slice; verifying with a one-shot parse", @@ -1016,45 +1041,38 @@ final class AppStore { qualityOfService: QualityOfService = .userInitiated ) async -> Bool { 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 { - if keys.scoped.provider != .all { - _ = await refreshQuietly( - key: keys.all, - includeOptimize: false, - force: force, - qualityOfService: qualityOfService - ) - } async let local = refreshQuietly( key: keys.local, includeOptimize: includeOptimize, force: force, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: gate ) async let combined = refresh( 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 { - if keys.scoped.provider != .all { - _ = await refreshQuietly( - key: keys.all, - includeOptimize: false, - force: force, - qualityOfService: qualityOfService - ) - } return await refresh( key: keys.scoped, includeOptimize: includeOptimize, force: force, showLoading: showLoading, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: gate ) } } @@ -1084,19 +1102,18 @@ final class AppStore { 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 { - if scopedKey.provider != .all { - _ = await refreshQuietly(key: allKey, includeOptimize: false, force: force) - } - async let local = refreshQuietly(key: localKey, 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 { - if scopedKey.provider != .all { - _ = await refreshQuietly(key: allKey, includeOptimize: false, force: force) - } - await refreshQuietly(key: scopedKey, includeOptimize: false, force: force) + await refreshQuietly(key: scopedKey, includeOptimize: false, force: force, acceptAfter: gate) } + _ = await allTask?.value } @discardableResult @@ -1105,7 +1122,8 @@ 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 @@ -1156,7 +1174,8 @@ final class AppStore { let fresh = try await fetchPayload( for: key, includeOptimize: includeOptimize, - 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") @@ -1262,7 +1281,8 @@ 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, @@ -1287,7 +1307,8 @@ final class AppStore { let fresh = try await fetchPayload( for: key, includeOptimize: includeOptimize, - qualityOfService: qualityOfService + qualityOfService: qualityOfService, + acceptAfter: acceptAfter ) if generationAtStart != payloadRefreshGeneration { NSLog("CodeBurn: dropping quiet fetch result for \(key.label) — refresh pipeline reset mid-fetch") diff --git a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift index 4a7bda54..37f8dab3 100644 --- a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift @@ -159,6 +159,48 @@ struct AppStoreRefreshConcurrencyTests { #expect(store.inFlightWaiterCountForTesting(period: .today, provider: .claude) == 0) } + @Test("the all-provider and scoped fetches run together and only acceptance is ordered") + func allAndScopedFetchesOverlap() async { + let script = ScriptedFetch() + script.payloadForProvider = { provider in + provider == .all + ? scriptedPayload(cost: 12, providers: ["claude": 12], + providerDetails: [ProviderDetail(id: "claude", label: "Claude", cost: 12, calls: 4, hasUsage: true)]) + : scriptedPayload(cost: 12, providers: ["claude": 12], + providerDetails: [ProviderDetail(id: "claude", label: "Claude", cost: 12, calls: 4, hasUsage: true)]) + } + script.install() + defer { script.uninstall() } + + let store = AppStore() + store.setCacheDateToTodayForTesting() + store.suppressRefreshesForTesting() + store.switchTo(provider: .claude) + + let task = Task { await store.refresh(includeOptimize: false, force: true) } + + // BOTH parses are in flight before either has produced a result. The + // serialized version could not reach two attempts until the first + // returned. + #expect(await settle(until: { script.attempts.count == 2 })) + #expect(Set(script.attempts.map(\.provider)) == Set([.all, .claude])) + #expect(script.isParked(0)) + #expect(script.isParked(1)) + + // Acceptance is still ordered: release the scoped fetch first and it + // must not land until the all-provider evidence has. + let scopedIndex = script.indices(for: .claude)[0] + let allIndex = script.indices(for: .all)[0] + script.release(scopedIndex) + for _ in 0..<50 { await Task.yield() } + #expect(store.cachedPayloadForTesting(period: .today, provider: .claude) == nil) + + script.release(allIndex) + #expect(await task.value) + #expect(store.cachedPayloadForTesting(period: .today, provider: .claude) != nil) + #expect(store.cachedPayloadForTesting(period: .today, provider: .all) != nil) + } + @Test("an interactive refresh joins an in-flight fetch instead of reporting failure") func interactiveRefreshJoinsInFlightFetch() async { let script = ScriptedFetch() From cd7de30bc4d5349087df6db89f1d6499cbe43d50 Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Wed, 2 Sep 2026 07:54:55 -0700 Subject: [PATCH 7/9] fix(menubar): a twice-contradicting scoped payload threw instead of being served When a scoped payload disagreed with its all-provider slice, the code re-ran the fetch bypassing the resident child and, if it STILL disagreed, threw. That verified payload is what the CLI actually reports for the provider, so throwing traded a real number for an error banner and left the tab with nothing at all. The payload is now cached with a contradictsAll flag on CachedPayload (add-only) and served, and the popover carries one subdued secondary line saying the total may be incomplete. The refresh paths treat an already-verified contradiction as usable, so a poll cannot re-run the one-shot parse forever. --- mac/Sources/CodeBurnMenubar/AppStore.swift | 69 ++++++++++++------- .../Views/MenuBarContent.swift | 8 +++ .../AppStoreRefreshConcurrencyTests.swift | 38 ++++++++++ 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index 4666cc01..ff732e87 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 } } @@ -41,14 +45,6 @@ struct PayloadCacheKey: Hashable { } } -private struct ProviderPayloadConsistencyError: Error, CustomStringConvertible { - let provider: ProviderFilter - - var description: String { - "Fresh \(provider.rawValue) verification still contradicted the matching all-provider payload." - } -} - @MainActor @Observable final class AppStore { @@ -908,10 +904,28 @@ 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 payload = cache[key]?.payload, - !providerPayloadContradictsAll(payload, for: key) else { return nil } - return payload + 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, @@ -968,7 +982,7 @@ final class AppStore { includeOptimize: Bool, qualityOfService: QualityOfService, acceptAfter: (@Sendable () async -> Void)? = nil - ) async throws -> MenubarPayload { + ) async throws -> (payload: MenubarPayload, contradictsAll: Bool) { let fresh = try await DataClient.fetch( period: key.period, day: key.day, @@ -984,7 +998,7 @@ final class AppStore { // 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 } + 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) @@ -999,10 +1013,17 @@ final class AppStore { bypassResident: true, qualityOfService: qualityOfService ) - guard !providerPayloadContradictsAll(verified, for: key) else { - throw ProviderPayloadConsistencyError(provider: key.provider) + // 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 + return (verified, stillContradicts) } @discardableResult @@ -1132,7 +1153,7 @@ final class AppStore { if !force, let cached = cache[key], cached.isFresh, - !providerPayloadContradictsAll(cached.payload, for: key) { return true } + 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 @@ -1198,11 +1219,11 @@ 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()) + cache[key] = CachedPayload(payload: fresh.payload, fetchedAt: Date(), contradictsAll: fresh.contradictsAll) if key == currentKey || (effectiveSelectedScope == .combined && key == localCurrentKey) { selectionFallbackPayload = nil } - reconcileClaudeConfigSelection(from: fresh, for: key) + reconcileClaudeConfigSelection(from: fresh.payload, for: key) lastSuccessByKey[key] = Date() lastErrorByKey[key] = nil succeeded = true @@ -1222,8 +1243,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 @@ -1288,7 +1309,7 @@ final class AppStore { if !force, let cached = cache[key], cached.isFresh, - !providerPayloadContradictsAll(cached.payload, for: key) { return true } + cachedPayloadIsUsable(cached, for: key) { return true } if inFlightKeys[key] != nil { await waitForInFlight(key) return consistentCachedPayload(for: key) != nil @@ -1320,8 +1341,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/Views/MenuBarContent.swift b/mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift index 1ad37af7..2f302e1d 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() diff --git a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift index 37f8dab3..cac80726 100644 --- a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshConcurrencyTests.swift @@ -229,4 +229,42 @@ struct AppStoreRefreshConcurrencyTests { #expect(await first.value) #expect(await second.value) } + + @Test("a twice-contradicting payload is cached and flagged instead of raising an error") + func twiceContradictingPayloadIsCachedAndFlagged() async { + let script = ScriptedFetch() + // The scoped payload reports nothing for a provider the all-provider + // slice says is active, and says so again on the verifying re-parse. + script.payloadForProvider = { provider in + provider == .all + ? scriptedPayload(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), + ]) + : scriptedPayload(cost: 0, calls: 0, providers: ["hermes agent": 0], + providerDetails: [ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false)]) + } + script.install() + defer { script.uninstall() } + + let store = AppStore() + store.setCacheDateToTodayForTesting() + store.suppressRefreshesForTesting() + store.switchTo(provider: .hermes) + + let task = Task { await store.refresh(includeOptimize: false, force: true) } + // all + scoped + the resident-bypassing verification of the scoped one. + #expect(await settle(until: { script.attempts.count >= 2 })) + for index in 0.. Date: Wed, 2 Sep 2026 07:55:43 -0700 Subject: [PATCH 8/9] fix(menubar): the selection fallback showed another provider's totals under the selected tab Switching providers stashed the outgoing payload and rendered it under the new tab until that tab's own fetch landed. That is not continuity, it is a wrong number: opening the Hermes tab printed the all-provider total, and the test suite pinned that behaviour by asserting $12 under Hermes and $12.34 under Cursor. The fallback is gone. With no cached data for the selection, `payload` is empty, `hasCachedData` is false, and the popover renders the loading state it already has for a cold key, so no dollar figure is shown for a provider whose data has not arrived. AgentTabStrip and MenuBarContent take the same hasCachedData gate for the same reason. --- mac/Sources/CodeBurnMenubar/AppStore.swift | 16 +++++---------- .../CodeBurnMenubar/Views/AgentTabStrip.swift | 6 +++++- .../Views/MenuBarContent.swift | 2 +- .../AppStoreRefreshRecoveryTests.swift | 20 ++++++++++++++++--- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index ff732e87..9789ad84 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -213,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 @@ -317,7 +316,11 @@ final class AppStore { return combinedPayload } } - return consistentCachedPayload(for: currentKey) ?? 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. @@ -654,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). @@ -815,7 +815,6 @@ final class AppStore { lastErrorByKey.removeAll() if clearCache { cache.removeAll() - selectionFallbackPayload = nil } } @@ -879,7 +878,6 @@ final class AppStore { if cacheDate != today { payloadRefreshGeneration &+= 1 cache.removeAll() - selectionFallbackPayload = nil loadingCountsByKey.removeAll() loadingStartedAtByKey.removeAll() finishAllInFlight() @@ -892,7 +890,6 @@ final class AppStore { func invalidateCache() { cache.removeAll() - selectionFallbackPayload = nil } private func reconcileClaudeConfigSelection(from payload: MenubarPayload, for key: PayloadCacheKey) { @@ -1220,9 +1217,6 @@ final class AppStore { return false } cache[key] = CachedPayload(payload: fresh.payload, fetchedAt: Date(), contradictsAll: fresh.contradictsAll) - if key == currentKey || (effectiveSelectedScope == .combined && key == localCurrentKey) { - selectionFallbackPayload = nil - } reconcileClaudeConfigSelection(from: fresh.payload, for: key) lastSuccessByKey[key] = Date() lastErrorByKey[key] = nil 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 2f302e1d..fe34eafd 100644 --- a/mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift +++ b/mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift @@ -127,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/AppStoreRefreshRecoveryTests.swift b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift index 1472b444..1682510b 100644 --- a/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/AppStoreRefreshRecoveryTests.swift @@ -118,7 +118,12 @@ struct AppStoreRefreshRecoveryTests { store.setCachedPayloadForTesting(falseZero, period: .today, provider: .hermes, fetchedAt: Date()) #expect(store.providerPayloadContradictsAllForTesting(falseZero, period: .today, provider: .hermes)) - #expect(store.payload.current.cost == 12) + // 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) } @@ -386,8 +391,8 @@ struct AppStoreRefreshRecoveryTests { #expect(!store.providerPayloadContradictsAllForTesting(zero, period: .today, provider: .hermes)) } - @Test("provider switches retain the last rendered payload until the target loads") - func providerSwitchRetainsLastRenderedPayload() { + @Test("a provider switch shows no figure until the target provider loads") + func providerSwitchShowsNoStaleFigure() { let store = AppStore() store.suppressRefreshesForTesting() store.setCachedPayloadForTesting( @@ -399,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) } From 034ae444d041ac7b933acfc805384a14b0d18cc7 Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Wed, 2 Sep 2026 07:55:54 -0700 Subject: [PATCH 9/9] test(menubar): pin every ProviderFilter case to a real CLI provider id ProviderFilter.cliArg is both the --provider argument and the id the payload-consistency guard matches against providerDetails. A case whose cliArg is not a 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 from the TypeScript side, where the registry is importable and the Swift source is readable. Table-driven over all 30 enum cases. No mapping needed fixing: .kimiCode already yields `kimicode`, which is the registry name (`kimi-code` is not a provider the CLI knows), and every other case resolves too. Setting .kimiCode to `kimi-code` makes this suite fail, which is the check working. --- tests/menubar-provider-alias-contract.test.ts | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/menubar-provider-alias-contract.test.ts 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) + }) +})