Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(device-link): 修复缓存取件取消与空间清理边界

Dasha930b2360f98
diff --git a/apps/desktop/src/main/device-link/filePeer.ts b/apps/desktop/src/main/device-link/filePeer.ts
index 9b4e8440dc..72256290f7 100644
--- a/apps/desktop/src/main/device-link/filePeer.ts
+++ b/apps/desktop/src/main/device-link/filePeer.ts
@@ -40,6 +40,7 @@ import { captureFilePeerScope, invalidateFilePeerScopes } from './filePeerScope'
import { createLogger } from '../logger.js';
import { handlePeerAttachment } from './peerAttachmentStore';
import { fileVersion } from './fileVersion';
+import { remoteFileVersionCache, type FileVersionMetadata } from './remoteFileVersionCache';
// Local-only diagnostics (scope is not upload-allowlisted): stages, sizes and timings, never paths.
const log = createLogger('device-link:filePeer');
@@ -699,7 +700,7 @@ export function tryPeerFile(
invoke: Invoke,
signal?: AbortSignal,
onProgress?: (received: number, total: number) => void,
- metadata?: import('./remoteFileVersionCache').FileVersionMetadata,
+ metadata?: FileVersionMetadata,
) {
const isCurrent = captureFilePeerScope(peer);
refreshCooldownOwner();
@@ -707,9 +708,11 @@ export function tryPeerFile(
peer,
async () => {
if (!isCurrent()) throw new Error('FILE_PEER_CANCELLED');
- const cache = metadata && validFileVersion(metadata.version)
- ? await import('./remoteFileVersionCache').then((module) => module.remoteFileVersionCache(peer, url, metadata)).catch(() => null)
- : null;
+ let cache = null;
+ try {
+ cache = metadata && validFileVersion(metadata.version)
+ ? remoteFileVersionCache(peer, url, metadata) : null;
+ } catch { /* Cache availability must not prevent a fresh transfer. */ }
if (metadata?.skipCache) await cache?.invalidate().catch(() => undefined);
const cached = metadata?.skipCache ? null : await cache?.get().catch(() => null);
if (cached && metadata) {
diff --git a/apps/mobile/src/__tests__/peerFileCache.test.ts b/apps/mobile/src/__tests__/peerFileCache.test.ts
index ec481b1824..a9d2356f73 100644
--- a/apps/mobile/src/__tests__/peerFileCache.test.ts
+++ b/apps/mobile/src/__tests__/peerFileCache.test.ts
@@ -6,6 +6,7 @@ const disk = vi.hoisted(() => ({
copying: false,
gate: undefined as Promise<void> | undefined,
seq: 0,
+ available: 10 * 1024 ** 3,
}));
vi.mock("expo-crypto", () => ({
CryptoDigestAlgorithm: { SHA256: "sha256" },
@@ -14,7 +15,7 @@ vi.mock("expo-crypto", () => ({
randomUUID: () => `copy-${++disk.seq}`,
}));
vi.mock("expo-file-system", () => ({
- Paths: { cache: "file:///cache", availableDiskSpace: 10 * 1024 ** 3 },
+ Paths: { cache: "file:///cache", get availableDiskSpace() { return disk.available; } },
Directory: class {
uri: string;
constructor(...parts: Array<string | { uri: string }>) {
@@ -90,6 +91,7 @@ afterEach(() => {
clearPeerMedia();
disk.gate = undefined;
disk.copying = false;
+ disk.available = 10 * 1024 ** 3;
});
const metadata = { size: 5, mimeType: "text/html", version: "a".repeat(64) };
async function seed(owner: string) {
@@ -140,3 +142,17 @@ it("invalidates a bad cache even if the forced transfer later fails, while consu
expect(await cache.read("device", "url", metadata, () => true)).toBeNull();
expect(disk.files.get(uri)).toEqual(Uint8Array.from([1, 2, 3, 4, 5]));
});
+
+it("reserves only one copy on a hit and preserves its source when space cannot be reclaimed", async () => {
+ const cache = await seed("pressure-owner");
+ const before = new Set(disk.files.keys());
+ const reserve = 256 * 1024 ** 2;
+ disk.available = reserve + metadata.size - 1;
+ expect(await cache.read("device", "url", metadata, () => true)).toBeNull();
+ expect(new Set(disk.files.keys())).toEqual(before);
+ disk.available = reserve + metadata.size;
+ const result = await cache.read("device", "url", metadata, () => true);
+ expect(result).not.toBeNull();
+ releasePeerMedia(peerMediaUri(result!)!);
+ expect(new Set(disk.files.keys())).toEqual(before);
+});
diff --git a/apps/mobile/src/__tests__/peerFileLifecycle.test.tsx b/apps/mobile/src/__tests__/peerFileLifecycle.test.tsx
index 6d7fbce9b7..f53bab8b7a 100644
--- a/apps/mobile/src/__tests__/peerFileLifecycle.test.tsx
+++ b/apps/mobile/src/__tests__/peerFileLifecycle.test.tsx
@@ -12,6 +12,9 @@ const state = vi.hoisted(() => ({
change: (_state: string) => {},
upload: undefined as any,
read: undefined as any,
+ download: undefined as any,
+ cache: { read: vi.fn(), store: vi.fn(), prune: vi.fn(), invalidate: vi.fn() },
+ deleted: vi.fn(),
reset: undefined as any,
commands: [] as any[],
rpc: vi.fn(),
@@ -65,13 +68,20 @@ vi.mock("react-native-webview", async () => {
vi.mock("expo-file-system", () => ({
Directory: class {
exists = false;
+ create() {}
},
- File: class {},
- Paths: { cache: "/cache" },
+ File: class {
+ uri = "/staging";
+ exists = true;
+ create() {}
+ open() { return { close() {} }; }
+ delete() { this.exists = false; state.deleted(); }
+ },
+ Paths: { cache: "/cache", availableDiskSpace: 10 * 1024 ** 3 },
}));
vi.mock("expo-crypto", () => ({ randomUUID: () => `id-${++state.sequence}` }));
vi.mock("@/device-link/peerFileCache", () => ({
- createPeerFileCache: async () => null,
+ createPeerFileCache: async () => state.cache,
}));
vi.mock("@/auth/AuthContext", () => ({
useAuth: () => ({
@@ -95,8 +105,12 @@ vi.mock("@cindy/device-link", async (original) => ({
...(await original<typeof import("@cindy/device-link")>()),
resolveDesktopIceServers: async () => [],
}));
-vi.mock("@/device-link/peerFileRegistry", () => ({
- installPeerFileDownload: () => () => {},
+vi.mock("@/device-link/peerFileRegistry", async (original) => ({
+ ...(await original<typeof import("@/device-link/peerFileRegistry")>()),
+ installPeerFileDownload: (callback: any) => {
+ state.download = callback;
+ return () => { state.download = undefined; };
+ },
installPeerInvoke: (callback: any) => {
state.read = callback;
return () => {
@@ -116,9 +130,6 @@ vi.mock("@/device-link/peerFileRegistry", () => ({
state.reset = undefined;
};
},
- clearPeerMedia() {},
- recordPeerMedia() {},
- canStagePeerMedia: () => true,
}));
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: Root;
@@ -139,6 +150,10 @@ beforeEach(() => {
state.sessionBound = true;
state.app = "active";
state.commands = [];
+ state.deleted.mockClear();
+ state.cache.read.mockReset().mockResolvedValue(null);
+ state.cache.store.mockReset().mockResolvedValue(undefined);
+ state.cache.prune.mockReset().mockResolvedValue(true);
state.open.mockReset().mockResolvedValue(undefined);
state.rpc
.mockReset()
@@ -150,6 +165,8 @@ beforeEach(() => {
idleMs: 300_000,
...(state.sessionBound ? { sessionBound: true } : {}),
}
+ : request.action === "open"
+ ? { ticket: "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", size: 0, mimeType: "text/html", version: "a".repeat(64) }
: { connection: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", sdp: "v=0" },
);
root = createRoot(document.createElement("div"));
@@ -233,3 +250,50 @@ it("rebuilds only on demand after failure and ignores delayed closure of the old
act(() => state.reset("device"));
expect(await read()).toBeNull();
});
+
+it.each([
+ ["background", false], ["reset", false], ["abort", false],
+ ["background", true], ["reset", true], ["abort", true],
+] as const)("rejects and releases a file cancelled during cache store: %s, store fails=%s", async (cause, fails) => {
+ let resume!: () => void;
+ state.cache.store.mockImplementation(() => new Promise<void>((resolve, reject) => {
+ resume = () => fails ? reject(new Error("cache unavailable")) : resolve();
+ }));
+ const controller = new AbortController();
+ const pending = state.download("device", "url", controller.signal, 1,
+ { size: 0, mimeType: "text/html", version: "a".repeat(64) });
+ const cancelled = expect(pending).rejects.toThrow("FILE_PEER_CANCELLED");
+ await vi.waitFor(() => expect(state.cache.store).toHaveBeenCalledTimes(1));
+ act(() => {
+ if (cause === "background") {
+ state.app = "background";
+ state.change("background");
+ } else if (cause === "reset") state.reset("device");
+ else controller.abort();
+ });
+ resume();
+ await cancelled;
+ expect(state.deleted).toHaveBeenCalledTimes(1);
+});
+
+it("returns a cache hit without reserving space for another transfer", async () => {
+ const cached = { ossKey: "", size: 5, mimeType: "text/html" };
+ state.cache.read.mockResolvedValue(cached);
+ expect(await state.download("device", "url", undefined, 1, cached)).toBe(cached);
+ expect(state.cache.prune).not.toHaveBeenCalled();
+ expect(state.open).not.toHaveBeenCalled();
+});
+
+it("releases a cache hit invalidated just before its promise resumes", async () => {
+ const { recordPeerMedia } = await import("@/device-link/peerFileRegistry");
+ const cached = { ossKey: "", size: 5, mimeType: "text/html" };
+ const dispose = vi.fn();
+ state.cache.read.mockImplementation(async () => {
+ recordPeerMedia(cached, "/cached-copy", dispose);
+ state.reset("device");
+ return cached;
+ });
+ await expect(state.download("device", "url")).rejects.toThrow("FILE_PEER_CANCELLED");
+ expect(dispose).toHaveBeenCalledOnce();
+ expect(state.open).not.toHaveBeenCalled();
+});
diff --git a/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts b/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts
index eec98927f8..0047d2efa7 100644
--- a/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts
+++ b/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts
@@ -36,6 +36,22 @@ const URL_B = 'xdt-image://cache/b.png';
const URL_C = 'xdt-image://cache/c.png';
describe('remoteMediaDiskCache', () => {
+ it('protects the source of a pending copy while reclaiming other entries', async () => {
+ const h = memoryIO();
+ let capacity = 10_000;
+ const cache = createRemoteMediaDiskCache(h.io, {
+ now: () => 1, reserveBytes: 500,
+ availableBytes: () => capacity - [...h.files.values()].reduce((sum, n) => sum + n, 0),
+ });
+ await cache.store(URL_A, 'file:///a', 'text/html', 1000);
+ await cache.store(URL_B, 'file:///b', 'text/html', 1000);
+ capacity = 2500;
+ expect(await cache.prune(1000, URL_A)).toBe(true);
+ expect(await cache.lookup(URL_A)).not.toBeNull();
+ expect(await cache.lookup(URL_B)).toBeNull();
+ expect(await cache.prune(2000, URL_A)).toBe(false);
+ expect(await cache.lookup(URL_A)).not.toBeNull();
+ });
it('recreates a removed cache directory on a later write and rejects invalid version paths', async () => {
const h = memoryIO();
let directoryExists = false;
diff --git a/apps/mobile/src/device-link/peerFileCache.ts b/apps/mobile/src/device-link/peerFileCache.ts
index 582316d141..c1bf06d50e 100644
--- a/apps/mobile/src/device-link/peerFileCache.ts
+++ b/apps/mobile/src/device-link/peerFileCache.ts
@@ -50,11 +50,11 @@ async function openPeerFileCache(owner: string) {
active: () => boolean,
) {
if (!metadata || !validFileVersion(metadata.version)) return null;
- const hit = await cache.lookup(
- JSON.stringify([device, url]),
- metadata.version,
- );
+ const source = JSON.stringify([device, url]);
+ const hit = await cache.lookup(source, metadata.version);
if (!active() || !hit || hit.size !== metadata.size) return null;
+ // Only the consumer copy needs new space; keep its source during eviction.
+ if (!(await cache.prune(metadata.size, source)) || !active()) return null;
// The consumer owns a separate staging file; cache eviction never removes it.
const directory = new Directory(
Paths.cache,
diff --git a/apps/mobile/src/device-link/peerFileTransport.tsx b/apps/mobile/src/device-link/peerFileTransport.tsx
index 49ae4ddfc9..37ec4e9331 100644
--- a/apps/mobile/src/device-link/peerFileTransport.tsx
+++ b/apps/mobile/src/device-link/peerFileTransport.tsx
@@ -45,6 +45,8 @@ import {
installPeerUploadProbe,
installPeerReset,
recordPeerMedia,
+ peerMediaUri,
+ releasePeerMedia,
clearPeerMedia,
type LocalPeerMedia as LocalMedia,
} from "./peerFileRegistry";
@@ -576,19 +578,27 @@ export function PeerFileTransport() {
return queueRead(
"connection",
async () => {
- if (!active()) throw new Error("FILE_PEER_CANCELLED");
+ const check = (result?: LocalMedia | null) => {
+ if (active() && !signal?.aborted) return;
+ const uri = result && peerMediaUri(result);
+ if (uri) releasePeerMedia(uri);
+ throw new Error("FILE_PEER_CANCELLED");
+ };
+ check();
const cache = await fileCache;
if (metadata?.skipCache) await cache?.invalidate(device, url).catch(() => undefined);
- if (metadata) await cache?.prune(metadata.size).catch(() => false);
const cached = metadata?.skipCache ? null : await cache?.read(device, url, metadata, () => active() && !signal?.aborted).catch(() => null);
- if (!active() || signal?.aborted) throw new Error('FILE_PEER_CANCELLED');
+ check(cached);
if (cached) {
mobileDebugLog('debug', 'files', 'file cache hit', { trace: readTrace, size: cached.size });
return cached;
}
+ if (metadata) await cache?.prune(metadata.size).catch(() => false);
+ check();
const result = await transfer(device, url, signal, readTrace);
if (result && active() && !signal?.aborted)
await cache?.store(device, url, metadata, result).catch(() => undefined);
+ check(result);
return result;
},
signal,
diff --git a/apps/mobile/src/session/remoteMediaDiskCache.ts b/apps/mobile/src/session/remoteMediaDiskCache.ts
index 9e5a2065a1..f7c3cf03f8 100644
--- a/apps/mobile/src/session/remoteMediaDiskCache.ts
+++ b/apps/mobile/src/session/remoteMediaDiskCache.ts
@@ -61,8 +61,8 @@ export interface RemoteMediaDiskCacheHit {
export interface RemoteMediaDiskCache {
/** 命中返回本地 file:// 引用并刷新 LRU 时间;未命中 / 文件丢失返回 null。 */
lookup(sourceUrl: string, version?: string): Promise<RemoteMediaDiskCacheHit | null>;
- /** Frees the oldest copies only when the next operation needs disk space. */
- prune(requiredBytes?: number): Promise<boolean>;
+ /** Frees the oldest copies under disk pressure, excluding the pending copy's source. */
+ prune(requiredBytes?: number, protectedSourceUrl?: string): Promise<boolean>;
remove(sourceUrl: string): Promise<void>;
/**
* 把已取件成功的图片落盘(同 url 并发只下载一次;失败静默,缓存是纯优化)。
@@ -192,15 +192,16 @@ export function createRemoteMediaDiskCache(
}
}
- async function prune(requiredBytes = 0): Promise<boolean> {
+ async function prune(requiredBytes = 0, protectedSourceUrl?: string): Promise<boolean> {
await init();
+ const protectedKey = protectedSourceUrl === undefined ? undefined : cacheKeyOf(protectedSourceUrl);
const enough = () => !options.availableBytes ||
options.availableBytes() >= requiredBytes + (options.reserveBytes ?? 0);
if (enough()) return true;
const oldest = [...entries!.entries()].sort((a, b) => a[1].lastUsedAt - b[1].lastUsedAt);
for (const [key, entry] of oldest) {
if (enough()) break;
- if (downloading.has(key)) continue;
+ if (key === protectedKey || downloading.has(key)) continue;
try {
await io.deleteFile(entry.name);
entries!.delete(key);