Skip to content

fix(app): keep the transcript pinned to the bottom when WebKit clamps the scroll position - #5390

Merged
liruifengv merged 2 commits into
release/2026-11-25from
chengluyu-fix-mobile-chat-scroll
Nov 25, 2026
Merged

liruifengv merged 2 commits into
release/2026-11-25from
chengluyu-fix-mobile-chat-scroll

Conversation

@chengluyu

@chengluyu chengluyu commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在 iOS(Safari、iOS Chrome,都是 WebKit 内核)上通过 Web 或 Remote Control 打开会话,常常停在会话中间,而不是最底部;点了「最新消息」回到底部后,流式回复过一会儿又不再跟随。这个 PR 修好后,打开会话会停在底部,流式回复期间保持跟随。

Bug Reproduction Steps

  1. iPhone 上用 Chrome 或 Safari 打开 kimi web / Remote Control 页面。
  2. 从会话列表进入一个较长的历史会话:页面先滚到底,随即跳到上方某个位置,并出现「最新消息」按钮。
  3. 点「最新消息」回到底部,发一条消息让模型流式回复:过一会儿就不再跟随新内容。

Playwright 的 WebKit 加手机视口也能稳定复现;Chromium 不复现。

Root Cause

Markdown 渲染器在重新渲染最后一行时,会先卸载再挂回代码块等节点。两步之间有代码强制触发了一次布局,所以内容会短暂变矮。WebKit 会立刻把 scrollTop 截断到这时更小的最大值;Chromium 会推迟这种截断,所以不受影响。内容在同一个任务里又长回原高度,scrollHeight 恢复了,scrollTop 却停在被截断后的位置。

滚动控制器随后在 scroll 事件或 resize 回调的 correct() 里看到位置意外上移、离底部超过阈值,就把它当成用户上滑,退出 follow 进入 anchored。于是页面停在中间,后续流式内容也不再跟随。

sequenceDiagram
    participant R as 渲染器(同一任务内)
    participant W as WebKit
    participant C as 滚动控制器
    R->>R: 卸载代码块
    R->>W: 强制布局
    W->>W: 内容变矮,scrollTop 被截断
    R->>R: 挂回代码块,内容恢复原高
    W-->>C: scroll 事件 / resize 回调里的 correct()
    C->>C: 改动前:判定为用户上滑,退出 follow
    C->>C: 改动后:判定为截断,写回底部
Loading

这是根本修复:控制器能分辨这种并非用户造成的截断,不依赖渲染器的具体行为。

真机日志还暴露了两个时序细节,都在这次一起处理:

  • 截断、恢复和 correct() 可能发生在同一次 Vue 更新里,这时 MutationObserver 回调还没执行。
  • 120Hz 设备上一帧只有约 8ms。

Code Changes

核心思路: 在 follow 模式下,满足三个条件的向上位移按截断处理,直接写回底部、保持 follow:

  • 刚有内容变化;
  • 没有任何用户输入在进行;
  • 位置向上移动。

内容变化来自滚动容器子树上的 MutationObserver。判断时先用 takeRecords() 同步读取还没投递的记录,再看「两帧内有过变化」的标记。只有确实没有内容变化时,意外滚动才像以前一样视为用户滚动。

 handleScroll(layout)
   if top < expected && (top at max || layout follow clamp ≤ 80px)
     adopt
+  if follow && top < expected && no wheel/key/touch/drag/press in flight
+      && (pending mutation records || mutated within the last two frames)
+    adopt top, write bottom, stay in follow
   ... treat as user scroll → anchored

此外还有两处输入判定上的补充:

  • 按住鼠标算作用户输入: 非滚动条位置的主键或中键按下,新增 press 意图。按住期间的滚动(比如拖选文字触发的自动滚动)仍算用户操作,会正常退出 follow。中键自动滚动在松开后还会继续,所以中键按下保持 sticky,直到下一次按下或回到底部才清除。
  • 手指下的元素被替换时也能结束触摸: touchend / touchcancel 现在也监听在 touchstart 的目标元素上。流式渲染替换了手指下的节点后,touchend 不会再冒泡到滚动容器,旧代码里 touching 会一直为 true,控制器从此不再写滚动位置。
阅读指南
  • packages/app-core/src/lib/transcriptScroll/scrollController.ts:contentClamp、onMutation、followTouchTarget,以及 press / drag-end 分支。
  • packages/app-core/src/lib/transcriptScroll/intent.ts:pointerdown 新增 press 意图。
  • packages/app-core/test/transcriptScroll/layoutSim.ts:模拟器新增假的 MutationObserver,包括 takeRecords 和 mutate / queueMutation,以及帧内取消回调的语义。

