Skip to content

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

fix(web): don't retry queries rejected by ClickHouse resource guardrails (#18301)

sprashant978e81811778109
diff --git a/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx b/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
index 3cd402a2c4..bea203edb5 100644
--- a/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
+++ b/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
@@ -19,7 +19,7 @@ import { Separator } from "@/src/components/ui/separator";
import Link from "next/link";
import { Card } from "@/src/components/ui/card";
import { useReadPath } from "@/src/features/events";
-import { api } from "@/src/utils/api";
+import { api, shouldRetryResourceQuery } from "@/src/utils/api";
import { JsonSkeleton } from "@/src/components/ui/CodeJsonViewer";
import { CommentDrawerController } from "@/src/features/comments";
import { getNumberFromMap } from "@/src/utils/map-utils";
@@ -55,14 +55,7 @@ export const SessionAnnotationProcessor: React.FC<
{ projectId, sessionId: item.objectId },
{
enabled: isV4,
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
},
);
diff --git a/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts b/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts
index 8773294cdf..8d23dc1da2 100644
--- a/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts
+++ b/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts
@@ -1,4 +1,4 @@
-import { api } from "@/src/utils/api";
+import { api, shouldRetryResourceQuery } from "@/src/utils/api";
import {
type AnnotationQueueItem,
AnnotationQueueObjectType,
@@ -29,14 +29,7 @@ export const useAnnotationObjectData = (
{ traceId: traceId as string, projectId },
{
enabled: isTraceOrObservation && !isV4,
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
},
);
@@ -57,14 +50,7 @@ export const useAnnotationObjectData = (
},
{
enabled: isSession && !isV4,
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
},
);
@@ -76,14 +62,7 @@ export const useAnnotationObjectData = (
},
{
enabled: isSession && isV4,
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
},
);
diff --git a/web/src/features/events/hooks/useEventsTraceData.clienttest.tsx b/web/src/features/events/hooks/useEventsTraceData.clienttest.tsx
index 9c73d36ade..49f56a3503 100644
--- a/web/src/features/events/hooks/useEventsTraceData.clienttest.tsx
+++ b/web/src/features/events/hooks/useEventsTraceData.clienttest.tsx
@@ -11,6 +11,7 @@ const { mockByTraceId, mockBatchIO, mockScoresForTrace } = vi.hoisted(() => ({
vi.mock("@/src/utils/api", () => ({
sendAsPostOption: {},
+ shouldRetryResourceQuery: vi.fn(),
api: {
events: {
byTraceId: {
diff --git a/web/src/features/events/hooks/useEventsTraceData.ts b/web/src/features/events/hooks/useEventsTraceData.ts
index 09026bf4f9..e7e15d4665 100644
--- a/web/src/features/events/hooks/useEventsTraceData.ts
+++ b/web/src/features/events/hooks/useEventsTraceData.ts
@@ -1,6 +1,10 @@
import { useMemo } from "react";
import { keepPreviousData } from "@tanstack/react-query";
-import { api, sendAsPostOption } from "@/src/utils/api";
+import {
+ api,
+ sendAsPostOption,
+ shouldRetryResourceQuery,
+} from "@/src/utils/api";
import { useLatched } from "@/src/hooks/useLatched";
import {
adaptEventsToTraceFormat,
@@ -71,10 +75,7 @@ export function useEventsTraceData(
},
{
enabled: enabled && !!traceId,
- retry(failureCount, error) {
- if (error.data?.code === "UNAUTHORIZED") return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
staleTime: 60 * 1000, // 1 minute
placeholderData: keepPreviousData,
},
diff --git a/web/src/features/sessions/SessionPages.tsx b/web/src/features/sessions/SessionPages.tsx
index 0c548b1356..32fe8b408d 100644
--- a/web/src/features/sessions/SessionPages.tsx
+++ b/web/src/features/sessions/SessionPages.tsx
@@ -15,7 +15,7 @@ import {
DetailPageNav,
useDetailPageLists,
} from "@/src/features/navigate-detail-pages";
-import { api } from "@/src/utils/api";
+import { api, shouldRetryResourceQuery } from "@/src/utils/api";
import { usdFormatter } from "@/src/utils/numbers";
import { getNumberFromMap } from "@/src/utils/map-utils";
import Link from "next/link";
@@ -450,14 +450,7 @@ export const SessionPage: React.FC<{
},
{
enabled: Boolean(projectId) && Boolean(sessionId),
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
},
);
const webCalloutAction = useWebCalloutAction(
@@ -1024,14 +1017,7 @@ export const SessionEventsPage: React.FC<{
},
{
enabled: !!projectId && !!sessionId,
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
},
);
@@ -1039,14 +1025,7 @@ export const SessionEventsPage: React.FC<{
{ projectId, sessionId },
{
enabled: !!projectId && !!sessionId,
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
},
);
diff --git a/web/src/features/traces/hooks/useTraceDetailData.clienttest.tsx b/web/src/features/traces/hooks/useTraceDetailData.clienttest.tsx
index e9cfe17884..be8269a0ed 100644
--- a/web/src/features/traces/hooks/useTraceDetailData.clienttest.tsx
+++ b/web/src/features/traces/hooks/useTraceDetailData.clienttest.tsx
@@ -30,6 +30,7 @@ vi.mock("@/src/features/events/hooks/useEventsTraceData", () => ({
// The traces-table query hook always runs (it's a hook; enabled:false on the
// beta path), so it only needs to return a query-shaped object.
vi.mock("@/src/utils/api", () => ({
+ shouldRetryResourceQuery: vi.fn(),
api: {
public: {
traceReadConfig: {
diff --git a/web/src/features/traces/hooks/useTraceDetailData.ts b/web/src/features/traces/hooks/useTraceDetailData.ts
index f334da7a0b..956972275d 100644
--- a/web/src/features/traces/hooks/useTraceDetailData.ts
+++ b/web/src/features/traces/hooks/useTraceDetailData.ts
@@ -1,4 +1,4 @@
-import { api } from "@/src/utils/api";
+import { api, shouldRetryResourceQuery } from "@/src/utils/api";
import { keepPreviousData } from "@tanstack/react-query";
import { useEventsTraceData, useReadPath } from "@/src/features/events";
import { useSession } from "next-auth/react";
@@ -51,14 +51,7 @@ export function useTraceDetailData({
!!projectId &&
!isTraceSourceLoading &&
!useEventsTraceSource,
- retry(failureCount, error) {
- if (
- error.data?.code === "UNAUTHORIZED" ||
- error.data?.code === "NOT_FOUND"
- )
- return false;
- return failureCount < 3;
- },
+ retry: shouldRetryResourceQuery,
staleTime: 60 * 1000,
placeholderData: keepPreviousData,
},
diff --git a/web/src/utils/api.clienttest.ts b/web/src/utils/api.clienttest.ts
index dc48fdbcda..c783ce73e0 100644
--- a/web/src/utils/api.clienttest.ts
+++ b/web/src/utils/api.clienttest.ts
@@ -22,6 +22,8 @@ import {
MAX_TRPC_GET_URL_BYTES,
reportNonTrpcError,
reportTrpcErrorWithoutToast,
+ shouldRetryQuery,
+ shouldRetryResourceQuery,
shouldSendQueryAsPost,
} from "@/src/utils/api";
import { getTrpcToastOperation } from "@/src/utils/trpcToastOperation";
@@ -652,6 +654,81 @@ describe("isExpectedTrpcClientError", () => {
});
});
+describe("shouldRetryQuery", () => {
+ it("does not retry a query rejected by the ClickHouse resource guardrail", () => {
+ // Production shape minted by `withErrorHandling` (web/src/server/api/
+ // trpc.ts) when a query times out or exceeds ClickHouse memory. A retry
+ // re-runs the same over-budget query while ClickHouse is still busy with
+ // the abandoned attempt.
+ const error = TRPCClientError.from({
+ error: {
+ code: -32600,
+ message:
+ "Your query could not be completed. Please narrow your request by adding more specific filters (e.g., a shorter date range).",
+ data: {
+ code: "UNPROCESSABLE_CONTENT",
+ httpStatus: 422,
+ path: "sessions.allFromEvents",
+ errorName: "ClickHouseResourceError",
+ },
+ },
+ });
+
+ expect(shouldRetryQuery(0, error)).toBe(false);
+ });
+
+ it("does not retry a missing resource (404)", () => {
+ const error = trpcServerError({
+ code: "NOT_FOUND",
+ httpStatus: 404,
+ path: "traces.byId",
+ });
+
+ expect(shouldRetryQuery(0, error)).toBe(false);
+ });
+
+ it.each([
+ [
+ "a server error",
+ trpcServerError({
+ code: "INTERNAL_SERVER_ERROR",
+ httpStatus: 500,
+ path: "sessions.allFromEvents",
+ }),
+ ],
+ [
+ "a network connectivity failure",
+ TRPCClientError.from(new TypeError("Failed to fetch")),
+ ],
+ ])("retries %s up to three times", (_label, error) => {
+ expect(shouldRetryQuery(0, error)).toBe(true);
+ expect(shouldRetryQuery(2, error)).toBe(true);
+ expect(shouldRetryQuery(3, error)).toBe(false);
+ });
+});
+
+describe("shouldRetryResourceQuery", () => {
+ it.each([
+ ["UNAUTHORIZED", 401],
+ ["UNPROCESSABLE_CONTENT", 422],
+ ])("does not retry %s", (code, httpStatus) => {
+ const error = trpcServerError({ code, httpStatus, path: "traces.byId" });
+
+ expect(shouldRetryResourceQuery(0, error)).toBe(false);
+ });
+
+ it("retries a server error up to three times", () => {
+ const error = trpcServerError({
+ code: "INTERNAL_SERVER_ERROR",
+ httpStatus: 500,
+ path: "traces.byId",
+ });
+
+ expect(shouldRetryResourceQuery(2, error)).toBe(true);
+ expect(shouldRetryResourceQuery(3, error)).toBe(false);
+ });
+});
+
describe("getTrpcErrorFingerprint", () => {
// The whole point of the fingerprint: unrelated tRPC failures must STOP
// grouping into one issue, while retries of the same failure class must
diff --git a/web/src/utils/api.ts b/web/src/utils/api.ts
index c17c916873..25b33202dd 100644
--- a/web/src/utils/api.ts
+++ b/web/src/utils/api.ts
@@ -668,6 +668,47 @@ const shouldSilenceError = (
return false;
};
+/**
+ * Default retry policy for tRPC queries.
+ *
+ * Don't retry on 404s: a deleted/missing resource never appears via retry, so
+ * failing fast avoids piling up pointless refetches (and ClickHouse load for
+ * resources backed by it).
+ *
+ * Don't retry on 422s either: the server mints UNPROCESSABLE_CONTENT only for
+ * query resource guardrails (`ClickHouseResourceError`) and oversized payloads
+ * (see {@link EXPECTED_TRPC_ERROR_CODES}). A retry re-runs the same over-budget
+ * query while ClickHouse is still saturated — self-hosted ClickHouse keeps
+ * executing the abandoned attempt (`cancel_http_readonly_queries_on_client_close`
+ * defaults to 0 outside ClickHouse Cloud) — so each rejection would multiply
+ * the load that caused it.
+ *
+ * Every other 4xx keeps the default retry/backoff — some (e.g. a route param
+ * that hasn't hydrated yet, a proxy-level 429) are transient and self-heal.
+ */
+export const shouldRetryQuery = (
+ failureCount: number,
+ error: unknown,
+): boolean => {
+ const httpStatus = getHttpStatus(error);
+ if (httpStatus === 404 || httpStatus === 422) {
+ return false;
+ }
+ return failureCount < 3;
+};
+
+/**
+ * Retry policy for single-resource detail queries (trace, session): also fails
+ * fast on 401 so the page can render its access error immediately. Use this
+ * instead of a hand-written `retry` so the {@link shouldRetryQuery} rules still
+ * apply — a per-query `retry` replaces the default rather than extending it.
+ */
+export const shouldRetryResourceQuery = (
+ failureCount: number,
+ error: unknown,
+): boolean =>
+ getHttpStatus(error) !== 401 && shouldRetryQuery(failureCount, error);
+
/** APIError is returned by api.*.*.useQuery */
export type APIError = TRPCClientErrorLike<AppRouter>;
@@ -725,17 +766,7 @@ export const api = createTRPCNext<AppRouter>({
queries: {
// react query defaults to `online`, but we want to disable it as it caused issues for some users
networkMode: "always",
- // Don't retry on 404s: a deleted/missing resource never appears via
- // retry, so failing fast avoids piling up pointless refetches (and
- // ClickHouse load for resources backed by it). Every other 4xx keeps
- // the default retry/backoff — some (e.g. a route param that hasn't
- // hydrated yet, a proxy-level 429) are transient and self-heal.
- retry: (failureCount, error) => {
- if (getHttpStatus(error) === 404) {
- return false;
- }
- return failureCount < 3;
- },
+ retry: shouldRetryQuery,
},
mutations: {
onError: (error) =>