Skip to content

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

fix(shared): apply score pagination when offset is 0 (#16405)

Niklas Semmler9d9abfaaefce
diff --git a/packages/shared/src/server/repositories/scores.test.ts b/packages/shared/src/server/repositories/scores.test.ts
new file mode 100644
index 0000000000..50b0c3db38
--- /dev/null
+++ b/packages/shared/src/server/repositories/scores.test.ts
@@ -0,0 +1,85 @@
+import { describe, it, expect, beforeEach, vi } from "vitest";
+
+const mockQueryClickhouse = vi.hoisted(() => vi.fn());
+
+vi.mock("./clickhouse", () => ({
+ queryClickhouse: mockQueryClickhouse,
+ queryClickhouseStream: vi.fn(),
+ queryClickhouseExecRaw: vi.fn(),
+ commandClickhouse: vi.fn(),
+ upsertClickhouse: vi.fn(),
+ parseClickhouseUTCDateTimeFormat: vi.fn(),
+ clickhouseCompliantRandomCharacters: vi.fn(() => "x"),
+ BLOB_EXPORT_PARQUET_CLICKHOUSE_SETTINGS: {},
+}));
+
+// Break the query-options -> server-barrel import cycle that otherwise fails
+// module initialization when the graph is entered from this repository file.
+vi.mock("../queries/clickhouse-sql/query-options", () => ({
+ shouldSkipObservationsFinal: vi.fn().mockResolvedValue(false),
+}));
+
+import {
+ getScoresForTraces,
+ getScoresForSessions,
+ getScoresForExperiments,
+} from "../index";
+
+const PAGINATION_CLAUSE = "limit {limit: Int32} offset {offset: Int32}";
+
+describe("score repository pagination", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockQueryClickhouse.mockResolvedValue([]);
+ });
+
+ const cases = [
+ {
+ name: "getScoresForTraces",
+ run: () =>
+ getScoresForTraces({
+ projectId: "proj-1",
+ traceIds: ["t-1"],
+ limit: 50,
+ offset: 0,
+ }),
+ },
+ {
+ name: "getScoresForSessions",
+ run: () =>
+ getScoresForSessions({
+ projectId: "proj-1",
+ sessionIds: ["s-1"],
+ limit: 50,
+ offset: 0,
+ }),
+ },
+ {
+ name: "getScoresForExperiments",
+ run: () =>
+ getScoresForExperiments({
+ projectId: "proj-1",
+ runIds: ["r-1"],
+ limit: 50,
+ offset: 0,
+ }),
+ },
+ ];
+
+ it.each(cases)(
+ "$name applies LIMIT/OFFSET when offset is 0",
+ async ({ run }) => {
+ await run();
+
+ const { query } = mockQueryClickhouse.mock.calls[0][0];
+ expect(query).toContain(PAGINATION_CLAUSE);
+ },
+ );
+
+ it("omits LIMIT/OFFSET when limit and offset are undefined", async () => {
+ await getScoresForTraces({ projectId: "proj-1", traceIds: ["t-1"] });
+
+ const { query } = mockQueryClickhouse.mock.calls[0][0];
+ expect(query).not.toContain(PAGINATION_CLAUSE);
+ });
+});
diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.ts
index d29c91a8e5..670871d375 100644
--- a/packages/shared/src/server/repositories/scores.ts
+++ b/packages/shared/src/server/repositories/scores.ts
@@ -261,7 +261,7 @@ export const getScoresForSessions = async <
AND s.data_type IN ({dataTypes: Array(String)})
ORDER BY s.event_ts DESC
LIMIT 1 BY s.id, s.project_id
- ${limit && offset ? `limit {limit: Int32} offset {offset: Int32}` : ""}
+ ${limit !== undefined && offset !== undefined ? `limit {limit: Int32} offset {offset: Int32}` : ""}
`;
const rows = await queryClickhouse<ScoreRecordReadType>({
@@ -310,7 +310,7 @@ export const getScoresForExperiments = async <
AND s.data_type IN ({dataTypes: Array(String)})
ORDER BY s.event_ts DESC
LIMIT 1 BY s.id, s.project_id
- ${limit && offset ? `limit {limit: Int32} offset {offset: Int32}` : ""}
+ ${limit !== undefined && offset !== undefined ? `limit {limit: Int32} offset {offset: Int32}` : ""}
`;
const rows = await queryClickhouse<ScoreRecordReadType>({
@@ -529,7 +529,7 @@ const getScoresForTracesInternal = async <
${levelFilter}
ORDER BY s.event_ts DESC
LIMIT 1 BY s.id, s.project_id
- ${limit && offset ? `limit {limit: Int32} offset {offset: Int32}` : ""}
+ ${limit !== undefined && offset !== undefined ? `limit {limit: Int32} offset {offset: Int32}` : ""}
`;
const rows = await queryClickhouse<
diff --git a/web/src/server/api/routers/sessions.ts b/web/src/server/api/routers/sessions.ts
index 80e0f367b7..9a88802800 100644
--- a/web/src/server/api/routers/sessions.ts
+++ b/web/src/server/api/routers/sessions.ts
@@ -426,8 +426,6 @@ export const sessionRouter = createTRPCRouter({
const scores = await getScoresForSessions({
projectId: ctx.session.projectId,
sessionIds: sessions.map((s) => s.session_id),
- limit: 1000,
- offset: 0,
});
const validatedScores = filterAndValidateDbScoreList({
@@ -495,8 +493,6 @@ export const sessionRouter = createTRPCRouter({
const scores = await getScoresForSessions({
projectId: ctx.session.projectId,
sessionIds: sessions.map((s) => s.session_id),
- limit: 1000,
- offset: 0,
});
const validatedScores = filterAndValidateDbScoreList({
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index c132519017..4ce558ee68 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -264,8 +264,6 @@ export const traceRouter = createTRPCRouter({
const traceScores = await getScoresForTraces({
projectId: ctx.session.projectId,
traceIds: res.map((r) => r.id),
- limit: 1000,
- offset: 0,
excludeMetadata: true,
includeHasMetadata: true,
});