Skip to content
Merged
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
274 changes: 274 additions & 0 deletions src/lib/actions/sandbox/rebuild-flow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { createRequire } from "node:module";

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

type RebuildSandbox =
typeof import("../../../../dist/lib/actions/sandbox/rebuild")["rebuildSandbox"];

const requireDist = createRequire(import.meta.url);
const rebuildModulePath = "../../../../dist/lib/actions/sandbox/rebuild.js";

type RebuildFlowOverrides = {
applyPreset?: (presetName: string) => boolean;
executeSandboxCommand?: () => { status: number; stdout: string; stderr: string } | null;
repairMutableConfigPerms?: () =>
| { applied: false; skipReason: "agent" | "locked" | "unreadable"; reason: string }
| { applied: true; verified: boolean; errors: string[] };
restoreSandboxState?: () => {
success: boolean;
restoredDirs: string[];
restoredFiles: string[];
failedDirs: string[];
failedFiles: string[];
};
};

type RebuildFlowHarness = {
rebuildSandbox: RebuildSandbox;
applyPresetSpy: MockInstance;
backupSandboxStateSpy: MockInstance;
errorSpy: MockInstance;
executeSandboxCommandSpy: MockInstance;
logSpy: MockInstance;
onboardSpy: MockInstance;
registryUpdateSpy: MockInstance;
relockSpy: MockInstance;
restoreSandboxStateSpy: MockInstance;
runOpenshellSpy: MockInstance;
};

const originalSandboxName = process.env.NEMOCLAW_SANDBOX_NAME;

function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}): RebuildFlowHarness {
delete require.cache[requireDist.resolve(rebuildModulePath)];

const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => undefined);

const gatewayDrift = requireDist("../../../../dist/lib/adapters/openshell/gateway-drift.js");
const openshellRuntime = requireDist("../../../../dist/lib/adapters/openshell/runtime.js");
const sandboxList = requireDist("../../../../dist/lib/openshell-sandbox-list.js");
const resolve = requireDist("../../../../dist/lib/adapters/openshell/resolve.js");
const agentDefs = requireDist("../../../../dist/lib/agent/defs.js");
const agentRuntime = requireDist("../../../../dist/lib/agent/runtime.js");
const onboardMod = requireDist("../../../../dist/lib/onboard.js");
const onboardSession = requireDist("../../../../dist/lib/state/onboard-session.js");
const registry = requireDist("../../../../dist/lib/state/registry.js");
const sandboxState = requireDist("../../../../dist/lib/state/sandbox.js");
const sandboxSession = requireDist("../../../../dist/lib/state/sandbox-session.js");
const sandboxVersion = requireDist("../../../../dist/lib/sandbox/version.js");
const destroy = requireDist("../../../../dist/lib/actions/sandbox/destroy.js");
const rebuildShields = requireDist("../../../../dist/lib/actions/sandbox/rebuild-shields.js");
const nim = requireDist("../../../../dist/lib/inference/nim.js");
const policies = requireDist("../../../../dist/lib/policy/index.js");
const processRecovery = requireDist("../../../../dist/lib/actions/sandbox/process-recovery.js");
const shields = requireDist("../../../../dist/lib/shields/index.js");

const session = {
sandboxName: "alpha",
provider: "ollama-local",
model: "nvidia/nemotron",
credentialEnv: null,
metadata: {},
hermesToolGateways: [],
};
const rebuildShieldsWindow = { relocked: false, wasLocked: false };
const agentDef = { name: "openclaw", expectedVersion: "0.2.0", messagingPlatforms: [] };

vi.spyOn(gatewayDrift, "detectOpenShellStateRpcPreflightIssue").mockReturnValue(null);
vi.spyOn(gatewayDrift, "detectOpenShellStateRpcResultIssue").mockReturnValue(null);
vi.spyOn(sandboxList, "captureSandboxListWithGatewayRecovery").mockResolvedValue({
result: { status: 0, output: "alpha Ready" },
});
vi.spyOn(resolve, "resolveOpenshell").mockReturnValue(null);
vi.spyOn(agentDefs, "loadAgent").mockReturnValue(agentDef);
vi.spyOn(agentRuntime, "getSessionAgent").mockReturnValue({ name: "openclaw" });
vi.spyOn(agentRuntime, "getAgentDisplayName").mockReturnValue("OpenClaw");
vi.spyOn(onboardSession, "loadSession").mockReturnValue(session);
vi.spyOn(onboardSession, "updateSession").mockImplementation((mutator: unknown) => {
if (typeof mutator !== "function") {
throw new TypeError("updateSession expected a mutator function");
}
(mutator as (value: typeof session) => typeof session | void)(session);
return session;
});
vi.spyOn(registry, "getSandbox").mockReturnValue({
name: "alpha",
provider: "ollama-local",
model: "nvidia/nemotron",
policies: ["npm"],
agent: null,
nimContainer: null,
});
vi.spyOn(registry, "listSandboxes").mockReturnValue({ sandboxes: [] });
const registryUpdateSpy = vi.spyOn(registry, "updateSandbox").mockImplementation(() => undefined);
vi.spyOn(sandboxSession, "getActiveSandboxSessions").mockReturnValue({
detected: false,
sessions: [],
});
vi.spyOn(sandboxVersion, "checkAgentVersion").mockReturnValue({
expectedVersion: "0.2.0",
sandboxVersion: "0.1.0",
});
vi.spyOn(rebuildShields, "openRebuildShieldsWindow").mockReturnValue(rebuildShieldsWindow);
const relockSpy = vi
.spyOn(rebuildShields, "relockRebuildShieldsWindow")
.mockImplementation((...args: unknown[]) => {
const window = args[1] as typeof rebuildShieldsWindow;
window.relocked = true;
return true;
});
const backupSandboxStateSpy = vi.spyOn(sandboxState, "backupSandboxState").mockReturnValue({
success: true,
backedUpDirs: ["workspace"],
backedUpFiles: ["user.md"],
failedDirs: [],
failedFiles: [],
manifest: {
backupPath: "/tmp/nemoclaw-rebuild-backup",
timestamp: "2026-06-01T00:00:00.000Z",
policyPresets: ["npm", "bad", "throw"],
},
});
const restoreSandboxStateSpy = vi.spyOn(sandboxState, "restoreSandboxState").mockImplementation(
overrides.restoreSandboxState ??
(() => ({
success: true,
restoredDirs: ["workspace"],
restoredFiles: ["user.md"],
failedDirs: [],
failedFiles: [],
})),
);
const runOpenshellSpy = vi
.spyOn(openshellRuntime, "runOpenshell")
.mockReturnValue({ status: 0, output: "" });
vi.spyOn(destroy, "removeSandboxRegistryEntry").mockImplementation(() => undefined);
vi.spyOn(nim, "stopNimContainer").mockImplementation(() => undefined);
vi.spyOn(nim, "stopNimContainerByName").mockImplementation(() => undefined);
const onboardSpy = vi.spyOn(onboardMod, "onboard").mockResolvedValue(undefined);
const applyPresetSpy = vi
.spyOn(policies, "applyPreset")
.mockImplementation((_sandboxName: unknown, presetName: unknown) => {
const normalizedPresetName = String(presetName);
if (overrides.applyPreset) return overrides.applyPreset(normalizedPresetName);
if (normalizedPresetName === "throw") throw new Error("preset boom");
return normalizedPresetName === "npm";
});
const executeSandboxCommandSpy = vi
.spyOn(processRecovery, "executeSandboxCommand")
.mockImplementation(
overrides.executeSandboxCommand ?? (() => ({ status: 0, stdout: "doctor ok", stderr: "" })),
);
vi.spyOn(shields, "repairMutableConfigPerms").mockImplementation(
overrides.repairMutableConfigPerms ?? (() => ({ applied: true, verified: true, errors: [] })),
);

