From 1f1b1860517628bb4bef0b195087db14b06afcc3 Mon Sep 17 00:00:00 2001 From: "exe.dev user" Date: Mon, 26 Oct 2026 14:40:43 +0000 Subject: [PATCH] Fix run trace scrolling and history anchoring --- .../src/features/runs/continuous-trace.tsx | 76 ++++++++++++++++--- packages/app/src/features/runs/detail.tsx | 2 +- packages/app/test/runs.browser.test.mjs | 31 ++++++++ 3 files changed, 97 insertions(+), 12 deletions(-) diff --git a/packages/app/src/features/runs/continuous-trace.tsx b/packages/app/src/features/runs/continuous-trace.tsx index 6a59acfa..d021e69f 100644 --- a/packages/app/src/features/runs/continuous-trace.tsx +++ b/packages/app/src/features/runs/continuous-trace.tsx @@ -1,6 +1,6 @@ import { traceClasses, EventBody } from "./event-body"; import { Disclosure, Summary, Card } from "../../shared/ui"; -import { useEffect, useMemo, useRef, useState } from "react"; +import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { useInfiniteQuery, useQuery, type InfiniteData } from "@tanstack/react-query"; import { api } from "factorize-api-client"; import { messageOf } from "../auth/session"; @@ -27,9 +27,12 @@ function conversation(events: Event[]): ConversationItem[] { export function ContinuousTrace({ runId, revision, active, onReset }: { runId: string; revision: string; active: boolean; onReset: (revision: string) => void }) { const [liveItems, setLiveItems] = useState([]); - const [atBottom, setAtBottom] = useState(true); const [expanded, setExpanded] = useState>(() => new Set()); - const anchor = useRef<{ height: number; top: number } | null>(null); + const scrollRef = useRef(null); + const followRef = useRef(true); + const historyLoadingRef = useRef(false); + const prependAnchor = useRef<{ height: number; top: number } | null>(null); + const initialAlignedRevision = useRef(null); const trace = useInfiniteQuery, string[], PageParam>({ queryKey: ["runs", runId, "conversation-trace", revision], initialPageParam: { mode: "recent" } as PageParam, @@ -57,17 +60,68 @@ export function ContinuousTrace({ runId, revision, active, onReset }: { runId: s useEffect(() => { const changed = live.data?.reset ? live.data : pages.find(page => page.reset || page.revision !== revision); if (changed) onReset(changed.revision); }, [live.data, pages, revision, onReset]); useEffect(() => { if (live.data && !live.data.reset && live.data.revision === revision) setLiveItems(previous => [...previous, ...live.data!.items]); }, [live.data, revision]); useEffect(() => { setLiveItems([]); setExpanded(new Set()); }, [revision]); + useEffect(() => { initialAlignedRevision.current = null; followRef.current = true; }, [revision]); + const updateScrollState = () => { + const element = scrollRef.current; + if (!element) return; + const bottom = element.scrollHeight - element.scrollTop - element.clientHeight <= 48; + followRef.current = bottom; + }; + const loadHistory = () => { + const element = scrollRef.current; + if (!element || !trace.hasNextPage || trace.isFetching || historyLoadingRef.current) return; + historyLoadingRef.current = true; + prependAnchor.current = { height: element.scrollHeight, top: element.scrollTop }; + void trace.fetchNextPage().finally(() => { historyLoadingRef.current = false; }); + }; useEffect(() => { - const onScroll = () => { const bottom = window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 48; setAtBottom(bottom); if (window.scrollY < 160 && trace.hasNextPage && !trace.isFetching) { anchor.current = { height: document.documentElement.scrollHeight, top: window.scrollY }; void trace.fetchNextPage(); } }; - window.addEventListener("scroll", onScroll, { passive: true }); return () => window.removeEventListener("scroll", onScroll); - }, [trace.hasNextPage, trace.isFetching, trace.fetchNextPage]); - useEffect(() => { if (!anchor.current || trace.isFetching) return; const previous = anchor.current; anchor.current = null; window.scrollTo({ top: previous.top + document.documentElement.scrollHeight - previous.height, behavior: "auto" }); }, [items.length, trace.isFetching]); - useEffect(() => { if (atBottom && live.data?.items.length) window.scrollTo({ top: document.documentElement.scrollHeight, behavior: "smooth" }); }, [atBottom, live.data]); - useEffect(() => { if (trace.data && !trace.isFetching && pages.length === 1 && items.length) window.scrollTo({ top: document.documentElement.scrollHeight, behavior: "auto" }); }, [trace.data, trace.isFetching]); + const element = scrollRef.current; + if (!element) return; + const onScroll = () => { updateScrollState(); if (element.scrollTop <= 160) loadHistory(); }; + element.addEventListener("scroll", onScroll, { passive: true }); + return () => element.removeEventListener("scroll", onScroll); + }); + useLayoutEffect(() => { + const element = scrollRef.current; + const previous = prependAnchor.current; + if (!element || !previous) return; + prependAnchor.current = null; + requestAnimationFrame(() => { + const current = scrollRef.current; + if (current) current.scrollTop = previous.top + current.scrollHeight - previous.height; + }); + }, [pages.length, items.length]); + useLayoutEffect(() => { + const element = scrollRef.current; + if (!element || !trace.data || trace.isFetching || !items.length) return; + if (pages.length === 1 && !prependAnchor.current && initialAlignedRevision.current !== revision) { + initialAlignedRevision.current = revision; + element.scrollTop = element.scrollHeight; + } + }, [trace.data, trace.isFetching, pages.length, items.length, revision]); + useLayoutEffect(() => { + const element = scrollRef.current; + if (!element || !live.data?.items.length || !followRef.current) return; + element.scrollTop = element.scrollHeight; + }, [live.data, items.length]); + useEffect(() => { + const element = scrollRef.current; + if (!element) return; + const resize = () => { + if (followRef.current) element.scrollTop = element.scrollHeight; + else updateScrollState(); + }; + window.addEventListener("resize", resize); + const observer = new ResizeObserver(resize); + observer.observe(element); + return () => { window.removeEventListener("resize", resize); observer.disconnect(); }; + }, []); const grouped = conversation(items); return {trace.error &&

{trace.error.message}

}{trace.isPending &&

Loading conversation…

} - {!trace.isPending && !grouped.length &&

No assistant messages yet.

} -
{grouped.map((item, index) => item.kind === "assistant" ?
: { const key = item.events[0]?.id + ":" + index; setExpanded(previous => { const next = new Set(previous); event.currentTarget.open ? next.add(key) : next.delete(key); return next; }); }}>{item.count} Tool Call{item.count === 1 ? "" : "s"}
{item.events.map(event =>
)}
)}
+
+ {!trace.isPending && !grouped.length &&

No assistant messages yet.

} +
{grouped.map((item, index) => item.kind === "assistant" ?
: { const key = item.events[0]?.id + ":" + index; const open = event.currentTarget.open; setExpanded(previous => { const next = new Set(previous); open ? next.add(key) : next.delete(key); return next; }); }}>{item.count} Tool Call{item.count === 1 ? "" : "s"}
{item.events.map(event =>
)}
)}
+
{active &&

Running…

}
; } diff --git a/packages/app/src/features/runs/detail.tsx b/packages/app/src/features/runs/detail.tsx index 17e2f394..f1075fa2 100644 --- a/packages/app/src/features/runs/detail.tsx +++ b/packages/app/src/features/runs/detail.tsx @@ -68,7 +68,7 @@ export function RunDetail() { {status.data && !active ? :

Replay is available after the run finishes and trace finalization completes.

} -