@@ -1438,14 +1438,18 @@ string AllocateSyntheticToolCallId()
14381438 {
14391439 if ( toolBlock . Kind == ChatToolContentKind . Call )
14401440 {
1441- _ = TryMatchCachedTool ( cachedTools , msg . Ts ) ;
14421441 var args = ConvertToolArgs ( toolBlock . Args ) ;
14431442 var callId = toolBlock . CallId ;
14441443 if ( string . IsNullOrWhiteSpace ( callId ) )
14451444 {
1445+ _ = TryMatchCachedTool ( cachedTools , msg . Ts ) ;
14461446 callId = AllocateSyntheticToolCallId ( ) ;
14471447 pendingUnkeyedToolCalls . Enqueue ( callId ) ;
14481448 }
1449+ else
1450+ {
1451+ _ = TryMatchCachedToolByCallId ( cachedTools , callId ) ;
1452+ }
14491453 rebuilt = ApplyAndCaptureMeta (
14501454 rebuilt ,
14511455 new ChatToolStartEvent (
@@ -1458,36 +1462,40 @@ string AllocateSyntheticToolCallId()
14581462 else
14591463 {
14601464 var callId = toolBlock . CallId ;
1461- if ( string . IsNullOrWhiteSpace ( callId ) )
1465+ var hasVerifiedCallId = ! string . IsNullOrWhiteSpace ( callId ) ;
1466+ if ( ! hasVerifiedCallId )
14621467 {
14631468 callId = pendingUnkeyedToolCalls . Count > 0
14641469 ? pendingUnkeyedToolCalls . Dequeue ( )
14651470 : AllocateSyntheticToolCallId ( ) ;
14661471 }
1472+ var resolvedCallId = callId ! ;
14671473
14681474 var correlationKey = new ChatToolCorrelationKey (
14691475 RunId : null ,
14701476 LegacyTurn : rebuilt . ToolLegacyTurn ,
1471- ToolCallId : callId ) ;
1477+ ToolCallId : resolvedCallId ) ;
14721478 if ( ! rebuilt . ActiveToolCalls . ContainsKey ( correlationKey ) )
14731479 {
1474- var cached = TryMatchCachedTool ( cachedTools , msg . Ts ) ;
1480+ var cached = hasVerifiedCallId
1481+ ? TryMatchCachedToolByCallId ( cachedTools , resolvedCallId )
1482+ : TryMatchCachedTool ( cachedTools , msg . Ts ) ;
14751483 var toolName = cached ? . ToolName ?? toolBlock . ToolName ;
14761484 rebuilt = ApplyAndCaptureMeta (
14771485 rebuilt ,
14781486 new ChatToolStartEvent (
14791487 cached ? . Label ?? toolName ,
14801488 toolName ,
1481- ToolCallId : callId ) ,
1489+ ToolCallId : resolvedCallId ) ,
14821490 msgMeta ) ;
14831491 }
14841492
14851493 var output = NativeToolProjector . TruncateToolOutput ( toolBlock . Text ?? string . Empty ) ;
14861494 rebuilt = ApplyAndCaptureMeta (
14871495 rebuilt ,
14881496 toolBlock . IsError
1489- ? new ChatToolErrorEvent ( output , callId )
1490- : new ChatToolOutputEvent ( output , callId ) ,
1497+ ? new ChatToolErrorEvent ( output , resolvedCallId )
1498+ : new ChatToolOutputEvent ( output , resolvedCallId ) ,
14911499 msgMeta ) ;
14921500 }
14931501 }
@@ -7332,16 +7340,37 @@ private static void MergeCachedToolMeta(CachedToolMeta existing, CachedToolMeta
73327340 if ( historyTsMs > 0 && candidate . Ts > 0 && candidate . Ts > historyTsMs + 300_000 )
73337341 return null ; // cached entry is >5 min after this history entry — not a match
73347342
7335- var match = cache . Dequeue ( ) ;
7336- var identity = NativeToolProjector . CanonicalizeToolIdentity (
7337- NormalizeCachedDisplayText ( match . ToolName ) ,
7338- match . IdentityStrength ) ;
7339- match . ToolName = identity . Name ;
7340- match . IdentityStrength = identity . Strength ;
7341- match . Label = NativeToolProjector . SanitizeToolDisplayValue (
7342- NormalizeCachedDisplayText ( match . Label ) ) ;
7343- match . ToolArgs = NormalizeCachedToolArgs ( match . ToolArgs ) ;
7344- return match ;
7343+ return CloneCachedToolMeta ( cache . Dequeue ( ) ) ;
7344+ }
7345+
7346+ /// <summary>
7347+ /// Consume the earliest cached entry with the exact structured call ID while
7348+ /// preserving the relative order of every unmatched entry.
7349+ /// </summary>
7350+ internal static CachedToolMeta ? TryMatchCachedToolByCallId (
7351+ Queue < CachedToolMeta > ? cache ,
7352+ string ? toolCallId )
7353+ {
7354+ if ( cache is null || cache . Count == 0 || string . IsNullOrWhiteSpace ( toolCallId ) )
7355+ return null ;
7356+
7357+ CachedToolMeta ? match = null ;
7358+ var entryCount = cache . Count ;
7359+ for ( var index = 0 ; index < entryCount ; index ++ )
7360+ {
7361+ var candidate = cache . Dequeue ( ) ;
7362+ if ( match is null
7363+ && ! string . IsNullOrWhiteSpace ( candidate . ToolCallId )
7364+ && string . Equals ( candidate . ToolCallId , toolCallId , StringComparison . Ordinal ) )
7365+ {
7366+ match = candidate ;
7367+ continue ;
7368+ }
7369+
7370+ cache . Enqueue ( candidate ) ;
7371+ }
7372+
7373+ return match is null ? null : CloneCachedToolMeta ( match ) ;
73457374 }
73467375
73477376 private static JsonObject ? NormalizeCachedToolArgs ( JsonObject ? args )
0 commit comments