Skip to content

fix(status): cap cache hit rate at 100% in status display#57400

Merged
hydro13 merged 1 commit intoopenclaw:mainfrom
hydro13:fix/status-cache-percentage
Mar 30, 2026
Merged

fix(status): cap cache hit rate at 100% in status display#57400
hydro13 merged 1 commit intoopenclaw:mainfrom
hydro13:fix/status-cache-percentage

Conversation

@hydro13
Copy link
Copy Markdown
Contributor

@hydro13 hydro13 commented Mar 30, 2026

Summary

formatTokensCompact() computed cache hit rate using totalTokens as the denominator. Legacy session rows with undersized totalTokens produced impossible values like "120% cached".

Fix

Use inputTokens + cacheRead + cacheWrite when prompt-side fields are available. Fall back to max(totalTokens, cacheRead + cacheWrite) for legacy data so the denominator never drops below the known cached tokens.

Testing

  • 14 test suites, 64 tests pass in src/commands/status
  • Typecheck (pnpm tsgo) clean
  • New regression tests:
    • Legacy undersized totalTokens → clamped to 100% (not 120%)
    • inputTokens-present path → uses correct prompt-side denominator (67% vs old 40%)

Fixes #26643

formatTokensCompact() computed cache rate using totalTokens as the
denominator. Legacy rows with undersized totalTokens produced
impossible values like 120%. Use inputTokens + cacheRead + cacheWrite
when prompt-side fields are available, falling back to
max(totalTokens, cacheRead + cacheWrite) for legacy data.

Fixes openclaw#26643
@openclaw-barnacle openclaw-barnacle bot added commands Command implementations size: S maintainer Maintainer-authored PR labels Mar 30, 2026
@hydro13 hydro13 self-assigned this Mar 30, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 30, 2026

Greptile Summary

This PR fixes a display bug in formatTokensCompact() where the cache hit rate denominator used totalTokens (which includes output tokens and can be undersized for legacy rows), producing impossible percentages like "120% cached". The fix prioritises inputTokens + cacheRead + cacheWrite as the denominator when inputTokens is present — this exactly equals the prompt-side token count in Anthropic's API model. For legacy sessions that predate inputTokens tracking, it falls back to Math.max(totalTokens, cacheRead + cacheWrite), ensuring the denominator never drops below the known cached-prompt tokens.

  • Logic is correct: inputTokens (non-cached prompt tokens) + cacheRead (cache hits) + cacheWrite (new cache creations) = total prompt tokens, so the denominator is properly scoped to prompt-side usage only.
  • No division-by-zero risk: the cache section is guarded by cacheRead > 0, so promptTokensFromParts (and both fallback branches) are always > 0.
  • Good defensive validation: each field is individually checked for Number.isFinite and >= 0 before use.
  • Tests added: two targeted regression tests cover both the legacy clamping path and the new inputTokens-present path with concrete expected percentages.
  • No behaviour change outside the cache hit rate display line.

Confidence Score: 5/5

Safe to merge — the fix is narrowly scoped to a display-only calculation, is mathematically sound, and is covered by regression tests.

No P0 or P1 findings. The denominator logic is correct (prompt-side tokens only), edge cases (zero inputTokens, legacy undefined, NaN/Infinity) are all guarded, division-by-zero is impossible inside the existing cacheRead > 0 guard, and two new regression tests validate both code paths.

No files require special attention.

Important Files Changed

Filename Overview
src/commands/status.format.ts Denominator for cache hit rate now uses prompt-side token fields (inputTokens + cacheRead + cacheWrite) when available, with a safe Math.max fallback for legacy rows — correctly prevents >100% display values.
src/commands/status.test.ts Two new regression tests added: one covering the legacy undersized-totalTokens path (clamped to 100%), one covering the inputTokens-present path (67% vs old 40%).
CHANGELOG.md Changelog entry added for the cache hit rate fix.

Reviews (1): Last reviewed commit: "fix(status): cap cache hit rate at 100% ..." | Re-trigger Greptile

@hydro13 hydro13 merged commit acf8470 into openclaw:main Mar 30, 2026
28 of 38 checks passed
@hydro13
Copy link
Copy Markdown
Contributor Author

hydro13 commented Mar 30, 2026

Merged via squash.

  • Merge SHA: acf8470
  • formatTokensCompact() now uses prompt-side token fields (inputTokens + cacheRead + cacheWrite) as denominator
  • Legacy fallback: max(totalTokens, cacheRead + cacheWrite) prevents >100%
  • CHANGELOG entry added
  • 64 status tests passing, typecheck clean

MonkeyLeeT pushed a commit to MonkeyLeeT/openclaw that referenced this pull request Mar 30, 2026
…7400)

formatTokensCompact() computed cache rate using totalTokens as the
denominator. Legacy rows with undersized totalTokens produced
impossible values like 120%. Use inputTokens + cacheRead + cacheWrite
when prompt-side fields are available, falling back to
max(totalTokens, cacheRead + cacheWrite) for legacy data.

Fixes openclaw#26643
@aisle-research-bot
Copy link
Copy Markdown

aisle-research-bot bot commented Mar 30, 2026

🤖 We're reviewing this PR with Aisle

We're running a security check on the changes in this PR now. This usually takes a few minutes. ⌛
We'll post the results here as soon as they're ready.

Progress:

  • Analysis
  • Finalization

Latest run failed. Keeping previous successful results. Trace ID: 019d3c7792f0af6f0c5cb559c709b01f.

Last updated on: 2026-03-30T09:47:27Z

pritchie pushed a commit to pritchie/openclaw that referenced this pull request Mar 30, 2026
…7400)

formatTokensCompact() computed cache rate using totalTokens as the
denominator. Legacy rows with undersized totalTokens produced
impossible values like 120%. Use inputTokens + cacheRead + cacheWrite
when prompt-side fields are available, falling back to
max(totalTokens, cacheRead + cacheWrite) for legacy data.

Fixes openclaw#26643
alexjiang1 pushed a commit to alexjiang1/openclaw that referenced this pull request Mar 31, 2026
…7400)

formatTokensCompact() computed cache rate using totalTokens as the
denominator. Legacy rows with undersized totalTokens produced
impossible values like 120%. Use inputTokens + cacheRead + cacheWrite
when prompt-side fields are available, falling back to
max(totalTokens, cacheRead + cacheWrite) for legacy data.

Fixes openclaw#26643
pgondhi987 pushed a commit to pgondhi987/openclaw that referenced this pull request Mar 31, 2026
…7400)

formatTokensCompact() computed cache rate using totalTokens as the
denominator. Legacy rows with undersized totalTokens produced
impossible values like 120%. Use inputTokens + cacheRead + cacheWrite
when prompt-side fields are available, falling back to
max(totalTokens, cacheRead + cacheWrite) for legacy data.

Fixes openclaw#26643
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: status cached percentage >100%

1 participant