Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(device-link): 允许逐出超预算的新文件缓存
Dashbbf855f24fa9
diff --git a/apps/desktop/src/main/device-link/__tests__/remoteFileVersionCacheMedia.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteFileVersionCacheMedia.test.tsnew file mode 100644index 0000000000..ca886a91b8--- /dev/null+++ b/apps/desktop/src/main/device-link/__tests__/remoteFileVersionCacheMedia.test.ts@@ -0,0 +1,124 @@+import { afterEach, beforeEach, expect, it, vi } from 'vitest';+import Database from 'better-sqlite3';+import { drizzle } from 'drizzle-orm/better-sqlite3';+import { eq } from 'drizzle-orm';+import { promises as fs } from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import type { LedgerDb } from '../../cindy-media/ledger';++const state = vi.hoisted(() => ({ root: '', db: undefined as LedgerDb | undefined }));+vi.mock('electron', () => ({ app: { getPath: () => state.root } }));+vi.mock('../../appSessionState', () => ({+ getActiveAppSession: () => ({ mode: 'cloud', dataOwnerId: 'owner' }),+}));+vi.mock('../../localDb/client/current', () => ({+ getDbClient: () => ({ drizzle: state.db }),+}));+vi.mock('../broadcast-tap', () => ({+ captureDataOwnerBroadcastScope: () => ({}),+ isDataOwnerBroadcastScopeCurrent: () => true,+}));+import * as schema from '../../localDb/schema';+import { 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';++const { default: migration0071 } =+ (await import('../../../../drizzle/scripts/0071_bright_ultron')) as {+ default: { run: (db: Database.Database) => void };+ };++let raw: Database.Database;+const png = Buffer.from('89504e470d0a1a0a0000000d49484452', 'hex');+const metadata = { version: 'a'.repeat(64), size: png.length, mimeType: 'image/png' };++beforeEach(async () => {+ state.root = await fs.mkdtemp(path.join(os.tmpdir(), 'remote-media-budget-'));+ raw = new Database(':memory:');+ raw.pragma('foreign_keys = ON');+ const migration = await fs.readFile(+ path.resolve(__dirname, '../../../../drizzle/0070_woozy_harpoon.sql'),+ 'utf8',+ );+ for (const statement of migration.split('--> statement-breakpoint')) {+ if (statement.trim()) raw.exec(statement);+ }+ migration0071.run(raw);+ state.db = drizzle(raw, { schema }) as unknown as LedgerDb;+});+afterEach(async () => {+ raw.close();+ await fs.rm(state.root, { recursive: true, force: true });+});++async function seedOtherCache(bytes: number) {+ const other = await integrationCachePut(+ {+ cacheKey: 'other:full',+ integration: 'other',+ buffer: Buffer.from('other'),+ mimeType: 'image/png',+ },+ state.db,+ );+ // Exercise the real global accounting without allocating a 512 MiB fixture.+ await state+ .db!.update(schema.mediaBlobs)+ .set({ bytes })+ .where(eq(schema.mediaBlobs.hash, other.hash))+ .run();+ return other;+}++it.each([+ { otherBytes: CACHE_LIMIT_BYTES - png.length, retained: true },+ { otherBytes: CACHE_LIMIT_BYTES, retained: false },+ { otherBytes: CACHE_LIMIT_BYTES + png.length, retained: false },+])(+ 'first remote write respects global usage $otherBytes; retained=$retained',+ async ({ otherBytes, retained }) => {+ const other = await seedOtherCache(otherBytes);+ const consumer = path.join(state.root, 'consumer.png');+ await fs.writeFile(consumer, png);+ const cache = remoteFileVersionCache('device', 'url', metadata)!;+ await cache.put(consumer);+ const cached = await cache.get();+ expect(Boolean(cached)).toBe(retained);+ expect(await fs.readFile(consumer)).toEqual(png);+ if (cached) {+ expect(cached).not.toBe(consumer);+ expect(await fs.readFile(cached)).toEqual(png);+ }+ expect((await getStorageStats(state.db)).cacheBytes).toBe(+ otherBytes + (retained ? png.length : 0),+ );+ expect((await integrationCacheGet('other:full', state.db))?.absPath).toBe(other.absPath);+ expect(await fs.readFile(other.absPath)).toEqual(Buffer.from('other'));+ },+);++it.each(['shared', 'pinned'])('pressure preserves a newly reused %s blob', async (protection) => {+ await seedOtherCache(CACHE_LIMIT_BYTES);+ const existing = await integrationCachePut(+ {+ cacheKey: 'other:same',+ integration: 'other',+ buffer: png,+ mimeType: 'image/png',+ },+ state.db,+ );+ if (protection === 'pinned') {+ await pinBlob(existing.hash, state.db);+ await removeRefs({ refKind: 'integration-cache', refId: 'other:same' }, state.db);+ }+ const consumer = path.join(state.root, 'consumer.png');+ await fs.writeFile(consumer, png);+ const cache = remoteFileVersionCache('device', 'url', metadata)!;+ await cache.put(consumer);+ expect(await cache.get()).toBe(existing.absPath);+ expect(await fs.readFile(existing.absPath)).toEqual(png);+ expect(await fs.readFile(consumer)).toEqual(png);+});diff --git a/apps/desktop/src/main/device-link/remoteFileVersionCache.ts b/apps/desktop/src/main/device-link/remoteFileVersionCache.tsindex 20fbb7dcc4..3bc9b56d65 100644--- a/apps/desktop/src/main/device-link/remoteFileVersionCache.ts+++ b/apps/desktop/src/main/device-link/remoteFileVersionCache.ts@@ -97,7 +97,7 @@ export function remoteFileVersionCache(}assertCurrent();if (!verified || !supportedMime(verified)) return;- const written = await integrationCachePut(+ await integrationCachePut({cacheKey: key,integration: INTEGRATION,@@ -110,7 +110,9 @@ export function remoteFileVersionCache(assertCurrent();// Use the warehouse's existing byte budget. Only this integration's cache// is eligible: attachments, pinned media and other integrations stay protected.- const live = new Set([written.hash]);+ // 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);assertCurrent();await evictCacheBlobs(diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.mdindex 71fb6a6c25..ce925360ae 100644--- a/docs/dev-rules/media-storage-and-protocols.md+++ b/docs/dev-rules/media-storage-and-protocols.md@@ -76,6 +76,7 @@- 远程文件的长期复用(2026-10-16 用户要求)不以短 TTL 清空:媒体经 `ingestMedia` /`integration-cache` 持久入仓,`remote-file` 命名空间按现有缓存预算自动 LRU 回收。预算用量统计整个媒体仓的 cache 字节,命名空间只限制逐出候选,不另分配一份额度。+ 消费者持有独立副本,新写入的仓内缓存同样可逐出;其他缓存已占满额度时不强留新副本。自动清理只接受该命名空间独占且仍为 `isCache` 的字节;扫描和条件删除都检查其他引用与 pin,不能清理聊天附件或其他集成的共享字节。其他媒体清理维持原手动流程。普通 HTML 等非媒体仍走 `remote-file-cache`。完整身份、复验与平台清理策略见