Skip to content

Commit fc23be8

Browse files
Record created threads and subagent progress
- Add parent-thread records for spawned child threads - Persist Claude subagent progress as reasoning items - Tighten projection verification around readable thread projections
1 parent c529c61 commit fc23be8

26 files changed

Lines changed: 1066 additions & 206 deletions

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,10 @@ function turnItemText(item: OrchestrationV2TurnItem): string | null {
445445
return item.summary ?? `${item.strategy} handoff to ${item.toProviderInstanceId}`;
446446
case "fork":
447447
return `Forked to thread ${item.targetThreadId}.`;
448+
case "thread_created":
449+
return `Created thread ${item.targetThreadId} with ${item.targetProviderInstanceId} (${item.targetModel}).`;
448450
case "subagent":
449-
return item.result ?? item.prompt;
451+
return item.result ?? item.progress ?? item.prompt;
450452
case "dynamic_tool":
451453
return jsonText({ toolName: item.toolName, input: item.input, output: item.output });
452454
}
@@ -863,6 +865,18 @@ const make = Effect.gen(function* () {
863865
Effect.gen(function* () {
864866
yield* requireCapability(scope);
865867
const parent = yield* loadProjection(scope.threadId);
868+
const parentRun = latestActiveRun(parent);
869+
if (
870+
parentRun === undefined ||
871+
parentRun.rootNodeId === null ||
872+
parentRun.providerInstanceId !== scope.providerInstanceId
873+
) {
874+
return yield* failure(
875+
"parent_not_active",
876+
"Thread creation requires an active run owned by this MCP provider session.",
877+
);
878+
}
879+
const parentNodeId = parentRun.rootNodeId;
866880
const providers = yield* loadProviders;
867881
const key = yield* requestKey(input.clientRequestId);
868882
const created = yield* Effect.forEach(
@@ -955,6 +969,29 @@ const make = Effect.gen(function* () {
955969
}
956970
const projection = yield* loadProjection(threadId);
957971
const run = projection.runs.at(-1);
972+
yield* threadManagement
973+
.dispatch({
974+
type: "thread.created.record",
975+
commandId: stableCommandId({
976+
scope,
977+
requestKey: key,
978+
operation: "record-created-thread",
979+
index,
980+
}),
981+
parentThreadId: scope.threadId,
982+
parentRunId: parentRun.id,
983+
parentNodeId,
984+
targetThreadId: threadId,
985+
targetRunId: run?.id ?? null,
986+
})
987+
.pipe(
988+
Effect.mapError((error) =>
989+
failure(
990+
"orchestration_error",
991+
`Unable to record thread ${index + 1} in the parent timeline: ${errorMessage(error)}`,
992+
),
993+
),
994+
);
958995
return {
959996
threadId,
960997
runId: run?.id ?? null,

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,35 @@ describe("orchestrator MCP toolkit", () => {
730730
.filter((message) => message.role === "user")
731731
.map((message) => message.text),
732732
).toEqual([createdThreadPrompt]);
733+
const createdThreadItems = (yield* orchestrator.getThreadProjection(
734+
parentThreadId,
735+
)).visibleTurnItems
736+
.map((row) => row.item)
737+
.filter((item) => item.type === "thread_created");
738+
expect(
739+
createdThreadItems.map((item) => ({
740+
targetThreadId: item.targetThreadId,
741+
targetRunId: item.targetRunId,
742+
title: item.title,
743+
providerInstanceId: item.targetProviderInstanceId,
744+
model: item.targetModel,
745+
})),
746+
).toEqual([
747+
{
748+
targetThreadId: emptyThread.threadId,
749+
targetRunId: null,
750+
title: emptyThread.title,
751+
providerInstanceId: codexInstanceId,
752+
model: codexModel,
753+
},
754+
{
755+
targetThreadId: promptedThread.threadId,
756+
targetRunId: promptedThread.runId,
757+
title: promptedThread.title,
758+
providerInstanceId: claudeInstanceId,
759+
model: claudeModel,
760+
},
761+
]);
733762

734763
const repeatedCreateCall = yield* invoke("create_threads", createInput);
735764
const repeatedCreated = yield* decodeCreateThreadsResult(
@@ -738,6 +767,15 @@ describe("orchestrator MCP toolkit", () => {
738767
expect(repeatedCreated.threads.map((thread) => thread.threadId)).toEqual(
739768
created.threads.map((thread) => thread.threadId),
740769
);
770+
expect(
771+
(yield* orchestrator.getThreadProjection(parentThreadId)).visibleTurnItems.filter(
772+
(row) => {
773+
if (row.item.type !== "thread_created") return false;
774+
const targetThreadId = row.item.targetThreadId;
775+
return created.threads.some((thread) => thread.threadId === targetThreadId);
776+
},
777+
),
778+
).toHaveLength(2);
741779

742780
const promptedReadCall = yield* invoke("t3_thread_read", {
743781
threadId: promptedThread.threadId,
@@ -814,6 +852,22 @@ describe("orchestrator MCP toolkit", () => {
814852
const activeThread = yield* decodeCreatedThread(
815853
activeThreadCall.structuredContent,
816854
).pipe(Effect.orDie);
855+
const activeThreadItem = (yield* orchestrator.getThreadProjection(
856+
parentThreadId,
857+
)).visibleTurnItems
858+
.map((row) => row.item)
859+
.find(
860+
(item) =>
861+
item.type === "thread_created" && item.targetThreadId === activeThread.threadId,
862+
);
863+
expect(activeThreadItem).toMatchObject({
864+
type: "thread_created",
865+
title: "Managed active thread",
866+
targetThreadId: activeThread.threadId,
867+
targetRunId: activeThread.runId,
868+
targetProviderInstanceId: codexInstanceId,
869+
targetModel: codexModel,
870+
});
817871
const activeProjection = yield* waitForProjection(
818872
orchestrator,
819873
activeThread.threadId,

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

Lines changed: 113 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,8 @@ interface ActiveClaudeSubagent {
17691769
readonly turnItemId: OrchestrationV2TurnItem["id"];
17701770
readonly turnItemOrdinal: number;
17711771
nextChildItemOrdinal: number;
1772+
progressItemOrdinal: number | null;
1773+
progressStartedAt: DateTime.Utc | null;
17721774
resultItemOrdinal: number | null;
17731775
}
17741776

@@ -2038,6 +2040,7 @@ export function makeClaudeAdapterV2(
20382040
readonly toolUseId?: string;
20392041
readonly prompt?: string;
20402042
readonly title?: string;
2043+
readonly progress?: string;
20412044
readonly result?: string;
20422045
readonly status: Extract<
20432046
OrchestrationV2ExecutionNode["status"],
@@ -2052,6 +2055,8 @@ export function makeClaudeAdapterV2(
20522055
if (existingSubagent === undefined && input.status !== "running") {
20532056
return;
20542057
}
2058+
const lifecycleChanged =
2059+
existingSubagent === undefined || existingSubagent.task.status !== input.status;
20552060

20562061
const now = yield* DateTime.now;
20572062
const nativeItemId = `task:${input.taskId}`;
@@ -2105,6 +2110,7 @@ export function makeClaudeAdapterV2(
21052110
status: input.status,
21062111
...(input.prompt === undefined ? {} : { prompt: input.prompt }),
21072112
...(input.title === undefined ? {} : { title: input.title }),
2113+
...(input.progress === undefined ? {} : { progress: input.progress }),
21082114
...(input.result === undefined ? {} : { result: input.result }),
21092115
completedAt: input.status === "running" ? null : now,
21102116
updatedAt: now,
@@ -2121,6 +2127,8 @@ export function makeClaudeAdapterV2(
21212127
}),
21222128
turnItemOrdinal,
21232129
nextChildItemOrdinal: existingSubagent?.nextChildItemOrdinal ?? 100,
2130+
progressItemOrdinal: existingSubagent?.progressItemOrdinal ?? null,
2131+
progressStartedAt: existingSubagent?.progressStartedAt ?? null,
21242132
resultItemOrdinal: existingSubagent?.resultItemOrdinal ?? null,
21252133
} satisfies ActiveClaudeSubagent;
21262134
input.context.subagentsByTaskId.set(input.taskId, subagent);
@@ -2153,52 +2161,54 @@ export function makeClaudeAdapterV2(
21532161
});
21542162
}
21552163

2156-
yield* emitProviderEvent({
2157-
type: "node.updated",
2158-
driver: CLAUDE_PROVIDER,
2159-
node: {
2160-
id: nodeId,
2161-
threadId: input.context.input.threadId,
2162-
runId: input.context.input.runId,
2163-
parentNodeId: input.context.input.rootNodeId,
2164-
rootNodeId: input.context.input.rootNodeId,
2165-
kind: "subagent",
2166-
status: input.status,
2167-
countsForRun: false,
2168-
providerThreadId: input.context.input.providerThread.id,
2169-
providerTurnId: input.context.providerTurnId,
2170-
nativeItemRef: {
2171-
driver: CLAUDE_PROVIDER,
2172-
nativeId: input.taskId,
2173-
strength: "strong",
2164+
if (lifecycleChanged) {
2165+
yield* emitProviderEvent({
2166+
type: "node.updated",
2167+
driver: CLAUDE_PROVIDER,
2168+
node: {
2169+
id: nodeId,
2170+
threadId: input.context.input.threadId,
2171+
runId: input.context.input.runId,
2172+
parentNodeId: input.context.input.rootNodeId,
2173+
rootNodeId: input.context.input.rootNodeId,
2174+
kind: "subagent",
2175+
status: input.status,
2176+
countsForRun: false,
2177+
providerThreadId: input.context.input.providerThread.id,
2178+
providerTurnId: input.context.providerTurnId,
2179+
nativeItemRef: {
2180+
driver: CLAUDE_PROVIDER,
2181+
nativeId: input.taskId,
2182+
strength: "strong",
2183+
},
2184+
runtimeRequestId: null,
2185+
checkpointScopeId: null,
2186+
startedAt: task.startedAt,
2187+
completedAt: input.status === "running" ? null : now,
21742188
},
2175-
runtimeRequestId: null,
2176-
checkpointScopeId: null,
2177-
startedAt: task.startedAt,
2178-
completedAt: input.status === "running" ? null : now,
2179-
},
2180-
});
2181-
yield* emitProviderEvent({
2182-
type: "node.updated",
2183-
driver: CLAUDE_PROVIDER,
2184-
node: {
2185-
id: childRootNodeId,
2186-
threadId: childThreadId,
2187-
runId: null,
2188-
parentNodeId: null,
2189-
rootNodeId: childRootNodeId,
2190-
kind: "root_turn",
2191-
status: input.status,
2192-
countsForRun: false,
2193-
providerThreadId: null,
2194-
providerTurnId: null,
2195-
nativeItemRef: task.nativeTaskRef,
2196-
runtimeRequestId: null,
2197-
checkpointScopeId: null,
2198-
startedAt: task.startedAt,
2199-
completedAt: input.status === "running" ? null : now,
2200-
},
2201-
});
2189+
});
2190+
yield* emitProviderEvent({
2191+
type: "node.updated",
2192+
driver: CLAUDE_PROVIDER,
2193+
node: {
2194+
id: childRootNodeId,
2195+
threadId: childThreadId,
2196+
runId: null,
2197+
parentNodeId: null,
2198+
rootNodeId: childRootNodeId,
2199+
kind: "root_turn",
2200+
status: input.status,
2201+
countsForRun: false,
2202+
providerThreadId: null,
2203+
providerTurnId: null,
2204+
nativeItemRef: task.nativeTaskRef,
2205+
runtimeRequestId: null,
2206+
checkpointScopeId: null,
2207+
startedAt: task.startedAt,
2208+
completedAt: input.status === "running" ? null : now,
2209+
},
2210+
});
2211+
}
22022212
if (existingSubagent === undefined) {
22032213
const promptNativeItemId = `${nativeItemId}:prompt`;
22042214
const promptArtifacts = makeSubagentConversationArtifacts({
@@ -2265,10 +2275,55 @@ export function makeClaudeAdapterV2(
22652275
providerInstanceId: task.providerInstanceId,
22662276
childThreadId: task.childThreadId,
22672277
prompt: task.prompt,
2278+
...(task.progress === undefined ? {} : { progress: task.progress }),
22682279
result: task.result,
22692280
},
22702281
});
22712282

2283+
const progress = task.progress?.trim();
2284+
if (
2285+
progress !== undefined &&
2286+
progress.length > 0 &&
2287+
(input.progress !== undefined || (lifecycleChanged && input.status !== "running"))
2288+
) {
2289+
const progressNativeItemId = `${nativeItemId}:progress`;
2290+
const progressItemOrdinal =
2291+
subagent.progressItemOrdinal ?? ++subagent.nextChildItemOrdinal;
2292+
const progressStartedAt = subagent.progressStartedAt ?? now;
2293+
subagent.progressItemOrdinal = progressItemOrdinal;
2294+
subagent.progressStartedAt = progressStartedAt;
2295+
yield* emitProviderEvent({
2296+
type: "turn_item.updated",
2297+
driver: CLAUDE_PROVIDER,
2298+
turnItem: {
2299+
id: idAllocator.derive.turnItemFromProviderItem({
2300+
driver: CLAUDE_PROVIDER,
2301+
nativeItemId: progressNativeItemId,
2302+
}),
2303+
threadId: childThreadId,
2304+
runId: null,
2305+
nodeId: childRootNodeId,
2306+
providerThreadId: null,
2307+
providerTurnId: null,
2308+
nativeItemRef: {
2309+
driver: CLAUDE_PROVIDER,
2310+
nativeId: progressNativeItemId,
2311+
strength: "strong",
2312+
},
2313+
parentItemId: null,
2314+
ordinal: progressItemOrdinal,
2315+
status: input.status,
2316+
title: "Subagent progress",
2317+
startedAt: progressStartedAt,
2318+
completedAt: input.status === "running" ? null : now,
2319+
updatedAt: now,
2320+
type: "reasoning",
2321+
text: progress,
2322+
streaming: input.status === "running",
2323+
},
2324+
});
2325+
}
2326+
22722327
if (
22732328
input.result !== undefined &&
22742329
input.result.trim().length > 0 &&
@@ -2670,6 +2725,19 @@ export function makeClaudeAdapterV2(
26702725
});
26712726
}
26722727

2728+
if (message.type === "system" && message.subtype === "task_progress") {
2729+
const progress = message.description.trim();
2730+
if (progress.length > 0) {
2731+
yield* updateClaudeSubagentNode({
2732+
context,
2733+
taskId: message.task_id,
2734+
...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }),
2735+
progress,
2736+
status: "running",
2737+
});
2738+
}
2739+
}
2740+
26732741
if (message.type === "system" && message.subtype === "task_notification") {
26742742
yield* updateClaudeSubagentNode({
26752743
context,

0 commit comments

Comments
 (0)