@@ -55,7 +55,7 @@ public static ChatTimelineState Apply(ChatTimelineState state, ChatEvent evt)
5555 ChatReasoningEndEvent => state . ActiveReasoningId is null ? state : state with { ActiveReasoningId = null } ,
5656 ChatMessageDeltaEvent e => UpsertAssistant ( BeginTurn ( state ) , e . Text , replace : false , streaming : true ) ,
5757 ChatMessageEvent e => UpsertAssistant ( BeginTurn ( state ) , e . Text , replace : true , streaming : e . IsStreaming , e . ReconcilePrevious ) ,
58- ChatTurnEndEvent => ApplyTurnEnd ( state ) ,
58+ ChatTurnEndEvent e => ApplyTurnEnd ( state , e . RetainToolCorrelations ) ,
5959 ChatIntentEvent e => state with { CurrentIntent = e . Intent } ,
6060 ChatToolStartEvent e => ApplyToolStart ( state , e ) ,
6161 ChatToolPresentationEvent e => ApplyToolPresentation ( state , e ) ,
@@ -372,7 +372,12 @@ static ChatTimelineState ApplyToolStart(ChatTimelineState state, ChatToolStartEv
372372 correlationId ) ;
373373 if ( ! pendingOutcomes . TryGetValue ( outcomeKey , out var outcome ) )
374374 continue ;
375- entry = ApplyToolOutcome ( entry , outcome ) ;
375+ entry = ApplyToolOutcome (
376+ entry ,
377+ outcome ,
378+ state . TerminalToolCorrelations ? . ContainsKey ( outcomeKey ) == true
379+ ? outcomeKey . RunId
380+ : null ) ;
376381 pendingOutcomes = pendingOutcomes . Remove ( outcomeKey ) ;
377382 if ( ! isTerminalReplay )
378383 terminalCorrelations = terminalCorrelations . Remove ( outcomeKey ) ;
@@ -529,7 +534,12 @@ existing with
529534 var outcomeKey = CorrelationKey ( entry . ToolRunId , entry . ToolLegacyTurn , correlationId ) ;
530535 if ( ! pendingOutcomes . TryGetValue ( outcomeKey , out var outcome ) )
531536 continue ;
532- entry = ApplyToolOutcome ( entry , outcome ) ;
537+ entry = ApplyToolOutcome (
538+ entry ,
539+ outcome ,
540+ state . TerminalToolCorrelations ? . ContainsKey ( outcomeKey ) == true
541+ ? outcomeKey . RunId
542+ : null ) ;
533543 pendingOutcomes = pendingOutcomes . Remove ( outcomeKey ) ;
534544 }
535545
@@ -547,26 +557,32 @@ static ChatTimelineState ApplyToolOutput(ChatTimelineState state, ChatToolOutput
547557 ApplyToolOutcome ( state , e . Text , e . ToolCallId , e . RunId , ChatToolCallStatus . Success ) ;
548558
549559 static ChatTimelineState ApplyToolError ( ChatTimelineState state , ChatToolErrorEvent e ) =>
550- ApplyToolOutcome ( state , e . Text , e . ToolCallId , e . RunId , ChatToolCallStatus . Error ) ;
560+ ApplyToolOutcome (
561+ state , e . Text , e . ToolCallId , e . RunId , ChatToolCallStatus . Error , e . ErrorTextQuality ) ;
551562
552563 static ChatTimelineState ApplyToolOutcome (
553564 ChatTimelineState state ,
554565 string text ,
555566 string ? toolCallId ,
556567 string ? runId ,
557- ChatToolCallStatus status )
568+ ChatToolCallStatus status ,
569+ ChatToolErrorTextQuality errorTextQuality = ChatToolErrorTextQuality . Unspecified )
558570 {
559571 var sequence = state . NextToolOutcomeSequence ;
560- var incoming = new ChatPendingToolOutcome ( text , status , sequence ) ;
572+ var incoming = new ChatPendingToolOutcome ( text , status , sequence , errorTextQuality ) ;
561573 var key = toolCallId is { Length : > 0 }
562574 ? ResolveBufferedCorrelationKey ( state , runId , toolCallId )
563575 : default ( ChatToolCorrelationKey ? ) ;
576+ var recoveryRunId = key is { } recoveryKey
577+ && state . TerminalToolCorrelations ? . ContainsKey ( recoveryKey ) == true
578+ ? runId
579+ : null ;
564580 var ( entries , entryId ) = ResolveToolEntry ( state , key ) ;
565581 if ( entryId is { } tid )
566582 {
567583 var idx = entries . FindIndex ( en => en . Id == tid ) ;
568584 if ( idx >= 0 )
569- entries = entries . SetItem ( idx , ApplyToolOutcome ( entries [ idx ] , incoming ) ) ;
585+ entries = entries . SetItem ( idx , ApplyToolOutcome ( entries [ idx ] , incoming , recoveryRunId ) ) ;
570586 }
571587
572588 var pendingOutcomes = state . PendingToolOutcomes
@@ -604,15 +620,41 @@ static ChatTimelineState ApplyToolOutcome(
604620
605621 static ChatTimelineItem ApplyToolOutcome (
606622 ChatTimelineItem entry ,
607- ChatPendingToolOutcome incoming )
623+ ChatPendingToolOutcome incoming ,
624+ string ? incomingRunId = null )
608625 {
609626 var currentStatus = entry . ToolResult ?? ChatToolCallStatus . InProgress ;
610627 var currentPrecedence = ToolOutcomePrecedence ( currentStatus ) ;
611628 var incomingPrecedence = ToolOutcomePrecedence ( incoming . Status ) ;
629+ var currentErrorQuality = currentStatus == ChatToolCallStatus . Error
630+ ? entry . ToolErrorTextQuality
631+ : ChatToolErrorTextQuality . Unspecified ;
632+ var incomingErrorQuality = incoming . Status == ChatToolCallStatus . Error
633+ ? incoming . ErrorTextQuality
634+ : ChatToolErrorTextQuality . Unspecified ;
612635 if ( incomingPrecedence < currentPrecedence
613636 || ( incomingPrecedence == currentPrecedence
614- && incoming . Sequence < entry . ToolOutcomeSequence ) )
637+ && ( incomingErrorQuality < currentErrorQuality
638+ || ( incomingErrorQuality == currentErrorQuality
639+ && incoming . Sequence < entry . ToolOutcomeSequence ) ) ) )
615640 {
641+ if ( currentStatus == ChatToolCallStatus . Interrupted
642+ && incoming . Status == ChatToolCallStatus . Success
643+ && ! string . IsNullOrWhiteSpace ( incomingRunId )
644+ && string . Equals ( incomingRunId , entry . ToolRunId , StringComparison . Ordinal )
645+ && incoming . Sequence >= entry . ToolOutcomeSequence )
646+ {
647+ return entry with
648+ {
649+ ToolResult = ChatToolCallStatus . Success ,
650+ ToolOutput = string . IsNullOrEmpty ( incoming . Text ) && entry . ToolOutput is not null
651+ ? entry . ToolOutput
652+ : incoming . Text ,
653+ ToolOutcomeSequence = incoming . Sequence ,
654+ ToolErrorTextQuality = incomingErrorQuality
655+ } ;
656+ }
657+
616658 if ( currentStatus == ChatToolCallStatus . Interrupted
617659 && incoming . Sequence >= entry . ToolOutcomeSequence
618660 && ! string . IsNullOrEmpty ( incoming . Text ) )
@@ -632,7 +674,8 @@ static ChatTimelineItem ApplyToolOutcome(
632674 ToolOutput = string . IsNullOrEmpty ( incoming . Text ) && entry . ToolOutput is not null
633675 ? entry . ToolOutput
634676 : incoming . Text ,
635- ToolOutcomeSequence = incoming . Sequence
677+ ToolOutcomeSequence = incoming . Sequence ,
678+ ToolErrorTextQuality = incomingErrorQuality
636679 } ;
637680 }
638681
@@ -644,6 +687,9 @@ static ChatPendingToolOutcome SelectToolOutcome(
644687 var incomingPrecedence = ToolOutcomePrecedence ( incoming . Status ) ;
645688 if ( incomingPrecedence != existingPrecedence )
646689 return incomingPrecedence > existingPrecedence ? incoming : existing ;
690+ if ( incoming . Status == ChatToolCallStatus . Error
691+ && incoming . ErrorTextQuality != existing . ErrorTextQuality )
692+ return incoming . ErrorTextQuality > existing . ErrorTextQuality ? incoming : existing ;
647693 return incoming . Sequence >= existing . Sequence ? incoming : existing ;
648694 }
649695
@@ -1308,7 +1354,9 @@ static ChatTimelineState PushEntry(ChatTimelineState state, ChatTimelineItemKind
13081354 } ;
13091355 }
13101356
1311- static ChatTimelineState ApplyTurnEnd ( ChatTimelineState state )
1357+ static ChatTimelineState ApplyTurnEnd (
1358+ ChatTimelineState state ,
1359+ bool retainToolCorrelations = true )
13121360 {
13131361 var entries = state . Entries ;
13141362
@@ -1390,23 +1438,30 @@ static ChatTimelineState ApplyTurnEnd(ChatTimelineState state)
13901438
13911439 var terminalCorrelations = state . TerminalToolCorrelations
13921440 ?? System . Collections . Immutable . ImmutableDictionary < ChatToolCorrelationKey , long > . Empty ;
1393- foreach ( var key in state . ActiveToolCalls . Keys )
1394- terminalCorrelations = terminalCorrelations . SetItem ( key , state . ToolLegacyTurn ) ;
13951441
13961442 var pendingPresentations = state . PendingToolPresentations
13971443 ?? System . Collections . Immutable . ImmutableDictionary < ChatToolCorrelationKey , ChatPendingToolPresentation > . Empty ;
1398- foreach ( var pair in pendingPresentations )
1399- {
1400- if ( ! terminalCorrelations . ContainsKey ( pair . Key ) )
1401- terminalCorrelations = terminalCorrelations . Add ( pair . Key , state . ToolLegacyTurn ) ;
1402- }
1403-
14041444 var pendingOutcomes = state . PendingToolOutcomes
14051445 ?? System . Collections . Immutable . ImmutableDictionary < ChatToolCorrelationKey , ChatPendingToolOutcome > . Empty ;
1406- foreach ( var key in pendingOutcomes . Keys )
1446+ if ( retainToolCorrelations )
1447+ {
1448+ foreach ( var key in state . ActiveToolCalls . Keys )
1449+ terminalCorrelations = terminalCorrelations . SetItem ( key , state . ToolLegacyTurn ) ;
1450+ foreach ( var pair in pendingPresentations )
1451+ {
1452+ if ( ! terminalCorrelations . ContainsKey ( pair . Key ) )
1453+ terminalCorrelations = terminalCorrelations . Add ( pair . Key , state . ToolLegacyTurn ) ;
1454+ }
1455+ foreach ( var key in pendingOutcomes . Keys )
1456+ {
1457+ if ( ! terminalCorrelations . ContainsKey ( key ) )
1458+ terminalCorrelations = terminalCorrelations . Add ( key , state . ToolLegacyTurn ) ;
1459+ }
1460+ }
1461+ else
14071462 {
1408- if ( ! terminalCorrelations . ContainsKey ( key ) )
1409- terminalCorrelations = terminalCorrelations . Add ( key , state . ToolLegacyTurn ) ;
1463+ foreach ( var key in state . ActiveToolCalls . Keys )
1464+ terminalCorrelations = terminalCorrelations . Remove ( key ) ;
14101465 }
14111466
14121467 var oldestRetainedTurn = Math . Max ( 0 , state . ToolLegacyTurn - MaxRetainedToolTurns + 1 ) ;
0 commit comments