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): roll back grants after late permission rejection

Dashe0e1890d037a
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/pluginWriteAccess.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/pluginWriteAccess.test.ts
index debdff699c..80e901e4c5 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/pluginWriteAccess.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/pluginWriteAccess.test.ts
@@ -47,6 +47,31 @@ describe('plugin write confirmation interleavings', () => {
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);
+ expect(f.live.setPermissionMode).toHaveBeenLastCalledWith('plan');
+ expect(f.epoch.client.tx).toHaveBeenLastCalledWith('bots.persistSessionPermission', {sessionId:'task', mode:'plan'});
+ });
+ it.each(['database', 'lastRead'] as const)('rolls back runtime and stored permission after late %s revocation', async point => {
+ const f=fixture();
+ const revoke=()=>f.change({permissionMode:'acceptEdits',model:'new'});
+ if(point==='database') f.epoch.client.tx.mockImplementationOnce(async()=>{revoke();return {updated:true};});
+ else f.service.get.mockImplementationOnce(async()=>({taskId:'task',revision:1,permissionMode:'plan'}))
+ .mockImplementationOnce(async()=>({taskId:'task',revision:1,permissionMode:'plan'}))
+ .mockImplementationOnce(async()=>{revoke();return {taskId:'task',revision:1,permissionMode:'auto'};});
+ await expect(f.run('auto')).rejects.toMatchObject({code:'PERMISSION_DENIED'});
+ expect(f.live.setPermissionMode.mock.calls).toEqual([['auto'],['plan']]);
+ expect(f.epoch.client.tx).toHaveBeenLastCalledWith('bots.persistSessionPermission',{sessionId:'task',mode:'plan'});
+ expect(f.config()).toEqual({permissionMode:'acceptEdits',model:'new'});
+ expect(f.write).not.toHaveBeenCalled();expect(f.slots.size).toBe(0);
+ });
+ it('rolls back a persisted grant if the final ownership read fails', async()=>{
+ const f=fixture();
+ f.service.get.mockImplementationOnce(async()=>({taskId:'task',revision:1,permissionMode:'plan'}))
+ .mockImplementationOnce(async()=>({taskId:'task',revision:1,permissionMode:'plan'}))
+ .mockRejectedValueOnce(new Error('owner revoked'));
+ await expect(f.run('auto')).rejects.toThrow('owner revoked');
+ expect(f.live.setPermissionMode).toHaveBeenLastCalledWith('plan');
+ expect(f.epoch.client.tx).toHaveBeenLastCalledWith('bots.persistSessionPermission',{sessionId:'task',mode:'plan'});
+ expect(f.write).not.toHaveBeenCalled();
});
});
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 77e9d3d753..9b8f453089 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -10312,23 +10312,26 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
if (fresh.revision !== task.revision) throw new PluginTaskError('TASK_BUSY', 'Task changed while awaiting permission');
await assertIdle();
const live = maker.getSession(task.taskId);
- if (live) await live.setPermissionMode(mode);
+ let persisted = false;
try {
+ if (live) await live.setPermissionMode(mode);
if (snapshot !== getCurrentDbClientSnapshot()) throw new PluginTaskError('PERMISSION_DENIED', 'Account changed');
const saved = await snapshot!.client.tx('bots.persistSessionPermission', {sessionId: task.taskId, mode});
+ persisted = saved.updated;
if (!saved.updated) throw new PluginTaskError('TASK_NOT_FOUND', 'Task not found');
+ if (snapshot !== getCurrentDbClientSnapshot()) throw new PluginTaskError('PERMISSION_DENIED', 'Account changed');
+ const updatedTask = await service.get(pluginId, task.taskId);
+ 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: updatedTask };
} catch (error) {
if (live) await live.setPermissionMode(task.permissionMode as Parameters<typeof live.setPermissionMode>[0]).catch(() => undefined);
+ if (persisted) await snapshot!.client.tx('bots.persistSessionPermission', {sessionId: task.taskId, mode: task.permissionMode});
throw error;
}
- if (snapshot !== getCurrentDbClientSnapshot()) throw new PluginTaskError('PERMISSION_DENIED', 'Account changed');
- await service.get(pluginId, task.taskId);
- 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); }
}