@@ -878,6 +878,192 @@ describe("OpenCode2 adapter", () => {
878878 } ) . pipe ( Effect . scoped ) ,
879879 ) ;
880880
881+ /** A `subagent` call's result while its subagent runs on (`session.tool.success` is at version 2). */
882+ const returnedRunning = ( id : string ) : ProviderReplayEntry => ( {
883+ type : "emit_inbound" ,
884+ frame : {
885+ type : "sdk.event" ,
886+ event : {
887+ id : `evt_toolsuccess_${ id } ` ,
888+ created : 1 ,
889+ type : "session.tool.success" ,
890+ data : {
891+ sessionID : SESSION ,
892+ assistantMessageID : "msg_assistant" ,
893+ id,
894+ content : [ { type : "text" , text : "The subagent is working in the background." } ] ,
895+ metadata : { sessionID : CHILD , status : "running" } ,
896+ executed : true ,
897+ } ,
898+ durable : { aggregateID : SESSION , seq : 1 , version : 2 } ,
899+ } ,
900+ } ,
901+ } ) ;
902+ /** The model calls the subagent again by its session; the tool returns while it runs on. */
903+ const callAgain = ( input : Record < string , unknown > ) : ReadonlyArray < ProviderReplayEntry > => {
904+ const tool = { sessionID : SESSION , assistantMessageID : "msg_assistant" , id : "call-again" } ;
905+ return [
906+ event ( "session.tool.input.started" , { ...tool , name : "subagent" } ) ,
907+ event ( "session.tool.called" , {
908+ ...tool ,
909+ name : "subagent" ,
910+ input : { description : "Again" , prompt : "also this" , sessionID : CHILD , ...input } ,
911+ executed : false ,
912+ } ) ,
913+ event ( "session.tool.progress" , {
914+ ...tool ,
915+ metadata : { sessionID : CHILD , status : "running" } ,
916+ } ) ,
917+ // A session called again gets the thread's rules.
918+ out ( "session.update" , { sessionID : CHILD , permissions : "<any>" } ) ,
919+ reply ( "session.update" , null ) ,
920+ returnedRunning ( tool . id ) ,
921+ ] ;
922+ } ;
923+ const childReport = ( inboxID : string , text : string ) =>
924+ event ( "session.inbox.enqueued" , {
925+ inboxID,
926+ sessionID : SESSION ,
927+ item : {
928+ type : "synthetic" ,
929+ payload : {
930+ text : `<subagent sessionID="${ CHILD } " state="completed" description="Sleep">\n${ text } \n</subagent>` ,
931+ description : "Sleep" ,
932+ metadata : { source : "subagent" , childID : CHILD , agent : "General" , state : "completed" } ,
933+ } ,
934+ delivery : "steer" ,
935+ } ,
936+ } ) ;
937+ /** The subagent rows by title, the thread's background roster and the child's turns, as emitted. */
938+ const watchCalls = (
939+ runtime : ProviderAdapterV2SessionRuntime ,
940+ thread : OrchestrationV2ProviderThread ,
941+ ) =>
942+ Effect . gen ( function * ( ) {
943+ const calls = new Map < string , { status : string ; result : string | null } > ( ) ;
944+ const childTurns = new Map < string , string > ( ) ;
945+ const state : { roster : ReadonlyArray < unknown > | undefined } = { roster : undefined } ;
946+ let wake = yield * Deferred . make < void > ( ) ;
947+ yield * runtime . events . pipe (
948+ Stream . tap ( ( event ) =>
949+ Effect . gen ( function * ( ) {
950+ if ( event . type === "subagent.updated" && event . subagent . title !== null ) {
951+ calls . set ( event . subagent . title , {
952+ status : event . subagent . status ,
953+ result : event . subagent . result ,
954+ } ) ;
955+ }
956+ if ( event . type === "provider_thread.updated" && event . providerThread . id === thread . id ) {
957+ state . roster = event . providerThread . pendingBackgroundTasks ;
958+ }
959+ const id =
960+ event . type === "provider_turn.updated"
961+ ? event . providerTurn . nativeTurnRef ?. nativeId
962+ : undefined ;
963+ if (
964+ event . type === "provider_turn.updated" &&
965+ id ?. startsWith ( `${ CHILD } :turn:` ) === true
966+ ) {
967+ childTurns . set ( id , event . providerTurn . status ) ;
968+ }
969+ yield * Deferred . succeed ( wake , undefined ) ;
970+ } ) ,
971+ ) ,
972+ Stream . runDrain ,
973+ Effect . forkScoped ,
974+ ) ;
975+ const until = ( check : ( ) => boolean ) : Effect . Effect < void > =>
976+ Effect . suspend ( ( ) => {
977+ if ( check ( ) ) return Effect . void ;
978+ return Effect . gen ( function * ( ) {
979+ wake = yield * Deferred . make < void > ( ) ;
980+ if ( check ( ) ) return ;
981+ yield * Deferred . await ( wake ) ;
982+ yield * until ( check ) ;
983+ } ) ;
984+ } ) ;
985+ return { calls, childTurns, state, until } ;
986+ } ) ;
987+ const launchedAndRunning = [
988+ ...backgroundLaunch ( CHILD ) ,
989+ returnedRunning ( "call-background" ) ,
990+ event ( "session.execution.started" , { sessionID : CHILD } ) ,
991+ ] ;
992+
993+ it . effect ( "settles every call that joined a running subagent with its one report" , ( ) =>
994+ Effect . gen ( function * ( ) {
995+ const { runtime, thread } = yield * resumed ( [
996+ ...launchedAndRunning ,
997+ // OpenCode steers the running subagent and joins its run: one report answers both calls.
998+ ...callAgain ( { background : true } ) ,
999+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
1000+ event ( "session.execution.succeeded" , { sessionID : CHILD } ) ,
1001+ childReport ( "msg_report" , "CHILD_OK" ) ,
1002+ ] ) ;
1003+ const watch = yield * watchCalls ( runtime , thread ) ;
1004+ yield * runtime . startTurn ( withLineage ( thread ) ) ;
1005+ yield * watch . until (
1006+ ( ) =>
1007+ watch . calls . get ( "Again" ) ?. status === "completed" &&
1008+ watch . calls . get ( "Sleep" ) ?. status === "completed" &&
1009+ watch . state . roster ?. length === 0 ,
1010+ ) ;
1011+ assert . deepEqual ( Object . fromEntries ( watch . calls ) , {
1012+ Sleep : { status : "completed" , result : "CHILD_OK" } ,
1013+ Again : { status : "completed" , result : "CHILD_OK" } ,
1014+ } ) ;
1015+ } ) . pipe ( Effect . scoped ) ,
1016+ ) ;
1017+
1018+ it . effect ( "keeps a foreground call that joined a background run for that run's report" , ( ) =>
1019+ Effect . gen ( function * ( ) {
1020+ const { runtime, thread } = yield * resumed ( [
1021+ ...launchedAndRunning ,
1022+ // Without `background`, the call joins the run in the background all the same.
1023+ ...callAgain ( { } ) ,
1024+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
1025+ event ( "session.execution.succeeded" , { sessionID : CHILD } ) ,
1026+ childReport ( "msg_report" , "CHILD_OK" ) ,
1027+ ] ) ;
1028+ const watch = yield * watchCalls ( runtime , thread ) ;
1029+ yield * runtime . startTurn ( withLineage ( thread ) ) ;
1030+ yield * watch . until (
1031+ ( ) =>
1032+ watch . calls . get ( "Again" ) ?. status === "completed" &&
1033+ watch . calls . get ( "Sleep" ) ?. status === "completed" &&
1034+ watch . state . roster ?. length === 0 ,
1035+ ) ;
1036+ // Not settled empty when the turn that made it ended.
1037+ assert . deepEqual ( watch . calls . get ( "Again" ) , { status : "completed" , result : "CHILD_OK" } ) ;
1038+ } ) . pipe ( Effect . scoped ) ,
1039+ ) ;
1040+
1041+ it . effect ( "keeps a call to a finished subagent for its own run's report" , ( ) =>
1042+ Effect . gen ( function * ( ) {
1043+ const { runtime, thread } = yield * resumed ( [
1044+ ...launchedAndRunning ,
1045+ event ( "session.execution.succeeded" , { sessionID : CHILD } ) ,
1046+ // Called again after it ended but before its report: the call starts a new run.
1047+ ...callAgain ( { background : true } ) ,
1048+ childReport ( "msg_report_1" , "FIRST_OK" ) ,
1049+ event ( "session.execution.started" , { sessionID : CHILD } ) ,
1050+ event ( "session.execution.succeeded" , { sessionID : CHILD } ) ,
1051+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
1052+ childReport ( "msg_report_2" , "SECOND_OK" ) ,
1053+ ] ) ;
1054+ const watch = yield * watchCalls ( runtime , thread ) ;
1055+ yield * runtime . startTurn ( withLineage ( thread ) ) ;
1056+ // The new run's turn starts after the first report was handled.
1057+ yield * watch . until ( ( ) => watch . childTurns . has ( `${ CHILD } :turn:2` ) ) ;
1058+ assert . deepEqual ( Object . fromEntries ( watch . calls ) , {
1059+ Sleep : { status : "completed" , result : "FIRST_OK" } ,
1060+ Again : { status : "running" , result : null } ,
1061+ } ) ;
1062+ yield * watch . until ( ( ) => watch . calls . get ( "Again" ) ?. status === "completed" ) ;
1063+ assert . deepEqual ( watch . calls . get ( "Again" ) , { status : "completed" , result : "SECOND_OK" } ) ;
1064+ } ) . pipe ( Effect . scoped ) ,
1065+ ) ;
1066+
8811067 it . effect ( "leaves no background work pending once a reconnect finds its subagent gone" , ( ) =>
8821068 Effect . gen ( function * ( ) {
8831069 // The subagent ends while the stream is down: its end, its report and
0 commit comments