Use OpenShell config home for host commands

This commit is contained in:
Codex
2026-04-28 16:24:51 +00:00
parent 33510bba85
commit 476d3a96bb
13 changed files with 82 additions and 97 deletions
+3 -8
View File
@@ -1,7 +1,7 @@
import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "@/app/lib/hostCommands"
import { OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -21,15 +21,10 @@ function stripAnsi(value: string) {
async function runOpenShell(args: string[], extraEnv: Record<string, string> = {}) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
env: hostCommandEnv({
...extraEnv,
PATH: HOST_PATH,
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 60000,
maxBuffer: 20 * 1024 * 1024,
})
+6 -18
View File
@@ -12,6 +12,7 @@ import {
NEMOCLAW_SETUP_CANDIDATES,
NODE_BIN,
OPENSHELL_BIN,
hostCommandEnv,
} from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -240,14 +241,9 @@ async function approveOpenClawDeviceRequests(sandboxName: string) {
"sh",
"-lc",
"openclaw devices approve --latest --json --timeout 10000",
], {
...process.env,
PATH: HOST_PATH,
], hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
}, 15000)
}), 15000)
const combinedOutput = `${result.stdout}\n${result.stderr}`.trim()
const noPending = NO_PENDING_DEVICE_REQUESTS.test(combinedOutput)
@@ -339,15 +335,13 @@ export async function POST(request: Request) {
if (blueprint === "nemoclaw-blueprint") {
const createCommand = buildNemoClawCreateCommand()
const env: NodeJS.ProcessEnv = {
...process.env,
PATH: HOST_PATH,
const env: NodeJS.ProcessEnv = hostCommandEnv({
NEMOCLAW_SANDBOX_NAME: sandboxName,
NEMOCLAW_NON_INTERACTIVE: "1",
NEMOCLAW_RECREATE_SANDBOX: "1",
NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE: "1",
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
}
})
if (!enableTailscale) {
env.NVIDIA_API_KEY = env.NVIDIA_API_KEY || "optional-local-mode"
@@ -414,13 +408,7 @@ export async function POST(request: Request) {
}
if (blueprint === "custom-sandbox") {
const env: NodeJS.ProcessEnv = {
...process.env,
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
}
const env: NodeJS.ProcessEnv = hostCommandEnv()
const createAttempt = await runCreateCommandBounded(OPENSHELL_BIN, ["sandbox", "create", "--name", sandboxName], env, 15000)
if (createAttempt.completed && createAttempt.exitCode !== 0) {
+5 -15
View File
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { resolveSandboxRef } from "@/app/lib/openshellHost"
import { HOST_PATH, NEMOCLAW_BIN, NEMOCLAW_CWD, OPENSHELL_BIN } from "@/app/lib/hostCommands"
import { NEMOCLAW_BIN, NEMOCLAW_CWD, OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -54,14 +54,9 @@ async function deleteSandbox(sandboxName: string) {
console.log(`[sandbox/delete] command:start sandbox=${sandboxName}`)
try {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, ["sandbox", "delete", sandboxName], {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 60000,
maxBuffer: 20 * 1024 * 1024,
})
@@ -93,14 +88,9 @@ async function cleanupNemoClawSandbox(sandboxName: string) {
try {
const { stdout, stderr } = await execFileAsync(NEMOCLAW_BIN, [sandboxName, "destroy", "--yes"], {
cwd: NEMOCLAW_CWD,
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 90000,
maxBuffer: 20 * 1024 * 1024,
})
+2 -8
View File
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server"
import { execFile } from "node:child_process"
import { hostname, networkInterfaces } from "node:os"
import { promisify } from "node:util"
import { HOST_PATH, NEMOCLAW_BIN, NODE_BIN, OPENSHELL_BIN } from "@/app/lib/hostCommands"
import { HOST_PATH, NEMOCLAW_BIN, NODE_BIN, OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
import { resolveRuntimeAuthority } from "@/app/lib/runtimeAuthority"
const execFileAsync = promisify(execFile)
@@ -164,13 +164,7 @@ async function execNemoclaw(args: string[]) {
async function execOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
env: hostCommandEnv(),
})
return { stdout, stderr }
+47 -7
View File
@@ -11,6 +11,11 @@ function unique(values: Array<string | undefined>) {
return Array.from(new Set(values.filter((value): value is string => Boolean(value))))
}
function inferHomeFromLocalBin(commandPath: string) {
const suffix = "/.local/bin/openshell"
return commandPath.endsWith(suffix) ? commandPath.slice(0, -suffix.length) : undefined
}
function safeIsDirectory(candidate: string) {
try {
return statSync(candidate).isDirectory()
@@ -51,11 +56,40 @@ function discoverHomeFiles(relativePath: string) {
return found
}
const OPENSHELL_BIN_CANDIDATES = unique([
process.env.OPENSHELL_BIN,
HOME ? path.join(HOME, ".local/bin/openshell") : undefined,
"/opt/homebrew/bin/openshell",
"/usr/local/bin/openshell",
])
export const OPENSHELL_BIN = firstExisting(OPENSHELL_BIN_CANDIDATES, "openshell")
function homeWithOpenShellConfig(candidates: Array<string | undefined>) {
return candidates
.filter((value): value is string => Boolean(value))
.find((candidate) => existsSync(path.join(candidate, ".config/openshell")))
}
const INFERRED_OPENSHELL_HOME = inferHomeFromLocalBin(OPENSHELL_BIN)
export const OPENSHELL_HOME = homeWithOpenShellConfig([
process.env.OPENSHELL_HOME,
HOME,
INFERRED_OPENSHELL_HOME,
]) || process.env.OPENSHELL_HOME || INFERRED_OPENSHELL_HOME || HOME
export const OPENSHELL_XDG_CONFIG_HOME = firstExisting([
process.env.OPENSHELL_XDG_CONFIG_HOME,
process.env.XDG_CONFIG_HOME && existsSync(path.join(process.env.XDG_CONFIG_HOME, "openshell")) ? process.env.XDG_CONFIG_HOME : undefined,
OPENSHELL_HOME ? path.join(OPENSHELL_HOME, ".config") : undefined,
], process.env.XDG_CONFIG_HOME || (OPENSHELL_HOME ? path.join(OPENSHELL_HOME, ".config") : ""))
function pathEntries() {
return [
process.env.TERMINAL_EXTRA_PATH,
process.env.OPENSHELL_CONTROL_VENV ? path.join(process.env.OPENSHELL_CONTROL_VENV, "bin") : undefined,
path.join(process.cwd(), ".venv/bin"),
OPENSHELL_HOME ? path.join(OPENSHELL_HOME, ".local/bin") : undefined,
HOME ? path.join(HOME, ".local/bin") : undefined,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin") : undefined,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.1/bin") : undefined,
@@ -73,6 +107,19 @@ function pathEntries() {
export const HOST_PATH = unique(pathEntries()).join(":")
export function hostCommandEnv(extra: Record<string, string | undefined> = {}) {
return {
...process.env,
...(OPENSHELL_HOME ? { HOME: OPENSHELL_HOME } : {}),
...(OPENSHELL_XDG_CONFIG_HOME ? { XDG_CONFIG_HOME: OPENSHELL_XDG_CONFIG_HOME } : {}),
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
...extra,
}
}
export const NODE_BIN = firstExisting([
process.env.NODE_BIN,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin/node") : undefined,
@@ -82,13 +129,6 @@ export const NODE_BIN = firstExisting([
"/usr/bin/node",
], "node")
export const OPENSHELL_BIN = firstExisting([
process.env.OPENSHELL_BIN,
HOME ? path.join(HOME, ".local/bin/openshell") : undefined,
"/opt/homebrew/bin/openshell",
"/usr/local/bin/openshell",
], "openshell")
export const OPENCLAW_BIN = firstExisting([
process.env.OPENCLAW_BIN,
HOME ? path.join(HOME, ".local/bin/openclaw") : undefined,
+3 -8
View File
@@ -1,6 +1,6 @@
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { HOST_PATH, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { resolveSandboxRef } from "./openshellHost"
const execFileAsync = promisify(execFile)
@@ -36,15 +36,10 @@ function normalizeProxyOrigin(value: string) {
async function runOpenShell(args: string[]) {
const { stdout } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
TERM: "dumb",
},
}),
timeout: 15000,
maxBuffer: 1024 * 1024,
})
+2 -8
View File
@@ -1,6 +1,6 @@
import { execFile, spawn } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENCLAW_BIN, OPENSHELL_BIN } from "./hostCommands"
import { HOST_PATH, OPENCLAW_BIN, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { getDefaultOpenClawInstance, getOpenClawDashboardPortForSandbox, resolveOpenClawInstance } from "./openclawInstances"
const execFileAsync = promisify(execFile)
@@ -44,13 +44,7 @@ export type DashboardProbe = {
export async function execOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
env: hostCommandEnv(),
})
return { stdout, stderr }
}
+3 -8
View File
@@ -1,6 +1,6 @@
import { spawn } from "node:child_process"
import path from "node:path"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { resolveSandboxRef } from "./openshellHost"
export const MAX_FILE_BYTES = Number.parseInt(process.env.SANDBOX_FILE_TRANSFER_MAX_BYTES || String(128 * 1024 * 1024), 10)
@@ -58,14 +58,9 @@ export function assertRequestContentLength(request: Request, maxBytes = MAX_MULT
function runSandboxExec(sandboxName: string, script: string, input?: Buffer, timeoutMs = 60000) {
return new Promise<{ stdout: Buffer; stderr: string; code: number | null }>((resolve, reject) => {
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 || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
stdio: ["pipe", "pipe", "pipe"],
})
const stdout: Buffer[] = []
+3 -8
View File
@@ -1,6 +1,6 @@
import { execFile, spawn } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { HOST_PATH, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { getSandboxInferenceConfig, type SandboxInferenceRoute } from "./sandboxInferenceStore"
const execFileAsync = promisify(execFile)
@@ -110,14 +110,9 @@ function buildOpenClawConfig(current: any, routes: SandboxInferenceRoute[], prim
async function runOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
timeout: 60000,
maxBuffer: 20 * 1024 * 1024,
})
+3 -8
View File
@@ -1,6 +1,6 @@
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { resolveSandboxRef } from "./openshellHost"
const execFileAsync = promisify(execFile)
@@ -25,15 +25,10 @@ type BrokerNetworkAction = {
function runOpenShell(args: string[], timeout = 60000) {
return execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
TERM: "dumb",
},
}),
timeout,
maxBuffer: 10 * 1024 * 1024,
})
+1
View File
@@ -410,6 +410,7 @@ upsert_env "NEXT_PUBLIC_ENABLE_SANDBOX_OPERATIONS" "true"
upsert_env "OPEN_SHELL_CONTAINER" "$OPEN_SHELL_CONTAINER_DEFAULT"
upsert_env "OPENSHELL_GATEWAY" "nemoclaw"
set_env "OPENSHELL_CONTROL_VENV" "${VIRTUAL_ENV:-$PROJECT_VENV}"
set_env "OPENSHELL_HOME" "${HOME:-}"
set_env "OPENSHELL_BIN" "${OPENSHELL_BIN:-}"
set_env "NEMOCLAW_BIN" "${NEMOCLAW_BIN:-}"
set_env "NEMOCLAW_SETUP" "${NEMOCLAW_SETUP:-}"
+1
View File
@@ -39,6 +39,7 @@ assert.match(installSource, /candidate="\$root\/scripts\/setup\.sh"/, 'installer
assert.match(installSource, /candidate="\$nested\/scripts\/setup\.sh"/, 'installer must scan one nested user-home level for setup.sh')
assert.match(installSource, /set_env "NEMOCLAW_SETUP"/, 'installer must persist discovered NemoClaw setup path')
assert.match(installSource, /set_env "NEMOCLAW_BIN"/, 'installer must persist discovered NemoClaw CLI path')
assert.match(installSource, /set_env "OPENSHELL_HOME"/, 'installer must persist the OpenShell config home')
assert.match(installSource, /set_env "OPENSHELL_BIN"/, 'installer must persist discovered OpenShell path')
assert.match(installSource, /upsert_env "TERMINAL_SERVER_AUTOSTART" "true"/, 'installer must enable terminal bridge autostart by default')
assert.match(installSource, /ensure_npx\(\)/, 'installer must install or verify npx for stdio MCP servers')
+3 -1
View File
@@ -61,7 +61,9 @@ assert.match(hostCommandsSource, /export const OPENSHELL_BIN[\s\S]*"openshell"/,
assert.match(deleteRouteSource, /\["sandbox", "delete", sandboxName\]/, 'delete route must call openshell sandbox delete with the sandbox name')
assert.match(deleteRouteSource, /validateSandboxName/, 'delete route must validate sandbox names before execution')
assert.match(deleteRouteSource, /resolveSandboxRef/, 'delete route must resolve sandbox ids to names before deleting')
assert.match(deleteRouteSource, /OPENSHELL_GATEWAY: process\.env\.OPENSHELL_GATEWAY \|\| "nemoclaw"/, 'delete route must use the NemoClaw gateway context by default')
assert.match(deleteRouteSource, /hostCommandEnv\(\{[\s\S]*OPENSHELL_GATEWAY: process\.env\.OPENSHELL_GATEWAY \|\| "nemoclaw"/, 'delete route must use the NemoClaw gateway context with the shared host command environment')
assert.match(hostCommandsSource, /export function hostCommandEnv/, 'host commands must provide a shared environment for OpenShell child processes')
assert.match(hostCommandsSource, /XDG_CONFIG_HOME/, 'shared host command environment must preserve the OpenShell config home')
assert.match(deleteRouteSource, /waitForSandboxDeleted/, 'delete route must poll authoritative inventory until the sandbox is gone')
assert.match(deleteRouteSource, /cleanupNemoClawSandbox/, 'delete route must clean up NemoClaw registry state after deleting from OpenShell')
assert.match(deleteRouteSource, /\[sandboxName, "destroy", "--yes"\]/, 'delete route must run the non-interactive NemoClaw destroy workflow')