Compare commits

...
Author SHA1 Message Date
Cursor Agent 082ec99238 Fix: guard onMessagesWheel against non-scrollable containers
The wheel handler unconditionally set shouldAutoScrollRef to false on
upward wheel events, even when the scroll container had no overflow.
Since no scroll event fires in that case, auto-scroll stayed permanently
disabled. Add a scrollHeight > clientHeight guard so the handler only
disables auto-scroll when the container actually has scrollable content.
2026-02-20 00:12:50 +00:00
Julius Marminge 0cb4e75728 Preserve chat auto-scroll until user scrolls up
- Track last scroll position and content height to avoid disabling tail mode on new messages
- Stop auto-scroll immediately on upward mouse wheel input
- Keep chat pinned to bottom reliably while streaming
2026-02-19 16:04:42 -08:00
Julius Marminge 01accc005e Defer chat auto-scroll to animation frame
- Queue tail-to-bottom scrolling with `requestAnimationFrame` to avoid redundant scroll calls
- Preserve user scroll position by checking auto-scroll intent before running
- Cancel any pending frame on unmount to prevent stale callbacks
2026-02-19 15:51:14 -08:00
Julius Marminge 0d9c26810f Tighten chat auto-tail to true bottom position
- Replace near-bottom threshold logic with strict bottom epsilon detection
- Rename scroll helper to `isScrollContainerAtBottom` and cover overscroll in tests
- Auto-tail chat/worklog updates only while user remains at bottom
2026-02-19 01:42:40 -08:00
3 changed files with 88 additions and 36 deletions

No files matched your search

