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): don't record a worktree created outside the source's filesystem (#20492)

Arvin Xu68ec8e79e002
diff --git a/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts b/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts
index c24ba40d01d..cc921c7208f 100644
--- a/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts
+++ b/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts
@@ -251,6 +251,56 @@ describe('recordWorktreeAdd', () => {
});
});
+describe('recordGitCommandEffects β€” worktree outside the source filesystem', () => {
+ const WSL_SOURCE = '\\\\wsl.localhost\\Ubuntu-22.04\\home\\zhy\\PROJECT\\RefRWKV';
+
+ it('ignores a worktree created inside WSL for a Windows-side source', async () => {
+ chatMocks.topics = {
+ t1: { metadata: { workingDirectoryConfig: { path: WSL_SOURCE, repoType: 'git' } } },
+ };
+
+ // Verbatim production command: runCommand on a Windows device shells into WSL.
+ await recordGitCommandEffects({
+ command:
+ 'wsl.exe -d Ubuntu-22.04 -- bash -lc "cd /home/zhy/PROJECT/RefRWKV && git rev-parse 27f78b6^ && git worktree add --detach /tmp/RefRWKV-lsh 27f78b6^"',
+ resultContent: 'Preparing worktree (detached HEAD 6692cd5)',
+ topicId: 't1',
+ });
+
+ expect(chatMocks.updateTopicMetadata).not.toHaveBeenCalled();
+ });
+
+ it('ignores a POSIX worktree the device reports for a Windows source', async () => {
+ chatMocks.topics = {
+ t1: { metadata: { workingDirectoryConfig: { path: 'C:\\repo', repoType: 'git' } } },
+ };
+ gitServiceMocks.listGitWorktrees.mockResolvedValue([{ branch: 'feat/x', path: '/mnt/c/wt' }]);
+
+ await recordGitCommandEffects({
+ command: 'git worktree add -b feat/x "$WT"',
+ topicId: 't1',
+ });
+
+ expect(chatMocks.updateTopicMetadata).not.toHaveBeenCalled();
+ });
+
+ it('still records a Windows worktree for a Windows source', async () => {
+ chatMocks.topics = {
+ t1: { metadata: { workingDirectoryConfig: { path: 'C:\\repo', repoType: 'git' } } },
+ };
+
+ await recordGitCommandEffects({ command: 'git worktree add C:\\wt', topicId: 't1' });
+
+ expect(chatMocks.updateTopicMetadata).toHaveBeenCalledWith('t1', {
+ workingDirectoryConfig: {
+ git: { activeWorktree: 'C:\\wt', isWorktree: true },
+ path: 'C:\\repo',
+ repoType: 'git',
+ },
+ });
+ });
+});
+
describe('recordGitCommandEffects', () => {
it('refreshes the local branch cache when the run is bound to this device', async () => {
chatMocks.topics = {
diff --git a/src/store/tool/slices/builtin/executors/worktreeDetection.ts b/src/store/tool/slices/builtin/executors/worktreeDetection.ts
index f7f77291a07..f12a9dcabdd 100644
--- a/src/store/tool/slices/builtin/executors/worktreeDetection.ts
+++ b/src/store/tool/slices/builtin/executors/worktreeDetection.ts
@@ -949,9 +949,22 @@ const resolveWorktreeAddFromDevice = async (
}
};
+const isWindowsPath = (p: string): boolean => /^[A-Z]:[\\/]/i.test(p) || p.startsWith('\\\\');
+
+/**
+ * The recorded worktree becomes the cwd of every later command on the source's
+ * device, so it must be an absolute path in the source's own filesystem namespace.
+ * A Windows source (`C:\repo`, `\\wsl.localhost\…`) whose command ran a nested
+ * shell β€” `wsl.exe -- bash -lc "git worktree add /tmp/wt"` β€” yields a POSIX path
+ * that does not exist for the Windows shell, and spawning there fails with ENOENT.
+ */
+const isWorktreePathOnSourceDevice = (path: string, source: string): boolean =>
+ isWindowsPath(source) ? isWindowsPath(path) : path.startsWith('/');
+
/**
* Turn a parse result into a recordable worktree: trust a statically resolved
- * path directly, otherwise read the real one back from the device.
+ * path directly, otherwise read the real one back from the device. Either way a
+ * path that is not usable as a cwd next to the source is dropped.
*/
const resolveWorktreeAddInfo = async (
parsed: WorktreeAddParseResult | undefined,
@@ -959,11 +972,13 @@ const resolveWorktreeAddInfo = async (
boundDeviceId?: string,
): Promise<WorktreeAddInfo | undefined> => {
if (!parsed) return undefined;
- if (parsed.path) {
+ if (parsed.path && isWorktreePathOnSourceDevice(parsed.path, source)) {
const branch = normalizeBranch(parsed.branch);
return { ...(branch ? { branch } : {}), path: parsed.path };
}
- return resolveWorktreeAddFromDevice(parsed, source, boundDeviceId);
+ const fromDevice = await resolveWorktreeAddFromDevice(parsed, source, boundDeviceId);
+ if (!fromDevice || !isWorktreePathOnSourceDevice(fromDevice.path, source)) return undefined;
+ return fromDevice;
};
/**