Repository navigation
Manage ChatModels with ref-counting, allow running in bg - #278476
Conversation
There was a problem hiding this comment.
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
ChatModelStoreextendingReferenceCollectionto manage ChatModel lifecycles with ref-counting - Added self-reference mechanism in
ChatModelto 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); |
There was a problem hiding this comment.
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:
| // this._sessionModels.delete(sessionResource); | ||
| model.dispose(); |
There was a problem hiding this comment.
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.
| // this._sessionModels.delete(sessionResource); | |
| model.dispose(); | |
| this._sessionModels.deleteAndDispose(sessionResource); |
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: Benjamin Pasero (@bpasero)Matched files:
Johannes Rieken (@jrieken)Matched files:
|
| 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) { |
There was a problem hiding this comment.
FYI Megan Rogge (@meganrogge) this tries to avoid opening the terminal for a chat session in the background
| }); | ||
|
|
||
| // 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? |
There was a problem hiding this comment.
That's clever 😛
There was a problem hiding this comment.
This gives me dejavu somehow, very clever!
Towards #277318