TypeScript
fix(server): stop both completion-wake task cohorts (#15331)
Mats Varnskühler6cd6372011dc
diff --git a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.tsindex 5220c4b6799..c188d60d4b7 100644--- a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts+++ b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts@@ -293,6 +293,152 @@ const seedParentWithTerminalTask = (input: {});it.layer(layerTest)("delegated completion delivery repairs", (it) => {+ it.effect(+ "interrupting a completion wake disposes both cohorts without reviving them on late results",+ () =>+ Effect.gen(function* () {+ const orchestrator = yield* Orchestrator.OrchestratorV2;+ const sink = yield* EventSink.EventSinkV2;+ const now = yield* DateTime.now;+ const threadId = ThreadId.make("stop-wake-parent");+ const originRunId = RunId.make("stop-wake-origin");+ const incomingTaskId = NodeId.make("stop-wake-incoming");+ yield* seedParentWithTerminalTask({+ threadId,+ projectId: ProjectId.make("stop-wake-project"),+ runId: originRunId,+ rootNodeId: NodeId.make("stop-wake-origin-root"),+ taskId: incomingTaskId,+ deliveryState: "claimed",+ deliveryTaskIds: [incomingTaskId],+ completionWake: "always",+ now,+ });+ const origin = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;+ yield* sink.write({+ events: [+ {+ ...runEvent({ threadId, runId: originRunId, ordinal: 1, status: "completed", now }),+ payload: { ...origin, status: "completed", completedAt: now },+ },+ ],+ });+ const delivery = origin.delegatedCompletion!.delivery!;+ yield* orchestrator.dispatch({+ type: "message.dispatch",+ createdBy: "agent",+ creationSource: "server",+ commandId: CommandId.make("stop-wake-start"),+ threadId,+ messageId: delivery.messageId,+ text: "Delegated task completed",+ attachments: [],+ modelSelection,+ dispatchMode: { type: "queue_after_active" },+ delegatedCompletion: {+ parentRunId: originRunId,+ generation: delivery.generation,+ taskIds: delivery.taskIds,+ },+ });+ yield* orchestrator.resumeQueuedRuns;+ const wake = (yield* orchestrator.getThreadProjection(threadId)).runs.find(+ (run) => run.userMessageId === delivery.messageId,+ )!;+ yield* orchestrator.dispatch({+ type: "delegated_task.request",+ createdBy: "agent",+ creationSource: "mcp",+ commandId: CommandId.make("stop-wake-delegate"),+ parentThreadId: threadId,+ parentRunId: wake.id,+ parentNodeId: wake.rootNodeId!,+ task: "Do the next round",+ modelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ completionWake: "always",+ });+ const delegated = (yield* orchestrator.getThreadProjection(threadId)).subagents.find(+ (task) => task.runId === wake.id,+ )!;+ yield* orchestrator.dispatch({+ type: "run.interrupt",+ commandId: CommandId.make("stop-wake-interrupt"),+ threadId,+ runId: wake.id,+ });+ const stopped = yield* orchestrator.getThreadProjection(threadId);+ for (const runId of [originRunId, wake.id]) {+ const run = stopped.runs.find((row) => row.id === runId)!;+ assert.equal(run.delegatedCompletion?.disposition, "stopped");+ assert.isNull(run.delegatedCompletion?.delivery);+ }+ assert.equal(stopped.runs.find((run) => run.id === wake.id)?.status, "interrupted");+ for (const taskId of [incomingTaskId, delegated.id]) {+ assert.equal(+ stopped.subagents.find((task) => task.id === taskId)?.completionDelivery?.state,+ "disposed",+ );+ }+ assert.equal(+ stopped.subagents.find((task) => task.id === incomingTaskId)?.result,+ "child finished",+ );++ yield* orchestrator.dispatch({+ type: "message.dispatch",+ createdBy: "user",+ creationSource: "web",+ commandId: CommandId.make("stop-wake-newer-work"),+ threadId,+ messageId: MessageId.make("stop-wake-newer-work"),+ text: "Start unrelated work",+ attachments: [],+ modelSelection,+ dispatchMode: { type: "queue_after_active" },+ });+ yield* orchestrator.resumeQueuedRuns;+ const newer = yield* orchestrator.getThreadProjection(threadId);+ assert.equal(newer.runs.at(-1)?.status, "starting");++ const childThreadId = delegated.childThreadId!;+ const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]!;+ const afterSequence = yield* sink.latestSequence();+ yield* sink.write({+ events: [+ {+ ...runEvent({+ threadId: childThreadId,+ runId: childRun.id,+ ordinal: childRun.ordinal,+ status: "completed",+ now,+ }),+ payload: { ...childRun, status: "completed", completedAt: now },+ },+ ],+ });+ yield* sink.stream({ afterSequence, eventType: "subagent.updated" }).pipe(+ Stream.filter(+ (stored) =>+ stored.event.type === "subagent.updated" &&+ stored.event.payload.id === delegated.id &&+ stored.event.payload.status === "completed",+ ),+ Stream.take(1),+ Stream.runDrain,+ );+ const finished = yield* orchestrator.getThreadProjection(threadId);+ const lateTask = finished.subagents.find((task) => task.id === delegated.id)!;+ assert.equal(lateTask.status, "completed");+ assert.isNotNull(lateTask.result);+ assert.equal(lateTask.completionDelivery?.state, "disposed");+ assert.deepEqual(finished.runs, newer.runs);+ assert.deepEqual(finished.messages, newer.messages);+ }),+ );+it.effect("acceptance batches pending siblings without acknowledging their results", () =>Effect.gen(function* () {const orchestrator = yield* Orchestrator.OrchestratorV2;diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 7e82d714917..86f591c16bb 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -9345,14 +9345,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functionow,}): Effect.gen(function* () {- yield* disposeDelegatedCompletionCohort({- command,- events,- projection: yield* getProjectionWithPendingEvents(command.threadId, events),- parentRunId: completionCohortRunId,- disposition: "stopped",- now,- });+ for (const parentRunId of new Set([completionCohortRunId, run.id])) {+ yield* disposeDelegatedCompletionCohort({+ command,+ events,+ projection: yield* getProjectionWithPendingEvents(command.threadId, events),+ parentRunId,+ disposition: "stopped",+ now,+ });+ }});const emitEvent = emit(events, command);