From d0a4d2ee5f3f50e5879d29e0622d366c21831e41 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 18:43:44 +0000 Subject: [PATCH 1/4] test(server): a forked Claude skill's commands reach its subagent thread Repro for #43001. Replays the frame order Claude Code sends for a `context: fork` skill: task_started without tool_use_id, a task_notification whose summary is the skill name, then the fork's frames under parent_tool_use_id "forked-command-". Fails on main because those frames never reach the subagent thread. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index a7b110e851f0a..c64ccc609e581 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -22241,6 +22241,130 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // Frame order captured from Claude Code 2.1.332: the fork's work arrives only after task_notification. + it.effect("shows a forked skill's commands and reply in its subagent thread", () => + 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("attempt-forked-skill-activity"), + text: "/fork-probe go", + attachments: [], + }), + ); + const forkParent = "forked-command-fork-probe"; + for (const frame of [ + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: "task-forked-activity", + description: "/fork-probe", + prompt: "go", + subagent_type: "general-purpose", + task_type: "local_agent", + skip_transcript: true, + ambient: true, + uuid: "00000000-0000-4000-8000-000000000a01", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "system", + subtype: "task_notification", + task_id: "task-forked-activity", + status: "completed", + summary: "/fork-probe", + output_file: "", + uuid: "00000000-0000-4000-8000-000000000a02", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "user", + parent_tool_use_id: forkParent, + message: { role: "user", content: [{ type: "text", text: "go" }] }, + uuid: "00000000-0000-4000-8000-000000000a03", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "assistant", + parent_tool_use_id: forkParent, + message: { + model: "claude-haiku-5-5", + id: "msg_fork_bash", + type: "message", + role: "assistant", + content: [ + { + type: "tool_use", + id: "toolu-fork-bash", + name: "Bash", + input: { command: "cat hello.txt", description: "Read hello.txt" }, + }, + ], + }, + uuid: "00000000-0000-4000-8000-000000000a04", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "user", + parent_tool_use_id: forkParent, + message: { + role: "user", + content: [ + { type: "tool_result", tool_use_id: "toolu-fork-bash", content: "greetings" }, + ], + }, + uuid: "00000000-0000-4000-8000-000000000a05", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "assistant", + parent_tool_use_id: forkParent, + message: { + model: "claude-haiku-5-5", + id: "msg_fork_reply", + type: "message", + role: "assistant", + content: [{ type: "text", text: "FORK-REPORT greetings" }], + }, + uuid: "00000000-0000-4000-8000-000000000a06", + session_id: WAKE_NATIVE_SESSION, + }), + makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000000a07", + result: "FORK-REPORT greetings", + }), + ]) { + yield* Queue.offer(harness.sdkMessages, frame); + } + yield* Queue.take(harness.terminalReceipts); + + const routing = subagentRouting(harness.events, ["toolu-fork-bash"]); + assert.isDefined(routing.childThreadId); + assert.deepEqual( + [...(routing.toolThreadIds.get("toolu-fork-bash") ?? [])], + [routing.childThreadId], + "the fork's Bash call never reached its subagent thread", + ); + assert.include(routing.assistantTexts(routing.childThreadId), "FORK-REPORT greetings"); + const subagent = harness.events + .filter((event) => event.type === "subagent.updated") + .at(-1)?.subagent; + assert.notEqual(subagent?.result, "/fork-probe", "the skill's name became its result"); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect.each([ { source: "custom model", From 646c4c77f685705446d1db5b161e7d26ec964dca Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 18:49:03 +0000 Subject: [PATCH 2/4] fix(server): forked Claude skills show their work in the subagent thread A `context: fork` skill (such as /code-review) starts its task with no tool_use_id and sends its frames under parent_tool_use_id "forked-command-", after its task_notification. No subagent was registered under that id, so the frames were dropped, and the notification's summary (just the skill name) became the subagent's result. Map the forked-command parent id to the fork's task, hold the fork's task_notification until its frames have been routed, and use the fork's last reply as its result. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.ts | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 2084067025143..f7f0346c56eef 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4140,6 +4140,20 @@ function isClaudeNonSubagentTask(message: SDKMessage): boolean { return isClaudeOpaqueBackgroundTaskType(claudeTaskTypeFromSdkMessage(message)); } +// A `context: fork` skill's task_started names no tool call; its frames use this parent id instead. +function claudeForkedSkillToolUseId( + message: Extract, +): string | undefined { + const skill = message.description.trim(); + return message.tool_use_id === undefined && + message.task_type === "local_agent" && + (message.spawn_depth ?? 1) <= 1 && + skill.length > 1 && + skill.startsWith("/") + ? `forked-command-${skill.slice(1)}` + : undefined; +} + function isClaudeBackgroundTasksChangedMessage(message: SDKMessage): boolean { return message.type === "system" && (message.subtype as string) === "background_tasks_changed"; } @@ -6414,6 +6428,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const workflowStopRequestedAt = new Map(); // Buffered idle outcomes must not settle a resumed child again. const appliedSubagentOutcomes = new WeakSet(); + // A forked skill's frames arrive only after its task_notification; hold that until they are in. + const forkedSkillToolUseIdsByTaskId = new Map(); + const heldForkedSkillNotifications = new Map(); // Subagent frames can precede the task_started that registers their owner. const pendingSubagentFramesByToolUseId = yield* Ref.make( new Map>(), @@ -12024,6 +12041,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( reopen: true, ...(message.run_id === undefined ? {} : { nativeRunId: message.run_id }), }); + const forkedSkillToolUseId = claudeForkedSkillToolUseId(message); + if (forkedSkillToolUseId !== undefined) { + forkedSkillToolUseIdsByTaskId.set(message.task_id, forkedSkillToolUseId); + yield* Ref.update(sessionSubagentTaskIdsByToolUseId, (current) => + new Map(current).set(forkedSkillToolUseId, message.task_id), + ); + } yield* Ref.update(wakeBuffers, (current) => { const entry = current.get(liveQuery.nativeThreadId); if (entry === undefined) return current; @@ -12173,13 +12197,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( !context.ignoredTaskIds.has(message.task_id) && !appliedSubagentOutcomes.has(message) ) { + // A forked skill's summary is only its name; its last reply is the report. + const result = forkedSkillToolUseIdsByTaskId.delete(message.task_id) + ? ((yield* Ref.get(sessionSubagentsByTaskId)).get(message.task_id) + ?.lastAssistantText ?? undefined) + : message.summary; yield* updateClaudeSubagentNode({ context, taskId: message.task_id, ...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }), workflowFrame: message, nativeOutcome: message, - result: message.summary, + ...(result === undefined ? {} : { result }), status: message.status === "completed" ? "completed" @@ -12696,7 +12725,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }); }); - const handleRoutedSdkMessage = Effect.fnUntraced(function* (input: { + const routeSdkMessage = Effect.fnUntraced(function* (input: { readonly query: ClaudeAgentSdkQuerySession; readonly message: SDKMessage; }) { @@ -12711,6 +12740,29 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } }); + const handleRoutedSdkMessage = Effect.fnUntraced(function* (input: { + readonly query: ClaudeAgentSdkQuerySession; + readonly message: SDKMessage; + }) { + const parentToolUseId = Reflect.get(input.message, "parent_tool_use_id"); + for (const [toolUseId, notification] of heldForkedSkillNotifications) { + if (toolUseId === parentToolUseId) continue; + heldForkedSkillNotifications.delete(toolUseId); + yield* routeSdkMessage({ query: input.query, message: notification }); + } + if (input.message.type === "system" && input.message.subtype === "task_notification") { + const forkedSkillToolUseId = forkedSkillToolUseIdsByTaskId.get(input.message.task_id); + if ( + forkedSkillToolUseId !== undefined && + !heldForkedSkillNotifications.has(forkedSkillToolUseId) + ) { + heldForkedSkillNotifications.set(forkedSkillToolUseId, input.message); + return; + } + } + yield* routeSdkMessage(input); + }); + const heldToolUseIds = (context: ActiveClaudeTurnContext): ReadonlySet => new Set( context.heldRootFrames.flatMap((frame) => From b203753e82159f2f7f5e9c9ef779bec658c051dd Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 19:02:24 +0000 Subject: [PATCH 3/4] fix(server): show a forked Claude skill's work while it runs Claude Code holds a `context: fork` skill's frames back until the fork ends, so its subagent thread stayed on "Thinking" for the whole run. While the fork runs, read its transcript (the same subagents/agent-.jsonl file that workflow members are read from) every second and project new lines into its thread. Frames the stream sends at the end are skipped when the transcript already showed their uuid. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 155 ++++++++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 103 +++++++++++- 2 files changed, 254 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c64ccc609e581..0f17483166dd1 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -22365,6 +22365,161 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("shows a forked skill's work from its transcript while it runs, once", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const configDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-fork-" }); + const subagentsDir = NodePath.join( + configDir, + "projects", + "-workspace", + WAKE_NATIVE_SESSION, + "subagents", + ); + yield* fileSystem.makeDirectory(subagentsDir, { recursive: true }); + const forkParent = "forked-command-fork-probe"; + const bashCall = { + type: "assistant" as const, + parent_tool_use_id: forkParent, + message: { + model: "claude-haiku-5-5", + id: "msg_fork_live_bash", + type: "message", + role: "assistant", + stop_reason: "tool_use", + content: [ + { + type: "tool_use", + id: "toolu-fork-live-bash", + name: "Bash", + input: { command: "cat hello.txt", description: "Read hello.txt" }, + }, + ], + }, + uuid: "00000000-0000-4000-8000-000000000b04", + session_id: WAKE_NATIVE_SESSION, + }; + yield* fileSystem.writeFileString( + NodePath.join(subagentsDir, "agent-task-forked-live.jsonl"), + `${JSON.stringify(bashCall)}\n`, + ); + const harness = yield* makeWakeHarnessWithOptions({ + environment: { CLAUDE_CONFIG_DIR: configDir }, + }); + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-forked-skill-live"), + text: "/fork-probe go", + attachments: [], + }), + ); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: "task-forked-live", + description: "/fork-probe", + prompt: "go", + subagent_type: "general-purpose", + task_type: "local_agent", + uuid: "00000000-0000-4000-8000-000000000b01", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* TestClock.adjust("1 second"); + yield* harness.until( + () => subagentRouting(harness.events, ["toolu-fork-live-bash"]).toolThreadIds.size > 0, + ); + const running = harness.events + .filter((event) => event.type === "subagent.updated") + .at(-1)?.subagent; + assert.equal(running?.status, "running", "the fork's command appeared only after it ended"); + + for (const frame of [ + claudeSdkFrame({ + type: "system", + subtype: "task_notification", + task_id: "task-forked-live", + status: "completed", + summary: "/fork-probe", + output_file: "", + uuid: "00000000-0000-4000-8000-000000000b02", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame(bashCall), + claudeSdkFrame({ + type: "user", + parent_tool_use_id: forkParent, + message: { + role: "user", + content: [ + { type: "tool_result", tool_use_id: "toolu-fork-live-bash", content: "greetings" }, + ], + }, + uuid: "00000000-0000-4000-8000-000000000b05", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "assistant", + parent_tool_use_id: forkParent, + message: { + model: "claude-haiku-5-5", + id: "msg_fork_live_reply", + type: "message", + role: "assistant", + content: [{ type: "text", text: "FORK-REPORT greetings" }], + }, + uuid: "00000000-0000-4000-8000-000000000b06", + session_id: WAKE_NATIVE_SESSION, + }), + makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000000b07", + result: "FORK-REPORT greetings", + }), + ]) { + yield* Queue.offer(harness.sdkMessages, frame); + } + yield* Queue.take(harness.terminalReceipts); + + const routing = subagentRouting(harness.events, ["toolu-fork-live-bash"]); + const toolItemIds = new Set( + harness.events.flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "toolu-fork-live-bash" + ? [event.turnItem.id] + : [], + ), + ); + assert.equal(toolItemIds.size, 1, "the fork's command was shown twice"); + const completedCall = harness.events.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "toolu-fork-live-bash", + ); + assert.equal( + completedCall?.type === "turn_item.updated" ? completedCall.turnItem.status : undefined, + "completed", + ); + assert.deepEqual(routing.assistantTexts(routing.childThreadId), ["FORK-REPORT greetings"]); + const subagent = harness.events + .filter((event) => event.type === "subagent.updated") + .at(-1)?.subagent; + assert.equal(subagent?.result, "FORK-REPORT greetings"); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect.each([ { source: "custom model", diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index f7f0346c56eef..cda20128fbddf 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -403,6 +403,7 @@ const CLAUDE_STDERR_TAIL_LENGTH = 2_048; const CLAUDE_STDERR_DRAIN_TIMEOUT = Duration.seconds(1); // How often a working CLI's process tree is recorded when no agent scope will stop what it leaves. const CLAUDE_DESCENDANT_POLL = "1 second"; +const CLAUDE_FORKED_SKILL_TRANSCRIPT_POLL = "1 second"; // Claude Code's refusal of bypassPermissions as root, which it explains only on stderr. const CLAUDE_ROOT_REFUSAL = "--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons"; @@ -6431,6 +6432,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // A forked skill's frames arrive only after its task_notification; hold that until they are in. const forkedSkillToolUseIdsByTaskId = new Map(); const heldForkedSkillNotifications = new Map(); + // The stream withholds a fork's frames until it ends, so its transcript is shown while it runs. + // Frames whose uuid the transcript already showed are skipped when they arrive. + const forkedSkillTranscripts = new Map< + string, + { readonly fiber: Fiber.Fiber; readonly shownUuids: Set } + >(); // Subagent frames can precede the task_started that registers their owner. const pendingSubagentFramesByToolUseId = yield* Ref.make( new Map>(), @@ -8347,10 +8354,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }); // Claude Code records the effort a subagent ran at only in its transcript. - const readSubagentTranscriptEffort = Effect.fnUntraced(function* ( + const findSubagentTranscriptDir = Effect.fnUntraced(function* ( context: ActiveClaudeTurnContext, taskId: string, - completed: boolean, ) { const cwd = context.input.runtimePolicy.cwd; if (cwd === null) return undefined; @@ -8375,8 +8381,17 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( break; } } - if (found === undefined) return undefined; - const transcriptDir = found; + return found === undefined ? undefined : { transcriptDir: found, configDir }; + }); + + const readSubagentTranscriptEffort = Effect.fnUntraced(function* ( + context: ActiveClaudeTurnContext, + taskId: string, + completed: boolean, + ) { + const located = yield* findSubagentTranscriptDir(context, taskId); + if (located === undefined) return undefined; + const { transcriptDir, configDir } = located; let metadata: WorkflowAgentMetadata = {}; let cursor = initialWorkflowAgentTranscriptCursor; const read = Effect.fnUntraced(function* () { @@ -9448,6 +9463,66 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return true; }); + const followForkedSkillTranscript = Effect.fnUntraced(function* ( + context: ActiveClaudeTurnContext, + taskId: string, + shownUuids: Set, + ) { + let located: Effect.Success>; + let cursor = initialWorkflowAgentTranscriptCursor; + while (true) { + yield* Effect.sleep(CLAUDE_FORKED_SKILL_TRANSCRIPT_POLL); + if ((yield* Ref.get(sessionSubagentsByTaskId)).get(taskId)?.task.status !== "running") { + return; + } + located ??= yield* findSubagentTranscriptDir(context, taskId); + if (located === undefined) continue; + const { transcriptDir, configDir } = located; + for (let drained = false; !drained;) { + const read = yield* readWorkflowAgentTranscript({ + transcriptDir, + agentId: taskId, + cursor, + ...(configDir === undefined ? {} : { configDir }), + }).pipe(Effect.option); + if (Option.isNone(read)) break; + const advanced = read.value.cursor.offset !== cursor.offset; + cursor = read.value.cursor; + // Stopping mid-line would leave a shown line unmarked and repeat it from the stream. + yield* Effect.uninterruptible( + Effect.gen(function* () { + for (const line of read.value.lines) { + const subagent = (yield* Ref.get(sessionSubagentsByTaskId)).get(taskId); + const uuid = text(field(line, "uuid")); + if (subagent === undefined || uuid === undefined || shownUuids.has(uuid)) { + continue; + } + if ( + (yield* projectSubagentTranscriptLine( + subagentProjectionTarget(subagent), + line, + )) && + isClaudeTranscriptConversationLine(line) && + line.type === "assistant" + ) { + const messageId = + typeof line.message.id === "string" ? line.message.id : null; + const reply = textFromClaudeContent(line.message.content); + subagent.lastAssistantText = + messageId !== null && messageId === subagent.lastAssistantMessageId + ? `${subagent.lastAssistantText ?? ""}\n${reply}` + : reply; + subagent.lastAssistantMessageId = messageId; + } + shownUuids.add(uuid); + } + }), + ); + drained = !read.value.truncated || !advanced; + } + } + }); + const buildApprovalRequestArtifacts = Effect.fnUntraced(function* (input: { readonly context: ActiveClaudeTurnContext; readonly nativeItemId: string; @@ -12047,6 +12122,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( yield* Ref.update(sessionSubagentTaskIdsByToolUseId, (current) => new Map(current).set(forkedSkillToolUseId, message.task_id), ); + const previous = forkedSkillTranscripts.get(forkedSkillToolUseId); + if (previous !== undefined) yield* Fiber.interrupt(previous.fiber); + const shownUuids = new Set(); + const fiber = yield* followForkedSkillTranscript( + context, + message.task_id, + shownUuids, + ).pipe(Effect.forkIn(sessionScope)); + forkedSkillTranscripts.set(forkedSkillToolUseId, { fiber, shownUuids }); } yield* Ref.update(wakeBuffers, (current) => { const entry = current.get(liveQuery.nativeThreadId); @@ -12748,6 +12832,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( for (const [toolUseId, notification] of heldForkedSkillNotifications) { if (toolUseId === parentToolUseId) continue; heldForkedSkillNotifications.delete(toolUseId); + forkedSkillTranscripts.delete(toolUseId); yield* routeSdkMessage({ query: input.query, message: notification }); } if (input.message.type === "system" && input.message.subtype === "task_notification") { @@ -12756,10 +12841,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( forkedSkillToolUseId !== undefined && !heldForkedSkillNotifications.has(forkedSkillToolUseId) ) { + const transcript = forkedSkillTranscripts.get(forkedSkillToolUseId); + if (transcript !== undefined) yield* Fiber.interrupt(transcript.fiber); heldForkedSkillNotifications.set(forkedSkillToolUseId, input.message); return; } } + const uuid = text(field(input.message, "uuid")); + if ( + typeof parentToolUseId === "string" && + uuid !== undefined && + forkedSkillTranscripts.get(parentToolUseId)?.shownUuids.has(uuid) === true + ) { + return; + } yield* routeSdkMessage(input); }); From 110abc9765b007337363618467ebf402cc141c59 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 19:34:08 +0000 Subject: [PATCH 4/4] fix(server): a forked Claude skill's report reaches its subagent thread Claude Code never streams a forked skill's final reply: it is only in the fork's transcript, written just before task_notification. The transcript reader stopped at the notification without reading it, and a system init frame that follows the notification released the held notification before the fork's frames, so the subagent settled with no reply and a null result. On the notification, read the transcript to the end (waiting out one flush if the reply is not there yet), and release the held notification only when the root turn resumes or the result arrives. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 31 +++++++++- .../Adapters/ClaudeAdapterV2.ts | 56 +++++++++++++++---- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 0f17483166dd1..a92e01ca6f7a2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -22438,6 +22438,25 @@ describe("ClaudeAdapterV2 background wake turns", () => { .at(-1)?.subagent; assert.equal(running?.status, "running", "the fork's command appeared only after it ended"); + // As captured from Claude Code 2.1.332: the fork's report is written to its transcript + // just before task_notification and never sent on the stream. + yield* fileSystem.writeFileString( + NodePath.join(subagentsDir, "agent-task-forked-live.jsonl"), + `${JSON.stringify(bashCall)}\n${JSON.stringify({ + type: "assistant", + parent_tool_use_id: forkParent, + message: { + model: "claude-haiku-5-5", + id: "msg_fork_live_reply", + type: "message", + role: "assistant", + stop_reason: "end_turn", + content: [{ type: "text", text: "FORK-REPORT greetings" }], + }, + uuid: "00000000-0000-4000-8000-000000000b06", + session_id: WAKE_NATIVE_SESSION, + })}\n`, + ); for (const frame of [ claudeSdkFrame({ type: "system", @@ -22449,6 +22468,12 @@ describe("ClaudeAdapterV2 background wake turns", () => { uuid: "00000000-0000-4000-8000-000000000b02", session_id: WAKE_NATIVE_SESSION, }), + claudeSdkFrame({ + type: "system", + subtype: "init", + uuid: "00000000-0000-4000-8000-000000000b09", + session_id: WAKE_NATIVE_SESSION, + }), claudeSdkFrame(bashCall), claudeSdkFrame({ type: "user", @@ -22464,15 +22489,15 @@ describe("ClaudeAdapterV2 background wake turns", () => { }), claudeSdkFrame({ type: "assistant", - parent_tool_use_id: forkParent, + parent_tool_use_id: null, message: { model: "claude-haiku-5-5", - id: "msg_fork_live_reply", + id: "msg_root_live_reply", type: "message", role: "assistant", content: [{ type: "text", text: "FORK-REPORT greetings" }], }, - uuid: "00000000-0000-4000-8000-000000000b06", + uuid: "00000000-0000-4000-8000-000000000b08", session_id: WAKE_NATIVE_SESSION, }), makeResultFrame({ diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index cda20128fbddf..7dbfe555118a1 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -6436,7 +6436,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // Frames whose uuid the transcript already showed are skipped when they arrive. const forkedSkillTranscripts = new Map< string, - { readonly fiber: Fiber.Fiber; readonly shownUuids: Set } + { + readonly fiber: Fiber.Fiber; + readonly shownUuids: Set; + readonly finish: Deferred.Deferred; + } >(); // Subagent frames can precede the task_started that registers their owner. const pendingSubagentFramesByToolUseId = yield* Ref.make( @@ -9467,16 +9471,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( context: ActiveClaudeTurnContext, taskId: string, shownUuids: Set, + finish: Deferred.Deferred, ) { let located: Effect.Success>; let cursor = initialWorkflowAgentTranscriptCursor; - while (true) { - yield* Effect.sleep(CLAUDE_FORKED_SKILL_TRANSCRIPT_POLL); - if ((yield* Ref.get(sessionSubagentsByTaskId)).get(taskId)?.task.status !== "running") { - return; - } - located ??= yield* findSubagentTranscriptDir(context, taskId); - if (located === undefined) continue; + let turnEnded = false; + const drain = Effect.fnUntraced(function* () { + if (located === undefined) return; const { transcriptDir, configDir } = located; for (let drained = false; !drained;) { const read = yield* readWorkflowAgentTranscript({ @@ -9515,11 +9516,34 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( subagent.lastAssistantMessageId = messageId; } shownUuids.add(uuid); + turnEnded = isClaudeTranscriptTurnEnd(line); } }), ); drained = !read.value.truncated || !advanced; } + }); + while (true) { + const finishing = yield* Deferred.await(finish).pipe( + Effect.as(true), + Effect.timeoutOrElse({ + duration: CLAUDE_FORKED_SKILL_TRANSCRIPT_POLL, + orElse: () => Effect.succeed(false), + }), + ); + if ((yield* Ref.get(sessionSubagentsByTaskId)).get(taskId)?.task.status !== "running") { + return; + } + located ??= yield* findSubagentTranscriptDir(context, taskId); + yield* drain(); + if (!finishing) continue; + // The fork's report is only in its transcript (the stream never sends it) and can land + // just after the task_notification. + if (located !== undefined && !turnEnded) { + yield* Effect.sleep(WORKFLOW_MEMBER_TRANSCRIPT_FLUSH_MS); + yield* drain(); + } + return; } }); @@ -12125,12 +12149,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const previous = forkedSkillTranscripts.get(forkedSkillToolUseId); if (previous !== undefined) yield* Fiber.interrupt(previous.fiber); const shownUuids = new Set(); + const finish = yield* Deferred.make(); const fiber = yield* followForkedSkillTranscript( context, message.task_id, shownUuids, + finish, ).pipe(Effect.forkIn(sessionScope)); - forkedSkillTranscripts.set(forkedSkillToolUseId, { fiber, shownUuids }); + forkedSkillTranscripts.set(forkedSkillToolUseId, { fiber, shownUuids, finish }); } yield* Ref.update(wakeBuffers, (current) => { const entry = current.get(liveQuery.nativeThreadId); @@ -12829,8 +12855,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( readonly message: SDKMessage; }) { const parentToolUseId = Reflect.get(input.message, "parent_tool_use_id"); + // The fork's frames end where the root turn resumes; a system frame (init) can come between. + const forkFramesDone = + input.message.type === "result" || + (input.message.type !== "system" && isClaudeRootTurnFrame(input.message)); for (const [toolUseId, notification] of heldForkedSkillNotifications) { - if (toolUseId === parentToolUseId) continue; + if (!forkFramesDone) break; heldForkedSkillNotifications.delete(toolUseId); forkedSkillTranscripts.delete(toolUseId); yield* routeSdkMessage({ query: input.query, message: notification }); @@ -12841,8 +12871,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( forkedSkillToolUseId !== undefined && !heldForkedSkillNotifications.has(forkedSkillToolUseId) ) { + // Read the rest of the transcript, including the report, before the fork can settle. const transcript = forkedSkillTranscripts.get(forkedSkillToolUseId); - if (transcript !== undefined) yield* Fiber.interrupt(transcript.fiber); + if (transcript !== undefined) { + yield* Deferred.succeed(transcript.finish, undefined); + yield* Fiber.join(transcript.fiber); + } heldForkedSkillNotifications.set(forkedSkillToolUseId, input.message); return; }