Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions docs/reference/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,10 @@ The sandbox name is required, and the command takes no flags. The sandbox must a

<AgentOnly variant="openclaw">

For an older OpenClaw registration without recorded lifecycle identity, `launch` requires a ready live sandbox with an observable identity before it starts the agent.
If this identity check fails, `launch` stops before starting the agent.
Run `$$nemoclaw <name> doctor`, resolve the reported issue, and retry `$$nemoclaw launch <name>`.

`launch` returns the agent's exit code when the post-command OpenClaw permission cleanup succeeds. If that cleanup cannot inspect, restore, or verify the mutable config permission contract, `launch` fails closed with exit `1` and prints `OpenClaw permission cleanup failed (...)` to `stderr`.

</AgentOnly>
Expand Down
204 changes: 203 additions & 1 deletion src/lib/actions/sandbox/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

import { afterEach, describe, expect, it, vi } from "vitest";

import { execSandbox, type SandboxExecCleanupDeps, workdirMissingMessage } from "./exec";
import { execTimeout, testTimeoutOptions } from "../../../../test/helpers/timeouts";

import {
withMcpLifecycleLock,
withMcpLifecycleLockSync,
isMcpLifecycleLockHeld,
} from "../../state/mcp-lifecycle-lock-acquisition";
import {
withPortableHostFence,
portableHostFencePath,
} from "../../state/portable-uninstall-retirement";
import {
execSandbox,
startSandboxExec,
type SandboxExecCleanupDeps,
workdirMissingMessage,
} from "./exec";

