From 912b3ba449e722a650613aeb13a9c7b9cd466ba3 Mon Sep 17 00:00:00 2001 From: Mike Olson Date: Wed, 11 Nov 2026 18:11:42 -0500 Subject: [PATCH] fix(orchestrator): Stop waiting on a Claude background wake that never opens Upstream keeps exited background commands listed until their wake run starts and releases abandoned continuation offers. Expire the remaining roster hold at the existing two-minute wake deadline when Claude never opens that wake, so a completed thread cannot stay Waiting indefinitely. --- .../Adapters/ClaudeAdapterV2.test.ts | 21 ++++++- .../Adapters/ClaudeAdapterV2.ts | 59 ++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index f423a9c40d335..a4bdc90d67d80 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -18133,7 +18133,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); - it.effect.each(["none", "before", "after", "sibling"] as const)( + it.effect.each(["none", "before", "after", "sibling", "never opens", "active wake"] as const)( "keeps an exited background command on the roster until its wake run starts (empty level %s notification)", (emptyLevel) => Effect.gen(function* () { @@ -18184,6 +18184,15 @@ describe("ClaudeAdapterV2 background wake turns", () => { yield* harness.offerAndWait(emptyRoster); yield* harness.offerAndWait(wakeNotification); if (emptyLevel === "after") yield* harness.offerAndWait(emptyRoster); + if (emptyLevel === "never opens") { + yield* TestClock.adjust("2 minutes"); + assert.deepEqual( + providerThreadRosterEvents(harness.events).at(-1)?.providerThread + .pendingBackgroundTasks, + [], + ); + return; + } // Query replacement reads the internal roster, which the exit already cleared. assert.isFalse(yield* harness.hasPendingBackgroundWork); yield* harness.offerAndWait(wakeTurnInit); @@ -18201,8 +18210,18 @@ describe("ClaudeAdapterV2 background wake turns", () => { }), ); yield* harness.offerAndWait(wakeAssistant); + if (emptyLevel === "active wake") { + const rosterCount = providerThreadRosterEvents(harness.events).length; + yield* TestClock.adjust("2 minutes"); + assert.lengthOf(providerThreadRosterEvents(harness.events), rosterCount); + } yield* harness.offerAndWait(wakeResult); yield* harness.awaitTerminal(2); + if (emptyLevel === "active wake") { + const rosterCount = providerThreadRosterEvents(harness.events).length; + yield* TestClock.adjust("2 minutes"); + assert.lengthOf(providerThreadRosterEvents(harness.events), rosterCount); + } const wakeStart = harness.events.findIndex( (event) => diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 665d8ec0b00d8..a49ec4b1a61a8 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -7049,11 +7049,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return tasks; }); + const owedWakeDeadlines = new Map>(); + const emitProviderThreadRoster = Effect.fnUntraced(function* (input: { readonly nativeThreadId: string; readonly providerThread: OrchestrationV2ProviderThread; readonly status?: OrchestrationV2ProviderThread["status"]; - }) { + }): Effect.fn.Return { const roster = rosterForNativeThread( yield* Ref.get(pendingBackgroundTasksByNativeThread), input.nativeThreadId, @@ -7063,6 +7065,61 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( input.nativeThreadId, ); const owedWakeTasks = yield* owedWakeTasksForNativeThread(input.nativeThreadId); + const deadlines = + owedWakeDeadlines.get(input.nativeThreadId) ?? new Map(); + for (const taskId of deadlines.keys()) { + if (!owedWakeTasks.some((task) => task.taskId === taskId)) deadlines.delete(taskId); + } + if (owedWakeTasks.length === 0) owedWakeDeadlines.delete(input.nativeThreadId); + else owedWakeDeadlines.set(input.nativeThreadId, deadlines); + for (const task of owedWakeTasks) { + if (deadlines.has(task.taskId)) continue; + const token = Symbol(task.taskId); + deadlines.set(task.taskId, token); + yield* Effect.sleep(CLAUDE_WAKE_TURN_START_DEADLINE).pipe( + Effect.andThen( + sdkMessageLock.withPermits(1)( + Effect.gen(function* () { + if (owedWakeDeadlines.get(input.nativeThreadId)?.get(task.taskId) !== token) + return false; + const previous = (yield* Ref.get(lastProviderThreadByNativeThread)).get( + input.nativeThreadId, + ); + const currentRoster = rosterForNativeThread( + yield* Ref.get(pendingBackgroundTasksByNativeThread), + input.nativeThreadId, + ); + if ( + previous === undefined || + currentRoster.has(task.taskId) || + !previous.pendingBackgroundTasks?.some( + (pending) => pending.taskId === task.taskId, + ) + ) { + deadlines.delete(task.taskId); + return false; + } + if ((yield* Ref.get(activeTurn)) !== null) return true; + deadlines.delete(task.taskId); + // A missing native wake must not hold the completed thread indefinitely. + yield* rememberProviderThread({ + ...previous, + pendingBackgroundTasks: (previous.pendingBackgroundTasks ?? []).filter( + (pending) => pending.taskId !== task.taskId, + ), + }); + yield* emitProviderThreadRoster({ + nativeThreadId: input.nativeThreadId, + providerThread: previous, + }); + return false; + }), + ), + ), + Effect.repeat({ while: (active) => active }), + Effect.forkIn(sessionScope), + ); + } const now = yield* DateTime.now; const providerThread: OrchestrationV2ProviderThread = { ...input.providerThread,