Skip to content

Commit c0e87bf

Browse files
committed
fix(webapp): use prisma writer for read-after-write of triggered run friendlyId
Switch the two read-after-write taskRun lookups (POST /api/v1/sessions and POST /api/v1/sessions/:s/end-and-continue) from $replica back to prisma. Both reads happen immediately after triggering a run on the writer; replica lag would null the result and turn a successful create into a 500, or fall back to leaking the internal cuid in the end-and-continue response.
1 parent 4b15c7d commit c0e87bf

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

apps/webapp/app/routes/api.v1.sessions.$session.end-and-continue.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type EndAndContinueSessionResponseBody,
55
} from "@trigger.dev/core/v3";
66
import { z } from "zod";
7-
import { $replica } from "~/db.server";
7+
import { $replica, prisma } from "~/db.server";
88
import { logger } from "~/services/logger.server";
99
import { swapSessionRun } from "~/services/realtime/sessionRunManager.server";
1010
import { resolveSessionByIdOrExternalId } from "~/services/realtime/sessions.server";
@@ -107,9 +107,12 @@ const { action, loader } = createActionApiRoute(
107107
reason,
108108
});
109109

110-
// The swap stored a TaskRun.id (cuid) in `currentRunId`; surface
111-
// the friendlyId for parity with the rest of the public API.
112-
const run = await $replica.taskRun.findFirst({
110+
// Read-after-write: the swap just triggered (or claimed) the
111+
// run on the writer, so read it from `prisma` rather than
112+
// `$replica`. A replica miss here would silently fall back to
113+
// returning the internal cuid, which the public API contract
114+
// says is a friendlyId.
115+
const run = await prisma.taskRun.findFirst({
113116
where: { id: result.runId },
114117
select: { friendlyId: true },
115118
});

apps/webapp/app/routes/api.v1.sessions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ const { action } = createActionApiRoute(
174174
reason: isCached ? "continuation" : "initial",
175175
});
176176

177-
// The newly triggered run's friendlyId, looked up via Prisma — we
178-
// need the friendly form for the wire response.
179-
const run = await $replica.taskRun.findFirst({
177+
// Read-after-write: the run was just triggered in this request,
178+
// so go to the writer rather than $replica. Replica lag here
179+
// would null this out and turn a successful create into a 500.
180+
const run = await prisma.taskRun.findFirst({
180181
where: { id: ensureResult.runId },
181182
select: { friendlyId: true },
182183
});

0 commit comments

Comments
 (0)