Skip to content

Commit daed1dd

Browse files
fix(server): a follow-up delegated task waits for the subagents its child starts (pingdotgg#34958)
1 parent 79dd6d7 commit daed1dd

5 files changed

Lines changed: 338 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";
@@ -1625,16 +1630,18 @@ const make = Effect.gen(function* () {
16251630
const childRun = delegatedTaskRun(childControls, task);
16261631
const terminalRun = latestTerminalResultRun(childControls, childRun);
16271632
const progress = delegatedTaskProgress(childControls);
1633+
const followupRuns =
1634+
childRun?.delegatedTaskId === task.id ? delegatedTaskRuns(childControls, task.id) : undefined;
16281635
const taskResultRun =
1629-
childRun?.delegatedTaskId === task.id
1630-
? delegatedTaskResultRun(childControls.runs, task.id)
1631-
: progress.resultRun;
1636+
followupRuns === undefined ? progress.resultRun : delegatedTaskResultRun(followupRuns);
16321637
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";
16381645
const resultRunIds = [
16391646
...new Set(
16401647
[taskResultRun?.id, terminalRun?.id].filter((id): id is RunId => id !== undefined),

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,6 +2937,251 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
29372937
};
29382938
});
29392939

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+
29403185
it.effect("waits for a run the follow-up's child queued on its own thread", () =>
29413186
Effect.gen(function* () {
29423187
const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ import {
128128
cancelledBeforeStart,
129129
continuationFailedBeforePrompt,
130130
continuationStartRetryDelayMs,
131+
delegatedTaskAwaitsSubagents,
131132
delegatedTaskResultRun,
133+
delegatedTaskRuns,
132134
isRestartContinuationSource,
133135
runOutcome,
134136
} from "./RestartBackgroundNote.ts";
@@ -13939,13 +13941,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1393913941
) {
1394013942
return;
1394113943
}
13942-
const taskRuns = childControls.runs.filter((run) => run.delegatedTaskId === options.task.id);
13944+
const taskRuns = delegatedTaskRuns(childControls, options.task.id);
1394313945
const childRun =
1394413946
taskRuns.length === 0
1394513947
? options.task.id === forkedFrom.nodeId
1394613948
? options.resultRun
1394713949
: undefined
13948-
: delegatedTaskResultRun(taskRuns, options.task.id);
13950+
: delegatedTaskResultRun(taskRuns);
1394913951
if (childRun === undefined) return;
1395013952
let outcome = runOutcome(childRun);
1395113953
let terminalStatus = delegatedTaskTerminalStatus(outcome);
@@ -14005,6 +14007,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1400514007
]);
1400614008
return;
1400714009
}
14010+
// A follow-up ends with the subagents its turns started, as the original task does.
14011+
if (delegatedTaskAwaitsSubagents(taskRuns, childControls.subagents)) return;
1400814012
if (
1400914013
yield* childAwaitsRestartContinuation(
1401014014
taskRuns.length === 0 ? childControls.runs : taskRuns,
@@ -15576,9 +15580,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1557615580
{ messageRoles: ["user"] },
1557715581
);
1557815582
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;
1558215591
return yield* childAwaitsRestartContinuation(runs, run);
1558315592
}
1558415593
const progress = delegatedTaskProgress(child);

0 commit comments

Comments
 (0)