errorSpy.mockClear();
logSpy.mockClear();
warnSpy.mockClear();

return {
rebuildSandbox: requireDist(rebuildModulePath).rebuildSandbox,
applyPresetSpy,
backupSandboxStateSpy,
errorSpy,
executeSandboxCommandSpy,
logSpy,
onboardSpy,
registryUpdateSpy,
relockSpy,
restoreSandboxStateSpy,
runOpenshellSpy,
};
}

describe("rebuildSandbox flow", () => {
beforeEach(() => {
delete process.env.NEMOCLAW_SANDBOX_NAME;
});

afterEach(() => {
vi.restoreAllMocks();
delete require.cache[requireDist.resolve(rebuildModulePath)];
if (originalSandboxName === undefined) {
delete process.env.NEMOCLAW_SANDBOX_NAME;
} else {
process.env.NEMOCLAW_SANDBOX_NAME = originalSandboxName;
}
});

it("backs up, recreates, restores, reapplies policy, and relocks on a successful OpenClaw rebuild", async () => {
const harness = createRebuildFlowHarness({
applyPreset: () => true,
});

await expect(
harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }),
).resolves.toBeUndefined();

expect(harness.backupSandboxStateSpy).toHaveBeenCalledWith("alpha");
expect(harness.runOpenshellSpy).toHaveBeenCalledWith(
["sandbox", "delete", "alpha"],
expect.objectContaining({ ignoreError: true }),
);
expect(harness.onboardSpy).toHaveBeenCalledWith(
expect.objectContaining({
resume: true,
nonInteractive: true,
recreateSandbox: true,
autoYes: true,
}),
);
expect(harness.restoreSandboxStateSpy).toHaveBeenCalledWith(
"alpha",
"/tmp/nemoclaw-rebuild-backup",
);
expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "npm");
expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "bad");
expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "throw");
expect(harness.registryUpdateSpy).toHaveBeenCalledWith("alpha", { agentVersion: "0.2.0" });
expect(harness.executeSandboxCommandSpy).toHaveBeenCalledWith("alpha", "openclaw doctor --fix");
expect(harness.relockSpy).toHaveBeenCalledWith("alpha", expect.any(Object), true, "nemoclaw");
expect(process.env.NEMOCLAW_SANDBOX_NAME).toBe("alpha");
expect(harness.logSpy.mock.calls.map((call) => String(call[0])).join("\n")).toContain(
"rebuilt successfully",
);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

it("finishes the rebuild while surfacing incomplete post-restore work", async () => {
const harness = createRebuildFlowHarness({
executeSandboxCommand: () => ({ status: 1, stdout: "", stderr: "hash refresh failed" }),
repairMutableConfigPerms: () => ({
applied: false,
skipReason: "unreadable",
reason: "cannot stat mutable config",
}),
restoreSandboxState: () => ({
success: false,
restoredDirs: ["workspace"],
restoredFiles: [],
failedDirs: ["config"],
failedFiles: ["user.md"],
}),
});

await expect(
harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }),
).resolves.toBeUndefined();

const output = harness.logSpy.mock.calls.map((call) => String(call[0])).join("\n");
expect(output).toContain("rebuilt but some post-restore steps were incomplete");
expect(output).toContain("State restore was incomplete");
expect(output).toContain("Mutable config permissions were not verified");
expect(output).toContain("Mutable OpenClaw config hash was not refreshed");
expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "bad");
expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "throw");
expect(harness.errorSpy).toHaveBeenCalledWith(expect.stringContaining("bad, throw"));
expect(harness.relockSpy).toHaveBeenCalledWith("alpha", expect.any(Object), true, "nemoclaw");
});
});