Skip to content

Commit c031311

Browse files
authored
Truncate coding agent problem_statement (#7860)
* truncate * truncate smarter * buffer * imports
1 parent 4ce81c3 commit c031311

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
@@ -19,7 +19,7 @@ import { CODING_AGENT, CODING_AGENT_AUTO_COMMIT_AND_PUSH } from '../common/setti
1919
import { ITelemetry } from '../common/telemetry';
2020
import { toOpenPullRequestWebviewUri } from '../common/uri';
2121
import { copilotEventToSessionStatus, copilotPRStatusToSessionStatus, IAPISessionLogs, ICopilotRemoteAgentCommandArgs, ICopilotRemoteAgentCommandResponse, OctokitCommon, RemoteAgentResult, RepoInfo } from './common';
22-
import { ChatSessionFromSummarizedChat, ChatSessionWithPR, CopilotApi, getCopilotApi, RemoteAgentJobPayload, SessionInfo, SessionSetupStep } from './copilotApi';
22+
import { ChatSessionFromSummarizedChat, ChatSessionWithPR, CopilotApi, getCopilotApi, RemoteAgentJobPayload, SessionInfo, SessionSetupStep, MAX_PROBLEM_STATEMENT_LENGTH } from './copilotApi';
2323
import { CodingAgentPRAndStatus, CopilotPRWatcher, CopilotStateModel } from './copilotPrWatcher';
2424
import { ChatSessionContentBuilder } from './copilotRemoteAgent/chatSessionContentBuilder';
2525
import { GitOperationsManager } from './copilotRemoteAgent/gitOperationsManager';
@@ -601,6 +601,14 @@ export class CopilotRemoteAgentManager extends Disposable {
601601
return `${header}\n\n${collapsedContext}`;
602602
};
603603

604+
let isTruncated = false;
605+
if (problemContext && (problemContext.length + prompt.length >= MAX_PROBLEM_STATEMENT_LENGTH)) {
606+
isTruncated = true;
607+
Logger.warn(`Truncating problemContext as it will cause us to exceed maximum problem_statement length (${MAX_PROBLEM_STATEMENT_LENGTH})`, CopilotRemoteAgentManager.ID);
608+
const availableLength = MAX_PROBLEM_STATEMENT_LENGTH - prompt.length;
609+
problemContext = problemContext.slice(-availableLength);
610+
}
611+
604612
const problemStatement: string = `${prompt}\n${problemContext ?? ''}`;
605613
const payload: RemoteAgentJobPayload = {
606614
problem_statement: problemStatement,
@@ -615,7 +623,7 @@ export class CopilotRemoteAgentManager extends Disposable {
615623
};
616624

617625
try {
618-
const { pull_request, session_id } = await capiClient.postRemoteAgentJob(owner, repo, payload);
626+
const { pull_request, session_id } = await capiClient.postRemoteAgentJob(owner, repo, payload, isTruncated);
619627
this._onDidCreatePullRequest.fire(pull_request.number);
620628
const webviewUri = await toOpenPullRequestWebviewUri({ owner, repo, pullRequestNumber: pull_request.number });
621629
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)