Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(plugins): 校验跳转手势并保留跨任务页签选择
Dash86023cf32983
diff --git a/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.md b/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.mdindex 5ab30020fa..b98a51efc8 100644--- a/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.md+++ b/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.md@@ -35,6 +35,8 @@- 用户点击主视图里的任务跳转按钮时,可调用 `cindy.taskView({ sessionId, focus: true })`:打开该任务的插件页签,并通过宿主既有任务导航切到该任务;已分离的侧栏也按主动打开处理。缺省或 `focus: false` 仍只开页签、不抢前台,后台工具调用不应传 `focus: true`。+ Host 须观察到该插件实例最近 3 秒内的真实鼠标/键盘点击,且一次手势只能消费一次+ (与派活共用);无手势、已过期或已消费均回 `PERMISSION_DENIED`,不开页也不导航。成功回执额外带 `focusRequested: true`,表示导航请求已投递,不表示目标任务一定存在;不存在的任务沿用任务导航的错误处理。旧宿主可能忽略 `focus`,缺该回执字段时应提示更新,不能声称已跳转。`peek: true` 始终只查询,即使同时传 `focus: true` 也不导航。diff --git a/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.tsindex 4bd8b97214..48bfed37a0 100644--- a/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts+++ b/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts@@ -8,6 +8,7 @@ import { describe, expect, it, vi } from 'vitest';import type { InstalledGhost } from '../../../shared/ghost';import { broadcastTaskViewOpen, GhostTaskViewSlot, type TaskViewSlotDeps } from '../taskViewSlot';+import { GhostErrandSlot } from '../errandSlot';function taskGhost(options: { taskView?: boolean; enabled?: boolean; instanceKey?: string } = {}): InstalledGhost {return {@@ -35,12 +36,41 @@ function makeSlot(overrides: Partial<TaskViewSlotDeps> = {}) {return true;}),focusedSessionId: () => 'focused-session',+ consumeUserGesture: () => true,...overrides,};return { slot: new GhostTaskViewSlot(deps), pushes };}describe('taskViewSlot', () => {+ // PR #6939 discussion_r4357789335:focus 不能把插件自报布尔值当作真实点击。+ // unit:复用派活手势账本,最小输入为无手势/过期/其他实例/已消费票据。+ it('requires a fresh, one-shot host gesture for the requesting plugin instance', () => {+ let now = 0;+ const gestures = new GhostErrandSlot({ getGhost: () => taskGhost(), now: () => now });+ const consumeUserGesture = vi.fn((id: string) => gestures.consumeUserGesture(id));+ const { slot, pushes } = makeSlot({ consumeUserGesture });+ const reveal = vi.fn();+ slot.setRevealSession(reveal);+ const denied = { ok: false, errorCode: 'PERMISSION_DENIED' };+ expect(slot.handleRequest('sim', { focus: true })).toMatchObject(denied);+ gestures.noteUserGesture('other');+ expect(slot.handleRequest('sim', { focus: true })).toMatchObject(denied);+ gestures.noteUserGesture('sim');+ now = 3_000;+ expect(slot.handleRequest('sim', { focus: true })).toMatchObject(denied);+ expect(pushes).toEqual([]);+ expect(reveal).not.toHaveBeenCalled();+ gestures.noteUserGesture('sim');+ const consumed = consumeUserGesture.mock.calls.length;+ slot.handleRequest('sim', { peek: true, focus: true });+ slot.handleRequest('sim', { focus: false });+ expect(consumeUserGesture).toHaveBeenCalledTimes(consumed);+ expect(slot.handleRequest('sim', { focus: true })).toMatchObject({ ok: true, focusRequested: true });+ expect(slot.handleRequest('sim', { focus: true })).toMatchObject(denied);+ expect(reveal).toHaveBeenCalledExactlyOnceWith('focused-session');+ });+// 主视图的显式跳转须同时导航任务与打开页签;原测试只覆盖后台开页。// 最小输入:focus:true;unit,缺省/peek 不抢前台,投递失败不导航。it('focus explicitly reveals the same task, while old requests and peek remain passive', () => {diff --git a/apps/desktop/src/main/cindy-brain/errandSlot.ts b/apps/desktop/src/main/cindy-brain/errandSlot.tsindex 352253219e..d4af689872 100644--- a/apps/desktop/src/main/cindy-brain/errandSlot.ts+++ b/apps/desktop/src/main/cindy-brain/errandSlot.ts@@ -459,7 +459,8 @@ export class GhostErrandSlot {return 'background';}- private consumeUserGesture(ghostId: string): boolean {+ /** Host 槽共用一次性手势;消费后不能再用于派活或任务页跳转。 */+ consumeUserGesture(ghostId: string): boolean {const expiresAt = this.userGestures.get(ghostId);if (expiresAt === undefined) return false;this.userGestures.delete(ghostId);diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.tsindex 89670db457..355d547f54 100644--- a/apps/desktop/src/main/cindy-brain/index.ts+++ b/apps/desktop/src/main/cindy-brain/index.ts@@ -3938,6 +3938,7 @@ export function getGhostTaskViewSlot(): GhostTaskViewSlot {taskViewSlotSingleton = new GhostTaskViewSlot({getGhost: findGhostForInstanceId,focusedSessionId: () => ghostSessionFocusTracker.current(),+ consumeUserGesture: (id) => getGhostErrandSlot().consumeUserGesture(id),broadcast: (payload) => broadcastTaskViewOpen(BrowserWindow.getAllWindows(), payload,(window, push) => sendGhostWindowPush(window, GHOST_TASK_VIEW_OPEN_CHANNEL, push),diff --git a/apps/desktop/src/main/cindy-brain/taskViewSlot.ts b/apps/desktop/src/main/cindy-brain/taskViewSlot.tsindex 84aa5e9565..3cd72ee71b 100644--- a/apps/desktop/src/main/cindy-brain/taskViewSlot.ts+++ b/apps/desktop/src/main/cindy-brain/taskViewSlot.ts@@ -57,6 +57,8 @@ export interface TaskViewSlotDeps {broadcast(payload: GhostTaskViewOpenPush): boolean;/** 台前任务快照(请求缺省 sessionId 时的落点;非任务页为 null)。 */focusedSessionId(): string | null;+ /** 消费 Host 观察到的该实例真实点击,不信任插件自报 focus。 */+ consumeUserGesture(ghostId: string): boolean;log?: { info: (msg: string, meta?: Record<string, unknown>) => void };}@@ -104,6 +106,8 @@ export class GhostTaskViewSlot {if (request.peek === true) return { ok: true, sessionId };if (request.focus === true && !this.revealSession)return fail('HOST_NOT_READY', '任务导航尚未就绪');+ if (request.focus === true && !this.deps.consumeUserGesture(ghostId))+ return fail('PERMISSION_DENIED', '切换任务需要用户当次点击');const delivered = this.deps.broadcast({instanceId: installedGhostStoragePart(ghost),sessionId,diff --git a/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openPluginTaskTab.test.ts b/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openPluginTaskTab.test.tsindex 7f6589683d..c2b60cd4c9 100644--- a/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openPluginTaskTab.test.ts+++ b/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openPluginTaskTab.test.ts@@ -7,7 +7,8 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { _resetStore, addTab, ensureHydrated, getBucket } from '../../store';import { routeSidebarCommand } from '../detachedSidebarRouting';-import { subscribeRevealSessionTab } from '../../globalTabs';+import { __resetGlobalTabsForTest, addGlobalPluginTab, getActiveGlobalTabId, setActiveGlobalTabId, subscribeRevealSessionTab } from '../../globalTabs';+import { __resetTabGroupsForTest, getGroupActiveTabId, setTabGroup } from '../../tabGroups';import { openPluginTaskTab } from '../openPluginTaskTab';import { _resetSidebarCommandsForTests, onRequestRightSidebarVisibility } from '../sidebarCommands';import { __resetSidebarTabWindowsStateForTest, __setSidebarTabWindowsStateForTest } from '@/lib/sidebarTabWindowState';@@ -30,6 +31,7 @@ const ipc = {const visibility = vi.fn();beforeEach(() => {+ __resetGlobalTabsForTest(); __resetTabGroupsForTest();__resetSidebarTabWindowsStateForTest();_resetStore(); _resetSidebarCommandsForTests();rows.clear(); active.clear(); localStorage.clear(); vi.clearAllMocks();@@ -37,12 +39,36 @@ beforeEach(() => {window.electronAPI = { localDb: { rightSidebarTabs: ipc }, platform: 'darwin' } as unknown as Window['electronAPI'];onRequestRightSidebarVisibility(visibility);});-afterEach(() => { _resetStore(); _resetSidebarCommandsForTests(); });+afterEach(() => { _resetStore(); _resetSidebarCommandsForTests(); __resetGlobalTabsForTest(); __resetTabGroupsForTest(); });const taskTabs = (sessionId: string) =>getBucket(sessionId).tabs.filter((tab) => tab.kind === 'plugin-task').map((tab) => tab.state);describe('openPluginTaskTab', () => {+ // PR #6939 discussion_r4357791816:先开页、后挂载目标任务时不能丢失选中。+ // unit:无 Shell 订阅,验证真实任务桶与全局/组激活位;保留其他组与弹出窗口选择。+ it.each(['same-group', 'other-group', 'popped-out', 'passive'] as const)(+ 'selects the target before its shell mounts without disturbing unrelated views: %s',+ async (state) => {+ const global = addGlobalPluginTab('manager')!;+ setActiveGlobalTabId(global.id);+ await openPluginTaskTab('task-2', 'sim', { userInitiated: false });+ const tab = getBucket('task-2').tabs[0];+ setTabGroup(tab.id, 'left');+ setTabGroup(global.id, state === 'other-group' ? 'right' : 'left');+ if (state === 'popped-out') {+ __setSidebarTabWindowsStateForTest({ [tab.id]: { windowId: 'window-1', sessionId: 'task-2', open: true } });+ window.electronAPI.sidebarTabWindow = { focus: vi.fn(async () => {}) } as unknown as Window['electronAPI']['sidebarTabWindow'];+ }+ await openPluginTaskTab('task-2', 'sim', { userInitiated: state !== 'passive' });+ expect(getBucket('task-2').activeTabId).toBe(tab.id);+ expect(getActiveGlobalTabId()).toBe(state === 'same-group' ? null : global.id);+ expect(getGroupActiveTabId('left', 'task-2')).toBe(+ state === 'same-group' || state === 'other-group' ? tab.id : null,+ );+ },+ );+// 弹出窗口自己的激活位与主窗任务桶不同;即使桶已激活目标也必须送选中请求。it('reveals an existing popped-out tab without stealing native focus', async () => {await openPluginTaskTab('task-1', 'sim');diff --git a/apps/desktop/src/renderer/features/right-sidebar/lib/openPluginTaskTab.ts b/apps/desktop/src/renderer/features/right-sidebar/lib/openPluginTaskTab.tsindex e8a3e6aebe..169697c09c 100644--- a/apps/desktop/src/renderer/features/right-sidebar/lib/openPluginTaskTab.ts+++ b/apps/desktop/src/renderer/features/right-sidebar/lib/openPluginTaskTab.ts@@ -1,13 +1,15 @@-import { requestRevealSessionTab } from '../globalTabs';+import { getActiveGlobalTabId, requestRevealSessionTab, setActiveGlobalTabId } from '../globalTabs';import { addTab, ensureHydrated, getBucket, setActiveTab } from '../store';import type { PluginTaskTabState } from '../plugins/plugin-task';+import { getTabGroup, setGroupActiveTabId } from '../tabGroups';+import { isTabPoppedOut } from '@/lib/sidebarTabWindowState';import { routeSidebarCommand } from './detachedSidebarRouting';import { requestRightSidebarVisibility } from './sidebarCommands';/*** 在指定任务的右侧栏打开插件任务页(taskView):同一任务里每个插件实例至多一个- * 任务页页签,已有则聚焦。来自插件逻辑页的请求不是用户当次手势,分离侧栏形态下- * 不抢前台(userInitiated:false)。+ * 任务页页签,已有则聚焦。后台插件请求传 userInitiated:false,不抢分离侧栏的前台;+ * 显式跳转只接受 Main 已验证真实手势的来源,并在目标 Shell 挂载前保留选中。*/export async function openPluginTaskTab(sessionId: string,@@ -34,7 +36,19 @@ export async function openPluginTaskTab(tabId = (await addTab(sessionId, 'plugin-task', { instanceId } satisfies PluginTaskTabState)).id;}- if (tabId) requestRevealSessionTab(sessionId, tabId);+ if (tabId) {+ // 跨任务导航可能尚未挂载目标 Shell,不能只发送会丢失的一次性 reveal。+ // 直接写既有激活位;组归属和弹出位置仍以用户的放置记忆为准。+ if (userInitiated && !isTabPoppedOut(tabId)) {+ const groupId = getTabGroup(tabId);+ setGroupActiveTabId(groupId, sessionId, tabId);+ const globalTabId = getActiveGlobalTabId();+ if (globalTabId && getTabGroup(globalTabId) === groupId && !isTabPoppedOut(globalTabId)) {+ setActiveGlobalTabId(null);+ }+ }+ requestRevealSessionTab(sessionId, tabId);+ }}if (route === 'attached' || route === 'routed') {requestRightSidebarVisibility('open', { sessionId, userInitiated });