diff --git a/web/hooks/useChatAutoScroll.ts b/web/hooks/useChatAutoScroll.ts index 08db25e18..9dfccffdc 100644 --- a/web/hooks/useChatAutoScroll.ts +++ b/web/hooks/useChatAutoScroll.ts @@ -49,17 +49,18 @@ export function useChatAutoScroll({ const container = containerRef.current; if (!container) return; const previous = positionRef.current; - const sameLayout = previous.height === container.scrollHeight && - previous.viewport === container.clientHeight; // The browser clamps scrollTop when content shrinks. That is not a user - // gesture. Neither clamping nor delayed growth may release/re-arm follow. - if (sameLayout) { - const remaining = container.scrollHeight - container.scrollTop - container.clientHeight; - if (remaining < 80 && (shouldAutoScrollRef.current || container.scrollTop > previous.top + 4)) { - shouldAutoScrollRef.current = true; - } - else if (container.scrollTop < previous.top - 4) shouldAutoScrollRef.current = false; + // gesture. Growth never moves it (overflow-anchor: none), so whatever the + // layout did, the position without the reader is at most the new maximum. + // Comparing against that keeps a scrollbar drag during streaming, when the + // reply grows between the drag and its scroll event, a gesture (#2072). + const max = Math.max(0, container.scrollHeight - container.clientHeight); + const expected = Math.min(previous.top, max); + const remaining = container.scrollHeight - container.scrollTop - container.clientHeight; + if (remaining < 80 && (shouldAutoScrollRef.current || container.scrollTop > expected + 4)) { + shouldAutoScrollRef.current = true; } + else if (container.scrollTop < expected - 4) shouldAutoScrollRef.current = false; rememberPosition(container); }, [rememberPosition]); diff --git a/web/tests/chat-autoscroll-layout.spec.tsx b/web/tests/chat-autoscroll-layout.spec.tsx index ca6689e78..ad6a5dd05 100644 --- a/web/tests/chat-autoscroll-layout.spec.tsx +++ b/web/tests/chat-autoscroll-layout.spec.tsx @@ -113,3 +113,21 @@ it("does not yank the viewport when the reader opens working notes", () => { expect(top).toBe(800); expect(following.current).toBe(false); }); + +it("lets the reader drag the scrollbar up while the reply is still growing (#2072)", () => { + render(); flushLayout(); + expect(top).toBe(800); + // A scrollbar drag fires no wheel/key event, and while streaming the reply + // grows between the drag and its scroll event. + top = 300; height = 1100; + fireEvent.scroll(screen.getByTestId("scroll")); flushLayout(); + expect(following.current).toBe(false); + height = 1400; flushLayout(); + expect(top).toBe(300); + // Dragging back to the live end follows again, even as it grows. + top = 1180; height = 1420; + fireEvent.scroll(screen.getByTestId("scroll")); flushLayout(); + expect(following.current).toBe(true); + height = 1600; flushLayout(); + expect(top).toBe(1400); +});