Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion frontend/src/types/electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ interface IPCResponse<T = any> {
}

interface ElectronAPI {
// Generic invoke method for direct IPC calls
// Generic invoke method. Daemon-owned channels route through the main-process
// daemon bridge while adapter-only channels stay on direct Electron IPC.
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic IPC bridge that returns different types based on channel
invoke: (channel: string, ...args: unknown[]) => Promise<any>;

Expand Down Expand Up @@ -460,6 +461,8 @@ interface CloudVmState {
// Additional electron interface for IPC event listeners
interface ElectronInterface {
openExternal: (url: string) => Promise<void>;
// Generic invoke method. Daemon-owned channels route through the main-process
// daemon bridge while adapter-only channels stay on direct Electron IPC.
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic IPC bridge that returns different types based on channel
invoke: (channel: string, ...args: unknown[]) => Promise<any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic IPC event callback that receives different argument types
Expand Down
61 changes: 61 additions & 0 deletions main/src/ipc/daemon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, it, vi } from 'vitest';
import { PaneCommandRegistry } from '../daemon/commandRegistry';
import { registerDaemonBridgeHandlers } from './daemon';

interface IpcMainStub {
handlers: Map<string, (_event: unknown, ...args: unknown[]) => Promise<unknown>>;
handle(channel: string, listener: (_event: unknown, ...args: unknown[]) => Promise<unknown>): void;
}

function createIpcMainStub(): IpcMainStub {
const handlers = new Map<string, (_event: unknown, ...args: unknown[]) => Promise<unknown>>();

return {
handlers,
handle(channel, listener) {
handlers.set(channel, listener);
},
};
}

describe('daemon IPC bridge', () => {
it('forwards daemon-owned channels into the shared command registry', async () => {
const registry = new PaneCommandRegistry();
const ipcMain = createIpcMainStub();
const handler = vi.fn(async (sessionId: string) => ({ success: true, data: sessionId }));

registry.register('sessions:get', handler);
registerDaemonBridgeHandlers(ipcMain, registry);

const bridge = ipcMain.handlers.get('daemon:invoke');
expect(bridge).toBeDefined();

await expect(bridge?.({}, 'sessions:get', 'session-1')).resolves.toEqual({
success: true,
data: 'session-1',
});
expect(handler).toHaveBeenCalledWith('session-1');
});

it('rejects adapter-only channels at the bridge boundary', async () => {
const registry = new PaneCommandRegistry();
const ipcMain = createIpcMainStub();

registerDaemonBridgeHandlers(ipcMain, registry);

const bridge = ipcMain.handlers.get('daemon:invoke');
await expect(bridge?.({}, 'sessions:open-ide', 'session-1')).rejects.toThrow(
'Channel "sessions:open-ide" is not daemon-owned',
);
});

it('rejects malformed bridge requests before reaching the registry', async () => {
const registry = new PaneCommandRegistry();
const ipcMain = createIpcMainStub();

registerDaemonBridgeHandlers(ipcMain, registry);

const bridge = ipcMain.handlers.get('daemon:invoke');
await expect(bridge?.({}, 123)).rejects.toThrow('Pane daemon bridge requires a string channel');
});
});
23 changes: 23 additions & 0 deletions main/src/ipc/daemon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isDaemonOwnedChannel } from '../../../shared/types/daemon';
import type { PaneCommandRegistry } from '../daemon/commandRegistry';

interface IpcMainHandleLike {
handle(channel: string, listener: (_event: unknown, ...args: unknown[]) => Promise<unknown>): void;
}

export function registerDaemonBridgeHandlers(
ipcMain: IpcMainHandleLike,
commandRegistry: PaneCommandRegistry,
): void {
ipcMain.handle('daemon:invoke', async (_event, channel: unknown, ...args: unknown[]) => {
if (typeof channel !== 'string') {
throw new Error('Pane daemon bridge requires a string channel');
}

if (!isDaemonOwnedChannel(channel)) {
throw new Error(`Channel "${channel}" is not daemon-owned`);
}

return commandRegistry.invoke(channel, args);
});
}
2 changes: 2 additions & 0 deletions main/src/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { registerCloudHandlers } from './cloud';
import { registerClipboardHandlers } from './clipboard';
import { registerResourceMonitorHandlers } from './resourceMonitor';
import { registerOnboardingHandlers } from './onboarding';
import { registerDaemonBridgeHandlers } from './daemon';
import { PaneCommandRegistry } from '../daemon/commandRegistry';


Expand Down Expand Up @@ -50,6 +51,7 @@ export function registerIpcHandlers(services: AppServices): PaneCommandRegistry
registerClipboardHandlers(ipcMain, services);
registerResourceMonitorHandlers(ipcMain, services, commandRegistry);
registerOnboardingHandlers(ipcMain, services);
registerDaemonBridgeHandlers(ipcMain, commandRegistry);

return commandRegistry;
}
Loading