Skip to content

The agent that grows with you

fix(usage-window): the status bar drops the plan gauge once a reply arrives off a window plan

Tekniumfeecf7cbd269
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
index 54f2e0d62ae..92392b292c9 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
@@ -1,4 +1,4 @@
-import type { BillingBlock } from '@hermes/shared'
+import { type BillingBlock, mergeUsage } from '@hermes/shared'
import { burstVibeHearts } from '@/components/chat/vibe-hearts'
import { translateNow } from '@/i18n'
@@ -442,11 +442,11 @@ export function handleMessageStreamEvent(ctx: GatewayEventContext): boolean {
// let a background tile's turn overwrite the primary's count.
updateSessionState(sessionId, state => ({
...state,
- usage: { calls: 0, input: 0, output: 0, total: 0, ...state.usage, ...payload.usage }
+ usage: mergeUsage({ calls: 0, input: 0, output: 0, total: 0, ...state.usage }, payload.usage)
}))
if (isActiveEvent) {
- setCurrentUsage(current => ({ ...current, ...payload.usage }))
+ setCurrentUsage(current => mergeUsage(current, payload.usage))
}
}
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 3ea0b9af6d3..d445ddcd941 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
@@ -1,4 +1,4 @@
-import { usageFromTick } from '@hermes/shared'
+import { mergeUsage, usageFromTick } from '@hermes/shared'
import { finalizeInterruptedMessages } from '@/lib/chat-messages'
import { normalizePersonalityValue } from '@/lib/chat-runtime'
@@ -307,8 +307,7 @@ export function handleSessionInfoEvent(ctx: GatewayEventContext): boolean {
return {
...nextState,
usage: {
- ...previousUsage,
- ...payload.usage,
+ ...mergeUsage({ ...previousUsage }, payload.usage),
calls: payload.usage.calls ?? previousUsage?.calls ?? 0,
// session.info is authoritative: omission means unavailable, not
// "reuse the previous runtime's compression count".
@@ -510,8 +509,7 @@ export function handleSessionInfoEvent(ctx: GatewayEventContext): boolean {
if (payload?.usage && (!explicitSid || isActiveEvent)) {
const usage = payload.usage
setCurrentUsage(current => ({
- ...current,
- ...usage,
+ ...mergeUsage(current, usage),
compressions: usage.compressions
}))
}
@@ -547,11 +545,11 @@ export function handleSessionInfoEvent(ctx: GatewayEventContext): boolean {
// while the primary-only global mirrors the active session.
updateSessionState(sessionId, state => ({
...state,
- usage: { calls: 0, input: 0, output: 0, total: 0, ...state.usage, ...usage }
+ usage: mergeUsage({ calls: 0, input: 0, output: 0, total: 0, ...state.usage }, usage)
}))
if (isActiveEvent) {
- setCurrentUsage(current => ({ ...current, ...usage }))
+ setCurrentUsage(current => mergeUsage(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 eb60e0dc918..b38a1541949 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
@@ -314,6 +314,29 @@ describe('useMessageStream status-bar usage scoping', () => {
expect($currentUsage.get()).toEqual({ calls: 3, compressions: 3, input: 1500, output: 90, total: 1590 })
})
+ it('drops the plan gauge once a complete usage payload omits it, but keeps it across partial ticks', () => {
+ const gauge = { id: 'day', label: 'Today', resets_at_ms: null, used_bp: 4_200 }
+ sessionStates.set(SID, { ...createClientSessionState(), usage: { ...BASELINE, plan_gauge: gauge } })
+ $currentUsage.set({ ...BASELINE, plan_gauge: gauge })
+ mountStream()
+
+ // A partial tick never carries the gauge; it must not flicker off.
+ act(() => stream.handleEvent({ payload: { usage: { input: 900 } }, session_id: SID, type: 'session.usage' }))
+ expect($currentUsage.get().plan_gauge).toEqual(gauge)
+
+ // Both usage builders always send `calls` and send `plan_gauge` only on a window plan.
+ const { usage: complete } = GATEWAY_FRAME
+ act(() =>
+ stream.handleEvent({ payload: { text: 'done', usage: complete }, session_id: SID, type: 'message.complete' })
+ )
+ expect($currentUsage.get().plan_gauge).toBeUndefined()
+ expect(sessionStates.get(SID)?.usage?.plan_gauge).toBeUndefined()
+
+ $currentUsage.set({ ...BASELINE, plan_gauge: gauge })
+ act(() => stream.handleEvent({ payload: GATEWAY_FRAME, session_id: SID, type: 'session.usage' }))
+ expect($currentUsage.get().plan_gauge).toBeUndefined()
+ })
+
it('ignores message.complete usage from a background session', () => {
mountStream()
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
index 17707c676ef..fb12b3dd130 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
@@ -1,5 +1,5 @@
import { skillInvocationText } from '@hermes/shared'
-import { parseCommandDispatch, parseSlashCommand } from '@hermes/shared'
+import { mergeUsage, parseCommandDispatch, parseSlashCommand } from '@hermes/shared'
import { type MutableRefObject, useCallback, useRef } from 'react'
import { mergeOlderTranscriptPage } from '@/app/chat/transcript-backfill'
@@ -922,7 +922,7 @@ export function useSlashCommand(deps: SlashCommandDeps) {
const usage = { ...result?.usage, ...result?.info?.usage }
if (Object.keys(usage).length && activeSessionIdRef.current === sessionId) {
- setCurrentUsage(current => ({ ...current, ...usage }))
+ setCurrentUsage(current => mergeUsage(current, usage))
}
if (result?.info?.title !== undefined) {
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index 440381ae486..75a4e608afb 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -1,3 +1,4 @@
+import { mergeUsage } from '@hermes/shared'
import { useStore } from '@nanostores/react'
import { type MutableRefObject, useCallback, useEffect, useRef } from 'react'
import type { NavigateFunction } from 'react-router'
@@ -605,12 +606,7 @@ export function useSessionActions({
setSelectedStoredSessionId(null)
selectedStoredSessionIdRef.current = null
setMessages([])
- setCurrentUsage({
- calls: 0,
- input: 0,
- output: 0,
- total: 0
- })
+ setCurrentUsage({ calls: 0, input: 0, output: 0, total: 0 })
setSessionStartedAt(null)
setTurnStartedAt(null)
// The composer's model/effort/fast is sticky UI state (persisted in
@@ -1576,7 +1572,7 @@ export function useSessionActions({
}
if (usage) {
- setCurrentUsage(current => ({ ...current, ...usage, compressions: usage.compressions }))
+ setCurrentUsage(current => ({ ...mergeUsage(current, usage), compressions: usage.compressions }))
}
publishDegradedWarmCache()
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
index b6b3ea122ed..511010ee004 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
@@ -1,3 +1,5 @@
+import { mergeUsage } from '@hermes/shared'
+
import { resolveSessionRpcOwner } from '@/app/contrib/wiring-routing'
import { textWithoutReferenceLines } from '@/components/assistant-ui/reference-kinds'
import { getSession } from '@/hermes'
@@ -2521,10 +2523,8 @@ export function applyRuntimeInfo(
publishRuntimeToComposer(sessionState)
if (info.usage) {
- // session.info/session.resume is an authoritative session snapshot, not a
- // partial live tick. Clear a missing compression count so switching from
- // a counted session to an older/cold runtime cannot leak the old value.
- setCurrentUsage(current => ({ ...current, ...info.usage, compressions: info.usage?.compressions }))
+ // session.info/resume is an authoritative snapshot: an omitted compression count or plan gauge clears.
+ setCurrentUsage(current => ({ ...mergeUsage(current, info.usage), compressions: info.usage?.compressions }))
}
}
diff --git a/apps/shared/src/index.ts b/apps/shared/src/index.ts
index 058010e4055..3bf687a2663 100644
--- a/apps/shared/src/index.ts
+++ b/apps/shared/src/index.ts
@@ -193,7 +193,7 @@ export {
} from './translucency'
export { type UpdateDebt, updateDebt, type UpdateDebtReceipt, type UpdateDebtStep } from './update-debt'
-export { usageFromTick, type UsageTickPayload } from './usage-tick'
+export { mergeUsage, usageFromTick, type UsageTickPayload } from './usage-tick'
export {
buildHermesWebSocketUrl,
type GatewayAuthMode,
diff --git a/apps/shared/src/usage-tick.ts b/apps/shared/src/usage-tick.ts
index f0b4310d132..c46107db2d0 100644
--- a/apps/shared/src/usage-tick.ts
+++ b/apps/shared/src/usage-tick.ts
@@ -1,4 +1,4 @@
-import type { SessionStats, SessionUsagePayload, Usage } from './gateway-contract.generated.js'
+import type { PlanGauge, SessionStats, SessionUsagePayload, Usage } from './gateway-contract.generated.js'
/** The owner's `session.usage` tick (`SessionUsagePayload`, canonical counters at the top level or the classic sidecar ticker's nested `usage`) or its `session.stats` reply (`SessionStats`). */
export type UsageTickPayload = SessionStats | SessionUsagePayload
@@ -20,6 +20,7 @@ export interface TickUsage {
input: number
model?: string
output: number
+ plan_gauge?: PlanGauge
prompt: number
reasoning: number
total: number
@@ -126,7 +127,11 @@ export function usageFromTick<Nested = Usage>(
}
}
- const derived = { ...usage, ...contextFields(payload), ...rateFields(payload) }
+ const derived: Partial<TickUsage> = { ...usage, ...contextFields(payload), ...rateFields(payload) }
+
+ if (payload.plan_gauge) {
+ derived.plan_gauge = payload.plan_gauge
+ }
if (Object.keys(derived).length === 0) {
return undefined
@@ -134,3 +139,17 @@ export function usageFromTick<Nested = Usage>(
return payload.model ? { ...derived, model: payload.model } : derived
}
+
+/** Merge a usage payload into the stored one: a partial tick keeps the fields it omits, but the plan gauge follows the latest complete payload. Both builders (`gateway/session_usage.py::tui_usage`, `tui_gateway/server.py::_get_usage`) always send `calls` and send `plan_gauge` only on a window plan, so a complete payload without it means the session is off the plan (or the allowance reset) and the old gauge goes. */
+export function mergeUsage<P extends object, N extends { calls?: number; plan_gauge?: unknown }>(
+ previous: P,
+ next: N | null | undefined
+): P & Partial<N> {
+ const merged = { ...previous, ...next }
+
+ if (next?.calls !== undefined && next.plan_gauge == null) {
+ delete (merged as { plan_gauge?: unknown }).plan_gauge
+ }
+
+ return merged
+}