From 0d997b115a6f255eba600e4be8266832b8f7152b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 23:49:48 +0000 Subject: [PATCH 1/4] test(server): unread Claude steer follow-ups missed by #39018 fail on main Test-only parts of 92c6a408c5, 0837a2d7d6 and 0893addc90 from Alb11747/t3code fix/claude-steer-followups, applied to main without their fixes: - a held result's usage is unavailable when Claude exits with a steer unread - a held result never settles when the steer offer fails - a stopped turn settles completed when Claude drops the held steer Refs #39433 Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 140 ++++++++++++++++++ ...laudeAutomaticDelivery.integration.test.ts | 13 +- 2 files changed, 151 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 0077d99f03f4e..5260d90953b38 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -6315,6 +6315,146 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // A result can be held for a steer whose offer then fails; nothing else would end the turn. + it.effect("settles a held result when the steer offer fails", () => + Effect.gen(function* () { + let failOffer: (message: SDKUserMessage) => Effect.Effect = () => Effect.void; + const harness = yield* makeWakeHarnessWithOptions({ + offer: (message) => + message.priority === "next" ? Effect.suspend(() => failOffer(message)) : Effect.void, + }); + failOffer = () => + harness + .offerAndWait( + claudeSdkFrame({ + ...makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000002600", + result: "Built.", + }), + user_message_uuid: harness.promptUuid(0), + }), + ) + .pipe(Effect.andThen(Effect.die("the steer offer was lost"))); + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const attemptId = RunAttemptId.make("attempt-steer-offer-failed"); + const input = makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId, + text: "Run the slow build.", + attachments: [], + }); + yield* harness.runtime.startTurn(input); + const steered = yield* Effect.exit( + harness.runtime.steerTurn({ + threadId: harness.threadId, + runId: input.runId, + providerThread: harness.providerThread, + providerTurnId: idAllocator.derive.providerTurn({ + driver: ClaudeAdapterV2.CLAUDE_PROVIDER, + nativeTurnId: `turn:${attemptId}`, + }), + message: { + createdBy: "user", + creationSource: "web", + messageId: MessageId.make("message-steer-offer-failed"), + text: "Steer", + attachments: [], + }, + }), + ); + assert.isTrue(Exit.isFailure(steered)); + yield* harness.awaitTerminal(1); + assert.equal(harness.terminalEvents()[0]?.status, "completed"); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + // Stop can land while a result waits for a steer; Claude dropping that steer must not complete it. + it.effect("keeps a stopped turn interrupted when Claude drops the steer it held for", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const attemptId = RunAttemptId.make("attempt-steer-stopped"); + const providerTurnId = idAllocator.derive.providerTurn({ + driver: ClaudeAdapterV2.CLAUDE_PROVIDER, + nativeTurnId: `turn:${attemptId}`, + }); + const input = makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId, + text: "Run the build.", + attachments: [], + }); + yield* harness.runtime.startTurn(input); + yield* harness.runtime.steerTurn({ + threadId: harness.threadId, + runId: input.runId, + providerThread: harness.providerThread, + providerTurnId, + message: { + createdBy: "user", + creationSource: "web", + messageId: MessageId.make("message-steer-stopped"), + text: "Steer", + attachments: [], + }, + }); + yield* harness.offerAndWait( + claudeSdkFrame({ + ...makeResultFrame({ uuid: "00000000-0000-4000-8000-000000002700", result: "Built." }), + user_message_uuid: harness.promptUuid(0), + }), + ); + // Stop records the turn as interrupted before it waits for the process to close. + yield* harness.runtime + .interruptTurn({ providerThread: harness.providerThread, providerTurnId }) + .pipe(Effect.ignore, Effect.forkScoped({ startImmediately: true })); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "command_lifecycle", + command_uuid: harness.promptUuid(1), + state: "cancelled", + uuid: "00000000-0000-4000-8000-000000002701", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* Queue.shutdown(harness.sdkMessages); + yield* harness.awaitTerminal(1); + assert.deepEqual( + harness.terminalEvents().map((event) => event.status), + ["interrupted"], + ); + // Still unread: the orchestrator cancels it with the Stop notice when the run ends. + assert.deepEqual( + harness.events.flatMap((event) => (event.type === "steer.updated" ? [event.status] : [])), + ["pending"], + ); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + // regression: #34393 a provider switch replayed a $skill from the handoff history it.effect("dispatches only skills from the user's text, not from inline handoff history", () => Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts b/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts index 93177fffbdde8..d874e14812aff 100644 --- a/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts +++ b/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts @@ -15,6 +15,7 @@ import { ThreadId, type OrchestrationV2DomainEvent, } from "@t3tools/contracts"; +import * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -1042,7 +1043,7 @@ it.effect.each([ ] as const)("handles an unread steer when %s", (outcome) => Effect.gen(function* () { const cwd = yield* checkpointWorkspace("claude-unread-steer"); - const sdkMessages = yield* Queue.unbounded(); + const sdkMessages = yield* Queue.unbounded(); const offers: SDKUserMessage[] = []; const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({ instanceId: modelSelection.instanceId, @@ -1241,9 +1242,17 @@ it.effect.each([ event.payload.userMessageId === steerId && event.payload.status === "queued", ); - yield* Queue.shutdown(sdkMessages); + // The first prompt's reply completes; its result waits for the steer, then Claude exits. + yield* answer(offers[0]!); + yield* Queue.end(sdkMessages); yield* Fiber.join(requeued); yield* worker.drain(); + assert.equal( + (yield* orchestrator.getThreadProjection(threadId)).providerTurns.find( + (turn) => turn.runAttemptId === parent.activeAttemptId, + )?.turnTokenUsage?.usageStatus, + "partial", + ); // The provider failure holds the queue, the follow-up included, until someone resumes it. assert.deepEqual(yield* queuedOrder, [ [MessageId.make("before"), true], From ad054405592dcb39f2b22eb429ea080f0f72454a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 23:55:46 +0000 Subject: [PATCH 2/4] fix(server): Claude turns holding a result for an unread steer end and report correctly - Settle a held result when the steer offer fails, so the turn ends. - Keep a stopped turn interrupted when Claude then drops the held steer. - Count the held result's usage when the turn ends with no result. - Give each unread-steer requeue its own command id so a rejected one can be retried. Fixes pingdotgg/t3code#39433 Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.ts | 32 +++++++++++++------ .../src/orchestration-v2/Orchestrator.ts | 6 +++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 3e1178912bf80..5d99670016adf 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -9387,14 +9387,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( dropped.pdfReservation.refund(); yield* persistAttachmentBudget(context.nativeThreadId); } - if (context.pendingSteers.size === 0 && context.heldResult !== undefined) { - yield* finalizeActiveTurn({ - context, - status: "completed", - completedAt: yield* DateTime.now, - result: context.heldResult, - }); - } + yield* settleHeldResult(context); + }); + + // A result held for steers settles the turn once no steer is left for Claude to read. A + // stopped turn settles as interrupted when its process closes. + const settleHeldResult = Effect.fnUntraced(function* (context: ActiveClaudeTurnContext) { + if ( + context.pendingSteers.size > 0 || + context.heldResult === undefined || + (yield* Ref.get(interruptedTurns)).has(context.providerTurnId) + ) + return; + yield* finalizeActiveTurn({ + context, + status: "completed", + completedAt: yield* DateTime.now, + result: context.heldResult, + }); }); const finalizeActiveTurn = Effect.fnUntraced(function* (requested: { @@ -9415,6 +9425,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ? { ...requested, status: "failed" as const, failure: retryBudgetFailure } : requested; input.context.steersClosed = true; + // A turn that ends without a result still bills the reply its held result recorded. + if (input.result === undefined) absorbHeldResult(input.context); yield* reasoningDeltas.flushTurn(input.context.nativeTurnId); if (input.status === "completed") { yield* assistantDeltas.flushTurn(input.context.nativeTurnId); @@ -14066,7 +14078,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ).pipe( Effect.onError(() => currentTurn.pendingSteers.delete(steerUuid) - ? emitSteerStatus(currentTurn, messageId, "completed") + ? emitSteerStatus(currentTurn, messageId, "completed").pipe( + Effect.andThen(settleHeldResult(currentTurn)), + ) : Effect.void, ), ); diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 73cf6e9782a22..b8294919e312d 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -3549,7 +3549,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio for (const { run, message, steer, afterRunOrdinal } of placed) { yield* dispatchWithReceiptEffect({ type: "message.dispatch", - commandId: CommandId.make(`command:unread-steer:${run.id}:${message.id}`), + // Per attempt, so a rejected one does not block a retry. The pending item, read under the + // thread lock, is what keeps a requeued steer from being sent again. + commandId: CommandId.make( + `command:unread-steer:${run.id}:${message.id}:${DateTime.toEpochMillis(yield* DateTime.now)}`, + ), threadId, messageId: message.id, steerFollowUpOfRunId: run.id, From d5176b602f519cfef85a025c1dc00ca7110400e0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 00:04:19 +0000 Subject: [PATCH 3/4] fix(server): warn when a Claude API error ends a turn with a steer unread The live Claude process may still hold the steer, so it is not resent; the existing "Steer delivery not confirmed" notice now covers a failed turn as it does Stop. Refs pingdotgg/t3code#39433 Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 36 +++++++++++++++---- .../Adapters/ClaudeAdapterV2.ts | 6 ++-- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 5260d90953b38..5f4d351a3f660 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -5795,19 +5795,29 @@ describe("ClaudeAdapterV2 background wake turns", () => { { readBy: "none", echoes: true, + endedBy: "stop", title: "warns that a steer Claude never read before Stop was not delivered", }, { readBy: "none", echoes: false, + endedBy: "stop", title: "does not warn about a steer on a CLI that never echoes prompt uuids", }, { readBy: "assistant", echoes: true, + endedBy: "stop", title: "does not warn about a steer Claude read before Stop", }, - ] as const)("$title", ({ readBy, echoes }) => + // regression: #39433 an API error ended the turn and the unread steer was cancelled silently + { + readBy: "none", + echoes: true, + endedBy: "api_error", + title: "warns that a steer Claude never read before an API error was not delivered", + }, + ] as const)("$title", ({ readBy, echoes, endedBy }) => Effect.gen(function* () { const harness = yield* makeWakeHarnessWithOptions({ close: (sdkMessages) => Queue.shutdown(sdkMessages), @@ -5899,11 +5909,25 @@ describe("ClaudeAdapterV2 background wake turns", () => { ); } const noticesBefore = harness.events.length; - yield* harness.runtime.interruptTurn({ - providerThread: harness.providerThread, - providerTurnId, - }); - assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "interrupted"); + if (endedBy === "stop") { + yield* harness.runtime.interruptTurn({ + providerThread: harness.providerThread, + providerTurnId, + }); + } else { + yield* harness.offerAndWait( + makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000015704", + result: "API Error: 500", + terminalReason: "api_error", + isError: true, + }), + ); + } + assert.equal( + (yield* Queue.take(harness.terminalReceipts)).status, + endedBy === "stop" ? "interrupted" : "failed", + ); const notices = harness.events .slice(noticesBefore) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 5d99670016adf..e12396a06eee0 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -9588,8 +9588,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }); } - if (input.status === "interrupted") { + // A failed turn's live Claude may still hold the steer, so it is not resent; warn instead. + if (input.status === "interrupted" || input.status === "failed") { for (const [uuid, { text, liveQuery }] of droppedSteers) { + if (input.status === "failed" && liveQuery !== currentQuery) continue; // Only a CLI proven to echo prompt uuids can leave a steer unconfirmed. if ( liveQuery.promptEchoMode !== "early" && @@ -9598,7 +9600,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( continue; } const nativeItemId = `unread-steer:${uuid}`; - const notice = `Claude did not confirm reading your steer before Stop. It may not have been delivered. Resend it if needed: "${text}"`; + const notice = `Claude did not confirm reading your steer before ${input.status === "failed" ? "the turn failed" : "Stop"}. It may not have been delivered. Resend it if needed: "${text}"`; yield* emitProviderEvent({ type: "turn_item.updated", driver: CLAUDE_PROVIDER, From 86d095c5cde9ff2bd16d2aa86dee0c0570eda7ea Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 00:26:59 +0000 Subject: [PATCH 4/4] fix(server): a held Claude result settles only a turn that is still open Finalizing closes steers before it clears them; a steer offer failing in that window must not finalize the turn a second time. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index e12396a06eee0..e4c6fd654a7a2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -9394,6 +9394,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // stopped turn settles as interrupted when its process closes. const settleHeldResult = Effect.fnUntraced(function* (context: ActiveClaudeTurnContext) { if ( + context.steersClosed || context.pendingSteers.size > 0 || context.heldResult === undefined || (yield* Ref.get(interruptedTurns)).has(context.providerTurnId)