Skip to content

The agent that grows with you

fix(gateway): carry the TUI usage view on the session.usage tick

Deepit466ea1a90818
diff --git a/gateway/session_usage.py b/gateway/session_usage.py
index 49a5e9903e1..6b02ffacaea 100644
--- a/gateway/session_usage.py
+++ b/gateway/session_usage.py
@@ -137,6 +137,20 @@ def tui_usage(stats: dict) -> dict:
return usage
+def usage_frame(stats: dict) -> dict:
+ """The ``session.usage`` event payload: BOTH views of the same counters, in one frame.
+
+ ``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
+ ``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
+ viewer from one frame, and leaves the counters untouched for every existing consumer.
+ """
+ return {"usage": tui_usage(stats), **stats}
+
+
def publish_usage(turn, agent, *, record: bool = False) -> None:
"""One ``session.usage`` frame on the viewer's stream; never breaks the turn. At turn end
(``record``) the same dict rides the captured execution result, so the turn's
@@ -145,7 +159,7 @@ def publish_usage(turn, agent, *, record: bool = False) -> None:
return
with contextlib.suppress(Exception):
stats = usage_stats(agent)
- turn._publish_execution("session.usage", stats)
+ turn._publish_execution("session.usage", usage_frame(stats))
if record:
from gateway.session_results import execution_result
captured = execution_result.get()
diff --git a/tests/gateway/test_local_viewer_usage.py b/tests/gateway/test_local_viewer_usage.py
index 8a9f1863e18..8dca3e3458d 100644
--- a/tests/gateway/test_local_viewer_usage.py
+++ b/tests/gateway/test_local_viewer_usage.py
@@ -60,8 +60,15 @@ def test_each_api_step_publishes_usage_only_for_local_viewers(platform):
turn._step_callback_sync(2, [])
kinds = [kind for kind, _ in published]
if platform == Platform.LOCAL:
- usage = next(payload for kind, payload in published if kind == 'session.usage')
- assert set(usage) == FIELDS and usage['api_calls'] == 2
+ from gateway.session_usage import usage_stats
+ from tui_gateway.contracts.registry import EVENTS
+ frame = next(payload for kind, payload in published if kind == 'session.usage')
+ # One frame, both views: the loose counters the classic CLI / TUI mirror reads, and the TUI
+ # view the Desktop status bar reads (without it its meters sit at the empty placeholder).
+ assert {key: value for key, value in frame.items() if key != 'usage'} == usage_stats(_agent())
+ assert frame['api_calls'] == 2
+ assert frame['usage']['calls'] == 2 and frame['usage']['avg_tps'] == 10.0
+ EVENTS['session.usage'].payload.model_validate(frame)
else:
assert 'session.usage' not in kinds
@@ -119,7 +126,28 @@ def test_local_viewer_turn_end_records_stats_for_the_completion():
publish_usage(turn, _agent(), record=True)
finally:
execution_result.reset(token)
- assert captured['session_usage'] == published[-1][1]
+ frame = published[-1][1]
+ # The captured stats stay the loose counters (``completion_payload`` maps them with
+ # ``tui_usage`` for ``message.complete``); only the published frame carries both views.
+ assert captured['session_usage'] == {key: value for key, value in frame.items() if key != 'usage'}
+ assert 'usage' in frame
+
+
+def test_usage_frame_carries_the_tui_view_the_desktop_status_bar_reads():
+ """The Desktop status bar reads only ``payload.usage`` (``lib/statusbar.tsx``): a frame carrying
+ the session-cumulative counters alone leaves its context / cache-hit / tokens-per-second meters
+ at the empty placeholder for the whole turn, because the embedded backend's tick
+ (``server._start_usage_ticker``) is the only producer of the nested view."""
+ from gateway.session_usage import usage_frame, usage_stats
+ from tui_gateway.contracts.registry import EVENTS
+ frame = usage_frame(usage_stats(_agent()))
+ EVENTS['session.usage'].payload.model_validate(frame)
+ # The classic CLI / TUI mirror (``hermes_cli.gateway_agent.apply_session_usage``) reads these.
+ assert frame['tokens_per_s'] == 10.0 and frame['context_length'] == 128000
+ # The Desktop status bar reads these: context bar, cache-hit rate, throughput.
+ usage = frame['usage']
+ assert usage['context_used'] == 900 and usage['context_max'] == 128000 and usage['context_percent'] == 1
+ assert usage['avg_tps'] == 10.0 and usage['cache_hit_pct'] == 9 and usage['compressions'] == 1
def test_turn_end_publishes_the_agents_banner_info():