Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(batch-actions): snapshot v4 events-table flag for session batch actions (#14201)

Niklas Semmler59110f3677cd
diff --git a/packages/shared/src/features/batchAction/types.ts b/packages/shared/src/features/batchAction/types.ts
index ab134e913c..fa4890bd90 100644
--- a/packages/shared/src/features/batchAction/types.ts
+++ b/packages/shared/src/features/batchAction/types.ts
@@ -35,6 +35,9 @@ export const BatchActionQuerySchema = z.object({
orderBy,
searchQuery: z.string().optional(),
searchType: z.array(TracingSearchType).optional(),
+ // Dispatch-time snapshot of the user's v4 beta flag; routes the sessions
+ // read stream to the events table instead of the legacy traces path.
+ useEventsTable: z.boolean().optional(),
});
export type BatchActionQuery = z.infer<typeof BatchActionQuerySchema>;
diff --git a/web/src/features/table/server/createBatchActionJob.ts b/web/src/features/table/server/createBatchActionJob.ts
index a51125d4b4..89b7b063f9 100644
--- a/web/src/features/table/server/createBatchActionJob.ts
+++ b/web/src/features/table/server/createBatchActionJob.ts
@@ -28,6 +28,7 @@ type CreateBatchActionJob = {
session: {
user: {
id: string;
+ v4BetaEnabled?: boolean | null;
};
orgId: string;
orgRole: Role;
@@ -56,6 +57,13 @@ export const createBatchActionJob = async ({
tableName,
});
+ // Snapshot the user's v4 beta flag so the worker reads from the same data
+ // source as the UI table; overrides any client-sent value.
+ const queryWithSnapshot: BatchActionQuery = {
+ ...query,
+ useEventsTable: session.user.v4BetaEnabled ?? false,
+ };
+
const batchActionId = generateBatchActionId(projectId, actionId, tableName);
const batchActionQueue = BatchActionQueue.getInstance();
@@ -88,7 +96,7 @@ export const createBatchActionJob = async ({
actionId,
tableName,
cutoffCreatedAt: new Date(),
- query,
+ query: queryWithSnapshot,
targetId: targetId,
type: actionType,
},
diff --git a/worker/src/__tests__/batchAction.test.ts b/worker/src/__tests__/batchAction.test.ts
index 4ea05f5b21..d509855413 100644
--- a/worker/src/__tests__/batchAction.test.ts
+++ b/worker/src/__tests__/batchAction.test.ts
@@ -866,6 +866,85 @@ describe("select all test suite", () => {
});
maybeDescribe("events table batch actions", () => {
+ it("should add sessions to annotation queue from events table when useEventsTable is true", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+
+ const sessionId1 = uuidv4();
+ const sessionId2 = uuidv4();
+
+ await prisma.traceSession.createMany({
+ data: [
+ { id: sessionId1, projectId },
+ { id: sessionId2, projectId },
+ ],
+ });
+
+ const now = Date.now() * 1000; // Events use microseconds
+
+ // Sessions exist only in the events table, so this fails if the
+ // useEventsTable flag is not passed through to the read stream.
+ const events = [
+ createEvent({
+ project_id: projectId,
+ trace_id: uuidv4(),
+ session_id: sessionId1,
+ type: "GENERATION",
+ start_time: now,
+ end_time: now + 1000000,
+ }),
+ createEvent({
+ project_id: projectId,
+ trace_id: uuidv4(),
+ session_id: sessionId2,
+ type: "GENERATION",
+ start_time: now,
+ end_time: now + 1000000,
+ }),
+ ];
+
+ await createEventsCh(events);
+
+ const queueId = uuidv4();
+ await prisma.annotationQueue.create({
+ data: {
+ id: queueId,
+ projectId,
+ name: "test-queue-events",
+ },
+ });
+
+ await handleBatchActionJob({
+ id: uuidv4(),
+ timestamp: new Date(),
+ name: QueueJobs.BatchActionProcessingJob as const,
+ payload: {
+ projectId,
+ actionId: "session-add-to-annotation-queue" as const,
+ tableName: BatchExportTableName.Sessions,
+ cutoffCreatedAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
+ targetId: queueId,
+ query: {
+ filter: [],
+ orderBy: { column: "createdAt", order: "DESC" },
+ useEventsTable: true,
+ },
+ type: BatchActionType.Create,
+ },
+ });
+
+ const queueItems = await prisma.annotationQueueItem.findMany({
+ where: { queueId, projectId },
+ });
+
+ expect(queueItems).toHaveLength(2);
+ const objectIds = queueItems.map((item) => item.objectId);
+ expect(objectIds).toContain(sessionId1);
+ expect(objectIds).toContain(sessionId2);
+ expect(queueItems.every((item) => item.objectType === "SESSION")).toBe(
+ true,
+ );
+ });
+
it("should add observations to dataset from events table with full mapping", async () => {
const { projectId } = await createOrgProjectAndApiKey();
diff --git a/worker/src/features/batchAction/handleBatchActionJob.ts b/worker/src/features/batchAction/handleBatchActionJob.ts
index 58d349dc8d..67eef57461 100644
--- a/worker/src/features/batchAction/handleBatchActionJob.ts
+++ b/worker/src/features/batchAction/handleBatchActionJob.ts
@@ -197,6 +197,7 @@ export const handleBatchActionJob = async (
...streamParams,
orderBy: query.orderBy,
tableName: tableName as BatchTableNames,
+ useEventsTable: query.useEventsTable,
});
// Process stream in database-sized batches