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(chat-input): keep the git chip when a branchless recorded worktree is deleted (#20923)

Arvin Xu6030697a2552
diff --git a/src/features/ChatInput/ControlBar/BranchSwitcher.tsx b/src/features/ChatInput/ControlBar/BranchSwitcher.tsx
index 11904d58249..5d9b5f63756 100644
--- a/src/features/ChatInput/ControlBar/BranchSwitcher.tsx
+++ b/src/features/ChatInput/ControlBar/BranchSwitcher.tsx
@@ -346,7 +346,7 @@ const BranchSwitcher = memo<BranchSwitcherProps>(
setBusyBranch(branch);
onOpenChange(false);
try {
- await switchWorktree(owner.path);
+ await switchWorktree(owner.path, branch);
} finally {
onAfterCheckout?.();
setBusyBranch(null);
diff --git a/src/features/ChatInput/ControlBar/GitStatus.tsx b/src/features/ChatInput/ControlBar/GitStatus.tsx
index 963a07b6445..45d0aaf5e29 100644
--- a/src/features/ChatInput/ControlBar/GitStatus.tsx
+++ b/src/features/ChatInput/ControlBar/GitStatus.tsx
@@ -154,7 +154,26 @@ const GitStatus = memo<GitStatusProps>(
(s) => !!deviceSelectors.getDeviceById(deviceId)(s)?.online,
);
const probeReachedDirectory = !deviceId || deviceOnline;
- const isStale = !branch && probeSettled && probeReachedDirectory && !!fallbackGit?.branch;
+ const probeFoundNothing = !branch && probeSettled && probeReachedDirectory;
+ // Worktrees picked from the bar were persisted without a branch, so the
+ // recorded worktree is all such a snapshot has. An empty probe alone can't
+ // condemn it: a remote read that fails in transit on a device still marked
+ // online comes back just as empty. Ask the source repo instead β€” the worktree
+ // is gone only when the source answers (a real listing always includes the
+ // source itself; a failed RPC is `[]`) and no longer lists it as live.
+ const recordedWorktree =
+ !fallbackGit?.branch && sourcePath && sourcePath !== path
+ ? fallbackGit?.activeWorktree
+ : undefined;
+ const { data: sourceWorktrees } = useFetchGitWorktrees(
+ deviceId,
+ probeFoundNothing && recordedWorktree ? sourcePath : undefined,
+ );
+ const recordedWorktreeGone =
+ !!sourceWorktrees?.length &&
+ !sourceWorktrees.some((worktree) => worktree.path === path && !worktree.prunable);
+ const isStale =
+ probeFoundNothing && (!!fallbackGit?.branch || (!!recordedWorktree && recordedWorktreeGone));
const { data: prData, mutate: mutatePR } = useFetchGitLinkedPR(
deviceId,
path,
diff --git a/src/features/ChatInput/ControlBar/StaleGitSnapshot.tsx b/src/features/ChatInput/ControlBar/StaleGitSnapshot.tsx
index cd27260e8a5..939997ac3e8 100644
--- a/src/features/ChatInput/ControlBar/StaleGitSnapshot.tsx
+++ b/src/features/ChatInput/ControlBar/StaleGitSnapshot.tsx
@@ -82,7 +82,7 @@ const StaleGitSnapshot = memo<StaleGitSnapshotProps>(
const [resetting, setResetting] = useState(false);
const switchWorktree = useSwitchWorktree({ agentId, isGithub, sourcePath: sourcePath ?? path });
- const { branch, explanation, isWorktree, pullRequest, reset, worktreePath } =
+ const { explanation, isWorktree, label, pullRequest, reset, worktreePath } =
resolveStaleSnapshot({ git, path, sourcePath });
const handleOpenPr = useCallback(() => {
@@ -108,7 +108,7 @@ const StaleGitSnapshot = memo<StaleGitSnapshotProps>(
const trigger = (
<div className={gitChipStyles.staleTrigger}>
<Icon icon={isWorktree ? GitForkIcon : GitBranchIcon} size={12} />
- <span className={styles.triggerLabel}>{branch}</span>
+ <span className={styles.triggerLabel}>{label}</span>
</div>
);
diff --git a/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx b/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx
index 2e51ae05118..7c69c4e585c 100644
--- a/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx
+++ b/src/features/ChatInput/ControlBar/WorktreeSwitcher.tsx
@@ -501,7 +501,7 @@ const WorktreeSwitcher = memo<WorktreeSwitcherProps>(
return;
}
- await switchWorktree(worktree.path);
+ await switchWorktree(worktree.path, worktree.detached ? undefined : worktree.branch);
setOpen(false);
},
[switchWorktree],
@@ -572,7 +572,7 @@ const WorktreeSwitcher = memo<WorktreeSwitcherProps>(
// Point the conversation at the freshly created worktree, then reconcile
// the list so the new row (now `current`) appears.
- await switchWorktree(result.worktreePath ?? worktreePath);
+ await switchWorktree(result.worktreePath ?? worktreePath, branch);
await onWorktreesChange?.();
return undefined;
},
diff --git a/src/features/ChatInput/ControlBar/__tests__/GitStatus.test.tsx b/src/features/ChatInput/ControlBar/__tests__/GitStatus.test.tsx
index 345ed8670a2..611cfb2558c 100644
--- a/src/features/ChatInput/ControlBar/__tests__/GitStatus.test.tsx
+++ b/src/features/ChatInput/ControlBar/__tests__/GitStatus.test.tsx
@@ -215,6 +215,75 @@ describe('GitStatus', () => {
expect(screen.queryByTestId('worktree-switcher')).not.toBeInTheDocument();
});
+ // Worktrees picked from the bar were persisted without a branch. The dead
+ // worktree alone must still hand off, or the reset-to-source action vanishes
+ // with the rest of the cluster and the conversation is stuck on a missing cwd.
+ describe('snapshot that recorded only the worktree, no branch', () => {
+ const branchless = { activeWorktree: '/tmp/lobehub-wt-subtask', isWorktree: true };
+ const source = { branch: 'canary', current: false, path: '/repo' };
+
+ const mockSourceListing = (listing: unknown[] | undefined) => {
+ gitHookMocks.useFetchGitWorktrees.mockImplementation(
+ (_deviceId: string | undefined, path?: string) => ({
+ data: path === '/repo' ? listing : [],
+ mutate: gitHookMocks.mutateWorktrees,
+ }),
+ );
+ };
+
+ const renderBranchless = () =>
+ render(
+ <GitStatus
+ isGithub
+ agentId="agent-1"
+ deviceId="device-1"
+ fallbackGit={branchless}
+ path="/tmp/lobehub-wt-subtask"
+ sourcePath="/repo"
+ />,
+ );
+
+ // Worktrees picked from the bar were persisted without a branch. Requiring
+ // one collapsed the whole cluster β€” reset-to-source action included.
+ it('hands off once the source repo no longer lists the worktree', () => {
+ mockSourceListing([source]);
+
+ renderBranchless();
+
+ expect(screen.getByTestId('stale-git-snapshot')).toBeInTheDocument();
+ });
+
+ it('hands off when the source lists the worktree only as prunable', () => {
+ mockSourceListing([
+ source,
+ { current: false, path: '/tmp/lobehub-wt-subtask', prunable: true },
+ ]);
+
+ renderBranchless();
+
+ expect(screen.getByTestId('stale-git-snapshot')).toBeInTheDocument();
+ });
+
+ // A remote read that fails in transit on a device still marked online comes
+ // back as empty as a deleted path; the server turns the failed listing into
+ // `[]`. Neither proves the worktree is gone.
+ it('does not condemn the worktree when the source listing failed', () => {
+ mockSourceListing([]);
+
+ const { container } = renderBranchless();
+
+ expect(container).toBeEmptyDOMElement();
+ });
+
+ it('does not condemn a worktree the source still lists as live', () => {
+ mockSourceListing([source, { current: false, path: '/tmp/lobehub-wt-subtask' }]);
+
+ const { container } = renderBranchless();
+
+ expect(container).toBeEmptyDOMElement();
+ });
+ });
+
it('skips the linked-PR lookup, which has no directory to run `gh` in', () => {
render(
<GitStatus
diff --git a/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx b/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx
index da35b2eb4b8..8474920b6a1 100644
--- a/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx
+++ b/src/features/ChatInput/ControlBar/__tests__/WorktreeSwitcher.test.tsx
@@ -418,7 +418,8 @@ describe('WorktreeSwitcher', () => {
expect(screen.getAllByLabelText('workingDirectory.removeWorktreeAction')).toHaveLength(1);
});
- it('commits the selected worktree path as the working directory', () => {
+ // The branch is the only label left once the worktree directory is deleted.
+ it('commits the selected worktree path and its branch as the working directory', () => {
render(
<WorktreeSwitcher
isGithub
@@ -446,7 +447,7 @@ describe('WorktreeSwitcher', () => {
fireEvent.click(screen.getByText('/repo-canary'));
expect(commitMock).toHaveBeenCalledWith({
- git: { activeWorktree: '/repo-canary' },
+ git: { activeWorktree: '/repo-canary', branch: 'canary' },
path: '/repo',
repoType: 'github',
});
diff --git a/src/features/ChatInput/ControlBar/__tests__/staleSnapshot.test.ts b/src/features/ChatInput/ControlBar/__tests__/staleSnapshot.test.ts
index 41ce08033ab..69c31e910f0 100644
--- a/src/features/ChatInput/ControlBar/__tests__/staleSnapshot.test.ts
+++ b/src/features/ChatInput/ControlBar/__tests__/staleSnapshot.test.ts
@@ -30,6 +30,23 @@ describe('resolveStaleSnapshot', () => {
expect(view.pullRequest?.number).toBe(18_454);
});
+ it('labels the chip with the recorded branch', () => {
+ const view = resolveStaleSnapshot({ git: deadWorktree, path: '/tmp/lobehub-wt-subtask' });
+
+ expect(view.label).toBe('feat/task-list-subtask-nesting');
+ });
+
+ it('falls back to the worktree name when no branch was recorded', () => {
+ const view = resolveStaleSnapshot({
+ git: { activeWorktree: '/tmp/agent-gateway-wt-asa', isWorktree: true },
+ path: '/tmp/agent-gateway-wt-asa',
+ sourcePath: '/Users/me/code/lobehub',
+ });
+
+ expect(view.label).toBe('agent-gateway-wt-asa');
+ expect(view.reset?.targetPath).toBe('/Users/me/code/lobehub');
+ });
+
it('names the missing worktree in the explanation', () => {
const view = resolveStaleSnapshot({ git: deadWorktree, path: '/tmp/lobehub-wt-subtask' });
diff --git a/src/features/ChatInput/ControlBar/staleSnapshot.ts b/src/features/ChatInput/ControlBar/staleSnapshot.ts
index 5b6b2f5e4cd..1136e4698d7 100644
--- a/src/features/ChatInput/ControlBar/staleSnapshot.ts
+++ b/src/features/ChatInput/ControlBar/staleSnapshot.ts
@@ -14,6 +14,8 @@ export interface StaleSnapshotView {
};
/** True when the recorded checkout was a linked worktree, not the repo itself. */
isWorktree: boolean;
+ /** What the chip shows: the recorded branch, else the dead checkout's name. */
+ label: string;
pullRequest?: DeviceGitLinkedPullRequest;
/**
* Where "go back" lands, and whether it is offered at all. Present only when a
@@ -56,6 +58,7 @@ export const resolveStaleSnapshot = ({
? { key: 'workingDirectory.staleWorktreeSnapshot', values: { name: worktreeName } }
: { key: 'workingDirectory.staleSnapshot' },
isWorktree,
+ label: git.branch || getWorkingDirectoryName(worktreePath) || worktreePath,
pullRequest: git.github?.pullRequest ?? undefined,
...(canReset
? { reset: { name: getWorkingDirectoryName(sourcePath), targetPath: sourcePath! } }
diff --git a/src/features/ChatInput/ControlBar/useSwitchWorktree.ts b/src/features/ChatInput/ControlBar/useSwitchWorktree.ts
index e1de2c4df43..47014965da6 100644
--- a/src/features/ChatInput/ControlBar/useSwitchWorktree.ts
+++ b/src/features/ChatInput/ControlBar/useSwitchWorktree.ts
@@ -19,10 +19,14 @@ export const useSwitchWorktree = ({ agentId, isGithub, sourcePath }: UseSwitchWo
const { commit } = useCommitWorkingDirectory(agentId);
return useCallback(
- async (worktreePath: string) => {
+ async (worktreePath: string, branch?: string) => {
const entry: WorkingDirEntry = {
- // Selecting the source repo itself clears the worktree override.
- ...(worktreePath === sourcePath ? {} : { git: { activeWorktree: worktreePath } }),
+ // Selecting the source repo itself clears the worktree override. The
+ // branch is recorded with the worktree because it is the only label the
+ // control bar has left once the worktree directory is deleted.
+ ...(worktreePath === sourcePath
+ ? {}
+ : { git: { activeWorktree: worktreePath, ...(branch ? { branch } : {}) } }),
path: sourcePath,
repoType: isGithub ? 'github' : 'git',
};