@devries-cameron20 / t3code Mirror · read-only / commits / a0b5df18bf
Fork 0

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 23, 03:00 UTC

fix(server): Claude wake runs no longer end empty while Claude is still answering (#15816)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Dara Adedeji committed Oct 18, 2026 at 16:05 UTC a0b5df18bf1314b0aa372eca3e79dd21ccbc0df6
2 files changed +118 -4
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+103
@@ -7360,6 +7360,109 @@ describe("ClaudeAdapterV2 background wake turns", () => {
7360 return { childThreadId, toolThreadIds, assistantTexts };
7361 };
7362
7363 + it.effect("keeps the wake run open for the turn Claude opens right after an empty one", () =>
7364 + Effect.scoped(
7365 + Effect.gen(function* () {
7366 + const harness = yield* makeWakeHarness;
7367 + const now = yield* DateTime.now;
7368 + const subagents = [
7369 + { taskId: "task-wake-pair-a", toolUseId: "toolu_wake_pair_a" },
7370 + { taskId: "task-wake-pair-b", toolUseId: "toolu_wake_pair_b" },
7371 + ];
7372 + const wakeAttempt = RunAttemptId.make("attempt-claude-wake-pair-2");
7373 +
7374 + yield* harness.runtime.startTurn(
7375 + makeClaudeTestTurnInput({
7376 + threadId: harness.threadId,
7377 + providerThread: harness.providerThread,
7378 + now,
7379 + attemptId: RunAttemptId.make("attempt-claude-wake-pair-1"),
7380 + text: "Audit both in the background.",
7381 + attachments: [],
7382 + }),
7383 + );
7384 + for (const [index, subagent] of subagents.entries()) {
7385 + yield* Queue.offer(
7386 + harness.sdkMessages,
7387 + makeSubagentTaskStartedFrame({
7388 + ...subagent,
7389 + uuid: `00000000-0000-4000-8000-00000000095${index}`,
7390 + }),
7391 + );
7392 + }
7393 + yield* Queue.offer(
7394 + harness.sdkMessages,
7395 + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000952", result: "Started." }),
7396 + );
7397 + yield* awaitUntil(() => harness.terminalEvents().length === 1, "first turn terminal");
7398 +
7399 + // Both subagents end together. Claude runs an empty turn for the first
7400 + // notification and opens the real one a millisecond later, all before
7401 + // the wake run attaches.
7402 + for (const [index, subagent] of subagents.entries()) {
7403 + yield* Queue.offer(
7404 + harness.sdkMessages,
7405 + makeSubagentNotificationFrame({
7406 + ...subagent,
7407 + summary: "AUDITED",
7408 + uuid: `00000000-0000-4000-8000-00000000096${index}`,
7409 + }),
7410 + );
7411 + }
7412 + yield* Queue.offer(harness.sdkMessages, wakeTurnInit);
7413 + yield* Queue.offer(
7414 + harness.sdkMessages,
7415 + makeResultFrame({
7416 + uuid: "00000000-0000-4000-8000-000000000962",
7417 + result: "",
7418 + numTurns: 0,
7419 + origin: { kind: "task-notification" },
7420 + terminalReason: null,
7421 + }),
7422 + );
7423 + yield* harness.offerAndWait(
7424 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000963" }),
7425 + );
7426 + assert.lengthOf(harness.continuationRequests, 1);
7427 +
7428 + yield* harness.runtime.startTurn(
7429 + makeClaudeTestTurnInput({
7430 + threadId: harness.threadId,
7431 + providerThread: harness.providerThread,
7432 + now,
7433 + attemptId: wakeAttempt,
7434 + text: "Background task completed.",
7435 + attachments: [],
7436 + providerTurnOrdinal: 2,
7437 + messageCreatedBy: "agent",
7438 + messageCreationSource: "provider",
7439 + }),
7440 + );
7441 + // Claude is still thinking, so the run stays working rather than
7442 + // settling with nothing to show.
7443 + assert.lengthOf(harness.terminalEvents(), 1);
7444 +
7445 + yield* harness.offerAndWait(wakeAssistant);
7446 + yield* harness.offerAndWait(wakeResult);
7447 + yield* awaitUntil(() => harness.terminalEvents().length === 2, "wake run terminal");
7448 + assert.equal(harness.terminalEvents()[1]?.status, "completed");
7449 + assert.lengthOf(harness.continuationRequests, 1);
7450 + assert.isTrue(
7451 + harness.events.some(
7452 + (event) =>
7453 + event.type === "message.updated" &&
7454 + event.message.runId === RunId.make(`run-${wakeAttempt}`) &&
7455 + event.message.text === WAKE_RESULT_TEXT,
7456 + ),
7457 + );
7458 + }).pipe(
7459 + Effect.provide(
7460 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7461 + ),
7462 + ),
7463 + ),
7464 + );
7465 +
7466 it.effect("a subagent re-run in the foreground does not join a later wake", () =>
7467 Effect.scoped(
7468 Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+15 -4
@@ -8688,8 +8688,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8688 }
8689 // Replay any result message last: a result finalizes the turn, and
8690 // replaying it before the rest would drop them back into the wake
8691 - // buffer and request another continuation.
8692 - const resultMessages = drained.filter((entry) => entry.type === "result");
8691 + // buffer and request another continuation. When Claude dequeues
8692 + // several notifications at once it can end an empty turn and open
8693 + // the next within a millisecond; that next turn is still running
8694 + // and owns this run, so the empty turn's result must not settle it.
8695 + const lastResultIndex = drained.findLastIndex((entry) => entry.type === "result");
8696 + const lastResult = drained[lastResultIndex];
8697 + const emptyTurnBeforeNextTurn =
8698 + lastResult?.type === "result" &&
8699 + lastResult.num_turns === 0 &&
8700 + !lastResult.is_error &&
8701 + terminalStatusFromResult(lastResult) === "completed" &&
8702 + drained
8703 + .slice(lastResultIndex + 1)
8704 + .some((entry) => isClaudeTurnStartMessage(entry));
8705 const opaqueReplayTombstones = taskIdSetForNativeThread(
8706 yield* Ref.get(opaqueBackgroundTaskReplayTombstonesByNativeThread),
8707 nativeThreadId,
@@ -8724,8 +8736,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8736 return next;
8737 });
8738 }
8727 - const lastResult = resultMessages.at(-1);
8728 - if (lastResult !== undefined) {
8739 + if (lastResult !== undefined && !emptyTurnBeforeNextTurn) {
8740 yield* handleSdkMessage({ query: querySession.query, message: lastResult });
8741 return;
8742 }