Skip to content

Commit 656715b

Browse files
fix(server): Claude wake runs no longer hang after a prompt joins the wake turn
When a background task finished while the thread was idle, Claude opened its own wake turn and T3 requested a continuation run. If a queued prompt was offered into that already-open wake turn, Claude answered both with one result, which settled the prompt's run. The continuation then drained a buffered init with no result, so it waited forever and blocked every queued run behind it. Mark a buffered, still-open wake turn as answered when a prompt is offered into it, and settle the continuation after replaying the wake frames. As a backstop, the wake-start deadline now keeps watching an opened wake and settles it once it streams nothing and holds no tool call or pending request for 10 minutes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fa4e467 commit 656715b

2 files changed

Lines changed: 180 additions & 33 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9484,6 +9484,110 @@ describe("ClaudeAdapterV2 background wake turns", () => {
94849484
),
94859485
);
94869486

9487+
const openIdleWakeTurn = (attemptPrefix: string) =>
9488+
Effect.gen(function* () {
9489+
const harness = yield* makeWakeHarness;
9490+
const now = yield* DateTime.now;
9491+
yield* harness.runtime.startTurn(
9492+
makeClaudeTestTurnInput({
9493+
threadId: harness.threadId,
9494+
providerThread: harness.providerThread,
9495+
now,
9496+
attemptId: RunAttemptId.make(`${attemptPrefix}-1`),
9497+
text: "Run the build in the background.",
9498+
attachments: [],
9499+
}),
9500+
);
9501+
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
9502+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
9503+
yield* harness.awaitTerminal(1);
9504+
yield* harness.offerAndWait(wakeNotification);
9505+
yield* harness.offerAndWait(wakeTurnInit);
9506+
assert.lengthOf(harness.continuationRequests, 1);
9507+
const startContinuation = (providerTurnOrdinal: number) =>
9508+
harness.runtime.startTurn(
9509+
makeClaudeTestTurnInput({
9510+
threadId: harness.threadId,
9511+
providerThread: harness.providerThread,
9512+
now,
9513+
attemptId: RunAttemptId.make(`${attemptPrefix}-continuation`),
9514+
text: "Background task completed.",
9515+
attachments: [],
9516+
providerTurnOrdinal,
9517+
messageCreatedBy: "agent",
9518+
messageCreationSource: "provider",
9519+
}),
9520+
);
9521+
return { harness, now, startContinuation };
9522+
});
9523+
9524+
it.effect("settles the continuation when a queued prompt joins the opened wake turn", () =>
9525+
Effect.scoped(
9526+
Effect.gen(function* () {
9527+
const { harness, now, startContinuation } = yield* openIdleWakeTurn(
9528+
"attempt-claude-wake-joined",
9529+
);
9530+
assert.equal(harness.continuationRequests[0]?.notification?.outcome, "completed");
9531+
9532+
yield* harness.runtime.startTurn(
9533+
makeClaudeTestTurnInput({
9534+
threadId: harness.threadId,
9535+
providerThread: harness.providerThread,
9536+
now,
9537+
attemptId: RunAttemptId.make("attempt-claude-wake-joined-2"),
9538+
text: "Webhook: a pull request was labeled.",
9539+
attachments: [],
9540+
providerTurnOrdinal: 2,
9541+
}),
9542+
);
9543+
yield* harness.until(() => harness.offeredMessages.length === 2);
9544+
// Claude answers the notification and the prompt in one turn with one result.
9545+
yield* harness.offerAndWait(wakeAssistant);
9546+
yield* harness.offerAndWait(
9547+
makeResultFrame({
9548+
uuid: "00000000-0000-4000-8000-000000000140",
9549+
result: "Ledger drift applied and the webhook handled.",
9550+
numTurns: 4,
9551+
}),
9552+
);
9553+
yield* harness.awaitTerminal(2);
9554+
assert.equal(harness.terminalEvents()[1]?.status, "completed");
9555+
9556+
yield* startContinuation(3);
9557+
yield* harness.awaitTerminal(3);
9558+
assert.equal(harness.terminalEvents()[2]?.status, "completed");
9559+
assert.lengthOf(harness.offeredMessages, 2);
9560+
}).pipe(
9561+
Effect.provide(
9562+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9563+
),
9564+
),
9565+
),
9566+
);
9567+
9568+
it.effect("settles an opened wake run that goes idle without a result", () =>
9569+
Effect.scoped(
9570+
Effect.gen(function* () {
9571+
const { harness, startContinuation } = yield* openIdleWakeTurn("attempt-claude-wake-idle");
9572+
yield* startContinuation(2);
9573+
assert.lengthOf(harness.terminalEvents(), 1);
9574+
9575+
yield* TestClock.adjust("9 minutes");
9576+
yield* harness.offerAndWait(wakeAssistant);
9577+
yield* TestClock.adjust("1 minute");
9578+
assert.lengthOf(harness.terminalEvents(), 1);
9579+
9580+
yield* TestClock.adjust("10 minutes");
9581+
yield* harness.awaitTerminal(2);
9582+
assert.equal(harness.terminalEvents()[1]?.status, "completed");
9583+
}).pipe(
9584+
Effect.provide(
9585+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9586+
),
9587+
),
9588+
),
9589+
);
9590+
94879591
it.effect.each([true, false])(
94889592
"carries a rejected wake rate limit into the continuation failure (event=%s)",
94899593
(standaloneEvent) =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ const CLAUDE_MCP_REGISTRATION_TIMEOUT = "90 seconds";
315315
// Claude can consume a subagent result before the launching turn ends, leaving
316316
// its notification with no native wake turn to answer the continuation.
317317
const CLAUDE_WAKE_TURN_START_DEADLINE = "2 minutes";
318+
// An opened wake turn that streams nothing and holds no tool call this long is idle.
319+
const CLAUDE_WAKE_TURN_IDLE_DEADLINE = "10 minutes";
318320
// Claude Code flushes transcript writes every 100 ms, so a workflow member's
319321
// reply can land after the progress frame that settles it. Its transcript
320322
// stays the source of its answer this long after it settles.
@@ -3929,6 +3931,7 @@ interface ActiveClaudeTurnContext {
39293931
promptEcho: "pending" | "confirmed";
39303932
promptStarted: boolean;
39313933
nativeWakeStarted: boolean;
3934+
wakeFrames: number;
39323935
gatedFramesBeforeEcho: number;
39333936
readonly heldRootFrames: Array<SDKMessage>;
39343937
}
@@ -4708,6 +4711,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
47084711
>();
47094712
// Buffered idle outcomes must not settle a resumed child again.
47104713
const appliedSubagentOutcomes = new WeakSet<SDKMessage>();
4714+
// A prompt offered into an opened wake turn is answered with it; that turn's result settles the prompt.
4715+
const wakeStartsAnsweredByPrompt = new WeakSet<SDKMessage>();
47114716
// Subagent frames can precede the task_started that registers their owner.
47124717
const pendingSubagentFramesByToolUseId = yield* Ref.make(
47134718
new Map<string, ReadonlyArray<SDKMessage>>(),
@@ -9351,12 +9356,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93519356
const message = input.message;
93529357
const context = yield* Ref.get(activeTurn);
93539358
const liveQuery = yield* Ref.get(queryContext);
9354-
if (
9355-
context?.promptUuid === null &&
9356-
liveQuery?.query === input.query &&
9357-
isClaudeRootTurnFrame(message)
9358-
) {
9359-
context.nativeWakeStarted = true;
9359+
if (context?.promptUuid === null && liveQuery?.query === input.query) {
9360+
context.wakeFrames += 1;
9361+
if (isClaudeRootTurnFrame(message)) context.nativeWakeStarted = true;
93609362
}
93619363
if (
93629364
context === null ||
@@ -10213,6 +10215,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1021310215
promptEcho: isClaudeProviderContinuationTurn(turnInput) ? "confirmed" : "pending",
1021410216
promptStarted: false,
1021510217
nativeWakeStarted: false,
10218+
wakeFrames: 0,
1021610219
gatedFramesBeforeEcho: 0,
1021710220
heldRootFrames: [],
1021810221
};
@@ -10277,6 +10280,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1027710280
if (userMessage !== null) {
1027810281
yield* attachTurn;
1027910282
yield* startUserTurnForWakeReports(nativeThreadId);
10283+
const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
10284+
const openWakeStart = buffered.findLast(isClaudeTurnStartMessage);
10285+
if (
10286+
openWakeStart !== undefined &&
10287+
!buffered
10288+
.slice(buffered.indexOf(openWakeStart))
10289+
.some((entry) => entry.type === "result")
10290+
) {
10291+
wakeStartsAnsweredByPrompt.add(openWakeStart);
10292+
}
1028010293
yield* Effect.uninterruptibleMask((restore) =>
1028110294
restore(
1028210295
querySession.query.offer(userMessage, () => {
@@ -10379,37 +10392,67 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1037910392
yield* handleSdkMessage({ query: querySession.query, message: lastResult });
1038010393
return;
1038110394
}
10395+
const latestWakeStart = drained.findLast(isClaudeTurnStartMessage);
1038210396
if (hasOpaqueTaskNotification && !context.nativeWakeStarted) {
1038310397
const completedAt = yield* DateTime.now;
1038410398
yield* finalizeActiveTurn({ context, status: "completed", completedAt });
10385-
} else if (!context.nativeWakeStarted) {
10386-
yield* Effect.sleep(CLAUDE_WAKE_TURN_START_DEADLINE).pipe(
10387-
Effect.andThen(
10388-
sdkMessageLock.withPermits(1)(
10389-
Effect.gen(function* () {
10390-
if (
10391-
(yield* Ref.get(activeTurn)) !== context ||
10392-
(yield* Ref.get(queryContext)) !== querySession ||
10393-
querySession.stopping ||
10394-
context.nativeWakeStarted ||
10395-
(yield* Ref.get(interruptedTurns)).has(context.providerTurnId)
10396-
)
10397-
return;
10398-
yield* Effect.logWarning(
10399-
"orchestration-v2.claude-wake-turn-never-opened",
10400-
{
10401-
providerTurnId: context.providerTurnId,
10402-
nativeThreadId,
10403-
},
10404-
);
10405-
const completedAt = yield* DateTime.now;
10406-
yield* finalizeActiveTurn({ context, status: "completed", completedAt });
10407-
}),
10408-
),
10409-
),
10410-
Effect.forkIn(sessionScope),
10411-
);
10399+
return;
1041210400
}
10401+
if (
10402+
latestWakeStart !== undefined &&
10403+
wakeStartsAnsweredByPrompt.has(latestWakeStart)
10404+
) {
10405+
yield* Effect.logInfo("orchestration-v2.claude-wake-turn-answered-by-prompt", {
10406+
providerTurnId: context.providerTurnId,
10407+
nativeThreadId,
10408+
});
10409+
const completedAt = yield* DateTime.now;
10410+
yield* finalizeActiveTurn({ context, status: "completed", completedAt });
10411+
return;
10412+
}
10413+
yield* Effect.gen(function* () {
10414+
let framesSeen = context.wakeFrames;
10415+
while (true) {
10416+
yield* Effect.sleep(
10417+
context.nativeWakeStarted
10418+
? CLAUDE_WAKE_TURN_IDLE_DEADLINE
10419+
: CLAUDE_WAKE_TURN_START_DEADLINE,
10420+
);
10421+
const settled = yield* sdkMessageLock.withPermits(1)(
10422+
Effect.gen(function* () {
10423+
if (
10424+
(yield* Ref.get(activeTurn)) !== context ||
10425+
(yield* Ref.get(queryContext)) !== querySession ||
10426+
querySession.stopping ||
10427+
(yield* Ref.get(interruptedTurns)).has(context.providerTurnId)
10428+
)
10429+
return true;
10430+
if (
10431+
context.nativeWakeStarted &&
10432+
(context.wakeFrames !== framesSeen ||
10433+
context.toolCalls.size > 0 ||
10434+
(yield* Ref.get(pendingRuntimeRequests)).size > 0)
10435+
) {
10436+
framesSeen = context.wakeFrames;
10437+
return false;
10438+
}
10439+
yield* Effect.logWarning(
10440+
context.nativeWakeStarted
10441+
? "orchestration-v2.claude-wake-turn-went-idle"
10442+
: "orchestration-v2.claude-wake-turn-never-opened",
10443+
{
10444+
providerTurnId: context.providerTurnId,
10445+
nativeThreadId,
10446+
},
10447+
);
10448+
const completedAt = yield* DateTime.now;
10449+
yield* finalizeActiveTurn({ context, status: "completed", completedAt });
10450+
return true;
10451+
}),
10452+
);
10453+
if (settled) return;
10454+
}
10455+
}).pipe(Effect.forkIn(sessionScope));
1041310456
}),
1041410457
);
1041510458
},

0 commit comments

Comments
 (0)