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, 17:15 UTC
fix(server): a follow-up delegated task waits for the subagents its child starts (#34958)
Julius Marminge committed
Nov 13, 2026 at 09:48 UTC
daed1dd327bbb72b1e0991bcf101a96011db5b06
5 files changed
+338
-27
apps/server/src/mcp/OrchestratorMcpService.ts
+16
-9
@@ -101,7 +101,12 @@ import type {
101
ProjectionRecordField,
102
ProjectionRecordFilter,
103
} from "../orchestration-v2/ProjectionStore.ts";
104
-import { delegatedTaskResultRun, runOutcome } from "../orchestration-v2/RestartBackgroundNote.ts";
104
+import {
105
+ delegatedTaskAwaitsSubagents,
106
+ delegatedTaskResultRun,
107
+ delegatedTaskRuns,
108
+ runOutcome,
109
+} from "../orchestration-v2/RestartBackgroundNote.ts";
110
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
111
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
112
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
@@ -1625,16 +1630,18 @@ const make = Effect.gen(function* () {
1630
const childRun = delegatedTaskRun(childControls, task);
1631
const terminalRun = latestTerminalResultRun(childControls, childRun);
1632
const progress = delegatedTaskProgress(childControls);
1633
+ const followupRuns =
1634
+ childRun?.delegatedTaskId === task.id ? delegatedTaskRuns(childControls, task.id) : undefined;
1635
const taskResultRun =
1629
- childRun?.delegatedTaskId === task.id
1630
- ? delegatedTaskResultRun(childControls.runs, task.id)
1631
- : progress.resultRun;
1636
+ followupRuns === undefined ? progress.resultRun : delegatedTaskResultRun(followupRuns);
1637
const taskWorkState =
1633
- childRun?.delegatedTaskId === task.id
1634
- ? isTerminalTaskStatus(taskStatusForRun(taskResultRun))
1635
- ? "result_available"
1636
- : "working"
1637
- : progress.state;
1638
+ followupRuns === undefined
1639
+ ? progress.state
1640
+ : !isTerminalTaskStatus(taskStatusForRun(taskResultRun))
1641
+ ? "working"
1642
+ : delegatedTaskAwaitsSubagents(followupRuns, childControls.subagents)
1643
+ ? "waiting_for_children"
1644
+ : "result_available";
1645
const resultRunIds = [
1646
...new Set(
1647
[taskResultRun?.id, terminalRun?.id].filter((id): id is RunId => id !== undefined),
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+245
@@ -2937,6 +2937,251 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
2937
};
2938
});
2939
2940
+ it.effect("holds a follow-up open while the child's own subagent still runs", () =>
2941
+ Effect.gen(function* () {
2942
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
2943
+ const eventSink = yield* EventSink.EventSinkV2;
2944
+ const now = yield* DateTime.now;
2945
+ const parentThreadId = ThreadId.make("thread:followup-grandchild-parent");
2946
+ const projectId = ProjectId.make("project:followup-grandchild");
2947
+ const parentRunId = RunId.make("run:followup-grandchild-parent");
2948
+ const rootNodeId = NodeId.make("node:followup-grandchild-parent");
2949
+ yield* seedParentWithTerminalTask({
2950
+ threadId: parentThreadId,
2951
+ projectId,
2952
+ runId: parentRunId,
2953
+ rootNodeId,
2954
+ taskId: NodeId.make("node:followup-grandchild-seed"),
2955
+ deliveryState: "acknowledged",
2956
+ now,
2957
+ });
2958
+ const child = yield* seedRestartCancelledChild({
2959
+ parentThreadId,
2960
+ projectId,
2961
+ parentRunId,
2962
+ rootNodeId,
2963
+ name: "followup-grandchild-child",
2964
+ completionWake: "always",
2965
+ continuationPending: false,
2966
+ runStatus: "completed",
2967
+ now,
2968
+ });
2969
+ yield* orchestrator.recoverDelegatedTasks;
2970
+ assert.equal(
2971
+ (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
2972
+ (task) => task.id === child.taskId,
2973
+ )?.status,
2974
+ "completed",
2975
+ );
2976
+
2977
+ const followupMessageId = MessageId.make("message:followup-grandchild-send");
2978
+ yield* orchestrator.dispatch({
2979
+ type: "message.dispatch",
2980
+ createdBy: "agent",
2981
+ creationSource: "mcp",
2982
+ commandId: CommandId.make("command:followup-grandchild-send"),
2983
+ threadId: child.childThreadId,
2984
+ senderThreadId: parentThreadId,
2985
+ messageId: followupMessageId,
2986
+ text: "Now delegate the next step.",
2987
+ attachments: [],
2988
+ dispatchMode: { type: "queue_after_active" },
2989
+ });
2990
+ const followupRun = (yield* orchestrator.getThreadProjection(
2991
+ child.childThreadId,
2992
+ )).runs.find((run) => run.userMessageId === followupMessageId)!;
2993
+ assert.isDefined(followupRun.delegatedTaskId);
2994
+ const followupTaskId = followupRun.delegatedTaskId!;
2995
+
2996
+ const grandchildId = NodeId.make("node:followup-grandchild-grandchild");
2997
+ const grandchild = {
2998
+ id: grandchildId,
2999
+ threadId: child.childThreadId,
3000
+ runId: followupRun.id,
3001
+ parentNodeId: NodeId.make("node:followup-grandchild-child-root"),
3002
+ origin: "app_owned" as const,
3003
+ createdBy: "agent" as const,
3004
+ driver,
3005
+ providerInstanceId: modelSelection.instanceId,
3006
+ providerThreadId: null,
3007
+ childThreadId: ThreadId.make("thread:followup-grandchild-grandchild"),
3008
+ nativeTaskRef: null,
3009
+ prompt: "Do the next step.",
3010
+ title: null,
3011
+ model: null,
3012
+ completionWake: "always" as const,
3013
+ status: "running" as const,
3014
+ result: null,
3015
+ startedAt: now,
3016
+ completedAt: null,
3017
+ updatedAt: now,
3018
+ };
3019
+ yield* eventSink.write({
3020
+ commandId: reconcileCommandId("followup-grandchild-turn-ended"),
3021
+ events: [
3022
+ {
3023
+ id: EventId.make("event:followup-grandchild-started"),
3024
+ type: "subagent.updated",
3025
+ threadId: child.childThreadId,
3026
+ runId: followupRun.id,
3027
+ nodeId: grandchildId,
3028
+ driver,
3029
+ providerInstanceId: modelSelection.instanceId,
3030
+ occurredAt: now,
3031
+ payload: grandchild,
3032
+ },
3033
+ {
3034
+ id: EventId.make("event:followup-grandchild-run-completed"),
3035
+ type: "run.updated",
3036
+ threadId: child.childThreadId,
3037
+ runId: followupRun.id,
3038
+ occurredAt: now,
3039
+ payload: { ...followupRun, status: "completed", startedAt: now, completedAt: now },
3040
+ },
3041
+ ],
3042
+ });
3043
+ yield* orchestrator.recoverDelegatedTasks;
3044
+
3045
+ const waiting = yield* orchestrator.getThreadProjection(parentThreadId);
3046
+ const heldFollowup = waiting.subagents.find((task) => task.id === followupTaskId);
3047
+ assert.isNull(
3048
+ heldFollowup?.result ?? null,
3049
+ "the follow-up closed while the child's own subagent was still running",
3050
+ );
3051
+ assert.equal(heldFollowup?.status, "running");
3052
+ assert.equal(
3053
+ waiting.contextTransfers.filter(
3054
+ (transfer) =>
3055
+ transfer.type === "subagent_result" &&
3056
+ transfer.sourceThreadId === child.childThreadId,
3057
+ ).length,
3058
+ 1,
3059
+ );
3060
+
3061
+ // Another turn in the child does not release the hold while the grandchild runs.
3062
+ for (const status of ["running", "completed"] as const) {
3063
+ yield* eventSink.write({
3064
+ commandId: reconcileCommandId(`followup-grandchild-user-${status}`),
3065
+ events: [
3066
+ runEvent({
3067
+ threadId: child.childThreadId,
3068
+ runId: RunId.make("run:followup-grandchild-user"),
3069
+ ordinal: followupRun.ordinal + 1,
3070
+ status,
3071
+ now,
3072
+ }),
3073
+ ],
3074
+ });
3075
+ yield* orchestrator.recoverDelegatedTasks;
3076
+ assert.isNull(
3077
+ (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
3078
+ (task) => task.id === followupTaskId,
3079
+ )?.result ?? null,
3080
+ `the follow-up closed while another child run was ${status}`,
3081
+ );
3082
+ }
3083
+
3084
+ // The grandchild's completion wakes the child in a run without a delegatedTaskId.
3085
+ const wakeRunId = RunId.make("run:followup-grandchild-wake");
3086
+ const wakeRun = (status: "running" | "completed") =>
3087
+ runEvent({
3088
+ threadId: child.childThreadId,
3089
+ runId: wakeRunId,
3090
+ ordinal: followupRun.ordinal + 2,
3091
+ status,
3092
+ now,
3093
+ });
3094
+ yield* eventSink.write({
3095
+ commandId: reconcileCommandId("followup-grandchild-settled"),
3096
+ events: [
3097
+ {
3098
+ id: EventId.make("event:followup-grandchild-completed"),
3099
+ type: "subagent.updated",
3100
+ threadId: child.childThreadId,
3101
+ runId: followupRun.id,
3102
+ nodeId: grandchildId,
3103
+ driver,
3104
+ providerInstanceId: modelSelection.instanceId,
3105
+ occurredAt: now,
3106
+ payload: {
3107
+ ...grandchild,
3108
+ status: "completed",
3109
+ result: "grandchild finished",
3110
+ completionDelivery: { state: "acknowledged", observedByRunId: wakeRunId },
3111
+ completedAt: now,
3112
+ },
3113
+ },
3114
+ {
3115
+ id: EventId.make("event:followup-grandchild-wake-message"),
3116
+ type: "message.updated",
3117
+ threadId: child.childThreadId,
3118
+ runId: wakeRunId,
3119
+ occurredAt: now,
3120
+ payload: {
3121
+ id: MessageId.make(`message:${wakeRunId}`),
3122
+ threadId: child.childThreadId,
3123
+ runId: wakeRunId,
3124
+ nodeId: null,
3125
+ role: "user",
3126
+ text: "Delegated task finished.",
3127
+ attachments: [],
3128
+ streaming: false,
3129
+ createdBy: "agent",
3130
+ creationSource: "server",
3131
+ delegatedCompletion: {
3132
+ parentRunId: followupRun.id,
3133
+ generation: 1,
3134
+ taskIds: [grandchildId],
3135
+ },
3136
+ createdAt: now,
3137
+ updatedAt: now,
3138
+ },
3139
+ },
3140
+ wakeRun("running"),
3141
+ ],
3142
+ });
3143
+ yield* orchestrator.recoverDelegatedTasks;
3144
+ const waking = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
3145
+ (task) => task.id === followupTaskId,
3146
+ );
3147
+ assert.isNull(waking?.result ?? null, "the follow-up closed before the child's wake ended");
3148
+
3149
+ yield* eventSink.write({
3150
+ commandId: reconcileCommandId("followup-grandchild-wake-finished"),
3151
+ events: [
3152
+ {
3153
+ id: EventId.make("event:followup-grandchild-wake-result"),
3154
+ type: "message.updated",
3155
+ threadId: child.childThreadId,
3156
+ runId: wakeRunId,
3157
+ occurredAt: now,
3158
+ payload: {
3159
+ id: MessageId.make("message:followup-grandchild-wake-result"),
3160
+ threadId: child.childThreadId,
3161
+ runId: wakeRunId,
3162
+ nodeId: null,
3163
+ role: "assistant",
3164
+ text: "The grandchild finished the next step.",
3165
+ attachments: [],
3166
+ streaming: false,
3167
+ createdBy: "agent",
3168
+ creationSource: "server",
3169
+ createdAt: now,
3170
+ updatedAt: now,
3171
+ },
3172
+ },
3173
+ wakeRun("completed"),
3174
+ ],
3175
+ });
3176
+ yield* orchestrator.recoverDelegatedTasks;
3177
+ const settled = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
3178
+ (task) => task.id === followupTaskId,
3179
+ );
3180
+ assert.equal(settled?.status, "completed");
3181
+ assert.equal(settled?.result, "The grandchild finished the next step.");
3182
+ }),
3183
+ );
3184
+
3185
it.effect("waits for a run the follow-up's child queued on its own thread", () =>
3186
Effect.gen(function* () {
3187
const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
apps/server/src/orchestration-v2/Orchestrator.ts
+14
-5
@@ -128,7 +128,9 @@ import {
128
cancelledBeforeStart,
129
continuationFailedBeforePrompt,
130
continuationStartRetryDelayMs,
131
+ delegatedTaskAwaitsSubagents,
132
delegatedTaskResultRun,
133
+ delegatedTaskRuns,
134
isRestartContinuationSource,
135
runOutcome,
136
} from "./RestartBackgroundNote.ts";
@@ -13939,13 +13941,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13941
) {
13942
return;
13943
}
13942
- const taskRuns = childControls.runs.filter((run) => run.delegatedTaskId === options.task.id);
13944
+ const taskRuns = delegatedTaskRuns(childControls, options.task.id);
13945
const childRun =
13946
taskRuns.length === 0
13947
? options.task.id === forkedFrom.nodeId
13948
? options.resultRun
13949
: undefined
13948
- : delegatedTaskResultRun(taskRuns, options.task.id);
13950
+ : delegatedTaskResultRun(taskRuns);
13951
if (childRun === undefined) return;
13952
let outcome = runOutcome(childRun);
13953
let terminalStatus = delegatedTaskTerminalStatus(outcome);
@@ -14005,6 +14007,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14007
]);
14008
return;
14009
}
14010
+ // A follow-up ends with the subagents its turns started, as the original task does.
14011
+ if (delegatedTaskAwaitsSubagents(taskRuns, childControls.subagents)) return;
14012
if (
14013
yield* childAwaitsRestartContinuation(
14014
taskRuns.length === 0 ? childControls.runs : taskRuns,
@@ -15576,9 +15580,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15580
{ messageRoles: ["user"] },
15581
);
15582
if (taskId !== undefined) {
15579
- const runs = child.runs.filter((run) => run.delegatedTaskId === taskId);
15580
- const run = delegatedTaskResultRun(runs, taskId);
15581
- if (run === undefined || delegatedTaskTerminalStatus(run.status) === null) return true;
15583
+ const runs = delegatedTaskRuns(child, taskId);
15584
+ const run = delegatedTaskResultRun(runs);
15585
+ if (
15586
+ run === undefined ||
15587
+ delegatedTaskTerminalStatus(run.status) === null ||
15588
+ delegatedTaskAwaitsSubagents(runs, child.subagents)
15589
+ )
15590
+ return true;
15591
return yield* childAwaitsRestartContinuation(runs, run);
15592
}
15593
const progress = delegatedTaskProgress(child);
apps/server/src/orchestration-v2/RestartBackgroundNote.ts
+49
-4
@@ -1,12 +1,15 @@
1
import type {
2
NodeId,
3
+ OrchestrationV2ConversationMessage,
4
OrchestrationV2PendingBackgroundTask,
5
OrchestrationV2ProviderTurn,
6
OrchestrationV2RestartCancelledBackgroundWork,
7
OrchestrationV2Run,
8
OrchestrationV2RunAttempt,
9
+ OrchestrationV2Subagent,
10
OrchestrationV2TurnItem,
11
} from "@t3tools/contracts";
12
+import { subagentHoldsCompletion } from "@t3tools/provider-core/server/subagentProjection";
13
import { latestRootProviderFailure, runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
14
15
type Work = OrchestrationV2RestartCancelledBackgroundWork;
@@ -187,14 +190,56 @@ export function cancelledBeforeStart(
190
return run.status === "cancelled" && neverStarted(run);
191
}
192
193
+/** A follow-up task's runs: those tagged with it, and the untagged wakes for subagents they started. */
194
+export function delegatedTaskRuns(
195
+ projection: {
196
+ readonly runs: ReadonlyArray<OrchestrationV2Run>;
197
+ readonly messages: ReadonlyArray<
198
+ Pick<OrchestrationV2ConversationMessage, "runId" | "delegatedCompletion">
199
+ >;
200
+ },
201
+ taskId: NodeId,
202
+): ReadonlyArray<OrchestrationV2Run> {
203
+ const owned = new Set(
204
+ projection.runs.filter((run) => run.delegatedTaskId === taskId).map((run) => run.id),
205
+ );
206
+ const untagged = new Set(
207
+ projection.runs.filter((run) => run.delegatedTaskId === undefined).map((run) => run.id),
208
+ );
209
+ for (let grew = owned.size > 0; grew;) {
210
+ grew = false;
211
+ for (const { runId, delegatedCompletion } of projection.messages) {
212
+ if (
213
+ runId === null ||
214
+ !untagged.has(runId) ||
215
+ owned.has(runId) ||
216
+ delegatedCompletion === undefined ||
217
+ !owned.has(delegatedCompletion.parentRunId)
218
+ )
219
+ continue;
220
+ owned.add(runId);
221
+ grew = true;
222
+ }
223
+ }
224
+ return projection.runs.filter((run) => owned.has(run.id));
225
+}
226
+
227
+/** A follow-up task's runs still owe a subagent they started its result. */
228
+export function delegatedTaskAwaitsSubagents(
229
+ runs: ReadonlyArray<OrchestrationV2Run>,
230
+ subagents: ReadonlyArray<
231
+ Pick<OrchestrationV2Subagent, "runId" | "status" | "completionDelivery">
232
+ >,
233
+): boolean {
234
+ const runIds = new Set<string | null>(runs.map((run) => run.id));
235
+ return subagents.some((task) => runIds.has(task.runId) && subagentHoldsCompletion(task));
236
+}
237
+
238
/** A follow-up task's latest run, passing over queued work it no longer waits on, as the original task does. */
239
export function delegatedTaskResultRun(
240
runs: ReadonlyArray<OrchestrationV2Run>,
193
- taskId: NodeId,
241
): OrchestrationV2Run | undefined {
195
- const taskRuns = runs
196
- .filter((run) => run.delegatedTaskId === taskId)
197
- .toSorted((a, b) => b.ordinal - a.ordinal);
242
+ const taskRuns = runs.toSorted((a, b) => b.ordinal - a.ordinal);
243
const awaited = taskRuns.filter(
244
(run) => !cancelledBeforeStart(run) && !(run.status === "queued" && run.queueHeld === true),
245
);
packages/provider-core/src/server/subagentProjection.ts
+14
-9
@@ -256,6 +256,19 @@ export function subagentResultForRun(
256
};
257
}
258
259
+export function subagentHoldsCompletion(
260
+ task: Pick<OrchestrationV2ThreadProjection["subagents"][number], "status" | "completionDelivery">,
261
+): boolean {
262
+ return (
263
+ isOrchestrationV2WorkActive(task.status) ||
264
+ // Publishing a child's result precedes scheduling its parent's wake.
265
+ // The parent still owes that follow-up even between those transactions.
266
+ (task.status !== "idle" &&
267
+ (task.completionDelivery?.state === "pending" ||
268
+ task.completionDelivery?.state === "claimed"))
269
+ );
270
+}
271
+
272
export function delegatedTaskProgress(projection: {
273
readonly runs: OrchestrationV2ThreadProjection["runs"];
274
readonly messages: ReadonlyArray<
@@ -282,15 +295,7 @@ export function delegatedTaskProgress(projection: {
295
(run) => !terminal(run.status) && !(run.status === "queued" && run.queueHeld === true),
296
);
297
const children =
285
- projection.subagents.some(
286
- (task) =>
287
- isOrchestrationV2WorkActive(task.status) ||
288
- // Publishing a child's result precedes scheduling its parent's wake.
289
- // The parent still owes that follow-up even between those transactions.
290
- (task.status !== "idle" &&
291
- (task.completionDelivery?.state === "pending" ||
292
- task.completionDelivery?.state === "claimed")),
293
- ) ||
298
+ projection.subagents.some(subagentHoldsCompletion) ||
299
projection.providerThreads.some((thread) =>
300
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []),
301
);