Behavior Changes and Affected Users

行为 改动前 改动后 谁依赖旧行为 退路
follow 模式下,内容刚变化、又没有用户输入时,位置被浏览器截断上移 视为用户上滑,退出 follow 写回底部,保持 follow 无。旧行为就是这个 bug 无需
follow 模式下,没有内容变化、也没有输入的上移(页内查找、读屏软件) 退出 follow 不变 页内查找、读屏用户 —
流式输出且处于 follow 时,浏览器原生页内查找跳到上方匹配 退出 follow,停在匹配处 被当成截断拉回底部 在 Web(含 Remote Control)里通过浏览器菜单、iOS「在页面中查找」或浮层打开时用浏览器原生查找的用户。Cmd/Ctrl+F 打开的是应用内搜索,不受影响;Desktop 应用的对话页没有原生查找 用应用内搜索(Cmd/Ctrl+F),或等流式结束后再用浏览器查找
内容变化期间,通过触摸、滚轮、键盘、滚动条拖动或按住鼠标上移 退出 follow 不变 所有客户端 —
内容变化期间,中键自动滚动松开后继续上移 退出 follow 不变(sticky press) 使用中键自动滚动的 Chromium 用户 —
readScrollIntent 对非滚动条位置的主键或中键 pointerdown 返回 null 返回 { kind: 'press', sticky } 仅滚动控制器使用这个内部 API —
手指下的节点在 touchend 前被替换 touching 卡住,follow 不再写位置 正常结束触摸,继续跟随 无。旧行为是 bug 无需

受影响的界面是 Desktop 和 Web(含 kimi web、Remote Control)的对话滚动,WebKit 内核受影响最明显。Chromium 和 Electron 上不会出现这种截断,新分支基本不会被触发。

测试覆盖与验证
  • scrollController.test.ts 新增以下用例,去掉对应修复都会失败:
    • 内容替换导致截断后保持 follow;
    • 截断发生在下一帧,或在 MutationObserver 回调之前;
    • 帧回调内的变化顺延了窗口;
    • 触摸、按住鼠标、中键松开后的自动滚动在内容变化时仍会退出 follow;
    • 内容变化两帧以后的无输入滚动仍会退出 follow;
    • 被触摸的节点被替换后能结束触摸。
  • intent.test.ts:按设计改了原断言,非滚动条主键或中键按下由 null 变为 press。
  • pnpm exec vitest run --reporter=agent packages/app-core/test/transcriptScroll:118 passed。
  • pnpm lint:通过。
  • pnpm --filter kimi-code-app run typecheck:通过。
  • 手工验证:
    • Playwright WebKit,390×664 触摸视口:首次打开、从会话列表切换会话、约 13 秒流式回复,全程停在底部并保持 follow。修复前同一脚本会停在底部上方约 970px。
    • Playwright Chromium:打开、流式、上滑退出、点「最新消息」恢复跟随均正常。最后几处改动之后没有在 Chromium 上重跑。
    • iPhone(iOS 26,Chrome)真机通过 Remote Control 加临时调试日志定位问题,最终版本由报告人确认打开会话停在底部。
  • 只在 macOS 上的 WebKit、Chromium 模拟和 iOS 真机上核对过,没有在 Windows 和 Linux 上运行。
  • 未运行 e2e。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.(按维护者决定,本 PR 不带 changeset)
  • Ran gen-docs skill, or this PR needs no doc update.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T06:24:17.580245Z c728f9a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: c728f9a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e24030f42b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@liruifengv

Copy link
Copy Markdown
Collaborator

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: c728f9acad

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@c728f9a
npx https://pkg.pr.new/@moonshot-ai/kimi-code@c728f9a

commit: c728f9a

@liruifengv
liruifengv merged commit 6eb6df5 into release/2026-11-25 Nov 25, 2026
23 of 25 checks passed
@liruifengv
liruifengv deleted the chengluyu-fix-mobile-chat-scroll branch November 25, 2026 07:08
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
… the scroll position (#5390)

* fix(app): keep the transcript pinned to the bottom when WebKit clamps the scroll position

* chore: drop changesets for the transcript scroll fix

---------

Co-authored-by: liruifengv <[email protected]>
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
… the scroll position (#5390)

* fix(app): keep the transcript pinned to the bottom when WebKit clamps the scroll position

* chore: drop changesets for the transcript scroll fix

---------

Co-authored-by: liruifengv <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants