@@ -60,6 +60,35 @@ const captureLimits = (override: Partial<CaptureLimits> | undefined): CaptureLim
6060} ;
6161const diagnosticFor = ( reason : StopReason , detail ?: string ) => Buffer . from (
6262 `[codeboost: ${ reason } ${ detail ? `: ${ detail . replace ( / [ \r \n ] + / g, ' ' ) . slice ( 0 , 512 ) } ` : '' } ]\n` ) ;
63+ /**
64+ * Retains captured output in fixed-size blocks, so memory scales with retained bytes rather than with the number
65+ * of write events a container emits.
66+ */
67+ export class ByteCollector {
68+ static readonly BLOCK_BYTES = 64 * 1024 ;
69+ private readonly blocks : Buffer [ ] = [ ] ;
70+ private used = 0 ;
71+ push ( data : Buffer ) : void {
72+ for ( let offset = 0 ; offset < data . length ; ) {
73+ let block = this . blocks . at ( - 1 ) ;
74+ if ( ! block || this . used === block . length ) {
75+ block = Buffer . allocUnsafe ( ByteCollector . BLOCK_BYTES ) ;
76+ this . blocks . push ( block ) ;
77+ this . used = 0 ;
78+ }
79+ const count = Math . min ( data . length - offset , block . length - this . used ) ;
80+ data . copy ( block , this . used , offset , offset + count ) ;
81+ this . used += count ;
82+ offset += count ;
83+ }
84+ }
85+ toBuffer ( ) : Buffer {
86+ if ( ! this . blocks . length ) return Buffer . alloc ( 0 ) ;
87+ return Buffer . concat ( [ ...this . blocks . slice ( 0 , - 1 ) , this . blocks . at ( - 1 ) ! . subarray ( 0 , this . used ) ] ) ;
88+ }
89+ get blockCount ( ) : number { return this . blocks . length ; }
90+ }
91+
6392const retainCleanupOwnership = ( profile : ContainerProfile , detail : string , register = true ) : InvocationHandle => {
6493 const invocation = assertPhasePolicy ( profile . policy ) ;
6594 let resolveSettled ! : ( result : InvocationResult ) => void , cleaning = false , complete = false ;
@@ -274,7 +303,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
274303 throw error ;
275304 }
276305
277- const stdoutChunks : Buffer [ ] = [ ] , stderrChunks : Buffer [ ] = [ ] ;
306+ const stdoutChunks = new ByteCollector ( ) , stderrChunks = new ByteCollector ( ) ;
278307 let stdoutBytes = 0 , stderrBytes = 0 , combinedBytes = 0 ;
279308 let stopReason : StopReason | undefined , failureDetail : string | undefined ;
280309 let closed = false , terminating = false , settlementComplete = false ;
@@ -388,7 +417,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
388417 ? new CaptureDeadlineError ( 'Adapter output capture exceeded the invocation deadline.' )
389418 : new Error ( 'Adapter output capture was cancelled.' ) ) , { once : true } ) ;
390419 } ) ;
391- const raw = Buffer . concat ( stdoutChunks , stdoutBytes ) ;
420+ const raw = stdoutChunks . toBuffer ( ) ;
392421 const operation = Promise . resolve ( options . decode ! ( profile , raw ,
393422 Math . max ( 1 , Math . min ( limits . stdoutBytes - stdoutBytes , limits . combinedBytes - combinedBytes ) ) ,
394423 budget , controller . signal ) ) ;
@@ -526,7 +555,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
526555 if ( ! protocolLine ( protocolBuffer ) ) capture ( 'stderr' , protocolBuffer , true ) ;
527556 protocolBuffer = Buffer . alloc ( 0 ) ;
528557 }
529- let finalStdout = Buffer . concat ( stdoutChunks , stdoutBytes ) , finalStderr = Buffer . concat ( stderrChunks ) ;
558+ let finalStdout = stdoutChunks . toBuffer ( ) , finalStderr = stderrChunks . toBuffer ( ) ;
530559 let exitCode = code , finalSignal = signal ;
531560 if ( ! stopReason && options . decode && ! profile . deferredOutput ) await decodeOutput ( ) ;
532561 if ( decodePromise ) await decodePromise ;
0 commit comments