πͺ’ 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
refactor(trace-graph-view): deterministic data-derived viewport (LFE-10675) (#14782)
Nikita Kabardin97123b0d7381
diff --git a/web/src/features/trace-graph-view/README.md b/web/src/features/trace-graph-view/README.mdindex aba0fc5e5a..576830a53d 100644--- a/web/src/features/trace-graph-view/README.md+++ b/web/src/features/trace-graph-view/README.md@@ -20,10 +20,15 @@ agentGraphData (tRPC getAgentGraphData)## Ownership-- **Viewport**: the d3-zoom transform is the single source of truth. Per-frame- pan/zoom writes the world div's CSS transform (and the edge- stroke-compensation var) imperatively β React state holds only the discrete- derivations (`compact` label threshold, `fitted` reveal, layout/error).+- **Viewport**: deterministic and data-derived β the rendered transform is+ always `userOverride ?? fit(layout, size)`. The user's last gesture+ (drag/wheel/pinch/toolbar zoom) is the ONLY viewport state; without one, fit+ re-applies on every layout/size change. Selection never moves the viewport+ (it's a ring/glow β under fit the node is always visible); Fit and a graph+ change clear the override. Per-frame pan/zoom writes the world div's CSS+ transform (and the edge stroke-compensation var) imperatively β React state+ holds only the discrete derivations (`compact` label threshold, `fitted`+ reveal, layout/error).- **Selection**: the `?observation=` URL param, wired in`components/TraceGraphView.tsx` (click-cycling through a node's observations,URLβnode sync with a parent-walk fallback for descendants without their owndiff --git a/web/src/features/trace-graph-view/components/ElkGraphRenderer.tsx b/web/src/features/trace-graph-view/components/ElkGraphRenderer.tsxindex 7028871b41..5a6e62ea68 100644--- a/web/src/features/trace-graph-view/components/ElkGraphRenderer.tsx+++ b/web/src/features/trace-graph-view/components/ElkGraphRenderer.tsx@@ -43,9 +43,6 @@ const MAX_FIT_SCALE = 1.2;const ZOOM_STEP = 1.4;// Below this scale labels are unreadable noise β show only node shape + icon.const LABEL_HIDE_SCALE = 0.5;-// One consistent scale to reveal a selection from a zoomed-out overview, so-// navigating between nodes pans rather than re-zooms (no "zoom jumping").-const SELECTION_REVEAL_SCALE = 0.9;const CLICK_MOVE_THRESHOLD = 4; // px; beyond this a pointerup is a drag, not a clickfunction toPath(points: { x: number; y: number }[]): string {@@ -92,19 +89,15 @@ export const ElkGraphRenderer: React.FC<ElkGraphRendererProps> = ({null,undefined> | null>(null);- // What framed the current view. Programmatic framings ("fit", "focus") stay- // LIVE: when the container resizes (peek panel opening/settling, divider- // drag, window resize) they re-apply against the new size β otherwise a- // deep-link focus computed against a transient early size stays frozen and- // the focused node ends up cropped/off-view. Only a real user gesture- // ("gesture") freezes the view.- const framingRef = useRef<"fit" | "focus" | "gesture">("fit");- // The node id (or null for an empty click) of the last in-canvas selection, so- // the focus effect re-frames only for selections from the tree/timeline.- const lastCanvasClickRef = useRef<string | null | undefined>(undefined);- // The selection last reflected by the focus effect, so a resize (which also- // re-fires that effect) doesn't re-frame a still-selected node.- const prevSelectedRef = useRef<string | null>(null);+ // THE deterministic viewport model: the rendered transform is always+ // overrideRef.current ?? fit(layout, size)+ // The user's last explicit viewport (drag/wheel/pinch/toolbar zoom) is the+ // ONLY real state; everything else derives. No override β the fit re-applies+ // on every layout/size change (peek panels settling, divider drags, window+ // resizes can never leave a stale frame). Selection does NOT move the+ // viewport β it's a ring/glow, and under fit the node is always visible.+ // Fit button and a graph change clear the override.+ const overrideRef = useRef<Transform | null>(null);const pointerDownPos = useRef<{ x: number; y: number } | null>(null);const [layout, setLayout] = useState<GraphLayout | null>(null);@@ -148,10 +141,8 @@ export const ElkGraphRenderer: React.FC<ElkGraphRendererProps> = ({setLayout(null);setLayoutError(false);setFitted(false);- framingRef.current = "fit";- // Also clear cross-graph view state so a graph change re-focuses the- // selected node on the new instance and drops stale hover highlighting.- prevSelectedRef.current = null;+ // A new graph gets a fresh fit; stale hover highlighting drops too.+ overrideRef.current = null;setHoveredId(null);computeGraphLayout(graph, nodeToObservationsMap).then((result) => {@@ -188,10 +179,10 @@ export const ElkGraphRenderer: React.FC<ElkGraphRendererProps> = ({const zoomBehavior = createZoom<HTMLDivElement, unknown>().scaleExtent([SCALE_MIN, SCALE_MAX]).on("zoom", (event: D3ZoomEvent<HTMLDivElement, unknown>) => {- // sourceEvent is set only for real gestures (drag/wheel/pinch) β- // programmatic transforms (fit/focus) keep their live framing kind.- if (event.sourceEvent) framingRef.current = "gesture";const { x, y, k } = event.transform;+ // sourceEvent is set only for real gestures (drag/wheel/pinch) β they+ // become the override; programmatic fits keep deriving.+ if (event.sourceEvent) overrideRef.current = { x, y, k };transformRef.current = { x, y, k };const world = worldRef.current;if (world) {@@ -265,99 +256,19 @@ export const ElkGraphRenderer: React.FC<ElkGraphRendererProps> = ({};}, [layout, size]);- // Live "fit" framing: re-fit on layout/size changes until a focus or a user- // gesture takes over.+ // THE framing effect β the whole model. The viewport derives from data:+ // no override β fit(layout, size), re-applied on every layout/size change+ // (deterministic; a peek panel settling late or a divider drag can never+ // strand a stale frame). With an override (user gesture) the view is the+ // user's β untouched until Fit or a graph change clears it.useEffect(() => {- if (framingRef.current !== "fit") return;+ if (overrideRef.current) return;const fit = computeFit();if (fit) applyTransform(fit);}, [computeFit, applyTransform]);- // Reflect external selection (tree/timeline): bring the node into view at the- // current zoom (or one reveal scale when zoomed out). In-canvas clicks don't- // move the view; selecting the root/empty returns to the full-graph overview.- // While the view is focus-framed, container size changes re-center the node- // against the NEW size (translate only β the zoom stays put), so a peek panel- // that finishes laying out after a deep-link focus, or a divider drag, can't- // leave the focused node cropped against a stale frame.- useEffect(() => {- const cameFromClick = lastCanvasClickRef.current === selectedNodeName;- lastCanvasClickRef.current = undefined;- // Wait for layout/size before acting β and don't mark the selection as- // reflected yet, so we retry once they're ready (e.g. a deep link with a- // preselected observation that arrives before the graph lays out).- if (!layout || size.width === 0) return;-- // Read d3-zoom's synchronously-updated scale (set by applyTransform before- // React re-renders), not a state mirror that's stale in the same commit.- const currentK = containerRef.current- ? zoomTransform(containerRef.current).k- : 1;- // Clamped focus framing: aim the node at the viewport center, but never- // leave blank canvas the graph could cover β on an axis where the scaled- // graph is larger than the viewport, clamp the pan to the graph bounds- // (a node near the graph's edge sits off-center rather than dragging- // emptiness in); where the graph is smaller, just center the whole graph.- // Without this, deep-link-focusing a node near the top of a small graph- // pushed the rest out of the panel while half the canvas sat empty.- const centerNode = (nodeId: string, k: number): boolean => {- const node = layout.nodes.find((n) => n.id === nodeId);- if (!node) return false;- const cx = node.x + node.width / 2;- const cy = node.y + node.height / 2;- const graphW = layout.width * k;- const graphH = layout.height * k;- const x =- graphW <= size.width- ? (size.width - graphW) / 2- : Math.min(0, Math.max(size.width - graphW, size.width / 2 - cx * k));- const y =- graphH <= size.height- ? (size.height - graphH) / 2- : Math.min(- 0,- Math.max(size.height - graphH, size.height / 2 - cy * k),- );- applyTransform({ k, x, y });- return true;- };-- if (prevSelectedRef.current === selectedNodeName) {- // No selection change β this run is a size/layout change. Keep a live- // focus framing honest by re-centering at the current zoom; leave user- // gestures alone ("fit" re-applies via the auto-fit effect above).- if (framingRef.current === "focus" && selectedNodeName) {- centerNode(selectedNodeName, currentK);- }- return;- }- prevSelectedRef.current = selectedNodeName;- // In-canvas clicks already selected the node; don't move the view. A- // leftover "focus" framing must be demoted to "gesture" so a later- // container resize can't re-center onto the clicked node β but a live- // "fit" framing stays live (clicking a node in a fitted overview should- // not kill auto-fit-on-resize).- if (cameFromClick) {- if (framingRef.current === "focus") framingRef.current = "gesture";- return;- }-- if (!selectedNodeName) {- framingRef.current = "fit";- const fit = computeFit();- if (fit) applyTransform(fit);- return;- }-- const k = currentK >= LABEL_HIDE_SCALE ? currentK : SELECTION_REVEAL_SCALE;- if (centerNode(selectedNodeName, k)) {- framingRef.current = "focus";- }- }, [selectedNodeName, layout, size, computeFit, applyTransform]);-const handleSelect = useCallback((id: string) => {- lastCanvasClickRef.current = id;onCanvasNodeNameChange?.(id);},[onCanvasNodeNameChange],@@ -373,7 +284,6 @@ export const ElkGraphRenderer: React.FC<ElkGraphRendererProps> = ({) {return;}- lastCanvasClickRef.current = null;onCanvasNodeNameChange?.(null);};@@ -381,14 +291,17 @@ export const ElkGraphRenderer: React.FC<ElkGraphRendererProps> = ({const selection = selectionRef.current;const zoomBehavior = zoomRef.current;if (!selection || !zoomBehavior) return;- // Toolbar zoom is a user decision β freeze the framing like a gesture- // (scaleBy goes through d3 without a sourceEvent, so set it explicitly).- framingRef.current = "gesture";zoomBehavior.scaleBy(selection, factor);+ // Toolbar zoom is a user decision β it becomes the override (scaleBy goes+ // through d3 without a sourceEvent, so record it explicitly).+ if (containerRef.current) {+ const { x, y, k } = zoomTransform(containerRef.current);+ overrideRef.current = { x, y, k };+ }};const handleFit = () => {- framingRef.current = "fit";+ overrideRef.current = null;const fit = computeFit();if (fit) applyTransform(fit);};