01 · Before and afterThe pattern
// claim → side effects → on failure, put the durable row back first
const won = await db.update(runs).set({ status: "running" })
.where(and(eq(runs.id, id), ownedByCaller(), eq(runs.status, "waiting")));
if (!won) return { resumed: false };
try { await cache.save(state); await queue.publish(step); }
catch (e) { await retry(() => revertToWaiting(id)); await cache.restore(prev); throw e; }02 · Run it on your appThe check
For each compare-and-set in your app, does the WHERE clause include tenant/workspace, and what undoes it if the next step fails?
If the honest answer is no or not sure, this is worth a look before it reaches production.