Skip to content

(feat)cache hit rate#809

Merged
nonoqing merged 2 commits into
GCWing:mainfrom
nonoqing:yuyiqing/dev
May 20, 2026
Merged

(feat)cache hit rate#809
nonoqing merged 2 commits into
GCWing:mainfrom
nonoqing:yuyiqing/dev

Conversation

@nonoqing
Copy link
Copy Markdown
Collaborator

No description provided.

nonoqing added 2 commits May 20, 2026 15:55
Adds `cache_hit_rate: Option<f64>` to UsageTokenBreakdown and
UsageModelBreakdown, computed in build_token_breakdown /
build_model_breakdown via a shared helper that:

- only counts records with `cached_tokens_available == true` in BOTH
  numerator (cached_tokens) and denominator (input_tokens), so a
  partially reporting provider does not get its hit rate artificially
  deflated by unreported input
- returns None when no record reports cache, or when the filtered
  input sum is zero (avoids divide-by-zero on tool-only turns)

Renderers (terminal + markdown) append a ` (NN%)` suffix to the
"Cached" cell when hit rate is available. When coverage is
Unavailable the existing "not reported" fallback is preserved
regardless of hit_rate state.

PartialEq-only on the affected breakdown structs (and their parent
SessionUsageReport) because Option<f64> precludes total equality.

Five compute-layer tests cover: all-reported normal case, no-record
reports, partial coverage (the bug-prone case), input_sum=0 edge,
per-model isolation. Two render tests lock the cell format and the
"not reported" fallback.
Renders the new backend `cacheHitRate` field across the three UI
surfaces that show cached tokens:

- Session report card: appends ` (NN%)` inline to the "Cached"
  cell when coverage is available/partial, preserves the existing
  "Cache not reported" fallback when unavailable
- Session detail panel: adds a dedicated "Hit rate" column to the
  per-model table; falls back to "-" when a model has no reported
  cache data
- Markdown export builder (`buildSessionUsageExportMarkdown`):
  same inline ` (NN%)` suffix so the exported markdown matches the
  Rust-side terminal/markdown renderer output

Two helpers in usageReportUtils:
- formatHitRateSuffix(rate, t) - ` (NN%)` for inline append
- formatHitRatePercent(rate, t) - bare `NN%` for table cells

i18n adds `usage.table.hitRate` to en-US/zh-CN/zh-TW.

Tests: 5 helper unit tests (round-trip / NaN / Infinity / rounding)
and 2 card behaviour tests (inline rendering + unavailable fallback).
@nonoqing nonoqing merged commit 795a7f0 into GCWing:main May 20, 2026
1 check passed
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