@@ -803,6 +803,62 @@ it.layer(
803803 } ) ,
804804 ) ;
805805
806+ it . effect ( "preserves large chunked OSC 52 and DCS strings across reopen" , ( ) =>
807+ Effect . gen ( function * ( ) {
808+ const { manager, ptyAdapter, logsDir } = yield * createManager ( ) ;
809+ yield * manager . open ( openInput ( ) ) ;
810+ const process = ptyAdapter . processes [ 0 ] ! ;
811+ const outputSeen = yield * Deferred . make < void > ( ) ;
812+ const unsubscribe = yield * manager . subscribe ( ( event ) =>
813+ event . type === "output" && event . data === "done\n"
814+ ? Deferred . succeed ( outputSeen , undefined ) . pipe ( Effect . asVoid )
815+ : Effect . void ,
816+ ) ;
817+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( unsubscribe ) ) ;
818+ const sequences = [
819+ { prefix : `\u001b]52;c;${ "YWJj" . repeat ( 16_384 ) } \u001b` , terminator : "\\" } ,
820+ { prefix : `\u0090q${ "?" . repeat ( 65_536 ) } ` , terminator : "\u009c" } ,
821+ ] ;
822+ const stripped = "\u001b]11;?\u0007\u0090$q m\u009c" ;
823+ const preserved = "\u001b[31m\u009d52;c;YWJj\u009c" ;
824+ const expected =
825+ sequences . map ( ( { prefix, terminator } ) => prefix + terminator + preserved ) . join ( "" ) +
826+ "done\n" ;
827+
828+ // Counts reads of strings larger than one emitted chunk, so rescanning the
829+ // pending prefix on every chunk (#23817) shows up as quadratic work.
830+ const charCodeAt = String . prototype . charCodeAt ;
831+ let largeStringReads = 0 ;
832+ yield * Effect . gen ( function * ( ) {
833+ // eslint-disable-next-line no-extend-native -- Count reads without retaining millions of spy call records.
834+ String . prototype . charCodeAt = function ( this : string , index : number ) {
835+ if ( this . length > 4_096 ) largeStringReads += 1 ;
836+ return charCodeAt . call ( this , index ) ;
837+ } ;
838+ for ( const { prefix, terminator } of sequences ) {
839+ for ( let offset = 0 ; offset < prefix . length ; offset += 4_096 ) {
840+ process . emitData ( prefix . slice ( offset , offset + 4_096 ) ) ;
841+ }
842+ process . emitData ( terminator + stripped + preserved ) ;
843+ }
844+ process . emitData ( "done\n" ) ;
845+ yield * Deferred . await ( outputSeen ) ;
846+ } ) . pipe (
847+ Effect . ensuring (
848+ Effect . sync ( ( ) => {
849+ // eslint-disable-next-line no-extend-native -- Restore the counting hook even when the test fails.
850+ String . prototype . charCodeAt = charCodeAt ;
851+ } ) ,
852+ ) ,
853+ ) ;
854+ yield * manager . close ( { threadId : "thread-1" } ) ;
855+
856+ expect ( largeStringReads ) . toBeLessThan ( 2 * expected . length ) ;
857+ expect ( yield * readFileString ( yield * historyLogPath ( logsDir ) ) ) . toBe ( expected ) ;
858+ expect ( ( yield * manager . open ( openInput ( ) ) ) . history . startsWith ( expected ) ) . toBe ( true ) ;
859+ } ) ,
860+ ) ;
861+
806862 it . effect (
807863 "preserves chunk-split ESC sequences with intermediate bytes without leaking final bytes" ,
808864 ( ) =>
0 commit comments