🪢 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(web): keep filtered traces paging past the first page (#17192)
Nikita Kabardin20e68fd5d04d
diff --git a/web/src/__tests__/server/traces-ui-table.servertest.ts b/web/src/__tests__/server/traces-ui-table.servertest.tsindex 9d72ce2791..6685f70f22 100644--- a/web/src/__tests__/server/traces-ui-table.servertest.ts+++ b/web/src/__tests__/server/traces-ui-table.servertest.ts@@ -5,11 +5,12 @@ import {createObservation,createTrace,getTracesTable,+ getTracesTableCount,type TracesTableUiReturnType,type ObservationRecordInsertType,type TraceRecordInsertType,} from "@langfuse/shared/src/server";-import { type FilterState } from "@langfuse/shared";+import { type FilterState, type TracingSearchType } from "@langfuse/shared";describe("Traces table API test", () => {it("should get a correct trace without observation", async () => {@@ -247,4 +248,61 @@ describe("Traces table API test", () => {});});});++ it.each(["ASC", "DESC"] as const)(+ "pages past the first page with a metadata filter and content search (%s)",+ async (order) => {+ const project_id = v4();+ const documentId = `doc-${v4()}`;+ const needle = `needle-${v4()}`;+ const traces = Array.from({ length: 12 }, (_, i) =>+ createTrace({+ id: `trace-${String(i).padStart(2, "0")}-${v4()}`,+ project_id,+ timestamp: Date.now() - i * 1_000,+ metadata: { documentId },+ input: `${needle} payload ${i}`,+ }),+ );+ await createTracesCh(traces);++ const filter: FilterState = [+ {+ column: "metadata",+ type: "stringObject",+ key: "documentId",+ operator: "=",+ value: documentId,+ },+ ];+ const searchType: TracingSearchType[] = ["content"];+ const query = {+ projectId: project_id,+ filter,+ searchQuery: needle,+ searchType,+ orderBy: { column: "timestamp" as const, order },+ limit: 5,+ };++ const [page0, page1, totalCount] = await Promise.all([+ getTracesTable({ ...query, page: 0 }),+ getTracesTable({ ...query, page: 1 }),+ // Count matches the UI: orderBy is always null so the aggregate+ // is not wrapped in a timestamp ORDER BY.+ getTracesTableCount({+ projectId: project_id,+ filter,+ searchQuery: needle,+ searchType: ["content"],+ }),+ ]);++ expect(totalCount).toBe(12);+ expect(page0).toHaveLength(5);+ expect(page1).toHaveLength(5);+ const page0Ids = new Set(page0.map((row) => row.id));+ expect(page1.every((row) => !page0Ids.has(row.id))).toBe(true);+ },+ );});diff --git a/web/src/components/table/data-table-pagination.tsx b/web/src/components/table/data-table-pagination.tsxindex 2dcf1ba7ee..5e0819c91b 100644--- a/web/src/components/table/data-table-pagination.tsx+++ b/web/src/components/table/data-table-pagination.tsx@@ -79,10 +79,14 @@ export function DataTablePagination<TData>({const pageCount = table.getPageCount();const setPageIndex = table.setPageIndex;useEffect(() => {+ // Count queries re-key on a filter/pin change and report pageCount as+ // unknown or 1 while in flight. Snapping back then traps the reader on+ // page 1 even though more rows exist.+ if (isLoading) return;if (currentPage > pageCount && pageCount > 0) {setPageIndex(0);}- }, [currentPage, pageCount, setPageIndex]);+ }, [currentPage, pageCount, setPageIndex, isLoading]);const handlePageNavigation = (newValue: string) => {if (newValue === "") {diff --git a/web/src/components/table/hooks/usePaginationWindowPin.clienttest.ts b/web/src/components/table/hooks/usePaginationWindowPin.clienttest.tsindex eb259a1ba2..a06f056f08 100644--- a/web/src/components/table/hooks/usePaginationWindowPin.clienttest.ts+++ b/web/src/components/table/hooks/usePaginationWindowPin.clienttest.ts@@ -1,11 +1,36 @@import { act, renderHook } from "@testing-library/react";import { afterEach, beforeEach, vi } from "vitest";-import { usePaginationWindowPin } from "./usePaginationWindowPin";+import {+ isLiveTailTimeSort,+ usePaginationWindowPin,+} from "./usePaginationWindowPin";const START = new Date("2026-08-12T12:00:00.000Z");const LIVE = { from: new Date("2026-08-11T12:00:00.000Z"), to: undefined };const NEWEST_ON_PAGE_1 = new Date("2026-08-12T11:59:58.000Z");+const OLDEST_ON_PAGE_1 = new Date("2026-08-11T12:05:00.000Z");++describe("isLiveTailTimeSort", () => {+ it("is true for the default time-DESC sort and false for ASC or other columns", () => {+ expect(isLiveTailTimeSort(null, "timestamp")).toBe(true);+ expect(+ isLiveTailTimeSort({ column: "timestamp", order: "DESC" }, "timestamp"),+ ).toBe(true);+ expect(+ isLiveTailTimeSort({ column: "startTime", order: "DESC" }, "startTime"),+ ).toBe(true);+ expect(+ isLiveTailTimeSort({ column: "timestamp", order: "ASC" }, "timestamp"),+ ).toBe(false);+ expect(+ isLiveTailTimeSort({ column: "startTime", order: "ASC" }, "startTime"),+ ).toBe(false);+ expect(+ isLiveTailTimeSort({ column: "name", order: "DESC" }, "timestamp"),+ ).toBe(false);+ });+});describe("usePaginationWindowPin", () => {beforeEach(() => {@@ -60,4 +85,67 @@ describe("usePaginationWindowPin", () => {const closed = renderHook(() => usePaginationWindowPin(absolute, 2));expect(closed.result.current.range).toBe(absolute);});++ it("does not pin a live window when the sort has no live tail", () => {+ const { result, rerender } = renderHook(+ ({ pageIndex }: { pageIndex: number }) =>+ usePaginationWindowPin(LIVE, pageIndex, { enabled: false }),+ { initialProps: { pageIndex: 0 } },+ );++ act(() => result.current.pinOnLeavingFirstPage(1, OLDEST_ON_PAGE_1));+ rerender({ pageIndex: 1 });++ expect(result.current.range?.to).toBeUndefined();+ expect(result.current.range).toBe(LIVE);+ });++ it("releases an existing pin when the sort stops being a live tail", () => {+ const { result, rerender } = renderHook(+ ({ pageIndex, enabled }: { pageIndex: number; enabled: boolean }) =>+ usePaginationWindowPin(LIVE, pageIndex, { enabled }),+ { initialProps: { pageIndex: 0, enabled: true } },+ );++ act(() => result.current.pinOnLeavingFirstPage(1, NEWEST_ON_PAGE_1));+ rerender({ pageIndex: 1, enabled: true });+ expect(result.current.range?.to).toEqual(NEWEST_ON_PAGE_1);++ rerender({ pageIndex: 1, enabled: false });+ expect(result.current.range?.to).toBeUndefined();+ });++ it("pins to now when a live-tail sort is turned on past page 1", () => {+ const { result, rerender } = renderHook(+ ({ enabled }: { enabled: boolean }) =>+ usePaginationWindowPin(LIVE, 1, { enabled }),+ { initialProps: { enabled: false } },+ );++ expect(result.current.range?.to).toBeUndefined();++ const later = new Date(START.getTime() + 40_000);+ vi.setSystemTime(later);+ rerender({ enabled: true });+ expect(result.current.range?.to).toEqual(later);+ });++ it("does not reuse a stale pin when the live tail is turned back on", () => {+ const { result, rerender } = renderHook(+ ({ enabled }: { enabled: boolean }) =>+ usePaginationWindowPin(LIVE, 1, { enabled }),+ { initialProps: { enabled: true } },+ );++ act(() => result.current.pinOnLeavingFirstPage(1, NEWEST_ON_PAGE_1));+ expect(result.current.range?.to).toEqual(NEWEST_ON_PAGE_1);++ rerender({ enabled: false });+ expect(result.current.range?.to).toBeUndefined();++ const later = new Date(START.getTime() + 40_000);+ vi.setSystemTime(later);+ rerender({ enabled: true });+ expect(result.current.range?.to).toEqual(later);+ });});diff --git a/web/src/components/table/hooks/usePaginationWindowPin.ts b/web/src/components/table/hooks/usePaginationWindowPin.tsindex ecf2755b47..bfd8ebcf83 100644--- a/web/src/components/table/hooks/usePaginationWindowPin.ts+++ b/web/src/components/table/hooks/usePaginationWindowPin.ts@@ -1,6 +1,24 @@import { useCallback, useMemo, useState } from "react";import { type TableDateRange } from "@/src/utils/date-range-utils";+type TimeOrderBy = { column: string; order: "ASC" | "DESC" } | null | undefined;++/**+ * Offset paging is only unstable on a live, open-ended timestamp-DESC set:+ * new rows arrive at the top and shift every later page. Timestamp-ASC (and+ * any non-time sort) appends or interleaves without moving page 1's offsets,+ * so a pin would only shrink the window.+ *+ * Null orderBy follows the tables' default (time DESC).+ */+export function isLiveTailTimeSort(+ orderBy: TimeOrderBy,+ timeColumn: string,+): boolean {+ if (!orderBy) return true;+ return orderBy.column === timeColumn && orderBy.order === "DESC";+}+/*** Holds the paged result set still while the reader is past the first page.*@@ -20,16 +38,33 @@ import { type TableDateRange } from "@/src/utils/date-range-utils";* returning to page 1 goes live again. Opening a later page directly has no page* 1 to align with, so it pins to that first render instead. An already-closed* window (an absolute range) is left alone.+ *+ * Pass `enabled: false` for sorts that do not have a live tail (time ASC, or+ * a non-time column). Pinning those to `rows[0]`'s timestamp collapses the+ * window to the first page and the pagination clamp snaps the reader back.*/export function usePaginationWindowPin(range: TableDateRange | undefined,pageIndex: number,+ options?: { enabled?: boolean },) {+ const enabled = options?.enabled ?? true;const [pinnedAt, setPinnedAt] = useState<Date | null>(() =>- pageIndex > 0 ? new Date() : null,+ enabled && pageIndex > 0 ? new Date() : null,);+ const [wasEnabled, setWasEnabled] = useState(enabled);++ // Switching onto a live-tail sort while already past page 1 is the same as+ // opening that page directly: there is no page-1 newest row to align with.+ // Pin to now so the window is not left open (or pinned to a stale ASC visit).+ if (enabled !== wasEnabled) {+ setWasEnabled(enabled);+ if (enabled && pageIndex > 0) {+ setPinnedAt(new Date());+ }+ }- const pin = pageIndex > 0 ? pinnedAt : null;+ const pin = enabled && pageIndex > 0 ? pinnedAt : null;const pinnedRange = useMemo(() => (range && !range.to && pin ? { from: range.from, to: pin } : range),@@ -39,9 +74,10 @@ export function usePaginationWindowPin(/** Call from the pagination handler, where the leaving page's rows are known. */const pinOnLeavingFirstPage = useCallback((nextPageIndex: number, newestVisible: Date | undefined) => {+ if (!enabled) return;if (nextPageIndex > 0) setPinnedAt(newestVisible ?? new Date());},- [],+ [enabled],);return { range: pinnedRange, pinOnLeavingFirstPage };diff --git a/web/src/components/table/use-cases/traces.tsx b/web/src/components/table/use-cases/traces.tsxindex 3f23267ca8..d3fc3a9cca 100644--- a/web/src/components/table/use-cases/traces.tsx+++ b/web/src/components/table/use-cases/traces.tsx@@ -56,7 +56,10 @@ import { ConnectedIOTableCell } from "@/src/components/table/ConnectedIOTableCelimport { useTableDateRange } from "@/src/hooks/useTableDateRange";import { useLiveTableDateRange } from "@/src/hooks/useLiveTableDateRange";import { usePendingRowIds } from "@/src/components/table/hooks/usePendingRowIds";-import { usePaginationWindowPin } from "@/src/components/table/hooks/usePaginationWindowPin";+import {+ isLiveTailTimeSort,+ usePaginationWindowPin,+} from "@/src/components/table/hooks/usePaginationWindowPin";import { joinTableCoreAndMetrics } from "@/src/components/table/utils/joinTableCoreAndMetrics";import useColumnOrder from "@/src/features/column-visibility/hooks/useColumnOrder";import { BatchExportTableButton } from "@/src/components/BatchExportTableButton";@@ -279,6 +282,7 @@ function TracesTableInternal({usePaginationWindowPin(dateRange,limitRows ? 0 : paginationState.pageIndex,+ { enabled: isLiveTailTimeSort(orderByState, "timestamp") },);const rowsDateRangeFilter: FilterState = toTimestampFilter(rowsDateRange);const userIdFilter: FilterState = userIddiff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsxindex 43b8fbab85..77f77382f8 100644--- a/web/src/features/events/components/EventsTable.tsx+++ b/web/src/features/events/components/EventsTable.tsx@@ -78,7 +78,10 @@ import {} from "@/src/components/table/data-table-row-height-switch";import { useTableDateRange } from "@/src/hooks/useTableDateRange";import { useLiveTableDateRange } from "@/src/hooks/useLiveTableDateRange";-import { usePaginationWindowPin } from "@/src/components/table/hooks/usePaginationWindowPin";+import {+ isLiveTailTimeSort,+ usePaginationWindowPin,+} from "@/src/components/table/hooks/usePaginationWindowPin";import {type TableDateRange,TABLE_AGGREGATION_OPTIONS,@@ -493,7 +496,11 @@ export default function ObservationsEventsTable({// row/count queries take the pinned upper bound instead, so offset paging does// not repeat or skip rows while the window keeps taking in newly ingested ones.const { range: rowsDateRange, pinOnLeavingFirstPage } =- usePaginationWindowPin(dateRange, limitRows ? 0 : paginationState.page - 1);+ usePaginationWindowPin(+ dateRange,+ limitRows ? 0 : paginationState.page - 1,+ { enabled: isLiveTailTimeSort(orderByState, "startTime") },+ );const dateRangeFilter: FilterState = toStartTimeFilterState(rowsDateRange);const appRootDefault = useAppRootDefault({