Skip to content

Commit 6e09223

Browse files
d-csTrigger.dev RepoOps
authored andcommitted
feat(run-engine): read a MemoryDB child's output from its completed waitpoint, and finish the stalled-snapshot job of a finished MemoryDB run
Mono-RevId: ea71517c33e80078f87544b7f28ee0fcd7532de9
1 parent 65a6ca7 commit 6e09223

5 files changed

Lines changed: 461 additions & 27 deletions

File tree

‎internal-packages/run-engine/src/engine/index.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
parseWaitpointId,
3535
RunId,
3636
mintWaitpointIdFor,
37+
resolveShard,
3738
type ShardKey,
3839
} from "@trigger.dev/core/v3/isomorphic";
3940
import {
@@ -109,6 +110,7 @@ import type { SystemResources } from "./systems/systems.js";
109110
import {
110111
type RunStore,
111112
type SnapshotRoute,
113+
MEMORYDB_SHARD_KEY,
112114
PENDING_UNIT_IN_PROGRESS_REASON,
113115
isPendingUnitInProgress,
114116
PostgresRunStore,
@@ -3990,7 +3992,23 @@ export class RunEngine {
39903992
}) {
39913993
const prisma = tx ?? this.prisma;
39923994
return await this.runLock.lock("handleStalledSnapshot", [runId], async () => {
3993-
const latestSnapshot = await getLatestExecutionSnapshot(prisma, runId, this.runStore);
3995+
const latest =
3996+
resolveShard(runId) === MEMORYDB_SHARD_KEY
3997+
? await getLatestExecutionSnapshotOrFinishedRun(prisma, runId, this.runStore, undefined, {
3998+
id: true,
3999+
})
4000+
: {
4001+
snapshot: await getLatestExecutionSnapshot(prisma, runId, this.runStore),
4002+
finishedRun: null,
4003+
};
4004+
if (latest.finishedRun) {
4005+
this.logger.log(
4006+
"RunEngine.#handleStalledSnapshot() run finished and its snapshot state expired, stopping the heartbeat.",
4007+
{ runId, snapshotId }
4008+
);
4009+
return;
4010+
}
4011+
const latestSnapshot = latest.snapshot;
39944012
if (latestSnapshot.id !== snapshotId) {
39954013
this.logger.log(
39964014
"RunEngine.#handleStalledSnapshot() no longer the latest snapshot, stopping the heartbeat.",

‎internal-packages/run-engine/src/engine/systems/completedWaitpointRecordAdapter.test.ts‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// Pure mapping coverage; storage routing and failures use real stores in the integration suite.
22
import { describe, expect, it } from "vitest";
3-
import { generateWaitpointId } from "@trigger.dev/core/v3/isomorphic";
3+
import {
4+
generateFriendlyId,
5+
generateRunOpsId,
6+
generateRunOpsIdV2,
7+
generateWaitpointId,
8+
RunId,
9+
} from "@trigger.dev/core/v3/isomorphic";
10+
import { MEMORYDB_SHARD_KEY } from "@internal/run-store";
411
import type {
512
WaitpointCompletion,
613
WaitpointRecordInput,
@@ -126,6 +133,66 @@ describe("storeRecordToCompletedWaitpointRecord — envelope fidelity", () => {
126133
expect(record.output).toEqual({ inline: '{"type":"BUILT_IN_ERROR"}' });
127134
});
128135

136+
// Catches: a MemoryDB child's RUN success mapped to deriveFromRun, or its stored output changed.
137+
it("keeps a MemoryDB child's stored RUN success output, in either id form", () => {
138+
const childId = generateRunOpsIdV2(MEMORYDB_SHARD_KEY);
139+
const forms = [
140+
{ outputType: "application/json", output: null },
141+
{ outputType: "text/plain", output: { inline: "" } },
142+
{ outputType: "application/json", output: { inline: '{"v":1}' } },
143+
{ outputType: "application/store", output: { inline: `packets/run_${childId}/output.json` } },
144+
];
145+
for (const completedByTaskRunId of [childId, `run_${childId}`]) {
146+
for (const form of forms) {
147+
const record = mapStoreRecord(
148+
storeEntry(
149+
generateWaitpointId("RUN"),
150+
{ type: "RUN", completedByTaskRunId },
151+
storeCompletion(form)
152+
)
153+
);
154+
expect([record.output, record.outputType]).toEqual([form.output, form.outputType]);
155+
}
156+
}
157+
});
158+
159+
// Catches: a RUN success of a cuid, nanoid, generation 1 or generation 2 child that keeps its stored output.
160+
it("keeps a RUN success of every other child id shape on deriveFromRun, also with a null output", () => {
161+
const shapes = {
162+
cuid: RunId.generate().id,
163+
nanoid: generateFriendlyId("run"),
164+
v1: generateRunOpsId(),
165+
v2: generateRunOpsIdV2("a"),
166+
};
167+
for (const [shape, completedByTaskRunId] of Object.entries(shapes)) {
168+
for (const output of [{ inline: '{"v":1}' }, null]) {
169+
const record = mapStoreRecord(
170+
storeEntry(
171+
generateWaitpointId("RUN"),
172+
{ type: "RUN", completedByTaskRunId },
173+
storeCompletion({ output })
174+
)
175+
);
176+
expect([shape, record.output]).toEqual([shape, { deriveFromRun: true }]);
177+
}
178+
}
179+
});
180+
181+
// Catches: a MemoryDB child's RUN error that does not keep its stored error output inline.
182+
it("carries a MemoryDB child's RUN ERROR inline", () => {
183+
const record = mapStoreRecord(
184+
storeEntry(
185+
generateWaitpointId("RUN"),
186+
{ type: "RUN", completedByTaskRunId: generateRunOpsIdV2(MEMORYDB_SHARD_KEY) },
187+
storeCompletion({ outputIsError: true, output: { inline: '{"type":"BUILT_IN_ERROR"}' } })
188+
)
189+
);
190+
expect([record.outputIsError, record.output]).toEqual([
191+
true,
192+
{ inline: '{"type":"BUILT_IN_ERROR"}' },
193+
]);
194+
});
195+
129196
it("preserves timestamps and type-specific metadata", () => {
130197
const id = generateWaitpointId("DATETIME");
131198
const record = mapStoreRecord(

‎internal-packages/run-engine/src/engine/systems/completedWaitpointRecordAdapter.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Resolves completed waitpoint ids to CompletedWaitpointRecords, one per distinct id. Legacy ids
22
// resolve through Postgres, and store-format ids through the waitpoint store.
3-
import { parseWaitpointId } from "@trigger.dev/core/v3/isomorphic";
4-
import type { CompletedWaitpointRecord } from "@internal/run-store";
3+
import { parseWaitpointId, resolveShard } from "@trigger.dev/core/v3/isomorphic";
4+
import { type CompletedWaitpointRecord, MEMORYDB_SHARD_KEY } from "@internal/run-store";
55
import type {
66
WaitpointCompletion,
77
WaitpointRecordInput,
@@ -87,8 +87,14 @@ function storeRecordOutput(
8787
completion: WaitpointCompletion
8888
): CompletedWaitpointRecord["output"] {
8989
// Before the null check: the store can hold a RUN success's output as null because it is derived
90-
// from TaskRun.output. Null must still give deriveFromRun here, unlike the Postgres write side.
91-
if (record.type === "RUN" && !completion.outputIsError && record.completedByTaskRunId) {
90+
// from TaskRun.output. Null must still give deriveFromRun here, unlike the Postgres write side. A
91+
// child born in MemoryDB keeps its stored output, so its parent never reads the child's run record.
92+
if (
93+
record.type === "RUN" &&
94+
!completion.outputIsError &&
95+
record.completedByTaskRunId &&
96+
resolveShard(record.completedByTaskRunId) !== MEMORYDB_SHARD_KEY
97+
) {
9298
return { deriveFromRun: true };
9399
}
94100
const output = completion.output;

0 commit comments

Comments
 (0)