fix: use OpenShell exec for inference apply (#39)

Co-authored-by: mmckeen-nv <mmckeen-nv@users.noreply.github.com>
This commit is contained in:
mmckeen-nv
2026-08-12 23:06:22 -07:00
committed by GitHub
co-authored by mmckeen-nv
parent 611291db3d
commit 3326735241
2 changed files with 16 additions and 14 deletions
+12 -14
View File
@@ -1,11 +1,9 @@
import { execFile, spawn } from "node:child_process" import { execFile, spawn } from "node:child_process"
import { promisify } from "node:util" 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" import { getSandboxInferenceConfig, type SandboxInferenceRoute } from "./sandboxInferenceStore"
const execFileAsync = promisify(execFile) 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) { function modelContextWindow(modelId: string) {
const normalized = modelId.toLowerCase() const normalized = modelId.toLowerCase()
@@ -155,7 +153,7 @@ function buildOpenClawConfig(current: any, routes: SandboxInferenceRoute[], prim
async function runOpenShell(args: string[]) { async function runOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, { const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: hostCommandEnv({ env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY?.trim() || undefined,
}), }),
timeout: 60000, timeout: 60000,
maxBuffer: 20 * 1024 * 1024, maxBuffer: 20 * 1024 * 1024,
@@ -163,10 +161,12 @@ async function runOpenShell(args: string[]) {
return { stdout: String(stdout).trim(), stderr: String(stderr).trim() } 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) => { 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], { const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], {
env: { ...process.env, PATH: HOST_PATH }, env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY?.trim() || undefined,
}),
stdio: ["pipe", "pipe", "pipe"], stdio: ["pipe", "pipe", "pipe"],
}) })
let stdout = "" let stdout = ""
@@ -181,7 +181,7 @@ async function runDockerKubectl(args: string[], input?: string) {
} }
async function readCurrentOpenClawConfig(sandboxName: 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") if (result.code !== 0) throw new Error(result.stderr || "Failed to read OpenClaw config")
return JSON.parse(result.stdout) 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 payload = `${JSON.stringify(config, null, 2)}\n`
const script = [ const script = [
"cat > /sandbox/.openclaw/openclaw.json", "cat > /sandbox/.openclaw/openclaw.json",
"chmod 444 /sandbox/.openclaw/openclaw.json", "chmod 600 /sandbox/.openclaw/openclaw.json",
"chown root:root /sandbox/.openclaw/openclaw.json",
"sha256sum /sandbox/.openclaw/openclaw.json > /sandbox/.openclaw/.config-hash", "sha256sum /sandbox/.openclaw/openclaw.json > /sandbox/.openclaw/.config-hash",
"chmod 444 /sandbox/.openclaw/.config-hash", "chmod 600 /sandbox/.openclaw/.config-hash",
"chown root:root /sandbox/.openclaw/.config-hash",
].join(" && ") ].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") if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw config")
return result return result
} }
async function restartOpenClawGatewayIfRunning(sandboxName: string) { 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" 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) { export async function applySandboxInferenceProfile(sandboxId: string, sandboxName: string) {
+4
View File
@@ -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(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, /\/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, /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, /\["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, /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/...') assert.match(sandboxInferenceApplyLibSource, /providerKey: "nvidia"[\s\S]*modelRef: route\.model/, 'live apply must preserve nvidia/... model refs without wrapping them as inference/...')