The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): show live streamed context gauge during turns (frozen-gauge fix)
Albert.Zhouc4bb76a132a1
diff --git a/apps/desktop/src/app/shell/hooks/use-statusbar-items.gauge.test.tsx b/apps/desktop/src/app/shell/hooks/use-statusbar-items.gauge.test.tsxnew file mode 100644index 00000000000..ce729655305--- /dev/null+++ b/apps/desktop/src/app/shell/hooks/use-statusbar-items.gauge.test.tsx@@ -0,0 +1,60 @@+import { describe, expect, it } from 'vitest'++import type { ContextBreakdown, UsageStats } from '@/types/hermes'++import { resolveContextGaugeUsage } from './use-statusbar-items'++// Regression for #70871's frozen-gauge half: while a turn is streaming the+// pre-turn context breakdown must NOT overlay the live streamed usage —+// otherwise the bar stays frozen at the turn-start value and jumps at turn end.+const CURRENT: UsageStats = {+ calls: 1,+ context_max: 1_000_000,+ context_percent: 27,+ context_used: 267_700,+ input: 10_000,+ output: 5_000,+ total: 15_000+}++const BREAKDOWN: ContextBreakdown = {+ categories: [],+ context_max: 1_000_000,+ context_percent: 30,+ context_used: 320_000,+ estimated_total: 350_000,+ model: 'test-model'+}++describe('resolveContextGaugeUsage', () => {+ it('keeps the live streamed usage mid-turn even when a breakdown is held', () => {+ const out = resolveContextGaugeUsage({ busy: true, breakdown: BREAKDOWN, current: CURRENT })++ expect(out.context_used).toBe(267_700)+ expect(out.context_percent).toBe(27)+ expect(out.input).toBe(10_000)+ })++ it('falls back to the streamed usage mid-turn when no breakdown is held', () => {+ const out = resolveContextGaugeUsage({ busy: true, breakdown: null, current: CURRENT })++ expect(out).toBe(CURRENT)+ })++ it('lets the breakdown win when the turn is idle', () => {+ const out = resolveContextGaugeUsage({ busy: false, breakdown: BREAKDOWN, current: CURRENT })++ expect(out.context_used).toBe(320_000)+ expect(out.context_percent).toBe(30)+ expect(out.context_max).toBe(1_000_000)+ // Non-context fields still come from the streamed usage object.+ expect(out.input).toBe(10_000)+ expect(out.calls).toBe(1)+ })++ it('keeps the streamed usage when idle without a breakdown', () => {+ const out = resolveContextGaugeUsage({ busy: false, breakdown: undefined, current: CURRENT })++ expect(out).toBe(CURRENT)+ })+})diff --git a/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx b/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsxindex 1421a6c7eed..c637236a097 100644--- a/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx+++ b/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx@@ -74,13 +74,43 @@ import {$updateStatus,openUpdateOverlayFor} from '@/store/updates'-import type { StatusResponse, UsageStats } from '@/types/hermes'+import type { ContextBreakdown, StatusResponse, UsageStats } from '@/types/hermes'import { CRON_ROUTE, SETTINGS_ROUTE, WEBHOOKS_ROUTE } from '../../routes'import type { StatusbarItem } from '../statusbar-controls'const EMPTY_USAGE: UsageStats = { calls: 0, input: 0, output: 0, total: 0 }+/**+ * Pick which usage figure the context gauge should paint.+ *+ * While a turn is in flight the backend streams live ``session.usage``+ * snapshots (tui_gateway/server.py `_start_usage_ticker`, 1s interval) and+ * the pre-turn breakdown would freeze the bar at the turn-start value — the+ * "context meter frozen during streaming, jumps at turn end" bug (#70871,+ * named in the #87903 discussion). Streamed usage wins mid-turn. Only when+ * the turn is idle does the breakdown win: a resumed session's streamed+ * store has no context fields, and the idle breakdown answers for it.+ */+export function resolveContextGaugeUsage(opts: {+ busy: boolean+ breakdown: ContextBreakdown | null | undefined+ current: UsageStats+}): UsageStats {+ const { busy, breakdown, current } = opts++ return !busy && breakdown+ ? {+ ...current,+ context_estimated: breakdown.context_estimated,+ context_source: breakdown.context_source,+ context_max: breakdown.context_max,+ context_percent: breakdown.context_percent,+ context_used: breakdown.context_used+ }+ : current+}+interface StatusbarItemsOptions {agentsOpen: booleanchatOpen: boolean@@ -297,26 +327,14 @@ export function useStatusbarItems({sessionId: activeSessionId})- // The breakdown wins whenever we have one, for two reasons: it reports the- // MEASURED occupancy once the backend has it (falling back to the estimate- // only before that), and it is keyed to the session it describes. The global- // `$currentUsage` is neither — a resumed session reports no context fields,- // and the store merges rather than replaces, so the PREVIOUS session's gauge- // numbers survive the switch. Mid-turn there's no breakdown by design and- // the streamed usage carries the gauge.+ // NOTE: the original "breakdown wins whenever we have one" is now gated on+ // `busy` — the breakdown could report MEASURED occupancy (or an estimate)+ // keyed to the session it describes, but it is a PRE-TURN value once a turn+ // is streaming, and overlaying it would freeze the live streamed gauge.+ // Mid-turn the streamed usage carries the gauge (see resolveContextGaugeUsage).const gaugeUsage = useMemo<UsageStats>(- () =>- contextBreakdown- ? {- ...currentUsage,- context_estimated: contextBreakdown.context_estimated,- context_source: contextBreakdown.context_source,- context_max: contextBreakdown.context_max,- context_percent: contextBreakdown.context_percent,- context_used: contextBreakdown.context_used- }- : currentUsage,- [contextBreakdown, currentUsage]+ () => resolveContextGaugeUsage({ busy, breakdown: contextBreakdown, current: currentUsage }),+ [busy, contextBreakdown, currentUsage])const contextUsage = useMemo(() => usageContextLabel(gaugeUsage), [gaugeUsage])