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(sidebar): 保持当前任务的优先级直到离开 (#4620)

Dash9406fed1e197
diff --git a/apps/desktop/src/renderer/__tests__/dialogueSidebarSection.test.ts b/apps/desktop/src/renderer/__tests__/dialogueSidebarSection.test.ts
index 74212d1de5..918a800170 100644
--- a/apps/desktop/src/renderer/__tests__/dialogueSidebarSection.test.ts
+++ b/apps/desktop/src/renderer/__tests__/dialogueSidebarSection.test.ts
@@ -74,6 +74,9 @@ describe('Mixed main list (sidebar-redesign D 期)', () => {
it('holds the current priority rank before click-path attention clear', () => {
const clickHandler = extractHandlerBlock(sidebarSource, 'handleSessionClick');
+ // Remote rows live in the merged index, not the local sessions array.
+ expect(clickHandler).toContain('const target = sessionsByIdRef.current.get(id)');
+ expect(clickHandler).toContain('target ? [target] : []');
expect(clickHandler.indexOf('holdSidebarViewedPriority')).toBeGreaterThan(-1);
expect(clickHandler.indexOf('holdSidebarViewedPriority')).toBeLessThan(
clickHandler.indexOf('clearNotification(id)'),
diff --git a/apps/desktop/src/renderer/__tests__/mainListModel.test.ts b/apps/desktop/src/renderer/__tests__/mainListModel.test.ts
index 8a2cccf7a2..5742088270 100644
--- a/apps/desktop/src/renderer/__tests__/mainListModel.test.ts
+++ b/apps/desktop/src/renderer/__tests__/mainListModel.test.ts
@@ -14,6 +14,7 @@ import {
buildMainListEntries,
getMainListEntrySessions,
holdViewedPriorityRank,
+ sessionPriorityRecencyMs,
sessionPriorityRank,
splitEntriesByDevice,
type MainListEntry,
@@ -712,6 +713,22 @@ describe('buildMainListEntries — 排序口径', () => {
expect(labels(entries)).toEqual(['s:needs-input', 's:just-read', 's:older-rest']);
});
+ it('does not apply an earlier visit time while the unread rank is still held', () => {
+ const viewed = session({ updatedAt: '2026-07-01T00:00:00Z', title: 'viewed' });
+ const hold = {
+ heldPriorityRanks: new Map<string, number>(),
+ recentlyViewedAtMs: new Map([[viewed.id, Date.parse('2026-08-01T00:00:00Z')]]),
+ };
+ const unread = { ...NO_PRIORITY, attentionSessionIds: new Set([viewed.id]) };
+ holdViewedPriorityRank(hold, viewed.id, unread);
+ const beforeRead = sessionPriorityRecencyMs(viewed, { ...unread, ...hold });
+ advanceViewedPriorityHold(hold, viewed.id, NO_PRIORITY, 1_000);
+ expect(sessionPriorityRecencyMs(viewed, { ...NO_PRIORITY, ...hold })).toBe(beforeRead);
+ const leaveAt = Date.parse('2026-09-01T00:00:00Z');
+ advanceViewedPriorityHold(hold, undefined, NO_PRIORITY, leaveAt);
+ expect(sessionPriorityRecencyMs(viewed, { ...NO_PRIORITY, ...hold })).toBe(leaveAt);
+ });
+
it('does not let leave time promote a still-waiting or running task', () => {
const waitingOld = session({ updatedAt: '2026-07-01T00:00:00Z', title: 'waiting-old' });
const waitingNew = session({ updatedAt: '2026-08-12T00:00:00Z', title: 'waiting-new' });
diff --git a/apps/desktop/src/renderer/__tests__/projectsSidebarSection.test.ts b/apps/desktop/src/renderer/__tests__/projectsSidebarSection.test.ts
index b4f114dbf1..43357668ff 100644
--- a/apps/desktop/src/renderer/__tests__/projectsSidebarSection.test.ts
+++ b/apps/desktop/src/renderer/__tests__/projectsSidebarSection.test.ts
@@ -108,15 +108,13 @@ describe('Projects sidebar section', () => {
'const remoteActivityRevision = useRemoteSessionActivityRevision()',
);
expect(projectsSectionSource).toContain(
- 'const activity = getRemoteSessionActivity(session.id, session.deviceLinkDeviceId)',
- );
- // running / needs-interaction / error / completed-unread 各归其档。
- expect(projectsSectionSource).toMatch(
- /activity\.phase === 'needs-interaction' \|\| activity\.phase === 'error'/,
+ 'getRemoteSessionActivity(session.id, session.deviceLinkDeviceId)?.phase',
);
+ // 档位映射由 sidebarPriorityContext 行为测试覆盖;排序和点击共用该入口。
+ expect(projectsSectionSource).toContain('sidebarPriorityContext(');
// 三个集合作为一个整体喂给混排模型。
expect(projectsSectionSource).toContain('priorityContext,');
- expect(projectsSectionSource).toContain('advanceViewedPriorityHold(');
+ expect(projectsSectionSource).toContain('useViewedPriorityHold(');
expect(projectsSectionSource).toContain('holdViewedPriorityRank(');
expect(projectsSectionSource).toContain('viewedSessionId ?? activeSessionId');
// 折叠豁免与排序同一口径(含远程),不再用只有本地的 notifications。
diff --git a/apps/desktop/src/renderer/__tests__/sidebarPriorityContext.test.ts b/apps/desktop/src/renderer/__tests__/sidebarPriorityContext.test.ts
new file mode 100644
index 0000000000..c8977e2a01
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/sidebarPriorityContext.test.ts
@@ -0,0 +1,31 @@
+import { expect, it } from 'vitest';
+import type { Session } from '@/lib/ccAgent.types';
+import { sidebarPriorityContext } from '../features/cc-agent/lib/sidebarPriorityContext';
+import {
+ advanceViewedPriorityHold,
+ holdViewedPriorityRank,
+ sessionPriorityRank,
+} from '../features/cc-agent/lib/mainListModel';
+import { LIVE_TASK_PRIORITY } from '../../shared/liveTaskPriority';
+
+it.each([
+ ['completed', LIVE_TASK_PRIORITY.unread],
+ ['needs-interaction', LIVE_TASK_PRIORITY.waiting],
+ ['error', LIVE_TASK_PRIORITY.waiting],
+ ['running', LIVE_TASK_PRIORITY.running],
+] as const)('captures remote %s before the read receipt removes its activity', (phase, rank) => {
+ const session = { id: 'remote', deviceLinkDeviceId: 'device' } as Session;
+ const local = { runningSessionIds: new Set<string>(), attentionSessionIds: new Set<string>() };
+ const state = {
+ heldPriorityRanks: new Map<string, number>(),
+ recentlyViewedAtMs: new Map<string, number>(),
+ };
+ const beforeRead = sidebarPriorityContext(local, [session], () => phase);
+ expect(sessionPriorityRank(session, beforeRead)).toBe(rank);
+ holdViewedPriorityRank(state, session.id, beforeRead);
+ const afterRead = sidebarPriorityContext(local, [session], () => undefined);
+ advanceViewedPriorityHold(state, session.id, afterRead, 1000);
+ expect(sessionPriorityRank(session, { ...afterRead, ...state })).toBe(rank);
+ advanceViewedPriorityHold(state, 'other', afterRead, 2000);
+ expect(sessionPriorityRank(session, { ...afterRead, ...state })).toBe(LIVE_TASK_PRIORITY.rest);
+});
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
index 61fe01cfa4..19b6b5f35a 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
@@ -173,6 +173,7 @@ import { PinnedSection, type PinnedSidebarEntry } from './sidebar/sections/Pinne
import { ProjectNode as ProjectNodeView } from './sidebar/sections/ProjectNode';
import { compareDialogueSessions, type DialogueSortBy } from './sidebar/sections/DialogueSection';
import { holdSidebarViewedPriority, ProjectsSection } from './sidebar/sections/ProjectsSection';
+import { sidebarPriorityContext } from './lib/sidebarPriorityContext';
import { toStoredSessionTitle } from './lib/sessionDisplayTitle';
import {
getVisibleSidebarSessionIds,
@@ -1855,8 +1856,8 @@ function ExpandedView({
urgentSetRef.current = urgentSet;
const attentionKindsRef = useRef(attentionKinds);
attentionKindsRef.current = attentionKinds;
- const runningSessionIdsRef = useRef(runningSessionIds);
- runningSessionIdsRef.current = runningSessionIds;
+ const runningSessionIdsRef = useRef(displayRunningSessionIds);
+ runningSessionIdsRef.current = displayRunningSessionIds;
const sidebarNotificationsRef = useRef(sidebarNotifications);
sidebarNotificationsRef.current = sidebarNotifications;
// viewedSessionId 同理:handleActionClick 只在触发归档那一刻用它算重定向目标。
@@ -2205,18 +2206,25 @@ function ExpandedView({
for (const [sessionId, kind] of attentionKindsRef.current) {
if (kind === 'awaiting' || kind === 'error') waiting.add(sessionId);
}
- holdSidebarViewedPriority(id, {
- runningSessionIds: runningSessionIdsRef.current,
- attentionSessionIds: sidebarNotificationsRef.current,
- waitingSessionIds: waiting,
- });
+ const target = sessionsByIdRef.current.get(id);
+ holdSidebarViewedPriority(
+ id,
+ sidebarPriorityContext(
+ {
+ runningSessionIds: runningSessionIdsRef.current,
+ attentionSessionIds: sidebarNotificationsRef.current,
+ waitingSessionIds: waiting,
+ },
+ target ? [target] : [],
+ (session) => getRemoteSessionActivity(session.id, session.deviceLinkDeviceId)?.phase,
+ ),
+ );
// F-SB-7: Clear done notification on click
clearNotification(id);
markAutomationSessionRunsRead(id);
clearSystemSessionAttention(id);
if (id === activeSessionIdRef.current) return; // No duplicate navigate.
if (import.meta.env.DEV) perfLog.debug(`sidebar:click sid=${id}`); // 纯诊断,生产剔除
- const target = sessionsRef.current.find((s) => s.id === id);
navigate(await resolveSessionRoute(id, target));
},
[navigate, clearNotification, markAutomationSessionRunsRead],
diff --git a/apps/desktop/src/renderer/features/cc-agent/hooks/__tests__/useViewedPriorityHold.test.tsx b/apps/desktop/src/renderer/features/cc-agent/hooks/__tests__/useViewedPriorityHold.test.tsx
new file mode 100644
index 0000000000..fe7dd77a19
--- /dev/null
+++ b/apps/desktop/src/renderer/features/cc-agent/hooks/__tests__/useViewedPriorityHold.test.tsx
@@ -0,0 +1,72 @@
+// @vitest-environment jsdom
+import { Suspense, startTransition, useState } from 'react';
+import { act, cleanup, render, screen } from '@testing-library/react';
+import { afterEach, expect, it } from 'vitest';
+import { useViewedPriorityHold } from '../useViewedPriorityHold';
+import { holdViewedPriorityRank, type ViewedPriorityHoldState } from '../../lib/mainListModel';
+import { LIVE_TASK_PRIORITY } from '../../../../../shared/liveTaskPriority';
+
+afterEach(cleanup);
+
+it('retains click-time unread priority across an interrupted route render until leaving', async () => {
+ const state: ViewedPriorityHoldState = {
+ heldPriorityRanks: new Map(),
+ recentlyViewedAtMs: new Map(),
+ };
+ const readContext = {
+ runningSessionIds: new Set<string>(),
+ attentionSessionIds: new Set<string>(),
+ };
+ let resolveRoute!: () => void;
+ let ready = false;
+ const route = new Promise<void>((resolve) => {
+ resolveRoute = resolve;
+ });
+ let navigate!: (id: string) => void;
+ let refresh!: () => void;
+ const rendered: string[] = [];
+ function List({ viewed, revision }: { viewed: string; revision: number }) {
+ const hold = useViewedPriorityHold(state, viewed, { ...readContext });
+ rendered.push(`${viewed}:${revision}`);
+ return (
+ <div data-testid="rank">
+ {viewed}:{hold.heldPriorityRanks.get(viewed)}
+ </div>
+ );
+ }
+ function Route({ viewed }: { viewed: string }) {
+ if (viewed === 'B' && !ready) throw route;
+ return null;
+ }
+ function App() {
+ const [viewed, setViewed] = useState('A');
+ const [revision, setRevision] = useState(0);
+ navigate = setViewed;
+ refresh = () => setRevision((value) => value + 1);
+ return (
+ <Suspense fallback="loading">
+ <List viewed={viewed} revision={revision} />
+ <Route viewed={viewed} />
+ </Suspense>
+ );
+ }
+ render(<App />);
+ act(() => {
+ holdViewedPriorityRank(state, 'B', { ...readContext, attentionSessionIds: new Set(['B']) });
+ startTransition(() => navigate('B'));
+ });
+ expect(rendered).toContain('B:0');
+ expect(screen.getByTestId('rank').textContent).toBe('A:3');
+ act(refresh);
+ expect(rendered).toContain('A:1');
+ expect(state.heldPriorityRanks.get('B')).toBe(LIVE_TASK_PRIORITY.unread);
+ await act(async () => {
+ ready = true;
+ resolveRoute();
+ await route;
+ });
+ expect(screen.getByTestId('rank').textContent).toBe('B:1');
+ act(() => navigate('C'));
+ expect(state.heldPriorityRanks.has('B')).toBe(false);
+ expect(state.recentlyViewedAtMs.has('B')).toBe(true);
+});
diff --git a/apps/desktop/src/renderer/features/cc-agent/hooks/useViewedPriorityHold.ts b/apps/desktop/src/renderer/features/cc-agent/hooks/useViewedPriorityHold.ts
new file mode 100644
index 0000000000..8df70adbb8
--- /dev/null
+++ b/apps/desktop/src/renderer/features/cc-agent/hooks/useViewedPriorityHold.ts
@@ -0,0 +1,32 @@
+import { useLayoutEffect, useMemo } from 'react';
+import {
+ advanceViewedPriorityHold,
+ type MainListPriorityContext,
+ type ViewedPriorityHoldState,
+} from '../lib/mainListModel';
+
+/** Only a committed route may release the previously viewed task's priority. */
+export function useViewedPriorityHold(
+ state: ViewedPriorityHoldState,
+ viewedSessionId: string | undefined,
+ ctx: MainListPriorityContext,
+): ViewedPriorityHoldState {
+ const projected = useMemo(
+ () =>
+ advanceViewedPriorityHold(
+ {
+ prevViewedId: state.prevViewedId,
+ heldPriorityRanks: new Map(state.heldPriorityRanks),
+ recentlyViewedAtMs: new Map(state.recentlyViewedAtMs),
+ },
+ viewedSessionId,
+ ctx,
+ Date.now(),
+ ),
+ [state, viewedSessionId, ctx],
+ );
+ useLayoutEffect(() => {
+ Object.assign(state, projected);
+ }, [state, projected]);
+ return projected;
+}
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts b/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts
index 1a56268139..1fe6ec00dc 100644
--- a/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts
@@ -174,7 +174,7 @@ export function holdViewedPriorityRank(
}
export function sessionPriorityRecencyMs(session: Session, ctx: MainListPriorityContext): number {
- if (sessionNaturalPriorityRank(session, ctx) !== LIVE_TASK_PRIORITY.rest) {
+ if (sessionPriorityRank(session, ctx) !== LIVE_TASK_PRIORITY.rest) {
return sessionActivityMs(session);
}
const viewedAt = ctx.recentlyViewedAtMs?.get(session.id) ?? 0;
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/sidebarPriorityContext.ts b/apps/desktop/src/renderer/features/cc-agent/lib/sidebarPriorityContext.ts
new file mode 100644
index 0000000000..acf70a6330
--- /dev/null
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/sidebarPriorityContext.ts
@@ -0,0 +1,30 @@
+import type { Session } from '@/lib/ccAgent.types';
+import type { RemoteSessionActivityPhase } from '@/features/device-link/remoteSessionActivityStore';
+import type { MainListPriorityContext } from './mainListModel';
+
+/** Click capture and list sorting must see the same local and remote activity. */
+export function sidebarPriorityContext(
+ ctx: MainListPriorityContext,
+ sessions: Iterable<Session>,
+ remotePhaseOf: (session: Session) => RemoteSessionActivityPhase | undefined,
+): MainListPriorityContext {
+ const running = new Set(ctx.runningSessionIds);
+ const attention = new Set(ctx.attentionSessionIds);
+ const waiting = new Set(ctx.waitingSessionIds);
+ for (const session of sessions) {
+ const phase = remotePhaseOf(session);
+ if (!phase) continue;
+ if (phase === 'running') {
+ running.add(session.id);
+ } else {
+ attention.add(session.id);
+ if (phase === 'needs-interaction' || phase === 'error') waiting.add(session.id);
+ }
+ }
+ return {
+ ...ctx,
+ runningSessionIds: running,
+ attentionSessionIds: attention,
+ waitingSessionIds: waiting,
+ };
+}
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
index c7981d6734..d93605639a 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
@@ -68,7 +68,6 @@ import {
DIALOGUE_GROUP_ALL_KEY,
} from '../../hooks/helpers/sidebarFilterCore';
import {
- advanceViewedPriorityHold,
buildMainListEntries,
getMainListEntrySessions,
holdViewedPriorityRank,
@@ -77,6 +76,8 @@ import {
type MainListEntry,
type ViewedPriorityHoldState,
} from '../../lib/mainListModel';
+import { sidebarPriorityContext } from '../../lib/sidebarPriorityContext';
+import { useViewedPriorityHold } from '../../hooks/useViewedPriorityHold';
import { projectKeyComparisonKey, type BotGroupNode } from '../../lib/projectGrouping';
import { buildSessionSourceLabelMap } from '../../lib/sessionSourceLabel';
import { useSessionAttentionKinds } from '@/lib/sessionAttentionStore';
@@ -457,45 +458,16 @@ export function ProjectsSection({
);
// 正在看的任务 id:files 路由下回落到被浏览文件所属任务。
const viewedIdForSort = viewedSessionId ?? activeSessionId;
- const priorityContext = useMemo(() => {
- const running = new Set(runningSessionIds);
- const attention = new Set(notifications);
+ const naturalPriorityContext = useMemo(() => {
const waiting = new Set<string>(urgentSet);
for (const [sessionId, kind] of attentionKinds) {
if (kind === 'awaiting' || kind === 'error') waiting.add(sessionId);
}
- const considerRemote = (session: Session) => {
- const activity = getRemoteSessionActivity(session.id, session.deviceLinkDeviceId);
- if (!activity) return; // 本地会话 / 无活动条目:一次 Map 查找即返回
- if (activity.phase === 'running') {
- running.add(session.id);
- return;
- }
- // needs-interaction / error → waiting;completed(镜像只保留未读终态,
- // 已读收尾包会删条目)→ 完成未读档。
- attention.add(session.id);
- if (activity.phase === 'needs-interaction' || activity.phase === 'error') {
- waiting.add(session.id);
- }
- };
- for (const project of projects) for (const session of project.sessions) considerRemote(session);
- for (const session of dialogues) considerRemote(session);
- for (const session of unclassified) considerRemote(session);
-
- const hold = advanceViewedPriorityHold(
- viewedPriorityHold,
- viewedIdForSort,
- { runningSessionIds: running, attentionSessionIds: attention, waitingSessionIds: waiting },
- Date.now(),
+ return sidebarPriorityContext(
+ { runningSessionIds, attentionSessionIds: notifications, waitingSessionIds: waiting },
+ [...projects.flatMap((project) => project.sessions), ...dialogues, ...unclassified],
+ (session) => getRemoteSessionActivity(session.id, session.deviceLinkDeviceId)?.phase,
);
-
- return {
- runningSessionIds: running,
- attentionSessionIds: attention,
- waitingSessionIds: waiting,
- heldPriorityRanks: hold.heldPriorityRanks,
- recentlyViewedAtMs: hold.recentlyViewedAtMs,
- };
// eslint-disable-next-line react-hooks/exhaustive-deps -- remoteActivityRevision 代表 getRemoteSessionActivity 读到的整表内容
}, [
runningSessionIds,
@@ -506,8 +478,16 @@ export function ProjectsSection({
dialogues,
unclassified,
remoteActivityRevision,
- viewedIdForSort,
]);
+ const hold = useViewedPriorityHold(viewedPriorityHold, viewedIdForSort, naturalPriorityContext);
+ const priorityContext = useMemo(
+ () => ({
+ ...naturalPriorityContext,
+ heldPriorityRanks: hold.heldPriorityRanks,
+ recentlyViewedAtMs: hold.recentlyViewedAtMs,
+ }),
+ [naturalPriorityContext, hold],
+ );
// starting 只让位给真实 in-flight:本地 isRunning(见 useStartingSessionIds),
// 远程 running / needs-interaction。终态 attention 不再吸收 —— 旧终态会误伤