-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(sandbox): remove the incomplete snapshot when creation fails #8211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f234cae
fix(sandbox): remove the incomplete snapshot when creation fails
laitingsheng 8c8110f
Merge branch 'main' into fix/snapshot-create-failed-cleanup
cv 6cbbf52
Merge branch 'main' into fix/snapshot-create-failed-cleanup
apurvvkumaria 4454266
fix(sandbox): drop the incomplete-snapshot retention surface
laitingsheng b2d81fb
Merge remote-tracking branch 'origin/main' into fix/snapshot-create-f…
laitingsheng e2647fa
docs(snapshot): clarify failed cleanup recovery
apurvvkumaria c8928aa
Merge branch 'main' into fix/snapshot-create-failed-cleanup
apurvvkumaria b98f271
merge(main): refresh branch for package fixture fix
apurvvkumaria 5692bf9
merge(main): include CLI timing manifest fix
apurvvkumaria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
src/lib/actions/sandbox/snapshot-failed-create-cleanup.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| backupSandboxState: vi.fn(), | ||
| captureOpenshell: vi.fn(() => ({ status: 0, output: "alpha Ready\n" })), | ||
| findBackup: vi.fn(() => ({ match: null })), | ||
| removeIncompleteSnapshot: vi.fn( | ||
| () => | ||
| ({ removed: true }) as { | ||
| removed: boolean; | ||
| error?: string; | ||
| }, | ||
| ), | ||
| })); | ||
|
|
||
| vi.mock("../../adapters/openshell/runtime", () => ({ | ||
| captureOpenshell: mocks.captureOpenshell, | ||
| getOpenshellBinary: vi.fn(() => "openshell"), | ||
| runOpenshell: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("../../runtime-recovery", () => ({ | ||
| parseLiveSandboxNames: vi.fn(() => new Set(["alpha"])), | ||
| })); | ||
|
|
||
| vi.mock("../../shields", () => ({ | ||
| isShieldsDown: vi.fn(() => true), | ||
| })); | ||
|
|
||
| vi.mock("../../shields/timer-bound-lock", () => ({ | ||
| withTimerBoundShieldsMutationLock: vi.fn( | ||
| (_sandboxName: string, _command: string, operation: () => unknown) => operation(), | ||
| ), | ||
| })); | ||
|
|
||
| vi.mock("../../state/registry", () => ({ | ||
| getBaselineExclusions: vi.fn(() => []), | ||
| getSandbox: vi.fn(() => ({ name: "alpha", agent: "openclaw" })), | ||
| })); | ||
|
|
||
| vi.mock("../../state/sandbox", () => ({ | ||
| backupSandboxState: mocks.backupSandboxState, | ||
| findBackup: mocks.findBackup, | ||
| removeIncompleteSnapshot: mocks.removeIncompleteSnapshot, | ||
| })); | ||
|
|
||
| vi.mock("./sandbox-gateway-routing", () => ({ | ||
| probeGatewayRunning: vi.fn(() => true), | ||
| selectSandboxGatewayIfRegistered: vi.fn(() => true), | ||
| usesGatewayMetadataProbe: vi.fn(() => false), | ||
| })); | ||
|
|
||
| const INCOMPLETE_PATH = "/home/user/.nemoclaw/rebuild-backups/alpha/2026-08-04T06-53-38-310Z"; | ||
|
|
||
| function failedCaptureWithPublishedSnapshot(overrides: Record<string, unknown> = {}) { | ||
| return { | ||
| success: false, | ||
| manifest: { backupPath: INCOMPLETE_PATH }, | ||
| backedUpDirs: ["workspace"], | ||
| failedDirs: [], | ||
| backedUpFiles: [], | ||
| failedFiles: ["openclaw.json"], | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("snapshot create cleanup after a failed capture", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mocks.removeIncompleteSnapshot.mockReturnValue({ removed: true }); | ||
| mocks.findBackup.mockReturnValue({ match: null }); | ||
| vi.spyOn(console, "log").mockImplementation(() => {}); | ||
| vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
laitingsheng marked this conversation as resolved.
|
||
|
|
||
| async function createSnapshot(request: Record<string, unknown> = {}): Promise<string> { | ||
| const { runSandboxSnapshot } = await import("./snapshot"); | ||
| await expect(runSandboxSnapshot("alpha", { kind: "create", ...request })).rejects.toMatchObject( | ||
| { exitCode: 1 }, | ||
| ); | ||
| return vi.mocked(console.error).mock.calls.flat().join("\n"); | ||
| } | ||
|
|
||
| it("reports the failed directories and files", async () => { | ||
| mocks.backupSandboxState.mockReturnValue( | ||
| failedCaptureWithPublishedSnapshot({ | ||
| failedDirs: ["workspace", "skills"], | ||
| failedDirReasons: { workspace: "permission denied" }, | ||
| }), | ||
| ); | ||
|
|
||
| const errors = await createSnapshot(); | ||
|
|
||
| expect(errors).toContain("Snapshot failed."); | ||
| expect(errors).toContain("Failed directories: workspace (permission denied), skills"); | ||
| expect(errors).toContain("Failed files: openclaw.json"); | ||
| }); | ||
|
|
||
| it("removes the snapshot so a later restore cannot select an incomplete capture (#8201)", async () => { | ||
| mocks.backupSandboxState.mockReturnValue(failedCaptureWithPublishedSnapshot()); | ||
|
|
||
| const errors = await createSnapshot(); | ||
|
|
||
| expect(mocks.removeIncompleteSnapshot).toHaveBeenCalledWith(INCOMPLETE_PATH); | ||
| expect(errors).toContain("Removed the incomplete snapshot."); | ||
| }); | ||
|
|
||
| it("keeps the snapshot when --keep-failed is passed", async () => { | ||
| mocks.backupSandboxState.mockReturnValue(failedCaptureWithPublishedSnapshot()); | ||
|
|
||
| const errors = await createSnapshot({ keepFailed: true }); | ||
|
|
||
| expect(mocks.removeIncompleteSnapshot).not.toHaveBeenCalled(); | ||
| expect(errors).toContain(`Kept the incomplete snapshot at ${INCOMPLETE_PATH}.`); | ||
| expect(errors).toContain("can be restored even though its capture did not complete"); | ||
| }); | ||
|
|
||
| it("keeps the snapshot when NEMOCLAW_KEEP_FAILED_SNAPSHOT is exactly 1", async () => { | ||
| vi.stubEnv("NEMOCLAW_KEEP_FAILED_SNAPSHOT", "1"); | ||
| mocks.backupSandboxState.mockReturnValue(failedCaptureWithPublishedSnapshot()); | ||
|
|
||
| await createSnapshot(); | ||
|
|
||
| expect(mocks.removeIncompleteSnapshot).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| "true", | ||
| "yes", | ||
| "0", | ||
| "", | ||
| ])("removes the snapshot when NEMOCLAW_KEEP_FAILED_SNAPSHOT is %j", async (value) => { | ||
| vi.stubEnv("NEMOCLAW_KEEP_FAILED_SNAPSHOT", value); | ||
| mocks.backupSandboxState.mockReturnValue(failedCaptureWithPublishedSnapshot()); | ||
|
|
||
| await createSnapshot(); | ||
|
|
||
| expect(mocks.removeIncompleteSnapshot).toHaveBeenCalledWith(INCOMPLETE_PATH); | ||
| }); | ||
|
|
||
| it("names the snapshot that is still listed when removal fails", async () => { | ||
| mocks.backupSandboxState.mockReturnValue(failedCaptureWithPublishedSnapshot()); | ||
| mocks.removeIncompleteSnapshot.mockReturnValue({ | ||
| removed: false, | ||
| error: "EACCES: permission denied", | ||
| }); | ||
|
|
||
| const errors = await createSnapshot(); | ||
|
|
||
| expect(errors).toContain( | ||
| `The incomplete snapshot at '${INCOMPLETE_PATH}' could not be removed: EACCES: permission denied`, | ||
| ); | ||
| expect(errors).toContain("Remove it before the next restore."); | ||
| }); | ||
|
|
||
| it("does not attempt removal when the capture failed before publishing a snapshot", async () => { | ||
| mocks.backupSandboxState.mockReturnValue({ | ||
| success: false, | ||
| backedUpDirs: [], | ||
| failedDirs: [], | ||
| backedUpFiles: [], | ||
| failedFiles: [], | ||
| error: "Snapshot name 'dup' already exists.", | ||
| }); | ||
|
|
||
| const errors = await createSnapshot(); | ||
|
|
||
| expect(mocks.removeIncompleteSnapshot).not.toHaveBeenCalled(); | ||
| expect(errors).toContain("Snapshot name 'dup' already exists."); | ||
| }); | ||
|
|
||
| it("does not touch a snapshot whose capture succeeded", async () => { | ||
| mocks.backupSandboxState.mockReturnValue({ | ||
| success: true, | ||
| manifest: { backupPath: INCOMPLETE_PATH, timestamp: "2026-08-04T06-53-38-310Z" }, | ||
| backedUpDirs: ["workspace"], | ||
| failedDirs: [], | ||
| backedUpFiles: ["openclaw.json"], | ||
| failedFiles: [], | ||
| }); | ||
| const { runSandboxSnapshot } = await import("./snapshot"); | ||
|
|
||
| await runSandboxSnapshot("alpha", { kind: "create", keepFailed: true }); | ||
|
|
||
| expect(mocks.removeIncompleteSnapshot).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.