From ad0c4f076706e4b6686d8e9dacd472f66452151d Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:33:29 -0500 Subject: [PATCH] fix(ui): keep mobile task output above the composer (#15282) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - People read agent output in the task conversation. > - Mobile tasks scroll the document and keep the composer above the footer. > - The document body keeps a fixed height while the thread grows. > - The old observer misses late thread growth, and a layout scroll event can cancel follow mode. > - This pull request observes the thread and preserves follow mode until the reader scrolls up. > - The benefit is visible new output without repeated manual scrolling. ## Linked Issues or Issue Description **What happened?** New task output can move behind the mobile composer while the reader is already at the bottom. Late content layout does not resize the fixed-height body. A scroll event after layout growth can also clear follow mode before the resize observer runs. **Expected behavior** Keep new output visible above the composer while the reader follows the bottom. Hold the reading position after an upward scroll. Resume following when the reader returns to the bottom. **Steps to reproduce** 1. Open a long task conversation on mobile and scroll to the bottom. 2. Let a streamed row or delayed content grow after the parent renders. 3. Check whether the latest output stays above the composer. 4. Scroll up to read earlier output, then return to the bottom. **Paperclip version or commit** Reproduced by regression tests against master at 6c36c07a4. **Deployment mode** Built from source. This is a mobile UI bug and does not depend on an adapter or database. Related work: #15228, #13229, and #13095. The search found no duplicate fix for mobile document auto-follow. ## What Changed - Observe the conversation box as well as the body for late layout growth. - Preserve bottom-follow intent when a layout scroll event arrives without an upward scroll. - Reconcile document scrolling when the viewport resizes. - Add regressions for late growth, event ordering, reading position, return to bottom, and viewport changes. - Add a bounded Storybook streaming fixture with the production thread, composer, and mobile footer. ## Verification - 244 focused tests pass across `useWindowAutoFollow`, `TaskMessageScroller`, and `TaskChatThread`. Three new regressions fail before the fix. - All 7,439 UI tests pass across 683 files (`pnpm exec vitest run --project @paperclipai/ui`). - `pnpm check:token-gates`, `pnpm -r typecheck`, and `pnpm build` pass. - The full repository test shards and browser E2E checks pass in CI on commit 43354eb38. The duplicate local `pnpm test:run` was stopped after 37 minutes once those CI test shards passed; that local run did not complete. - Browser check at 402 by 874: controlled streamed output stayed at document bottom, with the newest paragraph above the composer. Scrolling up held position as output grew. Returning to bottom resumed following. - Open Storybook's **Task chat / Mobile streaming follow / Streaming** story to repeat the check. The fixture makes no agent or provider calls. - Greptile is 5/5 on commit 43354eb38. All review threads are resolved. - All CI checks pass on the final commit, including the canary packaging gate. ## Risks - Scroll event order differs between browsers. The regression tests cover a scroll event before resize reconciliation and deliberate upward scrolling during growth. - The change affects the mobile document scroll owner. Desktop scrolling stays covered by its existing tests. - Existing same-task targets and browser history restoration still pass. ## Model Used OpenAI GPT-6 through Codex. The exact serving variant and context window are not exposed in this session. Capabilities used: reasoning, code editing, tool execution, and browser verification. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- .../task-chat/useWindowAutoFollow.test.tsx | 67 ++++++++++++++++++- .../task-chat/useWindowAutoFollow.ts | 21 ++++-- .../task-chat-mobile-follow.stories.tsx | 57 ++++++++++++++++ 3 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 ui/storybook/stories/task-chat-mobile-follow.stories.tsx diff --git a/ui/src/components/task-chat/useWindowAutoFollow.test.tsx b/ui/src/components/task-chat/useWindowAutoFollow.test.tsx index ba43d080b98..4cfde316a28 100644 --- a/ui/src/components/task-chat/useWindowAutoFollow.test.tsx +++ b/ui/src/components/task-chat/useWindowAutoFollow.test.tsx @@ -8,7 +8,7 @@ import { TaskChatScrollNavigation } from "./scroll-navigation"; function Host({ contentKey, enabled }: { contentKey: unknown; enabled: boolean }) { useWindowAutoFollow(contentKey, enabled); - return
thread
; + return
thread
; } /** @@ -189,6 +189,71 @@ describe("useWindowAutoFollow", () => { expect(scrollToCalls).toHaveLength(0); }); + it("follows a late thread resize without a React content update or body resize", async () => { + const observed = new Set(); + let resizeThread = () => {}; + vi.stubGlobal("ResizeObserver", class { + constructor(callback: ResizeObserverCallback) { + resizeThread = () => { + const thread = container.querySelector('[data-testid="task-chat-thread"]')!; + if (observed.has(thread)) callback([{ target: thread } as ResizeObserverEntry], this as unknown as ResizeObserver); + }; + } + observe(element: Element) { observed.add(element); } + disconnect() { observed.clear(); } + }); + const geometry = fakeWindowGeometry(); + render(0); + scrollToCalls = []; + + geometry.setScrollHeight(2400); + resizeThread(); + + expect(scrollToCalls).toContain(2400); + expect(window.scrollY).toBe(1600); + }); + + it("keeps following when a scroll event arrives after growth before reconciliation", async () => { + const geometry = fakeWindowGeometry(); + render(0); + + geometry.setScrollHeight(2400); + await scrollWindowTo(1200); // Still at the old bottom; the user did not move up. + scrollToCalls = []; + render(1); + + expect(scrollToCalls).toContain(2400); + }); + + it("holds an upward user scroll during growth and resumes after returning to the bottom", async () => { + const geometry = fakeWindowGeometry(); + render(0); + + geometry.setScrollHeight(2400); + await scrollWindowTo(900); + scrollToCalls = []; + render(1); + expect(scrollToCalls).toHaveLength(0); + + await scrollWindowTo(1600); + geometry.setScrollHeight(2700); + render(2); + expect(scrollToCalls).toContain(2700); + }); + + it("follows viewport resizing while pinned and holds it while reading older output", async () => { + render(0); + Object.defineProperty(window, "innerHeight", { value: 600, configurable: true }); + window.dispatchEvent(new Event("resize")); + expect(window.scrollY).toBe(1400); + + await scrollWindowTo(500); + scrollToCalls = []; + Object.defineProperty(window, "innerHeight", { value: 700, configurable: true }); + window.dispatchEvent(new Event("resize")); + expect(scrollToCalls).toHaveLength(0); + }); + it("does nothing when disabled", () => { render(0, false); expect(scrollToCalls).toHaveLength(0); diff --git a/ui/src/components/task-chat/useWindowAutoFollow.ts b/ui/src/components/task-chat/useWindowAutoFollow.ts index df17a1dcfcd..d971e94e42f 100644 --- a/ui/src/components/task-chat/useWindowAutoFollow.ts +++ b/ui/src/components/task-chat/useWindowAutoFollow.ts @@ -32,11 +32,13 @@ function scrollWindowToBottom(): void { */ export function useWindowAutoFollow(contentKey: unknown, enabled: boolean): void { const pinnedRef = useRef(true); + const lastScrollYRef = useRef(window.scrollY); const navigation = useTaskChatScrollNavigation(); const initialPositionApplied = useRef(false); const appliedNavigation = useRef({ key: navigation.key, hash: navigation.hash }); const anchorRef = useRef(null); const rememberAnchor = () => { + lastScrollYRef.current = window.scrollY; const root = document.querySelector('[data-testid="task-chat-thread"]'); if (root) anchorRef.current = readThreadScrollAnchor(root, 0, window.innerHeight); if (initialPositionApplied.current) navigation.remember(window.scrollY, anchorRef.current); @@ -56,16 +58,22 @@ export function useWindowAutoFollow(contentKey: unknown, enabled: boolean): void useEffect(() => { if (!enabled) return; const onScroll = () => { - pinnedRef.current = windowPinned(); + // Content or viewport growth can leave a gap below the old bottom before + // ResizeObserver runs. Only an upward scroll gives up existing follow + // intent; a scroll event at the same position must not strand the output. + pinnedRef.current = windowPinned() || (pinnedRef.current && window.scrollY >= lastScrollYRef.current); rememberAnchor(); }; window.addEventListener("scroll", onScroll, { passive: true }); - return () => window.removeEventListener("scroll", onScroll); + window.addEventListener("resize", reconcile); + return () => { + window.removeEventListener("scroll", onScroll); + window.removeEventListener("resize", reconcile); + }; }, [enabled, navigation.key, navigation.hash, navigation.ready]); useLayoutEffect(() => { if (!enabled || typeof ResizeObserver === "undefined") return; - const observed = document.body; let previousScrollHeight = scrollingElement()?.scrollHeight ?? null; const observer = new ResizeObserver(() => { const nextScrollHeight = scrollingElement()?.scrollHeight ?? null; @@ -78,7 +86,12 @@ export function useWindowAutoFollow(contentKey: unknown, enabled: boolean): void previousScrollHeight = nextScrollHeight; reconcile(); }); - observer.observe(observed); + // The mobile body is viewport-sized even when its contents overflow. Watch + // the thread box too, so streamed rows, images and expanding tools follow + // after their own layout changes without needing a new parent content key. + observer.observe(document.body); + const thread = document.querySelector('[data-testid="task-chat-thread"]'); + if (thread) observer.observe(thread); return () => observer.disconnect(); }, [enabled, navigation.key, navigation.hash, navigation.ready]); diff --git a/ui/storybook/stories/task-chat-mobile-follow.stories.tsx b/ui/storybook/stories/task-chat-mobile-follow.stories.tsx new file mode 100644 index 00000000000..1fb4d9b3cad --- /dev/null +++ b/ui/storybook/stories/task-chat-mobile-follow.stories.tsx @@ -0,0 +1,57 @@ +import { useEffect, useState, type CSSProperties } from "react"; +import type { Meta, StoryObj } from "@storybook/react-vite"; +import { MobileBottomNav } from "@/components/MobileBottomNav"; +import { TaskChatComposer } from "@/components/task-chat/TaskChatComposer"; +import { TaskChatComposerDock } from "@/components/task-chat/TaskChatComposerDock"; +import { TaskChatThreadView } from "@/components/task-chat/TaskChatThreadView"; +import { useWindowAutoFollow } from "@/components/task-chat/useWindowAutoFollow"; + +function StreamingResponse({ running }: { running: boolean }) { + const [lines, setLines] = useState(16); + useEffect(() => { + if (!running || lines >= 256) return; + const timer = window.setTimeout(() => setLines(lines + 1), 500); + return () => window.clearTimeout(timer); + }, [running, lines]); + return `Update ${index + 1}: I checked the next section and recorded the results for the final report.`).join("\n\n"), + }, + ]} />; +} + +function MobileFollow() { + const [running, setRunning] = useState(true); + // The child updates independently, exercising late layout growth as well as + // normal streaming without relying on a parent content-key notification. + useWindowAutoFollow(0, true); + return
+
Streaming progress report
+
+
+ + + setRunning(true)} + onStop={running ? async () => setRunning(false) : undefined} /> + +
+
+ +
; +} + +const meta = { + title: "Task chat/Mobile streaming follow", + component: MobileFollow, + parameters: { + layout: "fullscreen", + docs: { description: { component: "Controlled output in the production thread and composer. At the bottom, updates stay visible. Scroll up to hold your reading position, then return to the bottom to resume following. Stop pauses the fixture; send a message to resume. No agent or provider calls." } }, + }, + globals: { theme: "light", viewport: { value: "mobile", isRotated: false } }, +} satisfies Meta; +export default meta; +type Story = StoryObj; +export const Streaming: Story = {};