fix(worktrees): re-check checkouts between remote nested deletes; scale reply timeout

This commit is contained in:
OrcaWin
2026-11-08 23:44:12 -08:00
parent 84ee48db54
commit e5e36f932f
5 changed files with 51 additions and 13 deletions
@@ -8,10 +8,12 @@ import {
type ExecutionHostId
} from '../../../../shared/execution-host'
import { isFolderRepo } from '../../../../shared/repo-kind'
import { areWorktreePathsEqual } from '../../../git/worktrees/catalog/worktree-path-comparison'
import { resolveWorktreeRemovalRepoOwner } from '../../../workspaces/removal/worktree-removal-repo-owner'
import {
resolveWorktreeRemovalHome,
resolveWorktreeRemovalRoute
resolveWorktreeRemovalRoute,
type WorktreeRemovalRoute
} from '../../../workspaces/removal/worktree-removal-execution-host-route'
import {
assertNestedWorktreeRemovalApproval,
@@ -26,12 +28,11 @@ import type {
import { OrcaRuntimeWithRemoveManagedWorktree } from './workspace-removal'
import { listWorktreesOnRemovalRoute } from './worktree-removal-route-listing'
/** Lists what deleting the target would take with it: nested worktrees deepest-first, then the target. */
async function previewRuntimeNestedWorktreeRemoval(
async function listRuntimeRemovalWorktrees(
store: RuntimeStore,
target: RuntimeWorktreeRemovalTarget,
hostId: ExecutionHostId | undefined
): Promise<GitWorktreeInfo[]> {
): Promise<{ repoPath: string; route: WorktreeRemovalRoute; worktrees: GitWorktreeInfo[] }> {
const owner = resolveWorktreeRemovalRepoOwner(store, target.repoId, hostId)
if (owner.kind === 'ambiguous') {
throw new Error(
@@ -46,14 +47,36 @@ async function previewRuntimeNestedWorktreeRemoval(
}
const route = resolveWorktreeRemovalRoute(hostId ?? getRepoExecutionHostId(owner.repo))
const { registeredWorktrees } = await listWorktreesOnRemovalRoute(route, owner.repo, store)
return { repoPath: owner.repo.path, route, worktrees: registeredWorktrees }
}
/** Lists what deleting the target would take with it: nested worktrees deepest-first, then the target. */
async function previewRuntimeNestedWorktreeRemoval(
store: RuntimeStore,
target: RuntimeWorktreeRemovalTarget,
hostId: ExecutionHostId | undefined
): Promise<GitWorktreeInfo[]> {
const { repoPath, route, worktrees } = await listRuntimeRemovalWorktrees(store, target, hostId)
return getNestedWorktreeRemovalPlan({
repoPath: owner.repo.path,
repoPath,
worktreePath: target.path,
worktrees: registeredWorktrees,
worktrees,
home: resolveWorktreeRemovalHome(route)
})
}
// Earlier deletions take time; refuse if this checkout's branch or HEAD moved since review.
async function assertRuntimeCheckoutUnchanged(
store: RuntimeStore,
target: RuntimeWorktreeRemovalTarget,
hostId: ExecutionHostId | undefined,
expected: GitWorktreeInfo
): Promise<void> {
const { worktrees } = await listRuntimeRemovalWorktrees(store, target, hostId)
const current = worktrees.find((item) => areWorktreePathsEqual(item.path, expected.path))
assertNestedWorktreeRemovalApproval(current ? [current] : [], [expected])
}
/** Re-checks the reviewed plan, deletes nested children, then the target itself. */
async function removeRuntimeApprovedNestedWorktrees(args: {
store: RuntimeStore
@@ -69,10 +92,19 @@ async function removeRuntimeApprovedNestedWorktrees(args: {
}
const plan = await previewRuntimeNestedWorktreeRemoval(args.store, args.target, args.hostId)
assertNestedWorktreeRemovalApproval(plan, args.approved)
const preserved = await removeNestedWorktreePlanChildren(args.target.repoId, plan, (id) =>
args.removeChild(id)
const preserved = await removeNestedWorktreePlanChildren(
args.target.repoId,
plan,
async (id, child) => {
await assertRuntimeCheckoutUnchanged(args.store, args.target, args.hostId, child)
return args.removeChild(id)
}
)
try {
const parentCheckout = plan.at(-1)
if (parentCheckout) {
await assertRuntimeCheckoutUnchanged(args.store, args.target, args.hostId, parentCheckout)
}
const result = await args.removeTarget()
return preserved.length > 0 ? { ...result, nestedPreservedBranches: preserved } : result
} catch (error) {
@@ -47,7 +47,9 @@ function NestedRefusalToast({ hostId }: { hostId: ExecutionHostId | null }): Rea
worktreeName: 'react-use-effects',
nestedRemovalTarget: { id: WORKTREE_ID, executionHostId: hostId }
})
return () => toast.dismiss()
return () => {
toast.dismiss()
}
}, [hostId])
return (
<>
@@ -92,7 +92,10 @@ async function requestWorktreeRemoval(
{
// Why (#19334): the host may run an archive hook before it decides anything, then waits for
// Git's delete before it replies; outlast both when a hook can run.
timeoutMs: worktreeRemovalReplyTimeoutMs(!skipArchive),
timeoutMs: worktreeRemovalReplyTimeoutMs(
!skipArchive,
options?.approvedNestedWorktrees?.length
),
beforeSend
}
)
@@ -39,7 +39,7 @@ export function createWebWorktreeRemovalCalls(): Pick<
runHooks: skipArchive !== true
},
// Why: the host replies once Git has deleted the checkout, past the default 30 s budget.
worktreeRemovalReplyTimeoutMs(skipArchive !== true)
worktreeRemovalReplyTimeoutMs(skipArchive !== true, approvedNestedWorktrees?.length)
)
}
}
@@ -4,8 +4,9 @@
export const ARCHIVE_HOOK_TIMEOUT_MS = 120_000
/** Allow unbounded Git deletion and the host’s two-at-a-time queue after any hook deadline. A client timeout is not proof removal failed; settle from host listings. */
export function worktreeRemovalReplyTimeoutMs(runsArchiveHook: boolean): number {
return (runsArchiveHook ? ARCHIVE_HOOK_TIMEOUT_MS : 0) + 180_000
/** A nested delete removes each listed worktree in turn before replying. */
export function worktreeRemovalReplyTimeoutMs(runsArchiveHook: boolean, worktreeCount = 1): number {
return ((runsArchiveHook ? ARCHIVE_HOOK_TIMEOUT_MS : 0) + 180_000) * Math.max(1, worktreeCount)
}
/** RPC/CLI error code for a removal refused because the repo's archive hook did not succeed. */