@@ -8311,6 +8311,222 @@ describe("ClaudeAdapterV2 background wake turns", () => {
83118311 } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
83128312 ) ;
83138313
8314+ // Level entries from Claude Code 2.1.292 carry no ambient flag.
8315+ const backgroundTasksLevel = (
8316+ uuid : string ,
8317+ tasks : ReadonlyArray < { readonly taskId : string ; readonly taskType : string } > ,
8318+ ) =>
8319+ claudeSdkFrame ( {
8320+ type : "system" ,
8321+ subtype : "background_tasks_changed" ,
8322+ tasks : tasks . map ( ( task ) => ( {
8323+ task_id : task . taskId ,
8324+ task_type : task . taskType ,
8325+ description : "Background task" ,
8326+ } ) ) ,
8327+ uuid,
8328+ session_id : WAKE_NATIVE_SESSION ,
8329+ } as unknown as SDKMessage ) ;
8330+ // Frame order captured from Claude Code 2.1.292: the Agent call, the
8331+ // level, the backgrounded task_started, its async ack, the model's reply,
8332+ // then the result while the subagent still runs.
8333+ const backgroundAgentLaunchFrames = ( input : {
8334+ readonly prefix : string ;
8335+ readonly taskId : string ;
8336+ } ) => {
8337+ const toolUseId = `toolu-${ input . prefix } ` ;
8338+ return [
8339+ claudeSdkFrame ( {
8340+ type : "assistant" ,
8341+ message : {
8342+ model : "claude-sonnet-4-6" ,
8343+ id : `msg-${ input . prefix } -launch` ,
8344+ type : "message" ,
8345+ role : "assistant" ,
8346+ content : [
8347+ {
8348+ type : "tool_use" ,
8349+ id : toolUseId ,
8350+ name : "Agent" ,
8351+ input : {
8352+ description : "Implement the fix" ,
8353+ subagent_type : "general-purpose" ,
8354+ run_in_background : true ,
8355+ prompt : "Implement the fix." ,
8356+ } ,
8357+ } ,
8358+ ] ,
8359+ } ,
8360+ parent_tool_use_id : null ,
8361+ uuid : `${ input . prefix } -launch` ,
8362+ session_id : WAKE_NATIVE_SESSION ,
8363+ } ) ,
8364+ backgroundTasksLevel ( `${ input . prefix } -level` , [
8365+ { taskId : input . taskId , taskType : "local_agent" } ,
8366+ ] ) ,
8367+ claudeSdkFrame ( {
8368+ type : "system" ,
8369+ subtype : "task_started" ,
8370+ task_id : input . taskId ,
8371+ tool_use_id : toolUseId ,
8372+ description : "Implement the fix" ,
8373+ subagent_type : "general-purpose" ,
8374+ is_backgrounded : true ,
8375+ task_type : "local_agent" ,
8376+ prompt : "Implement the fix." ,
8377+ uuid : `${ input . prefix } -task-started` ,
8378+ session_id : WAKE_NATIVE_SESSION ,
8379+ } ) ,
8380+ claudeSdkFrame ( {
8381+ type : "user" ,
8382+ message : {
8383+ role : "user" ,
8384+ content : [
8385+ {
8386+ type : "tool_result" ,
8387+ tool_use_id : toolUseId ,
8388+ content : [ { type : "text" , text : "Async agent launched successfully." } ] ,
8389+ } ,
8390+ ] ,
8391+ } ,
8392+ parent_tool_use_id : null ,
8393+ uuid : `${ input . prefix } -ack` ,
8394+ session_id : WAKE_NATIVE_SESSION ,
8395+ } ) ,
8396+ ] ;
8397+ } ;
8398+
8399+ it . effect ( "defers goal completion while a background subagent runs" , ( ) =>
8400+ Effect . gen ( function * ( ) {
8401+ const harness = yield * makeWakeHarness ;
8402+ const condition = "both reviewers find no bugs" ;
8403+ const taskId = "task-goal-background-subagent" ;
8404+ const now = yield * DateTime . now ;
8405+ yield * harness . runtime . startTurn (
8406+ makeClaudeTestTurnInput ( {
8407+ threadId : harness . threadId ,
8408+ providerThread : harness . providerThread ,
8409+ now,
8410+ attemptId : RunAttemptId . make ( "goal-background-attempt" ) ,
8411+ text : `/goal ${ condition } ` ,
8412+ attachments : [ ] ,
8413+ } ) ,
8414+ ) ;
8415+ for ( const frame of [
8416+ syntheticFrame ( "goal-background-set" , `Goal set: ${ condition } ` ) ,
8417+ ...backgroundAgentLaunchFrames ( { prefix : "goal-background" , taskId } ) ,
8418+ makeAssistantTextFrame ( {
8419+ uuid : "goal-background-wait" ,
8420+ text : "Waiting for the background subagent before round 1." ,
8421+ } ) ,
8422+ makeResultFrame ( {
8423+ uuid : "goal-background-result" ,
8424+ result : "Waiting for the background subagent before round 1." ,
8425+ } ) ,
8426+ ] ) {
8427+ yield * Queue . offer ( harness . sdkMessages , frame ) ;
8428+ }
8429+ yield * Queue . take ( harness . terminalReceipts ) ;
8430+ // Claude skips the goal check while the subagent runs.
8431+ assert . equal ( goalStatuses ( harness . events ) . at ( - 1 ) ?. status , "active" ) ;
8432+
8433+ // The subagent ends; Claude opens a turn on its own, and the goal
8434+ // check at its end passes.
8435+ yield * harness . offerAndWait (
8436+ claudeSdkFrame ( {
8437+ type : "system" ,
8438+ subtype : "task_notification" ,
8439+ task_id : taskId ,
8440+ tool_use_id : "toolu-goal-background" ,
8441+ status : "completed" ,
8442+ output_file : `/tmp/${ taskId } .output` ,
8443+ summary : "Fix implemented." ,
8444+ uuid : "goal-background-notification" ,
8445+ session_id : WAKE_NATIVE_SESSION ,
8446+ } ) ,
8447+ ) ;
8448+ yield * harness . offerAndWait ( backgroundTasksLevel ( "goal-background-level-empty" , [ ] ) ) ;
8449+ yield * harness . offerAndWait ( wakeTurnInit ) ;
8450+ assert . lengthOf ( harness . continuationRequests , 1 ) ;
8451+ yield * harness . runtime . startTurn (
8452+ makeClaudeTestTurnInput ( {
8453+ threadId : harness . threadId ,
8454+ providerThread : harness . providerThread ,
8455+ now,
8456+ attemptId : RunAttemptId . make ( "goal-background-wake" ) ,
8457+ text : "Background task completed." ,
8458+ attachments : [ ] ,
8459+ providerTurnOrdinal : 2 ,
8460+ messageCreatedBy : "agent" ,
8461+ messageCreationSource : "provider" ,
8462+ } ) ,
8463+ ) ;
8464+ yield * harness . offerAndWait (
8465+ makeAssistantTextFrame ( { uuid : "goal-background-wake-work" , text : "No bugs found." } ) ,
8466+ ) ;
8467+ yield * harness . offerAndWait (
8468+ makeResultFrame ( {
8469+ uuid : "goal-background-wake-result" ,
8470+ result : "No bugs found." ,
8471+ origin : { kind : "task-notification" } ,
8472+ } ) ,
8473+ ) ;
8474+ yield * Queue . take ( harness . terminalReceipts ) ;
8475+ assert . deepEqual ( goalStatuses ( harness . events ) . at ( - 1 ) , {
8476+ objective : condition ,
8477+ status : "complete" ,
8478+ checks : 0 ,
8479+ } ) ;
8480+ } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
8481+ ) ;
8482+
8483+ // Claude's goal check waits on agents, workflows and shells by task type,
8484+ // ambient or not, and never on mcp_task or monitor_mcp/monitor_ws tasks.
8485+ // A task with no type is treated as one Claude waits on.
8486+ it . effect . each ( [
8487+ { taskType : "mcp_task" , ambient : false , status : "complete" } ,
8488+ { taskType : "local_agent" , ambient : true , status : "active" } ,
8489+ { taskType : undefined , ambient : false , status : "active" } ,
8490+ ] as const ) ( "treats a running $taskType (ambient $ambient) as goal $status" , ( input ) =>
8491+ Effect . gen ( function * ( ) {
8492+ const harness = yield * makeWakeHarness ;
8493+ const condition = "the docs build" ;
8494+ const taskId = `task-goal-${ input . taskType } ` ;
8495+ yield * harness . runtime . startTurn (
8496+ makeClaudeTestTurnInput ( {
8497+ threadId : harness . threadId ,
8498+ providerThread : harness . providerThread ,
8499+ now : yield * DateTime . now ,
8500+ attemptId : RunAttemptId . make ( `goal-${ input . taskType } -attempt` ) ,
8501+ text : `/goal ${ condition } ` ,
8502+ attachments : [ ] ,
8503+ } ) ,
8504+ ) ;
8505+ for ( const frame of [
8506+ syntheticFrame ( `goal-${ input . taskType } -set` , `Goal set: ${ condition } ` ) ,
8507+ backgroundTasksLevel ( `goal-${ input . taskType } -level` , [
8508+ { taskId, taskType : input . taskType ?? "local_agent" } ,
8509+ ] ) ,
8510+ claudeSdkFrame ( {
8511+ type : "system" ,
8512+ subtype : "task_started" ,
8513+ task_id : taskId ,
8514+ description : "Watch the docs" ,
8515+ ...( input . taskType === undefined ? { } : { task_type : input . taskType } ) ,
8516+ ...( input . ambient ? { skip_transcript : true , ambient : true } : { } ) ,
8517+ uuid : `goal-${ input . taskType } -task-started` ,
8518+ session_id : WAKE_NATIVE_SESSION ,
8519+ } ) ,
8520+ makeAssistantTextFrame ( { uuid : `goal-${ input . taskType } -work` , text : "The docs build." } ) ,
8521+ makeResultFrame ( { uuid : `goal-${ input . taskType } -result` , result : "The docs build." } ) ,
8522+ ] ) {
8523+ yield * Queue . offer ( harness . sdkMessages , frame ) ;
8524+ }
8525+ yield * Queue . take ( harness . terminalReceipts ) ;
8526+ assert . equal ( goalStatuses ( harness . events ) . at ( - 1 ) ?. status , input . status ) ;
8527+ } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
8528+ ) ;
8529+
83148530 it . effect ( "keeps a goal active after a command turn with no model output" , ( ) =>
83158531 Effect . gen ( function * ( ) {
83168532 const harness = yield * makeWakeHarness ;
0 commit comments