Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 修复发件箱恢复阻挡首页缓存加载

Cindy80fe8599aaf9
diff --git a/apps/mobile/src/__tests__/mobileHomeListCache.test.ts b/apps/mobile/src/__tests__/mobileHomeListCache.test.ts
index 555efc10c1..7cbfaad44c 100644
--- a/apps/mobile/src/__tests__/mobileHomeListCache.test.ts
+++ b/apps/mobile/src/__tests__/mobileHomeListCache.test.ts
@@ -342,6 +342,43 @@ describe('mobileHomeListCache', () => {
expect(store.has(__testing.legacyStorageKey)).toBe(false);
});
+ it('restores the home cache when an outbox task arrived before the full list', async () => {
+ const { remoteSessionStore } = await import('@/session/remoteSessionStore');
+ const { cacheHomeListSnapshot, getCachedHomeListSnapshot } = await import('@/session/mobileHomeListCache');
+ remoteSessionStore.clear();
+ try {
+ await cacheHomeListSnapshot(USER_ID, [makeSession('s-outbox', 'dev-a'), makeSession('s-other', 'dev-a')]);
+ const snapshot = await getCachedHomeListSnapshot(USER_ID);
+ const restored = makeSession('s-outbox', 'dev-a', { title: 'restored draft', pendingLocalCreation: true });
+ remoteSessionStore.upsertDeviceSession('dev-a', 'current computer', restored);
+ remoteSessionStore.hydrateDeviceSessionsIfEmpty('dev-a', 'cached computer', snapshot[0].sessions);
+ expect(remoteSessionStore.getSessions().map(s => s.id).sort()).toEqual(['s-other', 's-outbox']);
+ expect(remoteSessionStore.getSessions().find(s => s.id === 's-outbox')).toMatchObject({
+ title: 'restored draft', pendingLocalCreation: true, deviceLinkDeviceName: 'current computer',
+ });
+ expect(remoteSessionStore.getSessions().find(s => s.id === 's-outbox')?.cacheSeeded).toBeUndefined();
+ expect(remoteSessionStore.getSessions().find(s => s.id === 's-other')?.cacheSeeded).toBe(true);
+ remoteSessionStore.setDeviceSessions('dev-a', 'current computer', [makeSession('s-new', 'dev-a')]);
+ expect(remoteSessionStore.getSessions().map(s => s.id).sort()).toEqual(['s-new', 's-outbox']);
+ } finally { remoteSessionStore.clear(); }
+ });
+
+ it.each(['empty-list', 'removed-device', 'deleted', 'archived'] as const)(
+ 'does not restore stale home rows after %s', async (boundary) => {
+ const { remoteSessionStore } = await import('@/session/remoteSessionStore');
+ remoteSessionStore.clear();
+ try {
+ const cached = [makeSession('s-old', 'dev-a'), makeSession('s-other', 'dev-a')];
+ remoteSessionStore.upsertDeviceSession('dev-a', 'computer', cached[0]);
+ if (boundary === 'empty-list') remoteSessionStore.setDeviceSessions('dev-a', 'computer', []);
+ else if (boundary === 'removed-device') remoteSessionStore.removeDevice('dev-a');
+ else remoteSessionStore.applySessionPatch('dev-a', 's-old', { status: boundary });
+ remoteSessionStore.hydrateDeviceSessionsIfEmpty('dev-a', 'computer', cached);
+ expect(remoteSessionStore.getSessions()).toEqual([]);
+ } finally { remoteSessionStore.clear(); }
+ },
+ );
+
it('hydrateDeviceSessionsIfEmpty seeds an absent shard and never clobbers fresh data', async () => {
const { remoteSessionStore } = await import('@/session/remoteSessionStore');
const { cacheHomeListSnapshot, getCachedHomeListSnapshot } = await import('@/session/mobileHomeListCache');
diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.ts
index 7879f256d5..84a3c7dadd 100644
--- a/apps/mobile/src/session/remoteSessionStore.ts
+++ b/apps/mobile/src/session/remoteSessionStore.ts
@@ -3429,18 +3429,26 @@ export const remoteSessionStore = {
recomputeSessions();
},
- // 冷启动乐观 hydrate:仅当该设备 shard 尚不存在时,用本地首页快照缓存种入(先画缓存)。
- // 「if absent」是关键不变量——fresh loadHome 数据若已先到则绝不覆盖;fresh 之后到会走
- // setDeviceSessions 正常对账替换。与 hydrateMessagesIfEmpty 同一套语义,只作用于会话 shard。
+ // 单条详情/发件箱恢复不代表整份首页列表已经加载。首轮列表尚未到达时,缓存可以
+ // 补齐缺失行,但已有行始终优先。收到列表或列表变更后不再种入旧快照(含权威空列表、
+ // 归档/删除与设备移除),复用现有列表刷新记录和 mutation fence。
hydrateDeviceSessionsIfEmpty(deviceId: string, deviceName: string, rawSessions: readonly RemoteSession[]): void {
- if (!deviceId || rawSessions.length === 0 || shards.has(deviceId)) return;
+ if (!deviceId || rawSessions.length === 0 || sessionListRefreshSeen.has(deviceId)
+ || deviceSessionListMutationEpochs.has(deviceId)) return;
+ const existing = shards.get(deviceId);
+ const existingIds = new Set(existing?.sessions.map((session) => session.id));
+ const missing = rawSessions.filter((session) => !existingIds.has(session.id)
+ && sessionPendingWrites.pendingFields(session.id).length === 0);
+ if (missing.length === 0) return;
+ const currentDeviceName = existing?.deviceName ?? deviceName;
shards.set(deviceId, {
deviceId,
- deviceName,
+ deviceName: currentDeviceName,
// cacheSeeded 打标:缓存行字段经瘦身/截断(240 字符),只能撑首屏渲染与跳转,
// 不能作为发送参数;会话页据此在 fresh 元数据到达前禁发(codex review R15)。
// fresh 路径(setDeviceSessions / upsertDeviceSession)用服务器新对象,天然无标自净。
- sessions: rawSessions.map((s) => stamp({ ...s, cacheSeeded: true }, deviceId, deviceName)),
+ sessions: [...(existing?.sessions ?? []), ...missing.map((s) =>
+ stamp({ ...s, cacheSeeded: true }, deviceId, currentDeviceName))],
});
recomputeSessions();
},
@@ -3502,6 +3510,9 @@ export const remoteSessionStore = {
},
applySessionPatch(deviceId: string, sessionId: string, patch: Partial<RemoteSession>): void {
+ // Local optimistic removals must fence a late startup cache just like remote pushes,
+ // even when the removed row has not reached this shard yet.
+ if (patch.status === 'deleted' || patch.status === 'archived') bumpDeviceSessionListMutationEpoch(deviceId);
const shard = shards.get(deviceId);
if (!shard) {
if (patch.status === 'active') reseedHandlers.get(deviceId)?.forEach((handler) => handler());
diff --git a/docs/dev-rules/mobile-development.md b/docs/dev-rules/mobile-development.md
index fe083da1ed..b35f438974 100644
--- a/docs/dev-rules/mobile-development.md
+++ b/docs/dev-rules/mobile-development.md
@@ -115,6 +115,10 @@ pnpm --filter mobile test:smoke
### 消息缓存与首页偏好
+- 首页冷启动缓存不能把单条详情或发件箱恢复当成整份设备列表已加载:首轮列表尚未返回、
+ 且尚无列表变更时,用缓存补齐缺失任务,已有任务与待创建状态优先。收到权威列表
+ (包括空列表)、列表变更或设备移除后,不再补入旧缓存;缓存行继续保留 `cacheSeeded`
+ 标记。实现见 `remoteSessionStore.ts`,回归见 `mobileHomeListCache.test.ts`。
- iOS、Android 的最近消息窗口统一写入私有 `cache/session-messages-v1/` 文件,
不设总容量或按时间淘汰;分页、内存窗口、消息净化与显式删除语义不变。
该目录属于可再生缓存,系统低空间回收后会从主机重新获取,不作为消息权威副本。