Skip to content

Commit f0bbd9f

Browse files
committed
fix(amazonqFeatureDev): use shared context from action to check cancellation
1 parent d63ceb5 commit f0bbd9f

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

packages/core/src/amazonqFeatureDev/controllers/chat/controller.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ export class FeatureDevController {
284284
}
285285
}
286286

287+
private disposeToken(session: Session | undefined) {
288+
if (session?.state?.tokenSource?.token.isCancellationRequested) {
289+
session?.state.tokenSource?.dispose()
290+
if (session?.state?.tokenSource) {
291+
session.state.tokenSource = new vscode.CancellationTokenSource()
292+
}
293+
getLogger().debug('Request cancelled, skipping further processing')
294+
}
295+
}
296+
287297
// TODO add type
288298
private async processUserChatMessage(message: any) {
289299
if (message.message === undefined) {
@@ -319,6 +329,7 @@ export class FeatureDevController {
319329
await this.onCodeGeneration(session, message.message, message.tabID)
320330
}
321331
} catch (err: any) {
332+
this.disposeToken(session)
322333
this.processErrorChatMessage(err, message, session)
323334
// Lock the chat input until they explicitly click one of the follow ups
324335
this.messenger.sendChatInputEnabled(message.tabID, false)
@@ -413,11 +424,7 @@ export class FeatureDevController {
413424
// Finish processing the event
414425

415426
if (session?.state?.tokenSource?.token.isCancellationRequested) {
416-
session?.state.tokenSource?.dispose()
417-
if (session?.state?.tokenSource) {
418-
session.state.tokenSource = new vscode.CancellationTokenSource()
419-
}
420-
getLogger().debug('Request cancelled, skipping further processing')
427+
this.disposeToken(session)
421428
} else {
422429
this.messenger.sendAsyncEventProgress(tabID, false, undefined)
423430

packages/core/src/amazonqFeatureDev/session/sessionState.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ abstract class CodeGenBase {
124124
private pollCount = 180
125125
private requestDelay = 10000
126126
public tokenSource: vscode.CancellationTokenSource
127-
public isCancellationRequested: boolean
128127
public phase: SessionStatePhase = DevPhase.CODEGEN
129128
public readonly conversationId: string
130129
public readonly uploadId: string
@@ -135,7 +134,6 @@ abstract class CodeGenBase {
135134
public tabID: string
136135
) {
137136
this.tokenSource = new vscode.CancellationTokenSource()
138-
this.isCancellationRequested = false
139137
this.conversationId = config.conversationId
140138
this.uploadId = config.uploadId
141139
this.currentCodeGenerationId = config.currentCodeGenerationId || EmptyCodeGenID
@@ -147,12 +145,14 @@ abstract class CodeGenBase {
147145
codeGenerationId,
148146
telemetry: telemetry,
149147
workspaceFolders,
148+
isCancellationRequested,
150149
}: {
151150
messenger: Messenger
152151
fs: VirtualFileSystem
153152
codeGenerationId: string
154153
telemetry: TelemetryHelper
155154
workspaceFolders: CurrentWsFolders
155+
isCancellationRequested?: boolean
156156
}): Promise<{
157157
newFiles: NewFileInfo[]
158158
deletedFiles: DeletedFileInfo[]
@@ -162,7 +162,7 @@ abstract class CodeGenBase {
162162
}> {
163163
for (
164164
let pollingIteration = 0;
165-
pollingIteration < this.pollCount && !this.isCancellationRequested;
165+
pollingIteration < this.pollCount && !isCancellationRequested;
166166
++pollingIteration
167167
) {
168168
const codegenResult = await this.config.proxyClient.getCodeGeneration(this.conversationId, codeGenerationId)
@@ -238,7 +238,7 @@ abstract class CodeGenBase {
238238
}
239239
}
240240
}
241-
if (!this.isCancellationRequested) {
241+
if (!isCancellationRequested) {
242242
// still in progress
243243
const errorMessage = i18n('AWS.amazonq.featureDev.error.codeGen.timeout')
244244
throw new ToolkitError(errorMessage, { code: 'CodeGenTimeout' })
@@ -273,13 +273,6 @@ export class CodeGenState extends CodeGenBase implements SessionState {
273273
credentialStartUrl: AuthUtil.instance.startUrl,
274274
})
275275

276-
action.tokenSource?.token.onCancellationRequested(() => {
277-
this.isCancellationRequested = true
278-
if (action.tokenSource) {
279-
this.tokenSource = action.tokenSource
280-
}
281-
})
282-
283276
action.telemetry.setGenerateCodeIteration(this.currentIteration)
284277
action.telemetry.setGenerateCodeLastInvocationTime()
285278
const codeGenerationId = randomUUID()
@@ -291,7 +284,7 @@ export class CodeGenState extends CodeGenBase implements SessionState {
291284
this.currentCodeGenerationId
292285
)
293286

294-
if (!this.isCancellationRequested) {
287+
if (!action.tokenSource?.token.isCancellationRequested) {
295288
action.messenger.sendAnswer({
296289
message: i18n('AWS.amazonq.featureDev.pillText.generatingCode'),
297290
type: 'answer-part',
@@ -309,9 +302,10 @@ export class CodeGenState extends CodeGenBase implements SessionState {
309302
codeGenerationId,
310303
telemetry: action.telemetry,
311304
workspaceFolders: this.config.workspaceFolders,
305+
isCancellationRequested: action.tokenSource?.token.isCancellationRequested,
312306
})
313307

314-
if (codeGeneration && !this.isCancellationRequested) {
308+
if (codeGeneration && !action.tokenSource?.token.isCancellationRequested) {
315309
this.config.currentCodeGenerationId = codeGenerationId
316310
this.currentCodeGenerationId = codeGenerationId
317311
}
@@ -333,7 +327,7 @@ export class CodeGenState extends CodeGenBase implements SessionState {
333327
this.currentIteration + 1,
334328
this.codeGenerationRemainingIterationCount,
335329
this.codeGenerationTotalIterationCount,
336-
this.tokenSource,
330+
action.tokenSource,
337331
this.currentCodeGenerationId
338332
)
339333
return {
@@ -489,16 +483,18 @@ export class PrepareCodeGenState implements SessionState {
489483
)
490484

491485
await uploadCode(uploadUrl, zipFileBuffer, zipFileChecksum, kmsKeyArn)
492-
action.messenger.sendAnswer({
493-
message: i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted'),
494-
type: 'answer-part',
495-
tabID: this.tabID,
496-
})
486+
if (!action.tokenSource?.token.isCancellationRequested) {
487+
action.messenger.sendAnswer({
488+
message: i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted'),
489+
type: 'answer-part',
490+
tabID: this.tabID,
491+
})
497492

498-
action.messenger.sendUpdatePlaceholder(
499-
this.tabID,
500-
i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted')
501-
)
493+
action.messenger.sendUpdatePlaceholder(
494+
this.tabID,
495+
i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted')
496+
)
497+
}
502498

503499
return uploadId
504500
})

0 commit comments

Comments
 (0)