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): reject late Node handoff from stopped callers

Dashdfa1876095c0
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/downloadLifecycle.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/downloadLifecycle.test.ts
index 1256a39ca3..835b0abf3b 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/downloadLifecycle.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/downloadLifecycle.test.ts
@@ -10,10 +10,21 @@ it('uninstall waits for actual Node exit before removing plugin files and cache'
const body = source.slice(start, source.indexOf('\n}', start));
const wait = body.indexOf('await getGhostNodeRuntimeBroker().stopAndWait(id)');
expect(wait).toBeGreaterThan(0);
+ expect(body.indexOf('runtime.stop(id)')).toBeGreaterThan(0);
+ expect(body.indexOf('runtime.stop(id)')).toBeLessThan(wait);
expect(body.indexOf('await manager.uninstall(')).toBeGreaterThan(wait);
expect(body.indexOf('await pluginDownloads.removePlugin(')).toBeGreaterThan(wait);
});
+it('Node receipt handoff rechecks the original logic page identity after acquiring files', () => {
+ const start = source.indexOf("if (type === 'node-request')");
+ const body = source.slice(start, source.indexOf('\n }', start));
+ expect(body).toContain('pluginDownloads.withNodeDownloads(');
+ expect(body).toContain(
+ '!event.sender.isDestroyed() && ghostIdForLogicWebContents(event.sender.id) === id',
+ );
+});
+
it('quit awaits download drain and Node exit before removing captured anonymous roots', async () => {
const start = source.indexOf("onQuit('plugin-downloads'");
const end = source.indexOf("'async');", start) + "'async');".length;
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 dd3dd4b6c5..92d8469b59 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/downloadSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/downloadSlot.test.ts
@@ -1,9 +1,53 @@
-import { it, expect } from 'vitest';
+import { it, expect, vi } from 'vitest';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { PluginDownloadSlot } from '../downloadSlot';
+import { PluginDownloadCache } from '../downloadCache';
import type { InstalledGhost } from '../../../shared/ghost';
+it('a destroyed caller cannot enter Node after a delayed lease, even if the plugin remains enabled', async () => {
+ let finish!: () => void;
+ const barrier = new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ const release = vi.fn();
+ const acquire = vi
+ .spyOn(PluginDownloadCache.prototype, 'acquire')
+ .mockImplementation(async () => {
+ await barrier;
+ return { path: '/fixture/artifact', release };
+ });
+ let active = true;
+ const slot = new PluginDownloadSlot({
+ root: () => '/fixture',
+ scope: () => 'owner',
+ send() {},
+ getGhost: () => ({ enabled: true, approval: {}, manifest: { node: {} } }) as InstalledGhost,
+ download: async () => {
+ throw Error('unused');
+ },
+ });
+ const run = vi.fn(async () => ({ ok: true }));
+ try {
+ const request = slot.withNodeDownloads(
+ 'p',
+ { downloadTokens: { archive: 'receipt' } },
+ run,
+ () => active,
+ );
+ expect(acquire).toHaveBeenCalledOnce();
+ active = false; // runtime.stop destroys the caller before broker.stopAndWait.
+ finish();
+ expect(await request).toMatchObject({ ok: false });
+ expect(run).not.toHaveBeenCalled();
+ expect(release).toHaveBeenCalledOnce();
+ expect(await slot.withNodeDownloads('p', {}, run, () => active)).toMatchObject({ ok: false });
+ expect(run).not.toHaveBeenCalled();
+ } finally {
+ finish();
+ acquire.mockRestore();
+ }
+});
it('shutdown drains cancelled downloads and rejects new downloads and Node handoffs', async () => {
const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'download-shutdown-')));
let started!: () => void, finish!: () => void;
diff --git a/apps/desktop/src/main/cindy-brain/downloadSlot.ts b/apps/desktop/src/main/cindy-brain/downloadSlot.ts
index bf5210f2d2..9a896af804 100644
--- a/apps/desktop/src/main/cindy-brain/downloadSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/downloadSlot.ts
@@ -48,8 +48,10 @@ export class PluginDownloadSlot {
id: string,
payload: Record<string, unknown>,
run: (payload: unknown) => Promise<unknown>,
+ callerActive: () => boolean = () => true,
) {
- if (this.stopped) return { ok: false, errorCode: 'INVALID_REQUEST', message: '下载服务已停止' };
+ if (this.stopped || !callerActive())
+ return { ok: false, errorCode: 'INVALID_REQUEST', message: '下载服务已停止' };
if (payload.downloadTokens === undefined) return run(payload);
const releases: Array<() => void> = [];
try {
@@ -87,6 +89,7 @@ export class PluginDownloadSlot {
}
if (
this.stopped ||
+ !callerActive() ||
this.deps.scope() !== scope ||
!this.deps.getGhost(id)?.enabled ||
JSON.stringify(this.deps.getGhost(id)?.approval) !== approval
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 0b300e1ceb..212a32bdd9 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -7189,7 +7189,10 @@ export function registerGhostIpc(): void {
// node-request 只在 main.js → contextBridge → 主机方向开放。子进程反向
// JSON-RPC 请求恒被 broker 拒绝,因此 Node 不能绕过 main.js 控制 Cindy。
if (type === 'node-request') {
- return pluginDownloads.withNodeDownloads(id, payload as Record<string, unknown>, request => getGhostNodeRuntimeBroker().handleRequest(id, request));
+ // Uninstall destroys this logic page before waiting for the Node broker.
+ // Recheck after asynchronous receipt acquisition so a late request cannot
+ // reopen the stopped broker, even if the same plugin is installed again.
+ return pluginDownloads.withNodeDownloads(id, payload as Record<string, unknown>, request => getGhostNodeRuntimeBroker().handleRequest(id, request), () => !event.sender.isDestroyed() && ghostIdForLogicWebContents(event.sender.id) === id);
}
// pick-request = 系统级选文件夹(pick 槽):用户亲手选中即授权,取消即拒;
// 限速/单发/结果分档在 pickSlot。