Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(worktree): never offer to remove the source worktree (#16705)

Arvin Xu982209f6dc35
diff --git a/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx b/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx
index c71923bbea6..818b5a7fcad 100644
--- a/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx
+++ b/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx
@@ -336,8 +336,15 @@ const getWorktreeBranch = (
const isDisabled = (worktree: DeviceGitWorktreeListItem): boolean =>
!!worktree.bare || !!worktree.prunable;
-const canRemoveWorktree = (worktree: DeviceGitWorktreeListItem): boolean =>
- !worktree.current && !worktree.locked && !isDisabled(worktree);
+// The main/source worktree can never be removed (`git worktree remove <main>`
+// fails with "is a main working tree"), and when the agent runs on a linked
+// worktree it is listed with `current: false` β€” so exclude it by path too, not
+// just via the `current` flag, to avoid offering a delete that always errors.
+const canRemoveWorktree = (worktree: DeviceGitWorktreeListItem, sourcePath: string): boolean =>
+ !worktree.current &&
+ !worktree.locked &&
+ !isDisabled(worktree) &&
+ normalizeDisplayPath(worktree.path) !== normalizeDisplayPath(sourcePath);
interface DirtyStatProps {
status?: DeviceGitWorktreeListItem['status'];
@@ -494,7 +501,7 @@ const WorktreeSwitcher = memo<WorktreeSwitcherProps>(
);
const displayPath = getRelativeDisplayPath(worktree.path, sourcePath);
const disabled = isDisabled(worktree);
- const removable = canRemoveWorktree(worktree);
+ const removable = canRemoveWorktree(worktree, sourcePath);
return (
<DropdownMenuItem
diff --git a/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx b/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx
index 4ecc80d8d0b..2b9c23d911b 100644
--- a/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx
+++ b/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx
@@ -230,6 +230,45 @@ describe('WorktreeSwitcher', () => {
expect(messageErrorMock).not.toHaveBeenCalled();
});
+ it('never offers to remove the source worktree even when it is not current', () => {
+ render(
+ <WorktreeSwitcher
+ isGithub
+ agentId="agent-1"
+ currentBranch="feat/current"
+ deviceId="device-1"
+ path="/repo-canary"
+ sourcePath="/repo"
+ worktrees={[
+ {
+ // The main/source worktree β€” listed as non-current because the agent
+ // runs on a linked worktree. `git worktree remove` would always fail.
+ branch: 'main',
+ current: false,
+ path: '/repo',
+ status: { added: 0, clean: true, deleted: 0, modified: 0, total: 0 },
+ },
+ {
+ branch: 'canary',
+ current: true,
+ path: '/repo-canary',
+ status: { added: 0, clean: true, deleted: 0, modified: 0, total: 0 },
+ },
+ {
+ branch: 'feature',
+ current: false,
+ path: '/repo-feature',
+ status: { added: 0, clean: true, deleted: 0, modified: 0, total: 0 },
+ },
+ ]}
+ />,
+ );
+
+ // Only the linked branch worktree is removable; the source and the current
+ // worktree are both excluded.
+ expect(screen.getAllByLabelText('workingDirectory.removeWorktreeAction')).toHaveLength(1);
+ });
+
it('commits the selected worktree path as the working directory', () => {
render(
<WorktreeSwitcher