Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore(evals): fall back to bullmq `job.data.timestamp` allowing for successful retries (#6450)

marliessophie66df08e23257
diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.ts
index 2bd28f4315..4bb363c5bf 100644
--- a/packages/shared/src/server/repositories/observations.ts
+++ b/packages/shared/src/server/repositories/observations.ts
@@ -35,6 +35,18 @@ import {
import { env } from "../../env";
import { ClickHouseClientConfigOptions } from "@clickhouse/client";
+/**
+ * Checks if observation exists in clickhouse.
+ *
+ * @param {string} projectId - Project ID for the observation
+ * @param {string} id - ID of the observation
+ * @param {Date} startTime - Timestamp for time-based filtering, uses event payload or job timestamp
+ * @returns {Promise<boolean>} - True if observation exists
+ *
+ * Notes:
+ * β€’ Filters with two days lookback window subject to startTime
+ * β€’ Used for validating observation references before eval job creation
+ */
export const checkObservationExists = async (
projectId: string,
id: string,
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index 6cf1d09cbb..ebac4bf1ff 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -28,6 +28,19 @@ import {
import { env } from "../../env";
import { ClickHouseClientConfigOptions } from "@clickhouse/client";
+/**
+ * Checks if trace exists in clickhouse.
+ *
+ * @param {string} projectId - Project ID for the trace
+ * @param {string} traceId - ID of the trace to check
+ * @param {Date} timestamp - Timestamp for time-based filtering, uses event payload or job timestamp
+ * @param {FilterState} filter - Filter for the trace
+ * @returns {Promise<boolean>} - True if trace exists
+ *
+ * Notes:
+ * β€’ Filters within Β±2 day window
+ * β€’ Used for validating trace references before eval job creation
+ */
export const checkTraceExists = async (
projectId: string,
traceId: string,
diff --git a/worker/src/__tests__/evalService.filtering.test.ts b/worker/src/__tests__/evalService.filtering.test.ts
index 9250a3dbca..2f55380e8e 100644
--- a/worker/src/__tests__/evalService.filtering.test.ts
+++ b/worker/src/__tests__/evalService.filtering.test.ts
@@ -157,6 +157,7 @@ const test = baseTest.extend<{
traceId: traceId1,
...event1,
},
+ jobTimestamp: new Date(),
});
await createEvalJobs({
event: {
@@ -164,6 +165,7 @@ const test = baseTest.extend<{
traceId: traceId2,
...event2,
},
+ jobTimestamp: new Date(),
});
});
},
diff --git a/worker/src/__tests__/evalService.test.ts b/worker/src/__tests__/evalService.test.ts
index ca0b316582..4971309ecb 100644
--- a/worker/src/__tests__/evalService.test.ts
+++ b/worker/src/__tests__/evalService.test.ts
@@ -45,6 +45,7 @@ const openAIServer = new OpenAIServer({
hasActiveKey,
useDefaultResponse: false,
});
+const jobTimestamp = new Date();
beforeAll(openAIServer.setup);
beforeEach(async () => {
@@ -97,7 +98,7 @@ describe("eval service tests", () => {
traceId: traceId,
};
- await createEvalJobs({ event: payload });
+ await createEvalJobs({ event: payload, jobTimestamp });
const jobs = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -177,7 +178,7 @@ describe("eval service tests", () => {
observationId: observationId,
};
- await createEvalJobs({ event: payload });
+ await createEvalJobs({ event: payload, jobTimestamp });
const jobs = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -260,7 +261,7 @@ describe("eval service tests", () => {
};
// This should exit early without an error as there is no trace yet.
- await createEvalJobs({ event: payloadDataset });
+ await createEvalJobs({ event: payloadDataset, jobTimestamp });
const jobsAfterDataset = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -285,7 +286,7 @@ describe("eval service tests", () => {
traceId: traceId,
};
- await createEvalJobs({ event: payloadTrace });
+ await createEvalJobs({ event: payloadTrace, jobTimestamp });
const jobsAfterTrace = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -346,7 +347,7 @@ describe("eval service tests", () => {
};
// This should exit early without an error as there is no trace yet.
- await createEvalJobs({ event: payloadTrace });
+ await createEvalJobs({ event: payloadTrace, jobTimestamp });
const jobsAfterDataset = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -374,7 +375,7 @@ describe("eval service tests", () => {
datasetItemId,
};
- await createEvalJobs({ event: payloadDataset });
+ await createEvalJobs({ event: payloadDataset, jobTimestamp });
const jobsAfterTrace = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -415,7 +416,7 @@ describe("eval service tests", () => {
traceId: traceId,
};
- await createEvalJobs({ event: payload });
+ await createEvalJobs({ event: payload, jobTimestamp });
const jobs = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -469,8 +470,8 @@ describe("eval service tests", () => {
traceId: traceId,
};
- await createEvalJobs({ event: payload });
- await createEvalJobs({ event: payload }); // calling it twice to check it is only generated once
+ await createEvalJobs({ event: payload, jobTimestamp });
+ await createEvalJobs({ event: payload, jobTimestamp }); // calling it twice to check it is only generated once
const jobs = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -509,7 +510,7 @@ describe("eval service tests", () => {
traceId: traceId,
};
- await createEvalJobs({ event: payload });
+ await createEvalJobs({ event: payload, jobTimestamp });
const jobs = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -555,7 +556,7 @@ describe("eval service tests", () => {
traceId: traceId,
};
- await createEvalJobs({ event: payload });
+ await createEvalJobs({ event: payload, jobTimestamp });
const jobs = await kyselyPrisma.$kysely
.selectFrom("job_executions")
@@ -637,7 +638,7 @@ describe("eval service tests", () => {
traceId: traceId,
};
- await createEvalJobs({ event: payload });
+ await createEvalJobs({ event: payload, jobTimestamp });
// Wait for .5s
await new Promise((resolve) => setTimeout(resolve, 500));
@@ -653,6 +654,7 @@ describe("eval service tests", () => {
await createEvalJobs({
event: payload,
+ jobTimestamp,
}); // calling it twice to check it is only generated once
const jobs = await kyselyPrisma.$kysely
@@ -718,6 +720,7 @@ describe("eval service tests", () => {
await createEvalJobs({
event: payload,
+ jobTimestamp,
enforcedJobTimeScope: "NEW", // the config must contain NEW
});
@@ -771,6 +774,7 @@ describe("eval service tests", () => {
await createEvalJobs({
event: payload,
+ jobTimestamp,
enforcedJobTimeScope: "NEW", // the config must contain NEW
});
@@ -829,6 +833,7 @@ describe("eval service tests", () => {
await createEvalJobs({
event: payload,
+ jobTimestamp,
enforcedJobTimeScope: "EXISTING", // the config must contain NEW
});
@@ -873,6 +878,7 @@ describe("eval service tests", () => {
projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
traceId: traceId,
},
+ jobTimestamp,
});
const jobs = await kyselyPrisma.$kysely
diff --git a/worker/src/ee/evaluation/evalService.ts b/worker/src/ee/evaluation/evalService.ts
index ccc1576ef8..dc8bf3233f 100644
--- a/worker/src/ee/evaluation/evalService.ts
+++ b/worker/src/ee/evaluation/evalService.ts
@@ -66,16 +66,89 @@ const getS3StorageServiceClient = (bucketName: string): StorageService => {
return s3StorageServiceClient;
};
-// this function is used to determine which eval jobs to create for a given trace
-// there might be multiple eval jobs to create for a single trace
+/**
+ * Determines which eval jobs to create for a given event (traces or dataset run items).
+ * There might be multiple eval jobs to create for a single trace.
+ * Supports:
+ * - TraceQueue: Live trace data
+ * - DatasetRunItemUpsert: Live dataset run items
+ * - CreateEvalQueue: Historical batch data (traces or dataset run items)
+ *
+ * @param {Object} params - Function parameters
+ * @param {TraceQueueEventType|DatasetRunItemUpsertEventType|CreateEvalQueueEventType} params.event - Event that triggered job creation
+ * @param {Date} params.jobTimestamp - When the job was created
+ * @param {JobTimeScope} [params.enforcedJobTimeScope] - Optional filter for job configurations ("NEW"|"EXISTING")
+ *
+ * Data Flow Architecture for Evaluation Jobs
+ *
+ * β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+ * β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
+ * β”‚ TraceQueue β”‚ β”‚ DatasetRunItemUpsert β”‚ β”‚ CreateEvalQueue β”‚
+ * β”‚ - Live trace data β”‚ β”‚ - Live dataset run itemβ”‚ β”‚ - Historical batch β”‚
+ * β”‚ - No timestamp in body β”‚ β”‚ - No timestamp in body β”‚ β”‚ - Has timestamp in bodyβ”‚
+ * β”‚ - enforcedTimeScope=NEWβ”‚ β”‚ - enforcedTimeScope=NEWβ”‚ β”‚ - No enforcedTimeScope β”‚
+ * β”‚ - Always linked to β”‚ β”‚ - Always linked to β”‚ β”‚ - Always linked to β”‚
+ * β”‚ traces only β”‚ β”‚ traces & sometimes β”‚ β”‚ traces & sometimes β”‚
+ * β”‚ β”‚ β”‚ to observations β”‚ β”‚ to observations β”‚
+ * β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ * β”‚ β”‚ β”‚
+ * β”‚ β”‚ β”‚
+ * β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ * β”‚
+ * β–Ό
+ * β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+ * β”‚ β”‚
+ * β”‚ createEvalJobs function β”‚
+ * β”‚ ─────────────────────── β”‚
+ * β”‚ β”‚
+ * β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
+ * β”‚ β”‚ β”‚ β”‚
+ * β”‚ β”‚ 1. Fetch & Filter β”‚ β”‚
+ * β”‚ β”‚ - Fetches job configs β”‚ β”‚
+ * β”‚ β”‚ - Filters by time scope β”‚ β”‚
+ * β”‚ β”‚ - Creates evaluation jobs β”‚ β”‚
+ * β”‚ β”‚ β”‚ β”‚
+ * β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
+ * β”‚ β”‚ β”‚
+ * β”‚ β–Ό β”‚
+ * β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
+ * β”‚ β”‚ β”‚ β”‚
+ * β”‚ β”‚ 2. Validation Checks β”‚ β”‚
+ * β”‚ β”‚ β”‚ β”‚
+ * β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚
+ * β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
+ * β”‚ β”‚ β”‚ traceExists │◄───┼── Always run for all events β”‚
+ * β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
+ * β”‚ β”‚ β”‚ β”‚
+ * β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
+ * β”‚ β”‚ β”‚ observationExists │◄───┼── Only run for DatasetRunItemUpsertβ”‚
+ * β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ and CreateEvalQueue if β”‚
+ * β”‚ β”‚ β”‚ observationId is set β”‚
+ * β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
+ * β”‚ β”‚ β”‚
+ * β”‚ β–Ό β”‚
+ * β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
+ * β”‚ β”‚ β”‚ β”‚
+ * β”‚ β”‚ 3. EvaluationExecution β”‚ β”‚
+ * β”‚ β”‚ - Jobs queued with delay β”‚ β”‚
+ * β”‚ β”‚ - Includes job parameters β”‚ β”‚
+ * β”‚ β”‚ β”‚ β”‚
+ * β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
+ * β”‚ β”‚
+ * β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ *
+ * ─────────────────────────────────────────────────────────────────────────────────────────── β”‚
+ */
export const createEvalJobs = async ({
event,
+ jobTimestamp,
enforcedJobTimeScope,
}: {
event:
| TraceQueueEventType
| DatasetRunItemUpsertEventType
| CreateEvalQueueEventType;
+ jobTimestamp: Date;
enforcedJobTimeScope?: JobTimeScope;
}) => {
// Fetch all configs for a given project. Those may be dataset or trace configs.
@@ -124,7 +197,8 @@ export const createEvalJobs = async ({
const traceExists = await checkTraceExists(
event.projectId,
event.traceId,
- "timestamp" in event ? new Date(event.timestamp) : new Date(),
+ // Fallback to jobTimestamp if no payload timestamp is set to allow for successful retry attempts.
+ "timestamp" in event ? new Date(event.timestamp) : new Date(jobTimestamp),
config.target_object === "trace" ? validatedFilter : [],
);
@@ -177,7 +251,10 @@ export const createEvalJobs = async ({
const observationExists = await checkObservationExists(
event.projectId,
observationId,
- "timestamp" in event ? new Date(event.timestamp) : new Date(),
+ // Fallback to jobTimestamp if no payload timestamp is set to allow for successful retry attempts.
+ "timestamp" in event
+ ? new Date(event.timestamp)
+ : new Date(jobTimestamp),
);
if (!observationExists) {
logger.warn(
diff --git a/worker/src/queues/evalQueue.ts b/worker/src/queues/evalQueue.ts
index 77f926e119..00c7c2b332 100644
--- a/worker/src/queues/evalQueue.ts
+++ b/worker/src/queues/evalQueue.ts
@@ -19,6 +19,7 @@ export const evalJobTraceCreatorQueueProcessor = async (
try {
await createEvalJobs({
event: job.data.payload,
+ jobTimestamp: job.data.timestamp,
enforcedJobTimeScope: "NEW", // we must not execute evals which are intended for existing data only.
});
return true;
@@ -38,6 +39,7 @@ export const evalJobDatasetCreatorQueueProcessor = async (
try {
await createEvalJobs({
event: job.data.payload,
+ jobTimestamp: job.data.timestamp,
enforcedJobTimeScope: "NEW", // we must not execute evals which are intended for existing data only.
});
return true;
@@ -57,6 +59,7 @@ export const evalJobCreatorQueueProcessor = async (
try {
await createEvalJobs({
event: job.data.payload,
+ jobTimestamp: job.data.timestamp,
});
return true;
} catch (e) {