Skip to content

Commit ac1cb92

Browse files
fix(server): archive refuses a thread with running delegated tasks (#26078)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 48a56fa commit ac1cb92

4 files changed

Lines changed: 148 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3994,6 +3994,25 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
39943994
cause: `Thread ${command.threadId} is already archived.`,
39953995
});
39963996
}
3997+
if (command.type === "thread.archive") {
3998+
const records = yield* projectionStore
3999+
.getThreadRecords(command.threadId, ["subagents"])
4000+
.pipe(mapDispatchError(command));
4001+
if (
4002+
records.subagents.some(
4003+
(task) =>
4004+
task.origin === "app_owned" &&
4005+
task.childThreadId !== null &&
4006+
isOrchestrationV2WorkActive(task.status),
4007+
)
4008+
) {
4009+
return yield* new OrchestratorDispatchError({
4010+
commandId: command.commandId,
4011+
commandType: command.type,
4012+
cause: `Thread ${command.threadId} has running delegated tasks. Wait for them or cancel them before archiving.`,
4013+
});
4014+
}
4015+
}
39974016
if (command.type === "thread.unarchive" && thread.archivedAt === null) {
39984017
return yield* new OrchestratorDispatchError({
39994018
commandId: command.commandId,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ it.layer(layerTest)("a delegated task in a linked environment", (it) => {
226226
const index = yield* (yield* ProjectionStore.ProjectionStoreV2).listActiveShellIndex();
227227
assert.isTrue(index.find((entry) => entry.id === parent.threadId)?.hasRemoteTasks);
228228
assert.isFalse(index.find((entry) => entry.id === other.threadId)?.hasRemoteTasks);
229+
// Nothing here can cancel the task there, so it must not block archiving.
230+
yield* orchestrator.dispatch({
231+
type: "thread.archive",
232+
commandId: CommandId.make("command:archive:records"),
233+
threadId: parent.threadId,
234+
});
235+
assert.isNotNull((yield* orchestrator.getThreadShell(parent.threadId))?.archivedAt);
229236
}),
230237
);
231238

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6394,6 +6394,127 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
63946394
}),
63956395
);
63966396

6397+
it.effect("rejects archiving an idle parent until its delegated child completes", () =>
6398+
Effect.gen(function* () {
6399+
const orchestrator = yield* Orchestrator.OrchestratorV2;
6400+
const sink = yield* EventSink.EventSinkV2;
6401+
const threadId = ThreadId.make("archive-delegated-parent");
6402+
const projectId = ProjectId.make("archive-delegated-project");
6403+
yield* seedProject({
6404+
projectId,
6405+
title: "Delegated archive",
6406+
workspaceRoot: "/workspace/archive-delegated",
6407+
defaultModelSelection: null,
6408+
createdAt: "2026-09-07T00:00:00.000Z",
6409+
});
6410+
yield* orchestrator.dispatch({
6411+
type: "thread.create",
6412+
createdBy: "user",
6413+
creationSource: "web",
6414+
commandId: CommandId.make("archive-delegated-create"),
6415+
threadId,
6416+
projectId,
6417+
title: "Delegated archive",
6418+
modelSelection,
6419+
runtimeMode: "full-access",
6420+
interactionMode: "default",
6421+
branch: null,
6422+
worktreePath: null,
6423+
});
6424+
yield* orchestrator.dispatch({
6425+
type: "message.dispatch",
6426+
createdBy: "user",
6427+
creationSource: "web",
6428+
commandId: CommandId.make("archive-delegated-message"),
6429+
threadId,
6430+
messageId: MessageId.make("archive-delegated-message"),
6431+
text: "Delegate the work, then finish your turn.",
6432+
attachments: [],
6433+
modelSelection,
6434+
dispatchMode: { type: "start_immediately" },
6435+
});
6436+
const parentRun = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
6437+
yield* orchestrator.dispatch({
6438+
type: "delegated_task.request",
6439+
createdBy: "agent",
6440+
creationSource: "mcp",
6441+
commandId: CommandId.make("archive-delegated-request"),
6442+
parentThreadId: threadId,
6443+
parentRunId: parentRun.id,
6444+
parentNodeId: parentRun.rootNodeId!,
6445+
task: "Keep working after the parent finishes.",
6446+
modelSelection,
6447+
runtimeMode: "full-access",
6448+
interactionMode: "default",
6449+
completionWake: "always",
6450+
});
6451+
const delegatedParentRun = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
6452+
const now = yield* DateTime.now;
6453+
yield* sink.write({
6454+
events: [
6455+
{
6456+
id: EventId.make("archive-delegated-parent-completed"),
6457+
type: "run.updated",
6458+
threadId,
6459+
runId: parentRun.id,
6460+
occurredAt: now,
6461+
payload: { ...delegatedParentRun, status: "completed", completedAt: now },
6462+
},
6463+
],
6464+
});
6465+
const parent = yield* orchestrator.getThreadProjection(threadId);
6466+
const task = parent.subagents[0]!;
6467+
assert.equal(parent.runs[0]?.status, "completed");
6468+
assert.equal(task.origin, "app_owned");
6469+
assert.equal(task.status, "running");
6470+
assert.isNotNull(task.childThreadId);
6471+
6472+
// Archive must preserve the idle parent's path for receiving this child's result.
6473+
const error = yield* orchestrator
6474+
.dispatch({
6475+
type: "thread.archive",
6476+
commandId: CommandId.make("archive-delegated-blocked"),
6477+
threadId,
6478+
})
6479+
.pipe(Effect.flip);
6480+
assert.instanceOf(error, Orchestrator.OrchestratorDispatchError);
6481+
assert.equal(
6482+
error.cause,
6483+
`Thread ${threadId} has running delegated tasks. Wait for them or cancel them before archiving.`,
6484+
);
6485+
assert.isNull((yield* orchestrator.getThreadProjection(threadId)).thread.archivedAt);
6486+
6487+
const childThreadId = task.childThreadId!;
6488+
const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]!;
6489+
yield* sink.write({
6490+
events: [
6491+
{
6492+
id: EventId.make("archive-delegated-child-run-completed"),
6493+
type: "run.updated",
6494+
threadId: childThreadId,
6495+
runId: childRun.id,
6496+
occurredAt: now,
6497+
payload: { ...childRun, status: "completed", completedAt: now },
6498+
},
6499+
{
6500+
id: EventId.make("archive-delegated-child-completed"),
6501+
type: "subagent.updated",
6502+
threadId,
6503+
nodeId: task.id,
6504+
occurredAt: now,
6505+
payload: { ...task, status: "completed", result: "Done", completedAt: now },
6506+
},
6507+
],
6508+
});
6509+
yield* orchestrator.dispatch({
6510+
type: "thread.archive",
6511+
commandId: CommandId.make("archive-delegated-allowed"),
6512+
threadId,
6513+
});
6514+
assert.isNotNull((yield* orchestrator.getThreadProjection(threadId)).thread.archivedAt);
6515+
}),
6516+
);
6517+
63976518
it.effect("cancels preparing work on archive and keeps it cancelled through replay", () =>
63986519
Effect.gen(function* () {
63996520
const orchestrator = yield* Orchestrator.OrchestratorV2;

‎docs/user/thread-sidebar.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ On mobile, open a subagent from its conversation card or **Agents**. On iOS
273273
layouts with an inspector, its thread opens beside the parent. Maximize it to
274274
fill the screen; close the full-screen view to return to the parent and inspector.
275275
Stop on a thread also stops the subagents it delegated to.
276+
Before archiving the parent, wait for its delegated tasks to finish or cancel them.
276277

277278
Subagent threads started by the agent can't take messages; message the parent
278279
thread instead. When such a subagent needs an approval or an answer, the parent

0 commit comments

Comments
 (0)