@@ -493,7 +493,7 @@ describe("truncateOversizedToolResultsInMessages", () => {
493493 ) ;
494494 } ) ;
495495
496- it ( "keeps prompt projections byte- stable as history grows" , ( ) => {
496+ it ( "keeps prompt projections stable while enforcing aggregate recovery as history grows" , ( ) => {
497497 const prefix = [
498498 makeToolResult ( "p" . repeat ( 15_000 ) , "prefix_1" ) ,
499499 makeToolResult ( "q" . repeat ( 15_000 ) , "prefix_2" ) ,
@@ -521,8 +521,14 @@ describe("truncateOversizedToolResultsInMessages", () => {
521521 ) ;
522522
523523 expect ( first . truncatedCount ) . toBe ( 4 ) ;
524- expect ( second . truncatedCount ) . toBe ( 1 ) ;
525- expect ( second . messages . slice ( 0 , messages . length ) ) . toEqual ( first . messages ) ;
524+ expect ( second . truncatedCount ) . toBe ( 2 ) ;
525+ expect (
526+ second . messages . reduce (
527+ ( sum , message ) =>
528+ sum + ( message . role === "toolResult" ? getToolResultTextLength ( message ) : 0 ) ,
529+ 0 ,
530+ ) ,
531+ ) . toBeLessThanOrEqual ( 12_000 ) ;
526532 expect ( second . messages . every ( ( message ) => getToolResultTextLength ( message ) <= 12_000 ) ) . toBe (
527533 true ,
528534 ) ;
@@ -541,7 +547,7 @@ describe("truncateOversizedToolResultsInMessages", () => {
541547 stableState ,
542548 ) ;
543549 const stableSecond = truncateOversizedToolResultsInMessages (
544- [ ...stableHistory , makeToolResult ( "c" . repeat ( 15_000 ) , "stable_3" ) ] ,
550+ [ ...stableHistory , makeToolResult ( "c" . repeat ( 3_000 ) , "stable_3" ) ] ,
545551 128_000 ,
546552 12_000 ,
547553 12_000 ,
@@ -552,7 +558,7 @@ describe("truncateOversizedToolResultsInMessages", () => {
552558 const stableThird = truncateOversizedToolResultsInMessages (
553559 [
554560 ...stableHistory ,
555- makeToolResult ( "c" . repeat ( 15_000 ) , "stable_3" ) ,
561+ makeToolResult ( "c" . repeat ( 3_000 ) , "stable_3" ) ,
556562 makeToolResult ( "d" . repeat ( 15_000 ) , "stable_4" ) ,
557563 ] ,
558564 128_000 ,
@@ -564,7 +570,7 @@ describe("truncateOversizedToolResultsInMessages", () => {
564570 const stableFourth = truncateOversizedToolResultsInMessages (
565571 [
566572 ...stableHistory ,
567- makeToolResult ( "c" . repeat ( 15_000 ) , "stable_3" ) ,
573+ makeToolResult ( "c" . repeat ( 3_000 ) , "stable_3" ) ,
568574 makeToolResult ( "d" . repeat ( 15_000 ) , "stable_4" ) ,
569575 makeToolResult ( "e" . repeat ( 15_000 ) , "stable_5" ) ,
570576 ] ,
@@ -577,6 +583,124 @@ describe("truncateOversizedToolResultsInMessages", () => {
577583 expect ( lastText && getToolResultTextLength ( lastText ) ) . toBeLessThanOrEqual ( 12_000 ) ;
578584 } ) ;
579585
586+ it ( "preserves fresh trailing tool results when aggregate history is already saturated" , ( ) => {
587+ const projectionState = createToolResultPromptProjectionState ( ) ;
588+ const history : AgentMessage [ ] = [ ] ;
589+ for ( let index = 0 ; index < 50 ; index ++ ) {
590+ history . push ( makeAssistantMessage ( `call ${ index } ` ) ) ;
591+ history . push ( makeToolResult ( "x" . repeat ( 4_000 ) , `history_${ index } ` ) ) ;
592+ }
593+ history . push ( makeUserMessage ( "run echo" ) ) ;
594+
595+ const first = truncateOversizedToolResultsInMessages (
596+ history ,
597+ 1_000_000 ,
598+ 8_000 ,
599+ 32_000 ,
600+ projectionState ,
601+ ) ;
602+ expect ( first . truncatedCount ) . toBeGreaterThan ( 0 ) ;
603+
604+ const freshOutput = "ABC" ;
605+ const second = truncateOversizedToolResultsInMessages (
606+ [ ...history , makeAssistantMessage ( "running exec" ) , makeToolResult ( freshOutput , "fresh_exec" ) ] ,
607+ 1_000_000 ,
608+ 8_000 ,
609+ 32_000 ,
610+ projectionState ,
611+ ) ;
612+
613+ const freshResult = second . messages . at ( - 1 ) ;
614+ const totalChars = second . messages . reduce (
615+ ( sum , message ) =>
616+ sum + ( message . role === "toolResult" ? getToolResultTextLength ( message ) : 0 ) ,
617+ 0 ,
618+ ) ;
619+ expect ( freshResult ?. role ) . toBe ( "toolResult" ) ;
620+ expect ( freshResult && getFirstToolResultText ( freshResult ) ) . toBe ( freshOutput ) ;
621+ expect ( totalChars ) . toBeLessThanOrEqual ( 32_000 ) ;
622+ } ) ;
623+
624+ it ( "caps oversized fresh trailing tool results without clearing them for aggregate recovery" , ( ) => {
625+ const projectionState = createToolResultPromptProjectionState ( ) ;
626+ const history : AgentMessage [ ] = [ ] ;
627+ for ( let index = 0 ; index < 50 ; index ++ ) {
628+ history . push ( makeAssistantMessage ( `call ${ index } ` ) ) ;
629+ history . push ( makeToolResult ( "x" . repeat ( 4_000 ) , `history_${ index } ` ) ) ;
630+ }
631+ history . push ( makeUserMessage ( "run large command" ) ) ;
632+
633+ truncateOversizedToolResultsInMessages ( history , 1_000_000 , 8_000 , 32_000 , projectionState ) ;
634+
635+ const second = truncateOversizedToolResultsInMessages (
636+ [
637+ ...history ,
638+ makeAssistantMessage ( "running exec" ) ,
639+ makeToolResult ( "z" . repeat ( 20_000 ) , "fresh_large_exec" ) ,
640+ ] ,
641+ 1_000_000 ,
642+ 8_000 ,
643+ 32_000 ,
644+ projectionState ,
645+ ) ;
646+
647+ const freshResult = second . messages . at ( - 1 ) ;
648+ const freshText = freshResult ? getFirstToolResultText ( freshResult ) : "" ;
649+ const totalChars = second . messages . reduce (
650+ ( sum , message ) =>
651+ sum + ( message . role === "toolResult" ? getToolResultTextLength ( message ) : 0 ) ,
652+ 0 ,
653+ ) ;
654+ expect ( freshResult ?. role ) . toBe ( "toolResult" ) ;
655+ expect ( freshText . length ) . toBeGreaterThan ( 0 ) ;
656+ expect ( freshText . length ) . toBeLessThanOrEqual ( 8_000 ) ;
657+ expect ( freshText ) . toContain ( "truncated" ) ;
658+ expect ( totalChars ) . toBeLessThanOrEqual ( 32_000 ) ;
659+ } ) ;
660+
661+ it ( "falls back to trimming fresh trailing batches that exceed the aggregate budget" , ( ) => {
662+ const projectionState = createToolResultPromptProjectionState ( ) ;
663+ const messages : AgentMessage [ ] = [ makeUserMessage ( "run several tools" ) ] ;
664+ for ( let index = 0 ; index < 5 ; index ++ ) {
665+ messages . push ( makeToolResult ( String ( index ) . repeat ( 8_000 ) , `fresh_${ index } ` ) ) ;
666+ }
667+
668+ const result = truncateOversizedToolResultsInMessages (
669+ messages ,
670+ 1_000_000 ,
671+ 8_000 ,
672+ 32_000 ,
673+ projectionState ,
674+ ) ;
675+ const toolResults = result . messages . filter ( ( message ) => message . role === "toolResult" ) ;
676+ const totalChars = toolResults . reduce (
677+ ( sum , message ) => sum + getToolResultTextLength ( message ) ,
678+ 0 ,
679+ ) ;
680+
681+ expect ( result . truncatedCount ) . toBeGreaterThan ( 0 ) ;
682+ expect ( totalChars ) . toBeLessThanOrEqual ( 32_000 ) ;
683+ expect ( toolResults . every ( ( message ) => getFirstToolResultText ( message ) . length > 0 ) ) . toBe ( true ) ;
684+ } ) ;
685+
686+ it ( "keeps aggregate elision markers inside tiny explicit budgets" , ( ) => {
687+ const messages : AgentMessage [ ] = [
688+ makeToolResult ( "a" . repeat ( 100 ) , "tiny_1" ) ,
689+ makeToolResult ( "b" . repeat ( 100 ) , "tiny_2" ) ,
690+ makeToolResult ( "c" . repeat ( 100 ) , "tiny_3" ) ,
691+ ] ;
692+
693+ const result = truncateOversizedToolResultsInMessages ( messages , 128_000 , 100 , 8 ) ;
694+ const totalChars = result . messages . reduce (
695+ ( sum , message ) =>
696+ sum + ( message . role === "toolResult" ? getToolResultTextLength ( message ) : 0 ) ,
697+ 0 ,
698+ ) ;
699+
700+ expect ( result . truncatedCount ) . toBeGreaterThan ( 0 ) ;
701+ expect ( totalChars ) . toBeLessThanOrEqual ( 8 ) ;
702+ } ) ;
703+
580704 it ( "does not restore filtered image blocks when reusing a projection" , ( ) => {
581705 const projectionState = createToolResultPromptProjectionState ( ) ;
582706 const source = makeToolResult ( "x" . repeat ( 15_000 ) , "image_call" ) ;
0 commit comments