@@ -1088,11 +1088,7 @@ export class CopilotChatSessionsProvider extends Disposable implements ISessions
10881088 const sessions : ISession [ ] = [ ] ;
10891089
10901090 for ( const chat of allChats ) {
1091- let groupId = this . _groupModel . getSessionIdForChat ( chat . id ) ;
1092- if ( ! groupId ) {
1093- groupId = chat . id ;
1094- this . _groupModel . addChat ( groupId , chat . id ) ;
1095- }
1091+ const groupId = this . _groupModel . getSessionIdForChat ( chat . id ) ?? chat . id ;
10961092 if ( ! seen . has ( groupId ) ) {
10971093 seen . add ( groupId ) ;
10981094 sessions . push ( this . _chatToSession ( chat ) ) ;
@@ -1471,13 +1467,19 @@ export class CopilotChatSessionsProvider extends Disposable implements ISessions
14711467
14721468 return updatedSession ;
14731469 } catch ( error ) {
1474- // Clean up on error
1470+ // Clean up on error — fire changed on the parent session group
14751471 this . _sessionCache . delete ( key ) ;
14761472 this . _groupModel . removeChat ( newChatSession . id ) ;
14771473 this . _sessionGroupCache . delete ( sessionId ) ;
14781474 this . _currentNewSession = undefined ;
14791475 newChatSession . dispose ( ) ;
1480- this . _onDidChangeSessions . fire ( { added : [ ] , removed : [ ] , changed : [ this . _chatToSession ( newChatSession ) ] } ) ;
1476+ // Find the parent session's primary chat to fire a valid changed event
1477+ const parentChatIds = this . _groupModel . getChatIds ( sessionId ) ;
1478+ const parentChatId = parentChatIds [ 0 ] ;
1479+ const parentChat = parentChatId ? this . _sessionCache . get ( this . _localIdFromchatId ( parentChatId ) ) : undefined ;
1480+ if ( parentChat ) {
1481+ this . _onDidChangeSessions . fire ( { added : [ ] , removed : [ ] , changed : [ this . _chatToSession ( parentChat ) ] } ) ;
1482+ }
14811483 throw error ;
14821484 }
14831485 }
@@ -1673,26 +1675,42 @@ export class CopilotChatSessionsProvider extends Disposable implements ISessions
16731675 removedData : ICopilotChatSession [ ] ,
16741676 changedData : ICopilotChatSession [ ] ,
16751677 ) : void {
1678+ // Track session group IDs for removed chats before modifying the group model
1679+ const removedGroupIds = new Map < ICopilotChatSession , string | undefined > ( ) ;
1680+ for ( const removed of removedData ) {
1681+ removedGroupIds . set ( removed , this . _groupModel . getSessionIdForChat ( removed . id ) ) ;
1682+ }
1683+
16761684 // Handle removed chats: if a removed chat belongs to a group with
16771685 // remaining siblings, treat it as a changed event on the parent session
16781686 // instead of a removed session.
1679- const trulyRemovedSessions : ICopilotChatSession [ ] = [ ] ;
1687+ const trulyRemovedSessions : { chat : ICopilotChatSession ; groupId : string } [ ] = [ ] ;
1688+ const changedSessionIds = new Set < string > ( ) ;
16801689 for ( const removed of removedData ) {
1681- const sessionId = this . _groupModel . getSessionIdForChat ( removed . id ) ;
1690+ const sessionId = removedGroupIds . get ( removed ) ;
16821691 this . _groupModel . removeChat ( removed . id ) ;
16831692 if ( sessionId && this . _groupModel . getChatIds ( sessionId ) . length > 0 ) {
16841693 // Group still has other chats — invalidate cache and treat as changed
16851694 this . _sessionGroupCache . delete ( sessionId ) ;
1686- const primaryChatId = this . _groupModel . getChatIds ( sessionId ) [ 0 ] ;
1687- const primaryChat = this . _sessionCache . get ( this . _localIdFromchatId ( primaryChatId ) ) ;
1688- if ( primaryChat ) {
1689- changedData . push ( primaryChat ) ;
1695+ if ( ! changedSessionIds . has ( sessionId ) ) {
1696+ changedSessionIds . add ( sessionId ) ;
1697+ const primaryChatId = this . _groupModel . getChatIds ( sessionId ) [ 0 ] ;
1698+ const primaryChat = this . _sessionCache . get ( this . _localIdFromchatId ( primaryChatId ) ) ;
1699+ if ( primaryChat ) {
1700+ changedData . push ( primaryChat ) ;
1701+ }
16901702 }
16911703 } else {
1692- if ( sessionId ) {
1693- this . _sessionGroupCache . delete ( sessionId ) ;
1694- }
1695- trulyRemovedSessions . push ( removed ) ;
1704+ const groupId = sessionId ?? removed . id ;
1705+ this . _sessionGroupCache . delete ( groupId ) ;
1706+ trulyRemovedSessions . push ( { chat : removed , groupId } ) ;
1707+ }
1708+ }
1709+
1710+ // Seed ungrouped chats into the group model
1711+ for ( const added of addedData ) {
1712+ if ( ! this . _groupModel . getSessionIdForChat ( added . id ) ) {
1713+ this . _groupModel . addChat ( added . id , added . id ) ;
16961714 }
16971715 }
16981716
@@ -1702,21 +1720,34 @@ export class CopilotChatSessionsProvider extends Disposable implements ISessions
17021720 const existingGroupId = this . _groupModel . getSessionIdForChat ( added . id ) ;
17031721 if ( existingGroupId && existingGroupId !== added . id ) {
17041722 // This chat belongs to an existing session group — treat as changed
1705- changedData . push ( added ) ;
1723+ if ( ! changedSessionIds . has ( existingGroupId ) ) {
1724+ changedSessionIds . add ( existingGroupId ) ;
1725+ changedData . push ( added ) ;
1726+ }
17061727 } else {
17071728 newSessions . push ( added ) ;
17081729 }
17091730 }
17101731
1732+ // Deduplicate changed sessions by group ID
1733+ const seenChanged = new Set < string > ( ) ;
1734+ const deduplicatedChanged : ICopilotChatSession [ ] = [ ] ;
1735+ for ( const d of changedData ) {
1736+ const groupId = this . _groupModel . getSessionIdForChat ( d . id ) ?? d . id ;
1737+ if ( ! seenChanged . has ( groupId ) ) {
1738+ seenChanged . add ( groupId ) ;
1739+ deduplicatedChanged . push ( d ) ;
1740+ }
1741+ }
1742+
17111743 this . _onDidChangeSessions . fire ( {
17121744 added : newSessions . map ( d => this . _chatToSession ( d ) ) ,
1713- removed : trulyRemovedSessions . map ( d => {
1714- const groupId = d . id ;
1745+ removed : trulyRemovedSessions . map ( ( { chat, groupId } ) => {
17151746 const session = this . _sessionGroupCache . get ( groupId ) ;
17161747 this . _sessionGroupCache . delete ( groupId ) ;
1717- return session ?? this . _chatToSession ( d ) ;
1748+ return session ?? this . _chatToSession ( chat ) ;
17181749 } ) ,
1719- changed : changedData . map ( d => this . _chatToSession ( d ) ) ,
1750+ changed : deduplicatedChanged . map ( d => this . _chatToSession ( d ) ) ,
17201751 } ) ;
17211752 }
17221753
0 commit comments