Skip to content

Commit b90f28e

Browse files
nickmopensteipete
andauthored
fix(ui): prevent programmatic scrollTo from flipping chatUserNearBott… (#76991)
* fix(ui): prevent programmatic scrollTo from flipping chatUserNearBottom during streaming * fix(ui): preserve user scroll-up events that arrive during programmatic scroll guard window * test(ui): add unit coverage for programmatic scroll guard boundary and retry path * fix(ui): preserve chat scroll bookkeeping * chore: drop unrelated slack formatting * test: narrow inbound dedupe claim result --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 7f07fbd commit b90f28e

5 files changed

Lines changed: 212 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ Docs: https://docs.openclaw.ai
10951095
- Memory/LanceDB: declare `apache-arrow` in the bundled memory plugin package so LanceDB installs include its runtime peer. Fixes #76910. Thanks @afiqfiles-max.
10961096
- CLI/devices: retry explicit device-pair approval with `operator.admin` after a pairing-scope ownership denial, so existing admin-capable paired-device tokens can recover new Control UI/browser pairing after upgrades instead of requiring manual JSON edits. Fixes #76956. Thanks @neo19482.
10971097
- CLI/devices: stop local pairing fallback when the active Gateway names a pending request that is absent from the local pairing store, so profile or state-dir mismatches no longer make `openclaw devices list/approve` inspect the wrong store while a real device stays blocked. Thanks @vincentkoc.
1098+
- Control UI/webchat: fix streaming assistant responses causing the chat viewport to scroll upward by guarding `handleChatScroll` against scroll events triggered by the auto-scroll logic itself; introduces a `chatIsProgrammaticScroll` flag that suppresses near-bottom state updates during programmatic `scrollTo` calls so streaming output stays pinned to the bottom. Thanks @nickmopen.
10981099
- Google Meet: use the local call-control microphone button instead of disabled remote participant mute buttons, and block realtime speech when the OpenClaw Meet microphone remains muted.
10991100
- Google Meet: refresh realtime browser state during status and retry delayed speech after Meet finishes joining, so a just-opened in-call tab no longer leaves speech stuck behind stale `not-in-call` health.
11001101
- Plugins/install: recover the install ledger from the managed npm root when `plugins/installs.json` is empty or partial, so reinstalling Discord and Codex no longer makes the other installed plugin disappear.

‎extensions/slack/src/monitor/media.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ function expectSaveMediaBufferCall(mock: unknown, contentType: string, maxBytes:
141141
}
142142

143143
function expectVerboseLogContains(expected: string): void {
144-
const messages = vi.mocked(logVerbose).mock.calls.map((call) =>
145-
typeof call[0] === "string" ? call[0] : "",
146-
);
144+
const messages = vi
145+
.mocked(logVerbose)
146+
.mock.calls.map((call) => (typeof call[0] === "string" ? call[0] : ""));
147147
expect(messages.some((message) => message.includes(expected))).toBe(true);
148148
}
149149

‎ui/src/ui/app-scroll.test.ts‎

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function createScrollHost(
4444
chatUserNearBottom: true,
4545
chatHeaderControlsHidden: false,
4646
chatNewMessagesBelow: false,
47+
chatIsProgrammaticScroll: false,
48+
chatProgrammaticScrollTarget: 0,
4749
logsScrollFrame: null as number | null,
4850
logsAtBottom: true,
4951
topbarObserver: null as ResizeObserver | null,
@@ -309,5 +311,183 @@ describe("resetChatScroll", () => {
309311
expect(host.chatUserNearBottom).toBe(true);
310312
expect(host.chatLastScrollTop).toBe(0);
311313
expect(host.chatHeaderControlsHidden).toBe(false);
314+
expect(host.chatIsProgrammaticScroll).toBe(false);
315+
expect(host.chatProgrammaticScrollTarget).toBe(0);
316+
});
317+
});
318+
319+
/* ------------------------------------------------------------------ */
320+
/* Programmatic scroll guard */
321+
/* ------------------------------------------------------------------ */
322+
323+
describe("programmatic scroll guard", () => {
324+
beforeEach(() => {
325+
vi.useFakeTimers();
326+
vi.spyOn(window, "requestAnimationFrame").mockImplementation((cb) => {
327+
cb(0);
328+
return 1;
329+
});
330+
});
331+
332+
afterEach(() => {
333+
vi.useRealTimers();
334+
vi.restoreAllMocks();
335+
});
336+
337+
it("handleChatScroll suppresses own scroll event when scrollTop is at the programmatic target", () => {
338+
const { host } = createScrollHost({});
339+
host.chatUserNearBottom = true;
340+
host.chatIsProgrammaticScroll = true;
341+
// Simulates scrollTo(scrollHeight=1000): expected scrollTop = 1000 - 400 = 600.
342+
host.chatProgrammaticScrollTarget = 1000;
343+
344+
// Our own scroll event: scrollTop is at the clamped target position.
345+
const event = createScrollEvent(1000, 600, 400);
346+
handleChatScroll(host, event);
347+
348+
// Must remain true — our scroll-to-bottom event must not flip near-bottom state.
349+
expect(host.chatUserNearBottom).toBe(true);
350+
});
351+
352+
it("handleChatScroll processes user scroll-up that arrives during the guard window", () => {
353+
const { host } = createScrollHost({});
354+
host.chatUserNearBottom = true;
355+
host.chatIsProgrammaticScroll = true;
356+
// We had targeted the bottom of a 3000px page.
357+
host.chatProgrammaticScrollTarget = 3000;
358+
359+
// User scrolled up to 500 during the guard window — far below the target (2600).
360+
const event = createScrollEvent(3000, 500, 400); // distanceFromBottom = 2100 > 450
361+
handleChatScroll(host, event);
362+
363+
// Must flip to false — user intentionally scrolled up, streaming must not re-pin them.
364+
expect(host.chatUserNearBottom).toBe(false);
365+
});
366+
367+
it("scheduleChatScroll sets chatIsProgrammaticScroll before scrolling and clears it after rAF", async () => {
368+
const { host } = createScrollHost({
369+
scrollHeight: 2000,
370+
scrollTop: 1600,
371+
clientHeight: 400,
372+
});
373+
host.chatUserNearBottom = true;
374+
host.chatHasAutoScrolled = true;
375+
376+
scheduleChatScroll(host);
377+
await host.updateComplete;
378+
379+
// After rAF cleanup the flag must be cleared.
380+
expect(host.chatIsProgrammaticScroll).toBe(false);
381+
// Target was set to container scrollHeight before scrollTo.
382+
expect(host.chatProgrammaticScrollTarget).toBe(2000);
383+
// And scroll must have happened.
384+
expect(host.chatUserNearBottom).toBe(true);
385+
});
386+
387+
it("after programmatic scroll is done, a real user scroll-up correctly flips chatUserNearBottom to false", async () => {
388+
const { host } = createScrollHost({
389+
scrollHeight: 3000,
390+
scrollTop: 500,
391+
clientHeight: 400,
392+
});
393+
host.chatUserNearBottom = true;
394+
// Flag already cleared — simulates the state after the rAF cleanup ran.
395+
host.chatIsProgrammaticScroll = false;
396+
397+
// User genuinely scrolled far from bottom — must be respected.
398+
const event = createScrollEvent(3000, 500, 400); // distanceFromBottom = 2100 > 450
399+
handleChatScroll(host, event);
400+
401+
expect(host.chatUserNearBottom).toBe(false);
402+
});
403+
404+
it("guard boundary: scrollTop exactly one pixel below threshold is NOT suppressed (user scroll-up passes through)", () => {
405+
const { host } = createScrollHost({});
406+
host.chatUserNearBottom = true;
407+
host.chatIsProgrammaticScroll = true;
408+
// Programmatic target = 1000, clientHeight = 400 → threshold = 600.
409+
// scrollTop = 599 → 599 >= 600 is false → guard does NOT suppress the event.
410+
host.chatProgrammaticScrollTarget = 1000;
411+
412+
const event = createScrollEvent(1000, 599, 400); // distanceFromBottom = 1
413+
handleChatScroll(host, event);
414+
415+
// Event was processed: user is near bottom (dist=1 < 450) but the guard did not block it.
416+
expect(host.chatUserNearBottom).toBe(true);
417+
// chatLastScrollTop must have been updated — confirms the event was not short-circuited.
418+
expect(host.chatLastScrollTop).toBe(599);
419+
});
420+
421+
it("guard boundary: scrollTop exactly at threshold is suppressed", () => {
422+
const { host } = createScrollHost({});
423+
host.chatUserNearBottom = true;
424+
host.chatIsProgrammaticScroll = true;
425+
host.chatProgrammaticScrollTarget = 1000;
426+
host.chatLastScrollTop = 0;
427+
428+
// scrollTop = 600 → 600 >= 600 is true → guard suppresses the event.
429+
const event = createScrollEvent(1000, 600, 400);
430+
handleChatScroll(host, event);
431+
432+
// Scroll bookkeeping still advances so the next user scroll has the right direction.
433+
expect(host.chatLastScrollTop).toBe(600);
434+
});
435+
436+
it("suppressed programmatic scroll event does not mutate chatNewMessagesBelow", () => {
437+
const { host } = createScrollHost({});
438+
host.chatUserNearBottom = true;
439+
host.chatNewMessagesBelow = false;
440+
host.chatIsProgrammaticScroll = true;
441+
host.chatProgrammaticScrollTarget = 2000;
442+
443+
// Our own scroll event at the programmatic target position.
444+
const event = createScrollEvent(2000, 1600, 400);
445+
handleChatScroll(host, event);
446+
447+
// Event was suppressed — chatNewMessagesBelow must stay unchanged.
448+
expect(host.chatNewMessagesBelow).toBe(false);
449+
});
450+
451+
it("suppressed programmatic scroll preserves direction bookkeeping for the next user scroll-up", () => {
452+
const { host } = createScrollHost({});
453+
host.chatUserNearBottom = true;
454+
host.chatHeaderControlsHidden = true;
455+
host.chatIsProgrammaticScroll = true;
456+
host.chatProgrammaticScrollTarget = 3000;
457+
host.chatLastScrollTop = 0;
458+
459+
handleChatScroll(host, createScrollEvent(3000, 2600, 400));
460+
expect(host.chatLastScrollTop).toBe(2600);
461+
462+
host.chatIsProgrammaticScroll = false;
463+
handleChatScroll(host, createScrollEvent(3000, 2000, 400));
464+
465+
expect(host.chatHeaderControlsHidden).toBe(false);
466+
expect(host.chatUserNearBottom).toBe(false);
467+
});
468+
469+
it("retry timeout sets and clears chatIsProgrammaticScroll", async () => {
470+
const { host, container } = createScrollHost({
471+
scrollHeight: 2000,
472+
scrollTop: 1600,
473+
clientHeight: 400,
474+
});
475+
host.chatUserNearBottom = true;
476+
host.chatHasAutoScrolled = true;
477+
478+
scheduleChatScroll(host);
479+
await host.updateComplete;
480+
481+
// After the initial rAF the flag must already be cleared.
482+
expect(host.chatIsProgrammaticScroll).toBe(false);
483+
484+
// Advance past the retry delay (120ms) — retry scrollTop assignment fires.
485+
vi.advanceTimersByTime(150);
486+
487+
// After the retry's synchronous scrollTop assignment, the flag is set true.
488+
// A subsequent rAF clears it — but our mock runs rAF synchronously.
489+
expect(host.chatIsProgrammaticScroll).toBe(false);
490+
// Retry must have updated the programmatic target and scrolled.
491+
expect(host.chatProgrammaticScrollTarget).toBe(container.scrollHeight);
312492
});
313493
});

