@@ -2130,6 +2130,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21302130 const makeWakeHarnessWithOptions = ( options ?: {
21312131 readonly close ?: ( sdkMessages : Queue . Queue < SDKMessage > ) => Effect . Effect < void > ;
21322132 readonly interrupt ?: Effect . Effect < void > ;
2133+ readonly stopTask ?: ( taskId : string ) => Effect . Effect < void > ;
21332134 readonly environment ?: NodeJS . ProcessEnv ;
21342135 readonly settings ?: ClaudeSettings ;
21352136 // A CLI process opened after the first streams from its own queue, so the
@@ -2155,6 +2156,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21552156 const permissionModeChanges : Array < string > = [ ] ;
21562157 const continuationRequests : Array < ProviderContinuationRequests . ProviderContinuationRequest > =
21572158 [ ] ;
2159+ const subagentReceipts =
2160+ yield * Queue . unbounded <
2161+ Extract < ProviderAdapter . ProviderAdapterV2Event , { type : "subagent.updated" } >
2162+ > ( ) ;
21582163 const terminalReceipts =
21592164 yield * Queue . unbounded <
21602165 Extract < ProviderAdapter . ProviderAdapterV2Event , { type : "turn.terminal" } >
@@ -2227,6 +2232,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22272232 Effect . sync ( ( ) => {
22282233 permissionModeChanges . push ( mode ) ;
22292234 } ) ,
2235+ ...( options ?. stopTask === undefined ? { } : { stopTask : options . stopTask } ) ,
22302236 interrupt : options ?. interrupt ?? Effect . void ,
22312237 close : options ?. close ?.( sdkMessages ) ?? Effect . void ,
22322238 } ;
@@ -2253,6 +2259,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22532259 Stream . runForEach ( ( event ) =>
22542260 Effect . gen ( function * ( ) {
22552261 events . push ( event ) ;
2262+ if ( event . type === "subagent.updated" ) yield * Queue . offer ( subagentReceipts , event ) ;
22562263 if ( event . type === "turn.terminal" ) {
22572264 yield * Queue . offer ( terminalReceipts , event ) ;
22582265 }
@@ -2292,6 +2299,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22922299 continuationRequests,
22932300 events,
22942301 terminalReceipts,
2302+ subagentReceipts,
22952303 systemNoticeReceipts,
22962304 getOpenedOptions : ( ) => openedOptions ,
22972305 terminalEvents,
@@ -8076,6 +8084,107 @@ describe("ClaudeAdapterV2 background wake turns", () => {
80768084 ) ,
80778085 ) ;
80788086
8087+ it . effect . each ( [ false , true ] ) (
8088+ "stops one native subagent while its sibling keeps running, owner settled %s" ,
8089+ ( ownerSettled ) =>
8090+ Effect . scoped (
8091+ Effect . gen ( function * ( ) {
8092+ const stopped : string [ ] = [ ] ;
8093+ let closes = 0 ;
8094+ const harness = yield * makeWakeHarnessWithOptions ( {
8095+ stopTask : ( taskId ) =>
8096+ Effect . sync ( ( ) => {
8097+ stopped . push ( taskId ) ;
8098+ } ) ,
8099+ interrupt : Effect . die ( "A child stop must not interrupt its owner" ) ,
8100+ close : ( ) =>
8101+ Effect . sync ( ( ) => {
8102+ closes ++ ;
8103+ } ) ,
8104+ } ) ;
8105+ yield * harness . runtime . startTurn (
8106+ makeClaudeTestTurnInput ( {
8107+ threadId : harness . threadId ,
8108+ providerThread : harness . providerThread ,
8109+ now : yield * DateTime . now ,
8110+ attemptId : RunAttemptId . make ( "attempt-native-child-stop" ) ,
8111+ text : "Start two agents." ,
8112+ attachments : [ ] ,
8113+ } ) ,
8114+ ) ;
8115+ for ( const [ taskId , toolUseId , uuid ] of [
8116+ [ "agent-stop" , "toolu-stop" , "00000000-0000-4000-8000-000000000801" ] ,
8117+ [ "agent-keep" , "toolu-keep" , "00000000-0000-4000-8000-000000000802" ] ,
8118+ ] ) {
8119+ yield * harness . offerAndWait (
8120+ makeSubagentTaskStartedFrame ( { taskId : taskId ! , toolUseId : toolUseId ! , uuid : uuid ! } ) ,
8121+ ) ;
8122+ yield * Queue . take ( harness . subagentReceipts ) ;
8123+ }
8124+ if ( ownerSettled ) {
8125+ yield * harness . offerAndWait (
8126+ makeResultFrame ( {
8127+ uuid : "00000000-0000-4000-8000-000000000803" ,
8128+ result : "Agents are working." ,
8129+ } ) ,
8130+ ) ;
8131+ yield * Queue . take ( harness . terminalReceipts ) ;
8132+ }
8133+ const stop = harness . runtime . stopSubagent ;
8134+ assert . isDefined ( stop ) ;
8135+ yield * stop ! ( { providerThread : harness . providerThread , nativeTaskId : "agent-stop" } ) ;
8136+ assert . deepEqual ( stopped , [ "agent-stop" ] ) ;
8137+ yield * harness . offerAndWait (
8138+ claudeSdkFrame ( {
8139+ ...makeSubagentNotificationFrame ( {
8140+ taskId : "agent-stop" ,
8141+ toolUseId : "toolu-stop" ,
8142+ summary : "Stopped by user" ,
8143+ uuid : "00000000-0000-4000-8000-000000000804" ,
8144+ } ) ,
8145+ status : "stopped" ,
8146+ } ) ,
8147+ ) ;
8148+ if ( ownerSettled ) {
8149+ yield * harness . runtime . startTurn (
8150+ makeClaudeTestTurnInput ( {
8151+ threadId : harness . threadId ,
8152+ providerThread : harness . providerThread ,
8153+ now : yield * DateTime . now ,
8154+ attemptId : RunAttemptId . make ( "attempt-native-child-stop-wake" ) ,
8155+ text : "Background task stopped." ,
8156+ attachments : [ ] ,
8157+ providerTurnOrdinal : 2 ,
8158+ messageCreatedBy : "agent" ,
8159+ messageCreationSource : "provider" ,
8160+ } ) ,
8161+ ) ;
8162+ }
8163+ const stoppedEvent = yield * Queue . take ( harness . subagentReceipts ) ;
8164+ assert . equal ( stoppedEvent . subagent . status , "cancelled" ) ;
8165+ const tasks = harness . events . filter ( ( event ) => event . type === "subagent.updated" ) ;
8166+ assert . equal (
8167+ tasks . findLast ( ( event ) => event . subagent . nativeTaskRef ?. nativeId === "agent-stop" )
8168+ ?. subagent . status ,
8169+ "cancelled" ,
8170+ ) ;
8171+ assert . equal (
8172+ tasks . findLast ( ( event ) => event . subagent . nativeTaskRef ?. nativeId === "agent-keep" )
8173+ ?. subagent . status ,
8174+ "running" ,
8175+ ) ;
8176+ assert . lengthOf ( harness . terminalEvents ( ) , ownerSettled ? 1 : 0 ) ;
8177+ yield * stop ! ( { providerThread : harness . providerThread , nativeTaskId : "agent-stop" } ) ;
8178+ assert . deepEqual ( stopped , [ "agent-stop" ] ) ;
8179+ assert . equal ( closes , 0 ) ;
8180+ } ) . pipe (
8181+ Effect . provide (
8182+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
8183+ ) ,
8184+ ) ,
8185+ ) ,
8186+ ) ;
8187+
80798188 it . effect . each ( [ "requested" , "observed-before" , "observed-after" , "inherit" , "unknown" ] as const ) (
80808189 "records the subagent model from %s without inheriting the parent override" ,
80818190 ( source ) =>
0 commit comments