Skip to content

Commit e0388e3

Browse files
committed
fix race condition
1 parent c347320 commit e0388e3

File tree

1 file changed

+73
-67
lines changed

1 file changed

+73
-67
lines changed

packages/amazonq/src/app/inline/completion.ts

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -110,82 +110,88 @@ export class InlineCompletionManager implements Disposable {
110110
startLine: number,
111111
firstCompletionDisplayLatency?: number
112112
) => {
113-
// TODO: also log the seen state for other suggestions in session
114-
// Calculate timing metrics before diagnostic delay
115-
const totalSessionDisplayTime = performance.now() - requestStartTime
116-
let params: LogInlineCompletionSessionResultsParams = {
117-
sessionId: sessionId,
118-
completionSessionResult: {
119-
[item.itemId]: {
120-
seen: true,
121-
accepted: true,
122-
discarded: false,
113+
try {
114+
vsCodeState.isCodeWhispererEditing = true
115+
// TODO: also log the seen state for other suggestions in session
116+
// Calculate timing metrics before diagnostic delay
117+
const totalSessionDisplayTime = performance.now() - requestStartTime
118+
await sleep(500)
119+
const diagnosticDiff = getDiagnosticsDifferences(
120+
this.sessionManager.getActiveSession()?.diagnosticsBeforeAccept,
121+
getDiagnosticsOfCurrentFile()
122+
)
123+
const params: LogInlineCompletionSessionResultsParams = {
124+
sessionId: sessionId,
125+
completionSessionResult: {
126+
[item.itemId]: {
127+
seen: true,
128+
accepted: true,
129+
discarded: false,
130+
},
123131
},
124-
},
125-
totalSessionDisplayTime: totalSessionDisplayTime,
126-
firstCompletionDisplayLatency: firstCompletionDisplayLatency,
127-
addedDiagnostics: [],
128-
removedDiagnostics: [],
129-
}
130-
this.disposable.dispose()
131-
this.disposable = languages.registerInlineCompletionItemProvider(
132-
CodeWhispererConstants.platformLanguageIds,
133-
this.inlineCompletionProvider
134-
)
135-
if (item.references && item.references.length) {
136-
const referenceLog = ReferenceLogViewProvider.getReferenceLog(
137-
item.insertText as string,
138-
item.references,
139-
editor
132+
totalSessionDisplayTime: totalSessionDisplayTime,
133+
firstCompletionDisplayLatency: firstCompletionDisplayLatency,
134+
addedDiagnostics: diagnosticDiff.added.map((it) => toIdeDiagnostics(it)),
135+
removedDiagnostics: diagnosticDiff.removed.map((it) => toIdeDiagnostics(it)),
136+
}
137+
this.languageClient.sendNotification(this.logSessionResultMessageName, params)
138+
this.disposable.dispose()
139+
this.disposable = languages.registerInlineCompletionItemProvider(
140+
CodeWhispererConstants.platformLanguageIds,
141+
this.inlineCompletionProvider
140142
)
141-
ReferenceLogViewProvider.instance.addReferenceLog(referenceLog)
142-
ReferenceHoverProvider.instance.addCodeReferences(item.insertText as string, item.references)
143-
}
144-
if (item.mostRelevantMissingImports?.length) {
145-
await ImportAdderProvider.instance.onAcceptRecommendation(editor, item, startLine)
143+
if (item.references && item.references.length) {
144+
const referenceLog = ReferenceLogViewProvider.getReferenceLog(
145+
item.insertText as string,
146+
item.references,
147+
editor
148+
)
149+
ReferenceLogViewProvider.instance.addReferenceLog(referenceLog)
150+
ReferenceHoverProvider.instance.addCodeReferences(item.insertText as string, item.references)
151+
}
152+
if (item.mostRelevantMissingImports?.length) {
153+
await ImportAdderProvider.instance.onAcceptRecommendation(editor, item, startLine)
154+
}
155+
this.sessionManager.incrementSuggestionCount()
156+
// clear session manager states once accepted
157+
this.sessionManager.clear()
158+
} finally {
159+
vsCodeState.isCodeWhispererEditing = false
146160
}
147-
this.sessionManager.incrementSuggestionCount()
148-
// clear session manager states immediately once accepted
149-
this.sessionManager.clear()
150-
151-
// compute diagnostics differences AFTER the session is cleared.
152-
await sleep(1000)
153-
const diagnosticDiff = getDiagnosticsDifferences(
154-
this.sessionManager.getActiveSession()?.diagnosticsBeforeAccept,
155-
getDiagnosticsOfCurrentFile()
156-
)
157-
params.addedDiagnostics = diagnosticDiff.added.map((it) => toIdeDiagnostics(it))
158-
params.removedDiagnostics = diagnosticDiff.removed.map((it) => toIdeDiagnostics(it))
159-
this.languageClient.sendNotification(this.logSessionResultMessageName, params)
160161
}
161162
commands.registerCommand('aws.amazonq.acceptInline', onInlineAcceptance)
162163

163164
const onInlineRejection = async () => {
164-
await commands.executeCommand('editor.action.inlineSuggest.hide')
165-
// TODO: also log the seen state for other suggestions in session
166-
this.disposable.dispose()
167-
this.disposable = languages.registerInlineCompletionItemProvider(
168-
CodeWhispererConstants.platformLanguageIds,
169-
this.inlineCompletionProvider
170-
)
171-
const sessionId = this.sessionManager.getActiveSession()?.sessionId
172-
const itemId = this.sessionManager.getActiveRecommendation()[0]?.itemId
173-
if (!sessionId || !itemId) {
174-
return
175-
}
176-
const params: LogInlineCompletionSessionResultsParams = {
177-
sessionId: sessionId,
178-
completionSessionResult: {
179-
[itemId]: {
180-
seen: true,
181-
accepted: false,
182-
discarded: false,
165+
try {
166+
vsCodeState.isCodeWhispererEditing = true
167+
await commands.executeCommand('editor.action.inlineSuggest.hide')
168+
// TODO: also log the seen state for other suggestions in session
169+
this.disposable.dispose()
170+
this.disposable = languages.registerInlineCompletionItemProvider(
171+
CodeWhispererConstants.platformLanguageIds,
172+
this.inlineCompletionProvider
173+
)
174+
const sessionId = this.sessionManager.getActiveSession()?.sessionId
175+
const itemId = this.sessionManager.getActiveRecommendation()[0]?.itemId
176+
if (!sessionId || !itemId) {
177+
return
178+
}
179+
const params: LogInlineCompletionSessionResultsParams = {
180+
sessionId: sessionId,
181+
completionSessionResult: {
182+
[itemId]: {
183+
seen: true,
184+
accepted: false,
185+
discarded: false,
186+
},
183187
},
184-
},
188+
}
189+
this.languageClient.sendNotification(this.logSessionResultMessageName, params)
190+
// clear session manager states once rejected
191+
this.sessionManager.clear()
192+
} finally {
193+
vsCodeState.isCodeWhispererEditing = false
185194
}
186-
this.languageClient.sendNotification(this.logSessionResultMessageName, params)
187-
// clear session manager states once rejected
188-
this.sessionManager.clear()
189195
}
190196
commands.registerCommand('aws.amazonq.rejectCodeSuggestion', onInlineRejection)
191197
}

0 commit comments

Comments
 (0)