Skip to content

Commit 0c78322

Browse files
fix(server): a repeated forked skill gets its own subagent
Every run of a forked skill shares the derived `forked-command-<skill>` id. A second run in the same turn found the first, finished run under that id and reopened it, so the second run had no node of its own and its messages went into the first run's thread. The id now belongs to the newest run. AI-assisted; verified with the tests and tools listed above.
1 parent 47cc636 commit 0c78322

2 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17847,53 +17847,75 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1784717847
}),
1784817848
);
1784917849
// A `context: fork` skill has no Agent call: task_started carries no tool_use_id,
17850-
// and the fork's frames name `forked-command-<skill>` as their parent.
17851-
for (const frame of [
17850+
// and every run's frames name `forked-command-<skill>` as their parent.
17851+
const forkFrames = (taskId: string, text: string, uuidBase: number) => [
1785217852
claudeSdkFrame({
1785317853
type: "system",
1785417854
subtype: "task_started",
17855-
task_id: "task-forked-skill",
17855+
task_id: taskId,
1785617856
description: "/fork-probe",
1785717857
prompt: "go",
1785817858
subagent_type: "general-purpose",
1785917859
task_type: "local_agent",
17860-
uuid: "00000000-0000-4000-8000-000000000231",
17860+
uuid: `00000000-0000-4000-8000-000000000${uuidBase}`,
1786117861
session_id: WAKE_NATIVE_SESSION,
1786217862
}),
1786317863
claudeSdkFrame({
1786417864
type: "assistant",
1786517865
parent_tool_use_id: "forked-command-fork-probe",
1786617866
message: {
1786717867
model: "claude-haiku-4-5-20251001",
17868-
id: "msg_forked_skill_reply",
17868+
id: `msg_${taskId}`,
1786917869
type: "message",
1787017870
role: "assistant",
17871-
content: [{ type: "text", text: "OK" }],
17871+
content: [{ type: "text", text }],
1787217872
},
17873-
uuid: "00000000-0000-4000-8000-000000000232",
17873+
uuid: `00000000-0000-4000-8000-000000000${uuidBase + 1}`,
1787417874
session_id: WAKE_NATIVE_SESSION,
1787517875
}),
1787617876
claudeSdkFrame({
1787717877
type: "system",
1787817878
subtype: "task_notification",
17879-
task_id: "task-forked-skill",
17879+
task_id: taskId,
1788017880
status: "completed",
1788117881
summary: "/fork-probe",
1788217882
output_file: "",
17883-
uuid: "00000000-0000-4000-8000-000000000233",
17883+
uuid: `00000000-0000-4000-8000-000000000${uuidBase + 2}`,
1788417884
session_id: WAKE_NATIVE_SESSION,
1788517885
}),
17886-
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000234", result: "OK" }),
17886+
];
17887+
for (const frame of [
17888+
...forkFrames("task-fork-a", "first", 231),
17889+
...forkFrames("task-fork-b", "second", 241),
17890+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000251", result: "OK" }),
1788717891
]) {
1788817892
yield* Queue.offer(harness.sdkMessages, frame);
1788917893
}
1789017894
yield* Queue.take(harness.terminalReceipts);
1789117895

17892-
const fork = harness.events
17893-
.filter((event) => event.type === "subagent.updated")
17894-
.at(-1)?.subagent;
17895-
assert.equal(fork?.model, "claude-haiku-4-5");
17896-
assert.equal(fork?.modelSelection?.model, "claude-haiku-4-5");
17896+
const latestByTask = new Map(
17897+
harness.events.flatMap((event) =>
17898+
event.type === "subagent.updated"
17899+
? [[event.subagent.nativeTaskRef?.nativeId, event.subagent] as const]
17900+
: [],
17901+
),
17902+
);
17903+
const repliesIn = (threadId: string | null | undefined) =>
17904+
harness.events.flatMap((event) =>
17905+
event.type === "message.updated" && event.message.threadId === threadId
17906+
? [event.message.text]
17907+
: [],
17908+
);
17909+
assert.deepEqual(
17910+
["task-fork-a", "task-fork-b"].map((taskId) => {
17911+
const fork = latestByTask.get(taskId);
17912+
return [fork?.model, fork?.modelSelection?.model, repliesIn(fork?.childThreadId)];
17913+
}),
17914+
[
17915+
["claude-haiku-4-5", "claude-haiku-4-5", ["go", "first", "/fork-probe"]],
17916+
["claude-haiku-4-5", "claude-haiku-4-5", ["go", "second", "/fork-probe"]],
17917+
],
17918+
);
1789717919
}).pipe(
1789817920
Effect.provide(
1789917921
Layer.mergeAll(

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10303,6 +10303,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1030310303
if (toolUseId !== undefined) {
1030410304
pendingSubagentLaunchesByToolUseId.delete(toolUseId);
1030510305
}
10306+
// Every run of a forked skill shares its derived id; it belongs to the newest run.
10307+
if (
10308+
message.tool_use_id === undefined &&
10309+
toolUseId !== undefined &&
10310+
context.subagentsByToolUseId.get(toolUseId) !==
10311+
context.subagentsByTaskId.get(message.task_id)
10312+
) {
10313+
context.subagentsByToolUseId.delete(toolUseId);
10314+
}
1030610315
const owner =
1030710316
launch?.ownerToolUseId === undefined
1030810317
? undefined

0 commit comments

Comments
 (0)