Fix sandbox delete and inference routing

This commit is contained in:
mmckeen-nv
2026-04-29 16:11:49 -07:00
parent e55dc7b9e0
commit 15d59846f1
4 changed files with 71 additions and 54 deletions
+30 -38
View File
@@ -1,8 +1,10 @@
import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { mkdir, readFile, writeFile } from "node:fs/promises"
import path from "node:path"
import { promisify } from "node:util"
import { resolveSandboxRef } from "@/app/lib/openshellHost"
import { NEMOCLAW_BIN, NEMOCLAW_CWD, OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
import { OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -82,56 +84,46 @@ function isGatewayRecoverable(output: string) {
return /Unknown gateway|Deploy it first|transport error|tcp connect error|Connection refused|invalid peer certificate|BadSignature/i.test(output)
}
function isValidNemoClawSandboxName(value: string) {
return /^[a-z][a-z0-9-]*[a-z0-9]$|^[a-z]$/.test(value)
}
function isNemoClawSandboxNotRegistered(output: string, sandboxName: string) {
return new RegExp(`Unknown command:\\s*${sandboxName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, "i").test(output)
}
const NEMOCLAW_REGISTRY_PATH = path.join(process.env.HOME || "/tmp", ".nemoclaw", "sandboxes.json")
async function cleanupNemoClawSandbox(sandboxName: string) {
if (!isValidNemoClawSandboxName(sandboxName)) {
try {
const raw = await readFile(NEMOCLAW_REGISTRY_PATH, "utf8")
const registry = JSON.parse(raw)
const sandboxes = registry && typeof registry === "object" && registry.sandboxes && typeof registry.sandboxes === "object"
? registry.sandboxes
: {}
const existed = Object.prototype.hasOwnProperty.call(sandboxes, sandboxName)
if (existed) delete sandboxes[sandboxName]
const remaining = Object.keys(sandboxes)
const defaultSandbox = registry.defaultSandbox === sandboxName
? remaining[0] || null
: (registry.defaultSandbox && sandboxes[registry.defaultSandbox] ? registry.defaultSandbox : remaining[0] || null)
const next = { ...registry, sandboxes, defaultSandbox }
await mkdir(path.dirname(NEMOCLAW_REGISTRY_PATH), { recursive: true, mode: 0o700 })
await writeFile(NEMOCLAW_REGISTRY_PATH, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 })
return {
ok: true as const,
stdout: "",
stderr: "",
note: "Skipped NemoClaw registry cleanup because the target is not a valid NemoClaw sandbox name.",
note: existed
? "Pruned NemoClaw registry entry without destroying the shared OpenShell gateway."
: "NemoClaw registry already had no matching sandbox entry.",
}
}
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: NEMOCLAW_CWD,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
}),
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)}`)
} catch (error) {
const message = error instanceof Error ? error.message : String(error ?? "NemoClaw registry cleanup failed")
if (/ENOENT/.test(message)) {
return {
ok: true as const,
stdout,
stderr,
note: "NemoClaw registry already had no matching sandbox.",
stdout: "",
stderr: "",
note: "NemoClaw registry file does not exist; skipped registry cleanup.",
}
}
console.log(`[sandbox/delete] nemoclaw-cleanup:error sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)} message=${message}`)
return {
ok: false as const,
stdout,
stderr,
stdout: "",
stderr: "",
error: message,
}
}