Skip to content

Your lightweight open-source personal agent.

fix(webui): retain reading anchors through lazy Markdown layout

Xubin Rene1ca490d73ac
diff --git a/webui/browser/history-boundary.spec.ts b/webui/browser/history-boundary.spec.ts
index 0553d0d74..c379a341a 100644
--- a/webui/browser/history-boundary.spec.ts
+++ b/webui/browser/history-boundary.spec.ts
@@ -491,6 +491,35 @@ test("reduced-motion failure keeps its functional 48px slot and retry until natu
await expect(exhausted).toHaveCount(0);
});
+test("keeps the reading anchor when lazy Markdown loads after a fast prepend", async ({ page }) => {
+ let releaseMarkdown = () => {};
+ const ready = new Promise<void>((resolve) => { releaseMarkdown = resolve; });
+ await page.route("**/MarkdownTextRenderer.tsx*", async (route) => {
+ await ready;
+ await route.continue();
+ });
+ const history = await installHistory(page);
+ history.useFastResponse(true);
+ await page.clock.install({ time: new Date("2026-01-01T00:00:00Z") });
+ const { body, header } = await openSettledHistory(page);
+ await positionAtTop(body);
+ await body.hover();
+ await page.clock.pauseAt(new Date("2026-01-01T01:00:00Z"));
+ await page.mouse.wheel(0, -120);
+ await expect.poll(() => history.reads.length).toBe(1);
+ await expect(header).toHaveAttribute("data-state", "loading");
+ const readingTop = await anchorTop(body, "Question 40");
+ history.releaseFastResponse();
+ await expect(body.getByText("Question 28", { exact: true })).toBeAttached();
+ await expectAnchorCoordinate(body, "Question 40", readingTop);
+
+ releaseMarkdown();
+ await page.clock.resume();
+ await expect(body.locator('[data-thread-display-unit="a40"] p')).toHaveCount(8);
+ await stableLayout(body);
+ await expectAnchorCoordinate(body, "Question 40", readingTop);
+});
+
for (const elapsedMs of [0, 80]) test(`fast responses reclaim completed slots and keep the next boundary loading${elapsedMs ? " during spring travel" : ""}`, async ({ page }) => {
const history = await installHistory(page);
history.useFastResponse(true);
diff --git a/webui/src/components/thread/ThreadViewport.tsx b/webui/src/components/thread/ThreadViewport.tsx
index 1fdd921ac..60ac120d3 100644
--- a/webui/src/components/thread/ThreadViewport.tsx
+++ b/webui/src/components/thread/ThreadViewport.tsx
@@ -88,6 +88,7 @@ export const HISTORY_WINDOW_INCREMENT = 120;
interface HistoryScrollAnchor {
key: string;
offsetTop: number;
+ scrollTop: number;
}
const THREAD_DISPLAY_UNIT_SELECTOR = "[data-thread-display-unit]";
@@ -458,6 +459,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
}
historyScrollAnchorRef.current = {
key,
+ scrollTop: scroller.scrollTop,
// Normalize out the reveal lease, even when reading above its inline slot.
offsetTop: element.getBoundingClientRect().top - viewport.top
- (historySlotRef.current ? HISTORY_HOLD_PX : 0),
@@ -487,6 +489,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
const maxScrollTop = Math.max(0, scroller.scrollHeight - scroller.clientHeight);
const nextTop = Math.min(maxScrollTop, Math.max(0, scroller.scrollTop + delta));
threadMotionRef.current?.jumpTo(nextTop);
+ anchor.scrollTop = scroller.scrollTop;
return true;
}, []);
@@ -910,7 +913,10 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
current === logicallyAtBottom ? current : logicallyAtBottom,
);
if (owner === "user") {
- captureHistoryScrollAnchor();
+ // Restoring the anchor also emits scroll. Keep that reading position
+ // through lazy content resizing instead of selecting a prepended row.
+ const anchor = historyScrollAnchorRef.current;
+ if (!anchor || el.scrollTop !== anchor.scrollTop) captureHistoryScrollAnchor();
if (allowHistoryLoad) maybeLoadEarlierFromScroll();
if (allowHistoryLoad && distance < HISTORY_PREFETCH_MIN_PX
&& historyWindow?.hasMoreAfter && !navigation?.loading && !navigation?.error) navigation?.newer();