44 * Keeps bounded display tails in memory while spilling full output to private temp files when needed.
55 */
66import { finished } from "node:stream/promises" ;
7+ import { truncateUtf8Suffix } from "../../../utils/utf8-truncate.js" ;
78import { createPrivateTempWriteStream } from "./private-temp-file.js" ;
89import {
910 DEFAULT_MAX_BYTES ,
@@ -60,12 +61,11 @@ export class OutputAccumulator {
6061 private spillChunks : Buffer [ ] = [ ] ;
6162 private tailText = "" ;
6263 private tailBytes = 0 ;
63- private tailStartsAtLineBoundary = true ;
6464 private totalRawBytes = 0 ;
6565 private totalDecodedBytes = 0 ;
6666 private completedLines = 0 ;
6767 private totalLines = 0 ;
68- private currentLineBytes = 0 ;
68+ private lastLineBytes = 0 ;
6969 private hasOpenLine = false ;
7070 private finished = false ;
7171
@@ -76,7 +76,9 @@ export class OutputAccumulator {
7676 constructor ( options : OutputAccumulatorOptions = { } ) {
7777 this . maxLines = options . maxLines ?? DEFAULT_MAX_LINES ;
7878 this . maxBytes = options . maxBytes ?? DEFAULT_MAX_BYTES ;
79- this . maxRollingBytes = Math . max ( this . maxBytes * 2 , 1 ) ;
79+ // UTF-8 trimming can drop three bytes; truncateTail also ignores a final newline.
80+ // Keep enough extra bytes that an incomplete leading line cannot fit the display.
81+ this . maxRollingBytes = Math . max ( this . maxBytes * 2 , this . maxBytes + 5 ) ;
8082 this . tempFilePrefix = options . tempFilePrefix ?? "openclaw-output" ;
8183 this . createTextTransform = options . createTextTransform ;
8284 }
@@ -142,7 +144,7 @@ export class OutputAccumulator {
142144 }
143145
144146 snapshot ( options : { persistIfTruncated ?: boolean } = { } ) : OutputSnapshot {
145- const tailTruncation = truncateTail ( this . getSnapshotText ( ) , {
147+ const tailTruncation = truncateTail ( this . tailText , {
146148 maxLines : this . maxLines ,
147149 maxBytes : this . maxBytes ,
148150 } ) ;
@@ -182,7 +184,7 @@ export class OutputAccumulator {
182184 }
183185
184186 getLastLineBytes ( ) : number {
185- return this . currentLineBytes ;
187+ return this . lastLineBytes ;
186188 }
187189
188190 private appendDecodedText ( text : string ) : void {
@@ -195,56 +197,23 @@ export class OutputAccumulator {
195197 this . tailText += text ;
196198 this . tailBytes += bytes ;
197199 if ( this . tailBytes > this . maxRollingBytes * 2 ) {
198- this . trimTail ( ) ;
199- }
200-
201- let newlines = 0 ;
202- let lastNewline = - 1 ;
203- for ( let i = text . indexOf ( "\n" ) ; i !== - 1 ; i = text . indexOf ( "\n" , i + 1 ) ) {
204- newlines ++ ;
205- lastNewline = i ;
206- }
207- if ( newlines === 0 ) {
208- this . currentLineBytes += bytes ;
209- this . hasOpenLine = true ;
210- } else {
211- this . completedLines += newlines ;
212- const tail = text . slice ( lastNewline + 1 ) ;
213- this . currentLineBytes = byteLength ( tail ) ;
214- this . hasOpenLine = tail . length > 0 ;
215- }
216- this . totalLines = this . completedLines + ( this . hasOpenLine ? 1 : 0 ) ;
217- }
218-
219- private trimTail ( ) : void {
220- const buffer = Buffer . from ( this . tailText , "utf-8" ) ;
221- if ( buffer . length <= this . maxRollingBytes ) {
222- this . tailBytes = buffer . length ;
223- return ;
224- }
225-
226- let start = buffer . length - this . maxRollingBytes ;
227- while ( start < buffer . length ) {
228- const byte = buffer . at ( start ) ;
229- if ( byte === undefined || ( byte & 0xc0 ) !== 0x80 ) {
230- break ;
200+ this . tailText = truncateUtf8Suffix ( this . tailText , this . maxRollingBytes ) ;
201+ this . tailBytes = byteLength ( this . tailText ) ;
202+ }
203+
204+ // A terminator closes the last real line; its size still belongs in the footer.
205+ for ( let start = 0 ; start < text . length ; ) {
206+ const newline = text . indexOf ( "\n" , start ) ;
207+ const end = newline === - 1 ? text . length : newline ;
208+ this . lastLineBytes =
209+ ( this . hasOpenLine ? this . lastLineBytes : 0 ) + byteLength ( text . slice ( start , end ) ) ;
210+ this . hasOpenLine = newline === - 1 ;
211+ if ( ! this . hasOpenLine ) {
212+ this . completedLines ++ ;
231213 }
232- start ++ ;
233- }
234-
235- this . tailStartsAtLineBoundary =
236- start === 0 ? this . tailStartsAtLineBoundary : buffer . at ( start - 1 ) === 0x0a ;
237- this . tailText = buffer . subarray ( start ) . toString ( "utf-8" ) ;
238- this . tailBytes = byteLength ( this . tailText ) ;
239- }
240-
241- private getSnapshotText ( ) : string {
242- if ( this . tailStartsAtLineBoundary ) {
243- return this . tailText ;
214+ start = end + 1 ;
244215 }
245-
246- const firstNewline = this . tailText . indexOf ( "\n" ) ;
247- return firstNewline === - 1 ? this . tailText : this . tailText . slice ( firstNewline + 1 ) ;
216+ this . totalLines = this . completedLines + ( this . hasOpenLine ? 1 : 0 ) ;
248217 }
249218
250219 private shouldUseTempFile ( ) : boolean {
0 commit comments