Skip to content

Fix whitespace-only text block from orphaned CacheBreakpoint#4839

Merged
bhavyaus merged 1 commit intomainfrom
dev/bhavyau/fix-whitespace-cache-breakpoint
Mar 30, 2026
Merged

Fix whitespace-only text block from orphaned CacheBreakpoint#4839
bhavyaus merged 1 commit intomainfrom
dev/bhavyau/fix-whitespace-cache-breakpoint

Conversation

@bhavyaus
Copy link
Copy Markdown
Contributor

@bhavyaus bhavyaus commented Mar 30, 2026

Problem

Fixes microsoft/vscode#305956

Root Cause

In rawContentToAnthropicContent(), the CacheBreakpoint handler's else branch (when no preceding block exists):

// Old code
convertedContent.push({ type: 'text', text: ' ', cache_control: { type: 'ephemeral' } });

Fix

Instead of creating an invalid placeholder, defer the cache_control to the next cacheable content block that gets added. If no subsequent block exists, silently drop it — there's nothing to cache anyway.

  • pendingCacheControl flag tracks orphaned breakpoints internally
  • After each content block is pushed, attach any pending cache_control to it
  • No change to the function's return type or callers
  • No prompt cache regression: the "defer forward" behavior actually improves cache placement (cache covers real content, not a useless space token)

Test Coverage

  • New test: cache breakpoint before content defers cache_control to next block — verified it fails on old code (2 blocks including whitespace placeholder) and passes on new code (1 block with correct cache_control)
  • Updated test: cache_control-only tool content — orphaned breakpoint with no content is now silently dropped (impossible in practice since addCacheBreakpoints only targets messages with content)
  • All 31 existing tests pass

Copilot AI review requested due to automatic review settings March 30, 2026 21:20
When a CacheBreakpoint had no preceding content block to attach
cache_control to, rawContentToAnthropicContent created a placeholder
{ type: 'text', text: ' ' } block. The Anthropic API rejects
whitespace-only text blocks with 'text content block must contain
non-whitespace text'.

This happens in long conversations after prompt-tsx prunes content to
fit the token budget, leaving a CacheBreakpoint with no real content
before it.

Fix: defer the cache_control to the next cacheable content block. If
no subsequent block exists, silently drop it (nothing to cache anyway).

Fixes microsoft/vscode#305956
@bhavyaus bhavyaus force-pushed the dev/bhavyau/fix-whitespace-cache-breakpoint branch from 5f638d0 to ecd80ad Compare March 30, 2026 21:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an Anthropic Messages API validation error triggered when prompt pruning leaves an orphaned CacheBreakpoint, which previously produced an invalid whitespace-only text block.

Changes:

  • Update rawContentToAnthropicContent() to defer orphaned cache_control forward to the next cacheable content block (or drop it if none appears).
  • Update/extend unit tests to cover the new defer-forward behavior and the “drop if no content” case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/platform/endpoint/node/messagesApi.ts Replaces whitespace placeholder block with a pending/deferred cache_control mechanism to avoid invalid Anthropic text blocks.
src/platform/endpoint/test/node/messagesApi.spec.ts Updates the cache_control-only tool test expectation and adds a new test verifying deferred cache_control attachment to the next block.

@bhavyaus bhavyaus enabled auto-merge March 30, 2026 21:27
@bhavyaus bhavyaus added this pull request to the merge queue Mar 30, 2026
Merged via the queue into main with commit 64bb2b3 Mar 30, 2026
20 checks passed
@bhavyaus bhavyaus deleted the dev/bhavyau/fix-whitespace-cache-breakpoint branch March 30, 2026 21:58
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.

Getting an error that "messages: text content block must contain non-whitespace text" even though I'm sending a regular text instruction.

3 participants