Skip to content

Enhance secondary sessions with problem statement retrieval from Jobs API #7429

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 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/github/copilotRemoteAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ export class CopilotRemoteAgentManager extends Disposable {
if (sessionIndex === 0) {
sessionPrompt = await this.getInitialSessionPrompt(session, pullRequest, capi, sessionPrompt);
} else {
sessionPrompt = await this.getFollowUpSessionPrompt(sessionIndex, timelineEvents, sessionPrompt);
sessionPrompt = await this.getFollowUpSessionPrompt(session, sessionIndex, pullRequest, timelineEvents, capi, sessionPrompt);
}

// TODO: @rebornix, remove @copilot prefix from session prompt for now
Expand Down Expand Up @@ -944,10 +944,34 @@ export class CopilotRemoteAgentManager extends Disposable {
}

private async getFollowUpSessionPrompt(
session: SessionInfo,
sessionIndex: number,
pullRequest: PullRequestModel,
timelineEvents: readonly TimelineEvent[],
capi: CopilotApi,
defaultPrompt: string
): Promise<string> {
// First, try to get problem statement from Jobs API (similar to initial session)
try {
const jobInfo = await capi.getJobBySessionId(
pullRequest.base.repositoryCloneUrl.owner,
pullRequest.base.repositoryCloneUrl.repositoryName,
session.id
);
if (jobInfo && jobInfo.problem_statement) {
let prompt = jobInfo.problem_statement;
const titleMatch = jobInfo.problem_statement.match(/TITLE: \s*(.*)/i);
if (titleMatch && titleMatch[1]) {
prompt = titleMatch[1].trim();
}
Logger.appendLine(`Session ${sessionIndex}: Found problem_statement from Jobs API: ${prompt}`, CopilotRemoteAgentManager.ID);
return prompt;
}
} catch (error) {
Logger.warn(`Failed to get job info for session ${session.id}: ${error}`, CopilotRemoteAgentManager.ID);
}

// If no problem statement found, fall back to timeline event-based logic
const copilotStartedEvents = timelineEvents
.filter((event): event is CopilotStartedEvent => event.event === EventType.CopilotStarted)
.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
Expand Down
25 changes: 25 additions & 0 deletions src/test/github/copilotRemoteAgent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { default as assert } from 'assert';

describe('CopilotRemoteAgentManager - Secondary Session Problem Statement Retrieval', function () {
it('should extract title from problem statement correctly', function () {
// Test the TITLE extraction logic that was added to secondary sessions
const problemStatement1 = 'TITLE: Fix secondary session issue\nThis is a test problem statement for secondary sessions.';
const titleMatch1 = problemStatement1.match(/TITLE: \s*(.*)/i);
assert.strictEqual(titleMatch1?.[1]?.trim(), 'Fix secondary session issue');

// Test with different formatting
const problemStatement2 = 'TITLE: Handle chat sessions properly \nDetailed description here.';
const titleMatch2 = problemStatement2.match(/TITLE: \s*(.*)/i);
assert.strictEqual(titleMatch2?.[1]?.trim(), 'Handle chat sessions properly');

// Test without TITLE prefix
const problemStatement3 = 'Just a regular problem statement without title';
const titleMatch3 = problemStatement3.match(/TITLE: \s*(.*)/i);
assert.strictEqual(titleMatch3, null);
});
});