Skip to content

fix(elements): bound code block token cache with LRU eviction - #454

Open
ephraimduncan wants to merge 1 commit into
vercel:mainfrom
ephraimduncan:fix/code-block-token-cache-bound
Open

fix(elements): bound code block token cache with LRU eviction#454
ephraimduncan wants to merge 1 commit into
vercel:mainfrom
ephraimduncan:fix/code-block-token-cache-bound

Conversation

@ephraimduncan

Copy link
Copy Markdown

Problem

tokensCache in packages/elements/src/code-block.tsx is module-scoped and never evicts. Streaming call sites (ToolInput / ToolOutput in tool.tsx) render <CodeBlock> with a growing JSON string, and every intermediate version gets a unique cache key (the key includes code.length), so each streamed frame's full ThemedToken[][] was retained for the lifetime of the page. Long conversations grew tab memory without bound.

Fix

  • Cap tokensCache at 100 entries (MAX_TOKENS_CACHE_SIZE).
  • LRU semantics: cache hits refresh recency (delete + re-set); inserts at capacity evict the oldest entry, guarded against same-key overwrites miscounting.
  • highlighterCache is untouched — it's naturally bounded by the finite set of loaded languages.

Verification

  • New regression test in __tests__/code-block.test.tsx: fills 100 unique entries, refreshes one, overflows with 99 more, then asserts the refreshed entry is still served synchronously from cache while an unrefreshed entry returns null (evicted).
  • Test fails without the fix (verified via stash-revert run) and passes with it — 10/10 in Chromium.
  • tsc clean for the changed file; ultracite reports 0 warnings/errors.

Streaming tool input/output re-highlights every intermediate version of a
code string, and each unique string was cached forever in the module-scoped
tokensCache, growing tab memory without limit during long conversations.

Cap the cache at 100 entries: refresh recency on hit, evict the oldest
insertion when full. The language highlighter cache stays unbounded since
it is naturally limited by the set of loaded languages.
@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