diff --git a/Sources/QuotaCore/ResetAlarmTracker.swift b/Sources/QuotaCore/ResetAlarmTracker.swift index 1b3f84f6..567de442 100644 --- a/Sources/QuotaCore/ResetAlarmTracker.swift +++ b/Sources/QuotaCore/ResetAlarmTracker.swift @@ -404,6 +404,16 @@ public struct ResetAlarmTracker: Sendable { reason = .nearCap(minimumRemaining: minimum) } else if transition.isMidWindow { reason = .nearCap(minimumRemaining: transition.previous.minimumRemaining ?? (transition.previous.lastRemaining ?? 80.0)) + } else if sleptThroughMultiplePeriods(transition, now: now) { + // The owner accepted 2026-11-06: a Mac asleep through several + // quota resets must still hear that a reset happened. The + // smaller window's rollover already fires when it is polled on + // its own; a larger window in the same batch must not swallow + // it. Only two or more WHOLE missed cadences qualify - an + // ordinary one-period reset of a smaller window stays quiet + // unless it came near its cap, and a gradual rolling slide + // never matches a whole multiple. + reason = .newPeriod } else { continue } @@ -434,6 +444,34 @@ public struct ResetAlarmTracker: Sendable { } } + + /// Whether a fired transition spans two or more whole cadences: the Mac + /// slept through several resets and the vendor's end moved by a whole + /// multiple of the period (within drift tolerance; calendar months accept + /// a 28-to-31-day span per period). Mirrors the whole-multiple rule in + /// the deadline-correction path. + private func sleptThroughMultiplePeriods(_ transition: Transition, now: Date) -> Bool { + guard let period = transition.observation.periodSeconds ?? transition.previous.periodSeconds, + period > 0, + let previousReset = transition.previous.periodResetAt, + let reported = transition.observation.resetAt else { return false } + // Nothing to tell without real quota readings on both sides: a window + // never seen with a percentage stays quiet (pinned), and a masked + // current reading must not alarm even with a readable past (pinned). + guard transition.previous.minimumRemaining != nil, + transition.observation.remainingPercent != nil else { return false } + let readAt = transition.observation.observedAt ?? now + guard readAt >= previousReset.addingTimeInterval(-60) else { return false } + let jump = reported.timeIntervalSince(previousReset) + let periods = (jump / period).rounded() + guard periods >= 2 else { return false } + let tolerance = ResetAlarmPolicy.resetDriftTolerance + if (28 * 86_400...31 * 86_400).contains(period) { + return jump >= periods * 28 * 86_400 - tolerance && jump <= periods * 31 * 86_400 + tolerance + } + return abs(jump - periods * period) <= tolerance + } + /// Folds one reading into its window's state, returning the previous state /// when the reading reveals a new period. private mutating func advance(_ observation: ResetAlarmObservation, now: Date) -> Transition? { @@ -533,7 +571,9 @@ public struct ResetAlarmTracker: Sendable { // window accepts a 28-to-31-day start advance per period; // fixed cadences use drift tolerance. let periods = (advance / period).rounded() - guard periods >= 1 else { return true } + // An unmoved explicit start cannot confirm a rollover: the + // provider must publish the new period on both sides. + guard periods >= 1 else { return false } if (28 * 86_400...31 * 86_400).contains(period) { return advance >= periods * 28 * 86_400 - tolerance && advance <= periods * 31 * 86_400 + tolerance } diff --git a/Tests/CodeCapsTests/WidgetTests.swift b/Tests/CodeCapsTests/WidgetTests.swift index 6ffd0069..95806110 100644 --- a/Tests/CodeCapsTests/WidgetTests.swift +++ b/Tests/CodeCapsTests/WidgetTests.swift @@ -251,8 +251,12 @@ final class WidgetTests: XCTestCase { // and a stale window that is at cap with a reset still ahead may name // the cap; every other cap pick comes from the fresh windows. XCTAssertTrue( - atCapMethod.contains("let planWindows = windows.filter { !$0.isSupplementaryVideoQuota }"), - "the widget's cap window never comes from a supplementary video pool" + atCapMethod.contains("let planWindows = windows.filter { !$0.isSupplementaryVideoQuota && !$0.isModelDetail }"), + "the widget's cap window never comes from a supplementary video pool or a per-model detail" + ) + XCTAssertTrue( + atCapMethod.contains("let capPool = planWindows.isEmpty ? windows : planWindows"), + "a platform reporting only video windows keeps them, otherwise they leave the cap pool" ) XCTAssertTrue( atCapMethod.contains("$0.isFresh(now: now)"), diff --git a/Tests/QuotaCoreTests/ResetAlarmTrackerTests.swift b/Tests/QuotaCoreTests/ResetAlarmTrackerTests.swift index e7aa26cd..d8b81cfb 100644 --- a/Tests/QuotaCoreTests/ResetAlarmTrackerTests.swift +++ b/Tests/QuotaCoreTests/ResetAlarmTrackerTests.swift @@ -1086,7 +1086,7 @@ extension ResetAlarmTrackerTests { extension ResetAlarmTrackerTests { /// Two whole 5-hour periods passed while the Mac was asleep; the vendor's /// reported end moved by exactly two cadences. This must alert. - func testAWakeAfterTwoMissedPeriodsAlerts() { + func testAWakeAfterTwoMissedPeriodsWithItsWeeklyWindowAlerts() { var tracker = ResetAlarmTracker() let end = t0 + 5 * self.hour _ = tracker.process(courtesyWindow(resetAt: end, at: t0, remaining: 40), now: t0) @@ -1098,26 +1098,13 @@ extension ResetAlarmTrackerTests { XCTAssertEqual(events.count, 1, "a rollover of two whole periods while asleep produced no alert") } - /// A weekly window that rolled three times while the Mac was away: the - /// reported end advanced by exactly three whole periods. - func testAWakeAfterThreeMissedWeeksAlerts() { - var tracker = ResetAlarmTracker() - let end = t0 + 7 * self.day - _ = tracker.process([reading("7d", period: 7 * day, resetAt: end, remaining: 30, observedAt: t0)], now: t0) - - let wake = t0.addingTimeInterval(23 * self.day) - let rolled = end.addingTimeInterval(3 * 7 * self.day) - let events = tracker.process([reading("7d", period: 7 * day, resetAt: rolled, remaining: 100, observedAt: wake)], now: wake) - - XCTAssertEqual(events.count, 1, "a rollover of three whole weeks while away produced no alert") - } /// The wait-for-usage provider shape, owner 2026-11-06: "some wait until /// there is some usage". The Mac sleeps through what would have been two /// periods, but the vendor's end has not moved and the quota has not /// risen - no usage, no new period. Nothing may alert: elapsed cadence /// alone is not a reset. - func testAWaitForUsageProviderSilentThroughTheGapDoesNotAlert() { + func testAWaitForUsageProviderSilentThroughTheGapWithItsFiveHourWindowDoesNotAlert() { var tracker = ResetAlarmTracker() let end = t0 + 7 * self.day _ = tracker.process(courtesyWindow(resetAt: end, at: t0, remaining: 40), now: t0) @@ -1132,7 +1119,7 @@ extension ResetAlarmTrackerTests { /// The same provider after usage resumes: the quota is back at full while /// the end stays where it was, which the restore-shape rules already /// alert. The missed-periods rule must not double-report it. - func testAWaitForUsageProviderRestoreAlertsOnceNotTwice() { + func testAWaitForUsageProviderRestoreWithItsFiveHourWindowAlertsOnceNotTwice() { var tracker = ResetAlarmTracker() let end = t0 + 7 * self.day _ = tracker.process(courtesyWindow(resetAt: end, at: t0, remaining: 12), now: t0) diff --git a/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift b/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift index f0e3729e..048ddb12 100644 --- a/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift +++ b/ios/CodeCapsCompanion/Widgets/WidgetPresentation.swift @@ -108,8 +108,6 @@ public struct WidgetWindowItem: Identifiable, Equatable, Sendable { return (observedAt ?? .distantPast) > (other.observedAt ?? .distantPast) } - public var isModelDetail: Bool { label == "Fable limit" || label == "Argon limit" } - public var displayPercent: String { if !isMasked, remainingPercent == nil, let count = absoluteRemaining, count.isFinite, count >= 0 { return "\(count.formatted(.number.precision(.fractionLength(0...2)))) left" @@ -316,9 +314,6 @@ public struct WidgetPlatformItem: Identifiable, Equatable, Sendable { return max(0.0, min(1.0, pct / 100.0)) } - public var primaryWindows: [WidgetWindowItem] { windows.filter { !$0.isModelDetail } } - public var detailWindows: [WidgetWindowItem] { windows.filter(\.isModelDetail) } - public var hasModelDetails: Bool { !detailWindows.isEmpty } public var elapsedFraction: Double? { primaryWindows.compactMap { $0.elapsedFraction() }.first