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(plugins): reserve retained artifacts during download replacement

Dash7892b12fa54e
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/downloadCache.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/downloadCache.test.ts
index e008dbf114..2bd7bc7469 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/downloadCache.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/downloadCache.test.ts
@@ -4,6 +4,26 @@ import os from 'node:os';
import path from 'node:path';
import { PluginDownloadCache } from '../downloadCache';
+it('charges retained targets plus replacement growth without double charging partial files', async () => {
+ const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'download-replace-')));
+ const dir = path.join(root, 'p', 'a'.repeat(64));
+ const cache = new PluginDownloadCache(65536 + 200);
+ try {
+ await cache.reserve(root, dir, 100);
+ await fs.writeFile(path.join(dir, 'artifact'), Buffer.alloc(100));
+ cache.release(dir);
+ // A cache hit still fits; a miss must reserve the retained target as well.
+ await cache.reserve(root, dir, 100);
+ await cache.reserve(root, dir, 100, true);
+ await fs.writeFile(path.join(dir, 'artifact.part'), Buffer.alloc(60));
+ await cache.reserve(root, dir, 100, true);
+ await expect(cache.reserve(root, dir, 101, true)).rejects.toThrow('full');
+ expect((await fs.stat(path.join(dir, 'artifact'))).size).toBe(100);
+ } finally {
+ await fs.rm(root, { recursive: true, force: true });
+ }
+});
+
it('reserves an 8 GiB question with metadata without allocating its contents', async () => {
const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'download-large-quota-')));
const cache = new PluginDownloadCache();
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/downloadSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/downloadSlot.test.ts
index de52a6ce4b..6292095e1b 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/downloadSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/downloadSlot.test.ts
@@ -7,6 +7,42 @@ vi.mock('../../maker-host/outbound-fetch.js', () => ({ guardedOutboundFetch: gua
import { PluginDownloadSlot } from '../downloadSlot';
import { PluginDownloadCache } from '../downloadCache';
import type { InstalledGhost } from '../../../shared/ghost';
+import { createDownloader } from '../../downloader';
+import { createHash } from 'node:crypto';
+
+it('rechecks replacement quota only after a real downloader cache miss and before network', async () => {
+ const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'download-miss-quota-')));
+ const original = PluginDownloadCache.prototype.reserve;
+ const reserve = vi.spyOn(PluginDownloadCache.prototype, 'reserve').mockImplementation(function (this: PluginDownloadCache, root, dir, bytes, replacing) {
+ if (replacing) throw Error('Download cache is full');
+ return original.call(this, root, dir, bytes, replacing);
+ });
+ const download = createDownloader();
+ let target = '';
+ guarded.fetch.mockReset();
+ const slot = new PluginDownloadSlot({
+ root: id => path.join(root, id), scope: () => 'owner', send() {},
+ getGhost: () => ({ enabled: true, approval: {}, manifest: { node: {}, network: { hosts: ['github.com'] } } }) as unknown as InstalledGhost,
+ download: async options => {
+ if (!target) await fs.writeFile(options.targetPath, 'ok');
+ target = options.targetPath;
+ return download(options);
+ },
+ });
+ try {
+ const request = { kind: 'start', id: 'same', url: 'https://github.com/file', bytes: 2, sha256: createHash('sha256').update('ok').digest('hex') };
+ expect(await slot.handle('p', request)).toMatchObject({ ok: true });
+ expect(reserve.mock.calls.some(x => x[3])).toBe(false);
+ await fs.writeFile(target, 'no');
+ expect(await slot.handle('p', request)).toMatchObject({ ok: false });
+ expect(reserve.mock.calls.filter(x => x[3])).toHaveLength(1);
+ expect(guarded.fetch).not.toHaveBeenCalled();
+ expect(await fs.readFile(target, 'utf8')).toBe('no');
+ await expect(fs.stat(target + '.part')).rejects.toMatchObject({ code: 'ENOENT' });
+ } finally {
+ reserve.mockRestore(); await slot.stopAndWait(); await fs.rm(root, { recursive: true, force: true });
+ }
+});
it('drains a destroyed page before a new page resumes the same download id', async () => {
const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'download-page-')));
let oldPage = true, calls = 0, started!: () => void, release!: () => void;
diff --git a/apps/desktop/src/main/cindy-brain/downloadCache.ts b/apps/desktop/src/main/cindy-brain/downloadCache.ts
index 4216b4d7ca..0224656d80 100644
--- a/apps/desktop/src/main/cindy-brain/downloadCache.ts
+++ b/apps/desktop/src/main/cindy-brain/downloadCache.ts
@@ -32,11 +32,12 @@ export class PluginDownloadCache {
for (const [token, receipt] of this.receipts)
if (receipt.dir === dir) this.receipts.delete(token);
}
- async reserve(root: string, dir: string, bytes: number): Promise<void> {
+ async reserve(root: string, dir: string, bytes: number, replacing = false): Promise<void> {
return this.locked(async () => {
await fs.mkdir(root, { recursive: true });
if ((await fs.realpath(root)) !== path.resolve(root)) throw Error('Unsafe cache root');
const entries: Array<{ dir: string; bytes: number; time: number }> = [];
+ let replacementBytes = bytes + 65536;
for (const plugin of await fs.readdir(root, { withFileTypes: true })) {
if (!plugin.isDirectory()) throw Error('Unsafe download cache');
const pluginRoot = path.join(root, plugin.name);
@@ -45,13 +46,17 @@ export class PluginDownloadCache {
throw Error('Unsafe download cache');
const entryDir = path.join(pluginRoot, artifact.name);
let size = 0,
- time = 0;
+ time = 0,
+ partialSize = 0;
for (const name of await fs.readdir(entryDir)) {
const stat = await fs.lstat(path.join(entryDir, name));
if (!stat.isFile()) throw Error('Unsafe download cache');
size += stat.size;
+ if (name === 'artifact.part') partialSize = stat.size;
time = Math.max(time, stat.mtimeMs);
}
+ if (entryDir === dir)
+ replacementBytes = size - partialSize + Math.max(bytes, partialSize) + 65536;
// Charge directory/receipt overhead too: empty failed attempts are not free forever.
entries.push({
dir: entryDir,
@@ -61,7 +66,10 @@ export class PluginDownloadCache {
}
}
// Reserve before dispatch: concurrent queued downloads cannot oversubscribe disk.
- const planned = Math.max(bytes + 65536, entries.find((e) => e.dir === dir)?.bytes ?? 0);
+ const planned = Math.max(
+ replacing ? replacementBytes : bytes + 65536,
+ entries.find((e) => e.dir === dir)?.bytes ?? 0,
+ );
let total = entries.filter((e) => e.dir !== dir).reduce((sum, e) => sum + e.bytes, planned);
for (const entry of entries.sort((a, b) => a.time - b.time)) {
if (total <= this.limit) break;
diff --git a/apps/desktop/src/main/cindy-brain/downloadSlot.ts b/apps/desktop/src/main/cindy-brain/downloadSlot.ts
index b9ec9c9699..55d9f2e7f1 100644
--- a/apps/desktop/src/main/cindy-brain/downloadSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/downloadSlot.ts
@@ -238,13 +238,22 @@ export class PluginDownloadSlot {
}, 250);
// Plugin-supplied URLs: one SSRF-guarded hop at a time (DNS pinning, HTTPS only,
// no ambient cookies), re-checking the owner before every connection.
- const request: DownloadRequest = (url, init) =>
- guardedOutboundFetch(url, { ...init, credentials: 'omit' }, () => {
+ const request: DownloadRequest = async (url, init) => {
+ controller.signal.throwIfAborted();
+ // Cache hits never reach this callback. On a miss, the old target
+ // remains beside the growing .part until the downloader publishes it.
+ try {
+ await this.cache.reserve(path.dirname(root), dir, p.bytes as number, true);
+ } catch {
+ throw new DownloadError('SIZE', 'Download cache cannot reserve replacement space');
+ }
+ return guardedOutboundFetch(url, { ...init, credentials: 'omit' }, () => {
controller.signal.throwIfAborted();
// A revoked owner is final: do not let the downloader retry this hop.
if (!isUrlAllowed(url))
throw new DownloadError('URL_POLICY', 'Download URL is not allowed');
});
+ };
try {
const result = await this.deps.download({
url: p.url as string,