Skip to content

Commit 3275be8

Browse files
committed
fix(server): cancel preparing runs when archiving threads
> 🤖 Generated by GPT-6.1-sol in Codex via T3 Code.
1 parent 13aa882 commit 3275be8

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
@@ -3301,8 +3301,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33013301
);
33023302
const emitEvent = emit(events, command);
33033303
const activeRunIds = new Set(
3304-
projection.runs.filter((run) => run.status === "queued").map((run) => run.id),
3304+
projection.runs
3305+
.filter((run) => run.status === "queued" || run.status === "preparing")
3306+
.map((run) => run.id),
33053307
);
3308+
const preparingRuns = projection.runs.filter((run) => run.status === "preparing");
3309+
const preparationItems =
3310+
preparingRuns.length === 0
3311+
? []
3312+
: (yield* loadProjectionForCommand(command, ["turnItems"], {
3313+
turnItemTypes: ["command_execution"],
3314+
turnItemRunIds: preparingRuns.map((run) => run.id),
3315+
})).turnItems;
33063316
for (const run of projection.runs.filter((candidate) => activeRunIds.has(candidate.id))) {
33073317
yield* emitEvent({
33083318
type: "run.updated",
@@ -3313,6 +3323,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33133323
payload: { ...run, status: "cancelled", queuePosition: null, completedAt: now },
33143324
});
33153325
}
3326+
for (const run of preparingRuns) {
3327+
const preparationItem = preparationItems.find(
3328+
(item) =>
3329+
item.runId === run.id &&
3330+
item.type === "command_execution" &&
3331+
item.input === WORKSPACE_PREPARATION_INPUT &&
3332+
item.status === "running",
3333+
);
3334+
if (preparationItem === undefined) continue;
3335+
yield* emitEvent({
3336+
type: "turn-item.updated",
3337+
threadId: command.threadId,
3338+
runId: run.id,
3339+
nodeId: preparationItem.nodeId ?? undefined,
3340+
providerInstanceId: run.providerInstanceId,
3341+
occurredAt: now,
3342+
payload: {
3343+
...preparationItem,
3344+
status: "cancelled",
3345+
title: "Workspace preparation cancelled",
3346+
completedAt: now,
3347+
updatedAt: now,
3348+
},
3349+
});
3350+
}
33163351
for (const attempt of projection.attempts.filter(
33173352
(candidate) =>
33183353
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
@@ -3580,6 +3580,113 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
35803580
}),
35813581
);
35823582

