mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-28 00:55:49 +00:00
fix(menubar): a twice-contradicting scoped payload threw instead of being served
When a scoped payload disagreed with its all-provider slice, the code re-ran the fetch bypassing the resident child and, if it STILL disagreed, threw. That verified payload is what the CLI actually reports for the provider, so throwing traded a real number for an error banner and left the tab with nothing at all. The payload is now cached with a contradictsAll flag on CachedPayload (add-only) and served, and the popover carries one subdued secondary line saying the total may be incomplete. The refresh paths treat an already-verified contradiction as usable, so a poll cannot re-run the one-shot parse forever.
This commit is contained in:
parent
68f6d1ced3
commit
cd7de30bc4
3 changed files with 91 additions and 24 deletions
|
|
@ -8,6 +8,10 @@ private let menubarPeriodDefaultsKey = "CodeBurnMenubarPeriod"
|
|||
struct CachedPayload {
|
||||
let payload: MenubarPayload
|
||||
let fetchedAt: Date
|
||||
/// This scoped payload still disagreed with its all-provider slice after a
|
||||
/// resident-bypassing one-shot re-parse. It is the best answer the CLI can
|
||||
/// give, so it is served rather than discarded, with the UI saying so.
|
||||
var contradictsAll: Bool = false
|
||||
var isFresh: Bool { Date().timeIntervalSince(fetchedAt) < cacheTTLSeconds }
|
||||
}
|
||||
|
||||
|
|
@ -41,14 +45,6 @@ struct PayloadCacheKey: Hashable {
|
|||
}
|
||||
}
|
||||
|
||||
private struct ProviderPayloadConsistencyError: Error, CustomStringConvertible {
|
||||
let provider: ProviderFilter
|
||||
|
||||
var description: String {
|
||||
"Fresh \(provider.rawValue) verification still contradicted the matching all-provider payload."
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Observable
|
||||
final class AppStore {
|
||||
|
|
@ -908,10 +904,28 @@ final class AppStore {
|
|||
}
|
||||
}
|
||||
|
||||
/// A cached payload the refresh paths may keep instead of refetching. A
|
||||
/// contradicting one is refetched; one already VERIFIED as contradicting is
|
||||
/// not, or every poll would re-run the expensive one-shot parse forever.
|
||||
private func cachedPayloadIsUsable(_ cached: CachedPayload, for key: PayloadCacheKey) -> Bool {
|
||||
cached.contradictsAll || !providerPayloadContradictsAll(cached.payload, for: key)
|
||||
}
|
||||
|
||||
private func consistentCachedPayload(for key: PayloadCacheKey) -> MenubarPayload? {
|
||||
guard let payload = cache[key]?.payload,
|
||||
!providerPayloadContradictsAll(payload, for: key) else { return nil }
|
||||
return payload
|
||||
guard let cached = cache[key] else { return nil }
|
||||
// A payload already verified by a resident-bypassing re-parse is served
|
||||
// even though it still disagrees: it is the CLI's real answer, and
|
||||
// `selectedPayloadMayBeIncomplete` is what tells the user so.
|
||||
if cached.contradictsAll { return cached.payload }
|
||||
guard !providerPayloadContradictsAll(cached.payload, for: key) else { return nil }
|
||||
return cached.payload
|
||||
}
|
||||
|
||||
/// The visible payload was verified against its all-provider slice and
|
||||
/// still disagreed. The popover shows one subdued line rather than an error.
|
||||
var selectedPayloadMayBeIncomplete: Bool {
|
||||
if cache[currentKey]?.contradictsAll == true { return true }
|
||||
return effectiveSelectedScope == .combined && cache[localCurrentKey]?.contradictsAll == true
|
||||
}
|
||||
|
||||
/// An all-provider payload and its scoped sibling describe the same period,
|
||||
|
|
@ -968,7 +982,7 @@ final class AppStore {
|
|||
includeOptimize: Bool,
|
||||
qualityOfService: QualityOfService,
|
||||
acceptAfter: (@Sendable () async -> Void)? = nil
|
||||
) async throws -> MenubarPayload {
|
||||
) async throws -> (payload: MenubarPayload, contradictsAll: Bool) {
|
||||
let fresh = try await DataClient.fetch(
|
||||
period: key.period,
|
||||
day: key.day,
|
||||
|
|
@ -984,7 +998,7 @@ final class AppStore {
|
|||
// two parses overlap and only the ACCEPTANCE is ordered. Waiting for it
|
||||
// before starting cost a full serialized parse on every tab click.
|
||||
await acceptAfter?()
|
||||
guard providerPayloadContradictsAll(fresh, for: key) else { return fresh }
|
||||
guard providerPayloadContradictsAll(fresh, for: key) else { return (fresh, false) }
|
||||
|
||||
NSLog("CodeBurn: resident %@ payload contradicted its all-provider slice; verifying with a one-shot parse",
|
||||
key.provider.rawValue)
|
||||
|
|
@ -999,10 +1013,17 @@ final class AppStore {
|
|||
bypassResident: true,
|
||||
qualityOfService: qualityOfService
|
||||
)
|
||||
guard !providerPayloadContradictsAll(verified, for: key) else {
|
||||
throw ProviderPayloadConsistencyError(provider: key.provider)
|
||||
// A second contradiction is not an error to show the user. The one-shot
|
||||
// parse bypassed the resident child, so this IS what the CLI reports for
|
||||
// this provider right now; throwing here replaced a real number with an
|
||||
// error banner and left the tab with nothing at all. Serve it, flagged,
|
||||
// and let the popover add one subdued line saying it may be incomplete.
|
||||
let stillContradicts = providerPayloadContradictsAll(verified, for: key)
|
||||
if stillContradicts {
|
||||
NSLog("CodeBurn: verified %@ payload still contradicted its all-provider slice; serving it as possibly incomplete",
|
||||
key.provider.rawValue)
|
||||
}
|
||||
return verified
|
||||
return (verified, stillContradicts)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
|
|
@ -1132,7 +1153,7 @@ final class AppStore {
|
|||
if !force,
|
||||
let cached = cache[key],
|
||||
cached.isFresh,
|
||||
!providerPayloadContradictsAll(cached.payload, for: key) { return true }
|
||||
cachedPayloadIsUsable(cached, for: key) { return true }
|
||||
// Join an in-flight fetch instead of reporting failure. Returning false
|
||||
// here made recoverFromStuckLoading() announce a failed recovery while a
|
||||
// perfectly healthy fetch was still running, which is what the quiet
|
||||
|
|
@ -1198,11 +1219,11 @@ final class AppStore {
|
|||
NSLog("CodeBurn: dropping fetch result for \(key.label)/\(key.provider.rawValue) — calendar rolled mid-fetch")
|
||||
return false
|
||||
}
|
||||
cache[key] = CachedPayload(payload: fresh, fetchedAt: Date())
|
||||
cache[key] = CachedPayload(payload: fresh.payload, fetchedAt: Date(), contradictsAll: fresh.contradictsAll)
|
||||
if key == currentKey || (effectiveSelectedScope == .combined && key == localCurrentKey) {
|
||||
selectionFallbackPayload = nil
|
||||
}
|
||||
reconcileClaudeConfigSelection(from: fresh, for: key)
|
||||
reconcileClaudeConfigSelection(from: fresh.payload, for: key)
|
||||
lastSuccessByKey[key] = Date()
|
||||
lastErrorByKey[key] = nil
|
||||
succeeded = true
|
||||
|
|
@ -1222,8 +1243,8 @@ final class AppStore {
|
|||
invalidateStaleDayCache()
|
||||
return false
|
||||
}
|
||||
cache[key] = CachedPayload(payload: fallback, fetchedAt: Date())
|
||||
reconcileClaudeConfigSelection(from: fallback, for: key)
|
||||
cache[key] = CachedPayload(payload: fallback.payload, fetchedAt: Date(), contradictsAll: fallback.contradictsAll)
|
||||
reconcileClaudeConfigSelection(from: fallback.payload, for: key)
|
||||
lastSuccessByKey[key] = Date()
|
||||
lastErrorByKey[key] = nil
|
||||
return true
|
||||
|
|
@ -1288,7 +1309,7 @@ final class AppStore {
|
|||
if !force,
|
||||
let cached = cache[key],
|
||||
cached.isFresh,
|
||||
!providerPayloadContradictsAll(cached.payload, for: key) { return true }
|
||||
cachedPayloadIsUsable(cached, for: key) { return true }
|
||||
if inFlightKeys[key] != nil {
|
||||
await waitForInFlight(key)
|
||||
return consistentCachedPayload(for: key) != nil
|
||||
|
|
@ -1320,8 +1341,8 @@ final class AppStore {
|
|||
invalidateStaleDayCache()
|
||||
return false
|
||||
}
|
||||
cache[key] = CachedPayload(payload: fresh, fetchedAt: Date())
|
||||
reconcileClaudeConfigSelection(from: fresh, for: key)
|
||||
cache[key] = CachedPayload(payload: fresh.payload, fetchedAt: Date(), contradictsAll: fresh.contradictsAll)
|
||||
reconcileClaudeConfigSelection(from: fresh.payload, for: key)
|
||||
lastSuccessByKey[key] = Date()
|
||||
lastErrorByKey[key] = nil
|
||||
} catch {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,14 @@ struct MenuBarContent: View {
|
|||
ScrollView(.vertical, showsIndicators: false) {
|
||||
VStack(spacing: 0) {
|
||||
HeroSection()
|
||||
if store.selectedPayloadMayBeIncomplete {
|
||||
Text("This total may be incomplete.")
|
||||
.font(.system(size: 11))
|
||||
.foregroundStyle(Color.secondary.opacity(0.75))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.bottom, 6)
|
||||
}
|
||||
Divider().opacity(0.5)
|
||||
PeriodSegmentedControl()
|
||||
ScopeSegmentedControl()
|
||||
|
|
|
|||
|
|
@ -229,4 +229,42 @@ struct AppStoreRefreshConcurrencyTests {
|
|||
#expect(await first.value)
|
||||
#expect(await second.value)
|
||||
}
|
||||
|
||||
@Test("a twice-contradicting payload is cached and flagged instead of raising an error")
|
||||
func twiceContradictingPayloadIsCachedAndFlagged() async {
|
||||
let script = ScriptedFetch()
|
||||
// The scoped payload reports nothing for a provider the all-provider
|
||||
// slice says is active, and says so again on the verifying re-parse.
|
||||
script.payloadForProvider = { provider in
|
||||
provider == .all
|
||||
? scriptedPayload(cost: 12, providers: ["claude": 11.5, "hermes agent": 0.5],
|
||||
providerDetails: [
|
||||
ProviderDetail(id: "claude", label: "Claude", cost: 11.5, calls: 4, hasUsage: true),
|
||||
ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0.5, calls: 7, hasUsage: true),
|
||||
])
|
||||
: scriptedPayload(cost: 0, calls: 0, providers: ["hermes agent": 0],
|
||||
providerDetails: [ProviderDetail(id: "hermes", label: "Hermes Agent", cost: 0, calls: 0, hasUsage: false)])
|
||||
}
|
||||
script.install()
|
||||
defer { script.uninstall() }
|
||||
|
||||
let store = AppStore()
|
||||
store.setCacheDateToTodayForTesting()
|
||||
store.suppressRefreshesForTesting()
|
||||
store.switchTo(provider: .hermes)
|
||||
|
||||
let task = Task { await store.refresh(includeOptimize: false, force: true) }
|
||||
// all + scoped + the resident-bypassing verification of the scoped one.
|
||||
#expect(await settle(until: { script.attempts.count >= 2 }))
|
||||
for index in 0..<script.attempts.count { script.release(index) }
|
||||
#expect(await settle(until: { script.attempts.count == 3 }))
|
||||
script.release(2)
|
||||
_ = await task.value
|
||||
|
||||
// Cached and served, with the caveat flag set. It used to throw, which
|
||||
// left the tab with an error banner and no number at all.
|
||||
#expect(store.cachedPayloadForTesting(period: .today, provider: .hermes) != nil)
|
||||
#expect(store.selectedPayloadMayBeIncomplete)
|
||||
#expect(store.lastError == nil)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue