@devries-cameron20 / t3code Mirror · read-only / commits / 189f0f87ad
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 03:00 UTC

fix(server): cancel preparing runs when archiving threads (#20073)

Luís Miguel committed Oct 18, 2026 at 21:04 UTC 189f0f87ad9ef2a9dbaa4f5e5025b1fbf2df091e
2 files changed +143 -1
apps/server/src/orchestration-v2/Orchestrator.ts
+36 -1
@@ -3341,8 +3341,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
3341 );
3342 const emitEvent = emit(events, command);
3343 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),
3347 );
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;
3356 for (const run of projection.runs.filter((candidate) => activeRunIds.has(candidate.id))) {
3357 yield* emitEvent({
3358 type: "run.updated",
@@ -3353,6 +3363,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
3363 payload: { ...run, status: "cancelled", queuePosition: null, completedAt: now },
3364 });
3365 }
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 + }
3391 for (const attempt of projection.attempts.filter(
3392 (candidate) =>
3393 activeRunIds.has(candidate.runId) &&
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+107
@@ -3873,6 +3873,113 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
3873 }),
3874 );
3875
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 +
3983 it.effect("cancels queued work when a thread is archived", () =>
3984 Effect.gen(function* () {
3985 const orchestrator = yield* Orchestrator.OrchestratorV2;