Skip to content

Commit 61d0429

Browse files
committed
fix(ui): retain latest scroll commands until their destination
Do not treat the virtualizer's idle debounce or a decreasing offset as reader takeover. Settle at the actual DOM end within one pixel, retaining the separate eight-pixel UI-follow policy and explicit input cancellation. Cover retargeted idle delivery and near-end precision with the real dependency, synchronize pointer baseline capture, and type the existing composer snapshot as its textarea contract. Fixes #130892
1 parent 9d4070c commit 61d0429

3 files changed

Lines changed: 65 additions & 10 deletions

File tree

‎ui/src/e2e/chat-transcript-disclosure-anchor.e2e.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ suite.define(() => {
187187
// a wheel event or a changed offset.
188188
await page.mouse.click(track!.x + track!.width - 3, track!.y + 20);
189189
await page.locator(".chat-scroll-to-bottom").waitFor({ state: "visible" });
190+
// Chromium can commit its last canceled animation offset after the
191+
// pointer action returns. Capture the reader before releasing text.
192+
await waitForChatScrollIdle(page);
190193
}
191194
const interruptedOffset = await thread.evaluate((element) => element.scrollTop);
192195
if (interruption === "wheel") {

‎ui/src/pages/chat/components/chat-transcript-controller.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,56 @@ describe("chat transcript controller", () => {
414414
},
415415
);
416416

417+
it("keeps a smooth latest command through an idle observer delivery before reaching its target", async () => {
418+
const rows: TestContentRow[] = Array.from({ length: 40 }, (_, index) => ({
419+
kind: "content",
420+
key: `row:${index}`,
421+
content: html`<div>row ${index}</div>`,
422+
}));
423+
const { container, transcript } = await mountTestTranscript("idle-latest", rows);
424+
Object.defineProperties(container, {
425+
clientHeight: { configurable: true, value: 600 },
426+
scrollHeight: { configurable: true, value: 4800 },
427+
});
428+
const scrollTo = vi.fn();
429+
container.scrollTo = scrollTo;
430+
vi.useFakeTimers();
431+
try {
432+
container.scrollTop = 1000;
433+
container.dispatchEvent(new Event("scroll"));
434+
transcript.scrollToEnd({ behavior: "smooth" });
435+
expect(scrollTo).toHaveBeenLastCalledWith({ top: 4200, behavior: "smooth" });
436+
container.scrollTop = 1500;
437+
container.dispatchEvent(new Event("scroll"));
438+
Object.defineProperty(container, "scrollHeight", { configurable: true, value: 4900 });
439+
vi.advanceTimersByTime(16);
440+
expect(scrollTo).toHaveBeenLastCalledWith({ top: 4300, behavior: "smooth" });
441+
scrollTo.mockClear();
442+
443+
// A retargeted native animation can pause between offset events. Core's
444+
// idle debounce still fires, but the requested end has not been reached.
445+
vi.advanceTimersByTime(150);
446+
expect(transcript.isProgrammaticScroll).toBe(true);
447+
expect(scrollTo).not.toHaveBeenCalled();
448+
449+
// The 8px UI-follow boundary does not complete the native end command.
450+
container.scrollTop = 4296;
451+
container.dispatchEvent(new Event("scroll"));
452+
vi.advanceTimersByTime(150);
453+
expect(transcript.isProgrammaticScroll).toBe(false);
454+
expect(scrollTo).not.toHaveBeenCalled();
455+
456+
container.scrollTop = 4300;
457+
container.dispatchEvent(new Event("scroll"));
458+
vi.advanceTimersByTime(150);
459+
expect(scrollTo).toHaveBeenCalledExactlyOnceWith({ top: 4300, behavior: "instant" });
460+
expect(transcript.isProgrammaticScroll).toBe(false);
461+
} finally {
462+
transcript.hostDisconnected();
463+
vi.useRealTimers();
464+
}
465+
});
466+
417467
it("remeasures every visible pane transcript while preserving hidden transcript rows", async () => {
418468
const host = Object.assign(document.body.appendChild(document.createElement("div")), {
419469
addController: vi.fn(),

‎ui/src/pages/chat/components/chat-transcript-controller.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,19 @@ class ChatSessionVirtualizerHost implements ReactiveControllerHost, ChatTranscri
293293
if (element !== this.scrollElement) {
294294
return;
295295
}
296-
const previousOffset = instance.scrollOffset;
297296
callback(offset, scrolling);
298-
// Core must observe the user's offset before replaying skipped sizes;
299-
// otherwise its old destination can compensate the viewport back down.
300-
if (
301-
this.endScrollBehavior === "smooth" &&
302-
(!scrolling || offset < (previousOffset ?? offset))
303-
) {
304-
this.cancelScroll();
305-
} else if (!scrolling) {
306-
this.endScrollBehavior = null;
297+
// Idle can arrive between smooth retargets. Completion needs the
298+
// restore path's 1px precision, not the 8px UI-follow boundary.
299+
// The input listeners above own reader takeover.
300+
const settledAtEnd =
301+
!scrolling &&
302+
Math.abs((maxTranscriptScrollOffset(element) ?? 0) - (element?.scrollTop ?? 0)) <= 1;
303+
if (settledAtEnd && this.pendingScrollOffset === null) {
304+
if (this.endScrollBehavior === "smooth") {
305+
this.cancelScroll();
306+
} else {
307+
this.endScrollBehavior = null;
308+
}
307309
}
308310
});
309311
return () => {

0 commit comments

Comments
 (0)