describe("workdirMissingMessage", () => {
it("renders a user-facing CLI error with the offending path", () => {
Expand Down Expand Up @@ -303,3 +324,184 @@ describe("execSandbox scope-upgrade hint wiring (#9744)", () => {
expect(stderr).toBe("");
});
});

it.each([
{ mode: "sync", error: new Error("lock timed out"), commandCode: 0, invocationFailed: false },
{
mode: "async",
error: new Error("migration blocked"),
commandCode: 23,
invocationFailed: false,
},
{ mode: "async", error: "registry unavailable", commandCode: 1, invocationFailed: true },
])(
"reports $mode cleanup authority failure after command status $commandCode (#11647)",
async ({ mode, error, commandCode, invocationFailed }) => {
const stderr = vi.spyOn(console, "error").mockImplementation(() => {});
const release = vi.fn();
const inspect = vi.fn(() => ({ applies: true as const, ok: true, issues: [] }));
const exit = vi.fn((code: number): never => {
throw new Error(`exit:${code}`);
});
try {
const finish = await startSandboxExec(
"alpha",
["true"],
{},
{
selectGateway: () => ({ outcome: "unregistered", gatewayName: null }),
commandExecutor: {
probeDirectory: async () => ({ state: "present" }),
runStreaming: async () => ({
outcome: invocationFailed
? { kind: "failed", error: { kind: "invocation", message: "transport failed" } }
: { kind: "completed", exitCode: commandCode },
release,
}),
},
withCleanupAuthority:
mode === "sync"
? () => {
throw error;
}
: () => Promise.reject(error),
cleanupDeps: {
getSandbox: () => ({ agent: "openclaw" }),
inspectMutableConfigPerms: inspect,
repairMutableConfigPerms: () => ({ applied: true, verified: true, errors: [] }),
},
policyHint: {
env: {},
probeLogs: () => "",
enableAudit: () => {},
sleep: async () => {},
attempts: 1,
},
exit,
},
);
await expect(finish()).rejects.toThrow("exit:1");
expect(exit).toHaveBeenCalledExactlyOnceWith(1);
expect(stderr).toHaveBeenCalledWith(
` OpenClaw permission cleanup failed (command exit ${commandCode}; cleanup exit 1): cleanup authority unavailable: ${error instanceof Error ? error.message : error}`,
);
expect(
stderr.mock.calls.filter(([line]) => String(line).includes("Failed to invoke openshell")),
).toHaveLength(invocationFailed ? 1 : 0);
expect(inspect).not.toHaveBeenCalled();
expect(release).toHaveBeenCalledOnce();
} finally {
stderr.mockRestore();
}
},
);

it(
"releases authority to another process before interactive completion (#11647)",
testTimeoutOptions(15_000),
async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-launch-exec-"));
const stateDir = path.join(home, "state");
const lockOptions = { stateDir };
const sandboxName = "launch-exec";
let endSession: () => void = () => {};
const ended = new Promise<void>((resolve) => {
endSession = resolve;
});
const release = vi.fn();
const cleanup = vi.fn(() =>
withMcpLifecycleLockSync(
sandboxName,
() => {
expect(isMcpLifecycleLockHeld(sandboxName, stateDir)).toBe(true);
return { applies: true as const, ok: true, issues: [] };
},
lockOptions,
),
);
let dispatched = false;
let lockHeldDuringCleanup: boolean | null = null;
let fenceHeldDuringCleanup: boolean | null = null;
try {
const finish = await withPortableHostFence(home, () =>
withMcpLifecycleLock(
sandboxName,
() =>
startSandboxExec(
sandboxName,
["bash", "-lc", "openclaw tui"],
{ tty: true, stdin: true, timeoutSeconds: 0 },
{
selectGateway: () => ({ outcome: "unregistered", gatewayName: null }),
commandExecutor: {
probeDirectory: async () => ({ state: "present" }),
runStreaming: async () => {
expect(isMcpLifecycleLockHeld(sandboxName, stateDir)).toBe(true);
expect(fs.existsSync(portableHostFencePath(home))).toBe(true);
dispatched = true;
await ended;
return { outcome: { kind: "completed", exitCode: 0 }, release };
},
},
cleanupDeps: {
getSandbox: () => {
lockHeldDuringCleanup = isMcpLifecycleLockHeld(sandboxName, stateDir);
fenceHeldDuringCleanup = fs.existsSync(portableHostFencePath(home));
return { agent: "openclaw" };
},
inspectMutableConfigPerms: cleanup,
repairMutableConfigPerms: () => {
throw new Error("healthy config needs no repair");
},
},
exit: (code) => {
throw new Error(`exit:${code}`);
},
},
),
lockOptions,
),
);
expect(dispatched).toBe(true);
expect(cleanup).not.toHaveBeenCalled();
const contender = spawnSync(
process.execPath,
[
"--no-warnings",
"--import",
"tsx",
"--input-type=module",
"-e",
`
const retirement = (await import(process.argv[1])).default;
const lifecycle = (await import(process.argv[2])).default;
await retirement.withPortableHostFence(process.argv[3], () =>
lifecycle.withMcpLifecycleLock(process.argv[4], () => {
console.log("contender-entered");
}, { stateDir: process.argv[5] }),
);
`,
new URL("../../state/portable-uninstall-retirement.ts", import.meta.url).href,
new URL("../../state/mcp-lifecycle-lock-acquisition.ts", import.meta.url).href,
home,
sandboxName,
stateDir,
],
{ encoding: "utf8", timeout: execTimeout() },
);
expect(contender.status, contender.stderr || String(contender.error ?? "")).toBe(0);
expect(contender.stdout.trim()).toBe("contender-entered");
expect(cleanup).not.toHaveBeenCalled();
expect(release).not.toHaveBeenCalled();
endSession();
await expect(finish()).rejects.toThrow("exit:0");
expect(lockHeldDuringCleanup).toBe(false);
expect(fenceHeldDuringCleanup).toBe(false);
expect(cleanup).toHaveBeenCalledOnce();
expect(release).toHaveBeenCalledOnce();
} finally {
endSession();
fs.rmSync(home, { recursive: true, force: true });
}
},
);
Loading
Loading