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): resolve notification titles from complete session snapshots (#4781)

DavidShenXDe788bc242b56
diff --git a/apps/desktop/src/renderer/__tests__/sessionEventNotification.test.ts b/apps/desktop/src/renderer/__tests__/sessionEventNotification.test.ts
index e2743859cd..a7bfc4df7a 100644
--- a/apps/desktop/src/renderer/__tests__/sessionEventNotification.test.ts
+++ b/apps/desktop/src/renderer/__tests__/sessionEventNotification.test.ts
@@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
botOwnedSessionNotificationTitle,
+ findSessionNotificationSession,
sendSessionEventNotification,
} from '@/lib/sessionEventNotification';
@@ -95,4 +96,27 @@ describe('shared session event notifications', () => {
);
await expect(botOwnedSessionNotificationTitle('missing')).resolves.toBeNull();
});
+
+ it('looks up a notification session across complete and remote snapshots', () => {
+ const localVisible = [{ id: 'visible', title: 'Visible' }];
+ const allLocal = [{ id: 'archived', title: 'Renamed archived task' }];
+ const remote = [{ id: 'remote', title: 'Renamed remote task' }];
+
+ expect(findSessionNotificationSession('archived', [localVisible, allLocal, remote])).toEqual(
+ allLocal[0],
+ );
+ expect(findSessionNotificationSession('remote', [localVisible, allLocal, remote])).toEqual(
+ remote[0],
+ );
+ expect(findSessionNotificationSession('missing', [localVisible, allLocal, remote])).toBeNull();
+ });
+
+ it('prefers a named snapshot when the visible snapshot still has the draft title', () => {
+ const visible = [{ id: 'same-session', title: 'New Maker' }];
+ const complete = [{ id: 'same-session', title: 'Renamed task' }];
+
+ expect(findSessionNotificationSession('same-session', [visible, complete])).toEqual(
+ complete[0],
+ );
+ });
});
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
index 2724249d6b..8dd787d697 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
@@ -99,6 +99,7 @@ import { GhostPanelRestoreEntry } from '@/cindy-brain/GhostPanelRestoreEntry';
import { GhostMainViewNavEntries } from '@/components/sidebar/GhostMainViewNavEntries';
import {
botOwnedSessionNotificationTitle,
+ findSessionNotificationSession,
sendSessionEventNotification,
} from '@/lib/sessionEventNotification';
import type { Session } from '@/lib/ccAgent.types';
@@ -742,6 +743,8 @@ export function CCAgentSidebarUpper() {
>
<ExpandedView
sessionsHook={sessionsHook}
+ allSessionsForAttention={allSessionsForAttention}
+ remoteSessionsForNotification={remoteProjectSessions}
navigate={navigate}
activeSessionId={activeSessionId}
// 兜底直接用路由参数而非 filesSession?.id:filesSession 只从本地
@@ -791,6 +794,8 @@ type SessionsHook = ReturnType<typeof useCCSessions>;
interface ExpandedProps {
sessionsHook: SessionsHook;
+ allSessionsForAttention: Session[];
+ remoteSessionsForNotification: Session[];
navigate: ReturnType<typeof useNavigate>;
activeSessionId: string | undefined;
/** 「正在被用户注视」的会话 —— 供 attention 语义(running-status 通知豁免 /
@@ -827,6 +832,8 @@ const CONFIRM_INITIAL: ConfirmState = {
function ExpandedView({
sessionsHook,
+ allSessionsForAttention,
+ remoteSessionsForNotification,
navigate,
activeSessionId,
viewedSessionId,
@@ -1058,13 +1065,23 @@ function ExpandedView({
// 不会因此重跑 transition effect。通道、失焦与灵动岛去重由共享入口收口。
const sessionsRef = useRef(sessions);
sessionsRef.current = sessions;
+ // 通知不能只查当前侧栏桶:活跃筛选、归档切换和远程会话镜像都可能让目标
+ // 不在 sessionsRef 里。保留同步查找和原有兜底,避免标题解析影响通知发送。
+ const allSessionsForNotificationRef = useRef(allSessionsForAttention);
+ allSessionsForNotificationRef.current = allSessionsForAttention;
+ const remoteSessionsForNotificationRef = useRef(remoteSessionsForNotification);
+ remoteSessionsForNotificationRef.current = remoteSessionsForNotification;
// 通知文案里的「尚未起名」兜底。走 ref 与 sessionsRef 同款:fireSessionNotification
// 是 `[]` 依赖的稳定回调,直接闭包 t 会钉住首次渲染的语言。
const unnamedLabelRef = useRef('');
unnamedLabelRef.current = t('ccAgent.common.unnamedSession');
const fireSessionNotification = useCallback(
(sessionId: string, kind: 'done' | 'error' | 'needs-reply') => {
- const session = sessionsRef.current.find((s) => s.id === sessionId);
+ const session = findSessionNotificationSession(sessionId, [
+ sessionsRef.current,
+ allSessionsForNotificationRef.current,
+ remoteSessionsForNotificationRef.current,
+ ]);
// Orca worker 自身状态翻转不发独立通知 —— 等 lead 接到 worker_report 处理完
// 再以 lead 名义统一推一条,避免同一事件双重打扰。语义上用户应回到 lead 主对话
// 查看,而非跳到 worker 实现细节;与 effectiveRunningSessionIds 的角色聚合口径一致。
diff --git a/apps/desktop/src/renderer/lib/sessionEventNotification.ts b/apps/desktop/src/renderer/lib/sessionEventNotification.ts
index a60417d3e5..64153c3d58 100644
--- a/apps/desktop/src/renderer/lib/sessionEventNotification.ts
+++ b/apps/desktop/src/renderer/lib/sessionEventNotification.ts
@@ -1,9 +1,35 @@
import { getAgentIslandEnabled, isAgentIslandSupported } from '@/hooks/useAgentIslandSettings';
import { getFeishuNotificationsEnabled } from '@/hooks/useFeishuNotificationSettings';
import { getNotificationsEnabled } from '@/hooks/useNotificationSettings';
+import { isDefaultDraftSessionTitle } from '@cindy/maker-shared/session-title';
export type SessionEventNotificationKind = 'done' | 'error' | 'needs-reply';
+/** Find a session in the snapshots available to a notification owner. */
+export function findSessionNotificationSession<T extends { id: string; title?: unknown }>(
+ sessionId: string,
+ sources: readonly (readonly T[])[],
+): T | null {
+ let fallback: T | null = null;
+ for (const source of sources) {
+ const session = source.find((candidate) => candidate.id === sessionId);
+ if (!session) continue;
+ fallback ??= session;
+
+ // A stale visible snapshot can still contain the session with its initial
+ // placeholder title. Keep looking for a complete or remote snapshot with
+ // a title that can be shown in the notification.
+ if (
+ typeof session.title === 'string'
+ && session.title.trim()
+ && !isDefaultDraftSessionTitle(session.title)
+ ) {
+ return session;
+ }
+ }
+ return fallback;
+}
+
/** Resolve Bot-owned tasks omitted from the ordinary desktop session list. */
export async function botOwnedSessionNotificationTitle(
sessionId: string,