From 5ad2e8af28afbbc2fe161e25ac2157ea62944708 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 05:18:04 +0200 Subject: [PATCH] fix(orchestrator): decode direct Claude result blocks Co-authored-by: codex --- .../Adapters/ClaudeAdapterV2.test.ts | 81 +++++++++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 38 ++++----- 2 files changed, 101 insertions(+), 18 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 1918adc9e1..48f20b17fb 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -2157,6 +2157,87 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("extracts text from direct content-block subagent results", () => + Effect.scoped( + Effect.gen(function* () { + const SUBAGENT_TASK_ID = "task-direct-content-blocks"; + const SUBAGENT_TOOL_USE_ID = "toolu-direct-content-blocks"; + const harness = yield* makeWakeHarness; + const now = yield* DateTime.now; + const subagentEvents = () => + harness.events.filter( + (event): event is Extract => + event.type === "subagent.updated", + ); + + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make("attempt-claude-direct-content-blocks"), + text: "Delegate this task.", + attachments: [], + }), + ); + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: SUBAGENT_TASK_ID, + tool_use_id: SUBAGENT_TOOL_USE_ID, + description: "Delegated task", + subagent_type: "general-purpose", + task_type: "local_agent", + prompt: "Return the result.", + uuid: "00000000-0000-4000-8000-000000000206", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* awaitUntil(() => subagentEvents().length === 1, "subagent node created"); + + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "user", + message: { + role: "user", + content: [ + { + type: "tool_result", + tool_use_id: SUBAGENT_TOOL_USE_ID, + content: [ + { type: "text", text: "First line." }, + { type: "text", text: "Second line." }, + ], + }, + ], + }, + parent_tool_use_id: null, + uuid: "00000000-0000-4000-8000-000000000207", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* awaitUntil( + () => subagentEvents().at(-1)?.subagent.status === "completed", + "subagent terminal", + ); + + assert.equal(subagentEvents().at(-1)?.subagent.result, "First line.\nSecond line."); + + yield* Queue.offer( + harness.sdkMessages, + makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000000208", + result: "Delegation completed.", + }), + ); + yield* awaitUntil(() => harness.terminalEvents().length === 1, "turn terminal"); + }).pipe(Effect.provide(Layer.merge(idAllocatorLayer, NodeServices.layer))), + ), + ); + it.effect("releases the idle pin when a post-settle subagent stops without completing", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 3f9684c662..381a83340c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -1463,24 +1463,26 @@ function claudeNativeToolOutputText(output: ClaudeNativeToolOutput): string { function claudeSubagentResultText(output: ClaudeNativeToolOutput): string { const value = claudeNativeToolOutputValue(output); - if (typeof value === "object" && value !== null && "content" in value) { - const content = value.content; - if (Array.isArray(content)) { - const text = content - .flatMap((part) => - typeof part === "object" && - part !== null && - "type" in part && - part.type === "text" && - "text" in part && - typeof part.text === "string" - ? [part.text] - : [], - ) - .join("\n"); - if (text.length > 0) { - return text; - } + const content = Array.isArray(value) + ? value + : typeof value === "object" && value !== null && "content" in value + ? value.content + : undefined; + if (Array.isArray(content)) { + const text = content + .flatMap((part) => + typeof part === "object" && + part !== null && + "type" in part && + part.type === "text" && + "text" in part && + typeof part.text === "string" + ? [part.text] + : [], + ) + .join("\n"); + if (text.length > 0) { + return text; } } return claudeNativeToolOutputText(output);