Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 63 additions & 10 deletions bin/lib/onboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function hasStaleGateway(gwInfoOutput) {
return typeof gwInfoOutput === "string" && gwInfoOutput.length > 0 && gwInfoOutput.includes(GATEWAY_NAME);
}

function streamSandboxCreate(command, env = process.env) {
function streamSandboxCreate(command, env = process.env, options = {}) {
const child = spawn("bash", ["-lc", command], {
cwd: ROOT,
env,
Expand All @@ -197,6 +197,26 @@ function streamSandboxCreate(command, env = process.env) {
let lastPrintedLine = "";
let sawProgress = false;
let settled = false;
let polling = false;
const pollIntervalMs = options.pollIntervalMs || 2000;

function finish(result) {
if (settled) return;
settled = true;
if (pending) flushLine(pending);
if (readyTimer) clearInterval(readyTimer);
resolvePromise(result);
}

function detachChild() {
child.stdout?.removeAllListeners?.("data");
child.stderr?.removeAllListeners?.("data");
child.stdout?.destroy?.();
child.stderr?.destroy?.();
child.removeAllListeners?.("error");
child.removeAllListeners?.("close");
child.unref?.();
}

function shouldShowLine(line) {
return (
Expand Down Expand Up @@ -235,25 +255,53 @@ function streamSandboxCreate(command, env = process.env) {
child.stdout.on("data", onChunk);
child.stderr.on("data", onChunk);

let resolvePromise;
const readyTimer = options.readyCheck
? setInterval(() => {
if (settled || polling) return;
polling = true;
try {
let ready = false;
try {
ready = !!options.readyCheck();
} catch {
return;
}
if (!ready) return;
const detail = "Sandbox reported Ready before create stream exited; continuing.";
lines.push(detail);
if (detail !== lastPrintedLine) {
console.log(` ${detail}`);
lastPrintedLine = detail;
}
try {
child.kill("SIGTERM");
} catch {
// Best effort only — the child may have already exited.
}
detachChild();
finish({ status: 0, output: lines.join("\n"), sawProgress: true, forcedReady: true });
} finally {
polling = false;
}
}, pollIntervalMs)
: null;
readyTimer?.unref?.();

return new Promise((resolve) => {
resolvePromise = resolve;
child.on("error", (error) => {
if (settled) return;
settled = true;
if (pending) flushLine(pending);
// @ts-expect-error — Node ErrnoException has .code but TS types Error
const code = error && error.code;
const detail = code
? `spawn failed: ${error.message} (${code})`
: `spawn failed: ${error.message}`;
lines.push(detail);
resolve({ status: 1, output: lines.join("\n"), sawProgress: false });
finish({ status: 1, output: lines.join("\n"), sawProgress: false });
});

child.on("close", (code) => {
if (settled) return;
settled = true;
if (pending) flushLine(pending);
resolve({ status: code ?? 1, output: lines.join("\n"), sawProgress });
finish({ status: code ?? 1, output: lines.join("\n"), sawProgress });
});
});
}
Expand Down Expand Up @@ -1395,7 +1443,12 @@ async function createSandbox(gpu, model, provider, preferredInferenceApi = null)
...envArgs,
"nemoclaw-start",
])} 2>&1`;
const createResult = await streamSandboxCreate(createCommand, sandboxEnv);
const createResult = await streamSandboxCreate(createCommand, sandboxEnv, {
readyCheck: () => {
const list = runCaptureOpenshell(["sandbox", "list"], { ignoreError: true });
return isSandboxReady(list, sandboxName);
},
});

// Clean up build context regardless of outcome
run(`rm -rf "${buildCtx}"`, { ignoreError: true });
Expand Down
11 changes: 10 additions & 1 deletion scripts/nemoclaw-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,17 @@ if [ "$(id -u)" -ne 0 ]; then
exec "${NEMOCLAW_CMD[@]}"
fi

# In non-root mode, detach gateway stdout/stderr from the sandbox-create
# stream so openshell sandbox create can return once the container is ready.
touch /tmp/gateway.log
chmod 600 /tmp/gateway.log

# Separate log for auto-pair in non-root mode as well.
touch /tmp/auto-pair.log
chmod 600 /tmp/auto-pair.log

# Start gateway in background, auto-pair, then wait
"$OPENCLAW" gateway run &
nohup "$OPENCLAW" gateway run >/tmp/gateway.log 2>&1 &
GATEWAY_PID=$!
echo "[gateway] openclaw gateway launched (pid $GATEWAY_PID)"
start_auto_pair
Expand Down
2 changes: 1 addition & 1 deletion test/credential-exposure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("credential exposure in process arguments", () => {
const src = fs.readFileSync(ONBOARD_JS, "utf-8");

expect(src).toMatch(/const sandboxEnv = \{ \.\.\.process\.env \};/);
expect(src).toMatch(/streamSandboxCreate\(createCommand, sandboxEnv\)/);
expect(src).toMatch(/streamSandboxCreate\(createCommand, sandboxEnv(?:, \{)?/);
expect(src).not.toMatch(/envArgs\.push\(formatEnvAssignment\("NVIDIA_API_KEY"/);
expect(src).not.toMatch(/envArgs\.push\(formatEnvAssignment\("DISCORD_BOT_TOKEN"/);
expect(src).not.toMatch(/envArgs\.push\(formatEnvAssignment\("SLACK_BOT_TOKEN"/);
Expand Down
18 changes: 18 additions & 0 deletions test/nemoclaw-start.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import fs from "node:fs";
import path from "node:path";
import { describe, it, expect } from "vitest";

const START_SCRIPT = path.join(import.meta.dirname, "..", "scripts", "nemoclaw-start.sh");

describe("nemoclaw-start non-root fallback", () => {
it("detaches gateway output from sandbox create in non-root mode", () => {
const src = fs.readFileSync(START_SCRIPT, "utf-8");

expect(src).toMatch(/if \[ "\$\(id -u\)" -ne 0 \]; then/);
expect(src).toMatch(/touch \/tmp\/gateway\.log/);
expect(src).toMatch(/nohup "\$OPENCLAW" gateway run >\/tmp\/gateway\.log 2>&1 &/);
});
});
136 changes: 134 additions & 2 deletions test/onboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,14 @@ console.log(JSON.stringify({ liveExists, sandbox: registry.getSandbox("my-assist
});

assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(result.stdout.trim().split("\n").pop());
const payloadLine = result.stdout
.trim()
.split("\n")
.slice()
.reverse()
.find((line) => line.startsWith("{") && line.endsWith("}"));
assert.ok(payloadLine, `expected JSON payload in stdout:\n${result.stdout}`);
const payload = JSON.parse(payloadLine);
assert.equal(payload.liveExists, false);
assert.equal(payload.sandbox, null);
});
Expand Down Expand Up @@ -503,7 +510,14 @@ const { createSandbox } = require(${onboardPath});
});

assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(result.stdout.trim().split("\n").pop());
const payloadLine = result.stdout
.trim()
.split("\n")
.slice()
.reverse()
.find((line) => line.startsWith("{") && line.endsWith("}"));
assert.ok(payloadLine, `expected JSON payload in stdout:\n${result.stdout}`);
const payload = JSON.parse(payloadLine);
assert.equal(payload.sandboxName, "my-assistant");
const createCommand = payload.commands.find((entry) => entry.command.includes("'sandbox' 'create'"));
assert.ok(createCommand, "expected sandbox create command");
Expand All @@ -515,6 +529,124 @@ const { createSandbox } = require(${onboardPath});
assert.doesNotMatch(createCommand.command, /SLACK_BOT_TOKEN=/);
});

it("continues once the sandbox is Ready even if the create stream never closes", async () => {
const repoRoot = path.join(import.meta.dirname, "..");
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-onboard-create-ready-"));
const fakeBin = path.join(tmpDir, "bin");
const scriptPath = path.join(tmpDir, "create-sandbox-ready-check.js");
const payloadPath = path.join(tmpDir, "payload.json");
const onboardPath = JSON.stringify(path.join(repoRoot, "bin", "lib", "onboard.js"));
const runnerPath = JSON.stringify(path.join(repoRoot, "bin", "lib", "runner.js"));
const registryPath = JSON.stringify(path.join(repoRoot, "bin", "lib", "registry.js"));
const preflightPath = JSON.stringify(path.join(repoRoot, "bin", "lib", "preflight.js"));
const credentialsPath = JSON.stringify(path.join(repoRoot, "bin", "lib", "credentials.js"));

fs.mkdirSync(fakeBin, { recursive: true });
fs.writeFileSync(path.join(fakeBin, "openshell"), "#!/usr/bin/env bash\nexit 0\n", { mode: 0o755 });

const script = String.raw`
const runner = require(${runnerPath});
const registry = require(${registryPath});
const preflight = require(${preflightPath});
const credentials = require(${credentialsPath});
const childProcess = require("node:child_process");
const { EventEmitter } = require("node:events");
const fs = require("node:fs");

const commands = [];
let sandboxListCalls = 0;
const keepAlive = setInterval(() => {}, 1000);
runner.run = (command, opts = {}) => {
commands.push({ command, env: opts.env || null });
return { status: 0 };
};
runner.runCapture = (command) => {
if (command.includes("'sandbox' 'get' 'my-assistant'")) return "";
if (command.includes("'sandbox' 'list'")) {
sandboxListCalls += 1;
return sandboxListCalls >= 2 ? "my-assistant Ready" : "my-assistant Pending";
}
return "";
};
registry.registerSandbox = () => true;
registry.removeSandbox = () => true;
preflight.checkPortAvailable = async () => ({ ok: true });
credentials.prompt = async () => "";

childProcess.spawn = (...args) => {
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.killCalls = [];
child.unrefCalls = 0;
child.stdout.destroyCalls = 0;
child.stderr.destroyCalls = 0;
child.stdout.destroy = () => {
child.stdout.destroyCalls += 1;
};
child.stderr.destroy = () => {
child.stderr.destroyCalls += 1;
};
child.unref = () => {
child.unrefCalls += 1;
};
child.kill = (signal) => {
child.killCalls.push(signal);
process.nextTick(() => child.emit("close", signal === "SIGTERM" ? 0 : 1));
return true;
};
commands.push({ command: args[1][1], env: args[2]?.env || null, child });
process.nextTick(() => {
child.stdout.emit("data", Buffer.from("Created sandbox: my-assistant\n"));
});
return child;
};

const { createSandbox } = require(${onboardPath});

(async () => {
process.env.OPENSHELL_GATEWAY = "nemoclaw";
const sandboxName = await createSandbox(null, "gpt-5.4");
const createCommand = commands.find((entry) => entry.command.includes("'sandbox' 'create'"));
fs.writeFileSync(${JSON.stringify(payloadPath)}, JSON.stringify({
sandboxName,
sandboxListCalls,
killCalls: createCommand.child.killCalls,
unrefCalls: createCommand.child.unrefCalls,
stdoutDestroyCalls: createCommand.child.stdout.destroyCalls,
stderrDestroyCalls: createCommand.child.stderr.destroyCalls,
}));
clearInterval(keepAlive);
})().catch((error) => {
clearInterval(keepAlive);
console.error(error);
process.exit(1);
});
`;
fs.writeFileSync(scriptPath, script);

const result = spawnSync(process.execPath, [scriptPath], {
cwd: repoRoot,
encoding: "utf-8",
env: {
...process.env,
HOME: tmpDir,
PATH: `${fakeBin}:${process.env.PATH || ""}`,
NEMOCLAW_NON_INTERACTIVE: "1",
},
timeout: 15000,
});

assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(fs.readFileSync(payloadPath, "utf8"));
assert.equal(payload.sandboxName, "my-assistant");
assert.ok(payload.sandboxListCalls >= 2);
assert.deepEqual(payload.killCalls, ["SIGTERM"]);
assert.equal(payload.unrefCalls, 1);
assert.equal(payload.stdoutDestroyCalls, 1);
assert.equal(payload.stderrDestroyCalls, 1);
});

it("accepts gateway inference when system inference is separately not configured", () => {
const repoRoot = path.join(import.meta.dirname, "..");
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-onboard-inference-get-"));
Expand Down
Loading