Skip to content

fix: keep watch mode's tokens-by-model fresh when stats-cache.json is stale - #11

Merged
coldhighsun merged 1 commit into
mainfrom
fix/watch-models-stale-stats-cache
Aug 5, 2026
Merged

fix: keep watch mode's tokens-by-model fresh when stats-cache.json is stale#11
coldhighsun merged 1 commit into
mainfrom
fix/watch-models-stale-stats-cache

Conversation

@coldhighsun

Copy link
Copy Markdown
Owner

Summary

  • ClaudeUsageProvider.GetStatsCache only trusted stats-cache.json's day-granularity LastComputedDate, so it kept returning a stale snapshot all day even after new session activity, causing aimon watch models to look frozen while hours/today (which read transcripts directly) kept updating.
  • Freshness now also compares actual write timestamps against session transcripts, falling back to rebuilding from transcripts (StatsCacheBuilder) when they're newer.
  • Added SessionActivityTracker, fed incrementally by DataService's existing session-file FileSystemWatcher, 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.slnx
  • dotnet test AIUsageMonitor.slnx (33/33 passing, including new ClaudeUsageProviderTests covering both the stale-rebuild and fresh-reuse paths)
  • Manual: dotnet run --project src/AIUsageMonitor.Cli -- watch models --interval 5 while generating new usage in Claude Code, confirm tokens-by-model updates within a few seconds

… 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.
@coldhighsun
coldhighsun merged commit 87a7330 into main Aug 5, 2026
1 check passed
@coldhighsun
coldhighsun deleted the fix/watch-models-stale-stats-cache branch August 5, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant