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): 打开过的定时任务按普通任务驻留,切后台不再清空正文

Dash9adfed2582ac
diff --git a/apps/mobile/docs/task-message-memory-governance-architecture.md b/apps/mobile/docs/task-message-memory-governance-architecture.md
index 4ac69c1294..938404be73 100644
--- a/apps/mobile/docs/task-message-memory-governance-architecture.md
+++ b/apps/mobile/docs/task-message-memory-governance-architecture.md
@@ -6,7 +6,8 @@
### `sessionRetention.ts`
-唯一分类入口:`source === 'scheduler'` 返回 `schedule`,其余返回 `regular`。
+唯一分类入口:`source === 'scheduler'` 且从未打开过详情时返回 `schedule`,其余返回 `regular`。
+定时任务一旦被用户打开查看,本次 App 运行期间就与普通任务完全一致。
### `sessionMessageLifecycle.ts`
@@ -103,10 +104,11 @@ fresh session metadata arrives with source=scheduler
2. leave 先撤权,后判断能否做破坏性回收。
3. deferred reclaim 的所有者是常驻 controller,不是页面 effect。
4. `messages.length === 0` 不是磁盘删除指令。
-5. schedule 不读取或持久化长期完整消息缓存。
+5. 从未打开的 schedule 不读取或持久化长期完整消息缓存;打开过的按 regular 处理。
6. regular LRU 只淘汰内存,不碰磁盘窗口。
7. 本地系统卡没有服务端副本,所有压窗与缓存归一化必须保护。
-8. 当前详情保护对 regular 与 schedule 一致;回前台必须重新 enter。
+8. 当前详情保护对 regular 与 schedule 一致;回前台必须重新 enter。打开过详情的 schedule
+ 离场或切后台只压窗,不归零。
9. status、通知摘要与 live activity 不依赖完整消息驻留。
10. source 缺失时只能保守按 regular,不能用标题或索引猜测。
11. 页面聚合租约结束不代表异步发送结束;send/outbox 独立租约只能由自己的 `finally` 释放。
@@ -135,7 +137,7 @@ fresh session metadata arrives with source=scheduler
### 恢复前允许展示什么?
- regular 可展示仍在内存或合法 hydrate 的最新缓存窗口;
-- schedule 回收后展示加载态/空窗,直到新代际读取完成;
+- 从未打开的 schedule 没有驻留正文,首次打开展示加载态,直到读取完成;
- 不允许旧代际响应、旧订阅 push 或旧流式批次填回正文。
## 5. 交付检查清单
@@ -145,7 +147,7 @@ fresh session metadata arrives with source=scheduler
- [x] focus、AppState 与 session switch 接线。
- [x] 常驻工作租约与卸载后补回收。
- [x] async send 与 outbox dispatch 独立租约。
-- [x] schedule 单窗、隐藏后归零、禁用加载更早。
+- [x] 从未打开的 schedule 不驻留正文;打开后按 regular 驻留。
- [x] regular 80 条压窗与 800 条 / 64 MiB LRU。
- [x] 草稿、运行、交互、pending queue、本地工作保护。
- [x] regular 离场/LRU 释放详情投影并抬升 projection epoch。
diff --git a/apps/mobile/src/__tests__/mobileSessionMessageCache.test.ts b/apps/mobile/src/__tests__/mobileSessionMessageCache.test.ts
index 35a7e71d72..503460a13d 100644
--- a/apps/mobile/src/__tests__/mobileSessionMessageCache.test.ts
+++ b/apps/mobile/src/__tests__/mobileSessionMessageCache.test.ts
@@ -447,7 +447,26 @@ describe('mobileSessionMessageCache', () => {
await expect(getCachedSessionMessages('host-a', 'session-1')).resolves.toEqual([]);
});
- it('removeMessages on a schedule task clears cache instead of persisting the remaining body', async () => {
+ it('removeMessages on an unopened schedule task clears cache instead of persisting the remaining body', async () => {
+ const { cacheSessionMessages, getCachedSessionMessages } =
+ await import('@/session/mobileSessionMessageCache');
+ const { remoteSessionStore } = await import('@/session/remoteSessionStore');
+ remoteSessionStore.clear();
+ remoteSessionStore.setDeviceSessions('host-a', 'Mac', [
+ makeSession('session-1', { source: 'scheduler' }),
+ ]);
+ const rows = [
+ makeMessage({ id: 'a', clientId: 'a', createdAt: isoAt(1) }),
+ makeMessage({ id: 'b', clientId: 'b', createdAt: isoAt(2) }),
+ ];
+ await cacheSessionMessages('host-a', 'session-1', rows);
+
+ remoteSessionStore.removeMessages('session-1', ['a'], 'host-a');
+
+ await expect(getCachedSessionMessages('host-a', 'session-1')).resolves.toEqual([]);
+ });
+
+ it('removeMessages on an opened schedule task persists the remaining body like a regular task', async () => {
const { cacheSessionMessages, getCachedSessionMessages } =
await import('@/session/mobileSessionMessageCache');
const { remoteSessionStore } = await import('@/session/remoteSessionStore');
@@ -465,7 +484,9 @@ describe('mobileSessionMessageCache', () => {
remoteSessionStore.removeMessages('session-1', ['a'], 'host-a');
- await expect(getCachedSessionMessages('host-a', 'session-1')).resolves.toEqual([]);
+ await vi.waitFor(async () => {
+ expect((await getCachedSessionMessages('host-a', 'session-1')).map((row) => row.id)).toEqual(['b']);
+ });
expect(remoteSessionStore.getMessages('session-1').map((row) => row.id)).toEqual(['b']);
});
diff --git a/apps/mobile/src/__tests__/remoteHistoryReentry.test.ts b/apps/mobile/src/__tests__/remoteHistoryReentry.test.ts
index 5ccd90561c..e3aae677b7 100644
--- a/apps/mobile/src/__tests__/remoteHistoryReentry.test.ts
+++ b/apps/mobile/src/__tests__/remoteHistoryReentry.test.ts
@@ -209,7 +209,7 @@ describe('history reentry', () => {
expect(reopened.view.getSnapshot().items).toEqual([]);
});
- it('uses existing schedule reclamation while keeping a mounted background view addressable', async () => {
+ it('keeps an opened scheduled task readable after the app goes to the background', async () => {
remoteSessionStore.upsertDeviceSession('d', 'desktop', {
id: 's', source: 'scheduler', status: 'active', createdAt: '2026-09-08T00:00:00Z',
updatedAt: '2026-09-08T00:00:00Z', title: 's',
@@ -218,10 +218,11 @@ describe('history reentry', () => {
mount(entry);
const authority = remoteSessionStore.enterSessionMessageDetail('s');
await entry.view.refresh();
+ const items = entry.view.getSnapshot().items;
entry.view.setActive(false);
remoteSessionStore.leaveSessionMessageDetail('s', 'app-background', authority);
expect(remoteSessionStore.releaseSessionRuntimeState('s', { reason: 'app-background' })).toBe(true);
- expect(entry.view.getSnapshot()).toMatchObject({ items: [], ready: false });
+ expect(entry.view.getSnapshot()).toMatchObject({ items, ready: true });
expect(findRemoteHistoryView('d', 's')).toBe(entry.view);
});
diff --git a/apps/mobile/src/__tests__/remoteSessionStore.test.ts b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
index 7af8f876ad..e43318bb18 100644
--- a/apps/mobile/src/__tests__/remoteSessionStore.test.ts
+++ b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
@@ -5630,19 +5630,27 @@ describe('任务消息内存治理', () => {
expect(remoteSessionStore.canCommitUnenteredSessionMessageWindow(afterReset, 'dev-1')).toBe(true);
});
- it('schedule 失焦后回收到 0,后续 push 不会复活完整正文', async () => {
+ it('打开过的 schedule 离场后与 regular 一致:压回单窗而不是清空', async () => {
remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
+ expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
const authority = remoteSessionStore.enterSessionMessageDetail('s1');
- remoteSessionStore.setMessages('s1', [message('m1', 's1')], { authority });
+ expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
+ remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });
- remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
+ remoteSessionStore.leaveSessionMessageDetail('s1', 'app-background', authority);
await flushReclaim();
- expect(remoteSessionStore.getMessages('s1')).toEqual([]);
+ const rows = remoteSessionStore.getMessages('s1');
+ expect(rows).toHaveLength(80);
+ expect(rows.at(-1)?.id).toBe('s1-m-99');
+ });
+ it('从未打开的 schedule 不驻留推送来的完整正文', () => {
+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
sessionId: 's1',
- message: message('late-push', 's1'),
+ message: message('unopened-push', 's1'),
});
+ expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
expect(remoteSessionStore.getMessages('s1')).toEqual([]);
});
@@ -5684,34 +5692,34 @@ describe('任务消息内存治理', () => {
});
it('页面卸载后本地工作排空仍会完成 deferred reclaim', async () => {
- remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
const work = remoteSessionStore.acquireSessionMessageWork('s1', true);
const authority = remoteSessionStore.enterSessionMessageDetail('s1');
- remoteSessionStore.setMessages('s1', [message('m1', 's1')], { authority });
+ remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });
remoteSessionStore.leaveSessionMessageDetail('s1', 'session-switch', authority);
await flushReclaim();
- expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
+ expect(remoteSessionStore.getMessages('s1')).toHaveLength(100);
work.release();
await flushReclaim();
- expect(remoteSessionStore.getMessages('s1')).toEqual([]);
+ expect(remoteSessionStore.getMessages('s1')).toHaveLength(80);
});
- it('pending queue 暂缓 schedule 回收,queue 排空后由 store 主动补回收', async () => {
- remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
+ it('pending queue 暂缓回收,queue 排空后由 store 主动补回收', async () => {
+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
const authority = remoteSessionStore.enterSessionMessageDetail('s1');
- remoteSessionStore.setMessages('s1', [message('m1', 's1')], { authority });
+ remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });
remoteSessionStore.setInputProjection('s1', projection('s1'));
remoteSessionStore.leaveSessionMessageDetail('s1', 'app-background', authority);
await flushReclaim();
- expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
+ expect(remoteSessionStore.getMessages('s1')).toHaveLength(100);
remoteSessionStore.setInputProjection('s1', {
...projection('s1'),
pendingQueue: [],
});
await flushReclaim();
- expect(remoteSessionStore.getMessages('s1')).toEqual([]);
+ expect(remoteSessionStore.getMessages('s1')).toHaveLength(80);
});
it('regular 失焦压回单窗,同时保留窗口外的本地系统卡', async () => {
@@ -5878,18 +5886,18 @@ describe('任务消息内存治理', () => {
)).toBe(false);
});
- it('source 晚到改判 schedule:当前详情立即压窗,失焦后归零', async () => {
+ it('source 晚到为 scheduler:已打开的详情仍按 regular 驻留', async () => {
remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
const authority = remoteSessionStore.enterSessionMessageDetail('s1');
remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });
remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', { source: 'scheduler' }));
- expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
- expect(remoteSessionStore.getMessages('s1')).toHaveLength(80);
+ expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
+ expect(remoteSessionStore.getMessages('s1')).toHaveLength(100);
- remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur');
+ remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
await flushReclaim();
- expect(remoteSessionStore.getMessages('s1')).toEqual([]);
+ expect(remoteSessionStore.getMessages('s1')).toHaveLength(80);
});
it('归档会话会撤销旧 authority,并拒绝迟到 push 复活正文', () => {
diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.ts
index 09ce049489..f3d359d12d 100644
--- a/apps/mobile/src/session/remoteSessionStore.ts
+++ b/apps/mobile/src/session/remoteSessionStore.ts
@@ -756,6 +756,8 @@ const messageListBudgetStatsCache = new WeakMap<
>();
const sessionLastAccessOrder = new Map<string, number>();
let nextSessionAccessOrder = 0;
+/** 用户打开过详情的任务;定时任务进入此集合后按普通任务驻留。 */
+const sessionsOpenedInDetail = new Set<string>();
const messageStructureTokens = new WeakMap<readonly RemoteMessage[], object>();
const messageStructureChangedIndexes = new WeakMap<
readonly RemoteMessage[],
@@ -890,7 +892,7 @@ function sessionById(sessionId: string): RemoteSession | undefined {
}
function retentionForSession(sessionId: string): SessionRetentionKind {
- return classifySessionRetention(sessionById(sessionId));
+ return classifySessionRetention(sessionById(sessionId), sessionsOpenedInDetail.has(sessionId));
}
function touchSessionAccess(sessionId: string): void {
@@ -1591,7 +1593,7 @@ function commitInputProjection(sessionId: string, next: InputProjection): boolea
function recomputeSessions(): void {
const previousRetention = new Map(
- mergedSessions.map((session) => [session.id, classifySessionRetention(session)] as const),
+ mergedSessions.map((session) => [session.id, retentionForSession(session.id)] as const),
);
sessionDeviceIndex.clear();
// 跨 shard 去重 + 设备身份归一化。re-link 后同一 session.id 可能同时存在于 stale / current 两个 shard;
@@ -1658,7 +1660,7 @@ function recomputeSessions(): void {
session.userSendAt ?? session.updatedAt ?? session.createdAt,
{ consumePending: false },
) || liveRowsReanchored;
- const nextRetention = classifySessionRetention(session);
+ const nextRetention = retentionForSession(session.id);
if (nextRetention !== 'schedule' || previousRetention.get(session.id) === 'schedule') continue;
// source 晚到后立即切断旧缓存路径。旧 hydrate / debounce 回调还会在提交前
// 二次检查 retention;这里的串行删除负责收掉已经开始的旧写。
@@ -3225,9 +3227,11 @@ export const remoteSessionStore = {
},
enterSessionMessageDetail(sessionId: string): SessionMessageAuthority {
+ const retentionBefore = retentionForSession(sessionId);
+ sessionsOpenedInDetail.add(sessionId);
const authority = sessionMessageLifecycle.enter(sessionId);
touchSessionAccess(sessionId);
- if (enforceRegularMessageBudget()) {
+ if (enforceRegularMessageBudget() || retentionBefore !== retentionForSession(sessionId)) {
bumpMessageVersion();
emit();
}
@@ -5315,6 +5319,7 @@ export const remoteSessionStore = {
sessionMessageLifecycle.reset();
sessionLastAccessOrder.clear();
nextSessionAccessOrder = 0;
+ sessionsOpenedInDetail.clear();
sessionDeviceIndex.clear();
reseedHandlers.clear();
sessionListRefreshArmed.clear();
diff --git a/apps/mobile/src/session/sessionRetention.ts b/apps/mobile/src/session/sessionRetention.ts
index e55c8d2670..645a66e2ee 100644
--- a/apps/mobile/src/session/sessionRetention.ts
+++ b/apps/mobile/src/session/sessionRetention.ts
@@ -1,16 +1,18 @@
import type { RemoteSession } from '@/session/types';
/**
- * Mobile 消息驻留策略只由任务创建来源决定。
+ * Mobile 消息驻留策略由任务创建来源和用户是否打开过详情决定。
*
* `source` 缺失时保守按普通任务处理:宁可多驻留,也不能因为标题、schedule
* 索引等可变/晚到信息误回收用户任务。schedule 绑定既有任务时不会改写 source,
- * 因而仍属于 regular。
+ * 因而仍属于 regular。定时任务一旦被用户打开查看,就与普通任务一致,切后台
+ * 或离开详情不再整窗回收。
*/
export type SessionRetentionKind = 'regular' | 'schedule';
export function classifySessionRetention(
session: Pick<RemoteSession, 'source'> | null | undefined,
+ openedInDetail = false,
): SessionRetentionKind {
- return session?.source === 'scheduler' ? 'schedule' : 'regular';
+ return session?.source === 'scheduler' && !openedInDetail ? 'schedule' : 'regular';
}