Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(amazonq): validationException if empty supplemental context chunk is sent to the service #5920

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Bug Fix",
"description": "Fix empty chunks being sent to service and get validationException"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as vscode from 'vscode'
import * as sinon from 'sinon'
import * as crossFile from 'aws-core-vscode/codewhisperer'
import { aStringWithLineCount, createMockTextEditor } from 'aws-core-vscode/test'
import { crossFileContextConfig } from 'aws-core-vscode/codewhisperer'
import { FeatureConfigProvider, crossFileContextConfig } from 'aws-core-vscode/codewhisperer'
import {
assertTabCount,
closeAllEditors,
Expand All @@ -30,12 +30,17 @@ describe('crossFileContextUtil', function () {

let mockEditor: vscode.TextEditor

afterEach(function () {
sinon.restore()
})

describe('fetchSupplementalContextForSrc', function () {
beforeEach(async function () {
tempFolder = (await createTestWorkspaceFolder()).uri.fsPath
})

it('should fetch 3 chunks and each chunk should contains 50 lines', async function () {
it('opentabs context should fetch 3 chunks and each chunk should contains 50 lines', async function () {
sinon.stub(FeatureConfigProvider.instance, 'isNewProjectContextGroup').alwaysReturned(false)
await toTextEditor(aStringWithLineCount(200), 'CrossFile.java', tempFolder, { preview: false })
const myCurrentEditor = await toTextEditor('', 'TargetFile.java', tempFolder, {
preview: false,
Expand Down Expand Up @@ -207,6 +212,7 @@ describe('crossFileContextUtil', function () {

fileExtLists.forEach((fileExt) => {
it('should be non empty', async function () {
sinon.stub(FeatureConfigProvider.instance, 'isNewProjectContextGroup').alwaysReturned(false)
const editor = await toTextEditor('content-1', `file-1.${fileExt}`, tempFolder)
await toTextEditor('content-2', `file-2.${fileExt}`, tempFolder, { preview: false })
await toTextEditor('content-3', `file-3.${fileExt}`, tempFolder, { preview: false })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import assert from 'assert'
import * as vscode from 'vscode'
import * as sinon from 'sinon'
import * as crossFile from 'aws-core-vscode/codewhisperer'
import { TestFolder } from 'aws-core-vscode/test'
import { FeatureConfigProvider } from 'aws-core-vscode/codewhisperer'
import { toTextEditor } from 'aws-core-vscode/test'

describe('supplementalContextUtil', function () {
let testFolder: TestFolder

const fakeCancellationToken: vscode.CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: sinon.spy(),
}

beforeEach(async function () {
testFolder = await TestFolder.create()
sinon.stub(FeatureConfigProvider.instance, 'isNewProjectContextGroup').alwaysReturned(false)
})

afterEach(function () {
sinon.restore()
})

describe('fetchSupplementalContext', function () {
describe('openTabsContext', function () {
it('opentabContext should include chunks if non empty', async function () {
await toTextEditor('class Foo', 'Foo.java', testFolder.path, { preview: false })
await toTextEditor('class Bar', 'Bar.java', testFolder.path, { preview: false })
await toTextEditor('class Baz', 'Baz.java', testFolder.path, { preview: false })

const editor = await toTextEditor('public class Foo {}', 'Query.java', testFolder.path, {
preview: false,
})

const actual = await crossFile.fetchSupplementalContext(editor, fakeCancellationToken)
assert.ok(actual?.supplementalContextItems.length === 3)
})

it('opentabsContext should filter out empty chunks', async function () {
// open 3 files as supplemental context candidate files but none of them have contents
await toTextEditor('', 'Foo.java', testFolder.path, { preview: false })
await toTextEditor('', 'Bar.java', testFolder.path, { preview: false })
await toTextEditor('', 'Baz.java', testFolder.path, { preview: false })

const editor = await toTextEditor('public class Foo {}', 'Query.java', testFolder.path, {
preview: false,
})

const actual = await crossFile.fetchSupplementalContext(editor, fakeCancellationToken)
assert.ok(actual?.supplementalContextItems.length === 0)
})
})
})
})
31 changes: 20 additions & 11 deletions packages/core/src/codewhisperer/util/editorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { selectFrom } from '../../shared/utilities/tsUtils'
import { checkLeftContextKeywordsForJson } from './commonUtil'
import { CodeWhispererSupplementalContext } from '../models/model'
import { getOptOutPreference } from '../../shared/telemetry/util'
import { indent } from '../../shared'

let tabSize: number = getTabSizeSetting()

Expand Down Expand Up @@ -202,18 +203,26 @@ function logSupplementalContext(supplementalContext: CodeWhispererSupplementalCo
return
}

let logString = `CodeWhispererSupplementalContext:
isUtg: ${supplementalContext.isUtg},
isProcessTimeout: ${supplementalContext.isProcessTimeout},
contentsLength: ${supplementalContext.contentsLength},
latency: ${supplementalContext.latency},
`
let logString = indent(
`CodeWhispererSupplementalContext:
isUtg: ${supplementalContext.isUtg},
isProcessTimeout: ${supplementalContext.isProcessTimeout},
contentsLength: ${supplementalContext.contentsLength},
latency: ${supplementalContext.latency}
strategy: ${supplementalContext.strategy}`,
4,
true
).trimStart()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks for thinking about the log format, that helps with troubleshooting for both users and devs.


supplementalContext.supplementalContextItems.forEach((context, index) => {
logString += `Chunk ${index}:
Path: ${context.filePath}
Content: ${index}:${context.content}
Score: ${context.score}
-----------------------------------------------`
logString += indent(`\nChunk ${index}:\n`, 4, true)
logString += indent(
`Path: ${context.filePath}
Length: ${context.content.length}
Score: ${context.score}`,
8,
true
)
})

getLogger().debug(logString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export async function fetchSupplementalContextForSrcV1(
// DO NOT send code chunk with empty content
getLogger().debug(`CodeWhisperer finished fetching crossfile context out of ${relevantCrossFilePaths.length} files`)
return {
supplementalContextItems: supplementalContexts.filter((item) => item.content.trim().length !== 0),
Copy link
Contributor Author

@Will-ShaoHua Will-ShaoHua Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to upstream caller supplementalContextUtil.ts

supplementalContextItems: supplementalContexts,
strategy: 'OpenTabs_BM25',
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export async function fetchSupplementalContext(
return {
isUtg: isUtg,
isProcessTimeout: false,
supplementalContextItems: value.supplementalContextItems,
supplementalContextItems: value.supplementalContextItems.filter(
(item) => item.content.trim().length !== 0
),
contentsLength: value.supplementalContextItems.reduce((acc, curr) => acc + curr.content.length, 0),
latency: performance.now() - timesBeforeFetching,
strategy: value.strategy,
Expand Down
Loading