@@ -142,6 +142,11 @@ const promptAccepted = replyData("session.prompt", {
142142 delivery : "steer" ,
143143} ) ;
144144
145+ const emptyShellInventory : ReadonlyArray < ProviderReplayEntry > = [
146+ out ( "shell.list" , "<any>" ) ,
147+ reply ( "shell.list" , { location : { directory : WORK } , data : [ ] } ) ,
148+ ] ;
149+
145150const noOpenRequests : ReadonlyArray < ProviderReplayEntry > = [
146151 out ( "permission.list" , { sessionID : SESSION } ) ,
147152 replyData ( "permission.list" , [ ] ) ,
@@ -317,7 +322,8 @@ const colorForm = {
317322const resumed = (
318323 entries : ReadonlyArray < ProviderReplayEntry > ,
319324 options ?: {
320- readonly version ?: "2.2.5" ;
325+ readonly version ?: "2.2.1" | "2.2.5" | "2.2.6" ;
326+ readonly shellRecovery ?: ReadonlyArray < ProviderReplayEntry > ;
321327 readonly goal ?: OrchestrationV2ProviderThread [ "goal" ] ;
322328 readonly external ?: boolean ;
323329 readonly supervised ?: boolean ;
@@ -335,9 +341,16 @@ const resumed = (
335341 out ( "session.get" , { sessionID : SESSION } ) ,
336342 replyData ( "session.get" , sessionInfo ( ) ) ,
337343 ...noOpenRequests ,
338- ...( options ?. version === "2.2.5"
344+ ...( options ?. version === "2.2.5" || options ?. version === "2.2.6"
339345 ? [ out ( "rpc.call" , "<any>" ) , reply ( "rpc.call" , { output : null } ) ]
340346 : [ ] ) ,
347+ ...( options ?. shellRecovery ??
348+ ( options ?. version === undefined
349+ ? [ ]
350+ : [
351+ out ( "shell.list" , "<any>" ) ,
352+ reply ( "shell.list" , { location : { directory : WORK } , data : [ ] } ) ,
353+ ] ) ) ,
341354 ...withInstructions ( [
342355 ...( options ?. supervised === true
343356 ? [
@@ -884,6 +897,193 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
884897 ) ;
885898 return { attached : Deferred . await ( attached ) , terminal : Fiber . join ( terminal ) } ;
886899 } ) ;
900+ it . effect . each ( [ false , true , "thread" ] as const ) (
901+ "tracks native background shells through launch, retained output, stop=%s" ,
902+ ( stop ) =>
903+ Effect . gen ( function * ( ) {
904+ const shellID = "sh_background" ;
905+ const tool = {
906+ sessionID : SESSION ,
907+ assistantMessageID : "msg_assistant" ,
908+ id : "call-shell" ,
909+ } ;
910+ const gate = makeProviderReplayGate ( [ "shell-exit" ] ) ;
911+ const { runtime, thread } = yield * resumed (
912+ [
913+ out ( "session.prompt" , { sessionID : SESSION , text : "<any>" } ) ,
914+ promptAccepted ,
915+ event ( "session.execution.started" , { sessionID : SESSION } ) ,
916+ event ( "session.tool.input.started" , { ...tool , name : "shell" } ) ,
917+ event ( "session.tool.called" , {
918+ ...tool ,
919+ name : "shell" ,
920+ input : { command : "echo retained" , background : true } ,
921+ executed : false ,
922+ } ) ,
923+ event (
924+ "session.tool.success" ,
925+ {
926+ ...tool ,
927+ content : [ { type : "text" , text : "Launched" } ] ,
928+ metadata : { shellID } ,
929+ executed : false ,
930+ } ,
931+ undefined ,
932+ 2 ,
933+ ) ,
934+ out ( "shell.output" , "<any>" ) ,
935+ reply ( "shell.output" , {
936+ location : { directory : WORK } ,
937+ data : { output : "retained\n" , cursor : 9 , size : 9 , truncated : false } ,
938+ } ) ,
939+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
940+ ...( stop
941+ ? [ ]
942+ : [ event ( "shell.exited" , { id : shellID , exit : 0 , status : "exited" } , "shell-exit" ) ] ) ,
943+ out ( "shell.output" , "<any>" ) ,
944+ reply ( "shell.output" , {
945+ location : { directory : WORK } ,
946+ data : { output : "" , cursor : 9 , size : 9 , truncated : false } ,
947+ } ) ,
948+ ...( stop
949+ ? [
950+ out ( "shell.remove" , { id : shellID , "location[directory]" : WORK } ) ,
951+ event ( "shell.exited" , { id : shellID , exit : 143 , status : "exited" } ) ,
952+ reply ( "shell.remove" , null ) ,
953+ ]
954+ : [ ] ) ,
955+ ] ,
956+ { replayGate : gate } ,
957+ ) ;
958+ const items : Array < import ( "@t3tools/contracts" ) . OrchestrationV2TurnItem > = [ ] ;
959+ const terminal = yield * runtime . events . pipe (
960+ Stream . tap ( ( e ) =>
961+ Effect . sync ( ( ) => {
962+ if ( e . type === "turn_item.updated" ) items . push ( e . turnItem ) ;
963+ } ) ,
964+ ) ,
965+ Stream . filter ( ( e ) => e . type === "turn.terminal" ) ,
966+ Stream . runHead ,
967+ Effect . forkScoped ,
968+ ) ;
969+ yield * runtime . startTurn ( withLineage ( thread ) ) ;
970+ yield * Fiber . join ( terminal ) ;
971+ yield * Effect . logWarning ( "shell terminal received" ) ;
972+ assert . isTrue ( yield * runtime . hasPendingBackgroundWorkForThread ! ( thread ) ) ;
973+ const settled = yield * runtime . events . pipe (
974+ Stream . tap ( ( e ) =>
975+ Effect . sync ( ( ) => {
976+ if ( e . type === "turn_item.updated" ) items . push ( e . turnItem ) ;
977+ } ) ,
978+ ) ,
979+ Stream . filter (
980+ ( e ) =>
981+ e . type === "provider_thread.updated" &&
982+ e . providerThread . pendingBackgroundTasks ?. length === 0 ,
983+ ) ,
984+ Stream . runHead ,
985+ Effect . forkScoped ,
986+ ) ;
987+ if ( stop === "thread" )
988+ yield * runtime . interruptTurn ( {
989+ providerThread : thread ,
990+ providerTurnId : yield * providerTurnId ,
991+ requestRuntimeRestart : true ,
992+ } ) ;
993+ else if ( stop ) yield * runtime . stopTask ! ( { providerThread : thread , taskId : shellID } ) ;
994+ else gate . release ( "shell-exit" ) ;
995+ yield * Effect . logWarning ( "shell settling" ) ;
996+ yield * Fiber . join ( settled ) ;
997+ yield * Effect . logWarning ( "shell settled" ) ;
998+ assert . isFalse ( yield * runtime . hasPendingBackgroundWorkForThread ! ( thread ) ) ;
999+ assert . deepInclude ( items . filter ( ( i ) => i . type === "command_execution" ) . at ( - 1 ) , {
1000+ type : "command_execution" ,
1001+ status : stop ? "interrupted" : "completed" ,
1002+ output : "retained\n" ,
1003+ } ) ;
1004+ assert . isFalse ( items . some ( ( i ) => i . type === "subagent" ) ) ;
1005+ } ) . pipe ( Effect . scoped ) ,
1006+ ) ;
1007+
1008+ it . effect . each ( [ "shell" , "job" , "fallback" , "descendant" ] as const ) (
1009+ "recovers only owned native shells using %s inventory" ,
1010+ ( discovery ) =>
1011+ Effect . gen ( function * ( ) {
1012+ const id = "sh_recovered" ;
1013+ const info = {
1014+ id,
1015+ command : "sleep 600" ,
1016+ status : "running" ,
1017+ cwd : WORK ,
1018+ shell : "/bin/sh" ,
1019+ file : "/remote/output" ,
1020+ metadata : { sessionID : SESSION } ,
1021+ time : { started : 1 } ,
1022+ } ;
1023+ const response = ( data : unknown ) => ( { location : { directory : WORK } , data } ) ;
1024+ const inventory : Array < ProviderReplayEntry > =
1025+ discovery === "shell"
1026+ ? [ ]
1027+ : [
1028+ out ( "job.list" , { sessionID : SESSION , kind : "shell" , descendants : "true" } ) ,
1029+ reply (
1030+ "job.list" ,
1031+ discovery === "fallback"
1032+ ? { status : 404 , body : { } }
1033+ : {
1034+ data : [
1035+ {
1036+ kind : "shell" ,
1037+ shellID : id ,
1038+ sessionID : discovery === "descendant" ? "ses_descendant" : SESSION ,
1039+ command : info . command ,
1040+ status : "running" ,
1041+ } ,
1042+ ] ,
1043+ } ,
1044+ ) ,
1045+ ] ;
1046+ if ( discovery === "shell" || discovery === "fallback" )
1047+ inventory . push (
1048+ out ( "shell.list" , "<any>" ) ,
1049+ reply (
1050+ "shell.list" ,
1051+ response ( [ info , { ...info , id : "sh_other" , metadata : { sessionID : "ses_other" } } ] ) ,
1052+ ) ,
1053+ ) ;
1054+ inventory . push (
1055+ out ( "shell.get" , { id } ) ,
1056+ reply ( "shell.get" , response ( info ) ) ,
1057+ out ( "shell.output" , { id, cursor : "0" } ) ,
1058+ reply (
1059+ "shell.output" ,
1060+ response ( { output : "retained" , cursor : 8 , size : 8 , truncated : false } ) ,
1061+ ) ,
1062+ ) ;
1063+ const { runtime, thread } = yield * resumed (
1064+ [
1065+ out ( "shell.output" , { id, cursor : "8" } ) ,
1066+ reply ( "shell.output" , response ( { output : "" , cursor : 8 , size : 8 , truncated : false } ) ) ,
1067+ out ( "shell.remove" , { id } ) ,
1068+ reply ( "shell.remove" , null ) ,
1069+ ] ,
1070+ {
1071+ version : discovery === "shell" ? "2.2.1" : "2.2.6" ,
1072+ shellRecovery : inventory ,
1073+ external : true ,
1074+ } ,
1075+ ) ;
1076+ assert . deepEqual ( thread . pendingBackgroundTasks , [
1077+ { taskId : id , kind : "command" , description : info . command } ,
1078+ ] ) ;
1079+ assert . isTrue ( yield * runtime . hasPendingBackgroundWorkForThread ! ( thread ) ) ;
1080+ yield * runtime . stopTask ! ( { providerThread : thread , taskId : "sh_other" } ) ;
1081+ assert . isTrue ( yield * runtime . hasPendingBackgroundWorkForThread ! ( thread ) ) ;
1082+ yield * runtime . stopTask ! ( { providerThread : thread , taskId : id } ) ;
1083+ assert . isFalse ( yield * runtime . hasPendingBackgroundWorkForThread ! ( thread ) ) ;
1084+ } ) . pipe ( Effect . scoped ) ,
1085+ ) ;
1086+
8871087 it . effect ( "keeps background subagents running when a turn is interrupted to restart it" , ( ) =>
8881088 Effect . gen ( function * ( ) {
8891089 const { runtime, thread } = yield * resumed ( [
@@ -2422,6 +2622,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
24222622 ...( delivery === "pending"
24232623 ? [ out ( "rpc.call" , "<any>" ) , reply ( "rpc.call" , { output : { unreadable : true } } ) ]
24242624 : [ ] ) ,
2625+ ...( delivery === "pending" ? emptyShellInventory : [ ] ) ,
24252626 ...reconnect . slice ( 2 ) ,
24262627 ...( delivery === "pending"
24272628 ? [
@@ -5480,6 +5681,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
54805681 input : { sessionID : SESSION } ,
54815682 } ) ,
54825683 reply ( "rpc.call" , { output : nativeGoal ( "paused" ) } ) ,
5684+ ...emptyShellInventory ,
54835685 ] ,
54845686 { version : "2.2.5" } ,
54855687 ) ;
@@ -5524,6 +5726,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
55245726 ...recovered . slice ( 0 , 2 ) ,
55255727 out ( "rpc.call" , "<any>" ) ,
55265728 reply ( "rpc.call" , { output : nativeGoal ( "active" ) } ) ,
5729+ ...emptyShellInventory ,
55275730 ...recovered . slice ( 2 ) ,
55285731 ...noOpenRequests ,
55295732 ]
@@ -5655,6 +5858,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
56555858 ) ,
56565859 ]
56575860 : [ ] ) ,
5861+ ...emptyShellInventory ,
56585862 ...reconnect . slice ( 2 ) ,
56595863 ] ,
56605864 { version : "2.2.5" , replayGate : gate } ,
0 commit comments