Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): 按历史游标继续首屏补齐 (#4070)
Dashbabcf743851b
diff --git a/apps/mobile/src/__tests__/messageHistoryAutofillCallbacks.test.ts b/apps/mobile/src/__tests__/messageHistoryAutofillCallbacks.test.tsnew file mode 100644index 0000000000..caa8fcd5d7--- /dev/null+++ b/apps/mobile/src/__tests__/messageHistoryAutofillCallbacks.test.ts@@ -0,0 +1,100 @@+import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';+import ts from 'typescript';+import { describe, expect, it, vi } from 'vitest';+import { shouldAutoLoadEarlier } from '@/session/messageScroll';++// Execute the production progress-key calculation and callback, including the early dedupe guard.+const source = ts.createSourceFile('renderer.tsx', readFileSync(+ resolve(process.cwd(), 'src/session/MessageRenderer.tsx'), 'utf8',+), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);+let callbackSource = '';+let progressKeySource = '';+function visit(node: ts.Node) {+ if (ts.isVariableDeclaration(node) && node.name.getText(source) === 'attemptAutoLoadEarlier'+ && node.initializer && ts.isCallExpression(node.initializer)) {+ callbackSource = node.initializer.arguments[0].getText(source);+ }+ if (ts.isVariableDeclaration(node) && node.name.getText(source) === 'historyProgressKey'+ && node.initializer) {+ progressKeySource = node.initializer.getText(source);+ }+ ts.forEachChild(node, visit);+}+visit(source);++function fixture() {+ const requestLoadEarlier = vi.fn();+ const bindings = {+ onLoadEarlier: () => {},+ readingOlderRef: { current: false },+ queuedLoadEarlierRef: { current: false },+ userScrollForOlderRef: { current: false },+ listRevealed: true,+ initialHistoryAutofillRemainingRef: { current: 3 },+ regroupedHistoryContinuationRef: { current: false },+ firstItemKey: 'local-notice',+ loadEarlierProgressKey: 'host-80' as string | null,+ lastAutoLoadEarlierKeyRef: { current: null as string | null },+ listRef: { current: { getState: () => ({ isAtEnd: true, isAtStart: true, isNearStart: true }) } },+ loadEarlierAction: { visible: true, disabled: false },+ shouldAutoLoadEarlier,+ requestLoadEarlier,+ };+ const attempt = () => {+ if (!callbackSource || !progressKeySource) throw new Error('Missing production paging code');+ const compiled = ts.transpileModule(`const historyProgressKey = ${progressKeySource};+const callback = ${callbackSource};`, {+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },+ }).outputText;+ new Function(...Object.keys(bindings), `${compiled}\nreturn callback;`)(...Object.values(bindings))();+ };+ return { bindings, attempt, requestLoadEarlier };+}++describe('history autofill production callback', () => {+ it('continues through host pages behind an unchanged first rendered row, within the cold-open budget', () => {+ const { bindings, attempt, requestLoadEarlier } = fixture();+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(1);+ bindings.loadEarlierProgressKey = 'host-60';+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(2);+ bindings.loadEarlierProgressKey = 'host-40';+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(3);+ bindings.loadEarlierProgressKey = 'host-20';+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(3);+ });++ it('does not repeat failed/duplicate pages just because streaming changes the first rendered row', () => {+ const { bindings, attempt, requestLoadEarlier } = fixture();+ attempt();+ bindings.firstItemKey = 'changed-render-item';+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(1);+ });++ it('falls back to the first rendered row only when no host cursor is available', () => {+ const { bindings, attempt, requestLoadEarlier } = fixture();+ bindings.loadEarlierProgressKey = null;+ attempt();+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(1);+ bindings.firstItemKey = 'earlier-render-item';+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(2);+ });++ it('uses the same cursor progress for user-driven regroup continuation', () => {+ const { bindings, attempt, requestLoadEarlier } = fixture();+ bindings.userScrollForOlderRef.current = true;+ attempt();+ bindings.loadEarlierProgressKey = 'host-60';+ bindings.regroupedHistoryContinuationRef.current = true;+ attempt();+ expect(requestLoadEarlier).toHaveBeenCalledTimes(2);+ expect(bindings.initialHistoryAutofillRemainingRef.current).toBe(3);+ });+});diff --git a/apps/mobile/src/__tests__/messageScroll.test.ts b/apps/mobile/src/__tests__/messageScroll.test.tsindex d7e665664f..72571ab487 100644--- a/apps/mobile/src/__tests__/messageScroll.test.ts+++ b/apps/mobile/src/__tests__/messageScroll.test.ts@@ -253,9 +253,9 @@ describe('shouldAutoLoadEarlier', () => {actionVisible: true,atEnd: false,atStart: true,- firstItemKey: 'message-a',+ progressKey: 'message-a',initialAutoFillAllowed: false,- lastAttemptedFirstItemKey: null,+ lastAttemptedProgressKey: null,nearStart: true,userScrolledForOlder: true,};@@ -328,11 +328,11 @@ describe('shouldAutoLoadEarlier', () => {it('requires progress between attempts to avoid hammering a host that returns no new rows', () => {// 上次尝试后首项没变(加载失败 / host cursor 未命中拉回重复页)→ 不自动重试;- expect(shouldAutoLoadEarlier({ ...eligible, lastAttemptedFirstItemKey: 'message-a' })).toBe(false);+ expect(shouldAutoLoadEarlier({ ...eligible, lastAttemptedProgressKey: 'message-a' })).toBe(false);// prepend 真落地(首项变化)→ 允许级联拉下一页(小页填满预取区);- expect(shouldAutoLoadEarlier({ ...eligible, lastAttemptedFirstItemKey: 'message-z' })).toBe(true);+ expect(shouldAutoLoadEarlier({ ...eligible, lastAttemptedProgressKey: 'message-z' })).toBe(true);// 空列表无进展信号,不触发。- expect(shouldAutoLoadEarlier({ ...eligible, firstItemKey: null })).toBe(false);+ expect(shouldAutoLoadEarlier({ ...eligible, progressKey: null })).toBe(false);});});diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsxindex b96799e969..b5bd08c32e 100644--- a/apps/mobile/src/session/MessageRenderer.tsx+++ b/apps/mobile/src/session/MessageRenderer.tsx@@ -1386,8 +1386,8 @@ export function MessageRenderer({() => mobileMessageListKeysSignature(itemKeys),[itemKeys],);- // Keep main's rendered-row progress signal for near-start auto paging. The host cursor remains- // the stronger transaction commit signal because local synthetic rows can sit before host data.+ // Use host-cursor progress for both paging and transaction settlement. A history page may+ // expand a folded group or sit after a local synthetic row without changing the first item.const firstItemKey = itemKeys[0] ?? null;const historyProgressKey = loadEarlierProgressKey ?? firstItemKey;// A successful history page changes the oldest host cursor. Restore the captured visible row@@ -1891,7 +1891,7 @@ export function MessageRenderer({const continueAfterRegroup = userScrolledForOlder&& regroupedHistoryContinuationRef.current;if (!userScrolledForOlder && !initialAutoFillAllowed && !continueAfterRegroup) return;- if (firstItemKey !== null && lastAutoLoadEarlierKeyRef.current === firstItemKey) return;+ if (historyProgressKey !== null && lastAutoLoadEarlierKeyRef.current === historyProgressKey) return;if (continueAfterRegroup) {if (!loadEarlierAction.visible) {regroupedHistoryContinuationRef.current = false;@@ -1900,9 +1900,9 @@ export function MessageRenderer({// A merged page did make host-cursor progress but added no visible top-level history. Do not// reapply the near-start gate after app-owned anchor restoration moved the viewport; main// immediately continues from the changed first rendered row in the same situation.- if (loadEarlierAction.disabled || firstItemKey === null) return;+ if (loadEarlierAction.disabled || historyProgressKey === null) return;regroupedHistoryContinuationRef.current = false;- lastAutoLoadEarlierKeyRef.current = firstItemKey;+ lastAutoLoadEarlierKeyRef.current = historyProgressKey;requestLoadEarlier();return;}@@ -1961,18 +1961,18 @@ export function MessageRenderer({actionVisible: loadEarlierAction.visible,atEnd: listState.isAtEnd,atStart: listState.isAtStart || nativeAtStart,- firstItemKey,+ progressKey: historyProgressKey,initialAutoFillAllowed,- lastAttemptedFirstItemKey: lastAutoLoadEarlierKeyRef.current,+ lastAttemptedProgressKey: lastAutoLoadEarlierKeyRef.current,nearStart: listState.isNearStart || nativeNearStart,userScrolledForOlder,});if (!eligible) return;- lastAutoLoadEarlierKeyRef.current = firstItemKey;+ lastAutoLoadEarlierKeyRef.current = historyProgressKey;if (initialAutoFillAllowed) initialHistoryAutofillRemainingRef.current -= 1;requestLoadEarlier();}, [- firstItemKey,+ historyProgressKey,loadEarlierAction.disabled,loadEarlierAction.visible,listRevealed,diff --git a/apps/mobile/src/session/messageScroll.ts b/apps/mobile/src/session/messageScroll.tsindex 4d10189564..2c2fa85a27 100644--- a/apps/mobile/src/session/messageScroll.ts+++ b/apps/mobile/src/session/messageScroll.ts@@ -451,12 +451,12 @@ export interface MobileAutoLoadEarlierDecisionInput {atEnd: boolean;/** 列表当前也贴在内容开头;与 atEnd 同时成立才说明内容未撑满视口。 */atStart: boolean;- /** 当前首个渲染项 key(prepend 落地后必变,作为「上次尝试有进展」的信号)。 */- firstItemKey: string | null;+ /** 当前最旧的历史游标;调用方没有游标时退回首个渲染项 key。 */+ progressKey: string | null;/** 冷开补齐预算尚有余额;仍需 atStart + atEnd 确认视口未填满。 */initialAutoFillAllowed: boolean;- /** 上一次自动触发时的首项 key;相同说明上次尝试无进展(失败/重复页),不再自动重试。 */- lastAttemptedFirstItemKey: string | null;+ /** 上一次自动触发时的历史游标;相同说明上次尝试无进展(失败/重复页),不再自动重试。 */+ lastAttemptedProgressKey: string | null;/** 列表处于近顶预取区(LegendList getState().isNearStart,阈值 = onStartReachedThreshold × 视口)。 */nearStart: boolean;/** 用户产生过真实上翻意图(拖动 / 上跳导航);冷开初始布局不算。 */@@ -480,7 +480,7 @@ export interface MobileAutoLoadEarlierDecisionInput {* 防失控:* - 用户浏览态 atEnd 时不触发:贴底跟流不因自动预取被关掉 end-pin;只有冷开有界补齐* 允许短窗口同时 nearStart + atEnd 时拉取;- * - firstItemKey 去重:一次尝试后必须看到首项变化(真有 prepend)才允许下一次,+ * - progressKey 去重:一次尝试后必须看到历史游标变化(真有 prepend)才允许下一次,* 加载失败或拉回重复页(host cursor 未命中返回最新页)不会无限重试;用户重新拖动时清除,* 保证手势永远能重新驱动一次尝试。*/@@ -495,8 +495,8 @@ export function shouldAutoLoadEarlier(input: MobileAutoLoadEarlierDecisionInput)// request the previous page; only suppress user-driven prefetch when a longer list is still// pinned at the latest edge without also being at the history edge.if (input.atEnd && input.userScrolledForOlder && !input.atStart) return false;- if (!input.firstItemKey) return false;- return input.lastAttemptedFirstItemKey !== input.firstItemKey;+ if (!input.progressKey) return false;+ return input.lastAttemptedProgressKey !== input.progressKey;}export interface MobilePreviousUserJumpTarget {