Skip to content

fix(elements): clamp context usage percentage to valid range - #461

Open
ephraimduncan wants to merge 1 commit into
vercel:mainfrom
ephraimduncan:fix/context-clamp-percent
Open

fix(elements): clamp context usage percentage to valid range#461
ephraimduncan wants to merge 1 commit into
vercel:mainfrom
ephraimduncan:fix/context-clamp-percent

Conversation

@ephraimduncan

Copy link
Copy Markdown

Problem

Context computes usedTokens / maxTokens unguarded in three places (ContextIcon, ContextTrigger, ContextContentHeader):

  • maxTokens={0} with usedTokens={0} renders NaN% and passes NaN as the ring's strokeDashoffset
  • maxTokens={0} with positive usage renders ∞%
  • Over-budget usage (e.g. 150/100) renders 150%, overdraws the ring, and the Progress indicator's translateX(-${100 - value}%) becomes translateX(50%) — the bar looks empty exactly when the context is most full
  • Negative token values render outside the meaningful range

Fix

One shared clampUsedPercent helper applied to all three readouts:

  • non-finite input or maxTokens <= 0 → stable 0% empty state
  • otherwise the ratio clamped to [0, 1], so over-budget usage shows a full ring/bar instead of visually resetting

No transitions added — high-frequency streaming updates stay immediate.

Tests

Extended __tests__/context.test.tsx with the degenerate cases (0,0), (50,0), (-1,100) (bounded 0% text, empty ring) and the over-budget case (150,100) (100% text, zero dash offset, full progress bar). All 31 tests pass.

@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

@ephraimduncan is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant