@devries-cameron20 / t3code Mirror · read-only / commits / 1a5d3737b7
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 21, 19:45 UTC

fix(server): Claude replies stream paragraph by paragraph again (#35037)

Julius Marminge committed Nov 12, 2026 at 02:29 UTC 1a5d3737b7ed60f5372752f0e2607f24c66122ef
2 files changed +290 -53
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+88
@@ -6832,6 +6832,94 @@ describe("ClaudeAdapterV2 background wake turns", () => {
6832 ),
6833 );
6834
6835 + // regression: #17270 root text_delta frames stream the reply before the block's snapshot
6836 + it.effect("streams Claude assistant text deltas before the completed snapshot", () =>
6837 + Effect.gen(function* () {
6838 + const harness = yield* makeWakeHarness;
6839 + yield* harness.runtime.startTurn(
6840 + makeClaudeTestTurnInput({
6841 + threadId: harness.threadId,
6842 + providerThread: harness.providerThread,
6843 + now: yield* DateTime.now,
6844 + attemptId: RunAttemptId.make("text-stream-attempt"),
6845 + text: "Write two paragraphs",
6846 + attachments: [],
6847 + }),
6848 + );
6849 + const stream = (event: unknown) =>
6850 + claudeSdkFrame({
6851 + type: "stream_event",
6852 + event,
6853 + parent_tool_use_id: null,
6854 + session_id: WAKE_NATIVE_SESSION,
6855 + uuid: "stream-frame",
6856 + });
6857 + yield* harness.offerAndWait(
6858 + stream({ type: "message_start", message: { id: "text-message" } }),
6859 + );
6860 + yield* harness.offerAndWait(
6861 + stream({
6862 + type: "content_block_start",
6863 + index: 0,
6864 + content_block: { type: "text", text: "" },
6865 + }),
6866 + );
6867 + yield* harness.offerAndWait(
6868 + stream({
6869 + type: "content_block_delta",
6870 + index: 0,
6871 + delta: { type: "text_delta", text: "First paragraph.\n\n" },
6872 + }),
6873 + );
6874 + yield* harness.offerAndWait(
6875 + stream({
6876 + type: "content_block_delta",
6877 + index: 0,
6878 + delta: { type: "text_delta", text: "Second paragraph." },
6879 + }),
6880 + );
6881 + yield* harness.offerAndWait(stream({ type: "content_block_stop", index: 0 }));
6882 + yield* Queue.offer(
6883 + harness.sdkMessages,
6884 + claudeSdkFrame({
6885 + type: "assistant",
6886 + uuid: "text-snapshot",
6887 + session_id: WAKE_NATIVE_SESSION,
6888 + parent_tool_use_id: null,
6889 + message: {
6890 + id: "text-message",
6891 + model: "claude-sonnet-4-6",
6892 + content: [{ type: "text", text: "First paragraph.\n\nSecond paragraph." }],
6893 + },
6894 + }),
6895 + );
6896 + yield* Queue.offer(harness.sdkMessages, makeResultFrame({ uuid: "text-result", result: "" }));
6897 + yield* Queue.take(harness.terminalReceipts);
6898 + const replies = harness.events.flatMap((event) =>
6899 + event.type === "turn_item.updated" && event.turnItem.type === "assistant_message"
6900 + ? [event.turnItem]
6901 + : [],
6902 + );
6903 + // The stream's own item carries the reply; the block's snapshot does not repeat it.
6904 + assert.deepEqual(
6905 + [...new Set(replies.map((item) => item.nativeItemRef?.nativeId))],
6906 + ["text-message:text:0"],
6907 + );
6908 + assert.equal(replies.at(-1)?.text, "First paragraph.\n\nSecond paragraph.");
6909 + assert.equal(replies.at(-1)?.status, "completed");
6910 + }).pipe(
6911 + Effect.scoped,
6912 + Effect.provide(
6913 + Layer.mergeAll(
6914 + IdAllocator.layer,
6915 + McpProviderSessions.layer,
6916 + NodeServices.layer,
6917 + NodeCrypto.layer,
6918 + ),
6919 + ),
6920 + ),
6921 + );
6922 +
6923 // regression: #15892 non-execution metadata is carried through, not guessed from result text
6924 it.effect.each(["cancelled", "interrupted", "denied", "permission_denied", undefined])(
6925 "preserves native tool non-execution metadata %s without inferring a denial from text",
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+202 -53
@@ -4516,9 +4516,11 @@ function buildAssistantArtifacts(input: {
4516 readonly nativeItemId: string;
4517 readonly text: string;
4518 readonly ordinal: number;
4519 - readonly status?: "completed" | "interrupted";
4519 + readonly status?: "running" | "completed" | "interrupted";
4520 readonly startedAt: DateTime.Utc;
4521 readonly completedAt: DateTime.Utc;
4522 + // A streamed message keeps its first timestamp across updates.
4523 + readonly createdAt?: DateTime.Utc;
4524 }): {
4525 readonly node: OrchestrationV2ExecutionNode;
4526 readonly message: OrchestrationV2ConversationMessage;
@@ -4541,6 +4543,9 @@ function buildAssistantArtifacts(input: {
4543 nativeId: input.nativeItemId,
4544 strength: "strong" as const,
4545 };
4546 + const status = input.status ?? "completed";
4547 + const streaming = status === "running";
4548 + const completedAt = streaming ? null : input.completedAt;
4549
4550 return {
4551 node: {
@@ -4550,7 +4555,7 @@ function buildAssistantArtifacts(input: {
4555 parentNodeId: input.turnInput.rootNodeId,
4556 rootNodeId: input.turnInput.rootNodeId,
4557 kind: "assistant_message",
4553 - status: input.status ?? "completed",
4558 + status,
4559 countsForRun: false,
4560 providerThreadId: input.turnInput.providerThread.id,
4561 providerTurnId: input.providerTurnId,
@@ -4558,7 +4563,7 @@ function buildAssistantArtifacts(input: {
4563 runtimeRequestId: null,
4564 checkpointScopeId: null,
4565 startedAt: input.startedAt,
4561 - completedAt: input.completedAt,
4566 + completedAt,
4567 },
4568 message: {
4569 createdBy: "agent",
@@ -4570,8 +4575,8 @@ function buildAssistantArtifacts(input: {
4575 role: "assistant",
4576 text: input.text,
4577 attachments: [],
4573 - streaming: false,
4574 - createdAt: input.completedAt,
4578 + streaming,
4579 + createdAt: input.createdAt ?? input.completedAt,
4580 updatedAt: input.completedAt,
4581 },
4582 turnItem: {
@@ -4584,15 +4589,15 @@ function buildAssistantArtifacts(input: {
4589 nativeItemRef,
4590 parentItemId: null,
4591 ordinal: input.ordinal,
4587 - status: input.status ?? "completed",
4592 + status,
4593 title: null,
4594 startedAt: input.startedAt,
4590 - completedAt: input.completedAt,
4595 + completedAt,
4596 updatedAt: input.completedAt,
4597 type: "assistant_message",
4598 messageId,
4599 text: input.text,
4595 - streaming: false,
4600 + streaming,
4601 },
4602 };
4603 }
@@ -4654,6 +4659,19 @@ interface ActiveClaudeTurnContext {
4659 fallbackText: string;
4660 fallbackNativeItemId: string;
4661 emittedNativeItemIds: Set<string>;
4662 + // Text blocks stream as `${message.id}:text:${n}`; the block's snapshot has its own uuid.
4663 + readonly streamBlocks: Map<number, string>;
4664 + readonly nextBlockIndex: Map<string, number>;
4665 + readonly snapshotBlockIndex: Map<string, number>;
4666 + readonly streamItems: Map<
4667 + string,
4668 + {
4669 + readonly startedAt: DateTime.Utc;
4670 + status: "completed" | "interrupted";
4671 + // Set when the block stops; the snapshot only corrects it.
4672 + text: string | null;
4673 + }
4674 + >;
4675 // The root reply the latest assistant frame carried; a steer's aborted_streaming cuts it off.
4676 lastReply: {
4677 readonly nativeItemId: string;
@@ -8400,6 +8418,78 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8418 }),
8419 });
8420
8421 + const publishAssistantArtifacts = Effect.fnUntraced(function* (input: {
8422 + readonly context: ActiveClaudeTurnContext;
8423 + readonly nativeItemId: string;
8424 + readonly text: string;
8425 + readonly status: "running" | "completed" | "interrupted";
8426 + readonly startedAt: DateTime.Utc;
8427 + readonly completedAt: DateTime.Utc;
8428 + readonly createdAt?: DateTime.Utc;
8429 + }) {
8430 + const ordinal = yield* resolveItemOrdinal(input.context, input.nativeItemId);
8431 + const artifacts = buildAssistantArtifacts({
8432 + idAllocator,
8433 + turnInput: input.context.input,
8434 + providerTurnId: input.context.providerTurnId,
8435 + nativeItemId: input.nativeItemId,
8436 + text: input.text,
8437 + ordinal,
8438 + status: input.status,
8439 + startedAt: input.startedAt,
8440 + completedAt: input.completedAt,
8441 + ...(input.createdAt === undefined ? {} : { createdAt: input.createdAt }),
8442 + });
8443 + yield* Effect.all(
8444 + [
8445 + emitProviderEvent({
8446 + type: "node.updated",
8447 + driver: CLAUDE_PROVIDER,
8448 + node: artifacts.node,
8449 + }),
8450 + emitProviderEvent({
8451 + type: "message.updated",
8452 + driver: CLAUDE_PROVIDER,
8453 + message: artifacts.message,
8454 + }),
8455 + emitProviderEvent({
8456 + type: "turn_item.updated",
8457 + driver: CLAUDE_PROVIDER,
8458 + turnItem: artifacts.turnItem,
8459 + }),
8460 + ],
8461 + { concurrency: 1 },
8462 + );
8463 + });
8464 +
8465 + const assistantDeltas = yield* makeProviderTextDeltaCoalescer({
8466 + flushIntervalMs: 50,
8467 + emit:
8468 + /* Provider events include streaming deltas; avoid a span for each update. */
8469 + Effect.fnUntraced(function* (update) {
8470 + const context = yield* Ref.get(activeTurn);
8471 + if (context === null || context.nativeTurnId !== update.turnId) return;
8472 + const item = context.assistant.streamItems.get(update.itemId);
8473 + if (item === undefined || update.text.length === 0) return;
8474 + context.assistant.emittedNativeItemIds.add(update.itemId);
8475 + yield* publishAssistantArtifacts({
8476 + context,
8477 + nativeItemId: update.itemId,
8478 + text: update.text,
8479 + status: update.completed ? item.status : "running",
8480 + startedAt: item.startedAt,
8481 + createdAt: item.startedAt,
8482 + completedAt: yield* DateTime.now,
8483 + });
8484 + }),
8485 + });
8486 +
8487 + // Streamed text no snapshot will complete ends where the stream stopped.
8488 + const cutOffStreamedText = Effect.fnUntraced(function* (context: ActiveClaudeTurnContext) {
8489 + for (const item of context.assistant.streamItems.values()) item.status = "interrupted";
8490 + yield* assistantDeltas.flushTurn(context.nativeTurnId);
8491 + });
8492 +
8493 const endToolCalls = Effect.fnUntraced(function* (input: {
8494 readonly context: ActiveClaudeTurnContext | null;
8495 readonly toolCalls: ReadonlyArray<ActiveClaudeToolCall>;
@@ -8497,6 +8587,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8587 }) {
8588 input.context.steersClosed = true;
8589 yield* reasoningDeltas.flushTurn(input.context.nativeTurnId);
8590 + if (input.status === "completed") {
8591 + yield* assistantDeltas.flushTurn(input.context.nativeTurnId);
8592 + } else {
8593 + yield* cutOffStreamedText(input.context);
8594 + }
8595 // A turn that completes has taken in every steer; one that stops drops them unread.
8596 const droppedSteers = [...input.context.pendingSteers];
8597 input.context.pendingSteers.clear();
@@ -8863,48 +8958,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8958 });
8959 });
8960
8866 - const publishAssistantArtifacts = Effect.fnUntraced(function* (input: {
8867 - readonly context: ActiveClaudeTurnContext;
8868 - readonly nativeItemId: string;
8869 - readonly text: string;
8870 - readonly status: "completed" | "interrupted";
8871 - readonly startedAt: DateTime.Utc;
8872 - readonly completedAt: DateTime.Utc;
8873 - }) {
8874 - const ordinal = yield* resolveItemOrdinal(input.context, input.nativeItemId);
8875 - const artifacts = buildAssistantArtifacts({
8876 - idAllocator,
8877 - turnInput: input.context.input,
8878 - providerTurnId: input.context.providerTurnId,
8879 - nativeItemId: input.nativeItemId,
8880 - text: input.text,
8881 - ordinal,
8882 - status: input.status,
8883 - startedAt: input.startedAt,
8884 - completedAt: input.completedAt,
8885 - });
8886 - yield* Effect.all(
8887 - [
8888 - emitProviderEvent({
8889 - type: "node.updated",
8890 - driver: CLAUDE_PROVIDER,
8891 - node: artifacts.node,
8892 - }),
8893 - emitProviderEvent({
8894 - type: "message.updated",
8895 - driver: CLAUDE_PROVIDER,
8896 - message: artifacts.message,
8897 - }),
8898 - emitProviderEvent({
8899 - type: "turn_item.updated",
8900 - driver: CLAUDE_PROVIDER,
8901 - turnItem: artifacts.turnItem,
8902 - }),
8903 - ],
8904 - { concurrency: 1 },
8905 - );
8906 - });
8907 -
8961 const emitAssistantTextArtifacts = Effect.fnUntraced(function* (input: {
8962 readonly context: ActiveClaudeTurnContext;
8963 readonly nativeItemId: string;
@@ -9877,6 +9930,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9930 if (event.type === "message_start") {
9931 reasoning.messageId = event.message.id;
9932 reasoning.streamBlocks.clear();
9933 + context.assistant.streamBlocks.clear();
9934 const usage: ClaudeMessageUsage | undefined = event.message.usage;
9935 context.streamUsage =
9936 usage === undefined ? null : { messageId: event.message.id, usage };
@@ -9915,6 +9969,37 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9969 delta: event.delta.thinking,
9970 });
9971 }
9972 + } else if (
9973 + event.type === "content_block_start" &&
9974 + event.content_block.type === "text" &&
9975 + reasoning.messageId !== null
9976 + ) {
9977 + const assistant = context.assistant;
9978 + const index = assistant.nextBlockIndex.get(reasoning.messageId) ?? 0;
9979 + assistant.nextBlockIndex.set(reasoning.messageId, index + 1);
9980 + const itemId = `${reasoning.messageId}:text:${index}`;
9981 + assistant.streamBlocks.set(event.index, itemId);
9982 + assistant.lastReply = null;
9983 + assistant.streamItems.set(itemId, {
9984 + startedAt: yield* DateTime.now,
9985 + status: "completed",
9986 + text: null,
9987 + });
9988 + yield* resolveItemOrdinal(context, itemId);
9989 + yield* assistantDeltas.append({
9990 + turnId: context.nativeTurnId,
9991 + itemId,
9992 + delta: event.content_block.text,
9993 + });
9994 + } else if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
9995 + const itemId = context.assistant.streamBlocks.get(event.index);
9996 + if (itemId !== undefined) {
9997 + yield* assistantDeltas.append({
9998 + turnId: context.nativeTurnId,
9999 + itemId,
10000 + delta: event.delta.text,
10001 + });
10002 + }
10003 } else if (event.type === "content_block_stop") {
10004 const itemId = reasoning.streamBlocks.get(event.index);
10005 if (itemId !== undefined) {
@@ -9925,9 +10010,27 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10010 });
10011 reasoning.streamBlocks.delete(event.index);
10012 }
10013 + const textItemId = context.assistant.streamBlocks.get(event.index);
10014 + const textItem =
10015 + textItemId === undefined
10016 + ? undefined
10017 + : context.assistant.streamItems.get(textItemId);
10018 + if (textItemId !== undefined && textItem !== undefined) {
10019 + textItem.text = yield* assistantDeltas.complete({
10020 + turnId: context.nativeTurnId,
10021 + itemId: textItemId,
10022 + emitEmpty: false,
10023 + });
10024 + context.assistant.streamBlocks.delete(event.index);
10025 + }
10026 }
10027 return;
10028 }
10029 + // Snapshot text the stream did not deliver, once any of it streamed.
10030 + const unstreamedSnapshotText: Array<string> = [];
10031 + let streamedSnapshotReply: NonNullable<
10032 + ActiveClaudeTurnContext["assistant"]["lastReply"]
10033 + > | null = null;
10034 if (
10035 message.type === "assistant" &&
10036 !message.parent_tool_use_id &&
@@ -9936,6 +10039,34 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10039 context.reasoning.snapshots.add(message.uuid);
10040 // The SDK snapshots each completed block; count thinking blocks separately to preserve streamed ids.
10041 for (const block of message.message.content) {
10042 + if (block.type === "text") {
10043 + const assistant = context.assistant;
10044 + const index = assistant.snapshotBlockIndex.get(message.message.id) ?? 0;
10045 + assistant.snapshotBlockIndex.set(message.message.id, index + 1);
10046 + const itemId = `${message.message.id}:text:${index}`;
10047 + const streamed = assistant.streamItems.get(itemId);
10048 + if (streamed === undefined) {
10049 + unstreamedSnapshotText.push(block.text);
10050 + } else {
10051 + const text =
10052 + streamed.text !== null && (!block.text || block.text === streamed.text)
10053 + ? streamed.text
10054 + : yield* assistantDeltas.complete({
10055 + turnId: context.nativeTurnId,
10056 + itemId,
10057 + ...(block.text ? { finalText: block.text } : {}),
10058 + emitEmpty: false,
10059 + });
10060 + streamed.text = text;
10061 + if (text.length > 0) {
10062 + streamedSnapshotReply = {
10063 + nativeItemId: itemId,
10064 + text,
10065 + startedAt: streamed.startedAt,
10066 + };
10067 + }
10068 + }
10069 + }
10070 if (block.type !== "thinking") {
10071 const update = context.reasoning.lastThinking;
10072 context.reasoning.lastThinking = null;
@@ -9975,6 +10106,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10106 }
10107 : null;
10108 }
10109 + // A repeated snapshot frame must not re-emit streamed text under its uuid.
10110 + if (streamedSnapshotReply !== null && unstreamedSnapshotText.join("").length === 0) {
10111 + context.assistant.emittedNativeItemIds.add(message.uuid);
10112 + }
10113 }
10114
10115 if (message.type === "assistant" && input.replayed !== true) {
@@ -10752,7 +10887,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10887 }
10888 const assistantText = assistantTextFromSdkMessage(message);
10889 if (message.type === "assistant" && assistantParentToolUseId === null) {
10755 - context.assistant.lastReply = null;
10890 + context.assistant.lastReply = streamedSnapshotReply;
10891 }
10892 if (
10893 assistantText !== null &&
@@ -10802,13 +10937,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10937 return;
10938 }
10939 if (assistantText !== null && assistantText.text.length > 0) {
10940 + const text =
10941 + streamedSnapshotReply === null ? assistantText.text : unstreamedSnapshotText.join("");
10942 + if (text.length === 0) return;
10943 const startedAt = yield* emitAssistantTextArtifacts({
10944 context,
10945 nativeItemId: assistantText.nativeItemId,
10808 - text: assistantText.text,
10946 + text,
10947 });
10948 if (startedAt !== undefined) {
10811 - context.assistant.lastReply = { ...assistantText, startedAt };
10949 + context.assistant.lastReply = {
10950 + nativeItemId: assistantText.nativeItemId,
10951 + text,
10952 + startedAt,
10953 + };
10954 }
10955 return;
10956 }
@@ -10884,6 +11026,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
11026 if (!interrupted && wasSteered && isClaudeActiveSteeringAbortResult(message)) {
11027 const cutOffReply = context.assistant.lastReply;
11028 context.assistant.lastReply = null;
11029 + if (message.terminal_reason === "aborted_streaming") {
11030 + yield* cutOffStreamedText(context);
11031 + }
11032 if (message.terminal_reason === "aborted_streaming" && cutOffReply !== null) {
11033 yield* publishAssistantArtifacts({
11034 context,
@@ -12188,6 +12333,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12333 fallbackText: "",
12334 fallbackNativeItemId: `assistant:${turnInput.runId}`,
12335 emittedNativeItemIds: new Set(),
12336 + streamBlocks: new Map(),
12337 + nextBlockIndex: new Map(),
12338 + snapshotBlockIndex: new Map(),
12339 + streamItems: new Map(),
12340 lastReply: null,
12341 noticeTexts: new Set(),
12342 },