@@ -349,9 +349,16 @@ export type ToolUseRepairReport = {
349349 changed : boolean ;
350350} ;
351351
352+ function shouldPreserveErroredAssistantResults ( options ?: ToolUseResultPairingOptions ) : boolean {
353+ return (
354+ options ?. erroredAssistantResultPolicy !== undefined &&
355+ options . erroredAssistantResultPolicy !== "drop"
356+ ) ;
357+ }
358+
352359export function repairToolUseResultPairing (
353360 messages : AgentMessage [ ] ,
354- _options ?: ToolUseResultPairingOptions ,
361+ options ?: ToolUseResultPairingOptions ,
355362) : ToolUseRepairReport {
356363 // Anthropic (and Cloud Code Assist) reject transcripts where assistant tool calls are not
357364 // immediately followed by matching tool results. Session files can end up with results
@@ -403,6 +410,93 @@ export function repairToolUseResultPairing(
403410
404411 const assistant = msg as Extract < AgentMessage , { role : "assistant" } > ;
405412
413+ // Check for error/aborted stop reason early, before extractToolCallsFromAssistant,
414+ // because that function skips blocks without a non-empty id. For error/aborted
415+ // messages we need to strip ALL tool-like blocks regardless of id presence.
416+ const stopReason = ( assistant as { stopReason ?: string } ) . stopReason ;
417+ if ( stopReason === "error" || stopReason === "aborted" ) {
418+ // If the caller explicitly set erroredAssistantResultPolicy to "preserve",
419+ // fall back to the old behavior: push the message through unchanged and
420+ // optionally retain matching tool results for surviving tool calls.
421+ if ( shouldPreserveErroredAssistantResults ( options ) ) {
422+ const toolCalls = extractToolCallsFromAssistant ( assistant ) ;
423+ out . push ( msg ) ;
424+ if ( toolCalls . length > 0 ) {
425+ const toolCallIds = new Set ( toolCalls . map ( ( t ) => t . id ) ) ;
426+ const toolCallNamesById = new Map ( toolCalls . map ( ( t ) => [ t . id , t . name ] as const ) ) ;
427+ let j = i + 1 ;
428+ for ( ; j < messages . length ; j += 1 ) {
429+ const next = messages [ j ] ;
430+ if ( ! next || typeof next !== "object" ) {
431+ continue ;
432+ }
433+ const nextRole = ( next as { role ?: unknown } ) . role ;
434+ if ( nextRole === "assistant" ) {
435+ break ;
436+ }
437+ if ( nextRole === "toolResult" ) {
438+ const toolResult = next as Extract < AgentMessage , { role : "toolResult" } > ;
439+ const id = extractToolResultId ( toolResult ) ;
440+ if ( id && toolCallIds . has ( id ) ) {
441+ const normalizedToolResult = normalizeToolResultName (
442+ toolResult ,
443+ toolCallNamesById . get ( id ) ,
444+ ) ;
445+ pushToolResult ( normalizedToolResult ) ;
446+ }
447+ }
448+ }
449+ i = j - 1 ;
450+ }
451+ continue ;
452+ }
453+
454+ // Default behavior: strip orphaned tool_use/toolCall/functionCall blocks from
455+ // the assistant message instead of passing them through unchanged.
456+ // Previously we skipped synthesis to avoid creating synthetic tool_results for
457+ // incomplete tool calls (#4597), but preserving orphaned blocks causes permanent
458+ // session corruption — Anthropic rejects every subsequent request with
459+ // "tool_use ids were found without tool_result blocks" (#48354).
460+ const content = Array . isArray ( assistant . content ) ? assistant . content : [ ] ;
461+ const stripped = content . filter (
462+ ( b : { type ?: string } ) =>
463+ b && b . type !== "toolCall" && b . type !== "toolUse" && b . type !== "functionCall" ,
464+ ) ;
465+ if ( stripped . length < content . length ) {
466+ changed = true ;
467+ out . push ( {
468+ ...assistant ,
469+ content :
470+ stripped . length > 0
471+ ? stripped
472+ : [ { type : "text" as const , text : "[tool calls from errored turn stripped]" } ] ,
473+ } as typeof assistant ) ;
474+ } else {
475+ out . push ( msg ) ;
476+ }
477+ // Skip past any trailing messages up to the next assistant turn
478+ let j = i + 1 ;
479+ for ( ; j < messages . length ; j += 1 ) {
480+ const next = messages [ j ] ;
481+ if ( ! next || typeof next !== "object" ) {
482+ out . push ( next ) ;
483+ continue ;
484+ }
485+ const nextRole = ( next as { role ?: unknown } ) . role ;
486+ if ( nextRole === "assistant" ) {
487+ break ;
488+ }
489+ if ( nextRole !== "toolResult" ) {
490+ out . push ( next ) ;
491+ } else {
492+ // Drop orphaned tool results for the stripped tool calls
493+ changed = true ;
494+ }
495+ }
496+ i = j - 1 ;
497+ continue ;
498+ }
499+
406500 const toolCalls = extractToolCallsFromAssistant ( assistant ) ;
407501 if ( toolCalls . length === 0 ) {
408502 out . push ( msg ) ;
@@ -460,38 +554,6 @@ export function repairToolUseResultPairing(
460554 }
461555 }
462556
463- // Aborted/errored assistant turns: strip orphaned tool_use/toolCall/functionCall
464- // blocks from the assistant message instead of passing them through unchanged.
465- // Previously we skipped synthesis to avoid creating synthetic tool_results for
466- // incomplete tool calls (#4597), but preserving orphaned blocks causes permanent
467- // session corruption — Anthropic rejects every subsequent request with
468- // "tool_use ids were found without tool_result blocks" (#48354).
469- const stopReason = ( assistant as { stopReason ?: string } ) . stopReason ;
470- if ( stopReason === "error" || stopReason === "aborted" ) {
471- const content = Array . isArray ( assistant . content ) ? assistant . content : [ ] ;
472- const stripped = content . filter (
473- ( b : { type ?: string } ) =>
474- b && b . type !== "toolCall" && b . type !== "toolUse" && b . type !== "functionCall" ,
475- ) ;
476- if ( stripped . length < content . length ) {
477- changed = true ;
478- out . push ( {
479- ...assistant ,
480- content :
481- stripped . length > 0
482- ? stripped
483- : [ { type : "text" as const , text : "[tool calls from errored turn stripped]" } ] ,
484- } as typeof assistant ) ;
485- } else {
486- out . push ( msg ) ;
487- }
488- for ( const rem of remainder ) {
489- out . push ( rem ) ;
490- }
491- i = j - 1 ;
492- continue ;
493- }
494-
495557 out . push ( msg ) ;
496558
497559 if ( spanResultsById . size > 0 && remainder . length > 0 ) {
0 commit comments