Skip to content

Refactor timeline event polling to use timeout-based approach for Copilot sessions #7484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

Previously, two methods in the codebase used potentially infinite polling loops that could run indefinitely without timeout mechanisms, leading to poor user experience when waiting for Copilot events.

Changes

This PR refactors the polling logic in both affected files to use timeout-based approaches:

src/github/pullRequestOverview.ts - Timeline Event Polling

  • Replace infinite polling with a timeout-based approach that limits waiting to 1 minute
  • Wait for the first relevant Copilot timeline event instead of continuously fetching all timeline events
  • Maintain the same polling interval (2 seconds) for compatibility
  • Preserve existing completion detection logic that waits for CopilotPRStatus.Completed

src/github/copilotRemoteAgent.ts - Session Log Streaming

  • Add timeout mechanism with 1-hour timeout for long-running coding agent sessions
  • Enhanced error handling with timeout checks in both main polling logic and error handling
  • Maintain compatibility with existing 3-second polling interval and cancellation behavior
  • Add logging for timeout events to aid in debugging

Before

// pullRequestOverview.ts - infinite do-while loop
do {
    events = await this._getTimeline();
} while (copilotEventToStatus(mostRecentCopilotEvent(events)) !== CopilotPRStatus.Completed && 
         await new Promise<boolean>(c => setTimeout(() => c(true), 2000)));

// copilotRemoteAgent.ts - indefinite polling without timeout
const pollForUpdates = async (): Promise<void> => {
    // ... polling logic with no timeout check
    if (!token.isCancellationRequested && sessionInfo.state === 'in_progress') {
        setTimeout(pollForUpdates, pollingInterval);
    }
};

After

// pullRequestOverview.ts - timeout-based while loop
const maxWaitTime = 60 * 1000; // 1 minute timeout
const pollInterval = 2000; // 2 seconds
const startTime = Date.now();

while (Date.now() - startTime < maxWaitTime) {
    events = await this._getTimeline();
    if (copilotEventToStatus(mostRecentCopilotEvent(events)) === CopilotPRStatus.Completed) {
        break;
    }
    await new Promise<void>(resolve => setTimeout(resolve, pollInterval));
}

// copilotRemoteAgent.ts - timeout-based polling
const maxWaitTime = 60 * 60 * 1000; // 1 hour timeout for long sessions
const startTime = Date.now();

const pollForUpdates = async (): Promise<void> => {
    // Check for timeout
    if (Date.now() - startTime > maxWaitTime) {
        stream.markdown(vscode.l10n.t('Session polling timed out. The session may still be running.'));
        complete();
        return;
    }
    // ... rest of polling logic
};

Benefits

  • Prevents indefinite waiting by enforcing appropriate timeouts (1 minute for timeline events, 1 hour for coding sessions)
  • Improves user experience by providing predictable response times
  • Follows established patterns used elsewhere in the codebase
  • Maintains backward compatibility with existing functionality

The changes are minimal and surgical, affecting only the polling mechanisms while preserving all existing behavior for event detection and completion handling.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

- Replace do-while polling loop with timeout-based while loop
- Add 1-minute timeout mechanism for waiting on timeline events
- Wait for first Copilot event completion rather than indefinite polling
- Maintain same 2-second polling interval for compatibility

Co-authored-by: rebornix <[email protected]>
@Copilot Copilot AI changed the title [WIP] Refining Timeline Event Handling for Copilot Sessions Refactor timeline event polling to use timeout-based approach for Copilot sessions Aug 1, 2025
@Copilot Copilot AI requested a review from rebornix August 1, 2025 16:24
Copilot finished work on behalf of rebornix August 1, 2025 16:24
@rebornix
Copy link
Member

rebornix commented Aug 1, 2025

@copilot I want to update the wait in provideChatSessionContent in src/github/copilotRemoteAgent.ts


Copy link
Contributor Author

Copilot AI commented Aug 1, 2025

@copilot I want to update the wait in provideChatSessionContent in src/github/copilotRemoteAgent.ts

--- ...

Updated the streamSessionLogs method in src/github/copilotRemoteAgent.ts with timeout-based polling (1-hour timeout for long-running sessions). This prevents indefinite polling when sessions remain in 'in_progress' state, similar to the fix applied to pullRequestOverview.ts. See commit 1326aaa.

Copilot finished work on behalf of rebornix August 1, 2025 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants