From ae7a7c3a26bba8ee2426f58e126ed23e487ac8b7 Mon Sep 17 00:00:00 2001 From: mmckeen-nv Date: Thu, 30 Apr 2026 09:55:24 -0700 Subject: [PATCH] Stabilize OpenClaw gateway during sandbox create --- app/api/sandbox/create/route.ts | 18 +++++++++- app/lib/sandboxPrivilegedFiles.ts | 47 +++++++++++++++++++++++-- install_versioned_nemoclaw_openshell.sh | 16 +++++++++ tests/sandbox-lifecycle-check.mjs | 20 ++++++++++- 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/app/api/sandbox/create/route.ts b/app/api/sandbox/create/route.ts index 33e29fe..ae9e101 100644 --- a/app/api/sandbox/create/route.ts +++ b/app/api/sandbox/create/route.ts @@ -2,7 +2,7 @@ import { NextResponse } from "next/server" import { execFile, spawn } from "node:child_process" import { promisify } from "node:util" import { inspectSandbox } from "@/app/lib/openshellHost" -import { repairOpenClawWorkspacePermissions } from "@/app/lib/sandboxPrivilegedFiles" +import { repairOpenClawWorkspacePermissions, stabilizeOpenClawGatewayConfig } from "@/app/lib/sandboxPrivilegedFiles" import { commandExists, HOST_PATH, @@ -438,6 +438,11 @@ export async function POST(request: Request) { error: "Sandbox readiness polling produced no verification result.", } const created = readiness.verified + const gatewayConfigRepair = created ? await stabilizeOpenClawGatewayConfig(sandboxName).catch((error) => ({ + sandboxName, + path: "/sandbox/.openclaw/openclaw.json", + error: error instanceof Error ? error.message : "Failed to stabilize OpenClaw gateway config", + })) : null const workspaceRepair = created ? await repairOpenClawWorkspacePermissions(sandboxName).catch((error) => ({ sandboxName, path: "/sandbox/.openclaw/workspace", @@ -465,6 +470,7 @@ export async function POST(request: Request) { elapsedMs: readiness.elapsedMs, }, workspaceRepair, + gatewayConfigRepair, deviceApproval, stdout: result.stdout, stderr: result.stderr, @@ -475,6 +481,8 @@ export async function POST(request: Request) { : "NemoClaw blueprint workflow completed in local/default mode. Existing healthy OpenShell gateways are reused before any new gateway start is attempted.", workspaceRepair && "note" in workspaceRepair ? workspaceRepair.note : false, workspaceRepair && "error" in workspaceRepair ? `OpenClaw workspace permission repair failed: ${workspaceRepair.error}` : false, + gatewayConfigRepair && "note" in gatewayConfigRepair ? gatewayConfigRepair.note : false, + gatewayConfigRepair && "error" in gatewayConfigRepair ? `OpenClaw gateway config repair failed: ${gatewayConfigRepair.error}` : false, deviceApproval?.note, ) : "Blueprint command reported success, but the sandbox never reached a ready verification state afterward.", @@ -509,6 +517,11 @@ export async function POST(request: Request) { error: "Sandbox readiness polling produced no verification result.", } const created = readiness.verified + const gatewayConfigRepair = created ? await stabilizeOpenClawGatewayConfig(sandboxName).catch((error) => ({ + sandboxName, + path: "/sandbox/.openclaw/openclaw.json", + error: error instanceof Error ? error.message : "Failed to stabilize OpenClaw gateway config", + })) : null const workspaceRepair = created ? await repairOpenClawWorkspacePermissions(sandboxName).catch((error) => ({ sandboxName, path: "/sandbox/.openclaw/workspace", @@ -541,6 +554,7 @@ export async function POST(request: Request) { elapsedMs: readiness.elapsedMs, }, workspaceRepair, + gatewayConfigRepair, deviceApproval, policyPrepared, note: created @@ -552,6 +566,8 @@ export async function POST(request: Request) { : "Custom sandbox created."), workspaceRepair && "note" in workspaceRepair ? workspaceRepair.note : false, workspaceRepair && "error" in workspaceRepair ? `OpenClaw workspace permission repair failed: ${workspaceRepair.error}` : false, + gatewayConfigRepair && "note" in gatewayConfigRepair ? gatewayConfigRepair.note : false, + gatewayConfigRepair && "error" in gatewayConfigRepair ? `OpenClaw gateway config repair failed: ${gatewayConfigRepair.error}` : false, deviceApproval?.note, ) : "Create command started, but the sandbox never reached a ready verification state afterward.", diff --git a/app/lib/sandboxPrivilegedFiles.ts b/app/lib/sandboxPrivilegedFiles.ts index 0eed692..2b1fc85 100644 --- a/app/lib/sandboxPrivilegedFiles.ts +++ b/app/lib/sandboxPrivilegedFiles.ts @@ -50,6 +50,39 @@ export async function writeSandboxFilePrivileged( } } +export async function stabilizeOpenClawGatewayConfig(sandboxName: string) { + const patchConfig = [ + `const fs = require("fs")`, + `const path = "/sandbox/.openclaw/openclaw.json"`, + `const cfg = JSON.parse(fs.readFileSync(path, "utf8"))`, + `cfg.plugins = cfg.plugins && typeof cfg.plugins === "object" && !Array.isArray(cfg.plugins) ? cfg.plugins : {}`, + `cfg.plugins.entries = cfg.plugins.entries && typeof cfg.plugins.entries === "object" && !Array.isArray(cfg.plugins.entries) ? cfg.plugins.entries : {}`, + `cfg.plugins.entries.bonjour = { ...(cfg.plugins.entries.bonjour || {}), enabled: false }`, + `fs.writeFileSync(path, JSON.stringify(cfg, null, 2) + "\\n")`, + ].join("; ") + const script = [ + `node -e ${shellQuote(patchConfig)}`, + `chmod 444 /sandbox/.openclaw/openclaw.json`, + `chown root:root /sandbox/.openclaw/openclaw.json`, + `(cd /sandbox/.openclaw && sha256sum openclaw.json > .config-hash)`, + `chmod 444 /sandbox/.openclaw/.config-hash`, + `chown root:root /sandbox/.openclaw/.config-hash`, + `mkdir -p /sandbox/.openclaw-data/logs`, + `chown -R sandbox:sandbox /sandbox/.openclaw-data/logs 2>/dev/null || chown -R 998:998 /sandbox/.openclaw-data/logs`, + `openclaw gateway stop >/dev/null 2>&1 || true`, + `if command -v gosu >/dev/null 2>&1; then nohup gosu sandbox openclaw gateway run >/sandbox/.openclaw-data/logs/gateway.log 2>&1 & else nohup openclaw gateway run >/sandbox/.openclaw-data/logs/gateway.log 2>&1 & fi`, + ].join(" && ") + const result = await runDockerKubectl( + ["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script], + ) + if (result.code !== 0) throw new Error(result.stderr || "failed to stabilize OpenClaw gateway config") + return { + sandboxName, + path: "/sandbox/.openclaw/openclaw.json", + note: "Disabled the Bonjour gateway discovery plugin inside the sandbox because it crashes under the OpenShell network namespace.", + } +} + export async function repairOpenClawWorkspacePermissions(sandboxName: string) { const mutableDirs = [ "agents", @@ -64,13 +97,22 @@ export async function repairOpenClawWorkspacePermissions(sandboxName: string) { "memory", "logs", "credentials", + "flows", "sandbox", "telegram", + "plugin-runtime-deps", + ] + const mutableFiles = [ + "update-check.json", ] const script = [ + `mkdir -p /sandbox/.openclaw /sandbox/.openclaw-data/agents/main/agent`, `for dir in ${mutableDirs.map(shellQuote).join(" ")}; do target="/sandbox/.openclaw-data/$dir"; link="/sandbox/.openclaw/$dir"; mkdir -p "$target"; if [ -d "$link" ] && [ ! -L "$link" ]; then cp -a "$link/." "$target/" 2>/dev/null || true; rm -rf "$link"; fi; ln -sfn "$target" "$link"; done`, + `for file in ${mutableFiles.map(shellQuote).join(" ")}; do target="/sandbox/.openclaw-data/$file"; link="/sandbox/.openclaw/$file"; if [ ! -e "$target" ]; then if [ -f "$link" ] && [ ! -L "$link" ]; then cp "$link" "$target" 2>/dev/null || : > "$target"; else : > "$target"; fi; fi; if [ -e "$link" ] && [ ! -L "$link" ]; then rm -f "$link"; fi; ln -sfn "$target" "$link"; done`, + `approval="/sandbox/.openclaw/exec-approvals.json"; approval_data="/sandbox/.openclaw-data/exec-approvals.json"; tmp="/sandbox/.openclaw/.exec-approvals.reconcile.$$"; if [ -L "$approval" ]; then if [ -s "$approval_data" ]; then cp "$approval_data" "$tmp" 2>/dev/null || : > "$tmp"; else : > "$tmp"; fi; rm -f "$approval"; mv "$tmp" "$approval"; elif [ ! -e "$approval" ]; then : > "$approval"; fi`, `chown -R sandbox:sandbox /sandbox/.openclaw-data 2>/dev/null || chown -R 998:998 /sandbox/.openclaw-data`, - `for dir in ${mutableDirs.map(shellQuote).join(" ")}; do chmod 0775 "/sandbox/.openclaw-data/$dir"; done`, + `chown sandbox:sandbox /sandbox/.openclaw /sandbox/.openclaw/exec-approvals.json 2>/dev/null || chown 998:998 /sandbox/.openclaw /sandbox/.openclaw/exec-approvals.json`, + `chmod 0755 /sandbox/.openclaw && chmod 0600 /sandbox/.openclaw/exec-approvals.json`, ].join(" && ") const result = await runDockerKubectl( ["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script], @@ -81,6 +123,7 @@ export async function repairOpenClawWorkspacePermissions(sandboxName: string) { path: "/sandbox/.openclaw", target: "/sandbox/.openclaw-data", mutableDirs, - note: "Repaired OpenClaw mutable paths so AGENTS.md, session lock, and agent state writes resolve into writable .openclaw-data storage.", + mutableFiles, + note: "Reconciled the OpenClaw directory layout for NemoClaw: mutable state paths, AGENTS.md, session locks, update-check state, plugin runtime deps, and exec approvals are writable where the bundled OpenClaw expects them.", } } diff --git a/install_versioned_nemoclaw_openshell.sh b/install_versioned_nemoclaw_openshell.sh index 50cdfe1..8c481b0 100755 --- a/install_versioned_nemoclaw_openshell.sh +++ b/install_versioned_nemoclaw_openshell.sh @@ -12,6 +12,8 @@ OPENSHELL_VERSION="${OPENSHELL_VERSION:-v0.0.26}" OPENSHELL_INSTALL_URL="${OPENSHELL_INSTALL_URL:-https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh}" NEMOCLAW_INSTALL_TAG="${NEMOCLAW_INSTALL_TAG:-v0.0.15}" NEMOCLAW_ZIP_URL="${NEMOCLAW_ZIP_URL:-https://github.com/NVIDIA/NemoClaw/archive/refs/tags/${NEMOCLAW_INSTALL_TAG}.zip}" +OPENCLAW_VERSION="${OPENCLAW_VERSION:-2026.4.24}" +NEMOCLAW_BASE_IMAGE="${NEMOCLAW_BASE_IMAGE:-ghcr.io/nvidia/nemoclaw/sandbox-base:latest}" NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE="${NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE:-1}" NEMOCLAW_NON_INTERACTIVE="${NEMOCLAW_NON_INTERACTIVE:-1}" @@ -34,12 +36,16 @@ Options: Defaults: OPENSHELL_VERSION=$OPENSHELL_VERSION NEMOCLAW_INSTALL_TAG=$NEMOCLAW_INSTALL_TAG + OPENCLAW_VERSION=$OPENCLAW_VERSION + NEMOCLAW_BASE_IMAGE=$NEMOCLAW_BASE_IMAGE Environment overrides: OPENSHELL_VERSION OPENSHELL_INSTALL_URL NEMOCLAW_INSTALL_TAG NEMOCLAW_ZIP_URL + OPENCLAW_VERSION + NEMOCLAW_BASE_IMAGE NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE NEMOCLAW_NON_INTERACTIVE NVIDIA_API_KEY @@ -117,6 +123,7 @@ install_openshell() { install_nemoclaw() { require_command curl require_command sh + require_command docker local work_dir zip_path source_dir work_dir="$(mktemp -d)" @@ -143,11 +150,20 @@ install_nemoclaw() { NVIDIA_API_KEY="${NVIDIA_API_KEY:-}" \ ./install.sh ) + + log "Building NemoClaw stock base image with OpenClaw $OPENCLAW_VERSION" + docker build \ + -f "$source_dir/Dockerfile.base" \ + -t "$NEMOCLAW_BASE_IMAGE" \ + --build-arg "OPENCLAW_VERSION=$OPENCLAW_VERSION" \ + "$source_dir" } echo -e "${GREEN}=== Versioned OpenShell/NemoClaw Installer ===${NC}" echo "OpenShell: $OPENSHELL_VERSION" echo "NemoClaw: $NEMOCLAW_INSTALL_TAG" +echo "OpenClaw: $OPENCLAW_VERSION" +echo "Base image: $NEMOCLAW_BASE_IMAGE" echo "" if [[ "$SKIP_OPENSHELL" -eq 0 ]]; then diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index a8134c4..814dfd4 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -17,6 +17,7 @@ const sandboxInferenceStorePath = path.join(root, 'app/lib/sandboxInferenceStore const sandboxRestartRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/restart/route.ts') const gatewayRepairRoutePath = path.join(root, 'app/api/openshell/gateway/repair/route.ts') const sandboxPrivilegedFilesPath = path.join(root, 'app/lib/sandboxPrivilegedFiles.ts') +const versionedInstallerPath = path.join(root, 'install_versioned_nemoclaw_openshell.sh') 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') @@ -39,6 +40,7 @@ const sandboxInferenceStoreSource = await readFile(sandboxInferenceStorePath, 'u const sandboxRestartRouteSource = await readFile(sandboxRestartRoutePath, 'utf8') const gatewayRepairRouteSource = await readFile(gatewayRepairRoutePath, 'utf8') const sandboxPrivilegedFilesSource = await readFile(sandboxPrivilegedFilesPath, 'utf8') +const versionedInstallerSource = await readFile(versionedInstallerPath, 'utf8') const hookSource = await readFile(hookPath, 'utf8') const pageSource = await readFile(pagePath, 'utf8') const configPanelSource = await readFile(configPanelPath, 'utf8') @@ -64,16 +66,32 @@ assert.match(createRouteSource, /NEMOCLAW_EXPERIMENTAL = "1"/, 'blueprint create assert.match(createRouteSource, /NEMOCLAW_PROVIDER = "vllm"/, 'blueprint create must support the vLLM onboarding provider') assert.match(createRouteSource, /NEMOCLAW_PROVIDER = "nim-local"/, 'blueprint create must support the NVIDIA NIM onboarding provider') assert.match(createRouteSource, /NEMOCLAW_PROVIDER_KEY = apiKey/, 'blueprint create must pass NVIDIA API keys through provider env without changing NemoClaw') +assert.match(versionedInstallerSource, /OPENCLAW_VERSION="\$\{OPENCLAW_VERSION:-2026\.4\.24\}"/, 'versioned installer must pin the dashboard-compatible NemoClaw OpenClaw version') +assert.match(versionedInstallerSource, /docker build[\s\S]*Dockerfile\.base[\s\S]*--build-arg "OPENCLAW_VERSION=\$OPENCLAW_VERSION"[\s\S]*"\$source_dir"/, 'versioned installer must rebuild the stock NemoClaw base image with the pinned OpenClaw version') +assert.doesNotMatch(createRouteSource, /repairOpenClawRuntimePolicy|runtimePolicyRepair/, 'sandbox create must not mutate OpenShell filesystem policy for OpenClaw') +assert.match(createRouteSource, /stabilizeOpenClawGatewayConfig/, 'sandbox create must stabilize OpenClaw gateway config after readiness') +assert.match(createRouteSource, /gatewayConfigRepair/, 'sandbox create response must report OpenClaw gateway config stabilization status') assert.match(createRouteSource, /repairOpenClawWorkspacePermissions/, 'sandbox create must repair OpenClaw workspace permissions after readiness') assert.match(createRouteSource, /workspaceRepair/, 'sandbox create response must report OpenClaw workspace permission repair status') +assert.doesNotMatch(sandboxPrivilegedFilesSource, /openshell-openclaw-policy-|"policy", "set"|withoutReadOnlyOpenClaw/, 'OpenClaw repair must not mutate OpenShell filesystem policy') +assert.match(sandboxPrivilegedFilesSource, /plugins\.entries\.bonjour/, 'gateway config stabilization must disable the crashing Bonjour plugin') +assert.match(sandboxPrivilegedFilesSource, /sha256sum openclaw\.json > \.config-hash/, 'gateway config stabilization must refresh OpenClaw config hash') +assert.match(sandboxPrivilegedFilesSource, /openclaw gateway run/, 'gateway config stabilization must restart the OpenClaw gateway') assert.match(sandboxPrivilegedFilesSource, /"workspace"/, 'workspace repair must target the OpenClaw workspace directory') assert.match(sandboxPrivilegedFilesSource, /\/sandbox\/\.openclaw-data\/\$dir/, 'workspace repair must use the policy-writable OpenClaw data tree') assert.match(sandboxPrivilegedFilesSource, /ln -sfn/, 'workspace repair must link the OpenClaw workspace path into writable storage') -assert.match(sandboxPrivilegedFilesSource, /chown -R sandbox:sandbox \/sandbox\/\.openclaw-data/, 'workspace repair must make the writable OpenClaw data tree owned by the sandbox user') +assert.match(sandboxPrivilegedFilesSource, /"flows"/, 'workspace repair must include the stock NemoClaw flows symlink') +assert.match(sandboxPrivilegedFilesSource, /"plugin-runtime-deps"/, 'workspace repair must include the OpenClaw plugin runtime dependency directory') +assert.match(sandboxPrivilegedFilesSource, /"update-check\.json"/, 'workspace repair must include the stock NemoClaw update-check symlink') +assert.match(sandboxPrivilegedFilesSource, /exec-approvals\.json/, 'workspace repair must include exec approval state handling') +assert.match(sandboxPrivilegedFilesSource, /const mutableFiles = \[\s*"update-check\.json",\s*\]/, 'workspace repair must not leave exec approvals in the symlinked mutable files list for newer OpenClaw') +assert.match(sandboxPrivilegedFilesSource, /chmod 0600 \/sandbox\/\.openclaw\/exec-approvals\.json/, 'workspace repair must make exec approvals a real writable file for newer OpenClaw') +assert.match(sandboxPrivilegedFilesSource, /chown -R sandbox:sandbox \/sandbox\/\.openclaw-data/, 'workspace repair must restore stock NemoClaw ownership for OpenClaw data') assert.match(sandboxPrivilegedFilesSource, /AGENTS\.md/, 'workspace repair note must document the AGENTS.md permission failure it prevents') assert.match(sandboxPrivilegedFilesSource, /"agents"/, 'workspace repair must target the OpenClaw agent directory so sessions are writable through .openclaw-data') assert.match(sandboxPrivilegedFilesSource, /"agents"[\s\S]*"workspace"[\s\S]*"memory"/, 'workspace repair must cover NemoClaw expected OpenClaw mutable symlink directories') assert.match(sandboxPrivilegedFilesSource, /sessions\.json\.lock|session lock/, 'workspace repair note must document the sessions.json.lock permission failure it prevents') +assert.match(sandboxPrivilegedFilesSource, /bundled OpenClaw expects/, 'workspace repair note must document the dashboard reconciles to the bundled OpenClaw expectations') assert.match(hookSource, /type RefreshOptions = \{\s*force\?: boolean\s*\}/, 'inventory refresh must accept a force option') assert.match(hookSource, /!refreshOptions\?\.force && inFlightRef\.current/, 'forced inventory refresh must bypass existing in-flight reads')