Skip to content

The agent that grows with you

fix(cli): the status bar's context figure is the gateway's anchored one, with main's `~`

Tekniume610b40291a1
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index a71896cc7be..dc43a516e96 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -125,6 +125,8 @@ class GatewayAgent:
self._api_latency_history: list = []
self._api_output_history: list = []
self._turn_base_usage_anchor = None
+ self._context_tokens = 0
+ self._turn_calls_base = 0
self._current_task_id = self._current_turn_id = self._current_api_request_id = ""
self._memory_manager = None
self._memory_store = None
@@ -199,9 +201,9 @@ class GatewayAgent:
# context_display_source(): a "local_estimate" reading is a shown value != the real one.
compressor.last_real_prompt_tokens = (
None if source == "provider_usage" else compressor.last_prompt_tokens + 1)
- anchor = usage.get("usage_anchor")
- if anchor is None or isinstance(anchor, dict):
- self._turn_base_usage_anchor = anchor
+ if isinstance(usage.get("context_tokens"), int):
+ self._context_tokens = usage["context_tokens"]
+ self._anchor_display()
latency, tps = usage.get("avg_latency_s"), usage.get("tokens_per_s")
if isinstance(latency, (int, float)) and latency > 0 and isinstance(tps, (int, float)):
# The bar averages these deques (mean latency, sum/sum velocity): one aligned sample each.
@@ -334,6 +336,7 @@ class GatewayAgent:
def run_conversation(self, user_message, conversation_history=None, stream_callback=None,
task_id=None, persist_user_message=None, turn_author=None, **_kw):
history = list(conversation_history or [])
+ self._turn_calls_base = self.session_api_calls
self._interrupt_requested = False
self._interrupt_sent_at = None
self._drain_stale()
@@ -581,10 +584,38 @@ class GatewayAgent:
if not suffix and out.get("final_response") and not out.get("failed"):
out["messages"].append({"role": "assistant", "content": out["final_response"]})
out["session_id"] = self.session_id
- self._apply_usage(stored, usage)
self._session_messages = out["messages"]
+ self._apply_usage(stored, usage)
return out
+ def _anchor_display(self):
+ """Main's bar shows the anchored ``context_tokens`` by re-deriving it from
+ ``_turn_base_usage_anchor`` over ``_session_messages`` (``anchored_context_tokens``), and marks
+ it ``~`` when the turn appended rows after its first response. Rebuild an anchor over this
+ viewer's transcript that yields the gateway's figure: rows after the first API call of a
+ multi-call turn are the estimated delta (main's ``~``); a single reply is covered by the anchor."""
+ from agent.model_metadata import estimate_messages_tokens_rough
+ from agent.usage_anchor import capture_usage_anchor
+ messages = self._session_messages if isinstance(self._session_messages, list) else []
+ tokens = getattr(self, "_context_tokens", 0) or 0
+ if tokens <= 0 or not messages or self._generation is not None:
+ self._turn_base_usage_anchor = None # mid-turn: the bar falls back to last_prompt_tokens
+ return
+ # Main anchors on the turn's FIRST response, priced over the transcript through the turn's ask.
+ # A turn with no completed call (interrupted before a response) keeps the previous anchor, so
+ # its ask is estimated delta too.
+ last_user = next((i for i in range(len(messages) - 1, -1, -1) if messages[i].get("role") == "user"),
+ len(messages) - 1)
+ base = last_user + 1 if self.session_api_calls > self._turn_calls_base else last_user
+ if base <= 0:
+ self._turn_base_usage_anchor = None
+ return
+ delta = messages[base:]
+ if delta and delta[0].get("role") == "assistant":
+ delta = delta[1:]
+ estimate = estimate_messages_tokens_rough(delta, charge_stale_thinking=False) if delta else 0
+ self._turn_base_usage_anchor = capture_usage_anchor(max(1, tokens - estimate), 0, messages[:base])
+
def _apply_usage(self, stored, receipt_usage):
"""Turn end: the session-cumulative ``session.stats`` (the same dict ``session.usage`` carries);
a gateway without the verb leaves the per-turn receipt figures as before."""
diff --git a/tests/hermes_cli/test_gateway_agent.py b/tests/hermes_cli/test_gateway_agent.py
index aa9c64ddb90..8c2047d86dc 100644
--- a/tests/hermes_cli/test_gateway_agent.py
+++ b/tests/hermes_cli/test_gateway_agent.py
@@ -274,3 +274,39 @@ def test_turn_end_reads_session_stats_cumulative(make_agent):
agent.run_conversation("q")
assert agent.session_input_tokens == 500 and agent.session_api_calls == 5
assert agent.get_rate_limit_state().has_data and agent.get_rate_limit_state().requests_min.limit == 10
+
+
+def test_status_bar_context_is_the_gateways_anchored_figure_with_mains_estimate_marker(make_agent):
+ """Main's bar re-derives context_tokens from _turn_base_usage_anchor over _session_messages and
+ shows `~` when rows follow the turn's first response; an interrupted turn (no response) is `~`."""
+ from agent.usage_anchor import anchored_context_tokens
+ usage = dict(SESSION_USAGE, context_tokens=700, api_calls=2)
+ suffix = [{"role": "assistant", "content": "", "tool_calls": [{"id": "c1", "type": "function",
+ "function": {"name": "read_file", "arguments": "{}"}}]},
+ {"role": "tool", "tool_call_id": "c1", "content": "x" * 400},
+ {"role": "assistant", "content": "done"}]
+ agent, client = make_agent([frame("message.complete", {"text": "done", "outcome": "completed"})],
+ result={"final_response": "done", "api_calls": 2, "messages": suffix})
+ original = client.rpc
+
+ async def rpc(method, *, _timeout=None, **params):
+ if method == "session.stats":
+ return usage
+ return await original(method, _timeout=_timeout, **params)
+ client.rpc = rpc
+ agent.run_conversation("read it")
+ msgs = agent._session_messages
+ assert anchored_context_tokens(msgs, agent._turn_base_usage_anchor, charge_stale_thinking=False) == 700
+ assert len(msgs) > agent._turn_base_usage_anchor["base_count"] + 1 # rows after the first reply: `~`
+ agent2, client2 = make_agent([frame("message.complete", {"text": "", "outcome": "cancelled", "status": "interrupted"})])
+ original2 = client2.rpc
+
+ async def rpc2(method, *, _timeout=None, **params):
+ if method == "session.stats":
+ return usage
+ return await original2(method, _timeout=_timeout, **params)
+ client2.rpc = rpc2
+ agent2.session_api_calls = 2
+ agent2.run_conversation("story", conversation_history=[{"role": "user", "content": "a"},
+ {"role": "assistant", "content": "b"}])
+ assert agent2._turn_base_usage_anchor["base_count"] == 2 # the interrupted ask is estimated