diff --git a/cli.py b/cli.py index 09cdf2e171..3f730ae0fc 100644 --- a/cli.py +++ b/cli.py @@ -6178,6 +6178,27 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin): 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 _battery_status_style(category: str) -> str: """Map a battery colour category to a status-bar style class.""" @@ -7244,6 +7265,9 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin): parts.append(f"โš• {snapshot['model_short']}") if _ok("context_pct"): parts.append(percent_label) + cache = self._cache_hit_rate(snapshot, precision=0) + if cache and _ok("cache_hit"): + parts.append(cache[1]) if battery_label: parts.insert(0, battery_label) compressions = snapshot.get("compressions", 0) @@ -7286,6 +7310,9 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin): if battery_label: parts.insert(0, battery_label) compressions = snapshot.get("compressions", 0) + cache = self._cache_hit_rate(snapshot) + if cache and _ok("cache_hit"): + parts.append(cache[1]) if compressions and _ok("compressions"): parts.append(f"๐Ÿ—œ๏ธ {compressions}") bg_count = snapshot.get("active_background_tasks", 0) @@ -7388,6 +7415,9 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin): frags.append(("class:status-bar-strong", snapshot["model_short"])) if _ok("context_pct"): _append(frags, " ยท ", (self._status_bar_context_style(percent), percent_label)) + cache = self._cache_hit_rate(snapshot, precision=0) + if cache and _ok("cache_hit"): + _append(frags, " ยท ", (self._cache_hit_rate_style(cache[0]), cache[1])) if compressions and _ok("compressions"): _append(frags, " ยท ", (self._compression_count_style(compressions), f"๐Ÿ—œ๏ธ {compressions}")) if bg_count and _ok("bg_tasks"): @@ -7436,6 +7466,9 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin): ("class:status-bar-dim", " "), (bar_style, percent_label), ) + cache = self._cache_hit_rate(snapshot) + if cache and _ok("cache_hit"): + _append(frags, " โ”‚ ", (self._cache_hit_rate_style(cache[0]), cache[1])) if compressions and _ok("compressions"): _append(frags, " โ”‚ ", (self._compression_count_style(compressions), f"๐Ÿ—œ๏ธ {compressions}")) if bg_count and _ok("bg_tasks"): diff --git a/contributors/emails/Cheri8014@163.com b/contributors/emails/Cheri8014@163.com new file mode 100644 index 0000000000..24b21264d8 --- /dev/null +++ b/contributors/emails/Cheri8014@163.com @@ -0,0 +1 @@ +CheriWen diff --git a/tests/cli/test_cli_status_bar.py b/tests/cli/test_cli_status_bar.py index ab77872e1d..5b07bc15e2 100644 --- a/tests/cli/test_cli_status_bar.py +++ b/tests/cli/test_cli_status_bar.py @@ -508,3 +508,107 @@ class TestStatusBarFieldConfig: with patch.object(cli_mod, "CLI_CONFIG", {"display": {"status_bar": {"fields": ["duration"]}}}): second = cli_obj._get_status_bar_field_set() assert first == second == frozenset({"model"}) + + +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