Harden sandbox destroy workflow
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { NextResponse } from "next/server"
|
import { NextResponse } from "next/server"
|
||||||
import { execFile } from "node:child_process"
|
import { execFile } from "node:child_process"
|
||||||
import { promisify } from "node:util"
|
import { promisify } from "node:util"
|
||||||
|
import { resolveSandboxRef } from "@/app/lib/openshellHost"
|
||||||
|
|
||||||
const execFileAsync = promisify(execFile)
|
const execFileAsync = promisify(execFile)
|
||||||
const HOME = process.env.HOME || ""
|
const HOME = process.env.HOME || ""
|
||||||
@@ -31,6 +32,36 @@ function validateSandboxName(name: string) {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseDeleteTarget(body: any) {
|
||||||
|
const raw = typeof body?.sandboxName === "string"
|
||||||
|
? body.sandboxName.trim()
|
||||||
|
: typeof body?.sandboxId === "string"
|
||||||
|
? body.sandboxId.trim()
|
||||||
|
: ""
|
||||||
|
if (!raw) throw new Error("sandbox name or id is required")
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resolveDeleteTarget(ref: string) {
|
||||||
|
try {
|
||||||
|
const sandbox = await resolveSandboxRef(ref)
|
||||||
|
return {
|
||||||
|
requested: ref,
|
||||||
|
sandboxName: validateSandboxName(sandbox.name),
|
||||||
|
sandboxId: sandbox.id,
|
||||||
|
resolved: true,
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
requested: ref,
|
||||||
|
sandboxName: validateSandboxName(ref),
|
||||||
|
sandboxId: null,
|
||||||
|
resolved: false,
|
||||||
|
resolveError: error instanceof Error ? error.message : String(error ?? "Sandbox lookup failed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function deleteSandbox(sandboxName: string) {
|
async function deleteSandbox(sandboxName: string) {
|
||||||
const startedAt = Date.now()
|
const startedAt = Date.now()
|
||||||
console.log(`[sandbox/delete] command:start sandbox=${sandboxName}`)
|
console.log(`[sandbox/delete] command:start sandbox=${sandboxName}`)
|
||||||
@@ -39,10 +70,12 @@ async function deleteSandbox(sandboxName: string) {
|
|||||||
env: {
|
env: {
|
||||||
...process.env,
|
...process.env,
|
||||||
PATH: HOST_PATH,
|
PATH: HOST_PATH,
|
||||||
|
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||||
NO_COLOR: "1",
|
NO_COLOR: "1",
|
||||||
CLICOLOR: "0",
|
CLICOLOR: "0",
|
||||||
CLICOLOR_FORCE: "0",
|
CLICOLOR_FORCE: "0",
|
||||||
},
|
},
|
||||||
|
timeout: 60000,
|
||||||
maxBuffer: 20 * 1024 * 1024,
|
maxBuffer: 20 * 1024 * 1024,
|
||||||
})
|
})
|
||||||
console.log(`[sandbox/delete] command:done sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)}`)
|
console.log(`[sandbox/delete] command:done sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)}`)
|
||||||
@@ -59,37 +92,86 @@ async function deleteSandbox(sandboxName: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function waitForSandboxDeleted(sandboxName: string, timeoutMs: number, intervalMs: number) {
|
||||||
|
const startedAt = Date.now()
|
||||||
|
let attempts = 0
|
||||||
|
let lastError: string | null = null
|
||||||
|
|
||||||
|
while (elapsedMs(startedAt) < timeoutMs) {
|
||||||
|
attempts += 1
|
||||||
|
try {
|
||||||
|
await resolveSandboxRef(sandboxName)
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
deleted: true as const,
|
||||||
|
attempts,
|
||||||
|
elapsedMs: elapsedMs(startedAt),
|
||||||
|
lastError: error instanceof Error ? error.message : String(error ?? "Sandbox lookup failed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, intervalMs))
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await resolveSandboxRef(sandboxName)
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
deleted: true as const,
|
||||||
|
attempts,
|
||||||
|
elapsedMs: elapsedMs(startedAt),
|
||||||
|
lastError: error instanceof Error ? error.message : String(error ?? "Sandbox lookup failed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
deleted: false as const,
|
||||||
|
attempts,
|
||||||
|
elapsedMs: elapsedMs(startedAt),
|
||||||
|
lastError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const startedAt = Date.now()
|
const startedAt = Date.now()
|
||||||
console.log("[sandbox/delete] request:start")
|
console.log("[sandbox/delete] request:start")
|
||||||
try {
|
try {
|
||||||
const body = await request.json()
|
const body = await request.json()
|
||||||
const sandboxName = validateSandboxName(
|
const target = await resolveDeleteTarget(parseDeleteTarget(body))
|
||||||
typeof body?.sandboxName === "string" ? body.sandboxName.trim() : typeof body?.sandboxId === "string" ? body.sandboxId.trim() : "",
|
const result = await deleteSandbox(target.sandboxName)
|
||||||
)
|
|
||||||
const result = await deleteSandbox(sandboxName)
|
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
ok: false,
|
ok: false,
|
||||||
deleted: false,
|
deleted: false,
|
||||||
sandboxName,
|
requested: target.requested,
|
||||||
|
sandboxName: target.sandboxName,
|
||||||
|
sandboxId: target.sandboxId,
|
||||||
|
resolved: target.resolved,
|
||||||
|
resolveError: target.resolveError,
|
||||||
error: result.error,
|
error: result.error,
|
||||||
stdout: result.stdout,
|
stdout: result.stdout,
|
||||||
stderr: result.stderr,
|
stderr: result.stderr,
|
||||||
}, { status: 500 })
|
}, { status: 500 })
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`[sandbox/delete] request:complete sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)}`)
|
const deletion = await waitForSandboxDeleted(target.sandboxName, 45000, 1500)
|
||||||
|
const deleted = deletion.deleted
|
||||||
|
console.log(`[sandbox/delete] request:complete sandbox=${target.sandboxName} deleted=${deleted} elapsedMs=${elapsedMs(startedAt)}`)
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
ok: true,
|
ok: deleted,
|
||||||
deleted: true,
|
deleted,
|
||||||
sandboxName,
|
requested: target.requested,
|
||||||
|
sandboxName: target.sandboxName,
|
||||||
|
sandboxId: target.sandboxId,
|
||||||
|
resolved: target.resolved,
|
||||||
stdout: result.stdout,
|
stdout: result.stdout,
|
||||||
stderr: result.stderr,
|
stderr: result.stderr,
|
||||||
})
|
deletion,
|
||||||
|
note: deleted ? "Sandbox delete completed and inventory no longer reports it." : "Sandbox delete command completed, but inventory still reports the sandbox.",
|
||||||
|
}, { status: deleted ? 200 : 202 })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : "Sandbox delete failed"
|
const message = error instanceof Error ? error.message : "Sandbox delete failed"
|
||||||
const status = /required|must be|too long/.test(message) ? 400 : 500
|
const status = /required|must be|too long|name or id/.test(message) ? 400 : 500
|
||||||
console.log(`[sandbox/delete] request:error elapsedMs=${elapsedMs(startedAt)} message=${message}`)
|
console.log(`[sandbox/delete] request:error elapsedMs=${elapsedMs(startedAt)} message=${message}`)
|
||||||
return NextResponse.json({ ok: false, deleted: false, error: message }, { status })
|
return NextResponse.json({ ok: false, deleted: false, error: message }, { status })
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-3
@@ -76,6 +76,17 @@ export default function Dashboard() {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const refreshUntilSandboxGone = async (sandboxId: string, sandboxName: string) => {
|
||||||
|
for (let attempt = 0; attempt < 10; attempt += 1) {
|
||||||
|
const latest = await refresh({ force: true })
|
||||||
|
const stillPresent = latest.find((item) => item.id === sandboxId || item.name === sandboxName)
|
||||||
|
if (!stillPresent) return { latest, gone: true }
|
||||||
|
await sleep(1500)
|
||||||
|
}
|
||||||
|
|
||||||
|
return { latest: await refresh({ force: true }), gone: false }
|
||||||
|
}
|
||||||
|
|
||||||
const handleCreateSuccess = async (createdSandboxId: string) => {
|
const handleCreateSuccess = async (createdSandboxId: string) => {
|
||||||
setIsCreateMode(false)
|
setIsCreateMode(false)
|
||||||
setIsDestroyMode(false)
|
setIsDestroyMode(false)
|
||||||
@@ -102,10 +113,9 @@ export default function Dashboard() {
|
|||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
if (!response.ok) throw new Error([data.error, data.stdout, data.stderr].filter(Boolean).join('\n\n') || 'Failed to destroy sandbox')
|
if (!response.ok) throw new Error([data.error, data.stdout, data.stderr].filter(Boolean).join('\n\n') || 'Failed to destroy sandbox')
|
||||||
|
|
||||||
const latest = await refresh({ force: true })
|
const { gone } = await refreshUntilSandboxGone(deletingSandboxId, sandboxName)
|
||||||
const stillPresent = latest.find((item) => item.id === deletingSandboxId || item.name === sandboxName)
|
|
||||||
setDashboardSession((current) => updateDashboardSessionSelection(current, current.selectedSandboxId === deletingSandboxId ? null : current.selectedSandboxId))
|
setDashboardSession((current) => updateDashboardSessionSelection(current, current.selectedSandboxId === deletingSandboxId ? null : current.selectedSandboxId))
|
||||||
setLifecycleMessage(stillPresent ? `Delete started for ${sandboxName}. Inventory still reports it while cleanup finishes.` : `Sandbox ${sandboxName} destroyed.`)
|
setLifecycleMessage(gone ? `Sandbox ${sandboxName} destroyed.` : `Delete started for ${sandboxName}. Inventory still reports it while cleanup finishes.`)
|
||||||
setShowDeleteConfirm(false)
|
setShowDeleteConfirm(false)
|
||||||
setDeletingSandboxId(null)
|
setDeletingSandboxId(null)
|
||||||
setIsDestroyMode(false)
|
setIsDestroyMode(false)
|
||||||
|
|||||||
@@ -33,10 +33,14 @@ assert.match(hookSource, /requestIdRef = useRef\(0\)/, 'inventory refresh must g
|
|||||||
assert.match(deleteRouteSource, /OPENSHELL_BIN.*openshell/, 'delete route must use the OpenShell CLI')
|
assert.match(deleteRouteSource, /OPENSHELL_BIN.*openshell/, 'delete route must use the OpenShell CLI')
|
||||||
assert.match(deleteRouteSource, /\["sandbox", "delete", sandboxName\]/, 'delete route must call openshell sandbox delete with the sandbox name')
|
assert.match(deleteRouteSource, /\["sandbox", "delete", sandboxName\]/, 'delete route must call openshell sandbox delete with the sandbox name')
|
||||||
assert.match(deleteRouteSource, /validateSandboxName/, 'delete route must validate sandbox names before execution')
|
assert.match(deleteRouteSource, /validateSandboxName/, 'delete route must validate sandbox names before execution')
|
||||||
|
assert.match(deleteRouteSource, /resolveSandboxRef/, 'delete route must resolve sandbox ids to names before deleting')
|
||||||
|
assert.match(deleteRouteSource, /OPENSHELL_GATEWAY: process\.env\.OPENSHELL_GATEWAY \|\| "nemoclaw"/, 'delete route must use the NemoClaw gateway context by default')
|
||||||
|
assert.match(deleteRouteSource, /waitForSandboxDeleted/, 'delete route must poll authoritative inventory until the sandbox is gone')
|
||||||
|
|
||||||
assert.match(pageSource, /fetch\('\/api\/sandbox\/delete'/, 'destroy workflow must call the real sandbox delete endpoint')
|
assert.match(pageSource, /fetch\('\/api\/sandbox\/delete'/, 'destroy workflow must call the real sandbox delete endpoint')
|
||||||
assert.match(pageSource, /refresh\(\{ force: true \}\)/, 'create and destroy workflows must force fresh inventory reads')
|
assert.match(pageSource, /refresh\(\{ force: true \}\)/, 'create and destroy workflows must force fresh inventory reads')
|
||||||
assert.match(pageSource, /refreshUntilSandboxVisible/, 'create workflow must wait for the new sandbox to appear in inventory')
|
assert.match(pageSource, /refreshUntilSandboxVisible/, 'create workflow must wait for the new sandbox to appear in inventory')
|
||||||
|
assert.match(pageSource, /refreshUntilSandboxGone/, 'destroy workflow must wait for deleted sandboxes to leave inventory')
|
||||||
assert.doesNotMatch(pageSource, /console\.log\('Destroying sandbox:'/, 'destroy workflow must not be a UI-only placeholder')
|
assert.doesNotMatch(pageSource, /console\.log\('Destroying sandbox:'/, 'destroy workflow must not be a UI-only placeholder')
|
||||||
assert.doesNotMatch(configPanelSource, /onInventoryRefresh/, 'create panel must leave post-create inventory orchestration to the parent')
|
assert.doesNotMatch(configPanelSource, /onInventoryRefresh/, 'create panel must leave post-create inventory orchestration to the parent')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user