Skip to content

Commit 31a817c

Browse files
authored
[Candidate] Truncate coding agent problem_statement (#7862)
* Truncate coding agent problem_statement (#7860) * truncate * truncate smarter * buffer * imports * import order
1 parent 0f81ea8 commit 31a817c

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/github/copilotApi.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { hasEnterpriseUri } from './utils';
1919

2020
const LEARN_MORE_URL = 'https://aka.ms/coding-agent-docs';
2121
const PREMIUM_REQUESTS_URL = 'https://docs.github.com/en/copilot/concepts/copilot-billing/understanding-and-managing-requests-in-copilot#what-are-premium-requests';
22-
22+
// https://github.com/github/sweagentd/blob/59e7d9210ca3ebba029918387e525eea73cb1f4a/internal/problemstatement/problemstatement.go#L36-L53
23+
export const MAX_PROBLEM_STATEMENT_LENGTH = 30_000 - 50; // 50 character buffer
2324
export interface RemoteAgentJobPayload {
2425
problem_statement: string;
2526
event_type: string;
@@ -79,10 +80,15 @@ export class CopilotApi {
7980
owner: string,
8081
name: string,
8182
payload: RemoteAgentJobPayload,
83+
isTruncated: boolean,
8284
): Promise<RemoteAgentJobResponse> {
8385
const repoSlug = `${owner}/${name}`;
8486
const apiUrl = `/agents/swe/v0/jobs/${repoSlug}`;
8587
let status: number | undefined;
88+
89+
const problemStatementLength = payload.problem_statement.length.toString();
90+
const payloadJson = JSON.stringify(payload);
91+
const payloadLength = payloadJson.length.toString();
8692
Logger.trace(`postRemoteAgentJob: Posting job to ${apiUrl} with payload: ${JSON.stringify(payload)}`, CopilotApi.ID);
8793
try {
8894
const response = await this.makeApiCall(apiUrl, {
@@ -93,7 +99,7 @@ export class CopilotApi {
9399
'Content-Type': 'application/json',
94100
'Accept': 'application/json'
95101
},
96-
body: JSON.stringify(payload)
102+
body: payloadJson
97103
});
98104

99105
status = response.status;
@@ -106,21 +112,33 @@ export class CopilotApi {
106112
/*
107113
__GDPR__
108114
"remoteAgent.postRemoteAgentJob" : {
109-
"status" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
115+
"status" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
116+
"payloadLength": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
117+
"problemStatementLength": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
118+
"isTruncated": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
110119
}
111120
*/
112121
this.telemetry.sendTelemetryEvent('remoteAgent.postRemoteAgentJob', {
113122
status: status.toString(),
123+
payloadLength,
124+
problemStatementLength,
125+
isTruncated: isTruncated.toString(),
114126
});
115127
return data;
116128
} catch (error) {
117129
/* __GDPR__
118130
"remoteAgent.postRemoteAgentJob" : {
119-
"status" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
131+
"status" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
132+
"payloadLength": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
133+
"problemStatementLength": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
134+
"isTruncated": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
120135
}
121136
*/
122137
this.telemetry.sendTelemetryErrorEvent('remoteAgent.postRemoteAgentJob', {
123138
status: status?.toString() || '999',
139+
payloadLength,
140+
problemStatementLength,
141+
isTruncated: isTruncated.toString(),
124142
});
125143
throw error;
126144
}

src/github/copilotRemoteAgent.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { CODING_AGENT, CODING_AGENT_AUTO_COMMIT_AND_PUSH } from '../common/setti
1818
import { ITelemetry } from '../common/telemetry';
1919
import { toOpenPullRequestWebviewUri } from '../common/uri';
2020
import { copilotEventToSessionStatus, copilotPRStatusToSessionStatus, IAPISessionLogs, ICopilotRemoteAgentCommandArgs, ICopilotRemoteAgentCommandResponse, OctokitCommon, RemoteAgentResult, RepoInfo } from './common';
21-
import { ChatSessionFromSummarizedChat, ChatSessionWithPR, CopilotApi, getCopilotApi, RemoteAgentJobPayload, SessionInfo, SessionSetupStep } from './copilotApi';
21+
import { ChatSessionFromSummarizedChat, ChatSessionWithPR, CopilotApi, getCopilotApi, MAX_PROBLEM_STATEMENT_LENGTH, RemoteAgentJobPayload, SessionInfo, SessionSetupStep } from './copilotApi';
2222
import { CodingAgentPRAndStatus, CopilotPRWatcher, CopilotStateModel } from './copilotPrWatcher';
2323
import { ChatSessionContentBuilder } from './copilotRemoteAgent/chatSessionContentBuilder';
2424
import { GitOperationsManager } from './copilotRemoteAgent/gitOperationsManager';
@@ -572,6 +572,14 @@ export class CopilotRemoteAgentManager extends Disposable {
572572
return `${header}\n\n${collapsedContext}`;
573573
};
574574

575+
let isTruncated = false;
576+
if (problemContext && (problemContext.length + prompt.length >= MAX_PROBLEM_STATEMENT_LENGTH)) {
577+
isTruncated = true;
578+
Logger.warn(`Truncating problemContext as it will cause us to exceed maximum problem_statement length (${MAX_PROBLEM_STATEMENT_LENGTH})`, CopilotRemoteAgentManager.ID);
579+
const availableLength = MAX_PROBLEM_STATEMENT_LENGTH - prompt.length;
580+
problemContext = problemContext.slice(-availableLength);
581+
}
582+
575583
const problemStatement: string = `${prompt}\n${problemContext ?? ''}`;
576584
const payload: RemoteAgentJobPayload = {
577585
problem_statement: problemStatement,
@@ -586,7 +594,7 @@ export class CopilotRemoteAgentManager extends Disposable {
586594
};
587595

588596
try {
589-
const { pull_request, session_id } = await capiClient.postRemoteAgentJob(owner, repo, payload);
597+
const { pull_request, session_id } = await capiClient.postRemoteAgentJob(owner, repo, payload, isTruncated);
590598
this._onDidCreatePullRequest.fire(pull_request.number);
591599
const webviewUri = await toOpenPullRequestWebviewUri({ owner, repo, pullRequestNumber: pull_request.number });
592600
const prLlmString = `The remote agent has begun work and has created a pull request. Details about the pull request are being shown to the user. If the user wants to track progress or iterate on the agent's work, they should use the pull request.`;

0 commit comments

Comments
 (0)