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
111 changes: 95 additions & 16 deletions src/lib/actions/upgrade-sandboxes-recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ import { upgradeSandboxes, upgradeSandboxesDependencies } from "./upgrade-sandbo

type UpgradeSandboxes = typeof upgradeSandboxes;

function makeManifest(sandboxName: string) {
type ManifestAgentType = "openclaw" | "hermes";

const MANIFEST_DIR_BY_AGENT: Record<ManifestAgentType, string> = {
openclaw: "/sandbox/.openclaw",
hermes: "/sandbox/.hermes",
};

function makeManifest(sandboxName: string, agentType: ManifestAgentType = "openclaw") {
const timestamp = `2026-07-01T06-50-4${sandboxName.length}-044Z`;
return {
version: 1,
sandboxName,
timestamp,
agentType: "openclaw",
agentType,
agentVersion: "2026.5.27",
expectedVersion: "2026.5.27",
stateDirs: ["workspace"],
backedUpDirs: ["workspace"],
stateFiles: [],
dir: "/sandbox/.openclaw",
dir: MANIFEST_DIR_BY_AGENT[agentType],
backupPath: `/tmp/rebuild-backups/${sandboxName}/${timestamp}`,
blueprintDigest: null,
policyPresets: [],
Expand All @@ -40,16 +47,19 @@ function createRecoveryHarness(
gatewayPort?: number;
liveOutput?: string;
latestBackup?: ReturnType<typeof makeManifest> | null;
registryOverrides?: Record<
string,
Partial<{
agent: "openclaw" | "hermes" | "langchain-deepagents-code" | null;
agentVersion: string | null;
createdAt: string;
nemoclawVersion: string | null;
fromDockerfile: string | null;
pendingRouteReservation: true;
}>
manifestAgentTypes?: Partial<Record<string, ManifestAgentType>>;
registryOverrides?: Partial<
Record<
string,
Partial<{
agent: "openclaw" | "hermes" | "langchain-deepagents-code" | null;
agentVersion: string | null;
createdAt: string;
nemoclawVersion: string | null;
fromDockerfile: string | null;
pendingRouteReservation: true;
}>
>
>;
confirmedLegacyManagedNames?: string[] | string;
staleNames?: string[];
Expand Down Expand Up @@ -107,9 +117,12 @@ function createRecoveryHarness(
});
const latestBackupSpy = vi
.spyOn(sandboxState, "getLatestBackup")
.mockImplementation((...args: unknown[]) =>
options.latestBackup === undefined ? makeManifest(String(args[0])) : options.latestBackup,
);
.mockImplementation((...args: unknown[]) => {
const sandboxName = String(args[0]);
return options.latestBackup === undefined
? makeManifest(sandboxName, options.manifestAgentTypes?.[sandboxName])
: options.latestBackup;
});
vi.spyOn(sandboxState, "validateRebuildRecoveryManifest").mockImplementation(
(...args: unknown[]) => ({
ok: true as const,
Expand Down Expand Up @@ -190,6 +203,72 @@ describe("upgrade-sandboxes prepared backup recovery (#6114)", () => {
}
});

it.each([
{
mode: "automatic",
options: { auto: true },
expectedRebuilds: 2,
expectedSequence: [
"warning:/sandbox/.openclaw",
"warning:/sandbox/.hermes",
"rebuild:openclaw-box",
"rebuild:hermes-box",
],
},
{
mode: "check-only",
options: { check: true },
expectedRebuilds: 0,
expectedSequence: ["warning:/sandbox/.openclaw", "warning:/sandbox/.hermes"],
},
] as const)("warns with each agent's restore path before $mode mixed recovery (#7073)", async ({
options,
expectedRebuilds,
expectedSequence,
}) => {
const sequence: string[] = [];
const statePaths = ["/sandbox/.openclaw", "/sandbox/.hermes"];
const harness = createRecoveryHarness(["openclaw-box", "hermes-box"], {
manifestAgentTypes: { "openclaw-box": "openclaw", "hermes-box": "hermes" },
registryOverrides: {
"openclaw-box": { agent: "openclaw" },
"hermes-box": { agent: "hermes" },
},
});
vi.mocked(console.log).mockImplementation((...args) => {
const message = String(args[0]);
sequence.push(
...statePaths
.filter((candidate) =>
message.includes(`Recovery restores ${JSON.stringify(candidate)} state only`),
)
.map((statePath) => `warning:${statePath}`),
);
});
harness.rebuildSpy.mockImplementation(async (name: string) => {
sequence.push(`rebuild:${name}`);
});

await expect(harness.upgradeSandboxes(options)).resolves.toBeUndefined();

for (const statePath of statePaths) {
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(
`Recovery restores ${JSON.stringify(statePath)} state only for this sandbox`,
),
);
}
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining("Files outside this recorded managed state path"),
);
expect(console.log).toHaveBeenCalledWith(expect.stringContaining("/sandbox/user-data"));
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining("NOT preserved by the recreate"),
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
expect(harness.rebuildSpy).toHaveBeenCalledTimes(expectedRebuilds);
expect(sequence).toEqual(expectedSequence);
});

it("continues through all eligible sandboxes before reporting a recovery failure", async () => {
const harness = createRecoveryHarness(["alpha", "beta"]);
harness.rebuildSpy.mockRejectedValueOnce(new Error("alpha failed"));
Expand Down
7 changes: 7 additions & 0 deletions src/lib/actions/upgrade-sandboxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ export async function upgradeSandboxes(
console.log(
` ${recovery.sandbox.name} ${D}${recovery.manifest.timestamp}${R} (non-Ready)`,
);
// #7073: the validated manifest records the agent-specific managed state
// root restored for this sandbox. Warn before the destructive recreate so
// users can back up paths outside that exact root rather than silently
// losing them.
console.log(
` ${YW}⚠ Recovery restores ${JSON.stringify(recovery.manifest.dir)} state only for this sandbox. Files outside this recorded managed state path (e.g. /sandbox/user-data) are NOT preserved by the recreate — back them up before upgrading.${R}`,
);
}
}
if (rejectedRecoveries.length > 0) {
Expand Down