3583+
it.effect("cancels preparing work on archive and keeps it cancelled through replay", () =>
3584+
Effect.gen(function* () {
3585+
const orchestrator = yield* Orchestrator.OrchestratorV2;
3586+
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
3587+
const threadId = ThreadId.make("runtime-layer-archive-preparing-thread");
3588+
yield* orchestrator.dispatch({
3589+
type: "thread.create",
3590+
createdBy: "user",
3591+
creationSource: "web",
3592+
commandId: CommandId.make("archive-preparing-create"),
3593+
threadId,
3594+
projectId: ProjectId.make("archive-preparing-project"),
3595+
title: "Archive preparing work",
3596+
modelSelection,
3597+
runtimeMode: "full-access",
3598+
interactionMode: "default",
3599+
branch: null,
3600+
worktreePath: null,
3601+
});
3602+
yield* orchestrator.dispatch({
3603+
type: "message.dispatch",
3604+
createdBy: "user",
3605+
creationSource: "web",
3606+
commandId: CommandId.make("archive-preparing-message"),
3607+
threadId,
3608+
messageId: MessageId.make("archive-preparing-message"),
3609+
text: "Prepare my workspace.",
3610+
attachments: [],
3611+
modelSelection,
3612+
dispatchMode: { type: "defer_start" },
3613+
});
3614+
const before = yield* orchestrator.getThreadProjection(threadId);
3615+
assert.lengthOf(before.runs, 1);
3616+
assert.isEmpty(before.subagents);
3617+
const run = before.runs[0]!;
3618+
assert.equal(run.status, "preparing");
3619+
const preparation = before.turnItems.find((item) => item.type === "command_execution")!;
3620+
assert.equal(preparation.status, "running");
3621+
assert.isEmpty((yield* orchestrator.getThreadShell(threadId))!.pendingBackgroundTasks);
3622+
3623+
const archive = {
3624+
type: "thread.archive" as const,
3625+
commandId: CommandId.make("archive-preparing-archive"),
3626+
threadId,
3627+
};
3628+
const archivedResult = yield* orchestrator.dispatch(archive);
3629+
assert.equal((yield* orchestrator.dispatch(archive)).sequence, archivedResult.sequence);
3630+
3631+
const assertCancelled = Effect.gen(function* () {
3632+
const projection = yield* orchestrator.getThreadProjection(threadId);
3633+
const cancelledRun = projection.runs.find((candidate) => candidate.id === run.id)!;
3634+
assert.equal(cancelledRun.status, "cancelled");
3635+
assert.isNull(cancelledRun.queuePosition);
3636+
assert.isNotNull(cancelledRun.completedAt);
3637+
assert.equal(projection.attempts[0]?.status, "cancelled");
3638+
assert.isNotNull(projection.attempts[0]?.completedAt);
3639+
assert.equal(projection.nodes[0]?.status, "cancelled");
3640+
assert.isNotNull(projection.nodes[0]?.completedAt);
3641+
const item = projection.turnItems.find((candidate) => candidate.id === preparation.id)!;
3642+
assert.equal(item.status, "cancelled");
3643+
assert.equal(item.title, "Workspace preparation cancelled");
3644+
assert.deepEqual(item.completedAt, cancelledRun.completedAt);
3645+
assert.deepEqual(item.updatedAt, cancelledRun.completedAt);
3646+
assert.isEmpty((yield* orchestrator.getThreadShell(threadId))!.pendingBackgroundTasks);
3647+
assert.equal(yield* orchestrator.resumeQueuedRuns, 0);
3648+
});
3649+
assert.isNotNull((yield* orchestrator.getThreadProjection(threadId)).thread.archivedAt);
3650+
yield* assertCancelled;
3651+
assert.isTrue((yield* maintenance.rebuild).valid);
3652+
yield* assertCancelled;
3653+
3654+
// Preparation may finish after archive. Even reopening must not let its
3655+
// late callbacks revive the cancelled run or its background activity.
3656+
yield* orchestrator.dispatch({
3657+
type: "thread.unarchive",
3658+
commandId: CommandId.make("archive-preparing-unarchive"),
3659+
threadId,
3660+
});
3661+
for (const callback of [
3662+
{ type: "prepared-run.progress", phase: "setup" },
3663+
{ type: "prepared-run.release" },
3664+
{
3665+
type: "prepared-run.fail",
3666+
failure: {
3667+
class: "validation_error",
3668+
message: "Late preparation failure",
3669+
code: null,
3670+
retryable: false,
3671+
},
3672+
},
3673+
] as const) {
3674+
const error = yield* orchestrator
3675+
.dispatch({
3676+
...callback,
3677+
commandId: CommandId.make(`archive-preparing-late:${callback.type}`),
3678+
threadId,
3679+
runId: run.id,
3680+
})
3681+
.pipe(Effect.flip);
3682+
assert.equal(error._tag, "OrchestratorDispatchError");
3683+
}
3684+
yield* assertCancelled;
3685+
assert.isTrue((yield* maintenance.rebuild).valid);
3686+
yield* assertCancelled;
3687+
}),
3688+
);
3689+
35833690
it.effect("cancels queued work when a thread is archived", () =>
35843691
Effect.gen(function* () {
35853692
const orchestrator = yield* Orchestrator.OrchestratorV2;

0 commit comments

Comments
 (0)