Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(archive): 手机归档查询只取绑定投影,桌面远程任务总是直取最新绑定
Dashcc61ac0b79e0
diff --git a/apps/desktop/src/renderer/__tests__/remoteScheduleBindings.test.tsx b/apps/desktop/src/renderer/__tests__/remoteScheduleBindings.test.tsxindex 08a0c36e34..3ae4857b52 100644--- a/apps/desktop/src/renderer/__tests__/remoteScheduleBindings.test.tsx+++ b/apps/desktop/src/renderer/__tests__/remoteScheduleBindings.test.tsx@@ -199,13 +199,6 @@ describe('remote schedule binding badges', () => {expect(() => parseScheduleBindings([{ ...binding(), recurring: 'false' }])).toThrow();});- it('marks an empty first snapshot as loaded, distinguishing it from not-yet-loaded', () => {- remoteProjectsStore.setDeviceSessions('A', 'Device A', []);- expect(remoteProjectsStore.hasScheduleBindings('A')).toBe(false);- remoteProjectsStore.setDeviceScheduleBindings('A', []);- expect(remoteProjectsStore.hasScheduleBindings('A')).toBe(true);- });-it('keeps the persistent-session flag only when true, so archive confirmation can skip it', () => {expect(parseScheduleBindings([{ ...binding(), persistentSession: true }])).toEqual([{ ...binding(), persistentSession: true },diff --git a/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts b/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.tsindex 3dd09fa596..1f7f850084 100644--- a/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts+++ b/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts@@ -568,15 +568,10 @@ const actions = {const shard = shards.get(deviceId);if (!shard) return;const next = buildBindingMap(bindings);- // 首次快照即使为空也要落下:`undefined` 表示尚未加载,空 Map 才是权威的「没有绑定」。- if (shard.scheduleBindings && JSON.stringify([...shard.scheduleBindings]) === JSON.stringify([...next])) return;+ if (JSON.stringify([...(shard.scheduleBindings ?? [])]) === JSON.stringify([...next])) return;shard.scheduleBindings = next;subs.forEach((fn) => fn());},- /** 该设备的绑定快照是否已加载过(区分「尚未加载」与「确实没有绑定」)。 */- hasScheduleBindings(deviceId: string): boolean {- return shards.get(deviceId)?.scheduleBindings !== undefined;- },getSessionScheduleBindings(deviceId: string, sessionId: string): readonly ScheduleBinding[] {return shards.get(deviceId)?.scheduleBindings?.get(sessionId) ?? EMPTY_SCHEDULE_BINDINGS;},diff --git a/apps/desktop/src/renderer/features/scheduler/lib/__tests__/archiveScheduleWarning.test.ts b/apps/desktop/src/renderer/features/scheduler/lib/__tests__/archiveScheduleWarning.test.tsindex 622fcfe2c1..bc505c7796 100644--- a/apps/desktop/src/renderer/features/scheduler/lib/__tests__/archiveScheduleWarning.test.ts+++ b/apps/desktop/src/renderer/features/scheduler/lib/__tests__/archiveScheduleWarning.test.ts@@ -13,7 +13,6 @@ const mocks = vi.hoisted(() => ({getSnapshot: vi.fn(),pendingRefresh: vi.fn(),getSessionScheduleBindings: vi.fn(),- hasScheduleBindings: vi.fn(),invoke: vi.fn(),}));@@ -27,7 +26,6 @@ vi.mock('../schedulesStore', () => ({vi.mock('@/features/device-link/remoteProjectsStore', () => ({remoteProjectsStore: {getSessionScheduleBindings: mocks.getSessionScheduleBindings,- hasScheduleBindings: mocks.hasScheduleBindings,},}));@@ -49,8 +47,7 @@ describe('loadActiveBoundScheduleNames', () => {mocks.getSnapshot.mockReset();mocks.pendingRefresh.mockReset().mockReturnValue(null);mocks.getSessionScheduleBindings.mockReset().mockReturnValue([]);- mocks.hasScheduleBindings.mockReset().mockReturnValue(true);- mocks.invoke.mockReset();+ mocks.invoke.mockReset().mockResolvedValue([]);vi.stubGlobal('window', { electronAPI: { deviceLink: { invoke: mocks.invoke } } });});@@ -66,8 +63,7 @@ describe('loadActiveBoundScheduleNames', () => {expect([...result]).toEqual([['task', ['Daily']]]);});- it('远程绑定快照尚未加载时直取一次,而不是把空当作没有绑定', async () => {- mocks.hasScheduleBindings.mockReturnValue(false);+ it('远程任务总是直取最新绑定,每台设备一次,不信任本地镜像', async () => {mocks.invoke.mockResolvedValue([{ id: 'r', name: 'Remote', status: 'active', targetSessionId: 'a', cronExpr: '* * * * *' },]);@@ -82,30 +78,28 @@ describe('loadActiveBoundScheduleNames', () => {null,{ sessionBindings: true },]);+ expect(mocks.getSessionScheduleBindings).not.toHaveBeenCalled();+ expect(mocks.ensure).not.toHaveBeenCalled(); // 远程任务不读本机同 IDexpect([...result]).toEqual([['a', ['Remote']]]);});- it('远程快照未加载且直取超时时按无绑定处理', async () => {+ it('远程直取失败或超时时退回镜像里已有的快照', async () => {vi.useFakeTimers();try {- mocks.hasScheduleBindings.mockReturnValue(false);mocks.invoke.mockReturnValue(new Promise(() => {}));+ mocks.getSessionScheduleBindings.mockReturnValue([+ { id: 'm', name: 'Mirrored', status: 'active', targetSessionId: 'a' },+ ]);const pending = loadActiveBoundScheduleNames([{ id: 'a', deviceLinkDeviceId: 'mac-2' }]);await vi.advanceTimersByTimeAsync(COLD_SCHEDULE_LOAD_WAIT_MS);- expect((await pending).size).toBe(0);+ expect((await pending).get('a')).toEqual(['Mirrored']);} finally {vi.useRealTimers();}});- it('远程快照已就绪时不再发请求', async () => {- await loadActiveBoundScheduleNames([{ id: 'a', deviceLinkDeviceId: 'mac-2' }]);-- expect(mocks.invoke).not.toHaveBeenCalled();- });-it('持续会话归档后会换绑新任务继续跑,不提示', async () => {mocks.ensure.mockResolvedValue([{@@ -123,20 +117,6 @@ describe('loadActiveBoundScheduleNames', () => {expect(result.get('task')).toEqual(['Heartbeat']);});- it('远程任务读远程分片,不回退到本机同 ID', async () => {- mocks.getSessionScheduleBindings.mockReturnValue([- { id: 'r', name: 'Remote', status: 'active', targetSessionId: 'task' },- ]);-- const result = await loadActiveBoundScheduleNames([- { id: 'task', deviceLinkDeviceId: 'mac-2' },- ]);-- expect(mocks.getSessionScheduleBindings).toHaveBeenCalledWith('mac-2', 'task');- expect(mocks.ensure).not.toHaveBeenCalled();- expect(result.get('task')).toEqual(['Remote']);- });-it('有缓存时直接使用,不等列表请求', async () => {mocks.getSnapshot.mockReturnValue([{ id: 'a', name: 'Cached', status: 'active', targetSessionId: 'task' },@@ -182,7 +162,6 @@ describe('loadActiveBoundScheduleNames', () => {try {mocks.getSnapshot.mockReturnValue(null);mocks.ensure.mockReturnValue(new Promise(() => {}));- mocks.hasScheduleBindings.mockReturnValue(false);mocks.invoke.mockReturnValue(new Promise(() => {}));const pending = loadActiveBoundScheduleNames([diff --git a/apps/desktop/src/renderer/features/scheduler/lib/archiveScheduleWarning.ts b/apps/desktop/src/renderer/features/scheduler/lib/archiveScheduleWarning.tsindex 126a46c672..3c9825e4f5 100644--- a/apps/desktop/src/renderer/features/scheduler/lib/archiveScheduleWarning.ts+++ b/apps/desktop/src/renderer/features/scheduler/lib/archiveScheduleWarning.ts@@ -38,8 +38,9 @@ async function readLocalSchedules() {}/**- * 远程设备的绑定快照尚未加载(刚连上或重连后首拉还在途)时,空结果不是权威的:- * 直接取一次绑定列表,最多等 COLD_SCHEDULE_LOAD_WAIT_MS,失败或超时按「无绑定」处理。+ * 远程任务不信任本地镜像:刚连上 / 重连、或远端刚创建 / 恢复 / 改绑自动化时,镜像可能未加载或已过期。+ * 归档是用户主动的低频操作,所以每台设备直接取一次最新绑定列表,最多等 COLD_SCHEDULE_LOAD_WAIT_MS,+ * 失败或超时退回镜像里已有的快照(没有则当无绑定)。*/async function readRemoteBindingsOnce(deviceId: string,@@ -58,19 +59,14 @@ async function readRemoteBindingsOnce(export async function loadActiveBoundScheduleNames(targets: readonly ArchiveTarget[],): Promise<Map<string, string[]>> {- // 本机与各台冷远程设备的读取同时发起,共用一个等待窗口(混合设备批量归档不会叠加到 2 倍)。+ // 本机与各台远程设备的读取同时发起,共用一个等待窗口(混合设备批量归档不会叠加到 2 倍)。const needsLocal = targets.some((target) => !target.deviceLinkDeviceId);const localRead = needsLocal ? readLocalSchedules() : null;- // 快照未就绪的远程设备,每台只直接取一次。- const coldRemote = new Map<string, Promise<ReadonlyMap<string, ScheduleBinding[]> | null>>();+ const remoteReads = new Map<string, Promise<ReadonlyMap<string, ScheduleBinding[]> | null>>();for (const target of targets) {const deviceId = target.deviceLinkDeviceId;- if (- deviceId &&- !remoteProjectsStore.hasScheduleBindings(deviceId) &&- !coldRemote.has(deviceId)- ) {- coldRemote.set(+ if (deviceId && !remoteReads.has(deviceId)) {+ remoteReads.set(deviceId,readRemoteBindingsOnce(deviceId).then((list) => (list ? buildBindingMap(list) : null)),);@@ -85,9 +81,12 @@ export async function loadActiveBoundScheduleNames(const deviceId = target.deviceLinkDeviceId;let bindings: readonly ScheduleBinding[];if (!deviceId) bindings = localMap.get(target.id) ?? [];- else if (remoteProjectsStore.hasScheduleBindings(deviceId))- bindings = remoteProjectsStore.getSessionScheduleBindings(deviceId, target.id);- else bindings = (await coldRemote.get(deviceId))?.get(target.id) ?? [];+ else {+ const fresh = await remoteReads.get(deviceId);+ bindings = fresh+ ? (fresh.get(target.id) ?? [])+ : remoteProjectsStore.getSessionScheduleBindings(deviceId, target.id);+ }const names = bindings.filter((schedule) => schedule.status === 'active' && !schedule.persistentSession).map((schedule) => schedule.name);diff --git a/apps/mobile/app/devices/[deviceId].tsx b/apps/mobile/app/devices/[deviceId].tsxindex 9aa20ba6b5..eddb4cc9eb 100644--- a/apps/mobile/app/devices/[deviceId].tsx+++ b/apps/mobile/app/devices/[deviceId].tsx@@ -680,7 +680,7 @@ function DeviceDetailScreenContent() {setBulkScheduleCount(0);setBulkScheduleLoading(action === 'archive');if (action === 'archive') {- void countSessionsWithActiveBoundSchedules(maker, summary.candidates.map((session) => session.id))+ void countSessionsWithActiveBoundSchedules(deviceId, invoke, summary.candidates.map((session) => session.id)).then((count) => {if (bulkScheduleRequestRef.current !== scheduleRequest) return;setBulkScheduleCount(count);@@ -688,7 +688,7 @@ function DeviceDetailScreenContent() {});}setBulkConfirmAction(action);- }, [bulkActionSummaries, maker, t]);+ }, [bulkActionSummaries, deviceId, invoke, t]);const actionOverlays = (<>diff --git a/apps/mobile/src/__tests__/archiveScheduleConfirm.test.ts b/apps/mobile/src/__tests__/archiveScheduleConfirm.test.tsindex f6cf6b78e0..0f227dac0a 100644--- a/apps/mobile/src/__tests__/archiveScheduleConfirm.test.ts+++ b/apps/mobile/src/__tests__/archiveScheduleConfirm.test.ts@@ -6,9 +6,6 @@ const mocks = vi.hoisted(() => ({}));vi.mock('react-native', () => ({ Alert: { alert: vi.fn() } }));-vi.mock('@/device-link/mobileMakerTransport', () => ({- createMobileMakerTransport: () => ({ schedule: { list: mocks.list } }),-}));vi.mock('@/session/scheduleIndex', () => ({ sessionMayHaveScheduleRuns: mocks.mayHave }));import {@@ -17,7 +14,7 @@ import {SCHEDULE_LOOKUP_WAIT_MS,} from '@/session/archiveScheduleConfirm';-const args = { deviceId: 'pc', invoke: vi.fn(), sessionId: 'task' } as never;+const args = { deviceId: 'pc', invoke: mocks.list, sessionId: 'task' } as never;describe('archive schedule lookup deadline', () => {beforeEach(() => {@@ -31,7 +28,7 @@ describe('archive schedule lookup deadline', () => {mocks.list.mockReturnValue(new Promise(() => {}));const single = loadActiveBoundScheduleNames(args);- const bulk = countSessionsWithActiveBoundSchedules({ schedule: { list: mocks.list } } as never, ['task']);+ const bulk = countSessionsWithActiveBoundSchedules('pc', mocks.list as never, ['task']);await vi.advanceTimersByTimeAsync(SCHEDULE_LOOKUP_WAIT_MS);await expect(single).resolves.toEqual([]);@@ -47,6 +44,17 @@ describe('archive schedule lookup deadline', () => {await expect(loadActiveBoundScheduleNames(args)).resolves.toEqual(['Daily']);});+ it('asks only for the binding projection so a large list cannot overflow the frame', async () => {+ mocks.list.mockResolvedValue([]);++ await loadActiveBoundScheduleNames(args);+ await countSessionsWithActiveBoundSchedules('pc', mocks.list as never, ['task']);++ for (const call of mocks.list.mock.calls) {+ expect(call).toEqual(['pc', 'maker:schedule:list', [null, { sessionBindings: true }]]);+ }+ });+it('skips the request for tasks known to be unbound', async () => {mocks.mayHave.mockReturnValue(false);diff --git a/apps/mobile/src/session/archiveScheduleConfirm.ts b/apps/mobile/src/session/archiveScheduleConfirm.tsindex bd8ea744b7..f937cb9f43 100644--- a/apps/mobile/src/session/archiveScheduleConfirm.ts+++ b/apps/mobile/src/session/archiveScheduleConfirm.ts@@ -2,11 +2,7 @@ import { Alert } from 'react-native';import type { TFunction } from 'i18next';import { normalizeScheduleList } from '@/scheduler/scheduleModel';import type { RemoteSchedule } from '@/scheduler/types';-import {- createMobileMakerTransport,- type MobileMakerTransport,- type RemoteInvoke,-} from '@/device-link/mobileMakerTransport';+import type { RemoteInvoke } from '@/device-link/mobileMakerTransport';import { sessionMayHaveScheduleRuns } from '@/session/scheduleIndex';/**@@ -27,19 +23,28 @@ function withLookupDeadline<T>(lookup: Promise<T>, fallback: T): Promise<T> {});}+/**+ * 只取绑定投影(不含 prompt / 脚本等大字段):完整列表可能超过 device-link 帧限制,+ * 失败后被当成「无绑定」就会漏掉提醒。旧被控端忽略该参数,返回完整列表,行为不变。+ */+function listBindingSchedules(deviceId: string, invoke: RemoteInvoke): Promise<unknown> {+ return invoke(deviceId, 'maker:schedule:list', [null, { sessionBindings: true }]);+}+function stopsWhenTargetArchived(schedule: RemoteSchedule): boolean {return schedule.status === 'active' && !schedule.persistentSession && !!schedule.targetSessionId;}/** 批量归档:所选任务里归档后会停掉自动化的任务数(一次列表请求;失败按 0)。 */export async function countSessionsWithActiveBoundSchedules(- maker: Pick<MobileMakerTransport, 'schedule'>,+ deviceId: string,+ invoke: RemoteInvoke,sessionIds: readonly string[],): Promise<number> {try {const ids = new Set(sessionIds);const bound = new Set(- normalizeScheduleList(await withLookupDeadline(maker.schedule.list(), []))+ normalizeScheduleList(await withLookupDeadline(listBindingSchedules(deviceId, invoke), [])).filter((schedule) => stopsWhenTargetArchived(schedule) && ids.has(schedule.targetSessionId!)).map((schedule) => schedule.targetSessionId),);@@ -63,7 +68,7 @@ export async function loadActiveBoundScheduleNames({if (!sessionMayHaveScheduleRuns(deviceId, sessionId, source)) return [];try {const schedules = normalizeScheduleList(- await withLookupDeadline(createMobileMakerTransport({ deviceId, invoke }).schedule.list(), []),+ await withLookupDeadline(listBindingSchedules(deviceId, invoke), []),);return schedules.filter((schedule) => schedule.targetSessionId === sessionId && stopsWhenTargetArchived(schedule))