Skip to content

The agent that grows with you

fix(cli): keep the flat counters from the gateway's session.usage frame

unsupportedpastels926044a48cb4
diff --git a/gateway/session_usage.py b/gateway/session_usage.py
index af59fb5fe9f..b8b23caccdf 100644
--- a/gateway/session_usage.py
+++ b/gateway/session_usage.py
@@ -142,18 +142,22 @@ def usage_frame(stats: dict) -> dict:
``SessionUsagePayload`` (``tui_gateway/contracts/events.py``) allows either view — the TUI
``usage`` object (``server._start_usage_ticker``) or the session-cumulative counters at the top
- level — and the two readers on the far end want different halves. ``hermes_cli.gateway_agent``
- (the classic CLI / TUI mirror) reads only the loose counters; the Desktop status bar reads only
+ level — and the readers on the far end want different halves. The classic CLI
+ (``hermes_cli.gateway_connection.SessionState.apply_event``) keeps the top-level counters
+ whenever they are present and ignores the nested view; ACP (``acp_adapter.gateway_server``)
+ reads only the top level; a Desktop bundle older than ``usageFromTick`` reads only
``payload.usage``, so its context / cache-hit / tokens-per-second meters render their empty
- placeholder for as long as the frame carries the counters alone. Publishing both serves either
+ placeholder for as long as the frame carries the counters alone. Publishing both serves every
viewer from one frame, and leaves the counters untouched for every existing consumer.
``usage`` is the embedded ticker's ``server._get_usage`` view (same keys, same rules, same
omissions; ``avg_latency_s`` / ``avg_tps`` are rounded from the counters' two decimals), so a
Desktop bundle that reads only ``payload.usage`` fills its meters here too. The
top-level counters on this event are deprecated: new viewers read ``usage`` (or call
- ``session.stats`` for the counters). They stay for ``hermes_cli.gateway_agent``, the Ink TUI and
- Desktop's ``usageFromTick`` until those read ``usage``.
+ ``session.stats`` for the counters). Before they can be dropped, these readers must move off
+ them: the classic CLI's ``SessionState.apply_event`` → ``GatewayAgent.apply_session_usage``,
+ ACP's ``_project_usage`` (``usage_update`` / ``turn_usage``), and the flat branch of
+ ``usageFromTick`` (Desktop and Ink).
"""
return {"usage": tui_usage(stats), **stats}
diff --git a/hermes_cli/gateway_connection.py b/hermes_cli/gateway_connection.py
index 471fe4c24e0..eb5abb5a8c8 100644
--- a/hermes_cli/gateway_connection.py
+++ b/hermes_cli/gateway_connection.py
@@ -79,7 +79,11 @@ class SessionState:
elif kind == "todo.updated" and isinstance(payload, dict):
self.todos = list(payload.get("todos") or [])
elif kind == "session.usage" and isinstance(payload, dict):
- self.apply_usage(payload.get("usage") if isinstance(payload.get("usage"), dict) else payload)
+ # The gateway's frame carries the flat counters next to a nested TUI ``usage`` view this
+ # reader can't map (``session_usage.usage_frame``): the counters win when present.
+ nested = payload.get("usage")
+ flat = isinstance(payload.get("api_calls"), int) or not isinstance(nested, dict)
+ self.apply_usage(payload if flat else nested)
elif kind == "request.cancel" and isinstance(payload, dict):
for listener in list(self.request_listeners):
listener(payload)
diff --git a/tests/hermes_cli/test_gateway_agent.py b/tests/hermes_cli/test_gateway_agent.py
index fd5c15fde58..7b2701fe036 100644
--- a/tests/hermes_cli/test_gateway_agent.py
+++ b/tests/hermes_cli/test_gateway_agent.py
@@ -264,6 +264,25 @@ def test_session_usage_event_feeds_mains_status_bar_attributes(make_agent):
assert agent.get_rate_limit_state() is None
+def test_gateway_session_usage_frame_feeds_the_status_bar_mid_turn(make_agent):
+ """The frame the gateway really publishes (`usage_frame`: flat counters plus the nested TUI
+ `usage` view) lands the flat counters, not the nested view, on main's attributes mid-turn."""
+ from gateway.session_usage import usage_frame
+ usage = dict(SESSION_USAGE, compression_count=2)
+ seen = []
+ agent, _ = make_agent([frame("session.usage", usage_frame(usage), admission=None),
+ frame("message.complete", {"text": "x", "outcome": "completed"})])
+ agent.context_compressor.context_length = 0
+ # Runs after the agent's own listener: what the status bar shows right after the mid-turn frame.
+ agent._state().usage_listeners.append(lambda cached: seen.append((
+ dict(cached), agent.session_api_calls, agent.context_compressor.context_length,
+ agent.context_compressor.compression_count, list(agent._api_latency_history))))
+ agent.run_conversation("q")
+ cached, *bar = seen[0]
+ assert bar == [5, 128000, 2, [0.2]]
+ assert cached["api_calls"] == 5 and "context_max" not in cached
+
+
def test_turn_end_reads_session_stats_cumulative(make_agent):
"""/usage after a turn shows main's session-cumulative totals (session.stats), not the turn's."""
agent, client = make_agent([frame("message.complete", {"text": "x", "outcome": "completed"})],