@@ -9,7 +9,13 @@ import {
99 RetryOptions ,
1010 RunAnnotations ,
1111} from "@trigger.dev/core/v3" ;
12- import { getMaxDuration , SnapshotId } from "@trigger.dev/core/v3/isomorphic" ;
12+ import {
13+ getMaxDuration ,
14+ RunId ,
15+ runnerIdForAttempt ,
16+ runnerIdForRestore ,
17+ SnapshotId ,
18+ } from "@trigger.dev/core/v3/isomorphic" ;
1319import { placementTag } from "@trigger.dev/core/v3/serverOnly" ;
1420import type {
1521 BackgroundWorker ,
@@ -18,6 +24,7 @@ import type {
1824 RuntimeEnvironmentType ,
1925 WorkerDeploymentStatus ,
2026} from "@trigger.dev/database" ;
27+ import { boundedIn } from "@trigger.dev/database" ;
2128import { z } from "zod" ;
2229import type { BillingCache } from "../billingCache.js" ;
2330import type {
@@ -42,6 +49,7 @@ import type {
4249} from "../types.js" ;
4350import {
4451 getLatestExecutionSnapshotOrFinishedRun ,
52+ type EnhancedExecutionSnapshot ,
4553 type ExecutionSnapshotSystem ,
4654 type PreviousSnapshot ,
4755} from "./executionSnapshotSystem.js" ;
@@ -855,6 +863,12 @@ export class DequeueSystem {
855863 ? undefined
856864 : result . task . retryConfig ;
857865
866+ const previousRunnerId = await this . #previousRunnerId(
867+ prisma ,
868+ snapshot ,
869+ result . run . attemptNumber
870+ ) ;
871+
858872 // Pre-generate snapshot ID so we can construct the result without an extra read
859873 const { id : snapshotId , createdAt : snapshotCreatedAt } =
860874 this . executionSnapshotSystem . nextSnapshotIdentity ( snapshot , runId ) ;
@@ -1059,6 +1073,7 @@ export class DequeueSystem {
10591073 id : lockedTaskRun . projectId ,
10601074 } ,
10611075 placementTags : [ placementTag ( "paid" , isPaying ? "true" : "false" ) ] ,
1076+ ...( previousRunnerId ? { previousRunnerId } : { } ) ,
10621077 } satisfies DequeuedMessage ;
10631078 } ,
10641079 {
@@ -1272,6 +1287,64 @@ export class DequeueSystem {
12721287 return "TEMPLATE_WAIT" ;
12731288 }
12741289
1290+ /**
1291+ * The runner that executed the run's previous attempt, for placing a retry. Only a new attempt
1292+ * after the first gets one: a resume or a continuation keeps its attempt. The name is derived
1293+ * from the dequeue that created that runner, never from a runner id the workload reported, which
1294+ * it can set to anything.
1295+ */
1296+ async #previousRunnerId(
1297+ prisma : PrismaClientOrTransaction ,
1298+ snapshot : EnhancedExecutionSnapshot ,
1299+ attemptNumber : number | null
1300+ ) : Promise < string | undefined > {
1301+ if ( ! attemptNumber || snapshot . executionStatus !== "QUEUED" || snapshot . checkpointId ) {
1302+ return undefined ;
1303+ }
1304+ const route = snapshotRouteFromSnapshot ( snapshot ) ;
1305+ // shortcut: a Redis-only snapshot history has no indexed newest-first read, so those runs carry
1306+ // none; add one to the snapshot store if retries of those runs need placing.
1307+ if ( route . residency === "redis-primary" ) return undefined ;
1308+ const runId = snapshot . runId ;
1309+ // shortcut: only the newest dequeues are considered; a run released this many times without
1310+ // starting carries none.
1311+ const dequeues = await this . $ . runStore . findManyExecutionSnapshots (
1312+ {
1313+ where : { runId, isValid : true , executionStatus : "PENDING_EXECUTING" } ,
1314+ select : { id : true , attemptNumber : true , checkpointId : true } ,
1315+ orderBy : { createdAt : "desc" } ,
1316+ take : 20 ,
1317+ } ,
1318+ prisma ,
1319+ route
1320+ ) ;
1321+ if ( dequeues . length === 0 ) return undefined ;
1322+ // The runner of the newest dequeue that execution started from. A dequeue that was released or
1323+ // whose restore failed never ran, and a retry in place stays on the runner it started on.
1324+ const [ started ] = await this . $ . runStore . findManyExecutionSnapshots (
1325+ {
1326+ where : {
1327+ runId,
1328+ isValid : true ,
1329+ executionStatus : { in : [ "EXECUTING" , "EXECUTING_WITH_WAITPOINTS" ] } ,
1330+ previousSnapshotId : { in : boundedIn ( dequeues . map ( ( dequeue ) => dequeue . id ) ) } ,
1331+ } ,
1332+ select : { previousSnapshotId : true } ,
1333+ orderBy : { createdAt : "desc" } ,
1334+ take : 1 ,
1335+ } ,
1336+ prisma ,
1337+ route
1338+ ) ;
1339+ const dequeue = dequeues . find ( ( candidate ) => candidate . id === started ?. previousSnapshotId ) ;
1340+ if ( ! dequeue ) return undefined ;
1341+ const runFriendlyId = RunId . toFriendlyId ( runId ) ;
1342+ // The supervisor names a cold start's runner for the attempt the dequeue started.
1343+ return dequeue . checkpointId
1344+ ? runnerIdForRestore ( runFriendlyId , dequeue . checkpointId )
1345+ : runnerIdForAttempt ( runFriendlyId , ( dequeue . attemptNumber ?? 0 ) + 1 ) ;
1346+ }
1347+
12751348 async #acknowledgeExpiredFinishedRun( orgId : string , runId : string ) : Promise < void > {
12761349 this . $ . logger . warn (
12771350 "dequeueFromWorkerQueue: snapshot state expired for a finished run, removing from queue" ,
0 commit comments