fix: sync OpenClaw 2026.9.1 lifecycle (#50)
Co-authored-by: mmckeen-nv <mmckeen-nv@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import { execFile, spawn } from "node:child_process"
|
||||
import { promisify } from "node:util"
|
||||
import { HOST_PATH, OPENCLAW_BIN, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
|
||||
import { getDefaultOpenClawInstance, getOpenClawDashboardPortForSandbox, resolveOpenClawInstance } from "./openclawInstances"
|
||||
import { restartSandboxGatewayWithNemoClaw } from "./nemoclawCli"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
const OPENSHELL_GATEWAY = process.env.OPENSHELL_GATEWAY?.trim() || undefined
|
||||
@@ -421,13 +422,13 @@ async function execSandboxSsh(sandboxName: string, command: string, timeoutMs =
|
||||
}
|
||||
|
||||
async function ensureRemoteSandboxOpenClawDashboard(sandboxName: string) {
|
||||
const command = [
|
||||
`curl -fsS --max-time 2 http://127.0.0.1:${SANDBOX_DASHBOARD_REMOTE_PORT}/ >/dev/null 2>&1`,
|
||||
"||",
|
||||
`(nohup /usr/local/bin/openclaw gateway run --allow-unconfigured --bind loopback --port ${SANDBOX_DASHBOARD_REMOTE_PORT} >/tmp/gateway.log 2>&1 &)`
|
||||
].join(" ")
|
||||
|
||||
await execSandboxSsh(sandboxName, command).catch(() => null)
|
||||
try {
|
||||
await execSandboxSsh(sandboxName, `curl -fsS --max-time 2 http://127.0.0.1:${SANDBOX_DASHBOARD_REMOTE_PORT}/ >/dev/null`, 5000)
|
||||
return true
|
||||
} catch {
|
||||
const restart = await restartSandboxGatewayWithNemoClaw(sandboxName)
|
||||
if (!restart.ok) return false
|
||||
}
|
||||
|
||||
for (let attempt = 0; attempt < 16; attempt += 1) {
|
||||
try {
|
||||
@@ -448,7 +449,7 @@ async function ensureSandboxOpenClawDashboardTunnel(sandboxName: string) {
|
||||
const initial = await inspectListeningPort(port)
|
||||
if (initial.listenerPresent) return initial
|
||||
|
||||
await ensureRemoteSandboxOpenClawDashboard(sandboxName)
|
||||
if (!await ensureRemoteSandboxOpenClawDashboard(sandboxName)) return initial
|
||||
|
||||
const child = spawn("ssh", buildSandboxSshArgs(sandboxName, [
|
||||
"-N",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { execFile, spawn } from "node:child_process"
|
||||
import { promisify } from "node:util"
|
||||
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
|
||||
import { restartSandboxGatewayWithNemoClaw } from "./nemoclawCli"
|
||||
import { getSandboxInferenceConfig, type SandboxInferenceRoute } from "./sandboxInferenceStore"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
@@ -199,11 +200,6 @@ async function writeOpenClawConfig(sandboxName: string, config: any) {
|
||||
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 runOpenShellExec(sandboxName, script)
|
||||
}
|
||||
|
||||
export async function applySandboxInferenceProfile(sandboxId: string, sandboxName: string) {
|
||||
const config = await getSandboxInferenceConfig(sandboxId)
|
||||
const enabledRoutes = config.routes.filter((route) => route.enabled)
|
||||
@@ -213,12 +209,18 @@ export async function applySandboxInferenceProfile(sandboxId: string, sandboxNam
|
||||
const currentOpenClawConfig = await readCurrentOpenClawConfig(sandboxName)
|
||||
const nextOpenClawConfig = buildOpenClawConfig(currentOpenClawConfig, enabledRoutes, primary)
|
||||
await writeOpenClawConfig(sandboxName, nextOpenClawConfig)
|
||||
await restartOpenClawGatewayIfRunning(sandboxName)
|
||||
|
||||
const routeResult = await runOpenShell(["inference", "set", "--no-verify", "--provider", primary.provider, "--model", primary.model])
|
||||
const restartResult = await restartSandboxGatewayWithNemoClaw(sandboxName)
|
||||
if (!restartResult.ok) {
|
||||
throw new Error(
|
||||
restartResult.stderr || restartResult.error ||
|
||||
"NemoClaw could not verify the native agent gateway restart after applying inference config",
|
||||
)
|
||||
}
|
||||
return {
|
||||
primaryRoute: primary,
|
||||
routesApplied: enabledRoutes.length,
|
||||
gatewayRoute: routeResult,
|
||||
gatewayRestart: "nemoclaw-native-gateway",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
syncSandboxOpenClawMcpConfig,
|
||||
revokeSandboxOpenClawMcpConfig,
|
||||
} from "./sandboxOpenClawMcpConfig"
|
||||
import { repairOpenClawExecApprovalsFile, writeSandboxFilePrivileged } from "./sandboxPrivilegedFiles"
|
||||
import { writeSandboxFilePrivileged } from "./sandboxPrivilegedFiles"
|
||||
|
||||
export const SANDBOX_MCP_MANIFEST_PATH = "/sandbox/openshell_control_mcp.md"
|
||||
|
||||
@@ -112,8 +112,6 @@ export async function syncSandboxMcpManifest(
|
||||
options.brokerBaseUrl,
|
||||
handoff.token,
|
||||
)
|
||||
const execApprovals = await repairOpenClawExecApprovalsFile(sandboxName)
|
||||
|
||||
return {
|
||||
path: uploaded.path,
|
||||
sandboxName: uploaded.sandboxName,
|
||||
@@ -126,7 +124,6 @@ export async function syncSandboxMcpManifest(
|
||||
expiresAt: handoff.session.expiresAt,
|
||||
},
|
||||
openClaw,
|
||||
execApprovals,
|
||||
markdown: handoff.markdown,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { spawn } from "node:child_process"
|
||||
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
|
||||
import { restartSandboxGatewayWithNemoClaw } from "./nemoclawCli"
|
||||
|
||||
export const OPENSHELL_CONTROL_MCP_SERVER_NAME = "openshell-control"
|
||||
export const OPENCLAW_CONFIG_PATH = "/sandbox/.openclaw/openclaw.json"
|
||||
@@ -61,9 +62,14 @@ async function writeOpenClawConfig(sandboxName: string, config: Record<string, u
|
||||
if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw MCP config")
|
||||
}
|
||||
|
||||
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"
|
||||
await runSandboxShell(sandboxName, script)
|
||||
async function restartNativeGateway(sandboxName: string) {
|
||||
const result = await restartSandboxGatewayWithNemoClaw(sandboxName)
|
||||
if (!result.ok) {
|
||||
throw new Error(
|
||||
result.stderr || result.error ||
|
||||
"NemoClaw could not verify the native agent gateway restart after updating MCP config",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export function buildOpenClawMcpServerConfig(brokerBaseUrl: string, token: string) {
|
||||
@@ -94,7 +100,7 @@ export async function syncSandboxOpenClawMcpConfig(
|
||||
...current,
|
||||
mcp,
|
||||
})
|
||||
await restartOpenClawGatewayIfRunning(sandboxName)
|
||||
await restartNativeGateway(sandboxName)
|
||||
|
||||
return {
|
||||
path: OPENCLAW_CONFIG_PATH,
|
||||
@@ -121,7 +127,7 @@ export async function revokeSandboxOpenClawMcpConfig(sandboxName: string) {
|
||||
}
|
||||
|
||||
await writeOpenClawConfig(sandboxName, nextConfig)
|
||||
await restartOpenClawGatewayIfRunning(sandboxName)
|
||||
await restartNativeGateway(sandboxName)
|
||||
|
||||
return {
|
||||
path: OPENCLAW_CONFIG_PATH,
|
||||
|
||||
@@ -51,107 +51,3 @@ export async function writeSandboxFilePrivileged(
|
||||
bytes: payload.byteLength,
|
||||
}
|
||||
}
|
||||
|
||||
export async function repairOpenClawExecApprovalsFile(sandboxName: string) {
|
||||
const normalizeScript = [
|
||||
`const fs = require("fs")`,
|
||||
`const approval = "/sandbox/.openclaw/exec-approvals.json"`,
|
||||
`const tmp = process.argv[1]`,
|
||||
`let source = ""`,
|
||||
`try { source = fs.readFileSync(approval, "utf8") } catch {}`,
|
||||
`const trimmed = source.trim()`,
|
||||
`let payload = "{}\\n"`,
|
||||
`if (trimmed) { try { payload = JSON.stringify(JSON.parse(trimmed), null, 2) + "\\n" } catch {} }`,
|
||||
`fs.writeFileSync(tmp, payload)`,
|
||||
].join("; ")
|
||||
const script = [
|
||||
`mkdir -p /sandbox/.openclaw`,
|
||||
`tmp="/sandbox/.openclaw/.exec-approvals.json.$$"`,
|
||||
`node -e ${shellQuote(normalizeScript)} "$tmp"`,
|
||||
`rm -f /sandbox/.openclaw/exec-approvals.json`,
|
||||
`mv "$tmp" /sandbox/.openclaw/exec-approvals.json`,
|
||||
`chown sandbox:sandbox /sandbox/.openclaw/exec-approvals.json 2>/dev/null || chown 998:998 /sandbox/.openclaw/exec-approvals.json`,
|
||||
`chmod 0600 /sandbox/.openclaw/exec-approvals.json`,
|
||||
].join(" && ")
|
||||
const result = await runSandboxShell(sandboxName, script)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw exec approvals file")
|
||||
return {
|
||||
sandboxName,
|
||||
path: "/sandbox/.openclaw/exec-approvals.json",
|
||||
note: "Replaced OpenClaw exec approvals symlink with a real sandbox-owned file for newer OpenClaw versions.",
|
||||
}
|
||||
}
|
||||
|
||||
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 runSandboxShell(sandboxName, 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",
|
||||
"extensions",
|
||||
"workspace",
|
||||
"skills",
|
||||
"hooks",
|
||||
"identity",
|
||||
"devices",
|
||||
"canvas",
|
||||
"cron",
|
||||
"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`,
|
||||
`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 runSandboxShell(sandboxName, script)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw mutable paths")
|
||||
return {
|
||||
sandboxName,
|
||||
path: "/sandbox/.openclaw",
|
||||
target: "/sandbox/.openclaw-data",
|
||||
mutableDirs,
|
||||
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.",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user