Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 65 additions & 11 deletions packages/app/src/features/runs/continuous-trace.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<Event[]>([]);
const [atBottom, setAtBottom] = useState(true);
const [expanded, setExpanded] = useState<Set<string>>(() => new Set());
const anchor = useRef<{ height: number; top: number } | null>(null);
const scrollRef = useRef<HTMLDivElement>(null);
const followRef = useRef(true);
const historyLoadingRef = useRef(false);
const prependAnchor = useRef<{ height: number; top: number } | null>(null);
const initialAlignedRevision = useRef<string | null>(null);
const trace = useInfiniteQuery<TracePage, Error, InfiniteData<TracePage>, string[], PageParam>({
queryKey: ["runs", runId, "conversation-trace", revision],
initialPageParam: { mode: "recent" } as PageParam,
Expand Down Expand Up @@ -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 <Card className="border-0 p-0 shadow-none">{trace.error && <p role="alert">{trace.error.message}</p>}{trace.isPending && <p role="status">Loading conversation…</p>}
{!trace.isPending && !grouped.length && <p className="text-sm text-slate-600 dark:text-slate-400">No assistant messages yet.</p>}
<div data-run-trace className="min-w-0 space-y-4" aria-live={active ? "polite" : undefined}>{grouped.map((item, index) => item.kind === "assistant" ? <article key={item.event.id} className="min-w-0"><EventBody event={item.event} /></article> : <Disclosure key={item.events[0]?.id + ":" + index} className={traceClasses("tool_call")} open={expanded.has(item.events[0]?.id + ":" + index)} onToggle={event => { 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; }); }}><Summary>{item.count} Tool Call{item.count === 1 ? "" : "s"}</Summary><div className="space-y-3">{item.events.map(event => <div key={event.id}><EventBody event={event} /></div>)}</div></Disclosure>)}</div>
<div ref={scrollRef} data-run-trace-scroll aria-label="Run trace" className="h-[min(60rem,calc(100dvh-18rem))] min-h-64 min-w-0 overflow-y-auto overscroll-contain rounded-lg border border-stone-200 p-4 dark:border-slate-800" onScroll={updateScrollState}>
{!trace.isPending && !grouped.length && <p className="text-sm text-slate-600 dark:text-slate-400">No assistant messages yet.</p>}
<div data-run-trace className="flex min-h-full min-w-0 flex-col justify-end space-y-4" aria-live={active ? "polite" : undefined}>{grouped.map((item, index) => item.kind === "assistant" ? <article key={item.event.id} className="min-w-0"><EventBody event={item.event} /></article> : <Disclosure key={item.events[0]?.id + ":" + index} className={traceClasses("tool_call")} open={expanded.has(item.events[0]?.id + ":" + index)} onToggle={event => { 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; }); }}><Summary>{item.count} Tool Call{item.count === 1 ? "" : "s"}</Summary><div className="space-y-3">{item.events.map(event => <div key={event.id}><EventBody event={event} /></div>)}</div></Disclosure>)}</div>
</div>
{active && <p role="status" className="mt-5 text-sm font-medium"><span aria-hidden="true" className="mr-2 inline-block motion-safe:animate-pulse">●</span>Running…</p>}
</Card>;
}
2 changes: 1 addition & 1 deletion packages/app/src/features/runs/detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function RunDetail() {
<ExpandedRunDetail runId={runId!} />
{status.data && !active ? <ReplayTrace runId={runId!} /> : <p className="my-4">Replay is available after the run finishes and trace finalization completes.</p>}
</section>
<section role="tabpanel" id="run-panel-trace" aria-labelledby="run-tab-trace" hidden={tab !== "trace"} tabIndex={0}>
<section role="tabpanel" id="run-panel-trace" aria-labelledby="run-tab-trace" hidden={tab !== "trace"} tabIndex={0} className="flex min-h-0 flex-col">
<h2 className="my-4 text-xl font-semibold">Trace</h2>
<ContinuousTrace key={revision} runId={runId!} revision={revision} active={active} onReset={next => { if (next === revision) return; void cache.cancelQueries({ queryKey: ["runs", runId, "continuous-trace"] }); cache.removeQueries({ queryKey: ["runs", runId, "continuous-trace"] }); setRevision(next); cache.setQueryData(runStatusQuery(runId!).queryKey, previous => previous ? { ...previous, trace_revision: next } : previous); cache.removeQueries({ queryKey: ["runs", runId, "continuous-tail"] }); }} />
</section>
Expand Down
31 changes: 31 additions & 0 deletions packages/app/test/runs.browser.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,34 @@ test("stopping a run submits once, observes stopping state and keeps terminal fi
await page.getByText("stopped · Finalizing trace and artifacts", { exact: true }).waitFor(); assert.equal(await page.getByRole("button", { name: "Stop run", exact: true }).count(), 0);
await context.close();
});
test("run trace owns scrolling, preserves the history anchor, exhausts history, and never crashes on disclosure toggles", async () => {
const { context, page } = await setup(); const requests = [];
const event = (sequence, type = "assistant_message") => ({ id: `trace-${sequence}`, sequence, type, title: `Trace ${sequence}`, preview: `Trace ${sequence}`, display: {} });
const recent = [...Array.from({ length: 14 }, (_, index) => event(index + 11)), event(25, "tool_call"), event(26, "tool_result")];
const older = Array.from({ length: 10 }, (_, index) => event(index + 1));
await page.route(`**/api/v1/runs/${runId}/status`, route => route.fulfill({ json: { id: runId, job_id: jobId, job_name: "Job", run_name: "Run", state: "succeeded", finalizing: false, trace_revision: "rev", artifact_state: "stored", created_at: time, updated_at: time, started_at: time, destination_url: null } }));
await page.route(`**/api/v1/runs/${runId}/trace-pages?**`, route => {
const url = new URL(route.request().url()); requests.push(url);
if (url.searchParams.has("before")) return route.fulfill({ json: { items: older, nextCursor: null, previousCursor: null, revision: "rev", reset: false } });
if (url.searchParams.has("after")) return route.fulfill({ json: { items: [], nextCursor: null, previousCursor: null, revision: "rev", reset: false } });
return route.fulfill({ json: { items: recent, nextCursor: null, previousCursor: 10, revision: "rev", reset: false } });
});
await page.goto(origin + "/job-runs/" + runId); const scroller = page.locator("[data-run-trace-scroll]");
await page.getByText("Trace 24", { exact: true }).waitFor();
await page.getByText("1 Tool Call", { exact: true }).click();
assert.equal(await page.getByText("Cannot read properties of null", { exact: false }).count(), 0);
const initial = await scroller.evaluate(element => ({ top: element.scrollTop, height: element.scrollHeight, client: element.clientHeight }));
assert.ok(initial.height > initial.client && initial.top > 0, "initial trace should be bottom-aligned inside its own scroller");
const before = await scroller.evaluate(element => element.scrollHeight);
await scroller.evaluate(element => { element.scrollTop = 0; });
await page.waitForFunction(() => document.querySelectorAll("[data-run-trace] article").length >= 24);
const after = await scroller.evaluate(element => ({ top: element.scrollTop, height: element.scrollHeight }));
assert.ok(requests.some(url => url.searchParams.has("before")), "scrolling to the top should request history");
assert.ok(after.top > 0 && after.height > before, "prepending history should preserve the visible anchor");
await scroller.evaluate(element => { element.scrollTop = 0; }); await page.waitForTimeout(100);
assert.equal(requests.filter(url => url.searchParams.has("before")).length, 1, "exhausted history must not loop");
await page.setViewportSize({ width: 900, height: 700 }); const shortHeight = await scroller.evaluate(element => element.clientHeight);
await page.setViewportSize({ width: 900, height: 900 }); const tallHeight = await scroller.evaluate(element => element.clientHeight);
assert.ok(tallHeight > shortHeight, "trace viewport should resize with the window");
await context.close();
});
Loading