Skip to content

The agent that grows with you

fix(cli): ignore lifetime session tokens in the switch guard

686f6c6196c2d9ff783e
diff --git a/hermes_cli/model_selection_guards.py b/hermes_cli/model_selection_guards.py
index 849d1a043a8..e3dc7487257 100644
--- a/hermes_cli/model_selection_guards.py
+++ b/hermes_cli/model_selection_guards.py
@@ -35,16 +35,17 @@ class SelectionContext:
def selection_context_for_agent(agent: object) -> Optional[SelectionContext]:
- """:class:`SelectionContext` from a live ``AIAgent``: the compressor's measured
- ``last_prompt_tokens`` (what the provider billed on the latest turn), else the session prompt
- counter. ``None`` when no live size is known — the guard then stays silent rather than guess."""
+ """:class:`SelectionContext` from a live ``AIAgent``.
+
+ Uses the compressor's measured ``last_prompt_tokens`` (what the provider billed
+ on the latest turn). ``session_prompt_tokens`` is a lifetime sum and is not live
+ occupancy, so it is not a fallback (#126343). ``None`` when no positive
+ measurement exists — the guard stays silent rather than guess."""
if agent is None:
return None
try:
cc = getattr(agent, "context_compressor", None)
tokens = int(getattr(cc, "last_prompt_tokens", 0) or 0) if cc else 0
- if tokens <= 0:
- tokens = int(getattr(agent, "session_prompt_tokens", 0) or 0)
except Exception:
tokens = 0
if tokens <= 0:
diff --git a/tests/hermes_cli/test_context_cache_switch_guard.py b/tests/hermes_cli/test_context_cache_switch_guard.py
index 672b875b12d..75ece58ab90 100644
--- a/tests/hermes_cli/test_context_cache_switch_guard.py
+++ b/tests/hermes_cli/test_context_cache_switch_guard.py
@@ -57,22 +57,33 @@ class TestContextCacheGuard:
class TestSelectionContextForAgent:
- def test_measured_tokens_then_session_counter_fallback(self):
+ def test_uses_measured_prompt_tokens_not_the_lifetime_counter(self):
class _CC:
last_prompt_tokens = 123_456
class _Measured:
context_compressor = _CC()
+ session_prompt_tokens = 16_282_033
model = "current/model"
- class _Fallback:
+ class _LifetimeOnly:
context_compressor = None
- session_prompt_tokens = 42_000
+ session_prompt_tokens = 16_282_033
+ model = "current/model"
+
+ class _Awaiting:
+ class _Stale:
+ last_prompt_tokens = -1
+
+ context_compressor = _Stale()
+ session_prompt_tokens = 16_282_033
model = "current/model"
ctx = selection_context_for_agent(_Measured())
assert (ctx.context_tokens, ctx.current_model) == (123_456, "current/model")
- assert selection_context_for_agent(_Fallback()).context_tokens == 42_000
+ # session_prompt_tokens is a lifetime sum, not live occupancy (#126343).
+ assert selection_context_for_agent(_LifetimeOnly()) is None
+ assert selection_context_for_agent(_Awaiting()) is None
def test_no_agent_or_empty_session_returns_none(self):
class _Empty: