Skip to content

feat(app): show context cache hit rate#910

Merged
Astro-Han merged 3 commits into
devfrom
pawwork/issue-884-cache-hit-rate-flat
May 26, 2026
Merged

feat(app): show context cache hit rate#910
Astro-Han merged 3 commits into
devfrom
pawwork/issue-884-cache-hit-rate-flat

Conversation

@Astro-Han

@Astro-Han Astro-Han commented May 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • Show a computed Cache Hit Rate row in the Context tab instead of requiring users to interpret raw cache read/write tokens.
  • Keep raw cache read/write values visibly available below the percentage and add a focused Context tab snap that verifies the 90% display path in default and wide panel states.
  • Extend the e2e LLM fixture usage payload so chat and Responses API test paths can preserve cached input tokens.

Why

Issue #884 asks for cache hit rate visibility in the Context tab. The raw cache token row made users do the math manually and made cache behavior harder to observe during cache-first work.

Related Issue

Fixes #884

Human Review Status

Pending

Review Focus

  • Confirm the hit-rate formula matches the intended display contract: cache read / (input + cache read + cache write), rounded to a whole percent.
  • Confirm the visible raw cache token line is the right density/accessibility tradeoff for Context tab stats.
  • Confirm the e2e fixture cacheRead extension is narrow enough for both chat and Responses API test seeding.

Risk Notes

  • This changes a visible Context tab stat label/value and adds semantic color for cache hit rate.
  • Platform checklist item is left unticked because this does not touch platform, packaging, updater, signing, paths, shell behavior, or permissions.
  • Docs/release/dependency checklist item is left unticked because this does not touch docs, release notes, dependencies, credentials, deletion behavior, or generated content.

How To Verify

Focused app tests: bun test --preload ./happydom.ts ./src/components/session/session-context-metrics.test.ts ./src/components/session/session-context-breakdown.test.ts — 11 passed.
Responses fixture regression: bun test test/session/processor-effect.test.ts -t "preserves cached input tokens" — 1 passed; RED first showed input=1000 before cacheRead was preserved.
Typecheck: bun run typecheck — passed.
Lint: bun eslint "packages/app/src/components/session/session-context-metrics.ts" "packages/app/src/components/session/session-context-tab.tsx" — passed; follow-up lint on session-context-tab.tsx passed.
Visual snap: bun run snap session-context-cache — passed and wrote docs/design/preview/screenshots/session-context-cache.png showing default-width and wide two-column Context tab states with Cache Hit Rate = 90% and visible raw Cache Tokens (read/write): 900 / 0.
Diff check: git diff --cached --check — passed before commits.

Screenshots or Recordings

  • docs/design/preview/screenshots/session-context-cache.png from bun run snap session-context-cache.

Checklist

How to use this checklist:

  • Tick a box by replacing [ ] with [x]. Do not edit, add, or remove items.
  • The bot-applied label items can only be honestly ticked AFTER the PR is opened and the labeler / priority-triage bots have run — return to the PR description and tick them then.
  • Most items are required. The few that are conditional are explicitly marked (conditional); for those, leave unticked if they truly do not apply and explain why in Risk Notes. All other items must be ticked before requesting human review.
  • Type label — this PR carries exactly one of bug, enhancement, task, documentation. Type labels are author-added; the labeler bot does NOT assign them. Add the label in the GitHub UI, then tick this.
  • Routing labels — this PR carries at least one of app, ui, platform, harness, ci. The labeler bot assigns these on PR open based on changed paths. Confirm the bot's choice (or override if wrong), then tick this.
  • Priority label — this PR carries exactly one of P0, P1, P2, P3. The priority-triage bot suggests one on PR open. Confirm or override, then tick this.
  • Human Review Status above is set to Pending, Approved by @<reviewer>, or Not required: <reason> (default is Pending; "not required" is restricted to bot-authored low-risk PRs).
  • I linked the related issue, or stated in Summary why there is no issue.
  • I described the review focus and any meaningful risks.
  • I replaced the example block in How To Verify with the real verification steps and the key result for each.
  • I did not introduce unrelated refactors, dependencies, generated files, or file changes beyond the stated scope.
  • (conditional) I manually checked visible UI or copy changes when needed, with screenshots or recordings. Leave unticked only if no visible UI or copy changed.
  • (conditional) I considered macOS and Windows impact for platform, packaging, updater, signing, paths, shell, or permissions changes. Leave unticked only if no platform/packaging surface was touched.
  • (conditional) I called out docs, release notes, dependencies, permissions, credentials, deletion behavior, generated content, or local file changes when relevant. Leave unticked only if none of those surfaces was touched.
  • I reviewed the final diff for unrelated changes and suspicious dependency changes.
  • I am targeting dev, and my PR title and commit messages use Conventional Commits in English.

Summary by CodeRabbit

  • New Features

    • Introduced cache hit rate metric in session context statistics, displaying cached token usage as a rounded percentage with color-coded visual indicators.
  • Localization

    • Added cache hit rate label translations for English and Chinese language support.

Review Change Stack

@Astro-Han Astro-Han added enhancement New feature or request P2 Medium priority app Application behavior and product flows ui Design system and user interface labels May 25, 2026
@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR implements cache hit rate observability in the session context tab. It extends the test LLM server to report cached tokens, adds a computed cacheHitRate metric (rounded percentage) to the session context type, displays it with color coding (green ≥90%, yellow 50–90%, red <50%), and validates the complete flow with an end-to-end snapshot test covering responsive UI resizing.

Changes

Cache Hit Rate Feature

