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
41 changes: 30 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, useLayoutEffect, useMemo, useRef, useState } from "react";
import { useEffect, useLayoutEffect, useMemo, useRef, useState, type TouchEvent } from "react";
import { useInfiniteQuery, useQuery, type InfiniteData } from "@tanstack/react-query";
import { api } from "factorize-api-client";
import { messageOf } from "../auth/session";
Expand Down Expand Up @@ -28,9 +28,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 [expanded, setExpanded] = useState<Set<string>>(() => new Set());
const [pullDistance, setPullDistance] = useState(0);
const [pullLoading, setPullLoading] = useState(false);
const scrollRef = useRef<HTMLDivElement>(null);
const followRef = useRef(true);
const historyLoadingRef = useRef(false);
const pullStartY = useRef<number | null>(null);
const prependAnchor = useRef<{ height: number; top: number } | null>(null);
const initialAlignedRevision = useRef<string | null>(null);
const trace = useInfiniteQuery<TracePage, Error, InfiniteData<TracePage>, string[], PageParam>({
Expand Down Expand Up @@ -67,20 +70,35 @@ export function ContinuousTrace({ runId, revision, active, onReset }: { runId: s
const bottom = element.scrollHeight - element.scrollTop - element.clientHeight <= 48;
followRef.current = bottom;
};
const loadHistory = () => {
const loadHistory = async (pageCount = 1) => {
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; });
try {
for (let page = 0; page < pageCount; page += 1) {
const result = await trace.fetchNextPage();
if (!result.data?.pages.at(-1)?.previousCursor) break;
}
} finally {
historyLoadingRef.current = false;
}
};
const onTouchStart = (event: TouchEvent<HTMLDivElement>) => {
if (scrollRef.current?.scrollTop === 0 && event.touches[0]) pullStartY.current = event.touches[0].clientY;
};
const onTouchMove = (event: TouchEvent<HTMLDivElement>) => {
if (pullStartY.current === null || !event.touches[0]) return;
const distance = Math.max(0, event.touches[0].clientY - pullStartY.current);
setPullDistance(Math.min(distance, 96));
};
const onTouchEnd = () => {
const shouldLoad = pullDistance >= 56 && !pullLoading;
pullStartY.current = null;
if (!shouldLoad) { setPullDistance(0); return; }
setPullLoading(true);
void loadHistory(3).finally(() => { setPullLoading(false); setPullDistance(0); });
};
useEffect(() => {
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;
Expand Down Expand Up @@ -118,7 +136,8 @@ export function ContinuousTrace({ runId, revision, active, onReset }: { runId: s
}, []);
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>}
<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}>
<div ref={scrollRef} data-run-trace-scroll aria-label="Run trace" className="-mx-4 h-[min(60rem,calc(100svh-18rem))] min-h-64 w-[calc(100%+2rem)] min-w-0 overflow-y-auto overscroll-contain rounded-none border-0 p-0 dark:border-slate-800 sm:mx-0 sm:h-[min(60rem,calc(100svh-18rem))] sm:w-auto sm:rounded-lg sm:border sm:border-stone-200 sm:p-4" onScroll={updateScrollState} onTouchStart={onTouchStart} onTouchMove={onTouchMove} onTouchEnd={onTouchEnd} onTouchCancel={onTouchEnd}>
{(pullDistance > 0 || pullLoading) && <p data-run-trace-pull role="status" aria-live="polite" className="sticky top-0 z-10 -mb-8 flex h-8 items-center justify-center bg-stone-50/90 text-xs font-medium text-slate-600 backdrop-blur dark:bg-slate-950/90 dark:text-slate-400">{pullLoading ? "Loading older events…" : pullDistance >= 56 ? "Release to load older events" : `Pull to load older events · ${Math.round((pullDistance / 56) * 100)}%`}</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="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>
Expand Down
16 changes: 8 additions & 8 deletions packages/app/test/runs.browser.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ 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 () => {
test("run trace uses pull-to-load history, preserves the anchor, 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));
const olderPages = [Array.from({ length: 10 }, (_, index) => event(index + 1)), Array.from({ length: 10 }, (_, index) => event(index + 27)), Array.from({ length: 10 }, (_, index) => event(index + 37))];
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("before")) { const page = requests.filter(request => request.searchParams.has("before")).length - 1; return route.fulfill({ json: { items: olderPages[page], nextCursor: null, previousCursor: page < olderPages.length - 1 ? page + 1 : 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 } });
});
Expand All @@ -140,13 +140,13 @@ test("run trace owns scrolling, preserves the history anchor, exhausts history,
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);
await scroller.evaluate(element => { element.scrollTop = 0; const event = new Event("touchstart", { bubbles: true }); Object.defineProperty(event, "touches", { value: [{ clientY: 100 }] }); element.dispatchEvent(event); });
await scroller.evaluate(element => { const event = new Event("touchmove", { bubbles: true }); Object.defineProperty(event, "touches", { value: [{ clientY: 180 }] }); element.dispatchEvent(event); });
await scroller.evaluate(element => { const event = new Event("touchend", { bubbles: true }); Object.defineProperty(event, "touches", { value: [] }); element.dispatchEvent(event); });
await page.waitForFunction(() => document.querySelectorAll("[data-run-trace] article").length >= 44);
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.equal(requests.filter(url => url.searchParams.has("before")).length, 3, "a pull should load several history pages");
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");
Expand Down
Loading