Skip to content

Commit ac7bb5f

Browse files
fix(server): a follow-up delegated task waits for the subagents its child starts
A t3_thread_send follow-up settled as soon as its own tagged run ended, so the parent got the child's interim reply while a subagent the child started in that turn was still running. The server's wake for that subagent ran untagged, so the child's final reply never reached the parent. A follow-up's runs now include the untagged delegatedCompletion wakes for subagents its runs started. It stays open while those subagents or their deliveries are pending, and settles with the last of its runs. Finalization, delegatedTaskResultPending and task_status share this attribution. Fixes #21837 AI-assisted; verified with the tests and tools listed above.
1 parent ccbcfc4 commit ac7bb5f

4 files changed

Lines changed: 93 additions & 27 deletions

File tree

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ import type {
101101
ProjectionRecordField,
102102
ProjectionRecordFilter,
103103
} 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";
105110
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
106111
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
107112
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
@@ -1501,16 +1506,18 @@ const make = Effect.gen(function* () {
15011506
const childRun = delegatedTaskRun(childControls, task);
15021507
const terminalRun = latestTerminalResultRun(childControls, childRun);
15031508
const progress = delegatedTaskProgress(childControls);
1509+
const followupRuns =
1510+
childRun?.delegatedTaskId === task.id ? delegatedTaskRuns(childControls, task.id) : undefined;
15041511
const taskResultRun =
1505-
childRun?.delegatedTaskId === task.id
1506-
? delegatedTaskResultRun(childControls.runs, task.id)
1507-
: progress.resultRun;
1512+
followupRuns === undefined ? progress.resultRun : delegatedTaskResultRun(followupRuns);
15081513
const taskWorkState =
1509-
childRun?.delegatedTaskId === task.id
1510-
? isTerminalTaskStatus(taskStatusForRun(taskResultRun))
1511-
? "result_available"
1512-
: "working"
1513-
: progress.state;
1514+
followupRuns === undefined
1515+
? progress.state
1516+
: !isTerminalTaskStatus(taskStatusForRun(taskResultRun))
1517+
? "working"
1518+
: delegatedTaskAwaitsSubagents(followupRuns, childControls.subagents)
1519+
? "waiting_for_children"
1520+
: "result_available";
15141521
const resultRunIds = [
15151522
...new Set(
15161523
[taskResultRun?.id, terminalRun?.id].filter((id): id is RunId => id !== undefined),

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ import {
126126
cancelledBeforeStart,
127127
continuationFailedBeforePrompt,
128128
continuationStartRetryDelayMs,
129+
delegatedTaskAwaitsSubagents,
129130
delegatedTaskResultRun,
131+
delegatedTaskRuns,
130132
isRestartContinuationSource,
131133
runOutcome,
132134
} from "./RestartBackgroundNote.ts";
@@ -13509,13 +13511,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1350913511
) {
1351013512
return;
1351113513
}
13512-
const taskRuns = childControls.runs.filter((run) => run.delegatedTaskId === options.task.id);
13514+
const taskRuns = delegatedTaskRuns(childControls, options.task.id);
1351313515
const childRun =
1351413516
taskRuns.length === 0
1351513517
? options.task.id === forkedFrom.nodeId
1351613518
? options.resultRun
1351713519
: undefined
13518-
: delegatedTaskResultRun(taskRuns, options.task.id);
13520+
: delegatedTaskResultRun(taskRuns);
1351913521
if (childRun === undefined) return;
1352013522
let outcome = runOutcome(childRun);
1352113523
let terminalStatus = delegatedTaskTerminalStatus(outcome);
@@ -13575,6 +13577,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1357513577
]);
1357613578
return;
1357713579
}
13580+
// A follow-up ends with the subagents its turns started, as the original task does.
13581+
if (delegatedTaskAwaitsSubagents(taskRuns, childControls.subagents)) return;
1357813582
if (
1357913583
yield* childAwaitsRestartContinuation(
1358013584
taskRuns.length === 0 ? childControls.runs : taskRuns,
@@ -15136,9 +15140,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1513615140
{ messageRoles: ["user"] },
1513715141
);
1513815142
if (taskId !== undefined) {
15139-
const runs = child.runs.filter((run) => run.delegatedTaskId === taskId);
15140-
const run = delegatedTaskResultRun(runs, taskId);
15141-
if (run === undefined || delegatedTaskTerminalStatus(run.status) === null) return true;
15143+
const runs = delegatedTaskRuns(child, taskId);
15144+
const run = delegatedTaskResultRun(runs);
15145+
if (
15146+
run === undefined ||
15147+
delegatedTaskTerminalStatus(run.status) === null ||
15148+
delegatedTaskAwaitsSubagents(runs, child.subagents)
15149+
)
15150+
return true;
1514215151
return yield* childAwaitsRestartContinuation(runs, run);
1514315152
}
1514415153
const progress = delegatedTaskProgress(child);

‎apps/server/src/orchestration-v2/RestartBackgroundNote.ts‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type {
22
NodeId,
3+
OrchestrationV2ConversationMessage,
34
OrchestrationV2PendingBackgroundTask,
45
OrchestrationV2ProviderTurn,
56
OrchestrationV2RestartCancelledBackgroundWork,
67
OrchestrationV2Run,
78
OrchestrationV2RunAttempt,
9+
OrchestrationV2Subagent,
810
OrchestrationV2TurnItem,
911
} from "@t3tools/contracts";
12+
import { subagentHoldsCompletion } from "@t3tools/provider-core/server/subagentProjection";
1013
import { latestRootProviderFailure, runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
1114

1215
type Work = OrchestrationV2RestartCancelledBackgroundWork;
@@ -187,14 +190,56 @@ export function cancelledBeforeStart(
187190
return run.status === "cancelled" && neverStarted(run);
188191
}
189192

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+
190238
/** A follow-up task's latest run, passing over queued work it no longer waits on, as the original task does. */
191239
export function delegatedTaskResultRun(
192240
runs: ReadonlyArray<OrchestrationV2Run>,
193-
taskId: NodeId,
194241
): 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);
198243
const awaited = taskRuns.filter(
199244
(run) => !cancelledBeforeStart(run) && !(run.status === "queued" && run.queueHeld === true),
200245
);

‎packages/provider-core/src/server/subagentProjection.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ export function subagentResultForRun(
248248
};
249249
}
250250

