Skip to content

Commit 3f48b12

Browse files
authored
revert: revert for mid-loop compaction
This reverts commit 35f0795.
1 parent 088f0ba commit 3f48b12

File tree

7 files changed

+16
-132
lines changed

7 files changed

+16
-132
lines changed

server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,21 +1185,15 @@ describe('AgenticChatController', () => {
11851185

11861186
it('truncate input to 500k character ', async function () {
11871187
const input = 'X'.repeat(GENERATE_ASSISTANT_RESPONSE_INPUT_LIMIT + 10)
1188-
const request: GenerateAssistantResponseCommandInput = {
1189-
conversationState: {
1190-
currentMessage: {
1191-
userInputMessage: {
1192-
content: input,
1193-
},
1194-
},
1195-
chatTriggerType: undefined,
1196-
},
1197-
}
1198-
1199-
chatController.truncateRequest(request)
1200-
1188+
generateAssistantResponseStub.restore()
1189+
generateAssistantResponseStub = sinon.stub(CodeWhispererStreaming.prototype, 'generateAssistantResponse')
1190+
generateAssistantResponseStub.callsFake(() => {})
1191+
await chatController.onChatPrompt({ tabId: mockTabId, prompt: { prompt: input } }, mockCancellationToken)
1192+
assert.ok(generateAssistantResponseStub.called)
1193+
const calledRequestInput: GenerateAssistantResponseCommandInput =
1194+
generateAssistantResponseStub.firstCall.firstArg
12011195
assert.deepStrictEqual(
1202-
request.conversationState?.currentMessage?.userInputMessage?.content?.length,
1196+
calledRequestInput.conversationState?.currentMessage?.userInputMessage?.content?.length,
12031197
GENERATE_ASSISTANT_RESPONSE_INPUT_LIMIT
12041198
)
12051199
})

server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ import {
191191
MAX_OVERALL_CHARACTERS,
192192
FSREAD_MEMORY_BANK_MAX_PER_FILE,
193193
FSREAD_MEMORY_BANK_MAX_TOTAL,
194-
MID_LOOP_COMPACTION_HANDOFF_PROMPT,
195-
COMPACTION_PROMPT,
196194
} from './constants/constants'
197195
import {
198196
AgenticChatError,
@@ -948,11 +946,6 @@ export class AgenticChatController implements ChatHandlers {
948946

949947
const compactIds = session.getAllDeferredCompactMessageIds()
950948
await this.#invalidateCompactCommand(params.tabId, compactIds)
951-
// Set compactionDeclined flag if there were pending compaction requests
952-
// This prevents endless compaction warning loops when user declines compaction once
953-
if (compactIds.length > 0) {
954-
session.compactionDeclined = true
955-
}
956949
session.rejectAllDeferredToolExecutions(new ToolApprovalException('Command ignored: new prompt', false))
957950
await this.#invalidateAllShellCommands(params.tabId, session)
958951

@@ -988,10 +981,6 @@ export class AgenticChatController implements ChatHandlers {
988981
session.abortRequest()
989982
const compactIds = session.getAllDeferredCompactMessageIds()
990983
await this.#invalidateCompactCommand(params.tabId, compactIds)
991-
// Set compactionDeclined flag if there were pending compaction requests
992-
if (compactIds.length > 0) {
993-
session.compactionDeclined = true
994-
}
995984
void this.#invalidateAllShellCommands(params.tabId, session)
996985
session.rejectAllDeferredToolExecutions(new CancellationError('user'))
997986

@@ -1109,7 +1098,7 @@ export class AgenticChatController implements ChatHandlers {
11091098
}
11101099

11111100
// Result Handling - This happens only once
1112-
const result = await this.#handleFinalResult(
1101+
return await this.#handleFinalResult(
11131102
finalResult,
11141103
session,
11151104
params.tabId,
@@ -1118,13 +1107,6 @@ export class AgenticChatController implements ChatHandlers {
11181107
isNewConversation,
11191108
chatResultStream
11201109
)
1121-
1122-
// Reset compactionDeclined flag after successful completion
1123-
if (session.compactionDeclined) {
1124-
session.compactionDeclined = false
1125-
}
1126-
1127-
return result
11281110
} catch (err) {
11291111
// HACK: the chat-client needs to have a partial event with the associated messageId sent before it can accept the final result.
11301112
// Without this, the `working` indicator never goes away.
@@ -1196,29 +1178,25 @@ export class AgenticChatController implements ChatHandlers {
11961178
/**
11971179
* Prepares the initial request input for the chat prompt
11981180
*/
1199-
#getCompactionRequestInput(session: ChatSessionService, toolResults?: any[]): ChatCommandInput {
1181+
#getCompactionRequestInput(session: ChatSessionService): ChatCommandInput {
12001182
this.#debug('Preparing compaction request input')
12011183
// Get profileArn from the service manager if available
12021184
const profileArn = this.#serviceManager?.getActiveProfileArn()
12031185
const requestInput = this.#triggerContext.getCompactionChatCommandInput(
12041186
profileArn,
12051187
this.#getTools(session),
12061188
session.modelId,
1207-
this.#origin,
1208-
toolResults
1189+
this.#origin
12091190
)
12101191
return requestInput
12111192
}
12121193

12131194
/**
12141195
* Runs the compaction, making requests and processing tool uses until completion
12151196
*/
1216-
#shouldCompact(currentRequestCount: number, session: ChatSessionService): boolean {
1217-
const EFFECTIVE_COMPACTION_THRESHOLD = COMPACTION_CHARACTER_THRESHOLD - COMPACTION_PROMPT.length
1218-
if (currentRequestCount > EFFECTIVE_COMPACTION_THRESHOLD && !session.compactionDeclined) {
1219-
this.#debug(
1220-
`Current request total character count is: ${currentRequestCount}, prompting user to compact (threshold: ${EFFECTIVE_COMPACTION_THRESHOLD})`
1221-
)
1197+
#shouldCompact(currentRequestCount: number): boolean {
1198+
if (currentRequestCount > COMPACTION_CHARACTER_THRESHOLD) {
1199+
this.#debug(`Current request total character count is: ${currentRequestCount}, prompting user to compact`)
12221200
return true
12231201
} else {
12241202
return false
@@ -1401,10 +1379,6 @@ export class AgenticChatController implements ChatHandlers {
14011379
let currentRequestCount = 0
14021380
const pinnedContext = additionalContext?.filter(item => item.pinned)
14031381

1404-
// Store initial non-empty prompt for compaction handoff
1405-
const initialPrompt =
1406-
initialRequestInput.conversationState?.currentMessage?.userInputMessage?.content?.trim() || ''
1407-
14081382
metric.recordStart()
14091383
this.logSystemInformation()
14101384
while (true) {
@@ -1469,63 +1443,6 @@ export class AgenticChatController implements ChatHandlers {
14691443
this.#llmRequestStartTime = Date.now()
14701444
// Phase 3: Request Execution
14711445
currentRequestInput = sanitizeRequestInput(currentRequestInput)
1472-
1473-
if (this.#shouldCompact(currentRequestCount, session)) {
1474-
this.#features.logging.info(
1475-
`Entering mid-loop compaction at iteration ${iterationCount} with ${currentRequestCount} characters`
1476-
)
1477-
this.#telemetryController.emitMidLoopCompaction(
1478-
currentRequestCount,
1479-
iterationCount,
1480-
this.#features.runtime.serverInfo.version ?? ''
1481-
)
1482-
const messageId = this.#getMessageIdForCompact(uuid())
1483-
const confirmationResult = this.#processCompactConfirmation(messageId, currentRequestCount)
1484-
const cachedButtonBlockId = await chatResultStream.writeResultBlock(confirmationResult)
1485-
await this.waitForCompactApproval(messageId, chatResultStream, cachedButtonBlockId, session)
1486-
1487-
// Run compaction
1488-
const toolResults =
1489-
currentRequestInput.conversationState?.currentMessage?.userInputMessage?.userInputMessageContext
1490-
?.toolResults || []
1491-
const compactionRequestInput = this.#getCompactionRequestInput(session, toolResults)
1492-
const compactionResult = await this.#runCompaction(
1493-
compactionRequestInput,
1494-
session,
1495-
metric,
1496-
chatResultStream,
1497-
tabId,
1498-
promptId,
1499-
CompactHistoryActionType.Nudge,
1500-
session.conversationId,
1501-
token,
1502-
documentReference
1503-
)
1504-
1505-
if (!compactionResult.success) {
1506-
this.#features.logging.error(`Compaction failed: ${compactionResult.error}`)
1507-
return compactionResult
1508-
}
1509-
1510-
// Show compaction summary to user before continuing
1511-
await chatResultStream.writeResultBlock({
1512-
type: 'answer',
1513-
body:
1514-
(compactionResult.data?.chatResult.body || '') +
1515-
'\n\nConversation history has been compacted successfully!',
1516-
messageId: uuid(),
1517-
})
1518-
1519-
currentRequestInput = this.#updateRequestInputWithToolResults(
1520-
currentRequestInput,
1521-
[],
1522-
MID_LOOP_COMPACTION_HANDOFF_PROMPT + initialPrompt
1523-
)
1524-
shouldDisplayMessage = false
1525-
this.#features.logging.info(`Completed mid-loop compaction, restarting loop with handoff prompt`)
1526-
continue
1527-
}
1528-
15291446
// Note: these logs are very noisy, but contain information redacted on the backend.
15301447
this.#debug(
15311448
`generateAssistantResponse/SendMessage Request: ${JSON.stringify(currentRequestInput, this.#imageReplacer, 2)}`
@@ -1754,7 +1671,7 @@ export class AgenticChatController implements ChatHandlers {
17541671
currentRequestInput = this.#updateRequestInputWithToolResults(currentRequestInput, toolResults, content)
17551672
}
17561673

1757-
if (this.#shouldCompact(currentRequestCount, session)) {
1674+
if (this.#shouldCompact(currentRequestCount)) {
17581675
this.#telemetryController.emitCompactNudge(
17591676
currentRequestCount,
17601677
this.#features.runtime.serverInfo.version ?? ''

server/aws-lsp-codewhisperer/src/language-server/agenticChat/constants/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export const GENERATE_ASSISTANT_RESPONSE_INPUT_LIMIT = 500_000
1818
// 200K tokens * 3.5 = 700K characters, intentionally overestimating with 3.5:1 ratio
1919
export const MAX_OVERALL_CHARACTERS = 700_000
2020
export const COMPACTION_CHARACTER_THRESHOLD = 0.7 * MAX_OVERALL_CHARACTERS
21-
// TODO: We need to carefully craft a prompt for this supported by the science team
22-
export const MID_LOOP_COMPACTION_HANDOFF_PROMPT = `CONTEXT HANDOFF: Previous conversation was compacted. Continue with user's request: `
2321
export const COMPACTION_BODY = (threshold: number) =>
2422
`The context window is almost full (${threshold}%) and exceeding it will clear your history. Amazon Q can compact your history instead.`
2523
export const COMPACTION_HEADER_BODY = 'Compact chat history?'

server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/agenticChatTriggerContext.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,13 @@ export class AgenticChatTriggerContext {
113113
* @param tools Optional Bedrock tools
114114
* @param modelId Optional model ID
115115
* @param origin Optional origin
116-
* @param toolResults Optional tool results to include in compaction context
117116
* @returns ChatCommandInput - which is either SendMessageInput or GenerateAssistantResponseInput
118117
*/
119118
getCompactionChatCommandInput(
120119
profileArn?: string,
121120
tools: BedrockTools = [],
122121
modelId?: string,
123-
origin?: Origin,
124-
toolResults?: any[]
122+
origin?: Origin
125123
): ChatCommandInput {
126124
const data: ChatCommandInput = {
127125
conversationState: {
@@ -132,7 +130,6 @@ export class AgenticChatTriggerContext {
132130
userInputMessageContext: {
133131
tools,
134132
envState: this.#mapPlatformToEnvState(process.platform),
135-
toolResults: toolResults,
136133
},
137134
userIntent: undefined,
138135
origin: origin ? origin : 'IDE',

server/aws-lsp-codewhisperer/src/language-server/chat/chatSessionService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export class ChatSessionService {
3030
public contextListSent: boolean = false
3131
public modelId: string | undefined
3232
public isMemoryBankGeneration: boolean = false
33-
public compactionDeclined: boolean = false
3433
#lsp?: Features['lsp']
3534
#abortController?: AbortController
3635
#currentPromptId?: string

server/aws-lsp-codewhisperer/src/language-server/chat/telemetry/chatTelemetryController.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,6 @@ export class ChatTelemetryController {
243243
})
244244
}
245245

246-
public emitMidLoopCompaction(characters: number, iterationCount: number, languageServerVersion: string) {
247-
this.#telemetry.emitMetric({
248-
name: ChatTelemetryEventName.MidLoopCompaction,
249-
data: {
250-
characters,
251-
iterationCount,
252-
credentialStartUrl: this.#credentialsProvider.getConnectionMetadata()?.sso?.startUrl,
253-
languageServerVersion: languageServerVersion,
254-
},
255-
})
256-
}
257-
258246
public emitToolUseSuggested(
259247
toolUse: ToolUse,
260248
conversationId: string,

server/aws-lsp-codewhisperer/src/shared/telemetry/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ export enum ChatTelemetryEventName {
207207
LoadHistory = 'amazonq_loadHistory',
208208
CompactHistory = 'amazonq_compactHistory',
209209
CompactNudge = 'amazonq_compactNudge',
210-
MidLoopCompaction = 'amazonq_midLoopCompaction',
211210
ChatHistoryAction = 'amazonq_performChatHistoryAction',
212211
ExportTab = 'amazonq_exportTab',
213212
UiClick = 'ui_click',
@@ -234,7 +233,6 @@ export interface ChatTelemetryEventMap {
234233
[ChatTelemetryEventName.LoadHistory]: LoadHistoryEvent
235234
[ChatTelemetryEventName.CompactHistory]: CompactHistoryEvent
236235
[ChatTelemetryEventName.CompactNudge]: CompactNudgeEvent
237-
[ChatTelemetryEventName.MidLoopCompaction]: MidLoopCompactionEvent
238236
[ChatTelemetryEventName.ChatHistoryAction]: ChatHistoryActionEvent
239237
[ChatTelemetryEventName.ExportTab]: ExportTabEvent
240238
[ChatTelemetryEventName.UiClick]: UiClickEvent
@@ -406,13 +404,6 @@ export type CompactNudgeEvent = {
406404
languageServerVersion?: string
407405
}
408406

409-
export type MidLoopCompactionEvent = {
410-
characters: number
411-
iterationCount: number
412-
credentialStartUrl?: string
413-
languageServerVersion?: string
414-
}
415-
416407
export type ChatHistoryActionEvent = {
417408
action: ChatHistoryActionType
418409
result: Result

0 commit comments

Comments
 (0)