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): 任务页仅向可用主窗口投递

Dashfc7fd5deea21
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts
index 4e0b455c01..1eab298ad4 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts
@@ -7,7 +7,7 @@
import { describe, expect, it, vi } from 'vitest';
import type { InstalledGhost } from '../../../shared/ghost';
-import { GhostTaskViewSlot, type TaskViewSlotDeps } from '../taskViewSlot';
+import { broadcastTaskViewOpen, GhostTaskViewSlot, type TaskViewSlotDeps } from '../taskViewSlot';
function taskGhost(options: { taskView?: boolean; enabled?: boolean; instanceKey?: string } = {}): InstalledGhost {
return {
@@ -41,6 +41,45 @@ function makeSlot(overrides: Partial<TaskViewSlotDeps> = {}) {
}
describe('taskViewSlot', () => {
+ // PR #6861 discussion_r4353708280:原有 broadcast=false 用例绕过了真实选窗适配器。
+ // 最小失败输入:只有隐藏沙箱,或主窗正在重载;unit,验证回执及实际投递目标。
+ it.each(['absent', 'loading', 'destroyed-contents', 'crashed', 'closed', 'send-failed', 'ready'] as const)(
+ 'only reports delivery when a main renderer can consume it: %s',
+ (state) => {
+ const window = (url: string, main = false) => ({
+ isDestroyed: () => main && state === 'closed',
+ webContents: {
+ getURL: () => url,
+ isDestroyed: () => main && state === 'destroyed-contents',
+ isLoadingMainFrame: () => main && state === 'loading',
+ isCrashed: () => main && state === 'crashed',
+ },
+ });
+ const auxiliary = [
+ window('cindy-ghost://sim/__boot__'),
+ window('file:///app/index.html?secondaryWindow=1#/cc-agent'),
+ window('file:///app/index.html?sidebarWindow=1#/sidebar-window'),
+ window('file:///app/index.html?view=voice-input-overlay'),
+ window('about:blank'),
+ ];
+ const main = window('file:///app/index.html#/cc-agent', true);
+ const windows = state === 'absent' ? auxiliary : [...auxiliary, main];
+ const sent = vi.fn(() => {
+ if (state === 'send-failed') throw new Error('window closed during delivery');
+ });
+ const { slot } = makeSlot({
+ broadcast: (payload) => broadcastTaskViewOpen(
+ windows as Parameters<typeof broadcastTaskViewOpen>[0], payload, sent,
+ ),
+ });
+ expect(slot.handleRequest('sim', {})).toMatchObject(
+ state === 'ready' ? { ok: true, sessionId: 'focused-session' } : { ok: false, errorCode: 'HOST_NOT_READY' },
+ );
+ expect(sent.mock.calls).toEqual(state === 'ready' || state === 'send-failed'
+ ? [[main, { instanceId: 'sim', sessionId: 'focused-session' }]] : []);
+ },
+ );
+
it('opens in the requested task, or the focused task, and reports where it landed', () => {
const { slot, pushes } = makeSlot();
expect(slot.handleRequest('sim', { type: 'task-view-request', sessionId: 'task-1' })).toEqual({ ok: true, sessionId: 'task-1' });
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index e9b75b5e6c..60f5b8b051 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -405,7 +405,7 @@ import {
import { GhostPickSlot } from './pickSlot.js';
import { recordGhostPickedDir, relocateGhostPickedDirs } from './pickGrantsStore.js';
import { GhostPreviewSlot } from './previewSlot.js';
-import { GhostTaskViewSlot } from './taskViewSlot.js';
+import { broadcastTaskViewOpen, GhostTaskViewSlot } from './taskViewSlot.js';
import { GhostScheduleSlot, isMainShellWindowUrl } from './scheduleSlot.js';
import { GhostWorkspaceSlot, type WorkspaceSessionService } from './workspaceSlot.js';
import type { GhostTrustRegistry } from './ghostSignature.js';
@@ -3926,7 +3926,7 @@ export function getGhostPreviewSlot(): GhostPreviewSlot {
let taskViewSlotSingleton: GhostTaskViewSlot | null = null;
-/** 插件任务页开页通道(main → 全窗口 renderer;在指定任务的右侧栏开 / 聚焦任务页页签)。 */
+/** 插件任务页开页通道(main → 主窗口 renderer;在指定任务的右侧栏开 / 聚焦任务页页签)。 */
export const GHOST_TASK_VIEW_OPEN_CHANNEL = 'ghosts:task-view-open';
/** 任务页开页槽单例(taskView):资格审与落点解析在 GhostTaskViewSlot,这里只组装广播。 */
@@ -3935,13 +3935,10 @@ export function getGhostTaskViewSlot(): GhostTaskViewSlot {
taskViewSlotSingleton = new GhostTaskViewSlot({
getGhost: findGhostForInstanceId,
focusedSessionId: () => ghostSessionFocusTracker.current(),
- broadcast: (payload) => {
- const windows = BrowserWindow.getAllWindows().filter((window) => !window.isDestroyed());
- windows.forEach((window) => {
- sendGhostWindowPush(window, GHOST_TASK_VIEW_OPEN_CHANNEL, payload);
- });
- return windows.length > 0;
- },
+ broadcast: (payload) => broadcastTaskViewOpen(
+ BrowserWindow.getAllWindows(), payload,
+ (window, push) => sendGhostWindowPush(window, GHOST_TASK_VIEW_OPEN_CHANNEL, push),
+ ),
log,
});
}
diff --git a/apps/desktop/src/main/cindy-brain/taskViewSlot.ts b/apps/desktop/src/main/cindy-brain/taskViewSlot.ts
index 2cff0162fc..20cc71ac27 100644
--- a/apps/desktop/src/main/cindy-brain/taskViewSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/taskViewSlot.ts
@@ -15,8 +15,11 @@
* 纯逻辑 + 依赖注入:广播在 cindy-brain/index.ts 组装时注入。
*/
+import type { BrowserWindow } from 'electron';
+
import type { GhostPipeTaskViewResult, InstalledGhost } from '../../shared/ghost.js';
import { installedGhostStoragePart } from '../../shared/pluginIdentity.js';
+import { isMainShellWindowUrl } from './scheduleSlot.js';
/** 推给 renderer 的开页载荷(实例 id 由主机按已装清单填,不信自报)。 */
export interface GhostTaskViewOpenPush {
@@ -24,9 +27,32 @@ export interface GhostTaskViewOpenPush {
sessionId: string;
}
+/** Electron 投递适配器;保留在槽旁边以便免启动 Electron 验证窗口生命周期。 */
+export function broadcastTaskViewOpen<T extends Pick<BrowserWindow, 'isDestroyed' | 'webContents'>>(
+ windows: T[],
+ payload: GhostTaskViewOpenPush,
+ send: (window: T, payload: GhostTaskViewOpenPush) => void,
+): boolean {
+ let delivered = false;
+ for (const window of windows) {
+ if (window.isDestroyed()) continue;
+ const contents = window.webContents;
+ if (contents.isDestroyed() || contents.isCrashed() ||
+ contents.isLoadingMainFrame() || !isMainShellWindowUrl(contents.getURL())) continue;
+ // 沙箱、副任务窗、侧栏窗不消费此推送;重载期间也不能报告成功。
+ try {
+ send(window, payload);
+ delivered = true;
+ } catch {
+ // 窗口可能在检查后关闭;继续尝试其它主窗口,无投递则回 HOST_NOT_READY。
+ }
+ }
+ return delivered;
+}
+
export interface TaskViewSlotDeps {
getGhost(id: string): InstalledGhost | null;
- /** 把开页请求推给宿主窗口;false = 一个窗口都不在(HOST_NOT_READY)。 */
+ /** 把开页请求推给主窗口;false = 没有可接收的主 Renderer(HOST_NOT_READY)。 */
broadcast(payload: GhostTaskViewOpenPush): boolean;
/** 台前任务快照(请求缺省 sessionId 时的落点;非任务页为 null)。 */
focusedSessionId(): string | null;