fix(context): clamp -1 post-compression sentinel in sibling status paths

Whole-bug-class follow-up to the tui_gateway fix: the same -1
last_prompt_tokens sentinel (parked by conversation_compression after a
compression) leaked into other status readers, producing a raw -1 or a
NEGATIVE usage_percent on the transitional turn:

- agent/context_engine.py get_status() (the ABC default every external
  context engine inherits) — highest blast radius
- gateway/slash_commands.py /usage context line
- cli.py session usage printout

All clamped to >=0, mirroring cli.py _get_status_bar_snapshot and the
tui_gateway fix. Adds an ABC get_status sentinel-clamp regression test.
This commit is contained in:
kshitijk4poor 2026-07-01 13:20:55 +05:30 committed by kshitij
parent b6d8fc41c8
commit 8db6ed7bd9
5 changed files with 26 additions and 8 deletions

View file

@ -194,12 +194,17 @@ class ContextEngine(ABC):
Default returns the standard fields run_agent.py expects.
"""
# Clamp the -1 "compression just ran, awaiting real usage" sentinel
# (set by conversation_compression) to 0 so status readers don't see a
# raw -1 or a negative usage_percent on the transitional turn. Mirrors
# the CLI/gateway status-bar paths (cli.py, tui_gateway/server.py).
last_prompt = self.last_prompt_tokens if self.last_prompt_tokens > 0 else 0
return {
"last_prompt_tokens": self.last_prompt_tokens,
"last_prompt_tokens": last_prompt,
"threshold_tokens": self.threshold_tokens,
"context_length": self.context_length,
"usage_percent": (
min(100, self.last_prompt_tokens / self.context_length * 100)
min(100, last_prompt / self.context_length * 100)
if self.context_length else 0
),
"compression_count": self.compression_count,