@@ -356,7 +356,7 @@ export function truncateOversizedToolResultsInMessages(
356356 id : `message-${ index } ` ,
357357 type : "message" ,
358358 message : ( projectionKey && projectionState ?. replacements . get ( projectionKey ) ) ?? message ,
359- aggregateEligible : ! projectionKey || ! projectionState ?. replacements . has ( projectionKey ) ,
359+ aggregateEligible : ! projectionKey || ! projectionState ?. frozen . has ( projectionKey ) ,
360360 } ;
361361 } ) ;
362362 const plan = buildToolResultReplacementPlan ( {
@@ -365,6 +365,14 @@ export function truncateOversizedToolResultsInMessages(
365365 aggregateBudgetChars,
366366 minKeepChars : RECOVERY_MIN_KEEP_CHARS ,
367367 } ) ;
368+ if ( projectionState ) {
369+ for ( const message of messages ) {
370+ const projectionKey = getToolResultProjectionKey ( message ) ;
371+ if ( projectionKey ) {
372+ projectionState . frozen . add ( projectionKey ) ;
373+ }
374+ }
375+ }
368376 if ( plan . replacements . length === 0 ) {
369377 const projectedMessages = branch . map ( ( entry ) => entry . message as AgentMessage ) ;
370378 const hasProjectedChanges = projectedMessages . some (
@@ -382,8 +390,11 @@ export function truncateOversizedToolResultsInMessages(
382390 for ( const [ index , originalMessage ] of messages . entries ( ) ) {
383391 const projectedMessage = replacedBranch [ index ] ?. message ;
384392 const projectionKey = getToolResultProjectionKey ( originalMessage ) ;
385- if ( projectionKey && projectedMessage && projectedMessage !== originalMessage ) {
386- projectionState . replacements . set ( projectionKey , projectedMessage ) ;
393+ if ( projectionKey ) {
394+ projectionState . frozen . add ( projectionKey ) ;
395+ if ( projectedMessage && projectedMessage !== originalMessage ) {
396+ projectionState . replacements . set ( projectionKey , projectedMessage ) ;
397+ }
387398 }
388399 }
389400 }
@@ -431,21 +442,23 @@ type ToolResultReplacement = {
431442
432443export type ToolResultPromptProjectionState = {
433444 replacements : Map < string , AgentMessage > ;
445+ frozen : Set < string > ;
434446} ;
435447
436448export function createToolResultPromptProjectionState ( ) : ToolResultPromptProjectionState {
437- return { replacements : new Map < string , AgentMessage > ( ) } ;
449+ return { replacements : new Map < string , AgentMessage > ( ) , frozen : new Set < string > ( ) } ;
438450}
439451
440452function getToolResultProjectionKey ( message : AgentMessage ) : string | undefined {
441453 if ( message . role !== "toolResult" ) {
442454 return undefined ;
443455 }
444456 const toolCallId = ( message as { toolCallId ?: unknown } ) . toolCallId ;
457+ const timestamp = ( message as { timestamp ?: unknown } ) . timestamp ;
458+ const timestampKey = typeof timestamp === "number" ? `:${ timestamp } ` : "" ;
445459 if ( typeof toolCallId === "string" && toolCallId . length > 0 ) {
446- return `tool:${ toolCallId } ` ;
460+ return `tool:${ toolCallId } ${ timestampKey } ` ;
447461 }
448- const timestamp = ( message as { timestamp ?: unknown } ) . timestamp ;
449462 return typeof timestamp === "number" ? `timestamp:${ timestamp } ` : undefined ;
450463}
451464
0 commit comments