🪢 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(events): restore trace-level score filtering in v4 (LFE-10596) (#14675)
Nikita Kabardin4ce1cef206ec
diff --git a/web/src/__tests__/server/repositories/event-repository.servertest.ts b/web/src/__tests__/server/repositories/event-repository.servertest.tsindex e3b135a298..4b26e3bc2e 100644--- a/web/src/__tests__/server/repositories/event-repository.servertest.ts+++ b/web/src/__tests__/server/repositories/event-repository.servertest.ts@@ -3252,4 +3252,184 @@ describe("Clickhouse Events Repository Test", () => {expect(result.version).toBeUndefined();});});++ // LFE-10596: trace-level score filtering returned empty in v4. The events+ // table splits scores into observation-scoped (`scores_avg`, joined on+ // span_id) and trace-scoped (`trace_scores_avg`, joined on trace_id). A+ // trace-level score (observation_id NULL) can only match the trace-scoped+ // column, so its NAME must be offered under `trace_scores_avg` only — never+ // under `scores_avg`, where a filter on it can never match.+ maybe("LFE-10596 trace-level score filtering", () => {+ it("trace-level score matches trace_scores_avg but not scores_avg", async () => {+ const uniqueProjectId = randomUUID();+ const traceId = randomUUID();+ const spanId = randomUUID();++ await createEventsCh([+ createEvent({+ id: spanId,+ span_id: spanId,+ project_id: uniqueProjectId,+ trace_id: traceId,+ type: "SPAN",+ name: "Help Assistant",+ }),+ ]);++ await createScoresCh([+ createTraceScore({+ project_id: uniqueProjectId,+ trace_id: traceId,+ observation_id: null,+ name: "CSAT",+ value: 1,+ data_type: "NUMERIC",+ }),+ ]);++ const scoresAvgFilter: FilterCondition = {+ type: "numberObject",+ column: "scores_avg",+ operator: "=",+ key: "CSAT",+ value: 1,+ };+ const traceScoresAvgFilter: FilterCondition = {+ type: "numberObject",+ column: "trace_scores_avg",+ operator: "=",+ key: "CSAT",+ value: 1,+ };++ const [noFilterCount, scoresAvgCount, traceScoresAvgCount] =+ await Promise.all([+ getObservationsCountFromEventsTable({+ projectId: uniqueProjectId,+ filter: [],+ }),+ getObservationsCountFromEventsTable({+ projectId: uniqueProjectId,+ filter: [scoresAvgFilter],+ }),+ getObservationsCountFromEventsTable({+ projectId: uniqueProjectId,+ filter: [traceScoresAvgFilter],+ }),+ ]);++ expect(noFilterCount).toBe(1);+ expect(traceScoresAvgCount).toBe(1);+ expect(scoresAvgCount).toBe(0);+ });++ it("offers a trace-level numeric score under trace_scores_avg, not scores_avg", async () => {+ const uniqueProjectId = randomUUID();+ const traceId = randomUUID();+ const spanId = randomUUID();+ const observationId = randomUUID();+ const traceScoreName = `csat-${randomUUID()}`;+ const observationScoreName = `latency-rating-${randomUUID()}`;++ await createEventsCh([+ createEvent({+ id: spanId,+ span_id: spanId,+ project_id: uniqueProjectId,+ trace_id: traceId,+ type: "SPAN",+ name: "Help Assistant",+ }),+ ]);++ await createScoresCh([+ // Trace-level score (observation_id NULL) -> trace_scores_avg only.+ createTraceScore({+ project_id: uniqueProjectId,+ trace_id: traceId,+ observation_id: null,+ name: traceScoreName,+ value: 1,+ data_type: "NUMERIC",+ }),+ // Observation-level score -> scores_avg only.+ createTraceScore({+ project_id: uniqueProjectId,+ trace_id: traceId,+ observation_id: observationId,+ name: observationScoreName,+ value: 1,+ data_type: "NUMERIC",+ }),+ ]);++ const options = await getEventFilterOptions({+ projectId: uniqueProjectId,+ });++ expect(options.trace_scores_avg).toContain(traceScoreName);+ expect(options.scores_avg).not.toContain(traceScoreName);++ expect(options.scores_avg).toContain(observationScoreName);+ expect(options.trace_scores_avg).not.toContain(observationScoreName);+ });++ it("offers a trace-level categorical score under trace_score_categories, not score_categories", async () => {+ const uniqueProjectId = randomUUID();+ const traceId = randomUUID();+ const spanId = randomUUID();+ const observationId = randomUUID();+ const traceCategoryName = `hallucination-check-${randomUUID()}`;+ const observationCategoryName = `answer-relevancy-${randomUUID()}`;++ await createEventsCh([+ createEvent({+ id: spanId,+ span_id: spanId,+ project_id: uniqueProjectId,+ trace_id: traceId,+ type: "SPAN",+ name: "Help Assistant",+ }),+ ]);++ await createScoresCh([+ // Trace-level categorical (observation_id NULL) -> trace_score_categories only.+ createTraceScore({+ project_id: uniqueProjectId,+ trace_id: traceId,+ observation_id: null,+ name: traceCategoryName,+ value: 0,+ string_value: "faithful",+ data_type: "CATEGORICAL",+ }),+ // Observation-level categorical -> score_categories only.+ createTraceScore({+ project_id: uniqueProjectId,+ trace_id: traceId,+ observation_id: observationId,+ name: observationCategoryName,+ value: 0,+ string_value: "relevant",+ data_type: "CATEGORICAL",+ }),+ ]);++ const options = await getEventFilterOptions({+ projectId: uniqueProjectId,+ });++ const scoreCategoryLabels = options.score_categories.map((c) => c.label);+ const traceScoreCategoryLabels = options.trace_score_categories.map(+ (c) => c.label,+ );++ expect(traceScoreCategoryLabels).toContain(traceCategoryName);+ expect(scoreCategoryLabels).not.toContain(traceCategoryName);++ expect(scoreCategoryLabels).toContain(observationCategoryName);+ expect(traceScoreCategoryLabels).not.toContain(observationCategoryName);+ });+ });});diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.tsindex e2dfdf2e24..752b61badf 100644--- a/web/src/features/events/server/eventsService.ts+++ b/web/src/features/events/server/eventsService.ts@@ -46,6 +46,21 @@ const TRACE_SCORE_SCOPE_FILTER: FilterCondition[] = [},];+// Observation-level scores: written against a specific observation+// (observation_id set). These are the scores the observation `scores_avg` /+// `score_categories` columns aggregate and filter on (joined by span_id).+// Trace-level scores (observation_id NULL) live under `trace_scores_avg` /+// `trace_score_categories` instead, so they must NOT be offered here — filtering+// a trace-level score name via the observation column can never match (LFE-10596).+const OBSERVATION_SCORE_SCOPE_FILTER: FilterCondition[] = [+ {+ type: "null",+ column: "observationId",+ operator: "is not null",+ value: "",+ },+];+interface GetObservationsListParams {projectId: string;filter: any[];@@ -550,24 +565,32 @@ export async function getEventFilterOptions(requestedColumns.has(column),);const shouldLoadScoresAvg = requestedColumns.has("scores_avg");- const shouldLoadScoreCategories =- requestedColumns.has("score_categories") ||- requestedColumns.has("trace_score_categories");- const shouldLoadTraceScores =- requestedColumns.has("trace_scores_avg") ||- requestedColumns.has("trace_score_categories");+ const shouldLoadScoreCategories = requestedColumns.has("score_categories");+ const shouldLoadTraceScores = requestedColumns.has("trace_scores_avg");+ const shouldLoadTraceScoreCategories = requestedColumns.has(+ "trace_score_categories",+ );+ // Observation-scoped and trace-scoped discovery are kept separate so each+ // score column only offers names its filter/join can actually match.const [numericScoreNames,categoricalScoreNames,traceScoreColumns,+ traceCategoricalScoreColumns,eventFilterOptions,] = await Promise.all([shouldLoadScoresAvg- ? getNumericScoresGroupedByName(projectId, traceTimestampFilters)+ ? getNumericScoresGroupedByName(projectId, [+ ...OBSERVATION_SCORE_SCOPE_FILTER,+ ...traceTimestampFilters,+ ]): Promise.resolve([]),shouldLoadScoreCategories- ? getCategoricalScoresGroupedByName(projectId, traceTimestampFilters)+ ? getCategoricalScoresGroupedByName(projectId, [+ ...OBSERVATION_SCORE_SCOPE_FILTER,+ ...traceTimestampFilters,+ ]): Promise.resolve([]),shouldLoadTraceScores? getScoresGroupedByNameSourceType({@@ -575,6 +598,12 @@ export async function getEventFilterOptions(filter: [...TRACE_SCORE_SCOPE_FILTER, ...traceScoreTimestampFilters],}): Promise.resolve([]),+ shouldLoadTraceScoreCategories+ ? getCategoricalScoresGroupedByName(projectId, [+ ...TRACE_SCORE_SCOPE_FILTER,+ ...traceTimestampFilters,+ ])+ : Promise.resolve([]),eventColumns.length > 0? getEventsFilterOptionsForColumns({projectId,@@ -593,11 +622,6 @@ export async function getEventFilterOptions(.map((score) => score.name),),);- const traceCategoricalScoreNames = new Set(- traceScoreColumns- .filter((score) => score.dataType === "CATEGORICAL")- .map((score) => score.name),- );const eventFilterOptionsByColumn =toEventFilterOptionsByColumn(eventFilterOptions);@@ -606,16 +630,10 @@ export async function getEventFilterOptions(scores_avg: shouldLoadScoresAvg? numericScoreNames.map((score) => score.name): [],- score_categories: requestedColumns.has("score_categories")- ? categoricalScoreNames- : [],- trace_scores_avg: requestedColumns.has("trace_scores_avg")- ? traceNumericScoreNames- : [],- trace_score_categories: requestedColumns.has("trace_score_categories")- ? categoricalScoreNames.filter((score) =>- traceCategoricalScoreNames.has(score.label),- )+ score_categories: shouldLoadScoreCategories ? categoricalScoreNames : [],+ trace_scores_avg: shouldLoadTraceScores ? traceNumericScoreNames : [],+ trace_score_categories: shouldLoadTraceScoreCategories+ ? traceCategoricalScoreColumns: [],};}