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): 重连后统一失效旧日程索引

Dashec7701dc471f
diff --git a/apps/mobile/src/__tests__/scheduleIndex.test.ts b/apps/mobile/src/__tests__/scheduleIndex.test.ts
index 83c5e9d7e9..df52dcf37d 100644
--- a/apps/mobile/src/__tests__/scheduleIndex.test.ts
+++ b/apps/mobile/src/__tests__/scheduleIndex.test.ts
@@ -7,7 +7,7 @@ import {
invalidateRunningSessionScheduleEntries,
invalidateScheduleIndexForDevice,
invalidateTransientScheduleIndexFailureFor,
- invalidateTransientScheduleIndexFailures,
+ invalidateScheduleIndexesAfterLinkRecovery,
loadSessionScheduleIndex,
loadSessionScheduleIndexThrottled,
replaceSessionScheduleIndexEntries,
@@ -318,7 +318,7 @@ describe('loadSessionScheduleIndexThrottled (单飞 + TTL 节流)', () => {
expect(loadSessionScheduleIndexThrottled('dev-1', load, options)).toBe(first);
});
- it.each([false, true])('coalesces invalidated scans after the old scan settles (rejected=%s)', async (rejected) => {
+ it.each([[false, false], [true, false], [false, true], [true, true]])('coalesces invalidated scans after the old scan settles (rejected=%s, recovered=%s)', async (rejected, recovered) => {
resetScheduleIndexThrottleForTesting();
let finish!: (index: Map<string, RemoteSessionScheduleInfo>) => void;
let fail!: (error: Error) => void;
@@ -327,7 +327,8 @@ describe('loadSessionScheduleIndexThrottled (单飞 + TTL 节流)', () => {
.mockImplementationOnce(() => new Promise((resolve, reject) => { finish = resolve; fail = reject; }))
.mockResolvedValue(fresh);
const old = loadSessionScheduleIndexThrottled('dev-1', load).catch(() => undefined);
- invalidateScheduleIndexForDevice('dev-1');
+ if (recovered) invalidateScheduleIndexesAfterLinkRecovery();
+ else invalidateScheduleIndexForDevice('dev-1');
const home = loadSessionScheduleIndexThrottled('dev-1', load);
const task = loadSessionScheduleIndexThrottled('dev-1', load);
expect(load).toHaveBeenCalledTimes(1);
@@ -454,7 +455,7 @@ describe('loadSessionScheduleIndexThrottled (单飞 + TTL 节流)', () => {
});
expect(load).toHaveBeenCalledTimes(1);
// 重连(rehydrate 开始)→ 瞬态负缓存失效 → 立即重拉
- invalidateTransientScheduleIndexFailures();
+ invalidateScheduleIndexesAfterLinkRecovery();
await expect(loadSessionScheduleIndexThrottled('dev-n', load, { now })).resolves.toBeInstanceOf(Map);
expect(load).toHaveBeenCalledTimes(2);
},
@@ -499,8 +500,8 @@ describe('loadSessionScheduleIndexThrottled (单飞 + TTL 节流)', () => {
code: 'DEVICE_OFFLINE',
});
await Promise.resolve();
- // 全局重连钩子(NOT_CONNECTED 类专用)不清 DEVICE_OFFLINE 负缓存
- invalidateTransientScheduleIndexFailures();
+ // 全局重连钩子保留逐设备的 DEVICE_OFFLINE 负缓存
+ invalidateScheduleIndexesAfterLinkRecovery();
await expect(loadSessionScheduleIndexThrottled('dev-o', load, { now })).rejects.toMatchObject({
code: 'DEVICE_OFFLINE',
});
@@ -571,6 +572,8 @@ describe('loadSessionScheduleIndexThrottled (单飞 + TTL 节流)', () => {
code: 'DEVICE_UNRESPONSIVE',
});
await Promise.resolve();
+ // Relay 恢复不代表目标设备响应恢复,仍复用熔断负缓存。
+ invalidateScheduleIndexesAfterLinkRecovery();
// 熔断仍 open:失败 TTL 内复用负缓存,不压请求
await expect(loadSessionScheduleIndexThrottled('dev-1', load, { now })).rejects.toMatchObject({
code: 'DEVICE_UNRESPONSIVE',
diff --git a/apps/mobile/src/__tests__/scheduleRunRead.test.ts b/apps/mobile/src/__tests__/scheduleRunRead.test.ts
index d59ffe10da..0261e92e92 100644
--- a/apps/mobile/src/__tests__/scheduleRunRead.test.ts
+++ b/apps/mobile/src/__tests__/scheduleRunRead.test.ts
@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { projectScheduleEvent } from '@cindy/maker-shared/schedule-events';
import type { MobileMakerTransport } from '@/device-link/mobileMakerTransport';
import { markSessionScheduleRunsRead, unreadRunIdFromProjection } from '@/session/scheduleRunRead';
-import { invalidateScheduleIndexForDevice, loadSessionScheduleIndex, loadSessionScheduleIndexThrottled, loadSharedSessionScheduleIndex, resetScheduleIndexThrottleForTesting } from '@/session/scheduleIndex';
+import { invalidateScheduleIndexForDevice, invalidateScheduleIndexesAfterLinkRecovery, loadSessionScheduleIndex, loadSessionScheduleIndexThrottled, loadSharedSessionScheduleIndex, resetScheduleIndexThrottleForTesting } from '@/session/scheduleIndex';
function makerWith(
runs: readonly Record<string, unknown>[],
@@ -24,6 +24,21 @@ function transientError(message = 'target timed out'): Error & { code: string }
describe('markSessionScheduleRunsRead', () => {
beforeEach(resetScheduleIndexThrottleForTesting);
+ it('finds runs completed while disconnected even when the old success cache is still fresh', async () => {
+ const mark = vi.fn(async () => undefined);
+ const listRuns = vi.fn().mockResolvedValueOnce([]).mockResolvedValue([
+ { id: 'run-offline', scheduleId: 'sched-1', sessionId: 'session-1', status: 'success', firedAt: 1 },
+ ]);
+ const maker = makerWith([], mark, listRuns);
+ await loadSharedSessionScheduleIndex('dev-1', maker);
+ // No completed push arrives during disconnection. Recovery alone must refresh.
+ invalidateScheduleIndexesAfterLinkRecovery();
+ const home = loadSharedSessionScheduleIndex('dev-1', maker);
+ await expect(markSessionScheduleRunsRead(maker, 'session-1', 'dev-1')).resolves.toEqual(['run-offline']);
+ await home;
+ expect(listRuns).toHaveBeenCalledTimes(2);
+ expect(mark).toHaveBeenCalledExactlyOnceWith('run-offline');
+ });
it('retries a transient scan inside the shared load before caching failure', async () => {
const mark = vi.fn(async () => undefined);
const listRuns = vi.fn().mockRejectedValueOnce(transientError()).mockResolvedValue([
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index 0b659c40fc..cf861af138 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -73,7 +73,7 @@ import {
invalidateOfflineScheduleIndexFailureFor,
invalidateScheduleIndexForDevice,
invalidateTransientScheduleIndexFailureFor,
- invalidateTransientScheduleIndexFailures,
+ invalidateScheduleIndexesAfterLinkRecovery,
} from '@/session/scheduleIndex';
import { isTransientRemoteError } from '@/device-link/remoteRetry';
import {
@@ -699,10 +699,9 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
if (targetDeviceId) {
scheduler?.request(targetDeviceId);
} else {
- // Relay 重连 / App 回前台是共享生命周期事件:即使某设备此刻没有持有
- // topic,它此前因 WSS 掉线留下的 NOT_CONNECTED 负缓存也已经失效。
- // 单 peer 恢复不会走这里,仍只清自己的缓存。
- invalidateTransientScheduleIndexFailures();
+ // Relay 重连 / App 回前台可能漏过日程推送:在通知页面恢复前统一失效
+ // 旧成功快照和本机断线负缓存。单 peer 恢复仍只清自己的缓存。
+ invalidateScheduleIndexesAfterLinkRecovery();
const deviceIds = new Set(registryRef.current.deviceIds());
for (const deviceId of forcedPeerRecoveryIntentRef.current.deviceIds()) {
deviceIds.add(deviceId);
diff --git a/apps/mobile/src/session/scheduleIndex.ts b/apps/mobile/src/session/scheduleIndex.ts
index ae06b23902..4891a5a2e7 100644
--- a/apps/mobile/src/session/scheduleIndex.ts
+++ b/apps/mobile/src/session/scheduleIndex.ts
@@ -201,17 +201,14 @@ export function loadSessionScheduleIndexThrottled(
}
/**
- * 重连恢复钩子(review P1):普通断线(NOT_CONNECTED 等瞬态失败)产生的负缓存
- * 在链路恢复后立即失效——否则 30s 失败 TTL 内重连触发的 reseed 会吃到旧的
- * rejected promise,设备详情页把索引替换成空集、首页保留陈旧数据,且没有任何
- * 定时器在 TTL 过期后补拉。由 DeviceLinkContext 在每轮 rehydrate(只在 online
- * 时运行,重连必经)的共享生命周期入口调用;单 peer 恢复使用下方逐设备版本,
- * 熔断类负缓存不受影响(走各自的恢复旁路)。
+ * Relay 重连或回前台可能漏过 completed/read 推送,成功缓存也必须重新对账。
+ * 由共享恢复入口统一失效,在途扫描仍先结束再合并重拉。普通本机断线负缓存
+ * 同时清除;设备离线、熔断和请求超时的负缓存继续走各自的恢复旁路。
*/
-export function invalidateTransientScheduleIndexFailures(): void {
+export function invalidateScheduleIndexesAfterLinkRecovery(): void {
for (const [key, entry] of scheduleIndexThrottleEntries) {
- if (entry.failedAt !== null && entry.failedTransient) {
- scheduleIndexThrottleEntries.delete(key);
+ if (entry.failedAt === null || entry.failedTransient) {
+ invalidateScheduleIndexForDevice(key);
}
}
}