From 03b6cc4857c9b7455cc0b1c3632784bf08090e82 Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Sat, 29 Aug 2026 14:17:15 -0700 Subject: [PATCH] fix(menubar): harden the Cursor quota adapter Require every Dependencies field explicitly so a test can never fall through to the live adapter and read the local Cursor session. Surface an unreadable local store as its own honest error instead of a network failure. Read the state database off the main actor so a lock held by Cursor cannot stall the UI. Report the tighter of the Auto and API pools when Cursor omits the total, since averaging hides an exhausted pool behind an idle one. --- .../CapacityDockProviderQuotaService.swift | 13 +++------ .../Data/CursorSubscriptionService.swift | 18 ++++++++++--- .../CursorQuotaTests.swift | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/Data/CapacityDockProviderQuotaService.swift b/mac/Sources/CodeBurnMenubar/Data/CapacityDockProviderQuotaService.swift index dd30b0ea..4a437b24 100644 --- a/mac/Sources/CodeBurnMenubar/Data/CapacityDockProviderQuotaService.swift +++ b/mac/Sources/CodeBurnMenubar/Data/CapacityDockProviderQuotaService.swift @@ -17,19 +17,12 @@ enum CapacityDockProviderRefreshInteraction { @MainActor final class CapacityDockProviderQuotaService { struct Dependencies: Sendable { + // No defaults here: only `.live` may reach the real adapters, so a + // test can never silently read the local Cursor session or hit the + // network by omitting a field. var refreshClinePass: @Sendable (String) async throws -> QuotaSummary var refreshCursor: @Sendable () async throws -> QuotaSummary - init( - refreshClinePass: @escaping @Sendable (String) async throws -> QuotaSummary, - refreshCursor: @escaping @Sendable () async throws -> QuotaSummary = { - try await CursorSubscriptionService.refresh() - } - ) { - self.refreshClinePass = refreshClinePass - self.refreshCursor = refreshCursor - } - static let live = Dependencies( refreshClinePass: { apiKey in try await ClinePassSubscriptionService.refresh(apiKey: apiKey) diff --git a/mac/Sources/CodeBurnMenubar/Data/CursorSubscriptionService.swift b/mac/Sources/CodeBurnMenubar/Data/CursorSubscriptionService.swift index 9f3ca632..65331b68 100644 --- a/mac/Sources/CodeBurnMenubar/Data/CursorSubscriptionService.swift +++ b/mac/Sources/CodeBurnMenubar/Data/CursorSubscriptionService.swift @@ -12,6 +12,7 @@ enum CursorSubscriptionService { case noCredentials case expiredSession case authenticationRejected + case appDataUnreadable case rateLimited case providerUnavailable case parseFailure @@ -27,7 +28,7 @@ enum CursorSubscriptionService { switch self { case .noCredentials, .expiredSession, .authenticationRejected: return .terminalAuth - case .rateLimited, .providerUnavailable, .network: + case .appDataUnreadable, .rateLimited, .providerUnavailable, .network: return .transient case .parseFailure: return .parseFailure @@ -42,6 +43,8 @@ enum CursorSubscriptionService { return "The Cursor app session is expired or invalid. Sign in again, then click Retry." case .authenticationRejected: return "Cursor rejected the current app session. Sign in again, then click Retry." + case .appDataUnreadable: + return "Could not read the Cursor app's local session data. Quit and reopen Cursor, then click Retry." case .rateLimited: return "Cursor rate-limited the quota request." case .providerUnavailable: @@ -81,7 +84,12 @@ enum CursorSubscriptionService { static func refresh(deps: Deps = .live) async throws -> QuotaSummary { let accessToken: String do { - guard let loaded = try deps.loadAccessToken()?.trimmingCharacters(in: .whitespacesAndNewlines), + // The store read is synchronous SQLite with a 250ms busy timeout; + // keep it off the main actor so a lock held by Cursor cannot + // stall the UI. + let loadAccessToken = deps.loadAccessToken + let raw = try await Task.detached { try loadAccessToken() }.value + guard let loaded = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !loaded.isEmpty else { throw FetchError.noCredentials } @@ -89,7 +97,7 @@ enum CursorSubscriptionService { } catch let error as FetchError { throw error } catch { - throw FetchError.network + throw FetchError.appDataUnreadable } let session = try session(from: accessToken) @@ -141,7 +149,9 @@ enum CursorSubscriptionService { let monthlyPercent: Double? = { if let total = normalizedProviderPercent(plan?.totalPercentUsed) { return total } - if let autoPercent, let apiPercent { return (autoPercent + apiPercent) / 2 } + // A capacity gauge must surface the pool that is about to run + // out; averaging would hide an exhausted pool behind an idle one. + if let autoPercent, let apiPercent { return max(autoPercent, apiPercent) } if let apiPercent { return apiPercent } if let autoPercent { return autoPercent } if let ratio = ratio(used: plan?.used, limit: plan?.limit) { return ratio } diff --git a/mac/Tests/CodeBurnMenubarTests/CursorQuotaTests.swift b/mac/Tests/CodeBurnMenubarTests/CursorQuotaTests.swift index 69d27e54..e2f4ab50 100644 --- a/mac/Tests/CodeBurnMenubarTests/CursorQuotaTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/CursorQuotaTests.swift @@ -102,6 +102,33 @@ final class CursorQuotaTests: XCTestCase { await assertFetchError(.expiredSession, deps: expired) } + func testUnreadableLocalStoreIsReportedHonestlyWithoutNetwork() async throws { + struct SyntheticStoreFailure: Error {} + let deps = CursorSubscriptionService.Deps( + loadAccessToken: { throw SyntheticStoreFailure() }, + fetch: { request in + XCTFail("Network must not run when the local store is unreadable") + return (Data(), Self.httpResponse(request, status: 500)) + } + ) + await assertFetchError(.appDataUnreadable, deps: deps) + } + + func testMonthlyFallbackSurfacesTheTighterPool() throws { + let body = """ + { + "membershipType": "pro", + "individualUsage": { + "plan": { "autoPercentUsed": 20, "apiPercentUsed": 65 } + } + } + """ + + let summary = try CursorSubscriptionService.decode(Data(body.utf8)) + + XCTAssertEqual(summary.primary?.percent ?? -1, 0.65, accuracy: 0.0001) + } + func testCursorAuthenticationResponsesAreTerminal() async throws { for status in [401, 403] { let deps = CursorSubscriptionService.Deps(