diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 0077d99f03f4..5f4d351a3f66 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) @@ -6315,6 +6339,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/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 3e1178912bf8..e4c6fd654a7a 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -9387,14 +9387,25 @@ 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.steersClosed || + 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 +9426,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); @@ -9576,8 +9589,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" && @@ -9586,7 +9601,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, @@ -14066,7 +14081,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/ClaudeAutomaticDelivery.integration.test.ts b/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts index 93177fffbdde..d874e14812af 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], diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 73cf6e9782a2..b8294919e312 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,