From a28afd606bf2b865c1ec9ee60bf15e552dadb4f4 Mon Sep 17 00:00:00 2001 From: MadnessBubi <95427861+CUEB-QF-ZQS@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:54:53 +0800 Subject: [PATCH] fix(claude): preserve final-answer phases across continuations --- .../Adapters/ClaudeAdapterV2.test.ts | 198 ++++++++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 75 +++++++ 2 files changed, 273 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 5e855f3431203..b089d28b34551 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -3833,6 +3833,204 @@ describe("ClaudeAdapterV2 background wake turns", () => { }); const makeWakeHarness = makeWakeHarnessWithOptions(); + describe("Claude final answer phases", () => { + it.effect.each([ + "streamed", + "snapshot", + "late snapshot", + "repeated snapshot", + "result only", + ] as const)("preserves confirmed final-answer intent from %s delivery", (delivery) => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make(`answer-phase-${delivery}`), + text: "Explain the result, then check your work.", + attachments: [], + }), + ); + const snapshot = (id: string, value: string, stopReason: string | null, suffix = "") => + claudeSdkFrame({ + type: "assistant", + uuid: `${id}-snapshot${suffix}`, + session_id: WAKE_NATIVE_SESSION, + parent_tool_use_id: null, + message: { + id, + model: "claude-sonnet-4-6", + stop_reason: stopReason, + content: value ? [{ type: "text", text: value }] : [], + }, + }); + const stream = (event: unknown) => + claudeSdkFrame({ + type: "stream_event", + event, + parent_tool_use_id: null, + session_id: WAKE_NATIVE_SESSION, + uuid: "answer-phase-stream", + }); + if (delivery !== "result only") { + yield* harness.offerAndWait(snapshot("progress", "Checking.", "tool_use")); + if (delivery === "streamed") { + yield* harness.offerAndWait( + stream({ type: "message_start", message: { id: "answer" } }), + ); + yield* harness.offerAndWait( + stream({ + type: "content_block_start", + index: 0, + content_block: { type: "text", text: "" }, + }), + ); + yield* harness.offerAndWait( + stream({ + type: "content_block_delta", + index: 0, + delta: { type: "text_delta", text: "The complete answer." }, + }), + ); + yield* harness.offerAndWait(stream({ type: "content_block_stop", index: 0 })); + } + yield* harness.offerAndWait( + snapshot("answer", "The complete answer.", delivery === "snapshot" ? "end_turn" : null), + ); + if (delivery === "streamed") { + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "stream_event", + event: { type: "message_delta", delta: { stop_reason: "end_turn" } }, + parent_tool_use_id: "unrelated-subagent", + session_id: WAKE_NATIVE_SESSION, + uuid: "nested-completion", + api_message_id: "answer", + }), + ); + yield* harness.until(() => + harness.events.some( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.text === "The complete answer.", + ), + ); + const beforeRootCompletion = harness.events + .flatMap((event) => + event.type === "turn_item.updated" && event.turnItem.type === "assistant_message" + ? [event.turnItem] + : [], + ) + .findLast((item) => item.text === "The complete answer."); + assert.isDefined(beforeRootCompletion); + assert.equal(beforeRootCompletion?.phase, undefined); + yield* harness.offerAndWait( + stream({ type: "message_start", message: { id: "next-message" } }), + ); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "stream_event", + event: { type: "message_delta", delta: { stop_reason: "end_turn" } }, + parent_tool_use_id: null, + session_id: WAKE_NATIVE_SESSION, + uuid: "late-completion", + api_message_id: "answer", + }), + ); + } else if (delivery === "late snapshot" || delivery === "repeated snapshot") { + yield* harness.offerAndWait( + snapshot("answer", "", "end_turn", delivery === "late snapshot" ? "-metadata" : ""), + ); + } + yield* harness.offerAndWait( + snapshot("later-answer", "Verification finished.", "end_turn"), + ); + } + yield* harness.offerAndWait( + makeResultFrame({ + uuid: "answer-phase-result", + result: delivery === "result only" ? "The complete answer." : "Verification finished.", + }), + ); + yield* Queue.take(harness.terminalReceipts); + const latestByText = new Map( + harness.events.flatMap((event) => + event.type === "turn_item.updated" && event.turnItem.type === "assistant_message" + ? [[event.turnItem.text, event.turnItem] as const] + : [], + ), + ); + assert.equal(latestByText.get("The complete answer.")?.phase, "final_answer"); + if (delivery !== "result only") { + assert.equal(latestByText.get("Checking.")?.phase, undefined); + assert.equal(latestByText.get("Verification finished.")?.phase, "final_answer"); + } + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect.each(["tool_use", "max_tokens", "stop_sequence", null] as const)( + "does not confirm a reply that ended with %s", + (stopReason) => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("unconfirmed-answer-phase"), + text: "Explain the result.", + attachments: [], + }), + ); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "assistant", + uuid: "unconfirmed-answer", + session_id: WAKE_NATIVE_SESSION, + parent_tool_use_id: null, + message: { + id: "unconfirmed-message", + model: "claude-sonnet-4-6", + stop_reason: stopReason, + content: [{ type: "text", text: "Unconfirmed text." }], + }, + }), + ); + yield* harness.offerAndWait(makeResultFrame({ uuid: "unconfirmed-result", result: "" })); + yield* Queue.take(harness.terminalReceipts); + for (const event of harness.events) { + if (event.type === "turn_item.updated" && event.turnItem.type === "assistant_message") { + assert.equal(event.turnItem.phase, undefined); + } + } + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + }); + describe("ClaudeAdapterV2 session title sync", () => { type TitleSyncHarness = Effect.Success>; const startTurn = Effect.fnUntraced(function* ( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 6d6cc1fee8c95..2b1dd769c811b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4854,6 +4854,7 @@ function buildAssistantArtifacts(input: { readonly text: string; readonly ordinal: number; readonly status?: "running" | "completed" | "interrupted"; + readonly phase?: "final_answer"; readonly startedAt: DateTime.Utc; readonly completedAt: DateTime.Utc; // A streamed message keeps its first timestamp across updates. @@ -4933,6 +4934,7 @@ function buildAssistantArtifacts(input: { updatedAt: input.completedAt, type: "assistant_message", messageId, + ...(input.phase === undefined ? {} : { phase: input.phase }), text: input.text, streaming, }, @@ -5003,10 +5005,16 @@ interface ActiveClaudeTurnContext { readonly streamBlocks: Map; readonly nextBlockIndex: Map; readonly snapshotBlockIndex: Map; + readonly finalAnswerMessageIds: Set; + readonly itemsByNativeMessage: Map< + string, + Map> + >; readonly streamItems: Map< string, { readonly startedAt: DateTime.Utc; + readonly nativeMessageId: string; status: "completed" | "interrupted"; // Set when the block stops; the snapshot only corrects it. text: string | null; @@ -5014,6 +5022,7 @@ interface ActiveClaudeTurnContext { >; // The root reply the latest assistant frame carried; a steer's aborted_streaming cuts it off. lastReply: { + readonly nativeMessageId?: string; readonly nativeItemId: string; readonly text: string; readonly startedAt: DateTime.Utc; @@ -9082,6 +9091,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const publishAssistantArtifacts = Effect.fnUntraced(function* (input: { readonly context: ActiveClaudeTurnContext; + readonly nativeMessageId?: string; readonly nativeItemId: string; readonly text: string; readonly status: "running" | "completed" | "interrupted"; @@ -9098,10 +9108,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( text: input.text, ordinal, status: input.status, + ...(input.status === "completed" && + input.nativeMessageId !== undefined && + input.context.assistant.finalAnswerMessageIds.has(input.nativeMessageId) + ? { phase: "final_answer" as const } + : {}), startedAt: input.startedAt, completedAt: input.completedAt, ...(input.createdAt === undefined ? {} : { createdAt: input.createdAt }), }); + if ( + input.nativeMessageId !== undefined && + artifacts.turnItem.type === "assistant_message" + ) { + const items = + input.context.assistant.itemsByNativeMessage.get(input.nativeMessageId) ?? new Map(); + items.set(input.nativeItemId, artifacts.turnItem); + input.context.assistant.itemsByNativeMessage.set(input.nativeMessageId, items); + } yield* Effect.all( [ emitProviderEvent({ @@ -9124,6 +9148,29 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ); }); + const confirmFinalAnswer = Effect.fnUntraced(function* ( + context: ActiveClaudeTurnContext, + nativeMessageId: string, + ) { + if (context.assistant.finalAnswerMessageIds.has(nativeMessageId)) return; + context.assistant.finalAnswerMessageIds.add(nativeMessageId); + // The SDK can deliver end_turn after the completed text snapshot. + const items = context.assistant.itemsByNativeMessage.get(nativeMessageId); + if (items === undefined) return; + const now = yield* DateTime.now; + for (const [nativeItemId, item] of items) { + if (item.status !== "completed" || item.streaming || item.phase === "final_answer") + continue; + const turnItem = { ...item, phase: "final_answer" as const, updatedAt: now }; + items.set(nativeItemId, turnItem); + yield* emitProviderEvent({ + type: "turn_item.updated", + driver: CLAUDE_PROVIDER, + turnItem, + }); + } + }); + const assistantDeltas = yield* makeProviderTextDeltaCoalescer({ flushIntervalMs: 50, emit: @@ -9137,6 +9184,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( yield* publishAssistantArtifacts({ context, nativeItemId: update.itemId, + nativeMessageId: item.nativeMessageId, text: update.text, status: update.completed ? item.status : "running", startedAt: item.startedAt, @@ -9354,6 +9402,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( providerTurnId: input.context.providerTurnId, nativeItemId: input.context.assistant.fallbackNativeItemId, text: input.context.assistant.fallbackText, + ...(input.status === "completed" && + input.result?.subtype === "success" && + !input.result.is_error && + input.result.stop_reason === "end_turn" + ? { phase: "final_answer" as const } + : {}), ordinal, startedAt: input.context.startedAt, completedAt: input.completedAt, @@ -9637,6 +9691,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const emitAssistantTextArtifacts = Effect.fnUntraced(function* (input: { readonly context: ActiveClaudeTurnContext; + readonly nativeMessageId?: string; readonly nativeItemId: string; readonly text: string; }) { @@ -10714,6 +10769,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( usage === undefined ? null : { messageId: event.message.id, usage }; if (usage !== undefined) yield* emitContextUsage(context, usage, true); } else if (event.type === "message_delta") { + const nativeMessageId = text(field(message, "api_message_id")) ?? reasoning.messageId; + if (event.delta.stop_reason === "end_turn" && nativeMessageId !== null) { + yield* confirmFinalAnswer(context, nativeMessageId); + } const usage: typeof event.usage | undefined = event.usage; const stream = context.streamUsage; if (stream?.usage && usage !== undefined) { @@ -10759,6 +10818,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( assistant.streamBlocks.set(event.index, itemId); assistant.lastReply = null; assistant.streamItems.set(itemId, { + nativeMessageId: reasoning.messageId, startedAt: yield* DateTime.now, status: "completed", text: null, @@ -10804,6 +10864,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } return; } + if ( + message.type === "assistant" && + !message.parent_tool_use_id && + message.message.stop_reason === "end_turn" && + message.message.model !== "" && + field(message, "error") === undefined + ) { + yield* confirmFinalAnswer(context, message.message.id); + } // Snapshot text the stream did not deliver, once any of it streamed. const unstreamedSnapshotText: Array = []; let streamedSnapshotReply: NonNullable< @@ -10838,6 +10907,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( streamed.text = text; if (text.length > 0) { streamedSnapshotReply = { + nativeMessageId: message.message.id, nativeItemId: itemId, text, startedAt: streamed.startedAt, @@ -10858,6 +10928,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( yield* emitAssistantTextArtifacts({ context, nativeItemId: update.itemId, + nativeMessageId: update.messageId, text: update.text, }); } @@ -11771,11 +11842,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const startedAt = yield* emitAssistantTextArtifacts({ context, nativeItemId: assistantText.nativeItemId, + ...(message.type === "assistant" ? { nativeMessageId: message.message.id } : {}), text, }); if (startedAt !== undefined) { context.assistant.lastReply = { nativeItemId: assistantText.nativeItemId, + ...(message.type === "assistant" ? { nativeMessageId: message.message.id } : {}), text, startedAt, }; @@ -13281,6 +13354,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( streamBlocks: new Map(), nextBlockIndex: new Map(), snapshotBlockIndex: new Map(), + finalAnswerMessageIds: new Set(), + itemsByNativeMessage: new Map(), streamItems: new Map(), lastReply: null, noticeTexts: new Set(),