Skip to content

Commit 6cd6372

Browse files
fix(server): stop both completion-wake task cohorts (pingdotgg#15331)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 1d172c3 commit 6cd6372

2 files changed

Lines changed: 156 additions & 8 deletions

File tree

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,152 @@ const seedParentWithTerminalTask = (input: {
293293
});
294294

295295
it.layer(layerTest)("delegated completion delivery repairs", (it) => {
296+
it.effect(
297+
"interrupting a completion wake disposes both cohorts without reviving them on late results",
298+
() =>
299+
Effect.gen(function* () {
300+
const orchestrator = yield* Orchestrator.OrchestratorV2;
301+
const sink = yield* EventSink.EventSinkV2;
302+
const now = yield* DateTime.now;
303+
const threadId = ThreadId.make("stop-wake-parent");
304+
const originRunId = RunId.make("stop-wake-origin");
305+
const incomingTaskId = NodeId.make("stop-wake-incoming");
306+
yield* seedParentWithTerminalTask({
307+
threadId,
308+
projectId: ProjectId.make("stop-wake-project"),
309+
runId: originRunId,
310+
rootNodeId: NodeId.make("stop-wake-origin-root"),
311+
taskId: incomingTaskId,
312+
deliveryState: "claimed",
313+
deliveryTaskIds: [incomingTaskId],
314+
completionWake: "always",
315+
now,
316+
});
317+
const origin = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
318+
yield* sink.write({
319+
events: [
320+
{
321+
...runEvent({ threadId, runId: originRunId, ordinal: 1, status: "completed", now }),
322+
payload: { ...origin, status: "completed", completedAt: now },
323+
},
324+
],
325+
});
326+
const delivery = origin.delegatedCompletion!.delivery!;
327+
yield* orchestrator.dispatch({
328+
type: "message.dispatch",
329+
createdBy: "agent",
330+
creationSource: "server",
331+
commandId: CommandId.make("stop-wake-start"),
332+
threadId,
333+
messageId: delivery.messageId,
334+
text: "Delegated task completed",
335+
attachments: [],
336+
modelSelection,
337+
dispatchMode: { type: "queue_after_active" },
338+
delegatedCompletion: {
339+
parentRunId: originRunId,
340+
generation: delivery.generation,
341+
taskIds: delivery.taskIds,
342+
},
343+
});
344+
yield* orchestrator.resumeQueuedRuns;
345+
const wake = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
346+
(run) => run.userMessageId === delivery.messageId,
347+
)!;
348+
yield* orchestrator.dispatch({
349+
type: "delegated_task.request",
350+
createdBy: "agent",
351+
creationSource: "mcp",
352+
commandId: CommandId.make("stop-wake-delegate"),
353+
parentThreadId: threadId,
354+
parentRunId: wake.id,
355+
parentNodeId: wake.rootNodeId!,
356+
task: "Do the next round",
357+
modelSelection,
358+
runtimeMode: "full-access",
359+
interactionMode: "default",
360+
completionWake: "always",
361+
});
362+
const delegated = (yield* orchestrator.getThreadProjection(threadId)).subagents.find(
363+
(task) => task.runId === wake.id,
364+
)!;
365+
yield* orchestrator.dispatch({
366+
type: "run.interrupt",
367+
commandId: CommandId.make("stop-wake-interrupt"),
368+
threadId,
369+
runId: wake.id,
370+
});
371+
const stopped = yield* orchestrator.getThreadProjection(threadId);
372+
for (const runId of [originRunId, wake.id]) {
373+
const run = stopped.runs.find((row) => row.id === runId)!;
374+
assert.equal(run.delegatedCompletion?.disposition, "stopped");
375+
assert.isNull(run.delegatedCompletion?.delivery);
376+
}
377+
assert.equal(stopped.runs.find((run) => run.id === wake.id)?.status, "interrupted");
378+
for (const taskId of [incomingTaskId, delegated.id]) {
379+
assert.equal(
380+
stopped.subagents.find((task) => task.id === taskId)?.completionDelivery?.state,
381+
"disposed",
382+
);
383+
}
384+
assert.equal(
385+
stopped.subagents.find((task) => task.id === incomingTaskId)?.result,
386+
"child finished",
387+
);
388+
389+
yield* orchestrator.dispatch({
390+
type: "message.dispatch",
391+
createdBy: "user",
392+
creationSource: "web",
393+
commandId: CommandId.make("stop-wake-newer-work"),
394+
threadId,
395+
messageId: MessageId.make("stop-wake-newer-work"),
396+
text: "Start unrelated work",
397+
attachments: [],
398+
modelSelection,
399+
dispatchMode: { type: "queue_after_active" },
400+
});
401+
yield* orchestrator.resumeQueuedRuns;
402+
const newer = yield* orchestrator.getThreadProjection(threadId);
403+
assert.equal(newer.runs.at(-1)?.status, "starting");
404+
405+
const childThreadId = delegated.childThreadId!;
406+
const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]!;
407+
const afterSequence = yield* sink.latestSequence();
408+
yield* sink.write({
409+
events: [
410+
{
411+
...runEvent({
412+
threadId: childThreadId,
413+
runId: childRun.id,
414+
ordinal: childRun.ordinal,
415+
status: "completed",
416+
now,
417+
}),
418+
payload: { ...childRun, status: "completed", completedAt: now },
419+
},
420+
],
421+
});
422+
yield* sink.stream({ afterSequence, eventType: "subagent.updated" }).pipe(
423+
Stream.filter(
424+
(stored) =>
425+
stored.event.type === "subagent.updated" &&
426+
stored.event.payload.id === delegated.id &&
427+
stored.event.payload.status === "completed",
428+
),
429+
Stream.take(1),
430+
Stream.runDrain,
431+
);
432+
const finished = yield* orchestrator.getThreadProjection(threadId);
433+
const lateTask = finished.subagents.find((task) => task.id === delegated.id)!;
434+
assert.equal(lateTask.status, "completed");
435+
assert.isNotNull(lateTask.result);
436+
assert.equal(lateTask.completionDelivery?.state, "disposed");
437+
assert.deepEqual(finished.runs, newer.runs);
438+
assert.deepEqual(finished.messages, newer.messages);
439+
}),
440+
);
441+
296442
it.effect("acceptance batches pending siblings without acknowledging their results", () =>
297443
Effect.gen(function* () {
298444
const orchestrator = yield* Orchestrator.OrchestratorV2;

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9345,14 +9345,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
93459345
now,
93469346
})
93479347
: Effect.gen(function* () {
9348-
yield* disposeDelegatedCompletionCohort({
9349-
command,
9350-
events,
9351-
projection: yield* getProjectionWithPendingEvents(command.threadId, events),
9352-
parentRunId: completionCohortRunId,
9353-
disposition: "stopped",
9354-
now,
9355-
});
9348+
for (const parentRunId of new Set([completionCohortRunId, run.id])) {
9349+
yield* disposeDelegatedCompletionCohort({
9350+
command,
9351+
events,
9352+
projection: yield* getProjectionWithPendingEvents(command.threadId, events),
9353+
parentRunId,
9354+
disposition: "stopped",
9355+
now,
9356+
});
9357+
}
93569358
});
93579359

93589360
const emitEvent = emit(events, command);

0 commit comments

Comments
 (0)