From 312e04418e35a4b20fdc6e2135d0d4c65a3fffcb Mon Sep 17 00:00:00 2001 From: Charan Jagwani Date: Wed, 8 Jul 2026 19:48:45 -0700 Subject: [PATCH 1/3] fix(rebuild): preserve legacy OpenClaw config restores --- ...sandbox-recreated-openclaw-restore.test.ts | 306 ++++++++++++++++++ src/lib/state/sandbox.ts | 11 +- test/e2e/live/rebuild-openclaw.test.ts | 3 + 3 files changed, 319 insertions(+), 1 deletion(-) create mode 100644 src/lib/state/sandbox-recreated-openclaw-restore.test.ts diff --git a/src/lib/state/sandbox-recreated-openclaw-restore.test.ts b/src/lib/state/sandbox-recreated-openclaw-restore.test.ts new file mode 100644 index 00000000000..ba1c031d944 --- /dev/null +++ b/src/lib/state/sandbox-recreated-openclaw-restore.test.ts @@ -0,0 +1,306 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import { describe, expect, it } from "vitest"; + +import type { OpenClawImagePluginInstall } from "./openclaw-plugin-restore.js"; +import { restoreRecreatedSandboxState } from "./sandbox.js"; + +const OPENCLAW_DIR = "/sandbox/.openclaw"; + +function imageInstall(id: string, extensionDir: string): OpenClawImagePluginInstall { + const installPath = `${OPENCLAW_DIR}/extensions/${extensionDir}`; + return { id, installPath, loadPaths: [installPath] }; +} + +function writeExecutable(filePath: string, source: string): void { + fs.writeFileSync(filePath, source, { mode: 0o755 }); +} + +function extensionDir(install: OpenClawImagePluginInstall): string | null { + const prefix = `${OPENCLAW_DIR}/extensions/`; + return install.installPath.startsWith(prefix) ? install.installPath.slice(prefix.length) : null; +} + +function runRestoreScenario(options: { + backupConfig: Record; + backupExtensionDirs: string[]; + freshConfig: Record; + freshPluginInstalls: OpenClawImagePluginInstall[]; + previousPluginInstalls?: OpenClawImagePluginInstall[]; +}): { + cleanupCommand: string | undefined; + freshMarkers: Record; + restore: ReturnType; + restoredConfig: Record; + staleUserExtensionExists: boolean; + userExtensionMarker: string; +} { + const fixture = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-recreated-restore-")); + const previousOpenshellBin = process.env.NEMOCLAW_OPENSHELL_BIN; + const previousPath = process.env.PATH; + try { + const binDir = path.join(fixture, "bin"); + const openclawDir = path.join(fixture, "sandbox-root", ".openclaw"); + const extensionsDir = path.join(openclawDir, "extensions"); + const backupPath = path.join(fixture, "backup"); + const backupExtensionsDir = path.join(backupPath, "extensions"); + const sshLog = path.join(fixture, "ssh-log.jsonl"); + const freshExtensionDirs = [ + "nemoclaw", + ...options.freshPluginInstalls + .map(extensionDir) + .filter((entry): entry is string => entry !== null), + ]; + fs.mkdirSync(binDir, { recursive: true }); + + for (const extensionName of freshExtensionDirs) { + const target = path.join(extensionsDir, extensionName); + fs.mkdirSync(target, { recursive: true }); + fs.writeFileSync(path.join(target, "marker.txt"), `fresh-${extensionName}\n`); + } + fs.mkdirSync(path.join(extensionsDir, "stale-user-extension"), { recursive: true }); + fs.writeFileSync(path.join(extensionsDir, "stale-user-extension", "marker.txt"), "stale\n"); + + for (const extensionName of ["nemoclaw", ...options.backupExtensionDirs]) { + const target = path.join(backupExtensionsDir, extensionName); + fs.mkdirSync(target, { recursive: true }); + fs.writeFileSync(path.join(target, "marker.txt"), `old-${extensionName}\n`); + } + fs.mkdirSync(path.join(backupExtensionsDir, "user-extension"), { recursive: true }); + fs.writeFileSync(path.join(backupExtensionsDir, "user-extension", "marker.txt"), "restored\n"); + + fs.mkdirSync(backupPath, { recursive: true }); + fs.writeFileSync(path.join(backupPath, "openclaw.json"), JSON.stringify(options.backupConfig)); + fs.writeFileSync(path.join(openclawDir, "openclaw.json"), JSON.stringify(options.freshConfig)); + const manifest: Record = { + version: 1, + sandboxName: "alpha", + timestamp: "2026-07-08T12-00-00-000Z", + agentType: "openclaw", + agentVersion: null, + expectedVersion: null, + stateDirs: ["extensions"], + backedUpDirs: ["extensions"], + stateFiles: [{ path: "openclaw.json", strategy: "copy" }], + dir: OPENCLAW_DIR, + backupPath, + blueprintDigest: null, + }; + if (options.previousPluginInstalls !== undefined) { + manifest.openclawImagePluginInstalls = options.previousPluginInstalls; + } + fs.writeFileSync(path.join(backupPath, "rebuild-manifest.json"), JSON.stringify(manifest)); + + const openshell = path.join(binDir, "openshell"); + writeExecutable( + openshell, + `#!/usr/bin/env node +const args = process.argv.slice(2); +if (args[0] === "sandbox" && args[1] === "ssh-config") { + process.stdout.write("Host openshell-alpha\\n HostName 127.0.0.1\\n User sandbox\\n"); +} +process.exit(0); +`, + ); + writeExecutable( + path.join(binDir, "ssh"), + `#!/usr/bin/env node +const fs = require("node:fs"); +const path = require("node:path"); +const { spawnSync } = require("node:child_process"); +const cmd = process.argv[process.argv.length - 1] || ""; +const openclawDir = ${JSON.stringify(openclawDir)}; +const extensionsDir = ${JSON.stringify(extensionsDir)}; +fs.appendFileSync(${JSON.stringify(sshLog)}, JSON.stringify({ cmd }) + "\\n"); +function readStdin() { + const chunks = []; + for (;;) { + const buffer = Buffer.alloc(65536); + const count = fs.readSync(0, buffer, 0, buffer.length, null); + if (count === 0) break; + chunks.push(buffer.subarray(0, count)); + } + return Buffer.concat(chunks); +} +if (cmd.includes("${OPENCLAW_DIR}/extensions") && cmd.includes("-exec rm -rf")) { + const preserved = new Set(${JSON.stringify(freshExtensionDirs)}); + for (const entry of fs.readdirSync(extensionsDir)) { + if (!preserved.has(entry)) fs.rmSync(path.join(extensionsDir, entry), { recursive: true, force: true }); + } + process.exit(0); +} +if (cmd.includes("tar --no-same-owner -xf -")) { + const result = spawnSync("tar", ["--no-same-owner", "-xf", "-", "-C", openclawDir], { + input: readStdin(), + stdio: ["pipe", "pipe", "pipe"], + }); + process.exit(result.status || 0); +} +if (cmd.includes("chown") || cmd.includes("[ -d ")) process.exit(0); +if (cmd.includes("openclaw.json") && cmd.includes("cat --")) { + process.stdout.write(fs.readFileSync(path.join(openclawDir, "openclaw.json"))); + process.exit(0); +} +if (cmd.includes("openclaw.json") && cmd.includes(".nemoclaw-restore")) { + fs.writeFileSync(path.join(openclawDir, "openclaw.json"), readStdin()); + process.exit(0); +} +process.exit(1); +`, + ); + + process.env.NEMOCLAW_OPENSHELL_BIN = openshell; + process.env.PATH = `${binDir}:${previousPath ?? ""}`; + const restore = restoreRecreatedSandboxState("alpha", backupPath, { + targetAgentType: "openclaw", + freshOpenClawImagePluginInstalls: options.freshPluginInstalls, + }); + const loggedCommands = fs + .readFileSync(sshLog, "utf8") + .trim() + .split("\n") + .map((line) => JSON.parse(line).cmd as string); + + return { + cleanupCommand: loggedCommands.find((command) => command.includes("-exec rm -rf")), + freshMarkers: Object.fromEntries( + freshExtensionDirs.map((name) => [ + name, + fs.readFileSync(path.join(extensionsDir, name, "marker.txt"), "utf8"), + ]), + ), + restore, + restoredConfig: JSON.parse(fs.readFileSync(path.join(openclawDir, "openclaw.json"), "utf8")), + staleUserExtensionExists: fs.existsSync(path.join(extensionsDir, "stale-user-extension")), + userExtensionMarker: fs.readFileSync( + path.join(extensionsDir, "user-extension", "marker.txt"), + "utf8", + ), + }; + } finally { + if (previousOpenshellBin === undefined) delete process.env.NEMOCLAW_OPENSHELL_BIN; + else process.env.NEMOCLAW_OPENSHELL_BIN = previousOpenshellBin; + if (previousPath === undefined) delete process.env.PATH; + else process.env.PATH = previousPath; + fs.rmSync(fixture, { recursive: true, force: true }); + } +} + +function expectSuccessfulRestore(result: ReturnType): void { + expect(result.restore).toEqual({ + success: true, + restoredDirs: ["extensions"], + failedDirs: [], + restoredFiles: ["openclaw.json"], + failedFiles: [], + }); + expect(result.staleUserExtensionExists).toBe(false); + expect(result.userExtensionMarker).toBe("restored\n"); +} + +describe("recreated OpenClaw state restore", () => { + it.each([ + { provenance: "missing legacy", previousPluginInstalls: undefined }, + { provenance: "known-empty", previousPluginInstalls: [] }, + ])("restores config and extensions with $provenance previous provenance", ({ + previousPluginInstalls, + }) => { + const weather = imageInstall("weather", "weather"); + const result = runRestoreScenario({ + previousPluginInstalls, + freshPluginInstalls: [weather], + backupExtensionDirs: ["weather"], + backupConfig: { + gateway: { auth: { token: "stale-token" } }, + mcpServers: { filesystem: { command: "npx" } }, + plugins: { entries: { "user-plugin": { enabled: true } } }, + }, + freshConfig: { + gateway: { auth: { token: "fresh-token" } }, + plugins: { + entries: { weather: { enabled: true, config: { revision: "fresh" } } }, + load: { paths: weather.loadPaths }, + }, + }, + }); + + expectSuccessfulRestore(result); + expect(result.freshMarkers).toEqual({ + nemoclaw: "fresh-nemoclaw\n", + weather: "fresh-weather\n", + }); + expect(result.restoredConfig.gateway.auth.token).toBe("fresh-token"); + expect(result.restoredConfig.mcpServers.filesystem.command).toBe("npx"); + expect(result.restoredConfig.plugins.entries).toEqual({ + "user-plugin": { enabled: true }, + weather: { enabled: true, config: { revision: "fresh" } }, + }); + expect(result.cleanupCommand).toContain("! -name 'nemoclaw'"); + expect(result.cleanupCommand).toContain("! -name 'weather'"); + }); + + it("reconciles populated previous and fresh image-plugin provenance during config restore", () => { + const previousWeather = imageInstall("weather", "weather-v1"); + const freshWeather = imageInstall("weather", "weather-v2"); + const userPluginPath = `${OPENCLAW_DIR}/extensions/user-plugin`; + const result = runRestoreScenario({ + previousPluginInstalls: [previousWeather], + freshPluginInstalls: [freshWeather], + backupExtensionDirs: ["weather-v1"], + backupConfig: { + gateway: { auth: { token: "stale-token" } }, + channels: { + weather: { enabled: false, token: "stale-image-token" }, + "user-channel": { room: "keep" }, + }, + plugins: { + entries: { + weather: { enabled: false, config: { revision: "stale" } }, + "user-plugin": { enabled: true }, + }, + installs: { weather: { installPath: previousWeather.installPath } }, + load: { paths: [previousWeather.installPath, userPluginPath] }, + slots: { memory: "weather", contextEngine: "user-plugin" }, + }, + }, + freshConfig: { + gateway: { auth: { token: "fresh-token" } }, + channels: { weather: { enabled: true, endpoint: "fresh" } }, + plugins: { + entries: { weather: { enabled: true, config: { revision: "fresh" } } }, + load: { paths: freshWeather.loadPaths }, + slots: { memory: "weather" }, + }, + }, + }); + + expectSuccessfulRestore(result); + expect(result.freshMarkers).toEqual({ + nemoclaw: "fresh-nemoclaw\n", + "weather-v2": "fresh-weather-v2\n", + }); + expect(result.restoredConfig.gateway.auth.token).toBe("fresh-token"); + expect(result.restoredConfig.channels).toEqual({ + weather: { enabled: true, endpoint: "fresh" }, + "user-channel": { room: "keep" }, + }); + expect(result.restoredConfig.plugins.entries).toEqual({ + "user-plugin": { enabled: true }, + weather: { enabled: true, config: { revision: "fresh" } }, + }); + expect(result.restoredConfig.plugins.load.paths).toEqual([ + freshWeather.installPath, + userPluginPath, + ]); + expect(result.restoredConfig.plugins.slots).toEqual({ + contextEngine: "user-plugin", + memory: "weather", + }); + expect(result.restoredConfig.plugins.installs).toBeUndefined(); + }); +}); diff --git a/src/lib/state/sandbox.ts b/src/lib/state/sandbox.ts index 7d1c6599edf..53f732011cb 100644 --- a/src/lib/state/sandbox.ts +++ b/src/lib/state/sandbox.ts @@ -1549,6 +1549,15 @@ function restoreSandboxStateInternal( freshOpenClawImagePluginInstalls !== undefined ? manifest.openclawImagePluginInstalls : undefined; + // Fresh provenance is still authoritative for preserving image-managed + // extension directories during recreation. Config reconciliation, however, + // needs a complete before/after pair. Legacy and stock-image backups do not + // carry the previous baseline, so preserve their historical config-merge + // behavior by passing neither side of the pair to openclaw.json restore. + const configFreshOpenClawImagePluginInstalls = + previousOpenClawImagePluginInstalls !== undefined + ? freshOpenClawImagePluginInstalls + : undefined; try { const pluginRestorePlan = planOpenClawPluginRestore({ agentType: manifest.agentType, @@ -1710,7 +1719,7 @@ function restoreSandboxStateInternal( backupPath, shouldMergeOpenClawConfigStateFile(manifest.agentType, dir, spec), options.stateFileRestorePolicy, - freshOpenClawImagePluginInstalls, + configFreshOpenClawImagePluginInstalls, previousOpenClawImagePluginInstalls, ) ) { diff --git a/test/e2e/live/rebuild-openclaw.test.ts b/test/e2e/live/rebuild-openclaw.test.ts index a8ede0ae33a..bb9933c8e76 100644 --- a/test/e2e/live/rebuild-openclaw.test.ts +++ b/test/e2e/live/rebuild-openclaw.test.ts @@ -693,6 +693,9 @@ print(json.dumps({'seeded': saved == os.environ['PRE_REBUILD_GATEWAY_TOKEN'], 'h }, ); expectExitZero(rebuild, "nemoclaw rebuild"); + const rebuildText = resultText(rebuild); + expect(rebuildText).toContain(`Sandbox '${SANDBOX_NAME}' rebuilt successfully`); + expect(rebuildText).not.toContain("post-restore steps were incomplete"); // Phase 7: state preservation, upgrade, token rotation, backup hygiene, and // policy-preset preservation assertions. From b8b84fc4eb17f649bd58f8d133df1289e22fbfc3 Mon Sep 17 00:00:00 2001 From: Charan Jagwani Date: Wed, 8 Jul 2026 19:49:48 -0700 Subject: [PATCH 2/3] test(e2e): make device auth fixture sandbox reachable --- test/e2e/live/device-auth-health.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/e2e/live/device-auth-health.test.ts b/test/e2e/live/device-auth-health.test.ts index 09fa48cce12..6b06c938580 100644 --- a/test/e2e/live/device-auth-health.test.ts +++ b/test/e2e/live/device-auth-health.test.ts @@ -40,9 +40,13 @@ test("device auth health probes treat 401 as live instead of offline (#2342)", { timeout: LIVE_TIMEOUT_MS, }, async ({ artifacts, cleanup, host, sandbox, skip }) => { const installLog = artifacts.pathFor("phase-1-install-device-auth-health.log"); + // The sandbox cannot reach runner loopback, so expose the fixture through + // OpenShell's host bridge while keeping readiness checks local to the runner. const inference = await startFakeOpenAiCompatibleServer({ apiKey: INFERENCE_API_KEY, + host: "0.0.0.0", model: INFERENCE_MODEL, + publicHost: "host.openshell.internal", requireAuth: true, }); cleanup.add("close device-auth compatible inference fixture", async () => { From c2fbd61d1b08f2e45e40f253e85943a287b426ff Mon Sep 17 00:00:00 2001 From: Charan Jagwani Date: Wed, 8 Jul 2026 19:54:16 -0700 Subject: [PATCH 3/3] test(rebuild): keep restore fixture setup linear --- .../state/sandbox-recreated-openclaw-restore.test.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib/state/sandbox-recreated-openclaw-restore.test.ts b/src/lib/state/sandbox-recreated-openclaw-restore.test.ts index ba1c031d944..6fd08d0f1f5 100644 --- a/src/lib/state/sandbox-recreated-openclaw-restore.test.ts +++ b/src/lib/state/sandbox-recreated-openclaw-restore.test.ts @@ -7,6 +7,7 @@ import path from "node:path"; import { describe, expect, it } from "vitest"; +import { restoreEnvBulk } from "../../../test/helpers/env-test-helpers.js"; import type { OpenClawImagePluginInstall } from "./openclaw-plugin-restore.js"; import { restoreRecreatedSandboxState } from "./sandbox.js"; @@ -90,10 +91,10 @@ function runRestoreScenario(options: { dir: OPENCLAW_DIR, backupPath, blueprintDigest: null, + ...(options.previousPluginInstalls !== undefined + ? { openclawImagePluginInstalls: options.previousPluginInstalls } + : {}), }; - if (options.previousPluginInstalls !== undefined) { - manifest.openclawImagePluginInstalls = options.previousPluginInstalls; - } fs.writeFileSync(path.join(backupPath, "rebuild-manifest.json"), JSON.stringify(manifest)); const openshell = path.join(binDir, "openshell"); @@ -183,10 +184,7 @@ process.exit(1); ), }; } finally { - if (previousOpenshellBin === undefined) delete process.env.NEMOCLAW_OPENSHELL_BIN; - else process.env.NEMOCLAW_OPENSHELL_BIN = previousOpenshellBin; - if (previousPath === undefined) delete process.env.PATH; - else process.env.PATH = previousPath; + restoreEnvBulk({ NEMOCLAW_OPENSHELL_BIN: previousOpenshellBin, PATH: previousPath }); fs.rmSync(fixture, { recursive: true, force: true }); } }