diff --git a/src/main/ephemeral-vm/workspace-removal.test.ts b/src/main/ephemeral-vm/workspace-removal.test.ts index b5a7d683632..732c9a5c221 100644 --- a/src/main/ephemeral-vm/workspace-removal.test.ts +++ b/src/main/ephemeral-vm/workspace-removal.test.ts @@ -8,7 +8,14 @@ import { } from '../../shared/ephemeral-vm-runtime-store' import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes' import type { RuntimeRpcResponse } from '../../shared/runtime-rpc-envelope' -import { RecipeWorkspaceRemovalError } from '../../shared/recipe-host-removal' +import { + CLOUD_DELETE_NOT_SENT, + CLOUD_DELETE_NOT_SENT_IN_USE, + CLOUD_DELETE_NOT_SENT_NO_DESTROY, + CLOUD_DELETE_NOT_SENT_UNCHECKED, + isCloudDeleteNotSentError, + RecipeWorkspaceRemovalError +} from '../../shared/recipe-host-removal' import { mapRuntimeError } from '../runtime/rpc/errors' import { describeRecipeWorkspaceEnvironments, @@ -268,3 +275,86 @@ describe('recipe workspace removal on the provisioning host', () => { }) }) }) + +describe('Orca Cloud: Delete destroys the sandbox or fails visibly, never "kept" (REG-12)', () => { + const cloud: Partial = { + recipe: { id: 'cloud', name: 'Orca Cloud', create: 'orca-cloud create', provider: 'orca-cloud' } + } + const removedCheckout = (ports: ReturnType) => + ports.call.mock.calls.some(([, method]) => method === 'worktree.rm') + + it('the worktree list fails: an error, and nothing was deleted (no checkout removal, no destroy)', async () => { + seed('orca-worktree', cloud) + const ports = makePorts({ 'worktree.list': failed('runtime_unavailable') }) + await expect(removeRecipeWorkspace(root, params, ports)).rejects.toMatchObject({ + code: CLOUD_DELETE_NOT_SENT, + message: CLOUD_DELETE_NOT_SENT_UNCHECKED + }) + expect(removedCheckout(ports)).toBe(false) + expect(ports.cleanup).not.toHaveBeenCalled() + }) + + it('a truncated list is no proof either', async () => { + seed('orca-worktree', cloud) + const ports = makePorts({ 'worktree.list': ok({ truncated: true, worktrees: [] }) }) + await expect(removeRecipeWorkspace(root, params, ports)).rejects.toMatchObject({ + message: CLOUD_DELETE_NOT_SENT_UNCHECKED + }) + expect(removedCheckout(ports)).toBe(false) + expect(ports.cleanup).not.toHaveBeenCalled() + }) + + it('other workspaces still on the sandbox: an error naming that, nothing deleted', async () => { + seed('orca-worktree', cloud) + const ports = makePorts({ + 'worktree.list': ok({ + worktrees: [...onlyMain.worktrees, { id: 'repo::/workspace/repo-other' }] + }) + }) + await expect(removeRecipeWorkspace(root, params, ports)).rejects.toMatchObject({ + message: CLOUD_DELETE_NOT_SENT_IN_USE + }) + expect(removedCheckout(ports)).toBe(false) + expect(ports.cleanup).not.toHaveBeenCalled() + }) + + it('a sandbox nothing would destroy: refused up front, nothing deleted', async () => { + seed('orca-worktree', { ...cloud, cleanupStatus: 'disabled' }) + const ports = makePorts() + await expect(removeRecipeWorkspace(root, params, ports)).rejects.toMatchObject({ + message: CLOUD_DELETE_NOT_SENT_NO_DESTROY + }) + expect(ports.call).not.toHaveBeenCalled() + expect(ports.cleanup).not.toHaveBeenCalled() + }) + + it('a "cleaned" record without a destroy still never reads as deleted (guard)', async () => { + seed('orca-worktree', cloud) + const ports = makePorts({}, 'disabled') + await expect(removeRecipeWorkspace(root, params, ports)).rejects.toMatchObject({ + message: CLOUD_DELETE_NOT_SENT_NO_DESTROY + }) + }) + + it('a sandbox that is this workspace’s alone is destroyed, and only then reports success', async () => { + seed('orca-worktree', cloud) + const ports = makePorts() + await expect(removeRecipeWorkspace(root, params, ports)).resolves.toMatchObject({ + removed: true, + host: { state: 'destroyed' } + }) + expect(removedCheckout(ports)).toBe(true) + expect(ports.cleanup).toHaveBeenCalledWith('vm-a') + }) + + it('the not-sent errors are recognized for Try again', () => { + for (const copy of [ + CLOUD_DELETE_NOT_SENT_UNCHECKED, + CLOUD_DELETE_NOT_SENT_IN_USE, + CLOUD_DELETE_NOT_SENT_NO_DESTROY + ]) { + expect(isCloudDeleteNotSentError(`Error: ${copy}`)).toBe(true) + } + expect(isCloudDeleteNotSentError('Environment destroy failed.')).toBe(false) + }) +}) diff --git a/src/main/ephemeral-vm/workspace-removal.ts b/src/main/ephemeral-vm/workspace-removal.ts index 8d845dd68c3..d23c3891b43 100644 --- a/src/main/ephemeral-vm/workspace-removal.ts +++ b/src/main/ephemeral-vm/workspace-removal.ts @@ -8,6 +8,10 @@ import { import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes' import type { RuntimeRpcResponse } from '../../shared/runtime-rpc-envelope' import { + CLOUD_DELETE_NOT_SENT, + CLOUD_DELETE_NOT_SENT_IN_USE, + CLOUD_DELETE_NOT_SENT_NO_DESTROY, + CLOUD_DELETE_NOT_SENT_UNCHECKED, RecipeWorkspaceRemovalError, type RecipeEnvironmentDescription, type RecipeHostRemovalState @@ -167,6 +171,13 @@ export async function removeRecipeWorkspace( throw new Error('Recipe workspace ownership changed. Refresh and try again.') } const environmentId = runtime.runtimeEnvironmentId + const isOrcaCloud = runtime.recipe?.provider === 'orca-cloud' + if (isOrcaCloud && (runtime.recipe?.destroyDisabled || runtime.cleanupStatus === 'disabled')) { + // REG-12: nothing would destroy the sandbox, so refuse before touching anything. + throw new RecipeWorkspaceRemovalError(CLOUD_DELETE_NOT_SENT, CLOUD_DELETE_NOT_SENT_NO_DESTROY, { + runtimeId: runtime.id + }) + } const { destroyHost: _destroyHost, ...forwarded } = params let checkoutResult: Record = {} // A provisioned root is the VM's primary checkout, protected from ordinary Git deletion. @@ -177,6 +188,31 @@ export async function removeRecipeWorkspace( "This workspace's environment is asleep. Open the workspace to wake it, then delete it again." ) } + // Other workspaces on the same environment keep it alive; no answer is not proof it's empty. + const otherWorkspaces = async (): Promise => { + const listed = await ports.call(environmentId, 'worktree.list', {}) + const parsedList = listed.ok ? Worktrees.safeParse(listed.result).data : undefined + return parsedList && parsedList.truncated !== true + ? parsedList.worktrees.filter( + (worktree) => + worktree.isMainWorktree !== true && + worktree.removing !== true && + worktree.id !== params.worktree.slice(3) + ) + : undefined + } + // REG-12: deleting an Orca Cloud workspace means deleting its sandbox, so prove the sandbox is + // its alone before touching anything. A refusal then really deleted nothing (Try again is safe). + if (isOrcaCloud) { + const others = await otherWorkspaces() + if (!others || others.length > 0) { + throw new RecipeWorkspaceRemovalError( + CLOUD_DELETE_NOT_SENT, + others ? CLOUD_DELETE_NOT_SENT_IN_USE : CLOUD_DELETE_NOT_SENT_UNCHECKED, + { runtimeId: runtime.id } + ) + } + } const response = await ports.call(environmentId, 'worktree.rm', { ...forwarded, hostId: 'local', @@ -202,20 +238,13 @@ export async function removeRecipeWorkspace( } checkoutResult = { ...removed, removed: true } } - // Other workspaces on the same environment keep it alive; no answer is not proof it's empty. - const listed = await ports.call(environmentId, 'worktree.list', {}) - const parsedList = listed.ok ? Worktrees.safeParse(listed.result).data : undefined - const remaining = - parsedList && parsedList.truncated !== true - ? parsedList.worktrees.filter( - (worktree) => - worktree.isMainWorktree !== true && - worktree.removing !== true && - worktree.id !== params.worktree.slice(3) - ) - : undefined - if (!remaining || remaining.length > 0) { - return { ...checkoutResult, removed: true, host: { runtimeId: runtime.id, state: 'kept' } } + // A recipe VM shared by several workspaces stays while others live on it. (Orca Cloud proved + // its sandbox was this workspace's alone above, so its delete always goes on to destroy.) + if (!isOrcaCloud) { + const remaining = await otherWorkspaces() + if (!remaining || remaining.length > 0) { + return { ...checkoutResult, removed: true, host: { runtimeId: runtime.id, state: 'kept' } } + } } } const cleaned = await orderDestroy(() => ports.cleanup(runtime.id)) @@ -226,6 +255,12 @@ export async function removeRecipeWorkspace( { runtimeId: runtime.id } ) } + if (isOrcaCloud && cleaned.cleanupStatus === 'disabled') { + // Guard only (checked up front): a "cleaned" record without a destroy is never "Deleted". + throw new RecipeWorkspaceRemovalError(CLOUD_DELETE_NOT_SENT, CLOUD_DELETE_NOT_SENT_NO_DESTROY, { + runtimeId: runtime.id + }) + } return { ...checkoutResult, removed: true, diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-worktree-failure-toast.retry.test.tsx b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-worktree-failure-toast.retry.test.tsx new file mode 100644 index 00000000000..55baef73c3a --- /dev/null +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-worktree-failure-toast.retry.test.tsx @@ -0,0 +1,78 @@ +// @vitest-environment happy-dom +// REG-12: an Orca Cloud delete that failed before any sandbox delete was sent offers Try again. +import type React from 'react' +import { cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { + CLOUD_DELETE_NOT_SENT_IN_USE, + CLOUD_DELETE_NOT_SENT_NO_DESTROY, + CLOUD_DELETE_NOT_SENT_UNCHECKED +} from '../../../../../../shared/recipe-host-removal' + +const shown = vi.hoisted((): { title: string; description: React.ReactNode; duration: number } => ({ + title: '', + description: null, + duration: 0 +})) +vi.mock('sonner', () => { + const show = (title: string, options: { description: React.ReactNode; duration: number }) => { + shown.title = title + shown.description = options.description + shown.duration = options.duration + } + return { + toast: Object.assign(vi.fn(show), { error: vi.fn(show), info: vi.fn(show), dismiss: vi.fn() }) + } +}) + +import { showDeleteWorktreeFailureToast } from './delete-worktree-failure-toast' + +afterEach(cleanup) + +function showFor(error: string, onRetry = vi.fn()) { + showDeleteWorktreeFailureToast({ + error, + canForceDelete: false, + forceDeleteReason: null, + onViewChanges: vi.fn(), + onForceDelete: vi.fn(), + onDeleteAnyway: vi.fn(), + onRetry, + worktreeId: 'wt-cloud', + worktreeName: 'ik-actions' + }) + render(<>{shown.description}) + return onRetry +} + +describe('delete failure toast: Try again', () => { + it('offers Try again when no sandbox delete was sent, and it re-runs the delete', () => { + const onRetry = showFor(`Error: ${CLOUD_DELETE_NOT_SENT_UNCHECKED}`) + expect(shown.title).toBe("Couldn't delete ik-actions") + expect( + screen.getByText("Orca couldn't check the sandbox, so nothing was deleted.") + ).toBeTruthy() + // It stays until the user acts: deleting must not silently time out. + expect(shown.duration).toBe(Infinity) + fireEvent.click(screen.getByRole('button', { name: 'Try again' })) + expect(onRetry).toHaveBeenCalledOnce() + }) + + it('names the case: others still on the sandbox, or no delete went through', () => { + showFor(CLOUD_DELETE_NOT_SENT_IN_USE) + expect( + screen.getByText( + 'Other workspaces still run in this sandbox, so nothing was deleted. Delete them first.' + ) + ).toBeTruthy() + expect(screen.getByRole('button', { name: 'Try again' })).toBeTruthy() + cleanup() + showFor(CLOUD_DELETE_NOT_SENT_NO_DESTROY) + expect(screen.getByText("The delete didn't go through, so nothing was deleted.")).toBeTruthy() + }) + + it('does not offer it for other failures (a retry could repeat a partial destroy)', () => { + showFor('Environment destroy failed. The environment may still be running.') + expect(screen.queryByRole('button', { name: 'Try again' })).toBeNull() + }) +}) diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-worktree-failure-toast.tsx b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-worktree-failure-toast.tsx index dabd71a058f..0c07c836461 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-worktree-failure-toast.tsx +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-worktree-failure-toast.tsx @@ -5,6 +5,12 @@ import { useTranslate } from '@/i18n/use-translate' import { DeleteNestedWorktreesDialog } from './DeleteNestedWorktreesDialog' import { WorktreeForceDeleteButton } from './WorktreeForceDeleteButton' import { isNestedWorktreeRemovalError } from '../../../../../../shared/worktree/nested-removal' +import { + CLOUD_DELETE_NOT_SENT_IN_USE, + CLOUD_DELETE_NOT_SENT_NO_DESTROY, + isCloudDeleteNotSentError +} from '../../../../../../shared/recipe-host-removal' +import { translate } from '@/i18n/i18n' import { composeWorktreeHostIdentity } from '../../../../../../shared/worktree/host-qualified-identity' import type { ExecutionHostId } from '../../../../../../shared/execution-host' import { @@ -26,6 +32,8 @@ type DeleteWorktreeFailureToastOptions = { onForceDelete: () => void onAlwaysForceDelete?: () => Promise onDeleteAnyway: () => void + /** REG-12: a delete that failed before any sandbox delete was sent offers Try again. */ + onRetry?: () => void worktreeId: string executionHostId?: ExecutionHostId | null worktreeName: string @@ -52,6 +60,7 @@ function DeleteWorktreeFailureToastBody({ onForceDelete, onAlwaysForceDelete, onDeleteAnyway, + onRetry, toastId, nestedRemovalTarget, worktreeName, @@ -65,6 +74,7 @@ function DeleteWorktreeFailureToastBody({ onForceDelete: () => void onAlwaysForceDelete?: () => Promise onDeleteAnyway: () => void + onRetry?: () => void toastId: string nestedRemovalTarget?: WorktreeRemovalTarget worktreeName: string @@ -79,6 +89,10 @@ function DeleteWorktreeFailureToastBody({ toast.dismiss(toastId) onDeleteAnyway() } + const retry = (): void => { + toast.dismiss(toastId) + onRetry?.() + } return (
@@ -106,6 +120,11 @@ function DeleteWorktreeFailureToastBody({ onAlwaysForceDelete={onAlwaysForceDelete} /> ) : null} + {onRetry ? ( + + ) : null} {canWaiveArchiveHook ? (