Skip to content

Fix context size handling for Agent Host#322470

Open
pwang347 wants to merge 17 commits into
mainfrom
pawang/ahpContext
Open

Fix context size handling for Agent Host#322470
pwang347 wants to merge 17 commits into
mainfrom
pawang/ahpContext

Conversation

@pwang347

@pwang347 pwang347 commented Jun 22, 2026

Copy link
Copy Markdown
Member

Fixes #321707

Notes:

  • Agent host context in context widget now correctly matches input + output tokens (we were setting output to always 0)
  • Switching models will not also update the context widget, even if it wasn't actually used for the previous request

Dependent on AHP changes:

Copilot AI review requested due to automatic review settings June 22, 2026 22:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes context size handling for Agent Host-backed chat models by representing the selected context window as a first-class ModelSelection.maxContextWindow value (token count) rather than smuggling it through the string-only config bag. It also introduces recommendedContextWindows on models so the chat UI can present a context-size picker consistently across agents, while letting each agent map the numeric selection to its backend representation (e.g. Copilot SDK contextTier).

Changes:

  • Add recommendedContextWindows to model info and maxContextWindow to model selection in the agent-host protocol types, and plumb the new fields through side effects.
  • Synthesize a tokens-group “Context Size” picker from recommendedContextWindows, and lift the selected numeric value into ModelSelection.maxContextWindow when creating sessions.
  • Update Copilot agent/session launching to resolve SDK contextTier from the selected context window, and adjust tests accordingly.
Show a summary per file
File Description
src/vs/workbench/contrib/chat/test/browser/agentSessions/agentHostChatContribution.test.ts Adds coverage for lifting context size into maxContextWindow and for synthesizing the picker from recommendedContextWindows.
src/vs/workbench/contrib/chat/browser/agentSessions/agentHost/agentHostSessionHandler.ts Lifts tokens-group numeric config values into ModelSelection.maxContextWindow and includes it in selection equality checks.
src/vs/workbench/contrib/chat/browser/agentSessions/agentHost/agentHostLanguageModelProvider.ts Synthesizes a “Context Size” configuration property from recommendedContextWindows.
src/vs/platform/agentHost/test/node/copilotAgent.test.ts Updates model schema expectations and adds a materialization test for mapping selected context window → SDK tier.
src/vs/platform/agentHost/test/node/agentSideEffects.test.ts Updates model fixtures to include recommendedContextWindows.
src/vs/platform/agentHost/node/copilot/copilotSessionLauncher.ts Moves contextTier derivation into the agent and threads it through launch plans.
src/vs/platform/agentHost/node/copilot/copilotAgent.ts Exposes recommendedContextWindows, accepts maxContextWindow on selections, and resolves SDK contextTier robustly.
src/vs/platform/agentHost/node/agentSideEffects.ts Plumbs recommendedContextWindows into side-effect output.
src/vs/platform/agentHost/common/state/protocol/channels-root/state.ts Extends protocol state types with recommendedContextWindows and ModelSelection.maxContextWindow.
src/vs/platform/agentHost/common/agentService.ts Extends IAgentModelInfo with recommendedContextWindows.

Copilot's findings

  • Files reviewed: 10/10 changed files
  • Comments generated: 3

Comment thread src/vs/platform/agentHost/node/copilot/copilotSessionLauncher.ts Outdated
Comment thread src/vs/platform/agentHost/common/state/protocol/channels-root/state.ts Outdated
* directly. Replaced by the numeric {@link ContextSizeConfigKey}; still read from persisted sessions
* for backward compatibility.
*/
export const ContextTierConfigKey = 'contextTier';

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@roblourens thoughts on whether we need to keep this for persisted sessions?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's not clear to me, what data is being persisted that uses this key?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I believe the model config is persisted indirectly as part of src/vs/platform/agentHost/node/sessionDatabase.ts

So old chat sessions would try to restore with this key

Comment thread src/vs/platform/agentHost/common/state/protocol/.ahp-version Outdated
@pwang347 pwang347 marked this pull request as ready for review June 23, 2026 22:55

@roblourens roblourens left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks fine in general

* denominator, so switching models updates the widget before the next
* request is sent. The usage numerator still comes from the last response.
*/
private _selectedModelId: string | undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's the difference between selectedModelId and currentModelId?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

currentModelId is the actual model that was last executed, I think we still want to track it at some level instead of just writing over it. Wanted to sync with @lramos15 before actually fully removing.

It should be possible to preview changes to the context when switching models though

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

FWIW I chatted with Logan and we agreed on the new mental model (the widget should show the newly selected model as a preview, instead of the historic content)

* directly. Replaced by the numeric {@link ContextSizeConfigKey}; still read from persisted sessions
* for backward compatibility.
*/
export const ContextTierConfigKey = 'contextTier';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's not clear to me, what data is being persisted that uses this key?

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.

AHP: Context window mismatch between the context size picker and the context usage widget

4 participants