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(desktop): enforce learn activation remotely

xd-bobo373a52508009
diff --git a/apps/desktop/src/main/learn-host/__tests__/registerIpc.test.ts b/apps/desktop/src/main/learn-host/__tests__/registerIpc.test.ts
new file mode 100644
index 0000000000..066a18917f
--- /dev/null
+++ b/apps/desktop/src/main/learn-host/__tests__/registerIpc.test.ts
@@ -0,0 +1,67 @@
+import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const h = vi.hoisted(() => ({
+ enabled: true,
+ handlers: new Map<string, (event: unknown, ...args: unknown[]) => unknown>(),
+ startLearn: vi.fn(async () => ({ runId: 'learn-run-1' })),
+}));
+
+vi.mock('electron', () => ({
+ BrowserWindow: { getAllWindows: () => [] },
+ ipcMain: {
+ handle: (channel: string, handler: (event: unknown, ...args: unknown[]) => unknown) => {
+ h.handlers.set(channel, handler);
+ },
+ },
+}));
+
+vi.mock('../../logger', () => ({
+ createLogger: () => ({ warn: vi.fn() }),
+}));
+
+vi.mock('../../device-link/broadcast-tap', () => ({
+ tapWindowBroadcast: vi.fn(),
+}));
+
+vi.mock('../../skillhub/activationPreferences', () => ({
+ isCindyLearnSkillEnabled: () => h.enabled,
+}));
+
+vi.mock('../index', () => ({
+ getLearnController: () => ({ startLearn: h.startLearn }),
+}));
+
+import { LEARN_CHANNELS, registerLearnIpc } from '../registerIpc';
+
+beforeAll(() => {
+ registerLearnIpc();
+});
+
+beforeEach(() => {
+ h.enabled = true;
+ h.startLearn.mockClear();
+});
+
+function startLearn(input: Record<string, unknown>): Promise<unknown> {
+ const handler = h.handlers.get(LEARN_CHANNELS.START);
+ if (!handler) throw new Error('learn:start handler was not registered');
+ return Promise.resolve(handler({}, input));
+}
+
+describe('learn:start IPC activation gate', () => {
+ it('rejects local and Device Link calls after Learn is disabled on this host', async () => {
+ h.enabled = false;
+
+ await expect(startLearn({ input: '', sourceKind: 'session' })).rejects.toThrow(
+ /\[PERMISSION_DENIED\].*disabled in Local Skills/,
+ );
+ expect(h.startLearn).not.toHaveBeenCalled();
+ });
+
+ it('delegates to the Learn controller while the built-in Skill is enabled', async () => {
+ const request = { input: '', sourceKind: 'session' };
+
+ await expect(startLearn(request)).resolves.toEqual({ runId: 'learn-run-1' });
+ expect(h.startLearn).toHaveBeenCalledWith(request);
+ });
+});
diff --git a/apps/desktop/src/main/learn-host/registerIpc.ts b/apps/desktop/src/main/learn-host/registerIpc.ts
index 4d96ded3a9..a25b39c582 100644
--- a/apps/desktop/src/main/learn-host/registerIpc.ts
+++ b/apps/desktop/src/main/learn-host/registerIpc.ts
@@ -11,6 +11,7 @@ import { BrowserWindow, ipcMain } from 'electron';
import { createLogger } from '../logger';
import { tapWindowBroadcast } from '../device-link/broadcast-tap';
import { throwIpcError } from '../utils/ipcValidate';
+import { isCindyLearnSkillEnabled } from '../skillhub/activationPreferences';
import type { IpcErrorCode } from '../../shared/ipc-errors';
import type {
LearnEventPayload,
@@ -74,6 +75,9 @@ export function registerLearnIpc(): void {
if (_learnIpcRegistered) return;
_learnIpcRegistered = true;
ipcMain.handle(LEARN_CHANNELS.START, async (_event, req: LearnStartRequest) => {
+ if (!isCindyLearnSkillEnabled()) {
+ throwIpcError('PERMISSION_DENIED', 'Cindy Learn is disabled in Local Skills.');
+ }
try {
return await mustController().startLearn(req);
} catch (err) {