fix: recover fresh local terminal spawns from a deleted saved cwd (#7847)
After #7750 removed the containment guard, the residual #7239 failure mode is a persisted/inherited startupCwd whose directory no longer exists: every spawn dies with the provider's missing-directory error. Fresh local renderer spawns now opt in (cwdFallback: 'worktree') to recover at the workspace root with a generic in-terminal notice; reattach, SSH, remote-runtime, runtime/API and mobile callers keep exact cwd semantics, and existing directories — including outside the worktree (#7685) — spawn as requested. Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { FLOATING_TERMINAL_WORKTREE_ID } from './constants'
|
||||
import {
|
||||
resolveTerminalStartupCwd,
|
||||
@@ -87,6 +87,119 @@ describe('resolveTerminalStartupCwd', () => {
|
||||
).toBeUndefined()
|
||||
})
|
||||
|
||||
it('falls back to the workspace root when the requested cwd directory is missing', () => {
|
||||
const onFallbackToWorkspaceRoot = vi.fn()
|
||||
expect(
|
||||
resolveTerminalStartupCwd('/repo/app', '/repo/app/deleted-folder', {
|
||||
directoryExists: (path) => path === '/repo/app',
|
||||
onFallbackToWorkspaceRoot
|
||||
})
|
||||
).toBe('/repo/app')
|
||||
expect(onFallbackToWorkspaceRoot).toHaveBeenCalledWith('/repo/app/deleted-folder')
|
||||
})
|
||||
|
||||
it('falls back to a non-ASCII workspace root for a missing cwd (#7239)', () => {
|
||||
// Why: issue #7239 reproduced in a Japanese-named worktree; the fallback
|
||||
// must preserve the selected worktree path verbatim.
|
||||
const worktreePath = '/Users/motoki/orca/workspaces/nakamuramotoki/Fableと議論'
|
||||
expect(
|
||||
resolveTerminalStartupCwd(worktreePath, '/var/tmp/orca-stale', {
|
||||
directoryExists: (path) => path === worktreePath
|
||||
})
|
||||
).toBe(worktreePath)
|
||||
})
|
||||
|
||||
it('keeps an existing cwd outside the worktree when fallback is enabled (#7685)', () => {
|
||||
const onFallbackToWorkspaceRoot = vi.fn()
|
||||
expect(
|
||||
resolveTerminalStartupCwd('/repo/app', '/repo/app-other', {
|
||||
directoryExists: () => true,
|
||||
onFallbackToWorkspaceRoot
|
||||
})
|
||||
).toBe('/repo/app-other')
|
||||
expect(onFallbackToWorkspaceRoot).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps an existing nested cwd when fallback is enabled', () => {
|
||||
const onFallbackToWorkspaceRoot = vi.fn()
|
||||
expect(
|
||||
resolveTerminalStartupCwd('/repo/app', 'packages/web', {
|
||||
directoryExists: () => true,
|
||||
onFallbackToWorkspaceRoot
|
||||
})
|
||||
).toBe('/repo/app/packages/web')
|
||||
expect(onFallbackToWorkspaceRoot).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps the requested cwd when the workspace root is missing too', () => {
|
||||
// Why: unmounted volume / stopped WSL distro — falling back would spawn a
|
||||
// misleading shell; let the provider surface its normal error instead.
|
||||
const onFallbackToWorkspaceRoot = vi.fn()
|
||||
expect(
|
||||
resolveTerminalStartupCwd('/repo/app', '/repo/app/deleted-folder', {
|
||||
directoryExists: () => false,
|
||||
onFallbackToWorkspaceRoot
|
||||
})
|
||||
).toBe('/repo/app/deleted-folder')
|
||||
expect(onFallbackToWorkspaceRoot).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not probe when the requested cwd resolves to the workspace root', () => {
|
||||
const directoryExists = vi.fn(() => false)
|
||||
expect(
|
||||
resolveTerminalStartupCwd('/repo/app', '/repo/app', {
|
||||
directoryExists,
|
||||
onFallbackToWorkspaceRoot: () => {}
|
||||
})
|
||||
).toBe('/repo/app')
|
||||
expect(directoryExists).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('falls back from a missing parent-traversal cwd to the workspace root', () => {
|
||||
expect(
|
||||
resolveTerminalStartupCwd('/repo/app', '../deleted', {
|
||||
directoryExists: (path) => path === '/repo/app'
|
||||
})
|
||||
).toBe('/repo/app')
|
||||
})
|
||||
|
||||
it('recovers missing renderer cwd values against raw worktree IDs', () => {
|
||||
expect(
|
||||
resolveTerminalStartupCwdForWorkspace({
|
||||
workspaceId: 'repo-1::/repo/app',
|
||||
requestedCwd: '/repo/app/deleted-folder',
|
||||
missingDirFallback: {
|
||||
directoryExists: (path) => path === '/repo/app'
|
||||
}
|
||||
})
|
||||
).toBe('/repo/app')
|
||||
})
|
||||
|
||||
it('recovers missing cwd values against a resolved folder workspace root', () => {
|
||||
expect(
|
||||
resolveTerminalStartupCwdForWorkspace({
|
||||
workspaceId: folderWorkspaceKey('folder-1'),
|
||||
requestedCwd: 'deleted-folder',
|
||||
resolveFolderWorkspacePath: (id) => (id === 'folder-1' ? '/repo/app' : null),
|
||||
missingDirFallback: {
|
||||
directoryExists: (path) => path === '/repo/app'
|
||||
}
|
||||
})
|
||||
).toBe('/repo/app')
|
||||
})
|
||||
|
||||
it('never probes floating terminal cwds', () => {
|
||||
const directoryExists = vi.fn(() => false)
|
||||
expect(
|
||||
resolveTerminalStartupCwdForWorkspace({
|
||||
workspaceId: FLOATING_TERMINAL_WORKTREE_ID,
|
||||
requestedCwd: '/Volumes/work/notes',
|
||||
missingDirFallback: { directoryExists }
|
||||
})
|
||||
).toBe('/Volumes/work/notes')
|
||||
expect(directoryExists).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('resolves renderer PTY cwd values against folder workspace keys', () => {
|
||||
expect(
|
||||
resolveTerminalStartupCwdForWorkspace({
|
||||
|
||||
@@ -3,9 +3,17 @@ import { resolveRuntimePath } from './cross-platform-path'
|
||||
import { parseWorkspaceKey } from './workspace-scope'
|
||||
import { splitWorktreeIdForFilesystem } from './worktree-id'
|
||||
|
||||
export type TerminalStartupCwdMissingDirFallback = {
|
||||
// Why: only local callers can probe the filesystem — SSH/remote worktree
|
||||
// paths live on another host — so the existence check is injected.
|
||||
directoryExists: (path: string) => boolean
|
||||
onFallbackToWorkspaceRoot?: (missingCwd: string) => void
|
||||
}
|
||||
|
||||
export function resolveTerminalStartupCwd(
|
||||
worktreePath: string,
|
||||
requestedCwd?: string | null
|
||||
requestedCwd?: string | null,
|
||||
missingDirFallback?: TerminalStartupCwdMissingDirFallback
|
||||
): string | undefined {
|
||||
const trimmedCwd = requestedCwd?.trim()
|
||||
if (!trimmedCwd) {
|
||||
@@ -14,13 +22,29 @@ export function resolveTerminalStartupCwd(
|
||||
// Why: resolve relative requests against the worktree root and normalize
|
||||
// `..`; the cwd is intentionally not constrained to the worktree, so opening
|
||||
// or splitting a terminal outside it (e.g. after `cd ..`) is allowed. (#7685)
|
||||
return resolveRuntimePath(worktreePath, trimmedCwd)
|
||||
const resolvedCwd = resolveRuntimePath(worktreePath, trimmedCwd)
|
||||
if (
|
||||
missingDirFallback &&
|
||||
resolvedCwd !== worktreePath &&
|
||||
!missingDirFallback.directoryExists(resolvedCwd) &&
|
||||
missingDirFallback.directoryExists(worktreePath)
|
||||
) {
|
||||
// Why: a persisted/inherited startup folder can be deleted later; spawning
|
||||
// into it fails on every retry and bricks terminal creation for that tab
|
||||
// (#7239), so recover at the workspace root. If the root is missing too
|
||||
// (unmounted volume, stopped WSL distro), keep the requested cwd so the
|
||||
// provider surfaces its normal error instead of a misleading fallback.
|
||||
missingDirFallback.onFallbackToWorkspaceRoot?.(resolvedCwd)
|
||||
return worktreePath
|
||||
}
|
||||
return resolvedCwd
|
||||
}
|
||||
|
||||
export function resolveTerminalStartupCwdForWorkspace(args: {
|
||||
workspaceId?: string
|
||||
requestedCwd?: string | null
|
||||
resolveFolderWorkspacePath?: (folderWorkspaceId: string) => string | null | undefined
|
||||
missingDirFallback?: TerminalStartupCwdMissingDirFallback
|
||||
}): string | undefined {
|
||||
if (!args.requestedCwd || args.requestedCwd.trim().length === 0) {
|
||||
return undefined
|
||||
@@ -39,7 +63,7 @@ export function resolveTerminalStartupCwdForWorkspace(args: {
|
||||
// back to the provider default rather than guessing a base.
|
||||
return undefined
|
||||
}
|
||||
return resolveTerminalStartupCwd(workspacePath, args.requestedCwd)
|
||||
return resolveTerminalStartupCwd(workspacePath, args.requestedCwd, args.missingDirFallback)
|
||||
}
|
||||
|
||||
function resolveTerminalWorkspacePath(
|
||||
|
||||
Reference in New Issue
Block a user