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): 限定分离窗口能力作用域

DavidShen52b8d09370e5
diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.ts
index dbc7b5538b..f862606b1b 100644
--- a/apps/desktop/src/main/bootstrap-electron.ts
+++ b/apps/desktop/src/main/bootstrap-electron.ts
@@ -1361,7 +1361,7 @@ function resolveIOSSimulatorRendererWindow(
): BrowserWindow | null {
const owner = BrowserWindow.fromWebContents(target);
if (!owner || !isTrustedAppRendererWindow(owner)) return null;
- const sidebarTarget = rsbWindowController.getSidebarWebContents();
+ const sidebarTarget = rsbWindowController.getVisibleSidebarWebContents();
const isSidebar = sidebarTarget === target;
if (!isSidebar && !isMainShellWindowUrl(owner.webContents.getURL())) return null;
return owner;
@@ -1373,7 +1373,7 @@ configureIOSSimulatorRendererTargets((preferredTarget) => {
mainWindowRef && !mainWindowRef.isDestroyed() && !mainWindowRef.webContents.isDestroyed()
? mainWindowRef.webContents
: null;
- const sidebarTarget = rsbWindowController.getSidebarWebContents();
+ const sidebarTarget = rsbWindowController.getVisibleSidebarWebContents();
const belongsToMainFamily =
!preferredTarget || preferredTarget === mainTarget || preferredTarget === sidebarTarget;
if (!belongsToMainFamily) {
diff --git a/apps/desktop/src/main/ghost-panel-window/__tests__/preload-contract.test.ts b/apps/desktop/src/main/ghost-panel-window/__tests__/preload-contract.test.ts
index 2b836405d9..c9b5bd509b 100644
--- a/apps/desktop/src/main/ghost-panel-window/__tests__/preload-contract.test.ts
+++ b/apps/desktop/src/main/ghost-panel-window/__tests__/preload-contract.test.ts
@@ -87,8 +87,19 @@ describe('ghost panel preload contract', () => {
it('guards plugin mutations to the current detached panel id', () => {
expect(source).toContain("new URLSearchParams(window.location.search).get('ghostPanelWindow')");
expect(source).toContain('mutationErrorForGhostPanel(id)');
+ expect(source).toContain("return ipcRenderer.invoke('maker:ghost-panel-window:open', ghostId);");
+ expect(source).toContain("return ipcRenderer.invoke('maker:ghost-panel-window:set-detached', ghostId, detached);");
expect(source).toContain("ipcRenderer.invoke('ghosts:reload', id)");
expect(source).toContain("ipcRenderer.invoke('ghosts:set-enabled', id, enabled)");
+ expect(source).toContain("return ipcRenderer.invoke('ghosts:clear-unread', id, seenAt);");
+ });
+
+ it('scopes unread snapshots and badge events to the current detached panel', () => {
+ expect(source).toContain('onCurrentGhostBadge');
+ expect(source).toContain('onCurrentGhostUnreadSnapshot');
+ expect(source).toContain('if (ghostId !== currentGhostPanelId || typeof at !== \'number\') continue;');
+ expect(source).toContain('if (candidate.ghostId !== currentGhostPanelId) return;');
+ expect(source).toContain('if (candidate.ghostId !== currentGhostPanelId || typeof candidate.at !== \'number\') continue;');
});
it('uses shared lifecycle channel constants', () => {
diff --git a/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts b/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
index 603cf5befb..85dbbee36e 100644
--- a/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
@@ -670,6 +670,24 @@ describe('getHostWebContents', () => {
});
});
+describe('getVisibleSidebarWebContents', () => {
+ it('returns null for a cached hidden window while keeping the IPC sender available', () => {
+ const h = makeHarness({ detached: true });
+ h.controller.prewarm();
+ const win = h.windows[0];
+ markReady(h.controller, win);
+
+ expect(h.controller.getSidebarWebContents()).toBe(win.webContents);
+ expect(h.controller.getVisibleSidebarWebContents()).toBeNull();
+
+ h.controller.open({ userInitiated: false });
+ expect(h.controller.getVisibleSidebarWebContents()).toBe(win.webContents);
+
+ h.controller.close();
+ expect(h.controller.getVisibleSidebarWebContents()).toBeNull();
+ });
+});
+
// ═══════════════════════════════════════════════════════════════════════
// setContext / routeCommand
// ═══════════════════════════════════════════════════════════════════════
diff --git a/apps/desktop/src/main/right-sidebar-window/controller.ts b/apps/desktop/src/main/right-sidebar-window/controller.ts
index 137c64c03c..066aa6dc82 100644
--- a/apps/desktop/src/main/right-sidebar-window/controller.ts
+++ b/apps/desktop/src/main/right-sidebar-window/controller.ts
@@ -418,6 +418,19 @@ export class RsbWindowController {
: null;
}
+ /**
+ * Return the detached sidebar renderer only while it is visible.
+ *
+ * The cached hidden renderer remains a valid IPC sender (for lifecycle
+ * handshakes and preload event guards), but it must not stay in capability
+ * target lists while the user cannot see or interact with it.
+ */
+ getVisibleSidebarWebContents(): WebContents | null {
+ return this.visible && this.winRef && !this.winRef.isDestroyed()
+ ? this.winRef.webContents
+ : null;
+ }
+
/** 窗口存在 + 可见(隐藏复用模型中隐藏不算 open)。 */
isOpen(): boolean {
return (
diff --git a/apps/desktop/src/preload/ghostPanelWindowPreload.ts b/apps/desktop/src/preload/ghostPanelWindowPreload.ts
index d9610bbc79..e25154f6b2 100644
--- a/apps/desktop/src/preload/ghostPanelWindowPreload.ts
+++ b/apps/desktop/src/preload/ghostPanelWindowPreload.ts
@@ -76,8 +76,43 @@ function fanOut(channel: string): (cb: (payload: unknown) => void) => Unsub {
const fanOutGhostsChanged = fanOut('ghosts:changed');
const fanOutGhostRuntimeChanged = fanOut('ghosts:runtime-changed');
const fanOutGhostPreviewMedia = fanOut('ghosts:preview-media');
-const fanOutGhostBadge = fanOut('ghosts:badge');
-const fanOutGhostUnreadSnapshot = fanOut('ghosts:unread-snapshot');
+
+function onCurrentGhostBadge(
+ cb: (payload: { ghostId: string; unread: boolean; summary?: string; at?: number }) => void,
+): Unsub {
+ const listener = (_event: Electron.IpcRendererEvent, payload: unknown): void => {
+ if (currentGhostPanelId === null || typeof payload !== 'object' || payload === null) return;
+ const candidate = payload as { ghostId?: unknown };
+ if (candidate.ghostId !== currentGhostPanelId) return;
+ cb(payload as { ghostId: string; unread: boolean; summary?: string; at?: number });
+ };
+ ipcRenderer.on('ghosts:badge', listener);
+ return () => ipcRenderer.removeListener('ghosts:badge', listener);
+}
+
+function onCurrentGhostUnreadSnapshot(
+ cb: (payload: { entries: { ghostId: string; summary?: string; at: number }[] }) => void,
+): Unsub {
+ const listener = (_event: Electron.IpcRendererEvent, payload: unknown): void => {
+ if (currentGhostPanelId === null || typeof payload !== 'object' || payload === null) return;
+ const entries = (payload as { entries?: unknown }).entries;
+ if (!Array.isArray(entries)) return;
+ const scoped: { ghostId: string; summary?: string; at: number }[] = [];
+ for (const entry of entries) {
+ if (typeof entry !== 'object' || entry === null) continue;
+ const candidate = entry as { ghostId?: unknown; at?: unknown; summary?: unknown };
+ if (candidate.ghostId !== currentGhostPanelId || typeof candidate.at !== 'number') continue;
+ scoped.push({
+ ghostId: currentGhostPanelId,
+ at: candidate.at,
+ ...(typeof candidate.summary === 'string' ? { summary: candidate.summary } : {}),
+ });
+ }
+ cb({ entries: scoped });
+ };
+ ipcRenderer.on('ghosts:unread-snapshot', listener);
+ return () => ipcRenderer.removeListener('ghosts:unread-snapshot', listener);
+}
const appearanceSettings = ipcRenderer.sendSync(
'appearance-settings:get-sync',
@@ -163,13 +198,19 @@ contextBridge.exposeInMainWorld('electronAPI', {
ipcRenderer.sendSync('ghost-panel-window:get-state-sync') as GhostPanelWindowsState,
getState: (): Promise<GhostPanelWindowsState> =>
ipcRenderer.invoke('maker:ghost-panel-window:get-state'),
- open: (ghostId: string): Promise<void> =>
- ipcRenderer.invoke('maker:ghost-panel-window:open', ghostId),
+ open: (ghostId: string): Promise<void> => {
+ const error = mutationErrorForGhostPanel(ghostId);
+ if (error) return Promise.reject(error);
+ return ipcRenderer.invoke('maker:ghost-panel-window:open', ghostId);
+ },
setDetached: (
ghostId: string,
detached: boolean,
- ): Promise<GhostPanelWindowsState> =>
- ipcRenderer.invoke('maker:ghost-panel-window:set-detached', ghostId, detached),
+ ): Promise<GhostPanelWindowsState> => {
+ const error = mutationErrorForGhostPanel(ghostId);
+ if (error) return Promise.reject(error);
+ return ipcRenderer.invoke('maker:ghost-panel-window:set-detached', ghostId, detached);
+ },
onStateChanged: (cb: (state: GhostPanelWindowsState) => void): Unsub =>
onPayload('maker:push:ghost-panel-window:state-changed', cb),
rendererReady: (): Promise<void> =>
@@ -223,6 +264,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
onPreviewMedia: fanOutGhostPreviewMedia,
/** 未读角标快照(ghostUnreadStore 首帧)。 */
unreadSync: (): { entries: { ghostId: string; summary?: string; at: number }[] } => {
+ if (currentGhostPanelId === null) return { entries: [] };
try {
const result = ipcRenderer.sendSync('ghosts:unread') as { entries?: unknown } | null;
if (!Array.isArray(result?.entries)) return { entries: [] };
@@ -230,7 +272,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
for (const raw of result.entries) {
if (typeof raw !== 'object' || raw === null) continue;
const { ghostId, summary, at } = raw as Record<string, unknown>;
- if (typeof ghostId !== 'string' || typeof at !== 'number') continue;
+ if (ghostId !== currentGhostPanelId || typeof at !== 'number') continue;
entries.push({ ghostId, ...(typeof summary === 'string' ? { summary } : {}), at });
}
return { entries };
@@ -239,11 +281,14 @@ contextBridge.exposeInMainWorld('electronAPI', {
}
},
/** 熄灭未读(打开面板 = 已读)。 */
- clearUnread: (id: string, seenAt?: number): Promise<{ ok: boolean }> =>
- ipcRenderer.invoke('ghosts:clear-unread', id, seenAt),
+ clearUnread: (id: string, seenAt?: number): Promise<{ ok: boolean }> => {
+ const error = mutationErrorForGhostPanel(id);
+ if (error) return Promise.reject(error);
+ return ipcRenderer.invoke('ghosts:clear-unread', id, seenAt);
+ },
/** 未读角标变化。 */
- onBadge: fanOutGhostBadge,
+ onBadge: onCurrentGhostBadge,
/** 未读快照变化(ghostUnreadStore 增量更新)。 */
- onUnreadSnapshot: fanOutGhostUnreadSnapshot,
+ onUnreadSnapshot: onCurrentGhostUnreadSnapshot,
},
});