Skip to content

Refactor model selection logic into separate file. Add tons of tests#299210

Merged
lramos15 merged 4 commits intomainfrom
lramos15/ripe-pheasant
Mar 4, 2026
Merged

Refactor model selection logic into separate file. Add tons of tests#299210
lramos15 merged 4 commits intomainfrom
lramos15/ripe-pheasant

Conversation

@lramos15
Copy link
Member

@lramos15 lramos15 commented Mar 4, 2026

The number of user reports about the model picker incorrectly switching from under them is quite high.

This extracts the logic into its own file since it's a bit complex and also adds a ton of tests

Some bugs include

Copilot AI review requested due to automatic review settings March 4, 2026 15:36
@lramos15 lramos15 self-assigned this Mar 4, 2026
@vs-code-engineering vs-code-engineering bot added this to the 1.111.0 milestone Mar 4, 2026
Copy link
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 refactors chat model selection/reset logic out of ChatInputPart into a dedicated chatModelSelectionLogic.ts module and adds extensive unit test coverage to reduce “model picker switches out from under me” issues reported across multi-window/session scenarios.

Changes:

  • Added chatModelSelectionLogic.ts with reusable helpers for model filtering, default selection, persistence restore, sync-state reconciliation, and cache merging.
  • Updated ChatInputPart to use the extracted helpers (model list changes, persisted restore, sync-from-state, validation/reset, cache merge, default selection).
  • Added a large new test suite covering mode/session/inline constraints, persistence semantics, cache merge behavior, and race-like sequences.

Reviewed changes

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

File Description
src/vs/workbench/contrib/chat/browser/widget/input/chatModelSelectionLogic.ts New module encapsulating model selection, filtering, reset, and cache-merge decision logic.
src/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.ts Switched in-widget logic to the new helpers; refactored persistence restore and sync/reset paths.
src/vs/workbench/contrib/chat/test/browser/widget/input/chatModelSelectionLogic.test.ts New, comprehensive unit test suite for the extracted logic and key scenarios/races.
Comments suppressed due to low confidence (2)

src/vs/workbench/contrib/chat/browser/widget/input/chatModelSelectionLogic.ts:32

  • The session-type check hardcodes the string 'local'. Since the codebase already defines localChatSessionType (in chatSessionsService.ts), it would be more robust/maintainable to depend on that constant here to avoid future drift.
	if (sessionType && sessionType !== 'local') {
		return models.filter(entry =>

src/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.ts:953

  • There are now several repeated blocks that rebuild allModels via getLanguageModelIds().map(lookupLanguageModel(...)) (e.g. _syncFromModel, checkModelSupported, hasModelsTargetingSessionType, isModelValidForCurrentSession). Besides allocations, this duplication makes it easy for these call sites to diverge (e.g. around cache inclusion). Consider extracting a single helper (and reusing the same merged live+cache list where needed).
				const allModels = this.languageModelsService.getLanguageModelIds()
					.map(modelId => ({ identifier: modelId, metadata: this.languageModelsService.lookupLanguageModel(modelId)! }));
				const sessionType = this.getCurrentSessionType();
				const syncResult = resolveModelFromSyncState(state.selectedModel, this._currentLanguageModel.get(), allModels, sessionType, {
					location: this.location,
					currentModeKind: this.currentModeKind,
					isInlineChatV2Enabled: !!this.configurationService.getValue(InlineChatConfigKeys.EnableV2),
					sessionType,
				});
				if (syncResult.action === 'apply') {

@lramos15 lramos15 merged commit 98dc3fd into main Mar 4, 2026
20 checks passed
@lramos15 lramos15 deleted the lramos15/ripe-pheasant branch March 4, 2026 16:48
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