From 476d3a96bbc27c534be8f2b3a87109459a265611 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 28 Apr 2026 16:24:51 +0000 Subject: [PATCH] Use OpenShell config home for host commands --- app/api/inference/route.ts | 11 ++----- app/api/sandbox/create/route.ts | 24 ++++---------- app/api/sandbox/delete/route.ts | 20 +++--------- app/api/telemetry/real/route.ts | 10 ++---- app/lib/hostCommands.ts | 54 +++++++++++++++++++++++++++---- app/lib/mcpBrokerUrl.ts | 11 ++----- app/lib/openshellHost.ts | 10 ++---- app/lib/sandboxFiles.ts | 11 ++----- app/lib/sandboxInferenceApply.ts | 11 ++----- app/lib/sandboxPermissions.ts | 11 ++----- install.sh | 1 + tests/portable-install-check.mjs | 1 + tests/sandbox-lifecycle-check.mjs | 4 ++- 13 files changed, 82 insertions(+), 97 deletions(-) diff --git a/app/api/inference/route.ts b/app/api/inference/route.ts index 79f7704..a89d3cc 100644 --- a/app/api/inference/route.ts +++ b/app/api/inference/route.ts @@ -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 = {}) { 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, }) diff --git a/app/api/sandbox/create/route.ts b/app/api/sandbox/create/route.ts index 1d1fbbf..dcee22c 100644 --- a/app/api/sandbox/create/route.ts +++ b/app/api/sandbox/create/route.ts @@ -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) { diff --git a/app/api/sandbox/delete/route.ts b/app/api/sandbox/delete/route.ts index e6f6837..d1fcfc0 100644 --- a/app/api/sandbox/delete/route.ts +++ b/app/api/sandbox/delete/route.ts @@ -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, }) diff --git a/app/api/telemetry/real/route.ts b/app/api/telemetry/real/route.ts index ee72a1b..1b6d419 100644 --- a/app/api/telemetry/real/route.ts +++ b/app/api/telemetry/real/route.ts @@ -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 } diff --git a/app/lib/hostCommands.ts b/app/lib/hostCommands.ts index 7a17111..18772fd 100644 --- a/app/lib/hostCommands.ts +++ b/app/lib/hostCommands.ts @@ -11,6 +11,11 @@ function unique(values: Array) { 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) { + 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 = {}) { + 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, diff --git a/app/lib/mcpBrokerUrl.ts b/app/lib/mcpBrokerUrl.ts index 7690d49..cbd7005 100644 --- a/app/lib/mcpBrokerUrl.ts +++ b/app/lib/mcpBrokerUrl.ts @@ -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, }) diff --git a/app/lib/openshellHost.ts b/app/lib/openshellHost.ts index 961786a..9483428 100644 --- a/app/lib/openshellHost.ts +++ b/app/lib/openshellHost.ts @@ -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 } } diff --git a/app/lib/sandboxFiles.ts b/app/lib/sandboxFiles.ts index 88afa54..ff61dea 100644 --- a/app/lib/sandboxFiles.ts +++ b/app/lib/sandboxFiles.ts @@ -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[] = [] diff --git a/app/lib/sandboxInferenceApply.ts b/app/lib/sandboxInferenceApply.ts index a9fe543..8a20eaf 100644 --- a/app/lib/sandboxInferenceApply.ts +++ b/app/lib/sandboxInferenceApply.ts @@ -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, }) diff --git a/app/lib/sandboxPermissions.ts b/app/lib/sandboxPermissions.ts index 064285f..2fbddf6 100644 --- a/app/lib/sandboxPermissions.ts +++ b/app/lib/sandboxPermissions.ts @@ -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, }) diff --git a/install.sh b/install.sh index 2f70a6e..000555b 100755 --- a/install.sh +++ b/install.sh @@ -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:-}" diff --git a/tests/portable-install-check.mjs b/tests/portable-install-check.mjs index 5c45aaa..04e7bc1 100644 --- a/tests/portable-install-check.mjs +++ b/tests/portable-install-check.mjs @@ -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') diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index 916d111..761284a 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -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')