From d5e2d22880f374004abb3520ae650dc1c1ec8205 Mon Sep 17 00:00:00 2001 From: Resham Joshi Date: Sat, 12 Sep 2026 12:19:29 -0700 Subject: [PATCH] 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. --- mac/Sources/CodeBurnMenubar/AppStore.swift | 13 ++++++++++ .../Data/QuotaPacePresentation.swift | 19 ++++++-------- .../Views/CapacityDockView.swift | 6 +++-- .../CodeBurnMenubar/Views/SettingsView.swift | 2 +- .../CapacityDockGlanceTests.swift | 16 +++++++++--- .../CapacityDockPacePresentationTests.swift | 9 +++---- .../QuotaFreshnessTests.swift | 26 +++++++++++++++++++ 7 files changed, 68 insertions(+), 23 deletions(-) diff --git a/mac/Sources/CodeBurnMenubar/AppStore.swift b/mac/Sources/CodeBurnMenubar/AppStore.swift index c7d4df43..60abf091 100644 --- a/mac/Sources/CodeBurnMenubar/AppStore.swift +++ b/mac/Sources/CodeBurnMenubar/AppStore.swift @@ -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 diff --git a/mac/Sources/CodeBurnMenubar/Data/QuotaPacePresentation.swift b/mac/Sources/CodeBurnMenubar/Data/QuotaPacePresentation.swift index 85bf7866..25d6c8a3 100644 --- a/mac/Sources/CodeBurnMenubar/Data/QuotaPacePresentation.swift +++ b/mac/Sources/CodeBurnMenubar/Data/QuotaPacePresentation.swift @@ -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 } diff --git a/mac/Sources/CodeBurnMenubar/Views/CapacityDockView.swift b/mac/Sources/CodeBurnMenubar/Views/CapacityDockView.swift index 98e10b0a..dafd0bb0 100644 --- a/mac/Sources/CodeBurnMenubar/Views/CapacityDockView.swift +++ b/mac/Sources/CodeBurnMenubar/Views/CapacityDockView.swift @@ -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: diff --git a/mac/Sources/CodeBurnMenubar/Views/SettingsView.swift b/mac/Sources/CodeBurnMenubar/Views/SettingsView.swift index 738cc5bf..5933c659 100644 --- a/mac/Sources/CodeBurnMenubar/Views/SettingsView.swift +++ b/mac/Sources/CodeBurnMenubar/Views/SettingsView.swift @@ -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" diff --git a/mac/Tests/CodeBurnMenubarTests/CapacityDockGlanceTests.swift b/mac/Tests/CodeBurnMenubarTests/CapacityDockGlanceTests.swift index 4a3c7ce6..438f591a 100644 --- a/mac/Tests/CodeBurnMenubarTests/CapacityDockGlanceTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/CapacityDockGlanceTests.swift @@ -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( diff --git a/mac/Tests/CodeBurnMenubarTests/CapacityDockPacePresentationTests.swift b/mac/Tests/CodeBurnMenubarTests/CapacityDockPacePresentationTests.swift index 83f4bdce..8431ce0e 100644 --- a/mac/Tests/CodeBurnMenubarTests/CapacityDockPacePresentationTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/CapacityDockPacePresentationTests.swift @@ -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)) } } diff --git a/mac/Tests/CodeBurnMenubarTests/QuotaFreshnessTests.swift b/mac/Tests/CodeBurnMenubarTests/QuotaFreshnessTests.swift index 08dcd6e6..b76512e9 100644 --- a/mac/Tests/CodeBurnMenubarTests/QuotaFreshnessTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/QuotaFreshnessTests.swift @@ -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()