From f0e064aa1b99e56f7a19da3a1177a3ffa7649103 Mon Sep 17 00:00:00 2001 From: "exe.dev user" Date: Mon, 26 Oct 2026 15:12:36 +0000 Subject: [PATCH] Tweak run trace history loading and mobile layout --- .../src/features/runs/continuous-trace.tsx | 41 ++++++++++++++----- packages/app/test/runs.browser.test.mjs | 16 ++++---- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/app/src/features/runs/continuous-trace.tsx b/packages/app/src/features/runs/continuous-trace.tsx index d021e69f..98e950eb 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, 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"; @@ -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([]); const [expanded, setExpanded] = useState>(() => new Set()); + const [pullDistance, setPullDistance] = useState(0); + const [pullLoading, setPullLoading] = useState(false); const scrollRef = useRef(null); const followRef = useRef(true); const historyLoadingRef = useRef(false); + const pullStartY = useRef(null); const prependAnchor = useRef<{ height: number; top: number } | null>(null); const initialAlignedRevision = useRef(null); const trace = useInfiniteQuery, string[], PageParam>({ @@ -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) => { + if (scrollRef.current?.scrollTop === 0 && event.touches[0]) pullStartY.current = event.touches[0].clientY; + }; + const onTouchMove = (event: TouchEvent) => { + 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; @@ -118,7 +136,8 @@ export function ContinuousTrace({ runId, revision, active, onReset }: { runId: s }, []); const grouped = conversation(items); return {trace.error &&

{trace.error.message}

}{trace.isPending &&

Loading conversation…

} -
+
+ {(pullDistance > 0 || pullLoading) &&

{pullLoading ? "Loading older events…" : pullDistance >= 56 ? "Release to load older events" : `Pull to load older events · ${Math.round((pullDistance / 56) * 100)}%`}

} {!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 =>
)}
)}
diff --git a/packages/app/test/runs.browser.test.mjs b/packages/app/test/runs.browser.test.mjs index 0e562d6e..98d9ff62 100644 --- a/packages/app/test/runs.browser.test.mjs +++ b/packages/app/test/runs.browser.test.mjs @@ -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 } }); }); @@ -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");