@@ -13766,6 +13766,161 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1376613766 ),
1376713767 );
1376813768
13769+ it.effect("offers a later wake after an earlier wake's continuation dispatch failed", () =>
13770+ Effect.scoped(
13771+ Effect.gen(function* () {
13772+ const SECOND_TASK_ID = "b2ndwake";
13773+ const SECOND_TOOL_USE_ID = "toolu_second_wake";
13774+ const SECOND_SUMMARY = 'Background command "Second sleep" completed (exit code 0)';
13775+ const harness = yield* makeWakeHarness;
13776+ const now = yield* DateTime.now;
13777+
13778+ yield* harness.runtime.startTurn(
13779+ makeClaudeTestTurnInput({
13780+ threadId: harness.threadId,
13781+ providerThread: harness.providerThread,
13782+ now,
13783+ attemptId: RunAttemptId.make("attempt-claude-wake-dispatch-failed-1"),
13784+ text: "Run two builds in the background.",
13785+ attachments: [],
13786+ }),
13787+ );
13788+ yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
13789+ yield* Queue.offer(
13790+ harness.sdkMessages,
13791+ claudeSdkFrame({
13792+ ...wakeTaskStarted,
13793+ task_id: SECOND_TASK_ID,
13794+ tool_use_id: SECOND_TOOL_USE_ID,
13795+ description: "Second sleep",
13796+ uuid: "00000000-0000-4000-8000-000000000121",
13797+ }),
13798+ );
13799+ yield* Queue.offer(harness.sdkMessages, turnOneResult);
13800+ yield* harness.awaitTerminal(1);
13801+
13802+ yield* harness.offerAndWait(wakeNotification);
13803+ yield* harness.offerAndWait(wakeTurnInit);
13804+ assert.lengthOf(harness.continuationRequests, 1);
13805+ yield* harness.offerAndWait(wakeAssistant);
13806+ yield* harness.offerAndWait(wakeResult);
13807+
13808+ // A failed dispatch never attaches the wake turn; clearIfCurrent is the only release.
13809+ const failed = harness.continuationRequests[0]!;
13810+ assert.isDefined(failed.clearIfCurrent);
13811+ yield* failed.clearIfCurrent!();
13812+
13813+ yield* harness.offerAndWait(
13814+ claudeSdkFrame({
13815+ ...wakeNotification,
13816+ task_id: SECOND_TASK_ID,
13817+ tool_use_id: SECOND_TOOL_USE_ID,
13818+ output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
13819+ summary: SECOND_SUMMARY,
13820+ uuid: "00000000-0000-4000-8000-000000000123",
13821+ }),
13822+ );
13823+ yield* harness.offerAndWait(
13824+ claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000124" }),
13825+ );
13826+ assert.lengthOf(harness.continuationRequests, 2);
13827+ }).pipe(
13828+ Effect.provide(
13829+ Layer.mergeAll(
13830+ IdAllocator.layer,
13831+ McpProviderSessions.layer,
13832+ NodeServices.layer,
13833+ NodeCrypto.layer,
13834+ ),
13835+ ),
13836+ ),
13837+ ),
13838+ );
13839+
13840+ it.effect("keeps a newer wake latched when a stale continuation request is cleared", () =>
13841+ Effect.scoped(
13842+ Effect.gen(function* () {
13843+ const SECOND_TASK_ID = "b2ndwake";
13844+ const SECOND_TOOL_USE_ID = "toolu_second_wake";
13845+ const harness = yield* makeWakeHarness;
13846+ const now = yield* DateTime.now;
13847+
13848+ yield* harness.runtime.startTurn(
13849+ makeClaudeTestTurnInput({
13850+ threadId: harness.threadId,
13851+ providerThread: harness.providerThread,
13852+ now,
13853+ attemptId: RunAttemptId.make("attempt-claude-wake-stale-clear-1"),
13854+ text: "Run two builds in the background.",
13855+ attachments: [],
13856+ }),
13857+ );
13858+ yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
13859+ yield* Queue.offer(
13860+ harness.sdkMessages,
13861+ claudeSdkFrame({
13862+ ...wakeTaskStarted,
13863+ task_id: SECOND_TASK_ID,
13864+ tool_use_id: SECOND_TOOL_USE_ID,
13865+ description: "Second sleep",
13866+ uuid: "00000000-0000-4000-8000-000000000131",
13867+ }),
13868+ );
13869+ yield* Queue.offer(harness.sdkMessages, turnOneResult);
13870+ yield* harness.awaitTerminal(1);
13871+
13872+ yield* harness.offerAndWait(wakeNotification);
13873+ yield* harness.offerAndWait(wakeTurnInit);
13874+ assert.lengthOf(harness.continuationRequests, 1);
13875+ yield* harness.runtime.startTurn(
13876+ makeClaudeTestTurnInput({
13877+ threadId: harness.threadId,
13878+ providerThread: harness.providerThread,
13879+ now,
13880+ attemptId: RunAttemptId.make("attempt-claude-wake-stale-clear-2"),
13881+ text: "Background task completed.",
13882+ attachments: [],
13883+ providerTurnOrdinal: 2,
13884+ messageCreatedBy: "agent",
13885+ messageCreationSource: "provider",
13886+ }),
13887+ );
13888+ yield* harness.offerAndWait(wakeAssistant);
13889+ yield* harness.offerAndWait(wakeResult);
13890+ yield* harness.awaitTerminal(2);
13891+
13892+ yield* harness.offerAndWait(
13893+ claudeSdkFrame({
13894+ ...wakeNotification,
13895+ task_id: SECOND_TASK_ID,
13896+ tool_use_id: SECOND_TOOL_USE_ID,
13897+ output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
13898+ uuid: "00000000-0000-4000-8000-000000000133",
13899+ }),
13900+ );
13901+ yield* harness.offerAndWait(
13902+ claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000134" }),
13903+ );
13904+ assert.lengthOf(harness.continuationRequests, 2);
13905+
13906+ yield* harness.continuationRequests[0]!.clearIfCurrent!();
13907+ yield* harness.offerAndWait(
13908+ claudeSdkFrame({ ...wakeAssistant, uuid: "00000000-0000-4000-8000-000000000135" }),
13909+ );
13910+ assert.lengthOf(harness.continuationRequests, 2);
13911+ }).pipe(
13912+ Effect.provide(
13913+ Layer.mergeAll(
13914+ IdAllocator.layer,
13915+ McpProviderSessions.layer,
13916+ NodeServices.layer,
13917+ NodeCrypto.layer,
13918+ ),
13919+ ),
13920+ ),
13921+ ),
13922+ );
13923+
1376913924 it.effect.each([true, false])(
1377013925 "carries a rejected wake rate limit into the continuation failure (event=%s)",
1377113926 (standaloneEvent) =>
0 commit comments