@@ -4,7 +4,7 @@ import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
44import { FSUtil } from "@opencode-ai/core/fs-util"
55import { Npm } from "@opencode-ai/core/npm"
66import { httpClient } from "@opencode-ai/core/effect/app-node-platform"
7- import { Effect , Fiber , Layer } from "effect"
7+ import { Deferred , Effect , Fiber , Layer } from "effect"
88import { HttpClient } from "effect/unstable/http"
99import { GlobalBus , type GlobalEvent } from "@/bus/global"
1010import { Account } from "@/account/account"
@@ -23,7 +23,22 @@ import { NpmTest } from "../fake/npm"
2323import { tmpdirScoped } from "../fixture/fixture"
2424import { awaitWithTimeout , testEffect } from "../lib/effect"
2525
26- const noopBootstrap = Layer . succeed ( InstanceBootstrap . Service , InstanceBootstrap . Service . of ( { run : Effect . void } ) )
26+ let bootstrapRun : Effect . Effect < void > = Effect . void
27+ const noopBootstrap = Layer . succeed (
28+ InstanceBootstrap . Service ,
29+ InstanceBootstrap . Service . of ( { run : Effect . suspend ( ( ) => bootstrapRun ) } ) ,
30+ )
31+
32+ const setBootstrap = ( run : Effect . Effect < void > ) =>
33+ Effect . acquireRelease (
34+ Effect . sync ( ( ) => {
35+ bootstrapRun = run
36+ } ) ,
37+ ( ) =>
38+ Effect . sync ( ( ) => {
39+ bootstrapRun = Effect . void
40+ } ) ,
41+ )
2742const unexpectedHttp = HttpClient . make ( ( request ) =>
2843 Effect . die ( `unexpected http request: ${ request . method } ${ request . url } ` ) ,
2944)
@@ -99,4 +114,40 @@ describe("reloadWhenSessionsIdle", () => {
99114 expect ( yield * store . load ( { directory : dir } ) ) . not . toBe ( ctx )
100115 } ) ,
101116 )
117+
118+ it . live ( "waits for a session on an instance that loaded while another was still booting" , ( ) =>
119+ Effect . gen ( function * ( ) {
120+ const slow = yield * tmpdirScoped ( { git : true } )
121+ const fast = yield * tmpdirScoped ( { git : true } )
122+ const store = yield * InstanceStore . Service
123+ const status = yield * SessionStatus . Service
124+ const started = yield * Deferred . make < void > ( )
125+ const release = yield * Deferred . make < void > ( )
126+ yield * setBootstrap (
127+ Effect . gen ( function * ( ) {
128+ if ( ( yield * InstanceRef ) ?. directory !== slow ) return
129+ yield * Deferred . succeed ( started , undefined )
130+ yield * Deferred . await ( release )
131+ } ) ,
132+ )
133+ const disposed = yield * collectGlobalDisposed ( )
134+
135+ yield * store . load ( { directory : slow } ) . pipe ( Effect . forkScoped ( { startImmediately : true } ) )
136+ yield * Deferred . await ( started )
137+ const reload = yield * reloadWhenSessionsIdle ( ) . pipe ( Effect . forkScoped ( { startImmediately : true } ) )
138+ const ctx = yield * store . load ( { directory : fast } )
139+ yield * status . set ( sessionID , { type : "busy" } ) . pipe ( Effect . provideService ( InstanceRef , ctx ) )
140+ yield * Deferred . succeed ( release , undefined )
141+
142+ yield * Effect . sleep ( "600 millis" )
143+ expect ( disposed ) . toHaveLength ( 0 )
144+ expect ( yield * store . load ( { directory : fast } ) ) . toBe ( ctx )
145+
146+ yield * status . set ( sessionID , { type : "idle" } ) . pipe ( Effect . provideService ( InstanceRef , ctx ) )
147+ yield * awaitWithTimeout ( Fiber . join ( reload ) , "reload did not run after the session went idle" )
148+
149+ expect ( disposed ) . toHaveLength ( 1 )
150+ expect ( yield * store . load ( { directory : fast } ) ) . not . toBe ( ctx )
151+ } ) ,
152+ )
102153} )
0 commit comments