Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 点击灵动岛完成任务后立即收起提示 (#4094)
Dashee09e9ae2e8a
diff --git a/apps/desktop/src/main/agent-island/__tests__/service.test.ts b/apps/desktop/src/main/agent-island/__tests__/service.test.tsindex ba0bc14302..2152bbff2d 100644--- a/apps/desktop/src/main/agent-island/__tests__/service.test.ts+++ b/apps/desktop/src/main/agent-island/__tests__/service.test.ts@@ -1939,7 +1939,7 @@ describe('AgentIslandService native publishing', () => {});});- it('retains a completion click during renderer reload and collapses after the target is shown', async () => {+ it('collapses a completion before opening a loading window and retains its navigation', async () => {const { AgentIslandService } = await import('../service.js');const send = vi.fn();const mainWindow = {@@ -1967,7 +1967,11 @@ describe('AgentIslandService native publishing', () => {expect(publish.mock.calls.at(-1)?.[0]).toMatchObject({ mode: 'expanded' });const focusSession = (service as unknown as { focusSession(id: string): void }).focusSession.bind(service);+ vi.mocked(mainWindow.show).mockImplementation(() => {+ expect(publish.mock.calls.at(-1)?.[0]).toMatchObject({ mode: 'compact' });+ });focusSession('completed-session');+ expect(publish.mock.calls.at(-1)?.[0]).toMatchObject({ mode: 'compact' });// 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.diff --git a/apps/desktop/src/main/agent-island/__tests__/state.test.ts b/apps/desktop/src/main/agent-island/__tests__/state.test.tsindex 5d9f5dec20..5b6bc6553c 100644--- a/apps/desktop/src/main/agent-island/__tests__/state.test.ts+++ b/apps/desktop/src/main/agent-island/__tests__/state.test.ts@@ -1279,8 +1279,9 @@ describe('Agent Island display state', () => {expect(protectedDisplay.mode).toBe('expanded');expect(protectedDisplay.displaySurface).toBe('interactionCard');- expect(dismissAgentIslandActiveReveal(state, 2_200)).toBe(true);- const display = buildAgentIslandDisplayState(state, 2_250);+ expect(dismissAgentIslandActiveReveal(state, 1_399)).toBe(false);+ expect(dismissAgentIslandActiveReveal(state, 1_400)).toBe(true);+ const display = buildAgentIslandDisplayState(state, 1_401);expect(display.mode).toBe('compact');expect(display.displayPolicy).toBe('blocking');@@ -1632,15 +1633,17 @@ describe('Agent Island display state', () => {expect(getNextAgentIslandTimerAt(state, 1_900)).toBeNull();});- it('collapses after slow navigation without extending the background-window ack grace', () => {+ it.each([false, true])('collapses a completion immediately without extending the background-window ack grace (manual: %s)', (manual) => {const state = createAgentIslandState();applyAgentIslandEvent(state, { sessionId: 'done' }, doneEvent(), 1_000);- requestAgentIslandManualExpand(state);+ if (manual) requestAgentIslandManualExpand(state);expect(buildAgentIslandDisplayState(state, 1_100).mode).toBe('expanded');requestAgentIslandSessionFocus(state, 'done', 1_200);+ expect(buildAgentIslandDisplayState(state, 1_201).mode).toBe('compact');+ expect(state.sessions.get('done')?.unread).toBe(true);expect(isAgentIslandPendingFocusAck(state, 'done', 1_250)).toBe(true);- expect(buildAgentIslandDisplayState(state, 3_200).mode).toBe('expanded');+ expect(buildAgentIslandDisplayState(state, 3_200).mode).toBe('compact');expect(isAgentIslandPendingFocusAck(state, 'done', 3_200)).toBe(false);setAgentIslandAppFocused(state, true, 3_250);setAgentIslandVisibleSession(state, 'done', 3_300);@@ -1648,6 +1651,45 @@ describe('Agent Island display state', () => {expect(buildAgentIslandDisplayState(state, 3_350).mode).toBe('compact');});+ it('keeps a clicked completion stack collapsed and unread when navigation never arrives', () => {+ const state = createAgentIslandState();+ applyAgentIslandEvent(state, { sessionId: 'first' }, doneEvent(), 1_000);+ applyAgentIslandEvent(state, { sessionId: 'second' }, doneEvent(), 1_050);+ expect(buildAgentIslandDisplayState(state, 1_100).displaySurface).toBe('sessionList');+ setAgentIslandPointerZones(state, { menuBar: false, panel: true }, 1_110);++ requestAgentIslandSessionFocus(state, 'second', 1_120);+ expect(buildAgentIslandDisplayState(state, 1_121).mode).toBe('compact');+ // The pointer is still over the collapsed island; its next native report+ // must not turn the click into another hover expansion.+ setAgentIslandPointerZones(state, { menuBar: true, panel: false }, 1_130);+ for (const now of [2_000, 10_000, 62_000]) {+ const display = buildAgentIslandDisplayState(state, now);+ expect(display.mode).toBe('compact');+ expect(display.pillSnapshot.unreadCompletedCount).toBe(2);+ }+ expect(state.pendingFocusSessionId).toBeNull();+ });++ it.each(['manual', 'new-error'] as const)('does not close a newer %s surface on a late completion navigation', (surface) => {+ const state = createAgentIslandState();+ applyAgentIslandEvent(state, { sessionId: 'done' }, doneEvent(), 1_000);+ buildAgentIslandDisplayState(state, 1_100);+ requestAgentIslandSessionFocus(state, 'done', 1_200);+ expect(buildAgentIslandDisplayState(state, 1_201).mode).toBe('compact');++ if (surface === 'manual') {+ requestAgentIslandManualExpand(state);+ } else {+ applyAgentIslandEvent(state, { sessionId: 'done' }, statusEvent(true, 'Running'), 2_000);+ applyAgentIslandEvent(state, { sessionId: 'done' }, terminalErrorEvent('new failure'), 2_100);+ }+ expect(buildAgentIslandDisplayState(state, 2_200).mode).toBe('expanded');+ setAgentIslandVisibleSession(state, 'done', 3_200);+ expect(buildAgentIslandDisplayState(state, 3_201).mode).toBe('expanded');+ expect(state.pendingFocusSessionId).toBeNull();+ });+it('keeps the newest click when an earlier navigation finishes late', () => {const state = createAgentIslandState();applyAgentIslandEvent(state, { sessionId: 'a' }, statusEvent(true, 'Running'), 1_000);@@ -1947,8 +1989,9 @@ describe('Agent Island display state', () => {expect(protectedDisplay.mode).toBe('expanded');expect(protectedDisplay.displaySurface).toBe('completionCard');- expect(dismissAgentIslandActiveReveal(state, 3_200)).toBe(true);- const display = buildAgentIslandDisplayState(state, 3_250);+ expect(dismissAgentIslandActiveReveal(state, 2_399)).toBe(false);+ expect(dismissAgentIslandActiveReveal(state, 2_400)).toBe(true);+ const display = buildAgentIslandDisplayState(state, 2_401);expect(display.mode).toBe('compact');expect(display.displayPolicy).toBe('transient');diff --git a/apps/desktop/src/main/agent-island/service.ts b/apps/desktop/src/main/agent-island/service.tsindex 9925e47ccc..b985d0846e 100644--- a/apps/desktop/src/main/agent-island/service.ts+++ b/apps/desktop/src/main/agent-island/service.ts@@ -2327,11 +2327,11 @@ export class AgentIslandService {}const focusChanged = requestAgentIslandSessionFocus(this.state, nextSessionId, now);+ if (focusChanged) this.publish();// 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();}private dispatchMainWindowCommand(diff --git a/apps/desktop/src/main/agent-island/state.ts b/apps/desktop/src/main/agent-island/state.tsindex d623b1beca..6f23bd7d58 100644--- a/apps/desktop/src/main/agent-island/state.ts+++ b/apps/desktop/src/main/agent-island/state.ts@@ -52,6 +52,7 @@ export const AGENT_ISLAND_REVEAL_DWELL_MS = 5_000;export const AGENT_ISLAND_COMPLETION_REVEAL_DWELL_MS = 8_000;export const AGENT_ISLAND_ERROR_REVEAL_DWELL_MS = 12_000;export const AGENT_ISLAND_EXPANDED_MIN_DWELL_MS = 1_000;+const AGENT_ISLAND_OUTSIDE_CLICK_PROTECTION_MS = 300;export const AGENT_ISLAND_HOVER_EXPAND_DELAY_MS = 500;export const AGENT_ISLAND_MOUSE_LEAVE_COLLAPSE_DELAY_MS = 150;export const AGENT_ISLAND_HOVER_SHORT_COOLDOWN_MS = 300;@@ -188,6 +189,7 @@ export interface AgentIslandState {activeTransientSessionId: string | null;transientRevealQueue: string[];pendingFocusSessionId: string | null;+ pendingFocusDismissOnAck: boolean;// 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.@@ -236,6 +238,7 @@ export function createAgentIslandState(): AgentIslandState {activeTransientSessionId: null,transientRevealQueue: [],pendingFocusSessionId: null,+ pendingFocusDismissOnAck: false,pendingFocusUntil: null,lastDisplayMode: null,lastDisplayPolicy: null,@@ -270,6 +273,7 @@ export function resetAgentIslandState(state: AgentIslandState): void {state.activeTransientSessionId = fresh.activeTransientSessionId;state.transientRevealQueue = fresh.transientRevealQueue;state.pendingFocusSessionId = fresh.pendingFocusSessionId;+ state.pendingFocusDismissOnAck = fresh.pendingFocusDismissOnAck;state.pendingFocusUntil = fresh.pendingFocusUntil;state.lastDisplayMode = fresh.lastDisplayMode;state.lastDisplayPolicy = fresh.lastDisplayPolicy;@@ -1005,6 +1009,7 @@ function forgetAgentIslandSession(state: AgentIslandState, sessionId: string): vremoveQueuedTransientReveal(state, sessionId);if (state.pendingFocusSessionId === sessionId) {state.pendingFocusSessionId = null;+ state.pendingFocusDismissOnAck = false;state.pendingFocusUntil = null;}}@@ -1076,18 +1081,28 @@ export function requestAgentIslandSessionFocus(): boolean {const nextSessionId = typeof sessionId === 'string' && sessionId.trim() ? sessionId.trim() : null;if (!nextSessionId) return false;+ // Closing a completion notification is immediate feedback, not a read ack.+ // Keep navigation tracking below so a loading window can still open the task.+ let completionDismissed = false;+ const isCompletion = state.sessions.get(nextSessionId)?.phase === 'completed';+ if (isCompletion) {+ completionDismissed = dismissTransientReveals(state);+ completionDismissed = collapseAgentIslandToCompact(state, now) || completionDismissed;+ }if (state.visibleSessionIds.has(nextSessionId)) {const dismissed = dismissFocusedSessionReveal(state, nextSessionId, now);const collapsed = collapseAgentIslandToCompact(state, now);state.pendingFocusSessionId = null;+ state.pendingFocusDismissOnAck = false;state.pendingFocusUntil = null;- return dismissed || collapsed;+ return completionDismissed || dismissed || collapsed;}const previousSessionId = state.pendingFocusSessionId;const previousUntil = state.pendingFocusUntil;state.pendingFocusSessionId = nextSessionId;+ state.pendingFocusDismissOnAck = !isCompletion;state.pendingFocusUntil = now + AGENT_ISLAND_FOCUS_VERIFY_TIMEOUT_MS;- return previousSessionId !== state.pendingFocusSessionId || previousUntil !== state.pendingFocusUntil;+ return completionDismissed || previousSessionId !== state.pendingFocusSessionId || previousUntil !== state.pendingFocusUntil;}export function isAgentIslandPendingFocusAck(@@ -1100,7 +1115,11 @@ export function isAgentIslandPendingFocusAck(}export function dismissAgentIslandActiveReveal(state: AgentIslandState, now: number): boolean {- if (isExpandedProtected(state, now) && (hasActiveTransientReveal(state, now) || hasActiveBlockingReveal(state, now))) {+ // Explicit outside clicks have a shorter guard than automatic mouse-leave collapse.+ const clickProtectedUntil = state.expandedProtectUntil === null ? null+ : state.expandedProtectUntil - AGENT_ISLAND_EXPANDED_MIN_DWELL_MS + AGENT_ISLAND_OUTSIDE_CLICK_PROTECTION_MS;+ if (clickProtectedUntil !== null && clickProtectedUntil > now+ && (hasActiveTransientReveal(state, now) || hasActiveBlockingReveal(state, now))) {return false;}const dismissedTransientReveal = dismissPublishedTransientReveal(state);@@ -1397,6 +1416,7 @@ function updateFocusVerificationLifecycle(state: AgentIslandState, now: number):// 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.pendingFocusDismissOnAck = false;state.pendingFocusUntil = null;}@@ -1584,6 +1604,10 @@ function deferActiveTransientReveal(function dismissPublishedTransientReveal(state: AgentIslandState): boolean {if (state.lastDisplayMode !== 'expanded' || state.lastDisplayPolicy !== 'transient') return false;+ return dismissTransientReveals(state);+}++function dismissTransientReveals(state: AgentIslandState): boolean {const transientSessionIds = [state.activeTransientSessionId,...state.transientRevealQueue,@@ -1967,8 +1991,13 @@ function applyVerifiedFocusIfMatched(updateFocusVerificationLifecycle(state, now);const focusedSessionId = state.pendingFocusSessionId;if (!focusedSessionId || !state.visibleSessionIds.has(focusedSessionId)) return false;+ const dismissOnAck = state.pendingFocusDismissOnAck;state.pendingFocusSessionId = null;+ state.pendingFocusDismissOnAck = false;state.pendingFocusUntil = null;+ // Completion clicks already closed their notification. A delayed navigation+ // must not close a newer reveal or a list the user has since reopened.+ if (!dismissOnAck) return false;const dismissed = dismissFocusedSessionReveal(state, focusedSessionId, now);const collapsed = collapseAgentIslandToCompact(state, now);return dismissed || collapsed;