🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(chat): worktree directory label shows source repo + removal toast (#16856)
Arvin Xu828d47bfbf2e
diff --git a/locales/en-US/device.json b/locales/en-US/device.jsonindex c67705b0491..243a2f052e2 100644--- a/locales/en-US/device.json+++ b/locales/en-US/device.json@@ -69,6 +69,7 @@"workingDirectory.removeWorktreeAction": "Delete worktree","workingDirectory.removeWorktreeConfirm": "Delete worktree “{{name}}”? This removes the worktree directory but keeps its branch. Git will refuse if it has uncommitted changes.","workingDirectory.removeWorktreeFailed": "Delete worktree failed",+ "workingDirectory.removeWorktreePending": "Deleting worktree “{{name}}”…","workingDirectory.removeWorktreeSuccess": "Worktree deleted","workingDirectory.removeWorktreeTitle": "Delete worktree","workingDirectory.removed": "Removed “{{name}}”",diff --git a/locales/zh-CN/device.json b/locales/zh-CN/device.jsonindex 2b8cd06d885..02ee24812f9 100644--- a/locales/zh-CN/device.json+++ b/locales/zh-CN/device.json@@ -69,6 +69,7 @@"workingDirectory.removeWorktreeAction": "删除 Worktree","workingDirectory.removeWorktreeConfirm": "删除 Worktree“{{name}}”?这会移除该 worktree 目录,但保留其分支;如果仍有未提交更改,Git 会拒绝删除。","workingDirectory.removeWorktreeFailed": "删除 Worktree 失败",+ "workingDirectory.removeWorktreePending": "正在删除 Worktree“{{name}}”…","workingDirectory.removeWorktreeSuccess": "Worktree 已删除","workingDirectory.removeWorktreeTitle": "删除 Worktree","workingDirectory.removed": "已移除「{{name}}」",diff --git a/packages/locales/src/default/device.ts b/packages/locales/src/default/device.tsindex c01b7a5665a..cea18614951 100644--- a/packages/locales/src/default/device.ts+++ b/packages/locales/src/default/device.ts@@ -90,6 +90,7 @@ export default {'workingDirectory.removeWorktreeConfirm':'Delete worktree “{{name}}”? This removes the worktree directory but keeps its branch. Git will refuse if it has uncommitted changes.','workingDirectory.removeWorktreeFailed': 'Delete worktree failed',+ 'workingDirectory.removeWorktreePending': 'Deleting worktree “{{name}}”…','workingDirectory.removeWorktreeSuccess': 'Worktree deleted','workingDirectory.removeWorktreeTitle': 'Delete worktree','workingDirectory.worktreeSearchPlaceholder': 'Search worktrees',diff --git a/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsx b/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsxindex b4b8502e6c0..d56d232a7a3 100644--- a/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsx+++ b/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsx@@ -2,7 +2,7 @@import { isDesktop } from '@lobechat/const';import type { WorkingDirEntry } from '@lobechat/types';-import { getWorkingDirEffectivePath } from '@lobechat/types';+import { getWorkingDirSourcePath } from '@lobechat/types';import { isRecord } from '@lobechat/utils';import { Flexbox, Icon, Input, Popover, Tooltip } from '@lobehub/ui';import { toast } from '@lobehub/ui/base-ui';@@ -22,7 +22,7 @@ import { useTranslation } from 'react-i18next';import { openAddWorkingDirModal } from '@/features/WorkingDirectory';import {- resolveAgentWorkingDirectory,+ resolveAgentWorkingDirectorySource,resolveTargetDeviceId,} from '@/helpers/agentWorkingDirectory';import {@@ -337,9 +337,12 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =);const legacyAgentWorkingDirectory = getWorkingDirectoryPathString(rawLegacyAgentWorkingDirectory);- // The explicitly-selected cwd (no home fallback) — drives the active check and- // the Reset affordance.- const resolvedSelectedDir = resolveAgentWorkingDirectory({+ // The explicitly-selected REPO (no home fallback) — drives the directory label,+ // the active check, and the Reset affordance. Resolves to the SOURCE path+ // (repo root), never the active worktree: the label shows the repo the agent is+ // bound to, while the worktree switcher in git status tracks the active+ // worktree separately.+ const resolvedSelectedDir = resolveAgentWorkingDirectorySource({agencyConfig,currentDeviceId,deviceDefaultCwd,@@ -428,9 +431,9 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =};const renderRow = (entry: WorkingDirEntry) => {- const effectivePath = getWorkingDirEffectivePath(entry);- const isActive = effectivePath === selectedDir;- const isDefault = !!deviceDefaultCwd && effectivePath === deviceDefaultCwd;+ const sourcePath = getWorkingDirSourcePath(entry);+ const isActive = sourcePath === selectedDir;+ const isDefault = !!deviceDefaultCwd && sourcePath === deviceDefaultCwd;return (<Flexboxhorizontaldiff --git a/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx b/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsxindex 9c6d8b9793a..db83c9e41ec 100644--- a/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx+++ b/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx@@ -556,12 +556,21 @@ const WorktreeSwitcher = memo<WorktreeSwitcherProps>(// reconciles on the next `onWorktreesChange` revalidate.onOk: () => {setRemovingPaths((prev) => new Set(prev).add(worktree.path));+ // The dropdown is already closed, so a persistent loading toast is the+ // only signal that a (potentially slow) removal is in progress. It gets+ // swapped for the success/failure toast once the background op settles.+ const pendingToast = toast.loading(+ t('workingDirectory.removeWorktreePending', {+ name: getPathName(worktree.path),+ }),+ );void (async () => {const result = await gitService.removeGitWorktree({deviceId,path,worktreePath: worktree.path,});+ pendingToast.close();if (result.success) {// The list is hidden behind the closed dropdown, so this toast// is the only signal that the background removal finished.diff --git a/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx b/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsxindex ebd0f1dc55a..2af2903d9d9 100644--- a/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx+++ b/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx@@ -9,6 +9,10 @@ const confirmModalMock = vi.hoisted(() => vi.fn());const messageErrorMock = vi.hoisted(() => vi.fn());const messageSuccessMock = vi.hoisted(() => vi.fn());const removeGitWorktreeMock = vi.hoisted(() => vi.fn());+const toastLoadingCloseMock = vi.hoisted(() => vi.fn());+const toastLoadingMock = vi.hoisted(() =>+ vi.fn(() => ({ close: toastLoadingCloseMock, id: 'pending', update: vi.fn() })),+);vi.mock('../useCommitWorkingDirectory', () => ({useCommitWorkingDirectory: () => ({ commit: commitMock }),@@ -51,7 +55,12 @@ vi.mock('@lobehub/ui/base-ui', () => ({{children}</div>),- toast: { error: messageErrorMock, info: vi.fn(), success: messageSuccessMock },+ toast: {+ error: messageErrorMock,+ info: vi.fn(),+ loading: toastLoadingMock,+ success: messageSuccessMock,+ },}));vi.mock('antd-style', () => ({@@ -72,6 +81,8 @@ beforeEach(() => {confirmModalMock.mockReset();messageErrorMock.mockReset();messageSuccessMock.mockReset();+ toastLoadingMock.mockClear();+ toastLoadingCloseMock.mockReset();removeGitWorktreeMock.mockReset();removeGitWorktreeMock.mockResolvedValue({ success: true });});@@ -227,6 +238,11 @@ describe('WorktreeSwitcher', () => {// resolved value.confirmModalMock.mock.calls[0][0].onOk();+ // a pending toast surfaces immediately since the closed dropdown hides the row+ expect(toastLoadingMock).toHaveBeenCalledWith(+ 'workingDirectory.removeWorktreePending:{"name":"repo-detached"}',+ );+expect(removeGitWorktreeMock).toHaveBeenCalledWith({deviceId: 'device-1',path: '/repo',@@ -235,6 +251,8 @@ describe('WorktreeSwitcher', () => {await waitFor(() => {expect(onWorktreesChange).toHaveBeenCalled();});+ // the pending toast is dismissed before the terminal toast is shown+ expect(toastLoadingCloseMock).toHaveBeenCalledTimes(1);expect(messageSuccessMock).toHaveBeenCalledWith('workingDirectory.removeWorktreeSuccess');expect(messageErrorMock).not.toHaveBeenCalled();});@@ -279,6 +297,8 @@ describe('WorktreeSwitcher', () => {'fatal: worktree contains modified or untracked files',);});+ // the pending toast is dismissed even when the removal fails+ expect(toastLoadingCloseMock).toHaveBeenCalledTimes(1);expect(messageSuccessMock).not.toHaveBeenCalled();expect(onWorktreesChange).not.toHaveBeenCalled();});diff --git a/src/helpers/agentWorkingDirectory.test.ts b/src/helpers/agentWorkingDirectory.test.tsindex beb3d93be39..3b579c2560e 100644--- a/src/helpers/agentWorkingDirectory.test.ts+++ b/src/helpers/agentWorkingDirectory.test.ts@@ -4,6 +4,7 @@ import { describe, expect, it } from 'vitest';import {resolveAgentWorkingDirectory,resolveAgentWorkingDirectoryConfig,+ resolveAgentWorkingDirectorySource,resolveTargetDeviceId,} from './agentWorkingDirectory';@@ -124,6 +125,44 @@ describe('resolveAgentWorkingDirectory', () => {});});+describe('resolveAgentWorkingDirectorySource', () => {+ it('resolves to the SOURCE repo path, ignoring the active worktree', () => {+ // A worktree switch records `activeWorktree` but the directory label must+ // keep showing the repo root (where hetero CLI sessions actually anchor).+ const agencyConfig = cfg({+ executionTarget: 'local',+ workingDirByDevice: {+ cur: {+ git: { activeWorktree: '/repo-fix', branch: 'fix', isWorktree: true },+ path: '/repo',+ repoType: 'git',+ },+ },+ });++ expect(resolveAgentWorkingDirectorySource({ agencyConfig, currentDeviceId: 'cur' })).toBe(+ '/repo',+ );+ // the effective resolver still yields the worktree — the two intentionally+ // diverge, and git status (not the directory label) is what consumes it.+ expect(resolveAgentWorkingDirectory({ agencyConfig, currentDeviceId: 'cur' })).toBe(+ '/repo-fix',+ );+ });++ it('resolves the topic-level config to its source path', () => {+ expect(+ resolveAgentWorkingDirectorySource({+ topicWorkingDirectory: '/topic-effective',+ topicWorkingDirectoryConfig: {+ git: { activeWorktree: '/repo-fix', isWorktree: true },+ path: '/repo',+ },+ }),+ ).toBe('/repo');+ });+});+describe('resolveAgentWorkingDirectoryConfig', () => {it('keeps the topic-level rich config as a single structured directory', () => {const topicWorkingDirectoryConfig = {diff --git a/src/helpers/agentWorkingDirectory.ts b/src/helpers/agentWorkingDirectory.tsindex 2239bf10886..805cd625864 100644--- a/src/helpers/agentWorkingDirectory.ts+++ b/src/helpers/agentWorkingDirectory.ts@@ -3,7 +3,7 @@ import type {WorkingDirConfig,WorkingDirConfigValue,} from '@lobechat/types';-import { getWorkingDirEffectivePath } from '@lobechat/types';+import { getWorkingDirEffectivePath, getWorkingDirSourcePath } from '@lobechat/types';/*** The device a run targets: an explicitly bound remote device, this machine,@@ -77,3 +77,21 @@ export const resolveAgentWorkingDirectory = (const config = resolveAgentWorkingDirectoryConfig(params);return getWorkingDirEffectivePath(config);};++/**+ * Same precedence as {@link resolveAgentWorkingDirectory}, but resolves to the+ * SOURCE repo path (`config.path`) — the repo root, ignoring any active+ * worktree recorded in `config.git.activeWorktree`.+ *+ * Use this for the directory-picker DISPLAY, which shows the repo the agent is+ * bound to. The effective (worktree) path belongs to git status / the worktree+ * switcher, not the directory label: heterogeneous CLI agents anchor their+ * session cwd to the source repo (see `conversationLifecycle`), so showing the+ * worktree here would misrepresent where the run actually executes.+ */+export const resolveAgentWorkingDirectorySource = (+ params: Parameters<typeof resolveAgentWorkingDirectoryConfig>[0],+): string | undefined => {+ const config = resolveAgentWorkingDirectoryConfig(params);+ return getWorkingDirSourcePath(config);+};