🐛 fix(vscode): resume 会话用 resolveWorktreePath 避免误判 worktree 已清理

This commit is contained in:
2026-07-16 01:51:08 +08:00
parent aff4ee2758
commit b45257a38d
2 changed files with 25 additions and 4 deletions
+21 -1
View File
@@ -3,7 +3,27 @@ import { existsSync, mkdtempSync, rmSync } from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import { describe, expect, it } from 'vitest'
import { findProcessesUsingWorktree } from './worktree'
import { findProcessesUsingWorktree, resolveWorktreePath } from './worktree'
describe('resolveWorktreePath', () => {
const workspaceRoot = '/workspace/project'
it('绝对路径原样返回', () => {
const abs = '/home/user/Sources/worktree/repo/session-foo'
expect(resolveWorktreePath(abs, workspaceRoot)).toBe(abs)
})
it('~/... 展开为 home 下路径', () => {
const home = os.homedir()
expect(resolveWorktreePath('~/Sources/worktree/repo/session-foo', workspaceRoot))
.toBe(path.join(home, 'Sources/worktree/repo/session-foo'))
})
it('相对路径拼到 workspaceRoot 下', () => {
expect(resolveWorktreePath('.claude/worktrees/abc123', workspaceRoot))
.toBe(path.join(workspaceRoot, '.claude/worktrees/abc123'))
})
})
// 检测靠扫 /proc,只有 Linux 有;别的平台整组跳过(函数本身也会返回空)。
describe.runIf(existsSync('/proc'))('findProcessesUsingWorktree', () => {
+4 -3
View File
@@ -84,15 +84,16 @@ export async function handleResumeSession(panel: KanbanWebviewPanel, sessionId:
return
}
const workspaceRoot = workspace.workspaceFolders?.[0]?.uri.fsPath
// Implementation sessions live in a worktree; the caller can pass a
// workspace-relative `relCwd` so `--resume` runs from the right place.
// Implementation sessions live in a worktree; `relCwd` may be absolute /
// `~/...` / workspace-relative — must go through resolveWorktreePath
// (path.join(workspaceRoot, abs) would mangle absolute paths).
// If the worktree has since been cleaned up (typical: drop card to
// "完成" → auto `git worktree remove`), fall back to the workspace
// root instead of pointing the terminal at a missing directory.
let effectiveCwd: string | undefined = workspaceRoot
let cwdFallback = false
if (relCwd && workspaceRoot) {
const resolved = path.join(workspaceRoot, relCwd)
const resolved = resolveWorktreePath(relCwd, workspaceRoot)
if (fs.existsSync(resolved)) {
effectiveCwd = resolved
}