From 591251c57bf81716ad0b19e85d2271346a6c2fcf Mon Sep 17 00:00:00 2001 From: Steven Thomas Souza Date: Fri, 27 Feb 2026 10:07:03 -0500 Subject: [PATCH] Fix inflated velocity calculation in Usage Pace display Replace active-only velocity (which only counted time when utilization changed) with average velocity (total % change / total elapsed time). This fixes wildly inflated burn rates like 429%/hr when only 23% has been consumed, making time remaining estimates accurate. Co-Authored-By: Claude Opus 4.6 --- Sources/PingClaude/UsageVelocityTracker.swift | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/Sources/PingClaude/UsageVelocityTracker.swift b/Sources/PingClaude/UsageVelocityTracker.swift index edc26af..fc842b3 100644 --- a/Sources/PingClaude/UsageVelocityTracker.swift +++ b/Sources/PingClaude/UsageVelocityTracker.swift @@ -385,29 +385,21 @@ class UsageVelocityTracker: ObservableObject { return Array(samples[sessionStartIndex...]) } - /// Active-only velocity: only counts time intervals where utilization actually changed. - /// This gives "burn rate while actively using" not "average rate including idle time". + /// Average velocity: total utilization change over total elapsed time. + /// Returns %/hr, or nil if insufficient data. private func computeVelocity(from windowSamples: [UsageSample]) -> Double? { - guard windowSamples.count >= 2 else { return nil } + guard windowSamples.count >= 2, + let first = windowSamples.first, + let last = windowSamples.last else { return nil } - var activeSeconds: TimeInterval = 0 - var activeDelta: Double = 0 - let changeThreshold: Double = 0.1 // minimum % change to count as active + let totalSeconds = last.timestamp.timeIntervalSince(first.timestamp) + guard totalSeconds > 60 else { return nil } // need >1 min of data - for i in 1.. 0 else { return nil } - if delta > changeThreshold { - activeSeconds += curr.timestamp.timeIntervalSince(prev.timestamp) - activeDelta += delta - } - } - - guard activeSeconds > 60, activeDelta > 0 else { return nil } // need >1 min of active data - let activeHours = activeSeconds / 3600 - return activeDelta / activeHours + let totalHours = totalSeconds / 3600 + return totalDelta / totalHours } // MARK: - Formatting Helpers