fix(menubar): stop the dock resizing and claiming a refresh it is not doing

Reserving the pace slot only while connected made the panel relayout on
every refresh: the slot collapsed and the staleness band appeared. The
reservation is a property of the window shape, so it no longer asks the
connection. Windows block on the real Claude shapes, connected vs stale:
91/77 -> 91/91 at one and two windows, 164/136 -> 164/164 at three.

A Claude or Codex summary now reads stale on sample age alone, and the
manual cadence never refreshes, so "Last known usage - refreshing" and
the Settings "Refreshing..." became permanent lies. Both now ask whether
a fetch is actually in flight.
This commit is contained in:
Resham Joshi 2026-09-12 12:19:29 -07:00
parent 280c428b0e
commit d5e2d22880
7 changed files with 68 additions and 23 deletions

View file

@ -2342,6 +2342,19 @@ final class AppStore {
return nil
}
/// Whether a quota fetch for this provider is actually awaiting a response.
/// Claude and Codex now read `.stale` on sample age alone, which a manual
/// refresh cadence makes permanent, so the surfaces that say "refreshing"
/// have to ask. Every other provider still reaches `.stale` only from an
/// in-flight or just-failed fetch.
func quotaRefreshIsInFlight(for provider: CapacityDockProvider) -> Bool {
switch provider.legacyFilter {
case .claude: claudeRefreshInFlightRequest != nil
case .codex: codexRefreshInFlightRequest != nil
default: true
}
}
func capacityDockProviderIsConnected(_ provider: CapacityDockProvider) -> Bool {
guard let connection = capacityDockQuotaSummary(for: provider)?.connection else { return false }
return connection == .connected || connection == .stale

View file

@ -135,17 +135,14 @@ enum QuotaPacePresentation {
}
/// Whether the panel must reserve a pace slot under the window columns.
/// Deliberately time-independent: the slot is reserved whenever connected
/// data carries a displayable window with validated duration and a reset
/// date, even while the caption itself is still empty (window younger
/// than 3%). Reserving on data alone keeps the computed panel height from
/// changing under the pointer as wall-clock time crosses a threshold.
static func reservesLine(
for windows: [QuotaSummary.Window],
connection: QuotaSummary.Connection
) -> Bool {
guard connection == .connected else { return false }
return windows.contains { window in
/// Deliberately independent of both wall-clock time and connection state:
/// the slot is reserved whenever a displayed window carries a validated
/// duration and a reset date, even while the caption itself is empty (a
/// window younger than 3%, a stale sample, a refresh in flight). Reserving
/// on shape alone keeps the computed panel height from changing under the
/// pointer every time a refresh or a freshness horizon flips the caption.
static func reservesLine(for windows: [QuotaSummary.Window]) -> Bool {
windows.contains { window in
guard let seconds = window.windowSeconds, seconds > 0 else { return false }
return window.resetsAt != nil
}

View file

@ -165,7 +165,7 @@ enum CapacityDockGlance {
guard drawsWindows(quota) else { return false }
let shown = windows(quota)
guard !shown.isEmpty else { return false }
return QuotaPacePresentation.reservesLine(for: shown, connection: quota.connection)
return QuotaPacePresentation.reservesLine(for: shown)
}
/// The windows row's height for this quota: the plain row, or the row with
@ -1333,7 +1333,9 @@ struct CapacityDockDetailView: View {
.font(.system(size: 10))
.foregroundStyle(Color.capacityDockText.opacity(0.52))
case .stale:
Text("Last known usage · refreshing")
Text(store.quotaRefreshIsInFlight(for: provider)
? "Last known usage · refreshing"
: "Last known usage")
.font(.system(size: 10))
.foregroundStyle(.yellow.opacity(0.82))
case .transientFailure:

View file

@ -1956,7 +1956,7 @@ private struct GenericProviderConnectionSections: View {
switch summary.connection {
case .connected: return "Connected"
case .loading: return "Connecting…"
case .stale: return "Refreshing…"
case .stale: return store.quotaRefreshIsInFlight(for: provider) ? "Refreshing…" : "Connected"
case .transientFailure: return "Retrying"
case .terminalFailure: return "Reconnect required"
case .disconnected: return "Not connected"

View file

@ -85,7 +85,7 @@ struct CapacityDockGlanceTests {
#expect(CapacityDockGlance.windows(quota([])).isEmpty)
}
@Test("Pace slots reserve height only for connected windows with a validated duration")
@Test("Pace slots reserve height for any window with a validated duration")
func paceSlotReservedByDuration() {
let resetsAt = Date().addingTimeInterval(3 * 24 * 3600)
let withoutDuration = [window("5-hour", 0.2, resetsAt: resetsAt), window("Weekly", 0.5, resetsAt: resetsAt)]
@ -99,9 +99,17 @@ struct CapacityDockGlanceTests {
#expect(CapacityDockGlance.windowsHeight(for: quota(withoutDuration)) == CapacityDockGlance.windowsHeight)
let step = CapacityDockGlance.paceLineGap + CapacityDockGlance.paceLineHeight
#expect(CapacityDockGlance.windowsHeight(for: quota(withDuration)) == CapacityDockGlance.windowsHeight + step)
// Stale data carries the durations but no defensible estimate, so the
// slot is not reserved and the panel is shorter.
#expect(!CapacityDockGlance.drawsPace(quota(withDuration, connection: .stale)))
// Stale and loading data carry the durations but no defensible
// estimate. The slot stays reserved anyway: it is the caption that
// goes quiet, and the panel must not resize under the pointer every
// time a refresh starts or a sample ages out.
for connection in [QuotaSummary.Connection.stale, .loading] {
#expect(CapacityDockGlance.drawsPace(quota(withDuration, connection: connection)))
#expect(
CapacityDockGlance.windowsHeight(for: quota(withDuration, connection: connection))
== CapacityDockGlance.windowsHeight(for: quota(withDuration))
)
}
// The computed panel height reflects the reserved slot.
func detailHeight(_ windows: [QuotaSummary.Window], connection: QuotaSummary.Connection) -> CGFloat {
CapacityDockMetrics.detailHeight(

View file

@ -316,15 +316,14 @@ struct CapacityDockPacePresentationTests {
#expect(lines[1] == nil)
}
@Test("The panel reserves a slot on metadata, independent of wall-clock time")
@Test("The panel reserves a slot on metadata, independent of time and connection")
func reservationIsMetadataOnly() {
let resetsAt = f.now.addingTimeInterval(3 * 24 * 3600)
let eligible = [QuotaSummary.Window(label: "Weekly", percent: 0.5, resetsAt: resetsAt, windowSeconds: f.week, fetchedAt: f.now)]
let noDuration = [QuotaSummary.Window(label: "Weekly", percent: 0.5, resetsAt: resetsAt, fetchedAt: f.now)]
let noReset = [QuotaSummary.Window(label: "Weekly", percent: 0.5, resetsAt: nil, windowSeconds: f.week, fetchedAt: f.now)]
#expect(QuotaPacePresentation.reservesLine(for: eligible, connection: .connected))
#expect(!QuotaPacePresentation.reservesLine(for: noDuration, connection: .connected))
#expect(!QuotaPacePresentation.reservesLine(for: noReset, connection: .connected))
#expect(!QuotaPacePresentation.reservesLine(for: eligible, connection: .stale))
#expect(QuotaPacePresentation.reservesLine(for: eligible))
#expect(!QuotaPacePresentation.reservesLine(for: noDuration))
#expect(!QuotaPacePresentation.reservesLine(for: noReset))
}
}

View file

@ -202,6 +202,32 @@ struct QuotaFreshnessTests {
await gate.close()
}
@Test("an aged-out sample is not called refreshing unless a fetch is in flight")
func staleWithoutAFetchIsNotRefreshing() async {
let gate = CodexRefreshGate()
let store = AppStore()
store.codexUsage = codexUsage(fetchedAt: Date().addingTimeInterval(-QuotaSummary.freshnessThreshold - 1))
store.codexLoadState = .loaded
#expect(store.quotaSummary(for: .codex)?.connection == .stale)
// Manual cadence never refreshes, so this state is permanent.
#expect(store.quotaRefreshIsInFlight(for: .codex) == false)
store.codexQuotaBootstrapChecker = { true }
store.codexQuotaFetcher = { try await gate.wait() }
let refresh = Task { await store.refreshCodexReportingSuccess() }
guard await waitForWaiterCount(gate, 1) else {
await gate.close()
_ = await refresh.value
#expect(Bool(false), "refresh did not enter the controlled fetch")
return
}
#expect(store.quotaRefreshIsInFlight(for: .codex))
await gate.release(.success(nil))
_ = await refresh.value
#expect(store.quotaRefreshIsInFlight(for: .codex) == false)
await gate.close()
}
@Test("a cancelled refresh restores the prior state instead of reporting failure")
func cancelledRefreshRestoresPriorState() async {
let store = AppStore()