@@ -28,6 +28,7 @@ import * as PlatformError from "effect/PlatformError";
2828import * as Path from "effect/Path" ;
2929import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
3030import * as Ref from "effect/Ref" ;
31+ import * as Queue from "effect/Queue" ;
3132import * as Schema from "effect/Schema" ;
3233import * as Scope from "effect/Scope" ;
3334import * as Stream from "effect/Stream" ;
@@ -409,6 +410,41 @@ it("bounds long partial lines and joins surrogate pairs across chunk boundaries"
409410 }
410411} ) ;
411412
413+ it ( "restores bounded history across append compaction, clears and split Unicode" , ( ) => {
414+ const maxBytes = 64 ;
415+ const history = new TerminalManager . BoundedTerminalHistory ( 3 , "" , maxBytes ) ;
416+ let cursor : ReturnType < typeof history . persistenceUpdate > [ "cursor" ] | undefined ;
417+ let persisted = "" ;
418+ let compactions = 0 ;
419+ const flush = ( ) => {
420+ const update = history . persistenceUpdate ( cursor ) ;
421+ if ( ! update . append ) compactions += 1 ;
422+ const encoded = Buffer . from ( update . data ) . toString ( ) ;
423+ persisted = update . append ? persisted + encoded : encoded ;
424+ cursor = update . cursor ;
425+ expect ( Buffer . byteLength ( persisted ) ) . toBeLessThanOrEqual ( maxBytes * 2 ) ;
426+ expect ( retainedHistory ( persisted , 3 , maxBytes ) ) . toBe ( Buffer . from ( history . value ( ) ) . toString ( ) ) ;
427+ } ;
428+ for ( let index = 0 ; index < 100 ; index += 1 ) {
429+ history . append ( `line ${ index } 名\n` ) ;
430+ flush ( ) ;
431+ }
432+ expect ( compactions ) . toBeGreaterThan ( 1 ) ;
433+ expect ( compactions ) . toBeLessThan ( 50 ) ;
434+ history . clear ( ) ;
435+ flush ( ) ;
436+ expect ( persisted ) . toBe ( "" ) ;
437+ history . append ( "\ud83d" ) ;
438+ flush ( ) ;
439+ history . append ( "\ude80" ) ;
440+ flush ( ) ;
441+ expect ( persisted ) . toBe ( "🚀" ) ;
442+ // More output than the retained window between flushes must replace the file.
443+ history . append ( "z" . repeat ( maxBytes * 3 ) ) ;
444+ flush ( ) ;
445+ expect ( persisted ) . toBe ( "z" . repeat ( maxBytes ) ) ;
446+ } ) ;
447+
412448it . layer (
413449 Layer . mergeAll (
414450 NodeServices . layer ,
@@ -1045,6 +1081,119 @@ it.layer(
10451081 } ) ,
10461082 ) ;
10471083
1084+ it . effect ( "persists small terminal updates without rewriting the retained history" , ( ) =>
1085+ Effect . gen ( function * ( ) {
1086+ const fs = yield * FileSystem . FileSystem ;
1087+ const writes = yield * Queue . unbounded < number > ( ) ;
1088+ const maxBytes = 64 * 1024 ;
1089+ const { manager, ptyAdapter, logsDir } = yield * createManager ( 5_000 , {
1090+ historyByteLimit : maxBytes ,
1091+ } ) . pipe (
1092+ Effect . provideService ( FileSystem . FileSystem , {
1093+ ...fs ,
1094+ writeFileString : ( path , data , options ) =>
1095+ fs
1096+ . writeFileString ( path , data , options )
1097+ . pipe (
1098+ Effect . tap ( ( ) =>
1099+ data . length > 0 ? Queue . offer ( writes , Buffer . byteLength ( data ) ) : Effect . void ,
1100+ ) ,
1101+ ) ,
1102+ } ) ,
1103+ ) ;
1104+ yield * manager . open ( openInput ( ) ) ;
1105+ const process = ptyAdapter . processes [ 0 ] ! ;
1106+ process . emitData ( "a" . repeat ( maxBytes ) ) ;
1107+ let written = yield * Queue . take ( writes ) ;
1108+ for ( let index = 0 ; index < 16 ; index += 1 ) {
1109+ process . emitData ( "b" . repeat ( 1024 ) ) ;
1110+ written += yield * Queue . take ( writes ) ;
1111+ }
1112+ expect ( written ) . toBeLessThan ( maxBytes * 2 ) ;
1113+ const expected = "a" . repeat ( maxBytes - 16 * 1024 ) + "b" . repeat ( 16 * 1024 ) ;
1114+ const persisted = yield * fs . readFileString ( yield * historyLogPath ( logsDir ) ) ;
1115+ expect ( Buffer . byteLength ( persisted ) ) . toBe ( maxBytes + 16 * 1024 ) ;
1116+ for ( const failure of [ undefined , "write" , "rename" ] as const ) {
1117+ let failed = false ;
1118+ const fail = ( ) => {
1119+ failed = true ;
1120+ return Effect . fail (
1121+ PlatformError . systemError ( {
1122+ _tag : "Unknown" ,
1123+ module : "FileSystem" ,
1124+ method : failure === "write" ? "writeFileString" : "rename" ,
1125+ cause : { code : "ENOSPC" } ,
1126+ } ) ,
1127+ ) ;
1128+ } ;
1129+ const recovered = yield * createManager ( 5_000 , { historyByteLimit : maxBytes } ) . pipe (
1130+ Effect . provideService ( FileSystem . FileSystem , {
1131+ ...fs ,
1132+ writeFileString : ( file , data , options ) =>
1133+ failure === "write" && ! failed && data === expected
1134+ ? fs . writeFileString ( file , "partial" , options ) . pipe ( Effect . andThen ( fail ) )
1135+ : fs . writeFileString ( file , data , options ) ,
1136+ rename : ( from , to ) => ( failure === "rename" && ! failed ? fail ( ) : fs . rename ( from , to ) ) ,
1137+ } ) ,
1138+ ) ;
1139+ const recoveredPath = yield * historyLogPath ( recovered . logsDir ) ;
1140+ yield * fs . makeDirectory ( recovered . logsDir , { recursive : true } ) ;
1141+ yield * fs . writeFileString ( recoveredPath , persisted ) ;
1142+ const snapshot = yield * recovered . manager . open ( openInput ( ) ) ;
1143+ expect ( snapshot . status ) . toBe ( "running" ) ;
1144+ expect ( snapshot . history ) . toBe ( expected ) ;
1145+ expect ( failed ) . toBe ( failure !== undefined ) ;
1146+ expect ( yield * fs . readFileString ( recoveredPath ) ) . toBe (
1147+ failure === undefined ? expected : persisted ,
1148+ ) ;
1149+ expect ( yield * fs . readDirectory ( recovered . logsDir ) ) . toHaveLength ( 1 ) ;
1150+ }
1151+ yield * manager . close ( { threadId : "thread-1" } ) ;
1152+ expect ( ( yield * manager . open ( openInput ( ) ) ) . history ) . toBe ( expected ) ;
1153+ expect ( yield * fs . readFileString ( yield * historyLogPath ( logsDir ) ) ) . toBe ( expected ) ;
1154+ } ) ,
1155+ ) ;
1156+
1157+ it . effect ( "rewrites history after an append fails partway through" , ( ) =>
1158+ Effect . gen ( function * ( ) {
1159+ const fs = yield * FileSystem . FileSystem ;
1160+ const writes = yield * Queue . unbounded < void > ( ) ;
1161+ let failAppend = false ;
1162+ const { manager, ptyAdapter, logsDir } = yield * createManager ( ) . pipe (
1163+ Effect . provideService ( FileSystem . FileSystem , {
1164+ ...fs ,
1165+ writeFileString : ( path , data , options ) => {
1166+ if ( options ?. flag === "a" && failAppend ) {
1167+ failAppend = false ;
1168+ return fs . writeFileString ( path , data . slice ( 0 , 1 ) , options ) . pipe (
1169+ // The file cannot also be a parent directory: fail after the partial append.
1170+ Effect . andThen ( fs . writeFileString ( `${ path } /missing` , data ) ) ,
1171+ Effect . ensuring ( Queue . offer ( writes , undefined ) ) ,
1172+ ) ;
1173+ }
1174+ return fs
1175+ . writeFileString ( path , data , options )
1176+ . pipe (
1177+ Effect . tap ( ( ) => ( data . length > 0 ? Queue . offer ( writes , undefined ) : Effect . void ) ) ,
1178+ ) ;
1179+ } ,
1180+ } ) ,
1181+ ) ;
1182+ yield * manager . open ( openInput ( ) ) ;
1183+ const process = ptyAdapter . processes [ 0 ] ! ;
1184+ process . emitData ( "start:" ) ;
1185+ yield * Queue . take ( writes ) ;
1186+ failAppend = true ;
1187+ process . emitData ( "middle" ) ;
1188+ yield * Queue . take ( writes ) ;
1189+ expect ( failAppend ) . toBe ( false ) ;
1190+ process . emitData ( ":end" ) ;
1191+ yield * Queue . take ( writes ) ;
1192+ expect ( yield * fs . readFileString ( yield * historyLogPath ( logsDir ) ) ) . toBe ( "start:middle:end" ) ;
1193+ yield * manager . close ( { threadId : "thread-1" } ) ;
1194+ } ) ,
1195+ ) ;
1196+
10481197 it . effect ( "releases delivered output while the process queue stays busy" , ( ) =>
10491198 Effect . gen ( function * ( ) {
10501199 // Observe retained payloads, since delivery and bounded history alone cannot detect a leak.
0 commit comments