@@ -302,6 +302,9 @@ interface CreateManagerOptions {
302302 > ;
303303 subprocessPollIntervalMs ?: number ;
304304 processKillGraceMs ?: number ;
305+ registerTerminalProcesses ?: Parameters <
306+ typeof TerminalManager . makeWithOptions
307+ > [ 0 ] [ "registerTerminalProcesses" ] ;
305308 maxRetainedInactiveSessions ?: number ;
306309 historyByteLimit ?: number ;
307310 ptyAdapter ?: FakePtyAdapter ;
@@ -352,6 +355,9 @@ const createManager = (
352355 ? { subprocessPollIntervalMs : options . subprocessPollIntervalMs }
353356 : { } ) ,
354357 processKillGraceMs : options . processKillGraceMs ?? 1 ,
358+ ...( options . registerTerminalProcesses !== undefined
359+ ? { registerTerminalProcesses : options . registerTerminalProcesses }
360+ : { } ) ,
355361 ...( options . maxRetainedInactiveSessions !== undefined
356362 ? { maxRetainedInactiveSessions : options . maxRetainedInactiveSessions }
357363 : { } ) ,
@@ -1225,47 +1231,143 @@ it.layer(
12251231 } ) ,
12261232 ) ;
12271233
1228- it . effect ( "emits subprocess activity events when child-process state changes " , ( ) =>
1234+ it . effect ( "skips snapshots only when every terminal is quiet and wakes on input or output " , ( ) =>
12291235 Effect . gen ( function * ( ) {
1230- let inspect : {
1231- readonly hasRunningSubprocess : boolean ;
1232- readonly childCommand : string | null ;
1233- readonly processIds : ReadonlyArray < number > ;
1234- } = { hasRunningSubprocess : false , childCommand : null , processIds : [ ] } ;
1235- const { manager, getEvents } = yield * createManager ( 5 , {
1236- subprocessInspector : ( ) => Effect . succeed ( inspect ) ,
1237- subprocessPollIntervalMs : 20 ,
1238- } ) ;
1239-
1236+ let snapshots = 0 ;
1237+ let processes : Array < { pid : number ; ppid : number ; name : string } > = [ ] ;
1238+ let duringSnapshot : Effect . Effect < void > = Effect . void ;
1239+ const { manager, ptyAdapter, getEvents } = yield * createManager ( 5 , {
1240+ processKillGraceMs : 0 ,
1241+ subprocessPollIntervalMs : 1_000 ,
1242+ processTable : Effect . gen ( function * ( ) {
1243+ snapshots += 1 ;
1244+ const snapshot = processes ;
1245+ yield * duringSnapshot ;
1246+ return snapshot ;
1247+ } ) ,
1248+ } ) . pipe ( Effect . provide ( layerWithHostPlatform ( "linux" ) ) ) ;
1249+ const activity = yield * Queue . unbounded < Extract < TerminalEvent , { type : "activity" } > > ( ) ;
1250+ const output = yield * Deferred . make < void > ( ) ;
1251+ const unsubscribe = yield * manager . subscribe ( ( event ) =>
1252+ event . type === "activity"
1253+ ? Queue . offer ( activity , event ) . pipe ( Effect . asVoid )
1254+ : event . type === "output"
1255+ ? Deferred . succeed ( output , undefined ) . pipe ( Effect . asVoid )
1256+ : Effect . void ,
1257+ ) ;
1258+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( unsubscribe ) ) ;
12401259 yield * manager . open ( openInput ( ) ) ;
1241- expect ( ( yield * getEvents ) . some ( ( event ) => event . type === "activity" ) ) . toBe ( false ) ;
1260+ yield * manager . open ( openInput ( { terminalId : "second" } ) ) ;
1261+ const [ first , second ] = ptyAdapter . processes ;
1262+ const shells = ptyAdapter . processes . map ( ( { pid } ) => ( { pid, ppid : 1 , name : "zsh" } ) ) ;
1263+ processes = shells ;
1264+
1265+ yield * TestClock . adjust ( "1 second" ) ;
1266+ assert . equal ( snapshots , 1 ) ;
1267+ yield * TestClock . adjust ( "1 second" ) ;
1268+ assert . equal ( snapshots , 2 ) ;
1269+ yield * TestClock . adjust ( "3 seconds" ) ;
1270+ assert . equal ( snapshots , 2 ) ;
1271+ expect ( ( yield * getEvents ) . filter ( ( event ) => event . type === "activity" ) ) . toEqual ( [ ] ) ;
1272+
1273+ processes = [ ...shells , { pid : 100 , ppid : second ! . pid , name : "vim" } ] ;
1274+ yield * manager . write ( { threadId : "thread-1" , terminalId : "second" , data : "vim\r" } ) ;
1275+ yield * TestClock . adjust ( "1 second" ) ;
1276+ expect ( yield * Queue . take ( activity ) ) . toMatchObject ( {
1277+ terminalId : "second" ,
1278+ hasRunningSubprocess : true ,
1279+ label : "vim" ,
1280+ } ) ;
1281+ yield * TestClock . adjust ( "2 seconds" ) ;
1282+ assert . equal ( snapshots , 5 ) ;
1283+
1284+ processes = shells ;
1285+ yield * TestClock . adjust ( "1 second" ) ;
1286+ expect ( yield * Queue . take ( activity ) ) . toMatchObject ( {
1287+ terminalId : "second" ,
1288+ hasRunningSubprocess : false ,
1289+ label : "second" ,
1290+ } ) ;
1291+ yield * TestClock . adjust ( "3 seconds" ) ;
1292+ assert . equal ( snapshots , 7 ) ;
1293+
1294+ processes = [ ...shells , { pid : 101 , ppid : first ! . pid , name : "node" } ] ;
1295+ first ! . emitData ( "server started\r\n" ) ;
1296+ yield * Deferred . await ( output ) ;
1297+ yield * TestClock . adjust ( "1 second" ) ;
1298+ expect ( yield * Queue . take ( activity ) ) . toMatchObject ( {
1299+ terminalId : DEFAULT_TERMINAL_ID ,
1300+ hasRunningSubprocess : true ,
1301+ label : "node" ,
1302+ } ) ;
1303+ assert . equal ( snapshots , 8 ) ;
12421304
1243- inspect = { hasRunningSubprocess : true , childCommand : "vim" , processIds : [ 100 , 101 ] } ;
1244- yield * waitFor (
1245- Effect . map ( getEvents , ( events ) =>
1246- events . some (
1247- ( event ) =>
1248- event . type === "activity" &&
1249- event . hasRunningSubprocess === true &&
1250- event . label === "vim" ,
1251- ) ,
1252- ) ,
1253- "1200 millis" ,
1254- ) ;
1305+ processes = shells ;
1306+ duringSnapshot = Effect . gen ( function * ( ) {
1307+ duringSnapshot = Effect . void ;
1308+ yield * manager . write ( {
1309+ threadId : "thread-1" ,
1310+ terminalId : DEFAULT_TERMINAL_ID ,
1311+ data : "next command\r" ,
1312+ } ) ;
1313+ } ) . pipe ( Effect . orDie ) ;
1314+ yield * TestClock . adjust ( "1 second" ) ;
1315+ expect ( yield * Queue . take ( activity ) ) . toMatchObject ( { hasRunningSubprocess : false } ) ;
1316+ // Input during inspection still needs two subsequent unchanged snapshots.
1317+ yield * TestClock . adjust ( "2 seconds" ) ;
1318+ assert . equal ( snapshots , 11 ) ;
1319+ yield * TestClock . adjust ( "3 seconds" ) ;
1320+ assert . equal ( snapshots , 11 ) ;
1321+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1322+ ) ;
12551323
1256- inspect = { hasRunningSubprocess : false , childCommand : null , processIds : [ ] } ;
1257- yield * waitFor (
1258- Effect . map ( getEvents , ( events ) =>
1259- events . some (
1260- ( event ) =>
1261- event . type === "activity" &&
1262- event . hasRunningSubprocess === false &&
1263- event . label === "Terminal 1" ,
1264- ) ,
1265- ) ,
1266- "1200 millis" ,
1324+ it . effect ( "discovers and registers a silent child within 30 quiet ticks" , ( ) =>
1325+ Effect . gen ( function * ( ) {
1326+ let snapshots = 0 ;
1327+ let processes : Array < { pid : number ; ppid : number ; name : string } > = [ ] ;
1328+ const registered = yield * Deferred . make < {
1329+ readonly threadId : string ;
1330+ readonly terminalId : string ;
1331+ readonly processIds : ReadonlyArray < number > ;
1332+ } > ( ) ;
1333+ const { manager, ptyAdapter } = yield * createManager ( 5 , {
1334+ processKillGraceMs : 0 ,
1335+ subprocessPollIntervalMs : 1_000 ,
1336+ processTable : Effect . sync ( ( ) => {
1337+ snapshots += 1 ;
1338+ return processes ;
1339+ } ) ,
1340+ registerTerminalProcesses : ( input ) =>
1341+ input . processIds . includes ( 100 )
1342+ ? Deferred . succeed ( registered , input ) . pipe ( Effect . asVoid )
1343+ : Effect . void ,
1344+ } ) . pipe ( Effect . provide ( layerWithHostPlatform ( "linux" ) ) ) ;
1345+ const activity = yield * Queue . unbounded < Extract < TerminalEvent , { type : "activity" } > > ( ) ;
1346+ const unsubscribe = yield * manager . subscribe ( ( event ) =>
1347+ event . type === "activity" ? Queue . offer ( activity , event ) . pipe ( Effect . asVoid ) : Effect . void ,
12671348 ) ;
1268- } ) ,
1349+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( unsubscribe ) ) ;
1350+ yield * manager . open ( openInput ( ) ) ;
1351+ const shell = ptyAdapter . processes [ 0 ] ! ;
1352+ processes = [ { pid : shell . pid , ppid : 1 , name : "zsh" } ] ;
1353+ yield * TestClock . adjust ( "2 seconds" ) ;
1354+ assert . equal ( snapshots , 2 ) ;
1355+
1356+ processes = [ ...processes , { pid : 100 , ppid : shell . pid , name : "node" } ] ;
1357+ yield * TestClock . adjust ( "30 seconds" ) ;
1358+ assert . equal ( snapshots , 3 ) ;
1359+ assert . isTrue ( yield * Deferred . isDone ( registered ) ) ;
1360+ expect ( yield * Deferred . await ( registered ) ) . toEqual ( {
1361+ threadId : "thread-1" ,
1362+ terminalId : DEFAULT_TERMINAL_ID ,
1363+ processIds : [ shell . pid , 100 ] ,
1364+ } ) ;
1365+ expect ( yield * Queue . take ( activity ) ) . toMatchObject ( {
1366+ terminalId : DEFAULT_TERMINAL_ID ,
1367+ hasRunningSubprocess : true ,
1368+ label : "node" ,
1369+ } ) ;
1370+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
12691371 ) ;
12701372
12711373 it . effect ( "does not invoke subprocess polling until a terminal session is running" , ( ) =>
0 commit comments