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): 任务浏览后退/前进的三处边界(远端删除 / 编辑器按键 / 窄侧栏命中区)

WZBbiao7f531f8fb9e0
diff --git a/apps/desktop/src/renderer/__tests__/remoteProjectsStore.test.ts b/apps/desktop/src/renderer/__tests__/remoteProjectsStore.test.ts
index c387de7be6..0c74d4166c 100644
--- a/apps/desktop/src/renderer/__tests__/remoteProjectsStore.test.ts
+++ b/apps/desktop/src/renderer/__tests__/remoteProjectsStore.test.ts
@@ -18,6 +18,7 @@ import {
requestRemoteSessionStatus,
setRemoteReseedImpl,
setRemoteSessionBootstrapRetryImpl,
+ subscribeRemoteSessionRemoval,
} from '@/features/device-link/remoteProjectsStore';
import type { Session } from '@/lib/ccAgent.types';
@@ -403,6 +404,22 @@ describe('remoteProjectsStore', () => {
expect(getSessionDeviceId('s2')).toBe('dev-B');
});
+ it('通知只在意 sessionId 的旁路状态,包含不在任何分片里的会话', () => {
+ const removed: string[] = [];
+ const off = subscribeRemoteSessionRemoval((id) => removed.push(id));
+ remoteProjectsStore.setDeviceSessions('dev-B', 'B', [mk('s1')]);
+
+ remoteProjectsStore.applyPatch('dev-B', 's1', { status: 'deleted' });
+ // 不在任何分片里的会话(分片有界/设备早已移除)也得报:窗口里可能还挂着它的路由。
+ remoteProjectsStore.applyPatch('dev-gone', 's9', { status: 'deleted' });
+ // 非删除的 patch 不是消失。
+ remoteProjectsStore.applyPatch('dev-B', 's2', { status: 'archived' });
+
+ off();
+ remoteProjectsStore.applyPatch('dev-B', 's3', { status: 'deleted' });
+ expect(removed).toEqual(['s1', 's9']);
+ });
+
it('unpin 后触发 reseed,让仅因 includePinned 补入的旧会话被后续 snapshot 剔除', () => {
const reseed = vi.fn();
setRemoteReseedImpl(reseed);
diff --git a/apps/desktop/src/renderer/components/layout/ContentHeader.tsx b/apps/desktop/src/renderer/components/layout/ContentHeader.tsx
index c04a85e028..22463cf5f9 100644
--- a/apps/desktop/src/renderer/components/layout/ContentHeader.tsx
+++ b/apps/desktop/src/renderer/components/layout/ContentHeader.tsx
@@ -27,7 +27,11 @@ import { createContext, useContext, type ReactNode } from 'react';
import { useFeatureContentHeader } from '@/features/feature-context';
import { useMacFullscreen } from '@/hooks/useMacFullscreen';
import { cn } from '@/lib/utils';
-import { CHROME_ACTIONS_GEOMETRY } from './chromeActionsGeometry';
+import {
+ CHROME_ACTIONS_GEOMETRY,
+ CHROME_ACTIONS_MAC_RIGHT_EDGE,
+ chromeActionsHitHoleWidth,
+} from './chromeActionsGeometry';
export const CONTENT_HEADER_HEIGHT_PX = 46;
@@ -75,6 +79,9 @@ interface ContentHeaderProps {
showCollapsedActions: boolean;
/** 左侧栏是否处于 rail 态(拖到最窄但仍保留图标列)。 */
isSidebarRail: boolean;
+ /** 展开态侧栏宽度(窗口像素)。与按钮簇的几何一起决定本 header 要补挖多宽的
+ * no-drag 区:侧栏窄于簇右缘时它的洞会被自身 overflow-hidden 裁掉。 */
+ sidebarWidth?: number;
/** mac 右上固定唤起按钮是否在场。mac 据此在右端留按钮占位并决定是否留住空
* header;Windows 仅影响空 header 判定,
* 占位恒为窗口控制按钮宽。 */
@@ -89,6 +96,7 @@ export function ContentHeader({
sidebarVisible,
showCollapsedActions,
isSidebarRail,
+ sidebarWidth,
rightSidebarAvailable,
hidden,
trailing,
@@ -104,7 +112,16 @@ export function ContentHeader({
// main 的 drag header 左缘再挖一个洞,否则点击浮动的收放/菜单按钮会被 Electron
// 当成窗口拖拽吞掉。完全收起态 main 从 x=0 开始,既有 spacer 已覆盖该位置;
// 全屏态按钮回到 rail 内,也由 Sidebar 的既有洞覆盖。
- const needsRailChromeActionsHitHole = isMac && !isFullscreen && isSidebarRail;
+ // 展开态同理但只挖溢出那一截:侧栏可拖到 180(useSidebarResize 的下限),比簇右缘
+ // 202 窄,超出侧栏右缘的按钮同样压在 header 拖拽区上。
+ const chromeActionsHitHole =
+ isMac && !isFullscreen && (isSidebarRail || sidebarVisible)
+ ? chromeActionsHitHoleWidth(
+ isSidebarRail
+ ? CHROME_ACTIONS_GEOMETRY.macTrafficLightLeft
+ : (sidebarWidth ?? CHROME_ACTIONS_MAC_RIGHT_EDGE),
+ )
+ : 0;
// 设置页:Sidebar 隐藏且无折叠按钮占位 → header 退化为"隐形 chrome"
// (仅拖拽区 + Windows 窗口控制),不画下边框。
@@ -148,14 +165,14 @@ export function ContentHeader({
// 36px Tab 条那层(在面板身上,语义准)。
style={{ WebkitAppRegion: 'drag' } as React.CSSProperties}
>
- {needsRailChromeActionsHitHole && (
+ {chromeActionsHitHole > 0 && (
<div
aria-hidden
data-testid="content-header-rail-chrome-actions-hit-hole"
className="absolute left-0 top-0 h-full"
style={
{
- width: CHROME_ACTIONS_GEOMETRY.clusterWidth,
+ width: chromeActionsHitHole,
WebkitAppRegion: 'no-drag',
} as React.CSSProperties
}
@@ -215,6 +232,7 @@ export function ContentHeaderSlot({
sidebarVisible,
showCollapsedActions,
isSidebarRail,
+ sidebarWidth,
rightSidebarAvailable,
trailing,
children,
@@ -226,6 +244,7 @@ export function ContentHeaderSlot({
sidebarVisible={sidebarVisible}
showCollapsedActions={showCollapsedActions}
isSidebarRail={isSidebarRail}
+ sidebarWidth={sidebarWidth}
rightSidebarAvailable={rightSidebarAvailable}
hidden={hidden}
trailing={trailing}
diff --git a/apps/desktop/src/renderer/components/layout/MainLayout.tsx b/apps/desktop/src/renderer/components/layout/MainLayout.tsx
index a856ff16c3..1350378c32 100644
--- a/apps/desktop/src/renderer/components/layout/MainLayout.tsx
+++ b/apps/desktop/src/renderer/components/layout/MainLayout.tsx
@@ -1749,6 +1749,9 @@ export function MainLayout() {
sidebarVisible={!isSettingsRoute && !isSidebarCollapsed && !isRailMode}
showCollapsedActions={!isSettingsRoute && (isSidebarCollapsed || isRailMode)}
isSidebarRail={hasRailChromeActions && !rightSidebarOwnsRailChromeActions}
+ // 展开态侧栏可拖到 180(比按钮簇右缘 202 窄):那一段的按钮悬在 header
+ // 拖拽区上,由 ContentHeader 按侧栏宽补挖 no-drag。
+ sidebarWidth={sidebarWidth}
// M2(2026-07-09 口径修订):mac 右上浮层随面板在场常驻(折叠
// toggle 永远钉窗口右上角),ContentHeader 右端占位不再分侧别。
rightSidebarAvailable={rightSidebarAvailable}
diff --git a/apps/desktop/src/renderer/components/layout/__tests__/ChromeActions.fullscreen.test.tsx b/apps/desktop/src/renderer/components/layout/__tests__/ChromeActions.fullscreen.test.tsx
index b73f811d88..e306726b6a 100644
--- a/apps/desktop/src/renderer/components/layout/__tests__/ChromeActions.fullscreen.test.tsx
+++ b/apps/desktop/src/renderer/components/layout/__tests__/ChromeActions.fullscreen.test.tsx
@@ -39,6 +39,24 @@ const sessionPatches = vi.hoisted(() => {
};
});
+/** 被控端任务删除的来源(它们不经过本机 sessionsStore)。 */
+const remoteRemovals = vi.hoisted(() => {
+ type Listener = (sessionId: string) => void;
+ const listeners = new Set<Listener>();
+ return {
+ emit(sessionId: string) {
+ for (const listener of listeners) listener(sessionId);
+ },
+ subscribe(listener: Listener) {
+ listeners.add(listener);
+ return () => listeners.delete(listener);
+ },
+ clear() {
+ listeners.clear();
+ },
+ };
+});
+
vi.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));
@@ -51,6 +69,9 @@ vi.mock('@/contexts/AuthContext', () => ({
vi.mock('@/lib/sessionsStore', () => ({
sessionsStore: { subscribePatches: sessionPatches.subscribePatches },
}));
+vi.mock('@/features/device-link/remoteProjectsStore', () => ({
+ subscribeRemoteSessionRemoval: remoteRemovals.subscribe,
+}));
vi.mock('@/components/ui/dropdown-menu', () => ({
DropdownMenu: ({ children }: { children: ReactNode }) => <>{children}</>,
DropdownMenuContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
@@ -80,6 +101,7 @@ afterEach(() => {
fullscreenState.isMac = true;
fullscreenState.isFullscreen = true;
sessionPatches.clear();
+ remoteRemovals.clear();
delete (window as Partial<Window>).electronAPI;
});
@@ -145,6 +167,10 @@ function Harness({ history }: { history: SessionBrowseHistory }): React.ReactEle
<OpenTask id="b" />
<OpenTask id="c" />
<OpenDraft />
+ {/* CodeMirror 文件编辑器的真实 DOM 形状(FileBodyView 的 PlaintextEditor)。 */}
+ <div className="cm-editor">
+ <div className="cm-content" contentEditable suppressContentEditableWarning />
+ </div>
</>
);
}
@@ -166,14 +192,18 @@ function defaultCombo(id: AppShortcutId) {
return { platform, combo };
}
-function press(combo: { code: string; meta: boolean; ctrl: boolean; alt: boolean; shift: boolean }) {
- fireEvent.keyDown(window, {
+function keyInit(combo: { code: string; meta: boolean; ctrl: boolean; alt: boolean; shift: boolean }) {
+ return {
code: combo.code,
metaKey: combo.meta,
ctrlKey: combo.ctrl,
altKey: combo.alt,
shiftKey: combo.shift,
- });
+ };
+}
+
+function press(combo: Parameters<typeof keyInit>[0]) {
+ fireEvent.keyDown(window, keyInit(combo));
}
describe('session browse history buttons', () => {
@@ -258,6 +288,22 @@ describe('session browse history buttons', () => {
expect(screen.getByTestId('path').textContent).toBe('/cc-agent/b');
});
+ it('forgets a task deleted on the device it runs on', async () => {
+ const user = userEvent.setup();
+ renderAt('/cc-agent/a');
+
+ await user.click(screen.getByRole('button', { name: 'open b' }));
+ // 被控端任务不走 sessionsStore 广播,只会报一条「已消失」。
+ act(() => remoteRemovals.emit('a'));
+
+ await waitFor(() =>
+ expect(
+ screen.getByRole('button', { name: 'titleBar.historyBackUnavailable' }),
+ ).toBeTruthy(),
+ );
+ expect(screen.getByTestId('path').textContent).toBe('/cc-agent/b');
+ });
+
it('names the shortcut in the tooltip', async () => {
const user = userEvent.setup();
renderAt('/cc-agent/a');
@@ -282,4 +328,19 @@ describe('session browse history buttons', () => {
press(defaultCombo('session-history-forward').combo);
await waitFor(() => expect(screen.getByTestId('path').textContent).toBe('/cc-agent/b'));
});
+
+ it('leaves the bracket shortcut to the file editor that owns it', async () => {
+ renderAt('/cc-agent/a');
+ fireEvent.click(screen.getByRole('button', { name: 'open b' }));
+ expect(screen.getByTestId('path').textContent).toBe('/cc-agent/b');
+
+ // 焦点在 CodeMirror 里 → Mod-[ / Mod-] 是缩进命令,不能拿去导航(会丢掉刚改的内容)。
+ const editorContent = document.querySelector('.cm-content') as HTMLElement;
+ fireEvent.keyDown(editorContent, keyInit(defaultCombo('session-history-back').combo));
+ expect(screen.getByTestId('path').textContent).toBe('/cc-agent/b');
+
+ // 焦点回到普通页面 → 同一个组合照旧导航。
+ press(defaultCombo('session-history-back').combo);
+ await waitFor(() => expect(screen.getByTestId('path').textContent).toBe('/cc-agent/a'));
+ });
});
diff --git a/apps/desktop/src/renderer/components/layout/__tests__/ContentHeader.chrome-actions.test.tsx b/apps/desktop/src/renderer/components/layout/__tests__/ContentHeader.chrome-actions.test.tsx
index c81fee8eef..1660fb3fce 100644
--- a/apps/desktop/src/renderer/components/layout/__tests__/ContentHeader.chrome-actions.test.tsx
+++ b/apps/desktop/src/renderer/components/layout/__tests__/ContentHeader.chrome-actions.test.tsx
@@ -53,4 +53,42 @@ describe('ContentHeader rail ChromeActions hit hole', () => {
expect(screen.queryByTestId('content-header-rail-chrome-actions-hit-hole')).toBeNull();
});
+
+ it('carves exactly the part of the cluster that a narrow sidebar cannot cover', () => {
+ render(
+ <FeatureSidebarSlotProvider isCollapsed>
+ <ContentHeader
+ sidebarVisible
+ showCollapsedActions={false}
+ isSidebarRail={false}
+ // 侧栏可拖到 180:比簇右缘(mac 非全屏 78 + 124 = 202)窄,超出的一段要补挖。
+ sidebarWidth={180}
+ rightSidebarAvailable={false}
+ hidden={false}
+ />
+ </FeatureSidebarSlotProvider>,
+ );
+
+ const hitHole = screen.getByTestId('content-header-rail-chrome-actions-hit-hole');
+ expect(hitHole.style.width).toBe(
+ `${CHROME_ACTIONS_GEOMETRY.macTrafficLightLeft + CHROME_ACTIONS_GEOMETRY.clusterWidth - 180}px`,
+ );
+ });
+
+ it('leaves the header drag area alone when the sidebar covers the whole cluster', () => {
+ render(
+ <FeatureSidebarSlotProvider isCollapsed>
+ <ContentHeader
+ sidebarVisible
+ showCollapsedActions={false}
+ isSidebarRail={false}
+ sidebarWidth={260}
+ rightSidebarAvailable={false}
+ hidden={false}
+ />
+ </FeatureSidebarSlotProvider>,
+ );
+
+ expect(screen.queryByTestId('content-header-rail-chrome-actions-hit-hole')).toBeNull();
+ });
});
diff --git a/apps/desktop/src/renderer/components/layout/chromeActionsGeometry.ts b/apps/desktop/src/renderer/components/layout/chromeActionsGeometry.ts
index 7ce61dfe8a..ad2cc03b20 100644
--- a/apps/desktop/src/renderer/components/layout/chromeActionsGeometry.ts
+++ b/apps/desktop/src/renderer/components/layout/chromeActionsGeometry.ts
@@ -16,3 +16,21 @@ export const CHROME_ACTIONS_GEOMETRY = {
*/
clusterWidth: 124,
} as const;
+
+/** macOS 非全屏时浮动按钮簇的右缘(窗口坐标)。 */
+export const CHROME_ACTIONS_MAC_RIGHT_EDGE =
+ CHROME_ACTIONS_GEOMETRY.macTrafficLightLeft + CHROME_ACTIONS_GEOMETRY.clusterWidth;
+
+/**
+ * 左侧面板顶栏必须为浮动按钮簇补挖的 no-drag 宽度。
+ *
+ * 按钮簇钉死在窗口左上(mac 非全屏 x=78),而面板顶栏里的洞会被面板自己的
+ * overflow-hidden 按面板宽度裁掉。面板宽度小于簇右缘时(如侧栏拖到 180 的展开
+ * 最小宽),簇的右半截就悬在右侧顶栏的拖拽区上 —— 点前进会被当成拖窗口。这里
+ * 算出「超出面板右缘的那一截」,由右侧顶栏补挖;面板够宽(>= 簇右缘)或簇整个
+ * 落在面板外(rail 宽 78)时退回边界值。
+ */
+export function chromeActionsHitHoleWidth(panelRightEdge: number): number {
+ const overflow = CHROME_ACTIONS_MAC_RIGHT_EDGE - panelRightEdge;
+ return Math.min(CHROME_ACTIONS_GEOMETRY.clusterWidth, Math.max(0, overflow));
+}
diff --git a/apps/desktop/src/renderer/features/cc-agent/useSessionBrowseHistory.ts b/apps/desktop/src/renderer/features/cc-agent/useSessionBrowseHistory.ts
index 9a21bb0530..aeeb0000c4 100644
--- a/apps/desktop/src/renderer/features/cc-agent/useSessionBrowseHistory.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/useSessionBrowseHistory.ts
@@ -14,6 +14,7 @@
import { useCallback, useEffect, useRef, useSyncExternalStore } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
+import { subscribeRemoteSessionRemoval } from '@/features/device-link/remoteProjectsStore';
import { useAppShortcut } from '@/hooks/useAppShortcut';
import { sessionsStore } from '@/lib/sessionsStore';
import {
@@ -59,9 +60,16 @@ export function useSessionBrowseHistory(
useEffect(() => {
// 任务被删除时(本窗口或其它窗口的广播都走 sessionsStore.patchLocal)立即摘掉,
// 后退不会把用户送到一个已经打不开的任务上。
- return sessionsStore.subscribePatches((id, patch) => {
+ const offLocal = sessionsStore.subscribePatches((id, patch) => {
if (patch.status === 'deleted') history.forget(id);
});
+ // 被控端的任务不经过本机 sessionsStore:它们的生命周期只走 remoteProjectsStore
+ // 的 applyPatch,删除得再单独订一份,否则远端删掉的任务会留在栈里当死目标。
+ const offRemote = subscribeRemoteSessionRemoval((id) => history.forget(id));
+ return () => {
+ offLocal();
+ offRemote();
+ };
}, [history]);
useEffect(() => {
@@ -108,13 +116,18 @@ export function useSessionBrowseHistory(
const goBack = useCallback(() => go(-1), [go]);
const goForward = useCallback(() => go(1), [go]);
- // Windows / Linux 上 mod+[ 在终端里是 ESC(vim / readline):焦点在终端时让给 PTY,
- // 不做窗口级导航。mac 的 ⌘[ 在终端无原生含义,同一道门一并让路无副作用。
- const outsideTerminal = (event: KeyboardEvent) =>
- !(event.target instanceof Element && event.target.closest('.xterm'));
+ // Windows / Linux 上 mod+[ 在终端里是 ESC(vim / readline)、在 CodeMirror 编辑器里是
+ // 缩减/增加缩进(defaultKeymap 的 Mod-[ / Mod-]):焦点落在这两类编辑器内时让给它们,
+ // 不做窗口级导航(navigation 会丢掉编辑器里刚改的内容)。mac 的 ⌘[ / ⌘] 在两者中无
+ // 原生含义,同一道门一并让路无副作用。
+ const outsideOwnedKeyTargets = (event: KeyboardEvent) =>
+ !(event.target instanceof Element && event.target.closest('.xterm, .cm-editor'));
- useAppShortcut('session-history-back', (event) => outsideTerminal(event) && goBack());
- useAppShortcut('session-history-forward', (event) => outsideTerminal(event) && goForward());
+ useAppShortcut('session-history-back', (event) => outsideOwnedKeyTargets(event) && goBack());
+ useAppShortcut(
+ 'session-history-forward',
+ (event) => outsideOwnedKeyTargets(event) && goForward(),
+ );
return {
canBack: onStack ? index > 0 : index >= 0,
diff --git a/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts b/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts
index e025b0e86c..183c59a9aa 100644
--- a/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts
+++ b/apps/desktop/src/renderer/features/device-link/remoteProjectsStore.ts
@@ -97,6 +97,16 @@ let archivedFailedDeviceIds: ReadonlySet<string> = new Set();
* 对齐缓存名,避免下一轮拉取用旧名回填。这与「会话真相」无关,是设备元数据的即时性补丁。
*/
const renameSubs = new Set<(deviceId: string, name: string) => void>();
+/**
+ * 远端会话「已消失」通知订阅(sessionId)。被控端删除任务、设备被移除这两条路径都不经过本机
+ * `sessionsStore`(它们的生命周期只走 `applyPatch`),而窗口内拿着 sessionId 的旁路状态
+ * (如任务浏览栈)必须能跟着摘掉死目标,否则后退会把用户送到一个已经打不开的任务上。
+ */
+const removedSubs = new Set<(sessionId: string) => void>();
+
+function emitRemoved(sessionId: string): void {
+ for (const fn of removedSubs) fn(sessionId);
+}
/**
* snapshot epoch(per-device):防「旧 snapshot 覆盖新 snapshot」。listing tier 发起一次
@@ -842,6 +852,9 @@ const actions = {
// 「别的控制端刚删掉的会话」的正文一直留在盘上,直到 LRU 逐出 / 设备移除 / 登出
// (review: codex P1)。归档仍可从 Archived / All 打开,必须保留缓存供离线查看。
if (deleted) clearCachedMessages(deviceId, sessionId);
+ // 浏览栈等旁路状态只看得到 sessionId:删除通知必须发在下面所有早退之前,会话不在
+ // 当前分片时也要能收到(同一行的消息缓存清理已出于同样理由前置)。
+ if (deleted) emitRemoved(sessionId);
// 状态迁移叠加层:删除一律让位。active / archived 推送不带 updatedAt,可能是写库前
// 迟到的旧推送,不动叠加层,交给写库后的列表按内容确认(见 pendingStatuses)。
if (deleted && pendingStatuses.get(sessionId)?.token.deviceId === deviceId) {
@@ -1377,6 +1390,17 @@ function subscribeRename(fn: (deviceId: string, name: string) => void): () => vo
};
}
+/**
+ * 订阅「远端会话已消失」(被控端删除任务 / 设备移除)。与 `subscribe` 不同,它只报消失的
+ * sessionId,不覆盖其它变更 —— 旁路状态拿不到本机 sessions 的 patch 广播,只能靠这条。
+ */
+export function subscribeRemoteSessionRemoval(fn: (sessionId: string) => void): () => void {
+ removedSubs.add(fn);
+ return () => {
+ removedSubs.delete(fn);
+ };
+}
+
/** listing tier 挂载时注册「重拉某设备」的实现;卸载时传 null 清。 */
export function setRemoteReseedImpl(
fn: ((deviceId: string, status: RemoteSessionStatus) => void) | null,
@@ -1472,7 +1496,12 @@ export function useRemoteBootstrapLoadingDeviceIds(): ReadonlySet<string> {
}
/** 非组件上下文(presence 订阅器 / 传输层)读写入口。 */
-export const remoteProjectsStore = { ...actions, subscribe, subscribeRename };
+export const remoteProjectsStore = {
+ ...actions,
+ subscribe,
+ subscribeRename,
+ subscribeRemoved: subscribeRemoteSessionRemoval,
+};
/** 便捷导出:传输层只关心 origin 判定。 */
export function getSessionDeviceId(sessionId: string): string | undefined {