Skip to content

Commit 189f0f8

Browse files
authored
fix(server): cancel preparing runs when archiving threads (pingdotgg#20073)
1 parent ab89a59 commit 189f0f8

2 files changed

Lines changed: 143 additions & 1 deletion

File tree

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3341,8 +3341,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33413341
);
33423342
const emitEvent = emit(events, command);
33433343
const activeRunIds = new Set(
3344-
projection.runs.filter((run) => run.status === "queued").map((run) => run.id),
3344+
projection.runs
3345+
.filter((run) => run.status === "queued" || run.status === "preparing")
3346+
.map((run) => run.id),
33453347
);
3348+
const preparingRuns = projection.runs.filter((run) => run.status === "preparing");
3349+
const preparationItems =
3350+
preparingRuns.length === 0
3351+
? []
3352+
: (yield* loadProjectionForCommand(command, ["turnItems"], {
3353+
turnItemTypes: ["command_execution"],
3354+
turnItemRunIds: preparingRuns.map((run) => run.id),
3355+
})).turnItems;
33463356
for (const run of projection.runs.filter((candidate) => activeRunIds.has(candidate.id))) {
33473357
yield* emitEvent({
33483358
type: "run.updated",
@@ -3353,6 +3363,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33533363
payload: { ...run, status: "cancelled", queuePosition: null, completedAt: now },
33543364
});
33553365
}
3366+
for (const run of preparingRuns) {
3367+
const preparationItem = preparationItems.find(
3368+
(item) =>
3369+
item.runId === run.id &&
3370+
item.type === "command_execution" &&
3371+
item.input === WORKSPACE_PREPARATION_INPUT &&
3372+
item.status === "running",
3373+
);
3374+
if (preparationItem === undefined) continue;
3375+
yield* emitEvent({
3376+
type: "turn-item.updated",
3377+
threadId: command.threadId,
3378+
runId: run.id,
3379+
nodeId: preparationItem.nodeId ?? undefined,
3380+
providerInstanceId: run.providerInstanceId,
3381+
occurredAt: now,
3382+
payload: {
3383+
...preparationItem,
3384+
status: "cancelled",
3385+
title: "Workspace preparation cancelled",
3386+
completedAt: now,
3387+
updatedAt: now,
3388+
},
3389+
});
3390+
}
33563391
for (const attempt of projection.attempts.filter(
33573392
(candidate) =>
33583393
activeRunIds.has(candidate.runId) &&

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3873,6 +3873,113 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
38733873
}),
38743874
);
38753875

