@@ -564,6 +564,7 @@ interface HarnessOptions {
564564 input : ProjectSetupScriptRunner . ProjectSetupScriptRunnerInput ,
565565 ) => Effect . Effect < void > ;
566566 readonly onCloseIdle ?: ( ) => Effect . Effect < void > ;
567+ readonly onDeviceClose ?: ( hostId : string | undefined ) => Effect . Effect < void > ;
567568 readonly deviceCloseError ?: DeviceHostUnavailableError ;
568569 readonly deviceSessions ?: ReadonlyArray < { readonly hostId : string ; readonly deviceId : string } > ;
569570 readonly currentThreads ?: ReadonlyArray < OrchestrationV2AppThread > ;
@@ -706,6 +707,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
706707 ) ,
707708 close : ( input ) =>
708709 Ref . update ( deviceCloses , ( closes ) => [ ...closes , input ] ) . pipe (
710+ Effect . andThen ( options . onDeviceClose ?.( input . hostId ) ?? Effect . void ) ,
709711 Effect . andThen (
710712 options . deviceCloseError && options . deviceCloseError . hostId === input . hostId
711713 ? Effect . fail ( options . deviceCloseError )
@@ -1507,6 +1509,48 @@ describe("ThreadSettlementServiceV2 terminals", () => {
15071509 ) ,
15081510 ) ;
15091511
1512+ it . effect ( "keeps the remaining devices of a thread re-engaged during an earlier release" , ( ) =>
1513+ Effect . scoped (
1514+ Effect . gen ( function * ( ) {
1515+ const firstStarted = yield * Deferred . make < void > ( ) ;
1516+ const firstFinished = yield * Deferred . make < void > ( ) ;
1517+ const settled = appThread ( "multi-device-thread" , "settled" ) ;
1518+ const marker = appThread ( "marker-thread" , "settled" ) ;
1519+ const currentThreads = [ settled , marker ] ;
1520+ const fixture = yield * makeHarness ( {
1521+ snapshot : makeSnapshot ( [ ] ) ,
1522+ currentThreads,
1523+ deviceSessions : [
1524+ { hostId : "slow-ssh" , deviceId : "pixel" } ,
1525+ { hostId : "local" , deviceId : "emulator-5554" } ,
1526+ ] ,
1527+ onDeviceClose : ( hostId ) =>
1528+ hostId === "slow-ssh"
1529+ ? Deferred . succeed ( firstStarted , undefined ) . pipe (
1530+ Effect . andThen ( Deferred . await ( firstFinished ) ) ,
1531+ )
1532+ : Effect . void ,
1533+ } ) ;
1534+
1535+ yield * Effect . gen ( function * ( ) {
1536+ const service = yield * ThreadSettlementService . ThreadSettlementServiceV2 ;
1537+ yield * startHarness ( service , fixture . activation , fixture . snapshotReads ) ;
1538+ yield * fixture . publishEvent ( settledEvent ( settled ) ) ;
1539+ yield * Deferred . await ( firstStarted ) ;
1540+ currentThreads [ 0 ] = { ...settled , settledOverride : "active" } ;
1541+ yield * Deferred . succeed ( firstFinished , undefined ) ;
1542+ yield * fixture . publishEvent ( settledEvent ( marker ) ) ;
1543+ assert . deepStrictEqual ( yield * Queue . take ( fixture . closedIdle ) , { threadId : settled . id } ) ;
1544+ assert . deepStrictEqual ( yield * Queue . take ( fixture . closedIdle ) , { threadId : marker . id } ) ;
1545+ assert . deepStrictEqual (
1546+ ( yield * Ref . get ( fixture . deviceCloses ) ) . filter ( ( close ) => close . threadId === settled . id ) ,
1547+ [ { threadId : settled . id , hostId : "slow-ssh" , deviceId : "pixel" } ] ,
1548+ ) ;
1549+ } ) . pipe ( Effect . provide ( fixture . layer ) ) ;
1550+ } ) ,
1551+ ) ,
1552+ ) ;
1553+
15101554 it . effect ( "releases a settled thread's devices without shutting them down" , ( ) =>
15111555 Effect . scoped (
15121556 Effect . gen ( function * ( ) {
0 commit comments