codeburn/mac/Sources/CodeBurnMenubar/QuotaRefreshDecision.swift
iamtoruk 8b3cb68255 fix(mac): keep dock quota fresh when the payload tick is skipped
The background tick skips the whole refresh when local session files are
unchanged, but quota is server-side state, so the dock rings went stale
until the popover was opened. Run the quota half on skipped ticks, still
throttled by the subscription cadence.

Also refresh the dock providers before the Claude/Codex awaits so a slow
provider cannot delay them, anchor lastCodexRefreshAt on attempt like its
siblings, and query display sleep live instead of latching a flag that a
missed wake notification leaves stuck.
2026-09-12 03:47:46 -07:00

25 lines
998 B
Swift

import Foundation
/// Scheduling decisions for the live quota refresh, kept out of the
/// AppKit-bound refresh loop so they are testable on their own.
enum QuotaRefreshDecision {
/// The CLI payload half of a tick is gated on local session-file mtimes.
/// Quota is server-side state (window resets, usage from other machines),
/// so a tick whose payload was skipped as unchanged still owes the quota
/// half a run; it self-throttles on SubscriptionRefreshCadence.
static func needsQuotaOnlyTick(payloadRefreshDue: Bool, payloadSkippedUnchanged: Bool) -> Bool {
payloadRefreshDue && payloadSkippedUnchanged
}
static func isDue(
force: Bool,
autoRefreshAllowed: Bool,
lastAttemptAt: Date?,
now: Date,
threshold: TimeInterval
) -> Bool {
if force { return true }
guard autoRefreshAllowed else { return false }
return now.timeIntervalSince(lastAttemptAt ?? .distantPast) >= threshold
}
}