Skip to content

Commit 132d260

Browse files
committed
fix(amazonqFeatureDev): use shared context from action to check cancellation
1 parent 24ff1af commit 132d260

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
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
@@ -116,7 +116,6 @@ abstract class CodeGenBase {
116116
private pollCount = 180
117117
private requestDelay = 10000
118118
public tokenSource: vscode.CancellationTokenSource
119-
public isCancellationRequested: boolean
120119
public phase: SessionStatePhase = DevPhase.CODEGEN
121120
public readonly conversationId: string
122121
public readonly uploadId: string
@@ -127,7 +126,6 @@ abstract class CodeGenBase {
127126
public tabID: string
128127
) {
129128
this.tokenSource = new vscode.CancellationTokenSource()
130-
this.isCancellationRequested = false
131129
this.conversationId = config.conversationId
132130
this.uploadId = config.uploadId
133131
this.currentCodeGenerationId = config.currentCodeGenerationId || EmptyCodeGenID
@@ -139,12 +137,14 @@ abstract class CodeGenBase {
139137
codeGenerationId,
140138
telemetry: telemetry,
141139
workspaceFolders,
140+
isCancellationRequested,
142141
}: {
143142
messenger: Messenger
144143
fs: VirtualFileSystem
145144
codeGenerationId: string
146145
telemetry: TelemetryHelper
147146
workspaceFolders: CurrentWsFolders
147+
isCancellationRequested?: boolean
148148
}): Promise<{
149149
newFiles: NewFileInfo[]
150150
deletedFiles: DeletedFileInfo[]
@@ -154,7 +154,7 @@ abstract class CodeGenBase {
154154
}> {
155155
for (
156156
let pollingIteration = 0;
157-
pollingIteration < this.pollCount && !this.isCancellationRequested;
157+
pollingIteration < this.pollCount && !isCancellationRequested;
158158
++pollingIteration
159159
) {
160160
const codegenResult = await this.config.proxyClient.getCodeGeneration(this.conversationId, codeGenerationId)
@@ -230,7 +230,7 @@ abstract class CodeGenBase {
230230
}
231231
}
232232
}
233-
if (!this.isCancellationRequested) {
233+
if (!isCancellationRequested) {
234234
// still in progress
235235
const errorMessage = i18n('AWS.amazonq.featureDev.error.codeGen.timeout')
236236
throw new ToolkitError(errorMessage, { code: 'CodeGenTimeout' })
@@ -265,13 +265,6 @@ export class CodeGenState extends CodeGenBase implements SessionState {
265265
credentialStartUrl: AuthUtil.instance.startUrl,
266266
})
267267

268-
action.tokenSource?.token.onCancellationRequested(() => {
269-
this.isCancellationRequested = true
270-
if (action.tokenSource) {
271-
this.tokenSource = action.tokenSource
272-
}
273-
})
274-
275268
action.telemetry.setGenerateCodeIteration(this.currentIteration)
276269
action.telemetry.setGenerateCodeLastInvocationTime()
277270
const codeGenerationId = randomUUID()
@@ -283,7 +276,7 @@ export class CodeGenState extends CodeGenBase implements SessionState {
283276
this.currentCodeGenerationId
284277
)
285278

286-
if (!this.isCancellationRequested) {
279+
if (!action.tokenSource?.token.isCancellationRequested) {
287280
action.messenger.sendAnswer({
288281
message: i18n('AWS.amazonq.featureDev.pillText.generatingCode'),
289282
type: 'answer-part',
@@ -301,9 +294,10 @@ export class CodeGenState extends CodeGenBase implements SessionState {
301294
codeGenerationId,
302295
telemetry: action.telemetry,
303296
workspaceFolders: this.config.workspaceFolders,
297+
isCancellationRequested: action.tokenSource?.token.isCancellationRequested,
304298
})
305299

306-
if (codeGeneration && !this.isCancellationRequested) {
300+
if (codeGeneration && !action.tokenSource?.token.isCancellationRequested) {
307301
this.config.currentCodeGenerationId = codeGenerationId
308302
this.currentCodeGenerationId = codeGenerationId
309303
}
@@ -325,7 +319,7 @@ export class CodeGenState extends CodeGenBase implements SessionState {
325319
this.currentIteration + 1,
326320
this.codeGenerationRemainingIterationCount,
327321
this.codeGenerationTotalIterationCount,
328-
this.tokenSource,
322+
action.tokenSource,
329323
this.currentCodeGenerationId
330324
)
331325
return {
@@ -481,16 +475,18 @@ export class PrepareCodeGenState implements SessionState {
481475
)
482476

483477
await uploadCode(uploadUrl, zipFileBuffer, zipFileChecksum, kmsKeyArn)
484-
action.messenger.sendAnswer({
485-
message: i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted'),
486-
type: 'answer-part',
487-
tabID: this.tabID,
488-
})
478+
if (!action.tokenSource?.token.isCancellationRequested) {
479+
action.messenger.sendAnswer({
480+
message: i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted'),
481+
type: 'answer-part',
482+
tabID: this.tabID,
483+
})
489484

490-
action.messenger.sendUpdatePlaceholder(
491-
this.tabID,
492-
i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted')
493-
)
485+
action.messenger.sendUpdatePlaceholder(
486+
this.tabID,
487+
i18n('AWS.amazonq.featureDev.pillText.contextGatheringCompleted')
488+
)
489+
}
494490

495491
return uploadId
496492
})

packages/core/src/amazonqFeatureDev/util/files.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ export async function prepareRepoData(
6363
const iterator = ignoredExtensionMap.entries()
6464

6565
for (let i = 0; i < ignoredExtensionMap.size; i++) {
66-
const [key, value] = iterator.next().value
67-
await amznTelemetry.amazonq_bundleExtensionIgnored.run(async (bundleSpan) => {
68-
const event = {
69-
filenameExt: key,
70-
count: value,
71-
}
66+
const iteratorValue = iterator.next().value
67+
if (iteratorValue) {
68+
const [key, value] = iteratorValue
69+
await amznTelemetry.amazonq_bundleExtensionIgnored.run(async (bundleSpan) => {
70+
const event = {
71+
filenameExt: key,
72+
count: value,
73+
}
7274

73-
bundleSpan.record(event)
74-
})
75+
bundleSpan.record(event)
76+
})
77+
}
7578
}
7679

7780
telemetry.setRepositorySize(totalBytes)

0 commit comments

Comments
 (0)