|
| 1 | +// Covers the remote-mode seed obligation across a gateway restart: adopting an |
| 2 | +// existing sandbox must probe the managed roots instead of trusting process |
| 3 | +// memory, and must never re-seed roots that already hold content. |
| 4 | +import fs from "node:fs/promises"; |
| 5 | +import path from "node:path"; |
| 6 | +import type { CreateSandboxBackendParams } from "openclaw/plugin-sdk/sandbox"; |
| 7 | +import { |
| 8 | + resolvePreferredOpenClawTmpDir, |
| 9 | + tempWorkspace, |
| 10 | + type TempWorkspace, |
| 11 | +} from "openclaw/plugin-sdk/temp-path"; |
| 12 | +import { |
| 13 | + createSandboxBrowserConfig, |
| 14 | + createSandboxPruneConfig, |
| 15 | + createSandboxSshConfig, |
| 16 | +} from "openclaw/plugin-sdk/test-fixtures"; |
| 17 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 18 | +import { createOpenShellSandboxBackendFactory } from "./backend.js"; |
| 19 | +import { resolveOpenShellPluginConfig } from "./config.js"; |
| 20 | + |
| 21 | +const sdkMocks = vi.hoisted(() => ({ |
| 22 | + runSshSandboxCommand: vi.fn(), |
| 23 | + disposeSshSandboxSession: vi.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +const cliMocks = vi.hoisted(() => ({ |
| 27 | + runOpenShellCli: vi.fn(), |
| 28 | + createOpenShellSshSession: vi.fn(), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("openclaw/plugin-sdk/sandbox", async (importOriginal) => { |
| 32 | + const actual = await importOriginal<typeof import("openclaw/plugin-sdk/sandbox")>(); |
| 33 | + return { |
| 34 | + ...actual, |
| 35 | + runSshSandboxCommand: sdkMocks.runSshSandboxCommand, |
| 36 | + disposeSshSandboxSession: sdkMocks.disposeSshSandboxSession, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +vi.mock("./cli.js", async (importOriginal) => { |
| 41 | + const actual = await importOriginal<typeof import("./cli.js")>(); |
| 42 | + return { |
| 43 | + ...actual, |
| 44 | + runOpenShellCli: cliMocks.runOpenShellCli, |
| 45 | + createOpenShellSshSession: cliMocks.createOpenShellSshSession, |
| 46 | + }; |
| 47 | +}); |
| 48 | + |
| 49 | +const tempWorkspaces: TempWorkspace[] = []; |
| 50 | + |
| 51 | +function createOpenShellBackendSandboxConfig(): CreateSandboxBackendParams["cfg"] { |
| 52 | + return { |
| 53 | + mode: "all", |
| 54 | + backend: "openshell", |
| 55 | + scope: "session", |
| 56 | + workspaceAccess: "rw", |
| 57 | + workspaceRoot: "/tmp/openclaw-sandboxes", |
| 58 | + dockerTmpfsSource: "configured", |
| 59 | + docker: { |
| 60 | + image: "openclaw-sandbox:bookworm-slim", |
| 61 | + containerPrefix: "openclaw-sbx-", |
| 62 | + workdir: "/workspace", |
| 63 | + readOnlyRoot: false, |
| 64 | + tmpfs: [], |
| 65 | + network: "none", |
| 66 | + capDrop: [], |
| 67 | + binds: [], |
| 68 | + env: {}, |
| 69 | + }, |
| 70 | + ssh: createSandboxSshConfig("/tmp/openclaw-sandboxes"), |
| 71 | + browser: createSandboxBrowserConfig(), |
| 72 | + tools: { allow: ["*"], deny: [] }, |
| 73 | + prune: createSandboxPruneConfig(), |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +async function createAdoptedRemoteBackend(params: { probeStdout: string }) { |
| 78 | + const workspace = await tempWorkspace({ |
| 79 | + rootDir: resolvePreferredOpenClawTmpDir(), |
| 80 | + prefix: "openclaw-openshell-remote-seed-", |
| 81 | + }); |
| 82 | + tempWorkspaces.push(workspace); |
| 83 | + await fs.writeFile(path.join(workspace.dir, "seed.txt"), "seed", "utf8"); |
| 84 | + cliMocks.createOpenShellSshSession.mockResolvedValue({ |
| 85 | + command: "ssh", |
| 86 | + configPath: "/tmp/openclaw-openshell-test-ssh-config", |
| 87 | + host: "openshell-test", |
| 88 | + }); |
| 89 | + // `sandbox get` succeeds: the sandbox was created by a previous gateway |
| 90 | + // process that died before the first exec could run the one-time seed. |
| 91 | + cliMocks.runOpenShellCli.mockResolvedValue({ code: 0, stdout: "", stderr: "" }); |
| 92 | + sdkMocks.runSshSandboxCommand.mockImplementation(async ({ remoteCommand }) => ({ |
| 93 | + stdout: String(remoteCommand).includes("ls -A") |
| 94 | + ? Buffer.from(params.probeStdout) |
| 95 | + : Buffer.alloc(0), |
| 96 | + stderr: Buffer.alloc(0), |
| 97 | + code: 0, |
| 98 | + })); |
| 99 | + const backendFactory = createOpenShellSandboxBackendFactory({ |
| 100 | + pluginConfig: resolveOpenShellPluginConfig({ |
| 101 | + command: "openshell", |
| 102 | + mode: "remote", |
| 103 | + }), |
| 104 | + }); |
| 105 | + return await backendFactory({ |
| 106 | + sessionKey: "agent:main:turn", |
| 107 | + scopeKey: "agent:main", |
| 108 | + workspaceDir: workspace.dir, |
| 109 | + agentWorkspaceDir: workspace.dir, |
| 110 | + cfg: createOpenShellBackendSandboxConfig(), |
| 111 | + }); |
| 112 | +} |
| 113 | + |
| 114 | +function seedUploadCalls() { |
| 115 | + return cliMocks.runOpenShellCli.mock.calls.filter( |
| 116 | + ([params]) => params.args[0] === "sandbox" && params.args[1] === "upload", |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +describe("openshell remote-mode seed across gateway restart", () => { |
| 121 | + beforeEach(() => { |
| 122 | + vi.clearAllMocks(); |
| 123 | + }); |
| 124 | + |
| 125 | + afterEach(async () => { |
| 126 | + await Promise.all(tempWorkspaces.splice(0).map((workspace) => workspace.cleanup())); |
| 127 | + }); |
| 128 | + |
| 129 | + it("seeds an adopted sandbox whose managed roots are empty", async () => { |
| 130 | + const backend = await createAdoptedRemoteBackend({ probeStdout: "0\n" }); |
| 131 | + |
| 132 | + await backend.buildExecSpec({ command: "pwd", env: {}, usePty: false }); |
| 133 | + |
| 134 | + const uploads = seedUploadCalls(); |
| 135 | + expect(uploads.length).toBeGreaterThan(0); |
| 136 | + expect(uploads[0]?.[0]).toMatchObject({ |
| 137 | + args: expect.arrayContaining([expect.stringMatching(/\/seed\.txt$/), "/sandbox/"]), |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it("never re-seeds when a managed root already holds content", async () => { |
| 142 | + const backend = await createAdoptedRemoteBackend({ probeStdout: "1\n" }); |
| 143 | + |
| 144 | + await backend.buildExecSpec({ command: "pwd", env: {}, usePty: false }); |
| 145 | + |
| 146 | + expect(seedUploadCalls()).toHaveLength(0); |
| 147 | + const wipeCalls = sdkMocks.runSshSandboxCommand.mock.calls.filter(([params]) => |
| 148 | + String(params.remoteCommand).includes("rm -rf"), |
| 149 | + ); |
| 150 | + expect(wipeCalls).toHaveLength(0); |
| 151 | + }); |
| 152 | +}); |
0 commit comments