@@ -17500,6 +17500,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1750017500 const FINISHED_TOOL_USE_ID = "toolu-steer-finished";
1750117501 const calls: Array<string> = [];
1750217502 let beforeAnswer: Effect.Effect<void> = Effect.void;
17503+ const lateAnswer = yield* Deferred.make<void>();
1750317504 const harness = yield* makeWakeHarnessWithOptions({
1750417505 backgroundTasks: (toolUseId) =>
1750517506 Effect.sync(() => calls.push(toolUseId)).pipe(
@@ -17517,7 +17518,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1751717518 : backgrounding === "never answers"
1751817519 ? Effect.never
1751917520 : backgrounding === "answers after the steer went out"
17520- ? Effect.sleep("6 seconds" ).pipe(Effect.as(true))
17521+ ? Deferred.await(lateAnswer ).pipe(Effect.as(true))
1752117522 : Effect.succeed(true),
1752217523 ),
1752317524 ),
@@ -17670,16 +17671,29 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1767017671 ),
1767117672 );
1767217673 }
17674+ // The steer arms its 5-second backgrounding timeout after the request
17675+ // starts on another fiber, so one TestClock.adjust can land before that
17676+ // timer exists and leave the steer waiting forever. Tick until it ends.
17677+ const awaitSteerTimeout = <A, E>(fiber: Fiber.Fiber<A, E>) =>
17678+ Effect.gen(function* () {
17679+ for (let tick = 0; tick < 100 && fiber.pollUnsafe() === undefined; tick++) {
17680+ yield* TestClock.adjust("1 second");
17681+ yield* Effect.yieldNow;
17682+ }
17683+ if (fiber.pollUnsafe() === undefined) {
17684+ return yield* Effect.die("Timed out waiting for the steer.");
17685+ }
17686+ return yield* Fiber.await(fiber);
17687+ });
1767317688 const steering = yield* Effect.forkScoped(steer);
17674- if (
17689+ const timesOut =
1767517690 backgrounding === "never answers" ||
17676- backgrounding === "answers after the steer went out"
17677- ) {
17691+ backgrounding === "answers after the steer went out";
17692+ if (timesOut ) {
1767817693 yield* awaitUntil(() => calls.length === 1, "background request");
1767917694 assert.equal(harness.offeredMessages.length, offered);
17680- yield* TestClock.adjust("5 seconds");
1768117695 }
17682- const steered = yield* Fiber.await(steering);
17696+ const steered = yield* timesOut ? awaitSteerTimeout(steering) : Fiber.await(steering);
1768317697
1768417698 assert.deepEqual(calls, [FOREGROUND_TOOL_USE_ID]);
1768517699 if (backgrounding === "answers after the turn ended") {
@@ -17693,17 +17707,13 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1769317707 if (backgrounding === "never answers") {
1769417708 for (let repeat = 1; repeat <= 2; repeat++) {
1769517709 const again = yield* Effect.forkScoped(steer);
17696- for (let tick = 0; tick < 100 && again.pollUnsafe() === undefined; tick++) {
17697- yield* TestClock.adjust("1 second");
17698- yield* Effect.yieldNow;
17699- }
17700- assert.isTrue(Exit.isSuccess(yield* Fiber.await(again)));
17710+ assert.isTrue(Exit.isSuccess(yield* awaitSteerTimeout(again)));
1770117711 assert.equal(harness.offeredMessages.length, offered + 1 + repeat);
1770217712 }
1770317713 assert.deepEqual(calls, [FOREGROUND_TOOL_USE_ID]);
1770417714 }
1770517715 if (backgrounding === "answers after the steer went out") {
17706- yield* TestClock.adjust("1 second" );
17716+ yield* Deferred.succeed(lateAnswer, undefined );
1770717717 } else if (
1770817718 backgrounding !== "succeeds" &&
1770917719 backgrounding !== "answers after the subagent ran again"
0 commit comments