🪢 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(sessions): guard session-view trace-card I/O against non-array shape (regression from #15124) (#15133)
Nikita Kabardina67660029460
diff --git a/web/src/__tests__/server/sessions-observations-io-cap.servertest.ts b/web/src/__tests__/server/sessions-observations-io-cap.servertest.tsindex 626a0d9cca..784db16755 100644--- a/web/src/__tests__/server/sessions-observations-io-cap.servertest.ts+++ b/web/src/__tests__/server/sessions-observations-io-cap.servertest.ts@@ -6,6 +6,12 @@* whole (cards render exactly as before); larger fields come back as a* preview head plus their true length and a truncated flag, with the trace* peek / `sessions.observationFullIOFromEvents` as the full-reading surfaces.+ *+ * The procedure returns a BARE ARRAY of observations (backward-compatible with+ * clients that call `.find` on the response directly, LFE-10958 regression).+ * "More observations exist" is signalled in-band: up to+ * SESSION_OBSERVATIONS_PER_TRACE_LIMIT + 1 real observations come back, and+ * the extra (+1) row is the client's "has more" sentinel.*/import type { Session } from "next-auth";import { prisma } from "@langfuse/shared/src/db";@@ -34,6 +40,12 @@ const INLINE_LIMIT = 300_000;const PREVIEW_LIMIT = 4_000;const PER_TRACE_LIMIT = 50;+// The client derives "more exist" from the +1 sentinel: the response is a bare+// array of up to PER_TRACE_LIMIT + 1 real observations, so more than+// PER_TRACE_LIMIT real rows means the trace has more than the card shows.+const hasMore = (observations: { id: string }[], traceId: string) =>+ observations.filter((o) => o.id !== `t-${traceId}`).length > PER_TRACE_LIMIT;+maybe("sessions observations bounded I/O (events)", () => {const projectId = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";@@ -128,8 +140,8 @@ maybe("sessions observations bounded I/O (events)", () => {traceId,filter: [],});- expect(result.observations.length).toBeGreaterThanOrEqual(- Math.min(events.length, PER_TRACE_LIMIT),+ expect(result.length).toBeGreaterThanOrEqual(+ Math.min(events.length, PER_TRACE_LIMIT + 1),);});@@ -143,15 +155,14 @@ maybe("sessions observations bounded I/O (events)", () => {{ input: bigInput, output: "small" },]);- const { observations, hasMoreObservations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });- expect(hasMoreObservations).toBe(false);+ expect(hasMore(observations, traceId)).toBe(false);const small = observations.find((o) => o.id === `${traceId}-o0`);expect(small?.input).toBe("hello");@@ -174,13 +185,12 @@ maybe("sessions observations bounded I/O (events)", () => {{ metadataValues: [bigValue, "tiny"], metadataNames: ["big", "small"] },]);- const { observations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });const observation = observations[0];expect(observation.metadataTruncated).toBe(true);@@ -189,21 +199,22 @@ maybe("sessions observations bounded I/O (events)", () => {expect(metadata.small).toBe("tiny");});- it("caps observations per card and reports more exist", async () => {+ it("caps observations per card and signals more exist via the +1 sentinel", async () => {const { sessionId, traceId } = await seedObservations(Array.from({ length: PER_TRACE_LIMIT + 2 }, () => ({})),);- const { observations, hasMoreObservations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });- expect(observations.length).toBe(PER_TRACE_LIMIT);- expect(hasMoreObservations).toBe(true);+ // The response is bounded to the display limit plus one sentinel row; the+ // sentinel is what tells the client "more observations exist".+ expect(observations.length).toBe(PER_TRACE_LIMIT + 1);+ expect(hasMore(observations, traceId)).toBe(true);});it("trims to preview heads once the cumulative I/O budget is exhausted (valid-JSON payloads)", async () => {@@ -218,13 +229,12 @@ maybe("sessions observations bounded I/O (events)", () => {Array.from({ length: 8 }, () => ({ input: nearCap, output: "ok" })),);- const { observations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });const first = observations[0];expect(first.inputTruncated).toBe(false);@@ -242,9 +252,8 @@ maybe("sessions observations bounded I/O (events)", () => {it("does not let the synthetic trace row consume a card slot or trip hasMore", async () => {// 50 real observations + the synthetic trace-level row (id `t-<traceId>`,// the shape handleEventPropagationJob writes): all 50 real observations- // must come back alongside the synthetic row, and hasMoreObservations- // must stay false — the synthetic row is trace metadata, not observation- // number 51.+ // must come back alongside the synthetic row, and the +1 sentinel must not+ // appear — the synthetic row is trace metadata, not observation number 51.const { sessionId, traceId, baseTime } = await seedObservations(Array.from({ length: PER_TRACE_LIMIT }, () => ({})),);@@ -271,22 +280,21 @@ maybe("sessions observations bounded I/O (events)", () => {traceId,filter: [],});- expect(result.observations.length).toBe(PER_TRACE_LIMIT + 1);+ expect(result.length).toBe(PER_TRACE_LIMIT + 1);});- const { observations, hasMoreObservations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });expect(observations.some((o) => o.id === `t-${traceId}`)).toBe(true);expect(observations.filter((o) => o.id !== `t-${traceId}`).length).toBe(PER_TRACE_LIMIT,);- expect(hasMoreObservations).toBe(false);+ expect(hasMore(observations, traceId)).toBe(false);});it("collapses un-merged span versions to one observation (newest wins)", async () => {@@ -332,19 +340,18 @@ maybe("sessions observations bounded I/O (events)", () => {traceId,filter: [],});- expect(result.observations.length).toBeGreaterThanOrEqual(1);+ expect(result.length).toBeGreaterThanOrEqual(1);});- const { observations, hasMoreObservations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });expect(observations.length).toBe(1);- expect(hasMoreObservations).toBe(false);+ expect(hasMore(observations, traceId)).toBe(false);expect(observations[0].output).toBe("completed answer");});@@ -360,13 +367,12 @@ maybe("sessions observations bounded I/O (events)", () => {},]);- const { observations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });const observation = observations[0];expect((observation.metadata as Record<string, unknown>).k).toBe(@@ -389,13 +395,12 @@ maybe("sessions observations bounded I/O (events)", () => {})),);- const { observations } =- await caller.sessions.observationsForTraceFromEvents({- projectId,- sessionId,- traceId,- filter: [],- });+ const observations = await caller.sessions.observationsForTraceFromEvents({+ projectId,+ sessionId,+ traceId,+ filter: [],+ });const first = observations[0];expect(first.metadataTruncated).toBe(false);diff --git a/web/src/components/session/SessionObservationIO.tsx b/web/src/components/session/SessionObservationIO.tsxindex d771e3931d..e5d899fd35 100644--- a/web/src/components/session/SessionObservationIO.tsx+++ b/web/src/components/session/SessionObservationIO.tsx@@ -9,7 +9,7 @@ import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePosimport { compactNumberFormatter } from "@/src/utils/numbers";export type SessionTraceObservation =- RouterOutputs["sessions"]["observationsForTraceFromEvents"]["observations"][number];+ RouterOutputs["sessions"]["observationsForTraceFromEvents"][number];/** Display cap of a preview section — matches the server's preview head. */const PREVIEW_DISPLAY_CHARS = 4_000;diff --git a/web/src/components/session/TraceEventsRow.tsx b/web/src/components/session/TraceEventsRow.tsxindex 628f2a89e6..30f0dfc7fb 100644--- a/web/src/components/session/TraceEventsRow.tsx+++ b/web/src/components/session/TraceEventsRow.tsx@@ -157,10 +157,22 @@ export const TraceEventsRow = React.memo(// trace-level I/O that no observation shows (a v3-migrated trace can set// trace I/O apart from any observation; dropping it would lose content and// blind the annotation queue, which hides the trace panel).- const observations = observationsQuery.data?.observations;- const hasMoreObservations = Boolean(- observationsQuery.data?.hasMoreObservations,- );+ // `observationsForTraceFromEvents` returns a BARE ARRAY of observations.+ // #15124 briefly wrapped it as `{ observations, hasMoreObservations }`,+ // which crashed in-flight old clients that call `.find` on the response+ // directly during a rollout (LFE-10958 regression). Read it defensively so+ // BOTH shapes are safe: this new client can also briefly hit a not-yet-+ // updated server that still returns the envelope, so an Array method must+ // never run on a non-array value.+ type ObservationsResponse =+ RouterOutputs["sessions"]["observationsForTraceFromEvents"];+ const observationsData = observationsQuery.data as+ | ObservationsResponse+ | { observations?: ObservationsResponse }+ | undefined;+ const observations = Array.isArray(observationsData)+ ? observationsData+ : (observationsData?.observations ?? undefined);// Opens the trace peek AT the observation (the session page's peek config// mirrors row.observationId into the ?observation= param; the annotation@@ -171,13 +183,38 @@ export const TraceEventsRow = React.memo(},[openPeek, trace],);- const visibleObservations = React.useMemo(() => {- if (!observations) return undefined;+ const { visibleObservations, hasMoreObservations } = React.useMemo(() => {+ const all = observations;+ if (!all)+ return {+ visibleObservations: undefined,+ hasMoreObservations: false,+ };const syntheticTraceRowId = `t-${trace.id}`;- const syntheticRow = observations.find(+ // The server returns up to SESSION_CARD_OBSERVATIONS_NOTICE_COUNT + 1+ // real observations; the extra (+1) row is the "more exist" sentinel.+ // Show only the first NOTICE_COUNT real observations, keeping the+ // synthetic trace-level row wherever it sits (it never consumes a slot).+ let realCount = 0;+ let realShown = 0;+ const page: typeof all = [];+ for (const observation of all) {+ if (observation.id === syntheticTraceRowId) {+ page.push(observation);+ continue;+ }+ realCount++;+ if (realShown >= SESSION_CARD_OBSERVATIONS_NOTICE_COUNT) continue;+ page.push(observation);+ realShown++;+ }+ const hasMoreObservations =+ realCount > SESSION_CARD_OBSERVATIONS_NOTICE_COUNT;++ const syntheticRow = page.find((observation) => observation.id === syntheticTraceRowId,);- const realObservations = observations.filter(+ const realObservations = page.filter((observation) => observation.id !== syntheticTraceRowId,);// I/O can be server-truncated to a preview head (LFE-10958), so equal@@ -195,8 +232,12 @@ export const TraceEventsRow = React.memo(isEqual(observation.output, syntheticRow.output) &&observation.outputLength === syntheticRow.outputLength),);- if (!syntheticRowIsRedundant) return observations;- return realObservations.length > 0 ? realObservations : observations;+ const visibleObservations = !syntheticRowIsRedundant+ ? page+ : realObservations.length > 0+ ? realObservations+ : page;+ return { visibleObservations, hasMoreObservations };}, [observations, trace.id]);return (diff --git a/web/src/server/api/routers/sessions.ts b/web/src/server/api/routers/sessions.tsindex 45dd4edc88..dfdea12e0d 100644--- a/web/src/server/api/routers/sessions.ts+++ b/web/src/server/api/routers/sessions.ts@@ -835,9 +835,10 @@ export const sessionRouter = createTRPCRouter({];let orderBy: OrderByState = { column: "startTime", order: "ASC" };- // One extra row detects "more observations than the card shows", and- // one more because the synthetic trace-level row (id `t-<traceId>`)- // may sit in the fetched window without consuming a card slot.+ // One extra row is returned as the "more observations exist" sentinel+ // (the client shows the first LIMIT and surfaces a notice when it sees+ // the +1), and one more because the synthetic trace-level row (id+ // `t-<traceId>`) may sit in the fetched window without consuming a slot.let limit: number = SESSION_OBSERVATIONS_PER_TRACE_LIMIT + 2;let offset: number | undefined;@@ -880,15 +881,21 @@ export const sessionRouter = createTRPCRouter({// The synthetic trace-level row is metadata about the trace, not one of// its observations: it must neither consume one of the card's slots- // (displacing a real observation) nor count toward hasMore. The client- // decides whether to show or drop it (redundancy dedupe).+ // (displacing a real observation) nor count toward the "more exist"+ // signal. The client decides whether to show or drop it (redundancy+ // dedupe).+ //+ // BACKWARD-COMPATIBLE RESPONSE SHAPE (LFE-10958 regression): this+ // procedure returns a BARE ARRAY of observations. The client consumes+ // the response as an array and calls `.find`/`.filter` on it directly,+ // so wrapping it in an `{ observations, ... }` envelope crashes in-flight+ // old clients during a rollout ("x.find is not a function"). "More+ // observations exist" is therefore signalled IN-BAND: we return up to+ // SESSION_OBSERVATIONS_PER_TRACE_LIMIT + 1 real observations, and the+ // client treats that extra (+1) row as the "has more" sentinel, showing+ // only the first LIMIT. (With a positionFilter the fetch limit is 1, so+ // at most one row is returned and the sentinel can never appear.)const syntheticTraceRowId = `t-${input.traceId}`;- const realFetchedCount = fetched.filter(- (observation) => observation.id !== syntheticTraceRowId,- ).length;- const hasMoreObservations =- !positionFilter &&- realFetchedCount > SESSION_OBSERVATIONS_PER_TRACE_LIMIT;let realTaken = 0;const page: typeof fetched = [];for (const observation of fetched) {@@ -896,7 +903,8 @@ export const sessionRouter = createTRPCRouter({page.push(observation);continue;}- if (realTaken >= SESSION_OBSERVATIONS_PER_TRACE_LIMIT) continue;+ // Keep one past the display limit as the "more exist" sentinel.+ if (realTaken >= SESSION_OBSERVATIONS_PER_TRACE_LIMIT + 1) continue;page.push(observation);realTaken++;}@@ -958,7 +966,7 @@ export const sessionRouter = createTRPCRouter({};});- return { observations, hasMoreObservations };+ return observations;}),/*** Full raw I/O for one observation, for the session card's download