The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): the spinner's `↓ N tok` beside a tool reads the counters of the call that asked for it
Teknium034f8fb75c0a
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.pyindex fb15a88d666..5683b5c988c 100644--- a/hermes_cli/gateway_agent.py+++ b/hermes_cli/gateway_agent.py@@ -127,6 +127,7 @@ class GatewayAgent:self._turn_base_usage_anchor = Noneself._context_tokens = 0self._turn_calls_base = 0+ self._usage_stale = Falseself._current_task_id = self._current_turn_id = self._current_api_request_id = ""self._memory_manager = Noneself._memory_store = None@@ -337,6 +338,7 @@ class GatewayAgent: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._usage_stale = Trueself._interrupt_requested = Falseself._interrupt_sent_at = Noneself._drain_stale()@@ -433,6 +435,12 @@ class GatewayAgent:args = payload.get("args") if isinstance(payload.get("args"), dict) else {}call_id = payload.get("tool_id") or payload.get("tool_call_id") or ""starts[call_id] = time.monotonic()+ if self._usage_stale:+ # Main's counters move right after each API call, before its tools run (the spinner's+ # `↓ N tok` beside a tool or approval); the gateway's next session.usage frame comes only+ # at the next step, so the first tool of each batch reads session.stats once.+ self._usage_stale = False+ self._refresh_stats()if self.tool_progress_callback is not None:from agent.display import build_tool_previewself.tool_progress_callback("tool.started", name, build_tool_preview(name, args), args)@@ -451,6 +459,7 @@ class GatewayAgent:if self.tool_progress_callback is not None:self.tool_progress_callback("tool.completed", name, None, None, duration=duration,is_error=bool(payload.get("is_error")), result=result)+ self._usage_stale = Trueif name == "todo":self._todo_from_result(result)diff = payload.get("inline_diff")@@ -616,17 +625,23 @@ class GatewayAgent:estimate = estimate_messages_tokens_rough(delta, charge_stale_thinking=False) if delta else 0self._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."""+ def _refresh_stats(self):+ """``session.stats`` → the same attributes ``session.usage`` sets; False when unavailable."""try:stats = self.connection.call("session.stats", session_id=self.session_id, timeout=10)except GatewayClientError as exc:logger.debug("session.stats unavailable: %s", exc)- stats = None+ return Falsestats = stats.get("usage") if isinstance(stats, dict) and isinstance(stats.get("usage"), dict) else stats- if isinstance(stats, dict) and isinstance(stats.get("api_calls"), int):- self._state().apply_usage(stats)+ if not (isinstance(stats, dict) and isinstance(stats.get("api_calls"), int)):+ return False+ self._state().apply_usage(stats)+ return True++ 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."""+ if self._refresh_stats():return# Fallback: the classic-CLI result carries the gateway agent's session_* counters# (turn_finalizer _SESSION_TOKEN_KEYS); per-turn receipt usage only accumulates what is missing.diff --git a/tests/hermes_cli/test_gateway_agent.py b/tests/hermes_cli/test_gateway_agent.pyindex 18b0f289233..621dd3377a1 100644--- a/tests/hermes_cli/test_gateway_agent.py+++ b/tests/hermes_cli/test_gateway_agent.py@@ -310,3 +310,27 @@ def test_status_bar_context_is_the_gateways_anchored_figure_with_mains_estimate_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+++def test_first_tool_of_a_batch_reads_session_stats_for_mains_spinner_token_flow(make_agent):+ """Main's `↓ N tok` beside a running tool/approval reads session_output_tokens right after the API+ call that asked for the tool; the stand-in refreshes session.stats once per tool batch."""+ seen = []+ agent, client = make_agent(+ [frame("tool.start", {"tool_id": "c1", "name": "terminal", "args": {"command": "ls"}}),+ frame("tool.start", {"tool_id": "c2", "name": "terminal", "args": {"command": "pwd"}}),+ frame("tool.complete", {"tool_id": "c1", "name": "terminal", "args": {}, "result": "{}"}),+ frame("tool.complete", {"tool_id": "c2", "name": "terminal", "args": {}, "result": "{}"}),+ frame("message.complete", {"text": "x", "outcome": "completed"})],+ tool_progress_callback=lambda kind, *a, **k: seen.append((kind, agent.session_output_tokens)))+ original = client.rpc++ async def rpc(method, *, _timeout=None, **params):+ if method == "session.stats":+ client.calls.append((method, params))+ return dict(SESSION_USAGE, output_tokens=10)+ return await original(method, _timeout=_timeout, **params)+ client.rpc = rpc+ agent.run_conversation("q")+ assert seen[0] == ("tool.started", 10)+ assert [m for m, _ in client.calls].count("session.stats") == 2 # first tool.start + turn end