Skip to content
TypeScript

fix(server): parent Stop clears orphaned delegated follow-ups (#20310)

kuna7792cd544515
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 3f2493f6e5a..9ae2803617f 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -20,6 +20,7 @@ import {
ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
type ChatAttachment,
CommandId,
+ isOrchestrationV2WorkActive,
isProviderNativeSubagentThread,
MessageId,
type NodeId,
@@ -9514,6 +9515,111 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
),
);
if (projection.thread.deletedAt !== null) return undefined;
+ const cancelIdleOrphans = Effect.gen(function* () {
+ const pending = yield* Ref.get(events);
+ const children = new Map<
+ ThreadId,
+ ProjectionRecords<"runs" | "providerThreads" | "runtimeRequests" | "turnItems"> | null
+ >();
+ const idleChildren = new Map<ThreadId, boolean>();
+ let nodes: ReadonlyArray<OrchestrationV2ExecutionNode> | undefined;
+ for (const recorded of projection.subagents) {
+ const updated = pending.findLast(
+ (event) => event.type === "subagent.updated" && event.payload.id === recorded.id,
+ );
+ const task = updated?.type === "subagent.updated" ? updated.payload : recorded;
+ if (
+ task.origin !== "app_owned" ||
+ task.childThreadId === null ||
+ !isOrchestrationV2WorkActive(task.status)
+ )
+ continue;
+ const childThreadId = task.childThreadId;
+ const child = children.has(childThreadId)
+ ? (children.get(childThreadId) ?? null)
+ : yield* projectionStore
+ .getThreadRecords(
+ childThreadId,
+ ["runs", "providerThreads", "runtimeRequests", "turnItems"],
+ {
+ turnItemTypes: ["command_execution", "dynamic_tool", "subagent"],
+ turnItemStatuses: ["pending", "running", "waiting"],
+ },
+ )
+ .pipe(
+ Effect.catchTags({
+ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null),
+ }),
+ Effect.mapError(
+ (cause) => new OrchestratorProjectionError({ threadId: childThreadId, cause }),
+ ),
+ );
+ children.set(childThreadId, child);
+ if (child === null) continue;
+ if (
+ child.thread.lineage.parentThreadId !== command.threadId ||
+ child.thread.lineage.relationshipToParent !== "subagent" ||
+ (child.thread.forkedFrom?.type === "node" &&
+ child.thread.forkedFrom.nodeId === task.id) ||
+ child.runs.some((run) => run.delegatedTaskId === task.id)
+ )
+ continue;
+ const idle =
+ idleChildren.get(task.childThreadId) ??
+ (!child.runs.some((run) => run.status === "queued" || isBlockingRun(run)) &&
+ !child.providerThreads.some(
+ (thread) =>
+ thread.status === "active" || (thread.pendingBackgroundTasks?.length ?? 0) > 0,
+ ) &&
+ !child.runtimeRequests.some((request) => request.status === "pending") &&
+ child.turnItems.length === 0 &&
+ Option.isNone(yield* Effect.findFirst(child.runs, awaitsRestartContinuation)));
+ idleChildren.set(task.childThreadId, idle);
+ if (!idle) continue;
+ // Stop can cancel an idle orphan without guessing which completed run owned its result.
+ const now = yield* DateTime.now;
+ const emitEvent = emit(events, command);
+ yield* emitEvent({
+ type: "subagent.updated",
+ threadId: command.threadId,
+ nodeId: task.id,
+ occurredAt: now,
+ payload: {
+ ...task,
+ status: "cancelled",
+ completedAt: now,
+ updatedAt: now,
+ completionDelivery: { state: "disposed", observedByRunId: null },
+ },
+ });
+ nodes ??= (yield* projectionStore.getThreadRecords(command.threadId, ["nodes"])).nodes;
+ const node = nodes.find((candidate) => candidate.id === task.id);
+ if (node !== undefined)
+ yield* emitEvent({
+ type: "node.updated",
+ threadId: command.threadId,
+ nodeId: node.id,
+ occurredAt: now,
+ payload: { ...node, status: "cancelled", completedAt: now },
+ });
+ for (const item of projection.turnItems) {
+ if (item.type !== "subagent" || item.subagentId !== task.id) continue;
+ yield* emitEvent({
+ type: "turn-item.updated",
+ threadId: command.threadId,
+ nodeId: task.id,
+ occurredAt: now,
+ payload: { ...item, status: "cancelled", completedAt: now, updatedAt: now },
+ });
+ }
+ }
+ }).pipe(
+ Effect.mapError((cause) =>
+ Schema.is(OrchestratorProjectionError)(cause)
+ ? cause
+ : new OrchestratorProjectionError({ threadId: command.threadId, cause }),
+ ),
+ );
const latestRun = projection.runs.findLast((run) => run.status !== "queued");
const target =
projection.runs.findLast(
@@ -9552,6 +9658,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const enqueued = yield* Ref.get(interruptEffects);
yield* Ref.update(events, (existing) => [...existing, ...written]);
yield* Ref.update(effects, (existing) => [...existing, ...enqueued]);
+ yield* cancelIdleOrphans;
return interrupted.value;
}
yield* Effect.logWarning("thread.stop could not interrupt the running turn", {
@@ -9572,6 +9679,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
yield* markStoppedRun({ command, events, thread: projection.thread, run, now });
}
yield* holdStoppedThread({ command, events, projection, cohortRunIds: [], now });
+ yield* cancelIdleOrphans;
return undefined;
});
diff --git a/apps/server/src/orchestration-v2/ThreadStop.test.ts b/apps/server/src/orchestration-v2/ThreadStop.test.ts
index d49ca40e2ba..66f0d73c4a5 100644
--- a/apps/server/src/orchestration-v2/ThreadStop.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadStop.test.ts
@@ -1,9 +1,11 @@
import { assert, it } from "@effect/vitest";
import {
CommandId,
+ ContextTransferId,
EventId,
MessageId,
NodeId,
+ type OrchestrationV2DomainEvent,
ProjectId,
ProviderDriverKind,
ProviderInstanceId,
@@ -157,6 +159,267 @@ const threadState = (threadId: ThreadId) =>
};
});
+it.effect.each([
+ "idle",
+ "archived",
+ "missing",
+ "running",
+ "queued",
+ "waiting",
+ "correlated",
+ "provider-active",
+ "roster",
+ "background",
+ "question",
+ "restart",
+] as const)(
+ "Stop clears an orphaned follow-up only when its child is finished (%s)",
+ (childState) =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
+ const parentThreadId = ThreadId.make(`thread:orphan-parent:${childState}`);
+ yield* createWatchingThread(parentThreadId, 1);
+ yield* send(parentThreadId, "parent", "start_immediately");
+ const childThreadId = yield* delegate(parentThreadId, "original task");
+ const parent = yield* orchestrator.getThreadProjection(parentThreadId);
+ const child = yield* orchestrator.getThreadProjection(childThreadId);
+ const original = parent.subagents[0]!;
+ const originalItem = parent.turnItems.find((item) => item.type === "subagent")!;
+ const originalNode = parent.nodes.find((node) => node.id === original.id)!;
+ const taskId = NodeId.make(`node:orphan-followup:${childState}`);
+ const itemId = TurnItemId.make(`item:orphan-followup:${childState}`);
+ const followupChildThreadId =
+ childState === "missing" ? ThreadId.make("thread:missing-child") : childThreadId;
+ const now = yield* DateTime.now;
+ let sequence = 0;
+ const apply = (event: Omit<OrchestrationV2DomainEvent, "id">) =>
+ projections.apply({
+ ...event,
+ id: EventId.make(`event:orphan:${childState}:${sequence++}`),
+ } as OrchestrationV2DomainEvent);
+ yield* apply({
+ type: "subagent.updated",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: {
+ ...original,
+ status: "completed",
+ result: "Original result",
+ completedAt: now,
+ completionDelivery: { state: "acknowledged", observedByRunId: null },
+ },
+ });
+ yield* apply({
+ type: "context-transfer.created",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: {
+ ...child.contextTransfers[0]!,
+ id: ContextTransferId.make(`transfer:original-result:${childState}`),
+ type: "subagent_result",
+ sourceThreadId: childThreadId,
+ targetThreadId: parentThreadId,
+ sourcePoint: { threadId: childThreadId, runId: child.runs[0]!.id },
+ targetRunId: parent.runs[0]!.id,
+ },
+ });
+ yield* apply({
+ type: "node.updated",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: { ...originalNode, status: "completed", completedAt: now },
+ });
+ yield* apply({
+ type: "turn-item.updated",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: { ...originalItem, status: "completed", completedAt: now },
+ });
+ yield* apply({
+ type: "subagent.updated",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: {
+ ...original,
+ id: taskId,
+ childThreadId: followupChildThreadId,
+ status: "running",
+ result: null,
+ completedAt: null,
+ },
+ });
+ yield* apply({
+ type: "node.updated",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: { ...originalNode, id: taskId, status: "running" },
+ });
+ assert.equal(originalItem.type, "subagent");
+ if (originalItem.type !== "subagent") return;
+ yield* apply({
+ type: "turn-item.updated",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: {
+ ...originalItem,
+ id: itemId,
+ nodeId: taskId,
+ subagentId: taskId,
+ childThreadId: followupChildThreadId,
+ status: "running",
+ },
+ });
+ const runAtStart = child.runs[0]!;
+ const childRunStatus =
+ childState === "running" || childState === "queued" || childState === "waiting"
+ ? childState
+ : "completed";
+ yield* apply({
+ type: "run.updated",
+ threadId: childThreadId,
+ occurredAt: now,
+ payload: { ...runAtStart, status: "running", delegatedTaskId: taskId },
+ });
+ assert.equal(
+ (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]?.delegatedTaskId,
+ taskId,
+ );
+ // A terminal write based on the attempt's start snapshot loses a later steer binding.
+ yield* apply({
+ type: "run.updated",
+ threadId: childThreadId,
+ occurredAt: now,
+ payload: {
+ ...runAtStart,
+ status: childRunStatus,
+ completedAt: childRunStatus === "completed" ? now : null,
+ ...(childState === "correlated" ? { delegatedTaskId: taskId } : {}),
+ },
+ });
+ assert.equal(
+ (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]?.delegatedTaskId,
+ childState === "correlated" ? taskId : undefined,
+ );
+ if (childState === "provider-active" || childState === "roster") {
+ yield* apply({
+ type: "provider-thread.updated",
+ threadId: childThreadId,
+ occurredAt: now,
+ payload: {
+ ...child.providerThreads[0]!,
+ status: childState === "provider-active" ? "active" : "idle",
+ pendingBackgroundTasks:
+ childState === "roster" ? [{ taskId: "background-task", kind: "command" }] : [],
+ },
+ });
+ }
+ if (childState === "background") {
+ yield* apply({
+ type: "turn-item.updated",
+ threadId: childThreadId,
+ occurredAt: now,
+ payload: {
+ ...originalItem,
+ id: TurnItemId.make("item:child-background"),
+ threadId: childThreadId,
+ runId: runAtStart.id,
+ nodeId: runAtStart.rootNodeId,
+ type: "command_execution",
+ status: "running",
+ input: "test background work",
+ },
+ });
+ }
+ if (childState === "question") {
+ yield* apply({
+ type: "runtime-request.updated",
+ threadId: childThreadId,
+ occurredAt: now,
+ payload: {
+ id: RuntimeRequestId.make("request:child-question"),
+ nodeId: runAtStart.rootNodeId!,
+ providerTurnId: null,
+ nativeRequestRef: null,
+ kind: "user_input",
+ status: "pending",
+ responseCapability: { type: "message" },
+ createdAt: now,
+ resolvedAt: null,
+ },
+ });
+ }
+ if (childState === "restart") {
+ const outbox = yield* EffectOutbox.EffectOutboxV2;
+ yield* outbox.enqueue([
+ {
+ id: `effect:restart-continuation:${runAtStart.id}`,
+ commandId: CommandId.make("command:child-restart"),
+ threadId: childThreadId,
+ request: { type: "provider-runtime.continue", sourceRunId: runAtStart.id },
+ },
+ ]);
+ }
+ yield* apply({
+ type: "run.updated",
+ threadId: parentThreadId,
+ occurredAt: now,
+ payload: { ...parent.runs[0]!, status: "completed", completedAt: now },
+ });
+ if (childState === "archived") {
+ yield* apply({
+ type: "thread.archived",
+ threadId: childThreadId,
+ occurredAt: now,
+ payload: { ...child.thread, archivedAt: now },
+ });
+ }
+ const childBeforeStop = yield* orchestrator.getThreadProjection(childThreadId);
+ assert.lengthOf(
+ ((yield* orchestrator.getThreadShell(parentThreadId))?.pendingBackgroundTasks ?? []).filter(
+ (task) => task.kind === "subagent",
+ ),
+ 1,
+ );
+ yield* orchestrator.dispatch({
+ type: "thread.stop",
+ commandId: CommandId.make(`stop-orphan:${childState}`),
+ threadId: parentThreadId,
+ });
+ const after = yield* orchestrator.getThreadProjection(parentThreadId);
+ assert.isEmpty((after.thread.pullRequests ?? []).filter((link) => link.watch !== undefined));
+ const expected = childState === "idle" || childState === "archived" ? "cancelled" : "running";
+ assert.equal(after.subagents.find((task) => task.id === taskId)?.status, expected);
+ assert.equal(after.nodes.find((node) => node.id === taskId)?.status, expected);
+ assert.equal(after.turnItems.find((item) => item.id === itemId)?.status, expected);
+ assert.equal(
+ after.subagents.find((task) => task.id === taskId)?.completionDelivery?.state,
+ "disposed",
+ );
+ assert.equal(
+ after.subagents.find((task) => task.id === original.id)?.result,
+ "Original result",
+ );
+ assert.deepEqual(yield* orchestrator.getThreadProjection(childThreadId), childBeforeStop);
+ if (expected === "cancelled") {
+ assert.lengthOf(
+ (yield* orchestrator.getThreadShell(parentThreadId))?.pendingBackgroundTasks ?? [],
+ 0,
+ );
+ yield* orchestrator.recoverDelegatedTasks;
+ yield* orchestrator.dispatch({
+ type: "thread.stop",
+ commandId: CommandId.make(`stop-orphan-again:${childState}`),
+ threadId: parentThreadId,
+ });
+ const repeated = yield* orchestrator.getThreadProjection(parentThreadId);
+ assert.equal(repeated.subagents.find((task) => task.id === taskId)?.status, "cancelled");
+ assert.deepEqual(repeated.messages, after.messages);
+ assert.deepEqual(repeated.contextTransfers, after.contextTransfers);
+ }
+ }).pipe(Effect.provide(layerTest)),
+);
+
it.effect("Stop ends watches, holds queues, and stops the delegated tasks under the thread", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;