@@ -610,6 +610,28 @@ describe("OpenCode2 adapter", () => {
610610 lineage : { parentThreadId : null , relationshipToParent : null , rootThreadId : threadId } ,
611611 } as OrchestrationV2AppThread ,
612612 } ) ;
613+ /**
614+ * The continuation turn the orchestrator starts for an offered wake: its
615+ * message is the one the offer named, as `ProviderContinuationService` dispatches it.
616+ */
617+ const continuationTurn = (
618+ thread : OrchestrationV2ProviderThread ,
619+ offer : ProviderContinuationRequest ,
620+ suffix = "wake" ,
621+ ) => ( {
622+ ...withLineage ( thread ) ,
623+ runId : RunId . make ( `run:opencode2-adapter:${ suffix } ` ) ,
624+ runOrdinal : 2 ,
625+ providerTurnOrdinal : 2 ,
626+ attemptId : RunAttemptId . make ( `attempt:opencode2-adapter:${ suffix } ` ) ,
627+ message : {
628+ ...turnInput ( thread ) . message ,
629+ messageId : offer . messageId ! ,
630+ text : offer . detail ?? "" ,
631+ createdBy : "agent" as const ,
632+ creationSource : "provider" as const ,
633+ } ,
634+ } ) ;
613635 const CHILD = "ses_f1485c529ffea4URrYruwEg0Ja" ;
614636 /**
615637 * The single reader of the runtime's events: resolves `attached` once the
@@ -3762,19 +3784,7 @@ describe("OpenCode2 adapter", () => {
37623784 yield * Effect . gen ( function * ( ) {
37633785 while ( offers . length === 0 ) yield * Effect . yieldNow ;
37643786 } ) . pipe ( Effect . timeout ( "2 seconds" ) , Effect . orDie ) ;
3765- yield * runtime . startTurn ( {
3766- ...withLineage ( thread ) ,
3767- runId : RunId . make ( "run:opencode2-adapter:wake" ) ,
3768- runOrdinal : 2 ,
3769- providerTurnOrdinal : 2 ,
3770- attemptId : RunAttemptId . make ( "attempt:opencode2-adapter:wake" ) ,
3771- message : {
3772- ...turnInput ( thread ) . message ,
3773- messageId : MessageId . make ( "message:opencode2-adapter:wake" ) ,
3774- createdBy : "agent" as const ,
3775- creationSource : "provider" as const ,
3776- } ,
3777- } ) ;
3787+ yield * runtime . startTurn ( continuationTurn ( thread , offers [ 0 ] ! ) ) ;
37783788 yield * Deferred . await ( bothEnded ) ;
37793789 const [ first , second ] = turns ;
37803790 return { runtime, thread, first : first ! , second : second ! } ;
@@ -3841,6 +3851,107 @@ describe("OpenCode2 adapter", () => {
38413851 } ) . pipe ( Effect . scoped ) ,
38423852 ) ;
38433853
3854+ it . effect ( "replays only its own background reply into a continuation turn" , ( ) =>
3855+ Effect . gen ( function * ( ) {
3856+ const CHILD_B = "ses_f1485c529ffeBBBBBBBBBBBBBB" ;
3857+ const toolB = { sessionID : SESSION , assistantMessageID : "msg_assistant" , id : "call-b" } ;
3858+ const report = ( inboxID : string , child : string , text : string ) =>
3859+ event ( "session.inbox.enqueued" , {
3860+ inboxID,
3861+ sessionID : SESSION ,
3862+ item : {
3863+ type : "synthetic" ,
3864+ payload : {
3865+ text : `<subagent sessionID="${ child } " state="completed" description="Sleep">\n${ text } \n</subagent>` ,
3866+ description : "Sleep" ,
3867+ metadata : {
3868+ source : "subagent" ,
3869+ childID : child ,
3870+ agent : "General" ,
3871+ state : "completed" ,
3872+ } ,
3873+ } ,
3874+ delivery : "steer" ,
3875+ } ,
3876+ } ) ;
3877+ const reply = ( assistantMessageID : string , text : string ) =>
3878+ event ( "session.text.ended" , { sessionID : SESSION , assistantMessageID, ordinal : 0 , text } ) ;
3879+ const offers : Array < ProviderContinuationRequest > = [ ] ;
3880+ const { runtime, thread } = yield * resumed ( [
3881+ ...backgroundLaunch ( CHILD ) ,
3882+ // The same turn starts a second background subagent.
3883+ event ( "session.tool.input.started" , { ...toolB , name : "subagent" } ) ,
3884+ event ( "session.tool.called" , {
3885+ ...toolB ,
3886+ name : "subagent" ,
3887+ input : { description : "Sleep" , prompt : "sleep" , background : true } ,
3888+ executed : false ,
3889+ } ) ,
3890+ event ( "session.created" , { ...childCreated ( CHILD_B ) , sessionID : CHILD_B } ) ,
3891+ event ( "session.tool.progress" , {
3892+ ...toolB ,
3893+ metadata : { sessionID : CHILD_B , status : "running" } ,
3894+ } ) ,
3895+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
3896+ // Each subagent's report wakes the parent into a reply OpenCode runs on its own.
3897+ event ( "session.execution.succeeded" , { sessionID : CHILD } ) ,
3898+ report ( "msg_report_a" , CHILD , "A_OK" ) ,
3899+ event ( "session.execution.started" , { sessionID : SESSION } ) ,
3900+ event ( "session.inbox.delivered" , { sessionID : SESSION , inboxID : "msg_report_a" } ) ,
3901+ reply ( "msg_reply_a" , "REPLY_A" ) ,
3902+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
3903+ event ( "session.execution.succeeded" , { sessionID : CHILD_B } ) ,
3904+ report ( "msg_report_b" , CHILD_B , "B_OK" ) ,
3905+ event ( "session.execution.started" , { sessionID : SESSION } ) ,
3906+ event ( "session.inbox.delivered" , { sessionID : SESSION , inboxID : "msg_report_b" } ) ,
3907+ reply ( "msg_reply_b" , "REPLY_B" ) ,
3908+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
3909+ ] ) . pipe (
3910+ Effect . provideService ( ProviderContinuationRequests . ProviderContinuationRequests , {
3911+ offer : ( request ) => Effect . sync ( ( ) => void offers . push ( request ) ) ,
3912+ take : Effect . never ,
3913+ } ) ,
3914+ ) ;
3915+ const collected : Array < ProviderAdapterV2Event > = [ ] ;
3916+ const ended = yield * Deferred . make < void > ( ) ;
3917+ yield * runtime . events . pipe (
3918+ Stream . tap ( ( event ) =>
3919+ Effect . gen ( function * ( ) {
3920+ collected . push ( event ) ;
3921+ if ( collected . filter ( ( entry ) => entry . type === "turn.terminal" ) . length === 2 ) {
3922+ yield * Deferred . succeed ( ended , undefined ) ;
3923+ }
3924+ } ) ,
3925+ ) ,
3926+ Stream . runDrain ,
3927+ Effect . forkScoped ,
3928+ ) ;
3929+ yield * runtime . startTurn ( withLineage ( thread ) ) ;
3930+ yield * Effect . gen ( function * ( ) {
3931+ while ( offers . length < 2 ) yield * Effect . yieldNow ;
3932+ } ) . pipe ( Effect . timeout ( "2 seconds" ) , Effect . orDie ) ;
3933+ // A's continuation was cancelled before it started; B's runs.
3934+ yield * runtime . startTurn ( continuationTurn ( thread , offers [ 1 ] ! , "wake-b" ) ) ;
3935+ yield * Deferred . await ( ended ) ;
3936+ const continuationId = ( yield * IdAllocator . IdAllocatorV2 ) . derive . providerTurn ( {
3937+ driver : OPENCODE_PROVIDER ,
3938+ nativeTurnId : `${ SESSION } :attempt:attempt:opencode2-adapter:wake-b` ,
3939+ } ) ;
3940+ assert . deepEqual (
3941+ collected . flatMap ( ( event ) =>
3942+ event . type === "turn_item.updated" &&
3943+ event . turnItem . type === "assistant_message" &&
3944+ event . turnItem . providerTurnId === continuationId
3945+ ? [ event . turnItem . text ]
3946+ : [ ] ,
3947+ ) ,
3948+ [ "REPLY_B" ] ,
3949+ ) ;
3950+ // Nothing is left held for a continuation that will never run.
3951+ assert . isFalse ( yield * runtime . hasPendingBackgroundWork ! ) ;
3952+ } ) . pipe ( Effect . scoped , Effect . provide ( IdAllocator . layer ) ) ,
3953+ ) ;
3954+
38443955 it . effect ( "ends a follow-up turn a steer joined exactly once" , ( ) =>
38453956 Effect . gen ( function * ( ) {
38463957 const reportText = `<subagent sessionID="${ CHILD } " state="completed" description="Sleep">\nCHILD_OK\n</subagent>` ;
@@ -3918,19 +4029,7 @@ describe("OpenCode2 adapter", () => {
39184029 yield * Effect . gen ( function * ( ) {
39194030 while ( offers . length === 0 ) yield * Effect . yieldNow ;
39204031 } ) . pipe ( Effect . timeout ( "2 seconds" ) , Effect . orDie ) ;
3921- const followUp = {
3922- ...withLineage ( thread ) ,
3923- runId : RunId . make ( "run:opencode2-adapter:wake" ) ,
3924- runOrdinal : 2 ,
3925- providerTurnOrdinal : 2 ,
3926- attemptId : RunAttemptId . make ( "attempt:opencode2-adapter:wake" ) ,
3927- message : {
3928- ...turnInput ( thread ) . message ,
3929- messageId : MessageId . make ( "message:opencode2-adapter:wake" ) ,
3930- createdBy : "agent" as const ,
3931- creationSource : "provider" as const ,
3932- } ,
3933- } ;
4032+ const followUp = continuationTurn ( thread , offers [ 0 ] ! ) ;
39344033 yield * runtime . startTurn ( followUp ) ;
39354034 const followUpTurnId = ( yield * IdAllocator . IdAllocatorV2 ) . derive . providerTurn ( {
39364035 driver : OPENCODE_PROVIDER ,
0 commit comments