Skip to content

Commit fa431af

Browse files
authored
fix(amazonq): /test may truncate user prompt #6557
## Problem - If user gives user input prompt more than 4096 characters, this fails at RTS as model supports only 4096 input characters. `Value at 'userInput' failed to satisfy constraint: Member must have length less than or equal to 4096` - Ideally we should not get more than 4096 char input from user input but still features like `/test`, `/dev` were able to get the message length more than 4096. ## Solution - To resolve this issue, we are adding a truncation logic to continue the test generation workflow instead of failing at RTS. - No new tests were added as the functionality is not impacted.
1 parent f738715 commit fa431af

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Amazon Q /test: Truncating user input to 4096 characters for unit test generation."
4+
}

packages/core/src/amazonqTest/chat/controller/controller.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
testGenCompletedField,
2222
testGenProgressField,
2323
testGenSummaryMessage,
24+
maxUserPromptLength,
2425
} from '../../models/constants'
2526
import MessengerUtils, { ButtonActions } from './messenger/messengerUtils'
2627
import { getTelemetryReasonDesc, isAwsError } from '../../../shared/errors'
@@ -365,7 +366,8 @@ export class TestController {
365366
getLogger().debug('startTestGen tabId: %O', message.tabID)
366367
let fileName = ''
367368
let filePath = ''
368-
let userMessage = ''
369+
let userFacingMessage = ''
370+
let userPrompt = ''
369371
session.testGenerationStartTime = performance.now()
370372

371373
try {
@@ -381,6 +383,8 @@ export class TestController {
381383
)
382384
return
383385
}
386+
// Truncating the user prompt if the prompt is more than 4096.
387+
userPrompt = message.prompt.slice(0, maxUserPromptLength)
384388

385389
// check that the session is authenticated
386390
const authState = await AuthUtil.instance.getChatAuthState()
@@ -425,16 +429,16 @@ export class TestController {
425429
getLogger().debug(`File path: ${fileEditorToTest.document.uri.fsPath}`)
426430
filePath = fileEditorToTest.document.uri.fsPath
427431
fileName = path.basename(filePath)
428-
userMessage = message.prompt
432+
userFacingMessage = userPrompt
429433
? regenerateTests
430-
? `${message.prompt}`
431-
: `/test ${message.prompt}`
434+
? `${userPrompt}`
435+
: `/test ${userPrompt}`
432436
: `/test Generate unit tests for \`${fileName}\``
433437

434-
session.hasUserPromptSupplied = message.prompt.length > 0
438+
session.hasUserPromptSupplied = userPrompt.length > 0
435439

436440
// displaying user message prompt in Test tab
437-
this.messenger.sendMessage(userMessage, tabID, 'prompt')
441+
this.messenger.sendMessage(userFacingMessage, tabID, 'prompt')
438442
this.messenger.sendChatInputEnabled(tabID, false)
439443
this.sessionStorage.getSession().conversationState = ConversationState.IN_PROGRESS
440444
this.messenger.sendUpdatePromptProgress(message.tabID, testGenProgressField)
@@ -460,7 +464,7 @@ export class TestController {
460464
this.messenger.sendMessage(unsupportedMessage, tabID, 'answer')
461465
await this.onCodeGeneration(
462466
session,
463-
message.prompt,
467+
userPrompt,
464468
tabID,
465469
fileName,
466470
filePath,

packages/core/src/amazonqTest/models/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { BuildStatus } from '../chat/session/session'
1111
// For uniquely identifiying which chat messages should be routed to Test
1212
export const testChat = 'testChat'
1313

14+
export const maxUserPromptLength = 4096 // user prompt character limit from MPS and API model.
15+
1416
export const cancelTestGenButton: ChatItemButton = {
1517
id: ButtonActions.STOP_TEST_GEN,
1618
text: 'Cancel',

0 commit comments

Comments
 (0)