Skip to content

fix(grok): cache context breakdown like Codex/Claude - #382

Merged
xiufengsun merged 2 commits into
xiufengsun:mainfrom
Gyarados4157:fix/grok-context-breakdown-cache
Jul 28, 2026
Merged

fix(grok): cache context breakdown like Codex/Claude#382
xiufengsun merged 2 commits into
xiufengsun:mainfrom
Gyarados4157:fix/grok-context-breakdown-cache

Conversation

@Gyarados4157

@Gyarados4157 Gyarados4157 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Grok Context Breakdown was re-scanning every updates.jsonl whenever the dashboard switched provider tabs (Claude ↔ Codex ↔ Grok), so users with large Grok histories saw repeated “Scanning session logs…”.
  • Align Grok with the Codex/Claude approach:
    • Per-file parse cache (FILE_PARSE_CACHE, size+mtime keyed) — same idea as Codex PARSED_GROUP_CACHE
    • In-flight request coalescing so concurrent panel mounts share one scan
    • Result cache still keyed with inventory signature for correctness when files change
    • UI sticky cache in ContextBreakdownPanel so revisiting a source keeps the last payload instead of flashing the skeleton (loading && !data)

API surface

No API change. Still GET /functions/tokentracker-usage-category-breakdown?source=grok|codex|claude with the same response shape.

Test plan

  • node --test test/grok-context-breakdown.test.js (includes result cache, in-flight, file invalidation)
  • dashboard vitest ContextBreakdownPanel.test.jsx (sticky source revisit)
  • Manual: open Usage Overview → expand Grok → wait for first scan → switch to Claude/Codex → switch back to Grok (should not re-show long scan)

Summary by CodeRabbit

  • Bug Fixes
    • Prevented the context breakdown panel from flashing back to the loading state when revisiting a previously viewed source.
    • Avoided stale breakdown content by showing a loading hint when switching to an uncached source until fresh data is ready.
  • Performance
    • Improved repeated Grok context breakdown requests with stronger caching and automatic coalescing of duplicate in-flight computations.
    • Ensured breakdown totals update when underlying session/update data changes.
  • Tests
    • Added coverage for cached vs. uncached behavior and concurrency/deduplication.

Grok context breakdown re-scanned all session logs on every provider
tab switch because the result cache was brittle and files were never
memoized. Add Codex-style per-file parse cache, in-flight request
coalescing, and sticky UI state so revisiting Grok stays instant.
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 8dbd621d-c337-4c76-afe9-850e55245d70

📥 Commits

Reviewing files that changed from the base of the PR and between dc178bc and 4d4ec98.

📒 Files selected for processing (2)
  • dashboard/src/ui/dashboard/components/ContextBreakdownPanel.jsx
  • dashboard/src/ui/dashboard/components/__tests__/ContextBreakdownPanel.test.jsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • dashboard/src/ui/dashboard/components/ContextBreakdownPanel.jsx

📝 Walkthrough

Walkthrough

The PR adds versioned and memoized caching to Grok context aggregation, deduplicates concurrent computations, invalidates file parsing when session files change, and retains or clears dashboard breakdown data according to source-specific cache state during refreshes.

Changes

Context breakdown caching

Layer / File(s) Summary
Grok aggregation caching
src/lib/grok-context-breakdown.js, test/grok-context-breakdown.test.js
Grok results now use inventory-aware caching, in-flight request deduplication, bounded per-file parse caching, and tests for cache reuse, concurrency, and file-change invalidation.
Dashboard response caching
dashboard/src/ui/dashboard/components/ContextBreakdownPanel.jsx, dashboard/src/ui/dashboard/components/__tests__/ContextBreakdownPanel.test.jsx
Source- and range-keyed responses are restored before refetching, while uncached sources clear stale data; tests cover both pending-request behaviors.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Dashboard
  participant ContextBreakdownPanel
  participant getUsageCategoryBreakdown
  Dashboard->>ContextBreakdownPanel: Switch source or date range
  ContextBreakdownPanel->>ContextBreakdownPanel: Restore cached response or clear data
  ContextBreakdownPanel->>getUsageCategoryBreakdown: Fetch updated breakdown
  getUsageCategoryBreakdown-->>ContextBreakdownPanel: Return response
  ContextBreakdownPanel->>ContextBreakdownPanel: Cache and apply response
Loading

Possibly related PRs

Suggested reviewers: mm7894215

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main change: Grok context breakdown caching updated to match Codex/Claude behavior.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@dashboard/src/ui/dashboard/components/ContextBreakdownPanel.jsx`:
- Around line 702-706: Update the useEffect cache-miss path in
ContextBreakdownPanel so data is cleared before starting a fetch when
cacheRef.current[cacheKey] has no entry. Preserve cached-data rendering on cache
hits, and ensure the loading guard can show the skeleton instead of rendering
the previous source or date range payload.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: a9a1480c-039c-4b44-8769-b79bf9ea5da5

📥 Commits

Reviewing files that changed from the base of the PR and between 1c322a1 and dc178bc.

📒 Files selected for processing (4)
  • dashboard/src/ui/dashboard/components/ContextBreakdownPanel.jsx
  • dashboard/src/ui/dashboard/components/__tests__/ContextBreakdownPanel.test.jsx
  • src/lib/grok-context-breakdown.js
  • test/grok-context-breakdown.test.js

Comment thread dashboard/src/ui/dashboard/components/ContextBreakdownPanel.jsx Outdated
…flash

First visit to an uncached source kept the previous provider's payload in
state, so it briefly rendered under the new source's display branch with
mismatched totals. Reset data explicitly on cache miss so the loading
skeleton shows instead (CodeRabbit review finding).
@xiufengsun

Copy link
Copy Markdown
Owner

Pushed a follow-up commit addressing the CodeRabbit finding: on a cache miss the panel now resets data explicitly, so the previous source's payload can no longer flash under the new source's display branch while the fetch runs. Added a regression test for the uncached-source visit (12/12 vitest pass locally). Will merge once CI is green.

@xiufengsun

Copy link
Copy Markdown
Owner

CI green with the follow-up. Thanks for the caching work — the per-file parse cache plus in-flight coalescing is exactly the Codex/Claude pattern this needed. Merging.

@xiufengsun
xiufengsun merged commit 99379d0 into xiufengsun:main Jul 28, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants