fix: keep watch mode's tokens-by-model fresh when stats-cache.json is stale - #11
Merged
Merged
Conversation
… stale ClaudeUsageProvider.GetStatsCache only compared stats-cache.json's LastComputedDate (day granularity) to decide freshness, so it trusted Claude Code's own cache file for an entire day even if session transcripts had newer activity. This made the watch models view (and anything else sourced from StatsCache.ModelUsage) appear frozen while hours/today, which read transcripts directly, kept updating. Now freshness also compares actual write timestamps, falling back to rebuilding from session transcripts when they're newer than stats-cache.json. To avoid re-scanning the projects directory on every check, a new SessionActivityTracker maintains the latest session write time incrementally from DataService's existing FileSystemWatcher events instead of repeated directory scans.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ClaudeUsageProvider.GetStatsCacheonly trustedstats-cache.json's day-granularityLastComputedDate, so it kept returning a stale snapshot all day even after new session activity, causingaimon watch modelsto look frozen whilehours/today(which read transcripts directly) kept updating.StatsCacheBuilder) when they're newer.SessionActivityTracker, fed incrementally byDataService's existing session-fileFileSystemWatcher, so the freshness check is an O(1) in-memory read instead of a full directory re-scan on every call.Test plan
dotnet build AIUsageMonitor.slnxdotnet test AIUsageMonitor.slnx(33/33 passing, including newClaudeUsageProviderTestscovering both the stale-rebuild and fresh-reuse paths)dotnet run --project src/AIUsageMonitor.Cli -- watch models --interval 5while generating new usage in Claude Code, confirm tokens-by-model updates within a few seconds