@@ -9,7 +9,7 @@ import { Uri } from 'vscode';
99import { IGitExtensionService } from '../../../platform/git/common/gitExtensionService' ;
1010import { IGitService } from '../../../platform/git/common/gitService' ;
1111import { PullRequestSearchItem , SessionInfo } from '../../../platform/github/common/githubAPI' ;
12- import { IOctoKitService , JobInfo , RemoteAgentJobPayload } from '../../../platform/github/common/githubService' ;
12+ import { IOctoKitService , JobInfo , RemoteAgentJobPayload , RemoteAgentJobResponse } from '../../../platform/github/common/githubService' ;
1313import { ILogService } from '../../../platform/log/common/logService' ;
1414import { ITelemetryService } from '../../../platform/telemetry/common/telemetry' ;
1515import { Disposable , toDisposable } from '../../../util/vs/base/common/lifecycle' ;
@@ -1001,6 +1001,11 @@ export class CopilotCloudSessionsProvider extends Disposable implements vscode.C
10011001 }
10021002 }
10031003
1004+ // https://github.com/github/sweagentd/blob/main/docs/adr/0001-create-job-api.md
1005+ private validateRemoteAgentJobResponse ( response : unknown ) : response is RemoteAgentJobResponse {
1006+ return typeof response === 'object' && response !== null && 'job_id' in response && 'session_id' in response ;
1007+ }
1008+
10041009 private async waitForJobWithPullRequest (
10051010 owner : string ,
10061011 repo : string ,
@@ -1056,16 +1061,19 @@ export class CopilotCloudSessionsProvider extends Disposable implements vscode.C
10561061 if ( ! base_ref ) {
10571062 return { error : vscode . l10n . t ( 'Unable to determine the current branch.' ) , state : 'error' } ;
10581063 }
1059- let head_ref : string | undefined ; // This is the ref cloud agent starts work from (omitted unless we push local changes)
1064+ let head_ref : string | undefined ; // TODO: UNUSED! This is the ref cloud agent starts work from (omitted unless we push local changes)
10601065
1066+ // TODO: Make this automatic instead of a fatal error.
10611067 const remoteName =
10621068 repo ?. state . HEAD ?. upstream ?. remote ??
10631069 currentRepository ?. upstreamRemote ??
10641070 repo ?. state . remotes ?. [ 0 ] ?. name ;
10651071
10661072 if ( repo && remoteName && base_ref ) {
10671073 try {
1068- const remoteBranches = await repo . getBranches ( { remote : true } ) ;
1074+ const remoteBranches =
1075+ ( await repo . getBranches ( { remote : true } ) )
1076+ . filter ( b => b . remote ) ; // Has an associated remote
10691077 const expectedRemoteBranch = `${ remoteName } /${ base_ref } ` ;
10701078 const alternateNames = new Set < string > ( [
10711079 expectedRemoteBranch ,
@@ -1079,7 +1087,10 @@ export class CopilotCloudSessionsProvider extends Disposable implements vscode.C
10791087 if ( branch . remote && branch . remote !== remoteName ) {
10801088 return false ;
10811089 }
1082- const candidateName = branch . remote ? `${ branch . remote } /${ branch . name } ` : branch . name ;
1090+ const candidateName =
1091+ ( branch . remote && branch . name . startsWith ( branch . remote + '/' ) )
1092+ ? branch . name
1093+ : `${ branch . remote } /${ branch . name } ` ;
10831094 return alternateNames . has ( candidateName ) ;
10841095 } ) ;
10851096
@@ -1138,7 +1149,10 @@ export class CopilotCloudSessionsProvider extends Disposable implements vscode.C
11381149 chatStream ?. progress ( vscode . l10n . t ( 'Delegating to cloud agent' ) ) ;
11391150 this . logService . trace ( `Invoking cloud agent job with payload: ${ JSON . stringify ( payload ) } ` ) ;
11401151 const response = await this . _octoKitService . postCopilotAgentJob ( repoId . org , repoId . repo , JOBS_API_VERSION , payload ) ;
1141-
1152+ if ( ! this . validateRemoteAgentJobResponse ( response ) ) {
1153+ const statusCode = response ?. status ;
1154+ return { error : vscode . l10n . t ( `Received invalid response ${ statusCode ? statusCode + ' ' : '' } from cloud agent.` ) , innerError : `Response ${ JSON . stringify ( response ) } ` , state : 'error' } ;
1155+ }
11421156 // For v1 API, we need to fetch the job details to get the PR info
11431157 // Since the PR might not be created immediately, we need to poll for it
11441158 chatStream ?. progress ( vscode . l10n . t ( 'Creating pull request' ) ) ;
0 commit comments