Skip to content

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

fix(api): derive dashboard widget minVersion from the deployment write mode (#15565)

Lysander Kieselaae067ce7fdd
diff --git a/web/src/__tests__/server/unit/dashboard-widget-min-version.servertest.ts b/web/src/__tests__/server/unit/dashboard-widget-min-version.servertest.ts
new file mode 100644
index 0000000000..caea5a687d
--- /dev/null
+++ b/web/src/__tests__/server/unit/dashboard-widget-min-version.servertest.ts
@@ -0,0 +1,72 @@
+// The public widget API stamps the internal `minVersion` that decides whether a
+// widget queries the v1 tables or the v2 events_* tables. Only the deployment
+// knows which of the two it can serve, so these cases pin the behavior on a
+// deployment without a v4 write mode — the v3 default (LFE-14581).
+const envMock = vi.hoisted(() => ({}) as Record<string, unknown>);
+
+// Partial mock — real env underneath so the rest of the imported graph keeps
+// working; envMock keeps its object identity so per-test mutations stay visible.
+vi.mock("@/src/env.mjs", async (importOriginal) => {
+ const actual = (await importOriginal()) as { env: Record<string, unknown> };
+ Object.assign(envMock, actual.env);
+ return { env: envMock };
+});
+
+import {
+ normalizePublicDashboardWidgetInput,
+ validatePublicDashboardWidgetInput,
+} from "@/src/features/widgets/server/public-dashboard-widget-service";
+
+const baseInput = {
+ name: "API widget",
+ description: "",
+ view: "observations" as const,
+ dimensions: [],
+ metrics: [{ measure: "count", agg: "count" as const }],
+ filters: [],
+ chartType: "BAR_TIME_SERIES" as const,
+};
+
+describe("public dashboard widget minVersion", () => {
+ describe("legacy write mode, where the events_* tables are empty", () => {
+ beforeEach(() => {
+ envMock.LANGFUSE_MIGRATION_V4_WRITE_MODE = "legacy";
+ });
+
+ it("stamps a v1-expressible widget as v1 so it can render", () => {
+ const normalized = normalizePublicDashboardWidgetInput(baseInput);
+
+ expect(normalized.minVersion).toBe(1);
+ expect(() =>
+ validatePublicDashboardWidgetInput(normalized),
+ ).not.toThrow();
+ });
+
+ it("rejects a widget that genuinely needs v2 instead of persisting it unrenderable", () => {
+ const normalized = normalizePublicDashboardWidgetInput({
+ ...baseInput,
+ metrics: [{ measure: "traceId", agg: "uniq" as const }],
+ });
+
+ expect(() => validatePublicDashboardWidgetInput(normalized)).toThrow(
+ /Measure "traceId" is not available for view "observations" in version "v1"/,
+ );
+ });
+
+ it("caps a stored v2 widget back to v1 so a patch heals it", () => {
+ expect(normalizePublicDashboardWidgetInput(baseInput, 2).minVersion).toBe(
+ 1,
+ );
+ });
+ });
+
+ // dual writes the events_* tables, so v2 is servable there whatever the
+ // preview opt-in says — Monitors ships on exactly that test and its charts
+ // are v2-only. Capping here would break them.
+ it("keeps stamping v2 in dual write mode with the preview opt-in off", () => {
+ envMock.LANGFUSE_MIGRATION_V4_WRITE_MODE = "dual";
+ envMock.LANGFUSE_MIGRATION_V4_ALLOW_PREVIEW_OPT_IN = "false";
+
+ expect(normalizePublicDashboardWidgetInput(baseInput).minVersion).toBe(2);
+ });
+});
diff --git a/web/src/features/widgets/server/public-dashboard-widget-service.ts b/web/src/features/widgets/server/public-dashboard-widget-service.ts
index e5e7d5478f..a17e944998 100644
--- a/web/src/features/widgets/server/public-dashboard-widget-service.ts
+++ b/web/src/features/widgets/server/public-dashboard-widget-service.ts
@@ -26,6 +26,7 @@ import {
getWidgetImportFilterConfig,
partitionStoredUiTableFiltersToView,
} from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";
+import { env } from "@/src/env.mjs";
import {
MAX_PIVOT_TABLE_DIMENSIONS,
MAX_PIVOT_TABLE_METRICS,
@@ -101,9 +102,29 @@ type PublicDashboardWidgetInput = Omit<
"view"
> & { view: DashboardWidgetViewOutputType };
+/**
+ * v2 widgets read the `events_*` tables, which the worker only writes in `dual`
+ * and `events_only` mode. Under `legacy` they stay empty — and on a v3
+ * deployment they do not exist at all — so a v2 query cannot succeed there.
+ *
+ * `LANGFUSE_MIGRATION_V4_ALLOW_PREVIEW_OPT_IN` is deliberately not consulted:
+ * it gates whether users are *offered* the v4 read path, not whether the data
+ * is there. The same write-mode-only test is what ships Monitors, whose charts
+ * are v2-only (`web/src/components/layouts/routes.tsx` → `show`).
+ */
+function deploymentMinWidgetVersion(): number {
+ return env.LANGFUSE_MIGRATION_V4_WRITE_MODE === "legacy" ? 1 : 2;
+}
+
+/**
+ * `requestedMinVersion` is capped to what the deployment can actually query, so
+ * the API never persists a widget that can never render (LFE-14581). Omit it to
+ * take the deployment's own default, as create does. The cap never promotes, so
+ * a legacy v1 widget stays v1 on a v4 deployment.
+ */
export function normalizePublicDashboardWidgetInput(
input: PublicDashboardWidgetInput,
- minVersion = 2,
+ requestedMinVersion?: number,
): NormalizedWidgetInput {
const { mappedFilters, unsupportedFilters } =
partitionStoredUiTableFiltersToView(input.view, input.filters);
@@ -152,6 +173,8 @@ export function normalizePublicDashboardWidgetInput(
});
}
+ const deploymentMinVersion = deploymentMinWidgetVersion();
+
return {
...input,
filters: mappedFilters.map((filter) => ({
@@ -159,7 +182,10 @@ export function normalizePublicDashboardWidgetInput(
column: columnAliases[filter.column] ?? filter.column,
})),
chartConfig: chartConfig.data,
- minVersion,
+ minVersion:
+ requestedMinVersion === undefined
+ ? deploymentMinVersion
+ : Math.min(requestedMinVersion, deploymentMinVersion),
};
}
@@ -344,9 +370,12 @@ export async function updatePublicDashboardWidget(params: {
params.input.chartType !== undefined &&
params.input.chartType !== currentPublic.chartType;
// Keep the stored minVersion (and thus v1/v2 query semantics) unless the
- // caller explicitly changes the view; view changes land on v2 like create.
+ // caller explicitly changes the view; view changes take the deployment
+ // default like create. Either way normalize caps the result, so patching a
+ // widget that was stamped v2 on a deployment that cannot serve v2 heals it.
const mergedView = params.input.view ?? currentPublic.view;
- const minVersion = mergedView === currentPublic.view ? current.minVersion : 2;
+ const requestedMinVersion =
+ mergedView === currentPublic.view ? current.minVersion : undefined;
const input = normalizePublicDashboardWidgetInput(
{
...currentPublic,
@@ -360,7 +389,7 @@ export async function updatePublicDashboardWidget(params: {
? { type: params.input.chartType }
: currentPublic.chartConfig),
},
- minVersion,
+ requestedMinVersion,
);
validatePublicDashboardWidgetInput(input);
const updated = await DashboardService.updateWidget(