@@ -1028,6 +1028,7 @@ export class CopilotCLIChatSessionParticipant extends Disposable {
10281028 * So if we have multiple requests (can happen when steering) for the same untitled session.
10291029 */
10301030 private readonly pendingRequestsForUntitledSessions = new Map < string , Map < string , Promise < vscode . ChatResult | void > > > ( ) ;
1031+ private readonly pendingCommitWorktreeChangesBySession = new Map < string , Promise < void > > ( ) ;
10311032
10321033 /**
10331034 * Outer request handler that supports *yielding* for session steering.
@@ -1381,23 +1382,40 @@ export class CopilotCLIChatSessionParticipant extends Disposable {
13811382 }
13821383 }
13831384
1384-
13851385 private async commitWorktreeChangesIfNeeded ( session : ICopilotCLISession , token : vscode . CancellationToken ) : Promise < void > {
1386- if ( session . status === vscode . ChatSessionStatus . Completed && ! token . isCancellationRequested ) {
1387- const workingDirectory = getWorkingDirectory ( session . workspace ) ;
1388- if ( isIsolationEnabled ( session . workspace ) ) {
1389- // When isolation is enabled and we are using a git worktree, so we commit
1390- // all the changes in the worktree directory when the session is completed
1391- await this . copilotCLIWorktreeManagerService . handleRequestCompleted ( session . sessionId ) ;
1392- } else if ( workingDirectory ) {
1393- // When isolation is not enabled, we are operating in the workspace directly,
1394- // so we stage all the changes in the workspace directory when the session is
1395- // completed
1396- await this . workspaceFolderService . handleRequestCompleted ( workingDirectory ) ;
1397- }
1386+ if ( token . isCancellationRequested ) {
1387+ return ;
1388+ }
1389+
1390+ const existingPromise = this . pendingCommitWorktreeChangesBySession . get ( session . sessionId ) ;
1391+ if ( existingPromise ) {
1392+ return existingPromise ;
13981393 }
13991394
1400- await this . handlePullRequestCreated ( session ) ;
1395+ const commitPromise = ( async ( ) => {
1396+ if ( session . status === vscode . ChatSessionStatus . Completed ) {
1397+ const workingDirectory = getWorkingDirectory ( session . workspace ) ;
1398+ if ( isIsolationEnabled ( session . workspace ) ) {
1399+ // When isolation is enabled and we are using a git worktree, so we commit
1400+ // all the changes in the worktree directory when the session is completed
1401+ await this . copilotCLIWorktreeManagerService . handleRequestCompleted ( session . sessionId ) ;
1402+ } else if ( workingDirectory ) {
1403+ // When isolation is not enabled, we are operating in the workspace directly,
1404+ // so we stage all the changes in the workspace directory when the session is
1405+ // completed
1406+ await this . workspaceFolderService . handleRequestCompleted ( workingDirectory ) ;
1407+ }
1408+ }
1409+
1410+ await this . handlePullRequestCreated ( session ) ;
1411+ } ) ( ) . finally ( ( ) => {
1412+ if ( this . pendingCommitWorktreeChangesBySession . get ( session . sessionId ) === commitPromise ) {
1413+ this . pendingCommitWorktreeChangesBySession . delete ( session . sessionId ) ;
1414+ }
1415+ } ) ;
1416+
1417+ this . pendingCommitWorktreeChangesBySession . set ( session . sessionId , commitPromise ) ;
1418+ return commitPromise ;
14011419 }
14021420
14031421 private async handlePullRequestCreated ( session : ICopilotCLISession ) : Promise < void > {
0 commit comments