Skip to content

The agent that grows with you

fix(desktop): normalize canonical usage ticks

KoNit-Kabb5b43f1b85
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.ts
index 74939870feb..62231fba944 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.ts
@@ -31,6 +31,7 @@ import {
setYoloActive
} from '@/store/session'
import { reportInstallMethodWarning } from '@/store/updates'
+import type { UsageStats } from '@/types/hermes'
import {
applySessionInfoStatePatch,
@@ -41,6 +42,62 @@ import {
import type { GatewayEventContext } from './types'
+/** Normalize canonical top-level counters; legacy ticker frames stay nested. */
+function canonicalUsage(payload: GatewayEventContext['payload']): Partial<UsageStats> | undefined {
+ if (!payload || payload.usage) {
+ return payload?.usage
+ }
+
+ const number = (value: unknown): number | undefined =>
+ typeof value === 'number' && Number.isFinite(value) ? value : undefined
+ const input = number(payload.input_tokens) ?? number(payload.prompt_tokens)
+ const output = number(payload.output_tokens)
+ const total = number(payload.total_tokens)
+ const calls = number(payload.api_calls)
+ const prompt = number(payload.prompt_tokens)
+ const cacheRead = number(payload.cache_read_tokens)
+ const contextMax = number(payload.context_length)
+ const contextUsed = number(payload.last_prompt_tokens)
+ const tokensPerSecond = number(payload.tokens_per_s)
+ const compressionCount = number(payload.compression_count)
+
+ if ([input, output, total, calls, prompt, cacheRead, contextMax, contextUsed, tokensPerSecond].every(value => value === undefined)) {
+ return undefined
+ }
+
+ const usage: Partial<UsageStats> = {}
+ if (input !== undefined) {
+ usage.input = input
+ }
+ if (output !== undefined) {
+ usage.output = output
+ }
+ if (total !== undefined) {
+ usage.total = total
+ }
+ if (calls !== undefined) {
+ usage.calls = calls
+ }
+ if (contextMax !== undefined && contextUsed !== undefined && contextUsed > 0) {
+ usage.context_used = Math.min(contextUsed, contextMax)
+ usage.context_max = contextMax
+ usage.context_percent = Math.max(0, Math.min(100, Math.round((contextUsed / contextMax) * 100)))
+ usage.context_source = payload.context_source ?? 'provider_usage'
+ usage.context_estimated = usage.context_source !== 'provider_usage'
+ }
+ if (compressionCount !== undefined && contextMax !== undefined) {
+ usage.compressions = compressionCount
+ }
+ if (prompt !== undefined && prompt > 0 && cacheRead !== undefined && cacheRead > 0) {
+ usage.cache_hit_pct = Math.max(0, Math.min(100, Math.round((cacheRead / prompt) * 100)))
+ }
+ if (tokensPerSecond !== undefined) {
+ usage.avg_tps = Math.round(tokensPerSecond * 10) / 10
+ }
+
+ return usage
+}
+
/**
* Whether a `session.info` payload's `stored_session_id` may be treated as the
* selected conversation's, so its cwd and branch can be claimed for it
@@ -535,16 +592,18 @@ export function handleSessionInfoEvent(ctx: GatewayEventContext): boolean {
// Live usage tick emitted while a turn is mid-flight (see tui_gateway
// _start_usage_ticker) so the status-bar context window tracks growth
// during the turn instead of only jumping at message.complete.
- if (payload?.usage && sessionId) {
+ const usage = canonicalUsage(payload)
+
+ if (usage && sessionId) {
// Per-session twin first: a focused secondary tile reads this cache,
// while the primary-only global mirrors the active session.
updateSessionState(sessionId, state => ({
...state,
- usage: { calls: 0, input: 0, output: 0, total: 0, ...state.usage, ...payload.usage }
+ usage: { calls: 0, input: 0, output: 0, total: 0, ...state.usage, ...usage }
}))
if (isActiveEvent) {
- setCurrentUsage(current => ({ ...current, ...payload.usage }))
+ setCurrentUsage(current => ({ ...current, ...usage }))
}
}
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/usage.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/usage.test.tsx
index 6c995bbebfd..cd1ff2eba71 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/usage.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/usage.test.tsx
@@ -52,6 +52,37 @@ describe('useMessageStream status-bar usage scoping', () => {
})
})
+ it('normalizes a canonical top-level session.usage tick for the status bar', () => {
+ mountStream()
+
+ act(() =>
+ stream.handleEvent({
+ payload: {
+ api_calls: 3,
+ cache_read_tokens: 800,
+ input_tokens: 1_200,
+ output_tokens: 80,
+ prompt_tokens: 1_000,
+ tokens_per_s: 41.64,
+ total_tokens: 1_280
+ },
+ session_id: SID,
+ type: 'session.usage'
+ })
+ )
+
+ expect($currentUsage.get()).toEqual({
+ ...BASELINE,
+ avg_tps: 41.6,
+ cache_hit_pct: 80,
+ calls: 3,
+ input: 1_200,
+ output: 80,
+ total: 1_280
+ })
+ expect(sessionStates.get(SID)?.usage).toEqual($currentUsage.get())
+ })
+
it('caches a background session.usage tick without overwriting the primary status bar', () => {
mountStream()
diff --git a/apps/desktop/src/lib/chat-messages/types.ts b/apps/desktop/src/lib/chat-messages/types.ts
index 0a2844b3249..5ead7c793d9 100644
--- a/apps/desktop/src/lib/chat-messages/types.ts
+++ b/apps/desktop/src/lib/chat-messages/types.ts
@@ -125,6 +125,19 @@ export type GatewayEventPayload = {
install_warning?: string
personality?: string
usage?: Partial<UsageStats>
+ // Canonical session.usage ticks carry cumulative counters at the payload
+ // top level; older ticker frames nest the desktop Usage shape above.
+ api_calls?: number | null
+ cache_read_tokens?: number | null
+ compression_count?: number | null
+ context_length?: number | null
+ context_source?: string | null
+ input_tokens?: number | null
+ last_prompt_tokens?: number | null
+ output_tokens?: number | null
+ prompt_tokens?: number | null
+ tokens_per_s?: number | null
+ total_tokens?: number | null
// agent.terminal.output — live chunk for a read-only agent terminal tab
process_id?: string
chunk?: string