Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(files): 修复共享取件兼容并复验 HTML 缓存
Dash16229d33f022
diff --git a/apps/desktop/src/main/device-link/__tests__/fileAccess.test.ts b/apps/desktop/src/main/device-link/__tests__/fileAccess.test.tsindex 36d255199c..ce244f69dd 100644--- a/apps/desktop/src/main/device-link/__tests__/fileAccess.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/fileAccess.test.ts@@ -1,5 +1,6 @@import { beforeEach, describe, expect, it, vi } from 'vitest';import { promises as fs } from 'node:fs';+import { sharedTaskHostPeer } from '@cindy/device-link';const peer = vi.hoisted(() => vi.fn());const remove = vi.hoisted(() => vi.fn());const owner = vi.hoisted(() => ({ current: true }));@@ -19,6 +20,17 @@ beforeEach(() => {peer.mockResolvedValue(null);});describe('desktop shared file access', () => {+ it('reads through an older shared host without sending unknown metadata-only fields', async () => {+ const result = { ossKey: '', size: 3, mimeType: 'text/plain', inlineBase64: 'YWJj' };+ const invoke = vi.fn(async (_device, _channel, [request]) => {+ if ('metadataOnly' in request) throw new Error('PERMISSION_DENIED');+ return { ok: true, result };+ });+ const device = sharedTaskHostPeer('shared', 'host');+ expect(await readRemoteDeviceFile(device, 'xdt-file://open?path=/a.txt', invoke)).toEqual(result);+ expect(await readRemoteDeviceFile(device, 'xdt-file://open?path=/a.txt', invoke)).toEqual(result);+ expect(invoke).toHaveBeenCalledTimes(2);+ });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' };diff --git a/apps/desktop/src/main/device-link/__tests__/sharedTaskDispatch.test.ts b/apps/desktop/src/main/device-link/__tests__/sharedTaskDispatch.test.tsindex fcb0ea54f7..fd9fef220c 100644--- a/apps/desktop/src/main/device-link/__tests__/sharedTaskDispatch.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/sharedTaskDispatch.test.ts@@ -169,11 +169,16 @@ describe('sharedTask dispatch scope', () => {expect(() => assertSharedTaskInvoke(noHistory, { channel, args: ['task', 'bash-1'] })).toThrow();});it('accepts media preparation and OSS fallback without granting the file-peer channel', () => {- for (const prepareOnly of [true, false]) {- expect(() => assertSharedTaskInvoke(capture(), {- channel: 'device-link:media:fetch', args: [{ url: 'xdt-image://task/a.png', prepareOnly }],- })).not.toThrow();+ for (const options of [{ prepareOnly: true, metadataOnly: true }, { prepareOnly: true }, { prepareOnly: false }]) {+ const request = { channel: 'device-link:media:fetch', args: [{ url: 'xdt-image://task/a.png', ...options }] };+ for (const phase of ['invoke', 'result'] as const) {+ expect(() => assertSharedTaskInvoke(capture(), request, undefined, phase)).not.toThrow();+ expect(() => assertSharedTaskInvoke({ ...capture(), authorize: () => false }, request, undefined, phase)).toThrow('PERMISSION_DENIED');+ }}+ expect(() => assertSharedTaskInvoke(capture(), {+ channel: 'device-link:media:fetch', args: [{ url: 'xdt-image://task/a.png', metadataOnly: 'true' }],+ })).toThrow('PERMISSION_DENIED');expect(() => assertSharedTaskInvoke(capture(), {channel: 'device-link:media:fetch', args: [{ url: 'xdt-image://task/a.png', prepareOnly: true, sessionId: 'other' }],})).toThrow();diff --git a/apps/desktop/src/main/device-link/fileAccess.ts b/apps/desktop/src/main/device-link/fileAccess.tsindex 6168777fb6..cfa43b981f 100644--- a/apps/desktop/src/main/device-link/fileAccess.ts+++ b/apps/desktop/src/main/device-link/fileAccess.ts@@ -69,7 +69,8 @@ export function readRemoteDeviceFile(};const fetch = async (prepareOnly: boolean, metadataOnly = false): Promise<DeviceFileResult> => {const response = await invoke(device, DL_MEDIA_FETCH_CHANNEL, [- { url, prepareOnly, ...(metadataOnly ? { metadataOnly: true } : {}), ...(options.skipCache ? { skipCache: true } : {}) },+ // Older shared hosts reject unknown fields; retain their authorized inline/OSS path.+ { url, prepareOnly, ...(metadataOnly && !isSharedTaskPeer(device) ? { metadataOnly: true } : {}), ...(options.skipCache ? { skipCache: true } : {}) },]);if (!response.ok) {const error = (response as { error?: { code?: string; message?: string } }).error;diff --git a/apps/desktop/src/main/device-link/sharedTaskDispatch.ts b/apps/desktop/src/main/device-link/sharedTaskDispatch.tsindex 831e7417b2..18eaab5afb 100644--- a/apps/desktop/src/main/device-link/sharedTaskDispatch.ts+++ b/apps/desktop/src/main/device-link/sharedTaskDispatch.ts@@ -255,7 +255,8 @@ export function assertSharedTaskInvoke(if (channel === 'device-link:media:fetch') {const request = record(args[0]);if (args.length !== 1 || !request || typeof request.url !== 'string' ||- Object.keys(request).some((key) => !['url', 'skipCache', 'thumbnail', 'prepareOnly'].includes(key)) ||+ Object.keys(request).some((key) => !['url', 'skipCache', 'thumbnail', 'prepareOnly', 'metadataOnly'].includes(key)) ||+ (request.metadataOnly !== undefined && typeof request.metadataOnly !== 'boolean') ||!capture.authorize('attachment.read')) deny();// The media handler validates ledger/workdir ownership before reading bytes.return;diff --git a/apps/desktop/src/main/file-browser/__tests__/html-preview-ipc.test.ts b/apps/desktop/src/main/file-browser/__tests__/html-preview-ipc.test.tsindex 6c95c00c18..9f3a6617ec 100644--- a/apps/desktop/src/main/file-browser/__tests__/html-preview-ipc.test.ts+++ b/apps/desktop/src/main/file-browser/__tests__/html-preview-ipc.test.ts@@ -81,7 +81,7 @@ it('opens a preview before downloading even a large entry file', async () => {await expect(mocks.handle.mock.calls[0][1]({}, args)).resolves.toMatchObject({ ok: true });expect(read).not.toHaveBeenCalled();});-it('serves cached HTML while remote stat is blocked, then uses the changed file on the next open', async () => {+it.each([1, 2])('serves cached HTML while stat is blocked, then refreshes with mtime %s', async (mtimeMs) => {const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-preview-swr-test-'));let reply!: (value: any) => void;const metadata = { relPath: 'index.html', type: 'file' as const, size: 3, mtimeMs: 1 };@@ -99,10 +99,11 @@ it('serves cached HTML while remote stat is blocked, then uses the changed fileexpect(await (await readResource(second.url, '/index.html')).text()).toContain('old');expect(read).toHaveBeenCalledOnce();read.mockResolvedValueOnce(path.join(dir, 'new.html'));- reply({ ...metadata, mtimeMs: 2 });+ reply({ ...metadata, mtimeMs });await vi.waitFor(() => expect(read).toHaveBeenCalledTimes(2));const third = await open({}, args);expect(await (await readResource(third.url, '/index.html')).text()).toContain('new');+ expect(await (await readResource(second.url, '/index.html')).text()).toContain('old');} finally { await fs.rm(dir, { recursive: true, force: true }); }});it('passes the captured owner guard through reference-free cache ingestion', async () => {diff --git a/apps/desktop/src/main/file-browser/__tests__/remoteFileCacheVersions.test.ts b/apps/desktop/src/main/file-browser/__tests__/remoteFileCacheVersions.test.tsindex 57175bc81b..88b600215b 100644--- a/apps/desktop/src/main/file-browser/__tests__/remoteFileCacheVersions.test.ts+++ b/apps/desktop/src/main/file-browser/__tests__/remoteFileCacheVersions.test.ts@@ -118,6 +118,35 @@ it('distinguishes same-size sub-millisecond versions in downloads and write-thro);});+it.each(['success', 'failure', 'cancel'] as const)('revalidates unchanged stat without losing the old copy on %s', async (outcome) => {+ await putCachedContent(id, 'old');+ const original = __cacheTesting.cachePathFor(id);+ const started = barrier();+ const release = barrier();+ const controller = new AbortController();+ const finished = barrier();+ const executor = vi.fn(async (dest: string) => {+ started.resolve();+ await release.promise;+ try {+ if (outcome === 'failure') throw new Error('offline');+ await fs.writeFile(dest, 'new');+ } finally { finished.resolve(); }+ });+ const pending = fetchRemoteFileToCache(id, executor, vi.fn(), controller.signal, { revalidate: true });+ const settled = pending.then(() => 'success', () => 'failure');+ try {+ await vi.waitFor(() => expect(executor).toHaveBeenCalledOnce());+ await started.promise;+ expect(await fs.readFile(original, 'utf8')).toBe('old');+ if (outcome === 'cancel') controller.abort();+ } finally { release.resolve(); }+ expect(await settled).toBe(outcome === 'success' ? 'success' : 'failure');+ await finished.promise;+ await vi.waitFor(async () => expect((await fs.readdir(getRemoteFileCacheRoot())).some(name => name.endsWith('.part'))).toBe(false));+ expect(await fs.readFile(original, 'utf8')).toBe(outcome === 'success' ? 'new' : 'old');+});+it('keeps legacy rounded copies available offline without accepting them as exact hits', async () => {const exact = { ...id, mtimeMs: 1000 };const currentPath = __cacheTesting.cachePathFor(exact);diff --git a/apps/desktop/src/main/file-browser/html-preview-ipc.ts b/apps/desktop/src/main/file-browser/html-preview-ipc.tsindex 888785bb40..fa50b7089c 100644--- a/apps/desktop/src/main/file-browser/html-preview-ipc.ts+++ b/apps/desktop/src/main/file-browser/html-preview-ipc.ts@@ -25,6 +25,7 @@ const log = createLogger('html-preview');export const HTML_PREVIEW_CHANNEL = 'maker:html-preview:open';interface RemoteSource {stat(args: HtmlPreviewArgs, root: string, rel: string): Promise<Omit<DirEntry, 'name'>>;+ /** Validate the source even when size/mtime match; they are not a complete version. */read(args: HtmlPreviewArgs, root: string, entry: DirEntry, signal?: AbortSignal): Promise<string>;}@@ -118,7 +119,6 @@ export function registerHtmlPreviewIpc(remote: RemoteSource): void {const metadata = await remote.stat(args, root, rel);if (!isCurrent() || metadata.type !== 'file') return;const entry = { ...metadata, name: cached.metadata.name };- if (entry.size === cached.metadata.size && entry.mtimeMs === cached.metadata.mtimeMs) return;const file = await remote.read(args, root, entry);if (isCurrent()) files.set(key, { metadata: entry, path: file });})().catch(() => { /* Preserve the usable snapshot while offline. */ })diff --git a/apps/desktop/src/main/file-browser/index.ts b/apps/desktop/src/main/file-browser/index.tsindex 03446cf234..95bb462edf 100644--- a/apps/desktop/src/main/file-browser/index.ts+++ b/apps/desktop/src/main/file-browser/index.ts@@ -196,6 +196,8 @@ async function fetchRemoteBigFile(mtimeMs: number;remoteHostId?: string | null;deviceId?: string | null;+ /** HTML background reads must validate beyond the outer size/mtime cache key. */+ revalidate?: boolean;},onProgress: (received: number, total: number, phase?: 'upload' | 'download') => void,signal?: AbortSignal,@@ -334,6 +336,7 @@ async function fetchRemoteBigFile(},onProgress,signal,+ { revalidate: args.revalidate },);}if (args.remoteHostId) {@@ -357,6 +360,7 @@ async function fetchRemoteBigFile(),onProgress,signal,+ { revalidate: args.revalidate },);}throw new Error('fetch-remote requires remoteHostId or deviceId');@@ -420,6 +424,7 @@ export function registerFileBrowserIpc(): void {relPath: entry.relPath,size: entry.size,mtimeMs: entry.mtimeMs,+ revalidate: true,deviceId: args.origin.kind === 'device' ? args.origin.deviceId : undefined,remoteHostId: args.origin.kind === 'ssh' ? args.origin.remoteHostId : undefined,},diff --git a/apps/desktop/src/main/file-browser/remote-file-cache.ts b/apps/desktop/src/main/file-browser/remote-file-cache.tsindex a8c7250159..8d3f838b23 100644--- a/apps/desktop/src/main/file-browser/remote-file-cache.ts+++ b/apps/desktop/src/main/file-browser/remote-file-cache.ts@@ -328,6 +328,7 @@ export async function fetchRemoteFileToCache(executor: FetchExecutor,onProgress: FetchProgressFn,signal?: AbortSignal,+ options: { revalidate?: boolean } = {},): Promise<string> {const scope = id.scope ?? activeOwnerScopeKey();assertCacheOwner(scope);@@ -343,6 +344,24 @@ export async function fetchRemoteFileToCache(log.debug('cache progress consumer unavailable');}};+ // Only real fills enter inflight: a revalidation must never join a plain cache hit.+ // Join an active fill synchronously so another consumer's cancellation cannot abort it.+ const pendingFill = inflight.get(dest);+ if (!options.revalidate && (!pendingFill || pendingFill.controller.signal.aborted)) {+ const st = await fs.stat(dest).catch(() => null);+ assertCacheOwner(scope);+ if (signal?.aborted) throw new Error('FILE_PEER_CANCELLED');+ if (st?.isFile() && st.size === id.size) {+ const now = new Date();+ await fs.utimes(dest, now, now).catch(() => undefined);+ assertCacheOwner(scope);+ if (signal?.aborted) throw new Error('FILE_PEER_CANCELLED');+ consumerProgress(id.size, id.size);+ assertCacheOwner(scope);+ if (signal?.aborted) throw new Error('FILE_PEER_CANCELLED');+ return dest;+ }+ }const existing = inflight.get(dest);if (existing && !existing.controller.signal.aborted) {const consumer = Symbol('remote-file-consumer');@@ -378,22 +397,6 @@ export async function fetchRemoteFileToCache(};const firstConsumer = Symbol('remote-file-consumer');const run = (async () => {- try {- const st = await fs.stat(dest);- assertActive();- if (st.size === id.size) {- // 命中:touch 更新 LRU 位次,秒回。- const now = new Date();- await fs.utimes(dest, now, now).catch(() => undefined);- report(id.size, id.size);- assertCacheOwner(scope);- return dest;- }- assertCacheOwner(scope);- await fs.rm(dest, { force: true });- } catch {- // miss- }assertActive();await fs.mkdir(cacheDir(), { recursive: true });// An abandoned executor can still be unwinding when its replacement starts.diff --git a/apps/mobile/src/__tests__/mobileMakerTransport.test.ts b/apps/mobile/src/__tests__/mobileMakerTransport.test.tsindex e821b3d118..99ffa2f13b 100644--- a/apps/mobile/src/__tests__/mobileMakerTransport.test.ts+++ b/apps/mobile/src/__tests__/mobileMakerTransport.test.ts@@ -3,6 +3,7 @@ import {CONTROLLER_CAPABILITY_PROVIDER_LOGO_KINDS_V2,FILE_INLINE_MAX_BYTES,REMOTE_INVOKE_ALLOWLIST,+ sharedTaskHostPeer,} from "@cindy/device-link";import {createMobileMakerTransport,@@ -37,6 +38,17 @@ function harness() {}describe("mobile maker transport", () => {+ it('reads through an older shared host without sending unknown metadata-only fields', async () => {+ const result = { ossKey: '', size: 3, mimeType: 'text/plain', inlineBase64: 'YWJj' };+ const invoke = vi.fn(async (_device, _channel, [request]) => {+ if ('metadataOnly' in request) throw new Error('PERMISSION_DENIED');+ return result;+ });+ const maker = createMobileMakerTransport({ deviceId: sharedTaskHostPeer('shared', 'host'), invoke: invoke as RemoteInvoke });+ expect(await maker.fetchRemoteMedia('xdt-file://open?path=/a.txt', { stream: false })).toEqual(result);+ expect(await maker.fetchRemoteMedia('xdt-file://open?path=/a.txt', { stream: false })).toEqual(result);+ expect(invoke).toHaveBeenCalledTimes(2);+ });it('opens a cached file without waiting for connection or host metadata, then refreshes in background', async () => {setMobileAuthOwner('cache-first-owner');const cached = { ossKey: '', size: 3, mimeType: 'application/pdf', version: 'a'.repeat(64), inlineBase64: 'YWJj' };diff --git a/apps/mobile/src/device-link/mobileMakerTransport.ts b/apps/mobile/src/device-link/mobileMakerTransport.tsindex a63b6c578c..19e6bc490f 100644--- a/apps/mobile/src/device-link/mobileMakerTransport.ts+++ b/apps/mobile/src/device-link/mobileMakerTransport.ts@@ -1076,7 +1076,8 @@ export function createMobileMakerTransport({{url,prepareOnly,- ...(metadataOnly ? { metadataOnly: true } : {}),+ // Older shared hosts use a closed request-field allowlist.+ ...(metadataOnly && !isSharedTaskPeer(deviceId) ? { metadataOnly: true } : {}),...(opts?.skipCache ? { skipCache: true } : {}),...(opts?.thumbnail ? { thumbnail: true } : {}),},diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.mdindex 91185493f4..cca087e315 100644--- a/docs/dev-rules/protocol-compatibility.md+++ b/docs/dev-rules/protocol-compatibility.md@@ -863,12 +863,16 @@ Mobile 复用 `remote-media` 磁盘缓存,放入 realm/账号 SHA-256 命名`device-link:media:fetch` 在 `prepareOnly: true` 时接受可选 `metadataOnly: true`:仍先授权并从打开的文件句柄取版本,但小文件也只返回 `transferRequired: true` 与元数据。两端共用读取策略:小文件先查同一版本缓存,未命中再以原 `prepareOnly` 请求取 inline 字节;-大文件沿用 peer/OSS 读取。旧 Host 忽略 `metadataOnly`,新控制端接收原 inline/OSS 结果;+大文件沿用 peer/OSS 读取。同账号旧 Host 忽略 `metadataOnly`,新控制端接收原 inline/OSS 结果;+共享任务的旧 Host 使用闭集参数白名单,两端访客不发送 `metadataOnly`,沿用先授权的 inline/OSS+读取。新 Host 同时接受布尔型 `metadataOnly`,不改变成员、附件归属及工作目录校验。旧控制端不传此字段,新 Host 保持原行为。两段式导出缺版本时,以导出前后主机版本和大小一致作为登记条件,变化或无版本的结果不登记为长期有效。缓存失败不阻断正常读取。Mobile 的 HTML 资源、文件浏览器原图、PDF、下载/分享使用完整文件路径,先取当前账号、设备、完整 URL 对应的本地缓存,再后台检查版本;同源在途后台检查合并。连接恢复也放到缓存读取之后。-Desktop 完整文件与远程 HTML 预览复用已知元数据和现有磁盘副本,后台检查变化;进程重启后+Desktop 完整文件与远程 HTML 预览复用已知元数据和现有磁盘副本,后台检查变化;HTML 的大小/mtime+仅用于快照展示和传输上限,不作为后台跳过读取的版本判据。复验绕过外层 size/mtime 命中,+device 仍可复用主机确认版本的字节,SSH 重新读取;新版原子保存前保留旧副本。进程重启后尚无元数据索引时先查询版本,字节仍可从已有磁盘缓存复用。后台失败保留旧副本,更新完成后后续打开使用新版;不强制重载正在操作的 HTML、图片或 PDF。预览持有自己的副本,关闭时仅释放这份副本,不会删除可复用的源缓存。零字节 HTML 配套资源也可按版本缓存。