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): serialize permission prompts and preserve live settings

Dash58bd7cee5458
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/pluginWriteAccess.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/pluginWriteAccess.test.ts
new file mode 100644
index 0000000000..50144211f4
--- /dev/null
+++ b/apps/desktop/src/main/maker-ipc/__tests__/pluginWriteAccess.test.ts
@@ -0,0 +1,46 @@
+import { readFileSync } from 'node:fs';
+import { describe, expect, it, vi } from 'vitest';
+import ts from 'typescript';
+import { PluginTaskError } from '../pluginTaskService.js';
+
+// Execute the real switch branch with controlled Host boundaries.
+const source = readFileSync(new URL('../register.ts', import.meta.url), 'utf8');
+const branch = source.slice(source.indexOf(" case 'requestWriteAccess': {"), source.indexOf(" case 'startTeam': {"));
+const js = ts.transpileModule(`return async function(pluginId, request) { switch(request.kind) { ${branch} } }`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
+function fixture() {
+ let cfg: Record<string, unknown> = { permissionMode: 'plan', model: 'old' };
+ const epoch = { client: { tx: vi.fn(async () => ({ updated: true })) } };
+ const task = { taskId: 'task', revision: 1, permissionMode: 'plan' };
+ const service = { get: vi.fn(async () => task), listRuns: vi.fn(async () => ({ items: [] })) };
+ const live = { isTurnRunning: () => false, getTurnControlSnapshot: () => ({ pendingInteractionCount: 0 }), setPermissionMode: vi.fn(async () => {}) };
+ const slots = new Set<string>();
+ const dialog = { showMessageBox: vi.fn(async () => ({ response: 0 })) };
+ const write = vi.fn((_id: string, value: Record<string, unknown>) => { cfg = value; });
+ const deps = { service, getCurrentDbClientSnapshot: () => epoch, readGhostErrandConfig: () => cfg, pluginPermissionRequests: slots, PluginTaskError, maker: { getSession: () => live }, inputCoordinator: { getQueueControlSnapshot: () => ({ pendingQueue: [] }) }, dialog, t: (x: string) => x, getInstalledGhostName: () => 'fixture', clampErrandPermissionMode: (x: string) => x, writeGhostErrandConfig: write, broadcastSessionPatched: vi.fn() };
+ const run = new Function(...Object.keys(deps), js)(...Object.values(deps));
+ return { run: (mode = 'acceptEdits') => run('plugin', { kind: 'requestWriteAccess', taskId: 'task', mode }), service, live, epoch, dialog, slots, write, config: () => cfg, change: (next: Record<string, unknown>) => { cfg = next; } };
+}
+describe('plugin write confirmation interleavings', () => {
+ it('owns the existing slot across idle checks and releases it on failure', async () => {
+ const f = fixture(); let reject!: (e: Error) => void;
+ f.service.listRuns.mockImplementationOnce(() => new Promise((_, r) => { reject = r; }));
+ const first = f.run(); const rejected = expect(first).rejects.toThrow('idle failed');
+ await vi.waitFor(() => expect(f.slots.size).toBe(1));
+ await expect(f.run()).rejects.toMatchObject({ code: 'TASK_BUSY' });
+ expect(f.dialog.showMessageBox).not.toHaveBeenCalled(); expect(f.slots.size).toBe(1);
+ reject(new Error('idle failed')); await rejected; expect(f.slots.size).toBe(0);
+ await expect(f.run()).resolves.toMatchObject({ granted: true });
+ });
+ it.each(['runtime', 'database', 'lastRead'])('preserves unrelated configuration changed during %s', async point => {
+ const f = fixture(), change = () => f.change({ permissionMode: 'plan', model: 'new', workingDir: '/chosen', fastMode: true });
+ if (point === 'runtime') f.live.setPermissionMode.mockImplementationOnce(async () => { change(); });
+ if (point === 'database') f.epoch.client.tx.mockImplementationOnce(async () => { change(); return { updated: true }; });
+ if (point === 'lastRead') f.service.get.mockImplementationOnce(async () => ({ taskId: 'task', revision: 1, permissionMode: 'plan' })).mockImplementationOnce(async () => ({ taskId: 'task', revision: 1, permissionMode: 'plan' })).mockImplementationOnce(async () => { change(); return { taskId: 'task', revision: 1, permissionMode: 'plan' }; });
+ await f.run('auto'); expect(f.config()).toEqual({ permissionMode: 'auto', model: 'new', workingDir: '/chosen', fastMode: true });
+ });
+ it('does not overwrite a later permission change', async () => {
+ const f = fixture(); f.live.setPermissionMode.mockImplementationOnce(async () => { f.change({ permissionMode: 'acceptEdits', model: 'new' }); });
+ await expect(f.run('auto')).rejects.toMatchObject({ code: 'PERMISSION_DENIED' });
+ expect(f.write).not.toHaveBeenCalled(); expect(f.slots.size).toBe(0);
+ });
+});
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index bbf629ae3a..00d91dc37a 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -10283,9 +10283,9 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
const live = maker.getSession(task.taskId);
if (live?.isTurnRunning() || live?.getTurnControlSnapshot().pendingInteractionCount || inputCoordinator.getQueueControlSnapshot(task.taskId).pendingQueue.length) throw new PluginTaskError('TASK_BUSY', 'Wait for the current turn before changing permission');
};
- await assertIdle();
pluginPermissionRequests.add(pluginId);
try {
+ await assertIdle();
const result = await dialog.showMessageBox({
type: 'question', title: t(mode === 'auto' ? 'pluginTaskWriteAccess.autoTitle' : 'pluginTaskWriteAccess.title'),
message: t(mode === 'auto' ? 'pluginTaskWriteAccess.autoMessage' : 'pluginTaskWriteAccess.message').replace('{{name}}', getInstalledGhostName(pluginId) ?? pluginId),
@@ -10309,7 +10309,10 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
}
if (snapshot !== getCurrentDbClientSnapshot()) throw new PluginTaskError('PERMISSION_DENIED', 'Account changed');
await service.get(pluginId, task.taskId);
- if (mode === 'auto' || clampErrandPermissionMode(cfg.permissionMode) === 'plan') writeGhostErrandConfig(pluginId, {...cfg, permissionMode: mode});
+ if (snapshot !== getCurrentDbClientSnapshot()) throw new PluginTaskError('PERMISSION_DENIED', 'Account changed');
+ const currentConfig = readGhostErrandConfig(pluginId);
+ if (currentConfig.permissionMode !== cfg.permissionMode) throw new PluginTaskError('PERMISSION_DENIED', 'Permission settings changed');
+ if (mode === 'auto' || clampErrandPermissionMode(currentConfig.permissionMode) === 'plan') writeGhostErrandConfig(pluginId, {...currentConfig, permissionMode: mode});
broadcastSessionPatched(task.taskId, {permissionMode: mode});
return { granted: true, task: await service.get(pluginId, task.taskId) };
} finally { pluginPermissionRequests.delete(pluginId); }