@@ -1479,7 +1479,7 @@ export class CopilotRemoteAgentManager extends Disposable {
14791479 return { } ;
14801480 }
14811481
1482- stream . progress ( 'Working on your request...' ) ;
1482+ stream . progress ( vscode . l10n . t ( 'Delegating request to coding agent' ) ) ;
14831483
14841484 // Add follow-up comment to the PR
14851485 const result = await this . addFollowUpToExistingPR ( pullRequest . number , userPrompt ) ;
@@ -1492,8 +1492,10 @@ export class CopilotRemoteAgentManager extends Disposable {
14921492 stream . markdown ( result ) ;
14931493 stream . markdown ( '\n\n' ) ;
14941494
1495+ stream . progress ( vscode . l10n . t ( 'Waiting for progress' ) ) ;
1496+
14951497 // Wait for new session and stream its progress
1496- const newSession = await this . waitForNewSession ( pullRequest , stream , token ) ;
1498+ const newSession = await this . waitForNewSession ( pullRequest , stream , token , true ) ;
14971499 if ( ! newSession ) {
14981500 return { } ;
14991501 }
@@ -1522,16 +1524,37 @@ export class CopilotRemoteAgentManager extends Disposable {
15221524 return undefined ;
15231525 }
15241526
1525- const maxWaitTime = 2 * 60 * 1_000 ; // 2 minutes
1526- const pollInterval = 3_000 ; // 3 seconds
1527- const startTime = Date . now ( ) ;
1527+ let sessionInfo : SessionInfo | undefined ;
1528+
1529+ const waitForQueuedMaxRetries = 3 ;
1530+ const waitForQueuedDelay = 5_000 ; // 5 seconds
1531+
1532+ // Allow for a short delay before the session is marked as 'queued'
1533+ let waitForQueuedCount = 0 ;
1534+ do {
1535+ sessionInfo = await capi . getSessionInfo ( sessionId ) ;
1536+ if ( sessionInfo && sessionInfo . state === 'queued' ) {
1537+ Logger . trace ( 'Queued session found' , CopilotRemoteAgentManager . ID ) ;
1538+ break ;
1539+ }
1540+ if ( waitForQueuedCount < waitForQueuedMaxRetries ) {
1541+ Logger . trace ( 'Session not yet queued, waiting...' , CopilotRemoteAgentManager . ID ) ;
1542+ await new Promise ( resolve => setTimeout ( resolve , waitForQueuedDelay ) ) ;
1543+ }
1544+ ++ waitForQueuedCount ;
1545+ } while ( waitForQueuedCount <= waitForQueuedMaxRetries && ( ! token || ! token . isCancellationRequested ) ) ;
15281546
1529- const sessionInfo = await capi . getSessionInfo ( sessionId ) ;
15301547 if ( ! sessionInfo || sessionInfo . state !== 'queued' ) {
1548+ // Failure
1549+ Logger . trace ( 'Failed to find queued session' , CopilotRemoteAgentManager . ID ) ;
15311550 return ;
15321551 }
15331552
1534- Logger . appendLine ( `Session ${ sessionInfo . id } is queued, waiting to start...` , CopilotRemoteAgentManager . ID ) ;
1553+ const maxWaitTime = 2 * 60 * 1_000 ; // 2 minutes
1554+ const pollInterval = 3_000 ; // 3 seconds
1555+ const startTime = Date . now ( ) ;
1556+
1557+ Logger . appendLine ( `Session ${ sessionInfo . id } is queued, waiting for transition to in_progress...` , CopilotRemoteAgentManager . ID ) ;
15351558 while ( Date . now ( ) - startTime < maxWaitTime && ( ! token || ! token . isCancellationRequested ) ) {
15361559 const sessionInfo = await capi . getSessionInfo ( sessionId ) ;
15371560 if ( sessionInfo ?. state === 'in_progress' ) {
@@ -1545,13 +1568,14 @@ export class CopilotRemoteAgentManager extends Disposable {
15451568 private async waitForNewSession (
15461569 pullRequest : PullRequestModel ,
15471570 stream : vscode . ChatResponseStream ,
1548- token : vscode . CancellationToken
1571+ token : vscode . CancellationToken ,
1572+ waitForTransitionToInProgress : boolean = false
15491573 ) : Promise < SessionInfo | undefined > {
15501574 // Get the current number of sessions
15511575 const capi = await this . copilotApi ;
15521576 if ( ! capi ) {
15531577 stream . markdown ( vscode . l10n . t ( 'Failed to connect to Copilot API.' ) ) ;
1554- return undefined ;
1578+ return ;
15551579 }
15561580
15571581 const initialSessions = await capi . getAllSessions ( pullRequest . id ) ;
@@ -1567,15 +1591,24 @@ export class CopilotRemoteAgentManager extends Disposable {
15671591
15681592 // Check if a new session has started
15691593 if ( currentSessions . length > initialSessionCount ) {
1570- return currentSessions
1594+ const newSession = currentSessions
15711595 . sort ( ( a , b ) => new Date ( b . created_at ) . getTime ( ) - new Date ( a . created_at ) . getTime ( ) ) [ 0 ] ;
1596+ if ( ! waitForTransitionToInProgress ) {
1597+ return newSession ;
1598+ }
1599+ const inProgressSession = await this . waitForQueuedToInProgress ( newSession . id , token ) ;
1600+ if ( ! inProgressSession ) {
1601+ stream . markdown ( vscode . l10n . t ( 'Timed out waiting for coding agent to begin work. Please try again shortly.' ) ) ;
1602+ return ;
1603+ }
1604+ return inProgressSession ;
15721605 }
15731606
15741607 await new Promise ( resolve => setTimeout ( resolve , pollInterval ) ) ;
15751608 }
15761609
15771610 stream . markdown ( vscode . l10n . t ( 'Timed out waiting for the coding agent to respond. The agent may still be processing your request.' ) ) ;
1578- return undefined ;
1611+ return ;
15791612 }
15801613
15811614 private getIconForSession ( status : vscode . ChatSessionStatus ) : vscode . Uri | vscode . ThemeIcon {
0 commit comments