Skip to content

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

fix: remove trace tracesession fk (#4946)

Max Deichmann93a0ab6f3a2c
diff --git a/packages/shared/prisma/migrations/20250109083346_drop_trace_tracesession_fk/migration.sql b/packages/shared/prisma/migrations/20250109083346_drop_trace_tracesession_fk/migration.sql
new file mode 100644
index 0000000000..84259b78e4
--- /dev/null
+++ b/packages/shared/prisma/migrations/20250109083346_drop_trace_tracesession_fk/migration.sql
@@ -0,0 +1,2 @@
+-- DropForeignKey
+ALTER TABLE "traces" DROP CONSTRAINT "traces_session_id_project_id_fkey";
diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prisma
index b696aa2fc9..b0bfe51440 100644
--- a/packages/shared/prisma/schema.prisma
+++ b/packages/shared/prisma/schema.prisma
@@ -272,7 +272,6 @@ model TraceSession {
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
bookmarked Boolean @default(false)
public Boolean @default(false)
- traces Trace[]
@@id([id, projectId])
@@index([projectId])
@@ -284,25 +283,24 @@ model TraceSession {
// Update TraceView below when making changes to this model!
model Trace {
- id String @id @default(cuid())
- externalId String? @map("external_id")
- timestamp DateTime @default(now())
+ id String @id @default(cuid())
+ externalId String? @map("external_id")
+ timestamp DateTime @default(now())
name String?
- userId String? @map("user_id")
+ userId String? @map("user_id")
metadata Json?
release String?
version String?
- projectId String @map("project_id")
- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- public Boolean @default(false)
- bookmarked Boolean @default(false)
- tags String[] @default([])
+ projectId String @map("project_id")
+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
+ public Boolean @default(false)
+ bookmarked Boolean @default(false)
+ tags String[] @default([])
input Json?
output Json?
- sessionId String? @map("session_id")
- session TraceSession? @relation(fields: [sessionId, projectId], references: [id, projectId])
- createdAt DateTime @default(now()) @map("created_at")
- updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
+ sessionId String? @map("session_id")
+ createdAt DateTime @default(now()) @map("created_at")
+ updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
@@index([projectId, timestamp])
@@index([sessionId])
diff --git a/web/src/__tests__/async/sessions-ui-table.servertest.ts b/web/src/__tests__/async/sessions-ui-table.servertest.ts
index 8d054a9ee2..af0d8ad419 100644
--- a/web/src/__tests__/async/sessions-ui-table.servertest.ts
+++ b/web/src/__tests__/async/sessions-ui-table.servertest.ts
@@ -166,7 +166,7 @@ describe("trpc.sessions", () => {
);
expect(session1?.trace_tags).toEqual(["doe", "john"]);
expect(session1?.total_observations).toEqual(4);
- expect(session1?.duration).toBe("0");
+
expect(Number(session1?.session_input_cost)).toBeGreaterThan(0);
expect(Number(session1?.session_output_cost)).toBeGreaterThan(0);
expect(Number(session1?.session_total_cost)).toBeGreaterThan(0);
@@ -181,7 +181,7 @@ describe("trpc.sessions", () => {
expect(session2?.user_ids).toEqual(["user3"]);
expect(session2?.trace_tags).toEqual(["doe", "john"]);
expect(session2?.total_observations).toEqual(2);
- expect(session2?.duration).toBe("0");
+
expect(Number(session2?.session_input_cost)).toBeGreaterThan(0);
expect(Number(session2?.session_output_cost)).toBeGreaterThan(0);
expect(Number(session2?.session_total_cost)).toBeGreaterThan(0);
diff --git a/web/src/pages/api/public/sessions/[sessionId].ts b/web/src/pages/api/public/sessions/[sessionId].ts
index 117ff75441..03e04de606 100644
--- a/web/src/pages/api/public/sessions/[sessionId].ts
+++ b/web/src/pages/api/public/sessions/[sessionId].ts
@@ -22,28 +22,9 @@ export default withMiddlewares({
operation: "scores.countAll",
user: null,
pgExecution: async () => {
- const session = await prisma.traceSession.findUnique({
- where: {
- id_projectId: {
- id: sessionId,
- projectId: auth.scope.projectId,
- },
- },
- select: {
- id: true,
- createdAt: true,
- projectId: true,
- traces: true,
- },
- });
-
- if (!session) {
- throw new LangfuseNotFoundError(
- "Session not found within authorized project",
- );
- }
-
- return session;
+ throw new LangfuseNotFoundError(
+ "Session not found within authorized project",
+ );
},
clickhouseExecution: async () => {
const session = await prisma.traceSession.findUnique({
diff --git a/web/src/server/api/routers/comments.ts b/web/src/server/api/routers/comments.ts
index fe0e643968..7458d074d1 100644
--- a/web/src/server/api/routers/comments.ts
+++ b/web/src/server/api/routers/comments.ts
@@ -279,44 +279,10 @@ export const commentsRouter = createTRPCRouter({
operation: "comments.getTraceCommentCountsBySessionId",
user: ctx.session.user ?? undefined,
pgExecution: async () => {
- const session = await ctx.prisma.traceSession.findFirst({
- where: {
- id: input.sessionId,
- projectId: input.projectId,
- },
- include: {
- traces: {
- select: {
- id: true,
- },
- },
- },
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Session not found in project",
});
- if (!session) {
- throw new TRPCError({
- code: "NOT_FOUND",
- message: "Session not found in project",
- });
- }
-
- const allTraceCommentCounts = await ctx.prisma.$queryRaw<
- Array<{ objectId: string; count: bigint }>
- >`
- SELECT object_id as "objectId", COUNT(*) as count
- FROM comments
- WHERE project_id = ${input.projectId}
- AND object_type = 'TRACE'
- GROUP BY object_id
- `;
-
- const traceIds = new Set(
- session.traces.map((t: { id: string }) => t.id),
- );
- return new Map(
- allTraceCommentCounts
- .filter((c) => traceIds.has(c.objectId))
- .map(({ objectId, count }) => [objectId, Number(count)]),
- );
},
clickhouseExecution: async () => {
const clickhouseTraces = await getTracesIdentifierForSession(
diff --git a/web/src/server/api/routers/sessions.ts b/web/src/server/api/routers/sessions.ts
index b4a86a6328..cfafc87603 100644
--- a/web/src/server/api/routers/sessions.ts
+++ b/web/src/server/api/routers/sessions.ts
@@ -456,75 +456,10 @@ export const sessionRouter = createTRPCRouter({
operation: "sessions.byId",
user: ctx.session.user ?? undefined,
pgExecution: async () => {
- const session = await ctx.prisma.traceSession.findFirst({
- where: {
- id: input.sessionId,
- projectId: input.projectId,
- },
- include: {
- traces: {
- orderBy: {
- timestamp: "asc",
- },
- select: {
- id: true,
- userId: true,
- name: true,
- timestamp: true,
- },
- },
- },
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Session not found in project",
});
- if (!session) {
- throw new TRPCError({
- code: "NOT_FOUND",
- message: "Session not found in project",
- });
- }
-
- const totalCostQuery = Prisma.sql`
- SELECT
- SUM(COALESCE(o."calculated_total_cost", 0)) AS "totalCost"
- FROM observations_view o
- JOIN traces t ON t.id = o.trace_id
- WHERE
- t."session_id" = ${input.sessionId}
- AND t."project_id" = ${input.projectId}
- `;
-
- const [scores, costData] = await Promise.all([
- ctx.prisma.score.findMany({
- where: {
- traceId: {
- in: session.traces.map((t) => t.id),
- },
- projectId: input.projectId,
- },
- }),
- // costData
- ctx.prisma.$queryRaw<Array<{ totalCost: number }>>(
- totalCostQuery,
- ),
- ]);
-
- const validatedScores = filterAndValidateDbScoreList(
- scores,
- traceException,
- );
-
- return {
- ...session,
- traces: session.traces.map((t) => ({
- ...t,
- scores: validatedScores.filter((s) => s.traceId === t.id),
- })),
- totalCost: costData[0].totalCost ?? 0,
- users: [
- ...new Set(
- session.traces.map((t) => t.userId).filter((t) => t !== null),
- ),
- ],
- };
},
clickhouseExecution: async () => {
const postgresSession = await ctx.prisma.traceSession.findFirst({