diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index db1d527d..7187c220 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -166,8 +166,9 @@ final class AppStore { var copilotError: String? // Same file-based activation as Kimi/Gemini — reading // Copilot discovery never raises a keychain prompt, so we start dormant and - // auto-activate on the first refresh tick. - var copilotLoadState: SubscriptionLoadState = CopilotSubscriptionService.hasCredential ? .dormant : .notBootstrapped + // auto-activate on the first refresh tick unless the user explicitly + // disconnected. That opt-out is persisted and applied in `init`. + var copilotLoadState: SubscriptionLoadState = .notBootstrapped var antigravityUsage: AntigravityUsage? var antigravityError: String? @@ -201,6 +202,19 @@ final class AppStore { (CapacityDockProvider) -> Void = { CapacityDockPreferences.removeProvider($0) } + @ObservationIgnored var copilotQuotaRuntime: CopilotQuotaRuntime + + init(copilotQuotaRuntime: CopilotQuotaRuntime = .live) { + self.copilotQuotaRuntime = copilotQuotaRuntime + copilotLoadState = Self.initialCopilotLoadState(runtime: copilotQuotaRuntime) + } + + private static func initialCopilotLoadState(runtime: CopilotQuotaRuntime) -> SubscriptionLoadState { + if CopilotExplicitDisconnect.isSet(defaults: runtime.defaults) { + return .notBootstrapped + } + return runtime.hasCredential() ? .dormant : .notBootstrapped + } /// Generation tokens for the in-flight refresh tasks. Incremented on every /// disconnect / reset so a fetch that started before the disconnect cannot @@ -1708,13 +1722,19 @@ final class AppStore { /// Same prompt-free activation as Kimi/Gemini: the whole Copilot discovery /// chain is prompt-free, so the first refresh tick activates dormant state. + /// Automatic cadence must not clear an explicit disconnect; that happens + /// only in `connectCopilot`. func bootstrapCopilot() async { + if CopilotExplicitDisconnect.isSet(defaults: copilotQuotaRuntime.defaults) { + copilotLoadState = .notBootstrapped + return + } // Capture the generation before the await so a disconnect that lands // mid-fetch cannot be resurrected into .loaded when the fetch returns. let gen = copilotRefreshGen copilotLoadState = .bootstrapping do { - let usage = try await CopilotSubscriptionService.refresh() + let usage = try await copilotQuotaRuntime.refresh() guard gen == copilotRefreshGen else { return } copilotUsage = usage copilotError = nil @@ -1729,24 +1749,35 @@ final class AppStore { } } + /// User-initiated Connect / Reconnect / Load Quota. Clears persisted + /// explicit disconnect so discovery may run again. + func connectCopilot() async { + CopilotExplicitDisconnect.clear(defaults: copilotQuotaRuntime.defaults) + await bootstrapCopilot() + } + func refreshCopilot() async { _ = await refreshCopilotReportingSuccess() } @discardableResult func refreshCopilotReportingSuccess() async -> Bool { + if CopilotExplicitDisconnect.isSet(defaults: copilotQuotaRuntime.defaults) { + if copilotLoadState != .notBootstrapped { copilotLoadState = .notBootstrapped } + return false + } if case .dormant = copilotLoadState { await bootstrapCopilot() return copilotLoadState == .loaded } - guard CopilotSubscriptionService.hasCredential else { + guard copilotQuotaRuntime.hasCredential() else { if copilotLoadState != .notBootstrapped { copilotLoadState = .notBootstrapped } return false } let gen = copilotRefreshGen if copilotUsage == nil { copilotLoadState = .loading } do { - let usage = try await CopilotSubscriptionService.refresh() + let usage = try await copilotQuotaRuntime.refresh() guard gen == copilotRefreshGen else { return false } copilotUsage = usage copilotError = nil @@ -1765,7 +1796,8 @@ final class AppStore { } func disconnectCopilot() { - CopilotSubscriptionService.disconnect() + copilotQuotaRuntime.disconnectService() + CopilotExplicitDisconnect.mark(defaults: copilotQuotaRuntime.defaults) copilotRefreshGen &+= 1 copilotUsage = nil copilotError = nil @@ -2217,7 +2249,7 @@ final class AppStore { case .codex: await bootstrapCodex() case .kimiCode: await bootstrapKimi() case .gemini: await bootstrapGemini() - case .copilot: await bootstrapCopilot() + case .copilot: await connectCopilot() case .antigravity: await bootstrapAntigravity() default: break } diff --git a/mac/Sources/CodeBurnMenubar/Data/CopilotExplicitDisconnect.swift b/mac/Sources/CodeBurnMenubar/Data/CopilotExplicitDisconnect.swift new file mode 100644 index 00000000..930b647d --- /dev/null +++ b/mac/Sources/CodeBurnMenubar/Data/CopilotExplicitDisconnect.swift @@ -0,0 +1,39 @@ +import Foundation + +/// Persisted opt-out for live Copilot quota tracking after an explicit +/// Disconnect. Credentials stay untouched; this flag only stops automatic +/// discovery from bringing quota tracking back on the next refresh or relaunch. +/// Absent key is false, so first-use autodiscovery is unchanged. +enum CopilotExplicitDisconnect { + static let defaultsKey = "codeburn.copilot.explicitlyDisconnected" + + static func isSet(defaults: UserDefaults = .standard) -> Bool { + defaults.bool(forKey: defaultsKey) + } + + static func mark(defaults: UserDefaults = .standard) { + defaults.set(true, forKey: defaultsKey) + } + + static func clear(defaults: UserDefaults = .standard) { + defaults.removeObject(forKey: defaultsKey) + } +} + +/// Injectable Copilot quota I/O for AppStore. Production uses `.live`; +/// tests pass an ephemeral UserDefaults suite and stubbed fetch/credential +/// checks so they never touch installed prefs, Keychain, or `gh`. +@MainActor +struct CopilotQuotaRuntime { + var hasCredential: () -> Bool + var refresh: @Sendable () async throws -> CopilotUsage + var disconnectService: () -> Void + var defaults: UserDefaults + + static let live = CopilotQuotaRuntime( + hasCredential: { CopilotSubscriptionService.hasCredential }, + refresh: { try await CopilotSubscriptionService.refresh() }, + disconnectService: { CopilotSubscriptionService.disconnect() }, + defaults: .standard + ) +} diff --git a/mac/Sources/CodeBurnMenubar/Data/CopilotQuotaPresentation.swift b/mac/Sources/CodeBurnMenubar/Data/CopilotQuotaPresentation.swift index ab4bd38e..acfd05a0 100644 --- a/mac/Sources/CodeBurnMenubar/Data/CopilotQuotaPresentation.swift +++ b/mac/Sources/CodeBurnMenubar/Data/CopilotQuotaPresentation.swift @@ -9,11 +9,18 @@ import Foundation /// with a quiet caption instead of flapping to a reconnect screen; the /// reconnect screen is reserved for the no-data case, where there is /// genuinely nothing to show. +/// +/// Explicit Disconnect is different from missing credentials: the store keeps +/// `.notBootstrapped` while credentials stay on disk. Plan, Settings, and +/// Capacity Dock nil-quota copy therefore take the persisted opt-out flag and +/// must not claim the token is gone. enum CopilotQuotaPresentation { /// Which Plan-tab subview to render, given the load state and whether a /// last-known snapshot exists. enum PlanContent: Equatable { case noCredentials + /// Explicit Disconnect: quota tracking is off, credentials untouched. + case disconnected case loading case failed case transientFailed @@ -24,9 +31,26 @@ enum CopilotQuotaPresentation { case usage(idle: Bool) } - static func planContent(loadState: SubscriptionLoadState, hasUsage: Bool) -> PlanContent { + static let noCredentialsPlanTitle = "No Copilot credentials found" + static let noCredentialsPlanMessage = + "Sign in via an editor's Copilot plugin first. Then click Try Again." + static let disconnectedPlanTitle = "Copilot quota tracking disconnected" + static let disconnectedPlanMessage = + "Your Copilot credentials are untouched. Click Connect to resume." + static let noCredentialsSettingsDetail = + "Usage tracking still works. For live quota, sign in with the Copilot CLI or gh auth login, or paste a token below, then click Connect." + static let disconnectedSettingsDetail = + "Quota tracking disconnected. Credentials are untouched. Click Connect to resume." + + static func planContent( + loadState: SubscriptionLoadState, + hasUsage: Bool, + explicitlyDisconnected: Bool = false + ) -> PlanContent { switch loadState { - case .notBootstrapped, .noCredentials: + case .notBootstrapped: + return explicitlyDisconnected ? .disconnected : .noCredentials + case .noCredentials: return .noCredentials case .dormant, .bootstrapping: return .loading @@ -41,6 +65,10 @@ enum CopilotQuotaPresentation { } } + static func settingsNotConnectedDetail(explicitlyDisconnected: Bool) -> String { + explicitlyDisconnected ? disconnectedSettingsDetail : noCredentialsSettingsDetail + } + /// Snapshot age past which a loaded view stamps an "as of