11import { containerTest } from "@internal/testcontainers" ;
22import { trace } from "@internal/tracing" ;
3+ import { Logger } from "@trigger.dev/core/logger" ;
34import {
45 PostgresRunStore ,
56 RedisSnapshotStore ,
7+ SnapshotReadUnavailableError ,
68 TaskRunExecutionSnapshotStore ,
79 type CompletedWaitpointResolver ,
810} from "@internal/run-store" ;
@@ -15,6 +17,7 @@ import {
1517import { setTimeout } from "node:timers/promises" ;
1618import { describe , expect } from "vitest" ;
1719import type { PrismaClient , Waitpoint } from "@trigger.dev/database" ;
20+ import { ExecutionSnapshotNotFoundError } from "../errors.js" ;
1821import { RunEngine } from "../index.js" ;
1922import { getExecutionSnapshotsSince } from "../systems/executionSnapshotSystem.js" ;
2023import { copySnapshotsToReplica , createTestMetricsMeter } from "./helpers/replicaTestHelpers.js" ;
@@ -390,9 +393,8 @@ describe("RunEngine getSnapshotsSince", () => {
390393 }
391394 ) ;
392395
393- containerTest ( "returns null for invalid snapshot ID" , async ( { prisma, redisOptions } ) => {
396+ containerTest ( "rejects an unknown snapshot ID" , async ( { prisma, redisOptions } ) => {
394397 const authenticatedEnvironment = await setupAuthenticatedEnvironment ( prisma , "PRODUCTION" ) ;
395-
396398 const engine = new RunEngine ( {
397399 prisma,
398400 worker : {
@@ -447,21 +449,24 @@ describe("RunEngine getSnapshotsSince", () => {
447449 prisma
448450 ) ;
449451
450- // Query with invalid snapshot ID
451- const result = await engine . getSnapshotsSince ( {
452- runId : run . id ,
453- snapshotId : "invalid-snapshot-id" ,
454- } ) ;
455-
456- // Should return null (caught by getSnapshotsSince error handler)
457- expect ( result ) . toBeNull ( ) ;
452+ // Catches: an unknown since snapshot read as a failed read (null), which the route answers with 500.
453+ const logs = await captureLogs ( ( ) =>
454+ expect (
455+ engine . getSnapshotsSince ( {
456+ runId : run . id ,
457+ snapshotId : "invalid-snapshot-id" ,
458+ } )
459+ ) . rejects . toBeInstanceOf ( ExecutionSnapshotNotFoundError )
460+ ) ;
461+ // Catches: a not-found logged as a failed read, or losing its own error line.
462+ expectNotFoundLoggedSeparately ( logs ) ;
458463 } finally {
459464 await engine . quit ( ) ;
460465 }
461466 } ) ;
462467
463468 containerTest (
464- "returns null when the since snapshot belongs to a different run" ,
469+ "rejects with not-found when the since snapshot belongs to a different run" ,
465470 async ( { prisma, redisOptions } ) => {
466471 const authenticatedEnvironment = await setupAuthenticatedEnvironment ( prisma , "PRODUCTION" ) ;
467472
@@ -559,12 +564,12 @@ describe("RunEngine getSnapshotsSince", () => {
559564 const runASnapshot = runASnapshots [ 0 ] ;
560565
561566 // Poll run B using a snapshot id that belongs to run A.
562- const result = await engine . getSnapshotsSince ( {
563- runId : runB . id ,
564- snapshotId : runASnapshot . id ,
565- } ) ;
566-
567- expect ( result ) . toBeNull ( ) ;
567+ await expect (
568+ engine . getSnapshotsSince ( {
569+ runId : runB . id ,
570+ snapshotId : runASnapshot . id ,
571+ } )
572+ ) . rejects . toBeInstanceOf ( ExecutionSnapshotNotFoundError ) ;
568573 } finally {
569574 await engine . quit ( ) ;
570575 }
@@ -1060,10 +1065,9 @@ describe("RunEngine getSnapshotsSince", () => {
10601065 ) ;
10611066
10621067 containerTest (
1063- "returns null when the snapshot is missing on both replica and primary" ,
1068+ "rejects with not-found when the snapshot is missing on both replica and primary" ,
10641069 async ( { prisma, schemaOnlyPrisma, redisOptions } ) => {
10651070 const { meter, getCounterValue } = createTestMetricsMeter ( ) ;
1066-
10671071 const engine = new RunEngine ( {
10681072 prisma,
10691073 readOnlyPrisma : schemaOnlyPrisma ,
@@ -1099,12 +1103,15 @@ describe("RunEngine getSnapshotsSince", () => {
10991103
11001104 try {
11011105 // runId is never consulted - the since-snapshot lookup throws on the bogus id first.
1102- const result = await engine . getSnapshotsSince ( {
1103- runId : "run_does_not_exist" ,
1104- snapshotId : "snapshot_does_not_exist" ,
1105- } ) ;
1106-
1107- expect ( result ) . toBeNull ( ) ;
1106+ const logs = await captureLogs ( ( ) =>
1107+ expect (
1108+ engine . getSnapshotsSince ( {
1109+ runId : "run_does_not_exist" ,
1110+ snapshotId : "snapshot_does_not_exist" ,
1111+ } )
1112+ ) . rejects . toBeInstanceOf ( ExecutionSnapshotNotFoundError )
1113+ ) ;
1114+ expectNotFoundLoggedSeparately ( logs ) ;
11081115
11091116 // Permanent misses are deliberately NOT counted - the counter only tracks
11101117 // reads actually served by the primary fallback.
@@ -1704,6 +1711,29 @@ describe("RunEngine getSnapshotsSince", () => {
17041711 ) ;
17051712} ) ;
17061713
1714+ // Records the structured lines the engine's Logger emits, the same JSON the log alarms match on.
1715+ async function captureLogs ( fn : ( ) => Promise < unknown > ) {
1716+ const logs : Record < string , unknown > [ ] = [ ] ;
1717+ const original = Logger . onLog ;
1718+ Logger . onLog = ( log ) => logs . push ( log ) ;
1719+ try {
1720+ await fn ( ) ;
1721+ } finally {
1722+ Logger . onLog = original ;
1723+ }
1724+ return logs ;
1725+ }
1726+
1727+ function expectNotFoundLoggedSeparately ( logs : Record < string , unknown > [ ] ) {
1728+ expect ( logs . filter ( ( log ) => log . message === "Failed to getSnapshotsSince" ) ) . toEqual ( [ ] ) ;
1729+ expect ( logs ) . toContainEqual (
1730+ expect . objectContaining ( {
1731+ message : "getSnapshotsSince: since snapshot not found" ,
1732+ level : "error" ,
1733+ } )
1734+ ) ;
1735+ }
1736+
17071737// Counts the checkpoint lookups a since read makes through the run store, and records the client
17081738// each one used.
17091739class CheckpointLookupCountingStore extends PostgresRunStore {
@@ -2036,4 +2066,43 @@ describe("getExecutionSnapshotsSince checkpoint lookups for a MemoryDB-only run"
20362066 }
20372067 }
20382068 ) ;
2069+
2070+ // Catches: an unknown since snapshot on a MemoryDB-only run read as a failed read (a 503 and the
2071+ // failed-read alarm), or a gapped history read as not-found.
2072+ containerTest (
2073+ "an unknown since snapshot is not-found while the run's history is whole, and fails closed once gapped" ,
2074+ async ( { prisma, redisOptions } ) => {
2075+ const seeded = await seed ( prisma , redisOptions ) ;
2076+ try {
2077+ const reader = new TaskRunExecutionSnapshotStore ( seeded . delegate , {
2078+ store : seeded . store ,
2079+ mode : "redis-only" ,
2080+ logicalRunStoreRoute : "logical:1" ,
2081+ resolveCompletedWaitpoints : resolverReturning ( ( ) => false ) ,
2082+ } ) ;
2083+ const since = ( snapshotId : string ) =>
2084+ getExecutionSnapshotsSince (
2085+ prisma ,
2086+ seeded . runId ,
2087+ snapshotId ,
2088+ reader ,
2089+ undefined ,
2090+ seeded . env . id ,
2091+ seeded . env . organizationId ,
2092+ ( ) => true
2093+ ) ;
2094+
2095+ await expect ( since ( "snapshot_doesnotexist" ) ) . rejects . toBeInstanceOf (
2096+ ExecutionSnapshotNotFoundError
2097+ ) ;
2098+
2099+ await seeded . store . markGaps ( seeded . runId ) ;
2100+ await expect ( since ( "snapshot_doesnotexist" ) ) . rejects . toBeInstanceOf (
2101+ SnapshotReadUnavailableError
2102+ ) ;
2103+ } finally {
2104+ await seeded . store . quit ( ) ;
2105+ }
2106+ }
2107+ ) ;
20392108} ) ;
0 commit comments