Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(ui): stabilize conversation bottom anchoring (#1221)
hushaowu-rh722a6705e8e5
diff --git a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.tsindex 6bf3ef8252..1ef0e421c0 100644--- a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts+++ b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts@@ -9,7 +9,9 @@import { describe, expect, it } from 'vitest';import {+ resolveLastUserMessageObservation,resolveNearBottomOnScroll,+ resolveRenderPinDecision,shouldUnpinOnUpIntent,shouldUnpinOnWheel,UNPIN_MIN_SCROLLABLE_PX,@@ -165,3 +167,62 @@ describe('resolveNearBottomOnScroll', () => {).toBe(true);});});++describe('resolveRenderPinDecision', () => {+ it('explicit tail send takes ownership from a restored history anchor', () => {+ expect(resolveRenderPinDecision({+ restoring: true,+ newUserSend: true,+ nearBottom: false,+ })).toEqual({ clearRestoring: true, pinToBottom: true });+ });++ it('restored history remains anchored until an explicit user send', () => {+ expect(resolveRenderPinDecision({+ restoring: true,+ newUserSend: false,+ nearBottom: false,+ })).toEqual({ clearRestoring: false, pinToBottom: false });+ });++ it('keeps ordinary near-bottom auto-follow behavior', () => {+ expect(resolveRenderPinDecision({+ restoring: false,+ newUserSend: false,+ nearBottom: true,+ })).toEqual({ clearRestoring: false, pinToBottom: true });+ expect(resolveRenderPinDecision({+ restoring: false,+ newUserSend: false,+ nearBottom: false,+ })).toEqual({ clearRestoring: false, pinToBottom: false });+ });+});++describe('resolveLastUserMessageObservation', () => {+ it('seeds a restored user tail hydrated after mount without treating it as a send', () => {+ expect(+ resolveLastUserMessageObservation({+ restoring: true,+ tailUserMessageId: 'historical-user',+ previousTailUserMessageId: null,+ }),+ ).toEqual({+ baselineUserMessageId: 'historical-user',+ isNewUserSend: false,+ });+ });++ it('still detects a later user send after the restored baseline', () => {+ expect(+ resolveLastUserMessageObservation({+ restoring: true,+ tailUserMessageId: 'new-user',+ previousTailUserMessageId: 'historical-user',+ }),+ ).toEqual({+ baselineUserMessageId: 'historical-user',+ isNewUserSend: true,+ });+ });+});diff --git a/apps/desktop/src/renderer/components/chat/MessageStream.tsx b/apps/desktop/src/renderer/components/chat/MessageStream.tsxindex e8644db823..b5757759f9 100644--- a/apps/desktop/src/renderer/components/chat/MessageStream.tsx+++ b/apps/desktop/src/renderer/components/chat/MessageStream.tsx@@ -188,6 +188,8 @@ import {} from './viewportFillDetect';import {resolveNearBottomOnScroll,+ resolveLastUserMessageObservation,+ resolveRenderPinDecision,shouldUnpinOnUpIntent,shouldUnpinOnWheel,} from './autoFollowIntent';@@ -2196,7 +2198,10 @@ export function MessageStream({const prevScrollTopRef = useRef(0);/** clientId of the last user-role message we've already observed. Used to* detect a NEW user send → force pin regardless of prior scroll state. */- const lastUserMsgIdRef = useRef<string | null>(null);+ const lastUserMsg = messages[messages.length - 1];+ const lastUserMsgIdRef = useRef<string | null>(+ lastUserMsg?.role === 'user' ? lastUserMsg.clientId : null,+ );// ── render-window state ──// null = 默认窗口(取末尾 RENDER_WINDOW_INITIAL_ITEMS 个 item);非 null = 锚定到@@ -2982,12 +2987,6 @@ export function MessageStream({// result land.// biome-ignore lint/correctness/useExhaustiveDependencies: bottomPadding 是触发型依赖;overlay 高度变化时即使 effect 内不读取它,也必须重新 pin 到底。useLayoutEffect(() => {- // 还原中:跳过一切 auto-follow,位置由下面的还原 effect / ResizeObserver 接管。- if (restoringRef.current) {- const el = scrollRef.current;- if (el) prevScrollTopRef.current = el.scrollTop;- return;- }// tail item 在 visibleRenderItems 与 allRenderItems 末尾完全一致(window 始终// 包含最新的一段),用 visibleRenderItems 避免扩窗时多触发一次。// 用户消息总是产出独立的 message item(不进 segment / 不被丢弃),所以这里@@ -2995,15 +2994,26 @@ export function MessageStream({const lastItem = visibleRenderItems[visibleRenderItems.length - 1];const lastUserMsg =lastItem?.type === 'message' && lastItem.message.role === 'user' ? lastItem.message : null;- const isNewUserSend = lastUserMsg !== null && lastUserMsg.clientId !== lastUserMsgIdRef.current;+ const userMessageObservation = resolveLastUserMessageObservation({+ restoring: restoringRef.current,+ tailUserMessageId: lastUserMsg?.clientId ?? null,+ previousTailUserMessageId: lastUserMsgIdRef.current,+ });+ lastUserMsgIdRef.current = userMessageObservation.baselineUserMessageId;+ const decision = resolveRenderPinDecision({+ restoring: restoringRef.current,+ newUserSend: userMessageObservation.isNewUserSend,+ nearBottom: isNearBottomRef.current,+ });- if (isNewUserSend) {+ if (userMessageObservation.isNewUserSend && lastUserMsg) {lastUserMsgIdRef.current = lastUserMsg.clientId;+ }+ if (decision.clearRestoring) {+ restoringRef.current = false;isNearBottomRef.current = true;- pinToBottom();- } else if (isNearBottomRef.current) {- pinToBottom();}+ if (decision.pinToBottom) pinToBottom();const el = scrollRef.current;if (el) prevScrollTopRef.current = el.scrollTop;diff --git a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts b/apps/desktop/src/renderer/components/chat/autoFollowIntent.tsindex 99e55b0f6a..3176aab091 100644--- a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts+++ b/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts@@ -84,6 +84,75 @@ export function shouldUnpinOnUpIntent({ scrollHeight, clientHeight }: UpIntentUnreturn scrollHeight - clientHeight > UNPIN_MIN_SCROLLABLE_PX;}+export interface ResolveRenderPinArgs {+ /** A saved non-bottom viewport is currently being restored. */+ restoring: boolean;+ /** The current render introduced a new user message at the tail. */+ newUserSend: boolean;+ /** Auto-follow was active before this render. */+ nearBottom: boolean;+}++export interface ResolveRenderPinDecision {+ /** Explicit sends hand ownership back to the latest-message anchor. */+ clearRestoring: boolean;+ /** Pin the scroll container to its content end in this layout pass. */+ pinToBottom: boolean;+}++export interface ResolveLastUserMessageObservationArgs {+ /** A saved non-bottom viewport is currently being restored. */+ restoring: boolean;+ /** The current render's tail user message, if any. */+ tailUserMessageId: string | null;+ /** The last tail user message already observed by the mounted stream. */+ previousTailUserMessageId: string | null;+}++export interface ResolveLastUserMessageObservation {+ /** Baseline to store after observing the current render. */+ baselineUserMessageId: string | null;+ /** Whether the current tail user message is a newly sent message. */+ isNewUserSend: boolean;+}++/**+ * Distinguish restored history hydration from a user send at the tail.+ *+ * A restored stream can mount before its first history batch arrives. If that+ * batch ends in a user message, it must establish the baseline rather than+ * taking ownership from the restored viewport as a new send.+ */+export function resolveLastUserMessageObservation({+ restoring,+ tailUserMessageId,+ previousTailUserMessageId,+}: ResolveLastUserMessageObservationArgs): ResolveLastUserMessageObservation {+ const baselineUserMessageId =+ restoring && previousTailUserMessageId === null && tailUserMessageId !== null+ ? tailUserMessageId+ : previousTailUserMessageId;+ return {+ baselineUserMessageId,+ isNewUserSend: tailUserMessageId !== null && tailUserMessageId !== baselineUserMessageId,+ };+}++/**+ * Resolve the render-time priority between a saved history anchor and auto-follow.+ * Reopening a session must preserve a real reading position, but a user message+ * sent during that mounted session is an explicit request to resume at the tail.+ */+export function resolveRenderPinDecision({+ restoring,+ newUserSend,+ nearBottom,+}: ResolveRenderPinArgs): ResolveRenderPinDecision {+ if (newUserSend) return { clearRestoring: restoring, pinToBottom: true };+ if (restoring) return { clearRestoring: false, pinToBottom: false };+ return { clearRestoring: false, pinToBottom: nearBottom };+}+export interface ResolveNearBottomArgs {/** scroll 事件前的跟随态(isNearBottomRef) */wasNearBottom: boolean;diff --git a/apps/mobile/src/__tests__/messageListVirtualization.test.ts b/apps/mobile/src/__tests__/messageListVirtualization.test.tsindex 087d4ab745..f9d98169c6 100644--- a/apps/mobile/src/__tests__/messageListVirtualization.test.ts+++ b/apps/mobile/src/__tests__/messageListVirtualization.test.ts@@ -49,4 +49,12 @@ describe('mobile message list container', () => {expect(focusEffectSource).toContain('userScrollForOlderRef.current = true');expect(focusEffectSource).toContain('lastAutoLoadEarlierKeyRef.current = null');});++ it('protects follow state while imperative scroll and verifies the cold-open anchor', () => {+ const source = readFileSync(resolve(process.cwd(), 'src/session/MessageRenderer.tsx'), 'utf8');+ expect(source).toContain('programmaticScrollInFlight: programmaticScrollInFlightRef.current');+ expect(source).toContain('evaluateMobileAnchorVerify({');+ expect(source).toContain('initialAnchorVerifyFrameRef');+ expect(source).toContain('scrollToEndProgrammatically(false)');+ });});diff --git a/apps/mobile/src/__tests__/messageScroll.test.ts b/apps/mobile/src/__tests__/messageScroll.test.tsindex 54d51d1a95..3b13096383 100644--- a/apps/mobile/src/__tests__/messageScroll.test.ts+++ b/apps/mobile/src/__tests__/messageScroll.test.ts@@ -473,6 +473,21 @@ describe('resolveMobileNearBottomOnScroll', () => {scrollDelta: -10,})).toBe(true);});++ it('命令式滚动 settling 时忽略瞬时 metrics', () => {+ expect(resolveMobileNearBottomOnScroll({+ wasNearBottom: true,+ metrics: metricsAt(500),+ scrollDelta: -700,+ programmaticScrollInFlight: true,+ })).toBe(true);+ expect(resolveMobileNearBottomOnScroll({+ wasNearBottom: false,+ metrics: metricsAt(1200),+ scrollDelta: 300,+ programmaticScrollInFlight: true,+ })).toBe(false);+ });});describe('evaluateMobileFollowEndContentSizePin (贴底补滚振荡断路器)', () => {diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsxindex 5fef0f44ac..ccfecb0927 100644--- a/apps/mobile/src/session/MessageRenderer.tsx+++ b/apps/mobile/src/session/MessageRenderer.tsx@@ -285,6 +285,7 @@ import {buildMessageLoadEarlierAction,createMobileFollowEndPinState,evaluateMessageWindowUpdate,+ evaluateMobileAnchorVerify,evaluateMobileFollowEndContentSizePin,mobileMessageListTopPadding,MOBILE_FOLLOW_END_PIN_SUPPRESS_MS,@@ -318,6 +319,10 @@ const MESSAGE_CONTROL_HIT_SLOP = { bottom: 10, left: 10, right: 10, top: 10 };* 更长会放大「进入会话到内容可见」的感知延迟,不取。*/const MOBILE_INITIAL_ANCHOR_SETTLE_MS = 300;+/** Maximum time a native imperative scroll may be reported as in-flight. */+const MOBILE_PROGRAMMATIC_SCROLL_SETTLE_MS = 1000;+/** Animated jump-to-latest commands get a little more time to settle. */+const MOBILE_PROGRAMMATIC_ANIMATED_SCROLL_SETTLE_MS = 1400;/** Cold-open history fill is useful, but bounded so a short/duplicate host page cannot drain history forever. */const MAX_INITIAL_HISTORY_AUTOFILL_PAGES = 3;const MOBILE_MESSAGE_ESTIMATED_ITEM_SIZE = 140;@@ -554,6 +559,9 @@ export function MessageRenderer({const readingOlderRef = useRef(false);// 每次补页分配 generation:旧会话 / 旧请求的异步 settle 不得清掉新请求的抑制态。const readingOlderRequestGenerationRef = useRef(0);+ const programmaticScrollGenerationRef = useRef(0);+ const programmaticScrollTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);+ const programmaticScrollInFlightRef = useRef(false);const previousFollowLatestRequestKeyRef = useRef(followLatestRequestKey);const previousItemKeysRef = useRef<readonly string[]>([]);const scrollMetricsRef = useRef<MessageScrollMetrics>({@@ -571,8 +579,9 @@ export function MessageRenderer({// (弃用原因见下方 LegendList props 注释)。首批 items commit 后 rAF 命令式落底一次,// 目标偏差由 handleContentSize 的贴底补滚随后续测量自然校正。const initialAnchorDoneRef = useRef(false);- // 落底 rAF / 揭开 timer 的句柄(生命周期 = 每个 scrollResetKey 一轮,不挂 effect- // cleanup——原因见冷开落底 effect 的注释;清旧在落底 effect 自身开头,卸载时兜底清)。+ const initialAnchorGenerationRef = useRef(0);+ const initialAnchorVerifyFrameRef = useRef<number | null>(null);+ // 落底 rAF / verify loop / 揭开 timer 的句柄(生命周期 = 每个 scrollResetKey 一轮)。const initialAnchorFrameRef = useRef<number | null>(null);const initialRevealTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);// settle 遮罩:落底两段式期间列表保持 opacity 0,settle 窗口后揭开(规则 7 防跳动)。@@ -593,22 +602,29 @@ export function MessageRenderer({lastAutoLoadEarlierKeyRef.current = null;readingOlderRef.current = false;readingOlderRequestGenerationRef.current += 1;+ programmaticScrollGenerationRef.current += 1;+ programmaticScrollInFlightRef.current = false;+ if (programmaticScrollTimerRef.current !== null) {+ clearTimeout(programmaticScrollTimerRef.current);+ programmaticScrollTimerRef.current = null;+ }previousItemKeysRef.current = [];scrollMetricsRef.current = { contentHeight: 0, offsetY: 0, viewportHeight: 0 };followEndPinStateRef.current = createMobileFollowEndPinState();initialAnchorDoneRef.current = false;+ initialAnchorGenerationRef.current += 1;+ if (initialAnchorFrameRef.current !== null) {+ cancelAnimationFrame(initialAnchorFrameRef.current);+ initialAnchorFrameRef.current = null;+ }+ if (initialAnchorVerifyFrameRef.current !== null) {+ cancelAnimationFrame(initialAnchorVerifyFrameRef.current);+ initialAnchorVerifyFrameRef.current = null;+ }// settle 遮罩复位必须与列表重挂同帧(渲染期 setState,React 官方 prop-change 模式):// 走 effect 会晚一帧,新列表以旧 revealed=true 裸挂一帧,未锚定内容闪现。setListRevealed(false);}- // DEV-only:把列表控制器 + 滚动 metrics 暴露给性能 harness(临时,profiling/回归测量用)。- useEffect(() => {- if (!__DEV__) return;- devExposeList?.({- scrollTo: (y: number) => listRef.current?.scrollToOffset({ animated: false, offset: y }),- getMetrics: () => scrollMetricsRef.current,- });- }, [devExposeList]);const lastAppliedFocusKeyRef = useRef<string | null>(null);const [hasNewMessages, setHasNewMessages] = useState(false);const [isAwayFromBottom, setIsAwayFromBottom] = useState(false);@@ -618,6 +634,55 @@ export function MessageRenderer({// LightboxPage 手势 useMemo 的依赖,流式回复期间每 token 重建手势图,// 可能打断进行中的捏合/拖动手势(rule 7)。const closePayload = useCallback(() => setPayload(null), []);+ const markProgrammaticScroll = useCallback((animated: boolean) => {+ const generation = programmaticScrollGenerationRef.current + 1;+ programmaticScrollGenerationRef.current = generation;+ programmaticScrollInFlightRef.current = true;+ if (programmaticScrollTimerRef.current !== null) {+ clearTimeout(programmaticScrollTimerRef.current);+ }+ programmaticScrollTimerRef.current = setTimeout(() => {+ if (programmaticScrollGenerationRef.current !== generation) return;+ programmaticScrollTimerRef.current = null;+ programmaticScrollInFlightRef.current = false;+ }, animated+ ? MOBILE_PROGRAMMATIC_ANIMATED_SCROLL_SETTLE_MS+ : MOBILE_PROGRAMMATIC_SCROLL_SETTLE_MS);+ }, []);++ const clearProgrammaticScroll = useCallback(() => {+ programmaticScrollGenerationRef.current += 1;+ programmaticScrollInFlightRef.current = false;+ if (programmaticScrollTimerRef.current !== null) {+ clearTimeout(programmaticScrollTimerRef.current);+ programmaticScrollTimerRef.current = null;+ }+ }, []);++ const scrollToEndProgrammatically = useCallback((animated: boolean) => {+ markProgrammaticScroll(animated);+ void listRef.current?.scrollToEnd({ animated });+ }, [markProgrammaticScroll]);++ const scrollToOffsetProgrammatically = useCallback((offset: number, animated: boolean) => {+ markProgrammaticScroll(animated);+ void listRef.current?.scrollToOffset({ animated, offset });+ }, [markProgrammaticScroll]);++ const scrollToIndexProgrammatically = useCallback((index: number, viewPosition: number) => {+ markProgrammaticScroll(true);+ void listRef.current?.scrollToIndex({ animated: true, index, viewPosition });+ }, [markProgrammaticScroll]);++ // DEV-only:把列表控制器 + 滚动 metrics 暴露给性能 harness(临时,profiling/回归测量用)。+ useEffect(() => {+ if (!__DEV__) return;+ devExposeList?.({+ scrollTo: (y: number) => scrollToOffsetProgrammatically(y, false),+ getMetrics: () => scrollMetricsRef.current,+ });+ }, [devExposeList, scrollToOffsetProgrammatically]);+const listData = useMemo(() => [...items], [items]);// 遮罩重武装(review P1):真冷开(无缓存)时列表以空挂载,落底 effect 的空分支已把// 遮罩揭开;首批消息到达(0→N)且本会话尚未落底时,必须在**渲染期**重新武装遮罩——@@ -782,8 +847,8 @@ export function MessageRenderer({}setIsAwayFromBottom(false);setHasNewMessages(false);- void listRef.current?.scrollToEnd({ animated: true });- }, []);+ scrollToEndProgrammatically(true);+ }, [scrollToEndProgrammatically]);const jumpToPreviousUserMessage = useCallback(() => {if (!previousUserTarget) return;@@ -794,12 +859,8 @@ export function MessageRenderer({lastAutoLoadEarlierKeyRef.current = null;nearBottomRef.current = false;setIsAwayFromBottom(true);- void listRef.current?.scrollToIndex({- animated: true,- index: previousUserTarget.index,- viewPosition: 0.12,- });- }, [previousUserTarget]);+ scrollToIndexProgrammatically(previousUserTarget.index, 0.12);+ }, [previousUserTarget, scrollToIndexProgrammatically]);// 「跳到最新」请求(会话外部触发):命令式滚到底,之后由 handleContentSize 补滚维持贴底。useEffect(() => {@@ -816,8 +877,8 @@ export function MessageRenderer({}setHasNewMessages(false);setIsAwayFromBottom(false);- void listRef.current?.scrollToEnd({ animated: true });- }, [followLatestRequestKey]);+ scrollToEndProgrammatically(true);+ }, [followLatestRequestKey, scrollToEndProgrammatically]);// 自动加载更早:电平触发判定(shouldAutoLoadEarlier),在所有可能改变判定结果的时机重评估// (scroll 事件 / LegendList onStartReached 边沿 / eligibility 变化 effect)。@@ -908,6 +969,7 @@ export function MessageRenderer({const nearBottom = resolveMobileNearBottomOnScroll({wasNearBottom: nearBottomRef.current,metrics,+ programmaticScrollInFlight: programmaticScrollInFlightRef.current,scrollDelta: readingOlderRef.current ? 0 : metrics.offsetY - previousOffsetY,bottomOverlayHeight,});@@ -924,6 +986,7 @@ export function MessageRenderer({// 程序化 scrollToEnd 不会触发,故不会误置);同时记录拖动起点 offset,供// shouldUnpinMobileFollowOnDrag 判「相对起点累计上移」。const handleScrollBeginDrag = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {+ clearProgrammaticScroll();isDraggingRef.current = true;dragStartOffsetYRef.current = event.nativeEvent.contentOffset.y;userScrollForOlderRef.current = true;@@ -995,47 +1058,89 @@ export function MessageRenderer({followEndPinRecoveryTimerRef.current = setTimeout(() => {followEndPinRecoveryTimerRef.current = null;if (nearBottomRef.current && !readingOlderRef.current) {- void listRef.current?.scrollToEnd({ animated: false });+ scrollToEndProgrammatically(false);}}, MOBILE_FOLLOW_END_PIN_SUPPRESS_MS + 50);}if (decision.shouldScroll) {- void listRef.current?.scrollToEnd({ animated: false });+ scrollToEndProgrammatically(false);}}}, []);// 冷开落底(替代 initialScrollAtEnd,弃用原因见 LegendList props 注释):首批 items- // commit 后 rAF 一次命令式落底。此刻测量未完备、目标可能偏短,但 nearBottomRef 冷开- // 为 true,后续每次 contentSize 增长都由 handleContentSize 的贴底补滚继续拉到底,- // 最终收敛在真实底部(与迁移 LegendList 前的手搓落底同机制)。落底→补滚是两段式的,- // 肉眼可见「跳两下」(规则 7)——沿用老方案的 settle 遮罩:列表先以 opacity 0 挂载,- // 落底发起后等一个 settle 窗口(初窗测量与补滚基本结算)再揭开,揭开后不再隐藏。- // ⚠️ rAF / reveal timer 挂 ref 而非 effect cleanup:本 effect 依赖 listData.length,- // settle 窗口内消息追加会触发 cleanup——若把 timer 交给 cleanup,它会被清掉且因- // initialAnchorDoneRef 已置位不再重设,列表永久停在 opacity 0(即新一种白屏)。- // 两者的真实生命周期是「每个 scrollResetKey 一轮」,清理归会话切换 effect 与卸载。+ // commit 后先命令式落底,随后双帧校验 native metrics 是否真的到达 content end。+ // LegendList 可能仍在以估高换实高或等待 mVCP/data settle,所以一次 scrollToEnd 的+ // Promise/回调不等价于真实落底;verify 带独立 wait/retry 上限,只在跟随仍归本流程+ // 所有且用户未开始浏览历史时补滚。settled/give-up 后才揭开列表,固定 300ms 仅作+ // 首次校验前的最短遮罩窗口,不再是“已经落底”的假定。useEffect(() => {if (initialAnchorDoneRef.current) return;if (listData.length === 0) {- // 空会话没有落底问题,直接可见(空态/同步占位不该被遮罩藏住)。setListRevealed(true);return;}+initialAnchorDoneRef.current = true;- // 上个会话可能遗留的在飞句柄在此接管清理。不能放会话切换 reset effect:它声明- // 在本 effect 之后,同一轮 commit 里会把这里刚排好的新句柄误清。+ const generation = initialAnchorGenerationRef.current + 1;+ initialAnchorGenerationRef.current = generation;if (initialAnchorFrameRef.current !== null) cancelAnimationFrame(initialAnchorFrameRef.current);+ if (initialAnchorVerifyFrameRef.current !== null) cancelAnimationFrame(initialAnchorVerifyFrameRef.current);if (initialRevealTimerRef.current) clearTimeout(initialRevealTimerRef.current);++ const finish = () => {+ if (initialAnchorGenerationRef.current !== generation) return;+ if (initialAnchorVerifyFrameRef.current !== null) {+ cancelAnimationFrame(initialAnchorVerifyFrameRef.current);+ initialAnchorVerifyFrameRef.current = null;+ }+ initialRevealTimerRef.current = null;+ setListRevealed(true);+ };++ const startedAt = Date.now();+ const verify = (attempts: number, waitRounds: number) => {+ if (initialAnchorGenerationRef.current !== generation) return;+ const preserveVisibleContentPosition = readingOlderRef.current;+ const action = evaluateMobileAnchorVerify({+ attempts,+ listVisible: true,+ metrics: scrollMetricsRef.current,+ preserveVisibleContentPosition,+ stickToLatest: nearBottomRef.current && !userScrollForOlderRef.current,+ waitRounds,+ });+ if (action === 'settled' || action === 'give-up') {+ const remaining = Math.max(0, MOBILE_INITIAL_ANCHOR_SETTLE_MS - (Date.now() - startedAt));+ if (remaining === 0) finish();+ else initialRevealTimerRef.current = setTimeout(finish, remaining);+ return;+ }+ if (action === 'retry') scrollToEndProgrammatically(false);+ initialAnchorVerifyFrameRef.current = requestAnimationFrame(() => {+ initialAnchorVerifyFrameRef.current = requestAnimationFrame(() => {+ initialAnchorVerifyFrameRef.current = null;+ verify(+ attempts + (action === 'retry' ? 1 : 0),+ waitRounds + (action === 'wait' ? 1 : 0),+ );+ });+ });+ };+initialAnchorFrameRef.current = requestAnimationFrame(() => {initialAnchorFrameRef.current = null;- void listRef.current?.scrollToEnd({ animated: false });+ if (initialAnchorGenerationRef.current !== generation) return;+ scrollToEndProgrammatically(false);+ initialAnchorVerifyFrameRef.current = requestAnimationFrame(() => {+ initialAnchorVerifyFrameRef.current = requestAnimationFrame(() => {+ initialAnchorVerifyFrameRef.current = null;+ if (initialAnchorGenerationRef.current !== generation) return;+ verify(0, 0);+ });+ });});- initialRevealTimerRef.current = setTimeout(() => {- initialRevealTimerRef.current = null;- setListRevealed(true);- }, MOBILE_INITIAL_ANCHOR_SETTLE_MS);- }, [listData.length, scrollResetKey]);+ }, [listData.length, scrollResetKey, scrollToEndProgrammatically]);// 会话切换(scrollResetKey):重置浮标/近底等 UI 状态;LegendList 本体经 key={scrollResetKey}// 重挂并重新落底(上方冷开落底 effect;initialAnchorDoneRef 已在渲染期同步块复位)。@@ -1056,10 +1161,13 @@ export function MessageRenderer({// 卸载时清掉在飞的定时器/rAF(闭包引用 listRef,卸载后触发是无害 no-op,// 但不留悬挂句柄)。useEffect(() => () => {+ initialAnchorGenerationRef.current += 1;if (followEndPinRecoveryTimerRef.current) clearTimeout(followEndPinRecoveryTimerRef.current);+ clearProgrammaticScroll();if (initialAnchorFrameRef.current !== null) cancelAnimationFrame(initialAnchorFrameRef.current);+ if (initialAnchorVerifyFrameRef.current !== null) cancelAnimationFrame(initialAnchorVerifyFrameRef.current);if (initialRevealTimerRef.current) clearTimeout(initialRevealTimerRef.current);- }, []);+ }, [clearProgrammaticScroll]);// 顶部 chrome(如连接横幅)出现/消失 → topPadding 变 → contentContainerStyle.paddingTop 变 →// 所有 item 随之上下移。LegendList 的 maintainVisibleContentPosition 只跟 data / item 尺寸变化、@@ -1074,8 +1182,8 @@ export function MessageRenderer({const delta = topPadding - prev;if (delta === 0 || (nearBottomRef.current && !readingOlderRef.current)) return;const { offsetY } = scrollMetricsRef.current;- void listRef.current?.scrollToOffset({ animated: false, offset: Math.max(0, offsetY + delta) });- }, [topPadding]);+ scrollToOffsetProgrammatically(Math.max(0, offsetY + delta), false);+ }, [scrollToOffsetProgrammatically, topPadding]);// 深链/搜索:滚到指定消息(LegendList scrollToIndex 自带 offscreen 处理,无需 rAF/失败兜底)。useEffect(() => {@@ -1093,8 +1201,8 @@ export function MessageRenderer({lastAutoLoadEarlierKeyRef.current = null;nearBottomRef.current = false;setIsAwayFromBottom(true);- void listRef.current?.scrollToIndex({ animated: true, index, viewPosition: 0.45 });- }, [focusRunKey, focusedItemKey, listData]);+ scrollToIndexProgrammatically(index, 0.45);+ }, [focusRunKey, focusedItemKey, listData, scrollToIndexProgrammatically]);// 新消息红点:滚离底时来新消息(尾部 append)→ 提示。贴底时由 handleContentSize 补滚// 自动跟随、不提示。wasNearBottom 只看 nearBottomRef(跟随态唯一真相):以前 || 距离兜底diff --git a/apps/mobile/src/session/messageScroll.ts b/apps/mobile/src/session/messageScroll.tsindex fc5c80d2a6..4172e3352a 100644--- a/apps/mobile/src/session/messageScroll.ts+++ b/apps/mobile/src/session/messageScroll.ts@@ -112,11 +112,18 @@ export interface MobileNearBottomResolveInput {scrollDelta: number;/** 底部浮层高度(近底阈值随之放大,同 isNearMobileMessageListBottom)。 */bottomOverlayHeight?: number;+ /**+ * Whether the event was emitted while an imperative list scroll is settling.+ * Native list implementations can report a transient offset before the+ * commanded end position is applied; that event must not cancel auto-follow.+ */+ programmaticScrollInFlight?: boolean;}/*** scroll 事件驱动的近底/跟随态迁移(handleScroll 消费)。*+ * - 命令式滚动 settling → 保持原跟随态,不把瞬时 offset 当成用户上翻;* - 内容未撑满一屏 → true(与 isNearMessageListBottom 同口径);* - 距底超出阈值 → false(离底解除,原行为不变);* - 阈值带内且原本在跟 → 保持 true(贴底期间程序化 scrollToEnd 产生的@@ -126,6 +133,7 @@ export interface MobileNearBottomResolveInput {* 无方向守卫会立即翻回跟随,解除失效(修复核心)。*/export function resolveMobileNearBottomOnScroll(input: MobileNearBottomResolveInput): boolean {+ if (input.programmaticScrollInFlight) return input.wasNearBottom;const { contentHeight, viewportHeight } = input.metrics;if (contentHeight <= viewportHeight) return true;if (!isNearMobileMessageListBottom(input.metrics, input.bottomOverlayHeight)) return false;