3876+
it.effect("cancels preparing work on archive and keeps it cancelled through replay", () =>
3877+
Effect.gen(function* () {
3878+
const orchestrator = yield* Orchestrator.OrchestratorV2;
3879+
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
3880+
const threadId = ThreadId.make("runtime-layer-archive-preparing-thread");
3881+
yield* orchestrator.dispatch({
3882+
type: "thread.create",
3883+
createdBy: "user",
3884+
creationSource: "web",
3885+
commandId: CommandId.make("archive-preparing-create"),
3886+
threadId,
3887+
projectId: ProjectId.make("archive-preparing-project"),
3888+
title: "Archive preparing work",
3889+
modelSelection,
3890+
runtimeMode: "full-access",
3891+
interactionMode: "default",
3892+
branch: null,
3893+
worktreePath: null,
3894+
});
3895+
yield* orchestrator.dispatch({
3896+
type: "message.dispatch",
3897+
createdBy: "user",
3898+
creationSource: "web",
3899+
commandId: CommandId.make("archive-preparing-message"),
3900+
threadId,
3901+
messageId: MessageId.make("archive-preparing-message"),
3902+
text: "Prepare my workspace.",
3903+
attachments: [],
3904+
modelSelection,
3905+
dispatchMode: { type: "defer_start" },
3906+
});
3907+
const before = yield* orchestrator.getThreadProjection(threadId);
3908+
assert.lengthOf(before.runs, 1);
3909+
assert.isEmpty(before.subagents);
3910+
const run = before.runs[0]!;
3911+
assert.equal(run.status, "preparing");
3912+
const preparation = before.turnItems.find((item) => item.type === "command_execution")!;
3913+
assert.equal(preparation.status, "running");
3914+
assert.isEmpty((yield* orchestrator.getThreadShell(threadId))!.pendingBackgroundTasks);
3915+
3916+
const archive = {
3917+
type: "thread.archive" as const,
3918+
commandId: CommandId.make("archive-preparing-archive"),
3919+
threadId,
3920+
};
3921+
const archivedResult = yield* orchestrator.dispatch(archive);
3922+
assert.equal((yield* orchestrator.dispatch(archive)).sequence, archivedResult.sequence);
3923+
3924+
const assertCancelled = Effect.gen(function* () {
3925+
const projection = yield* orchestrator.getThreadProjection(threadId);
3926+
const cancelledRun = projection.runs.find((candidate) => candidate.id === run.id)!;
3927+
assert.equal(cancelledRun.status, "cancelled");
3928+
assert.isNull(cancelledRun.queuePosition);
3929+
assert.isNotNull(cancelledRun.completedAt);
3930+
assert.equal(projection.attempts[0]?.status, "cancelled");
3931+
assert.isNotNull(projection.attempts[0]?.completedAt);
3932+
assert.equal(projection.nodes[0]?.status, "cancelled");
3933+
assert.isNotNull(projection.nodes[0]?.completedAt);
3934+
const item = projection.turnItems.find((candidate) => candidate.id === preparation.id)!;
3935+
assert.equal(item.status, "cancelled");
3936+
assert.equal(item.title, "Workspace preparation cancelled");
3937+
assert.deepEqual(item.completedAt, cancelledRun.completedAt);
3938+
assert.deepEqual(item.updatedAt, cancelledRun.completedAt);
3939+
assert.isEmpty((yield* orchestrator.getThreadShell(threadId))!.pendingBackgroundTasks);
3940+
assert.equal(yield* orchestrator.resumeQueuedRuns, 0);
3941+
});
3942+
assert.isNotNull((yield* orchestrator.getThreadProjection(threadId)).thread.archivedAt);
3943+
yield* assertCancelled;
3944+
assert.isTrue((yield* maintenance.rebuild).valid);
3945+
yield* assertCancelled;
3946+
3947+
// Preparation may finish after archive. Even reopening must not let its
3948+
// late callbacks revive the cancelled run or its background activity.
3949+
yield* orchestrator.dispatch({
3950+
type: "thread.unarchive",
3951+
commandId: CommandId.make("archive-preparing-unarchive"),
3952+
threadId,
3953+
});
3954+
for (const callback of [
3955+
{ type: "prepared-run.progress", phase: "setup" },
3956+
{ type: "prepared-run.release" },
3957+
{
3958+
type: "prepared-run.fail",
3959+
failure: {
3960+
class: "validation_error",
3961+
message: "Late preparation failure",
3962+
code: null,
3963+
retryable: false,
3964+
},
3965+
},
3966+
] as const) {
3967+
const error = yield* orchestrator
3968+
.dispatch({
3969+
...callback,
3970+
commandId: CommandId.make(`archive-preparing-late:${callback.type}`),
3971+
threadId,
3972+
runId: run.id,
3973+
})
3974+
.pipe(Effect.flip);
3975+
assert.equal(error._tag, "OrchestratorDispatchError");
3976+
}
3977+
yield* assertCancelled;
3978+
assert.isTrue((yield* maintenance.rebuild).valid);
3979+
yield* assertCancelled;
3980+
}),
3981+
);
3982+
38763983
it.effect("cancels queued work when a thread is archived", () =>
38773984
Effect.gen(function* () {
38783985
const orchestrator = yield* Orchestrator.OrchestratorV2;

0 commit comments

Comments
 (0)