@@ -8,11 +8,12 @@ const content = "é한😀".repeat(32);
88const line = JSON . stringify ( { type : "text-delta" , text : content } ) ;
99const encoder = new TextEncoder ( ) ;
1010
11- function input ( bytes : Uint8Array , chunkSize : number ) {
11+ function input ( bytes : Uint8Array , chunkSize : number , beforeRead ?: ( offset : number ) => void ) {
1212 let offset = 0 ;
1313 let cancelled = false ;
1414 const body = new ReadableStream < Uint8Array > ( {
1515 pull ( controller ) {
16+ beforeRead ?.( offset ) ;
1617 if ( offset === bytes . length ) { controller . close ( ) ; return ; }
1718 const end = Math . min ( bytes . length , offset + chunkSize ) ;
1819 controller . enqueue ( bytes . subarray ( offset , end ) ) ;
@@ -56,14 +57,44 @@ for (const fragmented of [false, true]) {
5657 } ) ;
5758}
5859
59- test ( "Command Code buffer replacement preserves another owner's retained bytes" , async ( ) => {
60- const bytes = encoder . encode ( line ) ;
61- const budget = createTestTranslatorBudget ( { maxTurnBytes : bytes . length + 64 } ) ;
62- budget . chargeRetained ( 64 , { kind : "retained_collectors" } ) ;
63- for await ( const _event of adapter ( ) . parseStream ( input ( bytes , 1 ) . response , budget ) ) { /* drain */ }
64- expect ( budget . snapshot ( ) ) . toMatchObject ( { currentBytes : 64 , highWaterBytes : bytes . length + 64 , overflows : 0 } ) ;
65- budget . releaseRetained ( 64 , { kind : "retained_collectors" } ) ;
66- expect ( budget . snapshot ( ) . currentBytes ) . toBe ( 0 ) ;
60+ for ( const deficit of [ 0 , 1 ] ) {
61+ test ( `Command Code preserves another live_transient owner's bytes on ${ deficit ? "refused" : "successful" } growth` , async ( ) => {
62+ const bytes = encoder . encode ( line ) ;
63+ const budget = createTestTranslatorBudget ( { maxTurnBytes : bytes . length + 64 - deficit } ) ;
64+ budget . chargeRetained ( 64 , { kind : "live_transient" } ) ;
65+ const source = input ( bytes , 1 ) ;
66+ const events : AdapterEvent [ ] = [ ] ;
67+ const consume = async ( ) => {
68+ for await ( const event of adapter ( ) . parseStream ( source . response , budget ) ) events . push ( event ) ;
69+ } ;
70+ if ( deficit ) {
71+ await expect ( consume ( ) ) . rejects . toMatchObject ( { code : "translation_buffer_limit" } ) ;
72+ expect ( events ) . toHaveLength ( 0 ) ;
73+ expect ( source . cancelled ( ) ) . toBe ( true ) ;
74+ } else {
75+ await consume ( ) ;
76+ expect ( events . at ( - 1 ) ?. type ) . toBe ( "done" ) ;
77+ }
78+ expect ( budget . snapshot ( ) ) . toMatchObject ( { currentBytes : 64 , highWaterBytes : bytes . length + 64 - deficit , overflows : deficit } ) ;
79+ budget . releaseRetained ( 64 , { kind : "live_transient" } ) ;
80+ expect ( budget . snapshot ( ) . currentBytes ) . toBe ( 0 ) ;
81+ } ) ;
82+ }
83+
84+ test ( "Command Code releases drained bytes before reading the next fragment" , async ( ) => {
85+ const carryBytes = 16 ; // The second record's ASCII JSON prefix, before any text content.
86+ const firstReadBytes = encoder . encode ( line + "\n" ) . length + carryBytes ;
87+ const budget = createTestTranslatorBudget ( { maxTurnBytes : firstReadBytes } ) ;
88+ const retainedBeforeSecondRead : number [ ] = [ ] ;
89+ const source = input ( encoder . encode ( line + "\n" + line ) , firstReadBytes , offset => {
90+ if ( offset === firstReadBytes ) retainedBeforeSecondRead . push ( budget . snapshot ( ) . currentBytes ) ;
91+ } ) ;
92+ const events : AdapterEvent [ ] = [ ] ;
93+ for await ( const event of adapter ( ) . parseStream ( source . response , budget ) ) events . push ( event ) ;
94+ expect ( retainedBeforeSecondRead ) . toEqual ( [ carryBytes ] ) ;
95+ expect ( events . filter ( event => event . type === "text_delta" ) . map ( event => event . text ) ) . toEqual ( [ content , content ] ) ;
96+ expect ( events . at ( - 1 ) ?. type ) . toBe ( "done" ) ;
97+ expect ( budget . snapshot ( ) ) . toMatchObject ( { currentBytes : 0 , highWaterBytes : firstReadBytes , overflows : 0 } ) ;
6798} ) ;
6899
69100test ( "Command Code consumer return releases its retained record and cancels upstream" , async ( ) => {
0 commit comments