@@ -2677,6 +2677,33 @@ describe("context-window tool_result truncation", () => {
26772677 expect ( middleOutTruncate ( "hello world" , 100 ) ) . toBe ( "hello world" )
26782678 } )
26792679
2680+ it ( "returns text unchanged when its length exactly equals the limit" , ( ) => {
2681+ // Longer than the truncation marker, so `<=` is observable here: the marker-less
2682+ // fallback for tiny limits would otherwise return the same text under either operator.
2683+ const text = "A" . repeat ( 200 )
2684+ expect ( middleOutTruncate ( text , 200 ) ) . toBe ( text )
2685+ } )
2686+
2687+ it ( "stays within a limit too small to hold the truncation marker" , ( ) => {
2688+ const text = "A" . repeat ( 500 )
2689+ for ( const limit of [ 1 , 5 , 50 ] ) {
2690+ const result = middleOutTruncate ( text , limit )
2691+ expect ( result ) . toBe ( "A" . repeat ( limit ) )
2692+ }
2693+ } )
2694+
2695+ it ( "does not end a marker-less truncation on a lone high surrogate" , ( ) => {
2696+ // Index-based so a surrogate pair contributes BOTH of its code units to the assertion.
2697+ const codeUnits = ( value : string ) : number [ ] =>
2698+ Array . from ( { length : value . length } , ( _ , index ) => value . charCodeAt ( index ) )
2699+
2700+ // The limit lands between the halves of the pair at index 3, which would otherwise be
2701+ // emitted alone and make the payload un-encodable as UTF-8.
2702+ const result = middleOutTruncate ( `abc\uD83D\uDE00${ "z" . repeat ( 200 ) } ` , 4 )
2703+
2704+ expect ( codeUnits ( result ) ) . toEqual ( codeUnits ( "abc" ) )
2705+ } )
2706+
26802707 it ( "keeps the head and tail and inserts a truncation marker" , ( ) => {
26812708 const text = "A" . repeat ( 500 ) + "B" . repeat ( 500 )
26822709 const result = middleOutTruncate ( text , 200 )
@@ -2769,9 +2796,9 @@ describe("context-window tool_result truncation", () => {
27692796
27702797 const toolResult = findBlock ( messages [ 1 ] , "tool_result" )
27712798 const parts = toolResult . content as Array < { type : string ; text ?: string } >
2772- expect ( parts [ 0 ] . type ) . toBe ( "text" )
27732799 expect ( parts [ 0 ] . text ) . toContain ( "characters truncated" )
2774- expect ( parts . some ( ( part ) => part . type === "image" ) ) . toBe ( true )
2800+ // Exact shape: a surviving original text part would leave a third element behind.
2801+ expect ( parts . map ( ( part ) => part . type ) ) . toEqual ( [ "text" , "image" ] )
27752802 } )
27762803
27772804 it ( "ignores string content and skips messages that cannot hold tool_result blocks" , ( ) => {
0 commit comments