From b9712069767351453643b970a52d30c973859a79 Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Mon, 23 Nov 2026 17:29:09 +1100 Subject: [PATCH] fix(messages): keep a wheel whole when a correction moves nothing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Requested: stop one intermittently failing browser journey from failing CI: tests/browser/history-loading.spec.mjs:295, "pages inside one second › input after the page lands starts from the reader's row", on Linux WebKit. Find why the reader's row ends somewhere other than 40 px lower and fix that where the cause is. What fails - After the 40 px wheel the reader's row is 7.09, 22.25 and 7.86 px from where the journey expects it (CI runs 45108381111, 45096440304 and 45088508339, on three unrelated branches), and stays there for the whole 10 s poll. - Rate: 3 of the 118 "Browser journeys (webkit, 2/18)" jobs that completed in the 150 CI runs from 2026-11-23 00:23 to 05:44 UTC, counting each run's latest attempt. Cause: a product defect, in the reader's hold - Linux WebKit animates a mouse wheel over several frames. Any scroll write during that animation ends it where it is, including a write that changes nothing. In a bare page a -40 wheel moves 27 to 31 px in its first frame and reaches 40 in the next. Writing `scrollTop = scrollTop`, `scrollTop += 0.2` or `scrollTop += 5` in the first scroll event ended the wheel at that first step in 24 of 24 runs; 8 of 8 reached 40 without a write. - WebKit truncates scrollTop. The hold keeps the reader's row at an exact Y, so its first correction lands up to 1 px short, and every later measurement makes it write that same fraction again (for example 1225 to 1225.015625). The write moves nothing. - When a row measures while a wheel is still animating, that write ends the wheel. The reader loses the rest of the notch. - The three CI traces fit this. The scroller moved 32, 17 and 32 px of the 40. The list's height changed in the same step. Virtua's offset for the reader's row did not change, so nothing above it had moved and there was nothing to correct. Before the wheel the row sat 0.90, 0.75 and 0.14 px from the hold's Y. Each failure equals 40 minus the distance moved, less that fraction. Traces do not record scroll writes, so which code wrote is inferred there and observed locally. - Reproduced in a Linux WebKit container (Playwright 1.63.0 image) on main at 3064169012, with every scrollTop write, scrollBy and scrollTo on the scroller logged and a row below the reader grown as the wheel starts: 9 of 16 runs failed. In each, the only scroll write between the wheel and its scrollend was the hold's fraction, and the scroller stopped 1 to 24 px into the wheel. Virtua wrote nothing. The 7 that passed finished the wheel before the row measured. Fix: src/features/messages/scroll-correction.ts - correctScrollTop remembers, per scroller, the part of its last correction that the offset did not take when that part is under 1 px. A correction that asks for that same fraction again is skipped: the scroller already refused it, so the write could only interrupt the reader. - A shortfall of 1 px or more is the end of the scroll range, which content can move, so it is not remembered and is asked again. - Corrections that move the scroller are unchanged, and so is the Y the hold keeps: it still asks for its exact position, so truncation does not add up across corrections. - This is not specific to Linux. On Mac WebKit a skipped correction also no longer hides overflow for a task, so a correction that moves nothing no longer stops trackpad momentum. The thread panel's corrections use the same helper and skip the same repeats. Tests - scroll-correction.test.ts: a scroller that truncates gets no second write for the fraction it left, and still takes the next real correction. A correction the end of the range refused is written again. The first fails on main's helper; the second passes there and guards the 1 px rule. - The journey now grows the last mounted row as the wheel starts, so a row measures during the wheel on every run instead of by chance. The 40 px expectation and its 4 px tolerance are unchanged. Nothing is skipped, retried or loosened. Verification - Linux WebKit, container, two workers, this journey: - edited journey on main's helper: 3 of 20 failed (36.6, 36.8 and 38.1 px from the expected Y); - edited journey with the fix: 60 of 60 passed; - the logged reproduction with the fix: 32 of 32 passed. In 18 of them a row measured mid-wheel, no scroll write followed, and the wheel finished. - the journey with the fix under full CPU load: 24 of 24 passed; - the whole spec with the fix, once, WebKit and Chromium: 46 of 46 passed. - macOS, local: the journey 10 of 10 on Chromium and 10 of 10 on WebKit. - vitest: scroll-correction.test.ts with the ChannelTimeline and ThreadPanel suites, 9 files, 549 tests, all passed. - pnpm typecheck: pass. biome check on the three changed files: pass. Not run - The whole vitest suite, the other browser specs and the integration tests. - The desktop app, and WebKitGTK outside Playwright's build. - Before the journey was edited it passed 10 of 10 unloaded and 24 of 24 under full CPU load on main in the container. At CI's rate that says nothing either way, which is why the evidence above forces the measurement. Remaining gaps - A correction that has to move the scroller still ends an animated wheel in Linux WebKit: a row above the reader that changes height mid-wheel, or Virtua's own jump. That is needed to keep the row in place, and the browser does not expose the wheel's target to resume it. The three CI failures were not this case, and the journey does not force it. - The Virtua patch's jump can issue a sub-pixel scrollBy of its own. I did not see one in any log and left it alone. Signed-off-by: Matt Toohey Co-Authored-By: Claude Fable 5.2 --- .../messages/scroll-correction.test.ts | 31 +++++++++++++++++-- src/features/messages/scroll-correction.ts | 17 +++++++++- tests/browser/history-loading.spec.mjs | 16 +++++++--- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/features/messages/scroll-correction.test.ts b/src/features/messages/scroll-correction.test.ts index 7011118582..cabba95205 100644 --- a/src/features/messages/scroll-correction.test.ts +++ b/src/features/messages/scroll-correction.test.ts @@ -5,13 +5,15 @@ import { correctScrollTop } from "./scroll-correction"; const stubNavigator = (platform: string, vendor: string, maxTouchPoints = 0) => vi.stubGlobal("navigator", { platform, vendor, maxTouchPoints }); -function scroller() { +function scroller(writes?: number[], end = Number.POSITIVE_INFINITY) { const element = document.createElement("section"); let top = 100; Object.defineProperty(element, "scrollTop", { get: () => top, set: (value: number) => { - top = value; + // With `writes`, keep whole offsets inside the range, as WebKit does. + writes?.push(value); + top = writes ? Math.min(end, Math.trunc(value)) : value; }, }); return element; @@ -89,3 +91,28 @@ it("ignores a zero correction", () => { expect(element.scrollTop).toBe(100); expect(element.getAttribute("style")).toBeNull(); }); + +it("does not write again the fraction a whole-pixel scroller left", () => { + const writes: number[] = []; + const element = scroller(writes); + + correctScrollTop(element, 40.75); + // The reader scrolls on, and the next measurement asks for the same fraction. + element.scrollTop = 120; + writes.length = 0; + correctScrollTop(element, 0.75); + + expect(writes).toEqual([]); + correctScrollTop(element, 12.75); + expect(element.scrollTop).toBe(132); +}); + +it("asks again for a correction the end of the scroll range refused", () => { + const writes: number[] = []; + const element = scroller(writes, 100); + + correctScrollTop(element, 40); + correctScrollTop(element, 40); + + expect(writes).toEqual([140, 140]); +}); diff --git a/src/features/messages/scroll-correction.ts b/src/features/messages/scroll-correction.ts index cb3190cba7..2db859f12e 100644 --- a/src/features/messages/scroll-correction.ts +++ b/src/features/messages/scroll-correction.ts @@ -1,15 +1,25 @@ import { macWebKit } from "./mac-webkit"; const restores = new WeakMap void>(); +// The part of each scroller's last correction that its offset did not take. +const remainders = new WeakMap(); /** * Applies an automatic scroll correction. Mac WebKit can leave the corrected * viewport unpainted while native momentum continues, so stop the momentum the * same way the Virtua patch does (patches/virtua@0.54.2.patch): hide vertical overflow for * one task. The scroller must keep its inline size (`scrollbar-gutter: stable`). + * + * A scroller keeps whole offsets (WebKit truncates scrollTop), so a caller + * holding an exact position is left a fraction of a pixel short and asks for + * that fraction again after every measurement. Writing it again moves nothing, + * but any scroll write ends a wheel that Linux WebKit is still animating, and + * the reader loses the rest of it. Skip the fraction the last correction left. */ export function correctScrollTop(element: HTMLElement, delta: number) { if (!delta) return; + const left = remainders.get(element); + if (left !== undefined && Math.abs(delta - left) < 0.001) return; if (macWebKit()) { // Corrections in one task share the original declaration. restores.get(element)?.(); @@ -31,5 +41,10 @@ export function correctScrollTop(element: HTMLElement, delta: number) { const timer = setTimeout(restore); restores.set(element, restore); } - element.scrollTop += delta; + const target = element.scrollTop + delta; + element.scrollTop = target; + const remainder = target - element.scrollTop; + // A larger shortfall is the end of the scroll range, which content can move. + if (remainder && Math.abs(remainder) < 1) remainders.set(element, remainder); + else remainders.delete(element); } diff --git a/tests/browser/history-loading.spec.mjs b/tests/browser/history-loading.spec.mjs index 4cb6dd77da..b0580b82ef 100644 --- a/tests/browser/history-loading.spec.mjs +++ b/tests/browser/history-loading.spec.mjs @@ -306,12 +306,20 @@ test.describe("pages inside one second", () => { .poll(() => history(page).evaluate((e) => e.scrollHeight)) .toBeGreaterThan(height); await expectAnchor(page, before); + // Linux WebKit animates a wheel over several frames, and any scroll + // write ends it there. A row measuring meanwhile moves nothing above + // the reader, so it must not cost them the rest of the wheel. const wheeled = await history(page).evaluateHandle((e) => { const state = { wheel: false }; - e.addEventListener("wheel", () => (state.wheel = true), { - once: true, - passive: true, - }); + e.addEventListener( + "wheel", + () => { + state.wheel = true; + const rows = e.querySelectorAll("[data-message-id]"); + rows[rows.length - 1].style.paddingBottom = "10px"; + }, + { once: true, passive: true }, + ); return state; }); await page.mouse.wheel(0, -40);