🪢 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(dashboards): let a widget's own environment filter override the dashboard env selector (LFE-14333) (#15262)
Nikita Kabardin9b67f9763186
diff --git a/web/src/features/widgets/components/DashboardWidget.tsx b/web/src/features/widgets/components/DashboardWidget.tsxindex c1b3bde98d..dabd0aa482 100644--- a/web/src/features/widgets/components/DashboardWidget.tsx+++ b/web/src/features/widgets/components/DashboardWidget.tsx@@ -12,7 +12,6 @@ import {type metricAggregations,type views,} from "@langfuse/shared/query";-import { mapLegacyUiTableFilterToView } from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";import { type z } from "zod";import { Chart } from "@/src/features/widgets/chart-library/Chart";import { type FilterState, type OrderByState } from "@langfuse/shared";@@ -54,6 +53,7 @@ import {} from "@/src/components/ui/dropdown-menu";import {formatMetricName,+ mergeWidgetAndDashboardFilters,shouldUseWidgetSSE,sanitizePivotTableDefaultSort,getWidgetMetricPresentation,@@ -238,24 +238,26 @@ export function DashboardWidget({}): { type: chartType };+ const view = (widget.data?.view as z.infer<typeof views>) ?? "traces";++ // A widget's own environment filter overrides the dashboard's global+ // environment selector; other dashboard-global filters still merge in.+ // (LFE-14333 — see mergeWidgetAndDashboardFilters.)+ const mergedFilters = mergeWidgetAndDashboardFilters({+ view,+ widgetFilters: widget.data?.filters ?? [],+ dashboardFilters: filterState,+ });+return {- view: (widget.data?.view as z.infer<typeof views>) ?? "traces",+ view,dimensions: widget.data?.dimensions ?? [],metrics:widget.data?.metrics.map((metric) => ({measure: metric.measure,aggregation: metric.agg as z.infer<typeof metricAggregations>,})) ?? [],- filters: [- ...mapLegacyUiTableFilterToView(- (widget.data?.view as z.infer<typeof views>) ?? "traces",- widget.data?.filters ?? [],- ),- ...mapLegacyUiTableFilterToView(- (widget.data?.view as z.infer<typeof views>) ?? "traces",- filterState,- ),- ],+ filters: mergedFilters,timeDimension: isTimeSeries ? { granularity: "auto" as const } : null,fromTimestamp: fromTimestamp.toISOString(),toTimestamp: toTimestamp.toISOString(),@@ -468,14 +470,23 @@ export function DashboardWidget({// global) translated to the traces/observations table's applicable filters,// plus the widget's time range. Filters the table can't express are dropped// (surfaced as a hint), never errored. The widget-filter merge mirrors the- // query build above (widget.data.filters + dashboard filterState).+ // query build above via mergeWidgetAndDashboardFilters, so the environment+ // override applies here too: a widget with its own environment filter must+ // deep-link to a table scoped to ITS environment, not one carrying both the+ // widget's and the dashboard selector's contradictory environment filters+ // (which the table treats as applicable → empty table). (LFE-14333)+ // buildTableFilterHref maps to view space again internally; that re-map is+ // idempotent for the already-canonical columns this helper returns+ // (isCanonicalViewFilterColumn short-circuits them), so no filter is+ // double-mapped or dropped.const tableView = useMemo(() => {const view = widget.data?.view;if (!view) return undefined;- const mergedFilters: FilterState = [- ...(widget.data?.filters ?? []),- ...filterState,- ];+ const mergedFilters = mergeWidgetAndDashboardFilters({+ view: view as z.infer<typeof views>,+ widgetFilters: widget.data?.filters ?? [],+ dashboardFilters: filterState,+ });return buildTableFilterHref(projectId,view as z.infer<typeof views>,diff --git a/web/src/features/widgets/utils.clienttest.ts b/web/src/features/widgets/utils.clienttest.tsnew file mode 100644index 0000000000..a78e090f32--- /dev/null+++ b/web/src/features/widgets/utils.clienttest.ts@@ -0,0 +1,256 @@+import { type FilterState } from "@langfuse/shared";+import { mapLegacyUiTableFilterToView } from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";+import { mergeWidgetAndDashboardFilters } from "./utils";++/**+ * LFE-14333: on a dashboard, each widget's query is built from the widget's own+ * filters ANDed with the dashboard-injected global filters (the environment+ * selector, which defaults to hiding `langfuse-*` envs, + the dashboard filter+ * bar). Before the fix, a widget that scoped itself to a `langfuse-*` env got+ * both its own env filter AND the dashboard's env filter — an impossible+ * predicate that returned zero rows and rendered blank on the dashboard while+ * showing fine in the edit screen (which uses only the widget's own filter).+ * The fix: a widget's own environment filter wins.+ */+describe("mergeWidgetAndDashboardFilters", () => {+ const envFilter = (value: string[]): FilterState[number] => ({+ type: "stringOptions",+ column: "environment",+ operator: "any of",+ value,+ });++ it("drops the dashboard's global environment filter when the widget has its own (widget env wins)", () => {+ const widgetFilters: FilterState = [envFilter(["langfuse-llm-as-a-judge"])];+ // Dashboard env selector defaults to production/default (hides langfuse-*).+ const dashboardFilters: FilterState = [+ envFilter(["production", "default"]),+ ];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ const envFilters = merged.filter((f) => f.column === "environment");+ // Only the widget's own environment filter survives — no impossible AND.+ expect(envFilters).toHaveLength(1);+ expect(envFilters[0]).toMatchObject({+ column: "environment",+ value: ["langfuse-llm-as-a-judge"],+ });+ });++ it("applies the dashboard's global environment filter when the widget has none (default-hide preserved)", () => {+ const widgetFilters: FilterState = [];+ const dashboardFilters: FilterState = [+ envFilter(["production", "default"]),+ ];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ const envFilters = merged.filter((f) => f.column === "environment");+ expect(envFilters).toHaveLength(1);+ expect(envFilters[0]).toMatchObject({+ column: "environment",+ value: ["production", "default"],+ });+ });++ it("keeps non-environment dashboard-global filters even when the widget's env filter wins", () => {+ const widgetFilters: FilterState = [envFilter(["langfuse-eval"])];+ const dashboardFilters: FilterState = [+ envFilter(["production"]),+ {+ type: "stringOptions",+ column: "name",+ operator: "any of",+ value: ["chat"],+ },+ ];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ // Widget env wins (dashboard env dropped) but the dashboard's non-env+ // filter still merges in.+ expect(merged.filter((f) => f.column === "environment")).toHaveLength(1);+ expect(+ merged.some((f) => f.column === "name" && f.type === "stringOptions"),+ ).toBe(true);+ });++ it("detects a widget's own env filter stored under the legacy 'Environment' label (mapping normalizes it)", () => {+ // Legacy widgets can persist the environment filter under the uiTable label.+ const widgetFilters: FilterState = [+ {+ type: "stringOptions",+ column: "Environment",+ operator: "any of",+ value: ["langfuse-eval"],+ },+ ];+ const dashboardFilters: FilterState = [+ envFilter(["production", "default"]),+ ];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ const envFilters = merged.filter((f) => f.column === "environment");+ // The legacy label is normalized to the canonical `environment` column and+ // wins over the dashboard selector.+ expect(envFilters).toHaveLength(1);+ expect(envFilters[0]).toMatchObject({ value: ["langfuse-eval"] });+ });++ it("leaves both filter sets untouched when neither declares an environment filter", () => {+ const widgetFilters: FilterState = [+ {+ type: "stringOptions",+ column: "name",+ operator: "any of",+ value: ["chat"],+ },+ ];+ const dashboardFilters: FilterState = [+ {+ type: "string",+ column: "userId",+ operator: "=",+ value: "u1",+ },+ ];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ expect(merged).toHaveLength(2);+ expect(merged.filter((f) => f.column === "environment")).toHaveLength(0);+ });++ // The override is column-only and operator-agnostic: presence of ANY+ // environment filter on the widget triggers dropping ALL dashboard-global+ // environment filters. These fixtures lock that contract.+ describe("column-only, operator-agnostic contract", () => {+ it("detects a widget env filter regardless of operator (e.g. 'none of')", () => {+ const widgetFilters: FilterState = [+ {+ type: "stringOptions",+ column: "environment",+ operator: "none of",+ value: ["langfuse-eval"],+ },+ ];+ const dashboardFilters: FilterState = [envFilter(["production"])];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ const envFilters = merged.filter((f) => f.column === "environment");+ expect(envFilters).toHaveLength(1);+ expect(envFilters[0]).toMatchObject({+ operator: "none of",+ value: ["langfuse-eval"],+ });+ });++ it("drops ALL dashboard env filters and keeps ALL widget env filters when either side has multiple", () => {+ const widgetFilters: FilterState = [+ envFilter(["langfuse-a"]),+ envFilter(["langfuse-b"]),+ ];+ const dashboardFilters: FilterState = [+ envFilter(["production"]),+ envFilter(["default"]),+ ];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ const envFilters = merged.filter((f) => f.column === "environment");+ // Both of the widget's env filters survive; neither dashboard env filter does.+ expect(envFilters).toHaveLength(2);+ expect(envFilters.map((f) => f.value)).toEqual([+ ["langfuse-a"],+ ["langfuse-b"],+ ]);+ });++ it("still drops the dashboard env filter when widget and dashboard env are identical (no duplicate)", () => {+ const widgetFilters: FilterState = [envFilter(["production"])];+ const dashboardFilters: FilterState = [envFilter(["production"])];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });++ const envFilters = merged.filter((f) => f.column === "environment");+ // Widget wins → exactly one env filter, not a redundant AND of two equal ones.+ expect(envFilters).toHaveLength(1);+ expect(envFilters[0]).toMatchObject({ value: ["production"] });+ });+ });++ // "View as table" deep-link safety (LFE-14333 nit): DashboardWidget feeds the+ // helper's output into buildTableFilterHref, which maps to view space AGAIN.+ // That re-map must be a no-op on the helper's already-view-space output, or+ // the generated href would double-map / drop filters. Proven here directly.+ it("returns view-space filters that are idempotent under a second mapLegacyUiTableFilterToView (buildTableFilterHref re-map is safe)", () => {+ const widgetFilters: FilterState = [+ // Legacy label on the widget side to force a real first-pass remap.+ {+ type: "stringOptions",+ column: "Environment",+ operator: "any of",+ value: ["langfuse-eval"],+ },+ ];+ const dashboardFilters: FilterState = [+ envFilter(["production"]),+ {+ type: "stringOptions",+ column: "name",+ operator: "any of",+ value: ["chat"],+ },+ ];++ const merged = mergeWidgetAndDashboardFilters({+ view: "traces",+ widgetFilters,+ dashboardFilters,+ });+ const remapped = mapLegacyUiTableFilterToView("traces", merged);++ // buildTableFilterHref's internal remap changes nothing.+ expect(remapped).toEqual(merged);+ // And the deep-link carries exactly one (the widget's) environment filter.+ expect(remapped.filter((f) => f.column === "environment")).toHaveLength(1);+ expect(remapped.find((f) => f.column === "environment")).toMatchObject({+ value: ["langfuse-eval"],+ });+ });+});diff --git a/web/src/features/widgets/utils.ts b/web/src/features/widgets/utils.tsindex 41f4a685fc..b1b32bbb7d 100644--- a/web/src/features/widgets/utils.ts+++ b/web/src/features/widgets/utils.ts@@ -1,4 +1,5 @@import startCase from "lodash/startCase";+import { type z } from "zod";import { type FilterState } from "@langfuse/shared";import { type DashboardWidgetChartType } from "@langfuse/shared/src/db";import {@@ -11,6 +12,7 @@ import {type MetricFormatterFunction,type MissingBucketValue,} from "@/src/features/widgets/chart-library/chart-props";+import { mapLegacyUiTableFilterToView } from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";// Shared widget chart configuration typesexport type WidgetChartConfig = {@@ -60,6 +62,50 @@ export function sanitizePivotTableDefaultSort(return validDimensionSort || validMetricSort ? defaultSort : undefined;}+/**+ * Merges a widget's own filters with the dashboard-injected global filters (the+ * dashboard's environment selector + its filter bar) for the dashboard query.+ * Both sets are ANDed together in the query.+ *+ * A widget's own environment filter WINS: when the widget declares its own+ * environment filter, the dashboard's global environment filter is dropped for+ * that widget. Otherwise the two AND together into an impossible predicate+ * (e.g. `environment IN ("langfuse-llm-as-a-judge") AND environment IN+ * ("production", "default")`) that returns zero rows, so the widget renders+ * blank on the dashboard while showing fine in the edit screen (which applies+ * only the widget's own filter). Widgets WITHOUT their own environment filter+ * still receive the dashboard's global environment filter, preserving the+ * default-hide-`langfuse-*` behavior. The override is scoped to the+ * `environment` column only; every other dashboard-global filter still merges+ * as before. (LFE-14333; keeps LFE-7448 intact.)+ */+export function mergeWidgetAndDashboardFilters({+ view,+ widgetFilters,+ dashboardFilters,+}: {+ view: z.infer<typeof views>;+ widgetFilters: FilterState;+ dashboardFilters: FilterState;+}): FilterState {+ const mappedWidgetFilters = mapLegacyUiTableFilterToView(view, widgetFilters);+ const mappedDashboardFilters = mapLegacyUiTableFilterToView(+ view,+ dashboardFilters,+ );+ const widgetHasEnvironmentFilter = mappedWidgetFilters.some(+ (filter) => filter.column === "environment",+ );+ return [+ ...mappedWidgetFilters,+ ...(widgetHasEnvironmentFilter+ ? mappedDashboardFilters.filter(+ (filter) => filter.column !== "environment",+ )+ : mappedDashboardFilters),+ ];+}+/*** Formats a metric name for display, handling special cases like count_count -> Count*/