Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 灵动岛完成卡片不再被会话进程关闭抹掉 (#1101)
Dash49fe03ae3b76
diff --git a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.tsindex 5dae01fa23..375b42060b 100644--- a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts+++ b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts@@ -191,11 +191,11 @@ describe('maker:event hot path ordering', () => {/ipcMain\.handle\(MAKER_INVOKE\.CLOSE_SESSION,[\s\S]*?\n {2}\}\);/,)?.[0];- expect(closedBlock).toContain('handleAgentIslandSessionClosedAfterCleanup(session.id);');+ expect(closedBlock).toContain("handleAgentIslandSessionClosedAfterCleanup(session.id, 'process-closed');");expectOrder(closedBlock,"cleanupPendingInteractionsForSession(session.id, 'session_closed');",- 'handleAgentIslandSessionClosedAfterCleanup(session.id);',+ "handleAgentIslandSessionClosedAfterCleanup(session.id, 'process-closed');",);expect(source).toContain('Agent Island session close cleanup failed after mandatory session cleanup');expect(closeSessionHandler).toBeTruthy();diff --git a/apps/desktop/src/main/agent-island/__tests__/service.test.ts b/apps/desktop/src/main/agent-island/__tests__/service.test.tsindex a862e060b4..af31783d67 100644--- a/apps/desktop/src/main/agent-island/__tests__/service.test.ts+++ b/apps/desktop/src/main/agent-island/__tests__/service.test.ts@@ -4264,3 +4264,49 @@ describe('AgentIslandService session attention cleared bridge (error read semantexpect(publishSpy.mock.calls.length).toBeGreaterThan(publishCountAfterError);});});++describe('会话关闭原因决定条目去留', () => {+ it('进程关闭保留仍在展示的完成卡片,归档/删除照常硬删', async () => {+ const { AgentIslandService } = await import('../service.js');+ const publish = vi.fn(() => true);+ const service = new AgentIslandService({+ getMainWindow: () => null,+ nativeHost: { failed: false, publish },+ });+ syncEnabledForTest(service, publish);+ const sessions = (+ service as unknown as { state: { sessions: Map<string, unknown> } }+ ).state.sessions;++ // 临时会话调度的真实时序:done 之后 runner 的 fire finally 立刻 closeSession。+ // 此刻完成卡片刚弹出来,硬删条目会让它当场消失(用户看到「弹一下就收起」)。+ service.handleAgentEvent({ sessionId: 'ephemeral' }, doneEvent());+ expect(sessions.has('ephemeral')).toBe(true);+ service.handleSessionClosed('ephemeral', { reason: 'process-closed' });+ expect(sessions.has('ephemeral')).toBe(true);++ // 会话被归档 / 删除(默认 reason)语义是「这条记录不该再存在」→ 照常硬删。+ service.handleSessionClosed('ephemeral');+ expect(sessions.has('ephemeral')).toBe(false);+ });++ it('进程关闭时若已无展示需求,条目照常删除', async () => {+ const { AgentIslandService } = await import('../service.js');+ const publish = vi.fn(() => true);+ const service = new AgentIslandService({+ getMainWindow: () => null,+ nativeHost: { failed: false, publish },+ });+ syncEnabledForTest(service, publish);+ const sessions = (+ service as unknown as { state: { sessions: Map<string, unknown> } }+ ).state.sessions;++ // 只是跑起来、没有任何终态未读 → 进程一关就没有保留价值。+ service.handleUserPrompt({ sessionId: 'plain', agentKind: 'codex' }, 'hi');+ expect(sessions.has('plain')).toBe(true);++ service.handleSessionClosed('plain', { reason: 'process-closed' });+ expect(sessions.has('plain')).toBe(false);+ });+});diff --git a/apps/desktop/src/main/agent-island/__tests__/state.test.ts b/apps/desktop/src/main/agent-island/__tests__/state.test.tsindex 2eaf3062f5..ea3fbebb2c 100644--- a/apps/desktop/src/main/agent-island/__tests__/state.test.ts+++ b/apps/desktop/src/main/agent-island/__tests__/state.test.ts@@ -11,9 +11,11 @@ import {applyAgentIslandMetadata,applyAgentIslandUserPrompt,AGENT_ISLAND_COMPLETION_DWELL_MS,+ AGENT_ISLAND_COMPLETION_REVEAL_DWELL_MS,AGENT_ISLAND_MESSAGE_PREVIEW_MIN_DWELL_MS,AGENT_ISLAND_UNREAD_TRANSIENT_TTL_MS,buildAgentIslandDisplayState,+ closeAgentIslandSessionPreservingUnread,createAgentIslandState,dismissAgentIslandActiveReveal,getNextAgentIslandTimerAt,@@ -2138,3 +2140,67 @@ describe('Agent Island 未读驻留 TTL(避免几小时前完成的任务无限expect(display.totalCount).toBe(0);});});++describe('会话进程关闭不该抹掉正在展示的通知', () => {+ it('临时会话 run 收尾 closeSession 后,完成卡片仍能走完 dwell', () => {+ const state = createAgentIslandState();+ // 临时会话调度(非 heartbeat、非 persistentSession)的真实时序:done 之后 runner 的+ // fire finally 立刻 closeSession,两者只隔毫秒级。+ applyAgentIslandEvent(state, { sessionId: 'ephemeral', title: '每周巡检' }, doneEvent(), 2_000);+ closeAgentIslandSessionPreservingUnread(state, 'ephemeral', 2_050);++ const justAfterClose = buildAgentIslandDisplayState(state, 2_100);+ expect(justAfterClose.totalCount).toBe(1);+ expect(justAfterClose.mode).toBe('expanded');+ expect(justAfterClose.displaySurface).toBe('completionCard');++ // dwell 走完后照常收起,但未读条目保留(与普通完成一致)。+ const afterDwell = buildAgentIslandDisplayState(state, 2_000 + AGENT_ISLAND_COMPLETION_REVEAL_DWELL_MS + 500);+ expect(afterDwell.mode).toBe('compact');+ expect(afterDwell.pillSnapshot.unreadCompletedCount).toBe(1);+ });++ it('进程关闭会落下运行态,不让 pill 一直转', () => {+ const state = createAgentIslandState();+ applyAgentIslandEvent(state, { sessionId: 'running', title: '跑着' }, statusEvent(true, '思考中'), 1_000);+ // 运行中的会话本来是可见的(isSessionVisible 对 running 直接放行)。+ expect(buildAgentIslandDisplayState(state, 1_100).totalCount).toBe(1);++ closeAgentIslandSessionPreservingUnread(state, 'running', 1_200);++ // 没有未读终态也没有 dwell 需要保留 → 条目照常清掉。+ expect(buildAgentIslandDisplayState(state, 1_300).totalCount).toBe(0);+ });++ it('进程关闭时还挂着 pending 交互 → 整条删掉,不留一张点不动的审批卡片', () => {+ const state = createAgentIslandState();+ applyAgentIslandInteractionRequest(+ state,+ { sessionId: 'perm', title: '等审批' },+ {+ kind: 'permission',+ requestId: 'r1',+ toolName: 'Bash',+ input: { command: 'rm -rf dist' },+ },+ 1_000,+ );+ expect(buildAgentIslandDisplayState(state, 1_100).totalCount).toBe(1);++ // 进程一关,这个权限请求永远不会再被响应(service 侧同时删掉了 permissionRequests)。+ closeAgentIslandSessionPreservingUnread(state, 'perm', 1_200);++ expect(buildAgentIslandDisplayState(state, 1_300).totalCount).toBe(0);+ });++ it('已经没有展示需求的会话,进程关闭时照常删除', () => {+ const state = createAgentIslandState();+ applyAgentIslandEvent(state, { sessionId: 'read', title: '已读' }, doneEvent(), 2_000);+ // 用户已经看过 → 未读清零,条目不再需要展示。+ acknowledgeAgentIslandSessionRead(state, 'read', 3_000, { source: 'explicit' });++ closeAgentIslandSessionPreservingUnread(state, 'read', 3_100);++ expect(buildAgentIslandDisplayState(state, 3_200).totalCount).toBe(0);+ });+});diff --git a/apps/desktop/src/main/agent-island/service.ts b/apps/desktop/src/main/agent-island/service.tsindex 065f9f8029..b794a58001 100644--- a/apps/desktop/src/main/agent-island/service.ts+++ b/apps/desktop/src/main/agent-island/service.ts@@ -68,6 +68,7 @@ import {applyAgentIslandUserPrompt,buildAgentIslandDisplayState,buildAllSessionActivitySnapshots,+ closeAgentIslandSessionPreservingUnread,completeAgentIslandSessionWithoutAttention,createAgentIslandUserPromptRollbackToken,createAgentIslandState,@@ -977,7 +978,18 @@ export class AgentIslandService {}}- handleSessionClosed(sessionId: string): void {+ /**+ * @param options.reason+ * `'discarded'`(默认)= 这条记录不该再存在(会话归档 / 删除、Orca worker 被策略+ * 清除),条目硬删。+ * `'process-closed'` = 只是 agent 进程收了(典型:临时会话调度 run 终态后的+ * closeSession),仍在展示的完成 / 错误卡片必须留着走完 dwell,否则刚弹出的卡片会+ * 当场消失。见 `closeAgentIslandSessionPreservingUnread`。+ */+ handleSessionClosed(+ sessionId: string,+ options: { reason?: 'discarded' | 'process-closed' } = {},+ ): void {this.stoppedSessionIds.delete(sessionId);this.replacementTurnPendingSessionIds.delete(sessionId);this.replacementTurnDispatchingSessionIds.delete(sessionId);@@ -993,7 +1005,11 @@ export class AgentIslandService {this.deferredCompletions.delete(sessionId);// Remote auth retry closes the failed session before the renderer reports// whether its replacement turn started, so keep that deferred error here.- removeAgentIslandSession(this.state, sessionId);+ if (options.reason === 'process-closed') {+ closeAgentIslandSessionPreservingUnread(this.state, sessionId, Date.now());+ } else {+ removeAgentIslandSession(this.state, sessionId);+ }this.deletePermissionRequestsForSession(sessionId);this.publish();}diff --git a/apps/desktop/src/main/agent-island/state.ts b/apps/desktop/src/main/agent-island/state.tsindex 1c63c91150..475d435229 100644--- a/apps/desktop/src/main/agent-island/state.ts+++ b/apps/desktop/src/main/agent-island/state.ts@@ -37,7 +37,10 @@ export type AgentIslandInteractionRequest =export const AGENT_ISLAND_COMPLETION_DWELL_MS = 5_000;export const AGENT_ISLAND_ERROR_DWELL_MS = 12_000;export const AGENT_ISLAND_REVEAL_DWELL_MS = 5_000;-export const AGENT_ISLAND_COMPLETION_REVEAL_DWELL_MS = 12_000;+// 12s → 8s:完成卡片的 dwell 现在能真正走完(临时会话调度 run 收尾时的 closeSession+// 不再抹掉条目,见 closeAgentIslandSessionPreservingUnread),不需要再用更长的上限去弥补+// 「随时可能被整条删掉」。+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;export const AGENT_ISLAND_HOVER_EXPAND_DELAY_MS = 500;@@ -876,6 +879,46 @@ export function removeAgentIslandSession(state: AgentIslandState, sessionId: str}}+/**+ * 会话**进程**关闭:落下运行态,但保留仍需展示的通知条目。+ *+ * 为什么不能直接删:临时会话调度(非 heartbeat、非 persistentSession)在 run 终态之后+ * 立刻 closeSession(`scheduler-host/runner.ts` 的 fire finally),而完成卡片本该在岛上+ * 停留数秒。直接删条目会让刚弹出的卡片当场消失 —— 而且不是被别的内容顶掉,是整条记录+ * 没了,用户看到的就是「弹了一下就收起」。通知是**已发生事件的记录**,生命周期不该绑在+ * agent session 句柄上。+ *+ * 判据复用 `isSessionVisible`:仍需展示(未读终态未过 TTL / dwell 未走完 / 有 pending+ * 交互)就留着,由既有 lifecycle 到期或用户 ack 收掉;否则照常删除。+ *+ * 会话被归档 / 删除、或 Orca worker 被策略清除时**不走这里** —— 那些语义是「这条记录+ * 不该再存在」,继续用 `removeAgentIslandSession` 硬删。+ */+export function closeAgentIslandSessionPreservingUnread(+ state: AgentIslandState,+ sessionId: string,+ now: number,+): void {+ const session = state.sessions.get(sessionId);+ if (!session) {+ removeAgentIslandSession(state, sessionId);+ return;+ }+ // 进程已经没了,运行态必须落下来,否则 pill 会一直转着 working 动画。+ session.running = false;+ session.currentToolUseId = null;+ session.toolDetailUntil = null;+ // pending 交互随进程一起失效(service 侧同时会 deletePermissionRequestsForSession)。+ // 留着会让用户对着一张永远不会被响应的审批卡片点按钮,所以这类条目整条删掉 —— 要保的+ // 只是「已经发生完、还没被看到」的终态通知。+ if (session.pendingInteractionIds.size > 0) {+ removeAgentIslandSession(state, sessionId);+ return;+ }+ if (isSessionVisible(session, now)) return;+ removeAgentIslandSession(state, sessionId);+}+export function requestAgentIslandSessionFocus(state: AgentIslandState,sessionId: string,diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.tsindex ffe11fa8cb..003b378a6e 100644--- a/apps/desktop/src/main/maker-ipc/register.ts+++ b/apps/desktop/src/main/maker-ipc/register.ts@@ -2147,9 +2147,12 @@ function handleAgentIslandInteractionDismissedByRequestId(requestId: string): vo}}-function handleAgentIslandSessionClosedAfterCleanup(sessionId: string): void {+function handleAgentIslandSessionClosedAfterCleanup(+ sessionId: string,+ reason: 'discarded' | 'process-closed' = 'discarded',+): void {try {- getAgentIslandService()?.handleSessionClosed(sessionId);+ getAgentIslandService()?.handleSessionClosed(sessionId, { reason });} catch (error) {log.warn('Agent Island session close cleanup failed after mandatory session cleanup', {sessionId,@@ -3435,7 +3438,10 @@ export function wireSessionToIpc(session: ReturnType<Maker['getSession']>): void// 后台活动检测:会话进程已关闭(closeSession / 删除),清账并广播横幅熄灭。clearClaudeSessionBackgroundActivity(session.id);clearSessionPersistState(session.id);- handleAgentIslandSessionClosedAfterCleanup(session.id);+ // 进程关闭 ≠ 通知作废:临时会话调度(非 heartbeat / 非 persistentSession)在 run+ // 终态后立刻 closeSession,此刻完成卡片刚在灵动岛上弹出来。硬删条目会让它当场+ // 消失,所以这条路径保留仍在展示的卡片,由 dwell 到期或用户 ack 收掉。+ handleAgentIslandSessionClosedAfterCleanup(session.id, 'process-closed');}}));