Layer / File(s) Summary
Metric computation and test infrastructure
packages/opencode/test/lib/llm-server.ts, packages/app/src/components/session/session-context-metrics.ts, packages/app/src/components/session/session-context-metrics.test.ts
Usage type now includes optional cacheRead; cacheHitRate(input, read, write) helper computes a rounded percentage or null when denominators are invalid; Context type adds cacheHitRate field wired in build(); unit tests validate calculation and null cases.
UI rendering with color coding and tooltip
packages/app/src/components/session/session-context-tab.tsx
New cacheHitRateClass() helper maps hit rates to CSS classes (green ≥90%, yellow 50–90%, red <50%); cacheHitRate memo renders percentage with tooltip showing raw read/write counts; stats array replaces prior cacheTokens display.
Internationalization for cache hit rate label
packages/app/src/i18n/en.ts, packages/app/src/i18n/zh.ts
Adds context.stats.cacheHitRate translation keys in English (Cache Hit Rate) and Chinese (缓存命中率).
End-to-end snapshot test with responsive resizing
packages/app/e2e/snap/session-context-cache.snap.ts
Tests complete feature: opens project, triggers context cache interactions, verifies UI visibility of 90% hit rate, dismisses toast notifications, captures default and resized panel screenshots via widenRightPanel() helper, combines into grid snapshot.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A cache that's alive, its hit rate in sight,
Green when it's glowing, ninety percent bright!
No more raw numbers, no math in your head—
Watch the cache sparkle, green, yellow, or red! 🎨✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: adding a cache hit rate feature to the app's context display.
Linked Issues check ✅ Passed All P0 objectives from issue #884 are met: cache hit rate row added to context tab with proper formula, raw values preserved on hover, and graceful degradation when data is unavailable.
Out of Scope Changes check ✅ Passed All changes are directly scoped to issue #884: metrics computation, UI display, i18n support, tests, and e2e fixture extension for cache seeding are all within stated objectives.
Description check ✅ Passed PR description is comprehensive and follows the template structure with all required sections completed, including clear summary, reasoning, related issue, human review status, review focus, risk notes, verification steps, and fully checked checklist.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch pawwork/issue-884-cache-hit-rate-flat

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 and usage tips.

@github-actions github-actions Bot added the harness Model harness, prompts, tool descriptions, and session mechanics label May 25, 2026

@github-actions github-actions 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.

Suggested priority: P2 (includes user-path files (packages/app/src/components/session/session-context-metrics.test.ts, packages/app/src/components/session/session-context-metrics.ts, packages/app/src/components/session/session-context-tab.tsx, packages/app/src/i18n/en.ts, packages/app/src/i18n/zh.ts)).

P1/P0 are reserved for maintainer confirmation. Please relabel manually if this is a release blocker, security issue, data-loss risk, or updater/runtime failure.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a "Cache Hit Rate" metric to the session context view, updating the metrics calculation, UI tab display (with color-coded values), localization dictionaries, and the mock LLM server for testing. The review feedback correctly identifies a calculation bug in the cacheHitRate formula: the denominator double-counts tokens because the input tokens count already includes the cached read and write tokens. The reviewer suggests simplifying the formula to read / input and provides corresponding updates for the call site and unit tests.

Comment thread packages/app/src/components/session/session-context-metrics.ts
Comment thread packages/app/src/components/session/session-context-metrics.ts
Comment thread packages/app/src/components/session/session-context-metrics.test.ts
@github-actions

github-actions Bot commented May 25, 2026

Copy link
Copy Markdown

Perf delta summary

Comparator: pass

Profile / Scenario interaction median interaction worst long task max tbt frame gap p95 frame gap max jank count cls status
default / tool-default-open-heavy-bash 16 -> 16 (0) 24 -> 24 (0) 67 -> 69 (+2) 25 -> 26 (+1) 50 -> 33.4 (-16.6) 100 -> 166.6 (+66.6) 3 -> 2 (-1) 0.004 -> 0.004 (0) pass

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 `@packages/opencode/test/lib/llm-server.ts`:
- Line 164: The responseCompleted event sets input_tokens_details.cached_tokens
from input.usage?.cacheRead but flowParts (the code that reconstructs usage when
converting chat chunks into response events for responses(...)) currently only
builds { input, output } and thus drops cacheRead; update flowParts to propagate
input.usage?.cacheRead into the reconstructed usage (e.g., include cached_tokens
or preserve input.usage as part of the usage object) so that responses(...)
receives and emits cached_tokens the same way responseCompleted does; update any
code paths that map chunk.usage -> usage in flowParts/responses to copy
input.usage?.cacheRead into input_tokens_details.cached_tokens (and similarly
preserve any existing token detail fields) so cached_tokens is not lost.
🪄 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: 134708f7-35f0-49ad-8044-b6953e1e6136

📥 Commits

Reviewing files that changed from the base of the PR and between ecb8cd2 and 0e2fd0a.

📒 Files selected for processing (7)
  • packages/app/e2e/snap/session-context-cache.snap.ts
  • packages/app/src/components/session/session-context-metrics.test.ts
  • packages/app/src/components/session/session-context-metrics.ts
  • packages/app/src/components/session/session-context-tab.tsx
  • packages/app/src/i18n/en.ts
  • packages/app/src/i18n/zh.ts
  • packages/opencode/test/lib/llm-server.ts

Comment thread packages/opencode/test/lib/llm-server.ts
@Astro-Han Astro-Han merged commit f8a27e6 into dev May 26, 2026
29 checks passed
@Astro-Han Astro-Han deleted the pawwork/issue-884-cache-hit-rate-flat branch May 26, 2026 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app Application behavior and product flows enhancement New feature or request harness Model harness, prompts, tool descriptions, and session mechanics P2 Medium priority ui Design system and user interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Show cache hit rate in the context tab

1 participant