@@ -457,6 +457,29 @@ export function bridgeToResponsesSSE(
457457 retainFinishedItem ( item as OutputItem , bytesOf ( encrypted ) , "reasoning" ) ;
458458 outputIndex ++ ;
459459 } ;
460+ // Kiro reasoning round-trip. Kiro sends its encrypted blob at the END of a turn, while the
461+ // assistant message is still open, so this CANNOT emit on arrival: the open message still
462+ // owns `outputIndex` (it only advances on close), and an item emitted here would both reuse
463+ // that index and land BEFORE the message — where the parser's backwards pairing drops it as
464+ // orphaned. Stash it and flush after `done` has closed every open item instead.
465+ let pendingKiroRedacted : string | undefined ;
466+ let pendingKiroRedactedBytes = 0 ;
467+ const flushKiroRedactedReasoning = ( ) => {
468+ if ( ! pendingKiroRedacted ) return ;
469+ const previousBytes = pendingKiroRedactedBytes ;
470+ const encrypted = encodeReasoningEnvelope ( { krc : pendingKiroRedacted } ) ;
471+ const reservation = budget ?. reserveTransient ( bytesOf ( encrypted ) , { kind : "reasoning" } ) ;
472+ pendingKiroRedacted = undefined ;
473+ pendingKiroRedactedBytes = 0 ;
474+ reservation ?. commitRetained ( ) ;
475+ budget ?. releaseRetained ( previousBytes , { kind : "reasoning" } ) ;
476+ const itemId = `rs_${ uuid ( ) } ` ;
477+ const item = { type : "reasoning" , id : itemId , summary : [ ] as never [ ] , encrypted_content : encrypted } ;
478+ emit ( "response.output_item.added" , { output_index : outputIndex , item } ) ;
479+ emit ( "response.output_item.done" , { output_index : outputIndex , item } ) ;
480+ retainFinishedItem ( item as OutputItem , bytesOf ( encrypted ) , "reasoning" ) ;
481+ outputIndex ++ ;
482+ } ;
460483 // Full assistant text of a compaction turn (across message boundaries) — becomes the
461484 // synthetic compaction item's payload on done.
462485 let compactionText = "" ;
@@ -895,6 +918,12 @@ export function bridgeToResponsesSSE(
895918 pendingRedacted . push ( event . data ) ;
896919 break ;
897920 }
921+ case "kiro_redacted_reasoning" : {
922+ // Stash only — see flushKiroRedactedReasoning. One blob per turn, so last wins.
923+ pendingKiroRedactedBytes = replaceRetainedString ( pendingKiroRedactedBytes , event . data , "reasoning" ) ;
924+ pendingKiroRedacted = event . data ;
925+ break ;
926+ }
898927 case "reasoning_raw_delta" : {
899928 if ( options ?. hideThinkingSummary ) {
900929 ( { value : hiddenRawReasoningText , bytes : hiddenRawReasoningBytes } = appendString (
@@ -1068,6 +1097,9 @@ export function bridgeToResponsesSSE(
10681097 // Redacted-only turns (or hidden thinking without a trailing signature event) still
10691098 // need their envelope-only reasoning item so the blocks replay next turn.
10701099 flushHiddenReasoningEnvelope ( ) ;
1100+ // After every close above, so the blob lands AFTER the assistant message it belongs
1101+ // to and the parser's backwards pairing finds it.
1102+ flushKiroRedactedReasoning ( ) ;
10711103 if ( options ?. compaction ) {
10721104 // Exactly one compaction item per turn; codex-rs takes the first and fatals on 0.
10731105 const item = {
@@ -1396,6 +1428,10 @@ function buildResponseJSONWithBudget(
13961428 let batchSignatureBytes = 0 ;
13971429 let batchRedacted : string [ ] = [ ] ;
13981430 let batchRedactedBytes = 0 ;
1431+ // Kiro reasoning blob, held until after the trailing flushes so it lands AFTER the assistant
1432+ // message (see the streaming path). Retained because it outlives releaseTranslatedEvent.
1433+ let batchKiroRedacted : string | undefined ;
1434+ let batchKiroRedactedBytes = 0 ;
13991435 let currentToolCallId = "" ;
14001436 let currentToolCallName = "" ;
14011437 let currentToolCallArgs = "" ;
@@ -1567,6 +1603,16 @@ function buildResponseJSONWithBudget(
15671603 }
15681604 batchRedacted . push ( e . data ) ;
15691605 break ;
1606+ case "kiro_redacted_reasoning" :
1607+ // Stash only — pushed after the trailing flushes. One blob per turn, so last wins.
1608+ {
1609+ const dataBytes = bytesOf ( e . data ) ;
1610+ budget ?. chargeRetained ( dataBytes , { kind : "reasoning" } ) ;
1611+ if ( batchKiroRedactedBytes > 0 ) budget ?. releaseRetained ( batchKiroRedactedBytes , { kind : "reasoning" } ) ;
1612+ batchKiroRedactedBytes = dataBytes ;
1613+ }
1614+ batchKiroRedacted = e . data ;
1615+ break ;
15701616 case "reasoning_raw_delta" :
15711617 if ( currentText ) flushText ( "commentary" ) ;
15721618 if ( currentSummaryReasoning ) flushSummaryReasoning ( ) ;
@@ -1668,6 +1714,15 @@ function buildResponseJSONWithBudget(
16681714 flushRawReasoning ( ) ;
16691715 // Open tool call on a failed/incomplete turn must not land as status:"completed".
16701716 if ( currentToolCallId ) flushToolCall ( errorEvent || incompleteEvent ? "incomplete" : "completed" ) ;
1717+ if ( batchKiroRedacted ) {
1718+ // pushOutput reserves the item itself and releases the retained raw blob it replaces.
1719+ pushOutput ( {
1720+ type : "reasoning" , id : `rs_${ uuid ( ) } ` , summary : [ ] ,
1721+ encrypted_content : encodeReasoningEnvelope ( { krc : batchKiroRedacted } ) ,
1722+ } , batchKiroRedactedBytes , "reasoning" ) ;
1723+ batchKiroRedacted = undefined ;
1724+ batchKiroRedactedBytes = 0 ;
1725+ }
16711726 // A truncated turn must never be installed as replacement history: emit the
16721727 // compaction item only when the turn actually completed (#422).
16731728 if (
0 commit comments