🪢 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(trace): keep JSON-viewer meta-root row count in sync after collapse (LANGFUSE-456) (#15174)
Nikita Kabardin98853af1f211
diff --git a/web/src/components/ui/AdvancedJsonViewer/utils/treeExpansion.ts b/web/src/components/ui/AdvancedJsonViewer/utils/treeExpansion.tsindex e25989a6c3..14e8edb707 100644--- a/web/src/components/ui/AdvancedJsonViewer/utils/treeExpansion.ts+++ b/web/src/components/ui/AdvancedJsonViewer/utils/treeExpansion.ts@@ -320,44 +320,50 @@ export function applyExpansionState(// Apply expansion state to each nodepostOrder.forEach((node) => {- if (!node.isExpandable) return;-- let shouldExpand = true;-- if (collapsedPaths.has("*")) {- shouldExpand = false;- } else if (typeof expansionState === "boolean") {- shouldExpand = expansionState;- } else if (collapsedPaths.has(node.id)) {- // Exact match: explicitly collapsed- shouldExpand = false;- } else if (expandedPaths.has(node.id)) {- // Exact match: explicitly expanded- shouldExpand = true;- } else if (expandedAncestors.has(node.id)) {- // Prefix matching: This node is an ancestor of an expanded path- // e.g., if "messages.0.content.text" is expanded, expand "messages", "messages.0", etc.- shouldExpand = true;- }- // No match: keep default (shouldExpand = true)- // This ensures nodes not explicitly in stored state stay expanded-- node.isExpanded = shouldExpand;- // Set userExpand for:- // - Exact matches from expansionState- // - Ancestor nodes expanded via prefix matching (so they're tracked for export)- if (typeof expansionState === "boolean") {- node.userExpand = undefined;- } else if (expansionState[node.id] !== undefined) {- node.userExpand = expansionState[node.id];- } else if (expandedAncestors.has(node.id)) {- // Ancestors expanded via prefix matching should be tracked- node.userExpand = true;- } else {- node.userExpand = undefined;+ // Expansion decision only applies to expandable nodes.+ if (node.isExpandable) {+ let shouldExpand = true;++ if (collapsedPaths.has("*")) {+ shouldExpand = false;+ } else if (typeof expansionState === "boolean") {+ shouldExpand = expansionState;+ } else if (collapsedPaths.has(node.id)) {+ // Exact match: explicitly collapsed+ shouldExpand = false;+ } else if (expandedPaths.has(node.id)) {+ // Exact match: explicitly expanded+ shouldExpand = true;+ } else if (expandedAncestors.has(node.id)) {+ // Prefix matching: This node is an ancestor of an expanded path+ // e.g., if "messages.0.content.text" is expanded, expand "messages", "messages.0", etc.+ shouldExpand = true;+ }+ // No match: keep default (shouldExpand = true)+ // This ensures nodes not explicitly in stored state stay expanded++ node.isExpanded = shouldExpand;+ // Set userExpand for:+ // - Exact matches from expansionState+ // - Ancestor nodes expanded via prefix matching (so they're tracked for export)+ if (typeof expansionState === "boolean") {+ node.userExpand = undefined;+ } else if (expansionState[node.id] !== undefined) {+ node.userExpand = expansionState[node.id];+ } else if (expandedAncestors.has(node.id)) {+ // Ancestors expanded via prefix matching should be tracked+ node.userExpand = true;+ } else {+ node.userExpand = undefined;+ }}- // Recompute offsets+ // Recompute offsets for EVERY node, not just expandable ones. Post-order+ // guarantees children are already updated, and recomputeNodeOffsets is a+ // no-op for collapsed/childless nodes. Crucially this includes the synthetic+ // meta-root (isExpandable: false) whose visibleDescendantCount drives the+ // virtualizer's row count — skipping it left a stale/over-counted total after+ // a section collapse, producing phantom row indexes (LANGFUSE-456).recomputeNodeOffsets(node);});@@ -397,12 +403,16 @@ export function expandToDepth(tree: TreeState, depth: number): TreeState {// Apply expansion based on depthpostOrder.forEach((node) => {- if (!node.isExpandable) return;-- const shouldExpand = node.depth < depth;- node.isExpanded = shouldExpand;- node.userExpand = shouldExpand;+ // Expansion decision only applies to expandable nodes.+ if (node.isExpandable) {+ const shouldExpand = node.depth < depth;+ node.isExpanded = shouldExpand;+ node.userExpand = shouldExpand;+ }+ // Recompute offsets for EVERY node (see applyExpansionState): post-order ++ // no-op-on-collapsed makes this safe, and it keeps the non-expandable+ // meta-root's visibleDescendantCount in sync with the virtualizer row count.recomputeNodeOffsets(node);});diff --git a/web/src/components/ui/AdvancedJsonViewer/utils/treeNavigation.clienttest.ts b/web/src/components/ui/AdvancedJsonViewer/utils/treeNavigation.clienttest.tsindex ccf24860de..62f0ec76b0 100644--- a/web/src/components/ui/AdvancedJsonViewer/utils/treeNavigation.clienttest.ts+++ b/web/src/components/ui/AdvancedJsonViewer/utils/treeNavigation.clienttest.ts@@ -1,6 +1,8 @@// @vitest-environment jsdomimport { buildTreeFromJSON } from "./treeStructure";+import { buildMultiSectionTree } from "./multiSectionTree";+import { applyExpansionState } from "./treeExpansion";import {getAllVisibleNodes,getNodeByIndex,@@ -507,6 +509,60 @@ describe("getNodeByIndex after expansion changes", () => {expect(failedIndexes).toEqual([]);});++ it("CRITICAL: multi-section — meta-root count stays in sync after collapsing a section", () => {+ // Regression for LANGFUSE-456 ("[getNodeByIndex] Index out of bounds").+ // The virtualizer's row count is driven by the synthetic meta-root's+ // visibleDescendantCount. applyExpansionState used to skip non-expandable+ // nodes, so the meta-root's cached count stayed over-counted after a section+ // collapsed, and the virtualizer requested phantom row indexes.+ const sections = [+ {+ key: "input",+ data: {+ messages: [+ { role: "user", content: "hello" },+ { role: "assistant", content: "hi there" },+ ],+ },+ },+ {+ key: "output",+ data: { result: { text: "answer", tokens: [1, 2, 3] } },+ },+ ];++ const tree = buildMultiSectionTree(sections);++ // Sanity: fully expanded on build, every index resolves.+ const initialRowCount = 1 + tree.rootNode.visibleDescendantCount;+ for (let i = 0; i < initialRowCount; i++) {+ expect(getNodeByIndex(tree.rootNode, i)).not.toBeNull();+ }++ // Re-apply an expansion state that collapses the first section — this is what+ // happens on load / when switching observations and the tree is rebuilt.+ applyExpansionState(tree, { input__header: false });++ const rowCount = 1 + tree.rootNode.visibleDescendantCount;++ // The meta-root's cached descendant count MUST equal the real navigable count.+ // Pre-fix this was over-counted (still included the collapsed section's rows).+ const navigableCount = getAllVisibleNodes(tree.rootNode).length - 1;+ expect(tree.rootNode.visibleDescendantCount).toBe(navigableCount);++ // Every index in [0, rowCount) must resolve to a non-null node.+ // Pre-fix: the trailing phantom indexes walked into the collapsed section+ // header and returned null.+ const failedIndexes: number[] = [];+ for (let i = 0; i < rowCount; i++) {+ const node = getNodeByIndex(tree.rootNode, i);+ if (node === null) failedIndexes.push(i);+ }++ expect(failedIndexes).toEqual([]);+ expect(failedIndexes.length).toBe(0);+ });});describe("Edge Cases", () => {