mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-05-18 06:04:36 +00:00
Some checks are pending
CI / semgrep (push) Waiting to run
* Gate Claude OAuth refresh attempts on terminal failures
Anthropic returns invalid_grant (HTTP 400) when the user's refresh token has
been revoked or rotated, typically after they re-ran claude login on another
device. The previous code rethrew the raw error every refresh cycle, leaving
the Plan UI stuck on a Swift error string and pummeling Anthropic's token
endpoint forever.
The new SubscriptionRefreshGate captures a fingerprint of
~/.claude/.credentials.json on terminal failure and stops trying until that
fingerprint changes (the user re-logs-in). Transient 5xx/network failures
get exponential backoff capped at 6 hours.
Two new SubscriptionError cases let the UI distinguish "user must reconnect"
from "Anthropic is flaky right now" and show a clean reconnect CTA instead
of raw HTTP guts.
* Inline live-quota progress bar inside each AgentTab chip
When a provider exposes a live quota source, the AgentTab chip grows by ~3pt
to host a thin weekly-utilization bar directly under the label. Hovering the
chip reveals a popover with all four Anthropic windows (5-hour, weekly, weekly
Opus, weekly Sonnet) plus reset countdowns. Click still switches the tab as
before.
Today only Claude has a quota source (the existing /api/oauth/usage path);
other providers' chips render unchanged. The QuotaSummary abstraction lets
us bolt on Cursor/Copilot/Codex meters in follow-up commits.
Subscription is now refreshed eagerly on the periodic loop so the bar lights
up without forcing the user to open a deep view first. The previous
SubscriptionRefreshGate keeps a dead refresh token from spamming Anthropic.
Adds two new SubscriptionLoadState cases (terminalFailure, transientFailure)
so the deep Plan view shows a "reconnect" message instead of a raw Swift
error string when the user's claude login expired.
* Replace SubscriptionClient with credential-store + service architecture
The previous SubscriptionClient never persisted refreshed access tokens, so
every 30s tick read the expired token from Keychain, refreshed it (1 call),
fetched usage with the new token (2nd call), and threw the new token away —
3 API calls per cycle, which burned through Anthropic's per-account rate
budget and produced the 429s and `invalid_grant` loops users were seeing.
The replacement mirrors CodexBar's proven pattern:
- ClaudeCredentialStore owns the credential lifecycle. Bootstrap is strictly
user-initiated (Connect button in the Plan tab); the menubar does not touch
Claude's keychain at startup. After bootstrap, refreshed tokens — including
rotated refresh tokens — are persisted to a local cache file under
~/Library/Application Support/CodeBurn (mode 0600). Using a file instead of
our own keychain item means rebuild signature changes don't trigger a
startup keychain prompt; the only prompt the user ever sees is the one for
Claude Code-credentials on Connect.
- ClaudeUsageFetcher (folded into the service) is a pure /api/oauth/usage
call with one allowed 401-recovery roundtrip. 429s record an explicit
backoff window honouring Retry-After.
- ClaudeSubscriptionService orchestrates bootstrap / refresh / disconnect,
applies the 429 backoff, and surfaces terminal vs transient failures so
the UI can show the right CTA.
- Reading Claude's keychain now tries the entry keyed by NSUserName() first
and falls back to the unscoped query, so users who re-ran /login and ended
up with two Claude Code-credentials items pick up the fresh one. This was
the actual cause of "I logged in but the menubar still shows stale data".
User-facing additions:
- A proper Settings window (right-click → Settings…) with General / Claude /
About tabs. Provider quota cadence is configurable (Manual / 1m / 2m / 5m /
15m). New providers plug in as additional tabs.
- Plan tab: notBootstrapped → "Connect Claude subscription" CTA;
terminalFailure → "Reconnect Claude" with the correct /login instruction
for Claude Code 2.1; transientFailure preserves the last loaded view with
a retrying badge.
- AgentTab quota bar slot is always reserved so chip height doesn't jitter
when the user connects for the first time. Hover popover has 250ms enter
/ 150ms exit debounce so swiping across chips doesn't pop a popover for
every chip touched.
- Disconnect requires confirmation, clears capacityEstimates and the
subscription snapshot store so a reconnect under a different account
doesn't surface "Based on last cycle" projections from the old account.
Validator findings applied: cadence anchor only updates on successful
refresh (not every attempt), refresh-token rotation persists in memory
before keychain write so a write failure doesn't lock the user out, server
error bodies are sanitized (token redaction + 240-char cap) before they
reach the UI or NSLog, and Refresh Now refreshes both the menubar payload
and quota.
* Add Codex live quota + multi-provider warning, with validator fixes
CodexCredentialStore reads ~/.codex/auth.json (ChatGPT-mode only) on
user-initiated Connect, caches under Application Support like Claude.
CodexSubscriptionService hits chatgpt.com/backend-api/wham/usage with
the bearer token + ChatGPT-Account-Id header, parses primary/secondary
windows, additional per-model rate limits (e.g. GPT-5.3-Codex-Spark),
and credits balance with a Double-or-String fallback.
Plan-tier enum captures the full ChatGPT plan list including prolite,
free_workspace, education, quorum, k12, plus an unknown(String) case
that preserves the raw plan name when OpenAI ships a tier we haven't
mapped yet.
Multi-provider warning system:
- Menubar flame tints from neutral to yellow (70%) → orange (90%) →
red (100%) based on the worst-affected connected provider's worst
window. Uses NSImage.SymbolConfiguration palette colors.
- Popover header gains a warning row when any provider is at 70%+.
"Claude 79% of quota used", "Claude 79% · Codex 92%", or
"Claude over limit (105%)" when severity hits .danger.
- Hover popover gains a plan-name badge in the top-right corner so
users know which subscription is feeding the bar.
- Codex chip surfaces the credits balance and any non-zero per-model
additional rate limits as footer rows.
Validator fixes applied in the same commit:
- Provider-specific reconnect / disconnected copy in QuotaDetailPopover
(was hardcoded to Claude).
- Generation-token guard on refreshSubscriptionReportingSuccess and
refreshCodexReportingSuccess so a Disconnect during an in-flight
fetch can't resume after the await and re-populate the cleared state.
- Codex codexQuotaSummary promotes secondary to primary when only one
window is returned, so free / guest tiers don't render an empty bar.
- Memory-cache TTL is now actually consulted in currentRecord (the
isFresh check was dead code, leaving cached records valid forever).
- sanitizeForUI now redacts OpenAI sk-* keys, JWT tokens, and Bearer
headers in addition to Claude sk-ant-*.
- Removed diagnostic NSLog that wrote raw chatgpt.com response bodies
to the unified log.
- Codex Connect / Reconnect copy in Settings explains the auth.json
prerequisite and the API-key vs ChatGPT-mode distinction.
- Disconnect dialogs now state explicitly that the auth.json /
credentials keychain entry is left untouched.
- Plan badge in the popover gets line-limit + truncation + max-width
so a long unknown plan name can't overflow the row.
- Renamed shadowing `let max` to `let worst` in aggregateQuotaStatus.
* Add Codex Plan tab + size plan badge to content
The Plan tab is now visible when the Codex chip is selected, mirroring
the Claude tab's deep view. CodexPlanInsight renders the user's plan
tier ("Pro Lite", "Plus", etc.), the primary and secondary rate-limit
windows with reset countdowns, and any non-zero per-model additional
limits (e.g. GPT-5.3-Codex-Spark) so power users see them.
The "On pace at reset" projection that Claude's Plan view shows is not
included here — that math feeds from local Claude per-message spend
extrapolated against API quota windows, and our local Codex spend is
not a 1:1 signal for the ChatGPT-subscription rate windows reported by
wham/usage. Wiring a Codex extrapolator is a follow-up.
Drop the maxWidth=90 frame on the plan badge in the hover popover. It
was stretching short labels like "Pro Lite" to fill the full 90pt slot;
fixedSize makes the badge hug the text. Plan names are bounded short
strings, so truncation is a non-issue in practice.
234 lines
9.2 KiB
Swift
234 lines
9.2 KiB
Swift
import Foundation
|
|
|
|
/// Orchestrates "given a credential record, fetch live quota from Anthropic
|
|
/// and surface a result the UI can render". All token persistence lives in
|
|
/// `ClaudeCredentialStore`; the only state this service holds is the
|
|
/// 429 backoff window for the usage endpoint.
|
|
enum ClaudeSubscriptionService {
|
|
private static let usageURL = URL(string: "https://api.anthropic.com/api/oauth/usage")!
|
|
private static let betaHeader = "oauth-2025-04-20"
|
|
private static let userAgent = "claude-code/2.1.0"
|
|
private static let usageBlockedUntilKey = "codeburn.claude.usage.blockedUntil"
|
|
|
|
enum FetchError: Error, LocalizedError {
|
|
case notBootstrapped
|
|
case bootstrapFailed(ClaudeCredentialStore.StoreError)
|
|
case rateLimited(retryAt: Date)
|
|
case usageHTTPError(Int, String?)
|
|
case usageDecodeFailed
|
|
case network(Error)
|
|
case credential(ClaudeCredentialStore.StoreError)
|
|
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .notBootstrapped:
|
|
return "Connect Claude in the Plan tab to start tracking quota."
|
|
case let .bootstrapFailed(err):
|
|
return err.errorDescription
|
|
case let .rateLimited(retryAt):
|
|
let f = RelativeDateTimeFormatter()
|
|
f.unitsStyle = .short
|
|
return "Anthropic rate-limited the quota endpoint. Retrying \(f.localizedString(for: retryAt, relativeTo: Date()))."
|
|
case let .usageHTTPError(code, body):
|
|
return "Quota fetch failed (HTTP \(code))\(body.map { ": \($0)" } ?? "")"
|
|
case .usageDecodeFailed:
|
|
return "Quota response was malformed."
|
|
case let .network(err):
|
|
return "Network error: \(err.localizedDescription)"
|
|
case let .credential(err):
|
|
return err.errorDescription
|
|
}
|
|
}
|
|
|
|
/// True when the user must take action (re-run claude/login or click
|
|
/// Reconnect). Drives the red "Reconnect" UI path.
|
|
var isTerminal: Bool {
|
|
if case let .credential(err) = self { return err.isTerminal }
|
|
if case let .bootstrapFailed(err) = self { return err.isTerminal }
|
|
return false
|
|
}
|
|
|
|
var rateLimitRetryAt: Date? {
|
|
if case let .rateLimited(retryAt) = self { return retryAt }
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MARK: - Public API
|
|
|
|
/// User-initiated. Reads Claude's keychain (PROMPTS), copies to our keychain,
|
|
/// then fetches usage. Idempotent — safe to call again to "reconnect".
|
|
static func bootstrap() async throws -> SubscriptionUsage {
|
|
let record: ClaudeCredentialStore.CredentialRecord
|
|
do {
|
|
record = try ClaudeCredentialStore.bootstrap()
|
|
} catch let err as ClaudeCredentialStore.StoreError {
|
|
throw FetchError.bootstrapFailed(err)
|
|
}
|
|
return try await fetchWithRecord(initial: record)
|
|
}
|
|
|
|
/// Background refresh. Never prompts. Returns nil if not yet bootstrapped.
|
|
static func refreshIfBootstrapped() async throws -> SubscriptionUsage? {
|
|
guard ClaudeCredentialStore.isBootstrapCompleted else {
|
|
return nil
|
|
}
|
|
|
|
// Honour an outstanding rate-limit window — we recorded a 429 recently
|
|
// and Anthropic told us when to come back.
|
|
if let until = usageBlockedUntil(), until > Date() {
|
|
throw FetchError.rateLimited(retryAt: until)
|
|
}
|
|
|
|
do {
|
|
let token = try await ClaudeCredentialStore.freshAccessToken()
|
|
guard let token else { throw FetchError.notBootstrapped }
|
|
return try await fetch(token: token, allowOne401Recovery: true)
|
|
} catch let err as ClaudeCredentialStore.StoreError {
|
|
throw FetchError.credential(err)
|
|
} catch let err as FetchError {
|
|
throw err
|
|
}
|
|
}
|
|
|
|
/// Reset everything — used on user-initiated disconnect.
|
|
static func disconnect() {
|
|
ClaudeCredentialStore.resetBootstrap()
|
|
clearUsageBlock()
|
|
}
|
|
|
|
// MARK: - Internal
|
|
|
|
private static func fetchWithRecord(initial record: ClaudeCredentialStore.CredentialRecord) async throws -> SubscriptionUsage {
|
|
do {
|
|
return try await fetch(token: record.accessToken, allowOne401Recovery: true)
|
|
} catch let err as FetchError {
|
|
throw err
|
|
} catch let err as ClaudeCredentialStore.StoreError {
|
|
throw FetchError.credential(err)
|
|
}
|
|
}
|
|
|
|
private static func fetch(token: String, allowOne401Recovery: Bool) async throws -> SubscriptionUsage {
|
|
var request = URLRequest(url: usageURL)
|
|
request.httpMethod = "GET"
|
|
request.timeoutInterval = 30
|
|
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
|
request.setValue("application/json", forHTTPHeaderField: "Accept")
|
|
request.setValue(betaHeader, forHTTPHeaderField: "anthropic-beta")
|
|
request.setValue(userAgent, forHTTPHeaderField: "User-Agent")
|
|
|
|
let data: Data
|
|
let response: URLResponse
|
|
do {
|
|
(data, response) = try await URLSession.shared.data(for: request)
|
|
} catch {
|
|
throw FetchError.network(error)
|
|
}
|
|
guard let http = response as? HTTPURLResponse else {
|
|
throw FetchError.usageHTTPError(-1, nil)
|
|
}
|
|
|
|
switch http.statusCode {
|
|
case 200:
|
|
clearUsageBlock()
|
|
do {
|
|
let decoded = try JSONDecoder().decode(UsageResponse.self, from: data)
|
|
let tier = try ClaudeCredentialStore.subscriptionTier()
|
|
return mapResponse(decoded, rawTier: tier)
|
|
} catch {
|
|
throw FetchError.usageDecodeFailed
|
|
}
|
|
case 401:
|
|
if allowOne401Recovery {
|
|
let newToken = try await ClaudeCredentialStore.refreshAfter401()
|
|
return try await fetch(token: newToken, allowOne401Recovery: false)
|
|
}
|
|
throw FetchError.usageHTTPError(401, String(data: data, encoding: .utf8))
|
|
case 429:
|
|
let body = String(data: data, encoding: .utf8)
|
|
let retryAfter = parseRetryAfter(body: body)
|
|
let until = recordUsageRateLimit(retryAfterSeconds: retryAfter)
|
|
throw FetchError.rateLimited(retryAt: until)
|
|
default:
|
|
throw FetchError.usageHTTPError(http.statusCode, String(data: data, encoding: .utf8))
|
|
}
|
|
}
|
|
|
|
// MARK: - 429 backoff
|
|
|
|
private static func usageBlockedUntil() -> Date? {
|
|
UserDefaults.standard.object(forKey: usageBlockedUntilKey) as? Date
|
|
}
|
|
|
|
private static func clearUsageBlock() {
|
|
UserDefaults.standard.removeObject(forKey: usageBlockedUntilKey)
|
|
}
|
|
|
|
@discardableResult
|
|
private static func recordUsageRateLimit(retryAfterSeconds: Int?) -> Date {
|
|
let seconds = max(retryAfterSeconds ?? 300, 60)
|
|
let until = Date().addingTimeInterval(TimeInterval(seconds))
|
|
UserDefaults.standard.set(until, forKey: usageBlockedUntilKey)
|
|
return until
|
|
}
|
|
|
|
private static func parseRetryAfter(body: String?) -> Int? {
|
|
guard let body, let data = body.data(using: .utf8) else { return nil }
|
|
if let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
|
|
if let n = json["retry_after"] as? Int { return n }
|
|
if let s = json["retry_after"] as? String, let n = Int(s) { return n }
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MARK: - Response mapping
|
|
|
|
private struct UsageResponse: Decodable {
|
|
let fiveHour: Window?
|
|
let sevenDay: Window?
|
|
let sevenDayOpus: Window?
|
|
let sevenDaySonnet: Window?
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case fiveHour = "five_hour"
|
|
case sevenDay = "seven_day"
|
|
case sevenDayOpus = "seven_day_opus"
|
|
case sevenDaySonnet = "seven_day_sonnet"
|
|
}
|
|
}
|
|
|
|
private struct Window: Decodable {
|
|
let utilization: Double?
|
|
let resetsAt: String?
|
|
enum CodingKeys: String, CodingKey {
|
|
case utilization
|
|
case resetsAt = "resets_at"
|
|
}
|
|
}
|
|
|
|
private static func mapResponse(_ r: UsageResponse, rawTier: String?) -> SubscriptionUsage {
|
|
SubscriptionUsage(
|
|
tier: SubscriptionUsage.tier(from: rawTier),
|
|
rawTier: rawTier,
|
|
fiveHourPercent: r.fiveHour?.utilization,
|
|
fiveHourResetsAt: parseDate(r.fiveHour?.resetsAt),
|
|
sevenDayPercent: r.sevenDay?.utilization,
|
|
sevenDayResetsAt: parseDate(r.sevenDay?.resetsAt),
|
|
sevenDayOpusPercent: r.sevenDayOpus?.utilization,
|
|
sevenDayOpusResetsAt: parseDate(r.sevenDayOpus?.resetsAt),
|
|
sevenDaySonnetPercent: r.sevenDaySonnet?.utilization,
|
|
sevenDaySonnetResetsAt: parseDate(r.sevenDaySonnet?.resetsAt),
|
|
fetchedAt: Date()
|
|
)
|
|
}
|
|
|
|
private static func parseDate(_ s: String?) -> Date? {
|
|
guard let s, !s.isEmpty else { return nil }
|
|
let f = ISO8601DateFormatter()
|
|
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
if let d = f.date(from: s) { return d }
|
|
f.formatOptions = [.withInternetDateTime]
|
|
return f.date(from: s)
|
|
}
|
|
}
|