Skip to content

The agent that grows with you

fix(tui): read a canonical local viewer's session.usage tick

Teknium175d979e1c5d
diff --git a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
index 0f48af0132a..f5bdabb6c33 100644
--- a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
+++ b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
@@ -2268,6 +2268,26 @@ describe('createGatewayEventHandler', () => {
expect(getUiState().usage).toMatchObject({ context_percent: 55, input: 500, total: 540 })
})
+ it('maps a canonical local-viewer tick (top-level session.stats fields) onto the status-bar usage', () => {
+ patchUiState({ sid: 'sess-1', usage: ZERO })
+ const onEvent = createGatewayEventHandler(buildCtx([]))
+
+ onEvent({
+ payload: {
+ api_calls: 2, avg_latency_s: 0.14, cache_read_tokens: 0, compression_count: 1, context_length: 128000,
+ context_source: 'provider_usage', input_tokens: 100, last_prompt_tokens: 900, model: 'fake-model',
+ output_tokens: 40, prompt_tokens: 100, tokens_per_s: 229.4, total_tokens: 140
+ },
+ session_id: 'sess-1',
+ type: 'session.usage'
+ } as any)
+
+ expect(getUiState().usage).toMatchObject({
+ avg_latency_s: 0.1, avg_tps: 229.4, calls: 2, compressions: 1, context_max: 128000,
+ context_percent: 1, context_used: 900, input: 100, output: 40, total: 140
+ })
+ })
+
it('drops a tick for a non-focused session', () => {
patchUiState({ sid: 'focused', usage: ZERO })
const onEvent = createGatewayEventHandler(buildCtx([]))
diff --git a/ui-tui/src/app/createGatewayEventHandler.ts b/ui-tui/src/app/createGatewayEventHandler.ts
index 169fd5a2f85..7d9e36c3e52 100644
--- a/ui-tui/src/app/createGatewayEventHandler.ts
+++ b/ui-tui/src/app/createGatewayEventHandler.ts
@@ -8,6 +8,7 @@ import type { MessageDeltaPayload, SubagentStatus, Usage } from '@hermes/shared/
import { STARTUP_IMAGE, STARTUP_QUERY } from '../config/env.js'
import { STREAM_BATCH_MS } from '../config/timing.js'
import { buildSetupRequiredSections, setupRequiredTitle } from '../content/setup.js'
+import { usageFromTick } from '../domain/usage.js'
import type {
AnyGatewayEvent,
CommandsCatalogResponse,
@@ -1088,7 +1089,8 @@ export function createGatewayEventHandler(ctx: GatewayEventHandlerContext): (ev:
// mid-turn instead of only at message.complete. The session filter at
// the top of this handler already dropped ticks for non-focused
// sessions.
- const usage = ev.payload?.usage
+ // A canonical local viewer's tick carries the owner's counters at the top level.
+ const usage = usageFromTick(ev.payload)
if (usage) {
patchUiState(state => ({ ...state, usage: { ...state.usage, ...usage } }))
diff --git a/ui-tui/src/domain/usage.ts b/ui-tui/src/domain/usage.ts
index 3f3b67efc25..3a923f208c8 100644
--- a/ui-tui/src/domain/usage.ts
+++ b/ui-tui/src/domain/usage.ts
@@ -1,3 +1,84 @@
import type { Usage } from '@hermes/shared/gateway-events'
export const ZERO: Usage = { calls: 0, input: 0, output: 0, total: 0 }
+
+const num = (value: unknown): number | undefined =>
+ typeof value === 'number' && Number.isFinite(value) ? value : undefined
+
+/** A canonical local viewer's `session.usage` tick carries the owner's session-cumulative counters at
+ * the top level (`gateway/session_usage.py::usage_stats`, the `session.stats` dict); main's sidecar
+ * ticker sent its `_get_usage` shape under `usage`. Both land as the same status-bar `Usage`
+ * (`session_usage.tui_usage` is the Python twin used for `message.complete.usage`). */
+export function usageFromTick(payload: unknown): Partial<Usage> | undefined {
+ if (!payload || typeof payload !== 'object') {
+ return undefined
+ }
+
+ const p = payload as Record<string, unknown>
+
+ if (p.usage && typeof p.usage === 'object') {
+ return p.usage as Partial<Usage>
+ }
+
+ if (num(p.api_calls) === undefined && num(p.total_tokens) === undefined) {
+ return undefined
+ }
+
+ const usage: Partial<Usage> = {
+ calls: num(p.api_calls) ?? 0,
+ completion: num(p.completion_tokens) ?? 0,
+ input: num(p.input_tokens) || num(p.prompt_tokens) || 0,
+ output: num(p.output_tokens) || num(p.completion_tokens) || 0,
+ prompt: num(p.prompt_tokens) ?? 0,
+ reasoning: num(p.reasoning_tokens) ?? 0,
+ total: num(p.total_tokens) ?? 0
+ }
+
+ if (typeof p.model === 'string' && p.model) {
+ usage.model = p.model
+ }
+
+ const used = num(p.last_prompt_tokens) ?? 0
+ const max = num(p.context_length) ?? 0
+
+ if (used && max) {
+ const source = typeof p.context_source === 'string' && p.context_source ? p.context_source : 'provider_usage'
+ const clamped = Math.min(used, max)
+
+ Object.assign(usage, {
+ context_estimated: source !== 'provider_usage',
+ context_max: max,
+ context_percent: Math.max(0, Math.min(100, Math.round((clamped / max) * 100))),
+ context_source: source,
+ context_used: clamped
+ })
+ }
+
+ if (p.context_length !== undefined && p.context_length !== null) {
+ usage.compressions = num(p.compression_count) ?? 0
+ }
+
+ const promptTotal = num(p.prompt_tokens) ?? 0
+ const cacheRead = num(p.cache_read_tokens) ?? 0
+
+ if (promptTotal > 0 && cacheRead > 0) {
+ usage.cache_hit_pct = Math.max(0, Math.min(100, Math.round((cacheRead / promptTotal) * 100)))
+ }
+
+ const latency = num(p.avg_latency_s)
+ const tps = num(p.tokens_per_s)
+
+ if (latency !== undefined) {
+ usage.avg_latency_s = Math.round(latency * 10) / 10
+ }
+
+ if (tps !== undefined) {
+ usage.avg_tps = Math.round(tps * 10) / 10
+ }
+
+ if (num(p.active_subagents) !== undefined) {
+ usage.active_subagents = num(p.active_subagents)
+ }
+
+ return usage
+}