import type { Prisma } from "./client.js"; /** The caller selects authorized runs and owns the surrounding transaction and cleanup. */ export async function cancelRunsInTransaction( tx: Pick, runs: ReadonlyArray<{ id: string; taskId: string }>, cancelledAt: Date, ): Promise { if (runs.length === 0) return; const runIds = runs.map((run) => run.id); await tx.run.updateMany({ where: { id: { in: runIds } }, data: { status: "cancelled", completedAt: cancelledAt, leaseOwner: null, leaseExpiresAt: null, }, }); await tx.attempt.updateMany({ where: { runId: { in: runIds }, status: "running" }, data: { status: "cancelled", finishedAt: cancelledAt }, }); await tx.task.updateMany({ where: { id: { in: runs.map((run) => run.taskId) } }, data: { status: "cancelled" }, }); }