@@ -10,6 +10,7 @@ import {
1010 ProviderDriverKind ,
1111 ProviderInstanceId ,
1212 ProjectId ,
13+ retainedTerminalHistory ,
1314 TerminalProviderInstanceNotFoundError ,
1415} from "@t3tools/contracts" ;
1516import * as HostProcess from "@t3tools/shared/HostProcess" ;
@@ -2325,6 +2326,57 @@ it.layer(
23252326 } ) . pipe ( Effect . scoped ) ,
23262327 ) ;
23272328
2329+ it . effect ( "sends bounded attach streams only the history the client retains" , ( ) =>
2330+ Effect . gen ( function * ( ) {
2331+ const { manager, ptyAdapter } = yield * createManager ( ) ;
2332+ const maxHistoryBytes = 9 ;
2333+ yield * manager . open ( openInput ( ) ) ;
2334+ const process = ptyAdapter . processes [ 0 ] ! ;
2335+ const published = yield * Deferred . make < void > ( ) ;
2336+ const unsubscribe = yield * manager . subscribe ( ( event ) =>
2337+ event . type === "output" && event . data === "yz\n"
2338+ ? Deferred . succeed ( published , undefined ) . pipe ( Effect . asVoid )
2339+ : Effect . void ,
2340+ ) ;
2341+ process . emitData ( "aaaa\nx名😀" ) ;
2342+ process . emitData ( "yz\n" ) ;
2343+ yield * Deferred . await ( published ) ;
2344+ unsubscribe ( ) ;
2345+
2346+ const full = yield * Stream . toPull ( manager . attachOutputStream ( openInput ( ) , "full" ) ) ;
2347+ const bounded = yield * Stream . toPull (
2348+ manager . attachOutputStream ( { ...openInput ( ) , maxHistoryBytes } , "bounded" ) ,
2349+ ) ;
2350+ const expectRetained = Effect . fnUntraced ( function * ( ) {
2351+ const [ fullEvents , boundedEvents ] = [ yield * full , yield * bounded ] ;
2352+ const fullSnapshot = fullEvents . at ( - 1 ) ;
2353+ if ( fullSnapshot ?. type !== "snapshot" ) throw new Error ( "Expected a full snapshot" ) ;
2354+ expect ( boundedEvents . at ( - 1 ) ) . toEqual ( {
2355+ ...fullSnapshot ,
2356+ snapshot : {
2357+ ...fullSnapshot . snapshot ,
2358+ history : retainedTerminalHistory ( fullSnapshot . snapshot . history , maxHistoryBytes ) ,
2359+ } ,
2360+ } ) ;
2361+ return fullSnapshot . snapshot . history ;
2362+ } ) ;
2363+
2364+ expect ( yield * expectRetained ( ) ) . toBe ( "aaaa\nx名😀yz\n" ) ;
2365+ // The cap starts inside 名, so the tail begins at the next code point.
2366+ expect ( retainedTerminalHistory ( "aaaa\nx名😀yz\n" , maxHistoryBytes ) ) . toBe ( "😀yz\n" ) ;
2367+ process . emitData ( "after\n" ) ;
2368+ expect ( yield * full ) . toEqual ( yield * bounded ) ;
2369+
2370+ yield * publishPastOutputBudget ( manager , process ) ;
2371+ expect ( yield * expectRetained ( ) ) . toMatch ( / l a s t \n $ / ) ;
2372+
2373+ process . emitExit ( { exitCode : 0 , signal : 0 } ) ;
2374+ expect ( yield * full ) . toEqual ( yield * bounded ) ;
2375+ yield * manager . attachStream ( { ...openInput ( ) , restartIfNotRunning : true } , ( ) => Effect . void ) ;
2376+ yield * expectRetained ( ) ;
2377+ } ) . pipe ( Effect . scoped ) ,
2378+ ) ;
2379+
23282380 it . effect ( "does not replay in-flight output already included in an observer snapshot" , ( ) =>
23292381 Effect . gen ( function * ( ) {
23302382 const { manager, ptyAdapter } = yield * createManager ( ) ;
0 commit comments