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(agent-island): 自动提醒只展开当前任务 (#6278)

Dash918cf0fcc703
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 def9f3fdb3..02a2c1e962 100644
--- a/apps/desktop/src/main/agent-island/__tests__/state.test.ts
+++ b/apps/desktop/src/main/agent-island/__tests__/state.test.ts
@@ -893,6 +893,7 @@ describe('Agent Island display state', () => {
applyAgentIslandUserPrompt(state, { sessionId: 'silent', title: 'Task' }, 'run it', start);
applyAgentIslandEvent(state, { sessionId: 'silent' }, doneEvent(), start + 200);
+ requestAgentIslandManualExpand(state);
const sessions = buildAgentIslandDisplayState(state, start + 300).sessions;
const reply = sessions.find((session) => session.sessionId === 'reply');
const silent = sessions.find((session) => session.sessionId === 'silent');
@@ -1161,6 +1162,8 @@ describe('Agent Island display state', () => {
expect(firstDisplay.mode).toBe('expanded');
expect(firstDisplay.displayPolicy).toBe('transient');
expect(firstDisplay.currentSessionId).toBe('first');
+ expect(firstDisplay.displaySurface).toBe('completionCard');
+ expect(firstDisplay.sessions.map((session) => session.sessionId)).toEqual(['first']);
expect(firstDisplay.pillSnapshot.deferredRevealCount).toBe(1);
const stillFirstDisplay = buildAgentIslandDisplayState(state, 6_100);
@@ -1172,7 +1175,9 @@ describe('Agent Island display state', () => {
expect(secondDisplay.mode).toBe('expanded');
expect(secondDisplay.displayPolicy).toBe('transient');
expect(secondDisplay.currentSessionId).toBe('second');
- expect(secondDisplay.sessions.map((session) => session.sessionId)).toEqual(['second', 'first']);
+ expect(secondDisplay.displaySurface).toBe('completionCard');
+ expect(secondDisplay.sessions.map((session) => session.sessionId)).toEqual(['second']);
+ expect(secondDisplay.pillSnapshot.unreadCompletedCount).toBe(2);
});
it('keeps blocking interaction cards ahead of queued completion reveals', () => {
@@ -1718,10 +1723,11 @@ 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', () => {
+ it('keeps a clicked manual completion list collapsed and unread when navigation never arrives', () => {
const state = createAgentIslandState();
applyAgentIslandEvent(state, { sessionId: 'first' }, doneEvent(), 1_000);
applyAgentIslandEvent(state, { sessionId: 'second' }, doneEvent(), 1_050);
+ requestAgentIslandManualExpand(state);
expect(buildAgentIslandDisplayState(state, 1_100).displaySurface).toBe('sessionList');
setAgentIslandPointerZones(state, { menuBar: false, panel: true }, 1_110);
@@ -1942,20 +1948,28 @@ describe('Agent Island display state', () => {
expect(manualExpanded.sessions.map((session) => session.sessionId)).toEqual(['done', 'running']);
});
- it('stacks newer completion reveals without displacing the active completion', () => {
+ it.each(['completed', 'error'] as const)('keeps an old unread %s out of a new completion reveal but in the manual list', (phase) => {
const state = createAgentIslandState();
- applyAgentIslandEvent(state, { sessionId: 'first', title: 'First' }, doneEvent(), 2_000);
- expect(buildAgentIslandDisplayState(state, 2_100).currentSessionId).toBe('first');
+ applyAgentIslandEvent(state, { sessionId: 'old' }, phase === 'completed' ? doneEvent() : terminalErrorEvent('Failed'), 1_000);
+ expect(buildAgentIslandDisplayState(state, 1_100).currentSessionId).toBe('old');
+ expect(buildAgentIslandDisplayState(state, 20_000).mode).toBe('compact');
+
+ applyAgentIslandEvent(state, { sessionId: 'new' }, doneEvent(), 21_000);
+ const automatic = buildAgentIslandDisplayState(state, 21_100);
- applyAgentIslandEvent(state, { sessionId: 'second', title: 'Second' }, doneEvent(), 2_300);
- const stacked = buildAgentIslandDisplayState(state, 2_500);
+ expect(automatic.mode).toBe('expanded');
+ expect(automatic.displayPolicy).toBe('transient');
+ expect(automatic.displaySurface).toBe('completionCard');
+ expect(automatic.currentSessionId).toBe('new');
+ expect(automatic.sessions.map((session) => session.sessionId)).toEqual(['new']);
+ expect(automatic.totalCount).toBe(1);
+ expect(state.sessions.get('old')?.unread).toBe(true);
+ expect(buildAllSessionActivitySnapshots(state).map((session) => session.sessionId)).toEqual(['old', 'new']);
- expect(stacked.mode).toBe('expanded');
- expect(stacked.displayPolicy).toBe('transient');
- expect(stacked.displaySurface).toBe('sessionList');
- expect(stacked.currentSessionId).toBe('first');
- expect(stacked.sessions.map((session) => session.sessionId)).toEqual(['first', 'second']);
- expect(stacked.pillSnapshot.unreadCompletedCount).toBe(2);
+ requestAgentIslandManualExpand(state);
+ const manual = buildAgentIslandDisplayState(state, 21_200);
+ expect(manual.displaySurface).toBe('sessionList');
+ expect(manual.sessions.map((session) => session.sessionId).sort()).toEqual(['new', 'old']);
});
it('orders completed sessions first in the expanded session list', () => {
@@ -2066,7 +2080,7 @@ describe('Agent Island display state', () => {
expect(display.pillSnapshot.unreadCompletedCount).toBe(1);
});
- it('protects stacked completion reveals briefly before outside click can collapse them', () => {
+ it('protects the active completion before outside click advances to the queued reminder', () => {
const state = createAgentIslandState();
applyAgentIslandEvent(state, { sessionId: 'first', title: 'First' }, doneEvent(), 2_000);
applyAgentIslandEvent(state, { sessionId: 'second', title: 'Second' }, doneEvent(), 2_100);
@@ -2075,17 +2089,38 @@ describe('Agent Island display state', () => {
expect(dismissAgentIslandActiveReveal(state, 2_250)).toBe(false);
const protectedDisplay = buildAgentIslandDisplayState(state, 2_300);
expect(protectedDisplay.mode).toBe('expanded');
- expect(protectedDisplay.displaySurface).toBe('sessionList');
+ expect(protectedDisplay.displaySurface).toBe('completionCard');
expect(dismissAgentIslandActiveReveal(state, 3_300)).toBe(true);
const display = buildAgentIslandDisplayState(state, 3_350);
- expect(display.mode).toBe('compact');
+ expect(display.mode).toBe('expanded');
expect(display.displayPolicy).toBe('transient');
- expect(display.sessions.map((session) => session.sessionId)).toEqual(['second', 'first']);
+ expect(display.displaySurface).toBe('completionCard');
+ expect(display.sessions.map((session) => session.sessionId)).toEqual(['second']);
expect(display.pillSnapshot.unreadCompletedCount).toBe(2);
});
+ it.each(['completed', 'error'] as const)('keeps queued reminders after opening the current %s card', (phase) => {
+ const state = createAgentIslandState();
+ applyAgentIslandEvent(state, { sessionId: 'first' }, phase === 'completed' ? doneEvent() : terminalErrorEvent('Failed'), 1_000);
+ applyAgentIslandEvent(state, { sessionId: 'second' }, doneEvent(), 1_050);
+ expect(buildAgentIslandDisplayState(state, 1_100).sessions.map((session) => session.sessionId)).toEqual(['first']);
+
+ requestAgentIslandSessionFocus(state, 'first', 1_200);
+ // Errors keep the existing navigation-ack behavior; completion closes immediately.
+ if (phase === 'error') setAgentIslandVisibleSession(state, 'first', 1_250);
+ const next = buildAgentIslandDisplayState(state, 1_300);
+ expect(next.displaySurface).toBe('completionCard');
+ expect(next.sessions.map((session) => session.sessionId)).toEqual(['second']);
+ expect(state.sessions.get('first')?.unread).toBe(true);
+
+ // A late route report for the previous card must not close the new one.
+ setAgentIslandVisibleSession(state, 'first', 1_400);
+ expect(buildAgentIslandDisplayState(state, 1_450).sessions.map((session) => session.sessionId)).toEqual(['second']);
+ expect(buildAgentIslandDisplayState(state, 20_000).mode).toBe('compact');
+ });
+
it('collapses hover-expanded unread completion lists without clearing automatic reveals', () => {
const state = createAgentIslandState();
applyAgentIslandEvent(state, { sessionId: 'done', title: 'Done' }, doneEvent(), 2_000);
diff --git a/apps/desktop/src/main/agent-island/state.ts b/apps/desktop/src/main/agent-island/state.ts
index 2b278c5c9a..7876c19dd4 100644
--- a/apps/desktop/src/main/agent-island/state.ts
+++ b/apps/desktop/src/main/agent-island/state.ts
@@ -1402,7 +1402,11 @@ export function requestAgentIslandSessionFocus(
let completionDismissed = false;
const isCompletion = state.sessions.get(nextSessionId)?.phase === 'completed';
if (isCompletion) {
- completionDismissed = dismissTransientReveals(state);
+ // The manual list exposes the whole queue; a single card only disposes itself.
+ completionDismissed = dismissTransientReveals(
+ state,
+ state.lastDisplayPolicy === 'manualExpanded' ? undefined : nextSessionId,
+ );
completionDismissed = collapseAgentIslandToCompact(state, now) || completionDismissed;
}
if (state.visibleSessionIds.has(nextSessionId)) {
@@ -1525,7 +1529,7 @@ export function buildAgentIslandDisplayState(
const activeTransient = getActiveTransientSession(state, now);
const orderedSessions = orderSessionsForCurrentSurface(
state,
- orderSessionsForAutomaticTransientStack(sortedSessions, activeTransient, manualExpanded),
+ orderSessionsForAutomaticReveal(sortedSessions, activeTransient, manualExpanded),
manualExpanded,
now,
);
@@ -1535,8 +1539,8 @@ export function buildAgentIslandDisplayState(
const notchStatus = getNotchStatus(current, mode);
const displayPolicy = decision.displayPolicy;
const surfaceOrderedSessions = orderSessionsForDecisionSurface(decision, orderedSessions);
- const displaySessions = getDisplaySessionsForSurface(decision, surfaceOrderedSessions, now);
- const displaySurface = getDisplaySurface(decision, displaySessions, now);
+ const displaySessions = getDisplaySessionsForSurface(decision, surfaceOrderedSessions);
+ const displaySurface = getDisplaySurface(decision);
updateExpandedProtection(state, mode, displayPolicy, displaySurface, current?.sessionId ?? null, now);
const layoutMode = getLayoutMode(notchStatus);
const pointerInside = isPointerInsideIsland(state);
@@ -1569,7 +1573,7 @@ export function buildAgentIslandDisplayState(
* 展示面(getDisplaySessionsForSurface)过滤。
*
* 为什么不用 buildAgentIslandDisplayState 的 displaySessions:展示面在岛显示
- * transient 完成/错误卡时只保留可堆叠的 transient 会话,会过滤掉其它运行中/等待中的
+ * transient 完成/错误卡时只保留本次提醒的会话,会过滤掉其它未读/运行中/等待中的
* 会话;而 renderer 的 agentIslandActivity store 把每次 payload 当**整体替换**,用过滤
* 后的子集会把这些会话从活动 map 里丢掉、卡片回退陈旧 summary,直到岛面切换
* (PR #246 review)。侧栏卡片按 sessionId 各取所需,故这里取 state.sessions 全集。
@@ -1935,19 +1939,23 @@ function deferActiveTransientReveal(
function dismissPublishedTransientReveal(state: AgentIslandState): boolean {
if (state.lastDisplayMode !== 'expanded' || state.lastDisplayPolicy !== 'transient') return false;
- return dismissTransientReveals(state);
+ if (!state.lastDisplaySessionId) return false;
+ return dismissTransientReveals(state, state.lastDisplaySessionId);
}
-function dismissTransientReveals(state: AgentIslandState): boolean {
+function dismissTransientReveals(state: AgentIslandState, onlySessionId?: string): boolean {
const transientSessionIds = [
state.activeTransientSessionId,
...state.transientRevealQueue,
- ].filter((sessionId): sessionId is string => Boolean(sessionId));
+ ].filter((sessionId): sessionId is string => Boolean(sessionId)
+ && (onlySessionId === undefined || sessionId === onlySessionId));
if (transientSessionIds.length === 0) return false;
- state.activeTransientSessionId = null;
- state.transientRevealQueue = [];
+ if (state.activeTransientSessionId && transientSessionIds.includes(state.activeTransientSessionId)) {
+ state.activeTransientSessionId = null;
+ }
+ state.transientRevealQueue = state.transientRevealQueue.filter((sessionId) => !transientSessionIds.includes(sessionId));
let changed = false;
for (const sessionId of transientSessionIds) {
@@ -2130,7 +2138,7 @@ function orderSessionsForCurrentSurface(
return ordered;
}
-function orderSessionsForAutomaticTransientStack(
+function orderSessionsForAutomaticReveal(
sortedSessions: AgentIslandSessionState[],
activeTransient: AgentIslandSessionState | null,
manualExpanded: boolean,
@@ -2261,8 +2269,6 @@ function orderSessionsForDecisionSurface(
function getDisplaySurface(
decision: AgentIslandDisplayDecision,
- displaySessions: AgentIslandSessionState[],
- now: number,
): AgentIslandDisplaySurface {
if (decision.mode !== 'expanded') return 'collapsed';
switch (decision.surface.kind) {
@@ -2271,9 +2277,6 @@ function getDisplaySurface(
case 'blocking':
return 'interactionCard';
case 'transient':
- if (countStackedTransientSessions(displaySessions, now) > 1) {
- return 'sessionList';
- }
return 'completionCard';
default:
return 'collapsed';
@@ -2283,27 +2286,11 @@ function getDisplaySurface(
function getDisplaySessionsForSurface(
decision: AgentIslandDisplayDecision,
orderedSessions: AgentIslandSessionState[],
- now: number,
): AgentIslandSessionState[] {
if (decision.surface.kind !== 'transient') return orderedSessions;
- const transientSessions = orderedSessions.filter((session) => isStackableTransientSession(session, now));
- return transientSessions.length > 0 ? transientSessions : orderedSessions;
-}
-
-function countStackedTransientSessions(sessions: AgentIslandSessionState[], now: number): number {
- let count = 0;
- for (const session of sessions) {
- if (isStackableTransientSession(session, now)) count += 1;
- }
- return count;
-}
-
-function isStackableTransientSession(session: AgentIslandSessionState, now: number): boolean {
- if (session.phase !== 'completed' && session.phase !== 'error') return false;
- return session.unread
- || Boolean(session.revealUntil && session.revealUntil > now)
- || Boolean(session.completedUntil && session.completedUntil > now)
- || Boolean(session.errorUntil && session.errorUntil > now);
+ // Automatic notifications show only the active reveal; unread history stays
+ // available in the manual list and queued notifications get their own turn.
+ return [decision.surface.current];
}
// 徽标里「运行中」与「需要关注」互斥:等用户回答的任务只算需要关注,不重复计入运行中。