diff --git a/src/renderer/src/store/slices/worktrees/teardown/cloud-sandbox-delete-guard.ts b/src/renderer/src/store/slices/worktrees/teardown/cloud-sandbox-delete-guard.ts index 9c345df71a5..c3b6042e210 100644 --- a/src/renderer/src/store/slices/worktrees/teardown/cloud-sandbox-delete-guard.ts +++ b/src/renderer/src/store/slices/worktrees/teardown/cloud-sandbox-delete-guard.ts @@ -5,6 +5,84 @@ import type { EphemeralVmRuntimeRecord } from '../../../../../../shared/ephemera import type { RuntimeClientTarget } from '../../../../../../shared/runtime-client-target' import { CLOUD_ASLEEP_DELETE_REFUSAL } from '../../../../../../shared/worktree/removal' import type { RemoveWorktreeOptions } from './worktree-removal-options' +import type { WorktreeSliceGet } from '../listing/worktree-slice-types' +import type { ExecutionHostId } from '../../../../../../shared/execution-host' +import { getRepoIdFromWorktreeId } from '../../../../../../shared/worktree/id' +import { ensureHooksConfirmed } from '@/lib/ensure-hooks-confirmed' + +type Preflight = Awaited< + ReturnType> +> + +/** One Delete asks twice (archive-hook gate, then dispatch); a few seconds is the same answer. */ +const PREFLIGHT_REUSE_MS = 10_000 +const recent = new Map }>() + +function preflightCloudDelete( + runtimeEnvironmentId: string, + options: { consume?: boolean } = {} +): Promise | null { + const preflight = window.api.ephemeralVm?.preflightCloudDelete + if (!preflight) { + return null + } + const cached = recent.get(runtimeEnvironmentId) + if (options.consume) { + // The dispatch is the last ask of this Delete; a later Delete asks again. + recent.delete(runtimeEnvironmentId) + } + if (cached && Date.now() - cached.at < PREFLIGHT_REUSE_MS) { + return cached.result + } + if (options.consume) { + return preflight({ runtimeEnvironmentId }) + } + const result = preflight({ runtimeEnvironmentId }) + recent.set(runtimeEnvironmentId, { at: Date.now(), result }) + void result.catch(() => recent.delete(runtimeEnvironmentId)) + return result +} + +/** + * True when Delete won't talk to this workspace's server: an Orca Cloud sandbox that is asleep or + * already gone. Its archive hooks can't run, and asking the server would only wait out timeouts. + */ +export async function cloudSandboxSkipsServer( + runtimeEnvironmentId: string | null | undefined +): Promise { + if (!runtimeEnvironmentId) { + return false + } + const result = await preflightCloudDelete(runtimeEnvironmentId)?.catch(() => null) + return result?.ok === true && (result.asleep === true || result.gone === true) +} + +/** + * forget-local touches no remote/archive hooks; otherwise read current trust so concurrent + * same-repo deletes share one approval. An asleep Orca Cloud sandbox can't run archive hooks, and + * asking it would only wait out request timeouts (W9 measured 31 s), so its archive is skipped. + */ +export async function shouldSkipArchiveHooks( + get: WorktreeSliceGet, + forgetLocalOnly: boolean, + target: { + worktreeId: string + hostId: ExecutionHostId | undefined + runtimeEnvironmentId: string | null | undefined + } +): Promise { + if (forgetLocalOnly || (await cloudSandboxSkipsServer(target.runtimeEnvironmentId))) { + return true + } + const answer = await ensureHooksConfirmed( + get, + getRepoIdFromWorktreeId(target.worktreeId), + 'archive', + target.hostId, + target.runtimeEnvironmentId ?? undefined + ) + return answer === 'skip' +} /** * Orca Cloud: the row goes only once its sandbox is reachable for the destroy that follows. @@ -23,11 +101,10 @@ export async function prepareCloudSandboxDelete(args: { if (args.options?.mode === 'forget-local' || args.target.kind !== 'environment') { return args.options } - const preflight = window.api.ephemeralVm?.preflightCloudDelete - if (!preflight) { + const result = await preflightCloudDelete(args.target.environmentId, { consume: true }) + if (!result) { return args.options } - const result = await preflight({ runtimeEnvironmentId: args.target.environmentId }) if (!result.ok) { throw new Error(ephemeralVmErrorText(result)) } diff --git a/src/renderer/src/store/slices/worktrees/teardown/cloud-workspace-delete.test.ts b/src/renderer/src/store/slices/worktrees/teardown/cloud-workspace-delete.test.ts index 2af6b5cb6dc..a7ab90edba6 100644 --- a/src/renderer/src/store/slices/worktrees/teardown/cloud-workspace-delete.test.ts +++ b/src/renderer/src/store/slices/worktrees/teardown/cloud-workspace-delete.test.ts @@ -89,7 +89,9 @@ describe('Orca Cloud workspace delete', () => { const result = await store.getState().removeWorktree({ id: wt.id, executionHostId: null }, true) expect(result).toEqual({ ok: true }) - expect(calledMethods()).not.toContain('worktree.rm') + // Nothing goes to the asleep sandbox's server, not even the archive-hook check (W9: a 31 s + // delete was waiting out request timeouts against it). + expect(calledMethods()).toEqual([]) expect(mockApi.worktrees.forgetLocal).toHaveBeenCalled() expect(mockApi.ephemeralVm.cleanup).toHaveBeenCalledWith({ runtimeId: 'runtime-cloud' }) }) diff --git a/src/renderer/src/store/slices/worktrees/teardown/remove-worktree.ts b/src/renderer/src/store/slices/worktrees/teardown/remove-worktree.ts index 0daa81fc50d..6834027b9a4 100644 --- a/src/renderer/src/store/slices/worktrees/teardown/remove-worktree.ts +++ b/src/renderer/src/store/slices/worktrees/teardown/remove-worktree.ts @@ -1,9 +1,7 @@ -import { getRepoIdFromWorktreeId } from '../../../../../../shared/worktree/id' import type { WorktreeSlice } from '../worktree-slice-contract' import type { WorktreeSliceGet, WorktreeSliceSet } from '../listing/worktree-slice-types' import type { RemoveWorktreeResult } from '../../../../../../shared/worktree/create-types' import { parseExecutionHostId } from '../../../../../../shared/execution-host' -import { ensureHooksConfirmed } from '@/lib/ensure-hooks-confirmed' import { runtimeTargetForOwnerEnvironment } from '../../../../runtime/rpc/targets/runtime-client-target' import { forgetHugeRepoWarningDismissalsForWorktrees } from '@/lib/source-control/warnings/source-control-huge-repo-warning-dismissals' import { forgetWorktreeSleepIntent } from '@/lib/worktree-sleep-intent' @@ -45,6 +43,7 @@ import { } from './worktree-state-before-removal' import { resolveWorktreeRemovalPreference } from './worktree-removal-preference' import { completePreservedBranchCleanup } from './complete-preserved-branch-cleanup' +import { shouldSkipArchiveHooks } from './cloud-sandbox-delete-guard' export function createRemoveWorktree( set: WorktreeSliceSet, @@ -88,16 +87,11 @@ export function createRemoveWorktree( })) try { - // forget-local touches no remote/archive hooks; read current trust so concurrent same-repo deletes share one approval. - const skipArchive = policy.forgetLocalOnly - ? true - : (await ensureHooksConfirmed( - get, - getRepoIdFromWorktreeId(worktreeId), - 'archive', - hostId, - removalRoute?.runtimeEnvironmentId - )) === 'skip' + const skipArchive = await shouldSkipArchiveHooks(get, policy.forgetLocalOnly, { + worktreeId, + hostId, + runtimeEnvironmentId: removalRoute?.runtimeEnvironmentId + }) const worktreeBeforeRemoval = findWorktreeOnConfirmedHost( get,