Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(files): 保留更新前副本并校验缓存版本
Dash3171ab2f38d5
diff --git a/apps/desktop/src/main/device-link/__tests__/fileAccess.test.ts b/apps/desktop/src/main/device-link/__tests__/fileAccess.test.tsindex 25da52d402..36d255199c 100644--- a/apps/desktop/src/main/device-link/__tests__/fileAccess.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/fileAccess.test.ts@@ -19,6 +19,28 @@ beforeEach(() => {peer.mockResolvedValue(null);});describe('desktop shared file access', () => {+ it('waits for the current same-size version when filling an outer version cache', async () => {+ const old = { ossKey: '', size: 3, mimeType: 'text/html', version: '1'.repeat(64), inlineBase64: 'b2xk' };+ const fresh = { ...old, version: '2'.repeat(64), inlineBase64: 'bmV3' };+ const invoke = vi.fn().mockResolvedValue({ ok: true, result: old });+ const url = 'xdt-file://open?path=/versioned.html';+ await readRemoteDeviceFile('versioned', url, invoke);+ cache.get.mockResolvedValue('/cache/old');+ const read = vi.spyOn(fs, 'readFile').mockResolvedValue(Buffer.from('old'));+ let reply!: (value: unknown) => void;+ invoke.mockImplementationOnce(() => new Promise(resolve => { reply = resolve; }));+ let settled = false;+ const pending = readRemoteDeviceFile('versioned', url, invoke, { cacheFirst: false })+ .then(result => { settled = true; return result; });+ try {+ await vi.waitFor(() => expect(reply).toBeTypeOf('function'));+ expect(settled).toBe(false);+ expect(read).not.toHaveBeenCalled();+ reply({ ok: true, result: fresh });+ expect(await pending).toEqual(fresh);+ expect(cache.invalidate).not.toHaveBeenCalled();+ } finally { read.mockRestore(); }+ });it('returns cached HTML before a blocked host version check completes', async () => {const result = { ossKey: '', size: 3, mimeType: 'text/html', version: 'c'.repeat(64), inlineBase64: 'YWJj' };cache.get.mockResolvedValueOnce(null).mockResolvedValueOnce('/cache/instant');diff --git a/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.tsindex 9e99460c24..c813131cca 100644--- a/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts@@ -19,6 +19,7 @@ vi.mock('../mediaTransfer', () => ({ removeRemote: vi.fn() }));vi.mock('electron', () => ({ app: { getPath: () => state.root } }));vi.mock('../../appSessionState', () => ({getActiveAppSession: () => ({ mode: 'cloud', dataOwnerId: state.owner }),+ activeOwnerScopeKey: () => `${state.owner}:${state.generation}`,}));vi.mock('../../localDb/client/current', () => ({ getDbClient: () => ({ drizzle: state.db }) }));vi.mock('../broadcast-tap', () => ({diff --git a/apps/desktop/src/main/device-link/fileAccess.ts b/apps/desktop/src/main/device-link/fileAccess.tsindex 625b00ba55..6168777fb6 100644--- a/apps/desktop/src/main/device-link/fileAccess.ts+++ b/apps/desktop/src/main/device-link/fileAccess.ts@@ -33,6 +33,8 @@ export function readRemoteDeviceFile(/** Bytes actually written by the direct receiver, shared by every file entry point. */onProgress?: (received: number, total: number) => void;skipCache?: boolean;+ /** Version-keyed outer caches must wait for host validation before filling. */+ cacheFirst?: boolean;maxPeerBytes?: number;maxBytes?: number;stream?: boolean;@@ -76,7 +78,7 @@ export function readRemoteDeviceFile(return response.result as DeviceFileResult;};return readDeviceFile<LocalFileResult>({- cacheFirst: options.skipCache || isSharedTaskPeer(device) ? undefined : {+ cacheFirst: options.skipCache || options.cacheFirst === false || isSharedTaskPeer(device) ? undefined : {key: cacheKey,read: async () => {const previous = latestFiles.get(cacheKey);diff --git a/apps/desktop/src/main/file-browser/index.ts b/apps/desktop/src/main/file-browser/index.tsindex f5aaf31c4a..03446cf234 100644--- a/apps/desktop/src/main/file-browser/index.ts+++ b/apps/desktop/src/main/file-browser/index.ts@@ -226,6 +226,8 @@ async function fetchRemoteBigFile(remoteInvoke,{// Cache fills are shared: closing one preview must not cancel another consumer.+ // This fill is keyed by the caller's stat; never relabel stale bytes as that version.+ cacheFirst: false,workdir: args.workdir,relPath: args.relPath,maxBytes: Math.max(1, args.size),diff --git a/apps/mobile/src/__tests__/htmlPreviewCsp.test.ts b/apps/mobile/src/__tests__/htmlPreviewCsp.test.tsindex 29370a5ac5..2599c5d673 100644--- a/apps/mobile/src/__tests__/htmlPreviewCsp.test.ts+++ b/apps/mobile/src/__tests__/htmlPreviewCsp.test.ts@@ -295,7 +295,10 @@ describe('资源取件的安全接线(源码级守卫)', () => {expect(snapshotSource).toContain('finally { for (const key of uploaded) deps.deleteOssObject(key); }');expect(pageSource).toContain("method: 'DELETE'");const media = readFileSync(resolve(process.cwd(), 'src/session/remoteMedia.ts'), 'utf8');- expect(media.indexOf('opts?.onOssKey?.(fetched.ossKey);')).toBeLessThan(media.indexOf('const signed = await deps.presignGet('));+ const collected = media.indexOf('opts?.onOssKey?.(fetched.ossKey);');+ const presigned = media.indexOf('deps.presignGet(fetched.ossKey)');+ expect(collected).toBeGreaterThanOrEqual(0);+ expect(presigned).toBeGreaterThan(collected);});it('SSH 会话的资源取件必须带会话上下文', () => {diff --git a/apps/mobile/src/__tests__/peerFileCache.test.ts b/apps/mobile/src/__tests__/peerFileCache.test.tsindex dcff3f2bae..03b7f54eae 100644--- a/apps/mobile/src/__tests__/peerFileCache.test.ts+++ b/apps/mobile/src/__tests__/peerFileCache.test.ts@@ -184,3 +184,24 @@ it("reserves only one copy on a hit and preserves its source when space cannot breleasePeerMedia(peerMediaUri(result!)!);expect(new Set(disk.files.keys())).toEqual(before);});++it.each(['download', 'store', 'direct'] as const)('keeps the old version when %s cannot reserve replacement space', async mode => {+ const cache = await seed(`replace-pressure-${mode}`);+ const before = new Set(disk.files.keys());+ const newer = { ...metadata, version: 'b'.repeat(64) };+ disk.available = 256 * 1024 ** 2 + metadata.size - 1;+ if (mode === 'download') {+ expect(await cache.download('device', 'url', newer, 'https://unavailable', () => true)).toBeNull();+ } else if (mode === 'direct') {+ expect(await cache.prune('device', 'url', newer.size)).toBe(false);+ } else {+ const uri = 'file:///replacement';+ disk.files.set(uri, new Uint8Array(newer.size));+ const result = { ossKey: '', ...newer };+ recordPeerMedia(result, uri, () => disk.files.delete(uri));+ await cache.store('device', 'url', newer, result);+ releasePeerMedia(uri);+ }+ expect(new Set(disk.files.keys())).toEqual(before);+ expect(await cache.readLatest('device', 'url', () => true)).toMatchObject(metadata);+});diff --git a/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts b/apps/mobile/src/__tests__/remoteMediaDiskCache.test.tsindex 5093009f21..b625e2503a 100644--- a/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts+++ b/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts@@ -36,6 +36,17 @@ const URL_B = 'xdt-image://cache/b.png';const URL_C = 'xdt-image://cache/c.png';describe('remoteMediaDiskCache', () => {+ it('preserves the previous version when replacement storage is unavailable', async () => {+ const h = memoryIO();+ let available = 10_000;+ const cache = createRemoteMediaDiskCache(h.io, { availableBytes: () => available, reserveBytes: 500 });+ await cache.store(URL_A, 'https://old', 'text/html', 1000, 'a'.repeat(64));+ available = 1499;+ expect(await cache.store(URL_A, 'https://new', 'text/html', 1000, 'b'.repeat(64))).toBe(false);+ expect(h.io.download).toHaveBeenCalledTimes(1);+ expect(h.io.deleteFile).not.toHaveBeenCalled();+ expect(await cache.lookup(URL_A)).toMatchObject({ version: 'a'.repeat(64), size: 1000 });+ });it('keeps the previous version available until a changed download successfully commits', async () => {const { io } = memoryIO();const cache = createRemoteMediaDiskCache(io);diff --git a/apps/mobile/src/__tests__/remoteMediaDiskCacheExpo.test.ts b/apps/mobile/src/__tests__/remoteMediaDiskCacheExpo.test.tsindex 9ef3d5a6aa..3fcf94453e 100644--- a/apps/mobile/src/__tests__/remoteMediaDiskCacheExpo.test.ts+++ b/apps/mobile/src/__tests__/remoteMediaDiskCacheExpo.test.ts@@ -4,7 +4,9 @@ import {downloadRemoteMediaShareTemp,withDownloadedRemoteMediaFile,retainRemoteFilePreview,+ createExpoRemoteMediaDiskCacheIO,} from '@/session/remoteMediaDiskCacheExpo';+import { createRemoteMediaDiskCache } from '@/session/remoteMediaDiskCache';const io = vi.hoisted(() => ({size: 5,@@ -12,6 +14,7 @@ const io = vi.hoisted(() => ({copied: new Set<string>(),download: vi.fn(),remove: vi.fn(),+ move: vi.fn(),resumable: vi.fn(),cancel: vi.fn(async () => undefined),deleteAsync: vi.fn(async () => undefined),@@ -31,6 +34,7 @@ vi.mock('expo-file-system', () => ({uri: string;constructor(parent: string, name: string) { this.uri = `${parent}/${name}`; }create() {}+ list() { return []; }},File: class {uri: string;@@ -47,6 +51,9 @@ vi.mock('expo-file-system', () => ({get size() { return io.copying.has(this.uri) ? 0 : io.size; }get exists() { return true; }async base64() { return 'aGVsbG8='; }+ async text() { return ''; }+ write() {}+ moveSync(target: { uri: string }) { io.move(this.uri, target.uri); }delete() { io.remove(this.uri); }static downloadFileAsync = io.download;},@@ -57,12 +64,29 @@ beforeEach(() => {io.copying.clear();io.copied.clear();io.remove.mockClear();+ io.move.mockClear();io.download.mockReset().mockImplementation(async (_url, target) => target);io.resumable.mockReset().mockResolvedValue({ status: 200 });io.cancel.mockClear();io.deleteAsync.mockClear();});+describe('versioned cache with the Expo adapter', () => {+ it.each(['data:text/css;base64,', 'file:///empty.css', 'https://example.com/empty.css'])(+ 'retains a confirmed empty file from %s without accepting empty unversioned images', async source => {+ io.size = 0;+ const cache = createRemoteMediaDiskCache(createExpoRemoteMediaDiskCacheIO());+ const version = 'a'.repeat(64);+ expect(await cache.store('resource', source, 'text/css', 0, version)).toBe(true);+ expect(await cache.lookup('resource', version)).toMatchObject({ size: 0, version, mimeType: 'text/css' });+ expect(io.remove).not.toHaveBeenCalled();+ expect(io.move).toHaveBeenCalled();+ expect(await cache.store('image', 'https://example.com/empty.png', 'image/png', 0)).toBe(false);+ expect(await cache.lookup('image')).toBeNull();+ },+ );+});+describe('bounded temporary media downloads', () => {it('keeps a preview copy alive after transport staging expires, until its owner releases it', async () => {const { recordPeerMedia, releasePeerMedia } = await import('@/device-link/peerFileRegistry');diff --git a/apps/mobile/src/device-link/peerFileCache.ts b/apps/mobile/src/device-link/peerFileCache.tsindex 16d93ce59a..22ba6f1e9a 100644--- a/apps/mobile/src/device-link/peerFileCache.ts+++ b/apps/mobile/src/device-link/peerFileCache.ts@@ -52,7 +52,7 @@ async function openPeerFileCache(owner: string) {/** A completed OSS fallback joins the same version cache as direct downloads. */async download(device: string, url: string, metadata: Metadata, downloadUrl: string,active: () => boolean, signal?: AbortSignal): Promise<LocalPeerMedia | null> {- if (!active() || !validFileVersion(metadata.version) || !(await cache.prune(2 * metadata.size))) return null;+ if (!active() || !validFileVersion(metadata.version) || !(await api.prune(device, url, metadata.size))) return null;const uri = await downloadRemoteMediaShareTemp(downloadUrl, metadata.mimeType, undefined, signal);if (!uri) return null;const file = new File(uri);@@ -78,7 +78,7 @@ async function openPeerFileCache(owner: string) {await cache.store(JSON.stringify([device, url]),`data:${result.mimeType};base64,${result.inlineBase64}`, result.mimeType, result.size, result.version);},- prune: (size: number) => cache.prune(2 * size),+ prune: (device: string, url: string, size: number) => cache.prune(2 * size, JSON.stringify([device, url])),invalidate: (device: string, url: string) =>cache.remove(JSON.stringify([device, url])),async read(@@ -133,7 +133,7 @@ async function openPeerFileCache(owner: string) {return;const uri = peerMediaUri(result);// Account for both the durable copy and the consumer's upcoming copy.- if (!uri || !(await cache.prune(2 * result.size))) return;+ if (!uri || !(await api.prune(device, url, result.size))) return;await cache.store(JSON.stringify([device, url]),uri,diff --git a/apps/mobile/src/device-link/peerFileTransport.tsx b/apps/mobile/src/device-link/peerFileTransport.tsxindex c2ab474a87..8493be6a53 100644--- a/apps/mobile/src/device-link/peerFileTransport.tsx+++ b/apps/mobile/src/device-link/peerFileTransport.tsx@@ -596,7 +596,7 @@ export function PeerFileTransport() {mobileDebugLog('debug', 'files', 'file cache hit', { trace: readTrace, size: cached.size });return cached;}- if (metadata) await cache?.prune(metadata.size).catch(() => false);+ if (metadata) await cache?.prune(device, url, metadata.size).catch(() => false);check();const result = await transfer(device, url, signal, readTrace);if (result && active() && !signal?.aborted)diff --git a/apps/mobile/src/session/remoteMediaDiskCache.ts b/apps/mobile/src/session/remoteMediaDiskCache.tsindex 79df86e53e..d80f7d5fab 100644--- a/apps/mobile/src/session/remoteMediaDiskCache.ts+++ b/apps/mobile/src/session/remoteMediaDiskCache.ts@@ -31,7 +31,7 @@ export interface RemoteMediaDiskCacheIO {* 契约:落位必须原子(临时位置下载、成功后移入)——失败时不得破坏已存在的* 同名文件,store 的失败清理依赖这一点区分「半成品」与「现存好文件」。*/- download(url: string, name: string): Promise<number | null>;+ download(url: string, name: string, options?: { allowEmpty: boolean }): Promise<number | null>;/*** 把 base64 字节写入缓存文件(inline 缩略图落盘用);成功返回字节数,失败返回* null(不抛)。契约与 download 相同:落位必须原子,失败不得破坏既有同名文件。@@ -252,7 +252,7 @@ export function createRemoteMediaDiskCache(await init();// 单对象就超预算:落盘立刻会被 LRU 逐出,整个下载都是白费,直接跳过。if (expectedSize !== undefined && expectedSize > maxBytes) return false;- if (options.availableBytes && !(await prune(expectedSize ?? 0))) return false;+ if (options.availableBytes && !(await prune(expectedSize ?? 0, sourceUrl))) return false;const key = cacheKeyOf(sourceUrl);const run = async (): Promise<boolean> => {// The OS or manual cache cleanup may remove the directory after init.@@ -261,7 +261,7 @@ export function createRemoteMediaDiskCache(// version must never race an overwrite of the same physical file.const name = `${key}${version ? `-${version}` : ''}.${extOfMime(mimeType)}`;const prev = entries?.get(key);- const size = await io.download(downloadUrl, name);+ const size = await io.download(downloadUrl, name, { allowEmpty: !!version && expectedSize === 0 });if (size !== null && expectedSize !== undefined && size !== expectedSize) {await Promise.resolve(io.deleteFile(name)).catch(() => undefined);return false;diff --git a/apps/mobile/src/session/remoteMediaDiskCacheExpo.ts b/apps/mobile/src/session/remoteMediaDiskCacheExpo.tsindex bee7eca5fd..f3ce41f429 100644--- a/apps/mobile/src/session/remoteMediaDiskCacheExpo.ts+++ b/apps/mobile/src/session/remoteMediaDiskCacheExpo.ts@@ -251,16 +251,17 @@ export function createExpoRemoteMediaDiskCacheIO(namespace?: string): RemoteMedireturn null; // 缓存是纯优化,写入失败静默(渲染回退 data URI)}},- async download(url, name) {+ async download(url, name, options) {// 契约(RemoteMediaDiskCacheIO.download):失败不得破坏既有同名好文件。// downloadFileAsync 直接写目标(Android 失败可留半成品),所以先下到临时名,- // 正尺寸成功后原子顶替;失败 / 空文件清临时、不动现有文件(返回 null,上层+ // 确认尺寸后原子顶替;仅已确认版本与零尺寸的文件允许为空,普通图片仍拒绝空内容。+ // 失败清临时、不动现有文件(返回 null,上层// 保留旧条目继续离线可看)。残留的 .dl.tmp 由 init 对账当孤儿清掉。const tmp = new File(dir, `${name}.dl.tmp`);try {const downloaded = await copyRemoteFile(url, tmp);- const size = downloaded.size ?? 0;- if (size <= 0) {+ const size = downloaded.size ?? -1;+ if (size < 0 || (size === 0 && !options?.allowEmpty)) {if (tmp.exists) tmp.delete();return null;}