Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions libs/code/deepagents_code/tui/widgets/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,12 @@ def watch_rubric_label(self, new_value: str) -> None:
_CONTEXT_CRITICAL_PERCENT = 80.0
"""Context usage at which the percentage turns to alert."""

_CACHE_WARNING_PERCENT = 90.0
"""Cache hit rate below which the percentage turns to caution."""

_CACHE_CRITICAL_PERCENT = 60.0
"""Cache hit rate below which the percentage turns to alert."""

def _percent_color(self, percent: float) -> str:
"""Return the color that encodes how full the context window is."""
colors = theme.get_theme_colors(self)
Expand All @@ -815,6 +821,15 @@ def _percent_color(self, percent: float) -> str:
return colors.warning
return colors.muted

def _cache_hit_color(self, percent: float) -> str:
"""Return the color that encodes cache hit-rate health."""
colors = theme.get_theme_colors(self)
if percent < self._CACHE_CRITICAL_PERCENT:
return colors.error
if percent < self._CACHE_WARNING_PERCENT:
return colors.warning
return colors.muted

def _context_segment(self, count: int, *, approximate: bool = False) -> Content:
"""Build the context percentage and absolute-usage segment.

Expand Down Expand Up @@ -851,20 +866,24 @@ def _cache_segment(self) -> Content:
Returns:
Styled cache usage.
"""
hit_rate = ""
hit_rate = Content("")
if self.cache_input_tokens:
cached = min(self.cache_read_tokens, self.cache_input_tokens)
hit_rate = f"{cached / self.cache_input_tokens * 100:.0f}% hit"
percent = cached / self.cache_input_tokens * 100
hit_rate = Content.styled(
f"{percent:.0f}% hit", self._cache_hit_color(percent)
)
Comment thread
johannes117 marked this conversation as resolved.
Resolved
elif not self.cache_read_tokens and not self.cache_write_tokens:
hit_rate = "0% hit"
hit_rate = Content.styled("0% hit", self._cache_hit_color(0))
Comment thread
johannes117 marked this conversation as resolved.
Resolved
details = (
f"{_compact_tokens(self.cache_read_tokens)} read"
f" / {_compact_tokens(self.cache_write_tokens)} write"
)
return Content.assemble(
Content.styled("Cache", theme.get_theme_colors(self).muted),
" ",
f"{hit_rate} {get_glyphs().bullet} " if hit_rate else "",
hit_rate,
f" {get_glyphs().bullet} " if hit_rate.plain else "",
details,
)

Expand Down
10 changes: 10 additions & 0 deletions libs/code/tests/unit_tests/tui/widgets/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ async def test_context_percentage_color_thresholds(self) -> None:
assert bar._percent_color(80.0) == colors.warning
assert bar._percent_color(80.1) == colors.error

async def test_cache_hit_color_thresholds(self) -> None:
async with StatusBarApp().run_test() as pilot:
bar = pilot.app.query_one("#status-bar", StatusBar)
colors = theme.get_theme_colors(bar)

assert bar._cache_hit_color(59.9) == colors.error
assert bar._cache_hit_color(60.0) == colors.warning
assert bar._cache_hit_color(89.9) == colors.warning
assert bar._cache_hit_color(90.0) == colors.muted


class TestApprovalModeDisplay:
"""Tests for the three-state approval indicator."""
Expand Down
Loading