@devries-cameron20 / t3code Mirror · read-only / commits / 692dd9ce6f
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 22, 05:45 UTC

fix(claude): a background-task wake run no longer hangs after a user turn absorbed its wake (#28010)

Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>

maria committed Nov 3, 2026 at 02:45 UTC 692dd9ce6f91ce0f16f8189d681d3599e6d4d7aa
2 files changed +108 -1
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+80
@@ -11432,6 +11432,86 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11432 ),
11433 );
11434
11435 + it.effect("settles a continuation whose wake turn a user turn absorbed", () =>
11436 + Effect.scoped(
11437 + Effect.gen(function* () {
11438 + const harness = yield* makeWakeHarness;
11439 + const now = yield* DateTime.now;
11440 +
11441 + yield* harness.runtime.startTurn(
11442 + makeClaudeTestTurnInput({
11443 + threadId: harness.threadId,
11444 + providerThread: harness.providerThread,
11445 + now,
11446 + attemptId: RunAttemptId.make("attempt-claude-wake-absorbed-1"),
11447 + text: "Run the build in the background.",
11448 + attachments: [],
11449 + }),
11450 + );
11451 + yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
11452 + yield* Queue.offer(harness.sdkMessages, wakeNotification);
11453 + yield* Queue.offer(harness.sdkMessages, turnOneResult);
11454 + yield* harness.awaitTerminal(1);
11455 + yield* harness.offerAndWait(wakeTurnInit);
11456 + yield* harness.offerAndWait(wakeAssistant);
11457 + yield* harness.awaitContinuation(1);
11458 +
11459 + // Claude folds this prompt into the wake turn it already opened, so one result ends both.
11460 + yield* harness.runtime.startTurn(
11461 + makeClaudeTestTurnInput({
11462 + threadId: harness.threadId,
11463 + providerThread: harness.providerThread,
11464 + now,
11465 + attemptId: RunAttemptId.make("attempt-claude-wake-absorbed-2"),
11466 + text: "Any update on the pull request?",
11467 + attachments: [],
11468 + providerTurnOrdinal: 2,
11469 + }),
11470 + );
11471 + yield* harness.until(() => harness.offeredMessages.length === 2);
11472 + yield* Queue.offer(
11473 + harness.sdkMessages,
11474 + makeResultFrame({
11475 + uuid: "00000000-0000-4000-8000-000000000140",
11476 + result: "Checks passed and the pull request is up to date.",
11477 + }),
11478 + );
11479 + yield* harness.awaitTerminal(2);
11480 +
11481 + yield* harness.runtime.startTurn(
11482 + makeClaudeTestTurnInput({
11483 + threadId: harness.threadId,
11484 + providerThread: harness.providerThread,
11485 + now,
11486 + attemptId: RunAttemptId.make("attempt-claude-wake-absorbed-3"),
11487 + text: "Background task completed.",
11488 + attachments: [],
11489 + providerTurnOrdinal: 3,
11490 + messageCreatedBy: "agent",
11491 + messageCreationSource: "provider",
11492 + }),
11493 + );
11494 + yield* harness.awaitTerminal(3);
11495 + assert.equal(harness.terminalEvents()[2]?.status, "completed");
11496 + assert.isTrue(
11497 + harness.events.some(
11498 + (event) =>
11499 + event.type === "message.updated" && event.message.text === WAKE_ASSISTANT_TEXT,
11500 + ),
11501 + );
11502 + }).pipe(
11503 + Effect.provide(
11504 + Layer.mergeAll(
11505 + IdAllocator.layer,
11506 + McpProviderSessions.layer,
11507 + NodeServices.layer,
11508 + NodeCrypto.layer,
11509 + ),
11510 + ),
11511 + ),
11512 + ),
11513 + );
11514 +
11515 it.effect("ignores a zero-turn task-notification origin result during a normal user turn", () =>
11516 Effect.scoped(
11517 Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+28 -1
@@ -5031,6 +5031,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5031 }
5032 });
5033 const requestedContinuations = yield* Ref.make(new Set<string>());
5034 + const absorbedWakes = yield* Ref.make(new Set<string>());
5035 const heldProposedPlansByToolUseId = new Map<string, string>();
5036 const runtimeContext = yield* Effect.context<never>();
5037 const runPromise = Effect.runPromiseWith(runtimeContext);
@@ -5466,6 +5467,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5467 yield* endClaudeMonitorTasks((_taskId, task) => task.nativeThreadId === nativeThreadId);
5468 });
5469
5470 + const forgetAbsorbedWake = (nativeThreadId: string) =>
5471 + Ref.update(absorbedWakes, (current) => {
5472 + if (!current.has(nativeThreadId)) return current;
5473 + const updated = new Set(current);
5474 + updated.delete(nativeThreadId);
5475 + return updated;
5476 + });
5477 +
5478 const clearWakeStateForNativeThread =
5479 /* Provider events include streaming deltas; avoid a span for each update. */
5480 Effect.fnUntraced(function* (nativeThreadId: string) {
@@ -5486,8 +5495,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5495 updated.delete(nativeThreadId);
5496 return updated;
5497 });
5498 + yield* forgetAbsorbedWake(nativeThreadId);
5499 });
5500
5501 + // Claude runs one root turn at a time, so a user turn's result also ends a wake turn it
5502 + // folded in. The continuation replays that wake's buffered frames but gets no result of its own.
5503 + const markAbsorbedWake = Effect.fnUntraced(function* (nativeThreadId: string) {
5504 + const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
5505 + const lastResult = messages.findLastIndex((entry) => entry.type === "result");
5506 + if (!messages.slice(lastResult + 1).some(isClaudeRootTurnFrame)) return;
5507 + yield* Ref.update(absorbedWakes, (current) => new Set(current).add(nativeThreadId));
5508 + });
5509 +
5510 // The SDK emits no initial roster; reset roster and wake eligibility when opening a live query.
5511 const resetBackgroundTaskStateForNativeThreadProcess = Effect.fnUntraced(function* (
5512 nativeThreadId: string,
@@ -8070,6 +8089,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8089 if (message.type === "result") {
8090 bufferedResultArrivals.set(message, yield* DateTime.now);
8091 }
8092 + if (isClaudeRootTurnFrame(message)) {
8093 + yield* forgetAbsorbedWake(wakeInput.nativeThreadId);
8094 + }
8095 yield* Ref.update(wakeBuffers, (current) => {
8096 const existing = current.get(wakeInput.nativeThreadId);
8097 const updated = new Map(current);
@@ -9496,6 +9518,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9518 next.delete(context.providerTurnId);
9519 return next;
9520 });
9521 + if (!isClaudeProviderContinuationTurn(context.input)) {
9522 + yield* markAbsorbedWake(liveQuery.nativeThreadId);
9523 + }
9524 const usageLimited =
9525 context.authenticationFailureMessage === undefined &&
9526 (context.rejectedRateLimitTypes.size > 0 ||
@@ -10793,6 +10818,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10818 updated.delete(nativeThreadId);
10819 return updated;
10820 });
10821 + const wakeAbsorbed = (yield* Ref.get(absorbedWakes)).has(nativeThreadId);
10822 + yield* forgetAbsorbedWake(nativeThreadId);
10823 if (drained.length === 0) {
10824 const completedAt = yield* DateTime.now;
10825 yield* finalizeActiveTurn({ context, status: "completed", completedAt });
@@ -10861,7 +10888,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10888 yield* handleSdkMessage({ query: querySession.query, message: lastResult });
10889 return;
10890 }
10864 - if (hasOpaqueTaskNotification && !context.nativeWakeStarted) {
10891 + if (wakeAbsorbed || (hasOpaqueTaskNotification && !context.nativeWakeStarted)) {
10892 const completedAt = yield* DateTime.now;
10893 yield* finalizeActiveTurn({ context, status: "completed", completedAt });
10894 } else if (!context.nativeWakeStarted) {