From f5c359476b63a874fb45f06d11729d0948124592 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Fri, 6 Nov 2026 15:55:51 +0800 Subject: [PATCH] fix(webui): distinguish unknown history from exhaustion --- webui/browser/history-boundary-fixture.tsx | 3 +- webui/browser/history-hydration.spec.ts | 52 +++++++++++++++++++ webui/src/components/thread/ThreadShell.tsx | 2 + .../src/components/thread/ThreadViewport.tsx | 24 +++++++-- webui/src/hooks/useNanobotStream.ts | 3 +- webui/src/lib/thread-transcript.ts | 5 +- webui/src/tests/thread-transcript.test.ts | 19 +++++++ 7 files changed, 99 insertions(+), 9 deletions(-) diff --git a/webui/browser/history-boundary-fixture.tsx b/webui/browser/history-boundary-fixture.tsx index 76d316e11..2b3ccff15 100644 --- a/webui/browser/history-boundary-fixture.tsx +++ b/webui/browser/history-boundary-fixture.tsx @@ -27,7 +27,8 @@ export function HistoryBoundaryFixture({ local = false, navigate = false, tasks History boundary fixture} /> diff --git a/webui/browser/history-hydration.spec.ts b/webui/browser/history-hydration.spec.ts index 1c708b26c..c3a2b5789 100644 --- a/webui/browser/history-hydration.spec.ts +++ b/webui/browser/history-hydration.spec.ts @@ -222,6 +222,58 @@ test("pre-request compaction stays in place through retries and precedes the ans backend.healthy(); }); +for (const inputMethod of ["wheel", "touch"] as const) { + test(`keeps unresolved history retryable after live-first delivery (${inputMethod})`, async ({ page }) => { + const { backend, body } = await openFixture(page, inputMethod === "touch" ? 390 : 1440); + const initial = await backend.read(); + await backend.send(delta(5, LIVE)); + await expect(body.getByText(LIVE, { exact: true })).toBeVisible(); + + if (inputMethod === "touch") { + const cdp = await page.context().newCDPSession(page); + await cdp.send("Emulation.setTouchEmulationEnabled", { enabled: true, maxTouchPoints: 1 }); + const bounds = (await body.boundingBox())!; + const x = bounds.x + bounds.width / 2; + const y = bounds.y + 80; + await cdp.send("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [{ x, y }] }); + for (const travel of [40, 80, 120, 200]) { + await cdp.send("Input.dispatchTouchEvent", { type: "touchMove", touchPoints: [{ x, y: y + travel }] }); + } + await cdp.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] }); + await cdp.detach(); + } else { + await body.hover(); + await page.mouse.wheel(0, -120); + } + await expect(page.getByText("No earlier messages", { exact: true })).toHaveCount(0); + await expect(page.locator('.thread-history-status[data-state="loading"]')).toBeVisible(); + expect(backend.reads).toHaveLength(1); + + await initial.route.fulfill({ status: 503, body: "Synthetic history unavailable" }); + const retry = page.getByRole("button", { name: "Loading failed. Retry" }); + await expect(retry).toBeVisible(); + await expect(page.getByText("No earlier messages", { exact: true })).toHaveCount(0); + await expect(body.getByText(LIVE, { exact: true })).toBeVisible(); + await retry.click(); + const retried = await backend.read(A, null, 1); + await expect(page.locator('.thread-history-status[data-state="loading"]')).toBeVisible(); + await backend.release(retried, liveFirstHistory()); + await expect(body.getByText("Historical prefix question", { exact: true })).toBeAttached(); + await expect(body.getByText(LIVE, { exact: true })).toHaveCount(1); + await expect(page.getByText("No earlier messages", { exact: true })).toHaveCount(0); + + const older = await requestOlder(page, backend, body, "nan311-before-2"); + await backend.release(older, history(closedTurn("first-turn", "First question", "First answer."))); + await expect(body.getByText("First question", { exact: true })).toBeAttached(); + await body.hover(); + await page.mouse.wheel(0, -10_000); + await expect.poll(() => body.evaluate((node) => node.scrollTop)).toBeLessThanOrEqual(1); + await page.mouse.wheel(0, -120); + await expect(page.getByText("No earlier messages", { exact: true })).toBeVisible(); + backend.healthy(); + }); +} + for (const width of [1440, 390]) { test(`synthetic live-first latest admits its prefix without refresh, then prepends two cursor pages (${width}px)`, async ({ page }) => { const { backend, body } = await openFixture(page, width); diff --git a/webui/src/components/thread/ThreadShell.tsx b/webui/src/components/thread/ThreadShell.tsx index 069640f97..677f8f197 100644 --- a/webui/src/components/thread/ThreadShell.tsx +++ b/webui/src/components/thread/ThreadShell.tsx @@ -1553,6 +1553,8 @@ export function ThreadShell({ retryStatus={retryStatus} emptyState={emptyState} initialLoading={initialHistoryLoading} + initialError={historyError} + onRetryInitial={refreshHistory} composer={inlineComposer ?
: null} onComposerLayoutChange={alignSidebar} activeTurnId={viewportTurnId} diff --git a/webui/src/components/thread/ThreadViewport.tsx b/webui/src/components/thread/ThreadViewport.tsx index 1693a0959..bae552d23 100644 --- a/webui/src/components/thread/ThreadViewport.tsx +++ b/webui/src/components/thread/ThreadViewport.tsx @@ -50,6 +50,8 @@ interface ThreadViewportProps { navigationFrame?: HTMLElement | null; emptyState?: ReactNode; initialLoading?: boolean; + initialError?: string | null; + onRetryInitial?: () => void; scrollToBottomSignal?: number; activeTurnId?: string | null; activeTurnStartedHere?: boolean; @@ -62,7 +64,7 @@ interface ThreadViewportProps { mcpPresets?: McpPresetInfo[]; slashCommands?: SlashCommand[]; forkBoundaryMessageCount?: number | null; - hasMoreBefore?: boolean; + hasMoreBefore?: boolean | null; loadingOlder?: boolean; olderError?: string | null; userMessageOffset?: number; @@ -264,6 +266,8 @@ export const ThreadViewport = forwardRef 0 @@ -950,6 +955,11 @@ export const ThreadViewport = forwardRef { if (!direction) return; + if (direction === "backward" && atHistoryBoundary() && hasMoreBefore === null && hiddenMessageCount === 0) { + captureHistoryScrollAnchor(); + yieldCameraToUser(); + return; + } if (direction === "forward" && historyWindow?.hasMoreAfter && el.scrollHeight - el.scrollTop - el.clientHeight < HISTORY_PREFETCH_MIN_PX && !navigation?.loading) { @@ -983,7 +993,7 @@ export const ThreadViewport = forwardRef verticalTravel) gesture.horizontal = true; if (gesture.horizontal) return; - if (gesture.pulling || (atHistoryBoundary() && delta < 0 && hiddenMessageCount === 0)) { + if (gesture.pulling || (atHistoryBoundary() && hasMoreBefore !== null && delta < 0 && hiddenMessageCount === 0)) { if (!gesture.pulling) { prepareHistoryPull(); historySpringRef.current?.startTouch(); @@ -1144,7 +1154,9 @@ export const ThreadViewport = forwardRef 0 && conversationReady + const showInitialHistoryStatus = !navigationStatus && historyUnknown && hasMessages && conversationReady + && atHistoryTop && Boolean(initialLoading || initialError); + const showHistoryStatus = !historyUnknown && !historySlotCompleted && !navigationStatus && messages.length > 0 && conversationReady && (atHistoryTop && (!historyExhausted || showHistoryExhausted) || historyPhase !== "hidden" || loadingOlder); const historyStatus = showHistoryStatus ? ( {!retainedHistorySlotKey ? historyStatus : null} + {showInitialHistoryStatus ? onRetryInitial?.()} /> : null} {navigationStatus && navigation ? : null} {!hasMessages ?
: null} diff --git a/webui/src/hooks/useNanobotStream.ts b/webui/src/hooks/useNanobotStream.ts index 7c3b55fa4..083932f06 100644 --- a/webui/src/hooks/useNanobotStream.ts +++ b/webui/src/hooks/useNanobotStream.ts @@ -133,7 +133,8 @@ function appendProjectedSessionInput( function createTranscript(messages: UIMessage[], saved: ThreadTranscriptSnapshot | undefined, historyKey: string | null) { const owner = new ThreadTranscript(messages, saved); // The first paint must not enter the welcome layout while history is pending. - owner.paging = { ...owner.paging, loading: !!historyKey && owner.paging.version === 0 }; + owner.paging = { ...owner.paging, loading: !!historyKey && owner.paging.version === 0, + hasMoreBefore: historyKey ? owner.paging.hasMoreBefore : false }; return owner; } diff --git a/webui/src/lib/thread-transcript.ts b/webui/src/lib/thread-transcript.ts index 924b13539..6eda3176a 100644 --- a/webui/src/lib/thread-transcript.ts +++ b/webui/src/lib/thread-transcript.ts @@ -36,7 +36,8 @@ export interface ThreadPaging { error: string | null; olderError: string | null; beforeCursor: string | null; - hasMoreBefore: boolean; + /** Null until a history read establishes the oldest loaded boundary. */ + hasMoreBefore: boolean | null; userMessageOffset: number; forkBoundaryMessageCount: number | null; truncated: boolean; @@ -95,7 +96,7 @@ function copyText(value: TextSource): TextSource { } function emptyPaging(): ThreadPaging { return { loading: false, loadingOlder: false, error: null, olderError: null, - beforeCursor: null, hasMoreBefore: false, userMessageOffset: 0, + beforeCursor: null, hasMoreBefore: null, userMessageOffset: 0, forkBoundaryMessageCount: null, truncated: false, version: 0, lineage: 0, hasPendingToolCalls: false, completedTurnIds: [], completionIdsProvided: false, activeTurnId: null }; } diff --git a/webui/src/tests/thread-transcript.test.ts b/webui/src/tests/thread-transcript.test.ts index 5657ba5c8..220482bfc 100644 --- a/webui/src/tests/thread-transcript.test.ts +++ b/webui/src/tests/thread-transcript.test.ts @@ -41,6 +41,25 @@ function receive(thread: ThreadTranscript, ...events: ThreadProjectionEvent[]): } describe("ThreadTranscript admission", () => { + it("keeps the history boundary unknown through live delivery and a failed first read", () => { + const thread = new ThreadTranscript(); + const read = thread.begin("initial"); + receive(thread, { ...at("turn-now", 5), event: "delta", stream_id: "answer-now", text: "Live tail" }); + expect(thread.paging).toMatchObject({ loading: true, hasMoreBefore: null }); + + thread.finishRead(read, "History unavailable"); + expect(thread.paging).toMatchObject({ loading: false, error: "History unavailable", hasMoreBefore: null }); + const restored = new ThreadTranscript([], thread.save()); + expect(restored.paging.hasMoreBefore).toBeNull(); + const retry = restored.begin("latest"); + expect(restored.accept(history([ + input("turn-now", "Earlier question"), + { ...at("turn-now", 6), event: "stream_end", stream_id: "answer-now", text: "Live tail", resuming: true, merge_next: true }, + ]), retry)).toBe(true); + expect(restored.messages.map((row) => row.content)).toEqual(["Earlier question", "Live tail"]); + expect(restored.paging).toMatchObject({ loading: false, error: null, hasMoreBefore: false }); + }); + it.each(["succeeded", "failed", "cancelled"] as const)("reconciles one idle compaction across live phases and history (%s)", (phase) => { const thread = new ThreadTranscript(); const base = [