codeburn/mac/Sources/CodeBurnMenubar/Data/CapacityDockProviderQuotaService.swift
Resham Joshi aded4c2326
Some checks failed
Tests / desktop (push) Has been cancelled
Tests / test-platforms (windows-latest) (push) Has been cancelled
Windows Menubar CI / check (ubuntu-latest) (push) Has been cancelled
Windows Menubar CI / check (windows-latest) (push) Has been cancelled
CI / semgrep (push) Has been cancelled
Tests / test (22) (push) Has been cancelled
Tests / test (22.13.0) (push) Has been cancelled
Tests / test (26) (push) Has been cancelled
Tests / test-platforms (macos-latest) (push) Has been cancelled
Tests / test (24) (push) Has been cancelled
macOS Menubar CI / test (push) Has been cancelled
feat(devin): plan and quota from the Devin CLI cache (#1606)
* feat(quota): Devin plan quota from the Devin CLI's local cache

Reads the newest ~/.cache/devin/cli/user_status.*.bin and walks only the
plan status message of its protobuf payload: plan name, daily and weekly
remaining percent and their resets. No network call and no API key; each
reading says how old the cache is. Any out-of-range field fails the read
instead of showing a number, and a window whose reset has passed since
the fetch is dropped.

CLI reader feeds codeburn quota and the Windows dock; the Mac Capacity
Dock gets the same decoder, and Devin moves from browser session to
local CLI, live.

* docs(windows): count twelve quota readers

* fix(app): list Devin among menu bar quota providers
2026-10-02 16:43:15 +02:00

232 lines
8.3 KiB
Swift

import Foundation
/// Marks the refresh as an explicit user action without granting hidden access
/// to browser cookies, another app's Keychain items, or copied provider code.
@MainActor
enum CapacityDockProviderRefreshInteraction {
static func userInitiated<T>(
operation: () async throws -> T
) async rethrows -> T {
try await operation()
}
}
/// CodeBurn-owned registry for Capacity Dock providers that are not served by
/// AppStore's six native quota paths. A provider is connectable only when an
/// adapter is explicitly registered here.
@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
var refreshDevin: @Sendable () async throws -> QuotaSummary
var refreshGrok: @Sendable () async throws -> QuotaSummary
var refreshGrokBot: @Sendable () async throws -> QuotaSummary
var refreshZai: @Sendable (String?) async throws -> QuotaSummary
var refreshZcode: @Sendable () async throws -> QuotaSummary
static let live = Dependencies(
refreshClinePass: { apiKey in
try await ClinePassSubscriptionService.refresh(apiKey: apiKey)
},
refreshCursor: {
try await CursorSubscriptionService.refresh()
},
refreshDevin: {
try DevinSubscriptionService.refresh()
},
refreshGrok: {
try await GrokBuildSubscriptionService.refresh()
},
refreshGrokBot: {
try await GrokBotSubscriptionService.refresh()
},
refreshZai: { apiKey in
try await ZaiSubscriptionService.refresh(apiKey: apiKey)
},
refreshZcode: {
try await ZcodeSubscriptionService.refresh()
}
)
}
static let shared = CapacityDockProviderQuotaService(dependencies: .live)
private let dependencies: Dependencies
init(dependencies: Dependencies) {
self.dependencies = dependencies
}
func fetch(
provider: CapacityDockProvider,
credential: CapacityDockProviderCredential
) async throws -> QuotaSummary {
switch provider.id {
case "cursor":
do {
return try await dependencies.refreshCursor()
} catch is CancellationError {
throw CancellationError()
} catch {
throw CapacityDockProviderFetchFailure(error: error)
}
case "devin":
do {
return try await dependencies.refreshDevin()
} catch is CancellationError {
throw CancellationError()
} catch {
throw CapacityDockProviderFetchFailure(error: error)
}
case "grok":
do {
return try await dependencies.refreshGrok()
} catch is CancellationError {
throw CancellationError()
} catch {
throw CapacityDockProviderFetchFailure(error: error)
}
case "grokbot":
do {
return try await dependencies.refreshGrokBot()
} catch is CancellationError {
throw CancellationError()
} catch {
throw CapacityDockProviderFetchFailure(error: error)
}
case "clinepass":
guard let apiKey = credential.sanitizedOverride.apiKey else {
throw CapacityDockProviderFetchFailure(
message: ClinePassSubscriptionService.FetchError.noCredentials.localizedDescription,
disposition: .terminal
)
}
do {
return try await dependencies.refreshClinePass(apiKey)
} catch is CancellationError {
throw CancellationError()
} catch {
throw CapacityDockProviderFetchFailure(error: error)
}
case "zai":
do {
return try await dependencies.refreshZai(credential.sanitizedOverride.apiKey)
} catch is CancellationError {
throw CancellationError()
} catch {
throw CapacityDockProviderFetchFailure(error: error)
}
case "zcode":
do {
return try await dependencies.refreshZcode()
} catch is CancellationError {
throw CancellationError()
} catch {
throw CapacityDockProviderFetchFailure(error: error)
}
default:
throw CapacityDockProviderFetchFailure(
message: "\(provider.displayName) does not have a CodeBurn live quota adapter yet. "
+ "Remove it from the dock or choose a supported provider.",
disposition: .terminal
)
}
}
}
enum CapacityDockProviderFetchFailureDisposition: Equatable, Sendable {
case terminal
case transient
}
/// The only failure distinction the dock needs: user-actionable connection
/// failures versus failures that should preserve last-known quota and retry.
struct CapacityDockProviderFetchFailure: LocalizedError, Equatable, Sendable {
let message: String
let disposition: CapacityDockProviderFetchFailureDisposition
init(message: String, disposition: CapacityDockProviderFetchFailureDisposition) {
self.message = message
self.disposition = disposition
}
init(error: Error) {
message = error.localizedDescription
disposition = Self.disposition(for: error)
}
var errorDescription: String? { message }
static func disposition(for error: Error) -> CapacityDockProviderFetchFailureDisposition {
if let failure = error as? CapacityDockProviderFetchFailure {
return failure.disposition
}
if let error = error as? ClinePassSubscriptionService.FetchError {
switch error.classification {
case .terminalAuth:
return .terminal
case .transient, .parseFailure:
return .transient
}
}
if let error = error as? CursorSubscriptionService.FetchError {
switch error.classification {
case .terminalAuth:
return .terminal
case .transient, .parseFailure:
return .transient
}
}
if let error = error as? DevinSubscriptionService.FetchError {
switch error.classification {
case .terminalAuth:
return .terminal
case .transient, .parseFailure:
return .transient
}
}
if let error = error as? GrokBuildSubscriptionService.FetchError {
switch error.classification {
case .terminalAuth:
return .terminal
case .transient, .parseFailure:
return .transient
}
}
if let error = error as? GrokBotSubscriptionService.FetchError {
switch error.classification {
case .terminalAuth:
return .terminal
case .transient, .parseFailure:
return .transient
}
}
if let error = error as? ZaiSubscriptionService.FetchError {
switch error.classification {
case .terminalAuth:
return .terminal
case .transient, .parseFailure:
return .transient
}
}
if let error = error as? ZcodeSubscriptionService.FetchError {
switch error.classification {
case .terminalAuth:
return .terminal
case .transient, .parseFailure:
return .transient
}
}
if error is URLError {
return .transient
}
// Unknown adapter failures are treated as temporary. They must never
// erase a valid last-known connection merely because classification is
// incomplete.
return .transient
}
}