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): 移动预检改在目标目录确定后判定,修「移到对话」被误拦

Zhang Yunjin340e7a474cb7
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
index cb9fb4da91..b034cb250e 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
@@ -222,7 +222,7 @@ import {
} from './lib/sidebarCollapseConfig';
import { getSessionListCollapseView } from './lib/sessionListCollapse';
import { hasSessionSelectionModifier, type SessionClickModifiers } from './sidebar/SessionItem';
-import { managedWorktreeRootOf } from './lib/sessionMoveGuard';
+import { crossesWorktreeBoundary } from './lib/sessionMoveGuard';
import type { SessionMoveTarget } from './sidebar/sessionMoveTarget';
import {
DIALOGUE_FILTER_KEY,
@@ -2687,17 +2687,6 @@ function ExpandedView({
toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectRemoteUnsupported'));
return;
}
- // worktree 会话的工作区就是它自己的 worktree:只改 workingDir 会造成半移动
- // (侧栏按新项目归组,聊天框底部路径仍指旧 worktree)。判定与 Main 的
- // `managedWorktreeRoot` 同口径——比归属根,同一 worktree 根内的目录调整照过;
- // 移到对话不改目录,不受影响。
- const currentWorktreeRoot = managedWorktreeRootOf(session.workingDir);
- const targetWorktreeRoot =
- target.kind === 'project' ? managedWorktreeRootOf(target.workingDir) : null;
- if (currentWorktreeRoot && currentWorktreeRoot !== targetWorktreeRoot) {
- toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectWorktreeBlocked'));
- return;
- }
if (effectiveRunningSessionIds.has(sessionId)) {
toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectRunningBlocked'));
return;
@@ -2725,6 +2714,15 @@ function ExpandedView({
}
}
+ // worktree 会话的工作区就是它自己的 worktree:只改 workingDir 会造成半移动
+ // (侧栏按新项目归组,聊天框底部路径仍指旧 worktree)。判定放在目标目录确定**之后**:
+ // browseProject 选同一 worktree 根内的子目录仍然放行,跨根才拦;口径与 Main 的
+ // `managedWorktreeRoot` 一致,「移到对话」不改目录、不经过这里。
+ if (targetWorkingDir && crossesWorktreeBoundary(session.workingDir, targetWorkingDir)) {
+ toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectWorktreeBlocked'));
+ return;
+ }
+
const oldPatch = {
workingDir: session.workingDir,
workspaceKind: session.workspaceKind,
diff --git a/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx b/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
index 25b6a65ed2..d634048596 100644
--- a/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
@@ -76,7 +76,7 @@ import {
MENU_SUB_CONTENT_CLASS,
} from './sidebar/menuStyles';
import { SessionProjectMoveSubmenu } from './sidebar/SessionProjectMoveSubmenu';
-import { managedWorktreeRootOf } from './lib/sessionMoveGuard';
+import { crossesWorktreeBoundary } from './lib/sessionMoveGuard';
import type { SessionMoveTarget } from './sidebar/sessionMoveTarget';
import { SessionShareExportDialog } from './sidebar/SessionShareExportDialog';
import { SessionBranchTreeDialog } from './SessionBranchTreeDialog';
@@ -313,15 +313,6 @@ export function SessionContentHeader({
toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectRemoteUnsupported'));
return;
}
- // 与 CCAgentSidebarUpper 同款:worktree 会话不能改到它绑定 worktree 之外的目录
- // (半移动);比归属根,同一 worktree 根内的目录调整仍放行,移到对话不改目录。
- const currentWorktreeRoot = managedWorktreeRootOf(session.workingDir);
- const targetWorktreeRoot =
- target.kind === 'project' ? managedWorktreeRootOf(target.workingDir) : null;
- if (currentWorktreeRoot && currentWorktreeRoot !== targetWorktreeRoot) {
- toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectWorktreeBlocked'));
- return;
- }
if (runningSessionIds.has(session.id)) {
toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectRunningBlocked'));
return;
@@ -363,6 +354,14 @@ export function SessionContentHeader({
}
}
+ // 与 CCAgentSidebarUpper 同款:worktree 会话不能改到它绑定 worktree 之外的目录
+ // (半移动)。同样放在目标目录确定之后,同一 worktree 根内的目录调整仍放行,
+ // 「移到对话」不改目录、不经过这里。
+ if (targetWorkingDir && crossesWorktreeBoundary(session.workingDir, targetWorkingDir)) {
+ toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectWorktreeBlocked'));
+ return;
+ }
+
const oldPatch = {
workingDir: session.workingDir,
workspaceKind: session.workspaceKind,
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/__tests__/sessionMoveGuard.test.ts b/apps/desktop/src/renderer/features/cc-agent/lib/__tests__/sessionMoveGuard.test.ts
index a5f8bee389..4b121b1fe2 100644
--- a/apps/desktop/src/renderer/features/cc-agent/lib/__tests__/sessionMoveGuard.test.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/__tests__/sessionMoveGuard.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { managedWorktreeRootOf } from '../sessionMoveGuard';
+import { crossesWorktreeBoundary, managedWorktreeRootOf } from '../sessionMoveGuard';
describe('managedWorktreeRootOf', () => {
it('folds roots and their descendants to the same managed worktree root', () => {
@@ -30,3 +30,26 @@ describe('managedWorktreeRootOf', () => {
expect(managedWorktreeRootOf('')).toBeNull();
});
});
+
+describe('crossesWorktreeBoundary', () => {
+ const worktree = '/repo/.cindy-worktrees/steady-goodall';
+
+ it('blocks only targets outside the session\'s own worktree root', () => {
+ // 跨到别的项目(含它所属的 base repo)→ 拦。
+ expect(crossesWorktreeBoundary(worktree, '/repo/other-project')).toBe(true);
+ expect(crossesWorktreeBoundary(worktree, '/repo')).toBe(true);
+ expect(crossesWorktreeBoundary(worktree, '/repo/.cindy-worktrees/another-name')).toBe(true);
+ });
+
+ it('allows targets inside the same worktree root', () => {
+ expect(crossesWorktreeBoundary(worktree, worktree)).toBe(false);
+ expect(crossesWorktreeBoundary(worktree, `${worktree}/src`)).toBe(false);
+ expect(crossesWorktreeBoundary(`${worktree}/src`, `${worktree}/tests`)).toBe(false);
+ });
+
+ it('never blocks sessions that are not bound to a managed worktree', () => {
+ expect(crossesWorktreeBoundary('/repo', '/elsewhere')).toBe(false);
+ expect(crossesWorktreeBoundary(null, '/elsewhere')).toBe(false);
+ expect(crossesWorktreeBoundary('/repo/.worktrees/user-made', '/elsewhere')).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/sessionMoveGuard.ts b/apps/desktop/src/renderer/features/cc-agent/lib/sessionMoveGuard.ts
index e534542063..658939f822 100644
--- a/apps/desktop/src/renderer/features/cc-agent/lib/sessionMoveGuard.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/sessionMoveGuard.ts
@@ -50,3 +50,19 @@ export function managedWorktreeRootOf(value: string | null | undefined): string
if (segments.length < 2) return null;
return value.slice(0, segments[1].end);
}
+
+/**
+ * 该会话改到某个**已解析出的项目目录**时,是否跨出了它的 worktree 归属。
+ *
+ * 只对具体目录比较,不看 target 种类——调用方必须在目标目录确定之后调用
+ * (browseProject 要等选完目录);目标目录为空的情形(尤其是「移到对话」)
+ * 根本不改 workingDir,不要拿到这里。
+ */
+export function crossesWorktreeBoundary(
+ currentWorkingDir: string | null | undefined,
+ targetWorkingDir: string,
+): boolean {
+ const currentRoot = managedWorktreeRootOf(currentWorkingDir);
+ if (currentRoot === null) return false;
+ return currentRoot !== managedWorktreeRootOf(targetWorkingDir);
+}