@@ -228,6 +228,20 @@ export function parseRequest(body: unknown): OcxParsedRequest {
228228 const now = Date . now ( ) ;
229229 const messages : OcxMessage [ ] = [ ] ;
230230 const systemPrompt : string [ ] = [ ] ;
231+ // Responses reasoning siblings belong to the following assistant, including across call items.
232+ // Keep them off the message list until that assistant arrives; turn boundaries clear the array.
233+ const pendingReasoning : Array < { part : OcxThinkingContent ; envelopeSigned : boolean } > = [ ] ;
234+ // Assistant placeholder that first folds any pending reasoning into the same turn (official
235+ // grok-build preserves reasoning across call items; Anthropic replay requires thinking to
236+ // precede tool_use inside one assistant message).
237+ const assistantHolderWithReasoning = ( ) : OcxAssistantMessage => {
238+ const holder = ensureAssistantPlaceholder ( messages , data . model , now ) ;
239+ if ( pendingReasoning . length > 0 ) {
240+ holder . content . push ( ...pendingReasoning . map ( entry => entry . part ) ) ;
241+ pendingReasoning . length = 0 ;
242+ }
243+ return holder ;
244+ } ;
231245 // Tool specs surfaced by a prior tool_search (deferred tools, e.g. subagents). Codex does not
232246 // re-list these in `tools`, but chat models can only call listed tools — so we re-inject them.
233247 const loadedToolSpecs : unknown [ ] = [ ] ;
@@ -270,6 +284,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
270284 // is dropped silently. It must NOT flag _compactionRequest.
271285 const encrypted = ( item as { encrypted_content ?: unknown } ) . encrypted_content ;
272286 if ( effectiveType === "context_compaction" && typeof encrypted !== "string" ) continue ;
287+ pendingReasoning . length = 0 ;
273288 messages . push ( {
274289 role : "user" ,
275290 content : compactionItemToText ( typeof encrypted === "string" ? encrypted : undefined ) ,
@@ -297,6 +312,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
297312 // An agent_message is external input delivered to the parent agent.
298313 // Preserve it as a user-role turn so signed Anthropic thinking blocks
299314 // on either side are never merged into one modified assistant response.
315+ pendingReasoning . length = 0 ;
300316 messages . push ( {
301317 role : "user" ,
302318 content : hasContent ? content : "(sub-agent message received)" ,
@@ -310,20 +326,30 @@ export function parseRequest(body: unknown): OcxParsedRequest {
310326 const msg = item as { role ?: string ; content ?: unknown } ;
311327 switch ( msg . role ) {
312328 case "system" : {
329+ pendingReasoning . length = 0 ;
313330 const text = inputContentParts ( msg . content as unknown [ ] | string | undefined ) ;
314331 const flat = typeof text === "string" ? text : text . map ( p => ( p . type === "text" ? p . text : "" ) ) . join ( "" ) ;
315332 if ( flat . length > 0 ) systemPrompt . push ( flat ) ;
316333 break ;
317334 }
318335 case "user" :
319336 case "developer" : {
337+ pendingReasoning . length = 0 ;
320338 const content = inputContentParts ( msg . content as unknown [ ] | string | undefined ) ;
321339 messages . push ( { role : msg . role , content, timestamp : now } ) ;
322340 break ;
323341 }
324342 case "assistant" : {
325343 const parts = outputTextOf ( msg . content as unknown [ ] | string | undefined ) ;
326- messages . push ( { role : "assistant" , content : parts , model : data . model , timestamp : now } ) ;
344+ messages . push ( {
345+ role : "assistant" ,
346+ content : pendingReasoning . length > 0
347+ ? [ ...pendingReasoning . map ( entry => entry . part ) , ...parts ]
348+ : parts ,
349+ model : data . model ,
350+ timestamp : now ,
351+ } ) ;
352+ pendingReasoning . length = 0 ;
327353 break ;
328354 }
329355 }
@@ -334,20 +360,33 @@ export function parseRequest(body: unknown): OcxParsedRequest {
334360 const reasoning = item as { id ?: string ; summary ?: { text : string } [ ] ; content ?: { text : string } [ ] ; encrypted_content ?: string } ;
335361 const fromSummary = ( reasoning . summary ?? [ ] ) . map ( c => c . text ) . join ( "" ) ;
336362 const text = fromSummary || ( reasoning . content ?? [ ] ) . map ( c => c . text ) . join ( "" ) ;
337- // ocxr1 envelope: the REAL Anthropic signature (+ redacted blocks, + hidden signed text)
338- // captured by the bridge. Native OpenAI-encrypted blobs decode to null and keep today's
339- // placeholder signature (which the anthropic adapter correctly rejects on replay).
340363 const envelope = typeof reasoning . encrypted_content === "string"
341364 ? decodeReasoningEnvelope ( reasoning . encrypted_content )
342365 : null ;
343- const thinking : OcxThinkingContent = {
344- type : "thinking" ,
345- thinking : envelope ?. txt || text ,
346- signature : envelope ?. sig ?? JSON . stringify ( reasoning ) ,
347- ...( envelope ?. red ? { redacted : envelope . red } : { } ) ,
348- ...( reasoning . id ? { itemId : reasoning . id } : { } ) ,
349- } ;
350- ensureAssistantPlaceholder ( messages , data . model , now ) . content . push ( thinking ) ;
366+ const thinkingText = envelope ?. txt || text ;
367+
368+ // Native/non-ocxr1 encrypted-only reasoning is opaque here. Do not create a detached
369+ // assistant turn or invent replayable plaintext/signatures from the encrypted payload.
370+ if ( thinkingText . length > 0 ) {
371+ const part : OcxThinkingContent = {
372+ type : "thinking" ,
373+ thinking : thinkingText ,
374+ signature : envelope ?. sig ?? JSON . stringify ( reasoning ) ,
375+ ...( envelope ?. red ? { redacted : envelope . red } : { } ) ,
376+ ...( reasoning . id ? { itemId : reasoning . id } : { } ) ,
377+ } ;
378+ const envelopeSigned = typeof envelope ?. sig === "string" ;
379+ const previous = pendingReasoning [ pendingReasoning . length - 1 ] ;
380+
381+ if ( ! envelopeSigned && previous && ! previous . envelopeSigned ) {
382+ previous . part = {
383+ ...part ,
384+ thinking : `${ previous . part . thinking } \n${ part . thinking } ` ,
385+ } ;
386+ } else {
387+ pendingReasoning . push ( { part, envelopeSigned } ) ;
388+ }
389+ }
351390 continue ;
352391 }
353392
@@ -370,7 +409,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
370409 ...( call . id ? { thoughtSignature : call . id } : { } ) ,
371410 ...( call . namespace ? { namespace : call . namespace } : { } ) ,
372411 } ;
373- ensureAssistantPlaceholder ( messages , data . model , now ) . content . push ( toolCall ) ;
412+ assistantHolderWithReasoning ( ) . content . push ( toolCall ) ;
374413 continue ;
375414 }
376415
@@ -382,7 +421,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
382421 customWireName : call . name ,
383422 ...( call . id ? { thoughtSignature : call . id } : { } ) ,
384423 } ;
385- ensureAssistantPlaceholder ( messages , data . model , now ) . content . push ( toolCall ) ;
424+ assistantHolderWithReasoning ( ) . content . push ( toolCall ) ;
386425 continue ;
387426 }
388427
@@ -393,7 +432,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
393432 const callId = call . call_id ?? call . id ;
394433 if ( callId ) {
395434 const command = Array . isArray ( call . action ?. command ) ? call . action . command : [ ] ;
396- ensureAssistantPlaceholder ( messages , data . model , now ) . content . push ( {
435+ assistantHolderWithReasoning ( ) . content . push ( {
397436 type : "toolCall" , id : callId , name : "shell" ,
398437 arguments : command . length > 0 ? { command } : { } ,
399438 } ) ;
@@ -406,7 +445,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
406445 // knows the search already ran (prevents re-search loops); there is no output to pair.
407446 const call = item as { action ?: { type ?: string ; query ?: string } } ;
408447 const query = typeof call . action ?. query === "string" ? call . action . query : "" ;
409- ensureAssistantPlaceholder ( messages , data . model , now ) . content . push ( {
448+ assistantHolderWithReasoning ( ) . content . push ( {
410449 type : "text" , text : query ? `[web search performed: ${ query } ]` : "[web search performed]" ,
411450 } ) ;
412451 continue ;
@@ -417,14 +456,15 @@ export function parseRequest(body: unknown): OcxParsedRequest {
417456 // history stays complete (otherwise the model re-issues tool_search forever).
418457 const call = item as { id ?: string ; call_id ?: string ; arguments ?: unknown } ;
419458 const callId = call . call_id ?? call . id ?? "" ;
420- ensureAssistantPlaceholder ( messages , data . model , now ) . content . push ( {
459+ assistantHolderWithReasoning ( ) . content . push ( {
421460 type : "toolCall" , id : callId , name : "tool_search" ,
422461 arguments : isObj ( call . arguments ) ? call . arguments : { } ,
423462 } ) ;
424463 continue ;
425464 }
426465
427466 if ( effectiveType === "tool_search_output" ) {
467+ pendingReasoning . length = 0 ;
428468 // Pair the tool_search call with its result so the model sees what was loaded.
429469 const out = item as { call_id ?: string ; status ?: string ; tools ?: unknown [ ] } ;
430470 const specs = Array . isArray ( out . tools ) ? ( out . tools as Record < string , unknown > [ ] ) : [ ] ;
@@ -455,6 +495,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
455495 }
456496
457497 if ( effectiveType === "function_call_output" ) {
498+ pendingReasoning . length = 0 ;
458499 const output = item as { call_id : string ; output ?: string | unknown [ ] } ;
459500 const toolInfo = findToolById ( messages , output . call_id ) ;
460501 messages . push ( {
@@ -466,6 +507,7 @@ export function parseRequest(body: unknown): OcxParsedRequest {
466507 }
467508
468509 if ( effectiveType === "custom_tool_call_output" ) {
510+ pendingReasoning . length = 0 ;
469511 const output = item as { call_id : string ; output : string | unknown [ ] } ;
470512 const toolInfo = findToolById ( messages , output . call_id ) ;
471513 messages . push ( {
0 commit comments