From c153c4057fcd9fb2cac3ca44a4ba5af82d5dce31 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 16 Sep 2026 15:22:40 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=9C=20feat:=20Trace=20Viewer=20Steps,?= =?UTF-8?q?=20Sequence=20Scale=20and=20Previews=20(#16009)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ๐Ÿชœ feat: Trace Viewer Steps, Sequence Scale and Previews * ๐Ÿชœ fix: split tool-only rounds by run step; preview only a step's own roots * ๐Ÿชœ fix: keep windows and previews on the records they describe across pages, modes and failed wrappers * ๐Ÿชœ fix: keep step folds and previews honest across page boundaries and failed turns * ๐Ÿชœ fix: causal tie-breaks, final-only previews, span-shaped title runs, and truncating long names * ๐Ÿชœ fix: index previews in one pass, split agent handoffs, withhold parallel lanes * ๐Ÿชœ fix: skip the holes a streaming message leaves in its content * ๐Ÿชœ fix: reserve a round for compaction summaries; keep parallel-lane tools with their own model call * ๐Ÿชœ fix: record a handoff that starts with reasoning; look lanes up in a map * ๐Ÿชœ fix: per-lane leading steps, wrappers for turns without work yet, no final alignment for runs that ended early * ๐Ÿชœ fix: append to a step's per-tool bucket instead of copying it per call * fix: Preserve Trace Preview Alignment Across Run Boundaries * perf: Cache Trace Ancestors and Lane Branches --- client/src/components/Chat/Trace/Ledger.tsx | 319 ++++++--- client/src/components/Chat/Trace/Timeline.tsx | 76 +- client/src/components/Chat/Trace/Viewer.tsx | 159 ++++- .../Chat/Trace/__tests__/Viewer.test.tsx | 371 ++++++++-- .../Chat/Trace/__tests__/model.test.ts | 650 +++++++++++++++-- .../Chat/Trace/__tests__/preview.test.ts | 481 +++++++++++++ client/src/components/Chat/Trace/model.ts | 667 ++++++++++++++++-- client/src/components/Chat/Trace/preview.ts | 282 ++++++++ client/src/components/Chat/Trace/store.ts | 8 + client/src/locales/en/translation.json | 13 + packages/api/src/langfuse/reader.spec.ts | 1 + packages/api/src/langfuse/reader.ts | 37 +- packages/data-provider/src/types/traces.ts | 2 + 13 files changed, 2702 insertions(+), 364 deletions(-) create mode 100644 client/src/components/Chat/Trace/__tests__/preview.test.ts create mode 100644 client/src/components/Chat/Trace/preview.ts diff --git a/client/src/components/Chat/Trace/Ledger.tsx b/client/src/components/Chat/Trace/Ledger.tsx index 2631c73b86..e979cc082a 100644 --- a/client/src/components/Chat/Trace/Ledger.tsx +++ b/client/src/components/Chat/Trace/Ledger.tsx @@ -1,18 +1,29 @@ import { memo, useId, useMemo, useState } from 'react'; import { ChevronRight, CircleAlert, CircleDashed } from 'lucide-react'; import type { RefObject, CSSProperties, KeyboardEvent } from 'react'; -import type { TraceModel, TraceNode, TraceRow, TraceTurn, TraceWindow } from './model'; +import type { + TraceRow, + TraceNode, + TraceStep, + TraceTurn, + TraceSpan, + TraceModel, + TraceScale, + TraceWindow, +} from './model'; +import { spanOf, stepSpan, turnSpan, turnKey, boundsOf } from './model'; import { useTraceFormat, recordDurationText } from './format'; import { KIND_APPEARANCE, STATUS_LABEL } from './kinds'; import { formatTokens } from '~/utils/tokens'; import { useRowWindow } from './virtual'; import { useLocalize } from '~/hooks'; -import { turnKey } from './model'; import { cn } from '~/utils'; const ROW_HEIGHT = 32; const HEADER_HEIGHT = 28; const INDENT_PX = 14; +/** Tool names a step header lists before summarizing the rest as a count. */ +const STEP_TOOL_NAMES = 3; /** Shown only while the tree itself has keyboard focus, so a pointer user sees just the selection. */ const ACTIVE_RING = 'group-focus-visible/tree:ring-2 group-focus-visible/tree:ring-inset group-focus-visible/tree:ring-ring-primary'; @@ -21,17 +32,25 @@ const GRID = type Domain = { start: number; span: number }; -const barStyle = (start: number, end: number, domain: Domain): CSSProperties => ({ - left: `${((start - domain.start) / domain.span) * 100}%`, - width: `${((end - start) / domain.span) * 100}%`, +const barStyle = (span: TraceSpan, domain: Domain): CSSProperties => ({ + left: `${((span.start - domain.start) / domain.span) * 100}%`, + width: `${((span.end - span.start) / domain.span) * 100}%`, }); -function RecordBar({ node, domain }: { node: TraceNode; domain: Domain }) { +function RecordBar({ + node, + scale, + domain, +}: { + node: TraceNode; + scale: TraceScale; + domain: Domain; +}) { const { record } = node; const appearance = KIND_APPEARANCE[record.kind]; const solid = record.status === 'error' ? 'bg-status-error' : appearance.bar; - if (node.end == null) { + if (scale === 'time' && node.end == null) { return ( 0 ? (node.firstToken - node.start) / duration : 0; + scale === 'time' && node.firstToken != null && duration > 0 + ? (node.firstToken - node.start) / duration + : 0; return ( {ttftShare > 0 && ( ); } @@ -80,33 +102,37 @@ function recordTokens(node: TraceNode): string { return total > 0 ? formatTokens(total) : ''; } -const toDomain = (start: number, end: number): Domain => ({ - start, - span: Math.max(end - start, 1), +const toDomain = (span: TraceSpan): Domain => ({ + start: span.start, + span: Math.max(span.end - span.start, 1), }); /** - * The hierarchical record list: one row per turn and per record. Each bar is - * scaled to its own response, so a short turn in a long conversation stays - * legible, until an interval is focused and every row shares that scale. Rows - * are windowed, and the tree follows the ARIA tree pattern through + * The hierarchical record list: one row per turn, per step and per record. Each + * bar is scaled to its own response, so a short turn in a long conversation + * stays legible, until an interval is focused and every row shares that scale. + * Rows are windowed, and the tree follows the ARIA tree pattern through * `aria-activedescendant`, which must name a mounted row, so the active row * stays rendered while the window scrolls away from it. */ function Ledger({ rows, model, + scale, view, selectedId, treeRef, + previewFor, onSelect, onToggle, }: { rows: TraceRow[]; model: TraceModel; + scale: TraceScale; view: TraceWindow | null; selectedId: string | null; treeRef: RefObject; + previewFor: (node: TraceNode) => string | undefined; onSelect: (id: string) => void; onToggle: (key: string) => void; }) { @@ -121,15 +147,18 @@ function Ledger({ const indexByKey = useMemo(() => new Map(rows.map((row, index) => [row.key, index])), [rows]); const turnDomains = useMemo( - () => new Map(model.turns.map((turn) => [turn.messageId, toDomain(turn.start, turn.end)])), - [model], + () => new Map(model.turns.map((turn) => [turn.messageId, toDomain(turnSpan(turn, scale))])), + [model, scale], ); - const viewDomain = useMemo(() => (view ? toDomain(view.start, view.end) : null), [view]); + const viewDomain = useMemo(() => (view ? toDomain(view) : null), [view]); + const bounds = boundsOf(model, scale); const domainFor = (messageId: string): Domain => - viewDomain ?? turnDomains.get(messageId) ?? toDomain(model.start, model.end); + viewDomain ?? turnDomains.get(messageId) ?? toDomain(bounds); const activeIndex = activeKey != null ? (indexByKey.get(activeKey) ?? 0) : 0; const activeRow = rows[activeIndex]; const rowId = (key: string) => `${idPrefix}-${key}`; + const position = (value: number) => + scale === 'sequence' ? String(Math.round(value)) : format.duration(value - bounds.start); /** Keeps a keyboard-moved row inside the viewport so its windowed element mounts. */ const scrollIntoView = (index: number) => { @@ -163,11 +192,23 @@ function Ledger({ } }; + const parentKeyOf = (row: TraceRow): string | undefined => { + if (row.type === 'turn') { + return undefined; + } + if (row.type === 'step') { + return turnKey(row.step.messageId); + } + const { node } = row; + const stepParent = model.mode === 'simple' ? node.stepKey : null; + return node.viewParentId ?? stepParent ?? turnKey(node.record.messageId); + }; + const handleKeyDown = (event: KeyboardEvent) => { if (!activeRow) { return; } - const expandable = activeRow.type === 'turn' || activeRow.hasChildren; + const expandable = activeRow.type !== 'record' || activeRow.hasChildren; const pageSize = Math.max(1, Math.floor((scrollRef.current?.clientHeight ?? 0) / ROW_HEIGHT)); const handlers: Record void> = { ArrowDown: () => moveTo(activeIndex + 1), @@ -188,13 +229,10 @@ function Ledger({ onToggle(activeRow.key); return; } - if (activeRow.type === 'record') { - const { node } = activeRow; - const parentKey = node.parentId ?? turnKey(node.record.messageId); - const parentIndex = indexByKey.get(parentKey); - if (parentIndex != null) { - moveTo(parentIndex); - } + const parentKey = parentKeyOf(activeRow); + const parentIndex = parentKey != null ? indexByKey.get(parentKey) : undefined; + if (parentIndex != null) { + moveTo(parentIndex); } }, Enter: () => activate(activeRow), @@ -207,61 +245,142 @@ function Ledger({ } }; - const renderRow = (row: TraceRow, index: number) => { - const style: CSSProperties = { top: HEADER_HEIGHT + index * ROW_HEIGHT, height: ROW_HEIGHT }; - const active = activeRow?.key === row.key; - const common = { - id: rowId(row.key), - role: 'treeitem', - 'aria-posinset': row.position, - 'aria-setsize': row.setSize, - style, - onClick: () => activate(row), - }; + const chevron = (expanded: boolean) => ( +