Skip to content

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

fix(experiments): the bugs found while rebuilding the experiments UI (#16912)

Nikita Kabardin65bbc753c788
diff --git a/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx b/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx
index dcb6262695..3ba99945dc 100644
--- a/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx
+++ b/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { StrictMode, useState } from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import { type VisibilityState } from "@tanstack/react-table";
import { DataTableColumnVisibilityFilter } from "@/src/components/table/data-table-column-visibility-filter";
@@ -163,6 +163,28 @@ describe("DataTableColumnVisibilityFilter", () => {
});
});
+ // The capture used to sit inside the setColumnVisibility updater. An updater
+ // has to be pure, and React re-invokes it under StrictMode — which the app
+ // enables — so every toggle was counted twice. Rendering the harness in
+ // StrictMode is what makes this a guard rather than a restatement of the
+ // test above.
+ it("counts one toggle once under StrictMode", () => {
+ render(
+ <StrictMode>
+ <ColumnVisibilityFilterHarness />
+ </StrictMode>,
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: /columns/i }));
+ fireEvent.click(screen.getByText("Input"));
+
+ const events = h.capture.mock.calls.filter(
+ ([name]) => name === "table:column_visibility_changed",
+ );
+ expect(events).toHaveLength(1);
+ expect(events[0][1].selectedColumns).not.toContain("input");
+ });
+
it("notifies onColumnGroupToggle with the group id, not score names", () => {
render(<GroupedColumnVisibilityHarness />);
diff --git a/web/src/components/table/data-table-column-visibility-filter.tsx b/web/src/components/table/data-table-column-visibility-filter.tsx
index 75487ce37d..da650befed 100644
--- a/web/src/components/table/data-table-column-visibility-filter.tsx
+++ b/web/src/components/table/data-table-column-visibility-filter.tsx
@@ -333,21 +333,21 @@ export function DataTableColumnVisibilityFilter<TData, TValue>({
// calculate target state outside of setState to make it idempotent
const currentValue = columnVisibility[columnId];
const targetValue = !currentValue;
- setColumnVisibility((old: any) => {
- const newColumnVisibility = {
- ...old,
- [columnId]: targetValue,
- };
- const selectedColumns = Object.keys(newColumnVisibility).filter(
- (key) => newColumnVisibility[key],
- );
- capture("table:column_visibility_changed", {
- selectedColumns: selectedColumns,
- tableName,
- isV4,
- });
- return newColumnVisibility;
+ // The event belongs to the click, not to the state updater: an updater
+ // must be pure, and React invokes it twice under StrictMode — which
+ // double-counted every toggle. Same payload, emitted once.
+ const nextVisibility = { ...columnVisibility, [columnId]: targetValue };
+ capture("table:column_visibility_changed", {
+ selectedColumns: Object.keys(nextVisibility).filter(
+ (key) => nextVisibility[key],
+ ),
+ tableName,
+ isV4,
});
+ setColumnVisibility((old: any) => ({
+ ...old,
+ [columnId]: targetValue,
+ }));
},
// eslint disable is because we don't want the posthog capture as deps
// eslint-disable-next-line react-hooks/exhaustive-deps
diff --git a/web/src/components/table/table-view-presets/hooks/useViewData.ts b/web/src/components/table/table-view-presets/hooks/useViewData.ts
index a76c4c48f2..10375420ef 100644
--- a/web/src/components/table/table-view-presets/hooks/useViewData.ts
+++ b/web/src/components/table/table-view-presets/hooks/useViewData.ts
@@ -9,10 +9,16 @@ export const useViewData = ({
projectId: string;
}) => {
const { data: TableViewPresets } =
- api.TableViewPresets.getByTableName.useQuery({
- tableName,
- projectId,
- });
+ api.TableViewPresets.getByTableName.useQuery(
+ {
+ tableName,
+ projectId,
+ },
+ // `projectId` comes from `router.query`, which Next.js populates only
+ // after hydration; unguarded the query fires with `undefined` and the
+ // rejected zod input surfaces as a "Bad Request" toast.
+ { enabled: Boolean(projectId) },
+ );
return {
TableViewPresetsList: TableViewPresets,
diff --git a/web/src/features/experiments/components/table/ExperimentItemsTable.tsx b/web/src/features/experiments/components/table/ExperimentItemsTable.tsx
index cfe12ab5e9..1334e49692 100644
--- a/web/src/features/experiments/components/table/ExperimentItemsTable.tsx
+++ b/web/src/features/experiments/components/table/ExperimentItemsTable.tsx
@@ -346,7 +346,12 @@ export default function ExperimentItemsTable({
const queryFilter = useSidebarFilterState(
experimentItemsFilterConfig,
scoreFilterOptions,
- { stateLocation: "url", loading: isFilterOptionsLoading, isV4: true },
+ {
+ stateLocation: "url",
+ loading: isFilterOptionsLoading,
+ // v4-only surface — drives `isV4` on filters:* analytics.
+ isV4: true,
+ },
);
// Create ref-based wrapper to avoid stale closure when queryFilter updates
diff --git a/web/src/features/experiments/components/table/ExperimentsTable.tsx b/web/src/features/experiments/components/table/ExperimentsTable.tsx
index bb60f5393d..a6d6b86d55 100644
--- a/web/src/features/experiments/components/table/ExperimentsTable.tsx
+++ b/web/src/features/experiments/components/table/ExperimentsTable.tsx
@@ -361,6 +361,7 @@ export default function ExperimentsTable({
loading: isFilterOptionsPending,
stateLocation: "urlAndSessionStorage",
sessionFilterContextId,
+ // v4-only surface — drives `isV4` on filters:* analytics.
isV4: true,
});
diff --git a/web/src/features/experiments/hooks/useExperimentFilterOptions.ts b/web/src/features/experiments/hooks/useExperimentFilterOptions.ts
index ea9decbe28..8a0626be99 100644
--- a/web/src/features/experiments/hooks/useExperimentFilterOptions.ts
+++ b/web/src/features/experiments/hooks/useExperimentFilterOptions.ts
@@ -21,10 +21,18 @@ export function useExperimentFilterOptions({
projectId: string;
oldFilterState: FilterState;
}) {
+ // `projectId` comes from `router.query`, which Next.js populates only after
+ // hydration; unguarded queries fire with `undefined` and the rejected zod
+ // input surfaces as a "Bad Request" toast.
+ const isProjectReady = Boolean(projectId);
+
// Fetch datasets to get ID -> name mapping
- const datasets = api.datasets.allDatasetMeta.useQuery({
- projectId,
- });
+ const datasets = api.datasets.allDatasetMeta.useQuery(
+ {
+ projectId,
+ },
+ { enabled: isProjectReady },
+ );
// Extract start time filters for filter options query
const startTimeFilters = useMemo(() => {
@@ -36,10 +44,14 @@ export function useExperimentFilterOptions({
}, [oldFilterState]);
// Fetch experiment-specific filter options (scores scoped to experiment events)
- const filterOptions = api.experiments.filterOptions.useQuery({
- projectId,
- startTimeFilter: startTimeFilters.length > 0 ? startTimeFilters : undefined,
- });
+ const filterOptions = api.experiments.filterOptions.useQuery(
+ {
+ projectId,
+ startTimeFilter:
+ startTimeFilters.length > 0 ? startTimeFilters : undefined,
+ },
+ { enabled: isProjectReady },
+ );
const usedDatasets = useMemo(
() =>
diff --git a/web/src/features/experiments/hooks/useExperimentItemsFilterOptions.ts b/web/src/features/experiments/hooks/useExperimentItemsFilterOptions.ts
index 2307e23a1c..6cabc7cf64 100644
--- a/web/src/features/experiments/hooks/useExperimentItemsFilterOptions.ts
+++ b/web/src/features/experiments/hooks/useExperimentItemsFilterOptions.ts
@@ -34,7 +34,9 @@ export const useExperimentItemsFilterOptions = ({
const filterOptions = api.experiments.itemsFilterOptions.useQuery(
{ projectId, experimentIds },
{
- enabled: experimentIds.length > 0,
+ // `projectId` arrives with `router.query` after hydration; without it in
+ // the guard the query can fire with `undefined` and zod rejects it.
+ enabled: Boolean(projectId) && experimentIds.length > 0,
staleTime: Infinity,
refetchOnMount: false,
refetchOnWindowFocus: false,
diff --git a/web/src/features/experiments/hooks/useExperimentNames.ts b/web/src/features/experiments/hooks/useExperimentNames.ts
index cd7af2ccb9..189891dbc5 100644
--- a/web/src/features/experiments/hooks/useExperimentNames.ts
+++ b/web/src/features/experiments/hooks/useExperimentNames.ts
@@ -24,7 +24,9 @@ export function useExperimentNames({
{
projectId,
},
- { enabled: hasExperimentsReadAccess },
+ // `projectId` arrives with `router.query` after hydration; without it in
+ // the guard the query can fire with `undefined` and zod rejects it.
+ { enabled: Boolean(projectId) && hasExperimentsReadAccess },
);
const sortedExperimentNames = data?.experimentNames.sort((a, b) =>
diff --git a/web/src/features/experiments/hooks/useExperimentsTableData.ts b/web/src/features/experiments/hooks/useExperimentsTableData.ts
index 44b919294f..c9e1166141 100644
--- a/web/src/features/experiments/hooks/useExperimentsTableData.ts
+++ b/web/src/features/experiments/hooks/useExperimentsTableData.ts
@@ -67,8 +67,14 @@ export function useExperimentsTableData({
],
);
+ // `projectId` is read from `router.query`, which Next.js populates only after
+ // hydration. Without this guard both queries fire with `projectId: undefined`
+ // on a cold load and the rejected zod input surfaces as a "Bad Request" toast.
+ const isProjectReady = Boolean(projectId);
+
// Fetch experiments
const experimentsQuery = api.experiments.all.useQuery(getAllPayload, {
+ enabled: isProjectReady,
refetchOnWindowFocus: true,
});
@@ -88,13 +94,15 @@ export function useExperimentsTableData({
// Fetch metrics
const metricsQuery = api.experiments.metrics.useQuery(metricsPayload!, {
- enabled: experimentsQuery.isSuccess && metricsPayload !== null,
+ enabled:
+ isProjectReady && experimentsQuery.isSuccess && metricsPayload !== null,
refetchOnWindowFocus: false,
staleTime: 0,
});
// Fetch total count
const totalCountQuery = api.experiments.countAll.useQuery(getCountPayload, {
+ enabled: isProjectReady,
refetchOnWindowFocus: true,
});
@@ -103,7 +111,9 @@ export function useExperimentsTableData({
// Memoize joined data to prevent infinite re-renders
// Handle loading, error, and success states
const joinedData = useMemo(() => {
- if (experimentsQuery.isLoading) {
+ // A disabled query is neither loading nor errored, so keep the table in its
+ // loading state until the project id arrives instead of flashing "no rows".
+ if (!isProjectReady || experimentsQuery.isLoading) {
return { status: "loading" as const, rows: undefined };
}
@@ -117,6 +127,7 @@ export function useExperimentsTableData({
metricsQuery.data,
);
}, [
+ isProjectReady,
experimentsQuery.isLoading,
experimentsQuery.isError,
experimentsQuery.data?.data,