From 3326735241ea565c3d0969b8acc54ef42d003b11 Mon Sep 17 00:00:00 2001 From: mmckeen-nv <108093260+mmckeen-nv@users.noreply.github.com> Date: Wed, 12 Aug 2026 23:06:22 -0700 Subject: [PATCH] fix: use OpenShell exec for inference apply (#39) Co-authored-by: mmckeen-nv --- app/lib/sandboxInferenceApply.ts | 26 ++++++++++++-------------- tests/sandbox-lifecycle-check.mjs | 4 ++++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/lib/sandboxInferenceApply.ts b/app/lib/sandboxInferenceApply.ts index 9aeacf3..5e424b1 100644 --- a/app/lib/sandboxInferenceApply.ts +++ b/app/lib/sandboxInferenceApply.ts @@ -1,11 +1,9 @@ import { execFile, spawn } from "node:child_process" import { promisify } from "node:util" -import { HOST_PATH, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands" +import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands" import { getSandboxInferenceConfig, type SandboxInferenceRoute } from "./sandboxInferenceStore" const execFileAsync = promisify(execFile) -const DOCKER_BIN = process.env.DOCKER_BIN || "docker" -const OPENSHELL_CLUSTER_CONTAINER = process.env.OPENSHELL_CLUSTER_CONTAINER || "openshell-cluster-nemoclaw" function modelContextWindow(modelId: string) { const normalized = modelId.toLowerCase() @@ -155,7 +153,7 @@ function buildOpenClawConfig(current: any, routes: SandboxInferenceRoute[], prim async function runOpenShell(args: string[]) { const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, { env: hostCommandEnv({ - OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY?.trim() || undefined, }), timeout: 60000, maxBuffer: 20 * 1024 * 1024, @@ -163,10 +161,12 @@ async function runOpenShell(args: string[]) { return { stdout: String(stdout).trim(), stderr: String(stderr).trim() } } -async function runDockerKubectl(args: string[], input?: string) { +async function runOpenShellExec(sandboxName: string, script: string, input?: string) { return await new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => { - const child = spawn(DOCKER_BIN, ["exec", ...(input ? ["-i"] : []), OPENSHELL_CLUSTER_CONTAINER, "kubectl", ...args], { - env: { ...process.env, PATH: HOST_PATH }, + const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], { + env: hostCommandEnv({ + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY?.trim() || undefined, + }), stdio: ["pipe", "pipe", "pipe"], }) let stdout = "" @@ -181,7 +181,7 @@ async function runDockerKubectl(args: string[], input?: string) { } async function readCurrentOpenClawConfig(sandboxName: string) { - const result = await runDockerKubectl(["exec", "-n", "openshell", sandboxName, "--", "cat", "/sandbox/.openclaw/openclaw.json"]) + const result = await runOpenShellExec(sandboxName, "cat /sandbox/.openclaw/openclaw.json") if (result.code !== 0) throw new Error(result.stderr || "Failed to read OpenClaw config") return JSON.parse(result.stdout) } @@ -190,20 +190,18 @@ async function writeOpenClawConfig(sandboxName: string, config: any) { const payload = `${JSON.stringify(config, null, 2)}\n` const script = [ "cat > /sandbox/.openclaw/openclaw.json", - "chmod 444 /sandbox/.openclaw/openclaw.json", - "chown root:root /sandbox/.openclaw/openclaw.json", + "chmod 600 /sandbox/.openclaw/openclaw.json", "sha256sum /sandbox/.openclaw/openclaw.json > /sandbox/.openclaw/.config-hash", - "chmod 444 /sandbox/.openclaw/.config-hash", - "chown root:root /sandbox/.openclaw/.config-hash", + "chmod 600 /sandbox/.openclaw/.config-hash", ].join(" && ") - const result = await runDockerKubectl(["exec", "-i", "-n", "openshell", sandboxName, "--", "sh", "-lc", script], payload) + const result = await runOpenShellExec(sandboxName, script, payload) if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw config") return result } async function restartOpenClawGatewayIfRunning(sandboxName: string) { const script = "for p in /proc/[0-9]*; do cmd=$(tr '\\0' ' ' < \"$p/cmdline\" 2>/dev/null || true); case \"$cmd\" in *'openclaw gateway'*) kill \"${p##*/}\" 2>/dev/null || true;; esac; done" - return await runDockerKubectl(["exec", "-n", "openshell", sandboxName, "--", "sh", "-lc", script]) + return await runOpenShellExec(sandboxName, script) } export async function applySandboxInferenceProfile(sandboxId: string, sandboxName: string) { diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index 0bf028f..7d9f09f 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -295,6 +295,10 @@ assert.match(sandboxInferencePanelSource, /Enabled Routes/, 'selected sandbox UI assert.match(sandboxInferencePanelSource, /Add Route/, 'selected sandbox UI must allow adding provider/model routes') assert.match(sandboxInferenceApplyLibSource, /\/sandbox\/\.openclaw\/openclaw\.json/, 'live apply must patch OpenClaw config inside the running sandbox') assert.match(sandboxInferenceApplyLibSource, /sha256sum \/sandbox\/\.openclaw\/openclaw\.json > \/sandbox\/\.openclaw\/\.config-hash/, 'live apply must refresh the immutable OpenClaw config hash') +assert.match(sandboxInferenceApplyLibSource, /spawn\(OPENSHELL_BIN, \["sandbox", "exec", "-n", sandboxName/, 'live apply must use the supported OpenShell sandbox exec boundary') +assert.match(sandboxInferenceApplyLibSource, /OPENSHELL_GATEWAY: process\.env\.OPENSHELL_GATEWAY\?\.trim\(\) \|\| undefined/, 'live apply must respect the active gateway unless explicitly configured') +assert.doesNotMatch(sandboxInferenceApplyLibSource, /docker[\s\S]*kubectl|OPENSHELL_CLUSTER_CONTAINER/, 'live apply must not depend on obsolete cluster containers or in-container kubectl') +assert.doesNotMatch(sandboxInferenceApplyLibSource, /chmod 444|chown root:root/, 'live apply must leave OpenClaw config writable by the sandbox owner for subsequent updates') assert.match(sandboxInferenceApplyLibSource, /\["inference", "set", "--no-verify", "--provider", primary\.provider, "--model", primary\.model\]/, 'live apply must point OpenShell inference at the primary route') assert.match(sandboxInferenceApplyLibSource, /resolveInferenceModelIdentity/, 'live apply must normalize provider/model identities for OpenClaw inference routes') assert.match(sandboxInferenceApplyLibSource, /providerKey: "nvidia"[\s\S]*modelRef: route\.model/, 'live apply must preserve nvidia/... model refs without wrapping them as inference/...')