Skip to content

Manage ChatModels with ref-counting, allow running in bg - #278476

Merged
roblourens merged 17 commits into
mainfrom
roblou/planned-clownfish
Nov 21, 2025
Merged

roblourens merged 17 commits into
mainfrom
roblou/planned-clownfish

Conversation

@roblourens

Copy link
Copy Markdown
Member

Towards #277318

Copilot AI review requested due to automatic review settings November 20, 2025 00:40
@roblourens roblourens self-assigned this Nov 20, 2025

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 implements reference counting for ChatModel instances to enable better lifecycle management and allow chat sessions to run in the background. The key change is introducing IChatModelReference (a reference-counted wrapper) as the return type for session-related methods instead of returning ChatModel directly.

Key changes:

  • Introduced ChatModelStore extending ReferenceCollection to manage ChatModel lifecycles with ref-counting
  • Added self-reference mechanism in ChatModel to keep sessions alive while requests are in progress
  • Updated all consumers to acquire and properly dispose references instead of directly managing models

Reviewed Changes

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

Show a summary per file
File Description
chatServiceImpl.ts Implements ChatModelStore with reference counting via ReferenceCollection, manages automatic persistence on disposal
chatService.ts Updated interface to return IChatModelReference and added getActiveSessionReference method
chatModel.ts Added self-reference mechanism to keep models alive during in-progress requests using getActiveSessionReference
terminalChatWidget.ts Updated to store and dispose IChatModelReference instead of ChatModel directly
inlineChatSessionServiceImpl.ts Updated to use reference-counted models, though still calls forceClearSession inappropriately
inlineChatController.ts Updated to acquire references and use type casting to access ChatModel methods
inlineChatSession.ts Minor import reordering for consistency
chatViewPane.ts Replaced DisposableStore with MutableDisposable<IChatModelReference> for cleaner lifecycle management
chatQuick.ts Updated to properly acquire, store, and dispose model references
chatEditorInput.ts Uses MutableDisposable<IChatModelReference> with computed property for backward compatibility
mockChatService.ts Updated mock to match new interface signatures with references
chatService.test.ts Added helper functions to extract models from references while properly tracking disposal
chatEditingService.test.ts Updated tests to acquire references and properly dispose them
runInTerminalTool.ts Added check to only reveal terminal if chat session is open in a widget

if (!doesOtherSessionUseChatModel) {
this._chatService.clearSession(chatModel.sessionResource);
chatModel.dispose();
this._chatService.forceClearSession(chatModel.sessionResource);

Copilot AI Nov 20, 2025

Copy link

Choose a reason for hiding this comment

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

Calling forceClearSession here may bypass the reference counting system introduced in this PR. Since chatModelRef is added to the store at line 134, the reference will be properly disposed when the store is disposed. The call to forceClearSession could cause the model to be disposed while there are still active references. Consider removing this call and relying on the reference counting mechanism.

See below for a potential fix:


Copilot uses AI. Check for mistakes.
Comment on lines +1346 to 1347
// this._sessionModels.delete(sessionResource);
model.dispose();

Copilot AI Nov 20, 2025

Copy link

Choose a reason for hiding this comment

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

The forceClearSession method disposes the model directly without going through the reference collection's disposal mechanism. This bypasses the ref-counting system and could cause issues if there are still active references to this session. Consider removing the direct model.dispose() call and letting the reference collection handle disposal, or documenting why this force-disposal is necessary.

Suggested change
// this._sessionModels.delete(sessionResource);
model.dispose();
this._sessionModels.deleteAndDispose(sessionResource);

Copilot uses AI. Check for mistakes.
@roblourens
roblourens marked this pull request as ready for review November 21, 2025 04:30
@roblourens
roblourens enabled auto-merge (squash) November 21, 2025 04:31
@vs-code-engineering

vs-code-engineering Bot commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Benjamin Pasero (@bpasero)

Matched files:

  • src/vs/workbench/contrib/chat/browser/chatWidget.ts

Johannes Rieken (@jrieken)

Matched files:

  • src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts
  • src/vs/workbench/contrib/inlineChat/browser/inlineChatSessionServiceImpl.ts
  • src/vs/workbench/contrib/inlineChat/test/browser/inlineChatController.test.ts

if (this._configurationService.getValue(TerminalChatAgentToolsSettingId.OutputLocation) === 'terminal') {
private _handleTerminalVisibility(toolTerminal: IToolTerminal, chatSessionId: string) {
const chatSessionOpenInWidget = !!this._chatWidgetService.getWidgetBySessionResource(LocalChatSessionUri.forSession(chatSessionId));
if (this._configurationService.getValue(TerminalChatAgentToolsSettingId.OutputLocation) === 'terminal' && chatSessionOpenInWidget) {

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.

FYI Megan Rogge (@meganrogge) this tries to avoid opening the terminal for a chat session in the background

@vs-code-engineering vs-code-engineering Bot added this to the November 2025 milestone Nov 21, 2025
});

// Retain a reference to itself when a request is in progress, so the ChatModel stays alive in the background
// only while running a request. TODO also keep it alive for 5min or so so we don't have to dispose/restore too often?

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.

That's clever 😛

@rebornix Peng Lyu (rebornix) Nov 21, 2025 •

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.

This gives me dejavu somehow, very clever!

@roblourens
roblourens merged commit bbdd097 into main Nov 21, 2025
@roblourens
roblourens deleted the roblou/planned-clownfish branch November 21, 2025 05:24
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Jan 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants