@@ -213,6 +213,7 @@ interface CreateManagerOptions {
213213 subprocessPollIntervalMs ?: number ;
214214 processKillGraceMs ?: number ;
215215 maxRetainedInactiveSessions ?: number ;
216+ historyByteLimit ?: number ;
216217 ptyAdapter ?: FakePtyAdapter ;
217218}
218219
@@ -243,6 +244,9 @@ const createManager = (
243244 logsDir,
244245 historyLineLimit,
245246 ptyAdapter,
247+ ...( options . historyByteLimit !== undefined
248+ ? { historyByteLimit : options . historyByteLimit }
249+ : { } ) ,
246250 ...( options . shellResolver !== undefined ? { shellResolver : options . shellResolver } : { } ) ,
247251 ...( options . env !== undefined ? { env : options . env } : { } ) ,
248252 ...( options . subprocessInspector !== undefined
@@ -276,16 +280,27 @@ const createManager = (
276280const withHostPlatform = ( platform : NodeJS . Platform ) =>
277281 Layer . succeed ( HostProcessPlatform , platform ) ;
278282
279- // The previous split/join algorithm is the reference for retained text .
280- function retainedHistory ( text : string , maxLines : number ) : string {
283+ // Apply the existing line policy, then find the longest code-point-aligned byte tail .
284+ function retainedHistory ( text : string , maxLines : number , maxBytes = Infinity ) : string {
281285 const terminated = text . endsWith ( "\n" ) ;
282286 const lines = text . split ( "\n" ) ;
283287 if ( terminated ) lines . pop ( ) ;
284288 const retained = lines . slice ( Math . max ( 0 , lines . length - maxLines ) ) . join ( "\n" ) ;
285- return terminated ? `${ retained } \n` : retained ;
289+ const capped = terminated ? `${ retained } \n` : retained ;
290+ if ( Buffer . byteLength ( capped ) <= maxBytes ) return capped ;
291+ const points = Array . from ( capped ) ;
292+ let start = points . length ;
293+ let bytes = 0 ;
294+ while ( start > 0 ) {
295+ const next = Buffer . byteLength ( points [ start - 1 ] ! ) ;
296+ if ( bytes + next > maxBytes ) break ;
297+ bytes += next ;
298+ start -= 1 ;
299+ }
300+ return points . slice ( start ) . join ( "" ) ;
286301}
287302
288- it ( "preserves history across arbitrary chunks, Unicode, ANSI sequences, and clear" , ( ) => {
303+ it ( "preserves line and byte limits across arbitrary chunks, Unicode, ANSI sequences, and clear" , ( ) => {
289304 let randomSeed = 0x20260904 ;
290305 const fragments = [
291306 "" ,
@@ -308,25 +323,49 @@ it("preserves history across arbitrary chunks, Unicode, ANSI sequences, and clea
308323 return fragments [ randomSeed % fragments . length ] ! ;
309324 } ;
310325
311- for ( const maxLines of [ 0 , 1 , 3 , 5 , 5_000 ] ) {
312- let expected = retainedHistory ( "before\ninitial\n" , maxLines ) ;
313- const history = new TerminalManager . BoundedTerminalHistory ( maxLines , expected ) ;
314- expect ( history . value ( ) ) . toBe ( expected ) ;
326+ for ( const maxBytes of [ 0 , 3 , 8 , 64 , Infinity ] ) {
327+ for ( const maxLines of [ 0 , 1 , 3 , 5 , 5_000 ] ) {
328+ let expected = retainedHistory ( "before\ninitial\n" , maxLines , maxBytes ) ;
329+ const history = new TerminalManager . BoundedTerminalHistory (
330+ maxLines ,
331+ "before\ninitial\n" ,
332+ maxBytes ,
333+ ) ;
334+ expect ( history . value ( ) ) . toBe ( expected ) ;
315335
316- for ( let step = 0 ; step < 300 ; step += 1 ) {
317- if ( step % 73 === 0 ) {
318- history . clear ( ) ;
319- expected = "" ;
336+ for ( let step = 0 ; step < 300 ; step += 1 ) {
337+ if ( step % 73 === 0 ) {
338+ history . clear ( ) ;
339+ expected = "" ;
340+ expect ( history . value ( ) ) . toBe ( expected ) ;
341+ }
342+ const chunk = nextFragment ( ) + nextFragment ( ) ;
343+ history . append ( chunk ) ;
344+ expected = retainedHistory ( expected + chunk , maxLines , maxBytes ) ;
320345 expect ( history . value ( ) ) . toBe ( expected ) ;
321346 }
322- const chunk = nextFragment ( ) + nextFragment ( ) ;
323- history . append ( chunk ) ;
324- expected = retainedHistory ( expected + chunk , maxLines ) ;
325- expect ( history . value ( ) ) . toBe ( expected ) ;
326347 }
327348 }
328349} ) ;
329350
351+ it ( "bounds long partial lines and joins surrogate pairs across chunk boundaries" , ( ) => {
352+ const maxBytes = 65_539 ;
353+ let expected = "" ;
354+ const history = new TerminalManager . BoundedTerminalHistory ( 5_000 , "" , maxBytes ) ;
355+ const writes = [
356+ "a" . repeat ( 16_383 ) + "😀" + "b" . repeat ( 70_000 ) ,
357+ "\r" + "c" . repeat ( 70_000 ) + "\ud83d" ,
358+ "\ude80" + "d" . repeat ( 100 ) ,
359+ "\uFEFF" + "名" . repeat ( 30_000 ) ,
360+ ] ;
361+ for ( const text of writes ) {
362+ history . append ( text ) ;
363+ expected = retainedHistory ( expected + text , 5_000 , maxBytes ) ;
364+ expect ( history . value ( ) ) . toBe ( expected ) ;
365+ expect ( Buffer . byteLength ( history . value ( ) ) ) . toBeLessThanOrEqual ( maxBytes ) ;
366+ }
367+ } ) ;
368+
330369it ( "preserves retained lines as older storage is compacted" , ( ) => {
331370 for ( const maxLines of [ 3 , 5_000 ] ) {
332371 let expected = "" ;
@@ -1173,6 +1212,86 @@ it.layer(
11731212 } ) ,
11741213 ) ;
11751214
1215+ it . effect ( "bounds persisted and attached history without truncating live output" , ( ) =>
1216+ Effect . gen ( function * ( ) {
1217+ const { manager, ptyAdapter, logsDir } = yield * createManager ( 5 , { historyByteLimit : 10 } ) ;
1218+ const attachEvents = yield * Ref . make < ReadonlyArray < TerminalAttachStreamEvent > > ( [ ] ) ;
1219+ const unsubscribe = yield * manager . attachStream ( openInput ( ) , ( event ) =>
1220+ Ref . update ( attachEvents , ( events ) => [ ...events , event ] ) ,
1221+ ) ;
1222+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( unsubscribe ) ) ;
1223+ const writes = [ "a" . repeat ( 32 ) , "😀\rEND" ] ;
1224+ const process = ptyAdapter . processes [ 0 ] ! ;
1225+ for ( const text of writes ) process . emitData ( text ) ;
1226+ yield * manager . close ( { threadId : "thread-1" } ) ;
1227+ expect ( yield * readFileString ( yield * historyLogPath ( logsDir ) ) ) . toBe ( "aa😀\rEND" ) ;
1228+
1229+ const reopened = yield * manager . open ( openInput ( ) ) ;
1230+ const events = yield * Ref . get ( attachEvents ) ;
1231+ expect ( events . filter ( ( event ) => event . type === "output" ) . map ( ( event ) => event . data ) ) . toEqual (
1232+ writes ,
1233+ ) ;
1234+ const snapshot = events . filter ( ( event ) => event . type === "snapshot" ) . at ( - 1 ) ?. snapshot ;
1235+ expect ( snapshot ?. history ) . toBe ( "aa😀\rEND" ) ;
1236+ expect ( snapshot ?. sequence ) . toBe ( reopened . sequence ) ;
1237+ } ) ,
1238+ ) ;
1239+
1240+ for ( const source of [ "current" , "legacy" ] as const ) {
1241+ it . effect ( `reads only a Unicode-safe tail from oversized ${ source } history` , ( ) =>
1242+ Effect . gen ( function * ( ) {
1243+ const fs = yield * FileSystem . FileSystem ;
1244+ const path = yield * Path . Path ;
1245+ let sourcePath : string | undefined ;
1246+ let closedReads = 0 ;
1247+ const readRequests : number [ ] = [ ] ;
1248+ const trackedFileSystem = FileSystem . FileSystem . of ( {
1249+ ...fs ,
1250+ readFileString : ( candidate , encoding ) =>
1251+ candidate === sourcePath
1252+ ? Effect . die ( "History restoration must not read the whole file" )
1253+ : fs . readFileString ( candidate , encoding ) ,
1254+ open : ( candidate , options ) =>
1255+ Effect . gen ( function * ( ) {
1256+ if ( candidate !== sourcePath ) return yield * fs . open ( candidate , options ) ;
1257+ yield * Effect . addFinalizer ( ( ) =>
1258+ Effect . sync ( ( ) => {
1259+ closedReads += 1 ;
1260+ } ) ,
1261+ ) ;
1262+ const file = yield * fs . open ( candidate , options ) ;
1263+ return new Proxy ( file , {
1264+ get ( target , key ) {
1265+ if ( key === "read" ) {
1266+ return ( buffer : Uint8Array ) => {
1267+ readRequests . push ( buffer . byteLength ) ;
1268+ return target . read ( buffer . subarray ( 0 , 5 ) ) ;
1269+ } ;
1270+ }
1271+ return Reflect . get ( target , key , target ) ;
1272+ } ,
1273+ } ) ;
1274+ } ) ,
1275+ } ) ;
1276+ const { manager, logsDir } = yield * createManager ( 5 , { historyByteLimit : 15 } ) . pipe (
1277+ Effect . provideService ( FileSystem . FileSystem , trackedFileSystem ) ,
1278+ ) ;
1279+ const nextPath = yield * historyLogPath ( logsDir ) ;
1280+ sourcePath = source === "current" ? nextPath : path . join ( logsDir , "thread-1.log" ) ;
1281+ yield * fs . writeFileString ( sourcePath , "old" . repeat ( 32_768 ) + "😀\uFEFFnewest\ré" ) ;
1282+
1283+ const snapshot = yield * manager . open ( openInput ( ) ) ;
1284+ expect ( snapshot . history ) . toBe ( "\uFEFFnewest\ré" ) ;
1285+ expect ( readRequests ) . toEqual ( [ 15 , 10 , 5 ] ) ;
1286+ expect ( closedReads ) . toBe ( 1 ) ;
1287+ expect ( Buffer . from ( yield * fs . readFile ( nextPath ) ) . toString ( ) ) . toBe ( "\uFEFFnewest\ré" ) ;
1288+ if ( source === "legacy" ) expect ( yield * fs . exists ( sourcePath ) ) . toBe ( false ) ;
1289+ yield * manager . close ( { threadId : "thread-1" } ) ;
1290+ expect ( ( yield * manager . open ( openInput ( ) ) ) . history ) . toBe ( "\uFEFFnewest\ré" ) ;
1291+ } ) ,
1292+ ) ;
1293+ }
1294+
11761295 it . effect ( "strips replay-unsafe terminal query and reply sequences from persisted history" , ( ) =>
11771296 Effect . gen ( function * ( ) {
11781297 const { manager, ptyAdapter } = yield * createManager ( ) ;
0 commit comments