@@ -7360,6 +7360,109 @@ describe("ClaudeAdapterV2 background wake turns", () => {
73607360 return { childThreadId, toolThreadIds, assistantTexts };
73617361 };
73627362
7363+ it.effect("keeps the wake run open for the turn Claude opens right after an empty one", () =>
7364+ Effect.scoped(
7365+ Effect.gen(function* () {
7366+ const harness = yield* makeWakeHarness;
7367+ const now = yield* DateTime.now;
7368+ const subagents = [
7369+ { taskId: "task-wake-pair-a", toolUseId: "toolu_wake_pair_a" },
7370+ { taskId: "task-wake-pair-b", toolUseId: "toolu_wake_pair_b" },
7371+ ];
7372+ const wakeAttempt = RunAttemptId.make("attempt-claude-wake-pair-2");
7373+
7374+ yield* harness.runtime.startTurn(
7375+ makeClaudeTestTurnInput({
7376+ threadId: harness.threadId,
7377+ providerThread: harness.providerThread,
7378+ now,
7379+ attemptId: RunAttemptId.make("attempt-claude-wake-pair-1"),
7380+ text: "Audit both in the background.",
7381+ attachments: [],
7382+ }),
7383+ );
7384+ for (const [index, subagent] of subagents.entries()) {
7385+ yield* Queue.offer(
7386+ harness.sdkMessages,
7387+ makeSubagentTaskStartedFrame({
7388+ ...subagent,
7389+ uuid: `00000000-0000-4000-8000-00000000095${index}`,
7390+ }),
7391+ );
7392+ }
7393+ yield* Queue.offer(
7394+ harness.sdkMessages,
7395+ makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000952", result: "Started." }),
7396+ );
7397+ yield* awaitUntil(() => harness.terminalEvents().length === 1, "first turn terminal");
7398+
7399+ // Both subagents end together. Claude runs an empty turn for the first
7400+ // notification and opens the real one a millisecond later, all before
7401+ // the wake run attaches.
7402+ for (const [index, subagent] of subagents.entries()) {
7403+ yield* Queue.offer(
7404+ harness.sdkMessages,
7405+ makeSubagentNotificationFrame({
7406+ ...subagent,
7407+ summary: "AUDITED",
7408+ uuid: `00000000-0000-4000-8000-00000000096${index}`,
7409+ }),
7410+ );
7411+ }
7412+ yield* Queue.offer(harness.sdkMessages, wakeTurnInit);
7413+ yield* Queue.offer(
7414+ harness.sdkMessages,
7415+ makeResultFrame({
7416+ uuid: "00000000-0000-4000-8000-000000000962",
7417+ result: "",
7418+ numTurns: 0,
7419+ origin: { kind: "task-notification" },
7420+ terminalReason: null,
7421+ }),
7422+ );
7423+ yield* harness.offerAndWait(
7424+ claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000963" }),
7425+ );
7426+ assert.lengthOf(harness.continuationRequests, 1);
7427+
7428+ yield* harness.runtime.startTurn(
7429+ makeClaudeTestTurnInput({
7430+ threadId: harness.threadId,
7431+ providerThread: harness.providerThread,
7432+ now,
7433+ attemptId: wakeAttempt,
7434+ text: "Background task completed.",
7435+ attachments: [],
7436+ providerTurnOrdinal: 2,
7437+ messageCreatedBy: "agent",
7438+ messageCreationSource: "provider",
7439+ }),
7440+ );
7441+ // Claude is still thinking, so the run stays working rather than
7442+ // settling with nothing to show.
7443+ assert.lengthOf(harness.terminalEvents(), 1);
7444+
7445+ yield* harness.offerAndWait(wakeAssistant);
7446+ yield* harness.offerAndWait(wakeResult);
7447+ yield* awaitUntil(() => harness.terminalEvents().length === 2, "wake run terminal");
7448+ assert.equal(harness.terminalEvents()[1]?.status, "completed");
7449+ assert.lengthOf(harness.continuationRequests, 1);
7450+ assert.isTrue(
7451+ harness.events.some(
7452+ (event) =>
7453+ event.type === "message.updated" &&
7454+ event.message.runId === RunId.make(`run-${wakeAttempt}`) &&
7455+ event.message.text === WAKE_RESULT_TEXT,
7456+ ),
7457+ );
7458+ }).pipe(
7459+ Effect.provide(
7460+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7461+ ),
7462+ ),
7463+ ),
7464+ );
7465+
73637466 it.effect("a subagent re-run in the foreground does not join a later wake", () =>
73647467 Effect.scoped(
73657468 Effect.gen(function* () {
0 commit comments