Skip to content

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

fix(api,mcp): Stabilize score config pagination order (#13832)

Peter Chen J.77d95ad0d852
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.ts
index 5f4f0e0db8..f6553b36ea 100644
--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts
+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts
@@ -1675,6 +1675,41 @@ describe("MCP Read Tools", () => {
]),
);
});
+
+ it("should paginate score configs deterministically when createdAt timestamps tie", async () => {
+ const { context, projectId } = await createMcpTestSetup();
+ const sharedCreatedAt = new Date("2100-05-12T00:00:00.000Z");
+ const configIDs = [randomUUID(), randomUUID(), randomUUID()];
+
+ await prisma.scoreConfig.createMany({
+ data: configIDs.map((id, index) => ({
+ id,
+ projectId,
+ name: `mcp-tie-score-config-${index}-${id.slice(0, 8)}`,
+ dataType: ScoreConfigDataType.NUMERIC,
+ minValue: index,
+ maxValue: index + 1,
+ createdAt: sharedCreatedAt,
+ updatedAt: sharedCreatedAt,
+ })),
+ });
+
+ const firstPage = (await handleListScoreConfigs(
+ { limit: 2, page: 1 },
+ context,
+ )) as any;
+ const secondPage = (await handleListScoreConfigs(
+ { limit: 2, page: 2 },
+ context,
+ )) as any;
+
+ const tiedIDs = [...firstPage.data, ...secondPage.data]
+ .filter((config) => config.id && configIDs.includes(config.id))
+ .map((config) => config.id);
+
+ expect(tiedIDs).toEqual(configIDs.slice().sort());
+ expect(new Set(tiedIDs).size).toBe(configIDs.length);
+ });
});
describe("getScoreConfig tool", () => {
diff --git a/web/src/__tests__/server/score-configs.servertest.ts b/web/src/__tests__/server/score-configs.servertest.ts
index 694f798ea5..05c96a81f6 100644
--- a/web/src/__tests__/server/score-configs.servertest.ts
+++ b/web/src/__tests__/server/score-configs.servertest.ts
@@ -126,6 +126,50 @@ describe("/api/public/score-configs API Endpoint", () => {
});
});
+ it("should paginate score configs deterministically when createdAt timestamps tie", async () => {
+ const configIDs = [v4(), v4(), v4()];
+ const sharedCreatedAt = new Date("2100-05-12T00:00:00.000Z");
+
+ await prisma.scoreConfig.createMany({
+ data: configIDs.map((id, index) => ({
+ id,
+ projectId,
+ name: `tie-config-${index}-${id.slice(0, 8)}`,
+ description: `tie config ${index}`,
+ dataType: ScoreConfigDataType.NUMERIC,
+ minValue: index,
+ maxValue: index + 1,
+ createdAt: sharedCreatedAt,
+ updatedAt: sharedCreatedAt,
+ })),
+ });
+
+ const firstPage = await makeZodVerifiedAPICall(
+ GetScoreConfigsResponse,
+ "GET",
+ "/api/public/score-configs?limit=2&page=1",
+ undefined,
+ auth,
+ );
+ const secondPage = await makeZodVerifiedAPICall(
+ GetScoreConfigsResponse,
+ "GET",
+ "/api/public/score-configs?limit=2&page=2",
+ undefined,
+ auth,
+ );
+
+ expect(firstPage.status).toBe(200);
+ expect(secondPage.status).toBe(200);
+
+ const tiedIDs = [...firstPage.body.data, ...secondPage.body.data]
+ .filter((config) => config.id && configIDs.includes(config.id))
+ .map((config) => config.id);
+
+ expect(tiedIDs).toEqual(configIDs.slice().sort());
+ expect(new Set(tiedIDs).size).toBe(configIDs.length);
+ });
+
it("test invalid config id input", async () => {
const configId = "invalid-config-id";
diff --git a/web/src/__tests__/server/scores-trpc.servertest.ts b/web/src/__tests__/server/scores-trpc.servertest.ts
index 83f44a5ce8..8048cb4450 100644
--- a/web/src/__tests__/server/scores-trpc.servertest.ts
+++ b/web/src/__tests__/server/scores-trpc.servertest.ts
@@ -22,6 +22,7 @@ import type { Session } from "next-auth";
import { prisma } from "@langfuse/shared/src/db";
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
+import { ScoreConfigDataType } from "@langfuse/shared";
import {
createObservation,
createObservationsCh,
@@ -257,4 +258,43 @@ describe("scores trpc", () => {
).toEqual(["trace_level_score"]);
});
});
+
+ describe("scoreConfigs.all", () => {
+ it("should paginate score configs deterministically when createdAt timestamps tie", async () => {
+ const sharedCreatedAt = new Date("2100-05-12T00:00:00.000Z");
+ const configIds = [randomUUID(), randomUUID(), randomUUID()];
+
+ await prisma.scoreConfig.createMany({
+ data: configIds.map((id, index) => ({
+ id,
+ projectId,
+ name: `trpc-tie-config-${index}-${id.slice(0, 8)}`,
+ description: `trpc tie config ${index}`,
+ dataType: ScoreConfigDataType.NUMERIC,
+ minValue: index,
+ maxValue: index + 1,
+ createdAt: sharedCreatedAt,
+ updatedAt: sharedCreatedAt,
+ })),
+ });
+
+ const firstPage = await caller.scoreConfigs.all({
+ projectId,
+ page: 0,
+ limit: 2,
+ });
+ const secondPage = await caller.scoreConfigs.all({
+ projectId,
+ page: 1,
+ limit: 2,
+ });
+
+ const tiedIds = [...firstPage.configs, ...secondPage.configs]
+ .filter((config) => configIds.includes(config.id))
+ .map((config) => config.id);
+
+ expect(tiedIds).toEqual(configIds.slice().sort());
+ expect(new Set(tiedIds).size).toBe(configIds.length);
+ });
+ });
});
diff --git a/web/src/features/public-api/server/score-configs-api-service.ts b/web/src/features/public-api/server/score-configs-api-service.ts
index 8773e9db54..03ef380090 100644
--- a/web/src/features/public-api/server/score-configs-api-service.ts
+++ b/web/src/features/public-api/server/score-configs-api-service.ts
@@ -82,9 +82,7 @@ export const listScoreConfigs = async ({
where: {
projectId,
},
- orderBy: {
- createdAt: "desc",
- },
+ orderBy: [{ createdAt: "desc" }, { id: "asc" }],
take: limit,
skip: (page - 1) * limit,
}),
diff --git a/web/src/server/api/routers/scoreConfigs.ts b/web/src/server/api/routers/scoreConfigs.ts
index af05f6c7b9..a24c4d502a 100644
--- a/web/src/server/api/routers/scoreConfigs.ts
+++ b/web/src/server/api/routers/scoreConfigs.ts
@@ -67,9 +67,7 @@ export const scoreConfigsRouter = createTRPCRouter({
...(input.limit !== undefined && input.page !== undefined
? { take: input.limit, skip: input.page * input.limit }
: undefined),
- orderBy: {
- createdAt: "desc",
- },
+ orderBy: [{ createdAt: "desc" }, { id: "asc" }],
}),
ctx.prisma.scoreConfig.count({
where: {