The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): own busy-stale context breakdown rule in useContextBreakdown (#70871)
kshitijk4poor32385a905bc4
diff --git a/apps/desktop/src/app/shell/hooks/use-context-breakdown.ts b/apps/desktop/src/app/shell/hooks/use-context-breakdown.tsindex 0ae2e573d8e..881c4302cb6 100644--- a/apps/desktop/src/app/shell/hooks/use-context-breakdown.ts+++ b/apps/desktop/src/app/shell/hooks/use-context-breakdown.ts@@ -62,7 +62,9 @@ export function useContextBreakdown({ busy, enabled, requestGateway, sessionId }}, [busy, enabled, requestGateway, sessionId])return {- breakdown: fetched && fetched.sessionId === sessionId ? fetched.breakdown : null,+ // The effect clears `fetched` only after commit, so gate on `busy` here too:+ // the first busy render must not hand out the pre-turn snapshot.+ breakdown: !busy && fetched?.sessionId === sessionId ? fetched.breakdown : null,loading}}diff --git a/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx b/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsxindex c637236a097..07d2aa0e646 100644--- a/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx+++ b/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx@@ -74,43 +74,13 @@ import {$updateStatus,openUpdateOverlayFor} from '@/store/updates'-import type { ContextBreakdown, StatusResponse, UsageStats } from '@/types/hermes'+import type { 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@@ -327,14 +297,26 @@ export function useStatusbarItems({sessionId: activeSessionId})- // 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).+ // 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 useContextBreakdown returns null (the+ // snapshot is pre-turn), so the streamed usage carries the gauge.const gaugeUsage = useMemo<UsageStats>(- () => resolveContextGaugeUsage({ busy, breakdown: contextBreakdown, current: currentUsage }),- [busy, contextBreakdown, currentUsage]+ () =>+ 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])const contextUsage = useMemo(() => usageContextLabel(gaugeUsage), [gaugeUsage])