@@ -11,9 +11,11 @@ import { getCompactPrompt, frameCompactSummary } from "../prompt";
1111import {
1212 compactBudget ,
1313 compactPrefixes as selectPrefixes ,
14+ estimateRequestBytes ,
1415 estimateRequestTokens ,
1516 imageOffloadCount ,
1617 isContextOverflow ,
18+ MAX_REQUEST_BODY_BYTES ,
1719} from "../common/compaction" ;
1820
1921const originalHome = process . env . HOME ;
@@ -775,3 +777,178 @@ test("automatic compaction honors the configured tail budget", async () => {
775777 }
776778 assert . deepEqual ( retained , [ 2 , 4 ] ) ;
777779} ) ;
780+
781+ function imageMessage ( id : string , bytes : number ) : SessionMessage {
782+ return {
783+ ...message ( id , "user" , id ) ,
784+ contentParams : [ { type : "image_url" , image_url : { url : `data:image/png;base64,${ "A" . repeat ( bytes ) } ` } } ] ,
785+ } ;
786+ }
787+
788+ function setByteBudget ( f : ReturnType < typeof fixture > , bytes : number , retainTokens = 0 ) {
789+ // Exercise the real formula with small payloads instead of allocating 48 MiB per test.
790+ f . settings . contextWindow = ( MAX_REQUEST_BODY_BYTES - bytes ) / 2 ;
791+ f . settings . retainTokens = retainTokens ;
792+ }
793+
794+ test ( "message byte estimates include UTF-8, JSON escaping and image data" , ( ) => {
795+ const messages = [ { role : "user" as const , content : '中文\n"' } ] ;
796+ assert . equal ( estimateRequestBytes ( messages ) , new TextEncoder ( ) . encode ( JSON . stringify ( messages ) ) . length ) ;
797+ assert . ok ( estimateRequestBytes ( messages ) > JSON . stringify ( messages ) . length ) ;
798+ const image = new OpenAIMessageConverter ( ) . buildMessages ( [ imageMessage ( "image" , 10000 ) ] , true , "test-vision" ) ;
799+ assert . ok ( estimateRequestBytes ( image ) > 10000 ) ;
800+ assert . equal ( compactBudget ( 1024 * 1024 ) . byteThreshold , 46 * 1024 * 1024 ) ;
801+ for ( const window of [ 24 * 1024 * 1024 , 25 * 1024 * 1024 ] ) {
802+ assert . throws ( ( ) => compactBudget ( window ) , / r e q u e s t s i z e b u d g e t / ) ;
803+ }
804+ } ) ;
805+
806+ for ( const delta of [ - 1 , 0 , 1 ] ) {
807+ test ( `automatic compaction checks the byte threshold with a ${ delta } -byte difference` , async ( ) => {
808+ const history = [ imageMessage ( "old" , 20000 ) , message ( "tail" , "user" , "continue " . repeat ( 1000 ) ) ] ;
809+ const f = fixture ( history ) ;
810+ const context = await f . internal . prepareContext ( f . read ( ) , f . settings . model , true ) ;
811+ const bytes = estimateRequestBytes ( context . messages ) ;
812+ setByteBudget ( f , bytes - delta ) ;
813+ assert . ok ( f . internal . measureRequest ( "compact-test" , context . messages , f . settings . model ) . tokens < 190000 ) ;
814+ await f . run ( ) ;
815+ assert . equal ( f . requests . length , delta > 0 ? 2 : 1 ) ;
816+ assert . equal ( f . requests [ 0 ] . tool_choice === "none" , delta > 0 ) ;
817+ assert . ok ( f . requests . every ( ( request ) => estimateRequestBytes ( request . messages ) <= bytes - delta ) ) ;
818+ assert . equal ( f . read ( ) . find ( ( item ) => item . id === "tail" ) ?. compacted , false ) ;
819+ } ) ;
820+ }
821+
822+ test ( "oversized summary candidates shrink to the longest prefix within the byte budget" , async ( ) => {
823+ const f = fixture ( [
824+ ...[ "first" , "second" , "third" , "fourth" ] . map ( ( id ) => imageMessage ( id , 11000 ) ) ,
825+ message ( "tail" , "user" , "continue" ) ,
826+ ] ) ;
827+ setByteBudget ( f , 30000 ) ;
828+ await f . run ( ) ;
829+ assert . equal ( f . requests . length , 2 ) ;
830+ assert . equal ( f . requests [ 0 ] . tool_choice , "none" ) ;
831+ assert . equal ( f . requests [ 0 ] . messages . at ( - 1 ) . content , getCompactPrompt ( ) ) ;
832+ assert . deepEqual (
833+ f
834+ . read ( )
835+ . filter ( ( item ) => item . compacted )
836+ . map ( ( item ) => item . id ) ,
837+ [ "first" , "second" ]
838+ ) ;
839+ assert . ok ( f . requests . every ( ( request ) => estimateRequestBytes ( request . messages ) <= 30000 ) ) ;
840+ } ) ;
841+
842+ for ( const retainTokens of [ 12000 , 100000 ] ) {
843+ test ( `byte compaction falls back to no extra retention after a ${ retainTokens } -token retention pass` , async ( ) => {
844+ const f = fixture ( [
845+ message ( "old" , "user" , "x" . repeat ( 7000 ) ) ,
846+ ...[ "first" , "second" , "third" ] . map ( ( id ) => imageMessage ( id , 15000 ) ) ,
847+ message ( "tail" , "user" , "continue" ) ,
848+ ] ) ;
849+ setByteBudget ( f , 40000 , retainTokens ) ;
850+ await f . run ( ) ;
851+ assert . equal ( f . requests . length , retainTokens === 12000 ? 3 : 2 ) ;
852+ assert . ok ( f . requests . slice ( 0 , - 1 ) . every ( ( request ) => request . tool_choice === "none" ) ) ;
853+ assert . notEqual ( f . requests . at ( - 1 ) . tool_choice , "none" ) ;
854+ assert . ok ( f . requests . every ( ( request ) => estimateRequestBytes ( request . messages ) <= 40000 ) ) ;
855+ assert . equal ( f . read ( ) . find ( ( item ) => item . id === "tail" ) ?. compacted , false ) ;
856+ } ) ;
857+ }
858+
859+ test ( "byte compaction accepts a smaller payload even when its summary uses more tokens" , async ( ) => {
860+ const f = fixture (
861+ [
862+ ...[ "first" , "second" , "third" ] . map ( ( id ) => ( {
863+ ...message ( id , "user" , id ) ,
864+ contentParams : [
865+ { type : "image_url" , image_url : { url : `data:image/png;base64,${ "A" . repeat ( 11000 ) } ` , detail : "low" } } ,
866+ ] ,
867+ } ) ) ,
868+ message ( "tail" , "user" , "continue" ) ,
869+ ] ,
870+ ( ) => answer ( "x" . repeat ( 10000 ) )
871+ ) ;
872+ setByteBudget ( f , 30000 ) ;
873+ // Compacting two images saves bytes, although the text summary costs more tokens.
874+ const before = await f . internal . prepareContext ( f . read ( ) , f . settings . model , true ) ;
875+ assert . equal ( await f . compact ( ) , true ) ;
876+ assert . equal ( f . read ( ) . filter ( ( item ) => item . compacted ) . length , 2 ) ;
877+ const after = await f . internal . prepareContext ( f . read ( ) , f . settings . model , true ) ;
878+ assert . ok ( estimateRequestTokens ( after . messages ) > estimateRequestTokens ( before . messages ) ) ;
879+ assert . ok ( estimateRequestBytes ( after . messages ) < estimateRequestBytes ( before . messages ) ) ;
880+ } ) ;
881+
882+ test ( "unshrinkable byte-heavy input terminates without sending an oversized request" , async ( ) => {
883+ for ( const kind of [ "fixed" , "latest" , "prefix" , "many" ] as const ) {
884+ const huge = imageMessage ( "huge" , 31000 ) ;
885+ const history =
886+ kind === "fixed"
887+ ? [ { ...huge , role : "system" as const } , message ( "old" ) , message ( "tail" ) ]
888+ : kind === "latest"
889+ ? [ message ( "old" ) , huge ]
890+ : kind === "prefix"
891+ ? [ huge , message ( "tail" ) ]
892+ : [ ...Array . from ( { length : 10 } , ( _ , i ) => imageMessage ( `image-${ i } ` , 11000 ) ) , message ( "tail" ) ] ;
893+ const f = fixture ( history ) ;
894+ setByteBudget ( f , 30000 ) ;
895+ await assert . rejects ( f . run ( ) , / t o o l a r g e | d i d n o t r e d u c e / ) ;
896+ assert . ok ( f . requests . length <= 2 ) ;
897+ assert . ok (
898+ f . requests . every ( ( request ) => request . tool_choice === "none" && estimateRequestBytes ( request . messages ) <= 30000 )
899+ ) ;
900+ assert . equal ( f . read ( ) . at ( - 1 ) ?. compacted , false ) ;
901+ assert . ok ( f . read ( ) . every ( ( item ) => ! item . meta ?. omittedImages ) ) ;
902+ if ( kind === "fixed" || kind === "prefix" ) assert . deepEqual ( f . read ( ) , history ) ;
903+ }
904+ } ) ;
905+
906+ test ( "new tool results trigger byte compaction before the next assistant step" , async ( ) => {
907+ const call = {
908+ ...message ( "call" , "assistant" , "" ) ,
909+ messageParams : { tool_calls : [ { id : "read" , type : "function" , function : { name : "Read" , arguments : "{}" } } ] } ,
910+ } ;
911+ const result = { ...message ( "result" , "tool" , "x" . repeat ( 15000 ) ) , messageParams : { tool_call_id : "read" } } ;
912+ const f = fixture ( [ imageMessage ( "old" , 20000 ) , call , result ] ) ;
913+ setByteBudget ( f , 30000 ) ;
914+ await f . run ( ) ;
915+ assert . equal ( f . requests . length , 2 ) ;
916+ assert . equal ( f . requests [ 0 ] . tool_choice , "none" ) ;
917+ assert . equal ( f . requests [ 1 ] . messages . filter ( ( item : any ) => item . role === "tool" ) . length , 1 ) ;
918+ assert . equal ( f . requests [ 1 ] . messages . find ( ( item : any ) => item . role === "assistant" ) . tool_calls . length , 1 ) ;
919+ assert . ok ( f . requests . every ( ( request ) => estimateRequestBytes ( request . messages ) <= 30000 ) ) ;
920+ } ) ;
921+
922+ test ( "context-overflow recovery rechecks bytes when the summary grows the payload" , async ( ) => {
923+ const f = fixture ( [ message ( "old" , "user" , "x" . repeat ( 10000 ) ) , imageMessage ( "tail" , 18000 ) ] , ( _ , call ) => {
924+ if ( call === 1 ) throw { status : 400 , code : "context_length_exceeded" } ;
925+ return answer ( "中" . repeat ( 4500 ) ) ;
926+ } ) ;
927+ setByteBudget ( f , 30000 ) ;
928+ await assert . rejects ( f . run ( ) , / t o o l a r g e | d i d n o t r e d u c e / ) ;
929+ assert . equal ( f . requests [ 0 ] . tool_choice , undefined ) ;
930+ assert . ok ( f . read ( ) . some ( ( item ) => item . meta ?. isSummary ) ) ;
931+ assert . ok ( f . requests . slice ( 1 ) . every ( ( request ) => request . tool_choice === "none" ) ) ;
932+ assert . ok ( f . requests . every ( ( request ) => estimateRequestBytes ( request . messages ) <= 30000 ) ) ;
933+ } ) ;
934+
935+ test ( "Files API conservatively checks bytes before uploads and sends references after compaction" , async ( ) => {
936+ const f = fixture ( [
937+ ...[ "first" , "second" , "third" ] . map ( ( id ) => imageMessage ( id , 11000 ) ) ,
938+ message ( "tail" , "user" , "continue" ) ,
939+ ] ) ;
940+ setByteBudget ( f , 30000 ) ;
941+ const originalClient = f . internal . createOpenAIClient ;
942+ f . internal . createOpenAIClient = ( ) => ( { ...originalClient ( ) , apiKey : "test-key" } ) ;
943+ Object . assign ( f . settings , { filesApiEnabled : true } ) ;
944+ f . internal . deepSeekFiles = {
945+ ensureUploaded : async ( ) => ( { fileId : "file-image" , imageHash : "a" . repeat ( 64 ) , bytes : 8250 } ) ,
946+ } ;
947+ await f . run ( ) ;
948+ assert . equal ( f . requests . length , 2 ) ;
949+ assert . equal ( f . requests [ 0 ] . tool_choice , "none" ) ;
950+ for ( const request of f . requests ) {
951+ assert . match ( JSON . stringify ( request . messages ) , / f i l e - i m a g e / ) ;
952+ assert . doesNotMatch ( JSON . stringify ( request . messages ) , / b a s e 6 4 / ) ;
953+ }
954+ } ) ;
0 commit comments