The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(server): a Claude wake run settles when Claude never opens the wake turn (#19196)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
AdEx-Partners-DE committed
Oct 20, 2026 at 07:06 UTC
4b1426f5ece007f2e9620a81a13507911f785974
2 files changed
+252
-7
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+204
@@ -9571,6 +9571,210 @@ describe("ClaudeAdapterV2 background wake turns", () => {
9571
),
9572
);
9573
9574
+ const startSubagentWakeWithoutWakeTurn = (
9575
+ options?: Parameters<typeof makeWakeHarnessWithOptions>[0],
9576
+ bufferedFrames: ReadonlyArray<SDKMessage> = [],
9577
+ ) =>
9578
+ Effect.gen(function* () {
9579
+ const TASK_ID = "task-wake-never-opened";
9580
+ const TOOL_USE_ID = "toolu-wake-never-opened";
9581
+ const harness = yield* makeWakeHarnessWithOptions(options);
9582
+ const now = yield* DateTime.now;
9583
+ yield* harness.runtime.startTurn(
9584
+ makeClaudeTestTurnInput({
9585
+ threadId: harness.threadId,
9586
+ providerThread: harness.providerThread,
9587
+ now,
9588
+ attemptId: RunAttemptId.make("attempt-claude-wake-never-opened-1"),
9589
+ text: "Audit in the background.",
9590
+ attachments: [],
9591
+ }),
9592
+ );
9593
+ for (const frame of [
9594
+ makeSubagentTaskStartedFrame({
9595
+ taskId: TASK_ID,
9596
+ toolUseId: TOOL_USE_ID,
9597
+ uuid: "00000000-0000-4000-8000-000000000981",
9598
+ }),
9599
+ makeResultFrame({
9600
+ uuid: "00000000-0000-4000-8000-000000000982",
9601
+ result: "Auditing in the background.",
9602
+ }),
9603
+ ]) {
9604
+ yield* harness.offerAndWait(frame);
9605
+ }
9606
+ assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
9607
+ yield* harness.offerAndWait(
9608
+ makeSubagentNotificationFrame({
9609
+ taskId: TASK_ID,
9610
+ toolUseId: TOOL_USE_ID,
9611
+ summary: "Audit done.",
9612
+ uuid: "00000000-0000-4000-8000-000000000983",
9613
+ }),
9614
+ );
9615
+ yield* Queue.take(harness.continuationReceipts);
9616
+ for (const frame of bufferedFrames) {
9617
+ yield* harness.offerAndWait(frame);
9618
+ }
9619
+ yield* harness.runtime.startTurn(
9620
+ makeClaudeTestTurnInput({
9621
+ threadId: harness.threadId,
9622
+ providerThread: harness.providerThread,
9623
+ now,
9624
+ attemptId: RunAttemptId.make("attempt-claude-wake-never-opened-2"),
9625
+ text: "Background task completed.",
9626
+ attachments: [],
9627
+ providerTurnOrdinal: 2,
9628
+ messageCreatedBy: "agent",
9629
+ messageCreationSource: "provider",
9630
+ }),
9631
+ );
9632
+ assert.lengthOf(harness.terminalEvents(), 1);
9633
+ return harness;
9634
+ });
9635
+
9636
+ it.effect("settles a subagent wake run although a subagent reports progress meanwhile", () =>
9637
+ Effect.scoped(
9638
+ Effect.gen(function* () {
9639
+ const harness = yield* startSubagentWakeWithoutWakeTurn();
9640
+
9641
+ yield* TestClock.adjust("1 minute");
9642
+ assert.lengthOf(harness.terminalEvents(), 1);
9643
+ yield* harness.offerAndWait(
9644
+ claudeSdkFrame({
9645
+ type: "system",
9646
+ subtype: "task_progress",
9647
+ task_id: "task-still-running-elsewhere",
9648
+ description: "Reading more files",
9649
+ uuid: "00000000-0000-4000-8000-000000000984",
9650
+ session_id: WAKE_NATIVE_SESSION,
9651
+ }),
9652
+ );
9653
+ yield* TestClock.adjust("1 minute");
9654
+ assert.lengthOf(harness.terminalEvents(), 2);
9655
+ assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
9656
+ assert.lengthOf(harness.offeredMessages, 1);
9657
+ }).pipe(
9658
+ Effect.provide(
9659
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9660
+ ),
9661
+ ),
9662
+ ),
9663
+ );
9664
+
9665
+ it.effect("settles a subagent wake run with only subagent messages buffered", () =>
9666
+ Effect.scoped(
9667
+ Effect.gen(function* () {
9668
+ const harness = yield* startSubagentWakeWithoutWakeTurn(undefined, [
9669
+ ...makeSubagentAssistantFrames({
9670
+ parentToolUseId: "toolu-wake-never-opened",
9671
+ text: "Audit done.",
9672
+ uuid: "00000000-0000-4000-8000-000000000985",
9673
+ }),
9674
+ makeSubagentToolResultFrame({
9675
+ parentToolUseId: "toolu-wake-never-opened",
9676
+ toolUseId: "toolu-wake-child-bash",
9677
+ uuid: "00000000-0000-4000-8000-000000000986",
9678
+ }),
9679
+ ]);
9680
+
9681
+ yield* TestClock.adjust("2 minutes");
9682
+ assert.lengthOf(harness.terminalEvents(), 2);
9683
+ assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
9684
+ }).pipe(
9685
+ Effect.provide(
9686
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9687
+ ),
9688
+ ),
9689
+ ),
9690
+ );
9691
+
9692
+ it.effect("keeps a subagent wake run open while Claude works on the wake turn", () =>
9693
+ Effect.scoped(
9694
+ Effect.gen(function* () {
9695
+ const harness = yield* startSubagentWakeWithoutWakeTurn();
9696
+
9697
+ yield* harness.offerAndWait(wakeTurnInit);
9698
+ yield* TestClock.adjust("2 minutes");
9699
+ assert.lengthOf(harness.terminalEvents(), 1);
9700
+
9701
+ yield* harness.offerAndWait(wakeResult);
9702
+ assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
9703
+ }).pipe(
9704
+ Effect.provide(
9705
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9706
+ ),
9707
+ ),
9708
+ ),
9709
+ );
9710
+
9711
+ it.effect("keeps a subagent wake run open when root output starts streaming", () =>
9712
+ Effect.scoped(
9713
+ Effect.gen(function* () {
9714
+ const harness = yield* startSubagentWakeWithoutWakeTurn();
9715
+ yield* harness.offerAndWait(
9716
+ claudeSdkFrame({
9717
+ type: "stream_event",
9718
+ event: {
9719
+ type: "content_block_delta",
9720
+ index: 0,
9721
+ delta: { type: "text_delta", text: "Working" },
9722
+ },
9723
+ parent_tool_use_id: null,
9724
+ uuid: "00000000-0000-4000-8000-000000000987",
9725
+ session_id: WAKE_NATIVE_SESSION,
9726
+ }),
9727
+ );
9728
+
9729
+ yield* TestClock.adjust("2 minutes");
9730
+ assert.lengthOf(harness.terminalEvents(), 1);
9731
+ yield* harness.offerAndWait(wakeResult);
9732
+ assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
9733
+ }).pipe(
9734
+ Effect.provide(
9735
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9736
+ ),
9737
+ ),
9738
+ ),
9739
+ );
9740
+
9741
+ it.effect("does not complete a subagent wake run while Stop is pending", () =>
9742
+ Effect.scoped(
9743
+ Effect.gen(function* () {
9744
+ const stopRequested = yield* Deferred.make<void>();
9745
+ const releaseStop = yield* Deferred.make<void>();
9746
+ const harness = yield* startSubagentWakeWithoutWakeTurn({
9747
+ interrupt: Deferred.succeed(stopRequested, undefined).pipe(
9748
+ Effect.andThen(Deferred.await(releaseStop)),
9749
+ ),
9750
+ close: Queue.shutdown,
9751
+ });
9752
+ const stop = yield* harness.runtime
9753
+ .interruptTurn({
9754
+ providerThread: harness.providerThread,
9755
+ providerTurnId: (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
9756
+ driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
9757
+ nativeTurnId: "turn:attempt-claude-wake-never-opened-2",
9758
+ }),
9759
+ })
9760
+ .pipe(Effect.forkScoped);
9761
+ yield* Deferred.await(stopRequested);
9762
+
9763
+ yield* TestClock.adjust("2 minutes");
9764
+ assert.lengthOf(harness.terminalEvents(), 1);
9765
+
9766
+ yield* Deferred.succeed(releaseStop, undefined);
9767
+ yield* Fiber.join(stop);
9768
+ assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "interrupted");
9769
+ assert.lengthOf(harness.terminalEvents(), 2);
9770
+ }).pipe(
9771
+ Effect.provide(
9772
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9773
+ ),
9774
+ ),
9775
+ ),
9776
+ );
9777
+
9778
it.effect("keeps a nested subagent's launch in order while the root turn is idle", () =>
9779
Effect.scoped(
9780
Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+48
-7
@@ -287,6 +287,9 @@ const CLAUDE_SCRIPT_EXTENSIONS = [".js", ".mjs", ".tsx", ".ts", ".jsx"];
287
// The SDK puts no deadline on control requests. The CLI answers
288
// mcp_set_servers once each server connected or hit its own 30 s timeout.
289
const CLAUDE_MCP_REGISTRATION_TIMEOUT = "90 seconds";
290
+// Claude can consume a subagent result before the launching turn ends, leaving
291
+// its notification with no native wake turn to answer the continuation.
292
+const CLAUDE_WAKE_TURN_START_DEADLINE = "2 minutes";
293
// Claude Code flushes transcript writes every 100 ms, so a workflow member's
294
// reply can land after the progress frame that settles it. Its transcript
295
// stays the source of its answer this long after it settles.
@@ -2241,6 +2244,14 @@ function isClaudeTurnStartMessage(message: SDKMessage): boolean {
2244
return message.type === "system" && message.subtype === "init";
2245
}
2246
2247
+function isClaudeRootTurnFrame(message: SDKMessage): boolean {
2248
+ return (
2249
+ isClaudeTurnStartMessage(message) ||
2250
+ ((message.type === "user" || message.type === "assistant" || message.type === "stream_event") &&
2251
+ message.parent_tool_use_id == null)
2252
+ );
2253
+}
2254
+
2255
function claudePendingBackgroundTasksFromRoster(
2256
roster: ReadonlyMap<string, OrchestrationV2PendingBackgroundTask>,
2257
): ReadonlyArray<OrchestrationV2PendingBackgroundTask> {
@@ -3212,6 +3223,7 @@ interface ActiveClaudeTurnContext {
3223
readonly promptUuid: string | null;
3224
promptEcho: "pending" | "confirmed";
3225
promptStarted: boolean;
3226
+ nativeWakeStarted: boolean;
3227
gatedFramesBeforeEcho: number;
3228
readonly heldRootFrames: Array<SDKMessage>;
3229
}
@@ -8309,6 +8321,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8321
const message = input.message;
8322
const context = yield* Ref.get(activeTurn);
8323
const liveQuery = yield* Ref.get(queryContext);
8324
+ if (
8325
+ context?.promptUuid === null &&
8326
+ liveQuery?.query === input.query &&
8327
+ isClaudeRootTurnFrame(message)
8328
+ ) {
8329
+ context.nativeWakeStarted = true;
8330
+ }
8331
if (
8332
context === null ||
8333
context.promptUuid === null ||
@@ -9092,6 +9111,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9111
promptUuid,
9112
promptEcho: isClaudeProviderContinuationTurn(turnInput) ? "confirmed" : "pending",
9113
promptStarted: false,
9114
+ nativeWakeStarted: false,
9115
gatedFramesBeforeEcho: 0,
9116
heldRootFrames: [],
9117
};
@@ -9208,15 +9228,36 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9228
yield* handleSdkMessage({ query: querySession.query, message: lastResult });
9229
return;
9230
}
9211
- const hasNativeWakeFrame = drained.some(
9212
- (entry) =>
9213
- entry.type === "user" ||
9214
- entry.type === "assistant" ||
9215
- isClaudeTurnStartMessage(entry),
9216
- );
9217
- if (hasOpaqueTaskNotification && !hasNativeWakeFrame) {
9231
+ if (hasOpaqueTaskNotification && !context.nativeWakeStarted) {
9232
const completedAt = yield* DateTime.now;
9233
yield* finalizeActiveTurn({ context, status: "completed", completedAt });
9234
+ } else if (!context.nativeWakeStarted) {
9235
+ yield* Effect.sleep(CLAUDE_WAKE_TURN_START_DEADLINE).pipe(
9236
+ Effect.andThen(
9237
+ sdkMessageLock.withPermits(1)(
9238
+ Effect.gen(function* () {
9239
+ if (
9240
+ (yield* Ref.get(activeTurn)) !== context ||
9241
+ (yield* Ref.get(queryContext)) !== querySession ||
9242
+ querySession.stopping ||
9243
+ context.nativeWakeStarted ||
9244
+ (yield* Ref.get(interruptedTurns)).has(context.providerTurnId)
9245
+ )
9246
+ return;
9247
+ yield* Effect.logWarning(
9248
+ "orchestration-v2.claude-wake-turn-never-opened",
9249
+ {
9250
+ providerTurnId: context.providerTurnId,
9251
+ nativeThreadId,
9252
+ },
9253
+ );
9254
+ const completedAt = yield* DateTime.now;
9255
+ yield* finalizeActiveTurn({ context, status: "completed", completedAt });
9256
+ }),
9257
+ ),
9258
+ ),
9259
+ Effect.forkIn(sessionScope),
9260
+ );
9261
}
9262
}),
9263
);