codeburn/mac/Sources/CodeBurnMenubar/Data/CopilotQuotaPresentation.swift
ozymandiashh 8b5bd3ed4c fix(menubar): follow the credential's GitHub host for Copilot quota
Both Copilot quota readers hardcoded
https://api.github.com/copilot_internal/user and dropped the host their
credential was read from, so an enterprise on a GitHub Enterprise Cloud
data-residency host (<tenant>.ghe.com) could only ever report
"Temporarily unavailable": its token is not a dotcom token, and dotcom is
not an endpoint it may be sent to.

A discovered credential now carries its host. hosts.json is keyed by
GitHub host and newer apps.json files key by "<host>:<app id>", so the
token and the host always come from the same entry; the rungs that carry
no host of their own (an app-name apps.json key, COPILOT_GITHUB_TOKEN /
GH_TOKEN / GITHUB_TOKEN, gh auth token, a pasted token) mean dotcom. The
endpoint follows the host: api.github.com for github.com, and
api.<tenant>.ghe.com for an Enterprise Cloud host. With several hosts
signed in github.com wins, otherwise the first .ghe.com tenant in sorted
order, which keeps the pick stable across reads; with exactly one, that
host is used.

A host neither rule can address, such as a self-hosted GitHub Enterprise
Server install, fails with a message naming that host instead of falling
back to dotcom, and network and HTTP failures name the host that was
tried. macOS Settings now says which host answered in the Copilot
connection row. Tokens are still never logged and sanitizeForUI is
unchanged.

Closes #1286
2026-09-12 05:56:05 -07:00

88 lines
4 KiB
Swift

import Foundation
/// Pure display-decision helpers for the Copilot quota surfaces.
///
/// Copilot tokens are read straight from whichever signed-in client already
/// holds one, with no refresh path, so a revoked token sits in
/// `.terminalFailure` until the user signs in again. Like Kimi and
/// Gemini, the always-visible surfaces keep showing the last good snapshot
/// 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
case reconnect(reason: String?)
/// Render the loaded usage bars. `idle` is true when the login has
/// gone terminal but a snapshot is still on hand — the caller stamps a
/// quiet "sign in again" caption instead of hiding the data.
case usage(idle: Bool)
}
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:
return explicitlyDisconnected ? .disconnected : .noCredentials
case .noCredentials:
return .noCredentials
case .dormant, .bootstrapping:
return .loading
case .loading, .loaded:
return hasUsage ? .usage(idle: false) : .loading
case .failed:
return .failed
case .transientFailure:
return hasUsage ? .usage(idle: false) : .transientFailed
case .terminalFailure(let reason):
return hasUsage ? .usage(idle: true) : .reconnect(reason: reason)
}
}
/// Settings connection-row detail for a loaded snapshot. It always names
/// the host that answered, so a GitHub Enterprise Cloud tenant can see its
/// own `api.<tenant>.ghe.com` endpoint rather than a dotcom claim (#1286).
static func connectedSettingsDetail(plan: String?, apiHost: String) -> String {
let host = apiHost.isEmpty ? CopilotHostEndpoint.defaultAPIHost : apiHost
guard let plan, !plan.isEmpty else { return "Live quota tracked from \(host)." }
return "Plan: \(plan). Live quota tracked from \(host)."
}
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
static func isStale(fetchedAt: Date, now: Date = Date()) -> Bool {
now.timeIntervalSince(fetchedAt) > stalenessThreshold
}
}