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): 断线时保留设备分组

Dash0dab70804108
diff --git a/apps/desktop/src/renderer/__tests__/machineSwitcher.test.ts b/apps/desktop/src/renderer/__tests__/machineSwitcher.test.ts
index 9e46ecf084..c6a6bcee89 100644
--- a/apps/desktop/src/renderer/__tests__/machineSwitcher.test.ts
+++ b/apps/desktop/src/renderer/__tests__/machineSwitcher.test.ts
@@ -26,6 +26,7 @@ import {
import { buildSwitcherDevices, selectableDeviceIds } from '@/features/device-link/switcherDevices';
import { compareDevicesByName } from '@/features/device-link/deviceSort';
import { applyDeviceRename } from '@/features/device-link/useDeviceLinkDeviceList';
+import { buildSidebarDeviceIndex } from '@/features/cc-agent/lib/sidebarDeviceGrouping';
/** 构造最小设备视图(只填 buildSwitcherDevices 关心的字段)。 */
function mkDevice(
@@ -415,6 +416,53 @@ describe('buildSwitcherDevices', () => {
});
});
+describe('sidebar device grouping across disconnection', () => {
+ it('retains both cached devices and their order when every remote disconnects, then reconnects', () => {
+ const snapshots = [true, false, true].map((connected) =>
+ buildSidebarDeviceIndex(
+ buildSwitcherDevices({
+ fullList: null,
+ syncedDevices: [
+ { deviceId: 'b', deviceName: 'Mac B', sessionCount: 2, connected },
+ { deviceId: 'a', deviceName: 'Mac A', sessionCount: 1, connected },
+ ],
+ revoked: new Set(),
+ }),
+ ),
+ );
+ for (const index of snapshots) {
+ expect([...index.keys()]).toEqual(['a', 'b']);
+ expect([...index.values()].map((device) => device.name)).toEqual(['Mac A', 'Mac B']);
+ }
+ expect(snapshots.map((index) => [...index.values()].map((device) => device.online))).toEqual([
+ [true, true],
+ [false, false],
+ [true, true],
+ ]);
+ });
+
+ it('keeps connecting devices but excludes rejected devices even when cached', () => {
+ const index = buildSidebarDeviceIndex(
+ buildSwitcherDevices({
+ fullList: [mkDevice('connecting'), mkDevice('rejected')],
+ syncedDevices: [
+ { deviceId: 'rejected', deviceName: 'Rejected', sessionCount: 2, connected: false },
+ ],
+ revoked: new Set(['rejected']),
+ }),
+ );
+ expect([...index]).toEqual([['connecting', { name: 'CONNECTING', online: false }]]);
+ });
+
+ it('removes grouping availability when the retained device is removed', () => {
+ expect(buildSidebarDeviceIndex([]).size).toBe(0);
+ expect(
+ buildSidebarDeviceIndex([{ deviceId: 'rejected', name: 'Rejected', status: 'rejected' }])
+ .size,
+ ).toBe(0);
+ });
+});
+
describe('compareDevicesByName(切换栏与设置共用,稳定身份排序)', () => {
it('按名字 localeCompare → deviceId 兜底,不含状态/在线/时间', () => {
const arr = [
diff --git a/apps/desktop/src/renderer/__tests__/machineSwitcherMenu.test.ts b/apps/desktop/src/renderer/__tests__/machineSwitcherMenu.test.ts
index 1b1538b046..865a957612 100644
--- a/apps/desktop/src/renderer/__tests__/machineSwitcherMenu.test.ts
+++ b/apps/desktop/src/renderer/__tests__/machineSwitcherMenu.test.ts
@@ -455,10 +455,8 @@ describe('远程机器切换入口并入 SidebarTopNav(置顶段上方,固定不
expect(sidebarUpperSource).toContain('hasSettledOnlineDeviceSection(');
expect(projectsSectionSource).toContain('hasRemoteDevices={deviceGroupingAvailable}');
expect(projectsSectionSource).not.toContain('hasRemoteMachines');
- expect(sidebarUpperSource).toContain("if (device.status === 'rejected') continue;");
- expect(sidebarUpperSource).toContain(
- "if (device.status === 'connecting' && isRemoteDeviceMarkedDisconnected(device.deviceId))",
- );
+ expect(sidebarUpperSource).toContain('buildSidebarDeviceIndex(switcherDevices)');
+ expect(sidebarUpperSource).not.toContain('isRemoteDeviceMarkedDisconnected');
});
// (侧边栏重设计 D 期:按日期分组段已删除,其 MachineSwitcherMenu / 空态 /
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
index 61c423edd7..fdbbebbb36 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
@@ -261,7 +261,6 @@ import {
useRemoteArchivedLoadedDeviceIds,
useRemoteArchivedLoadingDeviceIds,
useRemoteDevices,
- isRemoteDeviceMarkedDisconnected,
useRemoteProjectSessions,
} from '@/features/device-link/remoteProjectsStore';
import {
@@ -284,6 +283,7 @@ import {
useSelectedMachineConnecting,
useSwitcherDevices,
} from '@/features/device-link/useMachineSwitcher';
+import { buildSidebarDeviceIndex } from './lib/sidebarDeviceGrouping';
import {
useDeviceLinkDeviceListSettled,
useDeviceLinkDeviceListRequestState,
@@ -1309,22 +1309,10 @@ function ExpandedView({
const switcherDevices = useSwitcherDevices();
// E 期「按设备分组」:远程设备顺序 + 名称/在线状态(设备切换栏同序同源)。
// 空 map = 没有远程设备 → ProjectsSection 隐藏该分组选项、不切段。
- const remoteDeviceIndex = useMemo(() => {
- const index = new Map<string, { name: string; online: boolean }>();
- for (const device of switcherDevices) {
- if (device.status === 'rejected') continue;
- // connecting 含两种:真在连,以及断线缓存。后者不能撑开设备分组,
- // 否则最后一台远程掉线后仍会留下「本机」段头;重连后偏好自动恢复。
- if (device.status === 'connecting' && isRemoteDeviceMarkedDisconnected(device.deviceId)) {
- continue;
- }
- index.set(device.deviceId, {
- name: device.name,
- online: device.status === 'connected',
- });
- }
- return index;
- }, [switcherDevices]);
+ const remoteDeviceIndex = useMemo(
+ () => buildSidebarDeviceIndex(switcherDevices),
+ [switcherDevices],
+ );
const deviceListSettled = useDeviceLinkDeviceListSettled();
const selectedDialogueDeviceResolution = useMemo(
() => resolveDialogueDeviceTarget(selectedMachineId, switcherDevices, deviceListSettled),
diff --git a/apps/desktop/src/renderer/features/cc-agent/hooks/helpers/sidebarFilterCore.ts b/apps/desktop/src/renderer/features/cc-agent/hooks/helpers/sidebarFilterCore.ts
index 1a4a0ec5bb..62756a1be5 100644
--- a/apps/desktop/src/renderer/features/cc-agent/hooks/helpers/sidebarFilterCore.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/hooks/helpers/sidebarFilterCore.ts
@@ -359,8 +359,8 @@ export function persistGroupDialogue(groupDialogue: boolean): void {
/* ============================== groupDevice load/persist ============================== */
/**
- * 「按设备分组」开关(E 期):默认开(定稿)。仅在有远程设备连接时可见/生效
- * (与顶部设备切换栏同一出现条件);仅本机时选项隐藏、效果自然为单段。
+ * 「按设备分组」开关(E 期):默认开(定稿)。有可选远程设备(含断线缓存)时可见/生效;
+ * 没有可选远程设备时选项隐藏、效果自然为单段,不改写偏好。
*/
export function loadGroupDevice(): boolean {
const storage = safeStorage();
diff --git a/apps/desktop/src/renderer/features/cc-agent/hooks/useSidebarFilter.ts b/apps/desktop/src/renderer/features/cc-agent/hooks/useSidebarFilter.ts
index 436b652a16..a574feecb4 100644
--- a/apps/desktop/src/renderer/features/cc-agent/hooks/useSidebarFilter.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/hooks/useSidebarFilter.ts
@@ -136,7 +136,7 @@ export interface UseSidebarFilterReturn {
groupBy: FilterGroupBy;
/** 「对话归为一组」开关(D 期):true = 无项目任务收进「对话」组;默认 false 散排。 */
groupDialogue: boolean;
- /** 「按设备分组」开关(E 期):默认 true;仅有远程设备连接时可见/生效。 */
+ /** 「按设备分组」开关(E 期):默认 true;有可选远程设备(含断线缓存)时可见/生效。 */
groupDevice: boolean;
/** Sidebar 主列表任务排序。默认 'recency'。 */
sortBy: FilterSortBy;
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/sidebarDeviceGrouping.ts b/apps/desktop/src/renderer/features/cc-agent/lib/sidebarDeviceGrouping.ts
new file mode 100644
index 0000000000..e7f786962d
--- /dev/null
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/sidebarDeviceGrouping.ts
@@ -0,0 +1,16 @@
+import type { SwitcherDevice } from '../../device-link/switcherDevices';
+
+/** 分组与设备切换栏共用设备集合;断线只改变在线标记,不移除保留的设备。 */
+export function buildSidebarDeviceIndex(
+ devices: readonly SwitcherDevice[],
+): Map<string, { name: string; online: boolean }> {
+ const index = new Map<string, { name: string; online: boolean }>();
+ for (const device of devices) {
+ if (device.status === 'rejected') continue;
+ index.set(device.deviceId, {
+ name: device.name,
+ online: device.status === 'connected',
+ });
+ }
+ return index;
+}
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/SidebarFilterPopover.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/SidebarFilterPopover.tsx
index 5d53ac26e3..220a2d780e 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/SidebarFilterPopover.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/SidebarFilterPopover.tsx
@@ -197,8 +197,8 @@ export interface SidebarFilterPopoverProps {
/** 当前范围内无项目任务数,用来在项目筛选里画「对话」选项。 */
dialogueCount?: number;
/**
- * 是否有远程设备连接(E 期)。「按设备分组」与顶部设备切换栏同一出现条件:
- * 仅远程连接时显示;仅本机时该选项整行隐藏。
+ * 是否有可选的远程设备(含断线缓存)。设备仍在切换栏可选时保留分组选项,
+ * 仅剩本机或被拒设备时隐藏,不因连接状态抖动改变分组。
*/
hasRemoteDevices?: boolean;
/**
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 feb82fa611..8791df5370 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
@@ -258,7 +258,7 @@ export interface ProjectsSectionProps {
onArchiveAll: (project: ProjectNodeData) => void;
/**
* E 期「按设备分组」:远程设备的展示顺序与名称/在线状态(设备切换栏同源)。
- * null / 空数组 = 没有远程设备连接 → 设备分组选项隐藏、不切段。
+ * 含断线缓存设备;null / 空 map = 没有可选远程设备 → 设备分组选项隐藏、不切段。
*/
remoteDeviceIndex?: ReadonlyMap<string, { name: string; online: boolean }> | null;
/**
@@ -568,7 +568,7 @@ export function ProjectsSection({
// eslint-disable-next-line react-hooks/exhaustive-deps -- remoteActivityRevision 代表 getRemoteSessionActivity 读到的整表内容
}, [projects, dialogues, unclassified, bots, remoteActivityRevision]);
- // E 期「按设备分组」:有远程设备连接 + 开关开 → 按设备切段(本机在前,
+ // E 期「按设备分组」:有可选远程设备(含断线缓存)+ 开关开 → 按设备切段(本机在前,
// 远程按设备切换栏顺序);其余情况单段直渲。切段后按当前排序重排本段。
// 自定义项目顺序可以和设备分组叠加:每段内项目行按全局序的子集排,段内可拖。
// 范围收窄到单台机器时照常保留分组与整理菜单选项(2026-10-05 用户定稿,推翻
diff --git a/docs/product-rules/sidebar-redesign-plan.md b/docs/product-rules/sidebar-redesign-plan.md
index 717f620c69..b99b1b2d4e 100644
--- a/docs/product-rules/sidebar-redesign-plan.md
+++ b/docs/product-rules/sidebar-redesign-plan.md
@@ -89,7 +89,10 @@
|---|---|---|
| 按项目 | **开** | 有项目的任务收进项目行;关闭则平铺、行尾带项目小标签 |
| 按对话 | **开** | 无项目任务收进「对话」组(归属分类名,按 task-and-conversation-naming §2.3);关闭则散排。组头右侧有与项目行等位的新建入口。(2026-08-12 用户裁决改默认开,原定稿为关) |
-| 按设备 | **开** | 最外层按设备切段,分组头带在线状态、可折叠;**仅有远程设备连接时该选项才出现**;远程断开时选项消失、效果一并解除——**只解除生效,不改写偏好**(生效 = 有远程设备 ∧ 偏好开,两者分离;断线再重连后按原偏好自动恢复分组。2026-08-13 复查补记:早期原型在断开时直接把偏好写成关,是错误做法,勿照抄)。**范围收窄到单台机器时照常保留**(2026-10-05 用户定稿,推翻 2026-08-13「单机范围退场」):选项行照常可见,偏好开时仍按设备切段、挂该设备的段头,与多机范围同一套 UI |
+| 按设备 | **开** | 最外层按设备切段,分组头带在线状态、可折叠;**设备切换栏仍有可选远程设备时保留选项与分组,包括断线缓存设备**(2026-10-12 用户定稿,推翻旧「远程断开时退场」):弱网、掉线与重连只更新在线状态,不解除分组;被拒设备不计入。没有可选远程设备时才隐藏选项、解除效果,始终不改写偏好。**范围收窄到单台机器时照常保留**(2026-10-05 用户定稿,推翻 2026-08-13「单机范围退场」):选项行照常可见,偏好开时仍按设备切段、挂该设备的段头,与多机范围同一套 UI |
+
+设备集合投影见 [`buildSidebarDeviceIndex`](../../apps/desktop/src/renderer/features/cc-agent/lib/sidebarDeviceGrouping.ts),
+断线、重连与被拒设备回归见 [`machineSwitcher.test.ts`](../../apps/desktop/src/renderer/__tests__/machineSwitcher.test.ts)。
层级方向固定:设备 → 项目/对话组 → 任务。全关 = 纯平铺。