@@ -326,6 +326,7 @@ const resumed = (
326326 readonly resolveExtractedTextPath ?: ( path : string ) => Effect . Effect < string | undefined > ;
327327 readonly fileSystem ?: FileSystem . FileSystem ;
328328 readonly onPrompt ?: ( input : unknown ) => void ;
329+ readonly borrowers ?: { current : number } ;
329330 } ,
330331) =>
331332 Effect . gen ( function * ( ) {
@@ -352,6 +353,7 @@ const resumed = (
352353 ] ,
353354 {
354355 ...( options ?. version === undefined ? { } : { version : options . version } ) ,
356+ ...( options ?. borrowers === undefined ? { } : { borrowers : options . borrowers } ) ,
355357 ...( options ?. fileSystem === undefined ? { } : { fileSystem : options . fileSystem } ) ,
356358 ...( options ?. onPrompt === undefined ? { } : { onPrompt : options . onPrompt } ) ,
357359 ...( options ?. external === undefined ? { } : { external : options . external } ) ,
@@ -3000,7 +3002,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30003002 } ) . pipe ( Effect . scoped ) ,
30013003 ) ;
30023004
3003- it . effect . each ( [ "history" , "list" ] as const ) (
3005+ it . effect . each ( [ "history" , "list" , "late-preview" ] as const ) (
30043006 "recovers native image job outcomes from %s after reconnect" ,
30053007 ( source ) =>
30063008 Effect . gen ( function * ( ) {
@@ -3026,14 +3028,14 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30263028 } ,
30273029 ] ,
30283030 } ) ;
3029- if ( source === "list ")
3031+ if ( source !== "history ")
30303032 frames . splice ( 2 , 0 , out ( "rpc.call" , "<any>" ) , reply ( "rpc.call" , { output : null } ) ) ;
30313033 return frames ;
30323034 } ) ( ) ,
30333035 out ( "message.list" , { sessionID : SESSION , type : "synthetic" , limit : "100" } ) ,
30343036 reply ( "message.list" , {
30353037 data :
3036- source === "list "
3038+ source !== "history "
30373039 ? [ ]
30383040 : [
30393041 {
@@ -3052,7 +3054,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30523054 ] ,
30533055 cursor : { } ,
30543056 } ) ,
3055- ...( source === "list "
3057+ ...( source !== "history "
30563058 ? [
30573059 out ( "imageJobs" , { sessionID : SESSION , kind : "image" } ) ,
30583060 reply ( "imageJobs" , {
@@ -3061,7 +3063,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30613063 kind : "image" ,
30623064 id : "image_gap" ,
30633065 sessionID : SESSION ,
3064- status : "error" ,
3066+ status : source === "late-preview" ? "completed" : "error" ,
30653067 path : "/tmp/image" ,
30663068 model : "test/image" ,
30673069 time : { started : 2 , completed : 3 } ,
@@ -3070,15 +3072,45 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30703072 } ) ,
30713073 ]
30723074 : [ ] ) ,
3075+ ...( source === "late-preview"
3076+ ? [
3077+ event ( "session.inbox.enqueued" , {
3078+ sessionID : SESSION ,
3079+ inboxID : "msg_image_late" ,
3080+ item : {
3081+ type : "synthetic" ,
3082+ delivery : "steer" ,
3083+ payload : {
3084+ description : "Image generation" ,
3085+ text : "Image request finished. Saved image: /tmp/image.png (test/image)." ,
3086+ metadata : {
3087+ source : "experimental_image_generate" ,
3088+ jobID : "image_gap" ,
3089+ state : "completed" ,
3090+ path : "/tmp/image" ,
3091+ } ,
3092+ } ,
3093+ } ,
3094+ } ) ,
3095+ out ( "image.read" , {
3096+ path : "/api/fs/read/image.png" ,
3097+ "location[directory]" : "/tmp" ,
3098+ } ) ,
3099+ reply ( "image.read" , { imageBase64 : "iVBORw0KGgo=" } ) ,
3100+ ]
3101+ : [ ] ) ,
30733102 ] ,
3074- source === "list " ? { version : "2.2.6" } : undefined ,
3103+ { ... ( source !== "history " ? { version : "2.2.6" as const } : { } ) , external : true } ,
30753104 ) ;
30763105 const events = yield * runtime . events . pipe (
30773106 Stream . takeUntil (
30783107 ( e ) =>
30793108 e . type === "turn_item.updated" &&
30803109 e . turnItem . nativeItemRef ?. nativeId === "call-image-gap" &&
3081- e . turnItem . status === "failed" ,
3110+ ( source === "late-preview"
3111+ ? e . turnItem . type === "dynamic_tool" &&
3112+ toolOutputImages ( e . turnItem . output ) . length > 0
3113+ : e . turnItem . status === "failed" ) ,
30823114 ) ,
30833115 Stream . runCollect ,
30843116 Effect . forkScoped ,
@@ -3087,7 +3119,11 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30873119 const collected = yield * Fiber . join ( events ) ;
30883120 assert . include (
30893121 JSON . stringify ( collected . at ( - 1 ) ) ,
3090- source === "list" ? "Image generation error" : "Recovered image failure" ,
3122+ source === "late-preview"
3123+ ? "Saved image: /tmp/image.png"
3124+ : source !== "history"
3125+ ? "Image generation error"
3126+ : "Recovered image failure" ,
30913127 ) ;
30923128 assert . isTrue (
30933129 collected . some (
@@ -3099,6 +3135,168 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30993135 } ) . pipe ( Effect . scoped ) ,
31003136 ) ;
31013137
3138+ it . effect ( "settles a recovered image outcome when preview I/O is interrupted" , ( ) =>
3139+ Effect . gen ( function * ( ) {
3140+ const reading = yield * Deferred . make < void > ( ) ;
3141+ const interrupted = yield * Deferred . make < void > ( ) ;
3142+ const fs = yield * FileSystem . FileSystem ;
3143+ const { runtime, thread } = yield * resumed (
3144+ [
3145+ out ( "session.prompt" , { sessionID : SESSION , id : PROMPT_ID , text : "<any>" } ) ,
3146+ promptAccepted ,
3147+ ...reconnected ( { } , "succeeded" , {
3148+ content : [
3149+ {
3150+ type : "tool" ,
3151+ id : "call-image-slow" ,
3152+ name : "experimental_image_generate" ,
3153+ executed : false ,
3154+ state : {
3155+ status : "completed" ,
3156+ input : { prompt : "A tree" } ,
3157+ content : [ { type : "text" , text : "Launched" } ] ,
3158+ metadata : { jobID : "image_slow" , path : "/tmp/image" , model : "test/image" } ,
3159+ } ,
3160+ time : { created : 2 , ran : 2 , completed : 2 } ,
3161+ } ,
3162+ ] ,
3163+ } ) ,
3164+ out ( "message.list" , { sessionID : SESSION , type : "synthetic" , limit : "100" } ) ,
3165+ reply ( "message.list" , {
3166+ data : [
3167+ {
3168+ id : "msg_image_slow" ,
3169+ type : "synthetic" ,
3170+ time : { created : 3 } ,
3171+ text : "Image request finished. Saved image: /tmp/image.png (test/image)." ,
3172+ description : "Image generation" ,
3173+ metadata : {
3174+ source : "experimental_image_generate" ,
3175+ jobID : "image_slow" ,
3176+ state : "completed" ,
3177+ path : "/tmp/image" ,
3178+ } ,
3179+ } ,
3180+ ] ,
3181+ cursor : { } ,
3182+ } ) ,
3183+ event ( "session.model.selected" , {
3184+ sessionID : SESSION ,
3185+ model : { providerID : "opencode" , id : "big-pickle" , variant : "recovery-observed" } ,
3186+ } ) ,
3187+ ] ,
3188+ {
3189+ fileSystem : {
3190+ ...fs ,
3191+ stat : ( path ) =>
3192+ path === "/tmp/image.png"
3193+ ? Deferred . succeed ( reading , undefined ) . pipe (
3194+ Effect . andThen ( Effect . never ) ,
3195+ Effect . onInterrupt ( ( ) => Deferred . succeed ( interrupted , undefined ) ) ,
3196+ )
3197+ : fs . stat ( path ) ,
3198+ } ,
3199+ } ,
3200+ ) ;
3201+ const events = yield * runtime . events . pipe (
3202+ Stream . takeUntil (
3203+ ( e ) =>
3204+ e . type === "provider_thread.updated" &&
3205+ e . providerThread . nativeMetadata ?. modelSelection ?. options ?. some (
3206+ ( option ) => option . value === "recovery-observed" ,
3207+ ) === true ,
3208+ ) ,
3209+ Stream . runCollect ,
3210+ Effect . forkScoped ,
3211+ ) ;
3212+ yield * runtime . startTurn ( turnInput ( thread ) ) ;
3213+ yield * Deferred . await ( reading ) ;
3214+ yield * TestClock . adjust ( "5 seconds" ) ;
3215+ yield * Deferred . await ( interrupted ) ;
3216+ const collected = yield * Fiber . join ( events ) ;
3217+ assert . isTrue (
3218+ collected . some (
3219+ ( e ) =>
3220+ e . type === "turn_item.updated" &&
3221+ e . turnItem . nativeItemRef ?. nativeId === "call-image-slow" &&
3222+ e . turnItem . status === "completed" ,
3223+ ) ,
3224+ ) ;
3225+ assert . isFalse ( yield * runtime . hasPendingBackgroundWorkForThread ! ( thread ) ) ;
3226+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
3227+ ) ;
3228+
3229+ it . effect ( "settles native image tasks when reconnect attempts fail" , ( ) =>
3230+ Effect . gen ( function * ( ) {
3231+ const attempts = yield * Effect . forEach ( [ 0 , 1 , 2 , 3 , 4 ] , ( ) => Deferred . make < void > ( ) ) ;
3232+ let count = 0 ;
3233+ let failures = 0 ;
3234+ const borrowers = {
3235+ get current ( ) {
3236+ return count ;
3237+ } ,
3238+ set current ( next : number ) {
3239+ if ( next < count && count === 2 ) {
3240+ const attempt = attempts [ failures ++ ] ;
3241+ if ( attempt !== undefined ) Deferred . doneUnsafe ( attempt , Effect . void ) ;
3242+ }
3243+ count = next ;
3244+ } ,
3245+ } ;
3246+ const tool = {
3247+ sessionID : SESSION ,
3248+ assistantMessageID : "msg_assistant" ,
3249+ id : "call-image-lost" ,
3250+ } ;
3251+ const { runtime, thread } = yield * resumed (
3252+ [
3253+ out ( "session.prompt" , { sessionID : SESSION , text : "<any>" } ) ,
3254+ promptAccepted ,
3255+ event ( "session.tool.input.started" , { ...tool , name : "experimental_image_generate" } ) ,
3256+ event (
3257+ "session.tool.success" ,
3258+ {
3259+ ...tool ,
3260+ content : [ { type : "text" , text : "Launched" } ] ,
3261+ metadata : { jobID : "image_lost" , path : "/tmp/image" , model : "test/image" } ,
3262+ executed : false ,
3263+ } ,
3264+ undefined ,
3265+ 2 ,
3266+ ) ,
3267+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
3268+ { type : "runtime_exit" , status : "success" } ,
3269+ ] ,
3270+ { borrowers } ,
3271+ ) ;
3272+ const events = yield * runtime . events . pipe ( Stream . runCollect , Effect . forkScoped ) ;
3273+ yield * runtime . startTurn ( turnInput ( thread ) ) ;
3274+ yield * Deferred . await ( attempts [ 0 ] ! ) ;
3275+ for ( const attempt of attempts . slice ( 1 ) ) {
3276+ yield * TestClock . adjust ( "2 seconds" ) ;
3277+ yield * Deferred . await ( attempt ) ;
3278+ }
3279+ const collected = yield * Fiber . join ( events ) ;
3280+ assert . isFalse ( yield * runtime . hasPendingBackgroundWorkForThread ! ( thread ) ) ;
3281+ assert . isTrue (
3282+ collected . some (
3283+ ( e ) =>
3284+ e . type === "turn_item.updated" &&
3285+ e . turnItem . nativeItemRef ?. nativeId === tool . id &&
3286+ e . turnItem . status === "failed" ,
3287+ ) ,
3288+ ) ;
3289+ assert . deepEqual (
3290+ collected
3291+ . flatMap ( ( e ) =>
3292+ e . type === "provider_thread.updated" ? [ e . providerThread . pendingBackgroundTasks ] : [ ] ,
3293+ )
3294+ . at ( - 1 ) ,
3295+ [ ] ,
3296+ ) ;
3297+ } ) . pipe ( Effect . scoped ) ,
3298+ ) ;
3299+
31023300 it . effect ( "projects successful native todo updates into one checklist" , ( ) =>
31033301 Effect . gen ( function * ( ) {
31043302 const updates = [
0 commit comments