diff --git a/cli.py b/cli.py index 62f4be1e320df..3d064ba35772c 100644 --- a/cli.py +++ b/cli.py @@ -3591,6 +3591,27 @@ def _status_bar_context_style(self, percent_used: Optional[int]) -> str: return "class:status-bar-warn" return "class:status-bar-good" + def _cache_hit_rate(self, snapshot: dict, precision: int = 1) -> "tuple[float, str] | None": + """Return (cache_pct, formatted_label) or None if no cache data. + + Centralises the cache-hit-rate computation so both the plain-text + status bar and the prompt-toolkit fragment path share one formula. + """ + cache_read = snapshot.get("session_cache_read_tokens", 0) + prompt_total = snapshot.get("session_prompt_tokens", 0) + if cache_read > 0 and prompt_total > 0: + cache_pct = cache_read / prompt_total * 100 + return cache_pct, f"โ—Ž {cache_pct:.{precision}f}%" + return None + + def _cache_hit_rate_style(self, cache_pct: float) -> str: + """Style for cache hit rate โ€” higher is better (opposite of context %).""" + if cache_pct >= 70: + return "class:status-bar-good" + if cache_pct >= 40: + return "class:status-bar-warn" + return "class:status-bar-bad" + @staticmethod def _compression_count_style(count: int) -> str: """Return a style class reflecting context compression pressure.""" @@ -3942,6 +3963,9 @@ def _build_status_bar_text(self, width: Optional[int] = None) -> str: return self._trim_status_bar_text(text, width) if width < 76: parts = [f"โš• {snapshot['model_short']}", percent_label] + cache = self._cache_hit_rate(snapshot, precision=0) + if cache: + parts.append(cache[1]) compressions = snapshot.get("compressions", 0) if compressions: parts.append(f"๐Ÿ—œ๏ธ {compressions}") @@ -3965,6 +3989,9 @@ def _build_status_bar_text(self, width: Optional[int] = None) -> str: compressions = snapshot.get("compressions", 0) parts = [f"โš• {snapshot['model_short']}", context_label, percent_label] + cache = self._cache_hit_rate(snapshot) + if cache: + parts.append(cache[1]) if compressions: parts.append(f"๐Ÿ—œ๏ธ {compressions}") bg_count = snapshot.get("active_background_tasks", 0) @@ -4021,6 +4048,10 @@ def _get_status_bar_fragments(self): ("class:status-bar-dim", " ยท "), (self._status_bar_context_style(percent), percent_label), ] + cache = self._cache_hit_rate(snapshot, precision=0) + if cache: + frags.append(("class:status-bar-dim", " ยท ")) + frags.append((self._cache_hit_rate_style(cache[0]), cache[1])) if compressions: frags.append(("class:status-bar-dim", " ยท ")) frags.append((self._compression_count_style(compressions), f"๐Ÿ—œ๏ธ {compressions}")) @@ -4060,6 +4091,10 @@ def _get_status_bar_fragments(self): ("class:status-bar-dim", " "), (bar_style, percent_label), ] + cache = self._cache_hit_rate(snapshot) + if cache: + frags.append(("class:status-bar-dim", " โ”‚ ")) + frags.append((self._cache_hit_rate_style(cache[0]), cache[1])) if compressions: frags.append(("class:status-bar-dim", " โ”‚ ")) frags.append((self._compression_count_style(compressions), f"๐Ÿ—œ๏ธ {compressions}")) diff --git a/tests/cli/test_cli_status_bar.py b/tests/cli/test_cli_status_bar.py index 47a78c5704433..c632a75de81a4 100644 --- a/tests/cli/test_cli_status_bar.py +++ b/tests/cli/test_cli_status_bar.py @@ -676,3 +676,107 @@ def test_explicit_width_skips_pt_lookup(self): mock_get_app.assert_not_called() mock_shutil.assert_not_called() assert len(text) > 0 + + +class TestCacheHitRate: + def test_cache_hit_rate_shown_in_wide_terminal(self): + cli_obj = _attach_agent( + _make_cli(), + prompt_tokens=10_000, + completion_tokens=2_000, + total_tokens=12_000, + api_calls=5, + context_tokens=12_000, + context_length=200_000, + cache_read_tokens=7600, + cache_write_tokens=0, + ) + + text = cli_obj._build_status_bar_text(width=120) + + assert "โ—Ž 76.0%" in text + + def test_cache_hit_rate_shown_in_narrow_terminal(self): + cli_obj = _attach_agent( + _make_cli(), + prompt_tokens=10_000, + completion_tokens=2_000, + total_tokens=12_000, + api_calls=5, + context_tokens=12_000, + context_length=200_000, + cache_read_tokens=5000, + cache_write_tokens=0, + ) + + text = cli_obj._build_status_bar_text(width=60) + + assert "โ—Ž 50%" in text + + def test_cache_hit_rate_hidden_when_zero(self): + cli_obj = _attach_agent( + _make_cli(), + prompt_tokens=10_000, + completion_tokens=2_000, + total_tokens=12_000, + api_calls=5, + context_tokens=12_000, + context_length=200_000, + cache_read_tokens=0, + cache_write_tokens=0, + ) + + text = cli_obj._build_status_bar_text(width=120) + + assert "โ—Ž" not in text + + def test_cache_hit_rate_hidden_when_no_data(self): + cli_obj = _attach_agent( + _make_cli(), + prompt_tokens=10_000, + completion_tokens=2_000, + total_tokens=12_000, + api_calls=5, + context_tokens=12_000, + context_length=200_000, + ) + + text = cli_obj._build_status_bar_text(width=120) + + assert "โ—Ž" not in text + + def test_cache_hit_rate_one_decimal(self): + cli_obj = _attach_agent( + _make_cli(), + prompt_tokens=10_000, + completion_tokens=2_000, + total_tokens=12_000, + api_calls=5, + context_tokens=12_000, + context_length=200_000, + cache_read_tokens=7620, + cache_write_tokens=0, + ) + + text = cli_obj._build_status_bar_text(width=120) + + assert "โ—Ž 76.2%" in text + + def test_cache_hit_rate_with_anthropic_style_cache(self): + """Anthropic has both cache_read and cache_write""" + cli_obj = _attach_agent( + _make_cli(), + prompt_tokens=10_000, + completion_tokens=2_000, + total_tokens=12_000, + api_calls=5, + context_tokens=12_000, + context_length=200_000, + cache_read_tokens=5000, + cache_write_tokens=2000, + ) + + text = cli_obj._build_status_bar_text(width=120) + + # cache_read / prompt_tokens = 5000 / 10000 = 50% + assert "โ—Ž 50.0%" in text