Skip to content

Commit ef53269

Browse files
committed
fix(conv): configurable history trim limit to prevent context loss
We trimmed conversation history at 600KB, over 6x too aggressive, which discarded older turns prematurely and caused context loss in longer agentic sessions. Kiro rejects oversized payloads with CONTENT_LENGTH_EXCEEDS_THRESHOLD; the hard limit is structure-dependent (verified against the live API): a single message survives up to ~7.6MB, but many-entry histories are rejected as low as ~5.9MB. Raise the trim threshold to a configurable max_payload_bytes (default 4MB), which stays safely below the lowest observed failure regardless of history shape while allowing far more context. Add [PAYLOAD] and [TRIM] debug logging so payload growth and trim events are visible.
1 parent 6514194 commit ef53269

4 files changed

Lines changed: 78 additions & 16 deletions

File tree

‎src/__tests__/edge-cases.test.ts‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ describe('sanitizeSchema via convertToolsToCodeWhisperer', () => {
277277
})
278278

279279
describe('payload trim preserves valid structure', () => {
280-
test('large payload trimmed to ≤600KB', () => {
280+
test('oversized payload trimmed below Kiro threshold', () => {
281281
const messages: any[] = []
282-
for (let i = 0; i < 80; i++) {
282+
// ~9MB of raw history so trimming must kick in (default cap is 4MB).
283+
for (let i = 0; i < 300; i++) {
283284
messages.push({ role: 'user', content: 'x'.repeat(10000) })
284285
messages.push({
285286
role: 'assistant',
@@ -291,7 +292,7 @@ describe('payload trim preserves valid structure', () => {
291292
}
292293
messages.push({
293294
role: 'user',
294-
content: [{ type: 'tool_result', tool_use_id: 'tool-79', content: 'result' }]
295+
content: [{ type: 'tool_result', tool_use_id: 'tool-299', content: 'result' }]
295296
})
296297
messages.push({ role: 'user', content: 'final question' })
297298

@@ -301,7 +302,33 @@ describe('payload trim preserves valid structure', () => {
301302
}
302303
const result = transformToSdkRequest(body, 'auto', auth)
303304
const payload = JSON.stringify(result.conversationState)
304-
expect(payload.length).toBeLessThanOrEqual(600_000)
305+
expect(payload.length).toBeLessThanOrEqual(4_000_000)
306+
})
307+
308+
test('respects a custom max_payload_bytes override', () => {
309+
const messages: any[] = []
310+
for (let i = 0; i < 80; i++) {
311+
messages.push({ role: 'user', content: 'x'.repeat(10000) })
312+
messages.push({ role: 'assistant', content: 'y'.repeat(10000) })
313+
}
314+
messages.push({ role: 'user', content: 'final question' })
315+
316+
// Force a tight 500KB cap via the maxPayloadBytes argument.
317+
const result = transformToSdkRequest(
318+
{ messages },
319+
'auto',
320+
auth,
321+
false,
322+
20000,
323+
undefined,
324+
'',
325+
true,
326+
undefined,
327+
undefined,
328+
500_000
329+
)
330+
const payload = JSON.stringify(result.conversationState)
331+
expect(payload.length).toBeLessThanOrEqual(500_000)
305332
})
306333

307334
test('history starts with userInputMessage after trim', () => {
@@ -810,10 +837,10 @@ describe('image carry-forward: no images on tool-result turns', () => {
810837

811838
describe('payload trim performance', () => {
812839
test('trims very large history in <50ms', () => {
813-
// 200 user/asst pairs of 10KB each = ~4MB raw — would be O(N²) without
814-
// the incremental size accounting.
840+
// 500 user/asst pairs of 10KB each = ~10MB raw — over the 4MB default cap,
841+
// so trimming runs and would be O(N²) without the incremental accounting.
815842
const messages: any[] = []
816-
for (let i = 0; i < 200; i++) {
843+
for (let i = 0; i < 500; i++) {
817844
messages.push({ role: 'user', content: 'u'.repeat(10000) })
818845
messages.push({ role: 'assistant', content: 'a'.repeat(10000) })
819846
}
@@ -825,7 +852,7 @@ describe('payload trim performance', () => {
825852

826853
// Result must still be under the limit.
827854
const size = JSON.stringify(result.conversationState).length
828-
expect(size).toBeLessThanOrEqual(600_000)
855+
expect(size).toBeLessThanOrEqual(4_000_000)
829856
// Allow 100ms on slow CI; fail if we regress to seconds (O(N²)).
830857
expect(elapsed).toBeLessThan(100)
831858
})

‎src/core/request/request-handler.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ export class RequestHandler {
293293
this.workspace,
294294
this.config.image_carry_forward,
295295
sessionId,
296-
{ effort: this.config.effort, autoEffortMapping: this.config.auto_effort_mapping }
296+
{ effort: this.config.effort, autoEffortMapping: this.config.auto_effort_mapping },
297+
this.config.max_payload_bytes
297298
)
298299
}
299300

‎src/plugin/config/schema.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,17 @@ export const KiroConfigSchema = z.object({
103103
// Kiro bills per session (request), not per token, so re-sending the same
104104
// images each turn has no billing impact. Only disable if you hit the
105105
// per-request 3.75MB image-payload cap on conversations with many heavy images.
106-
image_carry_forward: z.boolean().default(true)
106+
image_carry_forward: z.boolean().default(true),
107+
108+
// Maximum conversation-state payload size (bytes) before the plugin trims the
109+
// oldest history entries. Kiro's runtime endpoint rejects oversized payloads
110+
// with CONTENT_LENGTH_EXCEEDS_THRESHOLD. The hard limit is structure-dependent
111+
// (verified against the live API): a single message is accepted up to ~7.6MB,
112+
// but conversations with many history entries are rejected as low as ~5.9MB.
113+
// The 4MB default stays safely below the lowest observed failure regardless of
114+
// structure, while allowing far more context than a conservative cap. Raising
115+
// it risks 400s on long many-turn sessions; lowering it trims context sooner.
116+
max_payload_bytes: z.number().min(100_000).max(5_500_000).default(4_000_000)
107117
})
108118

109119
export type KiroConfig = z.infer<typeof KiroConfigSchema>
@@ -123,5 +133,6 @@ export const DEFAULT_CONFIG: KiroConfig = {
123133
auto_sync_kiro_cli: true,
124134
enable_log_api_request: false,
125135
auto_effort_mapping: true,
126-
image_carry_forward: true
136+
image_carry_forward: true,
137+
max_payload_bytes: 4_000_000
127138
}

‎src/plugin/request.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ function buildCodeWhispererRequest(
9797
showToast?: ToastFunction,
9898
workspace = '',
9999
carryForward = true,
100-
sessionId?: string
100+
sessionId?: string,
101+
maxPayloadBytes = 4_000_000
101102
): TransformResult {
102103
const req = typeof body === 'string' ? JSON.parse(body) : body
103104
const { messages, tools, system } = req
@@ -351,16 +352,24 @@ function buildCodeWhispererRequest(
351352
}
352353
}
353354

354-
// Trim history if payload exceeds Kiro's ~615KB limit.
355+
// Trim history if the payload approaches Kiro's request-size limit.
356+
// Kiro rejects oversized payloads with CONTENT_LENGTH_EXCEEDS_THRESHOLD. The
357+
// hard limit is structure-dependent (verified against the live API): a single
358+
// message survives up to ~7.6MB, but many-entry histories are rejected as low
359+
// as ~5.9MB. The configurable maxPayloadBytes (default 4MB) stays safely below
360+
// the lowest observed failure regardless of history shape.
355361
// Compute per-entry sizes once and shrink incrementally to avoid O(N²)
356362
// re-stringifying the full request on every iteration.
357-
const MAX_PAYLOAD_BYTES = 600_000
363+
const MAX_PAYLOAD_BYTES = maxPayloadBytes
364+
const trimStartLen = history.length
365+
let trimSizeBefore = 0
358366
if (history.length > 2) {
359367
const sizes = history.map((h) => JSON.stringify(h).length + 1)
360368
const baseRequest: any = { ...request, conversationState: { ...request.conversationState } }
361369
delete baseRequest.conversationState.history
362370
let totalSize = JSON.stringify(baseRequest).length + 2 // for `"history":[]`
363371
for (const s of sizes) totalSize += s
372+
trimSizeBefore = totalSize
364373

365374
while (history.length > 2 && totalSize > MAX_PAYLOAD_BYTES) {
366375
// Drop the two oldest entries (typically user + assistant pair).
@@ -390,6 +399,12 @@ function buildCodeWhispererRequest(
390399
}
391400
}
392401

402+
if (trimStartLen !== history.length) {
403+
logger.debug(
404+
`[TRIM] history ${trimStartLen}→${history.length} entries, ~${Math.round(trimSizeBefore / 1024)}KB exceeded ${Math.round(MAX_PAYLOAD_BYTES / 1024)}KB cap`
405+
)
406+
}
407+
393408
// After trimming, drop any current-message toolResults whose tool_use was
394409
// removed from history — sending a toolResult without a matching toolUse
395410
// in the surviving history causes Bedrock 400 ValidationException.
@@ -515,7 +530,8 @@ export function transformToSdkRequest(
515530
workspace = '',
516531
carryForward = true,
517532
sessionId?: string,
518-
effortConfig?: EffortConfig
533+
effortConfig?: EffortConfig,
534+
maxPayloadBytes = 4_000_000
519535
): SdkPreparedRequest {
520536
const { request, resolved, convId, fingerprint, toolNameMapper } = buildCodeWhispererRequest(
521537
body,
@@ -526,7 +542,8 @@ export function transformToSdkRequest(
526542
showToast,
527543
workspace,
528544
carryForward,
529-
sessionId
545+
sessionId,
546+
maxPayloadBytes
530547
)
531548

532549
// Resolve effort level based on config and model capabilities
@@ -544,6 +561,12 @@ export function transformToSdkRequest(
544561
const endpointFull = resolveKiroEndpoint(auth)
545562
const endpoint = endpointFull.replace(/\/generateAssistantResponse$/, '')
546563
const workspaceKey = sessionId ? `sess:${sessionId}` : workspace
564+
if (process.env.DEBUG || process.env.OPENCODE_LOG_LEVEL === 'debug') {
565+
const finalBytes = JSON.stringify(request.conversationState).length
566+
logger.debug(
567+
`[PAYLOAD] convId=${convId} ~${Math.round(finalBytes / 1024)}KB history=${request.conversationState.history?.length ?? 0} entries`
568+
)
569+
}
547570
return {
548571
conversationState: request.conversationState,
549572
profileArn: request.profileArn,

0 commit comments

Comments
 (0)