Skip to content

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

fix(scores): add id tiebreaker to scores table pagination (#18549)

Niklas Semmlerc03900b785e4
diff --git a/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap b/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap
index 73aed6603e..622f2b8e52 100644
--- a/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap
+++ b/packages/shared/src/server/repositories/__snapshots__/scores-selective-seek.golden.test.ts.snap
@@ -139,7 +139,9 @@ FROM
s.name,
s.id
) AS s
-ORDER BY s.timestamp DESC",
+ORDER BY
+ s.timestamp DESC,
+ s.id DESC",
},
]
`;
@@ -244,7 +246,9 @@ FROM
FINAL
WHERE (s.data_type IN ({param1:Array(String)})) AND (project_id = {param2:String}) AND (s.value > {param3:Decimal64(12)})
) AS s
-ORDER BY s.timestamp DESC
+ORDER BY
+ s.timestamp DESC,
+ s.id DESC
SETTINGS do_not_merge_across_partitions_select_final = 1",
},
]
diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.ts
index 94c6651dcb..ff05f6e01f 100644
--- a/packages/shared/src/server/repositories/scores.ts
+++ b/packages/shared/src/server/repositories/scores.ts
@@ -1401,6 +1401,11 @@ const getScoresUiGeneric = async <T>(props: {
);
const scoresFilterRes = scoresFilter.apply();
+ const orderByClause = orderByToClickhouseSql(
+ orderBy ?? null,
+ scoresTableUiColumnDefinitions,
+ );
+
// Only join traces for rows or if there is a trace filter on counts
const performTracesJoin =
props.select === "rows" ||
@@ -1414,7 +1419,7 @@ const getScoresUiGeneric = async <T>(props: {
WHERE s.project_id = {projectId: String}
AND s.data_type IN ({dataTypes: Array(String)})
${scoresFilterRes?.query ? `AND ${scoresFilterRes.query}` : ""}
- ${orderByToClickhouseSql(orderBy ?? null, scoresTableUiColumnDefinitions)}
+ ${props.select === "rows" ? withIdTiebreaker(orderByClause, orderBy) : orderByClause}
${limit !== undefined && offset !== undefined ? `limit {limit: Int32} offset {offset: Int32}` : ""}
`;
@@ -1724,7 +1729,7 @@ const getScoresUiGenericFromEvents = async <T>(props: {
// The trace join, ORDER BY and pagination run outside the dedup subquery.
const pageClause = `
${eventsJoin}
- ${orderByToClickhouseSql(orderBy ?? null, scoresTableUiColumnDefinitionsFromEvents)}
+ ${withIdTiebreaker(orderByToClickhouseSql(orderBy ?? null, scoresTableUiColumnDefinitionsFromEvents), orderBy)}
${limit !== undefined && offset !== undefined ? `limit {limit: Int32} offset {offset: Int32}` : ""}`;
const query =
props.select === "count"
@@ -1809,6 +1814,12 @@ const getScoresUiGenericFromEvents = async <T>(props: {
});
};
+/** Appends s.id in the primary sort direction so LIMIT/OFFSET pages are deterministic on ties */
+const withIdTiebreaker = (orderByClause: string, orderBy: OrderByState) =>
+ orderByClause && orderBy
+ ? `${orderByClause}, s.id ${orderBy.order}`
+ : orderByClause;
+
export const getScoresUiCountFromEvents = async (props: {
projectId: string;
filter: FilterState;
diff --git a/web/src/__tests__/server/scores-trpc.servertest.ts b/web/src/__tests__/server/scores-trpc.servertest.ts
index 23787b0d42..c324ee9504 100644
--- a/web/src/__tests__/server/scores-trpc.servertest.ts
+++ b/web/src/__tests__/server/scores-trpc.servertest.ts
@@ -207,6 +207,35 @@ describe("scores trpc", () => {
}
});
+ it("pages through scores with identical timestamps without duplicates or gaps on both read paths", async () => {
+ const timestamp = new Date();
+ const scores = Array.from({ length: 8 }, () =>
+ createTraceScore({ project_id: projectId, name: "batch", timestamp }),
+ );
+ await createScoresCh(scores);
+
+ for (const procedure of [
+ caller.scores.all,
+ caller.scores.allFromEvents,
+ ]) {
+ const pages = await Promise.all(
+ scores.map((_, page) =>
+ procedure({
+ projectId,
+ filter: [],
+ orderBy: { column: "timestamp", order: "DESC" },
+ page,
+ limit: 1,
+ }),
+ ),
+ );
+ const pagedIds = pages.flatMap((p) => p.scores.map(({ id }) => id));
+
+ expect(pagedIds).toHaveLength(scores.length);
+ expect(new Set(pagedIds)).toEqual(new Set(scores.map(({ id }) => id)));
+ }
+ });
+
it("applies search-bar name matching and repeated numeric bounds to v4 rows and counts", async () => {
await createScoresCh(
[