Skip to content

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

fix(web): group trace graph steps without recursive stack overflow (#17566)

cursor[bot]053753fac777
diff --git a/web/src/__tests__/buildStepData.clienttest.ts b/web/src/__tests__/buildStepData.clienttest.ts
index be2c24bb48..ee65c053af 100644
--- a/web/src/__tests__/buildStepData.clienttest.ts
+++ b/web/src/__tests__/buildStepData.clienttest.ts
@@ -531,6 +531,37 @@ describe("buildStepData", () => {
expect(result.every((obs) => obs.name.includes("__"))).toBe(true);
});
+ it(
+ "should group a long sequential chain without overflowing the call stack",
+ { timeout: 15_000 },
+ () => {
+ // Each non-overlapping observation becomes its own step group. A
+ // recursive walk overflows the browser stack on a long sequential
+ // trace even when every group makes progress.
+ const COUNT = 8_000;
+ const base = Date.parse("2025-08-21T18:53:25.000Z");
+ const observations: AgentGraphDataResponse[] = Array.from(
+ { length: COUNT },
+ (_, i) =>
+ createMockObservation({
+ id: `seq-${i}`,
+ name: `seq-${i}`,
+ startTime: new Date(base + i * 20).toISOString(),
+ endTime: new Date(base + i * 20 + 10).toISOString(),
+ }),
+ );
+
+ const result = buildStepData(observations);
+ const userObservations = result.filter(
+ (obs) => !obs.name.includes("__"),
+ );
+
+ expect(userObservations).toHaveLength(COUNT);
+ expect(userObservations[0].step).toBe(1);
+ expect(userObservations[COUNT - 1].step).toBe(COUNT);
+ },
+ );
+
it(
"should handle large number of observations with identical timestamps just below limit",
{ timeout: 5000 },
diff --git a/web/src/features/trace-graph-view/buildStepData.ts b/web/src/features/trace-graph-view/buildStepData.ts
index cedf30530c..ed2285f5bc 100644
--- a/web/src/features/trace-graph-view/buildStepData.ts
+++ b/web/src/features/trace-graph-view/buildStepData.ts
@@ -5,16 +5,15 @@ import {
LANGFUSE_END_NODE_NAME,
} from "./types";
-function buildStepGroups(
+function takeNextStepGroup(
observations: AgentGraphDataResponse[],
timestampCache: Map<string, { start: number; end: number }>,
-): AgentGraphDataResponse[][] {
- if (observations.length === 0) return [];
-
- const stepGroups: AgentGraphDataResponse[][] = [];
-
+): {
+ cleanedGroup: AgentGraphDataResponse[];
+ unprocessed: AgentGraphDataResponse[];
+} {
// create observation group and put the beginning observation in it
- let currentGroup = [observations[0]];
+ const currentGroup = [observations[0]];
const remainingObs = observations.slice(1);
// Track max end time for early termination optimization
@@ -67,23 +66,42 @@ function buildStepGroups(
});
// Inverted or otherwise invalid time ranges can cause cleanup to remove every
- // observation, which would recurse with identical input until the stack overflows.
- // To prevent this, if the cleaned group is empty, we will use the original group as a fallback
- // and mark all of its observations as processed to avoid infinite recursion.
+ // observation. Keep the original group so grouping always makes progress.
if (cleanedGroup.length === 0) {
cleanedGroup.push(...currentGroup);
currentGroup.forEach((o) => processedIds.add(o.id));
}
- stepGroups.push(cleanedGroup);
+ return {
+ cleanedGroup,
+ unprocessed: observations.filter((obs) => !processedIds.has(obs.id)),
+ };
+}
- // Optimization: use incrementally built processedIds set
- const unprocessed = observations.filter((obs) => !processedIds.has(obs.id));
+function buildStepGroups(
+ observations: AgentGraphDataResponse[],
+ timestampCache: Map<string, { start: number; end: number }>,
+): AgentGraphDataResponse[][] {
+ const stepGroups: AgentGraphDataResponse[][] = [];
+ let remaining = observations;
+
+ // Iterative so a long sequential chain (one group per observation) cannot
+ // overflow the call stack.
+ while (remaining.length > 0) {
+ const remainingCount = remaining.length;
+ const { cleanedGroup, unprocessed } = takeNextStepGroup(
+ remaining,
+ timestampCache,
+ );
+
+ if (cleanedGroup.length === 0 || unprocessed.length >= remainingCount) {
+ stepGroups.push([remaining[0]]);
+ remaining = remaining.slice(1);
+ continue;
+ }
- // process remaining observations in recursion
- if (unprocessed.length > 0) {
- const remainingStepGroups = buildStepGroups(unprocessed, timestampCache);
- stepGroups.push(...remainingStepGroups);
+ stepGroups.push(cleanedGroup);
+ remaining = unprocessed;
}
return stepGroups;