mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 02:39:13 +00:00
Merge pull request #1278 from avs-io/codex/copilot-disconnect-1273-20260909
Some checks are pending
CI / semgrep (push) Waiting to run
macOS Menubar CI / test (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.13.0) (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / desktop (push) Waiting to run
Some checks are pending
CI / semgrep (push) Waiting to run
macOS Menubar CI / test (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.13.0) (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / desktop (push) Waiting to run
fix(mac): preserve explicit Copilot disconnect
This commit is contained in:
commit
0fea397d14
8 changed files with 403 additions and 23 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
}
|
||||
|
|
@ -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 <time>" caption,
|
||||
/// so a bar can never silently masquerade as current.
|
||||
static let stalenessThreshold: TimeInterval = 10 * 60
|
||||
|
|
|
|||
|
|
@ -695,7 +695,12 @@ struct CapacityDockDetailView: View {
|
|||
} else {
|
||||
VStack(alignment: .leading, spacing: 11 * model.detailScale) {
|
||||
header(provider, plan: nil)
|
||||
Text(ProviderConnectionGuidance.dockInstruction(for: provider))
|
||||
Text(
|
||||
provider == .copilot
|
||||
&& CopilotExplicitDisconnect.isSet(defaults: store.copilotQuotaRuntime.defaults)
|
||||
? CopilotQuotaPresentation.disconnectedSettingsDetail
|
||||
: ProviderConnectionGuidance.dockInstruction(for: provider)
|
||||
)
|
||||
.font(.system(size: 12))
|
||||
.foregroundStyle(Color.capacityDockText.opacity(0.62))
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
|
|
|
|||
|
|
@ -2465,12 +2465,21 @@ private struct CopilotPlanInsight: View {
|
|||
|
||||
var body: some View {
|
||||
Group {
|
||||
switch CopilotQuotaPresentation.planContent(loadState: store.copilotLoadState, hasUsage: store.copilotUsage != nil) {
|
||||
switch CopilotQuotaPresentation.planContent(
|
||||
loadState: store.copilotLoadState,
|
||||
hasUsage: store.copilotUsage != nil,
|
||||
explicitlyDisconnected: CopilotExplicitDisconnect.isSet(defaults: store.copilotQuotaRuntime.defaults)
|
||||
) {
|
||||
case .noCredentials:
|
||||
PlanNoCredentialsView(
|
||||
title: "No Copilot credentials found",
|
||||
message: "Sign in via an editor's Copilot plugin first. Then click Try Again."
|
||||
) { Task { await store.bootstrapCopilot() } }
|
||||
title: CopilotQuotaPresentation.noCredentialsPlanTitle,
|
||||
message: CopilotQuotaPresentation.noCredentialsPlanMessage
|
||||
) { Task { await store.connectCopilot() } }
|
||||
case .disconnected:
|
||||
PlanConnectView(
|
||||
title: CopilotQuotaPresentation.disconnectedPlanTitle,
|
||||
message: CopilotQuotaPresentation.disconnectedPlanMessage
|
||||
) { Task { await store.connectCopilot() } }
|
||||
case .loading:
|
||||
PlanLoadingView(message: "Reading Copilot credentials...")
|
||||
case .failed:
|
||||
|
|
@ -2486,7 +2495,7 @@ private struct CopilotPlanInsight: View {
|
|||
title: "Refresh Copilot login",
|
||||
reason: reason,
|
||||
fallback: "Your Copilot sign-in has expired. Sign in via an editor's Copilot plugin again, then click Reconnect."
|
||||
) { Task { await store.bootstrapCopilot() } }
|
||||
) { Task { await store.connectCopilot() } }
|
||||
case let .usage(idle):
|
||||
if let usage = store.copilotUsage {
|
||||
loadedBody(usage: usage, idle: idle)
|
||||
|
|
|
|||
|
|
@ -1428,7 +1428,7 @@ private struct CopilotTokenSection: View {
|
|||
)
|
||||
token = ""
|
||||
CopilotSubscriptionService.resetProbeCache()
|
||||
await store.bootstrapCopilot()
|
||||
await store.connectCopilot()
|
||||
} catch {
|
||||
errorText = error.localizedDescription
|
||||
}
|
||||
|
|
@ -1506,8 +1506,12 @@ private struct CopilotConnectionRow: View {
|
|||
case .bootstrapping: return "Looking for a GitHub token on this Mac."
|
||||
case .loading: return "Background refresh in progress."
|
||||
case .dormant: return "Tap Load Quota to fetch live usage from api.github.com."
|
||||
case .notBootstrapped, .noCredentials:
|
||||
return "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."
|
||||
case .notBootstrapped:
|
||||
return CopilotQuotaPresentation.settingsNotConnectedDetail(
|
||||
explicitlyDisconnected: CopilotExplicitDisconnect.isSet(defaults: store.copilotQuotaRuntime.defaults)
|
||||
)
|
||||
case .noCredentials:
|
||||
return CopilotQuotaPresentation.noCredentialsSettingsDetail
|
||||
case .failed: return store.copilotError ?? ""
|
||||
}
|
||||
}
|
||||
|
|
@ -1529,13 +1533,13 @@ private struct CopilotConnectionRow: View {
|
|||
Text("CodeBurn will stop tracking Copilot quota. Every credential it read stays untouched, and your Copilot clients keep working.")
|
||||
}
|
||||
case .terminalFailure, .noCredentials, .failed:
|
||||
Button("Reconnect") { Task { await store.bootstrapCopilot() } }
|
||||
Button("Reconnect") { Task { await store.connectCopilot() } }
|
||||
.buttonStyle(.borderedProminent)
|
||||
case .dormant:
|
||||
Button("Load Quota") { Task { await store.bootstrapCopilot() } }
|
||||
Button("Load Quota") { Task { await store.connectCopilot() } }
|
||||
.buttonStyle(.borderedProminent)
|
||||
case .notBootstrapped:
|
||||
Button("Connect") { Task { await store.bootstrapCopilot() } }
|
||||
Button("Connect") { Task { await store.connectCopilot() } }
|
||||
.buttonStyle(.borderedProminent)
|
||||
case .bootstrapping:
|
||||
ProgressView().controlSize(.small)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue