Skip to content
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 inline completion supplementalContext length exceeding maximum in certain cases"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import * as FakeTimers from '@sinonjs/fake-timers'
import * as vscode from 'vscode'
import * as sinon from 'sinon'
import * as crossFile from 'aws-core-vscode/codewhisperer'
import { aStringWithLineCount, createMockTextEditor, installFakeClock } from 'aws-core-vscode/test'
import {
aLongStringWithLineCount,
aStringWithLineCount,
createMockTextEditor,
installFakeClock,
} from 'aws-core-vscode/test'
import { FeatureConfigProvider, crossFileContextConfig } from 'aws-core-vscode/codewhisperer'
import {
assertTabCount,
Expand Down Expand Up @@ -71,8 +76,8 @@ describe('crossFileContextUtil', function () {
assert.strictEqual(actual.supplementalContextItems[2].content.split('\n').length, 50)
})

it('for t1 group, should return repomap + opentabs context', async function () {
await toTextEditor(aStringWithLineCount(200), 'CrossFile.java', tempFolder, { preview: false })
it('for t1 group, should return repomap + opentabs context, should not exceed 20k total length', async function () {
await toTextEditor(aLongStringWithLineCount(200), 'CrossFile.java', tempFolder, { preview: false })
const myCurrentEditor = await toTextEditor('', 'TargetFile.java', tempFolder, {
preview: false,
})
Expand All @@ -85,25 +90,23 @@ describe('crossFileContextUtil', function () {
.withArgs(sinon.match.any, sinon.match.any, 'codemap')
.resolves([
{
content: 'foo',
content: 'foo'.repeat(3000),
score: 0,
filePath: 'q-inline',
},
])

const actual = await crossFile.fetchSupplementalContextForSrc(myCurrentEditor, fakeCancellationToken)
assert.ok(actual)
assert.strictEqual(actual.supplementalContextItems.length, 4)
assert.strictEqual(actual.supplementalContextItems.length, 3)
assert.strictEqual(actual?.strategy, 'codemap')
assert.deepEqual(actual?.supplementalContextItems[0], {
content: 'foo',
content: 'foo'.repeat(3000),
score: 0,
filePath: 'q-inline',
})

assert.strictEqual(actual.supplementalContextItems[1].content.split('\n').length, 50)
assert.strictEqual(actual.supplementalContextItems[2].content.split('\n').length, 50)
assert.strictEqual(actual.supplementalContextItems[3].content.split('\n').length, 50)
})

it.skip('for t2 group, should return global bm25 context and no repomap', async function () {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/codewhisperer/models/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const lineBreakWin = '\r\n'

export const supplementalContextTimeoutInMs = 100

export const supplementalContextMaxTotalLength = 20480
/**
* Ux of recommendations
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import * as vscode from 'vscode'
import { FeatureConfigProvider, fs } from '../../../shared'
import path = require('path')
import { BM25Document, BM25Okapi } from './rankBm25'
import { crossFileContextConfig, supplementalContextTimeoutInMs } from '../../models/constants'
import {
crossFileContextConfig,
supplementalContextTimeoutInMs,
supplementalContextMaxTotalLength,
} from '../../models/constants'
import { isTestFile } from './codeParsingUtil'
import { getFileDistance } from '../../../shared/filesystemUtilities'
import { getOpenFilesInWindow } from '../../../shared/utilities/editorUtilities'
Expand Down Expand Up @@ -99,13 +103,22 @@ export async function fetchSupplementalContextForSrc(
const opentabsContext = await fetchOpentabsContext(editor, cancellationToken)
const codemap = await fetchProjectContext(editor, 'codemap')

function addToResult(items: CodeWhispererSupplementalContextItem[]) {
for (const item of items) {
const curLen = result.reduce((acc, i) => acc + i.content.length, 0)
if (curLen + item.content.length < supplementalContextMaxTotalLength) {
result.push(item)
}
}
}

if (codemap && codemap.length > 0) {
result.push(...codemap)
addToResult(codemap)
hasCodemap = true
}

if (opentabsContext && opentabsContext.length > 0) {
result.push(...opentabsContext)
addToResult(opentabsContext)
hasOpentabs = true
}

Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/test/codewhisperer/testUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,12 @@ export function aStringWithLineCount(lineCount: number, start: number = 0): stri

return s.trimEnd()
}

export function aLongStringWithLineCount(lineCount: number, start: number = 0): string {
let s = ''
for (let i = start; i < start + lineCount; i++) {
s += `a`.repeat(100) + `line${i}\n`
}

return s.trimEnd()
}
Comment on lines +333 to +340
Copy link
Contributor

@Will-ShaoHua Will-ShaoHua Jan 14, 2025

Choose a reason for hiding this comment

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

todo: let's clean up and merge the 2 test util funcs aStringWithLineCount and aLongStringWithLineCount

Copy link
Contributor

Choose a reason for hiding this comment

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

not a blocker

Loading