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): GUI 预检改用活 worktree 绑定,与共享写路径同源

Zhang Yunjin7d2ce5aca399
diff --git a/apps/desktop/src/renderer/contexts/WorktreeContext.tsx b/apps/desktop/src/renderer/contexts/WorktreeContext.tsx
index 2b33e2b6c9..02281d042a 100644
--- a/apps/desktop/src/renderer/contexts/WorktreeContext.tsx
+++ b/apps/desktop/src/renderer/contexts/WorktreeContext.tsx
@@ -191,6 +191,15 @@ export function useReportWorktreeLiveness(): WorktreeContextValue['reportLivenes
return useCtx().reportLiveness;
}
+/**
+ * store 原样的绑定表(含失效目录)。判「这条任务是否还持有 worktree」用它 ——
+ * 与 main 的 worktreeStore 同口径:只要还登记着就是持有,目录是否失效是另一
+ * 个问题(那由徽标的 liveness 口径处理)。
+ */
+export function useRawWorktrees(): Record<string, WorktreeMeta> {
+ return useCtx().rawMetas;
+}
+
/** 让创建/恢复等明确知道 sessionId 的调用方只刷新对应 worktree。 */
export function useRefreshWorktreeForSession(): (sessionId: string) => Promise<void> {
return useCtx().refreshSession;
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
index b034cb250e..d8722c6010 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
@@ -49,6 +49,7 @@ import { projectDraftSessionTitle } from '@cindy/maker-shared/session-title';
import { cn } from '@/lib/utils';
import { toast } from '@/lib/toast';
import { isDataOwnerPushCurrent } from '@/contexts/dataOwnerGeneration';
+import { useRawWorktrees } from '@/contexts/WorktreeContext';
import { useCCSessions } from '@/hooks/useCCSessions';
import { useRecentWorkdirs } from '@/hooks/useRecentWorkdirs';
import { refreshPendingAlerts } from '@/hooks/usePendingAlertAttention';
@@ -828,6 +829,9 @@ function ExpandedView({
}: ExpandedProps) {
const { t, i18n } = useTranslation();
const localPlatform = window.electronAPI.platform;
+ // worktree 归属取 store 镜像(与 main 的 worktreeStore 同源):回收后仍留历史路径、
+ // 或存量半移动行的会话,不能靠 cwd 猜归属。
+ const worktreeBindings = useRawWorktrees();
const { sessions, refreshSessions, patchLocal, effectiveIncludeArchived } = sessionsHook;
const {
hiddenProjectKeys,
@@ -2714,11 +2718,12 @@ function ExpandedView({
}
}
- // worktree 会话的工作区就是它自己的 worktree:只改 workingDir 会造成半移动
+ // worktree 会话的工作区就是它绑定的 worktree:只改 workingDir 会造成半移动
// (侧栏按新项目归组,聊天框底部路径仍指旧 worktree)。判定放在目标目录确定**之后**:
- // browseProject 选同一 worktree 根内的子目录仍然放行,跨根才拦;口径与 Main 的
- // `managedWorktreeRoot` 一致,「移到对话」不改目录、不经过这里。
- if (targetWorkingDir && crossesWorktreeBoundary(session.workingDir, targetWorkingDir)) {
+ // browseProject 选同一 worktree 根内的子目录仍然放行,跨根才拦;归属取活绑定
+ // (worktreeStore 镜像),口径与 Main 的守卫一致;「移到对话」不改目录、不经过这里。
+ const boundWorktreePath = worktreeBindings[sessionId]?.path ?? null;
+ if (targetWorkingDir && crossesWorktreeBoundary(boundWorktreePath, targetWorkingDir)) {
toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectWorktreeBlocked'));
return;
}
@@ -2779,6 +2784,7 @@ function ExpandedView({
effectiveRunningSessionIds,
patchLocal,
t,
+ worktreeBindings,
],
);
diff --git a/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx b/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
index d634048596..a7418ae117 100644
--- a/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
@@ -77,6 +77,7 @@ import {
} from './sidebar/menuStyles';
import { SessionProjectMoveSubmenu } from './sidebar/SessionProjectMoveSubmenu';
import { crossesWorktreeBoundary } from './lib/sessionMoveGuard';
+import { useWorktreeForSession } from '@/contexts/WorktreeContext';
import type { SessionMoveTarget } from './sidebar/sessionMoveTarget';
import { SessionShareExportDialog } from './sidebar/SessionShareExportDialog';
import { SessionBranchTreeDialog } from './SessionBranchTreeDialog';
@@ -149,6 +150,9 @@ export function SessionContentHeader({
// active 桶里的会话。
const session = routeSessionById.get(sessionProp.id) ?? sessionProp;
const { runningSessionIds } = useSessionRunningStatus(session.id);
+ // worktree 归属取 store 镜像(与 main 的 worktreeStore 同源):回收后仍留历史路径、
+ // 或存量半移动行的会话,不能靠 cwd 猜归属。
+ const worktreeBinding = useWorktreeForSession(session.id, { includeInvalid: true });
const { confirm: confirmDialog } = useConfirmDialog();
const { runSessionAction, unarchiveSession } = useSessionLifecycleActions();
@@ -356,8 +360,9 @@ export function SessionContentHeader({
// 与 CCAgentSidebarUpper 同款:worktree 会话不能改到它绑定 worktree 之外的目录
// (半移动)。同样放在目标目录确定之后,同一 worktree 根内的目录调整仍放行,
+ // 归属取活绑定;
// 「移到对话」不改目录、不经过这里。
- if (targetWorkingDir && crossesWorktreeBoundary(session.workingDir, targetWorkingDir)) {
+ if (targetWorkingDir && crossesWorktreeBoundary(worktreeBinding?.path ?? null, targetWorkingDir)) {
toast.warning(t('ccAgent.sidebar.sessionMenu.moveToProjectWorktreeBlocked'));
return;
}
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 7e7d73010f..01f359c846 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
@@ -44,12 +44,15 @@ describe('crossesWorktreeBoundary', () => {
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);
+ it('never blocks sessions without a live worktree binding', () => {
+ // 没有绑定(普通任务,或 store 里已回收掉登记的会话)一律放行——归属真源是
+ // worktreeStore,cwd 只是历史路径,不能反过来把它拦死。
expect(crossesWorktreeBoundary(null, '/elsewhere')).toBe(false);
+ expect(crossesWorktreeBoundary(undefined, '/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 603ecb94e1..02629cf049 100644
--- a/apps/desktop/src/renderer/features/cc-agent/lib/sessionMoveGuard.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/sessionMoveGuard.ts
@@ -61,20 +61,25 @@ export function managedWorktreeRootOf(value: string | null | undefined): string
}
/**
- * 该会话改到某个**已解析出的项目目录**时,是否跨出了它的 worktree 归属。
+ * 该会话改到某个**已解析出的项目目录**时,是否跨出了它的 worktree 归属。
*
- * 只对具体目录比较,不看 target 种类——调用方必须在目标目录确定之后调用
- * (browseProject 要等选完目录);目标目录为空的情形(尤其是「移到对话」)
- * 根本不改 workingDir,不要拿到这里。
+ * 第一个参数是会话**当前活着的 worktree 绑定路径**(renderer 侧取自
+ * WorktreeContext 的 store 镜像,与 main 的 worktreeStore 同源),不是当前 cwd ——
+ * worktree 已被回收的会话会留着历史 worktree 路径,拿 cwd 当归属会把它永久拦死;
+ * 存量半移动行则相反(cwd 在普通项目、绑定仍在旧 worktree),必须按绑定拦。
+ *
+ * 只对具体目录比较,不看 target 种类——调用方必须在目标目录确定之后调用
+ * (browseProject 要等选完目录);目标目录为空的情形(尤其是「移到对话」)
+ * 根本不改 workingDir,不要拿到这里。
*/
export function crossesWorktreeBoundary(
- currentWorkingDir: string | null | undefined,
+ boundWorktreePath: string | null | undefined,
targetWorkingDir: string,
): boolean {
- const currentRoot = managedWorktreeRootOf(currentWorkingDir);
- if (currentRoot === null) return false;
+ const ownedRoot = managedWorktreeRootOf(boundWorktreePath);
+ if (ownedRoot === null) return false;
const targetRoot = managedWorktreeRootOf(targetWorkingDir);
// 比的是同一物理目录:分隔符风格(目录选择器给 `\`、库里存 `/`)与 Windows 盘符/
// UNC 大小写差异都不算跨根,比较口径与 Main 的守卫一致。
- return targetRoot === null || !workingDirEquals(currentRoot, targetRoot, { windows: isWindowsHost() });
+ return targetRoot === null || !workingDirEquals(ownedRoot, targetRoot, { windows: isWindowsHost() });
}