From c33310fecbb1ed3953bc4cf9d09c2bd637ac37f9 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 24 Apr 2026 17:39:31 +0000 Subject: [PATCH] Prevent stale NemoClaw registry resurrection --- app/api/sandbox/delete/route.ts | 87 +++++++++++++++++++++++++++++-- app/api/telemetry/real/route.ts | 15 +++--- tests/sandbox-lifecycle-check.mjs | 8 +++ 3 files changed, 98 insertions(+), 12 deletions(-) diff --git a/app/api/sandbox/delete/route.ts b/app/api/sandbox/delete/route.ts index ab9f864..49d743b 100644 --- a/app/api/sandbox/delete/route.ts +++ b/app/api/sandbox/delete/route.ts @@ -6,6 +6,7 @@ import { resolveSandboxRef } from "@/app/lib/openshellHost" const execFileAsync = promisify(execFile) const HOME = process.env.HOME || "" const OPENSHELL_BIN = process.env.OPENSHELL_BIN || `${HOME}/.local/bin/openshell` +const NEMOCLAW_BIN = process.env.NEMOCLAW_BIN || `${HOME}/NemoClaw/bin/nemoclaw.js` const HOST_PATH = [ `${HOME}/.local/bin`, `${HOME}/.nvm/versions/node/v22.22.2/bin`, @@ -92,6 +93,57 @@ async function deleteSandbox(sandboxName: string) { } } +function isSandboxNotFound(output: string) { + return /sandbox not found|status:\s*NotFound|not present in the live OpenShell gateway/i.test(output) +} + +function isNemoClawSandboxNotRegistered(output: string, sandboxName: string) { + return new RegExp(`Unknown command:\\s*${sandboxName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, "i").test(output) +} + +async function cleanupNemoClawSandbox(sandboxName: string) { + const startedAt = Date.now() + console.log(`[sandbox/delete] nemoclaw-cleanup:start sandbox=${sandboxName}`) + try { + const { stdout, stderr } = await execFileAsync(NEMOCLAW_BIN, [sandboxName, "destroy", "--yes"], { + cwd: `${HOME}/NemoClaw`, + env: { + ...process.env, + PATH: HOST_PATH, + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", + NO_COLOR: "1", + CLICOLOR: "0", + CLICOLOR_FORCE: "0", + }, + timeout: 90000, + maxBuffer: 20 * 1024 * 1024, + }) + console.log(`[sandbox/delete] nemoclaw-cleanup:done sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)}`) + return { ok: true as const, stdout: String(stdout).trim(), stderr: String(stderr).trim() } + } catch (error: any) { + const message = error instanceof Error ? error.message : String(error ?? "NemoClaw cleanup failed") + const stdout = String(error?.stdout || "").trim() + const stderr = String(error?.stderr || "").trim() + const output = [message, stdout, stderr].filter(Boolean).join("\n") + if (isNemoClawSandboxNotRegistered(output, sandboxName)) { + console.log(`[sandbox/delete] nemoclaw-cleanup:already-gone sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)}`) + return { + ok: true as const, + stdout, + stderr, + note: "NemoClaw registry already had no matching sandbox.", + } + } + console.log(`[sandbox/delete] nemoclaw-cleanup:error sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)} message=${message}`) + return { + ok: false as const, + stdout, + stderr, + error: message, + } + } +} + async function waitForSandboxDeleted(sandboxName: string, timeoutMs: number, intervalMs: number) { const startedAt = Date.now() let attempts = 0 @@ -139,7 +191,11 @@ export async function POST(request: Request) { const body = await request.json() const target = await resolveDeleteTarget(parseDeleteTarget(body)) const result = await deleteSandbox(target.sandboxName) - if (!result.ok) { + const deleteOutput = [result.error, result.stdout, result.stderr].filter(Boolean).join("\n") + const openShellAlreadyGone = !result.ok && isSandboxNotFound(deleteOutput) + const cleanup = await cleanupNemoClawSandbox(target.sandboxName) + + if (!result.ok && !openShellAlreadyGone) { return NextResponse.json({ ok: false, deleted: false, @@ -151,11 +207,29 @@ export async function POST(request: Request) { error: result.error, stdout: result.stdout, stderr: result.stderr, + nemoclaw: cleanup, + }, { status: 500 }) + } + + if (!cleanup.ok) { + return NextResponse.json({ + ok: false, + deleted: false, + requested: target.requested, + sandboxName: target.sandboxName, + sandboxId: target.sandboxId, + resolved: target.resolved, + openShellAlreadyGone, + openShell: result, + error: cleanup.error, + stdout: cleanup.stdout, + stderr: cleanup.stderr, + nemoclaw: cleanup, }, { status: 500 }) } const deletion = await waitForSandboxDeleted(target.sandboxName, 45000, 1500) - const deleted = deletion.deleted + const deleted = deletion.deleted || openShellAlreadyGone console.log(`[sandbox/delete] request:complete sandbox=${target.sandboxName} deleted=${deleted} elapsedMs=${elapsedMs(startedAt)}`) return NextResponse.json({ ok: deleted, @@ -164,10 +238,13 @@ export async function POST(request: Request) { sandboxName: target.sandboxName, sandboxId: target.sandboxId, resolved: target.resolved, - stdout: result.stdout, - stderr: result.stderr, + stdout: [result.stdout, cleanup.stdout].filter(Boolean).join("\n\n"), + stderr: [result.stderr, cleanup.stderr].filter(Boolean).join("\n\n"), + openShellAlreadyGone, + openShell: result, + nemoclaw: cleanup, deletion, - note: deleted ? "Sandbox delete completed and inventory no longer reports it." : "Sandbox delete command completed, but inventory still reports the sandbox.", + note: deleted ? "Sandbox delete completed and NemoClaw registry cleanup ran." : "Sandbox delete command completed, but inventory still reports the sandbox.", }, { status: deleted ? 200 : 202 }) } catch (error) { const message = error instanceof Error ? error.message : "Sandbox delete failed" diff --git a/app/api/telemetry/real/route.ts b/app/api/telemetry/real/route.ts index c6eb9f8..6d44b5b 100644 --- a/app/api/telemetry/real/route.ts +++ b/app/api/telemetry/real/route.ts @@ -340,14 +340,15 @@ async function readSandbox(name: string, defaultSandboxNames: Set): Prom export async function GET() { try { - const [{ stdout: sandboxListStdout }, nemoclawListResult, nemoclawStatusResult] = await Promise.all([ - execOpenShell(["sandbox", "list"]), - execNemoclaw(["list"]).catch(() => null), - execNemoclaw(["status"]).catch(() => null), - ]) - - const defaultSandboxNames = parseDefaultSandboxNames(nemoclawListResult?.stdout ?? "") + const { stdout: sandboxListStdout } = await execOpenShell(["sandbox", "list"]) const names = parseOpenShellSandboxNames(sandboxListStdout) + const [nemoclawListResult, nemoclawStatusResult] = names.length > 0 + ? await Promise.all([ + execNemoclaw(["list"]).catch(() => null), + execNemoclaw(["status"]).catch(() => null), + ]) + : [null, null] + const defaultSandboxNames = parseDefaultSandboxNames(nemoclawListResult?.stdout ?? "") const results = await Promise.all(names.map((name) => readSandbox(name, defaultSandboxNames))) const sandboxes = results.map((result) => result.summary) const items = results.map((result) => result.pod) diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index c0fecff..1ba49a3 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -5,12 +5,14 @@ import path from 'node:path' const root = process.cwd() const createRoutePath = path.join(root, 'app/api/sandbox/create/route.ts') const deleteRoutePath = path.join(root, 'app/api/sandbox/delete/route.ts') +const telemetryRoutePath = path.join(root, 'app/api/telemetry/real/route.ts') const hookPath = path.join(root, 'app/hooks/useSandboxInventory.ts') const pagePath = path.join(root, 'app/page.tsx') const configPanelPath = path.join(root, 'app/components/ConfigurationPanel.tsx') const createRouteSource = await readFile(createRoutePath, 'utf8') const deleteRouteSource = await readFile(deleteRoutePath, 'utf8') +const telemetryRouteSource = await readFile(telemetryRoutePath, 'utf8') const hookSource = await readFile(hookPath, 'utf8') const pageSource = await readFile(pagePath, 'utf8') const configPanelSource = await readFile(configPanelPath, 'utf8') @@ -36,6 +38,12 @@ assert.match(deleteRouteSource, /validateSandboxName/, 'delete route must valida 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(deleteRouteSource, /cleanupNemoClawSandbox/, 'delete route must clean up NemoClaw registry state after deleting from OpenShell') +assert.match(deleteRouteSource, /\[sandboxName, "destroy", "--yes"\]/, 'delete route must run the non-interactive NemoClaw destroy workflow') +assert.match(deleteRouteSource, /openShellAlreadyGone/, 'delete route must tolerate sandboxes that OpenShell already deleted while NemoClaw still has registry state') +assert.match(deleteRouteSource, /isNemoClawSandboxNotRegistered/, 'delete route must tolerate NemoClaw registry entries that were already cleaned up') +assert.match(telemetryRouteSource, /const names = parseOpenShellSandboxNames\(sandboxListStdout\)/, 'telemetry must derive inventory from OpenShell before consulting NemoClaw') +assert.match(telemetryRouteSource, /names\.length > 0[\s\S]*execNemoclaw\(\["list"\]\)[\s\S]*execNemoclaw\(\["status"\]\)/, 'telemetry must skip NemoClaw list/status when OpenShell reports zero live sandboxes') 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')