251+
export function subagentHoldsCompletion(
252+
task: Pick<OrchestrationV2ThreadProjection["subagents"][number], "status" | "completionDelivery">,
253+
): boolean {
254+
return (
255+
isOrchestrationV2WorkActive(task.status) ||
256+
// Publishing a child's result precedes scheduling its parent's wake.
257+
// The parent still owes that follow-up even between those transactions.
258+
(task.status !== "idle" &&
259+
(task.completionDelivery?.state === "pending" ||
260+
task.completionDelivery?.state === "claimed"))
261+
);
262+
}
263+
251264
export function delegatedTaskProgress(projection: {
252265
readonly runs: OrchestrationV2ThreadProjection["runs"];
253266
readonly messages: ReadonlyArray<
@@ -274,15 +287,7 @@ export function delegatedTaskProgress(projection: {
274287
(run) => !terminal(run.status) && !(run.status === "queued" && run.queueHeld === true),
275288
);
276289
const children =
277-
projection.subagents.some(
278-
(task) =>
279-
isOrchestrationV2WorkActive(task.status) ||
280-
// Publishing a child's result precedes scheduling its parent's wake.
281-
// The parent still owes that follow-up even between those transactions.
282-
(task.status !== "idle" &&
283-
(task.completionDelivery?.state === "pending" ||
284-
task.completionDelivery?.state === "claimed")),
285-
) ||
290+
projection.subagents.some(subagentHoldsCompletion) ||
286291
projection.providerThreads.some((thread) =>
287292
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []),
288293
);

0 commit comments

Comments
 (0)