+26 -16
View File
@@ -1,11 +1,11 @@
import { describe, expect, it } from "vitest";
import { AUTO_SCROLL_BOTTOM_THRESHOLD_PX, isScrollContainerNearBottom } from "./chat-scroll";
import { AUTO_SCROLL_BOTTOM_EPSILON_PX, isScrollContainerAtBottom } from "./chat-scroll";
describe("isScrollContainerNearBottom", () => {
describe("isScrollContainerAtBottom", () => {
it("returns true when already at bottom", () => {
expect(
isScrollContainerNearBottom({
isScrollContainerAtBottom({
scrollTop: 600,
clientHeight: 400,
scrollHeight: 1_000,
@@ -13,31 +13,31 @@ describe("isScrollContainerNearBottom", () => {
).toBe(true);
});
it("returns true when within the auto-scroll threshold", () => {
it("returns true when within the bottom epsilon", () => {
expect(
isScrollContainerNearBottom({
scrollTop: 540,
isScrollContainerAtBottom({
scrollTop: 599,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(true);
});
it("returns false when the user is meaningfully above the bottom", () => {
it("returns false when the user is above the epsilon", () => {
expect(
isScrollContainerNearBottom({
scrollTop: 520,
isScrollContainerAtBottom({
scrollTop: 597,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(false);
});
it("clamps negative thresholds to zero", () => {
it("clamps negative epsilons to zero", () => {
expect(
isScrollContainerNearBottom(
isScrollContainerAtBottom(
{
scrollTop: 539,
scrollTop: 599,
clientHeight: 400,
scrollHeight: 1_000,
},
@@ -46,17 +46,27 @@ describe("isScrollContainerNearBottom", () => {
).toBe(false);
});
it("falls back to the default threshold for non-finite values", () => {
it("falls back to the default epsilon for non-finite values", () => {
expect(
isScrollContainerNearBottom(
isScrollContainerAtBottom(
{
scrollTop: 540,
scrollTop: 599,
clientHeight: 400,
scrollHeight: 1_000,
},
Number.NaN,
),
).toBe(true);
expect(AUTO_SCROLL_BOTTOM_THRESHOLD_PX).toBe(64);
expect(AUTO_SCROLL_BOTTOM_EPSILON_PX).toBe(2);
});
it("treats overscrolled values as bottom", () => {
expect(
isScrollContainerAtBottom({
scrollTop: 610,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(true);
});
});
+8 -8
View File
@@ -1,4 +1,4 @@
export const AUTO_SCROLL_BOTTOM_THRESHOLD_PX = 64;
export const AUTO_SCROLL_BOTTOM_EPSILON_PX = 2;
interface ScrollPosition {
scrollTop: number;
@@ -6,19 +6,19 @@ interface ScrollPosition {
scrollHeight: number;
}
export function isScrollContainerNearBottom(
export function isScrollContainerAtBottom(
position: ScrollPosition,
thresholdPx = AUTO_SCROLL_BOTTOM_THRESHOLD_PX,
epsilonPx = AUTO_SCROLL_BOTTOM_EPSILON_PX,
): boolean {
const threshold = Number.isFinite(thresholdPx)
? Math.max(0, thresholdPx)
: AUTO_SCROLL_BOTTOM_THRESHOLD_PX;
const epsilon = Number.isFinite(epsilonPx)
? Math.max(0, epsilonPx)
: AUTO_SCROLL_BOTTOM_EPSILON_PX;
const { scrollTop, clientHeight, scrollHeight } = position;
if (![scrollTop, clientHeight, scrollHeight].every(Number.isFinite)) {
return true;
}
const distanceFromBottom = scrollHeight - clientHeight - scrollTop;
return distanceFromBottom <= threshold;
const distanceFromBottom = Math.max(0, scrollHeight - clientHeight - scrollTop);
return distanceFromBottom <= epsilon;
}
+54 -12
View File
@@ -17,6 +17,7 @@ import {
type DragEvent,
type FormEvent,
type KeyboardEvent,
type WheelEvent,
memo,
type RefObject,
useCallback,
@@ -61,7 +62,7 @@ import {
formatElapsed,
formatTimestamp,
} from "../session-logic";
import { isScrollContainerNearBottom } from "../chat-scroll";
import { isScrollContainerAtBottom } from "../chat-scroll";
import { useStore } from "../store";
import { truncateTitle } from "../truncateTitle";
import {
@@ -402,6 +403,9 @@ export default function ChatView({ threadId }: ChatViewProps) {
>(() => readLastInvokedScriptByProjectFromStorage());
const messagesScrollRef = useRef<HTMLDivElement>(null);
const shouldAutoScrollRef = useRef(true);
const lastKnownScrollTopRef = useRef(0);
const lastKnownScrollHeightRef = useRef(0);
const pendingAutoScrollFrameRef = useRef<number | null>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const composerCommandInputRef = useRef<HTMLInputElement>(null);
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
@@ -1094,33 +1098,70 @@ export default function ChatView({ threadId }: ChatViewProps) {
}
}, [lastInvokedScriptByProjectId]);
// Auto-scroll on new messages
const messageCount = activeThread?.messages.length ?? 0;
// Auto-tail while the user stays at the bottom.
const workLogCount = workLogEntries.length;
const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });
lastKnownScrollTopRef.current = scrollContainer.scrollTop;
lastKnownScrollHeightRef.current = scrollContainer.scrollHeight;
shouldAutoScrollRef.current = true;
}, []);
const requestAutoTailToBottom = useCallback(() => {
if (pendingAutoScrollFrameRef.current !== null) return;
pendingAutoScrollFrameRef.current = window.requestAnimationFrame(() => {
pendingAutoScrollFrameRef.current = null;
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom();
});
}, [scrollMessagesToBottom]);
const onMessagesScroll = useCallback(() => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
shouldAutoScrollRef.current = isScrollContainerNearBottom(scrollContainer);
const currentScrollTop = scrollContainer.scrollTop;
const currentScrollHeight = scrollContainer.scrollHeight;
const atBottom = isScrollContainerAtBottom(scrollContainer);
if (atBottom) {
shouldAutoScrollRef.current = true;
} else if (shouldAutoScrollRef.current) {
// While tailing, only relinquish control when the user scrolls upward.
const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1;
const contentHeightChanged =
Math.abs(currentScrollHeight - lastKnownScrollHeightRef.current) > 1;
if (scrolledUp && !contentHeightChanged) {
shouldAutoScrollRef.current = false;
}
}
lastKnownScrollTopRef.current = currentScrollTop;
lastKnownScrollHeightRef.current = currentScrollHeight;
}, []);
const onMessagesWheel = useCallback((event: WheelEvent<HTMLDivElement>) => {
if (event.deltaY < 0) {
const el = messagesScrollRef.current;
if (el && el.scrollHeight > el.clientHeight) {
shouldAutoScrollRef.current = false;
}
}
}, []);
useLayoutEffect(() => {
if (!activeThread?.id) return;
scrollMessagesToBottom();
}, [activeThread?.id, scrollMessagesToBottom]);
useEffect(() => {
useLayoutEffect(() => {
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom("smooth");
}, [messageCount, scrollMessagesToBottom]);
useEffect(() => {
if (phase !== "running") return;
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom("smooth");
}, [phase, workLogCount, scrollMessagesToBottom]);
requestAutoTailToBottom();
}, [activeThread?.messages, workLogCount, requestAutoTailToBottom]);
useEffect(
() => () => {
if (pendingAutoScrollFrameRef.current === null) return;
window.cancelAnimationFrame(pendingAutoScrollFrameRef.current);
pendingAutoScrollFrameRef.current = null;
},
[],
);
useEffect(() => {
setExpandedWorkGroups({});
@@ -2050,6 +2091,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
ref={messagesScrollRef}
className="min-h-0 flex-1 overflow-y-auto px-5 py-4"
onScroll={onMessagesScroll}
onWheel={onMessagesWheel}
>
<MessagesTimeline
hasMessages={activeThread.messages.length > 0}