🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(scores): bound scored-traces filter-option subquery to the view window (#17257)
Niklas Semmler452a76525b68
diff --git a/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts b/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.tsindex 5961bdcadc..3a75c243d3 100644--- a/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts+++ b/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts@@ -7,6 +7,7 @@ import {eventsTableTraceNameSql,} from "../../../eventsTable";import type { FilterState } from "../../../types";+import { convertDateToClickhouseDateTime } from "../../clickhouse/client";import { eventsTableUiColumnDefinitions } from "../../tableMappings/mapEventsTable";import { FilterList } from "./clickhouse-filter";import { EventsAggQueryBuilder } from "./event-query-builder";@@ -230,7 +231,16 @@ export type EventFilterOptionRow = {displayValue?: string;};-export type EventFilterOptionScope = "scoredTraces";+// scoredTraces restricts events to traces that carry a score. The optional+// time bounds mirror the scores list view's both-sided window on+// scores.timestamp, so offered options match what the windowed view can+// actually display (and the subquery prunes by partition/PK instead of+// scanning all history).+export type EventFilterOptionScope = {+ type: "scoredTraces";+ fromTime?: { operator: ">=" | ">"; value: Date };+ toTime?: { operator: "<=" | "<"; value: Date };+};const EVENTS_FILTER_OPTION_COLUMN_IDENTIFIER_PATTERN = /^[A-Za-z]+$/;@@ -375,10 +385,32 @@ const optionRowsArrayExpression = (column: EventFilterOptionColumn) => {const eventFilterOptionScopeCondition = (scope: EventFilterOptionScope,-): string => {- switch (scope) {- case "scoredTraces":- return "e.trace_id IN (SELECT DISTINCT trace_id FROM scores WHERE project_id = {projectId: String})";+): { condition: string; params: Record<string, unknown> } => {+ switch (scope.type) {+ case "scoredTraces": {+ const clauses = ["project_id = {projectId: String}"];+ const params: Record<string, unknown> = {};+ if (scope.fromTime) {+ clauses.push(+ `timestamp ${scope.fromTime.operator} {scoredTracesFromTime: DateTime64(3, 'UTC')}`,+ );+ params.scoredTracesFromTime = convertDateToClickhouseDateTime(+ scope.fromTime.value,+ );+ }+ if (scope.toTime) {+ clauses.push(+ `timestamp ${scope.toTime.operator} {scoredTracesToTime: DateTime64(3, 'UTC')}`,+ );+ params.scoredTracesToTime = convertDateToClickhouseDateTime(+ scope.toTime.value,+ );+ }+ return {+ condition: `e.trace_id IN (SELECT DISTINCT trace_id FROM scores WHERE ${clauses.join(" AND ")})`,+ params,+ };+ }}};@@ -426,7 +458,8 @@ export const buildEventsFilterOptionColumnQuery = (params: {.sampleRows(sampleRows);if (params.scope) {- queryBuilder.whereRaw(eventFilterOptionScopeCondition(params.scope));+ const scopeCondition = eventFilterOptionScopeCondition(params.scope);+ queryBuilder.whereRaw(scopeCondition.condition, scopeCondition.params);}return queryBuilder.buildWithParams();@@ -466,8 +499,10 @@ export const buildEventsFilterOptionsForColumnsQuery = (params: {aggregatedOptionsBuilder.sampleRows(sampleRows);if (params.scope) {+ const scopeCondition = eventFilterOptionScopeCondition(params.scope);aggregatedOptionsBuilder.whereRaw(- eventFilterOptionScopeCondition(params.scope),+ scopeCondition.condition,+ scopeCondition.params,);}diff --git a/web/src/__tests__/server/event-query-builder.servertest.ts b/web/src/__tests__/server/event-query-builder.servertest.tsindex c9b80eb83c..1277bab234 100644--- a/web/src/__tests__/server/event-query-builder.servertest.ts+++ b/web/src/__tests__/server/event-query-builder.servertest.ts@@ -271,20 +271,26 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {expect(Object.values(built.params)).toContain("quality");});- it("applies the scored traces scope without caller-provided raw SQL", () => {+ it("bounds the scored traces scope by the view's both-sided window", () => {+ const fromTime = new Date("2026-01-01T00:00:00.000Z");+ const toTime = new Date("2026-01-01T00:30:00.000Z");const built = buildEventsFilterOptionColumnQuery({projectId: "test-project",filter: [],column: "traceName",limit: 100,- scope: "scoredTraces",+ scope: {+ type: "scoredTraces",+ fromTime: { operator: ">=", value: fromTime },+ toTime: { operator: "<=", value: toTime },+ },});expect(built).not.toBeNull();if (!built) throw new Error("expected query");expect(built.query).toContain(- "e.trace_id IN (SELECT DISTINCT trace_id FROM scores WHERE project_id = {projectId: String})",+ "e.trace_id IN (SELECT DISTINCT trace_id FROM scores WHERE project_id = {projectId: String} AND timestamp >= {scoredTracesFromTime: DateTime64(3, 'UTC')} AND timestamp <= {scoredTracesToTime: DateTime64(3, 'UTC')})",);expect(built.query).toContain("COALESCE(nullIf(e.trace_name, ''), if((e.parent_span_id = '' OR e.is_app_root = true), nullIf(e.name, ''), NULL))",@@ -293,7 +299,52 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {expect(built.params).toMatchObject({projectId: "test-project",limit: 100,+ scoredTracesFromTime: "2026-01-01 00:00:00.000",+ scoredTracesToTime: "2026-01-01 00:30:00.000",+ });+ });++ it("preserves strict scores timestamp operators", () => {+ const built = buildEventsFilterOptionColumnQuery({+ projectId: "test-project",+ filter: [],+ column: "traceName",+ limit: 100,+ scope: {+ type: "scoredTraces",+ fromTime: {+ operator: ">",+ value: new Date("2026-01-01T00:00:00.000Z"),+ },+ toTime: { operator: "<", value: new Date("2026-01-01T00:30:00.000Z") },+ },});++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain(+ "AND timestamp > {scoredTracesFromTime: DateTime64(3, 'UTC')} AND timestamp < {scoredTracesToTime: DateTime64(3, 'UTC')}",+ );+ });++ it("omits scores timestamp bounds the view did not supply", () => {+ const built = buildEventsFilterOptionColumnQuery({+ projectId: "test-project",+ filter: [],+ column: "traceName",+ limit: 100,+ scope: { type: "scoredTraces" },+ });++ expect(built).not.toBeNull();+ if (!built) throw new Error("expected query");++ expect(built.query).toContain(+ "e.trace_id IN (SELECT DISTINCT trace_id FROM scores WHERE project_id = {projectId: String})",+ );+ expect(built.query).not.toContain("scoredTracesFromTime");+ expect(built.query).not.toContain("scoredTracesToTime");});it("builds a direct grouped query for one scalar filter option column", () => {diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.tsindex 7aba7ba137..8c3291f973 100644--- a/web/src/server/api/routers/scores.ts+++ b/web/src/server/api/routers/scores.ts@@ -381,17 +381,62 @@ export const scoresRouter = createTRPCRouter({);}+ // Bound the scored-traces semi-join by the same window the scores list+ // view applies on scores.timestamp, so the offered options match what the+ // windowed view can actually display. Preserve the caller's operator (not+ // just the instant) so a score exactly on a strict boundary is offered iff+ // the view would show it. Take the tightest bound on each side; on a tie+ // the strict operator wins because it excludes the boundary instant.+ const timestamps = timestampFilter ?? [];+ const lowerBound = timestamps+ .filter((tf) => tf.operator === ">=" || tf.operator === ">")+ .reduce<{ operator: ">=" | ">"; value: Date } | undefined>(+ (tightest, tf) => {+ const candidate = {+ operator: tf.operator as ">=" | ">",+ value: tf.value,+ };+ if (!tightest) return candidate;+ const diff = candidate.value.getTime() - tightest.value.getTime();+ if (diff > 0) return candidate;+ if (diff === 0 && candidate.operator === ">") return candidate;+ return tightest;+ },+ undefined,+ );+ const upperBound = timestamps+ .filter((tf) => tf.operator === "<=" || tf.operator === "<")+ .reduce<{ operator: "<=" | "<"; value: Date } | undefined>(+ (tightest, tf) => {+ const candidate = {+ operator: tf.operator as "<=" | "<",+ value: tf.value,+ };+ if (!tightest) return candidate;+ const diff = candidate.value.getTime() - tightest.value.getTime();+ if (diff < 0) return candidate;+ if (diff === 0 && candidate.operator === "<") return candidate;+ return tightest;+ },+ undefined,+ );+ const scope = {+ type: "scoredTraces" as const,+ fromTime: lowerBound,+ toTime: upperBound,+ };+const [names, tags, traceNames, userIds, stringValues] =await Promise.all([getScoreNames(input.projectId, timestampFilter ?? []),getEventsGroupedByTraceTags(input.projectId, eventsFilter, {- scope: "scoredTraces",+ scope,}),getEventsGroupedByTraceName(input.projectId, eventsFilter, {- scope: "scoredTraces",+ scope,}),getEventsGroupedByUserId(input.projectId, eventsFilter, {- scope: "scoredTraces",+ scope,}),getScoreStringValues(input.projectId, timestampFilter ?? []),]);