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): 修复灵动岛加载期间丢跳转与迟到确认不收起 (#3978)

Dash6b8b3a8d317c
diff --git a/apps/desktop/src/main/agent-island/__tests__/service.test.ts b/apps/desktop/src/main/agent-island/__tests__/service.test.ts
index b1c32bfcb1..ba0bc14302 100644
--- a/apps/desktop/src/main/agent-island/__tests__/service.test.ts
+++ b/apps/desktop/src/main/agent-island/__tests__/service.test.ts
@@ -25,6 +25,7 @@ import { AGENT_ISLAND_DISPLAY_CONFIG } from '../displayConfig.js';
import type { AgentIslandNativeFrame } from '../MacAgentIslandNativeHost.js';
import { markAppContentWindow } from '../../windowFocusClassifier.js';
import type { AgentIslandService } from '../service.js';
+import { setDeepLinkMainWindow, takePendingDeepLink } from '../../deepLink.js';
const REMOTE_DAEMON_CLOSED_REASON = 'remote_daemon_closed';
@@ -99,6 +100,7 @@ function tccDeniedError(): NodeJS.ErrnoException {
vi.mock('electron', () => {
return {
app: {
+ focus: vi.fn(),
getPreferredSystemLanguages: mocks.getPreferredSystemLanguages,
getLocale: mocks.getLocale,
},
@@ -136,6 +138,8 @@ vi.mock('../../device-link/broadcast-tap.js', () => ({
import { resetEpermGuidanceForTest } from '../../file-access/permissions.js';
beforeEach(() => {
+ setDeepLinkMainWindow(null);
+ takePendingDeepLink();
mocks.getSessionRowSnapshot.mockReset();
mocks.getSessionRowSnapshot.mockResolvedValue(null);
mocks.displays.splice(0, mocks.displays.length, mocks.primaryDisplay);
@@ -1883,11 +1887,15 @@ describe('AgentIslandService native publishing', () => {
const mainWindow = {
isDestroyed: () => false,
isMinimized: () => false,
+ isVisible: () => true,
restore: vi.fn(),
show: vi.fn(),
focus: vi.fn(),
- webContents: { send },
+ setAlwaysOnTop: vi.fn(),
+ moveTop: vi.fn(),
+ webContents: { send, isLoading: () => false },
} as unknown as BrowserWindow;
+ setDeepLinkMainWindow(mainWindow);
const publish = vi.fn((state: AgentIslandDisplayState, frameOrFrames: AgentIslandNativeFrame | AgentIslandNativeFrame[]) => {
void state;
void frameOrFrames;
@@ -1924,13 +1932,60 @@ describe('AgentIslandService native publishing', () => {
'target-session',
);
- expect(send).toHaveBeenCalledWith('notification:focus-session', 'target-session');
+ expect(send).toHaveBeenCalledWith('deep-link:navigate', { type: 'session', id: 'target-session' });
expect(publish.mock.calls.at(-1)?.[0]).toMatchObject({
mode: 'compact',
currentSessionId: 'target-session',
});
});
+ it('retains a completion click during renderer reload and collapses after the target is shown', async () => {
+ const { AgentIslandService } = await import('../service.js');
+ const send = vi.fn();
+ const mainWindow = {
+ isDestroyed: () => false,
+ isMinimized: () => false,
+ isVisible: () => false,
+ isFocused: () => true,
+ restore: vi.fn(),
+ show: vi.fn(),
+ focus: vi.fn(),
+ setAlwaysOnTop: vi.fn(),
+ moveTop: vi.fn(),
+ webContents: { send, isLoading: () => true },
+ } as unknown as BrowserWindow;
+ setDeepLinkMainWindow(mainWindow);
+ const publish = vi.fn((_state: AgentIslandDisplayState) => true);
+ const service = new AgentIslandService({
+ getMainWindow: () => mainWindow,
+ nativeHost: { failed: false, publish },
+ });
+ syncEnabledForTest(service, publish);
+ service.registerIpc();
+ service.handleUserPrompt({ sessionId: 'completed-session', agentKind: 'codex' }, 'run tests');
+ service.handleAgentEvent({ sessionId: 'completed-session', agentKind: 'codex' }, doneEvent());
+ expect(publish.mock.calls.at(-1)?.[0]).toMatchObject({ mode: 'expanded' });
+ const focusSession = (service as unknown as { focusSession(id: string): void }).focusSession.bind(service);
+
+ focusSession('completed-session');
+
+ // A loading renderer has no notification listener. The existing deep-link
+ // pull-on-mount path must retain the click instead of sending it into the gap.
+ expect(send).not.toHaveBeenCalled();
+ expect(mainWindow.show).toHaveBeenCalled();
+ expect(mainWindow.focus).toHaveBeenCalled();
+ expect(takePendingDeepLink()).toEqual({ type: 'session', id: 'completed-session' });
+ expect(takePendingDeepLink()).toBeNull();
+
+ service.setAppFocused(true);
+ mocks.browserWindowFromWebContents.mockReturnValue(mainWindow);
+ await registeredIpcHandler(AGENT_ISLAND_SET_VISIBLE_SESSION_CHANNEL)(
+ { sender: {} },
+ 'completed-session',
+ );
+ expect(publish.mock.calls.at(-1)?.[0]).toMatchObject({ mode: 'compact' });
+ });
+
it('smart-suppresses all visible split sessions', async () => {
const { AgentIslandService } = await import('../service.js');
const publish = vi.fn((state: AgentIslandDisplayState, frameOrFrames: AgentIslandNativeFrame | AgentIslandNativeFrame[]) => {
diff --git a/apps/desktop/src/main/agent-island/__tests__/state.test.ts b/apps/desktop/src/main/agent-island/__tests__/state.test.ts
index 9ac5f8aa52..5d9f5dec20 100644
--- a/apps/desktop/src/main/agent-island/__tests__/state.test.ts
+++ b/apps/desktop/src/main/agent-island/__tests__/state.test.ts
@@ -20,6 +20,7 @@ import {
createAgentIslandState,
dismissAgentIslandActiveReveal,
getNextAgentIslandTimerAt,
+ isAgentIslandPendingFocusAck,
markAgentIslandSessionAttention,
requestAgentIslandManualCollapse,
requestAgentIslandManualExpand,
@@ -1631,6 +1632,59 @@ describe('Agent Island display state', () => {
expect(getNextAgentIslandTimerAt(state, 1_900)).toBeNull();
});
+ it('collapses after slow navigation without extending the background-window ack grace', () => {
+ const state = createAgentIslandState();
+ applyAgentIslandEvent(state, { sessionId: 'done' }, doneEvent(), 1_000);
+ requestAgentIslandManualExpand(state);
+ expect(buildAgentIslandDisplayState(state, 1_100).mode).toBe('expanded');
+ requestAgentIslandSessionFocus(state, 'done', 1_200);
+ expect(isAgentIslandPendingFocusAck(state, 'done', 1_250)).toBe(true);
+
+ expect(buildAgentIslandDisplayState(state, 3_200).mode).toBe('expanded');
+ expect(isAgentIslandPendingFocusAck(state, 'done', 3_200)).toBe(false);
+ setAgentIslandAppFocused(state, true, 3_250);
+ setAgentIslandVisibleSession(state, 'done', 3_300);
+ acknowledgeAgentIslandSessionRead(state, 'done', 3_300);
+ expect(buildAgentIslandDisplayState(state, 3_350).mode).toBe('compact');
+ });
+
+ it('keeps the newest click when an earlier navigation finishes late', () => {
+ const state = createAgentIslandState();
+ applyAgentIslandEvent(state, { sessionId: 'a' }, statusEvent(true, 'Running'), 1_000);
+ applyAgentIslandEvent(state, { sessionId: 'b' }, statusEvent(true, 'Running'), 1_000);
+ requestAgentIslandManualExpand(state);
+ requestAgentIslandSessionFocus(state, 'a', 1_200);
+ requestAgentIslandSessionFocus(state, 'b', 1_300);
+ buildAgentIslandDisplayState(state, 3_200);
+
+ setAgentIslandVisibleSession(state, 'a', 3_250);
+ expect(buildAgentIslandDisplayState(state, 3_250).mode).toBe('expanded');
+ setAgentIslandVisibleSession(state, 'b', 3_300);
+ expect(buildAgentIslandDisplayState(state, 3_350).mode).toBe('compact');
+ });
+
+ it.each([false, true])('expires abandoned navigation before an ordinary visit (timer ran: %s)', (timerRan) => {
+ const state = createAgentIslandState();
+ applyAgentIslandEvent(state, { sessionId: 'a' }, statusEvent(true, 'Running'), 1_000);
+ requestAgentIslandManualExpand(state);
+ buildAgentIslandDisplayState(state, 1_100);
+ requestAgentIslandSessionFocus(state, 'a', 1_200);
+ buildAgentIslandDisplayState(state, 3_200);
+
+ // The grace is over, but the slow-navigation target still has a finite
+ // cleanup deadline. It must expire even if a route ack beats that timer.
+ expect(isAgentIslandPendingFocusAck(state, 'a', 3_200)).toBe(false);
+ expect(getNextAgentIslandTimerAt(state, 3_200)).toBe(61_200);
+ if (timerRan) buildAgentIslandDisplayState(state, 61_200);
+ setAgentIslandAppFocused(state, true, 61_200);
+ setAgentIslandVisibleSession(state, 'a', 61_200);
+
+ expect(buildAgentIslandDisplayState(state, 61_200).mode).toBe('expanded');
+ expect(state.pendingFocusSessionId).toBeNull();
+ expect(state.pendingFocusUntil).toBeNull();
+ expect(getNextAgentIslandTimerAt(state, 61_200)).toBeNull();
+ });
+
it('dismisses the first permission approval card after the clicked session becomes visible', () => {
const state = createAgentIslandState();
applyAgentIslandEvent(state, { sessionId: 'ask', title: 'Ask', agentKind: 'codex' }, statusEvent(true, 'Running'), 1_000);
diff --git a/apps/desktop/src/main/agent-island/service.ts b/apps/desktop/src/main/agent-island/service.ts
index f62b70644f..9925e47ccc 100644
--- a/apps/desktop/src/main/agent-island/service.ts
+++ b/apps/desktop/src/main/agent-island/service.ts
@@ -23,6 +23,7 @@ import {
import type { ApplicationMenuCommand } from '../../shared/applicationMenuCommands.js';
import { hasSessionAttention as hasAppBadgeSessionAttention } from '../appBadgeService.js';
+import { openMainWindowSession } from '../deepLink.js';
import {
AGENT_ISLAND_MAX_RESIZABLE_WIDTH,
AGENT_ISLAND_GET_DISPLAY_OPTIONS_CHANNEL,
@@ -2325,13 +2326,11 @@ export class AgentIslandService {
return;
}
- const mainWindow = this.deps.getMainWindow();
- if (!mainWindow || mainWindow.isDestroyed()) return;
const focusChanged = requestAgentIslandSessionFocus(this.state, nextSessionId, now);
- if (mainWindow.isMinimized()) mainWindow.restore();
- mainWindow.show();
- mainWindow.focus();
- mainWindow.webContents.send('notification:focus-session', nextSessionId);
+ // Reuse the primary-window handoff: it retains navigation while the
+ // renderer reloads and restores macOS app focus. A one-shot notification
+ // sent during loading is lost before MainLayout can acknowledge the task.
+ openMainWindowSession(nextSessionId);
if (focusChanged) this.publish();
}
diff --git a/apps/desktop/src/main/agent-island/state.ts b/apps/desktop/src/main/agent-island/state.ts
index 90b18843a0..d623b1beca 100644
--- a/apps/desktop/src/main/agent-island/state.ts
+++ b/apps/desktop/src/main/agent-island/state.ts
@@ -58,6 +58,7 @@ export const AGENT_ISLAND_HOVER_SHORT_COOLDOWN_MS = 300;
export const AGENT_ISLAND_TOOL_DETAIL_LINGER_MS = 2_000;
export const AGENT_ISLAND_MESSAGE_PREVIEW_MIN_DWELL_MS = 1_600;
export const AGENT_ISLAND_FOCUS_VERIFY_TIMEOUT_MS = 1_500;
+const AGENT_ISLAND_FOCUS_NAVIGATION_TIMEOUT_MS = 60_000;
// 未读的 completed / error 在**灵动岛浮窗**里驻留的上限;超过后即便用户没 ack,
// 也不再占用展开列表。岛 state 会按 TTL prune;远程绿/红点改订独立的
// remoteUnreadTerminals 账本,不跟完整会话(含活动文本)一起留下。
@@ -187,6 +188,9 @@ export interface AgentIslandState {
activeTransientSessionId: string | null;
transientRevealQueue: string[];
pendingFocusSessionId: string | null;
+ // Short grace for a renderer ack before OS focus settles. Navigation itself
+ // may take longer (for example a renderer reload); its bounded deadline is
+ // derived from this timestamp by pendingFocusNavigationExpiresAt.
pendingFocusUntil: number | null;
lastDisplayMode: AgentIslandDisplayState['mode'] | null;
lastDisplayPolicy: AgentIslandDisplayPolicy | null;
@@ -1089,8 +1093,9 @@ export function requestAgentIslandSessionFocus(
export function isAgentIslandPendingFocusAck(
state: AgentIslandState,
sessionId: string | readonly string[] | null,
+ now = Date.now(),
): boolean {
- if (!state.pendingFocusSessionId) return false;
+ if (!state.pendingFocusSessionId || !state.pendingFocusUntil || state.pendingFocusUntil <= now) return false;
return normalizeVisibleSessionIds(sessionId).includes(state.pendingFocusSessionId);
}
@@ -1253,7 +1258,7 @@ export function getNextAgentIslandTimerAt(state: AgentIslandState, now: number):
state.hoverIntentAt,
state.collapseAt,
state.hoverCooldownUntil && isPointerInsideIsland(state) ? state.hoverCooldownUntil : null,
- state.pendingFocusUntil,
+ pendingFocusNavigationExpiresAt(state),
state.expandedProtectUntil && state.protectedDismissPending ? state.expandedProtectUntil : null,
]) {
if (value && value > now && (next === null || value < next)) {
@@ -1380,8 +1385,17 @@ function completionRevealDwellMs(session: AgentIslandSessionState, now: number):
return Math.max(AGENT_ISLAND_COMPLETION_REVEAL_DWELL_MS, remainingPreviewMs + AGENT_ISLAND_REVEAL_DWELL_MS);
}
+function pendingFocusNavigationExpiresAt(state: AgentIslandState): number | null {
+ return state.pendingFocusUntil === null
+ ? null
+ : state.pendingFocusUntil - AGENT_ISLAND_FOCUS_VERIFY_TIMEOUT_MS + AGENT_ISLAND_FOCUS_NAVIGATION_TIMEOUT_MS;
+}
+
function updateFocusVerificationLifecycle(state: AgentIslandState, now: number): void {
- if (!state.pendingFocusUntil || state.pendingFocusUntil > now) return;
+ const expiresAt = pendingFocusNavigationExpiresAt(state);
+ if (expiresAt === null || expiresAt > now) return;
+ // Allow slow renderer loading, but do not let an abandoned navigation turn a
+ // much later ordinary visit into an acknowledgement of the old island click.
state.pendingFocusSessionId = null;
state.pendingFocusUntil = null;
}
@@ -1949,6 +1963,8 @@ function applyVerifiedFocusIfMatched(
state: AgentIslandState,
now: number,
): boolean {
+ // A route report can arrive before the expiry timer gets a chance to run.
+ updateFocusVerificationLifecycle(state, now);
const focusedSessionId = state.pendingFocusSessionId;
if (!focusedSessionId || !state.visibleSessionIds.has(focusedSessionId)) return false;
state.pendingFocusSessionId = null;