Skip to content

agent: floor context-size estimate with server-reported prompt_tokens#315444

Merged
bhavyaus merged 1 commit intomainfrom
dev/bhavyau/floor-context-by-server-usage
May 9, 2026
Merged

agent: floor context-size estimate with server-reported prompt_tokens#315444
bhavyaus merged 1 commit intomainfrom
dev/bhavyau/floor-context-by-server-usage

Conversation

@bhavyaus
Copy link
Copy Markdown
Collaborator

@bhavyaus bhavyaus commented May 9, 2026

The local tokenizer undercounts a few content kinds (Anthropic thinking
blocks, per-block JSON envelopes), so compaction can sit at ratio ~0.7
while server prompt_tokens is already over budget and kick-off never
fires.

Floor the local estimate with the previous fetch's usage.prompt_tokens
via a new TurnTokenUsageMetadata (renamed from AnthropicTokenUsageMetadata,
now recorded for all providers). findLast walks turns to cover both
within-turn iterations and iteration 1 of a fresh user turn.

Copilot AI review requested due to automatic review settings May 9, 2026 07:15
The local tokenizer undercounts certain content kinds (notably Anthropic
thinking blocks: the visible "text" is a small summary of much larger
reasoning, signature bytes, etc.) and ignores per-block JSON envelope
tokens. As a result the agent's compaction triggers can sit at ratio
~0.7 while the server's actual prompt_tokens is over the budget, so
neither pre-render nor post-render kick-off fires.

Use the server's reported usage as a floor:

- Rename AnthropicTokenUsageMetadata to TurnTokenUsageMetadata; record
  it for every provider on every successful fetch (drop the
  isAnthropicFamily guard). All providers populate usage.prompt_tokens.
- In AgentIntentInvocation.buildPrompt, walk the conversation turns
  with findLast to pick up the most recent turn that actually fetched
  (covers both within-turn iterations and the iteration-1-of-a-fresh-
  turn case where the current turn has no metadata yet).
- Math.max the local estimate with that server-reported promptTokens
  for contextRatio (telemetry only) and postRenderRatio (gates
  background compaction kick-off).

Post-compaction stays safe: the only behavioral consumer is
postRenderRatio, which is already guarded by the
"didSummarizeThisIteration" check, so a stale pre-compaction floor
cannot trigger a redundant kick-off in the same iteration.

Snapshot drift in defaultIntentRequestHandler.spec.ts.snap is benign:
mocks return usage:{0,0}, so the now-unconditional metadata ends up as
{promptTokens:0, outputTokens:0} on six previously-non-Anthropic
fixtures.
@bhavyaus bhavyaus force-pushed the dev/bhavyau/floor-context-by-server-usage branch from b3238d7 to cb7fbe2 Compare May 9, 2026 07:15
@bhavyaus bhavyaus enabled auto-merge (squash) May 9, 2026 07:15
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

This PR improves agent-mode summarization triggering by flooring the local context-size estimate with server-reported usage.prompt_tokens from the most recent successful fetch, addressing cases where the local tokenizer undercounts certain content kinds and compaction never kicks in despite being over budget.

Changes:

  • Introduces TurnTokenUsageMetadata (renamed/generalized from AnthropicTokenUsageMetadata) and records it for successful fetches across providers.
  • Updates agent intent logic to findLast turn token usage and use it as a floor when computing context ratios for compaction/summarization decisions.
  • Wires token-usage metadata into the returned ChatResult.metadata and updates snapshots accordingly.
Show a summary per file
File Description
extensions/copilot/src/extension/prompt/node/test/snapshots/defaultIntentRequestHandler.spec.ts.snap Updates expected result metadata to include promptTokens/outputTokens.
extensions/copilot/src/extension/prompt/node/defaultIntentRequestHandler.ts Switches result-metadata aggregation to include TurnTokenUsageMetadata.
extensions/copilot/src/extension/prompt/node/chatParticipantRequestHandler.ts Rehydrates token usage metadata from persisted chat history using TurnTokenUsageMetadata.
extensions/copilot/src/extension/prompt/common/conversation.ts Renames/generalizes token-usage metadata class and updates documentation.
extensions/copilot/src/extension/intents/node/toolCallingLoop.ts Records per-turn token usage metadata from fetchResult.usage on success (all providers).
extensions/copilot/src/extension/intents/node/agentIntent.ts Floors local token estimates with latest server prompt-token usage when computing summarization ratios.

Copilot's findings

  • Files reviewed: 6/6 changed files
  • Comments generated: 1

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 9, 2026

blocks-ci screenshots changed

Replace the contents of test/componentFixtures/blocks-ci-screenshots.md with:

Updated blocks-ci-screenshots.md
<!-- auto-generated by CI — do not edit manually -->

#### editor/codeEditor/CodeEditor/Dark
![screenshot](https://hediet-screenshots.azurewebsites.net/images/67bfb687fd2818bd53771a60660541b9ed6f38b80d37da0aac15d267ecaeacec)

#### editor/codeEditor/CodeEditor/Light
![screenshot](https://hediet-screenshots.azurewebsites.net/images/0469dd8d0a587d94a1eaec514c79917b93b9a38694ef2b767bb1892819ae0a55)

#### editor/inlineChatZoneWidget/InlineChatZoneWidget/Dark
![screenshot](https://hediet-screenshots.azurewebsites.net/images/97162fc53c861ee13dc78a18e41fe3a25a42f62dc52a560510ebf084a418e1c3)

#### editor/inlineChatZoneWidget/InlineChatZoneWidget/Light
![screenshot](https://hediet-screenshots.azurewebsites.net/images/3b7e2eb5cc9ba727e2bc1c5113c3e17d8e9a6ce9a37b77519be3716ceb9a9afa)

#### editor/inlineChatZoneWidget/InlineChatZoneWidgetTerminated/Dark
![screenshot](https://hediet-screenshots.azurewebsites.net/images/97162fc53c861ee13dc78a18e41fe3a25a42f62dc52a560510ebf084a418e1c3)

#### editor/inlineChatZoneWidget/InlineChatZoneWidgetTerminated/Light
![screenshot](https://hediet-screenshots.azurewebsites.net/images/3b7e2eb5cc9ba727e2bc1c5113c3e17d8e9a6ce9a37b77519be3716ceb9a9afa)

@bhavyaus bhavyaus merged commit 867957b into main May 9, 2026
24 of 25 checks passed
@bhavyaus bhavyaus deleted the dev/bhavyau/floor-context-by-server-usage branch May 9, 2026 07:32
@vs-code-engineering vs-code-engineering Bot added this to the 1.120.0 milestone May 9, 2026
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.

3 participants