-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(portable): reconcile timed-out stop state #9339
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
328 changes: 328 additions & 0 deletions
328
src/lib/onboard/experimental/portable-demo-lifecycle-stop.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,328 @@ | ||
| // 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 { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { PodmanSocketAuthorityDeps } from "../../adapters/podman"; | ||
| import type { CheckpointPortableRuntimeAuthority } from "../../state/onboard-checkpoint-types"; | ||
| import { | ||
| installPortableDemoSandboxLifecycle, | ||
| type PortableDemoLifecycleDeps, | ||
| portableDemoLifecycleInternals, | ||
| type PortablePodmanLifecycleCommandResult, | ||
| stopPortableDemoSandboxLifecycle, | ||
| } from "./portable-demo-lifecycle"; | ||
|
|
||
| const CONTAINER_ID = "a".repeat(64); | ||
| const SANDBOX_ID = "sandbox-id-alpha"; | ||
| const SOCKET_PATH = "/run/user/1001/podman/podman.sock"; | ||
| const RUNTIME_AUTHORITY: CheckpointPortableRuntimeAuthority = { | ||
| schemaVersion: 1, | ||
| kind: "podman", | ||
| ownership: "current-user", | ||
| uid: 1001, | ||
| homeDir: "/home/tester", | ||
| configHome: "/home/tester/.config", | ||
| runtimeDir: "/run/user/1001", | ||
| socketPath: SOCKET_PATH, | ||
| }; | ||
| const STARTUP_ARGV = [ | ||
| "env", | ||
| "CHAT_UI_URL=http://127.0.0.1:18789", | ||
| "NEMOCLAW_DASHBOARD_PORT=18789", | ||
| "OPENCLAW_HOME=/sandbox", | ||
| "OPENCLAW_STATE_DIR=/sandbox/.openclaw", | ||
| "OPENCLAW_WORKSPACE_DIR=/sandbox/.openclaw/workspace", | ||
| "NEMOCLAW_SANDBOX_NAME=alpha", | ||
| "/usr/local/bin/nemoclaw-start", | ||
| ]; | ||
|
|
||
| const temporaryDirectories: string[] = []; | ||
|
|
||
| function socketAuthorityDeps(): PodmanSocketAuthorityDeps { | ||
| const directoryInodes = new Map<string, bigint>(); | ||
| return { | ||
| uid: 1001, | ||
| lstat: (filePath) => { | ||
| const socket = filePath === SOCKET_PATH; | ||
| const directoryInode = directoryInodes.get(filePath) ?? BigInt(7000 + directoryInodes.size); | ||
| directoryInodes.set(filePath, directoryInode); | ||
| return { | ||
| dev: 8n, | ||
| ino: socket ? 9001n : directoryInode, | ||
| mode: socket ? 0o660n : filePath === path.dirname(SOCKET_PATH) ? 0o700n : 0o755n, | ||
| uid: socket ? 1001n : filePath.startsWith("/run/user/1001") ? 1001n : 0n, | ||
| isDirectory: () => !socket, | ||
| isSocket: () => socket, | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function createPodman() { | ||
| let running = true; | ||
| let containerId = CONTAINER_ID; | ||
| const podman = vi.fn( | ||
| (args: readonly string[], _env?: NodeJS.ProcessEnv): PortablePodmanLifecycleCommandResult => { | ||
| const command = args[0] === "--url" ? args.slice(2) : args; | ||
| switch (command[0]) { | ||
| case "version": | ||
| return { status: 0, stdout: JSON.stringify({ Server: { Version: "5.6.1" } }) }; | ||
| case "ps": | ||
| return { status: 0, stdout: `${CONTAINER_ID}\n` }; | ||
| case "inspect": | ||
| return { | ||
| status: 0, | ||
| stdout: JSON.stringify([ | ||
| { | ||
| Id: containerId, | ||
| Name: `openshell-default--alpha-${SANDBOX_ID}`, | ||
| Config: { | ||
| Labels: { | ||
| "openshell.managed": "true", | ||
| "openshell.ai/sandbox-id": SANDBOX_ID, | ||
| "openshell.ai/sandbox-name": "alpha", | ||
| "openshell.ai/sandbox-namespace": "", | ||
| "openshell.ai/sandbox-workspace": "default", | ||
| }, | ||
| }, | ||
| State: { Running: running }, | ||
| }, | ||
| ]), | ||
| }; | ||
| case "stop": | ||
| running = false; | ||
| return { status: 0 }; | ||
| case "update": | ||
| return { status: 0 }; | ||
| default: | ||
| throw new Error(`Unexpected Podman command: ${args.join(" ")}`); | ||
| } | ||
| }, | ||
| ); | ||
| return { | ||
| podman, | ||
| setContainerId(value: string) { | ||
| containerId = value; | ||
| }, | ||
| setRunning(value: boolean) { | ||
| running = value; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function lifecycleDeps( | ||
| stateDir: string, | ||
| podman: ReturnType<typeof createPodman>["podman"], | ||
| overrides: Partial<PortableDemoLifecycleDeps> = {}, | ||
| ): PortableDemoLifecycleDeps { | ||
| return { | ||
| platform: "linux", | ||
| podman, | ||
| podmanSocketAuthorityDeps: socketAuthorityDeps(), | ||
| stateDir, | ||
| hardenSocketDirectory: vi.fn(), | ||
| runtimeReadiness: { | ||
| uid: 1001, | ||
| home: RUNTIME_AUTHORITY.homeDir, | ||
| systemctl: () => ({ status: 0 }), | ||
| podmanCapture: () => ({ | ||
| status: 0, | ||
| stdout: JSON.stringify({ Server: { Version: "5.6.1" } }), | ||
| stderr: "", | ||
| }), | ||
| }, | ||
| log: vi.fn(), | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function createStopHarness() { | ||
| const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-portable-stop-")); | ||
| temporaryDirectories.push(stateDir); | ||
| const runtime = createPodman(); | ||
| installPortableDemoSandboxLifecycle( | ||
| "alpha", | ||
| STARTUP_ARGV, | ||
| { HOME: stateDir, NEMOCLAW_EXPERIMENTAL_PROFILE: "portable" }, | ||
| { | ||
| ...lifecycleDeps(stateDir, runtime.podman), | ||
| runtimeAuthority: RUNTIME_AUTHORITY, | ||
| }, | ||
| ); | ||
| const receiptPath = portableDemoLifecycleInternals.receiptPath("alpha", stateDir); | ||
| const receiptBefore = fs.readFileSync(receiptPath, "utf8"); | ||
| const originalPodman = runtime.podman.getMockImplementation()!; | ||
| runtime.podman.mockClear(); | ||
| return { originalPodman, receiptBefore, receiptPath, runtime, stateDir }; | ||
| } | ||
|
|
||
| function stopSandbox( | ||
| harness: ReturnType<typeof createStopHarness>, | ||
| overrides: Partial<PortableDemoLifecycleDeps> = {}, | ||
| beforeStop = vi.fn(), | ||
| ) { | ||
| return stopPortableDemoSandboxLifecycle( | ||
| "alpha", | ||
| { | ||
| agent: "openclaw", | ||
| gatewayName: "nemoclaw", | ||
| lifecycleGeneration: CONTAINER_ID, | ||
| openshellDriver: "docker", | ||
| }, | ||
| beforeStop, | ||
| lifecycleDeps(harness.stateDir, harness.runtime.podman, overrides), | ||
| ); | ||
| } | ||
|
|
||
| function timedOutStop( | ||
| harness: ReturnType<typeof createStopHarness>, | ||
| afterStop?: (command: readonly string[]) => PortablePodmanLifecycleCommandResult | undefined, | ||
| ) { | ||
| const timeout = Object.assign(new Error("spawnSync podman ETIMEDOUT"), { | ||
| code: "ETIMEDOUT", | ||
| }); | ||
| let stopAttempted = false; | ||
| harness.runtime.podman.mockImplementation((args, env) => { | ||
| const command = args[0] === "--url" ? args.slice(2) : args; | ||
| switch (command[0]) { | ||
| case "stop": | ||
| stopAttempted = true; | ||
| return { status: null, error: timeout }; | ||
| default: { | ||
| const result = stopAttempted ? afterStop?.(command) : undefined; | ||
| return result ?? harness.originalPodman(args, env); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function stopAfterSecondInspection(harness: ReturnType<typeof createStopHarness>) { | ||
| let inspectionsAfterStop = 0; | ||
| return (command: readonly string[]): undefined => { | ||
| switch (command[0]) { | ||
| case "inspect": | ||
| inspectionsAfterStop += 1; | ||
| switch (inspectionsAfterStop) { | ||
| case 2: | ||
| harness.runtime.setRunning(false); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function replaceContainerOnInspection(harness: ReturnType<typeof createStopHarness>) { | ||
| return (command: readonly string[]): undefined => { | ||
| switch (command[0]) { | ||
| case "inspect": | ||
| harness.runtime.setContainerId("b".repeat(64)); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function expectOnlyExactStopAndInspects(harness: ReturnType<typeof createStopHarness>): void { | ||
| const commands = harness.runtime.podman.mock.calls.map(([args]) => | ||
| args[0] === "--url" ? args.slice(2) : args, | ||
| ); | ||
| expect(commands.filter(([command]) => command === "stop")).toEqual([["stop", CONTAINER_ID]]); | ||
| expect(commands.every(([command]) => command === "inspect" || command === "stop")).toBe(true); | ||
| } | ||
|
|
||
| function expectReceiptUnchanged(harness: ReturnType<typeof createStopHarness>): void { | ||
| expect(fs.readFileSync(harness.receiptPath, "utf8")).toBe(harness.receiptBefore); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| for (const directory of temporaryDirectories.splice(0)) { | ||
| fs.rmSync(directory, { force: true, recursive: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe("portable demo sandbox stop reconciliation", () => { | ||
| it("reconciles an ETIMEDOUT stop to the exact receipt-owned container state (#9200)", () => { | ||
| const harness = createStopHarness(); | ||
| let now = 0; | ||
| timedOutStop(harness, stopAfterSecondInspection(harness)); | ||
| const beforeStop = vi.fn(); | ||
|
|
||
| expect( | ||
| stopSandbox( | ||
| harness, | ||
| { | ||
| now: () => now, | ||
| sleep: (milliseconds) => { | ||
| now += milliseconds; | ||
| }, | ||
| }, | ||
| beforeStop, | ||
| ), | ||
| ).toEqual({ kind: "stopped" }); | ||
|
|
||
| expect(beforeStop).toHaveBeenCalledExactlyOnceWith(); | ||
| expect(now).toBe(1_000); | ||
| expectReceiptUnchanged(harness); | ||
| expectOnlyExactStopAndInspects(harness); | ||
| }); | ||
|
|
||
| it("fails after bounded reconciliation when an ETIMEDOUT container remains running (#9200)", () => { | ||
| const harness = createStopHarness(); | ||
| let now = 0; | ||
| timedOutStop(harness); | ||
|
|
||
| expect(() => | ||
| stopSandbox(harness, { | ||
| now: () => now, | ||
| sleep: (milliseconds) => { | ||
| now += milliseconds; | ||
| }, | ||
| }), | ||
| ).toThrow("ETIMEDOUT"); | ||
|
|
||
| expect(now).toBe(30_000); | ||
| expectReceiptUnchanged(harness); | ||
| expectOnlyExactStopAndInspects(harness); | ||
| }); | ||
|
|
||
| it("rejects container identity drift while reconciling an ETIMEDOUT stop (#9200)", () => { | ||
| const harness = createStopHarness(); | ||
| timedOutStop(harness, replaceContainerOnInspection(harness)); | ||
|
|
||
| expect(() => stopSandbox(harness, { now: () => 0, sleep: vi.fn() })).toThrow( | ||
| "OpenShell identity does not match sandbox 'alpha'", | ||
| ); | ||
|
|
||
| expectReceiptUnchanged(harness); | ||
| expectOnlyExactStopAndInspects(harness); | ||
| }); | ||
|
|
||
| it("rejects a missing receipt-owned container while reconciling an ETIMEDOUT stop (#9200)", () => { | ||
| const harness = createStopHarness(); | ||
| timedOutStop(harness, (command) => | ||
| command[0] === "inspect" ? { status: 125, stderr: "Error: no such container" } : undefined, | ||
| ); | ||
|
|
||
| expect(() => stopSandbox(harness, { now: () => 0, sleep: vi.fn() })).toThrow( | ||
| "no longer has its recorded Podman container", | ||
| ); | ||
|
|
||
| expectReceiptUnchanged(harness); | ||
| expectOnlyExactStopAndInspects(harness); | ||
| }); | ||
|
|
||
| it("rejects an inspection failure while reconciling an ETIMEDOUT stop (#9200)", () => { | ||
| const harness = createStopHarness(); | ||
| timedOutStop(harness, (command) => | ||
| command[0] === "inspect" ? { status: 125, stderr: "permission denied" } : undefined, | ||
| ); | ||
|
|
||
| expect(() => stopSandbox(harness, { now: () => 0, sleep: vi.fn() })).toThrow( | ||
| "Inspecting portable sandbox 'alpha' failed: exit 125", | ||
| ); | ||
|
|
||
| expectReceiptUnchanged(harness); | ||
| expectOnlyExactStopAndInspects(harness); | ||
| }); | ||
| }); | ||
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.
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.