‎ui/src/ui/app-scroll.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type ScrollHost = {
1414
chatUserNearBottom: boolean;
1515
chatHeaderControlsHidden: boolean;
1616
chatNewMessagesBelow: boolean;
17+
chatIsProgrammaticScroll: boolean;
18+
chatProgrammaticScrollTarget: number;
1719
logsScrollFrame: number | null;
1820
logsAtBottom: boolean;
1921
topbarObserver: ResizeObserver | null;
@@ -75,11 +77,17 @@ export function scheduleChatScroll(host: ScrollHost, force = false, smooth = fal
7577
typeof window.matchMedia !== "function" ||
7678
!window.matchMedia("(prefers-reduced-motion: reduce)").matches);
7779
const scrollTop = target.scrollHeight;
80+
host.chatProgrammaticScrollTarget = scrollTop;
81+
host.chatIsProgrammaticScroll = true;
7882
if (typeof target.scrollTo === "function") {
7983
target.scrollTo({ top: scrollTop, behavior: smoothEnabled ? "smooth" : "auto" });
8084
} else {
8185
target.scrollTop = scrollTop;
8286
}
87+
// Clear the flag after the scroll event has fired (sync or next microtask).
88+
requestAnimationFrame(() => {
89+
host.chatIsProgrammaticScroll = false;
90+
});
8391
host.chatUserNearBottom = true;
8492
host.chatNewMessagesBelow = false;
8593
const retryDelay = effectiveForce ? 150 : 120;
@@ -98,7 +106,12 @@ export function scheduleChatScroll(host: ScrollHost, force = false, smooth = fal
98106
if (!shouldStickRetry) {
99107
return;
100108
}
109+
host.chatProgrammaticScrollTarget = latest.scrollHeight;
110+
host.chatIsProgrammaticScroll = true;
101111
latest.scrollTop = latest.scrollHeight;
112+
requestAnimationFrame(() => {
113+
host.chatIsProgrammaticScroll = false;
114+
});
102115
host.chatUserNearBottom = true;
103116
}, retryDelay);
104117
});
@@ -135,6 +148,17 @@ export function handleChatScroll(host: ScrollHost, event: Event) {
135148
const scrollTop = Math.max(0, container.scrollTop);
136149
const delta = scrollTop - host.chatLastScrollTop;
137150
host.chatLastScrollTop = scrollTop;
151+
// Ignore scroll events that we ourselves triggered — they must not flip
152+
// chatUserNearBottom to false while streaming content grows the page.
153+
// Only suppress if scrollTop is still at or above the position we scrolled to;
154+
// if it dropped below, the user scrolled up during the guard window and we must
155+
// process the event so streaming stops pinning them back to the bottom.
156+
if (
157+
host.chatIsProgrammaticScroll &&
158+
container.scrollTop >= host.chatProgrammaticScrollTarget - container.clientHeight
159+
) {
160+
return;
161+
}
138162
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight;
139163
host.chatUserNearBottom = distanceFromBottom < NEAR_BOTTOM_THRESHOLD;
140164
const hasUsefulScroll = container.scrollHeight - container.clientHeight > NEAR_BOTTOM_THRESHOLD;
@@ -168,6 +192,8 @@ export function resetChatScroll(host: ScrollHost) {
168192
host.chatLastScrollTop = 0;
169193
host.chatHeaderControlsHidden = false;
170194
host.chatNewMessagesBelow = false;
195+
host.chatIsProgrammaticScroll = false;
196+
host.chatProgrammaticScrollTarget = 0;
171197
}
172198

173199
export function exportLogs(lines: string[], label: string) {

‎ui/src/ui/app.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ export class OpenClawApp extends LitElement {
592592
chatLastScrollTop = 0;
593593
chatHasAutoScrolled = false;
594594
chatUserNearBottom = true;
595+
chatIsProgrammaticScroll = false;
596+
chatProgrammaticScrollTarget = 0;
595597
@state() chatNewMessagesBelow = false;
596598
nodesPollInterval: number | null = null;
597599
logsPollInterval: number | null = null;

0 commit comments

Comments
 (0)