@@ -9450,6 +9450,104 @@ describe("ClaudeAdapterV2 background wake turns", () => {
94509450 ),
94519451 );
94529452
9453+ it.effect.each(["buffered", "during drop"] as const)(
9454+ "a dropped wake does not end a subagent that has started again (%s)",
9455+ (startTiming) =>
9456+ Effect.scoped(
9457+ Effect.gen(function* () {
9458+ const harness = yield* makeWakeHarness;
9459+ const taskId = "task-drop-resumed";
9460+ const started = (uuid: string) =>
9461+ makeSubagentTaskStartedFrame({ taskId, toolUseId: uuid, uuid });
9462+ const notification = (uuid: string) =>
9463+ makeSubagentNotificationFrame({ taskId, toolUseId: uuid, uuid, summary: uuid });
9464+ yield* harness.runtime.startTurn(
9465+ makeClaudeTestTurnInput({
9466+ threadId: harness.threadId,
9467+ providerThread: harness.providerThread,
9468+ now: yield* DateTime.now,
9469+ attemptId: RunAttemptId.make("attempt-drop-resumed"),
9470+ text: "Start a background subagent.",
9471+ attachments: [],
9472+ }),
9473+ );
9474+ yield* harness.offerAndWait(started("drop-resumed-start"));
9475+ yield* Queue.take(harness.subagentReceipts);
9476+ yield* harness.offerAndWait(
9477+ makeResultFrame({ uuid: "drop-resumed-root-result", result: "Working in background." }),
9478+ );
9479+ yield* Queue.take(harness.terminalReceipts);
9480+ yield* harness.offerAndWait(notification("drop-resumed-old-end"));
9481+ const oldWake = yield* Queue.take(harness.continuationReceipts);
9482+ assert.isDefined(oldWake.clearIfCurrent);
9483+
9484+ // SendMessage resumes the same task while its earlier outcome still
9485+ // waits for the worker to drop the wake.
9486+ if (startTiming === "buffered") {
9487+ yield* harness.offerAndWait(started("drop-resumed-new-start"));
9488+ yield* oldWake.clearIfCurrent!();
9489+ } else {
9490+ const clock = yield* Clock.Clock;
9491+ const paused = yield* Deferred.make<void>();
9492+ const resume = yield* Deferred.make<void>();
9493+ // The first timestamp read in drop occurs after its atomic buffer
9494+ // removal, before it publishes the subagent's terminal state.
9495+ const dropping = yield* oldWake.clearIfCurrent!().pipe(
9496+ Effect.provideService(Clock.Clock, {
9497+ ...clock,
9498+ currentTimeMillis: Deferred.succeed(paused, undefined).pipe(
9499+ Effect.andThen(Deferred.await(resume)),
9500+ Effect.andThen(clock.currentTimeMillis),
9501+ ),
9502+ }),
9503+ Effect.forkScoped,
9504+ );
9505+ yield* Deferred.await(paused);
9506+ const offered = yield* Deferred.make<void>();
9507+ const restarting = yield* harness
9508+ .offerAndWait(started("drop-resumed-new-start"), offered)
9509+ .pipe(Effect.forkScoped);
9510+ yield* Deferred.await(offered);
9511+ // Let the SDK handler attempt the start while the drop is paused.
9512+ // The shared lock makes it wait until the outcome has been applied.
9513+ yield* Effect.yieldNow;
9514+ yield* Deferred.succeed(resume, undefined);
9515+ yield* Fiber.join(dropping);
9516+ yield* Fiber.join(restarting);
9517+ }
9518+ assert.isTrue(
9519+ yield* harness.hasPendingBackgroundWork,
9520+ "the newer start remains running after the older wake is dropped",
9521+ );
9522+ if (startTiming === "buffered") {
9523+ assert.isFalse(
9524+ harness.events.some(
9525+ (event) =>
9526+ event.type === "subagent.updated" &&
9527+ event.subagent.result === "drop-resumed-old-end",
9528+ ),
9529+ "the superseded notification is not projected",
9530+ );
9531+ } else {
9532+ yield* Queue.take(harness.subagentReceipts);
9533+ }
9534+
9535+ yield* harness.offerAndWait(notification("drop-resumed-new-end"));
9536+ const newWake = yield* Queue.take(harness.continuationReceipts);
9537+ assert.isDefined(newWake.clearIfCurrent);
9538+ yield* newWake.clearIfCurrent!();
9539+ const ended = yield* Queue.take(harness.subagentReceipts);
9540+ assert.equal(ended.subagent.result, "drop-resumed-new-end");
9541+ assert.equal(ended.subagent.status, "completed");
9542+ assert.isFalse(yield* harness.hasPendingBackgroundWork);
9543+ }).pipe(
9544+ Effect.provide(
9545+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9546+ ),
9547+ ),
9548+ ),
9549+ );
9550+
94539551 it.effect("ends buffered subagents when the worker drops their wake continuation", () =>
94549552 Effect.scoped(
94559553 Effect.gen(function* () {
0 commit comments