@@ -12733,6 +12733,198 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1273312733 ),
1273412734 );
1273512735
12736+ it.effect("keeps an idle-launched nested subagent completed through a sibling's wake", () =>
12737+ Effect.scoped(
12738+ Effect.gen(function* () {
12739+ const OUTER_TASK_ID = "task-16355-outer";
12740+ const OUTER_TOOL_USE_ID = "toolu-16355-outer";
12741+ const NESTED_TASK_ID = "task-16355-nested";
12742+ const NESTED_TOOL_USE_ID = "toolu-16355-nested";
12743+ const SIBLING_TASK_ID = "task-16355-sibling";
12744+ const SIBLING_TOOL_USE_ID = "toolu-16355-sibling";
12745+ const harness = yield* makeWakeHarness;
12746+ const now = yield* DateTime.now;
12747+ const nestedStatuses = () =>
12748+ harness.events.flatMap((event) =>
12749+ event.type === "subagent.updated" &&
12750+ event.subagent.nativeTaskRef?.nativeId === NESTED_TASK_ID
12751+ ? [event.subagent.status]
12752+ : [],
12753+ );
12754+ yield* harness.runtime.startTurn(
12755+ makeClaudeTestTurnInput({
12756+ threadId: harness.threadId,
12757+ providerThread: harness.providerThread,
12758+ now,
12759+ attemptId: RunAttemptId.make("attempt-claude-16355-1"),
12760+ text: "Run two audits in the background.",
12761+ attachments: [],
12762+ }),
12763+ );
12764+ for (const frame of [
12765+ makeSubagentTaskStartedFrame({
12766+ taskId: OUTER_TASK_ID,
12767+ toolUseId: OUTER_TOOL_USE_ID,
12768+ uuid: "00000000-0000-4000-8000-000000016301",
12769+ }),
12770+ makeSubagentTaskStartedFrame({
12771+ taskId: SIBLING_TASK_ID,
12772+ toolUseId: SIBLING_TOOL_USE_ID,
12773+ uuid: "00000000-0000-4000-8000-000000016302",
12774+ }),
12775+ makeResultFrame({
12776+ uuid: "00000000-0000-4000-8000-000000016303",
12777+ result: "Both audits run in the background.",
12778+ }),
12779+ ]) {
12780+ yield* Queue.offer(harness.sdkMessages, frame);
12781+ }
12782+ yield* harness.awaitTerminal(1);
12783+
12784+ // The background subagent launches a foreground nested one while the root is idle.
12785+ for (const frame of [
12786+ claudeSdkFrame({
12787+ type: "assistant",
12788+ message: {
12789+ model: "claude-sonnet-4-6",
12790+ id: "msg_16355_nested_launch",
12791+ type: "message",
12792+ role: "assistant",
12793+ content: [
12794+ {
12795+ type: "tool_use",
12796+ id: NESTED_TOOL_USE_ID,
12797+ name: "Agent",
12798+ input: { description: "Nested check", prompt: "Sleep, then reply NESTED_OK" },
12799+ },
12800+ ],
12801+ },
12802+ parent_tool_use_id: OUTER_TOOL_USE_ID,
12803+ uuid: "00000000-0000-4000-8000-000000016304",
12804+ session_id: WAKE_NATIVE_SESSION,
12805+ }),
12806+ claudeSdkFrame({
12807+ type: "system",
12808+ subtype: "task_started",
12809+ task_id: NESTED_TASK_ID,
12810+ tool_use_id: NESTED_TOOL_USE_ID,
12811+ description: "Nested check",
12812+ subagent_type: "general-purpose",
12813+ is_backgrounded: false,
12814+ spawn_depth: 2,
12815+ task_type: "local_agent",
12816+ prompt: "Sleep, then reply NESTED_OK",
12817+ uuid: "00000000-0000-4000-8000-000000016305",
12818+ session_id: WAKE_NATIVE_SESSION,
12819+ }),
12820+ claudeSdkFrame({
12821+ type: "system",
12822+ subtype: "task_progress",
12823+ task_id: NESTED_TASK_ID,
12824+ description: "Sleeping",
12825+ uuid: "00000000-0000-4000-8000-000000016306",
12826+ session_id: WAKE_NATIVE_SESSION,
12827+ }),
12828+ ]) {
12829+ yield* harness.offerAndWait(frame);
12830+ }
12831+
12832+ // A user turn runs while the nested and then the outer subagent finish.
12833+ yield* harness.runtime.startTurn(
12834+ makeClaudeTestTurnInput({
12835+ threadId: harness.threadId,
12836+ providerThread: harness.providerThread,
12837+ now,
12838+ attemptId: RunAttemptId.make("attempt-claude-16355-2"),
12839+ text: "Sleep for a while.",
12840+ attachments: [],
12841+ providerTurnOrdinal: 2,
12842+ }),
12843+ );
12844+ yield* harness.until(() => harness.offeredMessages.length === 2);
12845+ for (const frame of [
12846+ makeSubagentNotificationFrame({
12847+ taskId: NESTED_TASK_ID,
12848+ toolUseId: NESTED_TOOL_USE_ID,
12849+ summary: "NESTED_OK",
12850+ uuid: "00000000-0000-4000-8000-000000016307",
12851+ }),
12852+ makeSubagentToolResultFrame({
12853+ parentToolUseId: OUTER_TOOL_USE_ID,
12854+ uuid: "00000000-0000-4000-8000-000000016308",
12855+ toolUseId: NESTED_TOOL_USE_ID,
12856+ }),
12857+ ...makeSubagentAssistantFrames({
12858+ parentToolUseId: OUTER_TOOL_USE_ID,
12859+ uuid: "00000000-0000-4000-8000-000000016309",
12860+ text: "OUTER_DONE",
12861+ }),
12862+ makeSubagentNotificationFrame({
12863+ taskId: OUTER_TASK_ID,
12864+ toolUseId: OUTER_TOOL_USE_ID,
12865+ summary: "OUTER_DONE",
12866+ uuid: "00000000-0000-4000-8000-000000016310",
12867+ }),
12868+ makeResultFrame({
12869+ uuid: "00000000-0000-4000-8000-000000016311",
12870+ result: "Slept.",
12871+ }),
12872+ ]) {
12873+ yield* Queue.offer(harness.sdkMessages, frame);
12874+ }
12875+ yield* harness.awaitTerminal(2);
12876+
12877+ // The sibling's completion wakes a continuation while the root is idle.
12878+ for (const frame of [
12879+ makeSubagentNotificationFrame({
12880+ taskId: SIBLING_TASK_ID,
12881+ toolUseId: SIBLING_TOOL_USE_ID,
12882+ summary: "SIBLING_DONE",
12883+ uuid: "00000000-0000-4000-8000-000000016312",
12884+ }),
12885+ makeAssistantTextFrame({
12886+ uuid: "00000000-0000-4000-8000-000000016313",
12887+ text: "Both audits finished.",
12888+ }),
12889+ makeResultFrame({
12890+ uuid: "00000000-0000-4000-8000-000000016314",
12891+ result: "Both audits finished.",
12892+ origin: { kind: "task-notification" },
12893+ }),
12894+ ]) {
12895+ yield* Queue.offer(harness.sdkMessages, frame);
12896+ }
12897+ yield* harness.awaitContinuation(1);
12898+ yield* harness.runtime.startTurn(
12899+ makeClaudeTestTurnInput({
12900+ threadId: harness.threadId,
12901+ providerThread: harness.providerThread,
12902+ now,
12903+ attemptId: RunAttemptId.make("attempt-claude-16355-3"),
12904+ text: "Background task completed.",
12905+ attachments: [],
12906+ providerTurnOrdinal: 3,
12907+ messageCreatedBy: "agent",
12908+ messageCreationSource: "provider",
12909+ }),
12910+ );
12911+ yield* harness.awaitTerminal(3);
12912+
12913+ assert.equal(nestedStatuses().at(-1), "completed");
12914+ assert.isFalse(yield* harness.hasPendingBackgroundWork);
12915+ }).pipe(
12916+ Effect.provide(
12917+ Layer.mergeAll(
12918+ IdAllocator.layer,
12919+ McpProviderSessions.layer,
12920+ NodeServices.layer,
12921+ NodeCrypto.layer,
12922+ ),
12923+ ),
12924+ ),
12925+ ),
12926+ );
12927+
1273612928 it.effect.each(["exit", "Stop"] as const)(
1273712929 "settles a plain background subagent and its open call on %s after the root turn completes",
1273812930 (reason) =>
0 commit comments