Skip to content

Commit d212001

Browse files
committed
fix(server): Stop on a completion-wake run also disposes the tasks it delegated
Stopping a run woken by a delegated completion only disposed the incoming cohort, so tasks delegated during that wake run could still wake the parent after Stop. Dispose the wake run's own cohort as well. Fixes #15325
1 parent a21702d commit d212001

2 files changed

Lines changed: 118 additions & 11 deletions

File tree

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,101 @@ it.layer(TestLayer)("delegated completion delivery repairs", (it) => {
652652
}),
653653
);
654654
});
655+
656+
it.effect(
657+
"Stop disposes tasks spawned during a completion-wake run as well as its incoming cohort",
658+
() =>
659+
Effect.gen(function* () {
660+
const orchestrator = yield* Orchestrator.OrchestratorV2;
661+
const sink = yield* EventSink.EventSinkV2;
662+
const now = yield* DateTime.now;
663+
const threadId = ThreadId.make("stop-wake-parent");
664+
const originRunId = RunId.make("stop-wake-origin");
665+
const taskId = NodeId.make("stop-wake-incoming-task");
666+
yield* seedParentWithTerminalTask({
667+
threadId,
668+
projectId: ProjectId.make("stop-wake-project"),
669+
runId: originRunId,
670+
rootNodeId: NodeId.make("stop-wake-origin-root"),
671+
taskId,
672+
deliveryState: "claimed",
673+
deliveryTaskIds: [taskId],
674+
completionWake: "always",
675+
now,
676+
});
677+
const original = yield* orchestrator.getThreadProjection(threadId);
678+
yield* sink.write({
679+
events: [
680+
{
681+
id: EventId.make("stop-wake-origin-completed"),
682+
type: "run.updated",
683+
threadId,
684+
runId: originRunId,
685+
occurredAt: now,
686+
payload: { ...original.runs[0]!, status: "completed", completedAt: now },
687+
},
688+
],
689+
});
690+
const delivery = original.runs[0]!.delegatedCompletion!.delivery!;
691+
yield* orchestrator.dispatch({
692+
type: "message.dispatch",
693+
createdBy: "agent",
694+
creationSource: "server",
695+
commandId: CommandId.make("stop-wake-start"),
696+
threadId,
697+
messageId: delivery.messageId,
698+
text: "Delegated task completed",
699+
attachments: [],
700+
modelSelection,
701+
dispatchMode: { type: "queue_after_active" },
702+
delegatedCompletion: {
703+
parentRunId: originRunId,
704+
generation: delivery.generation,
705+
taskIds: [taskId],
706+
},
707+
});
708+
yield* orchestrator.resumeQueuedRuns;
709+
const wake = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
710+
(run) => run.userMessageId === delivery.messageId,
711+
)!;
712+
const requested = yield* orchestrator.dispatch({
713+
type: "delegated_task.request",
714+
createdBy: "agent",
715+
creationSource: "mcp",
716+
commandId: CommandId.make("stop-wake-new-task"),
717+
parentThreadId: threadId,
718+
parentRunId: wake.id,
719+
parentNodeId: wake.rootNodeId!,
720+
task: "Do the next round",
721+
modelSelection,
722+
runtimeMode: "full-access",
723+
interactionMode: "default",
724+
completionWake: "always",
725+
});
726+
const taskEvent = requested.storedEvents.find(
727+
(stored) => stored.event.type === "subagent.updated",
728+
);
729+
if (taskEvent?.event.type !== "subagent.updated")
730+
return yield* Effect.die("Missing new task");
731+
const newTaskId = taskEvent.event.payload.id;
732+
yield* orchestrator.dispatch({
733+
type: "run.interrupt",
734+
commandId: CommandId.make("stop-wake-stop"),
735+
threadId,
736+
runId: wake.id,
737+
});
738+
const stopped = yield* orchestrator.getThreadProjection(threadId);
739+
assert.equal(
740+
stopped.subagents.find((task) => task.id === newTaskId)?.completionDelivery?.state,
741+
"disposed",
742+
);
743+
assert.equal(
744+
stopped.runs.find((run) => run.id === wake.id)?.delegatedCompletion?.disposition,
745+
"stopped",
746+
);
747+
assert.equal(
748+
stopped.runs.find((run) => run.id === originRunId)?.delegatedCompletion?.disposition,
749+
"stopped",
750+
);
751+
}).pipe(Effect.provide(TestLayer)),
752+
);

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7929,18 +7929,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
79297929
const completionMessage = projection.messages.find(
79307930
(candidate) => candidate.id === run.userMessageId,
79317931
);
7932-
const completionCohortRunId = completionMessage?.delegatedCompletion?.parentRunId ?? run.id;
7932+
// A completion-wake run owns its incoming cohort and any tasks it delegated itself.
7933+
const completionCohortRunIds = new Set([
7934+
completionMessage?.delegatedCompletion?.parentRunId ?? run.id,
7935+
run.id,
7936+
]);
79337937
const stopCompletionCohort = () =>
7934-
Effect.gen(function* () {
7935-
yield* disposeDelegatedCompletionCohort({
7936-
command,
7937-
events,
7938-
projection: yield* getProjectionWithPendingEvents(command.threadId, events),
7939-
parentRunId: completionCohortRunId,
7940-
disposition: "stopped",
7941-
now,
7942-
});
7943-
});
7938+
Effect.forEach(
7939+
completionCohortRunIds,
7940+
(parentRunId) =>
7941+
Effect.gen(function* () {
7942+
yield* disposeDelegatedCompletionCohort({
7943+
command,
7944+
events,
7945+
projection: yield* getProjectionWithPendingEvents(command.threadId, events),
7946+
parentRunId,
7947+
disposition: "stopped",
7948+
now,
7949+
});
7950+
}),
7951+
{ discard: true },
7952+
);
79447953

79457954
const emitEvent = emit(events, command);
79467955
const holdQueuedRuns = Effect.forEach(

0 commit comments

Comments
 (0)