Skip to content

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

fix(graph): cause freeze through infinite loop (#9576)

Nimar9b534187804c
diff --git a/web/src/__tests__/buildStepData.clienttest.ts b/web/src/__tests__/buildStepData.clienttest.ts
index 63273efde6..a0f3852ce2 100644
--- a/web/src/__tests__/buildStepData.clienttest.ts
+++ b/web/src/__tests__/buildStepData.clienttest.ts
@@ -351,6 +351,92 @@ describe("buildStepData", () => {
});
describe("edge cases", () => {
+ it("should handle child starting before parent causing infinite loop (bug from trace 9f6079ff)", () => {
+ // child starts BEFORE parent, creating infinite loop, see LFE-7096
+ // The key is having multiple nested levels with same start times
+ const observations: AgentGraphDataResponse[] = [
+ createMockObservation({
+ id: "root",
+ name: "Root",
+ startTime: "2025-10-07 12:27:33.826",
+ endTime: "2025-10-07 12:27:33.829",
+ parentObservationId: null,
+ }),
+ createMockObservation({
+ id: "sibling1",
+ name: "Sibling1",
+ startTime: "2025-10-07 12:27:33.830",
+ endTime: "2025-10-07 12:27:33.830",
+ parentObservationId: "root",
+ }),
+ createMockObservation({
+ id: "sibling2",
+ name: "Sibling2",
+ startTime: "2025-10-07 12:27:33.830",
+ endTime: "2025-10-07 12:27:33.830",
+ parentObservationId: "root",
+ }),
+ createMockObservation({
+ id: "parent",
+ name: "Parent",
+ startTime: "2025-10-07 12:27:33.846", // Starts AFTER child
+ endTime: "2025-10-07 12:27:39.276",
+ parentObservationId: "root",
+ }),
+ createMockObservation({
+ id: "child",
+ name: "Child",
+ startTime: "2025-10-07 12:27:33.830", // Starts BEFORE parent!
+ endTime: "2025-10-07 12:27:39.274",
+ parentObservationId: "parent",
+ }),
+ createMockObservation({
+ id: "grandchild",
+ name: "Grandchild",
+ startTime: "2025-10-07 12:27:33.830",
+ endTime: "2025-10-07 12:27:36.437",
+ parentObservationId: "child",
+ }),
+ createMockObservation({
+ id: "ggchild1",
+ name: "GGChild1",
+ startTime: "2025-10-07 12:27:33.830",
+ endTime: "2025-10-07 12:27:33.830",
+ parentObservationId: "grandchild",
+ }),
+ createMockObservation({
+ id: "ggchild2",
+ name: "GGChild2",
+ startTime: "2025-10-07 12:27:33.830",
+ endTime: "2025-10-07 12:27:33.830",
+ parentObservationId: "grandchild",
+ }),
+ createMockObservation({
+ id: "gggchild",
+ name: "GGGChild",
+ startTime: "2025-10-07 12:27:33.866",
+ endTime: "2025-10-07 12:27:36.435",
+ parentObservationId: "grandchild",
+ }),
+ createMockObservation({
+ id: "ggggchild",
+ name: "GGGGChild",
+ startTime: "2025-10-07 12:27:33.866",
+ endTime: "2025-10-07 12:27:36.434",
+ parentObservationId: "gggchild",
+ }),
+ ];
+
+ const result = buildStepData(observations);
+
+ const userObservations = result.filter((obs) => !obs.name.includes("__"));
+ const parent = userObservations.find((o) => o.name === "Parent");
+ const child = userObservations.find((o) => o.name === "Child");
+
+ // Constraint must be enforced
+ expect(parent!.step!).toBeLessThan(child!.step!);
+ });
+
it("should handle empty array", () => {
const result = buildStepData([]);
// Should only have system nodes (__start__, __end__)
diff --git a/web/src/features/trace-graph-view/buildStepData.ts b/web/src/features/trace-graph-view/buildStepData.ts
index 9930a5a28a..40a2a538e5 100644
--- a/web/src/features/trace-graph-view/buildStepData.ts
+++ b/web/src/features/trace-graph-view/buildStepData.ts
@@ -104,59 +104,71 @@ function assignGlobalTimingSteps(
// apply span parent-child step constraint: any child must be at least parent_step + 1
// any step groups down the line will be pushed down by the same amount
let constraintViolations = true;
+ let iterationCount = 0;
+ const MAX_ITERATIONS = 1500;
while (constraintViolations) {
- constraintViolations = false;
-
- // Track step adjustments to apply during result building
- const stepAdjustments = new Map<string, number>();
- const idToStepMap = new Map<string, number>();
+ iterationCount++;
- // build step map while tracking step adjustments
- for (const obs of result) {
- stepAdjustments.set(obs.id, 0); // Initialize adjustment
- idToStepMap.set(obs.id, obs.step!); // we checked against null already
+ if (iterationCount > MAX_ITERATIONS) {
+ console.debug("Aborting graph processing due to excessive iterations.");
+ break;
}
- const stepPushes: Array<{ fromStep: number; pushCount: number }> = [];
- // identify if any spans must be pushed down
- for (const obs of result) {
- if (obs.parentObservationId) {
- const parentStep = idToStepMap.get(obs.parentObservationId);
- const currentStep = obs.step;
- if (
- parentStep !== undefined &&
- parentStep !== null &&
- currentStep !== null
- ) {
- const requiredMinStep = parentStep + 1;
- if (currentStep < requiredMinStep) {
- // Track step push: all observations at requiredMinStep+ need +1
- stepPushes.push({ fromStep: requiredMinStep, pushCount: 1 });
-
- stepAdjustments.set(obs.id, requiredMinStep - currentStep);
- constraintViolations = true;
- }
- }
+ constraintViolations = false;
+ // Track step adjustments to apply during result building
+ const adjustments = new Map<string, number>();
+ const obsMap = new Map(result.map((o) => [o.id, o]));
+
+ // helper to get all ancestors of an observation
+ const getAncestors = (obsId: string): Set<string> => {
+ const ancestors = new Set<string>();
+ let current = obsMap.get(obsId);
+ while (current?.parentObservationId) {
+ ancestors.add(current.parentObservationId);
+ current = obsMap.get(current.parentObservationId);
}
- }
+ return ancestors;
+ };
- // apply step pushes by directly incrementing affected observations
- for (const push of stepPushes) {
- for (const obs of result) {
- if (obs.step !== null && obs.step >= push.fromStep) {
- const currentAdjustment = stepAdjustments.get(obs.id) || 0;
- stepAdjustments.set(obs.id, currentAdjustment + push.pushCount);
+ // identify spans which must be pushed (violations) and calculate adjustments
+ for (const obs of result) {
+ if (!obs.parentObservationId || obs.step === null) continue;
+
+ const parent = obsMap.get(obs.parentObservationId);
+ if (!parent || parent.step === null) continue;
+
+ const requiredMinStep = parent.step + 1;
+ if (obs.step < requiredMinStep) {
+ constraintViolations = true;
+ const ancestors = getAncestors(obs.id);
+
+ // adjust the violating child and push all observations at future steps forward (except ancestors)
+ for (const target of result) {
+ if (target.step === null) continue;
+
+ if (target.id === obs.id) {
+ // child adjustment
+ adjustments.set(
+ target.id,
+ (adjustments.get(target.id) || 0) + (requiredMinStep - obs.step),
+ );
+ } else if (
+ target.step >= requiredMinStep &&
+ !ancestors.has(target.id)
+ ) {
+ // Push forward observations at future steps except for ancestors
+ adjustments.set(target.id, (adjustments.get(target.id) || 0) + 1);
+ }
}
}
}
- // Build result with step adjustments applied
result = result.map((obs) => ({
...obs,
step:
obs.step !== null
- ? obs.step + (stepAdjustments.get(obs.id) || 0)
+ ? obs.step + (adjustments.get(obs.id) || 0)
: obs.step,
}));
} // end loop to check if parent-child constraints require step adjustments