fix(cloud): deleting an asleep cloud workspace no longer asks its sandbox for archive hooks (31 s of request timeouts)

This commit is contained in:
Neil
2026-11-09 21:33:25 -08:00
parent ef689f5a50
commit e810a54dd2
3 changed files with 89 additions and 16 deletions
@@ -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<NonNullable<typeof window.api.ephemeralVm.preflightCloudDelete>>
>
/** 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<string, { at: number; result: Promise<Preflight> }>()
function preflightCloudDelete(
runtimeEnvironmentId: string,
options: { consume?: boolean } = {}
): Promise<Preflight> | 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<boolean> {
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<boolean> {
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))
}
@@ -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' })
})
@@ -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,