Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 隔离长期图片预算并阻止清理后迟到写回
Dash3ef0ca1e3067
diff --git a/apps/desktop/src/main/cindy-media/ledger.ts b/apps/desktop/src/main/cindy-media/ledger.tsindex 6e708c9da0..fb9ee15119 100644--- a/apps/desktop/src/main/cindy-media/ledger.ts+++ b/apps/desktop/src/main/cindy-media/ledger.ts@@ -608,6 +608,16 @@ export interface CacheBlobRow {lastAccessAt: number;}+/** Count shared blobs once, including bytes also referenced by other integrations. */+export async function getIntegrationCacheBytes(integration: string, db: LedgerDb = defaultDb()): Promise<number> {+ const [row] = await db.select({ bytes: sql<number>`COALESCE(SUM(${mediaBlobs.bytes}), 0)` })+ .from(mediaBlobs).where(and(eq(mediaBlobs.isCache, true), sql`EXISTS (+ SELECT 1 FROM ${mediaRefs} WHERE ${mediaRefs.hash} = ${mediaBlobs.hash}+ AND ${mediaRefs.refKind} = 'integration-cache' AND ${mediaRefs.originId} = ${integration}+ )`)).all();+ return row.bytes;+}+/** cache 性质 blob 按 LRU 升序(最久未用在前),供逐出候选规划。 */export async function listCacheBlobsByLru(db: LedgerDb = defaultDb(), integration?: string): Promise<CacheBlobRow[]> {return await dbdiff --git a/apps/desktop/src/main/device-link/__tests__/remoteFileVersionCacheMedia.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteFileVersionCacheMedia.test.tsindex ca886a91b8..c58991620b 100644--- a/apps/desktop/src/main/device-link/__tests__/remoteFileVersionCacheMedia.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/remoteFileVersionCacheMedia.test.ts@@ -20,7 +20,7 @@ vi.mock('../broadcast-tap', () => ({isDataOwnerBroadcastScopeCurrent: () => true,}));import * as schema from '../../localDb/schema';-import { getStorageStats, pinBlob, removeRefs } from '../../cindy-media/ledger';+import { addRef, getStorageStats, pinBlob, removeRefs } from '../../cindy-media/ledger';import { integrationCacheGet, integrationCachePut } from '../../cindy-media/integrationCache';import { CACHE_LIMIT_BYTES } from '../../cindy-media/recycler';import { remoteFileVersionCache } from '../remoteFileVersionCache';@@ -53,11 +53,11 @@ afterEach(async () => {await fs.rm(state.root, { recursive: true, force: true });});-async function seedOtherCache(bytes: number) {+async function seedOtherCache(bytes: number, integration = 'other') {const other = await integrationCachePut({- cacheKey: 'other:full',- integration: 'other',+ cacheKey: `${integration}:full`,+ integration,buffer: Buffer.from('other'),mimeType: 'image/png',},@@ -72,6 +72,21 @@ async function seedOtherCache(bytes: number) {return other;}+it.each([false, true])('excludes long-lived remote images from the automatic budget (shared=%s)', async (shared) => {+ const retained = await seedOtherCache(CACHE_LIMIT_BYTES * 2, 'remote-media');+ if (shared) await addRef({+ hash: retained.hash, refKind: 'integration-cache', refId: 'other:shared',+ originId: 'other', originKind: 'integration',+ }, state.db);+ const cache = remoteFileVersionCache('device', 'mutable-path', metadata)!;+ await cache.put(png);+ const cached = await cache.get();+ expect(cached).not.toBeNull();+ expect(await fs.readFile(cached!)).toEqual(png);+ expect((await getStorageStats(state.db)).cacheBytes).toBe(CACHE_LIMIT_BYTES * 2 + png.length);+ expect(await fs.readFile(retained.absPath)).toEqual(Buffer.from('other'));+});+it.each([{ otherBytes: CACHE_LIMIT_BYTES - png.length, retained: true },{ otherBytes: CACHE_LIMIT_BYTES, retained: false },diff --git a/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.tsindex eace79ed98..9e99460c24 100644--- a/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts@@ -13,6 +13,9 @@ const state = vi.hoisted(() => ({generation: 1,db: undefined as LedgerDb | undefined,}));+const peer = vi.hoisted(() => vi.fn());+vi.mock('../filePeer', () => ({ tryPeerFile: peer }));+vi.mock('../mediaTransfer', () => ({ removeRemote: vi.fn() }));vi.mock('electron', () => ({ app: { getPath: () => state.root } }));vi.mock('../../appSessionState', () => ({getActiveAppSession: () => ({ mode: 'cloud', dataOwnerId: state.owner }),@@ -31,6 +34,7 @@ import {remoteMediaPersistentCache,} from '../remoteMediaPersistentCache';import { remoteFileVersionCache } from '../remoteFileVersionCache';+import { readRemoteDeviceFile } from '../fileAccess';const { default: migration0071 } =(await import('../../../../drizzle/scripts/0071_bright_ultron')) as {@@ -45,6 +49,7 @@ function reopen() {state.db = drizzle(raw, { schema }) as unknown as LedgerDb;}beforeEach(async () => {+ peer.mockReset();state.root = await fs.mkdtemp(path.join(os.tmpdir(), 'remote-image-cache-'));state.owner = 'alice';state.generation = 1;@@ -57,6 +62,24 @@ beforeEach(async () => {if (statement.trim()) raw.exec(statement);migration0071.run(raw);});++it.each(['prepare', 'peer-queue'])('does not recreate the disk index after clearing during %s', async (phase) => {+ const metadata = { ossKey: '', transferRequired: true, version: 'b'.repeat(64), size: png.length, mimeType: 'image/png' };+ const invoke = vi.fn(async () => {+ if (phase === 'prepare') invalidateRemoteMediaPersistentWrites();+ return { ok: true, result: metadata };+ });+ peer.mockImplementationOnce(async (device, source, _invoke, _signal, _progress, captured) => {+ if (phase === 'peer-queue') invalidateRemoteMediaPersistentWrites();+ // The cache adapter is created only after the delayed host reply / queue slot.+ const cache = remoteFileVersionCache(device, source, captured)!;+ await expect(cache.put(png)).rejects.toThrow('REMOTE_MEDIA_CACHE_CLEARED');+ return { ...metadata, path: '/consumer-copy', dispose: async () => {} };+ });+ await readRemoteDeviceFile('computer', url, invoke);+ expect(peer).toHaveBeenCalledOnce();+ expect(await listCacheBlobsByLru(state.db, 'remote-media')).toEqual([]);+});afterEach(async () => {raw.close();await fs.rm(state.root, { recursive: true, force: true });diff --git a/apps/desktop/src/main/device-link/__tests__/remoteMediaProtocol.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteMediaProtocol.test.tsindex e7ae4c7deb..dee50ccd03 100644--- a/apps/desktop/src/main/device-link/__tests__/remoteMediaProtocol.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/remoteMediaProtocol.test.ts@@ -83,6 +83,7 @@ vi.mock('../../lightboxMediaActions', async (importOriginal) => {import { __testing, fetchRemoteMediaImageBytes } from '../remoteMediaProtocol';import { buildRemoteMediaUrl } from '../../../shared/remoteMediaUrl';+import { invalidateRemoteMediaPersistentWrites } from '../remoteMediaPersistentCache';const { handleRemoteMedia } = __testing;const URL_IMG = buildRemoteMediaUrl({ kind: 'device', deviceId: 'dev-1' }, 'xdt-image://s/a.png');@@ -106,6 +107,45 @@ beforeEach(() => {});describe('handleRemoteMedia', () => {+ it.each(['prepare', 'inline', 'peer', 'download', 'retry'])('does not refill caches after clearing during %s', async (phase) => {+ let invokes = 0;+ remoteInvoke.mockImplementation(async () => {+ invokes++;+ if (phase === 'prepare' || phase === 'inline' || (phase === 'retry' && invokes === 2)) invalidateRemoteMediaPersistentWrites();+ return { ok: true, result: phase === 'peer'+ ? { ossKey: '', size: 3, mimeType: 'image/png', transferRequired: true }+ : phase === 'inline' ? { ossKey: '', size: 3, mimeType: 'image/png', inlineBase64: 'aW1n' }+ : { ossKey: 'image', size: 3, mimeType: 'image/png' } };+ });+ const dispose = vi.fn(async () => {});+ if (phase === 'peer') {+ tryPeerFile.mockImplementationOnce(async () => {+ invalidateRemoteMediaPersistentWrites();+ return { path: '/temporary', size: 3, mimeType: 'image/png', dispose };+ });+ fsReadFile.mockResolvedValueOnce(Buffer.from('img'));+ }+ downloadToBuffer.mockImplementation(async () => {+ if (phase === 'retry' && invokes === 1) throw new Error('stale OSS key');+ if (phase === 'download') invalidateRemoteMediaPersistentWrites();+ return { bytes: Buffer.from('img') };+ });+ expect((await handleRemoteMedia(URL_IMG, null)).status).toBe(502);+ expect(recordLocal).not.toHaveBeenCalled();+ expect(persistentPut).not.toHaveBeenCalled();+ if (phase === 'peer') expect(dispose).toHaveBeenCalledOnce();+ });++ it('does not restore warm bytes from a disk read completed after cleanup', async () => {+ persistentGet.mockImplementationOnce(async () => {+ invalidateRemoteMediaPersistentWrites();+ return { bytes: Buffer.from('img'), mimeType: 'image/png' } as never;+ });+ expect((await handleRemoteMedia(URL_IMG, null)).status).toBe(502);+ expect(recordLocal).not.toHaveBeenCalled();+ expect(remoteInvoke).not.toHaveBeenCalled();+ });+it('serves a persisted image after the memory cache is empty without contacting the host', async () => {const bytes = Buffer.from('saved');persistentGet.mockResolvedValueOnce({ bytes, mimeType: 'image/png' } as never);@@ -328,10 +368,10 @@ describe('handleRemoteMedia', () => {expect(r.status).toBe(200);});- it('未命中 + 视频 → recordStream → OSS range 206 透传,不整下、不删 OSS', async () => {- remoteInvoke.mockResolvedValue({- ok: true,- result: { ossKey: 'oss/v.mp4', mimeType: 'video/mp4', size: 1000 },+ it.each([false, true])('视频保持流式播放,清理图片缓存不影响取件 (clear=%s)', async (clear) => {+ remoteInvoke.mockImplementation(async () => {+ if (clear) invalidateRemoteMediaPersistentWrites();+ return { ok: true, result: { ossKey: 'oss/v.mp4', mimeType: 'video/mp4', size: 1000 } };});recordStream.mockReturnValue({kind: 'stream',diff --git a/apps/desktop/src/main/device-link/fileAccess.ts b/apps/desktop/src/main/device-link/fileAccess.tsindex 62a9023ad8..b125fd8770 100644--- a/apps/desktop/src/main/device-link/fileAccess.ts+++ b/apps/desktop/src/main/device-link/fileAccess.ts@@ -9,6 +9,7 @@ import { tryPeerFile } from './filePeer';import { captureFilePeerScope } from './filePeerScope';import { removeRemote } from './mediaTransfer';import { captureDataOwnerBroadcastScope, isDataOwnerBroadcastScopeCurrent } from './broadcast-tap';+import { captureRemoteMediaCacheValidity } from './remoteMediaPersistentCache';type Invoke = Parameters<typeof tryPeerFile>[2];type LocalFileResult = DeviceFileResult & { path: string; dispose(): Promise<void> };@@ -28,9 +29,11 @@ export function readRemoteDeviceFile(workdir?: string;relPath?: string;fallback?: () => Promise<DeviceFileResult>;+ assertCacheCurrent?: () => void;} = {},) {const owner = captureDataOwnerBroadcastScope();+ const assertCacheCurrent = options.assertCacheCurrent ?? captureRemoteMediaCacheValidity();const isCurrent = captureFilePeerScope(device);const invoke: Invoke = async (target, channel, args) => {// A connection can outlive this read, including an offer answered after cancellation.@@ -94,7 +97,7 @@ export function readRemoteDeviceFile()return null;const result = await tryPeerFile(device, url, invoke, options.signal, options.onProgress,- options.skipCache ? { ...metadata, skipCache: true } : metadata);+ { ...metadata, ...(options.skipCache ? { skipCache: true } : {}), assertCacheCurrent });// A failed direct attempt is discarded before OSS starts; do not leave its partial// percentage/speed on screen during the fallback preparation.if (!result && !options.signal?.aborted && isCurrent()) {diff --git a/apps/desktop/src/main/device-link/remoteFileVersionCache.ts b/apps/desktop/src/main/device-link/remoteFileVersionCache.tsindex 3c1e429e0e..9de797c993 100644--- a/apps/desktop/src/main/device-link/remoteFileVersionCache.ts+++ b/apps/desktop/src/main/device-link/remoteFileVersionCache.ts@@ -16,12 +16,14 @@ import {writeVersionedRemoteFile,} from '../file-browser/remote-file-cache';import { captureDataOwnerBroadcastScope, isDataOwnerBroadcastScopeCurrent } from './broadcast-tap';-import { removeRefs } from '../cindy-media/ledger';+import { getIntegrationCacheBytes, removeRefs } from '../cindy-media/ledger';import { captureRemoteMediaCacheValidity, isImmutableRemoteMedia, REMOTE_MEDIA_INTEGRATION } from './remoteMediaPersistentCache';const INTEGRATION = 'remote-file';export type FileVersionMetadata = Pick<DeviceFileResult, 'version' | 'size' | 'mimeType'> & {skipCache?: boolean;+ /** Local-only guard captured before host preparation, never sent over the wire. */+ assertCacheCurrent?: () => void;};/** An adapter to the existing media warehouse / ordinary-file LRU, not another byte store.@@ -48,6 +50,7 @@ export function remoteFileVersionCache(const media = /^(image|audio|video|model)\//.test(mimeType);const db = media ? getDbClient().drizzle : undefined;const assertCurrent = () => {+ metadata.assertCacheCurrent?.();if (!isDataOwnerBroadcastScopeCurrent(owner)) throw new Error('FILE_PEER_CANCELLED');assertRetained();};@@ -147,7 +150,10 @@ export function remoteFileVersionCache(// The consumer owns the input file, not this warehouse copy. Even the// newest copy must be evictable when other integrations fill the budget.const live = new Set<string>();- const scan = await scanCache({ live, integration: INTEGRATION }, db);+ // Long-lived copies are manually managed and must not consume the automatic budget.+ const retainedBytes = await getIntegrationCacheBytes(REMOTE_MEDIA_INTEGRATION, db);+ assertCurrent();+ const scan = await scanCache({ live, integration: INTEGRATION, limitBytes: CACHE_LIMIT_BYTES + retainedBytes }, db);assertCurrent();await evictCacheBlobs({diff --git a/apps/desktop/src/main/device-link/remoteMediaProtocol.ts b/apps/desktop/src/main/device-link/remoteMediaProtocol.tsindex 068e9220ca..dff12029ba 100644--- a/apps/desktop/src/main/device-link/remoteMediaProtocol.ts+++ b/apps/desktop/src/main/device-link/remoteMediaProtocol.ts@@ -36,7 +36,7 @@ import * as cache from './remoteMediaCacheStore';import type { RemoteMediaEntry, StreamEntry } from './remoteMediaCacheStore';import { activeOwnerScopeKey } from '../appSessionState';import { remoteFileVersionCache } from './remoteFileVersionCache';-import { captureRemoteMediaOwner, isImmutableRemoteMedia, remoteMediaPersistentCache } from './remoteMediaPersistentCache';+import { captureRemoteMediaCacheValidity, captureRemoteMediaOwner, isImmutableRemoteMedia, remoteMediaPersistentCache } from './remoteMediaPersistentCache';const log = createLogger('device-link:remoteMediaProtocol');@@ -115,15 +115,23 @@ async function ensureEntry(deviceId: string, origUrl: string): Promise<RemoteMedconst pending = inflight.get(k);if (pending) return pending;const assertCurrent = captureRemoteMediaOwner();+ const assertCacheCurrent = captureRemoteMediaCacheValidity();const p = (async () => {let disk: ReturnType<typeof remoteMediaPersistentCache> = null;try { disk = remoteMediaPersistentCache(deviceId, origUrl); } catch { /* Cache is optional. */ }const saved = await disk?.get(LOCAL_BUFFER_MAX).catch(() => null);assertCurrent();- if (saved) return cache.recordLocal(deviceId, origUrl, saved.bytes, saved.mimeType);- const entry = await fetchAndCache(deviceId, origUrl);+ if (saved) {+ assertCacheCurrent();+ return cache.recordLocal(deviceId, origUrl, saved.bytes, saved.mimeType);+ }+ const entry = await fetchAndCache(deviceId, origUrl, assertCacheCurrent);assertCurrent();- if (entry.kind === 'local') await disk?.put(entry.bytes, entry.mimeType).catch(() => undefined);+ if (entry.kind === 'local') {+ assertCacheCurrent();+ await disk?.put(entry.bytes, entry.mimeType).catch(() => undefined);+ assertCacheCurrent();+ }assertCurrent();return entry;})().finally(() => inflight.delete(k));@@ -150,10 +158,10 @@ async function downloadLocalEntry(deviceId: string,origUrl: string,fetched: MediaFetchResult,+ assertCurrent: () => void,): Promise<RemoteMediaEntry> {- const assertCurrent = captureRemoteMediaOwner();let versioned: ReturnType<typeof remoteFileVersionCache> = null;- try { versioned = remoteFileVersionCache(deviceId, origUrl, fetched); } catch { /* Optional cache. */ }+ try { versioned = remoteFileVersionCache(deviceId, origUrl, { ...fetched, assertCacheCurrent: assertCurrent }); } catch { /* Optional cache. */ }try {const { bytes } = await downloadToBuffer(fetched.ossKey);assertCurrent();@@ -169,8 +177,10 @@ function toEntry(deviceId: string,origUrl: string,fetched: MediaFetchResult,+ assertCurrent: () => void,): Promise<RemoteMediaEntry> {if (fetched.inlineBase64 !== undefined) {+ assertCurrent();return Promise.resolve(cache.recordLocal(deviceId,@@ -186,19 +196,20 @@ function toEntry(cache.recordStream(deviceId, origUrl, fetched.ossKey, fetched.mimeType, fetched.size),);}- return downloadLocalEntry(deviceId, origUrl, fetched);+ return downloadLocalEntry(deviceId, origUrl, fetched, assertCurrent);}-async function fetchAndCache(deviceId: string, origUrl: string): Promise<RemoteMediaEntry> {+async function fetchAndCache(deviceId: string, origUrl: string, assertCacheCurrent: () => void): Promise<RemoteMediaEntry> {const assertCurrent = captureRemoteMediaOwner();const fetched = await readRemoteDeviceFile(deviceId, origUrl, remoteInvoke, {maxPeerBytes: LOCAL_BUFFER_MAX,stream: true,+ assertCacheCurrent,});if ('path' in fetched) {try {const bytes = await fs.readFile(fetched.path);- assertCurrent();+ assertCacheCurrent();return cache.recordLocal(deviceId,origUrl,@@ -211,15 +222,15 @@ async function fetchAndCache(deviceId: string, origUrl: string): Promise<RemoteM}assertCurrent();try {- return await toEntry(deviceId, origUrl, fetched);+ return await toEntry(deviceId, origUrl, fetched, assertCacheCurrent);} catch (err) {- assertCurrent();+ assertCacheCurrent();// 下载失败的常见根因:被控端图片上传去重缓存返回了悬空 key(对象已被消费方用后删)。// 带 skipCache 重取一次强制重传;仍失败按原语义抛给上层(renderer 占位 + 可重试)。log.warn(`remote media download failed, retrying with skipCache: ${String(err)}`);const refetched = await invokeMediaFetch(deviceId, origUrl, true);assertCurrent();- return toEntry(deviceId, origUrl, refetched);+ return toEntry(deviceId, origUrl, refetched, assertCacheCurrent);}}@@ -416,9 +427,10 @@ async function handleRemoteMedia(// cache miss 窗口):否则窗口期的并发请求会发起**不带 skipCache** 的普通取件,// 被控端去重缓存可能把同一把 stale key 再记回缓存、覆盖掉新对象。const p = (async () => {+ const assertCacheCurrent = captureRemoteMediaCacheValidity();const refetched = await invokeMediaFetch(deviceId, parsed.origUrl, true);assertCurrent();- return toEntry(deviceId, parsed.origUrl, refetched);+ return toEntry(deviceId, parsed.origUrl, refetched, assertCacheCurrent);})().finally(() => inflight.delete(k));inflight.set(k, p);const fresh = await p;diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.mdindex 132cf48ed0..82697aea1a 100644--- a/docs/dev-rules/media-storage-and-protocols.md+++ b/docs/dev-rules/media-storage-and-protocols.md@@ -75,7 +75,8 @@不可再生用户内容,必须沿用现有 pin/引用流程转为不可按缓存回收。- 远程文件的长期复用(2026-10-16 用户要求)不以短 TTL 清空:媒体经 `ingestMedia` /`integration-cache` 持久入仓,`remote-file` 命名空间按现有缓存预算自动 LRU 回收。- 预算用量统计整个媒体仓的 cache 字节,命名空间只限制逐出候选,不另分配一份额度。+ 预算用量统计整个媒体仓的 cache 字节,但排除手动管理的 `remote-media` 长期副本;+ 命名空间只限制逐出候选,不另分配一份额度。消费者持有独立副本,新写入的仓内缓存同样可逐出;其他缓存已占满额度时不强留新副本。自动清理只接受该命名空间独占且仍为 `isCache` 的字节;扫描和条件删除都检查其他引用与 pin,不能清理聊天附件或其他集成的共享字节。其他媒体清理维持原手动流程。diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.mdindex 9479afd957..ee3619adab 100644--- a/docs/dev-rules/protocol-compatibility.md+++ b/docs/dev-rules/protocol-compatibility.md@@ -841,6 +841,7 @@ prepare 授权与版本查询。缺失/非法版本的旧端不启用此缓存不设时间 TTL、16 MiB 单文件门槛或条目数门槛。账号代次用于拒绝迟到结果,不能进入持久缓存键。Desktop 图片等媒体复用 `cindy-media` 的 `integration-cache` 索引和既有 512 MiB 缓存预算,真实文件探测后流式入仓;超过预算的单份媒体不额外缓存。+手动清理的 `remote-media` 不可变图片副本不占此自动回收预算,包括与其他集成共享的字节。只对 `remote-file` 独占的可再生缓存按 LRU 自动回收,SQL 删除时复验 pin 和其他引用。HTML 等普通文件复用 `remote-file-cache` 的 4 GiB 总量上限、LRU 与启动残片清扫;同源新版本成功保存后清旧版本,不在媒体目录另存字节。